blob: 321d8be80871ce2fc4a5ab39c3189dad2370c988 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Shannon Nelson4fc8c672017-06-07 05:43:08 -04004 * Copyright(c) 2013 - 2017 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
Björn Töpel0c8493d2017-05-24 07:55:34 +020030#include <linux/bpf.h>
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000032/* Local includes */
33#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000034#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070035#include <net/udp_tunnel.h>
Scott Petersoned0980c2017-04-13 04:45:44 -040036/* All i40e tracepoints are defined by the include below, which
37 * must be included exactly once across the whole kernel with
38 * CREATE_TRACE_POINTS defined
39 */
40#define CREATE_TRACE_POINTS
41#include "i40e_trace.h"
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000042
43const char i40e_driver_name[] = "i40e";
44static const char i40e_driver_string[] =
45 "Intel(R) Ethernet Connection XL710 Network Driver";
46
47#define DRV_KERN "-k"
48
Bimmy Pujari15990832017-01-30 12:29:37 -080049#define DRV_VERSION_MAJOR 2
50#define DRV_VERSION_MINOR 1
Jacob Keller2318b402017-04-19 09:25:53 -040051#define DRV_VERSION_BUILD 14
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000052#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
53 __stringify(DRV_VERSION_MINOR) "." \
54 __stringify(DRV_VERSION_BUILD) DRV_KERN
55const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080056static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057
58/* a bit of forward declarations */
59static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
Maciej Sosin373149f2017-04-05 07:50:55 -040060static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000061static int i40e_add_vsi(struct i40e_vsi *vsi);
62static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000063static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000064static int i40e_setup_misc_vector(struct i40e_pf *pf);
65static void i40e_determine_queue_usage(struct i40e_pf *pf);
66static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Maciej Sosin373149f2017-04-05 07:50:55 -040067static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
68static int i40e_reset(struct i40e_pf *pf);
69static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080070static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080071static int i40e_veb_get_bw_info(struct i40e_veb *veb);
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 Keller278e7d02016-10-05 09:30:37 -07001576 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001577 i40e_del_mac_filter(vsi, netdev->dev_addr);
1578 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001579 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001580 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001581 if (vsi->type == I40E_VSI_MAIN) {
1582 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001583
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001584 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001585 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001586 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001587 if (ret)
1588 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1589 i40e_stat_str(hw, ret),
1590 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001591 }
1592
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001593 /* schedule our worker thread which will take care of
1594 * applying the new filter changes
1595 */
1596 i40e_service_event_schedule(vsi->back);
1597 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001598}
1599
1600/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07001601 * i40e_config_rss_aq - Prepare for RSS using AQ commands
1602 * @vsi: vsi structure
1603 * @seed: RSS hash seed
1604 **/
1605static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1606 u8 *lut, u16 lut_size)
1607{
1608 struct i40e_pf *pf = vsi->back;
1609 struct i40e_hw *hw = &pf->hw;
1610 int ret = 0;
1611
1612 if (seed) {
1613 struct i40e_aqc_get_set_rss_key_data *seed_dw =
1614 (struct i40e_aqc_get_set_rss_key_data *)seed;
1615 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1616 if (ret) {
1617 dev_info(&pf->pdev->dev,
1618 "Cannot set RSS key, err %s aq_err %s\n",
1619 i40e_stat_str(hw, ret),
1620 i40e_aq_str(hw, hw->aq.asq_last_status));
1621 return ret;
1622 }
1623 }
1624 if (lut) {
1625 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
1626
1627 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1628 if (ret) {
1629 dev_info(&pf->pdev->dev,
1630 "Cannot set RSS lut, err %s aq_err %s\n",
1631 i40e_stat_str(hw, ret),
1632 i40e_aq_str(hw, hw->aq.asq_last_status));
1633 return ret;
1634 }
1635 }
1636 return ret;
1637}
1638
1639/**
1640 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1641 * @vsi: VSI structure
1642 **/
1643static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1644{
1645 struct i40e_pf *pf = vsi->back;
1646 u8 seed[I40E_HKEY_ARRAY_SIZE];
1647 u8 *lut;
1648 int ret;
1649
1650 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1651 return 0;
1652 if (!vsi->rss_size)
1653 vsi->rss_size = min_t(int, pf->alloc_rss_size,
1654 vsi->num_queue_pairs);
1655 if (!vsi->rss_size)
1656 return -EINVAL;
1657 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1658 if (!lut)
1659 return -ENOMEM;
1660
1661 /* Use the user configured hash keys and lookup table if there is one,
1662 * otherwise use default
1663 */
1664 if (vsi->rss_lut_user)
1665 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1666 else
1667 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1668 if (vsi->rss_hkey_user)
1669 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1670 else
1671 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1672 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1673 kfree(lut);
1674 return ret;
1675}
1676
1677/**
1678 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1679 * @vsi: the VSI being configured,
1680 * @ctxt: VSI context structure
1681 * @enabled_tc: number of traffic classes to enable
1682 *
1683 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1684 **/
1685static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1686 struct i40e_vsi_context *ctxt,
1687 u8 enabled_tc)
1688{
1689 u16 qcount = 0, max_qcount, qmap, sections = 0;
1690 int i, override_q, pow, num_qps, ret;
1691 u8 netdev_tc = 0, offset = 0;
1692
1693 if (vsi->type != I40E_VSI_MAIN)
1694 return -EINVAL;
1695 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1696 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1697 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1698 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1699 num_qps = vsi->mqprio_qopt.qopt.count[0];
1700
1701 /* find the next higher power-of-2 of num queue pairs */
1702 pow = ilog2(num_qps);
1703 if (!is_power_of_2(num_qps))
1704 pow++;
1705 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1706 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1707
1708 /* Setup queue offset/count for all TCs for given VSI */
1709 max_qcount = vsi->mqprio_qopt.qopt.count[0];
1710 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1711 /* See if the given TC is enabled for the given VSI */
1712 if (vsi->tc_config.enabled_tc & BIT(i)) {
1713 offset = vsi->mqprio_qopt.qopt.offset[i];
1714 qcount = vsi->mqprio_qopt.qopt.count[i];
1715 if (qcount > max_qcount)
1716 max_qcount = qcount;
1717 vsi->tc_config.tc_info[i].qoffset = offset;
1718 vsi->tc_config.tc_info[i].qcount = qcount;
1719 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1720 } else {
1721 /* TC is not enabled so set the offset to
1722 * default queue and allocate one queue
1723 * for the given TC.
1724 */
1725 vsi->tc_config.tc_info[i].qoffset = 0;
1726 vsi->tc_config.tc_info[i].qcount = 1;
1727 vsi->tc_config.tc_info[i].netdev_tc = 0;
1728 }
1729 }
1730
1731 /* Set actual Tx/Rx queue pairs */
1732 vsi->num_queue_pairs = offset + qcount;
1733
1734 /* Setup queue TC[0].qmap for given VSI context */
1735 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1736 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1737 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1738 ctxt->info.valid_sections |= cpu_to_le16(sections);
1739
1740 /* Reconfigure RSS for main VSI with max queue count */
1741 vsi->rss_size = max_qcount;
1742 ret = i40e_vsi_config_rss(vsi);
1743 if (ret) {
1744 dev_info(&vsi->back->pdev->dev,
1745 "Failed to reconfig rss for num_queues (%u)\n",
1746 max_qcount);
1747 return ret;
1748 }
1749 vsi->reconfig_rss = true;
1750 dev_dbg(&vsi->back->pdev->dev,
1751 "Reconfigured rss with num_queues (%u)\n", max_qcount);
1752
1753 /* Find queue count available for channel VSIs and starting offset
1754 * for channel VSIs
1755 */
1756 override_q = vsi->mqprio_qopt.qopt.count[0];
1757 if (override_q && override_q < vsi->num_queue_pairs) {
1758 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1759 vsi->next_base_queue = override_q;
1760 }
1761 return 0;
1762}
1763
1764/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001765 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1766 * @vsi: the VSI being setup
1767 * @ctxt: VSI context structure
1768 * @enabled_tc: Enabled TCs bitmap
1769 * @is_add: True if called before Add VSI
1770 *
1771 * Setup VSI queue mapping for enabled traffic classes.
1772 **/
1773static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1774 struct i40e_vsi_context *ctxt,
1775 u8 enabled_tc,
1776 bool is_add)
1777{
1778 struct i40e_pf *pf = vsi->back;
1779 u16 sections = 0;
1780 u8 netdev_tc = 0;
1781 u16 numtc = 0;
1782 u16 qcount;
1783 u8 offset;
1784 u16 qmap;
1785 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001786 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001787
1788 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1789 offset = 0;
1790
1791 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1792 /* Find numtc from enabled TC bitmap */
1793 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001794 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001795 numtc++;
1796 }
1797 if (!numtc) {
1798 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1799 numtc = 1;
1800 }
1801 } else {
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07001802 /* At least TC0 is enabled in non-DCB, non-MQPRIO case */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001803 numtc = 1;
1804 }
1805
1806 vsi->tc_config.numtc = numtc;
1807 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001808 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001809 qcount = vsi->alloc_queue_pairs;
1810
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001811 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001812 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001813
1814 /* Setup queue offset/count for all TCs for given VSI */
1815 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1816 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001817 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001818 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001819 int pow, num_qps;
1820
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001821 switch (vsi->type) {
1822 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001823 qcount = min_t(int, pf->alloc_rss_size,
1824 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001825 break;
1826 case I40E_VSI_FDIR:
1827 case I40E_VSI_SRIOV:
1828 case I40E_VSI_VMDQ2:
1829 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001830 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001831 WARN_ON(i != 0);
1832 break;
1833 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001834 vsi->tc_config.tc_info[i].qoffset = offset;
1835 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001836
Shannon Nelson1e200e42015-02-27 09:15:24 +00001837 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001838 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001839 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001840 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001841 pow++;
1842 num_qps >>= 1;
1843 }
1844
1845 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1846 qmap =
1847 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1848 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1849
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001850 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001851 } else {
1852 /* TC is not enabled so set the offset to
1853 * default queue and allocate one queue
1854 * for the given TC.
1855 */
1856 vsi->tc_config.tc_info[i].qoffset = 0;
1857 vsi->tc_config.tc_info[i].qcount = 1;
1858 vsi->tc_config.tc_info[i].netdev_tc = 0;
1859
1860 qmap = 0;
1861 }
1862 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1863 }
1864
1865 /* Set actual Tx/Rx queue pairs */
1866 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001867 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1868 if (vsi->req_queue_pairs > 0)
1869 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001870 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001871 vsi->num_queue_pairs = pf->num_lan_msix;
1872 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001873
1874 /* Scheduler section valid can only be set for ADD VSI */
1875 if (is_add) {
1876 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1877
1878 ctxt->info.up_enable_bits = enabled_tc;
1879 }
1880 if (vsi->type == I40E_VSI_SRIOV) {
1881 ctxt->info.mapping_flags |=
1882 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1883 for (i = 0; i < vsi->num_queue_pairs; i++)
1884 ctxt->info.queue_mapping[i] =
1885 cpu_to_le16(vsi->base_queue + i);
1886 } else {
1887 ctxt->info.mapping_flags |=
1888 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1889 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1890 }
1891 ctxt->info.valid_sections |= cpu_to_le16(sections);
1892}
1893
1894/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001895 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1896 * @netdev: the netdevice
1897 * @addr: address to add
1898 *
1899 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1900 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1901 */
1902static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1903{
1904 struct i40e_netdev_priv *np = netdev_priv(netdev);
1905 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001906
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001907 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001908 return 0;
1909 else
1910 return -ENOMEM;
1911}
1912
1913/**
1914 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1915 * @netdev: the netdevice
1916 * @addr: address to add
1917 *
1918 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1919 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1920 */
1921static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1922{
1923 struct i40e_netdev_priv *np = netdev_priv(netdev);
1924 struct i40e_vsi *vsi = np->vsi;
1925
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001926 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001927
1928 return 0;
1929}
1930
1931/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001932 * i40e_set_rx_mode - NDO callback to set the netdev filters
1933 * @netdev: network interface device structure
1934 **/
1935static void i40e_set_rx_mode(struct net_device *netdev)
1936{
1937 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001938 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001939
Jacob Keller278e7d02016-10-05 09:30:37 -07001940 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001941
Jacob Keller6622f5c2016-10-05 09:30:32 -07001942 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1943 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001944
Jacob Keller278e7d02016-10-05 09:30:37 -07001945 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001946
1947 /* check for other flag changes */
1948 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1949 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1950 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1951 }
1952}
1953
1954/**
Jacob Keller671889e2016-12-02 12:33:00 -08001955 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001956 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001957 * @from: Pointer to list which contains MAC filter entries - changes to
1958 * those entries needs to be undone.
1959 *
Jacob Keller671889e2016-12-02 12:33:00 -08001960 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001961 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001962static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1963 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001964{
Jacob Keller278e7d02016-10-05 09:30:37 -07001965 struct i40e_mac_filter *f;
1966 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001967
Jacob Keller278e7d02016-10-05 09:30:37 -07001968 hlist_for_each_entry_safe(f, h, from, hlist) {
1969 u64 key = i40e_addr_to_hkey(f->macaddr);
1970
Kiran Patil21659032015-09-30 14:09:03 -04001971 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001972 hlist_del(&f->hlist);
1973 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001974 }
1975}
1976
1977/**
Jacob Keller671889e2016-12-02 12:33:00 -08001978 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1979 * @vsi: Pointer to vsi struct
1980 * @from: Pointer to list which contains MAC filter entries - changes to
1981 * those entries needs to be undone.
1982 *
1983 * MAC filter entries from this list were slated for addition.
1984 **/
1985static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1986 struct hlist_head *from)
1987{
1988 struct i40e_new_mac_filter *new;
1989 struct hlist_node *h;
1990
1991 hlist_for_each_entry_safe(new, h, from, hlist) {
1992 /* We can simply free the wrapper structure */
1993 hlist_del(&new->hlist);
1994 kfree(new);
1995 }
1996}
1997
1998/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001999 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08002000 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002001 *
2002 * Returns the next non-broadcast filter in the list. Required so that we
2003 * ignore broadcast filters within the list, since these are not handled via
2004 * the normal firmware update path.
2005 */
Jacob Keller671889e2016-12-02 12:33:00 -08002006static
2007struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002008{
Jacob Keller584a8872017-03-07 15:17:52 -08002009 hlist_for_each_entry_continue(next, hlist) {
2010 if (!is_broadcast_ether_addr(next->f->macaddr))
2011 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002012 }
2013
Jacob Keller584a8872017-03-07 15:17:52 -08002014 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002015}
2016
2017/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002018 * i40e_update_filter_state - Update filter state based on return data
2019 * from firmware
2020 * @count: Number of filters added
2021 * @add_list: return data from fw
2022 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04002023 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002024 * MAC filter entries from list were slated to be added to device. Returns
2025 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04002026 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002027static int
2028i40e_update_filter_state(int count,
2029 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08002030 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04002031{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002032 int retval = 0;
2033 int i;
Kiran Patil21659032015-09-30 14:09:03 -04002034
Jacob Kellerac9e2392016-11-11 12:39:27 -08002035 for (i = 0; i < count; i++) {
2036 /* Always check status of each filter. We don't need to check
2037 * the firmware return status because we pre-set the filter
2038 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2039 * request to the adminq. Thus, if it no longer matches then
2040 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002041 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08002042 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002043 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002044 } else {
2045 add_head->state = I40E_FILTER_ACTIVE;
2046 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002047 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08002048
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002049 add_head = i40e_next_filter(add_head);
2050 if (!add_head)
2051 break;
Kiran Patil21659032015-09-30 14:09:03 -04002052 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08002053
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002054 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04002055}
2056
2057/**
Jacob Keller00936312016-10-05 09:30:41 -07002058 * i40e_aqc_del_filters - Request firmware to delete a set of filters
2059 * @vsi: ptr to the VSI
2060 * @vsi_name: name to display in messages
2061 * @list: the list of filters to send to firmware
2062 * @num_del: the number of filters to delete
2063 * @retval: Set to -EIO on failure to delete
2064 *
2065 * Send a request to firmware via AdminQ to delete a set of filters. Uses
2066 * *retval instead of a return value so that success does not force ret_val to
2067 * be set to 0. This ensures that a sequence of calls to this function
2068 * preserve the previous value of *retval on successful delete.
2069 */
2070static
2071void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2072 struct i40e_aqc_remove_macvlan_element_data *list,
2073 int num_del, int *retval)
2074{
2075 struct i40e_hw *hw = &vsi->back->hw;
2076 i40e_status aq_ret;
2077 int aq_err;
2078
2079 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2080 aq_err = hw->aq.asq_last_status;
2081
2082 /* Explicitly ignore and do not report when firmware returns ENOENT */
2083 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2084 *retval = -EIO;
2085 dev_info(&vsi->back->pdev->dev,
2086 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2087 vsi_name, i40e_stat_str(hw, aq_ret),
2088 i40e_aq_str(hw, aq_err));
2089 }
2090}
2091
2092/**
2093 * i40e_aqc_add_filters - Request firmware to add a set of filters
2094 * @vsi: ptr to the VSI
2095 * @vsi_name: name to display in messages
2096 * @list: the list of filters to send to firmware
2097 * @add_head: Position in the add hlist
2098 * @num_add: the number of filters to add
2099 * @promisc_change: set to true on exit if promiscuous mode was forced on
2100 *
2101 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2102 * promisc_changed to true if the firmware has run out of space for more
2103 * filters.
2104 */
2105static
2106void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2107 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08002108 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07002109 int num_add, bool *promisc_changed)
2110{
2111 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07002112 int aq_err, fcnt;
2113
Jacob Kellerac9e2392016-11-11 12:39:27 -08002114 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07002115 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002116 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07002117
2118 if (fcnt != num_add) {
2119 *promisc_changed = true;
Jacob Keller0da36b92017-04-19 09:25:55 -04002120 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07002121 dev_warn(&vsi->back->pdev->dev,
2122 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2123 i40e_aq_str(hw, aq_err),
2124 vsi_name);
2125 }
2126}
2127
2128/**
Jacob Keller435c0842016-11-08 13:05:10 -08002129 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2130 * @vsi: pointer to the VSI
2131 * @f: filter data
2132 *
2133 * This function sets or clears the promiscuous broadcast flags for VLAN
2134 * filters in order to properly receive broadcast frames. Assumes that only
2135 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08002136 *
2137 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08002138 **/
Jacob Keller671889e2016-12-02 12:33:00 -08002139static i40e_status
2140i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2141 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08002142{
2143 bool enable = f->state == I40E_FILTER_NEW;
2144 struct i40e_hw *hw = &vsi->back->hw;
2145 i40e_status aq_ret;
2146
2147 if (f->vlan == I40E_VLAN_ANY) {
2148 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2149 vsi->seid,
2150 enable,
2151 NULL);
2152 } else {
2153 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2154 vsi->seid,
2155 enable,
2156 f->vlan,
2157 NULL);
2158 }
2159
Jacob Keller671889e2016-12-02 12:33:00 -08002160 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08002161 dev_warn(&vsi->back->pdev->dev,
2162 "Error %s setting broadcast promiscuous mode on %s\n",
2163 i40e_aq_str(hw, hw->aq.asq_last_status),
2164 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08002165
2166 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08002167}
2168
2169/**
Alan Bradybd5608b2017-11-14 07:00:52 -05002170 * i40e_set_promiscuous - set promiscuous mode
2171 * @pf: board private structure
2172 * @promisc: promisc on or off
2173 *
2174 * There are different ways of setting promiscuous mode on a PF depending on
2175 * what state/environment we're in. This identifies and sets it appropriately.
2176 * Returns 0 on success.
2177 **/
2178static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2179{
2180 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2181 struct i40e_hw *hw = &pf->hw;
2182 i40e_status aq_ret;
2183
2184 if (vsi->type == I40E_VSI_MAIN &&
2185 pf->lan_veb != I40E_NO_VEB &&
2186 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2187 /* set defport ON for Main VSI instead of true promisc
2188 * this way we will get all unicast/multicast and VLAN
2189 * promisc behavior but will not get VF or VMDq traffic
2190 * replicated on the Main VSI.
2191 */
2192 if (promisc)
2193 aq_ret = i40e_aq_set_default_vsi(hw,
2194 vsi->seid,
2195 NULL);
2196 else
2197 aq_ret = i40e_aq_clear_default_vsi(hw,
2198 vsi->seid,
2199 NULL);
2200 if (aq_ret) {
2201 dev_info(&pf->pdev->dev,
2202 "Set default VSI failed, err %s, aq_err %s\n",
2203 i40e_stat_str(hw, aq_ret),
2204 i40e_aq_str(hw, hw->aq.asq_last_status));
2205 }
2206 } else {
2207 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2208 hw,
2209 vsi->seid,
2210 promisc, NULL,
2211 true);
2212 if (aq_ret) {
2213 dev_info(&pf->pdev->dev,
2214 "set unicast promisc failed, err %s, aq_err %s\n",
2215 i40e_stat_str(hw, aq_ret),
2216 i40e_aq_str(hw, hw->aq.asq_last_status));
2217 }
2218 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2219 hw,
2220 vsi->seid,
2221 promisc, NULL);
2222 if (aq_ret) {
2223 dev_info(&pf->pdev->dev,
2224 "set multicast promisc failed, err %s, aq_err %s\n",
2225 i40e_stat_str(hw, aq_ret),
2226 i40e_aq_str(hw, hw->aq.asq_last_status));
2227 }
2228 }
2229
2230 if (!aq_ret)
2231 pf->cur_promisc = promisc;
2232
2233 return aq_ret;
2234}
2235
2236/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002237 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2238 * @vsi: ptr to the VSI
2239 *
2240 * Push any outstanding VSI filter changes through the AdminQ.
2241 *
2242 * Returns 0 or error value
2243 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002244int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002245{
Jacob Keller278e7d02016-10-05 09:30:37 -07002246 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002247 struct i40e_mac_filter *f;
2248 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002249 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002250 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002251 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002252 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002253 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002254 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002255 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002256 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002257 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002258 struct i40e_pf *pf;
2259 int num_add = 0;
2260 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002261 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002262 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002263 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002264 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002265
2266 /* empty array typed pointers, kcalloc later */
2267 struct i40e_aqc_add_macvlan_element_data *add_list;
2268 struct i40e_aqc_remove_macvlan_element_data *del_list;
2269
Jacob Keller0da36b92017-04-19 09:25:55 -04002270 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002271 usleep_range(1000, 2000);
2272 pf = vsi->back;
2273
2274 if (vsi->netdev) {
2275 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2276 vsi->current_netdev_flags = vsi->netdev->flags;
2277 }
2278
Jacob Keller278e7d02016-10-05 09:30:37 -07002279 INIT_HLIST_HEAD(&tmp_add_list);
2280 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002281
Shannon Nelson2d1de822016-05-16 10:26:44 -07002282 if (vsi->type == I40E_VSI_SRIOV)
2283 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2284 else if (vsi->type != I40E_VSI_MAIN)
2285 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2286
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002287 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2288 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2289
Jacob Keller278e7d02016-10-05 09:30:37 -07002290 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002291 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002292 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002293 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002294 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002295 hash_del(&f->hlist);
2296 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002297
2298 /* Avoid counting removed filters */
2299 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002300 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002301 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002302 /* Create a temporary i40e_new_mac_filter */
2303 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2304 if (!new)
2305 goto err_no_memory_locked;
2306
2307 /* Store pointer to the real filter */
2308 new->f = f;
2309 new->state = f->state;
2310
2311 /* Add it to the hash list */
2312 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002313 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002314
Jacob Keller489a3262016-11-11 12:39:31 -08002315 /* Count the number of active (current and new) VLAN
2316 * filters we have now. Does not count filters which
2317 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002318 */
2319 if (f->vlan > 0)
2320 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002321 }
2322
Jacob Keller489a3262016-11-11 12:39:31 -08002323 retval = i40e_correct_mac_vlan_filters(vsi,
2324 &tmp_add_list,
2325 &tmp_del_list,
2326 vlan_filters);
2327 if (retval)
2328 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002329
Jacob Keller278e7d02016-10-05 09:30:37 -07002330 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002331 }
2332
2333 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002334 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002335 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002336 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002337 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002338 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002339 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002340 if (!del_list)
2341 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002342
Jacob Keller278e7d02016-10-05 09:30:37 -07002343 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002344 cmd_flags = 0;
2345
Jacob Keller435c0842016-11-08 13:05:10 -08002346 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002347 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002348 */
2349 if (is_broadcast_ether_addr(f->macaddr)) {
2350 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2351
2352 hlist_del(&f->hlist);
2353 kfree(f);
2354 continue;
2355 }
2356
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002357 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002358 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002359 if (f->vlan == I40E_VLAN_ANY) {
2360 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002361 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002362 } else {
2363 del_list[num_del].vlan_tag =
2364 cpu_to_le16((u16)(f->vlan));
2365 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002366
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002367 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2368 del_list[num_del].flags = cmd_flags;
2369 num_del++;
2370
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002371 /* flush a full buffer */
2372 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002373 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2374 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002375 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002376 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002377 }
Kiran Patil21659032015-09-30 14:09:03 -04002378 /* Release memory for MAC filter entries which were
2379 * synced up with HW.
2380 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002381 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002382 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002383 }
Kiran Patil21659032015-09-30 14:09:03 -04002384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002385 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002386 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2387 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002388 }
2389
2390 kfree(del_list);
2391 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002392 }
2393
Jacob Keller278e7d02016-10-05 09:30:37 -07002394 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002395 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002396 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002397 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002398 list_size = filter_list_len *
2399 sizeof(struct i40e_aqc_add_macvlan_element_data);
2400 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002401 if (!add_list)
2402 goto err_no_memory;
2403
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002404 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002405 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Jacob Kellerd19cb642017-04-21 13:38:05 -07002406 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002407 vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002408 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002409 continue;
2410 }
Jacob Keller435c0842016-11-08 13:05:10 -08002411
2412 /* handle broadcast filters by updating the broadcast
2413 * promiscuous flag instead of adding a MAC filter.
2414 */
Jacob Keller671889e2016-12-02 12:33:00 -08002415 if (is_broadcast_ether_addr(new->f->macaddr)) {
2416 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2417 new->f))
2418 new->state = I40E_FILTER_FAILED;
2419 else
2420 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002421 continue;
2422 }
2423
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002424 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002425 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002426 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002427 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002428 ether_addr_copy(add_list[num_add].mac_addr,
2429 new->f->macaddr);
2430 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002431 add_list[num_add].vlan_tag = 0;
2432 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2433 } else {
2434 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002435 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002436 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002438 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002439 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002440 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002441 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2442 num_add++;
2443
2444 /* flush a full buffer */
2445 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002446 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2447 add_head, num_add,
2448 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002449 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002450 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451 }
2452 }
2453 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002454 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2455 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002456 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002457 /* Now move all of the filters from the temp add list back to
2458 * the VSI's list.
2459 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002460 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002461 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2462 /* Only update the state if we're still NEW */
2463 if (new->f->state == I40E_FILTER_NEW)
2464 new->f->state = new->state;
2465 hlist_del(&new->hlist);
2466 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002467 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002468 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002469 kfree(add_list);
2470 add_list = NULL;
2471 }
2472
Jacob Keller38326212016-11-11 12:39:26 -08002473 /* Determine the number of active and failed filters. */
2474 spin_lock_bh(&vsi->mac_filter_hash_lock);
2475 vsi->active_filters = 0;
2476 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2477 if (f->state == I40E_FILTER_ACTIVE)
2478 vsi->active_filters++;
2479 else if (f->state == I40E_FILTER_FAILED)
2480 failed_filters++;
2481 }
2482 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2483
2484 /* If promiscuous mode has changed, we need to calculate a new
2485 * threshold for when we are safe to exit
2486 */
2487 if (promisc_changed)
2488 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2489
2490 /* Check if we are able to exit overflow promiscuous mode. We can
2491 * safely exit if we didn't just enter, we no longer have any failed
2492 * filters, and we have reduced filters below the threshold value.
2493 */
Jacob Keller0da36b92017-04-19 09:25:55 -04002494 if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002495 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002496 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002497 dev_info(&pf->pdev->dev,
2498 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2499 vsi_name);
Jacob Keller0da36b92017-04-19 09:25:55 -04002500 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller38326212016-11-11 12:39:26 -08002501 promisc_changed = true;
2502 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002503 }
2504
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002505 /* if the VF is not trusted do not do promisc */
2506 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
Jacob Keller0da36b92017-04-19 09:25:55 -04002507 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002508 goto out;
2509 }
2510
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002511 /* check for changes in promiscuous modes */
2512 if (changed_flags & IFF_ALLMULTI) {
2513 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002515 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002516 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2517 vsi->seid,
2518 cur_multipromisc,
2519 NULL);
2520 if (aq_ret) {
2521 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002522 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002523 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002524 "set multi promisc failed on %s, err %s aq_err %s\n",
2525 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002526 i40e_stat_str(hw, aq_ret),
2527 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002528 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 }
Alan Bradye5887232017-06-07 05:43:10 -04002530
2531 if ((changed_flags & IFF_PROMISC) || promisc_changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002533
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002534 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
Jacob Kellerd19cb642017-04-21 13:38:05 -07002535 test_bit(__I40E_VSI_OVERFLOW_PROMISC,
Jacob Keller0da36b92017-04-19 09:25:55 -04002536 vsi->state));
Alan Bradybd5608b2017-11-14 07:00:52 -05002537 aq_ret = i40e_set_promiscuous(pf, cur_promisc);
Mitch Williamsea02e902015-11-09 15:35:50 -08002538 if (aq_ret) {
2539 retval = i40e_aq_rc_to_posix(aq_ret,
Alan Bradybd5608b2017-11-14 07:00:52 -05002540 hw->aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002541 dev_info(&pf->pdev->dev,
Alan Bradybd5608b2017-11-14 07:00:52 -05002542 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2543 cur_promisc ? "on" : "off",
2544 vsi_name,
2545 i40e_stat_str(hw, aq_ret),
2546 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002547 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002548 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002549out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002550 /* if something went wrong then set the changed flag so we try again */
2551 if (retval)
2552 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2553
Jacob Keller0da36b92017-04-19 09:25:55 -04002554 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002555 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002556
2557err_no_memory:
2558 /* Restore elements on the temporary add and delete lists */
2559 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002560err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002561 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2562 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002563 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2564
2565 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
Jacob Keller0da36b92017-04-19 09:25:55 -04002566 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002567 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568}
2569
2570/**
2571 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2572 * @pf: board private structure
2573 **/
2574static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2575{
2576 int v;
2577
2578 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2579 return;
2580 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2581
Mitch Williams505682c2014-05-20 08:01:37 +00002582 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002583 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002584 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2585 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2586
2587 if (ret) {
2588 /* come back and try again later */
2589 pf->flags |= I40E_FLAG_FILTER_SYNC;
2590 break;
2591 }
2592 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593 }
2594}
2595
2596/**
Björn Töpel0c8493d2017-05-24 07:55:34 +02002597 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2598 * @vsi: the vsi
2599 **/
2600static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2601{
2602 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2603 return I40E_RXBUFFER_2048;
2604 else
2605 return I40E_RXBUFFER_3072;
2606}
2607
2608/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2610 * @netdev: network interface device structure
2611 * @new_mtu: new value for maximum frame size
2612 *
2613 * Returns 0 on success, negative on failure
2614 **/
2615static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2616{
2617 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002618 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002619 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002620
Björn Töpel0c8493d2017-05-24 07:55:34 +02002621 if (i40e_enabled_xdp_vsi(vsi)) {
2622 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2623
2624 if (frame_size > i40e_max_xdp_frame_size(vsi))
2625 return -EINVAL;
2626 }
2627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002628 netdev_info(netdev, "changing MTU from %d to %d\n",
2629 netdev->mtu, new_mtu);
2630 netdev->mtu = new_mtu;
2631 if (netif_running(netdev))
2632 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002633 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2634 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635 return 0;
2636}
2637
2638/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002639 * i40e_ioctl - Access the hwtstamp interface
2640 * @netdev: network interface device structure
2641 * @ifr: interface request data
2642 * @cmd: ioctl command
2643 **/
2644int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2645{
2646 struct i40e_netdev_priv *np = netdev_priv(netdev);
2647 struct i40e_pf *pf = np->vsi->back;
2648
2649 switch (cmd) {
2650 case SIOCGHWTSTAMP:
2651 return i40e_ptp_get_ts_config(pf, ifr);
2652 case SIOCSHWTSTAMP:
2653 return i40e_ptp_set_ts_config(pf, ifr);
2654 default:
2655 return -EOPNOTSUPP;
2656 }
2657}
2658
2659/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2661 * @vsi: the vsi being adjusted
2662 **/
2663void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2664{
2665 struct i40e_vsi_context ctxt;
2666 i40e_status ret;
2667
2668 if ((vsi->info.valid_sections &
2669 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2670 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2671 return; /* already enabled */
2672
2673 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2674 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2675 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2676
2677 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002678 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002679 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2680 if (ret) {
2681 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002682 "update vlan stripping failed, err %s aq_err %s\n",
2683 i40e_stat_str(&vsi->back->hw, ret),
2684 i40e_aq_str(&vsi->back->hw,
2685 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002686 }
2687}
2688
2689/**
2690 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2691 * @vsi: the vsi being adjusted
2692 **/
2693void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2694{
2695 struct i40e_vsi_context ctxt;
2696 i40e_status ret;
2697
2698 if ((vsi->info.valid_sections &
2699 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2700 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2701 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2702 return; /* already disabled */
2703
2704 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2705 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2706 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2707
2708 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002709 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002710 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2711 if (ret) {
2712 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002713 "update vlan stripping failed, err %s aq_err %s\n",
2714 i40e_stat_str(&vsi->back->hw, ret),
2715 i40e_aq_str(&vsi->back->hw,
2716 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002717 }
2718}
2719
2720/**
2721 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2722 * @netdev: network interface to be adjusted
2723 * @features: netdev features to test if VLAN offload is enabled or not
2724 **/
2725static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2726{
2727 struct i40e_netdev_priv *np = netdev_priv(netdev);
2728 struct i40e_vsi *vsi = np->vsi;
2729
2730 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2731 i40e_vlan_stripping_enable(vsi);
2732 else
2733 i40e_vlan_stripping_disable(vsi);
2734}
2735
2736/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002737 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738 * @vsi: the vsi being configured
2739 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002740 *
2741 * This is a helper function for adding a new MAC/VLAN filter with the
2742 * specified VLAN for each existing MAC address already in the hash table.
2743 * This function does *not* perform any accounting to update filters based on
2744 * VLAN mode.
2745 *
2746 * NOTE: this function expects to be called while under the
2747 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002748 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002749int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002750{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002751 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002752 struct hlist_node *h;
2753 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002754
Jacob Keller278e7d02016-10-05 09:30:37 -07002755 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002756 if (f->state == I40E_FILTER_REMOVE)
2757 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002758 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002759 if (!add_f) {
2760 dev_info(&vsi->back->pdev->dev,
2761 "Could not add vlan filter %d for %pM\n",
2762 vid, f->macaddr);
2763 return -ENOMEM;
2764 }
2765 }
2766
Jacob Keller490a4ad2016-11-11 12:39:29 -08002767 return 0;
2768}
2769
2770/**
2771 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2772 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002773 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002774 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002775int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002776{
Jacob Keller489a3262016-11-11 12:39:31 -08002777 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002778
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002779 if (vsi->info.pvid)
Jacob Kellerf94484b2016-12-07 14:05:34 -08002780 return -EINVAL;
2781
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002782 /* The network stack will attempt to add VID=0, with the intention to
2783 * receive priority tagged packets with a VLAN of 0. Our HW receives
2784 * these packets by default when configured to receive untagged
2785 * packets, so we don't need to add a filter for this case.
2786 * Additionally, HW interprets adding a VID=0 filter as meaning to
2787 * receive *only* tagged traffic and stops receiving untagged traffic.
2788 * Thus, we do not want to actually add a filter for VID=0
2789 */
2790 if (!vid)
2791 return 0;
2792
Jacob Keller490a4ad2016-11-11 12:39:29 -08002793 /* Locked once because all functions invoked below iterates list*/
2794 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002795 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002796 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002797 if (err)
2798 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002799
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002800 /* schedule our worker thread which will take care of
2801 * applying the new filter changes
2802 */
2803 i40e_service_event_schedule(vsi->back);
2804 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002805}
2806
2807/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002808 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002809 * @vsi: the vsi being configured
2810 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002811 *
2812 * This function should be used to remove all VLAN filters which match the
2813 * given VID. It does not schedule the service event and does not take the
2814 * mac_filter_hash_lock so it may be combined with other operations under
2815 * a single invocation of the mac_filter_hash_lock.
2816 *
2817 * NOTE: this function expects to be called while under the
2818 * mac_filter_hash_lock
2819 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002820void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002821{
Alan Brady84f5ca62016-10-05 09:30:39 -07002822 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002823 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002824 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002825
Jacob Keller278e7d02016-10-05 09:30:37 -07002826 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002827 if (f->vlan == vid)
2828 __i40e_del_filter(vsi, f);
2829 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002830}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831
Jacob Keller490a4ad2016-11-11 12:39:29 -08002832/**
2833 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2834 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002835 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002836 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002837void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002838{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002839 if (!vid || vsi->info.pvid)
2840 return;
2841
Jacob Keller490a4ad2016-11-11 12:39:29 -08002842 spin_lock_bh(&vsi->mac_filter_hash_lock);
2843 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002844 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002845
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002846 /* schedule our worker thread which will take care of
2847 * applying the new filter changes
2848 */
2849 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002850}
2851
2852/**
2853 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2854 * @netdev: network interface to be adjusted
2855 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002856 *
2857 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002858 **/
2859static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2860 __always_unused __be16 proto, u16 vid)
2861{
2862 struct i40e_netdev_priv *np = netdev_priv(netdev);
2863 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002864 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002865
Jacob Keller6a1127852016-10-25 16:08:49 -07002866 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002867 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868
Jacob Kellerfcf6cfc2017-06-23 04:24:51 -04002869 ret = i40e_vsi_add_vlan(vsi, vid);
Jacob Keller6a1127852016-10-25 16:08:49 -07002870 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002871 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002872
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002873 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002874}
2875
2876/**
2877 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2878 * @netdev: network interface to be adjusted
2879 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002880 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002881 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002882 **/
2883static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2884 __always_unused __be16 proto, u16 vid)
2885{
2886 struct i40e_netdev_priv *np = netdev_priv(netdev);
2887 struct i40e_vsi *vsi = np->vsi;
2888
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002889 /* return code is ignored as there is nothing a user
2890 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002891 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002892 */
2893 i40e_vsi_kill_vlan(vsi, vid);
2894
2895 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002896
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897 return 0;
2898}
2899
2900/**
2901 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2902 * @vsi: the vsi being brought back up
2903 **/
2904static void i40e_restore_vlan(struct i40e_vsi *vsi)
2905{
2906 u16 vid;
2907
2908 if (!vsi->netdev)
2909 return;
2910
2911 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2912
2913 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2914 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2915 vid);
2916}
2917
2918/**
2919 * i40e_vsi_add_pvid - Add pvid for the VSI
2920 * @vsi: the vsi being adjusted
2921 * @vid: the vlan id to set as a PVID
2922 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002923int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002924{
2925 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002926 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002927
2928 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2929 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002930 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2931 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002932 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002933
2934 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002935 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002936 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2937 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002938 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002939 "add pvid failed, err %s aq_err %s\n",
2940 i40e_stat_str(&vsi->back->hw, ret),
2941 i40e_aq_str(&vsi->back->hw,
2942 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002943 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002944 }
2945
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002946 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002947}
2948
2949/**
2950 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2951 * @vsi: the vsi being adjusted
2952 *
2953 * Just use the vlan_rx_register() service to put it back to normal
2954 **/
2955void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2956{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002957 i40e_vlan_stripping_disable(vsi);
2958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002959 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002960}
2961
2962/**
2963 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2964 * @vsi: ptr to the VSI
2965 *
2966 * If this function returns with an error, then it's possible one or
2967 * more of the rings is populated (while the rest are not). It is the
2968 * callers duty to clean those orphaned rings.
2969 *
2970 * Return 0 on success, negative on failure
2971 **/
2972static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2973{
2974 int i, err = 0;
2975
2976 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002977 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002978
Björn Töpel74608d12017-05-24 07:55:35 +02002979 if (!i40e_enabled_xdp_vsi(vsi))
2980 return err;
2981
2982 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2983 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
2984
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002985 return err;
2986}
2987
2988/**
2989 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2990 * @vsi: ptr to the VSI
2991 *
2992 * Free VSI's transmit software resources
2993 **/
2994static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2995{
2996 int i;
2997
Björn Töpel74608d12017-05-24 07:55:35 +02002998 if (vsi->tx_rings) {
2999 for (i = 0; i < vsi->num_queue_pairs; i++)
3000 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3001 i40e_free_tx_resources(vsi->tx_rings[i]);
3002 }
Greg Rose8e9dca52013-12-18 13:45:53 +00003003
Björn Töpel74608d12017-05-24 07:55:35 +02003004 if (vsi->xdp_rings) {
3005 for (i = 0; i < vsi->num_queue_pairs; i++)
3006 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3007 i40e_free_tx_resources(vsi->xdp_rings[i]);
3008 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003009}
3010
3011/**
3012 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3013 * @vsi: ptr to the VSI
3014 *
3015 * If this function returns with an error, then it's possible one or
3016 * more of the rings is populated (while the rest are not). It is the
3017 * callers duty to clean those orphaned rings.
3018 *
3019 * Return 0 on success, negative on failure
3020 **/
3021static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3022{
3023 int i, err = 0;
3024
3025 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003026 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027 return err;
3028}
3029
3030/**
3031 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3032 * @vsi: ptr to the VSI
3033 *
3034 * Free all receive software resources
3035 **/
3036static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3037{
3038 int i;
3039
Greg Rose8e9dca52013-12-18 13:45:53 +00003040 if (!vsi->rx_rings)
3041 return;
3042
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003043 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00003044 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003045 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003046}
3047
3048/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003049 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3050 * @ring: The Tx ring to configure
3051 *
3052 * This enables/disables XPS for a given Tx descriptor ring
3053 * based on the TCs enabled for the VSI that ring belongs to.
3054 **/
3055static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3056{
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003057 int cpu;
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003058
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003059 if (!ring->q_vector || !ring->netdev || ring->ch)
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00003060 return;
3061
Jacob Keller6f853d42017-09-07 08:05:53 -04003062 /* We only initialize XPS once, so as not to overwrite user settings */
3063 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3064 return;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08003065
Jacob Keller6f853d42017-09-07 08:05:53 -04003066 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3067 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3068 ring->queue_index);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003069}
3070
3071/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003072 * i40e_configure_tx_ring - Configure a transmit ring context and rest
3073 * @ring: The Tx ring to configure
3074 *
3075 * Configure the Tx descriptor ring in the HMC context.
3076 **/
3077static int i40e_configure_tx_ring(struct i40e_ring *ring)
3078{
3079 struct i40e_vsi *vsi = ring->vsi;
3080 u16 pf_q = vsi->base_queue + ring->queue_index;
3081 struct i40e_hw *hw = &vsi->back->hw;
3082 struct i40e_hmc_obj_txq tx_ctx;
3083 i40e_status err = 0;
3084 u32 qtx_ctl = 0;
3085
3086 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003087 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003088 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3089 ring->atr_count = 0;
3090 } else {
3091 ring->atr_sample_rate = 0;
3092 }
3093
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003094 /* configure XPS */
3095 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003096
3097 /* clear the context structure first */
3098 memset(&tx_ctx, 0, sizeof(tx_ctx));
3099
3100 tx_ctx.new_context = 1;
3101 tx_ctx.base = (ring->dma / 128);
3102 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003103 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3104 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003105 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00003106 /* FDIR VSI tx ring can still use RS bit and writebacks */
3107 if (vsi->type != I40E_VSI_FDIR)
3108 tx_ctx.head_wb_ena = 1;
3109 tx_ctx.head_wb_addr = ring->dma +
3110 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003111
3112 /* As part of VSI creation/update, FW allocates certain
3113 * Tx arbitration queue sets for each TC enabled for
3114 * the VSI. The FW returns the handles to these queue
3115 * sets as part of the response buffer to Add VSI,
3116 * Update VSI, etc. AQ commands. It is expected that
3117 * these queue set handles be associated with the Tx
3118 * queues by the driver as part of the TX queue context
3119 * initialization. This has to be done regardless of
3120 * DCB as by default everything is mapped to TC0.
3121 */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003122
3123 if (ring->ch)
3124 tx_ctx.rdylist =
3125 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3126
3127 else
3128 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3129
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130 tx_ctx.rdylist_act = 0;
3131
3132 /* clear the context in the HMC */
3133 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3134 if (err) {
3135 dev_info(&vsi->back->pdev->dev,
3136 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3137 ring->queue_index, pf_q, err);
3138 return -ENOMEM;
3139 }
3140
3141 /* set the context in the HMC */
3142 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3143 if (err) {
3144 dev_info(&vsi->back->pdev->dev,
3145 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3146 ring->queue_index, pf_q, err);
3147 return -ENOMEM;
3148 }
3149
3150 /* Now associate this queue with this PCI function */
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003151 if (ring->ch) {
3152 if (ring->ch->type == I40E_VSI_VMDQ2)
3153 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3154 else
3155 return -EINVAL;
3156
3157 qtx_ctl |= (ring->ch->vsi_number <<
3158 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3159 I40E_QTX_CTL_VFVM_INDX_MASK;
Mitch Williams7a28d882014-10-17 03:14:52 +00003160 } else {
Amritha Nambiar8f88b302017-09-07 04:00:17 -07003161 if (vsi->type == I40E_VSI_VMDQ2) {
3162 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3163 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3164 I40E_QTX_CTL_VFVM_INDX_MASK;
3165 } else {
3166 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3167 }
Mitch Williams7a28d882014-10-17 03:14:52 +00003168 }
3169
Shannon Nelson13fd9772013-09-28 07:14:19 +00003170 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3171 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003172 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3173 i40e_flush(hw);
3174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003175 /* cache tail off for easier writes later */
3176 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3177
3178 return 0;
3179}
3180
3181/**
3182 * i40e_configure_rx_ring - Configure a receive ring context
3183 * @ring: The Rx ring to configure
3184 *
3185 * Configure the Rx descriptor ring in the HMC context.
3186 **/
3187static int i40e_configure_rx_ring(struct i40e_ring *ring)
3188{
3189 struct i40e_vsi *vsi = ring->vsi;
3190 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3191 u16 pf_q = vsi->base_queue + ring->queue_index;
3192 struct i40e_hw *hw = &vsi->back->hw;
3193 struct i40e_hmc_obj_rxq rx_ctx;
3194 i40e_status err = 0;
3195
Jesse Brandeburgbd6cd4e2017-08-29 05:32:35 -04003196 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003197
3198 /* clear the context structure first */
3199 memset(&rx_ctx, 0, sizeof(rx_ctx));
3200
3201 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003202
Alexander Duyckdab86af2017-03-14 10:15:27 -07003203 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3204 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205
3206 rx_ctx.base = (ring->dma / 128);
3207 rx_ctx.qlen = ring->count;
3208
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003209 /* use 32 byte descriptors */
3210 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003212 /* descriptor type is always zero
3213 * rx_ctx.dtype = 0;
3214 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003215 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003216
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003217 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003218 if (hw->revision_id == 0)
3219 rx_ctx.lrxqthresh = 0;
3220 else
Jacob Keller7362be92017-09-07 08:05:50 -04003221 rx_ctx.lrxqthresh = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003222 rx_ctx.crcstrip = 1;
3223 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003224 /* this controls whether VLAN is stripped from inner headers */
3225 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003226 /* set the prefena field to 1 because the manual says to */
3227 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003228
3229 /* clear the context in the HMC */
3230 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3231 if (err) {
3232 dev_info(&vsi->back->pdev->dev,
3233 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3234 ring->queue_index, pf_q, err);
3235 return -ENOMEM;
3236 }
3237
3238 /* set the context in the HMC */
3239 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3240 if (err) {
3241 dev_info(&vsi->back->pdev->dev,
3242 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3243 ring->queue_index, pf_q, err);
3244 return -ENOMEM;
3245 }
3246
Alexander Duyckca9ec082017-04-05 07:51:02 -04003247 /* configure Rx buffer alignment */
3248 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3249 clear_ring_build_skb_enabled(ring);
3250 else
3251 set_ring_build_skb_enabled(ring);
3252
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253 /* cache tail for quicker writes, and clear the reg before use */
3254 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3255 writel(0, ring->tail);
3256
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003257 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258
3259 return 0;
3260}
3261
3262/**
3263 * i40e_vsi_configure_tx - Configure the VSI for Tx
3264 * @vsi: VSI structure describing this set of rings and resources
3265 *
3266 * Configure the Tx VSI for operation.
3267 **/
3268static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3269{
3270 int err = 0;
3271 u16 i;
3272
Alexander Duyck9f65e152013-09-28 06:00:58 +00003273 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3274 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003275
Björn Töpel74608d12017-05-24 07:55:35 +02003276 if (!i40e_enabled_xdp_vsi(vsi))
3277 return err;
3278
3279 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3280 err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282 return err;
3283}
3284
3285/**
3286 * i40e_vsi_configure_rx - Configure the VSI for Rx
3287 * @vsi: the VSI being configured
3288 *
3289 * Configure the Rx VSI for operation.
3290 **/
3291static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3292{
3293 int err = 0;
3294 u16 i;
3295
Alexander Duyckdab86af2017-03-14 10:15:27 -07003296 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3297 vsi->max_frame = I40E_MAX_RXBUFFER;
3298 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3299#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003300 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3301 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003302 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3303 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3304#endif
3305 } else {
3306 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003307 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3308 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003309 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003310
3311 /* set up individual rings */
3312 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003313 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003314
3315 return err;
3316}
3317
3318/**
3319 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3320 * @vsi: ptr to the VSI
3321 **/
3322static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3323{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003324 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325 u16 qoffset, qcount;
3326 int i, n;
3327
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003328 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3329 /* Reset the TC information */
3330 for (i = 0; i < vsi->num_queue_pairs; i++) {
3331 rx_ring = vsi->rx_rings[i];
3332 tx_ring = vsi->tx_rings[i];
3333 rx_ring->dcb_tc = 0;
3334 tx_ring->dcb_tc = 0;
3335 }
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07003336 return;
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003337 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338
3339 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003340 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003341 continue;
3342
3343 qoffset = vsi->tc_config.tc_info[n].qoffset;
3344 qcount = vsi->tc_config.tc_info[n].qcount;
3345 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003346 rx_ring = vsi->rx_rings[i];
3347 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003348 rx_ring->dcb_tc = n;
3349 tx_ring->dcb_tc = n;
3350 }
3351 }
3352}
3353
3354/**
3355 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3356 * @vsi: ptr to the VSI
3357 **/
3358static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3359{
3360 if (vsi->netdev)
3361 i40e_set_rx_mode(vsi->netdev);
3362}
3363
3364/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003365 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3366 * @vsi: Pointer to the targeted VSI
3367 *
3368 * This function replays the hlist on the hw where all the SB Flow Director
3369 * filters were saved.
3370 **/
3371static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3372{
3373 struct i40e_fdir_filter *filter;
3374 struct i40e_pf *pf = vsi->back;
3375 struct hlist_node *node;
3376
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003377 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3378 return;
3379
Jacob Keller6d069422017-02-06 14:38:44 -08003380 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003381 pf->fd_tcp4_filter_cnt = 0;
3382 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003383 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003384 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003385
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003386 hlist_for_each_entry_safe(filter, node,
3387 &pf->fdir_filter_list, fdir_node) {
3388 i40e_add_del_fdir(vsi, filter, true);
3389 }
3390}
3391
3392/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003393 * i40e_vsi_configure - Set up the VSI for action
3394 * @vsi: the VSI being configured
3395 **/
3396static int i40e_vsi_configure(struct i40e_vsi *vsi)
3397{
3398 int err;
3399
3400 i40e_set_vsi_rx_mode(vsi);
3401 i40e_restore_vlan(vsi);
3402 i40e_vsi_config_dcb_rings(vsi);
3403 err = i40e_vsi_configure_tx(vsi);
3404 if (!err)
3405 err = i40e_vsi_configure_rx(vsi);
3406
3407 return err;
3408}
3409
3410/**
3411 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3412 * @vsi: the VSI being configured
3413 **/
3414static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3415{
Björn Töpel74608d12017-05-24 07:55:35 +02003416 bool has_xdp = i40e_enabled_xdp_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003417 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003418 struct i40e_hw *hw = &pf->hw;
3419 u16 vector;
3420 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003421 u32 qp;
3422
3423 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3424 * and PFINT_LNKLSTn registers, e.g.:
3425 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3426 */
3427 qp = vsi->base_queue;
3428 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003429 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003430 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3431
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003432 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003433 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003434 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3435 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3436 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003437 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003438 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3439 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3440 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003441 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003442 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003443
3444 /* Linked list for the queuepairs assigned to this vector */
3445 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3446 for (q = 0; q < q_vector->num_ringpairs; q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02003447 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003448 u32 val;
3449
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003451 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3452 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3453 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3454 (I40E_QUEUE_TYPE_TX <<
3455 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003456
3457 wr32(hw, I40E_QINT_RQCTL(qp), val);
3458
Björn Töpel74608d12017-05-24 07:55:35 +02003459 if (has_xdp) {
3460 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3461 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3462 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3463 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3464 (I40E_QUEUE_TYPE_TX <<
3465 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3466
3467 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3468 }
3469
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
Björn Töpel74608d12017-05-24 07:55:35 +02003471 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3472 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3473 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3474 (I40E_QUEUE_TYPE_RX <<
3475 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003476
3477 /* Terminate the linked list */
3478 if (q == (q_vector->num_ringpairs - 1))
Björn Töpel74608d12017-05-24 07:55:35 +02003479 val |= (I40E_QUEUE_END_OF_LIST <<
3480 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003481
3482 wr32(hw, I40E_QINT_TQCTL(qp), val);
3483 qp++;
3484 }
3485 }
3486
3487 i40e_flush(hw);
3488}
3489
3490/**
3491 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3492 * @hw: ptr to the hardware info
3493 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003494static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003495{
Jacob Kellerab437b52014-12-14 01:55:08 +00003496 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003497 u32 val;
3498
3499 /* clear things first */
3500 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3501 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3502
3503 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3504 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3505 I40E_PFINT_ICR0_ENA_GRST_MASK |
3506 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3507 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003508 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3509 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3510 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3511
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003512 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3513 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3514
Jacob Kellerab437b52014-12-14 01:55:08 +00003515 if (pf->flags & I40E_FLAG_PTP)
3516 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3517
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003518 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3519
3520 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003521 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3522 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003523
3524 /* OTHER_ITR_IDX = 0 */
3525 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3526}
3527
3528/**
3529 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3530 * @vsi: the VSI being configured
3531 **/
3532static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3533{
Björn Töpel74608d12017-05-24 07:55:35 +02003534 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00003535 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003536 struct i40e_pf *pf = vsi->back;
3537 struct i40e_hw *hw = &pf->hw;
3538 u32 val;
3539
3540 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003541 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003542 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003543 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3544 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003545 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3547 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3548
Jacob Kellerab437b52014-12-14 01:55:08 +00003549 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003550
3551 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3552 wr32(hw, I40E_PFINT_LNKLST0, 0);
3553
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003554 /* Associate the queue pair to the vector and enable the queue int */
Björn Töpel74608d12017-05-24 07:55:35 +02003555 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3556 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3557 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003558 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3559
3560 wr32(hw, I40E_QINT_RQCTL(0), val);
3561
Björn Töpel74608d12017-05-24 07:55:35 +02003562 if (i40e_enabled_xdp_vsi(vsi)) {
3563 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3564 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3565 (I40E_QUEUE_TYPE_TX
3566 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3567
3568 wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3569 }
3570
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003571 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3572 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3573 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3574
3575 wr32(hw, I40E_QINT_TQCTL(0), val);
3576 i40e_flush(hw);
3577}
3578
3579/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003580 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3581 * @pf: board private structure
3582 **/
3583void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3584{
3585 struct i40e_hw *hw = &pf->hw;
3586
3587 wr32(hw, I40E_PFINT_DYN_CTL0,
3588 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3589 i40e_flush(hw);
3590}
3591
3592/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003593 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3594 * @pf: board private structure
3595 **/
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003596void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003597{
3598 struct i40e_hw *hw = &pf->hw;
3599 u32 val;
3600
3601 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003602 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003603 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3604
3605 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3606 i40e_flush(hw);
3607}
3608
3609/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3611 * @irq: interrupt number
3612 * @data: pointer to a q_vector
3613 **/
3614static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3615{
3616 struct i40e_q_vector *q_vector = data;
3617
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003618 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003619 return IRQ_HANDLED;
3620
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003621 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003622
3623 return IRQ_HANDLED;
3624}
3625
3626/**
Alan Brady96db7762016-09-14 16:24:38 -07003627 * i40e_irq_affinity_notify - Callback for affinity changes
3628 * @notify: context as to what irq was changed
3629 * @mask: the new affinity mask
3630 *
3631 * This is a callback function used by the irq_set_affinity_notifier function
3632 * so that we may register to receive changes to the irq affinity masks.
3633 **/
3634static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3635 const cpumask_t *mask)
3636{
3637 struct i40e_q_vector *q_vector =
3638 container_of(notify, struct i40e_q_vector, affinity_notify);
3639
Jacob Keller7e4d01e2017-07-12 05:46:05 -04003640 cpumask_copy(&q_vector->affinity_mask, mask);
Alan Brady96db7762016-09-14 16:24:38 -07003641}
3642
3643/**
3644 * i40e_irq_affinity_release - Callback for affinity notifier release
3645 * @ref: internal core kernel usage
3646 *
3647 * This is a callback function used by the irq_set_affinity_notifier function
3648 * to inform the current notification subscriber that they will no longer
3649 * receive notifications.
3650 **/
3651static void i40e_irq_affinity_release(struct kref *ref) {}
3652
3653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003654 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3655 * @vsi: the VSI being configured
3656 * @basename: name for the vector
3657 *
3658 * Allocates MSI-X vectors and requests interrupts from the kernel.
3659 **/
3660static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3661{
3662 int q_vectors = vsi->num_q_vectors;
3663 struct i40e_pf *pf = vsi->back;
3664 int base = vsi->base_vector;
3665 int rx_int_idx = 0;
3666 int tx_int_idx = 0;
3667 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003668 int irq_num;
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003669 int cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670
3671 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003672 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673
Alan Brady96db7762016-09-14 16:24:38 -07003674 irq_num = pf->msix_entries[base + vector].vector;
3675
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003676 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3678 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3679 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003680 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003681 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3682 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003683 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003684 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3685 "%s-%s-%d", basename, "tx", tx_int_idx++);
3686 } else {
3687 /* skip this unused q_vector */
3688 continue;
3689 }
Alan Brady96db7762016-09-14 16:24:38 -07003690 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003691 vsi->irq_handler,
3692 0,
3693 q_vector->name,
3694 q_vector);
3695 if (err) {
3696 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003697 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003698 goto free_queue_irqs;
3699 }
Alan Brady96db7762016-09-14 16:24:38 -07003700
3701 /* register for affinity change notifications */
3702 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3703 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3704 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003705 /* Spread affinity hints out across online CPUs.
3706 *
3707 * get_cpu_mask returns a static constant mask with
3708 * a permanent lifetime so it's ok to pass to
3709 * irq_set_affinity_hint without making a copy.
Jacob Keller759dc4a2017-07-14 09:10:10 -04003710 */
Jacob Kellerbe664cb2017-08-29 05:32:31 -04003711 cpu = cpumask_local_spread(q_vector->v_idx, -1);
3712 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003713 }
3714
Shannon Nelson63741842014-04-23 04:50:16 +00003715 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003716 return 0;
3717
3718free_queue_irqs:
3719 while (vector) {
3720 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003721 irq_num = pf->msix_entries[base + vector].vector;
3722 irq_set_affinity_notifier(irq_num, NULL);
3723 irq_set_affinity_hint(irq_num, NULL);
3724 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003725 }
3726 return err;
3727}
3728
3729/**
3730 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3731 * @vsi: the VSI being un-configured
3732 **/
3733static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3734{
3735 struct i40e_pf *pf = vsi->back;
3736 struct i40e_hw *hw = &pf->hw;
3737 int base = vsi->base_vector;
3738 int i;
3739
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003740 /* disable interrupt causation from each queue */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003741 for (i = 0; i < vsi->num_queue_pairs; i++) {
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003742 u32 val;
3743
3744 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3745 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3746 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3747
3748 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3749 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3750 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3751
Björn Töpel74608d12017-05-24 07:55:35 +02003752 if (!i40e_enabled_xdp_vsi(vsi))
3753 continue;
3754 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003755 }
3756
Shannon Nelson2e5c26ea2017-06-07 05:43:11 -04003757 /* disable each interrupt */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003758 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3759 for (i = vsi->base_vector;
3760 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3761 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3762
3763 i40e_flush(hw);
3764 for (i = 0; i < vsi->num_q_vectors; i++)
3765 synchronize_irq(pf->msix_entries[i + base].vector);
3766 } else {
3767 /* Legacy and MSI mode - this stops all interrupt handling */
3768 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3769 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3770 i40e_flush(hw);
3771 synchronize_irq(pf->pdev->irq);
3772 }
3773}
3774
3775/**
3776 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3777 * @vsi: the VSI being configured
3778 **/
3779static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3780{
3781 struct i40e_pf *pf = vsi->back;
3782 int i;
3783
3784 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003785 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786 i40e_irq_dynamic_enable(vsi, i);
3787 } else {
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003788 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 }
3790
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003791 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003792 return 0;
3793}
3794
3795/**
Jacob Kellerc17401a2017-07-14 09:27:02 -04003796 * i40e_free_misc_vector - Free the vector that handles non-queue events
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003797 * @pf: board private structure
3798 **/
Jacob Kellerc17401a2017-07-14 09:27:02 -04003799static void i40e_free_misc_vector(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800{
3801 /* Disable ICR 0 */
3802 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3803 i40e_flush(&pf->hw);
Jacob Kellerc17401a2017-07-14 09:27:02 -04003804
3805 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3806 synchronize_irq(pf->msix_entries[0].vector);
3807 free_irq(pf->msix_entries[0].vector, pf);
3808 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3809 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003810}
3811
3812/**
3813 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3814 * @irq: interrupt number
3815 * @data: pointer to a q_vector
3816 *
3817 * This is the handler used for all MSI/Legacy interrupts, and deals
3818 * with both queue and non-queue interrupts. This is also used in
3819 * MSIX mode to handle the non-queue interrupts.
3820 **/
3821static irqreturn_t i40e_intr(int irq, void *data)
3822{
3823 struct i40e_pf *pf = (struct i40e_pf *)data;
3824 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003825 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003826 u32 icr0, icr0_remaining;
3827 u32 val, ena_mask;
3828
3829 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003830 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003831
Shannon Nelson116a57d2013-09-28 07:13:59 +00003832 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3833 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003834 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003835
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003836 /* if interrupt but no bits showing, must be SWINT */
3837 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3838 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3839 pf->sw_int_count++;
3840
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003841 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
Catherine Sullivan76429842017-06-07 05:43:12 -04003842 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003843 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003844 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Catherine Sullivan76429842017-06-07 05:43:12 -04003845 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003846 }
3847
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003848 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3849 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003850 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3851 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003852
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003853 /* We do not have a way to disarm Queue causes while leaving
3854 * interrupt enabled for all other causes, ideally
3855 * interrupt should be disabled while we are in NAPI but
3856 * this is not a performance path and napi_schedule()
3857 * can deal with rescheduling.
3858 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003859 if (!test_bit(__I40E_DOWN, pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003860 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003861 }
3862
3863 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3864 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003865 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003866 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003867 }
3868
3869 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3870 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003871 set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003872 }
3873
3874 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3875 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
Jacob Keller0da36b92017-04-19 09:25:55 -04003876 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003877 }
3878
3879 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
Jacob Keller0da36b92017-04-19 09:25:55 -04003880 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
3881 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003882 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3883 val = rd32(hw, I40E_GLGEN_RSTAT);
3884 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3885 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003886 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003888 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003889 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003890 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003891 pf->empr_count++;
Jacob Keller0da36b92017-04-19 09:25:55 -04003892 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003893 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003894 }
3895
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003896 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3897 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3898 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003899 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3900 rd32(hw, I40E_PFHMC_ERRORINFO),
3901 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003902 }
3903
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003904 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3905 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3906
3907 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003908 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003909 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003910 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003911 }
3912
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913 /* If a critical error is pending we have no choice but to reset the
3914 * device.
3915 * Report and mask out any remaining unexpected interrupts.
3916 */
3917 icr0_remaining = icr0 & ena_mask;
3918 if (icr0_remaining) {
3919 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3920 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003921 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003922 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003923 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003924 dev_info(&pf->pdev->dev, "device will be reset\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04003925 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003926 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003927 }
3928 ena_mask &= ~icr0_remaining;
3929 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003930 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003931
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003932enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003933 /* re-enable interrupt causes */
3934 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03003935 if (!test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003936 i40e_service_event_schedule(pf);
Jacob Kellerdbadbbe2017-09-07 08:05:49 -04003937 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003938 }
3939
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003940 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003941}
3942
3943/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003944 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3945 * @tx_ring: tx ring to clean
3946 * @budget: how many cleans we're allowed
3947 *
3948 * Returns true if there's any budget left (e.g. the clean is finished)
3949 **/
3950static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3951{
3952 struct i40e_vsi *vsi = tx_ring->vsi;
3953 u16 i = tx_ring->next_to_clean;
3954 struct i40e_tx_buffer *tx_buf;
3955 struct i40e_tx_desc *tx_desc;
3956
3957 tx_buf = &tx_ring->tx_bi[i];
3958 tx_desc = I40E_TX_DESC(tx_ring, i);
3959 i -= tx_ring->count;
3960
3961 do {
3962 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3963
3964 /* if next_to_watch is not set then there is no work pending */
3965 if (!eop_desc)
3966 break;
3967
3968 /* prevent any other reads prior to eop_desc */
Brian King52c69122017-11-17 11:05:44 -06003969 smp_rmb();
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003970
3971 /* if the descriptor isn't done, no work yet to do */
3972 if (!(eop_desc->cmd_type_offset_bsz &
3973 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3974 break;
3975
3976 /* clear next_to_watch to prevent false hangs */
3977 tx_buf->next_to_watch = NULL;
3978
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003979 tx_desc->buffer_addr = 0;
3980 tx_desc->cmd_type_offset_bsz = 0;
3981 /* move past filter desc */
3982 tx_buf++;
3983 tx_desc++;
3984 i++;
3985 if (unlikely(!i)) {
3986 i -= tx_ring->count;
3987 tx_buf = tx_ring->tx_bi;
3988 tx_desc = I40E_TX_DESC(tx_ring, 0);
3989 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003990 /* unmap skb header data */
3991 dma_unmap_single(tx_ring->dev,
3992 dma_unmap_addr(tx_buf, dma),
3993 dma_unmap_len(tx_buf, len),
3994 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003995 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3996 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003997
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003998 tx_buf->raw_buf = NULL;
3999 tx_buf->tx_flags = 0;
4000 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004001 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00004002 tx_desc->buffer_addr = 0;
4003 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004004
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00004005 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004006 tx_buf++;
4007 tx_desc++;
4008 i++;
4009 if (unlikely(!i)) {
4010 i -= tx_ring->count;
4011 tx_buf = tx_ring->tx_bi;
4012 tx_desc = I40E_TX_DESC(tx_ring, 0);
4013 }
4014
4015 /* update budget accounting */
4016 budget--;
4017 } while (likely(budget));
4018
4019 i += tx_ring->count;
4020 tx_ring->next_to_clean = i;
4021
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004022 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04004023 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004024
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08004025 return budget > 0;
4026}
4027
4028/**
4029 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4030 * @irq: interrupt number
4031 * @data: pointer to a q_vector
4032 **/
4033static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4034{
4035 struct i40e_q_vector *q_vector = data;
4036 struct i40e_vsi *vsi;
4037
4038 if (!q_vector->tx.ring)
4039 return IRQ_HANDLED;
4040
4041 vsi = q_vector->tx.ring->vsi;
4042 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4043
4044 return IRQ_HANDLED;
4045}
4046
4047/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004048 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004049 * @vsi: the VSI being configured
4050 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004051 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004052 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04004053static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004054{
Alexander Duyck493fb302013-09-28 07:01:44 +00004055 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00004056 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4057 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004058
4059 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004060 tx_ring->next = q_vector->tx.ring;
4061 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004062 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004063
Björn Töpel74608d12017-05-24 07:55:35 +02004064 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4065 if (i40e_enabled_xdp_vsi(vsi)) {
4066 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4067
4068 xdp_ring->q_vector = q_vector;
4069 xdp_ring->next = q_vector->tx.ring;
4070 q_vector->tx.ring = xdp_ring;
4071 q_vector->tx.count++;
4072 }
4073
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004074 rx_ring->q_vector = q_vector;
4075 rx_ring->next = q_vector->rx.ring;
4076 q_vector->rx.ring = rx_ring;
4077 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004078}
4079
4080/**
4081 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4082 * @vsi: the VSI being configured
4083 *
4084 * This function maps descriptor rings to the queue-specific vectors
4085 * we were allotted through the MSI-X enabling code. Ideally, we'd have
4086 * one vector per queue pair, but on a constrained vector budget, we
4087 * group the queue pairs as "efficiently" as possible.
4088 **/
4089static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4090{
4091 int qp_remaining = vsi->num_queue_pairs;
4092 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004093 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004094 int v_start = 0;
4095 int qp_idx = 0;
4096
4097 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4098 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00004099 * It is also important to go through all the vectors available to be
4100 * sure that if we don't use all the vectors, that the remaining vectors
4101 * are cleared. This is especially important when decreasing the
4102 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004103 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00004104 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004105 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4106
4107 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4108
4109 q_vector->num_ringpairs = num_ringpairs;
4110
4111 q_vector->rx.count = 0;
4112 q_vector->tx.count = 0;
4113 q_vector->rx.ring = NULL;
4114 q_vector->tx.ring = NULL;
4115
4116 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04004117 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004118 qp_idx++;
4119 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004120 }
4121 }
4122}
4123
4124/**
4125 * i40e_vsi_request_irq - Request IRQ from the OS
4126 * @vsi: the VSI being configured
4127 * @basename: name for the vector
4128 **/
4129static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4130{
4131 struct i40e_pf *pf = vsi->back;
4132 int err;
4133
4134 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4135 err = i40e_vsi_request_irq_msix(vsi, basename);
4136 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4137 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004138 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004139 else
4140 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00004141 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142
4143 if (err)
4144 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4145
4146 return err;
4147}
4148
4149#ifdef CONFIG_NET_POLL_CONTROLLER
4150/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08004151 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004152 * @netdev: network interface device structure
4153 *
4154 * This is used by netconsole to send skbs without having to re-enable
4155 * interrupts. It's not called while the normal interrupt routine is executing.
4156 **/
4157static void i40e_netpoll(struct net_device *netdev)
4158{
4159 struct i40e_netdev_priv *np = netdev_priv(netdev);
4160 struct i40e_vsi *vsi = np->vsi;
4161 struct i40e_pf *pf = vsi->back;
4162 int i;
4163
4164 /* if interface is down do nothing */
Jacob Keller0da36b92017-04-19 09:25:55 -04004165 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004166 return;
4167
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004168 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4169 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004170 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004171 } else {
4172 i40e_intr(pf->pdev->irq, netdev);
4173 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004174}
4175#endif
4176
Jacob Kellerc768e492017-04-13 04:45:47 -04004177#define I40E_QTX_ENA_WAIT_COUNT 50
4178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004179/**
Neerav Parikh23527302014-06-03 23:50:15 +00004180 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4181 * @pf: the PF being configured
4182 * @pf_q: the PF queue
4183 * @enable: enable or disable state of the queue
4184 *
4185 * This routine will wait for the given Tx queue of the PF to reach the
4186 * enabled or disabled state.
4187 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4188 * multiple retries; else will return 0 in case of success.
4189 **/
4190static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4191{
4192 int i;
4193 u32 tx_reg;
4194
4195 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4196 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4197 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4198 break;
4199
Neerav Parikhf98a2002014-09-13 07:40:44 +00004200 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004201 }
4202 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4203 return -ETIMEDOUT;
4204
4205 return 0;
4206}
4207
4208/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004209 * i40e_control_tx_q - Start or stop a particular Tx queue
4210 * @pf: the PF structure
4211 * @pf_q: the PF queue to configure
4212 * @enable: start or stop the queue
4213 *
4214 * This function enables or disables a single queue. Note that any delay
4215 * required after the operation is expected to be handled by the caller of
4216 * this function.
4217 **/
4218static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4219{
4220 struct i40e_hw *hw = &pf->hw;
4221 u32 tx_reg;
4222 int i;
4223
4224 /* warn the TX unit of coming changes */
4225 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4226 if (!enable)
4227 usleep_range(10, 20);
4228
4229 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4230 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4231 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4232 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4233 break;
4234 usleep_range(1000, 2000);
4235 }
4236
4237 /* Skip if the queue is already in the requested state */
4238 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4239 return;
4240
4241 /* turn on/off the queue */
4242 if (enable) {
4243 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4244 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4245 } else {
4246 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4247 }
4248
4249 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4250}
4251
4252/**
Björn Töpel74608d12017-05-24 07:55:35 +02004253 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4254 * @seid: VSI SEID
4255 * @pf: the PF structure
4256 * @pf_q: the PF queue to configure
4257 * @is_xdp: true if the queue is used for XDP
4258 * @enable: start or stop the queue
4259 **/
4260static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4261 bool is_xdp, bool enable)
4262{
4263 int ret;
4264
4265 i40e_control_tx_q(pf, pf_q, enable);
4266
4267 /* wait for the change to finish */
4268 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4269 if (ret) {
4270 dev_info(&pf->pdev->dev,
4271 "VSI seid %d %sTx ring %d %sable timeout\n",
4272 seid, (is_xdp ? "XDP " : ""), pf_q,
4273 (enable ? "en" : "dis"));
4274 }
4275
4276 return ret;
4277}
4278
4279/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004280 * i40e_vsi_control_tx - Start or stop a VSI's rings
4281 * @vsi: the VSI being configured
4282 * @enable: start or stop the rings
4283 **/
4284static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4285{
4286 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004287 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004288
4289 pf_q = vsi->base_queue;
4290 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Björn Töpel74608d12017-05-24 07:55:35 +02004291 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4292 pf_q,
4293 false /*is xdp*/, enable);
4294 if (ret)
Neerav Parikh23527302014-06-03 23:50:15 +00004295 break;
Björn Töpel74608d12017-05-24 07:55:35 +02004296
4297 if (!i40e_enabled_xdp_vsi(vsi))
4298 continue;
4299
4300 ret = i40e_control_wait_tx_q(vsi->seid, pf,
4301 pf_q + vsi->alloc_queue_pairs,
4302 true /*is xdp*/, enable);
4303 if (ret)
4304 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004305 }
4306
Neerav Parikh23527302014-06-03 23:50:15 +00004307 return ret;
4308}
4309
4310/**
4311 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4312 * @pf: the PF being configured
4313 * @pf_q: the PF queue
4314 * @enable: enable or disable state of the queue
4315 *
4316 * This routine will wait for the given Rx queue of the PF to reach the
4317 * enabled or disabled state.
4318 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4319 * multiple retries; else will return 0 in case of success.
4320 **/
4321static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4322{
4323 int i;
4324 u32 rx_reg;
4325
4326 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4327 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4328 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4329 break;
4330
Neerav Parikhf98a2002014-09-13 07:40:44 +00004331 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004332 }
4333 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4334 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004335
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004336 return 0;
4337}
4338
4339/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004340 * i40e_control_rx_q - Start or stop a particular Rx queue
4341 * @pf: the PF structure
4342 * @pf_q: the PF queue to configure
4343 * @enable: start or stop the queue
4344 *
4345 * This function enables or disables a single queue. Note that any delay
4346 * required after the operation is expected to be handled by the caller of
4347 * this function.
4348 **/
4349static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4350{
4351 struct i40e_hw *hw = &pf->hw;
4352 u32 rx_reg;
4353 int i;
4354
4355 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4356 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4357 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4358 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4359 break;
4360 usleep_range(1000, 2000);
4361 }
4362
4363 /* Skip if the queue is already in the requested state */
4364 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4365 return;
4366
4367 /* turn on/off the queue */
4368 if (enable)
4369 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4370 else
4371 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4372
4373 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4374}
4375
4376/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004377 * i40e_vsi_control_rx - Start or stop a VSI's rings
4378 * @vsi: the VSI being configured
4379 * @enable: start or stop the rings
4380 **/
4381static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4382{
4383 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004384 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004385
4386 pf_q = vsi->base_queue;
4387 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004388 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004389
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004390 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004391 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4392 if (ret) {
4393 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004394 "VSI seid %d Rx ring %d %sable timeout\n",
4395 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004396 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004397 }
4398 }
4399
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004400 /* Due to HW errata, on Rx disable only, the register can indicate done
4401 * before it really is. Needs 50ms to be sure
4402 */
4403 if (!enable)
4404 mdelay(50);
4405
Neerav Parikh23527302014-06-03 23:50:15 +00004406 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004407}
4408
4409/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004410 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004411 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004412 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004413int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004414{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004415 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416
4417 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004418 ret = i40e_vsi_control_rx(vsi, true);
4419 if (ret)
4420 return ret;
4421 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004422
4423 return ret;
4424}
4425
4426/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004427 * i40e_vsi_stop_rings - Stop a VSI's rings
4428 * @vsi: the VSI being configured
4429 **/
4430void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4431{
Jacob Keller34807562017-04-13 04:45:53 -04004432 /* When port TX is suspended, don't wait */
Jacob Keller0da36b92017-04-19 09:25:55 -04004433 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
Jacob Keller34807562017-04-13 04:45:53 -04004434 return i40e_vsi_stop_rings_no_wait(vsi);
4435
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004436 /* do rx first for enable and last for disable
4437 * Ignore return value, we need to shutdown whatever we can
4438 */
4439 i40e_vsi_control_tx(vsi, false);
4440 i40e_vsi_control_rx(vsi, false);
4441}
4442
4443/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004444 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4445 * @vsi: the VSI being shutdown
4446 *
4447 * This function stops all the rings for a VSI but does not delay to verify
4448 * that rings have been disabled. It is expected that the caller is shutting
4449 * down multiple VSIs at once and will delay together for all the VSIs after
4450 * initiating the shutdown. This is particularly useful for shutting down lots
4451 * of VFs together. Otherwise, a large delay can be incurred while configuring
4452 * each VSI in serial.
4453 **/
4454void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4455{
4456 struct i40e_pf *pf = vsi->back;
4457 int i, pf_q;
4458
4459 pf_q = vsi->base_queue;
4460 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4461 i40e_control_tx_q(pf, pf_q, false);
4462 i40e_control_rx_q(pf, pf_q, false);
4463 }
4464}
4465
4466/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004467 * i40e_vsi_free_irq - Free the irq association with the OS
4468 * @vsi: the VSI being configured
4469 **/
4470static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4471{
4472 struct i40e_pf *pf = vsi->back;
4473 struct i40e_hw *hw = &pf->hw;
4474 int base = vsi->base_vector;
4475 u32 val, qp;
4476 int i;
4477
4478 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4479 if (!vsi->q_vectors)
4480 return;
4481
Shannon Nelson63741842014-04-23 04:50:16 +00004482 if (!vsi->irqs_ready)
4483 return;
4484
4485 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004486 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004487 int irq_num;
4488 u16 vector;
4489
4490 vector = i + base;
4491 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004492
4493 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004494 if (!vsi->q_vectors[i] ||
4495 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004496 continue;
4497
Alan Brady96db7762016-09-14 16:24:38 -07004498 /* clear the affinity notifier in the IRQ descriptor */
4499 irq_set_affinity_notifier(irq_num, NULL);
Jacob Keller759dc4a2017-07-14 09:10:10 -04004500 /* remove our suggested affinity mask for this IRQ */
Alan Brady96db7762016-09-14 16:24:38 -07004501 irq_set_affinity_hint(irq_num, NULL);
4502 synchronize_irq(irq_num);
4503 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004504
4505 /* Tear down the interrupt queue link list
4506 *
4507 * We know that they come in pairs and always
4508 * the Rx first, then the Tx. To clear the
4509 * link list, stick the EOL value into the
4510 * next_q field of the registers.
4511 */
4512 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4513 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4514 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4515 val |= I40E_QUEUE_END_OF_LIST
4516 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4517 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4518
4519 while (qp != I40E_QUEUE_END_OF_LIST) {
4520 u32 next;
4521
4522 val = rd32(hw, I40E_QINT_RQCTL(qp));
4523
4524 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4525 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4526 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4527 I40E_QINT_RQCTL_INTEVENT_MASK);
4528
4529 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4530 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4531
4532 wr32(hw, I40E_QINT_RQCTL(qp), val);
4533
4534 val = rd32(hw, I40E_QINT_TQCTL(qp));
4535
4536 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4537 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4538
4539 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4540 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4541 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4542 I40E_QINT_TQCTL_INTEVENT_MASK);
4543
4544 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4545 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4546
4547 wr32(hw, I40E_QINT_TQCTL(qp), val);
4548 qp = next;
4549 }
4550 }
4551 } else {
4552 free_irq(pf->pdev->irq, pf);
4553
4554 val = rd32(hw, I40E_PFINT_LNKLST0);
4555 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4556 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4557 val |= I40E_QUEUE_END_OF_LIST
4558 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4559 wr32(hw, I40E_PFINT_LNKLST0, val);
4560
4561 val = rd32(hw, I40E_QINT_RQCTL(qp));
4562 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4563 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4564 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4565 I40E_QINT_RQCTL_INTEVENT_MASK);
4566
4567 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4568 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4569
4570 wr32(hw, I40E_QINT_RQCTL(qp), val);
4571
4572 val = rd32(hw, I40E_QINT_TQCTL(qp));
4573
4574 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4575 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4576 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4577 I40E_QINT_TQCTL_INTEVENT_MASK);
4578
4579 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4580 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4581
4582 wr32(hw, I40E_QINT_TQCTL(qp), val);
4583 }
4584}
4585
4586/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004587 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4588 * @vsi: the VSI being configured
4589 * @v_idx: Index of vector to be freed
4590 *
4591 * This function frees the memory allocated to the q_vector. In addition if
4592 * NAPI is enabled it will delete any references to the NAPI struct prior
4593 * to freeing the q_vector.
4594 **/
4595static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4596{
4597 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004598 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004599
4600 if (!q_vector)
4601 return;
4602
4603 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004604 i40e_for_each_ring(ring, q_vector->tx)
4605 ring->q_vector = NULL;
4606
4607 i40e_for_each_ring(ring, q_vector->rx)
4608 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004609
4610 /* only VSI w/ an associated netdev is set up w/ NAPI */
4611 if (vsi->netdev)
4612 netif_napi_del(&q_vector->napi);
4613
4614 vsi->q_vectors[v_idx] = NULL;
4615
4616 kfree_rcu(q_vector, rcu);
4617}
4618
4619/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004620 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4621 * @vsi: the VSI being un-configured
4622 *
4623 * This frees the memory allocated to the q_vectors and
4624 * deletes references to the NAPI struct.
4625 **/
4626static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4627{
4628 int v_idx;
4629
Alexander Duyck493fb302013-09-28 07:01:44 +00004630 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4631 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004632}
4633
4634/**
4635 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4636 * @pf: board private structure
4637 **/
4638static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4639{
4640 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4641 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4642 pci_disable_msix(pf->pdev);
4643 kfree(pf->msix_entries);
4644 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004645 kfree(pf->irq_pile);
4646 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004647 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4648 pci_disable_msi(pf->pdev);
4649 }
4650 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4651}
4652
4653/**
4654 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4655 * @pf: board private structure
4656 *
4657 * We go through and clear interrupt specific resources and reset the structure
4658 * to pre-load conditions
4659 **/
4660static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4661{
4662 int i;
4663
Jacob Kellerc17401a2017-07-14 09:27:02 -04004664 i40e_free_misc_vector(pf);
Shannon Nelsone1477582015-02-21 06:44:33 +00004665
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004666 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4667 I40E_IWARP_IRQ_PILE_ID);
4668
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004669 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004670 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004671 if (pf->vsi[i])
4672 i40e_vsi_free_q_vectors(pf->vsi[i]);
4673 i40e_reset_interrupt_capability(pf);
4674}
4675
4676/**
4677 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4678 * @vsi: the VSI being configured
4679 **/
4680static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4681{
4682 int q_idx;
4683
4684 if (!vsi->netdev)
4685 return;
4686
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004687 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4688 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4689
4690 if (q_vector->rx.ring || q_vector->tx.ring)
4691 napi_enable(&q_vector->napi);
4692 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004693}
4694
4695/**
4696 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4697 * @vsi: the VSI being configured
4698 **/
4699static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4700{
4701 int q_idx;
4702
4703 if (!vsi->netdev)
4704 return;
4705
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004706 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4707 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4708
4709 if (q_vector->rx.ring || q_vector->tx.ring)
4710 napi_disable(&q_vector->napi);
4711 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004712}
4713
4714/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004715 * i40e_vsi_close - Shut down a VSI
4716 * @vsi: the vsi to be quelled
4717 **/
4718static void i40e_vsi_close(struct i40e_vsi *vsi)
4719{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004720 struct i40e_pf *pf = vsi->back;
Jacob Keller0da36b92017-04-19 09:25:55 -04004721 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004722 i40e_down(vsi);
4723 i40e_vsi_free_irq(vsi);
4724 i40e_vsi_free_tx_resources(vsi);
4725 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004726 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004727 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jacob Keller0da36b92017-04-19 09:25:55 -04004728 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004729 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004730}
4731
4732/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004733 * i40e_quiesce_vsi - Pause a given VSI
4734 * @vsi: the VSI being paused
4735 **/
4736static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4737{
Jacob Keller0da36b92017-04-19 09:25:55 -04004738 if (test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004739 return;
4740
Jacob Keller0da36b92017-04-19 09:25:55 -04004741 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004742 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004743 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004744 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004745 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004746}
4747
4748/**
4749 * i40e_unquiesce_vsi - Resume a given VSI
4750 * @vsi: the VSI being resumed
4751 **/
4752static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4753{
Jacob Keller0da36b92017-04-19 09:25:55 -04004754 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004755 return;
4756
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004757 if (vsi->netdev && netif_running(vsi->netdev))
4758 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4759 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004760 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004761}
4762
4763/**
4764 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4765 * @pf: the PF
4766 **/
4767static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4768{
4769 int v;
4770
Mitch Williams505682c2014-05-20 08:01:37 +00004771 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004772 if (pf->vsi[v])
4773 i40e_quiesce_vsi(pf->vsi[v]);
4774 }
4775}
4776
4777/**
4778 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4779 * @pf: the PF
4780 **/
4781static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4782{
4783 int v;
4784
Mitch Williams505682c2014-05-20 08:01:37 +00004785 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004786 if (pf->vsi[v])
4787 i40e_unquiesce_vsi(pf->vsi[v]);
4788 }
4789}
4790
Neerav Parikh69129dc2014-11-12 00:18:46 +00004791/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004792 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004793 * @vsi: the VSI being configured
4794 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004795 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004796 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004797int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004798{
4799 struct i40e_pf *pf = vsi->back;
4800 int i, pf_q, ret;
4801
4802 pf_q = vsi->base_queue;
4803 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004804 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004805 ret = i40e_pf_txq_wait(pf, pf_q, false);
4806 if (ret) {
4807 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004808 "VSI seid %d Tx ring %d disable timeout\n",
4809 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004810 return ret;
4811 }
Björn Töpel74608d12017-05-24 07:55:35 +02004812
4813 if (!i40e_enabled_xdp_vsi(vsi))
4814 goto wait_rx;
4815
4816 /* Check and wait for the XDP Tx queue */
4817 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
4818 false);
4819 if (ret) {
4820 dev_info(&pf->pdev->dev,
4821 "VSI seid %d XDP Tx ring %d disable timeout\n",
4822 vsi->seid, pf_q);
4823 return ret;
4824 }
4825wait_rx:
4826 /* Check and wait for the Rx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004827 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4828 if (ret) {
4829 dev_info(&pf->pdev->dev,
4830 "VSI seid %d Rx ring %d disable timeout\n",
4831 vsi->seid, pf_q);
4832 return ret;
4833 }
4834 }
4835
Neerav Parikh69129dc2014-11-12 00:18:46 +00004836 return 0;
4837}
4838
Jacob Kellere4b433f2017-04-13 04:45:52 -04004839#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004840/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004841 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004842 * @pf: the PF
4843 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004844 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004845 * VSIs that are managed by this PF.
4846 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004847static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004848{
4849 int v, ret = 0;
4850
4851 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004852 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004853 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004854 if (ret)
4855 break;
4856 }
4857 }
4858
4859 return ret;
4860}
4861
4862#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004863
4864/**
4865 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4866 * @q_idx: TX queue number
4867 * @vsi: Pointer to VSI struct
4868 *
4869 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004870 * We proactively detect hung TX queues by checking if interrupts are disabled
4871 * but there are pending descriptors. If it appears hung, attempt to recover
4872 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004873 **/
4874static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4875{
4876 struct i40e_ring *tx_ring = NULL;
4877 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004878 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004879 int i;
4880
4881 pf = vsi->back;
4882
4883 /* now that we have an index, find the tx_ring struct */
4884 for (i = 0; i < vsi->num_queue_pairs; i++) {
4885 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4886 if (q_idx == vsi->tx_rings[i]->queue_index) {
4887 tx_ring = vsi->tx_rings[i];
4888 break;
4889 }
4890 }
4891 }
4892
4893 if (!tx_ring)
4894 return;
4895
4896 /* Read interrupt register */
4897 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4898 val = rd32(&pf->hw,
4899 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4900 tx_ring->vsi->base_vector - 1));
4901 else
4902 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4903
Alan Brady17daabb2017-04-05 07:50:56 -04004904 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004905
Alan Brady17daabb2017-04-05 07:50:56 -04004906 /* Interrupts are disabled and TX pending is non-zero,
4907 * trigger the SW interrupt (don't wait). Worst case
4908 * there will be one extra interrupt which may result
4909 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004910 */
Alan Brady17daabb2017-04-05 07:50:56 -04004911 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4912 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004913}
4914
4915/**
4916 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4917 * @pf: pointer to PF struct
4918 *
4919 * LAN VSI has netdev and netdev has TX queues. This function is to check
4920 * each of those TX queues if they are hung, trigger recovery by issuing
4921 * SW interrupt.
4922 **/
4923static void i40e_detect_recover_hung(struct i40e_pf *pf)
4924{
4925 struct net_device *netdev;
4926 struct i40e_vsi *vsi;
Jesse Brandeburgb85c94b2017-06-20 15:16:59 -07004927 unsigned int i;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004928
4929 /* Only for LAN VSI */
4930 vsi = pf->vsi[pf->lan_vsi];
4931
4932 if (!vsi)
4933 return;
4934
4935 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
Jacob Keller0da36b92017-04-19 09:25:55 -04004936 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
4937 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
Kiran Patilb03a8c12015-09-24 18:13:15 -04004938 return;
4939
4940 /* Make sure type is MAIN VSI */
4941 if (vsi->type != I40E_VSI_MAIN)
4942 return;
4943
4944 netdev = vsi->netdev;
4945 if (!netdev)
4946 return;
4947
4948 /* Bail out if netif_carrier is not OK */
4949 if (!netif_carrier_ok(netdev))
4950 return;
4951
4952 /* Go thru' TX queues for netdev */
4953 for (i = 0; i < netdev->num_tx_queues; i++) {
4954 struct netdev_queue *q;
4955
4956 q = netdev_get_tx_queue(netdev, i);
4957 if (q)
4958 i40e_detect_recover_hung_queue(i, vsi);
4959 }
4960}
4961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004962/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004963 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004964 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004965 *
4966 * Get TC map for ISCSI PF type that will include iSCSI TC
4967 * and LAN TC.
4968 **/
4969static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4970{
4971 struct i40e_dcb_app_priority_table app;
4972 struct i40e_hw *hw = &pf->hw;
4973 u8 enabled_tc = 1; /* TC0 is always enabled */
4974 u8 tc, i;
4975 /* Get the iSCSI APP TLV */
4976 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4977
4978 for (i = 0; i < dcbcfg->numapps; i++) {
4979 app = dcbcfg->app[i];
4980 if (app.selector == I40E_APP_SEL_TCPIP &&
4981 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4982 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004983 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004984 break;
4985 }
4986 }
4987
4988 return enabled_tc;
4989}
4990
4991/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004992 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4993 * @dcbcfg: the corresponding DCBx configuration structure
4994 *
4995 * Return the number of TCs from given DCBx configuration
4996 **/
4997static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4998{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004999 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00005000 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07005001 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005002
5003 /* Scan the ETS Config Priority Table to find
5004 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07005005 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005006 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07005007 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5008 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5009
5010 /* Now scan the bitmask to check for
5011 * contiguous TCs starting with TC0
5012 */
5013 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5014 if (num_tc & BIT(i)) {
5015 if (!tc_unused) {
5016 ret++;
5017 } else {
5018 pr_err("Non-contiguous TC - Disabling DCB\n");
5019 return 1;
5020 }
5021 } else {
5022 tc_unused = 1;
5023 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005024 }
5025
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07005026 /* There is always at least TC0 */
5027 if (!ret)
5028 ret = 1;
5029
5030 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005031}
5032
5033/**
5034 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5035 * @dcbcfg: the corresponding DCBx configuration structure
5036 *
5037 * Query the current DCB configuration and return the number of
5038 * traffic classes enabled from the given DCBX config
5039 **/
5040static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5041{
5042 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5043 u8 enabled_tc = 1;
5044 u8 i;
5045
5046 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005047 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005048
5049 return enabled_tc;
5050}
5051
5052/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005053 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5054 * @pf: PF being queried
5055 *
5056 * Query the current MQPRIO configuration and return the number of
5057 * traffic classes enabled.
5058 **/
5059static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5060{
5061 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5062 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5063 u8 enabled_tc = 1, i;
5064
5065 for (i = 1; i < num_tc; i++)
5066 enabled_tc |= BIT(i);
5067 return enabled_tc;
5068}
5069
5070/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005071 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5072 * @pf: PF being queried
5073 *
5074 * Return number of traffic classes enabled for the given PF
5075 **/
5076static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5077{
5078 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07005079 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005080 u8 num_tc = 0;
5081 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5082
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005083 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5084 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5085
5086 /* If neither MQPRIO nor DCB is enabled, then always use single TC */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005087 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5088 return 1;
5089
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005090 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005091 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5092 return i40e_dcb_get_num_tc(dcbcfg);
5093
5094 /* MFP mode return count of enabled TCs for this PF */
5095 if (pf->hw.func_caps.iscsi)
5096 enabled_tc = i40e_get_iscsi_tc_map(pf);
5097 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00005098 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005099
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005100 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005101 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005102 num_tc++;
5103 }
5104 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005105}
5106
5107/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005108 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5109 * @pf: PF being queried
5110 *
5111 * Return a bitmap for enabled traffic classes for this PF.
5112 **/
5113static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5114{
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005115 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5116 return i40e_mqprio_get_enabled_tc(pf);
5117
5118 /* If neither MQPRIO nor DCB is enabled for this PF then just return
5119 * default TC
5120 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005121 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07005122 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005123
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005124 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005125 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5126 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5127
Neerav Parikhfc51de92015-02-24 06:58:53 +00005128 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00005129 if (pf->hw.func_caps.iscsi)
5130 return i40e_get_iscsi_tc_map(pf);
5131 else
David Ertmanea6acb72016-09-20 07:10:50 -07005132 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005133}
5134
5135/**
5136 * i40e_vsi_get_bw_info - Query VSI BW Information
5137 * @vsi: the VSI being queried
5138 *
5139 * Returns 0 on success, negative value on failure
5140 **/
5141static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5142{
5143 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5144 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5145 struct i40e_pf *pf = vsi->back;
5146 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005147 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005148 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005149 int i;
5150
5151 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005152 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5153 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005154 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005155 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5156 i40e_stat_str(&pf->hw, ret),
5157 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005158 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005159 }
5160
5161 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005162 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5163 NULL);
5164 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005165 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005166 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5167 i40e_stat_str(&pf->hw, ret),
5168 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005169 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005170 }
5171
5172 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5173 dev_info(&pf->pdev->dev,
5174 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5175 bw_config.tc_valid_bits,
5176 bw_ets_config.tc_valid_bits);
5177 /* Still continuing */
5178 }
5179
5180 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5181 vsi->bw_max_quanta = bw_config.max_bw;
5182 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5183 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5184 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5185 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5186 vsi->bw_ets_limit_credits[i] =
5187 le16_to_cpu(bw_ets_config.credits[i]);
5188 /* 3 bits out of 4 for each TC */
5189 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5190 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00005191
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005192 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005193}
5194
5195/**
5196 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5197 * @vsi: the VSI being configured
5198 * @enabled_tc: TC bitmap
5199 * @bw_credits: BW shared credits per TC
5200 *
5201 * Returns 0 on success, negative value on failure
5202 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005203static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005204 u8 *bw_share)
5205{
5206 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005207 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005208 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005209
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005210 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005211 return 0;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005212 if (!vsi->mqprio_qopt.qopt.hw) {
5213 ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5214 if (ret)
5215 dev_info(&vsi->back->pdev->dev,
5216 "Failed to reset tx rate for vsi->seid %u\n",
5217 vsi->seid);
5218 return ret;
5219 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005220 bw_data.tc_valid_bits = enabled_tc;
5221 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5222 bw_data.tc_bw_credits[i] = bw_share[i];
5223
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005224 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
5225 NULL);
5226 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005227 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005228 "AQ command Config VSI BW allocation per TC failed = %d\n",
5229 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005230 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005231 }
5232
5233 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5234 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5235
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00005236 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005237}
5238
5239/**
5240 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5241 * @vsi: the VSI being configured
5242 * @enabled_tc: TC map to be enabled
5243 *
5244 **/
5245static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5246{
5247 struct net_device *netdev = vsi->netdev;
5248 struct i40e_pf *pf = vsi->back;
5249 struct i40e_hw *hw = &pf->hw;
5250 u8 netdev_tc = 0;
5251 int i;
5252 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5253
5254 if (!netdev)
5255 return;
5256
5257 if (!enabled_tc) {
5258 netdev_reset_tc(netdev);
5259 return;
5260 }
5261
5262 /* Set up actual enabled TCs on the VSI */
5263 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5264 return;
5265
5266 /* set per TC queues for the VSI */
5267 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5268 /* Only set TC queues for enabled tcs
5269 *
5270 * e.g. For a VSI that has TC0 and TC3 enabled the
5271 * enabled_tc bitmap would be 0x00001001; the driver
5272 * will set the numtc for netdev as 2 that will be
5273 * referenced by the netdev layer as TC 0 and 1.
5274 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005275 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005276 netdev_set_tc_queue(netdev,
5277 vsi->tc_config.tc_info[i].netdev_tc,
5278 vsi->tc_config.tc_info[i].qcount,
5279 vsi->tc_config.tc_info[i].qoffset);
5280 }
5281
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005282 if (pf->flags & I40E_FLAG_TC_MQPRIO)
5283 return;
5284
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005285 /* Assign UP2TC map for the VSI */
5286 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5287 /* Get the actual TC# for the UP */
5288 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5289 /* Get the mapped netdev TC# for the UP */
5290 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5291 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5292 }
5293}
5294
5295/**
5296 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5297 * @vsi: the VSI being configured
5298 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5299 **/
5300static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5301 struct i40e_vsi_context *ctxt)
5302{
5303 /* copy just the sections touched not the entire info
5304 * since not all sections are valid as returned by
5305 * update vsi params
5306 */
5307 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5308 memcpy(&vsi->info.queue_mapping,
5309 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5310 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5311 sizeof(vsi->info.tc_mapping));
5312}
5313
5314/**
5315 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5316 * @vsi: VSI to be configured
5317 * @enabled_tc: TC bitmap
5318 *
5319 * This configures a particular VSI for TCs that are mapped to the
5320 * given TC bitmap. It uses default bandwidth share for TCs across
5321 * VSIs to configure TC for a particular VSI.
5322 *
5323 * NOTE:
5324 * It is expected that the VSI queues have been quisced before calling
5325 * this function.
5326 **/
5327static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5328{
5329 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5330 struct i40e_vsi_context ctxt;
5331 int ret = 0;
5332 int i;
5333
5334 /* Check if enabled_tc is same as existing or new TCs */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005335 if (vsi->tc_config.enabled_tc == enabled_tc &&
5336 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005337 return ret;
5338
5339 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5340 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005341 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005342 bw_share[i] = 1;
5343 }
5344
5345 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5346 if (ret) {
5347 dev_info(&vsi->back->pdev->dev,
5348 "Failed configuring TC map %d for VSI %d\n",
5349 enabled_tc, vsi->seid);
5350 goto out;
5351 }
5352
5353 /* Update Queue Pairs Mapping for currently enabled UPs */
5354 ctxt.seid = vsi->seid;
5355 ctxt.pf_num = vsi->back->hw.pf_id;
5356 ctxt.vf_num = 0;
5357 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005358 ctxt.info = vsi->info;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005359 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5360 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5361 if (ret)
5362 goto out;
5363 } else {
5364 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5365 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005366
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005367 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5368 * queues changed.
5369 */
5370 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5371 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5372 vsi->num_queue_pairs);
5373 ret = i40e_vsi_config_rss(vsi);
5374 if (ret) {
5375 dev_info(&vsi->back->pdev->dev,
5376 "Failed to reconfig rss for num_queues\n");
5377 return ret;
5378 }
5379 vsi->reconfig_rss = false;
5380 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005381 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5382 ctxt.info.valid_sections |=
5383 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5384 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5385 }
5386
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07005387 /* Update the VSI after updating the VSI queue-mapping
5388 * information
5389 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005390 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5391 if (ret) {
5392 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005393 "Update vsi tc config failed, err %s aq_err %s\n",
5394 i40e_stat_str(&vsi->back->hw, ret),
5395 i40e_aq_str(&vsi->back->hw,
5396 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005397 goto out;
5398 }
5399 /* update the local VSI info with updated queue map */
5400 i40e_vsi_update_queue_map(vsi, &ctxt);
5401 vsi->info.valid_sections = 0;
5402
5403 /* Update current VSI BW information */
5404 ret = i40e_vsi_get_bw_info(vsi);
5405 if (ret) {
5406 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005407 "Failed updating vsi bw info, err %s aq_err %s\n",
5408 i40e_stat_str(&vsi->back->hw, ret),
5409 i40e_aq_str(&vsi->back->hw,
5410 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005411 goto out;
5412 }
5413
5414 /* Update the netdev TC setup */
5415 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5416out:
5417 return ret;
5418}
5419
5420/**
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005421 * i40e_get_link_speed - Returns link speed for the interface
5422 * @vsi: VSI to be configured
5423 *
5424 **/
5425int i40e_get_link_speed(struct i40e_vsi *vsi)
5426{
5427 struct i40e_pf *pf = vsi->back;
5428
5429 switch (pf->hw.phy.link_info.link_speed) {
5430 case I40E_LINK_SPEED_40GB:
5431 return 40000;
5432 case I40E_LINK_SPEED_25GB:
5433 return 25000;
5434 case I40E_LINK_SPEED_20GB:
5435 return 20000;
5436 case I40E_LINK_SPEED_10GB:
5437 return 10000;
5438 case I40E_LINK_SPEED_1GB:
5439 return 1000;
5440 default:
5441 return -EINVAL;
5442 }
5443}
5444
5445/**
5446 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5447 * @vsi: VSI to be configured
5448 * @seid: seid of the channel/VSI
5449 * @max_tx_rate: max TX rate to be configured as BW limit
5450 *
5451 * Helper function to set BW limit for a given VSI
5452 **/
5453int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5454{
5455 struct i40e_pf *pf = vsi->back;
Alan Brady6c32e0d2017-10-09 15:48:45 -07005456 u64 credits = 0;
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005457 int speed = 0;
5458 int ret = 0;
5459
5460 speed = i40e_get_link_speed(vsi);
5461 if (max_tx_rate > speed) {
5462 dev_err(&pf->pdev->dev,
5463 "Invalid max tx rate %llu specified for VSI seid %d.",
5464 max_tx_rate, seid);
5465 return -EINVAL;
5466 }
5467 if (max_tx_rate && max_tx_rate < 50) {
5468 dev_warn(&pf->pdev->dev,
5469 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5470 max_tx_rate = 50;
5471 }
5472
5473 /* Tx rate credits are in values of 50Mbps, 0 is disabled */
Alan Brady6c32e0d2017-10-09 15:48:45 -07005474 credits = max_tx_rate;
5475 do_div(credits, I40E_BW_CREDIT_DIVISOR);
5476 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
Amritha Nambiar5ecae412017-09-07 04:00:27 -07005477 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5478 if (ret)
5479 dev_err(&pf->pdev->dev,
5480 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5481 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5482 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5483 return ret;
5484}
5485
5486/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005487 * i40e_remove_queue_channels - Remove queue channels for the TCs
5488 * @vsi: VSI to be configured
5489 *
5490 * Remove queue channels for the TCs
5491 **/
5492static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5493{
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005494 enum i40e_admin_queue_err last_aq_status;
5495 struct i40e_cloud_filter *cfilter;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005496 struct i40e_channel *ch, *ch_tmp;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005497 struct i40e_pf *pf = vsi->back;
5498 struct hlist_node *node;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005499 int ret, i;
5500
5501 /* Reset rss size that was stored when reconfiguring rss for
5502 * channel VSIs with non-power-of-2 queue count.
5503 */
5504 vsi->current_rss_size = 0;
5505
5506 /* perform cleanup for channels if they exist */
5507 if (list_empty(&vsi->ch_list))
5508 return;
5509
5510 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5511 struct i40e_vsi *p_vsi;
5512
5513 list_del(&ch->list);
5514 p_vsi = ch->parent_vsi;
5515 if (!p_vsi || !ch->initialized) {
5516 kfree(ch);
5517 continue;
5518 }
5519 /* Reset queue contexts */
5520 for (i = 0; i < ch->num_queue_pairs; i++) {
5521 struct i40e_ring *tx_ring, *rx_ring;
5522 u16 pf_q;
5523
5524 pf_q = ch->base_queue + i;
5525 tx_ring = vsi->tx_rings[pf_q];
5526 tx_ring->ch = NULL;
5527
5528 rx_ring = vsi->rx_rings[pf_q];
5529 rx_ring->ch = NULL;
5530 }
5531
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07005532 /* Reset BW configured for this VSI via mqprio */
5533 ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5534 if (ret)
5535 dev_info(&vsi->back->pdev->dev,
5536 "Failed to reset tx rate for ch->seid %u\n",
5537 ch->seid);
5538
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07005539 /* delete cloud filters associated with this channel */
5540 hlist_for_each_entry_safe(cfilter, node,
5541 &pf->cloud_filter_list, cloud_node) {
5542 if (cfilter->seid != ch->seid)
5543 continue;
5544
5545 hash_del(&cfilter->cloud_node);
5546 if (cfilter->dst_port)
5547 ret = i40e_add_del_cloud_filter_big_buf(vsi,
5548 cfilter,
5549 false);
5550 else
5551 ret = i40e_add_del_cloud_filter(vsi, cfilter,
5552 false);
5553 last_aq_status = pf->hw.aq.asq_last_status;
5554 if (ret)
5555 dev_info(&pf->pdev->dev,
5556 "Failed to delete cloud filter, err %s aq_err %s\n",
5557 i40e_stat_str(&pf->hw, ret),
5558 i40e_aq_str(&pf->hw, last_aq_status));
5559 kfree(cfilter);
5560 }
5561
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005562 /* delete VSI from FW */
5563 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5564 NULL);
5565 if (ret)
5566 dev_err(&vsi->back->pdev->dev,
5567 "unable to remove channel (%d) for parent VSI(%d)\n",
5568 ch->seid, p_vsi->seid);
5569 kfree(ch);
5570 }
5571 INIT_LIST_HEAD(&vsi->ch_list);
5572}
5573
5574/**
5575 * i40e_is_any_channel - channel exist or not
5576 * @vsi: ptr to VSI to which channels are associated with
5577 *
5578 * Returns true or false if channel(s) exist for associated VSI or not
5579 **/
5580static bool i40e_is_any_channel(struct i40e_vsi *vsi)
5581{
5582 struct i40e_channel *ch, *ch_tmp;
5583
5584 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5585 if (ch->initialized)
5586 return true;
5587 }
5588
5589 return false;
5590}
5591
5592/**
5593 * i40e_get_max_queues_for_channel
5594 * @vsi: ptr to VSI to which channels are associated with
5595 *
5596 * Helper function which returns max value among the queue counts set on the
5597 * channels/TCs created.
5598 **/
5599static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5600{
5601 struct i40e_channel *ch, *ch_tmp;
5602 int max = 0;
5603
5604 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5605 if (!ch->initialized)
5606 continue;
5607 if (ch->num_queue_pairs > max)
5608 max = ch->num_queue_pairs;
5609 }
5610
5611 return max;
5612}
5613
5614/**
5615 * i40e_validate_num_queues - validate num_queues w.r.t channel
5616 * @pf: ptr to PF device
5617 * @num_queues: number of queues
5618 * @vsi: the parent VSI
5619 * @reconfig_rss: indicates should the RSS be reconfigured or not
5620 *
5621 * This function validates number of queues in the context of new channel
5622 * which is being established and determines if RSS should be reconfigured
5623 * or not for parent VSI.
5624 **/
5625static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5626 struct i40e_vsi *vsi, bool *reconfig_rss)
5627{
5628 int max_ch_queues;
5629
5630 if (!reconfig_rss)
5631 return -EINVAL;
5632
5633 *reconfig_rss = false;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07005634 if (vsi->current_rss_size) {
5635 if (num_queues > vsi->current_rss_size) {
5636 dev_dbg(&pf->pdev->dev,
5637 "Error: num_queues (%d) > vsi's current_size(%d)\n",
5638 num_queues, vsi->current_rss_size);
5639 return -EINVAL;
5640 } else if ((num_queues < vsi->current_rss_size) &&
5641 (!is_power_of_2(num_queues))) {
5642 dev_dbg(&pf->pdev->dev,
5643 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5644 num_queues, vsi->current_rss_size);
5645 return -EINVAL;
5646 }
5647 }
5648
5649 if (!is_power_of_2(num_queues)) {
5650 /* Find the max num_queues configured for channel if channel
5651 * exist.
5652 * if channel exist, then enforce 'num_queues' to be more than
5653 * max ever queues configured for channel.
5654 */
5655 max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5656 if (num_queues < max_ch_queues) {
5657 dev_dbg(&pf->pdev->dev,
5658 "Error: num_queues (%d) < max queues configured for channel(%d)\n",
5659 num_queues, max_ch_queues);
5660 return -EINVAL;
5661 }
5662 *reconfig_rss = true;
5663 }
5664
5665 return 0;
5666}
5667
5668/**
5669 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5670 * @vsi: the VSI being setup
5671 * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5672 *
5673 * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5674 **/
5675static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5676{
5677 struct i40e_pf *pf = vsi->back;
5678 u8 seed[I40E_HKEY_ARRAY_SIZE];
5679 struct i40e_hw *hw = &pf->hw;
5680 int local_rss_size;
5681 u8 *lut;
5682 int ret;
5683
5684 if (!vsi->rss_size)
5685 return -EINVAL;
5686
5687 if (rss_size > vsi->rss_size)
5688 return -EINVAL;
5689
5690 local_rss_size = min_t(int, vsi->rss_size, rss_size);
5691 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5692 if (!lut)
5693 return -ENOMEM;
5694
5695 /* Ignoring user configured lut if there is one */
5696 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5697
5698 /* Use user configured hash key if there is one, otherwise
5699 * use default.
5700 */
5701 if (vsi->rss_hkey_user)
5702 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5703 else
5704 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5705
5706 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5707 if (ret) {
5708 dev_info(&pf->pdev->dev,
5709 "Cannot set RSS lut, err %s aq_err %s\n",
5710 i40e_stat_str(hw, ret),
5711 i40e_aq_str(hw, hw->aq.asq_last_status));
5712 kfree(lut);
5713 return ret;
5714 }
5715 kfree(lut);
5716
5717 /* Do the update w.r.t. storing rss_size */
5718 if (!vsi->orig_rss_size)
5719 vsi->orig_rss_size = vsi->rss_size;
5720 vsi->current_rss_size = local_rss_size;
5721
5722 return ret;
5723}
5724
5725/**
5726 * i40e_channel_setup_queue_map - Setup a channel queue map
5727 * @pf: ptr to PF device
5728 * @vsi: the VSI being setup
5729 * @ctxt: VSI context structure
5730 * @ch: ptr to channel structure
5731 *
5732 * Setup queue map for a specific channel
5733 **/
5734static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5735 struct i40e_vsi_context *ctxt,
5736 struct i40e_channel *ch)
5737{
5738 u16 qcount, qmap, sections = 0;
5739 u8 offset = 0;
5740 int pow;
5741
5742 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5743 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5744
5745 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5746 ch->num_queue_pairs = qcount;
5747
5748 /* find the next higher power-of-2 of num queue pairs */
5749 pow = ilog2(qcount);
5750 if (!is_power_of_2(qcount))
5751 pow++;
5752
5753 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5754 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5755
5756 /* Setup queue TC[0].qmap for given VSI context */
5757 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5758
5759 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5760 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5761 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5762 ctxt->info.valid_sections |= cpu_to_le16(sections);
5763}
5764
5765/**
5766 * i40e_add_channel - add a channel by adding VSI
5767 * @pf: ptr to PF device
5768 * @uplink_seid: underlying HW switching element (VEB) ID
5769 * @ch: ptr to channel structure
5770 *
5771 * Add a channel (VSI) using add_vsi and queue_map
5772 **/
5773static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5774 struct i40e_channel *ch)
5775{
5776 struct i40e_hw *hw = &pf->hw;
5777 struct i40e_vsi_context ctxt;
5778 u8 enabled_tc = 0x1; /* TC0 enabled */
5779 int ret;
5780
5781 if (ch->type != I40E_VSI_VMDQ2) {
5782 dev_info(&pf->pdev->dev,
5783 "add new vsi failed, ch->type %d\n", ch->type);
5784 return -EINVAL;
5785 }
5786
5787 memset(&ctxt, 0, sizeof(ctxt));
5788 ctxt.pf_num = hw->pf_id;
5789 ctxt.vf_num = 0;
5790 ctxt.uplink_seid = uplink_seid;
5791 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5792 if (ch->type == I40E_VSI_VMDQ2)
5793 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5794
5795 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5796 ctxt.info.valid_sections |=
5797 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5798 ctxt.info.switch_id =
5799 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5800 }
5801
5802 /* Set queue map for a given VSI context */
5803 i40e_channel_setup_queue_map(pf, &ctxt, ch);
5804
5805 /* Now time to create VSI */
5806 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
5807 if (ret) {
5808 dev_info(&pf->pdev->dev,
5809 "add new vsi failed, err %s aq_err %s\n",
5810 i40e_stat_str(&pf->hw, ret),
5811 i40e_aq_str(&pf->hw,
5812 pf->hw.aq.asq_last_status));
5813 return -ENOENT;
5814 }
5815
5816 /* Success, update channel */
5817 ch->enabled_tc = enabled_tc;
5818 ch->seid = ctxt.seid;
5819 ch->vsi_number = ctxt.vsi_number;
5820 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
5821
5822 /* copy just the sections touched not the entire info
5823 * since not all sections are valid as returned by
5824 * update vsi params
5825 */
5826 ch->info.mapping_flags = ctxt.info.mapping_flags;
5827 memcpy(&ch->info.queue_mapping,
5828 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
5829 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
5830 sizeof(ctxt.info.tc_mapping));
5831
5832 return 0;
5833}
5834
5835static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
5836 u8 *bw_share)
5837{
5838 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5839 i40e_status ret;
5840 int i;
5841
5842 bw_data.tc_valid_bits = ch->enabled_tc;
5843 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5844 bw_data.tc_bw_credits[i] = bw_share[i];
5845
5846 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
5847 &bw_data, NULL);
5848 if (ret) {
5849 dev_info(&vsi->back->pdev->dev,
5850 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
5851 vsi->back->hw.aq.asq_last_status, ch->seid);
5852 return -EINVAL;
5853 }
5854
5855 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5856 ch->info.qs_handle[i] = bw_data.qs_handles[i];
5857
5858 return 0;
5859}
5860
5861/**
5862 * i40e_channel_config_tx_ring - config TX ring associated with new channel
5863 * @pf: ptr to PF device
5864 * @vsi: the VSI being setup
5865 * @ch: ptr to channel structure
5866 *
5867 * Configure TX rings associated with channel (VSI) since queues are being
5868 * from parent VSI.
5869 **/
5870static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
5871 struct i40e_vsi *vsi,
5872 struct i40e_channel *ch)
5873{
5874 i40e_status ret;
5875 int i;
5876 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5877
5878 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5879 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5880 if (ch->enabled_tc & BIT(i))
5881 bw_share[i] = 1;
5882 }
5883
5884 /* configure BW for new VSI */
5885 ret = i40e_channel_config_bw(vsi, ch, bw_share);
5886 if (ret) {
5887 dev_info(&vsi->back->pdev->dev,
5888 "Failed configuring TC map %d for channel (seid %u)\n",
5889 ch->enabled_tc, ch->seid);
5890 return ret;
5891 }
5892
5893 for (i = 0; i < ch->num_queue_pairs; i++) {
5894 struct i40e_ring *tx_ring, *rx_ring;
5895 u16 pf_q;
5896
5897 pf_q = ch->base_queue + i;
5898
5899 /* Get to TX ring ptr of main VSI, for re-setup TX queue
5900 * context
5901 */
5902 tx_ring = vsi->tx_rings[pf_q];
5903 tx_ring->ch = ch;
5904
5905 /* Get the RX ring ptr */
5906 rx_ring = vsi->rx_rings[pf_q];
5907 rx_ring->ch = ch;
5908 }
5909
5910 return 0;
5911}
5912
5913/**
5914 * i40e_setup_hw_channel - setup new channel
5915 * @pf: ptr to PF device
5916 * @vsi: the VSI being setup
5917 * @ch: ptr to channel structure
5918 * @uplink_seid: underlying HW switching element (VEB) ID
5919 * @type: type of channel to be created (VMDq2/VF)
5920 *
5921 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5922 * and configures TX rings accordingly
5923 **/
5924static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
5925 struct i40e_vsi *vsi,
5926 struct i40e_channel *ch,
5927 u16 uplink_seid, u8 type)
5928{
5929 int ret;
5930
5931 ch->initialized = false;
5932 ch->base_queue = vsi->next_base_queue;
5933 ch->type = type;
5934
5935 /* Proceed with creation of channel (VMDq2) VSI */
5936 ret = i40e_add_channel(pf, uplink_seid, ch);
5937 if (ret) {
5938 dev_info(&pf->pdev->dev,
5939 "failed to add_channel using uplink_seid %u\n",
5940 uplink_seid);
5941 return ret;
5942 }
5943
5944 /* Mark the successful creation of channel */
5945 ch->initialized = true;
5946
5947 /* Reconfigure TX queues using QTX_CTL register */
5948 ret = i40e_channel_config_tx_ring(pf, vsi, ch);
5949 if (ret) {
5950 dev_info(&pf->pdev->dev,
5951 "failed to configure TX rings for channel %u\n",
5952 ch->seid);
5953 return ret;
5954 }
5955
5956 /* update 'next_base_queue' */
5957 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
5958 dev_dbg(&pf->pdev->dev,
5959 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
5960 ch->seid, ch->vsi_number, ch->stat_counter_idx,
5961 ch->num_queue_pairs,
5962 vsi->next_base_queue);
5963 return ret;
5964}
5965
5966/**
5967 * i40e_setup_channel - setup new channel using uplink element
5968 * @pf: ptr to PF device
5969 * @type: type of channel to be created (VMDq2/VF)
5970 * @uplink_seid: underlying HW switching element (VEB) ID
5971 * @ch: ptr to channel structure
5972 *
5973 * Setup new channel (VSI) based on specified type (VMDq2/VF)
5974 * and uplink switching element (uplink_seid)
5975 **/
5976static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
5977 struct i40e_channel *ch)
5978{
5979 u8 vsi_type;
5980 u16 seid;
5981 int ret;
5982
5983 if (vsi->type == I40E_VSI_MAIN) {
5984 vsi_type = I40E_VSI_VMDQ2;
5985 } else {
5986 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
5987 vsi->type);
5988 return false;
5989 }
5990
5991 /* underlying switching element */
5992 seid = pf->vsi[pf->lan_vsi]->uplink_seid;
5993
5994 /* create channel (VSI), configure TX rings */
5995 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
5996 if (ret) {
5997 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
5998 return false;
5999 }
6000
6001 return ch->initialized ? true : false;
6002}
6003
6004/**
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006005 * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6006 * @vsi: ptr to VSI which has PF backing
6007 *
6008 * Sets up switch mode correctly if it needs to be changed and perform
6009 * what are allowed modes.
6010 **/
6011static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6012{
6013 u8 mode;
6014 struct i40e_pf *pf = vsi->back;
6015 struct i40e_hw *hw = &pf->hw;
6016 int ret;
6017
6018 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6019 if (ret)
6020 return -EINVAL;
6021
6022 if (hw->dev_caps.switch_mode) {
6023 /* if switch mode is set, support mode2 (non-tunneled for
6024 * cloud filter) for now
6025 */
6026 u32 switch_mode = hw->dev_caps.switch_mode &
6027 I40E_SWITCH_MODE_MASK;
6028 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6029 if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6030 return 0;
6031 dev_err(&pf->pdev->dev,
6032 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6033 hw->dev_caps.switch_mode);
6034 return -EINVAL;
6035 }
6036 }
6037
6038 /* Set Bit 7 to be valid */
6039 mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6040
6041 /* Set L4type to both TCP and UDP support */
6042 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_BOTH;
6043
6044 /* Set cloud filter mode */
6045 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6046
6047 /* Prep mode field for set_switch_config */
6048 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6049 pf->last_sw_conf_valid_flags,
6050 mode, NULL);
6051 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6052 dev_err(&pf->pdev->dev,
6053 "couldn't set switch config bits, err %s aq_err %s\n",
6054 i40e_stat_str(hw, ret),
6055 i40e_aq_str(hw,
6056 hw->aq.asq_last_status));
6057
6058 return ret;
6059}
6060
6061/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006062 * i40e_create_queue_channel - function to create channel
6063 * @vsi: VSI to be configured
6064 * @ch: ptr to channel (it contains channel specific params)
6065 *
6066 * This function creates channel (VSI) using num_queues specified by user,
6067 * reconfigs RSS if needed.
6068 **/
6069int i40e_create_queue_channel(struct i40e_vsi *vsi,
6070 struct i40e_channel *ch)
6071{
6072 struct i40e_pf *pf = vsi->back;
6073 bool reconfig_rss;
6074 int err;
6075
6076 if (!ch)
6077 return -EINVAL;
6078
6079 if (!ch->num_queue_pairs) {
6080 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6081 ch->num_queue_pairs);
6082 return -EINVAL;
6083 }
6084
6085 /* validate user requested num_queues for channel */
6086 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6087 &reconfig_rss);
6088 if (err) {
6089 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6090 ch->num_queue_pairs);
6091 return -EINVAL;
6092 }
6093
6094 /* By default we are in VEPA mode, if this is the first VF/VMDq
6095 * VSI to be added switch to VEB mode.
6096 */
6097 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
6098 (!i40e_is_any_channel(vsi))) {
6099 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
6100 dev_dbg(&pf->pdev->dev,
6101 "Failed to create channel. Override queues (%u) not power of 2\n",
6102 vsi->tc_config.tc_info[0].qcount);
6103 return -EINVAL;
6104 }
6105
6106 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6107 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6108
6109 if (vsi->type == I40E_VSI_MAIN) {
6110 if (pf->flags & I40E_FLAG_TC_MQPRIO)
6111 i40e_do_reset(pf, I40E_PF_RESET_FLAG,
6112 true);
6113 else
6114 i40e_do_reset_safe(pf,
6115 I40E_PF_RESET_FLAG);
6116 }
6117 }
6118 /* now onwards for main VSI, number of queues will be value
6119 * of TC0's queue count
6120 */
6121 }
6122
6123 /* By this time, vsi->cnt_q_avail shall be set to non-zero and
6124 * it should be more than num_queues
6125 */
6126 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6127 dev_dbg(&pf->pdev->dev,
6128 "Error: cnt_q_avail (%u) less than num_queues %d\n",
6129 vsi->cnt_q_avail, ch->num_queue_pairs);
6130 return -EINVAL;
6131 }
6132
6133 /* reconfig_rss only if vsi type is MAIN_VSI */
6134 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6135 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6136 if (err) {
6137 dev_info(&pf->pdev->dev,
6138 "Error: unable to reconfig rss for num_queues (%u)\n",
6139 ch->num_queue_pairs);
6140 return -EINVAL;
6141 }
6142 }
6143
6144 if (!i40e_setup_channel(pf, vsi, ch)) {
6145 dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6146 return -EINVAL;
6147 }
6148
6149 dev_info(&pf->pdev->dev,
6150 "Setup channel (id:%u) utilizing num_queues %d\n",
6151 ch->seid, ch->num_queue_pairs);
6152
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006153 /* configure VSI for BW limit */
6154 if (ch->max_tx_rate) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006155 u64 credits = ch->max_tx_rate;
6156
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006157 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6158 return -EINVAL;
6159
Alan Brady6c32e0d2017-10-09 15:48:45 -07006160 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006161 dev_dbg(&pf->pdev->dev,
6162 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6163 ch->max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07006164 credits,
6165 ch->seid);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006166 }
6167
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006168 /* in case of VF, this will be main SRIOV VSI */
6169 ch->parent_vsi = vsi;
6170
6171 /* and update main_vsi's count for queue_available to use */
6172 vsi->cnt_q_avail -= ch->num_queue_pairs;
6173
6174 return 0;
6175}
6176
6177/**
6178 * i40e_configure_queue_channels - Add queue channel for the given TCs
6179 * @vsi: VSI to be configured
6180 *
6181 * Configures queue channel mapping to the given TCs
6182 **/
6183static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6184{
6185 struct i40e_channel *ch;
Alan Brady6c32e0d2017-10-09 15:48:45 -07006186 u64 max_rate = 0;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006187 int ret = 0, i;
6188
6189 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
Amritha Nambiaraa5cb02a2017-10-27 02:35:40 -07006190 vsi->tc_seid_map[0] = vsi->seid;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006191 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6192 if (vsi->tc_config.enabled_tc & BIT(i)) {
6193 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6194 if (!ch) {
6195 ret = -ENOMEM;
6196 goto err_free;
6197 }
6198
6199 INIT_LIST_HEAD(&ch->list);
6200 ch->num_queue_pairs =
6201 vsi->tc_config.tc_info[i].qcount;
6202 ch->base_queue =
6203 vsi->tc_config.tc_info[i].qoffset;
6204
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006205 /* Bandwidth limit through tc interface is in bytes/s,
6206 * change to Mbit/s
6207 */
Alan Brady6c32e0d2017-10-09 15:48:45 -07006208 max_rate = vsi->mqprio_qopt.max_rate[i];
6209 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6210 ch->max_tx_rate = max_rate;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006211
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006212 list_add_tail(&ch->list, &vsi->ch_list);
6213
6214 ret = i40e_create_queue_channel(vsi, ch);
6215 if (ret) {
6216 dev_err(&vsi->back->pdev->dev,
6217 "Failed creating queue channel with TC%d: queues %d\n",
6218 i, ch->num_queue_pairs);
6219 goto err_free;
6220 }
Amritha Nambiaraa5cb02a2017-10-27 02:35:40 -07006221 vsi->tc_seid_map[i] = ch->seid;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006222 }
6223 }
6224 return ret;
6225
6226err_free:
6227 i40e_remove_queue_channels(vsi);
6228 return ret;
6229}
6230
6231/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006232 * i40e_veb_config_tc - Configure TCs for given VEB
6233 * @veb: given VEB
6234 * @enabled_tc: TC bitmap
6235 *
6236 * Configures given TC bitmap for VEB (switching) element
6237 **/
6238int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6239{
6240 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6241 struct i40e_pf *pf = veb->pf;
6242 int ret = 0;
6243 int i;
6244
6245 /* No TCs or already enabled TCs just return */
6246 if (!enabled_tc || veb->enabled_tc == enabled_tc)
6247 return ret;
6248
6249 bw_data.tc_valid_bits = enabled_tc;
6250 /* bw_data.absolute_credits is not set (relative) */
6251
6252 /* Enable ETS TCs with equal BW Share for now */
6253 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006254 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006255 bw_data.tc_bw_share_credits[i] = 1;
6256 }
6257
6258 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6259 &bw_data, NULL);
6260 if (ret) {
6261 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006262 "VEB bw config failed, err %s aq_err %s\n",
6263 i40e_stat_str(&pf->hw, ret),
6264 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006265 goto out;
6266 }
6267
6268 /* Update the BW information */
6269 ret = i40e_veb_get_bw_info(veb);
6270 if (ret) {
6271 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006272 "Failed getting veb bw config, err %s aq_err %s\n",
6273 i40e_stat_str(&pf->hw, ret),
6274 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006275 }
6276
6277out:
6278 return ret;
6279}
6280
6281#ifdef CONFIG_I40E_DCB
6282/**
6283 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6284 * @pf: PF struct
6285 *
6286 * Reconfigure VEB/VSIs on a given PF; it is assumed that
6287 * the caller would've quiesce all the VSIs before calling
6288 * this function
6289 **/
6290static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6291{
6292 u8 tc_map = 0;
6293 int ret;
6294 u8 v;
6295
6296 /* Enable the TCs available on PF to all VEBs */
6297 tc_map = i40e_pf_get_tc_map(pf);
6298 for (v = 0; v < I40E_MAX_VEB; v++) {
6299 if (!pf->veb[v])
6300 continue;
6301 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6302 if (ret) {
6303 dev_info(&pf->pdev->dev,
6304 "Failed configuring TC for VEB seid=%d\n",
6305 pf->veb[v]->seid);
6306 /* Will try to configure as many components */
6307 }
6308 }
6309
6310 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00006311 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006312 if (!pf->vsi[v])
6313 continue;
6314
6315 /* - Enable all TCs for the LAN VSI
6316 * - For all others keep them at TC0 for now
6317 */
6318 if (v == pf->lan_vsi)
6319 tc_map = i40e_pf_get_tc_map(pf);
6320 else
David Ertmanea6acb72016-09-20 07:10:50 -07006321 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006322
6323 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6324 if (ret) {
6325 dev_info(&pf->pdev->dev,
6326 "Failed configuring TC for VSI seid=%d\n",
6327 pf->vsi[v]->seid);
6328 /* Will try to configure as many components */
6329 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00006330 /* Re-configure VSI vectors based on updated TC map */
6331 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006332 if (pf->vsi[v]->netdev)
6333 i40e_dcbnl_set_all(pf->vsi[v]);
6334 }
6335 }
6336}
6337
6338/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006339 * i40e_resume_port_tx - Resume port Tx
6340 * @pf: PF struct
6341 *
6342 * Resume a port's Tx and issue a PF reset in case of failure to
6343 * resume.
6344 **/
6345static int i40e_resume_port_tx(struct i40e_pf *pf)
6346{
6347 struct i40e_hw *hw = &pf->hw;
6348 int ret;
6349
6350 ret = i40e_aq_resume_port_tx(hw, NULL);
6351 if (ret) {
6352 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006353 "Resume Port Tx failed, err %s aq_err %s\n",
6354 i40e_stat_str(&pf->hw, ret),
6355 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006356 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04006357 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006358 i40e_service_event_schedule(pf);
6359 }
6360
6361 return ret;
6362}
6363
6364/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006365 * i40e_init_pf_dcb - Initialize DCB configuration
6366 * @pf: PF being configured
6367 *
6368 * Query the current DCB configuration and cache it
6369 * in the hardware structure
6370 **/
6371static int i40e_init_pf_dcb(struct i40e_pf *pf)
6372{
6373 struct i40e_hw *hw = &pf->hw;
6374 int err = 0;
6375
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006376 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Jacob Kellerd36e41d2017-06-23 04:24:46 -04006377 if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006378 goto out;
6379
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006380 /* Get the initial DCB configuration */
6381 err = i40e_init_dcb(hw);
6382 if (!err) {
6383 /* Device/Function is not DCBX capable */
6384 if ((!hw->func_caps.dcb) ||
6385 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6386 dev_info(&pf->pdev->dev,
6387 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006388 } else {
6389 /* When status is not DISABLED then DCBX in FW */
6390 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6391 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006392
6393 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07006394 /* Enable DCB tagging only when more than one TC
6395 * or explicitly disable if only one TC
6396 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006397 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6398 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07006399 else
6400 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006401 dev_dbg(&pf->pdev->dev,
6402 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006403 }
Neerav Parikh014269f2014-04-01 07:11:48 +00006404 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006405 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006406 "Query for DCB configuration failed, err %s aq_err %s\n",
6407 i40e_stat_str(&pf->hw, err),
6408 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006409 }
6410
6411out:
6412 return err;
6413}
6414#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006415#define SPEED_SIZE 14
6416#define FC_SIZE 8
6417/**
6418 * i40e_print_link_message - print link up or down
6419 * @vsi: the VSI for which link needs a message
6420 */
Matt Jaredc156f852015-08-27 11:42:39 -04006421void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006422{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006423 enum i40e_aq_link_speed new_speed;
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006424 struct i40e_pf *pf = vsi->back;
Shannon Nelsona9165492015-09-03 17:19:00 -04006425 char *speed = "Unknown";
6426 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006427 char *fec = "";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006428 char *req_fec = "";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006429 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006430
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006431 new_speed = pf->hw.phy.link_info.link_speed;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006432
6433 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04006434 return;
6435 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08006436 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006437 if (!isup) {
6438 netdev_info(vsi->netdev, "NIC Link is Down\n");
6439 return;
6440 }
6441
Greg Rose148c2d82014-12-11 07:06:27 +00006442 /* Warn user if link speed on NPAR enabled partition is not at
6443 * least 10GB
6444 */
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006445 if (pf->hw.func_caps.npar_enable &&
6446 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6447 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
Greg Rose148c2d82014-12-11 07:06:27 +00006448 netdev_warn(vsi->netdev,
6449 "The partition detected link speed that is less than 10Gbps\n");
6450
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006451 switch (pf->hw.phy.link_info.link_speed) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006452 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006453 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006454 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006455 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006456 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07006457 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08006458 case I40E_LINK_SPEED_25GB:
6459 speed = "25 G";
6460 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006461 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006462 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006463 break;
6464 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006465 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006466 break;
Mitch Williams5960d332014-09-13 07:40:47 +00006467 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04006468 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00006469 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006470 default:
6471 break;
6472 }
6473
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006474 switch (pf->hw.fc.current_mode) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006475 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04006476 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006477 break;
6478 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006479 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006480 break;
6481 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04006482 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006483 break;
6484 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04006485 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006486 break;
6487 }
6488
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006489 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
Mariusz Stachura68e49702017-07-12 05:46:14 -04006490 req_fec = ", Requested FEC: None";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006491 fec = ", FEC: None";
6492 an = ", Autoneg: False";
6493
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006494 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006495 an = ", Autoneg: True";
6496
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006497 if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006498 I40E_AQ_CONFIG_FEC_KR_ENA)
6499 fec = ", FEC: CL74 FC-FEC/BASE-R";
Sudheer Mogilappagari3fded462017-07-14 09:10:18 -04006500 else if (pf->hw.phy.link_info.fec_info &
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006501 I40E_AQ_CONFIG_FEC_RS_ENA)
6502 fec = ", FEC: CL108 RS-FEC";
Mariusz Stachura68e49702017-07-12 05:46:14 -04006503
6504 /* 'CL108 RS-FEC' should be displayed when RS is requested, or
6505 * both RS and FC are requested
6506 */
6507 if (vsi->back->hw.phy.link_info.req_fec_info &
6508 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6509 if (vsi->back->hw.phy.link_info.req_fec_info &
6510 I40E_AQ_REQUEST_FEC_RS)
6511 req_fec = ", Requested FEC: CL108 RS-FEC";
6512 else
6513 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
6514 }
Henry Tieman3e03d7c2016-12-02 12:32:57 -08006515 }
6516
Mariusz Stachura68e49702017-07-12 05:46:14 -04006517 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
6518 speed, req_fec, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006519}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006520
6521/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006522 * i40e_up_complete - Finish the last steps of bringing up a connection
6523 * @vsi: the VSI being configured
6524 **/
6525static int i40e_up_complete(struct i40e_vsi *vsi)
6526{
6527 struct i40e_pf *pf = vsi->back;
6528 int err;
6529
6530 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6531 i40e_vsi_configure_msix(vsi);
6532 else
6533 i40e_configure_msi_and_legacy(vsi);
6534
6535 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006536 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006537 if (err)
6538 return err;
6539
Jacob Keller0da36b92017-04-19 09:25:55 -04006540 clear_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006541 i40e_napi_enable_all(vsi);
6542 i40e_vsi_enable_irq(vsi);
6543
6544 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6545 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00006546 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006547 netif_tx_start_all_queues(vsi->netdev);
6548 netif_carrier_on(vsi->netdev);
6549 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006550
6551 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006552 if (vsi->type == I40E_VSI_FDIR) {
6553 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08006554 pf->fd_add_err = 0;
6555 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00006556 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006557 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006558
6559 /* On the next run of the service_task, notify any clients of the new
6560 * opened netdev
6561 */
6562 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006563 i40e_service_event_schedule(pf);
6564
6565 return 0;
6566}
6567
6568/**
6569 * i40e_vsi_reinit_locked - Reset the VSI
6570 * @vsi: the VSI being configured
6571 *
6572 * Rebuild the ring structs after some configuration
6573 * has changed, e.g. MTU size.
6574 **/
6575static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6576{
6577 struct i40e_pf *pf = vsi->back;
6578
6579 WARN_ON(in_interrupt());
Jacob Keller0da36b92017-04-19 09:25:55 -04006580 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006581 usleep_range(1000, 2000);
6582 i40e_down(vsi);
6583
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006584 i40e_up(vsi);
Jacob Keller0da36b92017-04-19 09:25:55 -04006585 clear_bit(__I40E_CONFIG_BUSY, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006586}
6587
6588/**
6589 * i40e_up - Bring the connection back up after being down
6590 * @vsi: the VSI being configured
6591 **/
6592int i40e_up(struct i40e_vsi *vsi)
6593{
6594 int err;
6595
6596 err = i40e_vsi_configure(vsi);
6597 if (!err)
6598 err = i40e_up_complete(vsi);
6599
6600 return err;
6601}
6602
6603/**
6604 * i40e_down - Shutdown the connection processing
6605 * @vsi: the VSI being stopped
6606 **/
6607void i40e_down(struct i40e_vsi *vsi)
6608{
6609 int i;
6610
6611 /* It is assumed that the caller of this function
Jacob Kellerd19cb642017-04-21 13:38:05 -07006612 * sets the vsi->state __I40E_VSI_DOWN bit.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006613 */
6614 if (vsi->netdev) {
6615 netif_carrier_off(vsi->netdev);
6616 netif_tx_disable(vsi->netdev);
6617 }
6618 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07006619 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006620 i40e_napi_disable_all(vsi);
6621
6622 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00006623 i40e_clean_tx_ring(vsi->tx_rings[i]);
Björn Töpel74608d12017-05-24 07:55:35 +02006624 if (i40e_enabled_xdp_vsi(vsi))
6625 i40e_clean_tx_ring(vsi->xdp_rings[i]);
Alexander Duyck9f65e152013-09-28 06:00:58 +00006626 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006627 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07006628
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006629}
6630
6631/**
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006632 * i40e_validate_mqprio_qopt- validate queue mapping info
6633 * @vsi: the VSI being configured
6634 * @mqprio_qopt: queue parametrs
6635 **/
6636static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6637 struct tc_mqprio_qopt_offload *mqprio_qopt)
6638{
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006639 u64 sum_max_rate = 0;
Alan Brady6c32e0d2017-10-09 15:48:45 -07006640 u64 max_rate = 0;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006641 int i;
6642
6643 if (mqprio_qopt->qopt.offset[0] != 0 ||
6644 mqprio_qopt->qopt.num_tc < 1 ||
6645 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6646 return -EINVAL;
6647 for (i = 0; ; i++) {
6648 if (!mqprio_qopt->qopt.count[i])
6649 return -EINVAL;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006650 if (mqprio_qopt->min_rate[i]) {
6651 dev_err(&vsi->back->pdev->dev,
6652 "Invalid min tx rate (greater than 0) specified\n");
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006653 return -EINVAL;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006654 }
Alan Brady6c32e0d2017-10-09 15:48:45 -07006655 max_rate = mqprio_qopt->max_rate[i];
6656 do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6657 sum_max_rate += max_rate;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006658
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006659 if (i >= mqprio_qopt->qopt.num_tc - 1)
6660 break;
6661 if (mqprio_qopt->qopt.offset[i + 1] !=
6662 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
6663 return -EINVAL;
6664 }
6665 if (vsi->num_queue_pairs <
6666 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
6667 return -EINVAL;
6668 }
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006669 if (sum_max_rate > i40e_get_link_speed(vsi)) {
6670 dev_err(&vsi->back->pdev->dev,
6671 "Invalid max tx rate specified\n");
6672 return -EINVAL;
6673 }
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006674 return 0;
6675}
6676
6677/**
6678 * i40e_vsi_set_default_tc_config - set default values for tc configuration
6679 * @vsi: the VSI being configured
6680 **/
6681static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
6682{
6683 u16 qcount;
6684 int i;
6685
6686 /* Only TC0 is enabled */
6687 vsi->tc_config.numtc = 1;
6688 vsi->tc_config.enabled_tc = 1;
6689 qcount = min_t(int, vsi->alloc_queue_pairs,
6690 i40e_pf_get_max_q_per_tc(vsi->back));
6691 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6692 /* For the TC that is not enabled set the offset to to default
6693 * queue and allocate one queue for the given TC.
6694 */
6695 vsi->tc_config.tc_info[i].qoffset = 0;
6696 if (i == 0)
6697 vsi->tc_config.tc_info[i].qcount = qcount;
6698 else
6699 vsi->tc_config.tc_info[i].qcount = 1;
6700 vsi->tc_config.tc_info[i].netdev_tc = 0;
6701 }
6702}
6703
6704/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 * i40e_setup_tc - configure multiple traffic classes
6706 * @netdev: net device to configure
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006707 * @type_data: tc offload data
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006708 **/
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006709static int i40e_setup_tc(struct net_device *netdev, void *type_data)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006710{
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006711 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006712 struct i40e_netdev_priv *np = netdev_priv(netdev);
6713 struct i40e_vsi *vsi = np->vsi;
6714 struct i40e_pf *pf = vsi->back;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006715 u8 enabled_tc = 0, num_tc, hw;
6716 bool need_reset = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006717 int ret = -EINVAL;
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006718 u16 mode;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006719 int i;
6720
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006721 num_tc = mqprio_qopt->qopt.num_tc;
6722 hw = mqprio_qopt->qopt.hw;
6723 mode = mqprio_qopt->mode;
6724 if (!hw) {
6725 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6726 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
6727 goto config_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006728 }
6729
6730 /* Check if MFP enabled */
6731 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006732 netdev_info(netdev,
6733 "Configuring TC not supported in MFP mode\n");
6734 return ret;
6735 }
6736 switch (mode) {
6737 case TC_MQPRIO_MODE_DCB:
6738 pf->flags &= ~I40E_FLAG_TC_MQPRIO;
6739
6740 /* Check if DCB enabled to continue */
6741 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
6742 netdev_info(netdev,
6743 "DCB is not enabled for adapter\n");
6744 return ret;
6745 }
6746
6747 /* Check whether tc count is within enabled limit */
6748 if (num_tc > i40e_pf_get_num_tc(pf)) {
6749 netdev_info(netdev,
6750 "TC count greater than enabled on link for adapter\n");
6751 return ret;
6752 }
6753 break;
6754 case TC_MQPRIO_MODE_CHANNEL:
6755 if (pf->flags & I40E_FLAG_DCB_ENABLED) {
6756 netdev_info(netdev,
6757 "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
6758 return ret;
6759 }
6760 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
6761 return ret;
6762 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
6763 if (ret)
6764 return ret;
6765 memcpy(&vsi->mqprio_qopt, mqprio_qopt,
6766 sizeof(*mqprio_qopt));
6767 pf->flags |= I40E_FLAG_TC_MQPRIO;
6768 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6769 break;
6770 default:
6771 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006772 }
6773
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006774config_tc:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006775 /* Generate TC map for number of tc requested */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006776 for (i = 0; i < num_tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006777 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006778
6779 /* Requesting same TC configuration as already enabled */
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006780 if (enabled_tc == vsi->tc_config.enabled_tc &&
6781 mode != TC_MQPRIO_MODE_CHANNEL)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006782 return 0;
6783
6784 /* Quiesce VSI queues */
6785 i40e_quiesce_vsi(vsi);
6786
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006787 if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
6788 i40e_remove_queue_channels(vsi);
6789
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006790 /* Configure VSI for enabled TCs */
6791 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6792 if (ret) {
6793 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
6794 vsi->seid);
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006795 need_reset = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006796 goto exit;
6797 }
6798
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006799 if (pf->flags & I40E_FLAG_TC_MQPRIO) {
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006800 if (vsi->mqprio_qopt.max_rate[0]) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006801 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
6802
6803 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006804 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
6805 if (!ret) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07006806 u64 credits = max_tx_rate;
6807
6808 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006809 dev_dbg(&vsi->back->pdev->dev,
6810 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6811 max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07006812 credits,
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07006813 vsi->seid);
6814 } else {
6815 need_reset = true;
6816 goto exit;
6817 }
6818 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006819 ret = i40e_configure_queue_channels(vsi);
6820 if (ret) {
6821 netdev_info(netdev,
6822 "Failed configuring queue channels\n");
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006823 need_reset = true;
Amritha Nambiar8f88b302017-09-07 04:00:17 -07006824 goto exit;
6825 }
6826 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006827
6828exit:
Amritha Nambiara9ce82f2017-09-07 04:00:22 -07006829 /* Reset the configuration data to defaults, only TC0 is enabled */
6830 if (need_reset) {
6831 i40e_vsi_set_default_tc_config(vsi);
6832 need_reset = false;
6833 }
6834
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006835 /* Unquiesce VSI */
6836 i40e_unquiesce_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006837 return ret;
6838}
6839
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07006840/**
6841 * i40e_set_cld_element - sets cloud filter element data
6842 * @filter: cloud filter rule
6843 * @cld: ptr to cloud filter element data
6844 *
6845 * This is helper function to copy data into cloud filter element
6846 **/
6847static inline void
6848i40e_set_cld_element(struct i40e_cloud_filter *filter,
6849 struct i40e_aqc_cloud_filters_element_data *cld)
6850{
6851 int i, j;
6852 u32 ipa;
6853
6854 memset(cld, 0, sizeof(*cld));
6855 ether_addr_copy(cld->outer_mac, filter->dst_mac);
6856 ether_addr_copy(cld->inner_mac, filter->src_mac);
6857
6858 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
6859 return;
6860
6861 if (filter->n_proto == ETH_P_IPV6) {
6862#define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
6863 for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
6864 i++, j += 2) {
6865 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
6866 ipa = cpu_to_le32(ipa);
6867 memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
6868 }
6869 } else {
6870 ipa = be32_to_cpu(filter->dst_ipv4);
6871 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
6872 }
6873
6874 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
6875
6876 /* tenant_id is not supported by FW now, once the support is enabled
6877 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
6878 */
6879 if (filter->tenant_id)
6880 return;
6881}
6882
6883/**
6884 * i40e_add_del_cloud_filter - Add/del cloud filter
6885 * @vsi: pointer to VSI
6886 * @filter: cloud filter rule
6887 * @add: if true, add, if false, delete
6888 *
6889 * Add or delete a cloud filter for a specific flow spec.
6890 * Returns 0 if the filter were successfully added.
6891 **/
6892static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
6893 struct i40e_cloud_filter *filter, bool add)
6894{
6895 struct i40e_aqc_cloud_filters_element_data cld_filter;
6896 struct i40e_pf *pf = vsi->back;
6897 int ret;
6898 static const u16 flag_table[128] = {
6899 [I40E_CLOUD_FILTER_FLAGS_OMAC] =
6900 I40E_AQC_ADD_CLOUD_FILTER_OMAC,
6901 [I40E_CLOUD_FILTER_FLAGS_IMAC] =
6902 I40E_AQC_ADD_CLOUD_FILTER_IMAC,
6903 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
6904 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
6905 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
6906 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
6907 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
6908 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
6909 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
6910 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
6911 [I40E_CLOUD_FILTER_FLAGS_IIP] =
6912 I40E_AQC_ADD_CLOUD_FILTER_IIP,
6913 };
6914
6915 if (filter->flags >= ARRAY_SIZE(flag_table))
6916 return I40E_ERR_CONFIG;
6917
6918 /* copy element needed to add cloud filter from filter */
6919 i40e_set_cld_element(filter, &cld_filter);
6920
6921 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
6922 cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
6923 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
6924
6925 if (filter->n_proto == ETH_P_IPV6)
6926 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6927 I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
6928 else
6929 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
6930 I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
6931
6932 if (add)
6933 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
6934 &cld_filter, 1);
6935 else
6936 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
6937 &cld_filter, 1);
6938 if (ret)
6939 dev_dbg(&pf->pdev->dev,
6940 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
6941 add ? "add" : "delete", filter->dst_port, ret,
6942 pf->hw.aq.asq_last_status);
6943 else
6944 dev_info(&pf->pdev->dev,
6945 "%s cloud filter for VSI: %d\n",
6946 add ? "Added" : "Deleted", filter->seid);
6947 return ret;
6948}
6949
6950/**
6951 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
6952 * @vsi: pointer to VSI
6953 * @filter: cloud filter rule
6954 * @add: if true, add, if false, delete
6955 *
6956 * Add or delete a cloud filter for a specific flow spec using big buffer.
6957 * Returns 0 if the filter were successfully added.
6958 **/
6959static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
6960 struct i40e_cloud_filter *filter,
6961 bool add)
6962{
6963 struct i40e_aqc_cloud_filters_element_bb cld_filter;
6964 struct i40e_pf *pf = vsi->back;
6965 int ret;
6966
6967 /* Both (src/dst) valid mac_addr are not supported */
6968 if ((is_valid_ether_addr(filter->dst_mac) &&
6969 is_valid_ether_addr(filter->src_mac)) ||
6970 (is_multicast_ether_addr(filter->dst_mac) &&
6971 is_multicast_ether_addr(filter->src_mac)))
6972 return -EINVAL;
6973
6974 /* Make sure port is specified, otherwise bail out, for channel
6975 * specific cloud filter needs 'L4 port' to be non-zero
6976 */
6977 if (!filter->dst_port)
6978 return -EINVAL;
6979
6980 /* adding filter using src_port/src_ip is not supported at this stage */
6981 if (filter->src_port || filter->src_ipv4 ||
6982 !ipv6_addr_any(&filter->ip.v6.src_ip6))
6983 return -EINVAL;
6984
6985 /* copy element needed to add cloud filter from filter */
6986 i40e_set_cld_element(filter, &cld_filter.element);
6987
6988 if (is_valid_ether_addr(filter->dst_mac) ||
6989 is_valid_ether_addr(filter->src_mac) ||
6990 is_multicast_ether_addr(filter->dst_mac) ||
6991 is_multicast_ether_addr(filter->src_mac)) {
6992 /* MAC + IP : unsupported mode */
6993 if (filter->dst_ipv4)
6994 return -EINVAL;
6995
6996 /* since we validated that L4 port must be valid before
6997 * we get here, start with respective "flags" value
6998 * and update if vlan is present or not
6999 */
7000 cld_filter.element.flags =
7001 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7002
7003 if (filter->vlan_id) {
7004 cld_filter.element.flags =
7005 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7006 }
7007
7008 } else if (filter->dst_ipv4 ||
7009 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7010 cld_filter.element.flags =
7011 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7012 if (filter->n_proto == ETH_P_IPV6)
7013 cld_filter.element.flags |=
7014 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7015 else
7016 cld_filter.element.flags |=
7017 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7018 } else {
7019 dev_err(&pf->pdev->dev,
7020 "either mac or ip has to be valid for cloud filter\n");
7021 return -EINVAL;
7022 }
7023
7024 /* Now copy L4 port in Byte 6..7 in general fields */
7025 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7026 be16_to_cpu(filter->dst_port);
7027
7028 if (add) {
7029 /* Validate current device switch mode, change if necessary */
7030 ret = i40e_validate_and_set_switch_mode(vsi);
7031 if (ret) {
7032 dev_err(&pf->pdev->dev,
7033 "failed to set switch mode, ret %d\n",
7034 ret);
7035 return ret;
7036 }
7037
7038 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7039 &cld_filter, 1);
7040 } else {
7041 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7042 &cld_filter, 1);
7043 }
7044
7045 if (ret)
7046 dev_dbg(&pf->pdev->dev,
7047 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7048 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7049 else
7050 dev_info(&pf->pdev->dev,
7051 "%s cloud filter for VSI: %d, L4 port: %d\n",
7052 add ? "add" : "delete", filter->seid,
7053 ntohs(filter->dst_port));
7054 return ret;
7055}
7056
7057/**
7058 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7059 * @vsi: Pointer to VSI
7060 * @cls_flower: Pointer to struct tc_cls_flower_offload
7061 * @filter: Pointer to cloud filter structure
7062 *
7063 **/
7064static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7065 struct tc_cls_flower_offload *f,
7066 struct i40e_cloud_filter *filter)
7067{
7068 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7069 struct i40e_pf *pf = vsi->back;
7070 u8 field_flags = 0;
7071
7072 if (f->dissector->used_keys &
7073 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7074 BIT(FLOW_DISSECTOR_KEY_BASIC) |
7075 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7076 BIT(FLOW_DISSECTOR_KEY_VLAN) |
7077 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7078 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7079 BIT(FLOW_DISSECTOR_KEY_PORTS) |
7080 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7081 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7082 f->dissector->used_keys);
7083 return -EOPNOTSUPP;
7084 }
7085
7086 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7087 struct flow_dissector_key_keyid *key =
7088 skb_flow_dissector_target(f->dissector,
7089 FLOW_DISSECTOR_KEY_ENC_KEYID,
7090 f->key);
7091
7092 struct flow_dissector_key_keyid *mask =
7093 skb_flow_dissector_target(f->dissector,
7094 FLOW_DISSECTOR_KEY_ENC_KEYID,
7095 f->mask);
7096
7097 if (mask->keyid != 0)
7098 field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7099
7100 filter->tenant_id = be32_to_cpu(key->keyid);
7101 }
7102
7103 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
7104 struct flow_dissector_key_basic *key =
7105 skb_flow_dissector_target(f->dissector,
7106 FLOW_DISSECTOR_KEY_BASIC,
7107 f->key);
7108
7109 struct flow_dissector_key_basic *mask =
7110 skb_flow_dissector_target(f->dissector,
7111 FLOW_DISSECTOR_KEY_BASIC,
7112 f->mask);
7113
7114 n_proto_key = ntohs(key->n_proto);
7115 n_proto_mask = ntohs(mask->n_proto);
7116
7117 if (n_proto_key == ETH_P_ALL) {
7118 n_proto_key = 0;
7119 n_proto_mask = 0;
7120 }
7121 filter->n_proto = n_proto_key & n_proto_mask;
7122 filter->ip_proto = key->ip_proto;
7123 }
7124
7125 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7126 struct flow_dissector_key_eth_addrs *key =
7127 skb_flow_dissector_target(f->dissector,
7128 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7129 f->key);
7130
7131 struct flow_dissector_key_eth_addrs *mask =
7132 skb_flow_dissector_target(f->dissector,
7133 FLOW_DISSECTOR_KEY_ETH_ADDRS,
7134 f->mask);
7135
7136 /* use is_broadcast and is_zero to check for all 0xf or 0 */
7137 if (!is_zero_ether_addr(mask->dst)) {
7138 if (is_broadcast_ether_addr(mask->dst)) {
7139 field_flags |= I40E_CLOUD_FIELD_OMAC;
7140 } else {
7141 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7142 mask->dst);
7143 return I40E_ERR_CONFIG;
7144 }
7145 }
7146
7147 if (!is_zero_ether_addr(mask->src)) {
7148 if (is_broadcast_ether_addr(mask->src)) {
7149 field_flags |= I40E_CLOUD_FIELD_IMAC;
7150 } else {
7151 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7152 mask->src);
7153 return I40E_ERR_CONFIG;
7154 }
7155 }
7156 ether_addr_copy(filter->dst_mac, key->dst);
7157 ether_addr_copy(filter->src_mac, key->src);
7158 }
7159
7160 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
7161 struct flow_dissector_key_vlan *key =
7162 skb_flow_dissector_target(f->dissector,
7163 FLOW_DISSECTOR_KEY_VLAN,
7164 f->key);
7165 struct flow_dissector_key_vlan *mask =
7166 skb_flow_dissector_target(f->dissector,
7167 FLOW_DISSECTOR_KEY_VLAN,
7168 f->mask);
7169
7170 if (mask->vlan_id) {
7171 if (mask->vlan_id == VLAN_VID_MASK) {
7172 field_flags |= I40E_CLOUD_FIELD_IVLAN;
7173
7174 } else {
7175 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7176 mask->vlan_id);
7177 return I40E_ERR_CONFIG;
7178 }
7179 }
7180
7181 filter->vlan_id = cpu_to_be16(key->vlan_id);
7182 }
7183
7184 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
7185 struct flow_dissector_key_control *key =
7186 skb_flow_dissector_target(f->dissector,
7187 FLOW_DISSECTOR_KEY_CONTROL,
7188 f->key);
7189
7190 addr_type = key->addr_type;
7191 }
7192
7193 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
7194 struct flow_dissector_key_ipv4_addrs *key =
7195 skb_flow_dissector_target(f->dissector,
7196 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7197 f->key);
7198 struct flow_dissector_key_ipv4_addrs *mask =
7199 skb_flow_dissector_target(f->dissector,
7200 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
7201 f->mask);
7202
7203 if (mask->dst) {
7204 if (mask->dst == cpu_to_be32(0xffffffff)) {
7205 field_flags |= I40E_CLOUD_FIELD_IIP;
7206 } else {
7207 mask->dst = be32_to_cpu(mask->dst);
7208 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4\n",
7209 &mask->dst);
7210 return I40E_ERR_CONFIG;
7211 }
7212 }
7213
7214 if (mask->src) {
7215 if (mask->src == cpu_to_be32(0xffffffff)) {
7216 field_flags |= I40E_CLOUD_FIELD_IIP;
7217 } else {
7218 mask->src = be32_to_cpu(mask->src);
7219 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4\n",
7220 &mask->src);
7221 return I40E_ERR_CONFIG;
7222 }
7223 }
7224
7225 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
7226 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
7227 return I40E_ERR_CONFIG;
7228 }
7229 filter->dst_ipv4 = key->dst;
7230 filter->src_ipv4 = key->src;
7231 }
7232
7233 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
7234 struct flow_dissector_key_ipv6_addrs *key =
7235 skb_flow_dissector_target(f->dissector,
7236 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7237 f->key);
7238 struct flow_dissector_key_ipv6_addrs *mask =
7239 skb_flow_dissector_target(f->dissector,
7240 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
7241 f->mask);
7242
7243 /* src and dest IPV6 address should not be LOOPBACK
7244 * (0:0:0:0:0:0:0:1), which can be represented as ::1
7245 */
7246 if (ipv6_addr_loopback(&key->dst) ||
7247 ipv6_addr_loopback(&key->src)) {
7248 dev_err(&pf->pdev->dev,
7249 "Bad ipv6, addr is LOOPBACK\n");
7250 return I40E_ERR_CONFIG;
7251 }
7252 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
7253 field_flags |= I40E_CLOUD_FIELD_IIP;
7254
7255 memcpy(&filter->src_ipv6, &key->src.s6_addr32,
7256 sizeof(filter->src_ipv6));
7257 memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
7258 sizeof(filter->dst_ipv6));
7259 }
7260
7261 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
7262 struct flow_dissector_key_ports *key =
7263 skb_flow_dissector_target(f->dissector,
7264 FLOW_DISSECTOR_KEY_PORTS,
7265 f->key);
7266 struct flow_dissector_key_ports *mask =
7267 skb_flow_dissector_target(f->dissector,
7268 FLOW_DISSECTOR_KEY_PORTS,
7269 f->mask);
7270
7271 if (mask->src) {
7272 if (mask->src == cpu_to_be16(0xffff)) {
7273 field_flags |= I40E_CLOUD_FIELD_IIP;
7274 } else {
7275 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
7276 be16_to_cpu(mask->src));
7277 return I40E_ERR_CONFIG;
7278 }
7279 }
7280
7281 if (mask->dst) {
7282 if (mask->dst == cpu_to_be16(0xffff)) {
7283 field_flags |= I40E_CLOUD_FIELD_IIP;
7284 } else {
7285 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
7286 be16_to_cpu(mask->dst));
7287 return I40E_ERR_CONFIG;
7288 }
7289 }
7290
7291 filter->dst_port = key->dst;
7292 filter->src_port = key->src;
7293
7294 switch (filter->ip_proto) {
7295 case IPPROTO_TCP:
7296 case IPPROTO_UDP:
7297 break;
7298 default:
7299 dev_err(&pf->pdev->dev,
7300 "Only UDP and TCP transport are supported\n");
7301 return -EINVAL;
7302 }
7303 }
7304 filter->flags = field_flags;
7305 return 0;
7306}
7307
7308/**
7309 * i40e_handle_tclass: Forward to a traffic class on the device
7310 * @vsi: Pointer to VSI
7311 * @tc: traffic class index on the device
7312 * @filter: Pointer to cloud filter structure
7313 *
7314 **/
7315static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
7316 struct i40e_cloud_filter *filter)
7317{
7318 struct i40e_channel *ch, *ch_tmp;
7319
7320 /* direct to a traffic class on the same device */
7321 if (tc == 0) {
7322 filter->seid = vsi->seid;
7323 return 0;
7324 } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
7325 if (!filter->dst_port) {
7326 dev_err(&vsi->back->pdev->dev,
7327 "Specify destination port to direct to traffic class that is not default\n");
7328 return -EINVAL;
7329 }
7330 if (list_empty(&vsi->ch_list))
7331 return -EINVAL;
7332 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
7333 list) {
7334 if (ch->seid == vsi->tc_seid_map[tc])
7335 filter->seid = ch->seid;
7336 }
7337 return 0;
7338 }
7339 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
7340 return -EINVAL;
7341}
7342
7343/**
7344 * i40e_configure_clsflower - Configure tc flower filters
7345 * @vsi: Pointer to VSI
7346 * @cls_flower: Pointer to struct tc_cls_flower_offload
7347 *
7348 **/
7349static int i40e_configure_clsflower(struct i40e_vsi *vsi,
7350 struct tc_cls_flower_offload *cls_flower)
7351{
7352 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
7353 struct i40e_cloud_filter *filter = NULL;
7354 struct i40e_pf *pf = vsi->back;
7355 int err = 0;
7356
7357 if (tc < 0) {
7358 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
7359 return -EINVAL;
7360 }
7361
7362 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
7363 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
7364 return -EBUSY;
7365
7366 if (pf->fdir_pf_active_filters ||
7367 (!hlist_empty(&pf->fdir_filter_list))) {
7368 dev_err(&vsi->back->pdev->dev,
7369 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
7370 return -EINVAL;
7371 }
7372
7373 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
7374 dev_err(&vsi->back->pdev->dev,
7375 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
7376 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7377 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7378 }
7379
7380 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
7381 if (!filter)
7382 return -ENOMEM;
7383
7384 filter->cookie = cls_flower->cookie;
7385
7386 err = i40e_parse_cls_flower(vsi, cls_flower, filter);
7387 if (err < 0)
7388 goto err;
7389
7390 err = i40e_handle_tclass(vsi, tc, filter);
7391 if (err < 0)
7392 goto err;
7393
7394 /* Add cloud filter */
7395 if (filter->dst_port)
7396 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
7397 else
7398 err = i40e_add_del_cloud_filter(vsi, filter, true);
7399
7400 if (err) {
7401 dev_err(&pf->pdev->dev,
7402 "Failed to add cloud filter, err %s\n",
7403 i40e_stat_str(&pf->hw, err));
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007404 goto err;
7405 }
7406
7407 /* add filter to the ordered list */
7408 INIT_HLIST_NODE(&filter->cloud_node);
7409
7410 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
7411
7412 pf->num_cloud_filters++;
7413
7414 return err;
7415err:
7416 kfree(filter);
7417 return err;
7418}
7419
7420/**
7421 * i40e_find_cloud_filter - Find the could filter in the list
7422 * @vsi: Pointer to VSI
7423 * @cookie: filter specific cookie
7424 *
7425 **/
7426static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
7427 unsigned long *cookie)
7428{
7429 struct i40e_cloud_filter *filter = NULL;
7430 struct hlist_node *node2;
7431
7432 hlist_for_each_entry_safe(filter, node2,
7433 &vsi->back->cloud_filter_list, cloud_node)
7434 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
7435 return filter;
7436 return NULL;
7437}
7438
7439/**
7440 * i40e_delete_clsflower - Remove tc flower filters
7441 * @vsi: Pointer to VSI
7442 * @cls_flower: Pointer to struct tc_cls_flower_offload
7443 *
7444 **/
7445static int i40e_delete_clsflower(struct i40e_vsi *vsi,
7446 struct tc_cls_flower_offload *cls_flower)
7447{
7448 struct i40e_cloud_filter *filter = NULL;
7449 struct i40e_pf *pf = vsi->back;
7450 int err = 0;
7451
7452 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
7453
7454 if (!filter)
7455 return -EINVAL;
7456
7457 hash_del(&filter->cloud_node);
7458
7459 if (filter->dst_port)
7460 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
7461 else
7462 err = i40e_add_del_cloud_filter(vsi, filter, false);
7463
7464 kfree(filter);
7465 if (err) {
7466 dev_err(&pf->pdev->dev,
7467 "Failed to delete cloud filter, err %s\n",
7468 i40e_stat_str(&pf->hw, err));
7469 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
7470 }
7471
7472 pf->num_cloud_filters--;
7473 if (!pf->num_cloud_filters)
7474 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7475 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7476 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7477 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7478 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7479 }
7480 return 0;
7481}
7482
7483/**
7484 * i40e_setup_tc_cls_flower - flower classifier offloads
7485 * @netdev: net device to configure
7486 * @type_data: offload data
7487 **/
7488static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
7489 struct tc_cls_flower_offload *cls_flower)
7490{
7491 struct i40e_vsi *vsi = np->vsi;
7492
7493 if (cls_flower->common.chain_index)
7494 return -EOPNOTSUPP;
7495
7496 switch (cls_flower->command) {
7497 case TC_CLSFLOWER_REPLACE:
7498 return i40e_configure_clsflower(vsi, cls_flower);
7499 case TC_CLSFLOWER_DESTROY:
7500 return i40e_delete_clsflower(vsi, cls_flower);
7501 case TC_CLSFLOWER_STATS:
7502 return -EOPNOTSUPP;
7503 default:
7504 return -EINVAL;
7505 }
7506}
7507
7508static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7509 void *cb_priv)
7510{
7511 struct i40e_netdev_priv *np = cb_priv;
7512
7513 switch (type) {
7514 case TC_SETUP_CLSFLOWER:
7515 return i40e_setup_tc_cls_flower(np, type_data);
7516
7517 default:
7518 return -EOPNOTSUPP;
7519 }
7520}
7521
7522static int i40e_setup_tc_block(struct net_device *dev,
7523 struct tc_block_offload *f)
7524{
7525 struct i40e_netdev_priv *np = netdev_priv(dev);
7526
7527 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7528 return -EOPNOTSUPP;
7529
7530 switch (f->command) {
7531 case TC_BLOCK_BIND:
7532 return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
7533 np, np);
7534 case TC_BLOCK_UNBIND:
7535 tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
7536 return 0;
7537 default:
7538 return -EOPNOTSUPP;
7539 }
7540}
7541
Jiri Pirko2572ac52017-08-07 10:15:17 +02007542static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02007543 void *type_data)
John Fastabende4c67342016-02-16 21:16:15 -08007544{
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007545 switch (type) {
Nogah Frankel575ed7d2017-11-06 07:23:42 +01007546 case TC_SETUP_QDISC_MQPRIO:
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007547 return i40e_setup_tc(netdev, type_data);
7548 case TC_SETUP_BLOCK:
7549 return i40e_setup_tc_block(netdev, type_data);
7550 default:
Jiri Pirko38cf0422017-08-07 10:15:31 +02007551 return -EOPNOTSUPP;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007552 }
John Fastabende4c67342016-02-16 21:16:15 -08007553}
7554
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007555/**
7556 * i40e_open - Called when a network interface is made active
7557 * @netdev: network interface device structure
7558 *
7559 * The open entry point is called when a network interface is made
7560 * active by the system (IFF_UP). At this point all resources needed
7561 * for transmit and receive operations are allocated, the interrupt
7562 * handler is registered with the OS, the netdev watchdog subtask is
7563 * enabled, and the stack is notified that the interface is ready.
7564 *
7565 * Returns 0 on success, negative value on failure
7566 **/
Vasu Dev38e00432014-08-01 13:27:03 -07007567int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007568{
7569 struct i40e_netdev_priv *np = netdev_priv(netdev);
7570 struct i40e_vsi *vsi = np->vsi;
7571 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007572 int err;
7573
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007574 /* disallow open during test or if eeprom is broken */
Jacob Keller0da36b92017-04-19 09:25:55 -04007575 if (test_bit(__I40E_TESTING, pf->state) ||
7576 test_bit(__I40E_BAD_EEPROM, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577 return -EBUSY;
7578
7579 netif_carrier_off(netdev);
7580
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007581 err = i40e_vsi_open(vsi);
7582 if (err)
7583 return err;
7584
Jesse Brandeburg059dab62014-04-01 09:07:20 +00007585 /* configure global TSO hardware offload settings */
7586 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
7587 TCP_FLAG_FIN) >> 16);
7588 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
7589 TCP_FLAG_FIN |
7590 TCP_FLAG_CWR) >> 16);
7591 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
7592
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07007593 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007594
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007595 return 0;
7596}
7597
7598/**
7599 * i40e_vsi_open -
7600 * @vsi: the VSI to open
7601 *
7602 * Finish initialization of the VSI.
7603 *
7604 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04007605 *
7606 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007607 **/
7608int i40e_vsi_open(struct i40e_vsi *vsi)
7609{
7610 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007611 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007612 int err;
7613
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007614 /* allocate descriptors */
7615 err = i40e_vsi_setup_tx_resources(vsi);
7616 if (err)
7617 goto err_setup_tx;
7618 err = i40e_vsi_setup_rx_resources(vsi);
7619 if (err)
7620 goto err_setup_rx;
7621
7622 err = i40e_vsi_configure(vsi);
7623 if (err)
7624 goto err_setup_rx;
7625
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00007626 if (vsi->netdev) {
7627 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7628 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
7629 err = i40e_vsi_request_irq(vsi, int_name);
7630 if (err)
7631 goto err_setup_rx;
7632
7633 /* Notify the stack of the actual queue counts. */
7634 err = netif_set_real_num_tx_queues(vsi->netdev,
7635 vsi->num_queue_pairs);
7636 if (err)
7637 goto err_set_queues;
7638
7639 err = netif_set_real_num_rx_queues(vsi->netdev,
7640 vsi->num_queue_pairs);
7641 if (err)
7642 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007643
7644 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00007645 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00007646 dev_driver_string(&pf->pdev->dev),
7647 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00007648 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00007649
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00007650 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00007651 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00007652 goto err_setup_rx;
7653 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00007654
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007655 err = i40e_up_complete(vsi);
7656 if (err)
7657 goto err_up_complete;
7658
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007659 return 0;
7660
7661err_up_complete:
7662 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00007663err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007664 i40e_vsi_free_irq(vsi);
7665err_setup_rx:
7666 i40e_vsi_free_rx_resources(vsi);
7667err_setup_tx:
7668 i40e_vsi_free_tx_resources(vsi);
7669 if (vsi == pf->vsi[pf->lan_vsi])
Amritha Nambiarff424182017-09-07 04:00:11 -07007670 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007671
7672 return err;
7673}
7674
7675/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007676 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007677 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007678 *
7679 * This function destroys the hlist where all the Flow Director
7680 * filters were saved.
7681 **/
7682static void i40e_fdir_filter_exit(struct i40e_pf *pf)
7683{
7684 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08007685 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007686 struct hlist_node *node2;
7687
7688 hlist_for_each_entry_safe(filter, node2,
7689 &pf->fdir_filter_list, fdir_node) {
7690 hlist_del(&filter->fdir_node);
7691 kfree(filter);
7692 }
Jacob Keller097dbf52017-02-06 14:38:46 -08007693
Jacob Keller0e588de2017-02-06 14:38:50 -08007694 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
7695 list_del(&pit_entry->list);
7696 kfree(pit_entry);
7697 }
7698 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
7699
7700 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
7701 list_del(&pit_entry->list);
7702 kfree(pit_entry);
7703 }
7704 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
7705
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007706 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08007707 pf->fd_tcp4_filter_cnt = 0;
7708 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08007709 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08007710 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08007711
7712 /* Reprogram the default input set for TCP/IPv4 */
7713 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7714 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7715 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7716
7717 /* Reprogram the default input set for UDP/IPv4 */
7718 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7719 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7720 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7721
7722 /* Reprogram the default input set for SCTP/IPv4 */
7723 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7724 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
7725 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
7726
7727 /* Reprogram the default input set for Other/IPv4 */
7728 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7729 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00007730}
7731
7732/**
Amritha Nambiaraaf66502017-10-27 02:35:56 -07007733 * i40e_cloud_filter_exit - Cleans up the cloud filters
7734 * @pf: Pointer to PF
7735 *
7736 * This function destroys the hlist where all the cloud filters
7737 * were saved.
7738 **/
7739static void i40e_cloud_filter_exit(struct i40e_pf *pf)
7740{
7741 struct i40e_cloud_filter *cfilter;
7742 struct hlist_node *node;
7743
7744 hlist_for_each_entry_safe(cfilter, node,
7745 &pf->cloud_filter_list, cloud_node) {
7746 hlist_del(&cfilter->cloud_node);
7747 kfree(cfilter);
7748 }
7749 pf->num_cloud_filters = 0;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07007750
7751 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
7752 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
7753 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
7754 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
7755 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
7756 }
Amritha Nambiaraaf66502017-10-27 02:35:56 -07007757}
7758
7759/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760 * i40e_close - Disables a network interface
7761 * @netdev: network interface device structure
7762 *
7763 * The close entry point is called when an interface is de-activated
7764 * by the OS. The hardware is still under the driver's control, but
7765 * this netdev interface is disabled.
7766 *
7767 * Returns 0, this is not allowed to fail
7768 **/
Vasu Dev38e00432014-08-01 13:27:03 -07007769int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007770{
7771 struct i40e_netdev_priv *np = netdev_priv(netdev);
7772 struct i40e_vsi *vsi = np->vsi;
7773
Shannon Nelson90ef8d42014-03-14 07:32:26 +00007774 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007775
7776 return 0;
7777}
7778
7779/**
7780 * i40e_do_reset - Start a PF or Core Reset sequence
7781 * @pf: board private structure
7782 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04007783 * @lock_acquired: indicates whether or not the lock has been acquired
7784 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007785 *
7786 * The essential difference in resets is that the PF Reset
7787 * doesn't clear the packet buffers, doesn't reset the PE
7788 * firmware, and doesn't bother the other PFs on the chip.
7789 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007790void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007791{
7792 u32 val;
7793
7794 WARN_ON(in_interrupt());
7795
Mitch Williams263fc482014-04-23 04:50:11 +00007796
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007797 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007798 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007799
7800 /* Request a Global Reset
7801 *
7802 * This will start the chip's countdown to the actual full
7803 * chip reset event, and a warning interrupt to be sent
7804 * to all PFs, including the requestor. Our handler
7805 * for the warning interrupt will deal with the shutdown
7806 * and recovery of the switch setup.
7807 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007808 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007809 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7810 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
7811 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7812
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007813 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007814
7815 /* Request a Core Reset
7816 *
7817 * Same as Global Reset, except does *not* include the MAC/PHY
7818 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007819 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007820 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7821 val |= I40E_GLGEN_RTRIG_CORER_MASK;
7822 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
7823 i40e_flush(&pf->hw);
7824
Amritha Nambiarff424182017-09-07 04:00:11 -07007825 } else if (reset_flags & I40E_PF_RESET_FLAG) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007826
7827 /* Request a PF Reset
7828 *
7829 * Resets only the PF-specific registers
7830 *
7831 * This goes directly to the tear-down and rebuild of
7832 * the switch, since we need to do all the recovery as
7833 * for the Core Reset.
7834 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007835 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04007836 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007838 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007839 int v;
7840
7841 /* Find the VSI(s) that requested a re-init */
7842 dev_info(&pf->pdev->dev,
7843 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00007844 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007845 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007846
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007847 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07007848 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04007849 vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007850 i40e_vsi_reinit_locked(pf->vsi[v]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007851 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04007852 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007853 int v;
7854
7855 /* Find the VSI(s) that needs to be brought down */
7856 dev_info(&pf->pdev->dev, "VSI down requested\n");
7857 for (v = 0; v < pf->num_alloc_vsi; v++) {
7858 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04007859
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007860 if (vsi != NULL &&
Jacob Kellerd19cb642017-04-21 13:38:05 -07007861 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
Jacob Keller0da36b92017-04-19 09:25:55 -04007862 vsi->state)) {
7863 set_bit(__I40E_VSI_DOWN, vsi->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007864 i40e_down(vsi);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00007865 }
7866 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007867 } else {
7868 dev_info(&pf->pdev->dev,
7869 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007870 }
7871}
7872
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007873#ifdef CONFIG_I40E_DCB
7874/**
7875 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
7876 * @pf: board private structure
7877 * @old_cfg: current DCB config
7878 * @new_cfg: new DCB config
7879 **/
7880bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
7881 struct i40e_dcbx_config *old_cfg,
7882 struct i40e_dcbx_config *new_cfg)
7883{
7884 bool need_reconfig = false;
7885
7886 /* Check if ETS configuration has changed */
7887 if (memcmp(&new_cfg->etscfg,
7888 &old_cfg->etscfg,
7889 sizeof(new_cfg->etscfg))) {
7890 /* If Priority Table has changed reconfig is needed */
7891 if (memcmp(&new_cfg->etscfg.prioritytable,
7892 &old_cfg->etscfg.prioritytable,
7893 sizeof(new_cfg->etscfg.prioritytable))) {
7894 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007895 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007896 }
7897
7898 if (memcmp(&new_cfg->etscfg.tcbwtable,
7899 &old_cfg->etscfg.tcbwtable,
7900 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007901 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007902
7903 if (memcmp(&new_cfg->etscfg.tsatable,
7904 &old_cfg->etscfg.tsatable,
7905 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007906 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007907 }
7908
7909 /* Check if PFC configuration has changed */
7910 if (memcmp(&new_cfg->pfc,
7911 &old_cfg->pfc,
7912 sizeof(new_cfg->pfc))) {
7913 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007914 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007915 }
7916
7917 /* Check if APP Table has changed */
7918 if (memcmp(&new_cfg->app,
7919 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05007920 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007921 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007922 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05007923 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007924
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007925 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007926 return need_reconfig;
7927}
7928
7929/**
7930 * i40e_handle_lldp_event - Handle LLDP Change MIB event
7931 * @pf: board private structure
7932 * @e: event info posted on ARQ
7933 **/
7934static int i40e_handle_lldp_event(struct i40e_pf *pf,
7935 struct i40e_arq_event_info *e)
7936{
7937 struct i40e_aqc_lldp_get_mib *mib =
7938 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
7939 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007940 struct i40e_dcbx_config tmp_dcbx_cfg;
7941 bool need_reconfig = false;
7942 int ret = 0;
7943 u8 type;
7944
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007945 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07007946 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007947 return ret;
7948
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007949 /* Ignore if event is not for Nearest Bridge */
7950 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
7951 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007952 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007953 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
7954 return ret;
7955
7956 /* Check MIB Type and return if event for Remote MIB update */
7957 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007958 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04007959 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007960 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
7961 /* Update the remote cached instance and return */
7962 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
7963 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
7964 &hw->remote_dcbx_config);
7965 goto exit;
7966 }
7967
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007968 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07007969 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007970
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007971 /* Reset the old DCBx configuration data */
7972 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00007973 /* Get updated DCBX data from firmware */
7974 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007975 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007976 dev_info(&pf->pdev->dev,
7977 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
7978 i40e_stat_str(&pf->hw, ret),
7979 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007980 goto exit;
7981 }
7982
7983 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007984 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
7985 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007986 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007987 goto exit;
7988 }
7989
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007990 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
7991 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007992
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007993 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007994
7995 if (!need_reconfig)
7996 goto exit;
7997
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007998 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00007999 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008000 pf->flags |= I40E_FLAG_DCB_ENABLED;
8001 else
8002 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8003
Jacob Keller0da36b92017-04-19 09:25:55 -04008004 set_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008005 /* Reconfiguration needed quiesce all VSIs */
8006 i40e_pf_quiesce_all_vsi(pf);
8007
8008 /* Changes in configuration update VEB/VSI */
8009 i40e_dcb_reconfigure(pf);
8010
Neerav Parikh2fd75f32014-11-12 00:18:20 +00008011 ret = i40e_resume_port_tx(pf);
8012
Jacob Keller0da36b92017-04-19 09:25:55 -04008013 clear_bit(__I40E_PORT_SUSPENDED, pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00008014 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00008015 if (ret)
8016 goto exit;
8017
Neerav Parikh3fe06f42016-02-17 16:12:15 -08008018 /* Wait for the PF's queues to be disabled */
8019 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00008020 if (ret) {
8021 /* Schedule PF reset to recover */
Jacob Keller0da36b92017-04-19 09:25:55 -04008022 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Parikh, Neerav11e47702015-02-21 06:43:55 +00008023 i40e_service_event_schedule(pf);
8024 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00008025 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08008026 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
8027 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00008028 }
8029
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008030exit:
8031 return ret;
8032}
8033#endif /* CONFIG_I40E_DCB */
8034
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008035/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00008036 * i40e_do_reset_safe - Protected reset path for userland calls.
8037 * @pf: board private structure
8038 * @reset_flags: which reset is requested
8039 *
8040 **/
8041void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8042{
8043 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04008044 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00008045 rtnl_unlock();
8046}
8047
8048/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008049 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8050 * @pf: board private structure
8051 * @e: event info posted on ARQ
8052 *
8053 * Handler for LAN Queue Overflow Event generated by the firmware for PF
8054 * and VF queues
8055 **/
8056static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8057 struct i40e_arq_event_info *e)
8058{
8059 struct i40e_aqc_lan_overflow *data =
8060 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8061 u32 queue = le32_to_cpu(data->prtdcb_rupto);
8062 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8063 struct i40e_hw *hw = &pf->hw;
8064 struct i40e_vf *vf;
8065 u16 vf_id;
8066
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008067 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8068 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008069
8070 /* Queue belongs to VF, find the VF and issue VF reset */
8071 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8072 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8073 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8074 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8075 vf_id -= hw->func_caps.vf_base_id;
8076 vf = &pf->vf[vf_id];
8077 i40e_vc_notify_vf_reset(vf);
8078 /* Allow VF to process pending reset notification */
8079 msleep(20);
8080 i40e_reset_vf(vf, false);
8081 }
8082}
8083
8084/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008085 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8086 * @pf: board private structure
8087 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008088u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008089{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008090 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008091
8092 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8093 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8094 return fcnt_prog;
8095}
8096
8097/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008098 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008099 * @pf: board private structure
8100 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008101u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008102{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008103 u32 val, fcnt_prog;
8104
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008105 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8106 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8107 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8108 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8109 return fcnt_prog;
8110}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008111
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008112/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008113 * i40e_get_global_fd_count - Get total FD filters programmed on device
8114 * @pf: board private structure
8115 **/
8116u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8117{
8118 u32 val, fcnt_prog;
8119
8120 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8121 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8122 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8123 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8124 return fcnt_prog;
8125}
8126
8127/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008128 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
8129 * @pf: board private structure
8130 **/
8131void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
8132{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008133 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008134 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008135 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008136
Jacob Keller0da36b92017-04-19 09:25:55 -04008137 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008138 return;
8139
Jacob Keller47994c12017-04-19 09:25:57 -04008140 /* Check if we have enough room to re-enable FDir SB capability. */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008141 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00008142 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008143 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
8144 (pf->fd_add_err == 0) ||
8145 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Jacob Keller47994c12017-04-19 09:25:57 -04008146 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
8147 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
8148 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8149 (I40E_DEBUG_FD & pf->hw.debug_mask))
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008150 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 +00008151 }
8152 }
Jacob Kellera3417d22016-09-06 18:05:10 -07008153
Jacob Keller47994c12017-04-19 09:25:57 -04008154 /* We should wait for even more space before re-enabling ATR.
8155 * Additionally, we cannot enable ATR as long as we still have TCP SB
8156 * rules active.
Jacob Kellera3417d22016-09-06 18:05:10 -07008157 */
Jacob Keller47994c12017-04-19 09:25:57 -04008158 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
8159 (pf->fd_tcp4_filter_cnt == 0)) {
8160 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
8161 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
8162 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8163 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Kellera3417d22016-09-06 18:05:10 -07008164 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 +00008165 }
8166 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008167
8168 /* if hw had a problem adding a filter, delete it */
8169 if (pf->fd_inv > 0) {
8170 hlist_for_each_entry_safe(filter, node,
8171 &pf->fdir_filter_list, fdir_node) {
8172 if (filter->fd_id == pf->fd_inv) {
8173 hlist_del(&filter->fdir_node);
8174 kfree(filter);
8175 pf->fdir_pf_active_filters--;
Filip Sadowski013df592017-08-29 05:32:38 -04008176 pf->fd_inv = 0;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04008177 }
8178 }
8179 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008180}
8181
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008182#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008183#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008184/**
8185 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
8186 * @pf: board private structure
8187 **/
8188static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
8189{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008190 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008191 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008192 bool disable_atr = false;
8193 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008194 int reg;
8195
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008196 if (!time_after(jiffies, pf->fd_flush_timestamp +
8197 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
8198 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008199
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008200 /* If the flush is happening too quick and we have mostly SB rules we
8201 * should not re-enable ATR for some time.
8202 */
8203 min_flush_time = pf->fd_flush_timestamp +
8204 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
8205 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008206
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008207 if (!(time_after(jiffies, min_flush_time)) &&
8208 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
8209 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8210 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
8211 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008212 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008213
8214 pf->fd_flush_timestamp = jiffies;
Jacob Keller47994c12017-04-19 09:25:57 -04008215 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008216 /* flush all filters */
8217 wr32(&pf->hw, I40E_PFQF_CTL_1,
8218 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
8219 i40e_flush(&pf->hw);
8220 pf->fd_flush_cnt++;
8221 pf->fd_add_err = 0;
8222 do {
8223 /* Check FD flush status every 5-6msec */
8224 usleep_range(5000, 6000);
8225 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
8226 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
8227 break;
8228 } while (flush_wait_retry--);
8229 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
8230 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
8231 } else {
8232 /* replay sideband filters */
8233 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08008234 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Jacob Keller47994c12017-04-19 09:25:57 -04008235 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
Jacob Keller0da36b92017-04-19 09:25:55 -04008236 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04008237 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8238 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
8239 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008240}
8241
8242/**
8243 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
8244 * @pf: board private structure
8245 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00008246u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008247{
8248 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
8249}
8250
8251/* We can see up to 256 filter programming desc in transit if the filters are
8252 * being applied really fast; before we see the first
8253 * filter miss error on Rx queue 0. Accumulating enough error messages before
8254 * reacting will make sure we don't cause flush too often.
8255 */
8256#define I40E_MAX_FD_PROGRAM_ERROR 256
8257
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008258/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008259 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
8260 * @pf: board private structure
8261 **/
8262static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
8263{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008265 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008266 if (test_bit(__I40E_DOWN, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008267 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008268
Jacob Keller0da36b92017-04-19 09:25:55 -04008269 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008270 i40e_fdir_flush_and_replay(pf);
8271
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00008272 i40e_fdir_check_and_reenable(pf);
8273
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008274}
8275
8276/**
8277 * i40e_vsi_link_event - notify VSI of a link event
8278 * @vsi: vsi to be notified
8279 * @link_up: link up or down
8280 **/
8281static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
8282{
Jacob Keller0da36b92017-04-19 09:25:55 -04008283 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008284 return;
8285
8286 switch (vsi->type) {
8287 case I40E_VSI_MAIN:
8288 if (!vsi->netdev || !vsi->netdev_registered)
8289 break;
8290
8291 if (link_up) {
8292 netif_carrier_on(vsi->netdev);
8293 netif_tx_wake_all_queues(vsi->netdev);
8294 } else {
8295 netif_carrier_off(vsi->netdev);
8296 netif_tx_stop_all_queues(vsi->netdev);
8297 }
8298 break;
8299
8300 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008301 case I40E_VSI_VMDQ2:
8302 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008303 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008304 case I40E_VSI_MIRROR:
8305 default:
8306 /* there is no notification for other VSIs */
8307 break;
8308 }
8309}
8310
8311/**
8312 * i40e_veb_link_event - notify elements on the veb of a link event
8313 * @veb: veb to be notified
8314 * @link_up: link up or down
8315 **/
8316static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
8317{
8318 struct i40e_pf *pf;
8319 int i;
8320
8321 if (!veb || !veb->pf)
8322 return;
8323 pf = veb->pf;
8324
8325 /* depth first... */
8326 for (i = 0; i < I40E_MAX_VEB; i++)
8327 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
8328 i40e_veb_link_event(pf->veb[i], link_up);
8329
8330 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00008331 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008332 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
8333 i40e_vsi_link_event(pf->vsi[i], link_up);
8334}
8335
8336/**
8337 * i40e_link_event - Update netif_carrier status
8338 * @pf: board private structure
8339 **/
8340static void i40e_link_event(struct i40e_pf *pf)
8341{
Mitch Williams320684c2014-10-17 03:14:43 +00008342 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00008343 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04008344 i40e_status status;
8345 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008346
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04008347 /* save off old link status information */
8348 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
8349
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008350 /* set this to force the get_link_status call to refresh state */
8351 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008352
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008353 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04008354
8355 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08008356
8357 /* On success, disable temp link polling */
8358 if (status == I40E_SUCCESS) {
8359 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
8360 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
8361 } else {
8362 /* Enable link polling temporarily until i40e_get_link_status
8363 * returns I40E_SUCCESS
8364 */
8365 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04008366 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
8367 status);
8368 return;
8369 }
8370
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00008371 old_link_speed = pf->hw.phy.link_info_old.link_speed;
8372 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008373
8374 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00008375 new_link_speed == old_link_speed &&
Jacob Keller0da36b92017-04-19 09:25:55 -04008376 (test_bit(__I40E_VSI_DOWN, vsi->state) ||
Mitch Williams320684c2014-10-17 03:14:43 +00008377 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008378 return;
Mitch Williams320684c2014-10-17 03:14:43 +00008379
Sudheer Mogilappagari9a034492017-07-14 09:10:16 -04008380 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008381
8382 /* Notify the base of the switch tree connected to
8383 * the link. Floating VEBs are not notified.
8384 */
8385 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
8386 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
8387 else
Mitch Williams320684c2014-10-17 03:14:43 +00008388 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008389
8390 if (pf->vf)
8391 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008392
8393 if (pf->flags & I40E_FLAG_PTP)
8394 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008395}
8396
8397/**
Shannon Nelson21536712014-10-25 10:35:25 +00008398 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008399 * @pf: board private structure
8400 **/
8401static void i40e_watchdog_subtask(struct i40e_pf *pf)
8402{
8403 int i;
8404
8405 /* if interface is down do nothing */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008406 if (test_bit(__I40E_DOWN, pf->state) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04008407 test_bit(__I40E_CONFIG_BUSY, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008408 return;
8409
Shannon Nelson21536712014-10-25 10:35:25 +00008410 /* make sure we don't do these things too often */
8411 if (time_before(jiffies, (pf->service_timer_previous +
8412 pf->service_timer_period)))
8413 return;
8414 pf->service_timer_previous = jiffies;
8415
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08008416 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
8417 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04008418 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00008419
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008420 /* Update the stats for active netdevs so the network stack
8421 * can look at updated numbers whenever it cares to
8422 */
Mitch Williams505682c2014-05-20 08:01:37 +00008423 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008424 if (pf->vsi[i] && pf->vsi[i]->netdev)
8425 i40e_update_stats(pf->vsi[i]);
8426
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008427 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
8428 /* Update the stats for the active switching components */
8429 for (i = 0; i < I40E_MAX_VEB; i++)
8430 if (pf->veb[i])
8431 i40e_update_veb_stats(pf->veb[i]);
8432 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008433
Jacob Keller61189552017-05-03 10:29:01 -07008434 i40e_ptp_rx_hang(pf);
Jacob Keller0bc07062017-05-03 10:29:02 -07008435 i40e_ptp_tx_hang(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008436}
8437
8438/**
8439 * i40e_reset_subtask - Set up for resetting the device and driver
8440 * @pf: board private structure
8441 **/
8442static void i40e_reset_subtask(struct i40e_pf *pf)
8443{
8444 u32 reset_flags = 0;
8445
Jacob Keller0da36b92017-04-19 09:25:55 -04008446 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008447 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008448 clear_bit(__I40E_REINIT_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008449 }
Jacob Keller0da36b92017-04-19 09:25:55 -04008450 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008451 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008452 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008453 }
Jacob Keller0da36b92017-04-19 09:25:55 -04008454 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008455 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008456 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008457 }
Jacob Keller0da36b92017-04-19 09:25:55 -04008458 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08008459 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jacob Keller0da36b92017-04-19 09:25:55 -04008460 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008461 }
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008462 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
8463 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
8464 clear_bit(__I40E_DOWN_REQUESTED, pf->state);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00008465 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008466
8467 /* If there's a recovery already waiting, it takes
8468 * precedence before starting a new reset sequence.
8469 */
Jacob Keller0da36b92017-04-19 09:25:55 -04008470 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04008471 i40e_prep_for_reset(pf, false);
8472 i40e_reset(pf);
8473 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008474 }
8475
8476 /* If we're already down or resetting, just bail */
8477 if (reset_flags &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03008478 !test_bit(__I40E_DOWN, pf->state) &&
Jacob Keller0da36b92017-04-19 09:25:55 -04008479 !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
Jacob Kellerdfc4ff62017-06-07 05:43:13 -04008480 i40e_do_reset(pf, reset_flags, false);
Maciej Sosin373149f2017-04-05 07:50:55 -04008481 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008482}
8483
8484/**
8485 * i40e_handle_link_event - Handle link event
8486 * @pf: board private structure
8487 * @e: event info posted on ARQ
8488 **/
8489static void i40e_handle_link_event(struct i40e_pf *pf,
8490 struct i40e_arq_event_info *e)
8491{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008492 struct i40e_aqc_get_link_status *status =
8493 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008494
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00008495 /* Do a new status request to re-enable LSE reporting
8496 * and load new status information into the hw struct
8497 * This completely ignores any state information
8498 * in the ARQ event info, instead choosing to always
8499 * issue the AQ update link status command.
8500 */
8501 i40e_link_event(pf);
8502
Filip Sadowski9a858172017-08-29 05:32:37 -04008503 /* Check if module meets thermal requirements */
8504 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00008505 dev_err(&pf->pdev->dev,
Filip Sadowski9a858172017-08-29 05:32:37 -04008506 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
8507 dev_err(&pf->pdev->dev,
8508 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8509 } else {
8510 /* check for unqualified module, if link is down, suppress
8511 * the message if link was forced to be down.
8512 */
8513 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
8514 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
8515 (!(status->link_info & I40E_AQ_LINK_UP)) &&
8516 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
8517 dev_err(&pf->pdev->dev,
8518 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
8519 dev_err(&pf->pdev->dev,
8520 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
8521 }
8522 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008523}
8524
8525/**
8526 * i40e_clean_adminq_subtask - Clean the AdminQ rings
8527 * @pf: board private structure
8528 **/
8529static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
8530{
8531 struct i40e_arq_event_info event;
8532 struct i40e_hw *hw = &pf->hw;
8533 u16 pending, i = 0;
8534 i40e_status ret;
8535 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00008536 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008537 u32 val;
8538
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008539 /* Do not run clean AQ when PF reset fails */
Jacob Keller0da36b92017-04-19 09:25:55 -04008540 if (test_bit(__I40E_RESET_FAILED, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00008541 return;
8542
Shannon Nelson86df2422014-05-20 08:01:35 +00008543 /* check for error indications */
8544 val = rd32(&pf->hw, pf->hw.aq.arq.len);
8545 oldval = val;
8546 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008547 if (hw->debug_mask & I40E_DEBUG_AQ)
8548 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008549 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
8550 }
8551 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008552 if (hw->debug_mask & I40E_DEBUG_AQ)
8553 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008554 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08008555 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00008556 }
8557 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008558 if (hw->debug_mask & I40E_DEBUG_AQ)
8559 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008560 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
8561 }
8562 if (oldval != val)
8563 wr32(&pf->hw, pf->hw.aq.arq.len, val);
8564
8565 val = rd32(&pf->hw, pf->hw.aq.asq.len);
8566 oldval = val;
8567 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008568 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8569 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008570 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
8571 }
8572 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008573 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8574 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008575 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
8576 }
8577 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08008578 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
8579 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00008580 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
8581 }
8582 if (oldval != val)
8583 wr32(&pf->hw, pf->hw.aq.asq.len, val);
8584
Mitch Williams1001dc32014-11-11 20:02:19 +00008585 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
8586 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008587 if (!event.msg_buf)
8588 return;
8589
8590 do {
8591 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00008592 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008593 break;
Mitch Williams56497972014-06-04 08:45:18 +00008594 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008595 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
8596 break;
8597 }
8598
8599 opcode = le16_to_cpu(event.desc.opcode);
8600 switch (opcode) {
8601
8602 case i40e_aqc_opc_get_link_status:
8603 i40e_handle_link_event(pf, &event);
8604 break;
8605 case i40e_aqc_opc_send_msg_to_pf:
8606 ret = i40e_vc_process_vf_msg(pf,
8607 le16_to_cpu(event.desc.retval),
8608 le32_to_cpu(event.desc.cookie_high),
8609 le32_to_cpu(event.desc.cookie_low),
8610 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00008611 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008612 break;
8613 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008614 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08008615#ifdef CONFIG_I40E_DCB
8616 rtnl_lock();
8617 ret = i40e_handle_lldp_event(pf, &event);
8618 rtnl_unlock();
8619#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008620 break;
8621 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00008622 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008623 i40e_handle_lan_overflow_event(pf, &event);
8624 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00008625 case i40e_aqc_opc_send_msg_to_peer:
8626 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
8627 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07008628 case i40e_aqc_opc_nvm_erase:
8629 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08008630 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08008631 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
8632 "ARQ NVM operation 0x%04x completed\n",
8633 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07008634 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008635 default:
8636 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08008637 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00008638 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008639 break;
8640 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08008641 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008642
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08008643 if (i < pf->adminq_work_limit)
Jacob Keller0da36b92017-04-19 09:25:55 -04008644 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08008645
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008646 /* re-enable Admin queue interrupt cause */
8647 val = rd32(hw, I40E_PFINT_ICR0_ENA);
8648 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
8649 wr32(hw, I40E_PFINT_ICR0_ENA, val);
8650 i40e_flush(hw);
8651
8652 kfree(event.msg_buf);
8653}
8654
8655/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008656 * i40e_verify_eeprom - make sure eeprom is good to use
8657 * @pf: board private structure
8658 **/
8659static void i40e_verify_eeprom(struct i40e_pf *pf)
8660{
8661 int err;
8662
8663 err = i40e_diag_eeprom_test(&pf->hw);
8664 if (err) {
8665 /* retry in case of garbage read */
8666 err = i40e_diag_eeprom_test(&pf->hw);
8667 if (err) {
8668 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
8669 err);
Jacob Keller0da36b92017-04-19 09:25:55 -04008670 set_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008671 }
8672 }
8673
Jacob Keller0da36b92017-04-19 09:25:55 -04008674 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008675 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
Jacob Keller0da36b92017-04-19 09:25:55 -04008676 clear_bit(__I40E_BAD_EEPROM, pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00008677 }
8678}
8679
8680/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008681 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008682 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008683 *
8684 * enable switch loop back or die - no point in a return value
8685 **/
8686static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
8687{
8688 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8689 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008690 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008691
8692 ctxt.seid = pf->main_vsi_seid;
8693 ctxt.pf_num = pf->hw.pf_id;
8694 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008695 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8696 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008697 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008698 "couldn't get PF vsi config, err %s aq_err %s\n",
8699 i40e_stat_str(&pf->hw, ret),
8700 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008701 return;
8702 }
8703 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8704 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8705 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8706
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008707 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8708 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008709 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008710 "update vsi switch failed, err %s aq_err %s\n",
8711 i40e_stat_str(&pf->hw, ret),
8712 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008713 }
8714}
8715
8716/**
8717 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008718 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008719 *
8720 * disable switch loop back or die - no point in a return value
8721 **/
8722static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
8723{
8724 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8725 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008726 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008727
8728 ctxt.seid = pf->main_vsi_seid;
8729 ctxt.pf_num = pf->hw.pf_id;
8730 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008731 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
8732 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008733 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008734 "couldn't get PF vsi config, err %s aq_err %s\n",
8735 i40e_stat_str(&pf->hw, ret),
8736 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008737 return;
8738 }
8739 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
8740 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
8741 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
8742
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008743 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
8744 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008745 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008746 "update vsi switch failed, err %s aq_err %s\n",
8747 i40e_stat_str(&pf->hw, ret),
8748 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00008749 }
8750}
8751
8752/**
Neerav Parikh51616012015-02-06 08:52:14 +00008753 * i40e_config_bridge_mode - Configure the HW bridge mode
8754 * @veb: pointer to the bridge instance
8755 *
8756 * Configure the loop back mode for the LAN VSI that is downlink to the
8757 * specified HW bridge instance. It is expected this function is called
8758 * when a new HW bridge is instantiated.
8759 **/
8760static void i40e_config_bridge_mode(struct i40e_veb *veb)
8761{
8762 struct i40e_pf *pf = veb->pf;
8763
Shannon Nelson6dec1012015-09-28 14:12:30 -04008764 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
8765 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
8766 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00008767 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
8768 i40e_disable_pf_switch_lb(pf);
8769 else
8770 i40e_enable_pf_switch_lb(pf);
8771}
8772
8773/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008774 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
8775 * @veb: pointer to the VEB instance
8776 *
8777 * This is a recursive function that first builds the attached VSIs then
8778 * recurses in to build the next layer of VEB. We track the connections
8779 * through our own index numbers because the seid's from the HW could
8780 * change across the reset.
8781 **/
8782static int i40e_reconstitute_veb(struct i40e_veb *veb)
8783{
8784 struct i40e_vsi *ctl_vsi = NULL;
8785 struct i40e_pf *pf = veb->pf;
8786 int v, veb_idx;
8787 int ret;
8788
8789 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00008790 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008791 if (pf->vsi[v] &&
8792 pf->vsi[v]->veb_idx == veb->idx &&
8793 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
8794 ctl_vsi = pf->vsi[v];
8795 break;
8796 }
8797 }
8798 if (!ctl_vsi) {
8799 dev_info(&pf->pdev->dev,
8800 "missing owner VSI for veb_idx %d\n", veb->idx);
8801 ret = -ENOENT;
8802 goto end_reconstitute;
8803 }
8804 if (ctl_vsi != pf->vsi[pf->lan_vsi])
8805 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
8806 ret = i40e_add_vsi(ctl_vsi);
8807 if (ret) {
8808 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008809 "rebuild of veb_idx %d owner VSI failed: %d\n",
8810 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008811 goto end_reconstitute;
8812 }
8813 i40e_vsi_reset_stats(ctl_vsi);
8814
8815 /* create the VEB in the switch and move the VSI onto the VEB */
8816 ret = i40e_add_veb(veb, ctl_vsi);
8817 if (ret)
8818 goto end_reconstitute;
8819
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008820 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
8821 veb->bridge_mode = BRIDGE_MODE_VEB;
8822 else
8823 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00008824 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00008825
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008826 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00008827 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008828 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
8829 continue;
8830
8831 if (pf->vsi[v]->veb_idx == veb->idx) {
8832 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04008833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008834 vsi->uplink_seid = veb->seid;
8835 ret = i40e_add_vsi(vsi);
8836 if (ret) {
8837 dev_info(&pf->pdev->dev,
8838 "rebuild of vsi_idx %d failed: %d\n",
8839 v, ret);
8840 goto end_reconstitute;
8841 }
8842 i40e_vsi_reset_stats(vsi);
8843 }
8844 }
8845
8846 /* create any VEBs attached to this VEB - RECURSION */
8847 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
8848 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
8849 pf->veb[veb_idx]->uplink_seid = veb->seid;
8850 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
8851 if (ret)
8852 break;
8853 }
8854 }
8855
8856end_reconstitute:
8857 return ret;
8858}
8859
8860/**
8861 * i40e_get_capabilities - get info about the HW
8862 * @pf: the PF struct
8863 **/
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008864static int i40e_get_capabilities(struct i40e_pf *pf,
8865 enum i40e_admin_queue_opc list_type)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008866{
8867 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
8868 u16 data_size;
8869 int buf_len;
8870 int err;
8871
8872 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
8873 do {
8874 cap_buf = kzalloc(buf_len, GFP_KERNEL);
8875 if (!cap_buf)
8876 return -ENOMEM;
8877
8878 /* this loads the data into the hw struct for us */
8879 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008880 &data_size, list_type,
8881 NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008882 /* data loaded, buffer no longer needed */
8883 kfree(cap_buf);
8884
8885 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
8886 /* retry with a larger buffer */
8887 buf_len = data_size;
8888 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
8889 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008890 "capability discovery failed, err %s aq_err %s\n",
8891 i40e_stat_str(&pf->hw, err),
8892 i40e_aq_str(&pf->hw,
8893 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008894 return -ENODEV;
8895 }
8896 } while (err);
8897
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008898 if (pf->hw.debug_mask & I40E_DEBUG_USER) {
8899 if (list_type == i40e_aqc_opc_list_func_capabilities) {
8900 dev_info(&pf->pdev->dev,
8901 "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",
8902 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
8903 pf->hw.func_caps.num_msix_vectors,
8904 pf->hw.func_caps.num_msix_vectors_vf,
8905 pf->hw.func_caps.fd_filters_guaranteed,
8906 pf->hw.func_caps.fd_filters_best_effort,
8907 pf->hw.func_caps.num_tx_qp,
8908 pf->hw.func_caps.num_vsis);
8909 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
8910 dev_info(&pf->pdev->dev,
8911 "switch_mode=0x%04x, function_valid=0x%08x\n",
8912 pf->hw.dev_caps.switch_mode,
8913 pf->hw.dev_caps.valid_functions);
8914 dev_info(&pf->pdev->dev,
8915 "SR-IOV=%d, num_vfs for all function=%u\n",
8916 pf->hw.dev_caps.sr_iov_1_1,
8917 pf->hw.dev_caps.num_vfs);
8918 dev_info(&pf->pdev->dev,
8919 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
8920 pf->hw.dev_caps.num_vsis,
8921 pf->hw.dev_caps.num_rx_qp,
8922 pf->hw.dev_caps.num_tx_qp);
8923 }
8924 }
8925 if (list_type == i40e_aqc_opc_list_func_capabilities) {
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008926#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
8927 + pf->hw.func_caps.num_vfs)
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008928 if (pf->hw.revision_id == 0 &&
8929 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
8930 dev_info(&pf->pdev->dev,
8931 "got num_vsis %d, setting num_vsis to %d\n",
8932 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
8933 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
8934 }
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008935 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008936 return 0;
8937}
8938
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008939static int i40e_vsi_clear(struct i40e_vsi *vsi);
8940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008941/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008942 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008943 * @pf: board private structure
8944 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008945static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008946{
8947 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008948
Jesse Brandeburg407e0632014-06-03 23:50:12 +00008949 /* quick workaround for an NVM issue that leaves a critical register
8950 * uninitialized
8951 */
8952 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
8953 static const u32 hkey[] = {
8954 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
8955 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
8956 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
8957 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07008958 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00008959
8960 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
8961 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
8962 }
8963
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008964 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008965 return;
8966
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008967 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07008968 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008969
8970 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008971 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008972 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
8973 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008974 if (!vsi) {
8975 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008976 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07008977 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008978 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008979 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008980 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00008981
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008982 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008983}
8984
8985/**
8986 * i40e_fdir_teardown - release the Flow Director resources
8987 * @pf: board private structure
8988 **/
8989static void i40e_fdir_teardown(struct i40e_pf *pf)
8990{
Alexander Duyck4b816442016-10-11 15:26:53 -07008991 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008992
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00008993 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07008994 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
8995 if (vsi)
8996 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008997}
8998
8999/**
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07009000 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9001 * @vsi: PF main vsi
9002 * @seid: seid of main or channel VSIs
9003 *
9004 * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9005 * existed before reset
9006 **/
9007static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9008{
9009 struct i40e_cloud_filter *cfilter;
9010 struct i40e_pf *pf = vsi->back;
9011 struct hlist_node *node;
9012 i40e_status ret;
9013
9014 /* Add cloud filters back if they exist */
9015 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9016 cloud_node) {
9017 if (cfilter->seid != seid)
9018 continue;
9019
9020 if (cfilter->dst_port)
9021 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9022 true);
9023 else
9024 ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9025
9026 if (ret) {
9027 dev_dbg(&pf->pdev->dev,
9028 "Failed to rebuild cloud filter, err %s aq_err %s\n",
9029 i40e_stat_str(&pf->hw, ret),
9030 i40e_aq_str(&pf->hw,
9031 pf->hw.aq.asq_last_status));
9032 return ret;
9033 }
9034 }
9035 return 0;
9036}
9037
9038/**
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009039 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9040 * @vsi: PF main vsi
9041 *
9042 * Rebuilds channel VSIs if they existed before reset
9043 **/
9044static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9045{
9046 struct i40e_channel *ch, *ch_tmp;
9047 i40e_status ret;
9048
9049 if (list_empty(&vsi->ch_list))
9050 return 0;
9051
9052 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9053 if (!ch->initialized)
9054 break;
9055 /* Proceed with creation of channel (VMDq2) VSI */
9056 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9057 if (ret) {
9058 dev_info(&vsi->back->pdev->dev,
9059 "failed to rebuild channels using uplink_seid %u\n",
9060 vsi->uplink_seid);
9061 return ret;
9062 }
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009063 if (ch->max_tx_rate) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07009064 u64 credits = ch->max_tx_rate;
9065
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009066 if (i40e_set_bw_limit(vsi, ch->seid,
9067 ch->max_tx_rate))
9068 return -EINVAL;
9069
Alan Brady6c32e0d2017-10-09 15:48:45 -07009070 do_div(credits, I40E_BW_CREDIT_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009071 dev_dbg(&vsi->back->pdev->dev,
9072 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9073 ch->max_tx_rate,
Alan Brady6c32e0d2017-10-09 15:48:45 -07009074 credits,
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009075 ch->seid);
9076 }
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07009077 ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9078 if (ret) {
9079 dev_dbg(&vsi->back->pdev->dev,
9080 "Failed to rebuild cloud filters for channel VSI %u\n",
9081 ch->seid);
9082 return ret;
9083 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009084 }
9085 return 0;
9086}
9087
9088/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009089 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009090 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04009091 * @lock_acquired: indicates whether or not the lock has been acquired
9092 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009093 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009094 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009095 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04009096static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009097{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009098 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00009099 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009100 u32 v;
9101
Jacob Keller0da36b92017-04-19 09:25:55 -04009102 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9103 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00009104 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08009105 if (i40e_check_asq_alive(&pf->hw))
9106 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009107
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009108 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009109
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009110 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04009111 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9112 if (!lock_acquired)
9113 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009114 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04009115 if (!lock_acquired)
9116 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009117
Mitch Williams505682c2014-05-20 08:01:37 +00009118 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009119 if (pf->vsi[v])
9120 pf->vsi[v]->seid = 0;
9121 }
9122
9123 i40e_shutdown_adminq(&pf->hw);
9124
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009125 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00009126 if (hw->hmc.hmc_obj) {
9127 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00009128 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00009129 dev_warn(&pf->pdev->dev,
9130 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009131 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009132}
9133
9134/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009135 * i40e_send_version - update firmware with driver version
9136 * @pf: PF struct
9137 */
9138static void i40e_send_version(struct i40e_pf *pf)
9139{
9140 struct i40e_driver_version dv;
9141
9142 dv.major_version = DRV_VERSION_MAJOR;
9143 dv.minor_version = DRV_VERSION_MINOR;
9144 dv.build_version = DRV_VERSION_BUILD;
9145 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00009146 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009147 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
9148}
9149
9150/**
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04009151 * i40e_get_oem_version - get OEM specific version information
9152 * @hw: pointer to the hardware structure
9153 **/
9154static void i40e_get_oem_version(struct i40e_hw *hw)
9155{
9156 u16 block_offset = 0xffff;
9157 u16 block_length = 0;
9158 u16 capabilities = 0;
9159 u16 gen_snap = 0;
9160 u16 release = 0;
9161
9162#define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
9163#define I40E_NVM_OEM_LENGTH_OFFSET 0x00
9164#define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
9165#define I40E_NVM_OEM_GEN_OFFSET 0x02
9166#define I40E_NVM_OEM_RELEASE_OFFSET 0x03
9167#define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
9168#define I40E_NVM_OEM_LENGTH 3
9169
9170 /* Check if pointer to OEM version block is valid. */
9171 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
9172 if (block_offset == 0xffff)
9173 return;
9174
9175 /* Check if OEM version block has correct length. */
9176 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
9177 &block_length);
9178 if (block_length < I40E_NVM_OEM_LENGTH)
9179 return;
9180
9181 /* Check if OEM version format is as expected. */
9182 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
9183 &capabilities);
9184 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
9185 return;
9186
9187 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
9188 &gen_snap);
9189 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
9190 &release);
9191 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
9192 hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
9193}
9194
9195/**
Maciej Sosin373149f2017-04-05 07:50:55 -04009196 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
9197 * @pf: board private structure
9198 **/
9199static int i40e_reset(struct i40e_pf *pf)
9200{
9201 struct i40e_hw *hw = &pf->hw;
9202 i40e_status ret;
9203
9204 ret = i40e_pf_reset(hw);
9205 if (ret) {
9206 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Jacob Keller0da36b92017-04-19 09:25:55 -04009207 set_bit(__I40E_RESET_FAILED, pf->state);
9208 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Maciej Sosin373149f2017-04-05 07:50:55 -04009209 } else {
9210 pf->pfr_count++;
9211 }
9212 return ret;
9213}
9214
9215/**
9216 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009217 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009218 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04009219 * @lock_acquired: indicates whether or not the lock has been acquired
9220 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009221 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04009222static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009223{
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009224 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009225 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009226 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009227 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04009228 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04009229 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009230
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -03009231 if (test_bit(__I40E_DOWN, pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00009232 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009233 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009234
9235 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
9236 ret = i40e_init_adminq(&pf->hw);
9237 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009238 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
9239 i40e_stat_str(&pf->hw, ret),
9240 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00009241 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009242 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -04009243 i40e_get_oem_version(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009244
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009245 /* re-verify the eeprom if we just had an EMP reset */
Jacob Keller0da36b92017-04-19 09:25:55 -04009246 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009247 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00009248
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00009249 i40e_clear_pxe_mode(hw);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07009250 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009251 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009252 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009253
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009254 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08009255 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009256 if (ret) {
9257 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
9258 goto end_core_reset;
9259 }
9260 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
9261 if (ret) {
9262 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
9263 goto end_core_reset;
9264 }
9265
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009266#ifdef CONFIG_I40E_DCB
9267 ret = i40e_init_pf_dcb(pf);
9268 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00009269 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
9270 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9271 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009272 }
9273#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009274 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04009275 if (!lock_acquired)
9276 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009277 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009278 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04009279 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009280
Shannon Nelson2f0aff42016-01-04 10:33:08 -08009281 /* The driver only wants link up/down and module qualification
9282 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009283 */
9284 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08009285 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07009286 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08009287 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009288 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009289 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
9290 i40e_stat_str(&pf->hw, ret),
9291 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00009292
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009293 /* make sure our flow control settings are restored */
9294 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
9295 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04009296 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
9297 i40e_stat_str(&pf->hw, ret),
9298 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009299
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009300 /* Rebuild the VSIs and VEBs that existed before reset.
9301 * They are still in our local switch element arrays, so only
9302 * need to rebuild the switch model in the HW.
9303 *
9304 * If there were VEBs but the reconstitution failed, we'll try
9305 * try to recover minimal use by getting the basic PF VSI working.
9306 */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009307 if (vsi->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00009308 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009309 /* find the one VEB connected to the MAC, and find orphans */
9310 for (v = 0; v < I40E_MAX_VEB; v++) {
9311 if (!pf->veb[v])
9312 continue;
9313
9314 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
9315 pf->veb[v]->uplink_seid == 0) {
9316 ret = i40e_reconstitute_veb(pf->veb[v]);
9317
9318 if (!ret)
9319 continue;
9320
9321 /* If Main VEB failed, we're in deep doodoo,
9322 * so give up rebuilding the switch and set up
9323 * for minimal rebuild of PF VSI.
9324 * If orphan failed, we'll report the error
9325 * but try to keep going.
9326 */
9327 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
9328 dev_info(&pf->pdev->dev,
9329 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
9330 ret);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009331 vsi->uplink_seid = pf->mac_seid;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009332 break;
9333 } else if (pf->veb[v]->uplink_seid == 0) {
9334 dev_info(&pf->pdev->dev,
9335 "rebuild of orphan VEB failed: %d\n",
9336 ret);
9337 }
9338 }
9339 }
9340 }
9341
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009342 if (vsi->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00009343 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009344 /* no VEB, so rebuild only the Main VSI */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009345 ret = i40e_add_vsi(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009346 if (ret) {
9347 dev_info(&pf->pdev->dev,
9348 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04009349 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009350 }
9351 }
9352
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009353 if (vsi->mqprio_qopt.max_rate[0]) {
Alan Brady6c32e0d2017-10-09 15:48:45 -07009354 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9355 u64 credits = 0;
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009356
Alan Brady6c32e0d2017-10-09 15:48:45 -07009357 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009358 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
Alan Brady6c32e0d2017-10-09 15:48:45 -07009359 if (ret)
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009360 goto end_unlock;
Alan Brady6c32e0d2017-10-09 15:48:45 -07009361
9362 credits = max_tx_rate;
9363 do_div(credits, I40E_BW_CREDIT_DIVISOR);
9364 dev_dbg(&vsi->back->pdev->dev,
9365 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9366 max_tx_rate,
9367 credits,
9368 vsi->seid);
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009369 }
9370
Amritha Nambiar2f4b4112017-10-27 02:36:01 -07009371 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
9372 if (ret)
9373 goto end_unlock;
9374
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009375 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
9376 * for this main VSI if they exist
9377 */
Amritha Nambiar2027d4d2017-09-07 04:00:32 -07009378 ret = i40e_rebuild_channels(vsi);
Amritha Nambiar8f88b302017-09-07 04:00:17 -07009379 if (ret)
9380 goto end_unlock;
9381
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04009382 /* Reconfigure hardware for allowing smaller MSS in the case
9383 * of TSO, so that we avoid the MDD being fired and causing
9384 * a reset in the case of small MSS+TSO.
9385 */
9386#define I40E_REG_MSS 0x000E64DC
9387#define I40E_REG_MSS_MIN_MASK 0x3FF0000
9388#define I40E_64BYTE_MSS 0x400000
9389 val = rd32(hw, I40E_REG_MSS);
9390 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
9391 val &= ~I40E_REG_MSS_MIN_MASK;
9392 val |= I40E_64BYTE_MSS;
9393 wr32(hw, I40E_REG_MSS, val);
9394 }
9395
Jacob Kellerd36e41d2017-06-23 04:24:46 -04009396 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00009397 msleep(75);
9398 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
9399 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009400 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
9401 i40e_stat_str(&pf->hw, ret),
9402 i40e_aq_str(&pf->hw,
9403 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00009404 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009405 /* reinit the misc interrupt */
9406 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9407 ret = i40e_setup_misc_vector(pf);
9408
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04009409 /* Add a filter to drop all Flow control frames from any VSI from being
9410 * transmitted. By doing so we stop a malicious VF from sending out
9411 * PAUSE or PFC frames and potentially controlling traffic for other
9412 * PF/VF VSIs.
9413 * The FW can still send Flow control frames if enabled.
9414 */
9415 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
9416 pf->main_vsi_seid);
9417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009418 /* restart the VSIs that were rebuilt and running before the reset */
9419 i40e_pf_unquiesce_all_vsi(pf);
9420
Jacob Keller024b05f2017-04-13 04:45:46 -04009421 /* Release the RTNL lock before we start resetting VFs */
9422 if (!lock_acquired)
9423 rtnl_unlock();
9424
Alan Bradybd5608b2017-11-14 07:00:52 -05009425 /* Restore promiscuous settings */
9426 ret = i40e_set_promiscuous(pf, pf->cur_promisc);
9427 if (ret)
9428 dev_warn(&pf->pdev->dev,
9429 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
9430 pf->cur_promisc ? "on" : "off",
9431 i40e_stat_str(&pf->hw, ret),
9432 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9433
Jacob Kellere4b433f2017-04-13 04:45:52 -04009434 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08009435
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00009437 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009438
Jacob Keller024b05f2017-04-13 04:45:46 -04009439 /* We've already released the lock, so don't do it again */
9440 goto end_core_reset;
9441
Maciej Sosin373149f2017-04-05 07:50:55 -04009442end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04009443 if (!lock_acquired)
9444 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009445end_core_reset:
Jacob Keller0da36b92017-04-19 09:25:55 -04009446 clear_bit(__I40E_RESET_FAILED, pf->state);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00009447clear_recovery:
Jacob Keller0da36b92017-04-19 09:25:55 -04009448 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009449}
9450
9451/**
Maciej Sosin373149f2017-04-05 07:50:55 -04009452 * i40e_reset_and_rebuild - reset and rebuild using a saved config
9453 * @pf: board private structure
9454 * @reinit: if the Main VSI needs to re-initialized.
9455 * @lock_acquired: indicates whether or not the lock has been acquired
9456 * before this function was called.
9457 **/
9458static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
9459 bool lock_acquired)
9460{
9461 int ret;
9462 /* Now we wait for GRST to settle out.
9463 * We don't have to delete the VEBs or VSIs from the hw switch
9464 * because the reset will make them disappear.
9465 */
9466 ret = i40e_reset(pf);
9467 if (!ret)
9468 i40e_rebuild(pf, reinit, lock_acquired);
9469}
9470
9471/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009472 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009473 * @pf: board private structure
9474 *
9475 * Close up the VFs and other things in prep for a Core Reset,
9476 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04009477 * @lock_acquired: indicates whether or not the lock has been acquired
9478 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009479 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04009480static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009481{
Maciej Sosin373149f2017-04-05 07:50:55 -04009482 i40e_prep_for_reset(pf, lock_acquired);
9483 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009484}
9485
9486/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009487 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009488 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009489 *
9490 * Called from the MDD irq handler to identify possibly malicious vfs
9491 **/
9492static void i40e_handle_mdd_event(struct i40e_pf *pf)
9493{
9494 struct i40e_hw *hw = &pf->hw;
9495 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00009496 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009497 struct i40e_vf *vf;
9498 u32 reg;
9499 int i;
9500
Jacob Keller0da36b92017-04-19 09:25:55 -04009501 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009502 return;
9503
9504 /* find what triggered the MDD event */
9505 reg = rd32(hw, I40E_GL_MDET_TX);
9506 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009507 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
9508 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00009509 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009510 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07009511 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009512 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00009513 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
9514 I40E_GL_MDET_TX_QUEUE_SHIFT) -
9515 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009516 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009517 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 +00009518 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009519 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
9520 mdd_detected = true;
9521 }
9522 reg = rd32(hw, I40E_GL_MDET_RX);
9523 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009524 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
9525 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07009526 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00009527 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00009528 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
9529 I40E_GL_MDET_RX_QUEUE_SHIFT) -
9530 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009531 if (netif_msg_rx_err(pf))
9532 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
9533 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009534 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
9535 mdd_detected = true;
9536 }
9537
Neerav Parikhdf430b12014-06-04 01:23:15 +00009538 if (mdd_detected) {
9539 reg = rd32(hw, I40E_PF_MDET_TX);
9540 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
9541 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009542 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00009543 pf_mdd_detected = true;
9544 }
9545 reg = rd32(hw, I40E_PF_MDET_RX);
9546 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
9547 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009548 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00009549 pf_mdd_detected = true;
9550 }
9551 /* Queue belongs to the PF, initiate a reset */
9552 if (pf_mdd_detected) {
Jacob Keller0da36b92017-04-19 09:25:55 -04009553 set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
Neerav Parikhdf430b12014-06-04 01:23:15 +00009554 i40e_service_event_schedule(pf);
9555 }
9556 }
9557
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009558 /* see if one of the VFs needs its hand slapped */
9559 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
9560 vf = &(pf->vf[i]);
9561 reg = rd32(hw, I40E_VP_MDET_TX(i));
9562 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
9563 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
9564 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009565 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
9566 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009567 }
9568
9569 reg = rd32(hw, I40E_VP_MDET_RX(i));
9570 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
9571 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
9572 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00009573 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
9574 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009575 }
9576
9577 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
9578 dev_info(&pf->pdev->dev,
9579 "Too many MDD events on VF %d, disabled\n", i);
9580 dev_info(&pf->pdev->dev,
9581 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04009582 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009583 }
9584 }
9585
9586 /* re-enable mdd interrupt cause */
Jacob Keller0da36b92017-04-19 09:25:55 -04009587 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009588 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
9589 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
9590 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
9591 i40e_flush(hw);
9592}
9593
Jacob Kellerd8b2c702017-06-20 15:17:00 -07009594static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
9595{
9596 switch (port->type) {
9597 case UDP_TUNNEL_TYPE_VXLAN:
9598 return "vxlan";
9599 case UDP_TUNNEL_TYPE_GENEVE:
9600 return "geneve";
9601 default:
9602 return "unknown";
9603 }
9604}
9605
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009606/**
Alexander Duyck1f190d92017-04-19 09:25:51 -04009607 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
9608 * @pf: board private structure
9609 **/
9610static void i40e_sync_udp_filters(struct i40e_pf *pf)
9611{
9612 int i;
9613
9614 /* loop through and set pending bit for all active UDP filters */
9615 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
9616 if (pf->udp_ports[i].port)
9617 pf->pending_udp_bitmap |= BIT_ULL(i);
9618 }
9619
9620 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9621}
9622
9623/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009624 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009625 * @pf: board private structure
9626 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08009627static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009628{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009629 struct i40e_hw *hw = &pf->hw;
9630 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009631 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009632 int i;
9633
Singhai, Anjali6a899022015-12-14 12:21:18 -08009634 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009635 return;
9636
Singhai, Anjali6a899022015-12-14 12:21:18 -08009637 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009638
9639 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08009640 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
9641 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04009642 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07009643 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07009644 ret = i40e_aq_add_udp_tunnel(hw, port,
9645 pf->udp_ports[i].type,
9646 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07009647 else
9648 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009649
9650 if (ret) {
Jacob Kellerd8b2c702017-06-20 15:17:00 -07009651 dev_info(&pf->pdev->dev,
9652 "%s %s port %d, index %d failed, err %s aq_err %s\n",
9653 i40e_tunnel_name(&pf->udp_ports[i]),
9654 port ? "add" : "delete",
9655 port, i,
9656 i40e_stat_str(&pf->hw, ret),
9657 i40e_aq_str(&pf->hw,
9658 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04009659 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009660 }
9661 }
9662 }
9663}
9664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009665/**
9666 * i40e_service_task - Run the driver's async subtasks
9667 * @work: pointer to work_struct containing our data
9668 **/
9669static void i40e_service_task(struct work_struct *work)
9670{
9671 struct i40e_pf *pf = container_of(work,
9672 struct i40e_pf,
9673 service_task);
9674 unsigned long start_time = jiffies;
9675
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00009676 /* don't bother with service tasks if a reset is in progress */
Jacob Keller0da36b92017-04-19 09:25:55 -04009677 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00009678 return;
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00009679
Jacob Keller0da36b92017-04-19 09:25:55 -04009680 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
Mitch Williams91089032016-11-21 13:03:51 -08009681 return;
9682
Kiran Patilb03a8c12015-09-24 18:13:15 -04009683 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08009684 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009685 i40e_reset_subtask(pf);
9686 i40e_handle_mdd_event(pf);
9687 i40e_vc_process_vflr_event(pf);
9688 i40e_watchdog_subtask(pf);
9689 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08009690 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
9691 /* Client subtask will reopen next time through. */
9692 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
9693 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
9694 } else {
9695 i40e_client_subtask(pf);
9696 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
9697 i40e_notify_client_of_l2_param_changes(
9698 pf->vsi[pf->lan_vsi]);
9699 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
9700 }
9701 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009702 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009703 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009704 i40e_clean_adminq_subtask(pf);
9705
Mitch Williams91089032016-11-21 13:03:51 -08009706 /* flush memory to make sure state is correct before next watchdog */
9707 smp_mb__before_atomic();
Jacob Keller0da36b92017-04-19 09:25:55 -04009708 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009709
9710 /* If the tasks have taken longer than one timer cycle or there
9711 * is more work to be done, reschedule the service task now
9712 * rather than wait for the timer to tick again.
9713 */
9714 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
Jacob Keller0da36b92017-04-19 09:25:55 -04009715 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
9716 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
9717 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009718 i40e_service_event_schedule(pf);
9719}
9720
9721/**
9722 * i40e_service_timer - timer callback
9723 * @data: pointer to PF struct
9724 **/
Kees Cook26566ea2017-10-16 17:29:35 -07009725static void i40e_service_timer(struct timer_list *t)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009726{
Kees Cook26566ea2017-10-16 17:29:35 -07009727 struct i40e_pf *pf = from_timer(pf, t, service_timer);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009728
9729 mod_timer(&pf->service_timer,
9730 round_jiffies(jiffies + pf->service_timer_period));
9731 i40e_service_event_schedule(pf);
9732}
9733
9734/**
9735 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
9736 * @vsi: the VSI being configured
9737 **/
9738static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
9739{
9740 struct i40e_pf *pf = vsi->back;
9741
9742 switch (vsi->type) {
9743 case I40E_VSI_MAIN:
9744 vsi->alloc_queue_pairs = pf->num_lan_qps;
9745 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9746 I40E_REQ_DESCRIPTOR_MULTIPLE);
9747 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
9748 vsi->num_q_vectors = pf->num_lan_msix;
9749 else
9750 vsi->num_q_vectors = 1;
9751
9752 break;
9753
9754 case I40E_VSI_FDIR:
9755 vsi->alloc_queue_pairs = 1;
9756 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
9757 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07009758 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009759 break;
9760
9761 case I40E_VSI_VMDQ2:
9762 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
9763 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9764 I40E_REQ_DESCRIPTOR_MULTIPLE);
9765 vsi->num_q_vectors = pf->num_vmdq_msix;
9766 break;
9767
9768 case I40E_VSI_SRIOV:
9769 vsi->alloc_queue_pairs = pf->num_vf_qps;
9770 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
9771 I40E_REQ_DESCRIPTOR_MULTIPLE);
9772 break;
9773
9774 default:
9775 WARN_ON(1);
9776 return -ENODATA;
9777 }
9778
9779 return 0;
9780}
9781
9782/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009783 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
Rami Rosen3d7d7a82017-09-16 05:49:48 +03009784 * @vsi: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009785 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009786 *
9787 * On error: returns error code (negative)
9788 * On success: returns 0
9789 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009790static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009791{
Björn Töpel74608d12017-05-24 07:55:35 +02009792 struct i40e_ring **next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009793 int size;
9794 int ret = 0;
9795
Björn Töpel74608d12017-05-24 07:55:35 +02009796 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
9797 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
9798 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009799 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
9800 if (!vsi->tx_rings)
9801 return -ENOMEM;
Björn Töpel74608d12017-05-24 07:55:35 +02009802 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
9803 if (i40e_enabled_xdp_vsi(vsi)) {
9804 vsi->xdp_rings = next_rings;
9805 next_rings += vsi->alloc_queue_pairs;
9806 }
9807 vsi->rx_rings = next_rings;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009808
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009809 if (alloc_qvectors) {
9810 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00009811 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009812 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
9813 if (!vsi->q_vectors) {
9814 ret = -ENOMEM;
9815 goto err_vectors;
9816 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009817 }
9818 return ret;
9819
9820err_vectors:
9821 kfree(vsi->tx_rings);
9822 return ret;
9823}
9824
9825/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
9827 * @pf: board private structure
9828 * @type: type of VSI
9829 *
9830 * On error: returns error code (negative)
9831 * On success: returns vsi index in PF (positive)
9832 **/
9833static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
9834{
9835 int ret = -ENODEV;
9836 struct i40e_vsi *vsi;
9837 int vsi_idx;
9838 int i;
9839
9840 /* Need to protect the allocation of the VSIs at the PF level */
9841 mutex_lock(&pf->switch_mutex);
9842
9843 /* VSI list may be fragmented if VSI creation/destruction has
9844 * been happening. We can afford to do a quick scan to look
9845 * for any free VSIs in the list.
9846 *
9847 * find next empty vsi slot, looping back around if necessary
9848 */
9849 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00009850 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009851 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00009852 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009853 i = 0;
9854 while (i < pf->next_vsi && pf->vsi[i])
9855 i++;
9856 }
9857
Mitch Williams505682c2014-05-20 08:01:37 +00009858 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009859 vsi_idx = i; /* Found one! */
9860 } else {
9861 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00009862 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009863 }
9864 pf->next_vsi = ++i;
9865
9866 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
9867 if (!vsi) {
9868 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00009869 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009870 }
9871 vsi->type = type;
9872 vsi->back = pf;
Jacob Keller0da36b92017-04-19 09:25:55 -04009873 set_bit(__I40E_VSI_DOWN, vsi->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009874 vsi->flags = 0;
9875 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04009876 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00009877 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
9878 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009879 vsi->netdev_registered = false;
9880 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07009881 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00009882 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009883
Alexander Duyck9f65e152013-09-28 06:00:58 +00009884 ret = i40e_set_num_rings_in_vsi(vsi);
9885 if (ret)
9886 goto err_rings;
9887
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009888 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009889 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00009890 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00009891
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009892 /* Setup default MSIX irq handler for VSI */
9893 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
9894
Kiran Patil21659032015-09-30 14:09:03 -04009895 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07009896 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009897 pf->vsi[vsi_idx] = vsi;
9898 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00009899 goto unlock_pf;
9900
Alexander Duyck9f65e152013-09-28 06:00:58 +00009901err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00009902 pf->next_vsi = i - 1;
9903 kfree(vsi);
9904unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009905 mutex_unlock(&pf->switch_mutex);
9906 return ret;
9907}
9908
9909/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009910 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
9911 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009912 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009913 *
9914 * On error: returns error code (negative)
9915 * On success: returns 0
9916 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009917static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009918{
9919 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009920 if (free_qvectors) {
9921 kfree(vsi->q_vectors);
9922 vsi->q_vectors = NULL;
9923 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009924 kfree(vsi->tx_rings);
9925 vsi->tx_rings = NULL;
9926 vsi->rx_rings = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +02009927 vsi->xdp_rings = NULL;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00009928}
9929
9930/**
Helin Zhang28c58692015-10-26 19:44:27 -04009931 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
9932 * and lookup table
9933 * @vsi: Pointer to VSI structure
9934 */
9935static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
9936{
9937 if (!vsi)
9938 return;
9939
9940 kfree(vsi->rss_hkey_user);
9941 vsi->rss_hkey_user = NULL;
9942
9943 kfree(vsi->rss_lut_user);
9944 vsi->rss_lut_user = NULL;
9945}
9946
9947/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009948 * i40e_vsi_clear - Deallocate the VSI provided
9949 * @vsi: the VSI being un-configured
9950 **/
9951static int i40e_vsi_clear(struct i40e_vsi *vsi)
9952{
9953 struct i40e_pf *pf;
9954
9955 if (!vsi)
9956 return 0;
9957
9958 if (!vsi->back)
9959 goto free_vsi;
9960 pf = vsi->back;
9961
9962 mutex_lock(&pf->switch_mutex);
9963 if (!pf->vsi[vsi->idx]) {
9964 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
9965 vsi->idx, vsi->idx, vsi, vsi->type);
9966 goto unlock_vsi;
9967 }
9968
9969 if (pf->vsi[vsi->idx] != vsi) {
9970 dev_err(&pf->pdev->dev,
9971 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
9972 pf->vsi[vsi->idx]->idx,
9973 pf->vsi[vsi->idx],
9974 pf->vsi[vsi->idx]->type,
9975 vsi->idx, vsi, vsi->type);
9976 goto unlock_vsi;
9977 }
9978
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00009979 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009980 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9981 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
9982
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009983 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04009984 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00009985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009986 pf->vsi[vsi->idx] = NULL;
9987 if (vsi->idx < pf->next_vsi)
9988 pf->next_vsi = vsi->idx;
9989
9990unlock_vsi:
9991 mutex_unlock(&pf->switch_mutex);
9992free_vsi:
9993 kfree(vsi);
9994
9995 return 0;
9996}
9997
9998/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00009999 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10000 * @vsi: the VSI being cleaned
10001 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +000010002static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +000010003{
10004 int i;
10005
Greg Rose8e9dca52013-12-18 13:45:53 +000010006 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +000010007 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +000010008 kfree_rcu(vsi->tx_rings[i], rcu);
10009 vsi->tx_rings[i] = NULL;
10010 vsi->rx_rings[i] = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020010011 if (vsi->xdp_rings)
10012 vsi->xdp_rings[i] = NULL;
Mitch Williams00403f02013-09-28 07:13:13 +000010013 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +000010014 }
Alexander Duyck9f65e152013-09-28 06:00:58 +000010015}
10016
10017/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010018 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10019 * @vsi: the VSI being configured
10020 **/
10021static int i40e_alloc_rings(struct i40e_vsi *vsi)
10022{
Björn Töpel74608d12017-05-24 07:55:35 +020010023 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010024 struct i40e_pf *pf = vsi->back;
Björn Töpel74608d12017-05-24 07:55:35 +020010025 struct i40e_ring *ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010026
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010027 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +000010028 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +000010029 /* allocate space for both Tx and Rx in one shot */
Björn Töpel74608d12017-05-24 07:55:35 +020010030 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10031 if (!ring)
Alexander Duyck9f65e152013-09-28 06:00:58 +000010032 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010033
Björn Töpel74608d12017-05-24 07:55:35 +020010034 ring->queue_index = i;
10035 ring->reg_idx = vsi->base_queue + i;
10036 ring->ring_active = false;
10037 ring->vsi = vsi;
10038 ring->netdev = vsi->netdev;
10039 ring->dev = &pf->pdev->dev;
10040 ring->count = vsi->num_desc;
10041 ring->size = 0;
10042 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010043 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +020010044 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10045 ring->tx_itr_setting = pf->tx_itr_default;
10046 vsi->tx_rings[i] = ring++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010047
Björn Töpel74608d12017-05-24 07:55:35 +020010048 if (!i40e_enabled_xdp_vsi(vsi))
10049 goto setup_rx;
10050
10051 ring->queue_index = vsi->alloc_queue_pairs + i;
10052 ring->reg_idx = vsi->base_queue + ring->queue_index;
10053 ring->ring_active = false;
10054 ring->vsi = vsi;
10055 ring->netdev = NULL;
10056 ring->dev = &pf->pdev->dev;
10057 ring->count = vsi->num_desc;
10058 ring->size = 0;
10059 ring->dcb_tc = 0;
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010060 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
Björn Töpel74608d12017-05-24 07:55:35 +020010061 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10062 set_ring_xdp(ring);
10063 ring->tx_itr_setting = pf->tx_itr_default;
10064 vsi->xdp_rings[i] = ring++;
10065
10066setup_rx:
10067 ring->queue_index = i;
10068 ring->reg_idx = vsi->base_queue + i;
10069 ring->ring_active = false;
10070 ring->vsi = vsi;
10071 ring->netdev = vsi->netdev;
10072 ring->dev = &pf->pdev->dev;
10073 ring->count = vsi->num_desc;
10074 ring->size = 0;
10075 ring->dcb_tc = 0;
10076 ring->rx_itr_setting = pf->rx_itr_default;
10077 vsi->rx_rings[i] = ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010078 }
10079
10080 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +000010081
10082err_out:
10083 i40e_vsi_clear_rings(vsi);
10084 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010085}
10086
10087/**
10088 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10089 * @pf: board private structure
10090 * @vectors: the number of MSI-X vectors to request
10091 *
10092 * Returns the number of vectors reserved, or error
10093 **/
10094static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10095{
Alexander Gordeev7b37f372014-02-18 11:11:42 +010010096 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10097 I40E_MIN_MSIX, vectors);
10098 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010099 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +010010100 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010101 vectors = 0;
10102 }
10103
10104 return vectors;
10105}
10106
10107/**
10108 * i40e_init_msix - Setup the MSIX capability
10109 * @pf: board private structure
10110 *
10111 * Work with the OS to set up the MSIX vectors needed.
10112 *
Shannon Nelson3b444392015-02-26 16:15:57 +000010113 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010114 **/
10115static int i40e_init_msix(struct i40e_pf *pf)
10116{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010117 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -080010118 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010119 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010120 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +000010121 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010122 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010123
10124 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
10125 return -ENODEV;
10126
10127 /* The number of vectors we'll request will be comprised of:
10128 * - Add 1 for "other" cause for Admin Queue events, etc.
10129 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010130 * - Queues being used for RSS.
10131 * We don't need as many as max_rss_size vectors.
10132 * use rss_size instead in the calculation since that
10133 * is governed by number of cpus in the system.
10134 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010135 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010136 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010137 * Once we count this up, try the request.
10138 *
10139 * If we can't get what we want, we'll simplify to nearly nothing
10140 * and try again. If that still fails, we punt.
10141 */
Shannon Nelson1e200e42015-02-27 09:15:24 +000010142 vectors_left = hw->func_caps.num_msix_vectors;
10143 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010144
Shannon Nelson1e200e42015-02-27 09:15:24 +000010145 /* reserve one vector for miscellaneous handler */
10146 if (vectors_left) {
10147 v_budget++;
10148 vectors_left--;
10149 }
10150
Jacob Kellerc0cf70a2017-01-24 10:24:01 -080010151 /* reserve some vectors for the main PF traffic queues. Initially we
10152 * only reserve at most 50% of the available vectors, in the case that
10153 * the number of online CPUs is large. This ensures that we can enable
10154 * extra features as well. Once we've enabled the other features, we
10155 * will use any remaining vectors to reach as close as we can to the
10156 * number of online CPUs.
10157 */
10158 cpus = num_online_cpus();
10159 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +000010160 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010161
10162 /* reserve one vector for sideband flow director */
10163 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10164 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -070010165 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010166 v_budget++;
10167 vectors_left--;
10168 } else {
Tushar Davea70e4072016-05-16 12:40:53 -070010169 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010170 }
10171 }
John W Linville83840e42015-01-14 03:06:28 +000010172
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010173 /* can we reserve enough for iWARP? */
10174 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010175 iwarp_requested = pf->num_iwarp_msix;
10176
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010177 if (!vectors_left)
10178 pf->num_iwarp_msix = 0;
10179 else if (vectors_left < pf->num_iwarp_msix)
10180 pf->num_iwarp_msix = 1;
10181 v_budget += pf->num_iwarp_msix;
10182 vectors_left -= pf->num_iwarp_msix;
10183 }
10184
Shannon Nelson1e200e42015-02-27 09:15:24 +000010185 /* any vectors left over go for VMDq support */
10186 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
10187 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
10188 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
10189
Stefan Assmann9ca57e92016-09-19 13:37:49 +020010190 if (!vectors_left) {
10191 pf->num_vmdq_msix = 0;
10192 pf->num_vmdq_qps = 0;
10193 } else {
10194 /* if we're short on vectors for what's desired, we limit
10195 * the queues per vmdq. If this is still more than are
10196 * available, the user will need to change the number of
10197 * queues/vectors used by the PF later with the ethtool
10198 * channels command
10199 */
10200 if (vmdq_vecs < vmdq_vecs_wanted)
10201 pf->num_vmdq_qps = 1;
10202 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010203
Stefan Assmann9ca57e92016-09-19 13:37:49 +020010204 v_budget += vmdq_vecs;
10205 vectors_left -= vmdq_vecs;
10206 }
Shannon Nelson1e200e42015-02-27 09:15:24 +000010207 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010208
Jacob Kellerc0cf70a2017-01-24 10:24:01 -080010209 /* On systems with a large number of SMP cores, we previously limited
10210 * the number of vectors for num_lan_msix to be at most 50% of the
10211 * available vectors, to allow for other features. Now, we add back
10212 * the remaining vectors. However, we ensure that the total
10213 * num_lan_msix will not exceed num_online_cpus(). To do this, we
10214 * calculate the number of vectors we can add without going over the
10215 * cap of CPUs. For systems with a small number of CPUs this will be
10216 * zero.
10217 */
10218 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
10219 pf->num_lan_msix += extra_vectors;
10220 vectors_left -= extra_vectors;
10221
10222 WARN(vectors_left < 0,
10223 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
10224
10225 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010226 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
10227 GFP_KERNEL);
10228 if (!pf->msix_entries)
10229 return -ENOMEM;
10230
10231 for (i = 0; i < v_budget; i++)
10232 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +000010233 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010234
Shannon Nelson3b444392015-02-26 16:15:57 +000010235 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010236 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
10237 kfree(pf->msix_entries);
10238 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -030010239 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010240 return -ENODEV;
10241
Shannon Nelson3b444392015-02-26 16:15:57 +000010242 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010243 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010244 pf->num_vmdq_vsis = 0;
10245 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010246 pf->num_lan_qps = 1;
10247 pf->num_lan_msix = 1;
10248
Shannon Nelson3e6b1cf2017-10-10 14:56:58 -070010249 } else if (v_actual != v_budget) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010250 /* If we have limited resources, we will start with no vectors
10251 * for the special features and then allocate vectors to some
10252 * of these features based on the policy and at the end disable
10253 * the features that did not get any vectors.
10254 */
Shannon Nelson3b444392015-02-26 16:15:57 +000010255 int vec;
10256
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010257 dev_info(&pf->pdev->dev,
Shannon Nelson3e6b1cf2017-10-10 14:56:58 -070010258 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
10259 v_actual, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010260 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +000010261 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010262
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010263 /* Scale vector usage down */
10264 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010265 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +000010266 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010267
10268 /* partition out the remaining vectors */
10269 switch (vec) {
10270 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010271 pf->num_lan_msix = 1;
10272 break;
10273 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010274 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10275 pf->num_lan_msix = 1;
10276 pf->num_iwarp_msix = 1;
10277 } else {
10278 pf->num_lan_msix = 2;
10279 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010280 break;
10281 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010282 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
10283 pf->num_iwarp_msix = min_t(int, (vec / 3),
10284 iwarp_requested);
10285 pf->num_vmdq_vsis = min_t(int, (vec / 3),
10286 I40E_DEFAULT_NUM_VMDQ_VSI);
10287 } else {
10288 pf->num_vmdq_vsis = min_t(int, (vec / 2),
10289 I40E_DEFAULT_NUM_VMDQ_VSI);
10290 }
Stefan Assmannabd97a92016-09-19 13:37:51 +020010291 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10292 pf->num_fdsb_msix = 1;
10293 vec--;
10294 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010295 pf->num_lan_msix = min_t(int,
10296 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
10297 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010298 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010299 break;
10300 }
10301 }
10302
Stefan Assmannabd97a92016-09-19 13:37:51 +020010303 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
10304 (pf->num_fdsb_msix == 0)) {
10305 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
10306 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070010307 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Stefan Assmannabd97a92016-09-19 13:37:51 +020010308 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +000010309 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10310 (pf->num_vmdq_msix == 0)) {
10311 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
10312 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
10313 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010314
10315 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
10316 (pf->num_iwarp_msix == 0)) {
10317 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
10318 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
10319 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +020010320 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
10321 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
10322 pf->num_lan_msix,
10323 pf->num_vmdq_msix * pf->num_vmdq_vsis,
10324 pf->num_fdsb_msix,
10325 pf->num_iwarp_msix);
10326
Shannon Nelson3b444392015-02-26 16:15:57 +000010327 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010328}
10329
10330/**
Greg Rose90e04072014-03-06 08:59:57 +000010331 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +000010332 * @vsi: the VSI being configured
10333 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010334 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +000010335 *
10336 * We allocate one q_vector. If allocation fails we return -ENOMEM.
10337 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010338static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +000010339{
10340 struct i40e_q_vector *q_vector;
10341
10342 /* allocate q_vector */
10343 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
10344 if (!q_vector)
10345 return -ENOMEM;
10346
10347 q_vector->vsi = vsi;
10348 q_vector->v_idx = v_idx;
Jacob Keller759dc4a2017-07-14 09:10:10 -040010349 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010350
Alexander Duyck493fb302013-09-28 07:01:44 +000010351 if (vsi->netdev)
10352 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +000010353 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +000010354
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +000010355 q_vector->rx.latency_range = I40E_LOW_LATENCY;
10356 q_vector->tx.latency_range = I40E_LOW_LATENCY;
10357
Alexander Duyck493fb302013-09-28 07:01:44 +000010358 /* tie q_vector and vsi together */
10359 vsi->q_vectors[v_idx] = q_vector;
10360
10361 return 0;
10362}
10363
10364/**
Greg Rose90e04072014-03-06 08:59:57 +000010365 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010366 * @vsi: the VSI being configured
10367 *
10368 * We allocate one q_vector per queue interrupt. If allocation fails we
10369 * return -ENOMEM.
10370 **/
Greg Rose90e04072014-03-06 08:59:57 +000010371static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010372{
10373 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010374 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010375
10376 /* if not MSIX, give the one vector only to the LAN VSI */
10377 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10378 num_q_vectors = vsi->num_q_vectors;
10379 else if (vsi == pf->vsi[pf->lan_vsi])
10380 num_q_vectors = 1;
10381 else
10382 return -EINVAL;
10383
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010384 current_cpu = cpumask_first(cpu_online_mask);
10385
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010386 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010387 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +000010388 if (err)
10389 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -030010390 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
10391 if (unlikely(current_cpu >= nr_cpu_ids))
10392 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010393 }
10394
10395 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +000010396
10397err_out:
10398 while (v_idx--)
10399 i40e_free_q_vector(vsi, v_idx);
10400
10401 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010402}
10403
10404/**
10405 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
10406 * @pf: board private structure to initialize
10407 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010408static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010409{
Shannon Nelson3b444392015-02-26 16:15:57 +000010410 int vectors = 0;
10411 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010412
10413 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +000010414 vectors = i40e_init_msix(pf);
10415 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010416 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010417 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010418 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010419 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010420 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010421 I40E_FLAG_SRIOV_ENABLED |
10422 I40E_FLAG_FD_SB_ENABLED |
10423 I40E_FLAG_FD_ATR_ENABLED |
10424 I40E_FLAG_VMDQ_ENABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070010425 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010426
10427 /* rework the queue expectations without MSIX */
10428 i40e_determine_queue_usage(pf);
10429 }
10430 }
10431
10432 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10433 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -080010434 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +000010435 vectors = pci_enable_msi(pf->pdev);
10436 if (vectors < 0) {
10437 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
10438 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010439 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
10440 }
Shannon Nelson3b444392015-02-26 16:15:57 +000010441 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010442 }
10443
Shannon Nelson958a3e32013-09-28 07:13:28 +000010444 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -080010445 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +000010446
Shannon Nelson3b444392015-02-26 16:15:57 +000010447 /* set up vector assignment tracking */
10448 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
10449 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010450 if (!pf->irq_pile) {
10451 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
10452 return -ENOMEM;
10453 }
Shannon Nelson3b444392015-02-26 16:15:57 +000010454 pf->irq_pile->num_entries = vectors;
10455 pf->irq_pile->search_hint = 0;
10456
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010457 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +000010458 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010459
10460 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010461}
10462
10463/**
Jacob Kellerb980c062017-07-14 09:27:06 -040010464 * i40e_restore_interrupt_scheme - Restore the interrupt scheme
10465 * @pf: private board data structure
10466 *
10467 * Restore the interrupt scheme that was cleared when we suspended the
10468 * device. This should be called during resume to re-allocate the q_vectors
10469 * and reacquire IRQs.
10470 */
10471static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
10472{
10473 int err, i;
10474
10475 /* We cleared the MSI and MSI-X flags when disabling the old interrupt
10476 * scheme. We need to re-enabled them here in order to attempt to
10477 * re-acquire the MSI or MSI-X vectors
10478 */
10479 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
10480
10481 err = i40e_init_interrupt_scheme(pf);
10482 if (err)
10483 return err;
10484
10485 /* Now that we've re-acquired IRQs, we need to remap the vectors and
10486 * rings together again.
10487 */
10488 for (i = 0; i < pf->num_alloc_vsi; i++) {
10489 if (pf->vsi[i]) {
10490 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
10491 if (err)
10492 goto err_unwind;
10493 i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
10494 }
10495 }
10496
10497 err = i40e_setup_misc_vector(pf);
10498 if (err)
10499 goto err_unwind;
10500
10501 return 0;
10502
10503err_unwind:
10504 while (i--) {
10505 if (pf->vsi[i])
10506 i40e_vsi_free_q_vectors(pf->vsi[i]);
10507 }
10508
10509 return err;
10510}
Jacob Kellerb980c062017-07-14 09:27:06 -040010511
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010512/**
10513 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
10514 * @pf: board private structure
10515 *
10516 * This sets up the handler for MSIX 0, which is used to manage the
10517 * non-queue interrupts, e.g. AdminQ and errors. This is not used
10518 * when in MSI or Legacy interrupt mode.
10519 **/
10520static int i40e_setup_misc_vector(struct i40e_pf *pf)
10521{
10522 struct i40e_hw *hw = &pf->hw;
10523 int err = 0;
10524
Jacob Kellerc17401a2017-07-14 09:27:02 -040010525 /* Only request the IRQ once, the first time through. */
10526 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010527 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010528 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010529 if (err) {
Jacob Kellerc17401a2017-07-14 09:27:02 -040010530 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010531 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -080010532 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010533 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010534 return -EFAULT;
10535 }
10536 }
10537
Jacob Kellerab437b52014-12-14 01:55:08 +000010538 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010539
10540 /* associate no queues to the misc vector */
10541 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
10542 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
10543
10544 i40e_flush(hw);
10545
Jacob Kellerdbadbbe2017-09-07 08:05:49 -040010546 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010547
10548 return err;
10549}
10550
10551/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -080010552 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
10553 * @vsi: Pointer to vsi structure
10554 * @seed: Buffter to store the hash keys
10555 * @lut: Buffer to store the lookup table entries
10556 * @lut_size: Size of buffer to store the lookup table entries
10557 *
10558 * Return 0 on success, negative on failure
10559 */
10560static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
10561 u8 *lut, u16 lut_size)
10562{
10563 struct i40e_pf *pf = vsi->back;
10564 struct i40e_hw *hw = &pf->hw;
10565 int ret = 0;
10566
10567 if (seed) {
10568 ret = i40e_aq_get_rss_key(hw, vsi->id,
10569 (struct i40e_aqc_get_set_rss_key_data *)seed);
10570 if (ret) {
10571 dev_info(&pf->pdev->dev,
10572 "Cannot get RSS key, err %s aq_err %s\n",
10573 i40e_stat_str(&pf->hw, ret),
10574 i40e_aq_str(&pf->hw,
10575 pf->hw.aq.asq_last_status));
10576 return ret;
10577 }
10578 }
10579
10580 if (lut) {
10581 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
10582
10583 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
10584 if (ret) {
10585 dev_info(&pf->pdev->dev,
10586 "Cannot get RSS lut, err %s aq_err %s\n",
10587 i40e_stat_str(&pf->hw, ret),
10588 i40e_aq_str(&pf->hw,
10589 pf->hw.aq.asq_last_status));
10590 return ret;
10591 }
10592 }
10593
10594 return ret;
10595}
10596
10597/**
Helin Zhang043dd652015-10-21 19:56:23 -040010598 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -040010599 * @vsi: Pointer to vsi structure
10600 * @seed: RSS hash seed
10601 * @lut: Lookup table
10602 * @lut_size: Lookup table size
10603 *
10604 * Returns 0 on success, negative on failure
10605 **/
10606static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
10607 const u8 *lut, u16 lut_size)
10608{
10609 struct i40e_pf *pf = vsi->back;
10610 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -070010611 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -040010612 u8 i;
10613
10614 /* Fill out hash function seed */
10615 if (seed) {
10616 u32 *seed_dw = (u32 *)seed;
10617
Mitch Williamsc4e18682016-04-12 08:30:40 -070010618 if (vsi->type == I40E_VSI_MAIN) {
10619 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -080010620 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -070010621 } else if (vsi->type == I40E_VSI_SRIOV) {
10622 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -080010623 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -070010624 } else {
10625 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
10626 }
Helin Zhange69ff812015-10-21 19:56:22 -040010627 }
10628
10629 if (lut) {
10630 u32 *lut_dw = (u32 *)lut;
10631
Mitch Williamsc4e18682016-04-12 08:30:40 -070010632 if (vsi->type == I40E_VSI_MAIN) {
10633 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10634 return -EINVAL;
10635 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10636 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
10637 } else if (vsi->type == I40E_VSI_SRIOV) {
10638 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
10639 return -EINVAL;
10640 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -080010641 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -070010642 } else {
10643 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
10644 }
Helin Zhange69ff812015-10-21 19:56:22 -040010645 }
10646 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010647
10648 return 0;
10649}
10650
10651/**
Helin Zhang043dd652015-10-21 19:56:23 -040010652 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
10653 * @vsi: Pointer to VSI structure
10654 * @seed: Buffer to store the keys
10655 * @lut: Buffer to store the lookup table entries
10656 * @lut_size: Size of buffer to store the lookup table entries
10657 *
10658 * Returns 0 on success, negative on failure
10659 */
10660static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
10661 u8 *lut, u16 lut_size)
10662{
10663 struct i40e_pf *pf = vsi->back;
10664 struct i40e_hw *hw = &pf->hw;
10665 u16 i;
10666
10667 if (seed) {
10668 u32 *seed_dw = (u32 *)seed;
10669
10670 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010671 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -040010672 }
10673 if (lut) {
10674 u32 *lut_dw = (u32 *)lut;
10675
10676 if (lut_size != I40E_HLUT_ARRAY_SIZE)
10677 return -EINVAL;
10678 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
10679 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
10680 }
10681
10682 return 0;
10683}
10684
10685/**
10686 * i40e_config_rss - Configure RSS keys and lut
10687 * @vsi: Pointer to VSI structure
10688 * @seed: RSS hash seed
10689 * @lut: Lookup table
10690 * @lut_size: Lookup table size
10691 *
10692 * Returns 0 on success, negative on failure
10693 */
10694int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10695{
10696 struct i40e_pf *pf = vsi->back;
10697
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010698 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Helin Zhang043dd652015-10-21 19:56:23 -040010699 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
10700 else
10701 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
10702}
10703
10704/**
10705 * i40e_get_rss - Get RSS keys and lut
10706 * @vsi: Pointer to VSI structure
10707 * @seed: Buffer to store the keys
10708 * @lut: Buffer to store the lookup table entries
10709 * lut_size: Size of buffer to store the lookup table entries
10710 *
10711 * Returns 0 on success, negative on failure
10712 */
10713int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
10714{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -080010715 struct i40e_pf *pf = vsi->back;
10716
Jacob Kellerd36e41d2017-06-23 04:24:46 -040010717 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
Anjali Singhai Jain95a73782015-12-22 14:25:04 -080010718 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
10719 else
10720 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -040010721}
10722
10723/**
Helin Zhange69ff812015-10-21 19:56:22 -040010724 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
10725 * @pf: Pointer to board private structure
10726 * @lut: Lookup table
10727 * @rss_table_size: Lookup table size
10728 * @rss_size: Range of queue number for hashing
10729 */
Alan Bradyf1582352016-08-24 11:33:46 -070010730void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
10731 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010732{
Helin Zhange69ff812015-10-21 19:56:22 -040010733 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010734
Helin Zhange69ff812015-10-21 19:56:22 -040010735 for (i = 0; i < rss_table_size; i++)
10736 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010737}
10738
10739/**
Helin Zhang043dd652015-10-21 19:56:23 -040010740 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010741 * @pf: board private structure
10742 **/
Helin Zhang043dd652015-10-21 19:56:23 -040010743static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010744{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +000010745 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010746 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -040010747 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +000010748 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +000010749 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010750 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -040010751 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010753 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010754 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
10755 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010756 hena |= i40e_pf_get_default_rss_hena(pf);
10757
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010758 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
10759 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010760
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010761 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010762 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010763 reg_val = (pf->rss_table_size == 512) ?
10764 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
10765 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -080010766 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +000010767
Helin Zhang28c58692015-10-26 19:44:27 -040010768 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010769 if (!vsi->rss_size) {
10770 u16 qcount;
10771
10772 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10773 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
10774 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -070010775 if (!vsi->rss_size)
10776 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -040010777
Helin Zhange69ff812015-10-21 19:56:22 -040010778 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
10779 if (!lut)
10780 return -ENOMEM;
10781
Helin Zhang28c58692015-10-26 19:44:27 -040010782 /* Use user configured lut if there is one, otherwise use default */
10783 if (vsi->rss_lut_user)
10784 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
10785 else
10786 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -040010787
Helin Zhang28c58692015-10-26 19:44:27 -040010788 /* Use user configured hash key if there is one, otherwise
10789 * use default.
10790 */
10791 if (vsi->rss_hkey_user)
10792 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
10793 else
10794 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -040010795 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -040010796 kfree(lut);
10797
10798 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010799}
10800
10801/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010802 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
10803 * @pf: board private structure
10804 * @queue_count: the requested queue count for rss.
10805 *
10806 * returns 0 if rss is not enabled, if enabled returns the final rss queue
10807 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -040010808 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010809 **/
10810int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
10811{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010812 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10813 int new_rss_size;
10814
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010815 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
10816 return 0;
10817
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010818 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010819
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010820 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010821 u16 qcount;
10822
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010823 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -040010824 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010825
Helin Zhangacd65442015-10-26 19:44:28 -040010826 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010827
Maciej Sosin373149f2017-04-05 07:50:55 -040010828 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -040010829
10830 /* Discard the user configured hash keys and lut, if less
10831 * queues are enabled.
10832 */
10833 if (queue_count < vsi->rss_size) {
10834 i40e_clear_rss_config_user(vsi);
10835 dev_dbg(&pf->pdev->dev,
10836 "discard user configured hash keys and lut\n");
10837 }
10838
10839 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -080010840 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
10841 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -040010842
Helin Zhang043dd652015-10-21 19:56:23 -040010843 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010844 }
Lihong Yang12815052016-09-27 11:28:48 -070010845 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
10846 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -040010847 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010848}
10849
10850/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010851 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010852 * @pf: board private structure
10853 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010854i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010855{
10856 i40e_status status;
10857 bool min_valid, max_valid;
10858 u32 max_bw, min_bw;
10859
10860 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
10861 &min_valid, &max_valid);
10862
10863 if (!status) {
10864 if (min_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010865 pf->min_bw = min_bw;
Greg Rosef4492db2015-02-06 08:52:12 +000010866 if (max_valid)
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010867 pf->max_bw = max_bw;
Greg Rosef4492db2015-02-06 08:52:12 +000010868 }
10869
10870 return status;
10871}
10872
10873/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010874 * i40e_set_partition_bw_setting - Set BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010875 * @pf: board private structure
10876 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010877i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010878{
10879 struct i40e_aqc_configure_partition_bw_data bw_data;
10880 i40e_status status;
10881
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010882 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040010883 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010884 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
10885 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
Greg Rosef4492db2015-02-06 08:52:12 +000010886
10887 /* Set the new bandwidths */
10888 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
10889
10890 return status;
10891}
10892
10893/**
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010894 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
Greg Rosef4492db2015-02-06 08:52:12 +000010895 * @pf: board private structure
10896 **/
Shannon Nelson4fc8c672017-06-07 05:43:08 -040010897i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
Greg Rosef4492db2015-02-06 08:52:12 +000010898{
10899 /* Commit temporary BW setting to permanent NVM image */
10900 enum i40e_admin_queue_err last_aq_status;
10901 i40e_status ret;
10902 u16 nvm_word;
10903
10904 if (pf->hw.partition_id != 1) {
10905 dev_info(&pf->pdev->dev,
10906 "Commit BW only works on partition 1! This is partition %d",
10907 pf->hw.partition_id);
10908 ret = I40E_NOT_SUPPORTED;
10909 goto bw_commit_out;
10910 }
10911
10912 /* Acquire NVM for read access */
10913 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
10914 last_aq_status = pf->hw.aq.asq_last_status;
10915 if (ret) {
10916 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010917 "Cannot acquire NVM for read access, err %s aq_err %s\n",
10918 i40e_stat_str(&pf->hw, ret),
10919 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010920 goto bw_commit_out;
10921 }
10922
10923 /* Read word 0x10 of NVM - SW compatibility word 1 */
10924 ret = i40e_aq_read_nvm(&pf->hw,
10925 I40E_SR_NVM_CONTROL_WORD,
10926 0x10, sizeof(nvm_word), &nvm_word,
10927 false, NULL);
10928 /* Save off last admin queue command status before releasing
10929 * the NVM
10930 */
10931 last_aq_status = pf->hw.aq.asq_last_status;
10932 i40e_release_nvm(&pf->hw);
10933 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010934 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
10935 i40e_stat_str(&pf->hw, ret),
10936 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010937 goto bw_commit_out;
10938 }
10939
10940 /* Wait a bit for NVM release to complete */
10941 msleep(50);
10942
10943 /* Acquire NVM for write access */
10944 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
10945 last_aq_status = pf->hw.aq.asq_last_status;
10946 if (ret) {
10947 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010948 "Cannot acquire NVM for write access, err %s aq_err %s\n",
10949 i40e_stat_str(&pf->hw, ret),
10950 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010951 goto bw_commit_out;
10952 }
10953 /* Write it back out unchanged to initiate update NVM,
10954 * which will force a write of the shadow (alt) RAM to
10955 * the NVM - thus storing the bandwidth values permanently.
10956 */
10957 ret = i40e_aq_update_nvm(&pf->hw,
10958 I40E_SR_NVM_CONTROL_WORD,
10959 0x10, sizeof(nvm_word),
10960 &nvm_word, true, NULL);
10961 /* Save off last admin queue command status before releasing
10962 * the NVM
10963 */
10964 last_aq_status = pf->hw.aq.asq_last_status;
10965 i40e_release_nvm(&pf->hw);
10966 if (ret)
10967 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010968 "BW settings NOT SAVED, err %s aq_err %s\n",
10969 i40e_stat_str(&pf->hw, ret),
10970 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +000010971bw_commit_out:
10972
10973 return ret;
10974}
10975
10976/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010977 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
10978 * @pf: board private structure to initialize
10979 *
10980 * i40e_sw_init initializes the Adapter private data structure.
10981 * Fields are initialized based on PCI device information and
10982 * OS network device settings (MTU size).
10983 **/
10984static int i40e_sw_init(struct i40e_pf *pf)
10985{
10986 int err = 0;
10987 int size;
10988
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010989 /* Set default capability flags */
10990 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
10991 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +000010992 I40E_FLAG_MSIX_ENABLED;
10993
Mitch Williamsca99eb92014-04-04 04:43:07 +000010994 /* Set default ITR */
Jacob Keller42702552017-09-07 08:05:48 -040010995 pf->rx_itr_default = I40E_ITR_RX_DEF;
10996 pf->tx_itr_default = I40E_ITR_TX_DEF;
Mitch Williamsca99eb92014-04-04 04:43:07 +000010997
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010998 /* Depending on PF configurations, it is possible that the RSS
10999 * maximum might end up larger than the available queues
11000 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040011001 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -040011002 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +000011003 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011004 pf->rss_size_max = min_t(int, pf->rss_size_max,
11005 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011006 if (pf->hw.func_caps.rss) {
11007 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -040011008 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11009 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011010 }
11011
Catherine Sullivan2050bc62013-12-18 13:46:03 +000011012 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -040011013 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +000011014 pf->flags |= I40E_FLAG_MFP_ENABLED;
11015 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Shannon Nelson4fc8c672017-06-07 05:43:08 -040011016 if (i40e_get_partition_bw_setting(pf)) {
Greg Rosef4492db2015-02-06 08:52:12 +000011017 dev_warn(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -040011018 "Could not get partition bw settings\n");
11019 } else {
Greg Rosef4492db2015-02-06 08:52:12 +000011020 dev_info(&pf->pdev->dev,
Shannon Nelson4fc8c672017-06-07 05:43:08 -040011021 "Partition BW Min = %8.8x, Max = %8.8x\n",
11022 pf->min_bw, pf->max_bw);
11023
11024 /* nudge the Tx scheduler */
11025 i40e_set_partition_bw_setting(pf);
11026 }
Catherine Sullivan2050bc62013-12-18 13:46:03 +000011027 }
11028
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011029 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
11030 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
11031 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
11032 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -040011033 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
11034 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011035 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +000011036 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -040011037 else
11038 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080011039 pf->fdir_pf_filter_count =
11040 pf->hw.func_caps.fd_filters_guaranteed;
11041 pf->hw.fdir_shared_filter_count =
11042 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 }
11044
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040011045 if (pf->hw.mac.type == I40E_MAC_X722) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011046 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
11047 I40E_HW_128_QP_RSS_CAPABLE |
11048 I40E_HW_ATR_EVICT_CAPABLE |
11049 I40E_HW_WB_ON_ITR_CAPABLE |
11050 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
11051 I40E_HW_NO_PCI_LINK_CHECK |
11052 I40E_HW_USE_SET_LLDP_MIB |
11053 I40E_HW_GENEVE_OFFLOAD_CAPABLE |
11054 I40E_HW_PTP_L4_CAPABLE |
11055 I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
11056 I40E_HW_OUTER_UDP_CSUM_CAPABLE);
Anjali Singhai Jain10a955f2017-06-23 04:24:48 -040011057
11058#define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
11059 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
11060 I40E_FDEVICT_PCTYPE_DEFAULT) {
11061 dev_warn(&pf->pdev->dev,
11062 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
11063 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
11064 }
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040011065 } else if ((pf->hw.aq.api_maj_ver > 1) ||
11066 ((pf->hw.aq.api_maj_ver == 1) &&
11067 (pf->hw.aq.api_min_ver > 4))) {
11068 /* Supported in FW API version higher than 1.4 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011069 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040011070 }
11071
11072 /* Enable HW ATR eviction if possible */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011073 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
Anjali Singhai Jain5a433192017-06-23 04:24:45 -040011074 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
11075
Bimmy Pujari6de432c2016-11-11 12:39:38 -080011076 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011077 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011078 (pf->hw.aq.fw_maj_ver < 4))) {
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011079 pf->hw_features |= I40E_HW_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011080 /* No DCB support for FW < v4.33 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011081 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011082 }
11083
11084 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -080011085 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011086 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
11087 (pf->hw.aq.fw_maj_ver < 4)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011088 pf->hw_features |= I40E_HW_STOP_FW_LLDP;
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011089
11090 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -080011091 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011092 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
11093 (pf->hw.aq.fw_maj_ver >= 5)))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011094 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011095
Alan Bradyc3d26b72017-08-29 05:32:32 -040011096 /* Enable PTP L4 if FW > v6.0 */
11097 if (pf->hw.mac.type == I40E_MAC_XL710 &&
11098 pf->hw.aq.fw_maj_ver >= 6)
11099 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
11100
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011101 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011102 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040011103 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -070011104 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011105 }
11106
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011107 if (pf->hw.func_caps.iwarp) {
11108 pf->flags |= I40E_FLAG_IWARP_ENABLED;
11109 /* IWARP needs one extra vector for CQP just like MISC.*/
11110 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
11111 }
11112
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +000011114 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011115 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
11116 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
11117 pf->num_req_vfs = min_t(int,
11118 pf->hw.func_caps.num_vfs,
11119 I40E_MAX_VF_COUNT);
11120 }
11121#endif /* CONFIG_PCI_IOV */
11122 pf->eeprom_version = 0xDEAD;
11123 pf->lan_veb = I40E_NO_VEB;
11124 pf->lan_vsi = I40E_NO_VSI;
11125
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -040011126 /* By default FW has this off for performance reasons */
11127 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
11128
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011129 /* set up queue assignment tracking */
11130 size = sizeof(struct i40e_lump_tracking)
11131 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
11132 pf->qp_pile = kzalloc(size, GFP_KERNEL);
11133 if (!pf->qp_pile) {
11134 err = -ENOMEM;
11135 goto sw_init_done;
11136 }
11137 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
11138 pf->qp_pile->search_hint = 0;
11139
Anjali Singhai Jain327fe042014-06-04 01:23:26 +000011140 pf->tx_timeout_recovery_level = 1;
11141
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011142 mutex_init(&pf->switch_mutex);
11143
11144sw_init_done:
11145 return err;
11146}
11147
11148/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011149 * i40e_set_ntuple - set the ntuple feature flag and take action
11150 * @pf: board private structure to initialize
11151 * @features: the feature set that the stack is suggesting
11152 *
11153 * returns a bool to indicate if reset needs to happen
11154 **/
11155bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
11156{
11157 bool need_reset = false;
11158
11159 /* Check if Flow Director n-tuple support was enabled or disabled. If
11160 * the state changed, we need to reset.
11161 */
11162 if (features & NETIF_F_NTUPLE) {
11163 /* Enable filters and mark for reset */
11164 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
11165 need_reset = true;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011166 /* enable FD_SB only if there is MSI-X vector and no cloud
11167 * filters exist
11168 */
11169 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
Tushar Davea70e4072016-05-16 12:40:53 -070011170 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011171 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
11172 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011173 } else {
11174 /* turn off filters, mark for reset and clear SW filter list */
11175 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11176 need_reset = true;
11177 i40e_fdir_filter_exit(pf);
11178 }
Jacob Keller47994c12017-04-19 09:25:57 -040011179 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
11180 I40E_FLAG_FD_SB_AUTO_DISABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011181 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11182
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +000011183 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -080011184 pf->fd_add_err = 0;
11185 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +000011186 /* if ATR was auto disabled it can be re-enabled. */
Jacob Keller47994c12017-04-19 09:25:57 -040011187 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
11188 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
11189 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
11190 (I40E_DEBUG_FD & pf->hw.debug_mask))
Jacob Keller234dc4e2016-09-06 18:05:09 -070011191 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
11192 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011193 }
11194 return need_reset;
11195}
11196
11197/**
Alan Bradyd8ec9862016-07-27 12:02:38 -070011198 * i40e_clear_rss_lut - clear the rx hash lookup table
11199 * @vsi: the VSI being configured
11200 **/
11201static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
11202{
11203 struct i40e_pf *pf = vsi->back;
11204 struct i40e_hw *hw = &pf->hw;
11205 u16 vf_id = vsi->vf_id;
11206 u8 i;
11207
11208 if (vsi->type == I40E_VSI_MAIN) {
11209 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11210 wr32(hw, I40E_PFQF_HLUT(i), 0);
11211 } else if (vsi->type == I40E_VSI_SRIOV) {
11212 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11213 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
11214 } else {
11215 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11216 }
11217}
11218
11219/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011220 * i40e_set_features - set the netdev feature flags
11221 * @netdev: ptr to the netdev being adjusted
11222 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -040011223 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011224 **/
11225static int i40e_set_features(struct net_device *netdev,
11226 netdev_features_t features)
11227{
11228 struct i40e_netdev_priv *np = netdev_priv(netdev);
11229 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011230 struct i40e_pf *pf = vsi->back;
11231 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011232
Alan Bradyd8ec9862016-07-27 12:02:38 -070011233 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
11234 i40e_pf_config_rss(pf);
11235 else if (!(features & NETIF_F_RXHASH) &&
11236 netdev->features & NETIF_F_RXHASH)
11237 i40e_clear_rss_lut(vsi);
11238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011239 if (features & NETIF_F_HW_VLAN_CTAG_RX)
11240 i40e_vlan_stripping_enable(vsi);
11241 else
11242 i40e_vlan_stripping_disable(vsi);
11243
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011244 if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
11245 dev_err(&pf->pdev->dev,
11246 "Offloaded tc filters active, can't turn hw_tc_offload off");
11247 return -EINVAL;
11248 }
11249
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011250 need_reset = i40e_set_ntuple(pf, features);
11251
11252 if (need_reset)
Amritha Nambiarff424182017-09-07 04:00:11 -070011253 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +000011254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011255 return 0;
11256}
11257
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011258/**
Singhai, Anjali6a899022015-12-14 12:21:18 -080011259 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011260 * @pf: board private structure
11261 * @port: The UDP port to look up
11262 *
11263 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
11264 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011265static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011266{
11267 u8 i;
11268
11269 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -040011270 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011271 return i;
11272 }
11273
11274 return i;
11275}
11276
11277/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011278 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011279 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011280 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011281 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011282static void i40e_udp_tunnel_add(struct net_device *netdev,
11283 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011284{
11285 struct i40e_netdev_priv *np = netdev_priv(netdev);
11286 struct i40e_vsi *vsi = np->vsi;
11287 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011288 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011289 u8 next_idx;
11290 u8 idx;
11291
Singhai, Anjali6a899022015-12-14 12:21:18 -080011292 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011293
11294 /* Check if port already exists */
11295 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011296 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011297 return;
11298 }
11299
11300 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -080011301 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011302
11303 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011304 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011305 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011306 return;
11307 }
11308
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011309 switch (ti->type) {
11310 case UDP_TUNNEL_TYPE_VXLAN:
11311 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
11312 break;
11313 case UDP_TUNNEL_TYPE_GENEVE:
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011314 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011315 return;
11316 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
11317 break;
11318 default:
11319 return;
11320 }
11321
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011322 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -040011323 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -080011324 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
11325 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011326}
11327
11328/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011329 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011330 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011331 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011332 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011333static void i40e_udp_tunnel_del(struct net_device *netdev,
11334 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011335{
11336 struct i40e_netdev_priv *np = netdev_priv(netdev);
11337 struct i40e_vsi *vsi = np->vsi;
11338 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011339 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011340 u8 idx;
11341
Singhai, Anjali6a899022015-12-14 12:21:18 -080011342 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011343
11344 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011345 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
11346 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000011347
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011348 switch (ti->type) {
11349 case UDP_TUNNEL_TYPE_VXLAN:
11350 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
11351 goto not_found;
11352 break;
11353 case UDP_TUNNEL_TYPE_GENEVE:
11354 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
11355 goto not_found;
11356 break;
11357 default:
11358 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -080011359 }
11360
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011361 /* if port exists, set it to 0 (mark for deletion)
11362 * and make it pending
11363 */
Jacob Keller27826fd2017-04-19 09:25:50 -040011364 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011365 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -080011366 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
11367
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011368 return;
11369not_found:
11370 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -080011371 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -080011372}
11373
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011374static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +010011375 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011376{
11377 struct i40e_netdev_priv *np = netdev_priv(netdev);
11378 struct i40e_pf *pf = np->vsi->back;
11379 struct i40e_hw *hw = &pf->hw;
11380
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011381 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011382 return -EOPNOTSUPP;
11383
11384 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
11385 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
11386
11387 return 0;
11388}
11389
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -080011390/**
11391 * i40e_ndo_fdb_add - add an entry to the hardware database
11392 * @ndm: the input from the stack
11393 * @tb: pointer to array of nladdr (unused)
11394 * @dev: the net device pointer
11395 * @addr: the MAC address entry being added
11396 * @flags: instructions from stack about fdb operation
11397 */
Greg Rose4ba0dea2014-03-06 08:59:55 +000011398static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
11399 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +010011400 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +000011401 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +000011402{
11403 struct i40e_netdev_priv *np = netdev_priv(dev);
11404 struct i40e_pf *pf = np->vsi->back;
11405 int err = 0;
11406
11407 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
11408 return -EOPNOTSUPP;
11409
Or Gerlitz65891fe2014-12-14 18:19:05 +020011410 if (vid) {
11411 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
11412 return -EINVAL;
11413 }
11414
Greg Rose4ba0dea2014-03-06 08:59:55 +000011415 /* Hardware does not support aging addresses so if a
11416 * ndm_state is given only allow permanent addresses
11417 */
11418 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
11419 netdev_info(dev, "FDB only supports static addresses\n");
11420 return -EINVAL;
11421 }
11422
11423 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
11424 err = dev_uc_add_excl(dev, addr);
11425 else if (is_multicast_ether_addr(addr))
11426 err = dev_mc_add_excl(dev, addr);
11427 else
11428 err = -EINVAL;
11429
11430 /* Only return duplicate errors if NLM_F_EXCL is set */
11431 if (err == -EEXIST && !(flags & NLM_F_EXCL))
11432 err = 0;
11433
11434 return err;
11435}
11436
Neerav Parikh51616012015-02-06 08:52:14 +000011437/**
11438 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
11439 * @dev: the netdev being configured
11440 * @nlh: RTNL message
11441 *
11442 * Inserts a new hardware bridge if not already created and
11443 * enables the bridging mode requested (VEB or VEPA). If the
11444 * hardware bridge has already been inserted and the request
11445 * is to change the mode then that requires a PF reset to
11446 * allow rebuild of the components with required hardware
11447 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -040011448 *
11449 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +000011450 **/
11451static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -040011452 struct nlmsghdr *nlh,
11453 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +000011454{
11455 struct i40e_netdev_priv *np = netdev_priv(dev);
11456 struct i40e_vsi *vsi = np->vsi;
11457 struct i40e_pf *pf = vsi->back;
11458 struct i40e_veb *veb = NULL;
11459 struct nlattr *attr, *br_spec;
11460 int i, rem;
11461
11462 /* Only for PF VSI for now */
11463 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11464 return -EOPNOTSUPP;
11465
11466 /* Find the HW bridge for PF VSI */
11467 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11468 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11469 veb = pf->veb[i];
11470 }
11471
11472 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11473
11474 nla_for_each_nested(attr, br_spec, rem) {
11475 __u16 mode;
11476
11477 if (nla_type(attr) != IFLA_BRIDGE_MODE)
11478 continue;
11479
11480 mode = nla_get_u16(attr);
11481 if ((mode != BRIDGE_MODE_VEPA) &&
11482 (mode != BRIDGE_MODE_VEB))
11483 return -EINVAL;
11484
11485 /* Insert a new HW bridge */
11486 if (!veb) {
11487 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
11488 vsi->tc_config.enabled_tc);
11489 if (veb) {
11490 veb->bridge_mode = mode;
11491 i40e_config_bridge_mode(veb);
11492 } else {
11493 /* No Bridge HW offload available */
11494 return -ENOENT;
11495 }
11496 break;
11497 } else if (mode != veb->bridge_mode) {
11498 /* Existing HW bridge but different mode needs reset */
11499 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070011500 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
11501 if (mode == BRIDGE_MODE_VEB)
11502 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11503 else
11504 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Amritha Nambiarff424182017-09-07 04:00:11 -070011505 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
Neerav Parikh51616012015-02-06 08:52:14 +000011506 break;
11507 }
11508 }
11509
11510 return 0;
11511}
11512
11513/**
11514 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
11515 * @skb: skb buff
11516 * @pid: process id
11517 * @seq: RTNL message seq #
11518 * @dev: the netdev being configured
11519 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -040011520 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +000011521 *
11522 * Return the mode in which the hardware bridge is operating in
11523 * i.e VEB or VEPA.
11524 **/
Neerav Parikh51616012015-02-06 08:52:14 +000011525static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11526 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -040011527 u32 __always_unused filter_mask,
11528 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +000011529{
11530 struct i40e_netdev_priv *np = netdev_priv(dev);
11531 struct i40e_vsi *vsi = np->vsi;
11532 struct i40e_pf *pf = vsi->back;
11533 struct i40e_veb *veb = NULL;
11534 int i;
11535
11536 /* Only for PF VSI for now */
11537 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
11538 return -EOPNOTSUPP;
11539
11540 /* Find the HW bridge for the PF VSI */
11541 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
11542 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
11543 veb = pf->veb[i];
11544 }
11545
11546 if (!veb)
11547 return 0;
11548
Nicolas Dichtel46c264d2015-04-28 18:33:49 +020011549 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +020011550 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +000011551}
Neerav Parikh51616012015-02-06 08:52:14 +000011552
Joe Stringerf44a75e2015-04-14 17:09:14 -070011553/**
11554 * i40e_features_check - Validate encapsulated packet conforms to limits
11555 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -060011556 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -070011557 * @features: Offload features that the stack believes apply
11558 **/
11559static netdev_features_t i40e_features_check(struct sk_buff *skb,
11560 struct net_device *dev,
11561 netdev_features_t features)
11562{
Alexander Duyckf114dca2016-10-25 16:08:46 -070011563 size_t len;
11564
11565 /* No point in doing any of this if neither checksum nor GSO are
11566 * being requested for this frame. We can rule out both by just
11567 * checking for CHECKSUM_PARTIAL
11568 */
11569 if (skb->ip_summed != CHECKSUM_PARTIAL)
11570 return features;
11571
11572 /* We cannot support GSO if the MSS is going to be less than
11573 * 64 bytes. If it is then we need to drop support for GSO.
11574 */
11575 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
11576 features &= ~NETIF_F_GSO_MASK;
11577
11578 /* MACLEN can support at most 63 words */
11579 len = skb_network_header(skb) - skb->data;
11580 if (len & ~(63 * 2))
11581 goto out_err;
11582
11583 /* IPLEN and EIPLEN can support at most 127 dwords */
11584 len = skb_transport_header(skb) - skb_network_header(skb);
11585 if (len & ~(127 * 4))
11586 goto out_err;
11587
11588 if (skb->encapsulation) {
11589 /* L4TUNLEN can support 127 words */
11590 len = skb_inner_network_header(skb) - skb_transport_header(skb);
11591 if (len & ~(127 * 2))
11592 goto out_err;
11593
11594 /* IPLEN can support at most 127 dwords */
11595 len = skb_inner_transport_header(skb) -
11596 skb_inner_network_header(skb);
11597 if (len & ~(127 * 4))
11598 goto out_err;
11599 }
11600
11601 /* No need to validate L4LEN as TCP is the only protocol with a
11602 * a flexible value and we support all possible values supported
11603 * by TCP, which is at most 15 dwords
11604 */
Joe Stringerf44a75e2015-04-14 17:09:14 -070011605
11606 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -070011607out_err:
11608 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -070011609}
11610
Björn Töpel0c8493d2017-05-24 07:55:34 +020011611/**
11612 * i40e_xdp_setup - add/remove an XDP program
11613 * @vsi: VSI to changed
11614 * @prog: XDP program
11615 **/
11616static int i40e_xdp_setup(struct i40e_vsi *vsi,
11617 struct bpf_prog *prog)
11618{
11619 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
11620 struct i40e_pf *pf = vsi->back;
11621 struct bpf_prog *old_prog;
11622 bool need_reset;
11623 int i;
11624
11625 /* Don't allow frames that span over multiple buffers */
11626 if (frame_size > vsi->rx_buf_len)
11627 return -EINVAL;
11628
11629 if (!i40e_enabled_xdp_vsi(vsi) && !prog)
11630 return 0;
11631
11632 /* When turning XDP on->off/off->on we reset and rebuild the rings. */
11633 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
11634
11635 if (need_reset)
11636 i40e_prep_for_reset(pf, true);
11637
11638 old_prog = xchg(&vsi->xdp_prog, prog);
11639
11640 if (need_reset)
11641 i40e_reset_and_rebuild(pf, true, true);
11642
11643 for (i = 0; i < vsi->num_queue_pairs; i++)
11644 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
11645
11646 if (old_prog)
11647 bpf_prog_put(old_prog);
11648
11649 return 0;
11650}
11651
11652/**
Jakub Kicinskif4e63522017-11-03 13:56:16 -070011653 * i40e_xdp - implements ndo_bpf for i40e
Björn Töpel0c8493d2017-05-24 07:55:34 +020011654 * @dev: netdevice
11655 * @xdp: XDP command
11656 **/
11657static int i40e_xdp(struct net_device *dev,
Jakub Kicinskif4e63522017-11-03 13:56:16 -070011658 struct netdev_bpf *xdp)
Björn Töpel0c8493d2017-05-24 07:55:34 +020011659{
11660 struct i40e_netdev_priv *np = netdev_priv(dev);
11661 struct i40e_vsi *vsi = np->vsi;
11662
11663 if (vsi->type != I40E_VSI_MAIN)
11664 return -EINVAL;
11665
11666 switch (xdp->command) {
11667 case XDP_SETUP_PROG:
11668 return i40e_xdp_setup(vsi, xdp->prog);
11669 case XDP_QUERY_PROG:
11670 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
Daniel Borkmanneb230392017-06-24 21:13:52 +020011671 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
Björn Töpel0c8493d2017-05-24 07:55:34 +020011672 return 0;
11673 default:
11674 return -EINVAL;
11675 }
11676}
11677
Shannon Nelson37a29732015-02-27 09:15:19 +000011678static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011679 .ndo_open = i40e_open,
11680 .ndo_stop = i40e_close,
11681 .ndo_start_xmit = i40e_lan_xmit_frame,
11682 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
11683 .ndo_set_rx_mode = i40e_set_rx_mode,
11684 .ndo_validate_addr = eth_validate_addr,
11685 .ndo_set_mac_address = i40e_set_mac,
11686 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011687 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011688 .ndo_tx_timeout = i40e_tx_timeout,
11689 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
11690 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
11691#ifdef CONFIG_NET_POLL_CONTROLLER
11692 .ndo_poll_controller = i40e_netpoll,
11693#endif
John Fastabende4c67342016-02-16 21:16:15 -080011694 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011695 .ndo_set_features = i40e_set_features,
11696 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
11697 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -040011698 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011699 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +000011700 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +000011701 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -070011702 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070011703 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
11704 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011705 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +000011706 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -070011707 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +000011708 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
11709 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jakub Kicinskif4e63522017-11-03 13:56:16 -070011710 .ndo_bpf = i40e_xdp,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011711};
11712
11713/**
11714 * i40e_config_netdev - Setup the netdev flags
11715 * @vsi: the VSI being configured
11716 *
11717 * Returns 0 on success, negative value on failure
11718 **/
11719static int i40e_config_netdev(struct i40e_vsi *vsi)
11720{
11721 struct i40e_pf *pf = vsi->back;
11722 struct i40e_hw *hw = &pf->hw;
11723 struct i40e_netdev_priv *np;
11724 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -080011725 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011726 u8 mac_addr[ETH_ALEN];
11727 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -070011728 netdev_features_t hw_enc_features;
11729 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011730
11731 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000011732 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011733 if (!netdev)
11734 return -ENOMEM;
11735
11736 vsi->netdev = netdev;
11737 np = netdev_priv(netdev);
11738 np->vsi = vsi;
11739
Preethi Banalabacd75c2017-03-27 14:43:18 -070011740 hw_enc_features = NETIF_F_SG |
11741 NETIF_F_IP_CSUM |
11742 NETIF_F_IPV6_CSUM |
11743 NETIF_F_HIGHDMA |
11744 NETIF_F_SOFT_FEATURES |
11745 NETIF_F_TSO |
11746 NETIF_F_TSO_ECN |
11747 NETIF_F_TSO6 |
11748 NETIF_F_GSO_GRE |
11749 NETIF_F_GSO_GRE_CSUM |
11750 NETIF_F_GSO_PARTIAL |
11751 NETIF_F_GSO_UDP_TUNNEL |
11752 NETIF_F_GSO_UDP_TUNNEL_CSUM |
11753 NETIF_F_SCTP_CRC |
11754 NETIF_F_RXHASH |
11755 NETIF_F_RXCSUM |
11756 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011757
Jacob Kellerd36e41d2017-06-23 04:24:46 -040011758 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040011759 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
11760
11761 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011762
Preethi Banalabacd75c2017-03-27 14:43:18 -070011763 netdev->hw_enc_features |= hw_enc_features;
11764
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011765 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -070011766 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011767
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000011768 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070011769 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
11770
Preethi Banalabacd75c2017-03-27 14:43:18 -070011771 hw_features = hw_enc_features |
11772 NETIF_F_HW_VLAN_CTAG_TX |
11773 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +000011774
Preethi Banalabacd75c2017-03-27 14:43:18 -070011775 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011776
Preethi Banalabacd75c2017-03-27 14:43:18 -070011777 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -040011778 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011779
11780 if (vsi->type == I40E_VSI_MAIN) {
11781 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +000011782 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011783 /* The following steps are necessary for two reasons. First,
11784 * some older NVM configurations load a default MAC-VLAN
11785 * filter that will accept any tagged packet, and we want to
11786 * replace this with a normal filter. Additionally, it is
11787 * possible our MAC address was provided by the platform using
11788 * Open Firmware or similar.
11789 *
11790 * Thus, we need to remove the default filter and install one
11791 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -080011792 */
11793 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070011794 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080011795 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070011796 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011797 } else {
Jacob Keller8c9eb352017-07-12 05:46:12 -040011798 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
11799 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
11800 * the end, which is 4 bytes long, so force truncation of the
11801 * original name by IFNAMSIZ - 4
11802 */
11803 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
11804 IFNAMSIZ - 4,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011805 pf->vsi[pf->lan_vsi]->netdev->name);
11806 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -040011807
Jacob Keller278e7d02016-10-05 09:30:37 -070011808 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080011809 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -070011810 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011811 }
Kiran Patil21659032015-09-30 14:09:03 -040011812
Jacob Keller435c0842016-11-08 13:05:10 -080011813 /* Add the broadcast filter so that we initially will receive
11814 * broadcast packets. Note that when a new VLAN is first added the
11815 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
11816 * specific filters as part of transitioning into "vlan" operation.
11817 * When more VLANs are added, the driver will copy each existing MAC
11818 * filter and add it for the new VLAN.
11819 *
11820 * Broadcast filters are handled specially by
11821 * i40e_sync_filters_subtask, as the driver must to set the broadcast
11822 * promiscuous bit instead of adding this directly as a MAC/VLAN
11823 * filter. The subtask will update the correct broadcast promiscuous
11824 * bits as VLANs become active or inactive.
11825 */
11826 eth_broadcast_addr(broadcast);
11827 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -080011828 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -080011829 spin_unlock_bh(&vsi->mac_filter_hash_lock);
11830
Greg Rose9a173902014-05-22 06:32:02 +000011831 ether_addr_copy(netdev->dev_addr, mac_addr);
11832 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -070011833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011834 netdev->priv_flags |= IFF_UNICAST_FLT;
11835 netdev->priv_flags |= IFF_SUPP_NOFCS;
11836 /* Setup netdev TC information */
11837 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
11838
11839 netdev->netdev_ops = &i40e_netdev_ops;
11840 netdev->watchdog_timeo = 5 * HZ;
11841 i40e_set_ethtool_ops(netdev);
11842
Jarod Wilson91c527a2016-10-17 15:54:05 -040011843 /* MTU range: 68 - 9706 */
11844 netdev->min_mtu = ETH_MIN_MTU;
Mitch Williams1e3a5fd2017-06-23 04:24:43 -040011845 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
Jarod Wilson91c527a2016-10-17 15:54:05 -040011846
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011847 return 0;
11848}
11849
11850/**
11851 * i40e_vsi_delete - Delete a VSI from the switch
11852 * @vsi: the VSI being removed
11853 *
11854 * Returns 0 on success, negative value on failure
11855 **/
11856static void i40e_vsi_delete(struct i40e_vsi *vsi)
11857{
11858 /* remove default VSI is not allowed */
11859 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
11860 return;
11861
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011862 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011863}
11864
11865/**
Neerav Parikh51616012015-02-06 08:52:14 +000011866 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
11867 * @vsi: the VSI being queried
11868 *
11869 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
11870 **/
11871int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
11872{
11873 struct i40e_veb *veb;
11874 struct i40e_pf *pf = vsi->back;
11875
11876 /* Uplink is not a bridge so default to VEB */
11877 if (vsi->veb_idx == I40E_NO_VEB)
11878 return 1;
11879
11880 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040011881 if (!veb) {
11882 dev_info(&pf->pdev->dev,
11883 "There is no veb associated with the bridge\n");
11884 return -ENOENT;
11885 }
Neerav Parikh51616012015-02-06 08:52:14 +000011886
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -040011887 /* Uplink is a bridge in VEPA mode */
11888 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
11889 return 0;
11890 } else {
11891 /* Uplink is a bridge in VEB mode */
11892 return 1;
11893 }
11894
11895 /* VEPA is now default bridge, so return 0 */
11896 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +000011897}
11898
11899/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011900 * i40e_add_vsi - Add a VSI to the switch
11901 * @vsi: the VSI being configured
11902 *
11903 * This initializes a VSI context depending on the VSI type to be added and
11904 * passes it down to the add_vsi aq command.
11905 **/
11906static int i40e_add_vsi(struct i40e_vsi *vsi)
11907{
11908 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011909 struct i40e_pf *pf = vsi->back;
11910 struct i40e_hw *hw = &pf->hw;
11911 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -070011912 struct i40e_mac_filter *f;
11913 struct hlist_node *h;
11914 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -040011915
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011916 u8 enabled_tc = 0x1; /* TC0 enabled */
11917 int f_count = 0;
11918
11919 memset(&ctxt, 0, sizeof(ctxt));
11920 switch (vsi->type) {
11921 case I40E_VSI_MAIN:
11922 /* The PF's main VSI is already setup as part of the
11923 * device initialization, so we'll not bother with
11924 * the add_vsi call, but we will retrieve the current
11925 * VSI context.
11926 */
11927 ctxt.seid = pf->main_vsi_seid;
11928 ctxt.pf_num = pf->hw.pf_id;
11929 ctxt.vf_num = 0;
11930 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
11931 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
11932 if (ret) {
11933 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011934 "couldn't get PF vsi config, err %s aq_err %s\n",
11935 i40e_stat_str(&pf->hw, ret),
11936 i40e_aq_str(&pf->hw,
11937 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011938 return -ENOENT;
11939 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070011940 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011941 vsi->info.valid_sections = 0;
11942
11943 vsi->seid = ctxt.seid;
11944 vsi->id = ctxt.vsi_number;
11945
11946 enabled_tc = i40e_pf_get_tc_map(pf);
11947
Mitch Williams64615b52017-08-29 05:32:30 -040011948 /* Source pruning is enabled by default, so the flag is
11949 * negative logic - if it's set, we need to fiddle with
11950 * the VSI to disable source pruning.
11951 */
11952 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
11953 memset(&ctxt, 0, sizeof(ctxt));
11954 ctxt.seid = pf->main_vsi_seid;
11955 ctxt.pf_num = pf->hw.pf_id;
11956 ctxt.vf_num = 0;
11957 ctxt.info.valid_sections |=
11958 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
11959 ctxt.info.switch_id =
11960 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
11961 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11962 if (ret) {
11963 dev_info(&pf->pdev->dev,
11964 "update vsi failed, err %s aq_err %s\n",
11965 i40e_stat_str(&pf->hw, ret),
11966 i40e_aq_str(&pf->hw,
11967 pf->hw.aq.asq_last_status));
11968 ret = -ENOENT;
11969 goto err;
11970 }
11971 }
11972
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011973 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000011974 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
11975 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011976 memset(&ctxt, 0, sizeof(ctxt));
11977 ctxt.seid = pf->main_vsi_seid;
11978 ctxt.pf_num = pf->hw.pf_id;
11979 ctxt.vf_num = 0;
11980 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
11981 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
11982 if (ret) {
11983 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011984 "update vsi failed, err %s aq_err %s\n",
11985 i40e_stat_str(&pf->hw, ret),
11986 i40e_aq_str(&pf->hw,
11987 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011988 ret = -ENOENT;
11989 goto err;
11990 }
11991 /* update the local VSI info queue map */
11992 i40e_vsi_update_queue_map(vsi, &ctxt);
11993 vsi->info.valid_sections = 0;
11994 } else {
11995 /* Default/Main VSI is only enabled for TC0
11996 * reconfigure it to enable all TCs that are
11997 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +000011998 * For MFP case the iSCSI PF would use this
11999 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012000 */
12001 ret = i40e_vsi_config_tc(vsi, enabled_tc);
12002 if (ret) {
Carolyn Wyborny19279232017-07-14 09:10:07 -040012003 /* Single TC condition is not fatal,
12004 * message and continue
12005 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012006 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012007 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
12008 enabled_tc,
12009 i40e_stat_str(&pf->hw, ret),
12010 i40e_aq_str(&pf->hw,
12011 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012012 }
12013 }
12014 break;
12015
12016 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012017 ctxt.pf_num = hw->pf_id;
12018 ctxt.vf_num = 0;
12019 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000012020 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012021 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070012022 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
12023 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +000012024 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070012025 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +000012026 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -070012027 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +000012028 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012029 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012030 break;
12031
12032 case I40E_VSI_VMDQ2:
12033 ctxt.pf_num = hw->pf_id;
12034 ctxt.vf_num = 0;
12035 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000012036 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012037 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
12038
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012039 /* This VSI is connected to VEB so the switch_id
12040 * should be set to zero by default.
12041 */
Neerav Parikh51616012015-02-06 08:52:14 +000012042 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12043 ctxt.info.valid_sections |=
12044 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12045 ctxt.info.switch_id =
12046 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12047 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012048
12049 /* Setup the VSI tx/rx queue map for TC0 only for now */
12050 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12051 break;
12052
12053 case I40E_VSI_SRIOV:
12054 ctxt.pf_num = hw->pf_id;
12055 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
12056 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +000012057 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012058 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
12059
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012060 /* This VSI is connected to VEB so the switch_id
12061 * should be set to zero by default.
12062 */
Neerav Parikh51616012015-02-06 08:52:14 +000012063 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
12064 ctxt.info.valid_sections |=
12065 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
12066 ctxt.info.switch_id =
12067 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
12068 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012069
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012070 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
12071 ctxt.info.valid_sections |=
12072 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
12073 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -070012074 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
12075 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012076 }
12077
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012078 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
12079 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +000012080 if (pf->vf[vsi->vf_id].spoofchk) {
12081 ctxt.info.valid_sections |=
12082 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
12083 ctxt.info.sec_flags |=
12084 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
12085 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
12086 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012087 /* Setup the VSI tx/rx queue map for TC0 only for now */
12088 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
12089 break;
12090
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060012091 case I40E_VSI_IWARP:
12092 /* send down message to iWARP */
12093 break;
12094
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012095 default:
12096 return -ENODEV;
12097 }
12098
12099 if (vsi->type != I40E_VSI_MAIN) {
12100 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
12101 if (ret) {
12102 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012103 "add vsi failed, err %s aq_err %s\n",
12104 i40e_stat_str(&pf->hw, ret),
12105 i40e_aq_str(&pf->hw,
12106 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012107 ret = -ENOENT;
12108 goto err;
12109 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -070012110 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012111 vsi->info.valid_sections = 0;
12112 vsi->seid = ctxt.seid;
12113 vsi->id = ctxt.vsi_number;
12114 }
12115
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070012116 vsi->active_filters = 0;
Jacob Keller0da36b92017-04-19 09:25:55 -040012117 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -070012118 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012119 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -070012120 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -070012121 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012122 f_count++;
12123 }
Jacob Keller278e7d02016-10-05 09:30:37 -070012124 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040012125
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012126 if (f_count) {
12127 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
12128 pf->flags |= I40E_FLAG_FILTER_SYNC;
12129 }
12130
12131 /* Update VSI BW information */
12132 ret = i40e_vsi_get_bw_info(vsi);
12133 if (ret) {
12134 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012135 "couldn't get vsi bw info, err %s aq_err %s\n",
12136 i40e_stat_str(&pf->hw, ret),
12137 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012138 /* VSI is already added so not tearing that up */
12139 ret = 0;
12140 }
12141
12142err:
12143 return ret;
12144}
12145
12146/**
12147 * i40e_vsi_release - Delete a VSI and free its resources
12148 * @vsi: the VSI being removed
12149 *
12150 * Returns 0 on success or < 0 on error
12151 **/
12152int i40e_vsi_release(struct i40e_vsi *vsi)
12153{
Jacob Keller278e7d02016-10-05 09:30:37 -070012154 struct i40e_mac_filter *f;
12155 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012156 struct i40e_veb *veb = NULL;
12157 struct i40e_pf *pf;
12158 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -070012159 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012160
12161 pf = vsi->back;
12162
12163 /* release of a VEB-owner or last VSI is not allowed */
12164 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
12165 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
12166 vsi->seid, vsi->uplink_seid);
12167 return -ENODEV;
12168 }
12169 if (vsi == pf->vsi[pf->lan_vsi] &&
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030012170 !test_bit(__I40E_DOWN, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012171 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
12172 return -ENODEV;
12173 }
12174
12175 uplink_seid = vsi->uplink_seid;
12176 if (vsi->type != I40E_VSI_SRIOV) {
12177 if (vsi->netdev_registered) {
12178 vsi->netdev_registered = false;
12179 if (vsi->netdev) {
12180 /* results in a call to i40e_close() */
12181 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012182 }
12183 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +000012184 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012185 }
12186 i40e_vsi_disable_irq(vsi);
12187 }
12188
Jacob Keller278e7d02016-10-05 09:30:37 -070012189 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -070012190
12191 /* clear the sync flag on all filters */
12192 if (vsi->netdev) {
12193 __dev_uc_unsync(vsi->netdev, NULL);
12194 __dev_mc_unsync(vsi->netdev, NULL);
12195 }
12196
12197 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -070012198 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -070012199 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -070012200
Jacob Keller278e7d02016-10-05 09:30:37 -070012201 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -040012202
Jesse Brandeburg17652c62015-11-05 17:01:02 -080012203 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012204
12205 i40e_vsi_delete(vsi);
12206 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +000012207 if (vsi->netdev) {
12208 free_netdev(vsi->netdev);
12209 vsi->netdev = NULL;
12210 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012211 i40e_vsi_clear_rings(vsi);
12212 i40e_vsi_clear(vsi);
12213
12214 /* If this was the last thing on the VEB, except for the
12215 * controlling VSI, remove the VEB, which puts the controlling
12216 * VSI onto the next level down in the switch.
12217 *
12218 * Well, okay, there's one more exception here: don't remove
12219 * the orphan VEBs yet. We'll wait for an explicit remove request
12220 * from up the network stack.
12221 */
Mitch Williams505682c2014-05-20 08:01:37 +000012222 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012223 if (pf->vsi[i] &&
12224 pf->vsi[i]->uplink_seid == uplink_seid &&
12225 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12226 n++; /* count the VSIs */
12227 }
12228 }
12229 for (i = 0; i < I40E_MAX_VEB; i++) {
12230 if (!pf->veb[i])
12231 continue;
12232 if (pf->veb[i]->uplink_seid == uplink_seid)
12233 n++; /* count the VEBs */
12234 if (pf->veb[i]->seid == uplink_seid)
12235 veb = pf->veb[i];
12236 }
12237 if (n == 0 && veb && veb->uplink_seid != 0)
12238 i40e_veb_release(veb);
12239
12240 return 0;
12241}
12242
12243/**
12244 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
12245 * @vsi: ptr to the VSI
12246 *
12247 * This should only be called after i40e_vsi_mem_alloc() which allocates the
12248 * corresponding SW VSI structure and initializes num_queue_pairs for the
12249 * newly allocated VSI.
12250 *
12251 * Returns 0 on success or negative on failure
12252 **/
12253static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
12254{
12255 int ret = -ENOENT;
12256 struct i40e_pf *pf = vsi->back;
12257
Alexander Duyck493fb302013-09-28 07:01:44 +000012258 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012259 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
12260 vsi->seid);
12261 return -EEXIST;
12262 }
12263
12264 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +000012265 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012266 vsi->seid, vsi->base_vector);
12267 return -EEXIST;
12268 }
12269
Greg Rose90e04072014-03-06 08:59:57 +000012270 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012271 if (ret) {
12272 dev_info(&pf->pdev->dev,
12273 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
12274 vsi->num_q_vectors, vsi->seid, ret);
12275 vsi->num_q_vectors = 0;
12276 goto vector_setup_out;
12277 }
12278
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -040012279 /* In Legacy mode, we do not have to get any other vector since we
12280 * piggyback on the misc/ICR0 for queue interrupts.
12281 */
12282 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
12283 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +000012284 if (vsi->num_q_vectors)
12285 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
12286 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012287 if (vsi->base_vector < 0) {
12288 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +000012289 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
12290 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012291 i40e_vsi_free_q_vectors(vsi);
12292 ret = -ENOENT;
12293 goto vector_setup_out;
12294 }
12295
12296vector_setup_out:
12297 return ret;
12298}
12299
12300/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012301 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
12302 * @vsi: pointer to the vsi.
12303 *
12304 * This re-allocates a vsi's queue resources.
12305 *
12306 * Returns pointer to the successfully allocated and configured VSI sw struct
12307 * on success, otherwise returns NULL on failure.
12308 **/
12309static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
12310{
Björn Töpel74608d12017-05-24 07:55:35 +020012311 u16 alloc_queue_pairs;
John Underwoodf5340392016-02-18 09:19:24 -080012312 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012313 u8 enabled_tc;
12314 int ret;
12315
John Underwoodf5340392016-02-18 09:19:24 -080012316 if (!vsi)
12317 return NULL;
12318
12319 pf = vsi->back;
12320
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012321 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
12322 i40e_vsi_clear_rings(vsi);
12323
12324 i40e_vsi_free_arrays(vsi, false);
12325 i40e_set_num_rings_in_vsi(vsi);
12326 ret = i40e_vsi_alloc_arrays(vsi, false);
12327 if (ret)
12328 goto err_vsi;
12329
Björn Töpel74608d12017-05-24 07:55:35 +020012330 alloc_queue_pairs = vsi->alloc_queue_pairs *
12331 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12332
12333 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012334 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000012335 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012336 "failed to get tracking for %d queues for VSI %d err %d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020012337 alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012338 goto err_vsi;
12339 }
12340 vsi->base_queue = ret;
12341
12342 /* Update the FW view of the VSI. Force a reset of TC and queue
12343 * layout configurations.
12344 */
12345 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
12346 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
12347 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
12348 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080012349 if (vsi->type == I40E_VSI_MAIN)
12350 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000012351
12352 /* assign it some queues */
12353 ret = i40e_alloc_rings(vsi);
12354 if (ret)
12355 goto err_rings;
12356
12357 /* map all of the rings to the q_vectors */
12358 i40e_vsi_map_rings_to_vectors(vsi);
12359 return vsi;
12360
12361err_rings:
12362 i40e_vsi_free_q_vectors(vsi);
12363 if (vsi->netdev_registered) {
12364 vsi->netdev_registered = false;
12365 unregister_netdev(vsi->netdev);
12366 free_netdev(vsi->netdev);
12367 vsi->netdev = NULL;
12368 }
12369 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12370err_vsi:
12371 i40e_vsi_clear(vsi);
12372 return NULL;
12373}
12374
12375/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012376 * i40e_vsi_setup - Set up a VSI by a given type
12377 * @pf: board private structure
12378 * @type: VSI type
12379 * @uplink_seid: the switch element to link to
12380 * @param1: usage depends upon VSI type. For VF types, indicates VF id
12381 *
12382 * This allocates the sw VSI structure and its queue resources, then add a VSI
12383 * to the identified VEB.
12384 *
12385 * Returns pointer to the successfully allocated and configure VSI sw struct on
12386 * success, otherwise returns NULL on failure.
12387 **/
12388struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
12389 u16 uplink_seid, u32 param1)
12390{
12391 struct i40e_vsi *vsi = NULL;
12392 struct i40e_veb *veb = NULL;
Björn Töpel74608d12017-05-24 07:55:35 +020012393 u16 alloc_queue_pairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012394 int ret, i;
12395 int v_idx;
12396
12397 /* The requested uplink_seid must be either
12398 * - the PF's port seid
12399 * no VEB is needed because this is the PF
12400 * or this is a Flow Director special case VSI
12401 * - seid of an existing VEB
12402 * - seid of a VSI that owns an existing VEB
12403 * - seid of a VSI that doesn't own a VEB
12404 * a new VEB is created and the VSI becomes the owner
12405 * - seid of the PF VSI, which is what creates the first VEB
12406 * this is a special case of the previous
12407 *
12408 * Find which uplink_seid we were given and create a new VEB if needed
12409 */
12410 for (i = 0; i < I40E_MAX_VEB; i++) {
12411 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
12412 veb = pf->veb[i];
12413 break;
12414 }
12415 }
12416
12417 if (!veb && uplink_seid != pf->mac_seid) {
12418
Mitch Williams505682c2014-05-20 08:01:37 +000012419 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012420 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
12421 vsi = pf->vsi[i];
12422 break;
12423 }
12424 }
12425 if (!vsi) {
12426 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
12427 uplink_seid);
12428 return NULL;
12429 }
12430
12431 if (vsi->uplink_seid == pf->mac_seid)
12432 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
12433 vsi->tc_config.enabled_tc);
12434 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
12435 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12436 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000012437 if (veb) {
12438 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
12439 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040012440 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000012441 return NULL;
12442 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040012443 /* We come up by default in VEPA mode if SRIOV is not
12444 * already enabled, in which case we can't force VEPA
12445 * mode.
12446 */
12447 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
12448 veb->bridge_mode = BRIDGE_MODE_VEPA;
12449 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12450 }
Neerav Parikh51616012015-02-06 08:52:14 +000012451 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000012452 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012453 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12454 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12455 veb = pf->veb[i];
12456 }
12457 if (!veb) {
12458 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
12459 return NULL;
12460 }
12461
12462 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12463 uplink_seid = veb->seid;
12464 }
12465
12466 /* get vsi sw struct */
12467 v_idx = i40e_vsi_mem_alloc(pf, type);
12468 if (v_idx < 0)
12469 goto err_alloc;
12470 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080012471 if (!vsi)
12472 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012473 vsi->type = type;
12474 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
12475
12476 if (type == I40E_VSI_MAIN)
12477 pf->lan_vsi = v_idx;
12478 else if (type == I40E_VSI_SRIOV)
12479 vsi->vf_id = param1;
12480 /* assign it some queues */
Björn Töpel74608d12017-05-24 07:55:35 +020012481 alloc_queue_pairs = vsi->alloc_queue_pairs *
12482 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
12483
12484 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012485 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000012486 dev_info(&pf->pdev->dev,
12487 "failed to get tracking for %d queues for VSI %d err=%d\n",
Björn Töpel74608d12017-05-24 07:55:35 +020012488 alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012489 goto err_vsi;
12490 }
12491 vsi->base_queue = ret;
12492
12493 /* get a VSI from the hardware */
12494 vsi->uplink_seid = uplink_seid;
12495 ret = i40e_add_vsi(vsi);
12496 if (ret)
12497 goto err_vsi;
12498
12499 switch (vsi->type) {
12500 /* setup the netdev if needed */
12501 case I40E_VSI_MAIN:
12502 case I40E_VSI_VMDQ2:
12503 ret = i40e_config_netdev(vsi);
12504 if (ret)
12505 goto err_netdev;
12506 ret = register_netdev(vsi->netdev);
12507 if (ret)
12508 goto err_netdev;
12509 vsi->netdev_registered = true;
12510 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080012511#ifdef CONFIG_I40E_DCB
12512 /* Setup DCB netlink interface */
12513 i40e_dcbnl_setup(vsi);
12514#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012515 /* fall through */
12516
12517 case I40E_VSI_FDIR:
12518 /* set up vectors and rings if needed */
12519 ret = i40e_vsi_setup_vectors(vsi);
12520 if (ret)
12521 goto err_msix;
12522
12523 ret = i40e_alloc_rings(vsi);
12524 if (ret)
12525 goto err_rings;
12526
12527 /* map all of the rings to the q_vectors */
12528 i40e_vsi_map_rings_to_vectors(vsi);
12529
12530 i40e_vsi_reset_stats(vsi);
12531 break;
12532
12533 default:
12534 /* no netdev or rings for the other VSI types */
12535 break;
12536 }
12537
Jacob Kellerd36e41d2017-06-23 04:24:46 -040012538 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040012539 (vsi->type == I40E_VSI_VMDQ2)) {
12540 ret = i40e_vsi_config_rss(vsi);
12541 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012542 return vsi;
12543
12544err_rings:
12545 i40e_vsi_free_q_vectors(vsi);
12546err_msix:
12547 if (vsi->netdev_registered) {
12548 vsi->netdev_registered = false;
12549 unregister_netdev(vsi->netdev);
12550 free_netdev(vsi->netdev);
12551 vsi->netdev = NULL;
12552 }
12553err_netdev:
12554 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
12555err_vsi:
12556 i40e_vsi_clear(vsi);
12557err_alloc:
12558 return NULL;
12559}
12560
12561/**
12562 * i40e_veb_get_bw_info - Query VEB BW information
12563 * @veb: the veb to query
12564 *
12565 * Query the Tx scheduler BW configuration data for given VEB
12566 **/
12567static int i40e_veb_get_bw_info(struct i40e_veb *veb)
12568{
12569 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
12570 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
12571 struct i40e_pf *pf = veb->pf;
12572 struct i40e_hw *hw = &pf->hw;
12573 u32 tc_bw_max;
12574 int ret = 0;
12575 int i;
12576
12577 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
12578 &bw_data, NULL);
12579 if (ret) {
12580 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012581 "query veb bw config failed, err %s aq_err %s\n",
12582 i40e_stat_str(&pf->hw, ret),
12583 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012584 goto out;
12585 }
12586
12587 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
12588 &ets_data, NULL);
12589 if (ret) {
12590 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012591 "query veb bw ets config failed, err %s aq_err %s\n",
12592 i40e_stat_str(&pf->hw, ret),
12593 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012594 goto out;
12595 }
12596
12597 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
12598 veb->bw_max_quanta = ets_data.tc_bw_max;
12599 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000012600 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012601 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
12602 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
12603 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
12604 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
12605 veb->bw_tc_limit_credits[i] =
12606 le16_to_cpu(bw_data.tc_bw_limits[i]);
12607 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
12608 }
12609
12610out:
12611 return ret;
12612}
12613
12614/**
12615 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
12616 * @pf: board private structure
12617 *
12618 * On error: returns error code (negative)
12619 * On success: returns vsi index in PF (positive)
12620 **/
12621static int i40e_veb_mem_alloc(struct i40e_pf *pf)
12622{
12623 int ret = -ENOENT;
12624 struct i40e_veb *veb;
12625 int i;
12626
12627 /* Need to protect the allocation of switch elements at the PF level */
12628 mutex_lock(&pf->switch_mutex);
12629
12630 /* VEB list may be fragmented if VEB creation/destruction has
12631 * been happening. We can afford to do a quick scan to look
12632 * for any free slots in the list.
12633 *
12634 * find next empty veb slot, looping back around if necessary
12635 */
12636 i = 0;
12637 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
12638 i++;
12639 if (i >= I40E_MAX_VEB) {
12640 ret = -ENOMEM;
12641 goto err_alloc_veb; /* out of VEB slots! */
12642 }
12643
12644 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
12645 if (!veb) {
12646 ret = -ENOMEM;
12647 goto err_alloc_veb;
12648 }
12649 veb->pf = pf;
12650 veb->idx = i;
12651 veb->enabled_tc = 1;
12652
12653 pf->veb[i] = veb;
12654 ret = i;
12655err_alloc_veb:
12656 mutex_unlock(&pf->switch_mutex);
12657 return ret;
12658}
12659
12660/**
12661 * i40e_switch_branch_release - Delete a branch of the switch tree
12662 * @branch: where to start deleting
12663 *
12664 * This uses recursion to find the tips of the branch to be
12665 * removed, deleting until we get back to and can delete this VEB.
12666 **/
12667static void i40e_switch_branch_release(struct i40e_veb *branch)
12668{
12669 struct i40e_pf *pf = branch->pf;
12670 u16 branch_seid = branch->seid;
12671 u16 veb_idx = branch->idx;
12672 int i;
12673
12674 /* release any VEBs on this VEB - RECURSION */
12675 for (i = 0; i < I40E_MAX_VEB; i++) {
12676 if (!pf->veb[i])
12677 continue;
12678 if (pf->veb[i]->uplink_seid == branch->seid)
12679 i40e_switch_branch_release(pf->veb[i]);
12680 }
12681
12682 /* Release the VSIs on this VEB, but not the owner VSI.
12683 *
12684 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
12685 * the VEB itself, so don't use (*branch) after this loop.
12686 */
Mitch Williams505682c2014-05-20 08:01:37 +000012687 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012688 if (!pf->vsi[i])
12689 continue;
12690 if (pf->vsi[i]->uplink_seid == branch_seid &&
12691 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
12692 i40e_vsi_release(pf->vsi[i]);
12693 }
12694 }
12695
12696 /* There's one corner case where the VEB might not have been
12697 * removed, so double check it here and remove it if needed.
12698 * This case happens if the veb was created from the debugfs
12699 * commands and no VSIs were added to it.
12700 */
12701 if (pf->veb[veb_idx])
12702 i40e_veb_release(pf->veb[veb_idx]);
12703}
12704
12705/**
12706 * i40e_veb_clear - remove veb struct
12707 * @veb: the veb to remove
12708 **/
12709static void i40e_veb_clear(struct i40e_veb *veb)
12710{
12711 if (!veb)
12712 return;
12713
12714 if (veb->pf) {
12715 struct i40e_pf *pf = veb->pf;
12716
12717 mutex_lock(&pf->switch_mutex);
12718 if (pf->veb[veb->idx] == veb)
12719 pf->veb[veb->idx] = NULL;
12720 mutex_unlock(&pf->switch_mutex);
12721 }
12722
12723 kfree(veb);
12724}
12725
12726/**
12727 * i40e_veb_release - Delete a VEB and free its resources
12728 * @veb: the VEB being removed
12729 **/
12730void i40e_veb_release(struct i40e_veb *veb)
12731{
12732 struct i40e_vsi *vsi = NULL;
12733 struct i40e_pf *pf;
12734 int i, n = 0;
12735
12736 pf = veb->pf;
12737
12738 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000012739 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012740 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
12741 n++;
12742 vsi = pf->vsi[i];
12743 }
12744 }
12745 if (n != 1) {
12746 dev_info(&pf->pdev->dev,
12747 "can't remove VEB %d with %d VSIs left\n",
12748 veb->seid, n);
12749 return;
12750 }
12751
12752 /* move the remaining VSI to uplink veb */
12753 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
12754 if (veb->uplink_seid) {
12755 vsi->uplink_seid = veb->uplink_seid;
12756 if (veb->uplink_seid == pf->mac_seid)
12757 vsi->veb_idx = I40E_NO_VEB;
12758 else
12759 vsi->veb_idx = veb->veb_idx;
12760 } else {
12761 /* floating VEB */
12762 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
12763 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
12764 }
12765
12766 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
12767 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012768}
12769
12770/**
12771 * i40e_add_veb - create the VEB in the switch
12772 * @veb: the VEB to be instantiated
12773 * @vsi: the controlling VSI
12774 **/
12775static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
12776{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012777 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080012778 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012779 int ret;
12780
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012781 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070012782 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080012783 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070012784
12785 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012786 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012787 dev_info(&pf->pdev->dev,
12788 "couldn't add VEB, err %s aq_err %s\n",
12789 i40e_stat_str(&pf->hw, ret),
12790 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012791 return -EPERM;
12792 }
12793
12794 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012795 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012796 &veb->stats_idx, NULL, NULL, NULL);
12797 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012798 dev_info(&pf->pdev->dev,
12799 "couldn't get VEB statistics idx, err %s aq_err %s\n",
12800 i40e_stat_str(&pf->hw, ret),
12801 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012802 return -EPERM;
12803 }
12804 ret = i40e_veb_get_bw_info(veb);
12805 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040012806 dev_info(&pf->pdev->dev,
12807 "couldn't get VEB bw info, err %s aq_err %s\n",
12808 i40e_stat_str(&pf->hw, ret),
12809 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
12810 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012811 return -ENOENT;
12812 }
12813
12814 vsi->uplink_seid = veb->seid;
12815 vsi->veb_idx = veb->idx;
12816 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
12817
12818 return 0;
12819}
12820
12821/**
12822 * i40e_veb_setup - Set up a VEB
12823 * @pf: board private structure
12824 * @flags: VEB setup flags
12825 * @uplink_seid: the switch element to link to
12826 * @vsi_seid: the initial VSI seid
12827 * @enabled_tc: Enabled TC bit-map
12828 *
12829 * This allocates the sw VEB structure and links it into the switch
12830 * It is possible and legal for this to be a duplicate of an already
12831 * existing VEB. It is also possible for both uplink and vsi seids
12832 * to be zero, in order to create a floating VEB.
12833 *
12834 * Returns pointer to the successfully allocated VEB sw struct on
12835 * success, otherwise returns NULL on failure.
12836 **/
12837struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
12838 u16 uplink_seid, u16 vsi_seid,
12839 u8 enabled_tc)
12840{
12841 struct i40e_veb *veb, *uplink_veb = NULL;
12842 int vsi_idx, veb_idx;
12843 int ret;
12844
12845 /* if one seid is 0, the other must be 0 to create a floating relay */
12846 if ((uplink_seid == 0 || vsi_seid == 0) &&
12847 (uplink_seid + vsi_seid != 0)) {
12848 dev_info(&pf->pdev->dev,
12849 "one, not both seid's are 0: uplink=%d vsi=%d\n",
12850 uplink_seid, vsi_seid);
12851 return NULL;
12852 }
12853
12854 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000012855 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012856 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
12857 break;
Mitch Williams505682c2014-05-20 08:01:37 +000012858 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012859 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
12860 vsi_seid);
12861 return NULL;
12862 }
12863
12864 if (uplink_seid && uplink_seid != pf->mac_seid) {
12865 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
12866 if (pf->veb[veb_idx] &&
12867 pf->veb[veb_idx]->seid == uplink_seid) {
12868 uplink_veb = pf->veb[veb_idx];
12869 break;
12870 }
12871 }
12872 if (!uplink_veb) {
12873 dev_info(&pf->pdev->dev,
12874 "uplink seid %d not found\n", uplink_seid);
12875 return NULL;
12876 }
12877 }
12878
12879 /* get veb sw struct */
12880 veb_idx = i40e_veb_mem_alloc(pf);
12881 if (veb_idx < 0)
12882 goto err_alloc;
12883 veb = pf->veb[veb_idx];
12884 veb->flags = flags;
12885 veb->uplink_seid = uplink_seid;
12886 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
12887 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
12888
12889 /* create the VEB in the switch */
12890 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
12891 if (ret)
12892 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000012893 if (vsi_idx == pf->lan_vsi)
12894 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012895
12896 return veb;
12897
12898err_veb:
12899 i40e_veb_clear(veb);
12900err_alloc:
12901 return NULL;
12902}
12903
12904/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000012905 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012906 * @pf: board private structure
12907 * @ele: element we are building info from
12908 * @num_reported: total number of elements
12909 * @printconfig: should we print the contents
12910 *
12911 * helper function to assist in extracting a few useful SEID values.
12912 **/
12913static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
12914 struct i40e_aqc_switch_config_element_resp *ele,
12915 u16 num_reported, bool printconfig)
12916{
12917 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
12918 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
12919 u8 element_type = ele->element_type;
12920 u16 seid = le16_to_cpu(ele->seid);
12921
12922 if (printconfig)
12923 dev_info(&pf->pdev->dev,
12924 "type=%d seid=%d uplink=%d downlink=%d\n",
12925 element_type, seid, uplink_seid, downlink_seid);
12926
12927 switch (element_type) {
12928 case I40E_SWITCH_ELEMENT_TYPE_MAC:
12929 pf->mac_seid = seid;
12930 break;
12931 case I40E_SWITCH_ELEMENT_TYPE_VEB:
12932 /* Main VEB? */
12933 if (uplink_seid != pf->mac_seid)
12934 break;
12935 if (pf->lan_veb == I40E_NO_VEB) {
12936 int v;
12937
12938 /* find existing or else empty VEB */
12939 for (v = 0; v < I40E_MAX_VEB; v++) {
12940 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
12941 pf->lan_veb = v;
12942 break;
12943 }
12944 }
12945 if (pf->lan_veb == I40E_NO_VEB) {
12946 v = i40e_veb_mem_alloc(pf);
12947 if (v < 0)
12948 break;
12949 pf->lan_veb = v;
12950 }
12951 }
12952
12953 pf->veb[pf->lan_veb]->seid = seid;
12954 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
12955 pf->veb[pf->lan_veb]->pf = pf;
12956 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
12957 break;
12958 case I40E_SWITCH_ELEMENT_TYPE_VSI:
12959 if (num_reported != 1)
12960 break;
12961 /* This is immediately after a reset so we can assume this is
12962 * the PF's VSI
12963 */
12964 pf->mac_seid = uplink_seid;
12965 pf->pf_seid = downlink_seid;
12966 pf->main_vsi_seid = seid;
12967 if (printconfig)
12968 dev_info(&pf->pdev->dev,
12969 "pf_seid=%d main_vsi_seid=%d\n",
12970 pf->pf_seid, pf->main_vsi_seid);
12971 break;
12972 case I40E_SWITCH_ELEMENT_TYPE_PF:
12973 case I40E_SWITCH_ELEMENT_TYPE_VF:
12974 case I40E_SWITCH_ELEMENT_TYPE_EMP:
12975 case I40E_SWITCH_ELEMENT_TYPE_BMC:
12976 case I40E_SWITCH_ELEMENT_TYPE_PE:
12977 case I40E_SWITCH_ELEMENT_TYPE_PA:
12978 /* ignore these for now */
12979 break;
12980 default:
12981 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
12982 element_type, seid);
12983 break;
12984 }
12985}
12986
12987/**
12988 * i40e_fetch_switch_configuration - Get switch config from firmware
12989 * @pf: board private structure
12990 * @printconfig: should we print the contents
12991 *
12992 * Get the current switch configuration from the device and
12993 * extract a few useful SEID values.
12994 **/
12995int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
12996{
12997 struct i40e_aqc_get_switch_config_resp *sw_config;
12998 u16 next_seid = 0;
12999 int ret = 0;
13000 u8 *aq_buf;
13001 int i;
13002
13003 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
13004 if (!aq_buf)
13005 return -ENOMEM;
13006
13007 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
13008 do {
13009 u16 num_reported, num_total;
13010
13011 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
13012 I40E_AQ_LARGE_BUF,
13013 &next_seid, NULL);
13014 if (ret) {
13015 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040013016 "get switch config failed err %s aq_err %s\n",
13017 i40e_stat_str(&pf->hw, ret),
13018 i40e_aq_str(&pf->hw,
13019 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013020 kfree(aq_buf);
13021 return -ENOENT;
13022 }
13023
13024 num_reported = le16_to_cpu(sw_config->header.num_reported);
13025 num_total = le16_to_cpu(sw_config->header.num_total);
13026
13027 if (printconfig)
13028 dev_info(&pf->pdev->dev,
13029 "header: %d reported %d total\n",
13030 num_reported, num_total);
13031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013032 for (i = 0; i < num_reported; i++) {
13033 struct i40e_aqc_switch_config_element_resp *ele =
13034 &sw_config->element[i];
13035
13036 i40e_setup_pf_switch_element(pf, ele, num_reported,
13037 printconfig);
13038 }
13039 } while (next_seid != 0);
13040
13041 kfree(aq_buf);
13042 return ret;
13043}
13044
13045/**
13046 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
13047 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013048 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013049 *
13050 * Returns 0 on success, negative value on failure
13051 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013052static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013053{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013054 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013055 int ret;
13056
13057 /* find out what's out there already */
13058 ret = i40e_fetch_switch_configuration(pf, false);
13059 if (ret) {
13060 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040013061 "couldn't fetch switch config, err %s aq_err %s\n",
13062 i40e_stat_str(&pf->hw, ret),
13063 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013064 return ret;
13065 }
13066 i40e_pf_reset_stats(pf);
13067
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013068 /* set the switch config bit for the whole device to
13069 * support limited promisc or true promisc
13070 * when user requests promisc. The default is limited
13071 * promisc.
13072 */
13073
13074 if ((pf->hw.pf_id == 0) &&
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013075 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013076 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013077 pf->last_sw_conf_flags = flags;
13078 }
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013079
13080 if (pf->hw.pf_id == 0) {
13081 u16 valid_flags;
13082
13083 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
Amritha Nambiar5efe0c62017-10-27 02:35:45 -070013084 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013085 NULL);
13086 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
13087 dev_info(&pf->pdev->dev,
13088 "couldn't set switch config bits, err %s aq_err %s\n",
13089 i40e_stat_str(&pf->hw, ret),
13090 i40e_aq_str(&pf->hw,
13091 pf->hw.aq.asq_last_status));
13092 /* not a fatal problem, just keep going */
13093 }
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013094 pf->last_sw_conf_valid_flags = valid_flags;
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070013095 }
13096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013097 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013098 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013099 struct i40e_vsi *vsi = NULL;
13100 u16 uplink_seid;
13101
13102 /* Set up the PF VSI associated with the PF's main VSI
13103 * that is already in the HW switch
13104 */
13105 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
13106 uplink_seid = pf->veb[pf->lan_veb]->seid;
13107 else
13108 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013109 if (pf->lan_vsi == I40E_NO_VSI)
13110 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
13111 else if (reinit)
13112 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013113 if (!vsi) {
13114 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
Amritha Nambiaraaf66502017-10-27 02:35:56 -070013115 i40e_cloud_filter_exit(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013116 i40e_fdir_teardown(pf);
13117 return -EAGAIN;
13118 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013119 } else {
13120 /* force a reset of TC and queue layout configurations */
13121 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040013122
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013123 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13124 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13125 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13126 }
13127 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
13128
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013129 i40e_fdir_sb_setup(pf);
13130
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013131 /* Setup static PF queue filter control settings */
13132 ret = i40e_setup_pf_filter_control(pf);
13133 if (ret) {
13134 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
13135 ret);
13136 /* Failure here should not stop continuing other steps */
13137 }
13138
13139 /* enable RSS in the HW, even for only one queue, as the stack can use
13140 * the hash
13141 */
13142 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040013143 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013144
13145 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013146 i40e_link_event(pf);
13147
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000013148 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013149 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
13150 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000013151
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000013152 i40e_ptp_init(pf);
13153
Alexander Duyck1f190d92017-04-19 09:25:51 -040013154 /* repopulate tunnel port filters */
13155 i40e_sync_udp_filters(pf);
13156
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013157 return ret;
13158}
13159
13160/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013161 * i40e_determine_queue_usage - Work out queue distribution
13162 * @pf: board private structure
13163 **/
13164static void i40e_determine_queue_usage(struct i40e_pf *pf)
13165{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013166 int queues_left;
Shannon Nelsone50d5752017-07-24 18:17:42 -070013167 int q_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013168
13169 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013170
13171 /* Find the max queues to be put into basic use. We'll always be
13172 * using TC0, whether or not DCB is running, and TC0 will get the
13173 * big RSS set.
13174 */
13175 queues_left = pf->hw.func_caps.num_tx_qp;
13176
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013177 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000013178 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013179 /* one qp for PF, no queues for anything else */
13180 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040013181 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013182
13183 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013184 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013185 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013186 I40E_FLAG_FD_SB_ENABLED |
13187 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013188 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070013189 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013190 I40E_FLAG_SRIOV_ENABLED |
13191 I40E_FLAG_VMDQ_ENABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013192 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Frank Zhang9aa7e932014-05-20 08:01:42 +000013193 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
13194 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000013195 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013196 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000013197 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040013198 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000013199 queues_left -= pf->num_lan_qps;
13200
13201 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013202 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000013203 I40E_FLAG_FD_SB_ENABLED |
13204 I40E_FLAG_FD_ATR_ENABLED |
13205 I40E_FLAG_DCB_ENABLED |
13206 I40E_FLAG_VMDQ_ENABLED);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013207 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013208 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013209 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013210 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013211 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070013212 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
13213 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013214 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
13215 }
Shannon Nelsone50d5752017-07-24 18:17:42 -070013216
13217 /* limit lan qps to the smaller of qps, cpus or msix */
13218 q_max = max_t(int, pf->rss_size_max, num_online_cpus());
13219 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
13220 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
13221 pf->num_lan_qps = q_max;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000013222
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013223 queues_left -= pf->num_lan_qps;
13224 }
13225
13226 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
13227 if (queues_left > 1) {
13228 queues_left -= 1; /* save 1 queue for FD */
13229 } else {
13230 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013231 pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013232 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
13233 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013234 }
13235
13236 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13237 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080013238 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
13239 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013240 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
13241 }
13242
13243 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
13244 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
13245 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
13246 (queues_left / pf->num_vmdq_qps));
13247 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
13248 }
13249
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000013250 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040013251 dev_dbg(&pf->pdev->dev,
13252 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
13253 pf->hw.func_caps.num_tx_qp,
13254 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040013255 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
13256 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
13257 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013258}
13259
13260/**
13261 * i40e_setup_pf_filter_control - Setup PF static filter control
13262 * @pf: PF to be setup
13263 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000013264 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013265 * settings. If PE/FCoE are enabled then it will also set the per PF
13266 * based filter sizes required for them. It also enables Flow director,
13267 * ethertype and macvlan type filter settings for the pf.
13268 *
13269 * Returns 0 on success, negative on failure
13270 **/
13271static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
13272{
13273 struct i40e_filter_control_settings *settings = &pf->filter_settings;
13274
13275 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
13276
13277 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080013278 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013279 settings->enable_fdir = true;
13280
13281 /* Ethtype and MACVLAN filters enabled for PF */
13282 settings->enable_ethtype = true;
13283 settings->enable_macvlan = true;
13284
13285 if (i40e_set_filter_control(&pf->hw, settings))
13286 return -ENOENT;
13287
13288 return 0;
13289}
13290
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013291#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040013292#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013293static void i40e_print_features(struct i40e_pf *pf)
13294{
13295 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080013296 char *buf;
13297 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013298
Joe Perches3b195842015-12-03 04:20:57 -080013299 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
13300 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013301 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013302
Joe Perches3b195842015-12-03 04:20:57 -080013303 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013304#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080013305 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013306#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070013307 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040013308 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070013309 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013310 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080013311 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013312 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080013313 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000013314 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080013315 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
13316 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000013317 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000013318 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080013319 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080013320 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080013321 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013322 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080013323 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040013324 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080013325 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040013326 else
Joe Perches3b195842015-12-03 04:20:57 -080013327 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013328
Joe Perches3b195842015-12-03 04:20:57 -080013329 dev_info(&pf->pdev->dev, "%s\n", buf);
13330 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040013331 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013332}
13333
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013334/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013335 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013336 * @pdev: PCI device information struct
13337 * @pf: board private structure
13338 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040013339 * Look up the MAC address for the device. First we'll try
13340 * eth_platform_get_mac_address, which will check Open Firmware, or arch
13341 * specific fallback. Otherwise, we'll default to the stored value in
13342 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013343 **/
13344static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
13345{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040013346 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
13347 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013348}
13349
13350/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013351 * i40e_probe - Device initialization routine
13352 * @pdev: PCI device information struct
13353 * @ent: entry in i40e_pci_tbl
13354 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000013355 * i40e_probe initializes a PF identified by a pci_dev structure.
13356 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013357 * and a hardware reset occur.
13358 *
13359 * Returns 0 on success, negative on failure
13360 **/
13361static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13362{
Catherine Sullivane8278452015-02-06 08:52:08 +000013363 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013364 struct i40e_pf *pf;
13365 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000013366 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040013367 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013368 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060013369 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040013370 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000013371 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040013372 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013373
13374 err = pci_enable_device_mem(pdev);
13375 if (err)
13376 return err;
13377
13378 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000013379 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000013380 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000013381 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
13382 if (err) {
13383 dev_err(&pdev->dev,
13384 "DMA configuration failed: 0x%x\n", err);
13385 goto err_dma;
13386 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013387 }
13388
13389 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013390 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013391 if (err) {
13392 dev_info(&pdev->dev,
13393 "pci_request_selected_regions failed %d\n", err);
13394 goto err_pci_reg;
13395 }
13396
13397 pci_enable_pcie_error_reporting(pdev);
13398 pci_set_master(pdev);
13399
13400 /* Now that we have a PCI connection, we need to do the
13401 * low level device setup. This is primarily setting up
13402 * the Admin Queue structures and then querying for the
13403 * device's current profile information.
13404 */
13405 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
13406 if (!pf) {
13407 err = -ENOMEM;
13408 goto err_pf_alloc;
13409 }
13410 pf->next_vsi = 0;
13411 pf->pdev = pdev;
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013412 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013413
13414 hw = &pf->hw;
13415 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000013416
Shannon Nelson2ac8b672015-07-23 16:54:37 -040013417 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
13418 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000013419
Shannon Nelson2ac8b672015-07-23 16:54:37 -040013420 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013421 if (!hw->hw_addr) {
13422 err = -EIO;
13423 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
13424 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040013425 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013426 goto err_ioremap;
13427 }
13428 hw->vendor_id = pdev->vendor;
13429 hw->device_id = pdev->device;
13430 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
13431 hw->subsystem_vendor_id = pdev->subsystem_vendor;
13432 hw->subsystem_device_id = pdev->subsystem_device;
13433 hw->bus.device = PCI_SLOT(pdev->devfn);
13434 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080013435 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000013436 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013437
Scott Petersonab243ec2017-08-22 06:57:54 -040013438 /* Select something other than the 802.1ad ethertype for the
13439 * switch to use internally and drop on ingress.
13440 */
13441 hw->switch_tag = 0xffff;
13442 hw->first_tag = ETH_P_8021AD;
13443 hw->second_tag = ETH_P_8021Q;
13444
Jacob Keller0e588de2017-02-06 14:38:50 -080013445 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
13446 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
13447
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080013448 /* set up the locks for the AQ, do this only once in probe
13449 * and destroy them only once in remove
13450 */
13451 mutex_init(&hw->aq.asq_mutex);
13452 mutex_init(&hw->aq.arq_mutex);
13453
Alexander Duyck5d4ca232016-09-30 08:21:46 -040013454 pf->msg_enable = netif_msg_init(debug,
13455 NETIF_MSG_DRV |
13456 NETIF_MSG_PROBE |
13457 NETIF_MSG_LINK);
13458 if (debug < -1)
13459 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000013460
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000013461 /* do a special CORER for clearing PXE mode once at init */
13462 if (hw->revision_id == 0 &&
13463 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
13464 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
13465 i40e_flush(hw);
13466 msleep(200);
13467 pf->corer_count++;
13468
13469 i40e_clear_pxe_mode(hw);
13470 }
13471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013472 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000013473 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013474 err = i40e_pf_reset(hw);
13475 if (err) {
13476 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
13477 goto err_pf_reset;
13478 }
13479 pf->pfr_count++;
13480
13481 hw->aq.num_arq_entries = I40E_AQ_LEN;
13482 hw->aq.num_asq_entries = I40E_AQ_LEN;
13483 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13484 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
13485 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000013486
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000013487 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000013488 "%s-%s:misc",
13489 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013490
13491 err = i40e_init_shared_code(hw);
13492 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040013493 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
13494 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013495 goto err_pf_reset;
13496 }
13497
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000013498 /* set up a default setting for link flow control */
13499 pf->hw.fc.requested_mode = I40E_FC_NONE;
13500
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013501 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040013502 if (err) {
13503 if (err == I40E_ERR_FIRMWARE_API_VERSION)
13504 dev_info(&pdev->dev,
13505 "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");
13506 else
13507 dev_info(&pdev->dev,
13508 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
13509
13510 goto err_pf_reset;
13511 }
Filip Sadowski5bbb2e22017-06-07 05:43:09 -040013512 i40e_get_oem_version(hw);
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040013513
Shannon Nelson6dec1012015-09-28 14:12:30 -040013514 /* provide nvm, fw, api versions */
13515 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
13516 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
13517 hw->aq.api_maj_ver, hw->aq.api_min_ver,
13518 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040013519
Catherine Sullivan7aa67612014-07-09 07:46:17 +000013520 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
Mitch Williams22b965512017-07-14 09:27:09 -040013521 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
Shannon Nelson278b6f62014-06-04 01:41:03 +000013522 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000013523 "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 -040013524 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
Shannon Nelson278b6f62014-06-04 01:41:03 +000013525 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000013526 "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 +000013527
Shannon Nelson4eb3f762014-03-06 08:59:58 +000013528 i40e_verify_eeprom(pf);
13529
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000013530 /* Rev 0 hardware was never productized */
13531 if (hw->revision_id < 1)
13532 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");
13533
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000013534 i40e_clear_pxe_mode(hw);
Amritha Nambiar2f4b4112017-10-27 02:36:01 -070013535 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013536 if (err)
13537 goto err_adminq_setup;
13538
13539 err = i40e_sw_init(pf);
13540 if (err) {
13541 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
13542 goto err_sw_init;
13543 }
13544
13545 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080013546 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013547 if (err) {
13548 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
13549 goto err_init_lan_hmc;
13550 }
13551
13552 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
13553 if (err) {
13554 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
13555 err = -ENOENT;
13556 goto err_configure_lan_hmc;
13557 }
13558
Neerav Parikhb686ece2014-12-14 01:55:11 +000013559 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
13560 * Ignore error return codes because if it was already disabled via
13561 * hardware settings this will fail
13562 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013563 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000013564 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
13565 i40e_aq_stop_lldp(hw, true, NULL);
13566 }
13567
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050013568 /* allow a platform config to override the HW addr */
13569 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040013570
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000013571 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013572 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
13573 err = -EIO;
13574 goto err_mac_addr;
13575 }
13576 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000013577 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000013578 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
13579 if (is_valid_ether_addr(hw->mac.port_addr))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013580 pf->hw_features |= I40E_HW_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013581
13582 pci_set_drvdata(pdev, pf);
13583 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080013584#ifdef CONFIG_I40E_DCB
13585 err = i40e_init_pf_dcb(pf);
13586 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000013587 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070013588 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000013589 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080013590 }
13591#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013592
13593 /* set up periodic task facility */
Kees Cook26566ea2017-10-16 17:29:35 -070013594 timer_setup(&pf->service_timer, i40e_service_timer, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013595 pf->service_timer_period = HZ;
13596
13597 INIT_WORK(&pf->service_task, i40e_service_task);
Jacob Keller0da36b92017-04-19 09:25:55 -040013598 clear_bit(__I40E_SERVICE_SCHED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013599
Shannon Nelson1d5109d2015-08-26 15:14:08 -040013600 /* NVM bit on means WoL disabled for the port */
13601 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080013602 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040013603 pf->wol_en = false;
13604 else
13605 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000013606 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
13607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013608 /* set up the main switch operations */
13609 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040013610 err = i40e_init_interrupt_scheme(pf);
13611 if (err)
13612 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013613
Mitch Williams505682c2014-05-20 08:01:37 +000013614 /* The number of VSIs reported by the FW is the minimum guaranteed
13615 * to us; HW supports far more and we share the remaining pool with
13616 * the other PFs. We allocate space for more than the guarantee with
13617 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013618 */
Mitch Williams505682c2014-05-20 08:01:37 +000013619 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
13620 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
13621 else
13622 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
13623
13624 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080013625 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
13626 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000013627 if (!pf->vsi) {
13628 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013629 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000013630 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013631
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040013632#ifdef CONFIG_PCI_IOV
13633 /* prep for VF support */
13634 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
13635 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040013636 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040013637 if (pci_num_vf(pdev))
13638 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13639 }
13640#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000013641 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013642 if (err) {
13643 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
13644 goto err_vsis;
13645 }
Amritha Nambiar8f88b302017-09-07 04:00:17 -070013646 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
Helin Zhang58fc3262015-10-01 14:37:38 -040013647
13648 /* Make sure flow control is set according to current settings */
13649 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
13650 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
13651 dev_dbg(&pf->pdev->dev,
13652 "Set fc with err %s aq_err %s on get_phy_cap\n",
13653 i40e_stat_str(hw, err),
13654 i40e_aq_str(hw, hw->aq.asq_last_status));
13655 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
13656 dev_dbg(&pf->pdev->dev,
13657 "Set fc with err %s aq_err %s on set_phy_config\n",
13658 i40e_stat_str(hw, err),
13659 i40e_aq_str(hw, hw->aq.asq_last_status));
13660 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
13661 dev_dbg(&pf->pdev->dev,
13662 "Set fc with err %s aq_err %s on get_link_info\n",
13663 i40e_stat_str(hw, err),
13664 i40e_aq_str(hw, hw->aq.asq_last_status));
13665
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000013666 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000013667 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000013668 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
13669 i40e_vsi_open(pf->vsi[i]);
13670 break;
13671 }
13672 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013673
Shannon Nelson2f0aff42016-01-04 10:33:08 -080013674 /* The driver only wants link up/down and module qualification
13675 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000013676 */
13677 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080013678 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070013679 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080013680 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000013681 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040013682 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
13683 i40e_stat_str(&pf->hw, err),
13684 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000013685
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040013686 /* Reconfigure hardware for allowing smaller MSS in the case
13687 * of TSO, so that we avoid the MDD being fired and causing
13688 * a reset in the case of small MSS+TSO.
13689 */
13690 val = rd32(hw, I40E_REG_MSS);
13691 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
13692 val &= ~I40E_REG_MSS_MIN_MASK;
13693 val |= I40E_64BYTE_MSS;
13694 wr32(hw, I40E_REG_MSS, val);
13695 }
13696
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013697 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000013698 msleep(75);
13699 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
13700 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040013701 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
13702 i40e_stat_str(&pf->hw, err),
13703 i40e_aq_str(&pf->hw,
13704 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000013705 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013706 /* The main driver is (mostly) up and happy. We need to set this state
13707 * before setting up the misc vector or we get a race and the vector
13708 * ends up disabled forever.
13709 */
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013710 clear_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013711
13712 /* In case of MSIX we are going to setup the misc vector right here
13713 * to handle admin queue events etc. In case of legacy and MSI
13714 * the misc functionality and queue processing is combined in
13715 * the same vector and that gets setup at open.
13716 */
13717 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13718 err = i40e_setup_misc_vector(pf);
13719 if (err) {
13720 dev_info(&pdev->dev,
13721 "setup of misc vector failed: %d\n", err);
13722 goto err_vsis;
13723 }
13724 }
13725
Greg Rosedf805f62014-04-04 04:43:16 +000013726#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013727 /* prep for VF support */
13728 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000013729 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
Jacob Keller0da36b92017-04-19 09:25:55 -040013730 !test_bit(__I40E_BAD_EEPROM, pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013731 /* disable link interrupts for VFs */
13732 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
13733 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
13734 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
13735 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080013736
13737 if (pci_num_vf(pdev)) {
13738 dev_info(&pdev->dev,
13739 "Active VFs found, allocating resources.\n");
13740 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
13741 if (err)
13742 dev_info(&pdev->dev,
13743 "Error %d allocating resources for existing VFs\n",
13744 err);
13745 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013746 }
Greg Rosedf805f62014-04-04 04:43:16 +000013747#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013748
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013749 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13750 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
13751 pf->num_iwarp_msix,
13752 I40E_IWARP_IRQ_PILE_ID);
13753 if (pf->iwarp_base_vector < 0) {
13754 dev_info(&pdev->dev,
13755 "failed to get tracking for %d vectors for IWARP err=%d\n",
13756 pf->num_iwarp_msix, pf->iwarp_base_vector);
13757 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
13758 }
13759 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000013760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013761 i40e_dbg_pf_init(pf);
13762
13763 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000013764 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013765
13766 /* since everything's happy, start the service_task timer */
13767 mod_timer(&pf->service_timer,
13768 round_jiffies(jiffies + pf->service_timer_period));
13769
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013770 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070013771 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13772 err = i40e_lan_add_device(pf);
13773 if (err)
13774 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
13775 err);
13776 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013777
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013778#define PCI_SPEED_SIZE 8
13779#define PCI_WIDTH_SIZE 8
13780 /* Devices on the IOSF bus do not have this information
13781 * and will report PCI Gen 1 x 1 by default so don't bother
13782 * checking them.
13783 */
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013784 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013785 char speed[PCI_SPEED_SIZE] = "Unknown";
13786 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013787
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013788 /* Get the negotiated link width and speed from PCI config
13789 * space
13790 */
13791 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
13792 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013793
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013794 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013795
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040013796 switch (hw->bus.speed) {
13797 case i40e_bus_speed_8000:
13798 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
13799 case i40e_bus_speed_5000:
13800 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
13801 case i40e_bus_speed_2500:
13802 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
13803 default:
13804 break;
13805 }
13806 switch (hw->bus.width) {
13807 case i40e_bus_width_pcie_x8:
13808 strncpy(width, "8", PCI_WIDTH_SIZE); break;
13809 case i40e_bus_width_pcie_x4:
13810 strncpy(width, "4", PCI_WIDTH_SIZE); break;
13811 case i40e_bus_width_pcie_x2:
13812 strncpy(width, "2", PCI_WIDTH_SIZE); break;
13813 case i40e_bus_width_pcie_x1:
13814 strncpy(width, "1", PCI_WIDTH_SIZE); break;
13815 default:
13816 break;
13817 }
13818
13819 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
13820 speed, width);
13821
13822 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
13823 hw->bus.speed < i40e_bus_speed_8000) {
13824 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
13825 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
13826 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000013827 }
13828
Catherine Sullivane8278452015-02-06 08:52:08 +000013829 /* get the requested speeds from the fw */
13830 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
13831 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040013832 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
13833 i40e_stat_str(&pf->hw, err),
13834 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000013835 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
13836
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040013837 /* get the supported phy types from the fw */
13838 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
13839 if (err)
13840 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
13841 i40e_stat_str(&pf->hw, err),
13842 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040013843
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040013844 /* Add a filter to drop all Flow control frames from any VSI from being
13845 * transmitted. By doing so we stop a malicious VF from sending out
13846 * PAUSE or PFC frames and potentially controlling traffic for other
13847 * PF/VF VSIs.
13848 * The FW can still send Flow control frames if enabled.
13849 */
13850 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
13851 pf->main_vsi_seid);
13852
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080013853 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080013854 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013855 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080013856 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
Jacob Kellerd36e41d2017-06-23 04:24:46 -040013857 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000013858 /* print a string summarizing features */
13859 i40e_print_features(pf);
13860
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013861 return 0;
13862
13863 /* Unwind what we've done if something failed in the setup */
13864err_vsis:
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013865 set_bit(__I40E_DOWN, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013866 i40e_clear_interrupt_scheme(pf);
13867 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000013868err_switch_setup:
13869 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013870 del_timer_sync(&pf->service_timer);
13871err_mac_addr:
13872err_configure_lan_hmc:
13873 (void)i40e_shutdown_lan_hmc(hw);
13874err_init_lan_hmc:
13875 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013876err_sw_init:
13877err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013878err_pf_reset:
13879 iounmap(hw->hw_addr);
13880err_ioremap:
13881 kfree(pf);
13882err_pf_alloc:
13883 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013884 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013885err_pci_reg:
13886err_dma:
13887 pci_disable_device(pdev);
13888 return err;
13889}
13890
13891/**
13892 * i40e_remove - Device removal routine
13893 * @pdev: PCI device information struct
13894 *
13895 * i40e_remove is called by the PCI subsystem to alert the driver
13896 * that is should release a PCI device. This could be caused by a
13897 * Hot-Plug event, or because the driver is going to be removed from
13898 * memory.
13899 **/
13900static void i40e_remove(struct pci_dev *pdev)
13901{
13902 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013903 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013904 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013905 int i;
13906
13907 i40e_dbg_pf_exit(pf);
13908
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000013909 i40e_ptp_stop(pf);
13910
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013911 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080013912 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
13913 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040013914
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013915 /* no more scheduling of any task */
Jacob Keller0da36b92017-04-19 09:25:55 -040013916 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030013917 set_bit(__I40E_DOWN, pf->state);
Kees Cook26566ea2017-10-16 17:29:35 -070013918 if (pf->service_timer.function)
Shannon Nelsonc99abb42016-03-10 14:59:45 -080013919 del_timer_sync(&pf->service_timer);
13920 if (pf->service_task.func)
13921 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013922
Mitch Williams921c4672017-03-30 00:46:08 -070013923 /* Client close must be called explicitly here because the timer
13924 * has been stopped.
13925 */
13926 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
13927
Mitch Williamseb2d80b2014-02-13 03:48:48 -080013928 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
13929 i40e_free_vfs(pf);
13930 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
13931 }
13932
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013933 i40e_fdir_teardown(pf);
13934
13935 /* If there is a switch structure or any orphans, remove them.
13936 * This will leave only the PF's VSI remaining.
13937 */
13938 for (i = 0; i < I40E_MAX_VEB; i++) {
13939 if (!pf->veb[i])
13940 continue;
13941
13942 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
13943 pf->veb[i]->uplink_seid == 0)
13944 i40e_switch_branch_release(pf->veb[i]);
13945 }
13946
13947 /* Now we can shutdown the PF's VSI, just before we kill
13948 * adminq and hmc.
13949 */
13950 if (pf->vsi[pf->lan_vsi])
13951 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
13952
Amritha Nambiaraaf66502017-10-27 02:35:56 -070013953 i40e_cloud_filter_exit(pf);
13954
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013955 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070013956 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
13957 ret_code = i40e_lan_del_device(pf);
13958 if (ret_code)
13959 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
13960 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060013961 }
13962
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013963 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080013964 if (hw->hmc.hmc_obj) {
13965 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000013966 if (ret_code)
13967 dev_warn(&pdev->dev,
13968 "Failed to destroy the HMC resources: %d\n",
13969 ret_code);
13970 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013971
13972 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070013973 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013974
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080013975 /* destroy the locks only once, here */
13976 mutex_destroy(&hw->aq.arq_mutex);
13977 mutex_destroy(&hw->aq.asq_mutex);
13978
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013979 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
13980 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000013981 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013982 if (pf->vsi[i]) {
13983 i40e_vsi_clear_rings(pf->vsi[i]);
13984 i40e_vsi_clear(pf->vsi[i]);
13985 pf->vsi[i] = NULL;
13986 }
13987 }
13988
13989 for (i = 0; i < I40E_MAX_VEB; i++) {
13990 kfree(pf->veb[i]);
13991 pf->veb[i] = NULL;
13992 }
13993
13994 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013995 kfree(pf->vsi);
13996
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080013997 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000013998 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020013999 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014000
14001 pci_disable_pcie_error_reporting(pdev);
14002 pci_disable_device(pdev);
14003}
14004
14005/**
14006 * i40e_pci_error_detected - warning that something funky happened in PCI land
14007 * @pdev: PCI device information struct
14008 *
14009 * Called to warn that something happened and the error handling steps
14010 * are in progress. Allows the driver to quiesce things, be ready for
14011 * remediation.
14012 **/
14013static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
14014 enum pci_channel_state error)
14015{
14016 struct i40e_pf *pf = pci_get_drvdata(pdev);
14017
14018 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
14019
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070014020 if (!pf) {
14021 dev_info(&pdev->dev,
14022 "Cannot recover - error happened during device probe\n");
14023 return PCI_ERS_RESULT_DISCONNECT;
14024 }
14025
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014026 /* shutdown all operations */
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014027 if (!test_bit(__I40E_SUSPENDED, pf->state))
14028 i40e_prep_for_reset(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014029
14030 /* Request a slot reset */
14031 return PCI_ERS_RESULT_NEED_RESET;
14032}
14033
14034/**
14035 * i40e_pci_error_slot_reset - a PCI slot reset just happened
14036 * @pdev: PCI device information struct
14037 *
14038 * Called to find if the driver can work with the device now that
14039 * the pci slot has been reset. If a basic connection seems good
14040 * (registers are readable and have sane content) then return a
14041 * happy little PCI_ERS_RESULT_xxx.
14042 **/
14043static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
14044{
14045 struct i40e_pf *pf = pci_get_drvdata(pdev);
14046 pci_ers_result_t result;
14047 int err;
14048 u32 reg;
14049
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040014050 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014051 if (pci_enable_device_mem(pdev)) {
14052 dev_info(&pdev->dev,
14053 "Cannot re-enable PCI device after reset.\n");
14054 result = PCI_ERS_RESULT_DISCONNECT;
14055 } else {
14056 pci_set_master(pdev);
14057 pci_restore_state(pdev);
14058 pci_save_state(pdev);
14059 pci_wake_from_d3(pdev, false);
14060
14061 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
14062 if (reg == 0)
14063 result = PCI_ERS_RESULT_RECOVERED;
14064 else
14065 result = PCI_ERS_RESULT_DISCONNECT;
14066 }
14067
14068 err = pci_cleanup_aer_uncorrect_error_status(pdev);
14069 if (err) {
14070 dev_info(&pdev->dev,
14071 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
14072 err);
14073 /* non-fatal, continue */
14074 }
14075
14076 return result;
14077}
14078
14079/**
Alan Brady19b79602017-08-29 05:32:39 -040014080 * i40e_pci_error_reset_prepare - prepare device driver for pci reset
14081 * @pdev: PCI device information struct
14082 */
14083static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
14084{
14085 struct i40e_pf *pf = pci_get_drvdata(pdev);
14086
14087 i40e_prep_for_reset(pf, false);
14088}
14089
14090/**
14091 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
14092 * @pdev: PCI device information struct
14093 */
14094static void i40e_pci_error_reset_done(struct pci_dev *pdev)
14095{
14096 struct i40e_pf *pf = pci_get_drvdata(pdev);
14097
14098 i40e_reset_and_rebuild(pf, false, false);
14099}
14100
14101/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014102 * i40e_pci_error_resume - restart operations after PCI error recovery
14103 * @pdev: PCI device information struct
14104 *
14105 * Called to allow the driver to bring things back up after PCI error
14106 * and/or reset recovery has finished.
14107 **/
14108static void i40e_pci_error_resume(struct pci_dev *pdev)
14109{
14110 struct i40e_pf *pf = pci_get_drvdata(pdev);
14111
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040014112 dev_dbg(&pdev->dev, "%s\n", __func__);
Jacob Keller0da36b92017-04-19 09:25:55 -040014113 if (test_bit(__I40E_SUSPENDED, pf->state))
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014114 return;
14115
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014116 i40e_handle_reset_warning(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014117}
14118
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014119/**
Joshua Hay1d680052016-12-12 15:44:08 -080014120 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
14121 * using the mac_address_write admin q function
14122 * @pf: pointer to i40e_pf struct
14123 **/
14124static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
14125{
14126 struct i40e_hw *hw = &pf->hw;
14127 i40e_status ret;
14128 u8 mac_addr[6];
14129 u16 flags = 0;
14130
14131 /* Get current MAC address in case it's an LAA */
14132 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
14133 ether_addr_copy(mac_addr,
14134 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
14135 } else {
14136 dev_err(&pf->pdev->dev,
14137 "Failed to retrieve MAC address; using default\n");
14138 ether_addr_copy(mac_addr, hw->mac.addr);
14139 }
14140
14141 /* The FW expects the mac address write cmd to first be called with
14142 * one of these flags before calling it again with the multicast
14143 * enable flags.
14144 */
14145 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
14146
14147 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
14148 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
14149
14150 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14151 if (ret) {
14152 dev_err(&pf->pdev->dev,
14153 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
14154 return;
14155 }
14156
14157 flags = I40E_AQC_MC_MAG_EN
14158 | I40E_AQC_WOL_PRESERVE_ON_PFR
14159 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
14160 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
14161 if (ret)
14162 dev_err(&pf->pdev->dev,
14163 "Failed to enable Multicast Magic Packet wake up\n");
14164}
14165
14166/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014167 * i40e_shutdown - PCI callback for shutting down
14168 * @pdev: PCI device information struct
14169 **/
14170static void i40e_shutdown(struct pci_dev *pdev)
14171{
14172 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014173 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014174
Jacob Keller0da36b92017-04-19 09:25:55 -040014175 set_bit(__I40E_SUSPENDED, pf->state);
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030014176 set_bit(__I40E_DOWN, pf->state);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014177 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040014178 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014179 rtnl_unlock();
14180
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014181 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14182 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14183
Catherine Sullivan02b42492015-07-10 19:35:59 -040014184 del_timer_sync(&pf->service_timer);
14185 cancel_work_sync(&pf->service_task);
Amritha Nambiaraaf66502017-10-27 02:35:56 -070014186 i40e_cloud_filter_exit(pf);
Catherine Sullivan02b42492015-07-10 19:35:59 -040014187 i40e_fdir_teardown(pf);
14188
Mitch Williams921c4672017-03-30 00:46:08 -070014189 /* Client close must be called explicitly here because the timer
14190 * has been stopped.
14191 */
14192 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
14193
Jacob Kellerd36e41d2017-06-23 04:24:46 -040014194 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080014195 i40e_enable_mc_magic_wake(pf);
14196
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014197 i40e_prep_for_reset(pf, false);
Catherine Sullivan02b42492015-07-10 19:35:59 -040014198
14199 wr32(hw, I40E_PFPM_APM,
14200 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14201 wr32(hw, I40E_PFPM_WUFC,
14202 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14203
Shannon Nelsone1477582015-02-21 06:44:33 +000014204 i40e_clear_interrupt_scheme(pf);
14205
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014206 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014207 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014208 pci_set_power_state(pdev, PCI_D3hot);
14209 }
14210}
14211
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014212/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014213 * i40e_suspend - PM callback for moving to D3
14214 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014215 **/
Arnd Bergmann254d1522017-10-10 10:17:38 +020014216static int __maybe_unused i40e_suspend(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014217{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014218 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014219 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014220 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014221
Jacob Keller401586c2017-07-14 09:27:04 -040014222 /* If we're already suspended, then there is nothing to do */
14223 if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
14224 return 0;
14225
Mauro S. M. Rodrigues9e6c9c02017-05-12 23:26:56 -030014226 set_bit(__I40E_DOWN, pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070014227
Jacob Keller5c499222017-07-14 09:27:05 -040014228 /* Ensure service task will not be running */
14229 del_timer_sync(&pf->service_timer);
14230 cancel_work_sync(&pf->service_task);
14231
Jacob Kellerd36e41d2017-06-23 04:24:46 -040014232 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
Joshua Hay1d680052016-12-12 15:44:08 -080014233 i40e_enable_mc_magic_wake(pf);
14234
Jacob Kellerdfc4ff62017-06-07 05:43:13 -040014235 i40e_prep_for_reset(pf, false);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014236
Shannon Nelson8e2773a2013-11-28 06:39:22 +000014237 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
14238 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
14239
Jacob Kellerb980c062017-07-14 09:27:06 -040014240 /* Clear the interrupt scheme and release our IRQs so that the system
14241 * can safely hibernate even when there are a large number of CPUs.
14242 * Otherwise hibernation might fail when mapping all the vectors back
14243 * to CPU0.
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014244 */
Jacob Kellerb980c062017-07-14 09:27:06 -040014245 i40e_clear_interrupt_scheme(pf);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014246
14247 return 0;
14248}
14249
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014250/**
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014251 * i40e_resume - PM callback for waking up from D3
14252 * @dev: generic device information structure
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014253 **/
Arnd Bergmann254d1522017-10-10 10:17:38 +020014254static int __maybe_unused i40e_resume(struct device *dev)
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014255{
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014256 struct pci_dev *pdev = to_pci_dev(dev);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000014257 struct i40e_pf *pf = pci_get_drvdata(pdev);
Jacob Kellerb980c062017-07-14 09:27:06 -040014258 int err;
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014259
Jacob Keller401586c2017-07-14 09:27:04 -040014260 /* If we're not suspended, then there is nothing to do */
14261 if (!test_bit(__I40E_SUSPENDED, pf->state))
14262 return 0;
14263
Jacob Kellerb980c062017-07-14 09:27:06 -040014264 /* We cleared the interrupt scheme when we suspended, so we need to
14265 * restore it now to resume device functionality.
14266 */
14267 err = i40e_restore_interrupt_scheme(pf);
14268 if (err) {
14269 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
14270 err);
14271 }
14272
Jacob Keller401586c2017-07-14 09:27:04 -040014273 clear_bit(__I40E_DOWN, pf->state);
14274 i40e_reset_and_rebuild(pf, false, false);
14275
14276 /* Clear suspended state last after everything is recovered */
14277 clear_bit(__I40E_SUSPENDED, pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014278
Jacob Keller5c499222017-07-14 09:27:05 -040014279 /* Restart the service task */
14280 mod_timer(&pf->service_timer,
14281 round_jiffies(jiffies + pf->service_timer_period));
14282
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014283 return 0;
14284}
14285
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014286static const struct pci_error_handlers i40e_err_handler = {
14287 .error_detected = i40e_pci_error_detected,
14288 .slot_reset = i40e_pci_error_slot_reset,
Alan Brady19b79602017-08-29 05:32:39 -040014289 .reset_prepare = i40e_pci_error_reset_prepare,
14290 .reset_done = i40e_pci_error_reset_done,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014291 .resume = i40e_pci_error_resume,
14292};
14293
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014294static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
14295
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014296static struct pci_driver i40e_driver = {
14297 .name = i40e_driver_name,
14298 .id_table = i40e_pci_tbl,
14299 .probe = i40e_probe,
14300 .remove = i40e_remove,
Jacob Keller0e5d3da2017-07-14 09:27:03 -040014301 .driver = {
14302 .pm = &i40e_pm_ops,
14303 },
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014304 .shutdown = i40e_shutdown,
14305 .err_handler = &i40e_err_handler,
14306 .sriov_configure = i40e_pci_sriov_configure,
14307};
14308
14309/**
14310 * i40e_init_module - Driver registration routine
14311 *
14312 * i40e_init_module is the first routine called when the driver is
14313 * loaded. All it does is register with the PCI subsystem.
14314 **/
14315static int __init i40e_init_module(void)
14316{
14317 pr_info("%s: %s - version %s\n", i40e_driver_name,
14318 i40e_driver_string, i40e_driver_version_str);
14319 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000014320
Jacob Keller4d5957c2017-06-20 15:16:54 -070014321 /* There is no need to throttle the number of active tasks because
14322 * each device limits its own task using a state bit for scheduling
14323 * the service task, and the device tasks do not interfere with each
14324 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
14325 * since we need to be able to guarantee forward progress even under
14326 * memory pressure.
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014327 */
Jacob Keller4d5957c2017-06-20 15:16:54 -070014328 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014329 if (!i40e_wq) {
14330 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
14331 return -ENOMEM;
14332 }
14333
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014334 i40e_dbg_init();
14335 return pci_register_driver(&i40e_driver);
14336}
14337module_init(i40e_init_module);
14338
14339/**
14340 * i40e_exit_module - Driver exit cleanup routine
14341 *
14342 * i40e_exit_module is called just before the driver is removed
14343 * from memory.
14344 **/
14345static void __exit i40e_exit_module(void)
14346{
14347 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080014348 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014349 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000014350}
14351module_exit(i40e_exit_module);