blob: 0630442681705052ff72fbdb64dd28ed111b6a97 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08004 * Copyright(c) 2013 - 2016 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>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070034#include <net/udp_tunnel.h>
Scott Petersoned0980c2017-04-13 04:45:44 -040035/* All i40e tracepoints are defined by the include below, which
36 * must be included exactly once across the whole kernel with
37 * CREATE_TRACE_POINTS defined
38 */
39#define CREATE_TRACE_POINTS
40#include "i40e_trace.h"
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000041
42const char i40e_driver_name[] = "i40e";
43static const char i40e_driver_string[] =
44 "Intel(R) Ethernet Connection XL710 Network Driver";
45
46#define DRV_KERN "-k"
47
Bimmy Pujari15990832017-01-30 12:29:37 -080048#define DRV_VERSION_MAJOR 2
49#define DRV_VERSION_MINOR 1
50#define DRV_VERSION_BUILD 7
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000051#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
52 __stringify(DRV_VERSION_MINOR) "." \
53 __stringify(DRV_VERSION_BUILD) DRV_KERN
54const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080055static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000056
57/* a bit of forward declarations */
58static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
Maciej Sosin373149f2017-04-05 07:50:55 -040059static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000060static int i40e_add_vsi(struct i40e_vsi *vsi);
61static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000062static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000063static int i40e_setup_misc_vector(struct i40e_pf *pf);
64static void i40e_determine_queue_usage(struct i40e_pf *pf);
65static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Maciej Sosin373149f2017-04-05 07:50:55 -040066static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
67static int i40e_reset(struct i40e_pf *pf);
68static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080069static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080070static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000071
72/* i40e_pci_tbl - PCI Device ID Table
73 *
74 * Last entry must be all 0s
75 *
76 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
77 * Class, Class Mask, private data (not used) }
78 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020079static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080084 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040091 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070094 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040095 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
96 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080097 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
98 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000099 /* required last entry */
100 {0, }
101};
102MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
103
104#define I40E_MAX_VF_COUNT 128
105static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400106module_param(debug, uint, 0);
107MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000108
109MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
110MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
111MODULE_LICENSE("GPL");
112MODULE_VERSION(DRV_VERSION);
113
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800114static struct workqueue_struct *i40e_wq;
115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000116/**
117 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
118 * @hw: pointer to the HW structure
119 * @mem: ptr to mem struct to fill out
120 * @size: size of memory requested
121 * @alignment: what to align the allocation to
122 **/
123int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
124 u64 size, u32 alignment)
125{
126 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
127
128 mem->size = ALIGN(size, alignment);
129 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
130 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000131 if (!mem->va)
132 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000133
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000134 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000135}
136
137/**
138 * i40e_free_dma_mem_d - OS specific memory free for shared code
139 * @hw: pointer to the HW structure
140 * @mem: ptr to mem struct to free
141 **/
142int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
143{
144 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
145
146 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
147 mem->va = NULL;
148 mem->pa = 0;
149 mem->size = 0;
150
151 return 0;
152}
153
154/**
155 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
156 * @hw: pointer to the HW structure
157 * @mem: ptr to mem struct to fill out
158 * @size: size of memory requested
159 **/
160int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
161 u32 size)
162{
163 mem->size = size;
164 mem->va = kzalloc(size, GFP_KERNEL);
165
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000166 if (!mem->va)
167 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000168
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000169 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000170}
171
172/**
173 * i40e_free_virt_mem_d - OS specific memory free for shared code
174 * @hw: pointer to the HW structure
175 * @mem: ptr to mem struct to free
176 **/
177int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
178{
179 /* it's ok to kfree a NULL pointer */
180 kfree(mem->va);
181 mem->va = NULL;
182 mem->size = 0;
183
184 return 0;
185}
186
187/**
188 * i40e_get_lump - find a lump of free generic resource
189 * @pf: board private structure
190 * @pile: the pile of resource to search
191 * @needed: the number of items needed
192 * @id: an owner id to stick on the items assigned
193 *
194 * Returns the base item index of the lump, or negative for error
195 *
196 * The search_hint trick and lack of advanced fit-finding only work
197 * because we're highly likely to have all the same size lump requests.
198 * Linear search time and any fragmentation should be minimal.
199 **/
200static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
201 u16 needed, u16 id)
202{
203 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000204 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000205
206 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
207 dev_info(&pf->pdev->dev,
208 "param err: pile=%p needed=%d id=0x%04x\n",
209 pile, needed, id);
210 return -EINVAL;
211 }
212
213 /* start the linear search with an imperfect hint */
214 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000215 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000216 /* skip already allocated entries */
217 if (pile->list[i] & I40E_PILE_VALID_BIT) {
218 i++;
219 continue;
220 }
221
222 /* do we have enough in this lump? */
223 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
224 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
225 break;
226 }
227
228 if (j == needed) {
229 /* there was enough, so assign it to the requestor */
230 for (j = 0; j < needed; j++)
231 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
232 ret = i;
233 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000234 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000235 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400236
237 /* not enough, so skip over it and continue looking */
238 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000239 }
240
241 return ret;
242}
243
244/**
245 * i40e_put_lump - return a lump of generic resource
246 * @pile: the pile of resource to search
247 * @index: the base item index
248 * @id: the owner id of the items assigned
249 *
250 * Returns the count of items in the lump
251 **/
252static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
253{
254 int valid_id = (id | I40E_PILE_VALID_BIT);
255 int count = 0;
256 int i;
257
258 if (!pile || index >= pile->num_entries)
259 return -EINVAL;
260
261 for (i = index;
262 i < pile->num_entries && pile->list[i] == valid_id;
263 i++) {
264 pile->list[i] = 0;
265 count++;
266 }
267
268 if (count && index < pile->search_hint)
269 pile->search_hint = index;
270
271 return count;
272}
273
274/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700275 * i40e_find_vsi_from_id - searches for the vsi with the given id
276 * @pf - the pf structure to search for the vsi
277 * @id - id of the vsi it is searching for
278 **/
279struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
280{
281 int i;
282
283 for (i = 0; i < pf->num_alloc_vsi; i++)
284 if (pf->vsi[i] && (pf->vsi[i]->id == id))
285 return pf->vsi[i];
286
287 return NULL;
288}
289
290/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000291 * i40e_service_event_schedule - Schedule the service task to wake up
292 * @pf: board private structure
293 *
294 * If not already scheduled, this puts the task into the work queue
295 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600296void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000297{
298 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800299 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800300 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000301}
302
303/**
304 * i40e_tx_timeout - Respond to a Tx Hang
305 * @netdev: network interface device structure
306 *
307 * If any port has noticed a Tx timeout, it is likely that the whole
308 * device is munged, not just the one netdev port, so go for the full
309 * reset.
310 **/
311static void i40e_tx_timeout(struct net_device *netdev)
312{
313 struct i40e_netdev_priv *np = netdev_priv(netdev);
314 struct i40e_vsi *vsi = np->vsi;
315 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400316 struct i40e_ring *tx_ring = NULL;
317 unsigned int i, hung_queue = 0;
318 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000319
320 pf->tx_timeout_count++;
321
Kiran Patilb03a8c12015-09-24 18:13:15 -0400322 /* find the stopped queue the same way the stack does */
323 for (i = 0; i < netdev->num_tx_queues; i++) {
324 struct netdev_queue *q;
325 unsigned long trans_start;
326
327 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200328 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400329 if (netif_xmit_stopped(q) &&
330 time_after(jiffies,
331 (trans_start + netdev->watchdog_timeo))) {
332 hung_queue = i;
333 break;
334 }
335 }
336
337 if (i == netdev->num_tx_queues) {
338 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
339 } else {
340 /* now that we have an index, find the tx_ring struct */
341 for (i = 0; i < vsi->num_queue_pairs; i++) {
342 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
343 if (hung_queue ==
344 vsi->tx_rings[i]->queue_index) {
345 tx_ring = vsi->tx_rings[i];
346 break;
347 }
348 }
349 }
350 }
351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000352 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400353 pf->tx_timeout_recovery_level = 1; /* reset after some time */
354 else if (time_before(jiffies,
355 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
356 return; /* don't do any new action before the next timeout */
357
358 if (tx_ring) {
359 head = i40e_get_head(tx_ring);
360 /* Read interrupt register */
361 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
362 val = rd32(&pf->hw,
363 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
364 tx_ring->vsi->base_vector - 1));
365 else
366 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
367
368 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",
369 vsi->seid, hung_queue, tx_ring->next_to_clean,
370 head, tx_ring->next_to_use,
371 readl(tx_ring->tail), val);
372 }
373
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000374 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400375 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
376 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000377
378 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000379 case 1:
380 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
381 break;
382 case 2:
383 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
384 break;
385 case 3:
386 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
387 break;
388 default:
389 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000390 break;
391 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400392
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000393 i40e_service_event_schedule(pf);
394 pf->tx_timeout_recovery_level++;
395}
396
397/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000398 * i40e_get_vsi_stats_struct - Get System Network Statistics
399 * @vsi: the VSI we care about
400 *
401 * Returns the address of the device statistics structure.
402 * The statistics are actually updated from the service task.
403 **/
404struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
405{
406 return &vsi->net_stats;
407}
408
409/**
410 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
411 * @netdev: network interface device structure
412 *
413 * Returns the address of the device statistics structure.
414 * The statistics are actually updated from the service task.
415 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800416static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800417 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000418{
419 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000420 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000421 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000422 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
423 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000424
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000425 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800426 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000427
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800428 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800429 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800430
Alexander Duyck980e9b12013-09-28 06:01:03 +0000431 rcu_read_lock();
432 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000433 u64 bytes, packets;
434 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000435
Alexander Duyck980e9b12013-09-28 06:01:03 +0000436 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
437 if (!tx_ring)
438 continue;
439
440 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700441 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000442 packets = tx_ring->stats.packets;
443 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700444 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000445
446 stats->tx_packets += packets;
447 stats->tx_bytes += bytes;
448 rx_ring = &tx_ring[1];
449
450 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700451 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000452 packets = rx_ring->stats.packets;
453 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700454 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455
456 stats->rx_packets += packets;
457 stats->rx_bytes += bytes;
458 }
459 rcu_read_unlock();
460
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000461 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462 stats->multicast = vsi_stats->multicast;
463 stats->tx_errors = vsi_stats->tx_errors;
464 stats->tx_dropped = vsi_stats->tx_dropped;
465 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400466 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000467 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
468 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000469}
470
471/**
472 * i40e_vsi_reset_stats - Resets all stats of the given vsi
473 * @vsi: the VSI to have its stats reset
474 **/
475void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
476{
477 struct rtnl_link_stats64 *ns;
478 int i;
479
480 if (!vsi)
481 return;
482
483 ns = i40e_get_vsi_stats_struct(vsi);
484 memset(ns, 0, sizeof(*ns));
485 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
486 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
487 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000488 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000489 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400490 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000491 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400492 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000493 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400494 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000495 sizeof(vsi->tx_rings[i]->stats));
496 memset(&vsi->tx_rings[i]->tx_stats, 0,
497 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000498 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000499 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000500 vsi->stat_offsets_loaded = false;
501}
502
503/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000504 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000505 * @pf: the PF to be reset
506 **/
507void i40e_pf_reset_stats(struct i40e_pf *pf)
508{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000509 int i;
510
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000511 memset(&pf->stats, 0, sizeof(pf->stats));
512 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
513 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000514
515 for (i = 0; i < I40E_MAX_VEB; i++) {
516 if (pf->veb[i]) {
517 memset(&pf->veb[i]->stats, 0,
518 sizeof(pf->veb[i]->stats));
519 memset(&pf->veb[i]->stats_offsets, 0,
520 sizeof(pf->veb[i]->stats_offsets));
521 pf->veb[i]->stat_offsets_loaded = false;
522 }
523 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700524 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000525}
526
527/**
528 * i40e_stat_update48 - read and update a 48 bit stat from the chip
529 * @hw: ptr to the hardware info
530 * @hireg: the high 32 bit reg to read
531 * @loreg: the low 32 bit reg to read
532 * @offset_loaded: has the initial offset been loaded yet
533 * @offset: ptr to current offset value
534 * @stat: ptr to the stat
535 *
536 * Since the device stats are not reset at PFReset, they likely will not
537 * be zeroed when the driver starts. We'll save the first values read
538 * and use them as offsets to be subtracted from the raw values in order
539 * to report stats that count from zero. In the process, we also manage
540 * the potential roll-over.
541 **/
542static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
543 bool offset_loaded, u64 *offset, u64 *stat)
544{
545 u64 new_data;
546
Shannon Nelsonab600852014-01-17 15:36:39 -0800547 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000548 new_data = rd32(hw, loreg);
549 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
550 } else {
551 new_data = rd64(hw, loreg);
552 }
553 if (!offset_loaded)
554 *offset = new_data;
555 if (likely(new_data >= *offset))
556 *stat = new_data - *offset;
557 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400558 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000559 *stat &= 0xFFFFFFFFFFFFULL;
560}
561
562/**
563 * i40e_stat_update32 - read and update a 32 bit stat from the chip
564 * @hw: ptr to the hardware info
565 * @reg: the hw reg to read
566 * @offset_loaded: has the initial offset been loaded yet
567 * @offset: ptr to current offset value
568 * @stat: ptr to the stat
569 **/
570static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
571 bool offset_loaded, u64 *offset, u64 *stat)
572{
573 u32 new_data;
574
575 new_data = rd32(hw, reg);
576 if (!offset_loaded)
577 *offset = new_data;
578 if (likely(new_data >= *offset))
579 *stat = (u32)(new_data - *offset);
580 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400581 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000582}
583
584/**
585 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
586 * @vsi: the VSI to be updated
587 **/
588void i40e_update_eth_stats(struct i40e_vsi *vsi)
589{
590 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
591 struct i40e_pf *pf = vsi->back;
592 struct i40e_hw *hw = &pf->hw;
593 struct i40e_eth_stats *oes;
594 struct i40e_eth_stats *es; /* device's eth stats */
595
596 es = &vsi->eth_stats;
597 oes = &vsi->eth_stats_offsets;
598
599 /* Gather up the stats that the hw collects */
600 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
601 vsi->stat_offsets_loaded,
602 &oes->tx_errors, &es->tx_errors);
603 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
604 vsi->stat_offsets_loaded,
605 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000606 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
607 vsi->stat_offsets_loaded,
608 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
609 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
610 vsi->stat_offsets_loaded,
611 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000612
613 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
614 I40E_GLV_GORCL(stat_idx),
615 vsi->stat_offsets_loaded,
616 &oes->rx_bytes, &es->rx_bytes);
617 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
618 I40E_GLV_UPRCL(stat_idx),
619 vsi->stat_offsets_loaded,
620 &oes->rx_unicast, &es->rx_unicast);
621 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
622 I40E_GLV_MPRCL(stat_idx),
623 vsi->stat_offsets_loaded,
624 &oes->rx_multicast, &es->rx_multicast);
625 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
626 I40E_GLV_BPRCL(stat_idx),
627 vsi->stat_offsets_loaded,
628 &oes->rx_broadcast, &es->rx_broadcast);
629
630 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
631 I40E_GLV_GOTCL(stat_idx),
632 vsi->stat_offsets_loaded,
633 &oes->tx_bytes, &es->tx_bytes);
634 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
635 I40E_GLV_UPTCL(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->tx_unicast, &es->tx_unicast);
638 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
639 I40E_GLV_MPTCL(stat_idx),
640 vsi->stat_offsets_loaded,
641 &oes->tx_multicast, &es->tx_multicast);
642 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
643 I40E_GLV_BPTCL(stat_idx),
644 vsi->stat_offsets_loaded,
645 &oes->tx_broadcast, &es->tx_broadcast);
646 vsi->stat_offsets_loaded = true;
647}
648
649/**
650 * i40e_update_veb_stats - Update Switch component statistics
651 * @veb: the VEB being updated
652 **/
653static void i40e_update_veb_stats(struct i40e_veb *veb)
654{
655 struct i40e_pf *pf = veb->pf;
656 struct i40e_hw *hw = &pf->hw;
657 struct i40e_eth_stats *oes;
658 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400659 struct i40e_veb_tc_stats *veb_oes;
660 struct i40e_veb_tc_stats *veb_es;
661 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000662
663 idx = veb->stats_idx;
664 es = &veb->stats;
665 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400666 veb_es = &veb->tc_stats;
667 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000668
669 /* Gather up the stats that the hw collects */
670 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
671 veb->stat_offsets_loaded,
672 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000673 if (hw->revision_id > 0)
674 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
675 veb->stat_offsets_loaded,
676 &oes->rx_unknown_protocol,
677 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000678 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
679 veb->stat_offsets_loaded,
680 &oes->rx_bytes, &es->rx_bytes);
681 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
682 veb->stat_offsets_loaded,
683 &oes->rx_unicast, &es->rx_unicast);
684 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
685 veb->stat_offsets_loaded,
686 &oes->rx_multicast, &es->rx_multicast);
687 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
688 veb->stat_offsets_loaded,
689 &oes->rx_broadcast, &es->rx_broadcast);
690
691 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
692 veb->stat_offsets_loaded,
693 &oes->tx_bytes, &es->tx_bytes);
694 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
695 veb->stat_offsets_loaded,
696 &oes->tx_unicast, &es->tx_unicast);
697 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
698 veb->stat_offsets_loaded,
699 &oes->tx_multicast, &es->tx_multicast);
700 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
701 veb->stat_offsets_loaded,
702 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400703 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
704 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
705 I40E_GLVEBTC_RPCL(i, idx),
706 veb->stat_offsets_loaded,
707 &veb_oes->tc_rx_packets[i],
708 &veb_es->tc_rx_packets[i]);
709 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
710 I40E_GLVEBTC_RBCL(i, idx),
711 veb->stat_offsets_loaded,
712 &veb_oes->tc_rx_bytes[i],
713 &veb_es->tc_rx_bytes[i]);
714 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
715 I40E_GLVEBTC_TPCL(i, idx),
716 veb->stat_offsets_loaded,
717 &veb_oes->tc_tx_packets[i],
718 &veb_es->tc_tx_packets[i]);
719 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
720 I40E_GLVEBTC_TBCL(i, idx),
721 veb->stat_offsets_loaded,
722 &veb_oes->tc_tx_bytes[i],
723 &veb_es->tc_tx_bytes[i]);
724 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000725 veb->stat_offsets_loaded = true;
726}
727
728/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000729 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000730 * @vsi: the VSI to be updated
731 *
732 * There are a few instances where we store the same stat in a
733 * couple of different structs. This is partly because we have
734 * the netdev stats that need to be filled out, which is slightly
735 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000736 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000737 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000738static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000739{
740 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000741 struct rtnl_link_stats64 *ons;
742 struct rtnl_link_stats64 *ns; /* netdev stats */
743 struct i40e_eth_stats *oes;
744 struct i40e_eth_stats *es; /* device's eth stats */
745 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000746 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000747 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000748 u64 bytes, packets;
749 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400750 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400751 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000752 u64 rx_p, rx_b;
753 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000754 u16 q;
755
756 if (test_bit(__I40E_DOWN, &vsi->state) ||
757 test_bit(__I40E_CONFIG_BUSY, &pf->state))
758 return;
759
760 ns = i40e_get_vsi_stats_struct(vsi);
761 ons = &vsi->net_stats_offsets;
762 es = &vsi->eth_stats;
763 oes = &vsi->eth_stats_offsets;
764
765 /* Gather up the netdev and vsi stats that the driver collects
766 * on the fly during packet processing
767 */
768 rx_b = rx_p = 0;
769 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400770 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771 rx_page = 0;
772 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000773 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000774 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000775 /* locate Tx ring */
776 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000777
Alexander Duyck980e9b12013-09-28 06:01:03 +0000778 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700779 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000780 packets = p->stats.packets;
781 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700782 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000783 tx_b += bytes;
784 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000785 tx_restart += p->tx_stats.restart_queue;
786 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400787 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400788 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000789
790 /* Rx queue is part of the same block as Tx queue */
791 p = &p[1];
792 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700793 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000794 packets = p->stats.packets;
795 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700796 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000797 rx_b += bytes;
798 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000799 rx_buf += p->rx_stats.alloc_buff_failed;
800 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000802 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 vsi->tx_restart = tx_restart;
804 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400805 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400806 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000807 vsi->rx_page_failed = rx_page;
808 vsi->rx_buf_failed = rx_buf;
809
810 ns->rx_packets = rx_p;
811 ns->rx_bytes = rx_b;
812 ns->tx_packets = tx_p;
813 ns->tx_bytes = tx_b;
814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000815 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000816 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 ons->tx_errors = oes->tx_errors;
818 ns->tx_errors = es->tx_errors;
819 ons->multicast = oes->rx_multicast;
820 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000821 ons->rx_dropped = oes->rx_discards;
822 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000823 ons->tx_dropped = oes->tx_discards;
824 ns->tx_dropped = es->tx_discards;
825
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000826 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000828 ns->rx_crc_errors = pf->stats.crc_errors;
829 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
830 ns->rx_length_errors = pf->stats.rx_length_errors;
831 }
832}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000834/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000835 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000836 * @pf: the PF to be updated
837 **/
838static void i40e_update_pf_stats(struct i40e_pf *pf)
839{
840 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
841 struct i40e_hw_port_stats *nsd = &pf->stats;
842 struct i40e_hw *hw = &pf->hw;
843 u32 val;
844 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000845
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000846 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
847 I40E_GLPRT_GORCL(hw->port),
848 pf->stat_offsets_loaded,
849 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
850 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
851 I40E_GLPRT_GOTCL(hw->port),
852 pf->stat_offsets_loaded,
853 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
854 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
855 pf->stat_offsets_loaded,
856 &osd->eth.rx_discards,
857 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000858 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
859 I40E_GLPRT_UPRCL(hw->port),
860 pf->stat_offsets_loaded,
861 &osd->eth.rx_unicast,
862 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000863 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
864 I40E_GLPRT_MPRCL(hw->port),
865 pf->stat_offsets_loaded,
866 &osd->eth.rx_multicast,
867 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000868 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
869 I40E_GLPRT_BPRCL(hw->port),
870 pf->stat_offsets_loaded,
871 &osd->eth.rx_broadcast,
872 &nsd->eth.rx_broadcast);
873 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
874 I40E_GLPRT_UPTCL(hw->port),
875 pf->stat_offsets_loaded,
876 &osd->eth.tx_unicast,
877 &nsd->eth.tx_unicast);
878 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
879 I40E_GLPRT_MPTCL(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->eth.tx_multicast,
882 &nsd->eth.tx_multicast);
883 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
884 I40E_GLPRT_BPTCL(hw->port),
885 pf->stat_offsets_loaded,
886 &osd->eth.tx_broadcast,
887 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000888
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000889 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
890 pf->stat_offsets_loaded,
891 &osd->tx_dropped_link_down,
892 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000893
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000894 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
895 pf->stat_offsets_loaded,
896 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000897
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000898 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
899 pf->stat_offsets_loaded,
900 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000901
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000902 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
903 pf->stat_offsets_loaded,
904 &osd->mac_local_faults,
905 &nsd->mac_local_faults);
906 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->mac_remote_faults,
909 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000910
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000911 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->rx_length_errors,
914 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000915
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000916 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->link_xon_rx, &nsd->link_xon_rx);
919 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
920 pf->stat_offsets_loaded,
921 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800922 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
923 pf->stat_offsets_loaded,
924 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000925 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
926 pf->stat_offsets_loaded,
927 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000928
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000929 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800930 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
931 pf->stat_offsets_loaded,
932 &osd->priority_xoff_rx[i],
933 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000935 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000936 &osd->priority_xon_rx[i],
937 &nsd->priority_xon_rx[i]);
938 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000939 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000940 &osd->priority_xon_tx[i],
941 &nsd->priority_xon_tx[i]);
942 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000943 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000944 &osd->priority_xoff_tx[i],
945 &nsd->priority_xoff_tx[i]);
946 i40e_stat_update32(hw,
947 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000948 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000949 &osd->priority_xon_2_xoff[i],
950 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000951 }
952
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000953 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
954 I40E_GLPRT_PRC64L(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->rx_size_64, &nsd->rx_size_64);
957 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
958 I40E_GLPRT_PRC127L(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->rx_size_127, &nsd->rx_size_127);
961 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
962 I40E_GLPRT_PRC255L(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->rx_size_255, &nsd->rx_size_255);
965 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
966 I40E_GLPRT_PRC511L(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->rx_size_511, &nsd->rx_size_511);
969 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
970 I40E_GLPRT_PRC1023L(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_size_1023, &nsd->rx_size_1023);
973 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
974 I40E_GLPRT_PRC1522L(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->rx_size_1522, &nsd->rx_size_1522);
977 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
978 I40E_GLPRT_PRC9522L(hw->port),
979 pf->stat_offsets_loaded,
980 &osd->rx_size_big, &nsd->rx_size_big);
981
982 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
983 I40E_GLPRT_PTC64L(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->tx_size_64, &nsd->tx_size_64);
986 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
987 I40E_GLPRT_PTC127L(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->tx_size_127, &nsd->tx_size_127);
990 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
991 I40E_GLPRT_PTC255L(hw->port),
992 pf->stat_offsets_loaded,
993 &osd->tx_size_255, &nsd->tx_size_255);
994 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
995 I40E_GLPRT_PTC511L(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->tx_size_511, &nsd->tx_size_511);
998 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
999 I40E_GLPRT_PTC1023L(hw->port),
1000 pf->stat_offsets_loaded,
1001 &osd->tx_size_1023, &nsd->tx_size_1023);
1002 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1003 I40E_GLPRT_PTC1522L(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->tx_size_1522, &nsd->tx_size_1522);
1006 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1007 I40E_GLPRT_PTC9522L(hw->port),
1008 pf->stat_offsets_loaded,
1009 &osd->tx_size_big, &nsd->tx_size_big);
1010
1011 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->rx_undersize, &nsd->rx_undersize);
1014 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->rx_fragments, &nsd->rx_fragments);
1017 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1018 pf->stat_offsets_loaded,
1019 &osd->rx_oversize, &nsd->rx_oversize);
1020 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1021 pf->stat_offsets_loaded,
1022 &osd->rx_jabber, &nsd->rx_jabber);
1023
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001024 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001025 i40e_stat_update32(hw,
1026 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001027 pf->stat_offsets_loaded,
1028 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001029 i40e_stat_update32(hw,
1030 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001031 pf->stat_offsets_loaded,
1032 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001033 i40e_stat_update32(hw,
1034 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1035 pf->stat_offsets_loaded,
1036 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001037
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001038 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1039 nsd->tx_lpi_status =
1040 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1041 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1042 nsd->rx_lpi_status =
1043 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1044 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1045 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1046 pf->stat_offsets_loaded,
1047 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1048 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1049 pf->stat_offsets_loaded,
1050 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1051
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001052 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001053 !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001054 nsd->fd_sb_status = true;
1055 else
1056 nsd->fd_sb_status = false;
1057
1058 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001059 !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001060 nsd->fd_atr_status = true;
1061 else
1062 nsd->fd_atr_status = false;
1063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001064 pf->stat_offsets_loaded = true;
1065}
1066
1067/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001068 * i40e_update_stats - Update the various statistics counters.
1069 * @vsi: the VSI to be updated
1070 *
1071 * Update the various stats for this VSI and its related entities.
1072 **/
1073void i40e_update_stats(struct i40e_vsi *vsi)
1074{
1075 struct i40e_pf *pf = vsi->back;
1076
1077 if (vsi == pf->vsi[pf->lan_vsi])
1078 i40e_update_pf_stats(pf);
1079
1080 i40e_update_vsi_stats(vsi);
1081}
1082
1083/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001084 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1085 * @vsi: the VSI to be searched
1086 * @macaddr: the MAC address
1087 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001088 *
1089 * Returns ptr to the filter object or NULL
1090 **/
1091static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001092 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001093{
1094 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001095 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001096
1097 if (!vsi || !macaddr)
1098 return NULL;
1099
Jacob Keller278e7d02016-10-05 09:30:37 -07001100 key = i40e_addr_to_hkey(macaddr);
1101 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001102 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001103 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001104 return f;
1105 }
1106 return NULL;
1107}
1108
1109/**
1110 * i40e_find_mac - Find a mac addr in the macvlan filters list
1111 * @vsi: the VSI to be searched
1112 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001113 *
1114 * Returns the first filter with the provided MAC address or NULL if
1115 * MAC address was not found
1116 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001117struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001118{
1119 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001120 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001121
1122 if (!vsi || !macaddr)
1123 return NULL;
1124
Jacob Keller278e7d02016-10-05 09:30:37 -07001125 key = i40e_addr_to_hkey(macaddr);
1126 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001127 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001128 return f;
1129 }
1130 return NULL;
1131}
1132
1133/**
1134 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1135 * @vsi: the VSI to be searched
1136 *
1137 * Returns true if VSI is in vlan mode or false otherwise
1138 **/
1139bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1140{
Jacob Kellercbebb852016-10-05 09:30:40 -07001141 /* If we have a PVID, always operate in VLAN mode */
1142 if (vsi->info.pvid)
1143 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001144
Jacob Kellercbebb852016-10-05 09:30:40 -07001145 /* We need to operate in VLAN mode whenever we have any filters with
1146 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1147 * time, incurring search cost repeatedly. However, we can notice two
1148 * things:
1149 *
1150 * 1) the only place where we can gain a VLAN filter is in
1151 * i40e_add_filter.
1152 *
1153 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001154 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001155 *
1156 * Thus, we can simply use a boolean value, has_vlan_filters which we
1157 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1158 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001159 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001160 * filters here and can perform the check at the same time. This
1161 * results in avoiding embedding a loop for VLAN mode inside another
1162 * loop over all the filters, and should maintain correctness as noted
1163 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001164 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001165 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001166}
1167
1168/**
Jacob Keller489a3262016-11-11 12:39:31 -08001169 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1170 * @vsi: the VSI to configure
1171 * @tmp_add_list: list of filters ready to be added
1172 * @tmp_del_list: list of filters ready to be deleted
1173 * @vlan_filters: the number of active VLAN filters
1174 *
1175 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1176 * behave as expected. If we have any active VLAN filters remaining or about
1177 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1178 * so that they only match against untagged traffic. If we no longer have any
1179 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1180 * so that they match against both tagged and untagged traffic. In this way,
1181 * we ensure that we correctly receive the desired traffic. This ensures that
1182 * when we have an active VLAN we will receive only untagged traffic and
1183 * traffic matching active VLANs. If we have no active VLANs then we will
1184 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1185 *
1186 * Finally, in a similar fashion, this function also corrects filters when
1187 * there is an active PVID assigned to this VSI.
1188 *
1189 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1190 *
1191 * This function is only expected to be called from within
1192 * i40e_sync_vsi_filters.
1193 *
1194 * NOTE: This function expects to be called while under the
1195 * mac_filter_hash_lock
1196 */
1197static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1198 struct hlist_head *tmp_add_list,
1199 struct hlist_head *tmp_del_list,
1200 int vlan_filters)
1201{
Jacob Keller5cb25902016-12-12 15:44:15 -08001202 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001203 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001204 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001205 struct hlist_node *h;
1206 int bkt, new_vlan;
1207
1208 /* To determine if a particular filter needs to be replaced we
1209 * have the three following conditions:
1210 *
1211 * a) if we have a PVID assigned, then all filters which are
1212 * not marked as VLAN=PVID must be replaced with filters that
1213 * are.
1214 * b) otherwise, if we have any active VLANS, all filters
1215 * which are marked as VLAN=-1 must be replaced with
1216 * filters marked as VLAN=0
1217 * c) finally, if we do not have any active VLANS, all filters
1218 * which are marked as VLAN=0 must be replaced with filters
1219 * marked as VLAN=-1
1220 */
1221
1222 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001223 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001224 if (pvid && new->f->vlan != pvid)
1225 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001226 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1227 new->f->vlan = 0;
1228 else if (!vlan_filters && new->f->vlan == 0)
1229 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001230 }
1231
1232 /* Update the remaining active filters */
1233 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1234 /* Combine the checks for whether a filter needs to be changed
1235 * and then determine the new VLAN inside the if block, in
1236 * order to avoid duplicating code for adding the new filter
1237 * then deleting the old filter.
1238 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001239 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001240 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1241 (!vlan_filters && f->vlan == 0)) {
1242 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001243 if (pvid)
1244 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001245 else if (vlan_filters)
1246 new_vlan = 0;
1247 else
1248 new_vlan = I40E_VLAN_ANY;
1249
1250 /* Create the new filter */
1251 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1252 if (!add_head)
1253 return -ENOMEM;
1254
Jacob Keller671889e2016-12-02 12:33:00 -08001255 /* Create a temporary i40e_new_mac_filter */
1256 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1257 if (!new)
1258 return -ENOMEM;
1259
1260 new->f = add_head;
1261 new->state = add_head->state;
1262
1263 /* Add the new filter to the tmp list */
1264 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001265
1266 /* Put the original filter into the delete list */
1267 f->state = I40E_FILTER_REMOVE;
1268 hash_del(&f->hlist);
1269 hlist_add_head(&f->hlist, tmp_del_list);
1270 }
1271 }
1272
1273 vsi->has_vlan_filter = !!vlan_filters;
1274
1275 return 0;
1276}
1277
1278/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001279 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1280 * @vsi: the PF Main VSI - inappropriate for any other VSI
1281 * @macaddr: the MAC address
1282 *
1283 * Remove whatever filter the firmware set up so the driver can manage
1284 * its own filtering intelligently.
1285 **/
1286static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1287{
1288 struct i40e_aqc_remove_macvlan_element_data element;
1289 struct i40e_pf *pf = vsi->back;
1290
1291 /* Only appropriate for the PF main VSI */
1292 if (vsi->type != I40E_VSI_MAIN)
1293 return;
1294
1295 memset(&element, 0, sizeof(element));
1296 ether_addr_copy(element.mac_addr, macaddr);
1297 element.vlan_tag = 0;
1298 /* Ignore error returns, some firmware does it this way... */
1299 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1300 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1301
1302 memset(&element, 0, sizeof(element));
1303 ether_addr_copy(element.mac_addr, macaddr);
1304 element.vlan_tag = 0;
1305 /* ...and some firmware does it this way. */
1306 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1307 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1308 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1309}
1310
1311/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001312 * i40e_add_filter - Add a mac/vlan filter to the VSI
1313 * @vsi: the VSI to be searched
1314 * @macaddr: the MAC address
1315 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001316 *
1317 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001318 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001319 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001320 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001321 **/
1322struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001323 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001324{
1325 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001326 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001327
1328 if (!vsi || !macaddr)
1329 return NULL;
1330
Jacob Keller1bc87e82016-10-05 09:30:31 -07001331 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001332 if (!f) {
1333 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1334 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001335 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001336
Jacob Kellercbebb852016-10-05 09:30:40 -07001337 /* Update the boolean indicating if we need to function in
1338 * VLAN mode.
1339 */
1340 if (vlan >= 0)
1341 vsi->has_vlan_filter = true;
1342
Greg Rose9a173902014-05-22 06:32:02 +00001343 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001344 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001345 /* If we're in overflow promisc mode, set the state directly
1346 * to failed, so we don't bother to try sending the filter
1347 * to the hardware.
1348 */
1349 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1350 f->state = I40E_FILTER_FAILED;
1351 else
1352 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001353 INIT_HLIST_NODE(&f->hlist);
1354
1355 key = i40e_addr_to_hkey(macaddr);
1356 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001357
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001358 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1359 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1360 }
1361
Jacob Keller1bc87e82016-10-05 09:30:31 -07001362 /* If we're asked to add a filter that has been marked for removal, it
1363 * is safe to simply restore it to active state. __i40e_del_filter
1364 * will have simply deleted any filters which were previously marked
1365 * NEW or FAILED, so if it is currently marked REMOVE it must have
1366 * previously been ACTIVE. Since we haven't yet run the sync filters
1367 * task, just restore this filter to the ACTIVE state so that the
1368 * sync task leaves it in place
1369 */
1370 if (f->state == I40E_FILTER_REMOVE)
1371 f->state = I40E_FILTER_ACTIVE;
1372
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373 return f;
1374}
1375
1376/**
Jacob Keller290d2552016-10-05 09:30:36 -07001377 * __i40e_del_filter - Remove a specific filter from the VSI
1378 * @vsi: VSI to remove from
1379 * @f: the filter to remove from the list
1380 *
1381 * This function should be called instead of i40e_del_filter only if you know
1382 * the exact filter you will remove already, such as via i40e_find_filter or
1383 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001384 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001385 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001386 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001387 * ANOTHER NOTE: This function MUST be called from within the context of
1388 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1389 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001390 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001391void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001392{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001393 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001394 return;
1395
Alan Bradya410c822016-12-12 15:44:07 -08001396 /* If the filter was never added to firmware then we can just delete it
1397 * directly and we don't want to set the status to remove or else an
1398 * admin queue command will unnecessarily fire.
1399 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001400 if ((f->state == I40E_FILTER_FAILED) ||
1401 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001402 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001403 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001404 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001405 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001406 }
Alan Bradya410c822016-12-12 15:44:07 -08001407
1408 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1409 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001410}
1411
1412/**
Jacob Keller290d2552016-10-05 09:30:36 -07001413 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1414 * @vsi: the VSI to be searched
1415 * @macaddr: the MAC address
1416 * @vlan: the VLAN
1417 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001418 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001419 * being held.
1420 * ANOTHER NOTE: This function MUST be called from within the context of
1421 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1422 * instead of list_for_each_entry().
1423 **/
1424void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1425{
1426 struct i40e_mac_filter *f;
1427
1428 if (!vsi || !macaddr)
1429 return;
1430
1431 f = i40e_find_filter(vsi, macaddr, vlan);
1432 __i40e_del_filter(vsi, f);
1433}
1434
1435/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001436 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001437 * @vsi: the VSI to be searched
1438 * @macaddr: the mac address to be filtered
1439 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001440 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1441 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001442 * unique vlan that already exists. If a PVID has been assigned, instead only
1443 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001444 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001445 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001446 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001447struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1448 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001449{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001450 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001451 struct hlist_node *h;
1452 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001453
1454 if (vsi->info.pvid)
1455 return i40e_add_filter(vsi, macaddr,
1456 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001457
Jacob Keller7aaf95362016-11-11 12:39:33 -08001458 if (!i40e_is_vsi_in_vlan(vsi))
1459 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1460
Jacob Keller278e7d02016-10-05 09:30:37 -07001461 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001462 if (f->state == I40E_FILTER_REMOVE)
1463 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001464 add = i40e_add_filter(vsi, macaddr, f->vlan);
1465 if (!add)
1466 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001467 }
1468
Jacob Keller5feb3d72016-10-05 09:30:34 -07001469 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001470}
1471
1472/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001473 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001474 * @vsi: the VSI to be searched
1475 * @macaddr: the mac address to be removed
1476 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001477 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1478 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001479 *
1480 * Returns 0 for success, or error
1481 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001482int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001483{
Jacob Keller278e7d02016-10-05 09:30:37 -07001484 struct i40e_mac_filter *f;
1485 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001486 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001487 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001488
Jacob Keller278e7d02016-10-05 09:30:37 -07001489 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1490 "Missing mac_filter_hash_lock\n");
1491 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001492 if (ether_addr_equal(macaddr, f->macaddr)) {
1493 __i40e_del_filter(vsi, f);
1494 found = true;
1495 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001496 }
Jacob Keller290d2552016-10-05 09:30:36 -07001497
1498 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001499 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001500 else
1501 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001502}
1503
1504/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001505 * i40e_set_mac - NDO callback to set mac address
1506 * @netdev: network interface device structure
1507 * @p: pointer to an address structure
1508 *
1509 * Returns 0 on success, negative on failure
1510 **/
1511static int i40e_set_mac(struct net_device *netdev, void *p)
1512{
1513 struct i40e_netdev_priv *np = netdev_priv(netdev);
1514 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001515 struct i40e_pf *pf = vsi->back;
1516 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001517 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001518
1519 if (!is_valid_ether_addr(addr->sa_data))
1520 return -EADDRNOTAVAIL;
1521
Shannon Nelson30650cc2014-07-29 04:01:50 +00001522 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1523 netdev_info(netdev, "already using mac address %pM\n",
1524 addr->sa_data);
1525 return 0;
1526 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001527
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001528 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1529 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1530 return -EADDRNOTAVAIL;
1531
Shannon Nelson30650cc2014-07-29 04:01:50 +00001532 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1533 netdev_info(netdev, "returning to hw mac address %pM\n",
1534 hw->mac.addr);
1535 else
1536 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1537
Jacob Keller278e7d02016-10-05 09:30:37 -07001538 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001539 i40e_del_mac_filter(vsi, netdev->dev_addr);
1540 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001541 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001542 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001543 if (vsi->type == I40E_VSI_MAIN) {
1544 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001547 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001548 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001549 if (ret)
1550 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1551 i40e_stat_str(hw, ret),
1552 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001553 }
1554
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001555 /* schedule our worker thread which will take care of
1556 * applying the new filter changes
1557 */
1558 i40e_service_event_schedule(vsi->back);
1559 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560}
1561
1562/**
1563 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1564 * @vsi: the VSI being setup
1565 * @ctxt: VSI context structure
1566 * @enabled_tc: Enabled TCs bitmap
1567 * @is_add: True if called before Add VSI
1568 *
1569 * Setup VSI queue mapping for enabled traffic classes.
1570 **/
1571static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1572 struct i40e_vsi_context *ctxt,
1573 u8 enabled_tc,
1574 bool is_add)
1575{
1576 struct i40e_pf *pf = vsi->back;
1577 u16 sections = 0;
1578 u8 netdev_tc = 0;
1579 u16 numtc = 0;
1580 u16 qcount;
1581 u8 offset;
1582 u16 qmap;
1583 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001584 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001585
1586 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1587 offset = 0;
1588
1589 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1590 /* Find numtc from enabled TC bitmap */
1591 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001592 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001593 numtc++;
1594 }
1595 if (!numtc) {
1596 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1597 numtc = 1;
1598 }
1599 } else {
1600 /* At least TC0 is enabled in case of non-DCB case */
1601 numtc = 1;
1602 }
1603
1604 vsi->tc_config.numtc = numtc;
1605 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001606 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001607 qcount = vsi->alloc_queue_pairs;
1608
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001609 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001610 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001611
1612 /* Setup queue offset/count for all TCs for given VSI */
1613 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1614 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001615 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001616 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 int pow, num_qps;
1618
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001619 switch (vsi->type) {
1620 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001621 qcount = min_t(int, pf->alloc_rss_size,
1622 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001623 break;
1624 case I40E_VSI_FDIR:
1625 case I40E_VSI_SRIOV:
1626 case I40E_VSI_VMDQ2:
1627 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001628 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629 WARN_ON(i != 0);
1630 break;
1631 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001632 vsi->tc_config.tc_info[i].qoffset = offset;
1633 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001634
Shannon Nelson1e200e42015-02-27 09:15:24 +00001635 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001636 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001638 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001639 pow++;
1640 num_qps >>= 1;
1641 }
1642
1643 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1644 qmap =
1645 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1646 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1647
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001648 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001649 } else {
1650 /* TC is not enabled so set the offset to
1651 * default queue and allocate one queue
1652 * for the given TC.
1653 */
1654 vsi->tc_config.tc_info[i].qoffset = 0;
1655 vsi->tc_config.tc_info[i].qcount = 1;
1656 vsi->tc_config.tc_info[i].netdev_tc = 0;
1657
1658 qmap = 0;
1659 }
1660 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1661 }
1662
1663 /* Set actual Tx/Rx queue pairs */
1664 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001665 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1666 if (vsi->req_queue_pairs > 0)
1667 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001668 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001669 vsi->num_queue_pairs = pf->num_lan_msix;
1670 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001671
1672 /* Scheduler section valid can only be set for ADD VSI */
1673 if (is_add) {
1674 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1675
1676 ctxt->info.up_enable_bits = enabled_tc;
1677 }
1678 if (vsi->type == I40E_VSI_SRIOV) {
1679 ctxt->info.mapping_flags |=
1680 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1681 for (i = 0; i < vsi->num_queue_pairs; i++)
1682 ctxt->info.queue_mapping[i] =
1683 cpu_to_le16(vsi->base_queue + i);
1684 } else {
1685 ctxt->info.mapping_flags |=
1686 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1687 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1688 }
1689 ctxt->info.valid_sections |= cpu_to_le16(sections);
1690}
1691
1692/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001693 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1694 * @netdev: the netdevice
1695 * @addr: address to add
1696 *
1697 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1698 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1699 */
1700static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1701{
1702 struct i40e_netdev_priv *np = netdev_priv(netdev);
1703 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001704
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001705 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001706 return 0;
1707 else
1708 return -ENOMEM;
1709}
1710
1711/**
1712 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1713 * @netdev: the netdevice
1714 * @addr: address to add
1715 *
1716 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1717 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1718 */
1719static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1720{
1721 struct i40e_netdev_priv *np = netdev_priv(netdev);
1722 struct i40e_vsi *vsi = np->vsi;
1723
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001724 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001725
1726 return 0;
1727}
1728
1729/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001730 * i40e_set_rx_mode - NDO callback to set the netdev filters
1731 * @netdev: network interface device structure
1732 **/
1733static void i40e_set_rx_mode(struct net_device *netdev)
1734{
1735 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001736 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001737
Jacob Keller278e7d02016-10-05 09:30:37 -07001738 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001739
Jacob Keller6622f5c2016-10-05 09:30:32 -07001740 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1741 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001742
Jacob Keller278e7d02016-10-05 09:30:37 -07001743 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001744
1745 /* check for other flag changes */
1746 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1747 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1748 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1749 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001750
1751 /* schedule our worker thread which will take care of
1752 * applying the new filter changes
1753 */
1754 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001755}
1756
1757/**
Jacob Keller671889e2016-12-02 12:33:00 -08001758 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001759 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001760 * @from: Pointer to list which contains MAC filter entries - changes to
1761 * those entries needs to be undone.
1762 *
Jacob Keller671889e2016-12-02 12:33:00 -08001763 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001764 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001765static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1766 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001767{
Jacob Keller278e7d02016-10-05 09:30:37 -07001768 struct i40e_mac_filter *f;
1769 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001770
Jacob Keller278e7d02016-10-05 09:30:37 -07001771 hlist_for_each_entry_safe(f, h, from, hlist) {
1772 u64 key = i40e_addr_to_hkey(f->macaddr);
1773
Kiran Patil21659032015-09-30 14:09:03 -04001774 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001775 hlist_del(&f->hlist);
1776 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001777 }
1778}
1779
1780/**
Jacob Keller671889e2016-12-02 12:33:00 -08001781 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1782 * @vsi: Pointer to vsi struct
1783 * @from: Pointer to list which contains MAC filter entries - changes to
1784 * those entries needs to be undone.
1785 *
1786 * MAC filter entries from this list were slated for addition.
1787 **/
1788static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1789 struct hlist_head *from)
1790{
1791 struct i40e_new_mac_filter *new;
1792 struct hlist_node *h;
1793
1794 hlist_for_each_entry_safe(new, h, from, hlist) {
1795 /* We can simply free the wrapper structure */
1796 hlist_del(&new->hlist);
1797 kfree(new);
1798 }
1799}
1800
1801/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001802 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001803 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001804 *
1805 * Returns the next non-broadcast filter in the list. Required so that we
1806 * ignore broadcast filters within the list, since these are not handled via
1807 * the normal firmware update path.
1808 */
Jacob Keller671889e2016-12-02 12:33:00 -08001809static
1810struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001811{
Jacob Keller584a8872017-03-07 15:17:52 -08001812 hlist_for_each_entry_continue(next, hlist) {
1813 if (!is_broadcast_ether_addr(next->f->macaddr))
1814 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001815 }
1816
Jacob Keller584a8872017-03-07 15:17:52 -08001817 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001818}
1819
1820/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001821 * i40e_update_filter_state - Update filter state based on return data
1822 * from firmware
1823 * @count: Number of filters added
1824 * @add_list: return data from fw
1825 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001826 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001827 * MAC filter entries from list were slated to be added to device. Returns
1828 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001829 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001830static int
1831i40e_update_filter_state(int count,
1832 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001833 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001834{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001835 int retval = 0;
1836 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001837
Jacob Kellerac9e2392016-11-11 12:39:27 -08001838 for (i = 0; i < count; i++) {
1839 /* Always check status of each filter. We don't need to check
1840 * the firmware return status because we pre-set the filter
1841 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1842 * request to the adminq. Thus, if it no longer matches then
1843 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001844 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001845 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001846 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001847 } else {
1848 add_head->state = I40E_FILTER_ACTIVE;
1849 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001850 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001851
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001852 add_head = i40e_next_filter(add_head);
1853 if (!add_head)
1854 break;
Kiran Patil21659032015-09-30 14:09:03 -04001855 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001856
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001857 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001858}
1859
1860/**
Jacob Keller00936312016-10-05 09:30:41 -07001861 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1862 * @vsi: ptr to the VSI
1863 * @vsi_name: name to display in messages
1864 * @list: the list of filters to send to firmware
1865 * @num_del: the number of filters to delete
1866 * @retval: Set to -EIO on failure to delete
1867 *
1868 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1869 * *retval instead of a return value so that success does not force ret_val to
1870 * be set to 0. This ensures that a sequence of calls to this function
1871 * preserve the previous value of *retval on successful delete.
1872 */
1873static
1874void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1875 struct i40e_aqc_remove_macvlan_element_data *list,
1876 int num_del, int *retval)
1877{
1878 struct i40e_hw *hw = &vsi->back->hw;
1879 i40e_status aq_ret;
1880 int aq_err;
1881
1882 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1883 aq_err = hw->aq.asq_last_status;
1884
1885 /* Explicitly ignore and do not report when firmware returns ENOENT */
1886 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1887 *retval = -EIO;
1888 dev_info(&vsi->back->pdev->dev,
1889 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1890 vsi_name, i40e_stat_str(hw, aq_ret),
1891 i40e_aq_str(hw, aq_err));
1892 }
1893}
1894
1895/**
1896 * i40e_aqc_add_filters - Request firmware to add a set of filters
1897 * @vsi: ptr to the VSI
1898 * @vsi_name: name to display in messages
1899 * @list: the list of filters to send to firmware
1900 * @add_head: Position in the add hlist
1901 * @num_add: the number of filters to add
1902 * @promisc_change: set to true on exit if promiscuous mode was forced on
1903 *
1904 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1905 * promisc_changed to true if the firmware has run out of space for more
1906 * filters.
1907 */
1908static
1909void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1910 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001911 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001912 int num_add, bool *promisc_changed)
1913{
1914 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001915 int aq_err, fcnt;
1916
Jacob Kellerac9e2392016-11-11 12:39:27 -08001917 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001918 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001919 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001920
1921 if (fcnt != num_add) {
1922 *promisc_changed = true;
1923 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001924 dev_warn(&vsi->back->pdev->dev,
1925 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1926 i40e_aq_str(hw, aq_err),
1927 vsi_name);
1928 }
1929}
1930
1931/**
Jacob Keller435c0842016-11-08 13:05:10 -08001932 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1933 * @vsi: pointer to the VSI
1934 * @f: filter data
1935 *
1936 * This function sets or clears the promiscuous broadcast flags for VLAN
1937 * filters in order to properly receive broadcast frames. Assumes that only
1938 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001939 *
1940 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001941 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001942static i40e_status
1943i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1944 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001945{
1946 bool enable = f->state == I40E_FILTER_NEW;
1947 struct i40e_hw *hw = &vsi->back->hw;
1948 i40e_status aq_ret;
1949
1950 if (f->vlan == I40E_VLAN_ANY) {
1951 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1952 vsi->seid,
1953 enable,
1954 NULL);
1955 } else {
1956 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1957 vsi->seid,
1958 enable,
1959 f->vlan,
1960 NULL);
1961 }
1962
Jacob Keller671889e2016-12-02 12:33:00 -08001963 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001964 dev_warn(&vsi->back->pdev->dev,
1965 "Error %s setting broadcast promiscuous mode on %s\n",
1966 i40e_aq_str(hw, hw->aq.asq_last_status),
1967 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001968
1969 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001970}
1971
1972/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001973 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1974 * @vsi: ptr to the VSI
1975 *
1976 * Push any outstanding VSI filter changes through the AdminQ.
1977 *
1978 * Returns 0 or error value
1979 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001980int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001981{
Jacob Keller278e7d02016-10-05 09:30:37 -07001982 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08001983 struct i40e_mac_filter *f;
1984 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001985 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08001986 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001987 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001988 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001989 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001990 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001991 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001992 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001993 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 struct i40e_pf *pf;
1995 int num_add = 0;
1996 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001997 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001998 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001999 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002000 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002001
2002 /* empty array typed pointers, kcalloc later */
2003 struct i40e_aqc_add_macvlan_element_data *add_list;
2004 struct i40e_aqc_remove_macvlan_element_data *del_list;
2005
2006 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2007 usleep_range(1000, 2000);
2008 pf = vsi->back;
2009
2010 if (vsi->netdev) {
2011 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2012 vsi->current_netdev_flags = vsi->netdev->flags;
2013 }
2014
Jacob Keller278e7d02016-10-05 09:30:37 -07002015 INIT_HLIST_HEAD(&tmp_add_list);
2016 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002017
Shannon Nelson2d1de822016-05-16 10:26:44 -07002018 if (vsi->type == I40E_VSI_SRIOV)
2019 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2020 else if (vsi->type != I40E_VSI_MAIN)
2021 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002023 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2024 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2025
Jacob Keller278e7d02016-10-05 09:30:37 -07002026 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002027 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002028 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002029 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002030 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002031 hash_del(&f->hlist);
2032 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002033
2034 /* Avoid counting removed filters */
2035 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002036 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002037 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002038 /* Create a temporary i40e_new_mac_filter */
2039 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2040 if (!new)
2041 goto err_no_memory_locked;
2042
2043 /* Store pointer to the real filter */
2044 new->f = f;
2045 new->state = f->state;
2046
2047 /* Add it to the hash list */
2048 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002049 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002050
Jacob Keller489a3262016-11-11 12:39:31 -08002051 /* Count the number of active (current and new) VLAN
2052 * filters we have now. Does not count filters which
2053 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002054 */
2055 if (f->vlan > 0)
2056 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002057 }
2058
Jacob Keller489a3262016-11-11 12:39:31 -08002059 retval = i40e_correct_mac_vlan_filters(vsi,
2060 &tmp_add_list,
2061 &tmp_del_list,
2062 vlan_filters);
2063 if (retval)
2064 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002065
Jacob Keller278e7d02016-10-05 09:30:37 -07002066 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002067 }
2068
2069 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002070 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002071 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002072 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002073 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002074 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002075 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002076 if (!del_list)
2077 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002078
Jacob Keller278e7d02016-10-05 09:30:37 -07002079 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002080 cmd_flags = 0;
2081
Jacob Keller435c0842016-11-08 13:05:10 -08002082 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002083 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002084 */
2085 if (is_broadcast_ether_addr(f->macaddr)) {
2086 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2087
2088 hlist_del(&f->hlist);
2089 kfree(f);
2090 continue;
2091 }
2092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002093 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002094 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002095 if (f->vlan == I40E_VLAN_ANY) {
2096 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002097 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002098 } else {
2099 del_list[num_del].vlan_tag =
2100 cpu_to_le16((u16)(f->vlan));
2101 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002103 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2104 del_list[num_del].flags = cmd_flags;
2105 num_del++;
2106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002107 /* flush a full buffer */
2108 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002109 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2110 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002111 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002112 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002113 }
Kiran Patil21659032015-09-30 14:09:03 -04002114 /* Release memory for MAC filter entries which were
2115 * synced up with HW.
2116 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002117 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002118 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119 }
Kiran Patil21659032015-09-30 14:09:03 -04002120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002121 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002122 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2123 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002124 }
2125
2126 kfree(del_list);
2127 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002128 }
2129
Jacob Keller278e7d02016-10-05 09:30:37 -07002130 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002131 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002132 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002133 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002134 list_size = filter_list_len *
2135 sizeof(struct i40e_aqc_add_macvlan_element_data);
2136 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002137 if (!add_list)
2138 goto err_no_memory;
2139
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002140 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002141 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002142 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2143 &vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002144 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002145 continue;
2146 }
Jacob Keller435c0842016-11-08 13:05:10 -08002147
2148 /* handle broadcast filters by updating the broadcast
2149 * promiscuous flag instead of adding a MAC filter.
2150 */
Jacob Keller671889e2016-12-02 12:33:00 -08002151 if (is_broadcast_ether_addr(new->f->macaddr)) {
2152 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2153 new->f))
2154 new->state = I40E_FILTER_FAILED;
2155 else
2156 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002157 continue;
2158 }
2159
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002160 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002161 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002162 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002163 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002164 ether_addr_copy(add_list[num_add].mac_addr,
2165 new->f->macaddr);
2166 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002167 add_list[num_add].vlan_tag = 0;
2168 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2169 } else {
2170 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002171 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002172 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002173 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002174 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002175 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002176 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002177 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2178 num_add++;
2179
2180 /* flush a full buffer */
2181 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002182 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2183 add_head, num_add,
2184 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002185 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002186 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187 }
2188 }
2189 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002190 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2191 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002192 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002193 /* Now move all of the filters from the temp add list back to
2194 * the VSI's list.
2195 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002196 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002197 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2198 /* Only update the state if we're still NEW */
2199 if (new->f->state == I40E_FILTER_NEW)
2200 new->f->state = new->state;
2201 hlist_del(&new->hlist);
2202 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002203 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002204 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002205 kfree(add_list);
2206 add_list = NULL;
2207 }
2208
Jacob Keller38326212016-11-11 12:39:26 -08002209 /* Determine the number of active and failed filters. */
2210 spin_lock_bh(&vsi->mac_filter_hash_lock);
2211 vsi->active_filters = 0;
2212 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2213 if (f->state == I40E_FILTER_ACTIVE)
2214 vsi->active_filters++;
2215 else if (f->state == I40E_FILTER_FAILED)
2216 failed_filters++;
2217 }
2218 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2219
2220 /* If promiscuous mode has changed, we need to calculate a new
2221 * threshold for when we are safe to exit
2222 */
2223 if (promisc_changed)
2224 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2225
2226 /* Check if we are able to exit overflow promiscuous mode. We can
2227 * safely exit if we didn't just enter, we no longer have any failed
2228 * filters, and we have reduced filters below the threshold value.
2229 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002230 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002231 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002232 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002233 dev_info(&pf->pdev->dev,
2234 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2235 vsi_name);
2236 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2237 promisc_changed = true;
2238 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002239 }
2240
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002241 /* if the VF is not trusted do not do promisc */
2242 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2243 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2244 goto out;
2245 }
2246
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002247 /* check for changes in promiscuous modes */
2248 if (changed_flags & IFF_ALLMULTI) {
2249 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002250
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002251 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002252 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2253 vsi->seid,
2254 cur_multipromisc,
2255 NULL);
2256 if (aq_ret) {
2257 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002258 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002259 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002260 "set multi promisc failed on %s, err %s aq_err %s\n",
2261 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002262 i40e_stat_str(hw, aq_ret),
2263 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002264 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002265 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002266 if ((changed_flags & IFF_PROMISC) ||
2267 (promisc_changed &&
2268 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002269 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002271 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2272 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2273 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002274 if ((vsi->type == I40E_VSI_MAIN) &&
2275 (pf->lan_veb != I40E_NO_VEB) &&
2276 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002277 /* set defport ON for Main VSI instead of true promisc
2278 * this way we will get all unicast/multicast and VLAN
2279 * promisc behavior but will not get VF or VMDq traffic
2280 * replicated on the Main VSI.
2281 */
2282 if (pf->cur_promisc != cur_promisc) {
2283 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002284 if (cur_promisc)
2285 aq_ret =
2286 i40e_aq_set_default_vsi(hw,
2287 vsi->seid,
2288 NULL);
2289 else
2290 aq_ret =
2291 i40e_aq_clear_default_vsi(hw,
2292 vsi->seid,
2293 NULL);
2294 if (aq_ret) {
2295 retval = i40e_aq_rc_to_posix(aq_ret,
2296 hw->aq.asq_last_status);
2297 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002298 "Set default VSI failed on %s, err %s, aq_err %s\n",
2299 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002300 i40e_stat_str(hw, aq_ret),
2301 i40e_aq_str(hw,
2302 hw->aq.asq_last_status));
2303 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002304 }
2305 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002306 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002307 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002308 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002309 cur_promisc, NULL,
2310 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002311 if (aq_ret) {
2312 retval =
2313 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002314 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002315 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002316 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2317 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002318 i40e_stat_str(hw, aq_ret),
2319 i40e_aq_str(hw,
2320 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002321 }
2322 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002323 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002324 vsi->seid,
2325 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002326 if (aq_ret) {
2327 retval =
2328 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002329 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002330 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002331 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2332 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002333 i40e_stat_str(hw, aq_ret),
2334 i40e_aq_str(hw,
2335 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002336 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002337 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002338 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2339 vsi->seid,
2340 cur_promisc, NULL);
2341 if (aq_ret) {
2342 retval = i40e_aq_rc_to_posix(aq_ret,
2343 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002344 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002345 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 i40e_stat_str(hw, aq_ret),
2347 i40e_aq_str(hw,
2348 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002349 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002350 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002351out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002352 /* if something went wrong then set the changed flag so we try again */
2353 if (retval)
2354 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2355
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002356 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002357 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002358
2359err_no_memory:
2360 /* Restore elements on the temporary add and delete lists */
2361 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002362err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002363 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2364 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002365 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2366
2367 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2368 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2369 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002370}
2371
2372/**
2373 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2374 * @pf: board private structure
2375 **/
2376static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2377{
2378 int v;
2379
2380 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2381 return;
2382 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2383
Mitch Williams505682c2014-05-20 08:01:37 +00002384 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002385 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002386 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2387 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2388
2389 if (ret) {
2390 /* come back and try again later */
2391 pf->flags |= I40E_FLAG_FILTER_SYNC;
2392 break;
2393 }
2394 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002395 }
2396}
2397
2398/**
2399 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2400 * @netdev: network interface device structure
2401 * @new_mtu: new value for maximum frame size
2402 *
2403 * Returns 0 on success, negative on failure
2404 **/
2405static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2406{
2407 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002408 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002409 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002411 netdev_info(netdev, "changing MTU from %d to %d\n",
2412 netdev->mtu, new_mtu);
2413 netdev->mtu = new_mtu;
2414 if (netif_running(netdev))
2415 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002416 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2417 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002418 return 0;
2419}
2420
2421/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002422 * i40e_ioctl - Access the hwtstamp interface
2423 * @netdev: network interface device structure
2424 * @ifr: interface request data
2425 * @cmd: ioctl command
2426 **/
2427int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2428{
2429 struct i40e_netdev_priv *np = netdev_priv(netdev);
2430 struct i40e_pf *pf = np->vsi->back;
2431
2432 switch (cmd) {
2433 case SIOCGHWTSTAMP:
2434 return i40e_ptp_get_ts_config(pf, ifr);
2435 case SIOCSHWTSTAMP:
2436 return i40e_ptp_set_ts_config(pf, ifr);
2437 default:
2438 return -EOPNOTSUPP;
2439 }
2440}
2441
2442/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002443 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2444 * @vsi: the vsi being adjusted
2445 **/
2446void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2447{
2448 struct i40e_vsi_context ctxt;
2449 i40e_status ret;
2450
2451 if ((vsi->info.valid_sections &
2452 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2453 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2454 return; /* already enabled */
2455
2456 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2457 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2458 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2459
2460 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002461 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002462 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2463 if (ret) {
2464 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002465 "update vlan stripping failed, err %s aq_err %s\n",
2466 i40e_stat_str(&vsi->back->hw, ret),
2467 i40e_aq_str(&vsi->back->hw,
2468 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002469 }
2470}
2471
2472/**
2473 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2474 * @vsi: the vsi being adjusted
2475 **/
2476void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2477{
2478 struct i40e_vsi_context ctxt;
2479 i40e_status ret;
2480
2481 if ((vsi->info.valid_sections &
2482 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2483 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2484 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2485 return; /* already disabled */
2486
2487 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2488 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2489 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2490
2491 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002492 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002493 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2494 if (ret) {
2495 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002496 "update vlan stripping failed, err %s aq_err %s\n",
2497 i40e_stat_str(&vsi->back->hw, ret),
2498 i40e_aq_str(&vsi->back->hw,
2499 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002500 }
2501}
2502
2503/**
2504 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2505 * @netdev: network interface to be adjusted
2506 * @features: netdev features to test if VLAN offload is enabled or not
2507 **/
2508static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2509{
2510 struct i40e_netdev_priv *np = netdev_priv(netdev);
2511 struct i40e_vsi *vsi = np->vsi;
2512
2513 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2514 i40e_vlan_stripping_enable(vsi);
2515 else
2516 i40e_vlan_stripping_disable(vsi);
2517}
2518
2519/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002520 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002521 * @vsi: the vsi being configured
2522 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002523 *
2524 * This is a helper function for adding a new MAC/VLAN filter with the
2525 * specified VLAN for each existing MAC address already in the hash table.
2526 * This function does *not* perform any accounting to update filters based on
2527 * VLAN mode.
2528 *
2529 * NOTE: this function expects to be called while under the
2530 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002531 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002532int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002533{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002534 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002535 struct hlist_node *h;
2536 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002537
Jacob Keller278e7d02016-10-05 09:30:37 -07002538 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002539 if (f->state == I40E_FILTER_REMOVE)
2540 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002541 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002542 if (!add_f) {
2543 dev_info(&vsi->back->pdev->dev,
2544 "Could not add vlan filter %d for %pM\n",
2545 vid, f->macaddr);
2546 return -ENOMEM;
2547 }
2548 }
2549
Jacob Keller490a4ad2016-11-11 12:39:29 -08002550 return 0;
2551}
2552
2553/**
2554 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2555 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002556 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002557 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002558int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002559{
Jacob Keller489a3262016-11-11 12:39:31 -08002560 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002561
Jacob Kellerf94484b2016-12-07 14:05:34 -08002562 if (!vid || vsi->info.pvid)
2563 return -EINVAL;
2564
Jacob Keller490a4ad2016-11-11 12:39:29 -08002565 /* Locked once because all functions invoked below iterates list*/
2566 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002567 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002568 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002569 if (err)
2570 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002571
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002572 /* schedule our worker thread which will take care of
2573 * applying the new filter changes
2574 */
2575 i40e_service_event_schedule(vsi->back);
2576 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002577}
2578
2579/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002580 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002581 * @vsi: the vsi being configured
2582 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002583 *
2584 * This function should be used to remove all VLAN filters which match the
2585 * given VID. It does not schedule the service event and does not take the
2586 * mac_filter_hash_lock so it may be combined with other operations under
2587 * a single invocation of the mac_filter_hash_lock.
2588 *
2589 * NOTE: this function expects to be called while under the
2590 * mac_filter_hash_lock
2591 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002592void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593{
Alan Brady84f5ca62016-10-05 09:30:39 -07002594 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002595 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002596 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002597
Jacob Keller278e7d02016-10-05 09:30:37 -07002598 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002599 if (f->vlan == vid)
2600 __i40e_del_filter(vsi, f);
2601 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002602}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002603
Jacob Keller490a4ad2016-11-11 12:39:29 -08002604/**
2605 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2606 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002607 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002608 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002609void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002610{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002611 if (!vid || vsi->info.pvid)
2612 return;
2613
Jacob Keller490a4ad2016-11-11 12:39:29 -08002614 spin_lock_bh(&vsi->mac_filter_hash_lock);
2615 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002616 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002617
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002618 /* schedule our worker thread which will take care of
2619 * applying the new filter changes
2620 */
2621 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002622}
2623
2624/**
2625 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2626 * @netdev: network interface to be adjusted
2627 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002628 *
2629 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002630 **/
2631static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2632 __always_unused __be16 proto, u16 vid)
2633{
2634 struct i40e_netdev_priv *np = netdev_priv(netdev);
2635 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002636 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002637
Jacob Keller6a1127852016-10-25 16:08:49 -07002638 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002639 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002640
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002641 /* If the network stack called us with vid = 0 then
2642 * it is asking to receive priority tagged packets with
2643 * vlan id 0. Our HW receives them by default when configured
2644 * to receive untagged packets so there is no need to add an
2645 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002646 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002647 if (vid)
2648 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649
Jacob Keller6a1127852016-10-25 16:08:49 -07002650 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002651 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002652
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002653 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002654}
2655
2656/**
2657 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2658 * @netdev: network interface to be adjusted
2659 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002660 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002661 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002662 **/
2663static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2664 __always_unused __be16 proto, u16 vid)
2665{
2666 struct i40e_netdev_priv *np = netdev_priv(netdev);
2667 struct i40e_vsi *vsi = np->vsi;
2668
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002669 /* return code is ignored as there is nothing a user
2670 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002671 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002672 */
2673 i40e_vsi_kill_vlan(vsi, vid);
2674
2675 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002676
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002677 return 0;
2678}
2679
2680/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002681 * i40e_macaddr_init - explicitly write the mac address filters
2682 *
2683 * @vsi: pointer to the vsi
2684 * @macaddr: the MAC address
2685 *
2686 * This is needed when the macaddr has been obtained by other
2687 * means than the default, e.g., from Open Firmware or IDPROM.
2688 * Returns 0 on success, negative on failure
2689 **/
2690static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2691{
2692 int ret;
2693 struct i40e_aqc_add_macvlan_element_data element;
2694
2695 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2696 I40E_AQC_WRITE_TYPE_LAA_WOL,
2697 macaddr, NULL);
2698 if (ret) {
2699 dev_info(&vsi->back->pdev->dev,
2700 "Addr change for VSI failed: %d\n", ret);
2701 return -EADDRNOTAVAIL;
2702 }
2703
2704 memset(&element, 0, sizeof(element));
2705 ether_addr_copy(element.mac_addr, macaddr);
2706 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2707 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2708 if (ret) {
2709 dev_info(&vsi->back->pdev->dev,
2710 "add filter failed err %s aq_err %s\n",
2711 i40e_stat_str(&vsi->back->hw, ret),
2712 i40e_aq_str(&vsi->back->hw,
2713 vsi->back->hw.aq.asq_last_status));
2714 }
2715 return ret;
2716}
2717
2718/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2720 * @vsi: the vsi being brought back up
2721 **/
2722static void i40e_restore_vlan(struct i40e_vsi *vsi)
2723{
2724 u16 vid;
2725
2726 if (!vsi->netdev)
2727 return;
2728
2729 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2730
2731 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2732 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2733 vid);
2734}
2735
2736/**
2737 * i40e_vsi_add_pvid - Add pvid for the VSI
2738 * @vsi: the vsi being adjusted
2739 * @vid: the vlan id to set as a PVID
2740 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002741int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002742{
2743 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002744 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002745
2746 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2747 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002748 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2749 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002750 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002751
2752 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002753 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002754 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2755 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002757 "add pvid failed, err %s aq_err %s\n",
2758 i40e_stat_str(&vsi->back->hw, ret),
2759 i40e_aq_str(&vsi->back->hw,
2760 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002761 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002762 }
2763
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002764 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765}
2766
2767/**
2768 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2769 * @vsi: the vsi being adjusted
2770 *
2771 * Just use the vlan_rx_register() service to put it back to normal
2772 **/
2773void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2774{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002775 i40e_vlan_stripping_disable(vsi);
2776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002777 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002778}
2779
2780/**
2781 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2782 * @vsi: ptr to the VSI
2783 *
2784 * If this function returns with an error, then it's possible one or
2785 * more of the rings is populated (while the rest are not). It is the
2786 * callers duty to clean those orphaned rings.
2787 *
2788 * Return 0 on success, negative on failure
2789 **/
2790static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2791{
2792 int i, err = 0;
2793
2794 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002795 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002796
2797 return err;
2798}
2799
2800/**
2801 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2802 * @vsi: ptr to the VSI
2803 *
2804 * Free VSI's transmit software resources
2805 **/
2806static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2807{
2808 int i;
2809
Greg Rose8e9dca52013-12-18 13:45:53 +00002810 if (!vsi->tx_rings)
2811 return;
2812
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002813 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002814 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002815 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002816}
2817
2818/**
2819 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2820 * @vsi: ptr to the VSI
2821 *
2822 * If this function returns with an error, then it's possible one or
2823 * more of the rings is populated (while the rest are not). It is the
2824 * callers duty to clean those orphaned rings.
2825 *
2826 * Return 0 on success, negative on failure
2827 **/
2828static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2829{
2830 int i, err = 0;
2831
2832 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002833 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002834 return err;
2835}
2836
2837/**
2838 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2839 * @vsi: ptr to the VSI
2840 *
2841 * Free all receive software resources
2842 **/
2843static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2844{
2845 int i;
2846
Greg Rose8e9dca52013-12-18 13:45:53 +00002847 if (!vsi->rx_rings)
2848 return;
2849
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002850 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002851 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002852 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002853}
2854
2855/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002856 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2857 * @ring: The Tx ring to configure
2858 *
2859 * This enables/disables XPS for a given Tx descriptor ring
2860 * based on the TCs enabled for the VSI that ring belongs to.
2861 **/
2862static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2863{
2864 struct i40e_vsi *vsi = ring->vsi;
2865 cpumask_var_t mask;
2866
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002867 if (!ring->q_vector || !ring->netdev)
2868 return;
2869
2870 /* Single TC mode enable XPS */
2871 if (vsi->tc_config.numtc <= 1) {
2872 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002873 netif_set_xps_queue(ring->netdev,
2874 &ring->q_vector->affinity_mask,
2875 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002876 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2877 /* Disable XPS to allow selection based on TC */
2878 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2879 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2880 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002881 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002882
2883 /* schedule our worker thread which will take care of
2884 * applying the new filter changes
2885 */
2886 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002887}
2888
2889/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002890 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2891 * @ring: The Tx ring to configure
2892 *
2893 * Configure the Tx descriptor ring in the HMC context.
2894 **/
2895static int i40e_configure_tx_ring(struct i40e_ring *ring)
2896{
2897 struct i40e_vsi *vsi = ring->vsi;
2898 u16 pf_q = vsi->base_queue + ring->queue_index;
2899 struct i40e_hw *hw = &vsi->back->hw;
2900 struct i40e_hmc_obj_txq tx_ctx;
2901 i40e_status err = 0;
2902 u32 qtx_ctl = 0;
2903
2904 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002905 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002906 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2907 ring->atr_count = 0;
2908 } else {
2909 ring->atr_sample_rate = 0;
2910 }
2911
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002912 /* configure XPS */
2913 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002914
2915 /* clear the context structure first */
2916 memset(&tx_ctx, 0, sizeof(tx_ctx));
2917
2918 tx_ctx.new_context = 1;
2919 tx_ctx.base = (ring->dma / 128);
2920 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002921 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2922 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002923 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002924 /* FDIR VSI tx ring can still use RS bit and writebacks */
2925 if (vsi->type != I40E_VSI_FDIR)
2926 tx_ctx.head_wb_ena = 1;
2927 tx_ctx.head_wb_addr = ring->dma +
2928 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002929
2930 /* As part of VSI creation/update, FW allocates certain
2931 * Tx arbitration queue sets for each TC enabled for
2932 * the VSI. The FW returns the handles to these queue
2933 * sets as part of the response buffer to Add VSI,
2934 * Update VSI, etc. AQ commands. It is expected that
2935 * these queue set handles be associated with the Tx
2936 * queues by the driver as part of the TX queue context
2937 * initialization. This has to be done regardless of
2938 * DCB as by default everything is mapped to TC0.
2939 */
2940 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2941 tx_ctx.rdylist_act = 0;
2942
2943 /* clear the context in the HMC */
2944 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2945 if (err) {
2946 dev_info(&vsi->back->pdev->dev,
2947 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2948 ring->queue_index, pf_q, err);
2949 return -ENOMEM;
2950 }
2951
2952 /* set the context in the HMC */
2953 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2954 if (err) {
2955 dev_info(&vsi->back->pdev->dev,
2956 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2957 ring->queue_index, pf_q, err);
2958 return -ENOMEM;
2959 }
2960
2961 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002962 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002963 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002964 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2965 I40E_QTX_CTL_VFVM_INDX_MASK;
2966 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002967 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002968 }
2969
Shannon Nelson13fd9772013-09-28 07:14:19 +00002970 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2971 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002972 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2973 i40e_flush(hw);
2974
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002975 /* cache tail off for easier writes later */
2976 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2977
2978 return 0;
2979}
2980
2981/**
2982 * i40e_configure_rx_ring - Configure a receive ring context
2983 * @ring: The Rx ring to configure
2984 *
2985 * Configure the Rx descriptor ring in the HMC context.
2986 **/
2987static int i40e_configure_rx_ring(struct i40e_ring *ring)
2988{
2989 struct i40e_vsi *vsi = ring->vsi;
2990 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2991 u16 pf_q = vsi->base_queue + ring->queue_index;
2992 struct i40e_hw *hw = &vsi->back->hw;
2993 struct i40e_hmc_obj_rxq rx_ctx;
2994 i40e_status err = 0;
2995
2996 ring->state = 0;
2997
2998 /* clear the context structure first */
2999 memset(&rx_ctx, 0, sizeof(rx_ctx));
3000
3001 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003002
Alexander Duyckdab86af2017-03-14 10:15:27 -07003003 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3004 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003005
3006 rx_ctx.base = (ring->dma / 128);
3007 rx_ctx.qlen = ring->count;
3008
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003009 /* use 32 byte descriptors */
3010 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003012 /* descriptor type is always zero
3013 * rx_ctx.dtype = 0;
3014 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003015 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003017 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003018 if (hw->revision_id == 0)
3019 rx_ctx.lrxqthresh = 0;
3020 else
3021 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022 rx_ctx.crcstrip = 1;
3023 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003024 /* this controls whether VLAN is stripped from inner headers */
3025 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003026 /* set the prefena field to 1 because the manual says to */
3027 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003028
3029 /* clear the context in the HMC */
3030 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3031 if (err) {
3032 dev_info(&vsi->back->pdev->dev,
3033 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3034 ring->queue_index, pf_q, err);
3035 return -ENOMEM;
3036 }
3037
3038 /* set the context in the HMC */
3039 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3040 if (err) {
3041 dev_info(&vsi->back->pdev->dev,
3042 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3043 ring->queue_index, pf_q, err);
3044 return -ENOMEM;
3045 }
3046
Alexander Duyckca9ec082017-04-05 07:51:02 -04003047 /* configure Rx buffer alignment */
3048 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3049 clear_ring_build_skb_enabled(ring);
3050 else
3051 set_ring_build_skb_enabled(ring);
3052
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003053 /* cache tail for quicker writes, and clear the reg before use */
3054 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3055 writel(0, ring->tail);
3056
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003057 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003058
3059 return 0;
3060}
3061
3062/**
3063 * i40e_vsi_configure_tx - Configure the VSI for Tx
3064 * @vsi: VSI structure describing this set of rings and resources
3065 *
3066 * Configure the Tx VSI for operation.
3067 **/
3068static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3069{
3070 int err = 0;
3071 u16 i;
3072
Alexander Duyck9f65e152013-09-28 06:00:58 +00003073 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3074 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003075
3076 return err;
3077}
3078
3079/**
3080 * i40e_vsi_configure_rx - Configure the VSI for Rx
3081 * @vsi: the VSI being configured
3082 *
3083 * Configure the Rx VSI for operation.
3084 **/
3085static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3086{
3087 int err = 0;
3088 u16 i;
3089
Alexander Duyckdab86af2017-03-14 10:15:27 -07003090 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3091 vsi->max_frame = I40E_MAX_RXBUFFER;
3092 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3093#if (PAGE_SIZE < 8192)
Alexander Duyckca9ec082017-04-05 07:51:02 -04003094 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3095 (vsi->netdev->mtu <= ETH_DATA_LEN)) {
Alexander Duyckdab86af2017-03-14 10:15:27 -07003096 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3097 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3098#endif
3099 } else {
3100 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003101 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3102 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003103 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003104
3105 /* set up individual rings */
3106 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003107 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003108
3109 return err;
3110}
3111
3112/**
3113 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3114 * @vsi: ptr to the VSI
3115 **/
3116static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3117{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003118 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 u16 qoffset, qcount;
3120 int i, n;
3121
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003122 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3123 /* Reset the TC information */
3124 for (i = 0; i < vsi->num_queue_pairs; i++) {
3125 rx_ring = vsi->rx_rings[i];
3126 tx_ring = vsi->tx_rings[i];
3127 rx_ring->dcb_tc = 0;
3128 tx_ring->dcb_tc = 0;
3129 }
3130 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003131
3132 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003133 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003134 continue;
3135
3136 qoffset = vsi->tc_config.tc_info[n].qoffset;
3137 qcount = vsi->tc_config.tc_info[n].qcount;
3138 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003139 rx_ring = vsi->rx_rings[i];
3140 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 rx_ring->dcb_tc = n;
3142 tx_ring->dcb_tc = n;
3143 }
3144 }
3145}
3146
3147/**
3148 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3149 * @vsi: ptr to the VSI
3150 **/
3151static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3152{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003153 struct i40e_pf *pf = vsi->back;
3154 int err;
3155
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003156 if (vsi->netdev)
3157 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003158
3159 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3160 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3161 if (err) {
3162 dev_warn(&pf->pdev->dev,
3163 "could not set up macaddr; err %d\n", err);
3164 }
3165 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003166}
3167
3168/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003169 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3170 * @vsi: Pointer to the targeted VSI
3171 *
3172 * This function replays the hlist on the hw where all the SB Flow Director
3173 * filters were saved.
3174 **/
3175static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3176{
3177 struct i40e_fdir_filter *filter;
3178 struct i40e_pf *pf = vsi->back;
3179 struct hlist_node *node;
3180
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003181 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3182 return;
3183
Jacob Keller6d069422017-02-06 14:38:44 -08003184 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003185 pf->fd_tcp4_filter_cnt = 0;
3186 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003187 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003188 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003189
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003190 hlist_for_each_entry_safe(filter, node,
3191 &pf->fdir_filter_list, fdir_node) {
3192 i40e_add_del_fdir(vsi, filter, true);
3193 }
3194}
3195
3196/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003197 * i40e_vsi_configure - Set up the VSI for action
3198 * @vsi: the VSI being configured
3199 **/
3200static int i40e_vsi_configure(struct i40e_vsi *vsi)
3201{
3202 int err;
3203
3204 i40e_set_vsi_rx_mode(vsi);
3205 i40e_restore_vlan(vsi);
3206 i40e_vsi_config_dcb_rings(vsi);
3207 err = i40e_vsi_configure_tx(vsi);
3208 if (!err)
3209 err = i40e_vsi_configure_rx(vsi);
3210
3211 return err;
3212}
3213
3214/**
3215 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3216 * @vsi: the VSI being configured
3217 **/
3218static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3219{
3220 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221 struct i40e_hw *hw = &pf->hw;
3222 u16 vector;
3223 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003224 u32 qp;
3225
3226 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3227 * and PFINT_LNKLSTn registers, e.g.:
3228 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3229 */
3230 qp = vsi->base_queue;
3231 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003232 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003233 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3234
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003235 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003236 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003237 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3238 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3239 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003240 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003241 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3242 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3243 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003244 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003245 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003246
3247 /* Linked list for the queuepairs assigned to this vector */
3248 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3249 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003250 u32 val;
3251
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003252 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3253 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3254 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3255 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3256 (I40E_QUEUE_TYPE_TX
3257 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3258
3259 wr32(hw, I40E_QINT_RQCTL(qp), val);
3260
3261 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3262 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3263 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3264 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3265 (I40E_QUEUE_TYPE_RX
3266 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3267
3268 /* Terminate the linked list */
3269 if (q == (q_vector->num_ringpairs - 1))
3270 val |= (I40E_QUEUE_END_OF_LIST
3271 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3272
3273 wr32(hw, I40E_QINT_TQCTL(qp), val);
3274 qp++;
3275 }
3276 }
3277
3278 i40e_flush(hw);
3279}
3280
3281/**
3282 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3283 * @hw: ptr to the hardware info
3284 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003285static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003286{
Jacob Kellerab437b52014-12-14 01:55:08 +00003287 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003288 u32 val;
3289
3290 /* clear things first */
3291 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3292 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3293
3294 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3295 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3296 I40E_PFINT_ICR0_ENA_GRST_MASK |
3297 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3298 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003299 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3300 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3301 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3302
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003303 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3304 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3305
Jacob Kellerab437b52014-12-14 01:55:08 +00003306 if (pf->flags & I40E_FLAG_PTP)
3307 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003309 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3310
3311 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003312 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3313 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003314
3315 /* OTHER_ITR_IDX = 0 */
3316 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3317}
3318
3319/**
3320 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3321 * @vsi: the VSI being configured
3322 **/
3323static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3324{
Alexander Duyck493fb302013-09-28 07:01:44 +00003325 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003326 struct i40e_pf *pf = vsi->back;
3327 struct i40e_hw *hw = &pf->hw;
3328 u32 val;
3329
3330 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003331 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003332 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3334 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003335 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003336 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3337 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3338
Jacob Kellerab437b52014-12-14 01:55:08 +00003339 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003340
3341 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3342 wr32(hw, I40E_PFINT_LNKLST0, 0);
3343
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003344 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003345 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3346 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3347 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3348
3349 wr32(hw, I40E_QINT_RQCTL(0), val);
3350
3351 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3352 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3353 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3354
3355 wr32(hw, I40E_QINT_TQCTL(0), val);
3356 i40e_flush(hw);
3357}
3358
3359/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003360 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3361 * @pf: board private structure
3362 **/
3363void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3364{
3365 struct i40e_hw *hw = &pf->hw;
3366
3367 wr32(hw, I40E_PFINT_DYN_CTL0,
3368 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3369 i40e_flush(hw);
3370}
3371
3372/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003373 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3374 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003375 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003376 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003377void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003378{
3379 struct i40e_hw *hw = &pf->hw;
3380 u32 val;
3381
3382 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003383 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003384 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3385
3386 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3387 i40e_flush(hw);
3388}
3389
3390/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003391 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3392 * @irq: interrupt number
3393 * @data: pointer to a q_vector
3394 **/
3395static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3396{
3397 struct i40e_q_vector *q_vector = data;
3398
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003399 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003400 return IRQ_HANDLED;
3401
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003402 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003403
3404 return IRQ_HANDLED;
3405}
3406
3407/**
Alan Brady96db7762016-09-14 16:24:38 -07003408 * i40e_irq_affinity_notify - Callback for affinity changes
3409 * @notify: context as to what irq was changed
3410 * @mask: the new affinity mask
3411 *
3412 * This is a callback function used by the irq_set_affinity_notifier function
3413 * so that we may register to receive changes to the irq affinity masks.
3414 **/
3415static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3416 const cpumask_t *mask)
3417{
3418 struct i40e_q_vector *q_vector =
3419 container_of(notify, struct i40e_q_vector, affinity_notify);
3420
3421 q_vector->affinity_mask = *mask;
3422}
3423
3424/**
3425 * i40e_irq_affinity_release - Callback for affinity notifier release
3426 * @ref: internal core kernel usage
3427 *
3428 * This is a callback function used by the irq_set_affinity_notifier function
3429 * to inform the current notification subscriber that they will no longer
3430 * receive notifications.
3431 **/
3432static void i40e_irq_affinity_release(struct kref *ref) {}
3433
3434/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3436 * @vsi: the VSI being configured
3437 * @basename: name for the vector
3438 *
3439 * Allocates MSI-X vectors and requests interrupts from the kernel.
3440 **/
3441static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3442{
3443 int q_vectors = vsi->num_q_vectors;
3444 struct i40e_pf *pf = vsi->back;
3445 int base = vsi->base_vector;
3446 int rx_int_idx = 0;
3447 int tx_int_idx = 0;
3448 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003449 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450
3451 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003452 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453
Alan Brady96db7762016-09-14 16:24:38 -07003454 irq_num = pf->msix_entries[base + vector].vector;
3455
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003456 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003457 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3458 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3459 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003460 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3462 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003463 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003464 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3465 "%s-%s-%d", basename, "tx", tx_int_idx++);
3466 } else {
3467 /* skip this unused q_vector */
3468 continue;
3469 }
Alan Brady96db7762016-09-14 16:24:38 -07003470 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471 vsi->irq_handler,
3472 0,
3473 q_vector->name,
3474 q_vector);
3475 if (err) {
3476 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003477 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003478 goto free_queue_irqs;
3479 }
Alan Brady96db7762016-09-14 16:24:38 -07003480
3481 /* register for affinity change notifications */
3482 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3483 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3484 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003486 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 }
3488
Shannon Nelson63741842014-04-23 04:50:16 +00003489 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003490 return 0;
3491
3492free_queue_irqs:
3493 while (vector) {
3494 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003495 irq_num = pf->msix_entries[base + vector].vector;
3496 irq_set_affinity_notifier(irq_num, NULL);
3497 irq_set_affinity_hint(irq_num, NULL);
3498 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 }
3500 return err;
3501}
3502
3503/**
3504 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3505 * @vsi: the VSI being un-configured
3506 **/
3507static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3508{
3509 struct i40e_pf *pf = vsi->back;
3510 struct i40e_hw *hw = &pf->hw;
3511 int base = vsi->base_vector;
3512 int i;
3513
3514 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003515 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3516 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003517 }
3518
3519 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3520 for (i = vsi->base_vector;
3521 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3522 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3523
3524 i40e_flush(hw);
3525 for (i = 0; i < vsi->num_q_vectors; i++)
3526 synchronize_irq(pf->msix_entries[i + base].vector);
3527 } else {
3528 /* Legacy and MSI mode - this stops all interrupt handling */
3529 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3530 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3531 i40e_flush(hw);
3532 synchronize_irq(pf->pdev->irq);
3533 }
3534}
3535
3536/**
3537 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3538 * @vsi: the VSI being configured
3539 **/
3540static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3541{
3542 struct i40e_pf *pf = vsi->back;
3543 int i;
3544
3545 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003546 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003547 i40e_irq_dynamic_enable(vsi, i);
3548 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003549 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003550 }
3551
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003552 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003553 return 0;
3554}
3555
3556/**
3557 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3558 * @pf: board private structure
3559 **/
3560static void i40e_stop_misc_vector(struct i40e_pf *pf)
3561{
3562 /* Disable ICR 0 */
3563 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3564 i40e_flush(&pf->hw);
3565}
3566
3567/**
3568 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3569 * @irq: interrupt number
3570 * @data: pointer to a q_vector
3571 *
3572 * This is the handler used for all MSI/Legacy interrupts, and deals
3573 * with both queue and non-queue interrupts. This is also used in
3574 * MSIX mode to handle the non-queue interrupts.
3575 **/
3576static irqreturn_t i40e_intr(int irq, void *data)
3577{
3578 struct i40e_pf *pf = (struct i40e_pf *)data;
3579 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003580 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003581 u32 icr0, icr0_remaining;
3582 u32 val, ena_mask;
3583
3584 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003585 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586
Shannon Nelson116a57d2013-09-28 07:13:59 +00003587 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3588 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003589 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003590
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003591 /* if interrupt but no bits showing, must be SWINT */
3592 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3593 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3594 pf->sw_int_count++;
3595
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003596 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3597 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3598 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3599 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003600 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003601 }
3602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003603 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3604 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003605 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3606 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003607
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003608 /* We do not have a way to disarm Queue causes while leaving
3609 * interrupt enabled for all other causes, ideally
3610 * interrupt should be disabled while we are in NAPI but
3611 * this is not a performance path and napi_schedule()
3612 * can deal with rescheduling.
3613 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003614 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003615 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003616 }
3617
3618 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3619 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3620 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003621 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003622 }
3623
3624 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3625 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3626 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3627 }
3628
3629 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3630 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3631 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3632 }
3633
3634 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3635 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3636 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3637 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3638 val = rd32(hw, I40E_GLGEN_RSTAT);
3639 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3640 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003641 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003642 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003643 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003644 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003645 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003646 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003647 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003648 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003649 }
3650
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003651 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3652 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3653 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003654 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3655 rd32(hw, I40E_PFHMC_ERRORINFO),
3656 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003657 }
3658
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003659 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3660 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3661
3662 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003663 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003664 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003665 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003666 }
3667
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003668 /* If a critical error is pending we have no choice but to reset the
3669 * device.
3670 * Report and mask out any remaining unexpected interrupts.
3671 */
3672 icr0_remaining = icr0 & ena_mask;
3673 if (icr0_remaining) {
3674 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3675 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003676 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003678 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003679 dev_info(&pf->pdev->dev, "device will be reset\n");
3680 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3681 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682 }
3683 ena_mask &= ~icr0_remaining;
3684 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003685 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003687enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688 /* re-enable interrupt causes */
3689 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003690 if (!test_bit(__I40E_DOWN, &pf->state)) {
3691 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003692 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003693 }
3694
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003695 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696}
3697
3698/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003699 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3700 * @tx_ring: tx ring to clean
3701 * @budget: how many cleans we're allowed
3702 *
3703 * Returns true if there's any budget left (e.g. the clean is finished)
3704 **/
3705static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3706{
3707 struct i40e_vsi *vsi = tx_ring->vsi;
3708 u16 i = tx_ring->next_to_clean;
3709 struct i40e_tx_buffer *tx_buf;
3710 struct i40e_tx_desc *tx_desc;
3711
3712 tx_buf = &tx_ring->tx_bi[i];
3713 tx_desc = I40E_TX_DESC(tx_ring, i);
3714 i -= tx_ring->count;
3715
3716 do {
3717 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3718
3719 /* if next_to_watch is not set then there is no work pending */
3720 if (!eop_desc)
3721 break;
3722
3723 /* prevent any other reads prior to eop_desc */
3724 read_barrier_depends();
3725
3726 /* if the descriptor isn't done, no work yet to do */
3727 if (!(eop_desc->cmd_type_offset_bsz &
3728 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3729 break;
3730
3731 /* clear next_to_watch to prevent false hangs */
3732 tx_buf->next_to_watch = NULL;
3733
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003734 tx_desc->buffer_addr = 0;
3735 tx_desc->cmd_type_offset_bsz = 0;
3736 /* move past filter desc */
3737 tx_buf++;
3738 tx_desc++;
3739 i++;
3740 if (unlikely(!i)) {
3741 i -= tx_ring->count;
3742 tx_buf = tx_ring->tx_bi;
3743 tx_desc = I40E_TX_DESC(tx_ring, 0);
3744 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003745 /* unmap skb header data */
3746 dma_unmap_single(tx_ring->dev,
3747 dma_unmap_addr(tx_buf, dma),
3748 dma_unmap_len(tx_buf, len),
3749 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003750 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3751 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003752
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003753 tx_buf->raw_buf = NULL;
3754 tx_buf->tx_flags = 0;
3755 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003756 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003757 tx_desc->buffer_addr = 0;
3758 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003759
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003760 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003761 tx_buf++;
3762 tx_desc++;
3763 i++;
3764 if (unlikely(!i)) {
3765 i -= tx_ring->count;
3766 tx_buf = tx_ring->tx_bi;
3767 tx_desc = I40E_TX_DESC(tx_ring, 0);
3768 }
3769
3770 /* update budget accounting */
3771 budget--;
3772 } while (likely(budget));
3773
3774 i += tx_ring->count;
3775 tx_ring->next_to_clean = i;
3776
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003777 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003778 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003779
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003780 return budget > 0;
3781}
3782
3783/**
3784 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3785 * @irq: interrupt number
3786 * @data: pointer to a q_vector
3787 **/
3788static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3789{
3790 struct i40e_q_vector *q_vector = data;
3791 struct i40e_vsi *vsi;
3792
3793 if (!q_vector->tx.ring)
3794 return IRQ_HANDLED;
3795
3796 vsi = q_vector->tx.ring->vsi;
3797 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3798
3799 return IRQ_HANDLED;
3800}
3801
3802/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003803 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003804 * @vsi: the VSI being configured
3805 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003806 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003807 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003808static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003809{
Alexander Duyck493fb302013-09-28 07:01:44 +00003810 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003811 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3812 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003813
3814 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003815 tx_ring->next = q_vector->tx.ring;
3816 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003817 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003818
3819 rx_ring->q_vector = q_vector;
3820 rx_ring->next = q_vector->rx.ring;
3821 q_vector->rx.ring = rx_ring;
3822 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003823}
3824
3825/**
3826 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3827 * @vsi: the VSI being configured
3828 *
3829 * This function maps descriptor rings to the queue-specific vectors
3830 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3831 * one vector per queue pair, but on a constrained vector budget, we
3832 * group the queue pairs as "efficiently" as possible.
3833 **/
3834static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3835{
3836 int qp_remaining = vsi->num_queue_pairs;
3837 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003838 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003839 int v_start = 0;
3840 int qp_idx = 0;
3841
3842 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3843 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003844 * It is also important to go through all the vectors available to be
3845 * sure that if we don't use all the vectors, that the remaining vectors
3846 * are cleared. This is especially important when decreasing the
3847 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003848 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003849 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003850 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3851
3852 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3853
3854 q_vector->num_ringpairs = num_ringpairs;
3855
3856 q_vector->rx.count = 0;
3857 q_vector->tx.count = 0;
3858 q_vector->rx.ring = NULL;
3859 q_vector->tx.ring = NULL;
3860
3861 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003862 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003863 qp_idx++;
3864 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003865 }
3866 }
3867}
3868
3869/**
3870 * i40e_vsi_request_irq - Request IRQ from the OS
3871 * @vsi: the VSI being configured
3872 * @basename: name for the vector
3873 **/
3874static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3875{
3876 struct i40e_pf *pf = vsi->back;
3877 int err;
3878
3879 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3880 err = i40e_vsi_request_irq_msix(vsi, basename);
3881 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3882 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003883 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003884 else
3885 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003886 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887
3888 if (err)
3889 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3890
3891 return err;
3892}
3893
3894#ifdef CONFIG_NET_POLL_CONTROLLER
3895/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003896 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003897 * @netdev: network interface device structure
3898 *
3899 * This is used by netconsole to send skbs without having to re-enable
3900 * interrupts. It's not called while the normal interrupt routine is executing.
3901 **/
3902static void i40e_netpoll(struct net_device *netdev)
3903{
3904 struct i40e_netdev_priv *np = netdev_priv(netdev);
3905 struct i40e_vsi *vsi = np->vsi;
3906 struct i40e_pf *pf = vsi->back;
3907 int i;
3908
3909 /* if interface is down do nothing */
3910 if (test_bit(__I40E_DOWN, &vsi->state))
3911 return;
3912
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3914 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003915 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003916 } else {
3917 i40e_intr(pf->pdev->irq, netdev);
3918 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003919}
3920#endif
3921
Jacob Kellerc768e492017-04-13 04:45:47 -04003922#define I40E_QTX_ENA_WAIT_COUNT 50
3923
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003924/**
Neerav Parikh23527302014-06-03 23:50:15 +00003925 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3926 * @pf: the PF being configured
3927 * @pf_q: the PF queue
3928 * @enable: enable or disable state of the queue
3929 *
3930 * This routine will wait for the given Tx queue of the PF to reach the
3931 * enabled or disabled state.
3932 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3933 * multiple retries; else will return 0 in case of success.
3934 **/
3935static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3936{
3937 int i;
3938 u32 tx_reg;
3939
3940 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3941 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3942 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3943 break;
3944
Neerav Parikhf98a2002014-09-13 07:40:44 +00003945 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003946 }
3947 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3948 return -ETIMEDOUT;
3949
3950 return 0;
3951}
3952
3953/**
Jacob Kellerc768e492017-04-13 04:45:47 -04003954 * i40e_control_tx_q - Start or stop a particular Tx queue
3955 * @pf: the PF structure
3956 * @pf_q: the PF queue to configure
3957 * @enable: start or stop the queue
3958 *
3959 * This function enables or disables a single queue. Note that any delay
3960 * required after the operation is expected to be handled by the caller of
3961 * this function.
3962 **/
3963static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
3964{
3965 struct i40e_hw *hw = &pf->hw;
3966 u32 tx_reg;
3967 int i;
3968
3969 /* warn the TX unit of coming changes */
3970 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3971 if (!enable)
3972 usleep_range(10, 20);
3973
3974 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
3975 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3976 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3977 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3978 break;
3979 usleep_range(1000, 2000);
3980 }
3981
3982 /* Skip if the queue is already in the requested state */
3983 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3984 return;
3985
3986 /* turn on/off the queue */
3987 if (enable) {
3988 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
3989 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
3990 } else {
3991 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
3992 }
3993
3994 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3995}
3996
3997/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003998 * i40e_vsi_control_tx - Start or stop a VSI's rings
3999 * @vsi: the VSI being configured
4000 * @enable: start or stop the rings
4001 **/
4002static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4003{
4004 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004005 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004006
4007 pf_q = vsi->base_queue;
4008 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004009 i40e_control_tx_q(pf, pf_q, enable);
Matt Jared351499ab2014-04-23 04:50:03 +00004010
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004011 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004012 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4013 if (ret) {
4014 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004015 "VSI seid %d Tx ring %d %sable timeout\n",
4016 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004017 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004018 }
4019 }
4020
Neerav Parikh23527302014-06-03 23:50:15 +00004021 return ret;
4022}
4023
4024/**
4025 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4026 * @pf: the PF being configured
4027 * @pf_q: the PF queue
4028 * @enable: enable or disable state of the queue
4029 *
4030 * This routine will wait for the given Rx queue of the PF to reach the
4031 * enabled or disabled state.
4032 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4033 * multiple retries; else will return 0 in case of success.
4034 **/
4035static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4036{
4037 int i;
4038 u32 rx_reg;
4039
4040 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4041 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4042 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4043 break;
4044
Neerav Parikhf98a2002014-09-13 07:40:44 +00004045 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004046 }
4047 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4048 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004049
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004050 return 0;
4051}
4052
4053/**
Jacob Kellerc768e492017-04-13 04:45:47 -04004054 * i40e_control_rx_q - Start or stop a particular Rx queue
4055 * @pf: the PF structure
4056 * @pf_q: the PF queue to configure
4057 * @enable: start or stop the queue
4058 *
4059 * This function enables or disables a single queue. Note that any delay
4060 * required after the operation is expected to be handled by the caller of
4061 * this function.
4062 **/
4063static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4064{
4065 struct i40e_hw *hw = &pf->hw;
4066 u32 rx_reg;
4067 int i;
4068
4069 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4070 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4071 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4072 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4073 break;
4074 usleep_range(1000, 2000);
4075 }
4076
4077 /* Skip if the queue is already in the requested state */
4078 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4079 return;
4080
4081 /* turn on/off the queue */
4082 if (enable)
4083 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4084 else
4085 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4086
4087 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4088}
4089
4090/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004091 * i40e_vsi_control_rx - Start or stop a VSI's rings
4092 * @vsi: the VSI being configured
4093 * @enable: start or stop the rings
4094 **/
4095static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4096{
4097 struct i40e_pf *pf = vsi->back;
Jacob Kellerc768e492017-04-13 04:45:47 -04004098 int i, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004099
4100 pf_q = vsi->base_queue;
4101 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kellerc768e492017-04-13 04:45:47 -04004102 i40e_control_rx_q(pf, pf_q, enable);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004103
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004104 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004105 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4106 if (ret) {
4107 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004108 "VSI seid %d Rx ring %d %sable timeout\n",
4109 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004110 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004111 }
4112 }
4113
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004114 /* Due to HW errata, on Rx disable only, the register can indicate done
4115 * before it really is. Needs 50ms to be sure
4116 */
4117 if (!enable)
4118 mdelay(50);
4119
Neerav Parikh23527302014-06-03 23:50:15 +00004120 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004121}
4122
4123/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004124 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004125 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004126 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004127int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004128{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004129 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004130
4131 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004132 ret = i40e_vsi_control_rx(vsi, true);
4133 if (ret)
4134 return ret;
4135 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004136
4137 return ret;
4138}
4139
4140/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004141 * i40e_vsi_stop_rings - Stop a VSI's rings
4142 * @vsi: the VSI being configured
4143 **/
4144void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4145{
Jacob Keller34807562017-04-13 04:45:53 -04004146 /* When port TX is suspended, don't wait */
4147 if (test_bit(__I40E_PORT_SUSPENDED, &vsi->back->state))
4148 return i40e_vsi_stop_rings_no_wait(vsi);
4149
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004150 /* do rx first for enable and last for disable
4151 * Ignore return value, we need to shutdown whatever we can
4152 */
4153 i40e_vsi_control_tx(vsi, false);
4154 i40e_vsi_control_rx(vsi, false);
4155}
4156
4157/**
Jacob Kellere4b433f2017-04-13 04:45:52 -04004158 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4159 * @vsi: the VSI being shutdown
4160 *
4161 * This function stops all the rings for a VSI but does not delay to verify
4162 * that rings have been disabled. It is expected that the caller is shutting
4163 * down multiple VSIs at once and will delay together for all the VSIs after
4164 * initiating the shutdown. This is particularly useful for shutting down lots
4165 * of VFs together. Otherwise, a large delay can be incurred while configuring
4166 * each VSI in serial.
4167 **/
4168void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4169{
4170 struct i40e_pf *pf = vsi->back;
4171 int i, pf_q;
4172
4173 pf_q = vsi->base_queue;
4174 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4175 i40e_control_tx_q(pf, pf_q, false);
4176 i40e_control_rx_q(pf, pf_q, false);
4177 }
4178}
4179
4180/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004181 * i40e_vsi_free_irq - Free the irq association with the OS
4182 * @vsi: the VSI being configured
4183 **/
4184static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4185{
4186 struct i40e_pf *pf = vsi->back;
4187 struct i40e_hw *hw = &pf->hw;
4188 int base = vsi->base_vector;
4189 u32 val, qp;
4190 int i;
4191
4192 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4193 if (!vsi->q_vectors)
4194 return;
4195
Shannon Nelson63741842014-04-23 04:50:16 +00004196 if (!vsi->irqs_ready)
4197 return;
4198
4199 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004200 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004201 int irq_num;
4202 u16 vector;
4203
4204 vector = i + base;
4205 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004206
4207 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004208 if (!vsi->q_vectors[i] ||
4209 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004210 continue;
4211
Alan Brady96db7762016-09-14 16:24:38 -07004212 /* clear the affinity notifier in the IRQ descriptor */
4213 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004214 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004215 irq_set_affinity_hint(irq_num, NULL);
4216 synchronize_irq(irq_num);
4217 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004218
4219 /* Tear down the interrupt queue link list
4220 *
4221 * We know that they come in pairs and always
4222 * the Rx first, then the Tx. To clear the
4223 * link list, stick the EOL value into the
4224 * next_q field of the registers.
4225 */
4226 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4227 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4228 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4229 val |= I40E_QUEUE_END_OF_LIST
4230 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4231 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4232
4233 while (qp != I40E_QUEUE_END_OF_LIST) {
4234 u32 next;
4235
4236 val = rd32(hw, I40E_QINT_RQCTL(qp));
4237
4238 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4239 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4240 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4241 I40E_QINT_RQCTL_INTEVENT_MASK);
4242
4243 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4244 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4245
4246 wr32(hw, I40E_QINT_RQCTL(qp), val);
4247
4248 val = rd32(hw, I40E_QINT_TQCTL(qp));
4249
4250 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4251 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4252
4253 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4254 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4255 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4256 I40E_QINT_TQCTL_INTEVENT_MASK);
4257
4258 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4259 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4260
4261 wr32(hw, I40E_QINT_TQCTL(qp), val);
4262 qp = next;
4263 }
4264 }
4265 } else {
4266 free_irq(pf->pdev->irq, pf);
4267
4268 val = rd32(hw, I40E_PFINT_LNKLST0);
4269 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4270 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4271 val |= I40E_QUEUE_END_OF_LIST
4272 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4273 wr32(hw, I40E_PFINT_LNKLST0, val);
4274
4275 val = rd32(hw, I40E_QINT_RQCTL(qp));
4276 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4277 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4278 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4279 I40E_QINT_RQCTL_INTEVENT_MASK);
4280
4281 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4282 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4283
4284 wr32(hw, I40E_QINT_RQCTL(qp), val);
4285
4286 val = rd32(hw, I40E_QINT_TQCTL(qp));
4287
4288 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4289 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4290 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4291 I40E_QINT_TQCTL_INTEVENT_MASK);
4292
4293 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4294 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4295
4296 wr32(hw, I40E_QINT_TQCTL(qp), val);
4297 }
4298}
4299
4300/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004301 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4302 * @vsi: the VSI being configured
4303 * @v_idx: Index of vector to be freed
4304 *
4305 * This function frees the memory allocated to the q_vector. In addition if
4306 * NAPI is enabled it will delete any references to the NAPI struct prior
4307 * to freeing the q_vector.
4308 **/
4309static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4310{
4311 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004312 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004313
4314 if (!q_vector)
4315 return;
4316
4317 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004318 i40e_for_each_ring(ring, q_vector->tx)
4319 ring->q_vector = NULL;
4320
4321 i40e_for_each_ring(ring, q_vector->rx)
4322 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004323
4324 /* only VSI w/ an associated netdev is set up w/ NAPI */
4325 if (vsi->netdev)
4326 netif_napi_del(&q_vector->napi);
4327
4328 vsi->q_vectors[v_idx] = NULL;
4329
4330 kfree_rcu(q_vector, rcu);
4331}
4332
4333/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004334 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4335 * @vsi: the VSI being un-configured
4336 *
4337 * This frees the memory allocated to the q_vectors and
4338 * deletes references to the NAPI struct.
4339 **/
4340static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4341{
4342 int v_idx;
4343
Alexander Duyck493fb302013-09-28 07:01:44 +00004344 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4345 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004346}
4347
4348/**
4349 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4350 * @pf: board private structure
4351 **/
4352static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4353{
4354 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4355 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4356 pci_disable_msix(pf->pdev);
4357 kfree(pf->msix_entries);
4358 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004359 kfree(pf->irq_pile);
4360 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004361 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4362 pci_disable_msi(pf->pdev);
4363 }
4364 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4365}
4366
4367/**
4368 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4369 * @pf: board private structure
4370 *
4371 * We go through and clear interrupt specific resources and reset the structure
4372 * to pre-load conditions
4373 **/
4374static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4375{
4376 int i;
4377
Shannon Nelsone1477582015-02-21 06:44:33 +00004378 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004379 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004380 synchronize_irq(pf->msix_entries[0].vector);
4381 free_irq(pf->msix_entries[0].vector, pf);
4382 }
4383
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004384 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4385 I40E_IWARP_IRQ_PILE_ID);
4386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004387 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004388 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004389 if (pf->vsi[i])
4390 i40e_vsi_free_q_vectors(pf->vsi[i]);
4391 i40e_reset_interrupt_capability(pf);
4392}
4393
4394/**
4395 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4396 * @vsi: the VSI being configured
4397 **/
4398static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4399{
4400 int q_idx;
4401
4402 if (!vsi->netdev)
4403 return;
4404
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004405 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4406 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4407
4408 if (q_vector->rx.ring || q_vector->tx.ring)
4409 napi_enable(&q_vector->napi);
4410 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004411}
4412
4413/**
4414 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4415 * @vsi: the VSI being configured
4416 **/
4417static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4418{
4419 int q_idx;
4420
4421 if (!vsi->netdev)
4422 return;
4423
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004424 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4425 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4426
4427 if (q_vector->rx.ring || q_vector->tx.ring)
4428 napi_disable(&q_vector->napi);
4429 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004430}
4431
4432/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004433 * i40e_vsi_close - Shut down a VSI
4434 * @vsi: the vsi to be quelled
4435 **/
4436static void i40e_vsi_close(struct i40e_vsi *vsi)
4437{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004438 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004439 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4440 i40e_down(vsi);
4441 i40e_vsi_free_irq(vsi);
4442 i40e_vsi_free_tx_resources(vsi);
4443 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004444 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004445 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4446 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4447 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004448}
4449
4450/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004451 * i40e_quiesce_vsi - Pause a given VSI
4452 * @vsi: the VSI being paused
4453 **/
4454static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4455{
4456 if (test_bit(__I40E_DOWN, &vsi->state))
4457 return;
4458
4459 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004460 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004461 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004462 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004463 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004464}
4465
4466/**
4467 * i40e_unquiesce_vsi - Resume a given VSI
4468 * @vsi: the VSI being resumed
4469 **/
4470static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4471{
4472 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4473 return;
4474
4475 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4476 if (vsi->netdev && netif_running(vsi->netdev))
4477 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4478 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004479 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004480}
4481
4482/**
4483 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4484 * @pf: the PF
4485 **/
4486static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4487{
4488 int v;
4489
Mitch Williams505682c2014-05-20 08:01:37 +00004490 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004491 if (pf->vsi[v])
4492 i40e_quiesce_vsi(pf->vsi[v]);
4493 }
4494}
4495
4496/**
4497 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4498 * @pf: the PF
4499 **/
4500static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4501{
4502 int v;
4503
Mitch Williams505682c2014-05-20 08:01:37 +00004504 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004505 if (pf->vsi[v])
4506 i40e_unquiesce_vsi(pf->vsi[v]);
4507 }
4508}
4509
Neerav Parikh69129dc2014-11-12 00:18:46 +00004510/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004511 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004512 * @vsi: the VSI being configured
4513 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004514 * Wait until all queues on a given VSI have been disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004515 **/
Jacob Kellere4b433f2017-04-13 04:45:52 -04004516int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004517{
4518 struct i40e_pf *pf = vsi->back;
4519 int i, pf_q, ret;
4520
4521 pf_q = vsi->base_queue;
4522 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004523 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004524 ret = i40e_pf_txq_wait(pf, pf_q, false);
4525 if (ret) {
4526 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004527 "VSI seid %d Tx ring %d disable timeout\n",
4528 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004529 return ret;
4530 }
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004531 /* Check and wait for the Tx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004532 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4533 if (ret) {
4534 dev_info(&pf->pdev->dev,
4535 "VSI seid %d Rx ring %d disable timeout\n",
4536 vsi->seid, pf_q);
4537 return ret;
4538 }
4539 }
4540
Neerav Parikh69129dc2014-11-12 00:18:46 +00004541 return 0;
4542}
4543
Jacob Kellere4b433f2017-04-13 04:45:52 -04004544#ifdef CONFIG_I40E_DCB
Neerav Parikh69129dc2014-11-12 00:18:46 +00004545/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004546 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004547 * @pf: the PF
4548 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004549 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004550 * VSIs that are managed by this PF.
4551 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004552static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004553{
4554 int v, ret = 0;
4555
4556 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004557 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004558 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004559 if (ret)
4560 break;
4561 }
4562 }
4563
4564 return ret;
4565}
4566
4567#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004568
4569/**
4570 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4571 * @q_idx: TX queue number
4572 * @vsi: Pointer to VSI struct
4573 *
4574 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004575 * We proactively detect hung TX queues by checking if interrupts are disabled
4576 * but there are pending descriptors. If it appears hung, attempt to recover
4577 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004578 **/
4579static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4580{
4581 struct i40e_ring *tx_ring = NULL;
4582 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004583 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004584 int i;
4585
4586 pf = vsi->back;
4587
4588 /* now that we have an index, find the tx_ring struct */
4589 for (i = 0; i < vsi->num_queue_pairs; i++) {
4590 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4591 if (q_idx == vsi->tx_rings[i]->queue_index) {
4592 tx_ring = vsi->tx_rings[i];
4593 break;
4594 }
4595 }
4596 }
4597
4598 if (!tx_ring)
4599 return;
4600
4601 /* Read interrupt register */
4602 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4603 val = rd32(&pf->hw,
4604 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4605 tx_ring->vsi->base_vector - 1));
4606 else
4607 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4608
Alan Brady17daabb2017-04-05 07:50:56 -04004609 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004610
Alan Brady17daabb2017-04-05 07:50:56 -04004611 /* Interrupts are disabled and TX pending is non-zero,
4612 * trigger the SW interrupt (don't wait). Worst case
4613 * there will be one extra interrupt which may result
4614 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004615 */
Alan Brady17daabb2017-04-05 07:50:56 -04004616 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4617 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004618}
4619
4620/**
4621 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4622 * @pf: pointer to PF struct
4623 *
4624 * LAN VSI has netdev and netdev has TX queues. This function is to check
4625 * each of those TX queues if they are hung, trigger recovery by issuing
4626 * SW interrupt.
4627 **/
4628static void i40e_detect_recover_hung(struct i40e_pf *pf)
4629{
4630 struct net_device *netdev;
4631 struct i40e_vsi *vsi;
4632 int i;
4633
4634 /* Only for LAN VSI */
4635 vsi = pf->vsi[pf->lan_vsi];
4636
4637 if (!vsi)
4638 return;
4639
4640 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4641 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4642 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4643 return;
4644
4645 /* Make sure type is MAIN VSI */
4646 if (vsi->type != I40E_VSI_MAIN)
4647 return;
4648
4649 netdev = vsi->netdev;
4650 if (!netdev)
4651 return;
4652
4653 /* Bail out if netif_carrier is not OK */
4654 if (!netif_carrier_ok(netdev))
4655 return;
4656
4657 /* Go thru' TX queues for netdev */
4658 for (i = 0; i < netdev->num_tx_queues; i++) {
4659 struct netdev_queue *q;
4660
4661 q = netdev_get_tx_queue(netdev, i);
4662 if (q)
4663 i40e_detect_recover_hung_queue(i, vsi);
4664 }
4665}
4666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004667/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004668 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004669 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004670 *
4671 * Get TC map for ISCSI PF type that will include iSCSI TC
4672 * and LAN TC.
4673 **/
4674static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4675{
4676 struct i40e_dcb_app_priority_table app;
4677 struct i40e_hw *hw = &pf->hw;
4678 u8 enabled_tc = 1; /* TC0 is always enabled */
4679 u8 tc, i;
4680 /* Get the iSCSI APP TLV */
4681 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4682
4683 for (i = 0; i < dcbcfg->numapps; i++) {
4684 app = dcbcfg->app[i];
4685 if (app.selector == I40E_APP_SEL_TCPIP &&
4686 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4687 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004688 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004689 break;
4690 }
4691 }
4692
4693 return enabled_tc;
4694}
4695
4696/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004697 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4698 * @dcbcfg: the corresponding DCBx configuration structure
4699 *
4700 * Return the number of TCs from given DCBx configuration
4701 **/
4702static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4703{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004704 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004705 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004706 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004707
4708 /* Scan the ETS Config Priority Table to find
4709 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004710 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004711 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004712 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4713 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4714
4715 /* Now scan the bitmask to check for
4716 * contiguous TCs starting with TC0
4717 */
4718 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4719 if (num_tc & BIT(i)) {
4720 if (!tc_unused) {
4721 ret++;
4722 } else {
4723 pr_err("Non-contiguous TC - Disabling DCB\n");
4724 return 1;
4725 }
4726 } else {
4727 tc_unused = 1;
4728 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004729 }
4730
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004731 /* There is always at least TC0 */
4732 if (!ret)
4733 ret = 1;
4734
4735 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004736}
4737
4738/**
4739 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4740 * @dcbcfg: the corresponding DCBx configuration structure
4741 *
4742 * Query the current DCB configuration and return the number of
4743 * traffic classes enabled from the given DCBX config
4744 **/
4745static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4746{
4747 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4748 u8 enabled_tc = 1;
4749 u8 i;
4750
4751 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004752 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004753
4754 return enabled_tc;
4755}
4756
4757/**
4758 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4759 * @pf: PF being queried
4760 *
4761 * Return number of traffic classes enabled for the given PF
4762 **/
4763static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4764{
4765 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004766 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004767 u8 num_tc = 0;
4768 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4769
4770 /* If DCB is not enabled then always in single TC */
4771 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4772 return 1;
4773
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004774 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004775 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4776 return i40e_dcb_get_num_tc(dcbcfg);
4777
4778 /* MFP mode return count of enabled TCs for this PF */
4779 if (pf->hw.func_caps.iscsi)
4780 enabled_tc = i40e_get_iscsi_tc_map(pf);
4781 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004782 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004783
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004784 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004785 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004786 num_tc++;
4787 }
4788 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004789}
4790
4791/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004792 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4793 * @pf: PF being queried
4794 *
4795 * Return a bitmap for enabled traffic classes for this PF.
4796 **/
4797static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4798{
4799 /* If DCB is not enabled for this PF then just return default TC */
4800 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004801 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004802
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004803 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004804 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4805 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4806
Neerav Parikhfc51de92015-02-24 06:58:53 +00004807 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004808 if (pf->hw.func_caps.iscsi)
4809 return i40e_get_iscsi_tc_map(pf);
4810 else
David Ertmanea6acb72016-09-20 07:10:50 -07004811 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004812}
4813
4814/**
4815 * i40e_vsi_get_bw_info - Query VSI BW Information
4816 * @vsi: the VSI being queried
4817 *
4818 * Returns 0 on success, negative value on failure
4819 **/
4820static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4821{
4822 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4823 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4824 struct i40e_pf *pf = vsi->back;
4825 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004826 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004827 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004828 int i;
4829
4830 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004831 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4832 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004834 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4835 i40e_stat_str(&pf->hw, ret),
4836 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004837 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838 }
4839
4840 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004841 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4842 NULL);
4843 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004844 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004845 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4846 i40e_stat_str(&pf->hw, ret),
4847 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004848 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 }
4850
4851 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4852 dev_info(&pf->pdev->dev,
4853 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4854 bw_config.tc_valid_bits,
4855 bw_ets_config.tc_valid_bits);
4856 /* Still continuing */
4857 }
4858
4859 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4860 vsi->bw_max_quanta = bw_config.max_bw;
4861 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4862 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4863 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4864 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4865 vsi->bw_ets_limit_credits[i] =
4866 le16_to_cpu(bw_ets_config.credits[i]);
4867 /* 3 bits out of 4 for each TC */
4868 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4869 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004870
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004871 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004872}
4873
4874/**
4875 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4876 * @vsi: the VSI being configured
4877 * @enabled_tc: TC bitmap
4878 * @bw_credits: BW shared credits per TC
4879 *
4880 * Returns 0 on success, negative value on failure
4881 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004882static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004883 u8 *bw_share)
4884{
4885 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004886 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004887 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004888
4889 bw_data.tc_valid_bits = enabled_tc;
4890 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4891 bw_data.tc_bw_credits[i] = bw_share[i];
4892
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004893 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4894 NULL);
4895 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004896 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004897 "AQ command Config VSI BW allocation per TC failed = %d\n",
4898 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004899 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004900 }
4901
4902 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4903 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4904
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004905 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004906}
4907
4908/**
4909 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4910 * @vsi: the VSI being configured
4911 * @enabled_tc: TC map to be enabled
4912 *
4913 **/
4914static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4915{
4916 struct net_device *netdev = vsi->netdev;
4917 struct i40e_pf *pf = vsi->back;
4918 struct i40e_hw *hw = &pf->hw;
4919 u8 netdev_tc = 0;
4920 int i;
4921 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4922
4923 if (!netdev)
4924 return;
4925
4926 if (!enabled_tc) {
4927 netdev_reset_tc(netdev);
4928 return;
4929 }
4930
4931 /* Set up actual enabled TCs on the VSI */
4932 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4933 return;
4934
4935 /* set per TC queues for the VSI */
4936 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4937 /* Only set TC queues for enabled tcs
4938 *
4939 * e.g. For a VSI that has TC0 and TC3 enabled the
4940 * enabled_tc bitmap would be 0x00001001; the driver
4941 * will set the numtc for netdev as 2 that will be
4942 * referenced by the netdev layer as TC 0 and 1.
4943 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004944 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004945 netdev_set_tc_queue(netdev,
4946 vsi->tc_config.tc_info[i].netdev_tc,
4947 vsi->tc_config.tc_info[i].qcount,
4948 vsi->tc_config.tc_info[i].qoffset);
4949 }
4950
4951 /* Assign UP2TC map for the VSI */
4952 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4953 /* Get the actual TC# for the UP */
4954 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4955 /* Get the mapped netdev TC# for the UP */
4956 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4957 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4958 }
4959}
4960
4961/**
4962 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4963 * @vsi: the VSI being configured
4964 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4965 **/
4966static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4967 struct i40e_vsi_context *ctxt)
4968{
4969 /* copy just the sections touched not the entire info
4970 * since not all sections are valid as returned by
4971 * update vsi params
4972 */
4973 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4974 memcpy(&vsi->info.queue_mapping,
4975 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4976 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4977 sizeof(vsi->info.tc_mapping));
4978}
4979
4980/**
4981 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4982 * @vsi: VSI to be configured
4983 * @enabled_tc: TC bitmap
4984 *
4985 * This configures a particular VSI for TCs that are mapped to the
4986 * given TC bitmap. It uses default bandwidth share for TCs across
4987 * VSIs to configure TC for a particular VSI.
4988 *
4989 * NOTE:
4990 * It is expected that the VSI queues have been quisced before calling
4991 * this function.
4992 **/
4993static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4994{
4995 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4996 struct i40e_vsi_context ctxt;
4997 int ret = 0;
4998 int i;
4999
5000 /* Check if enabled_tc is same as existing or new TCs */
5001 if (vsi->tc_config.enabled_tc == enabled_tc)
5002 return ret;
5003
5004 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5005 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005006 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005007 bw_share[i] = 1;
5008 }
5009
5010 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5011 if (ret) {
5012 dev_info(&vsi->back->pdev->dev,
5013 "Failed configuring TC map %d for VSI %d\n",
5014 enabled_tc, vsi->seid);
5015 goto out;
5016 }
5017
5018 /* Update Queue Pairs Mapping for currently enabled UPs */
5019 ctxt.seid = vsi->seid;
5020 ctxt.pf_num = vsi->back->hw.pf_id;
5021 ctxt.vf_num = 0;
5022 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005023 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005024 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5025
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005026 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5027 ctxt.info.valid_sections |=
5028 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5029 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5030 }
5031
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005032 /* Update the VSI after updating the VSI queue-mapping information */
5033 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5034 if (ret) {
5035 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005036 "Update vsi tc config failed, err %s aq_err %s\n",
5037 i40e_stat_str(&vsi->back->hw, ret),
5038 i40e_aq_str(&vsi->back->hw,
5039 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005040 goto out;
5041 }
5042 /* update the local VSI info with updated queue map */
5043 i40e_vsi_update_queue_map(vsi, &ctxt);
5044 vsi->info.valid_sections = 0;
5045
5046 /* Update current VSI BW information */
5047 ret = i40e_vsi_get_bw_info(vsi);
5048 if (ret) {
5049 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005050 "Failed updating vsi bw info, err %s aq_err %s\n",
5051 i40e_stat_str(&vsi->back->hw, ret),
5052 i40e_aq_str(&vsi->back->hw,
5053 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005054 goto out;
5055 }
5056
5057 /* Update the netdev TC setup */
5058 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5059out:
5060 return ret;
5061}
5062
5063/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005064 * i40e_veb_config_tc - Configure TCs for given VEB
5065 * @veb: given VEB
5066 * @enabled_tc: TC bitmap
5067 *
5068 * Configures given TC bitmap for VEB (switching) element
5069 **/
5070int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5071{
5072 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5073 struct i40e_pf *pf = veb->pf;
5074 int ret = 0;
5075 int i;
5076
5077 /* No TCs or already enabled TCs just return */
5078 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5079 return ret;
5080
5081 bw_data.tc_valid_bits = enabled_tc;
5082 /* bw_data.absolute_credits is not set (relative) */
5083
5084 /* Enable ETS TCs with equal BW Share for now */
5085 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005086 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005087 bw_data.tc_bw_share_credits[i] = 1;
5088 }
5089
5090 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5091 &bw_data, NULL);
5092 if (ret) {
5093 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005094 "VEB bw config failed, err %s aq_err %s\n",
5095 i40e_stat_str(&pf->hw, ret),
5096 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005097 goto out;
5098 }
5099
5100 /* Update the BW information */
5101 ret = i40e_veb_get_bw_info(veb);
5102 if (ret) {
5103 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005104 "Failed getting veb bw config, err %s aq_err %s\n",
5105 i40e_stat_str(&pf->hw, ret),
5106 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005107 }
5108
5109out:
5110 return ret;
5111}
5112
5113#ifdef CONFIG_I40E_DCB
5114/**
5115 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5116 * @pf: PF struct
5117 *
5118 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5119 * the caller would've quiesce all the VSIs before calling
5120 * this function
5121 **/
5122static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5123{
5124 u8 tc_map = 0;
5125 int ret;
5126 u8 v;
5127
5128 /* Enable the TCs available on PF to all VEBs */
5129 tc_map = i40e_pf_get_tc_map(pf);
5130 for (v = 0; v < I40E_MAX_VEB; v++) {
5131 if (!pf->veb[v])
5132 continue;
5133 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5134 if (ret) {
5135 dev_info(&pf->pdev->dev,
5136 "Failed configuring TC for VEB seid=%d\n",
5137 pf->veb[v]->seid);
5138 /* Will try to configure as many components */
5139 }
5140 }
5141
5142 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005143 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005144 if (!pf->vsi[v])
5145 continue;
5146
5147 /* - Enable all TCs for the LAN VSI
5148 * - For all others keep them at TC0 for now
5149 */
5150 if (v == pf->lan_vsi)
5151 tc_map = i40e_pf_get_tc_map(pf);
5152 else
David Ertmanea6acb72016-09-20 07:10:50 -07005153 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005154
5155 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5156 if (ret) {
5157 dev_info(&pf->pdev->dev,
5158 "Failed configuring TC for VSI seid=%d\n",
5159 pf->vsi[v]->seid);
5160 /* Will try to configure as many components */
5161 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005162 /* Re-configure VSI vectors based on updated TC map */
5163 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005164 if (pf->vsi[v]->netdev)
5165 i40e_dcbnl_set_all(pf->vsi[v]);
5166 }
5167 }
5168}
5169
5170/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005171 * i40e_resume_port_tx - Resume port Tx
5172 * @pf: PF struct
5173 *
5174 * Resume a port's Tx and issue a PF reset in case of failure to
5175 * resume.
5176 **/
5177static int i40e_resume_port_tx(struct i40e_pf *pf)
5178{
5179 struct i40e_hw *hw = &pf->hw;
5180 int ret;
5181
5182 ret = i40e_aq_resume_port_tx(hw, NULL);
5183 if (ret) {
5184 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005185 "Resume Port Tx failed, err %s aq_err %s\n",
5186 i40e_stat_str(&pf->hw, ret),
5187 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005188 /* Schedule PF reset to recover */
5189 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5190 i40e_service_event_schedule(pf);
5191 }
5192
5193 return ret;
5194}
5195
5196/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005197 * i40e_init_pf_dcb - Initialize DCB configuration
5198 * @pf: PF being configured
5199 *
5200 * Query the current DCB configuration and cache it
5201 * in the hardware structure
5202 **/
5203static int i40e_init_pf_dcb(struct i40e_pf *pf)
5204{
5205 struct i40e_hw *hw = &pf->hw;
5206 int err = 0;
5207
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005208 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005209 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005210 goto out;
5211
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005212 /* Get the initial DCB configuration */
5213 err = i40e_init_dcb(hw);
5214 if (!err) {
5215 /* Device/Function is not DCBX capable */
5216 if ((!hw->func_caps.dcb) ||
5217 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5218 dev_info(&pf->pdev->dev,
5219 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005220 } else {
5221 /* When status is not DISABLED then DCBX in FW */
5222 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5223 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005224
5225 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005226 /* Enable DCB tagging only when more than one TC
5227 * or explicitly disable if only one TC
5228 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005229 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5230 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005231 else
5232 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005233 dev_dbg(&pf->pdev->dev,
5234 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005235 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005236 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005237 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005238 "Query for DCB configuration failed, err %s aq_err %s\n",
5239 i40e_stat_str(&pf->hw, err),
5240 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005241 }
5242
5243out:
5244 return err;
5245}
5246#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005247#define SPEED_SIZE 14
5248#define FC_SIZE 8
5249/**
5250 * i40e_print_link_message - print link up or down
5251 * @vsi: the VSI for which link needs a message
5252 */
Matt Jaredc156f852015-08-27 11:42:39 -04005253void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005254{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005255 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005256 char *speed = "Unknown";
5257 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005258 char *fec = "";
5259 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005260
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005261 new_speed = vsi->back->hw.phy.link_info.link_speed;
5262
5263 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005264 return;
5265 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005266 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005267 if (!isup) {
5268 netdev_info(vsi->netdev, "NIC Link is Down\n");
5269 return;
5270 }
5271
Greg Rose148c2d82014-12-11 07:06:27 +00005272 /* Warn user if link speed on NPAR enabled partition is not at
5273 * least 10GB
5274 */
5275 if (vsi->back->hw.func_caps.npar_enable &&
5276 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5277 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5278 netdev_warn(vsi->netdev,
5279 "The partition detected link speed that is less than 10Gbps\n");
5280
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005281 switch (vsi->back->hw.phy.link_info.link_speed) {
5282 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005283 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005284 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005285 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005286 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005287 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005288 case I40E_LINK_SPEED_25GB:
5289 speed = "25 G";
5290 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005291 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005292 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005293 break;
5294 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005295 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005296 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005297 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005298 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005299 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005300 default:
5301 break;
5302 }
5303
5304 switch (vsi->back->hw.fc.current_mode) {
5305 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005306 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005307 break;
5308 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005309 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005310 break;
5311 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005312 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005313 break;
5314 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005315 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005316 break;
5317 }
5318
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005319 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5320 fec = ", FEC: None";
5321 an = ", Autoneg: False";
5322
5323 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5324 an = ", Autoneg: True";
5325
5326 if (vsi->back->hw.phy.link_info.fec_info &
5327 I40E_AQ_CONFIG_FEC_KR_ENA)
5328 fec = ", FEC: CL74 FC-FEC/BASE-R";
5329 else if (vsi->back->hw.phy.link_info.fec_info &
5330 I40E_AQ_CONFIG_FEC_RS_ENA)
5331 fec = ", FEC: CL108 RS-FEC";
5332 }
5333
5334 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5335 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005336}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005337
5338/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005339 * i40e_up_complete - Finish the last steps of bringing up a connection
5340 * @vsi: the VSI being configured
5341 **/
5342static int i40e_up_complete(struct i40e_vsi *vsi)
5343{
5344 struct i40e_pf *pf = vsi->back;
5345 int err;
5346
5347 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5348 i40e_vsi_configure_msix(vsi);
5349 else
5350 i40e_configure_msi_and_legacy(vsi);
5351
5352 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005353 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005354 if (err)
5355 return err;
5356
5357 clear_bit(__I40E_DOWN, &vsi->state);
5358 i40e_napi_enable_all(vsi);
5359 i40e_vsi_enable_irq(vsi);
5360
5361 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5362 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005363 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005364 netif_tx_start_all_queues(vsi->netdev);
5365 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005366 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005367 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005368 /* need to check for qualified module here*/
5369 if ((pf->hw.phy.link_info.link_info &
5370 I40E_AQ_MEDIA_AVAILABLE) &&
5371 (!(pf->hw.phy.link_info.an_info &
5372 I40E_AQ_QUALIFIED_MODULE)))
5373 netdev_err(vsi->netdev,
5374 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005375 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005376
5377 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005378 if (vsi->type == I40E_VSI_FDIR) {
5379 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005380 pf->fd_add_err = 0;
5381 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005382 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005383 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005384
5385 /* On the next run of the service_task, notify any clients of the new
5386 * opened netdev
5387 */
5388 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005389 i40e_service_event_schedule(pf);
5390
5391 return 0;
5392}
5393
5394/**
5395 * i40e_vsi_reinit_locked - Reset the VSI
5396 * @vsi: the VSI being configured
5397 *
5398 * Rebuild the ring structs after some configuration
5399 * has changed, e.g. MTU size.
5400 **/
5401static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5402{
5403 struct i40e_pf *pf = vsi->back;
5404
5405 WARN_ON(in_interrupt());
5406 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5407 usleep_range(1000, 2000);
5408 i40e_down(vsi);
5409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005410 i40e_up(vsi);
5411 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5412}
5413
5414/**
5415 * i40e_up - Bring the connection back up after being down
5416 * @vsi: the VSI being configured
5417 **/
5418int i40e_up(struct i40e_vsi *vsi)
5419{
5420 int err;
5421
5422 err = i40e_vsi_configure(vsi);
5423 if (!err)
5424 err = i40e_up_complete(vsi);
5425
5426 return err;
5427}
5428
5429/**
5430 * i40e_down - Shutdown the connection processing
5431 * @vsi: the VSI being stopped
5432 **/
5433void i40e_down(struct i40e_vsi *vsi)
5434{
5435 int i;
5436
5437 /* It is assumed that the caller of this function
5438 * sets the vsi->state __I40E_DOWN bit.
5439 */
5440 if (vsi->netdev) {
5441 netif_carrier_off(vsi->netdev);
5442 netif_tx_disable(vsi->netdev);
5443 }
5444 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005445 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005446 i40e_napi_disable_all(vsi);
5447
5448 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005449 i40e_clean_tx_ring(vsi->tx_rings[i]);
5450 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005451 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005453}
5454
5455/**
5456 * i40e_setup_tc - configure multiple traffic classes
5457 * @netdev: net device to configure
5458 * @tc: number of traffic classes to enable
5459 **/
5460static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5461{
5462 struct i40e_netdev_priv *np = netdev_priv(netdev);
5463 struct i40e_vsi *vsi = np->vsi;
5464 struct i40e_pf *pf = vsi->back;
5465 u8 enabled_tc = 0;
5466 int ret = -EINVAL;
5467 int i;
5468
5469 /* Check if DCB enabled to continue */
5470 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5471 netdev_info(netdev, "DCB is not enabled for adapter\n");
5472 goto exit;
5473 }
5474
5475 /* Check if MFP enabled */
5476 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5477 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5478 goto exit;
5479 }
5480
5481 /* Check whether tc count is within enabled limit */
5482 if (tc > i40e_pf_get_num_tc(pf)) {
5483 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5484 goto exit;
5485 }
5486
5487 /* Generate TC map for number of tc requested */
5488 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005489 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005490
5491 /* Requesting same TC configuration as already enabled */
5492 if (enabled_tc == vsi->tc_config.enabled_tc)
5493 return 0;
5494
5495 /* Quiesce VSI queues */
5496 i40e_quiesce_vsi(vsi);
5497
5498 /* Configure VSI for enabled TCs */
5499 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5500 if (ret) {
5501 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5502 vsi->seid);
5503 goto exit;
5504 }
5505
5506 /* Unquiesce VSI */
5507 i40e_unquiesce_vsi(vsi);
5508
5509exit:
5510 return ret;
5511}
5512
John Fastabend16e5cc62016-02-16 21:16:43 -08005513static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5514 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005515{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005516 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005517 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005518
5519 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5520
5521 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005522}
5523
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005524/**
5525 * i40e_open - Called when a network interface is made active
5526 * @netdev: network interface device structure
5527 *
5528 * The open entry point is called when a network interface is made
5529 * active by the system (IFF_UP). At this point all resources needed
5530 * for transmit and receive operations are allocated, the interrupt
5531 * handler is registered with the OS, the netdev watchdog subtask is
5532 * enabled, and the stack is notified that the interface is ready.
5533 *
5534 * Returns 0 on success, negative value on failure
5535 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005536int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537{
5538 struct i40e_netdev_priv *np = netdev_priv(netdev);
5539 struct i40e_vsi *vsi = np->vsi;
5540 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005541 int err;
5542
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005543 /* disallow open during test or if eeprom is broken */
5544 if (test_bit(__I40E_TESTING, &pf->state) ||
5545 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546 return -EBUSY;
5547
5548 netif_carrier_off(netdev);
5549
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005550 err = i40e_vsi_open(vsi);
5551 if (err)
5552 return err;
5553
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005554 /* configure global TSO hardware offload settings */
5555 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5556 TCP_FLAG_FIN) >> 16);
5557 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5558 TCP_FLAG_FIN |
5559 TCP_FLAG_CWR) >> 16);
5560 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5561
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005562 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005563
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005564 return 0;
5565}
5566
5567/**
5568 * i40e_vsi_open -
5569 * @vsi: the VSI to open
5570 *
5571 * Finish initialization of the VSI.
5572 *
5573 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005574 *
5575 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005576 **/
5577int i40e_vsi_open(struct i40e_vsi *vsi)
5578{
5579 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005580 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005581 int err;
5582
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583 /* allocate descriptors */
5584 err = i40e_vsi_setup_tx_resources(vsi);
5585 if (err)
5586 goto err_setup_tx;
5587 err = i40e_vsi_setup_rx_resources(vsi);
5588 if (err)
5589 goto err_setup_rx;
5590
5591 err = i40e_vsi_configure(vsi);
5592 if (err)
5593 goto err_setup_rx;
5594
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005595 if (vsi->netdev) {
5596 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5597 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5598 err = i40e_vsi_request_irq(vsi, int_name);
5599 if (err)
5600 goto err_setup_rx;
5601
5602 /* Notify the stack of the actual queue counts. */
5603 err = netif_set_real_num_tx_queues(vsi->netdev,
5604 vsi->num_queue_pairs);
5605 if (err)
5606 goto err_set_queues;
5607
5608 err = netif_set_real_num_rx_queues(vsi->netdev,
5609 vsi->num_queue_pairs);
5610 if (err)
5611 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005612
5613 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005614 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005615 dev_driver_string(&pf->pdev->dev),
5616 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005617 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005618
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005619 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005620 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005621 goto err_setup_rx;
5622 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005624 err = i40e_up_complete(vsi);
5625 if (err)
5626 goto err_up_complete;
5627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005628 return 0;
5629
5630err_up_complete:
5631 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005632err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005633 i40e_vsi_free_irq(vsi);
5634err_setup_rx:
5635 i40e_vsi_free_rx_resources(vsi);
5636err_setup_tx:
5637 i40e_vsi_free_tx_resources(vsi);
5638 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005639 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005640
5641 return err;
5642}
5643
5644/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005645 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005646 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005647 *
5648 * This function destroys the hlist where all the Flow Director
5649 * filters were saved.
5650 **/
5651static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5652{
5653 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005654 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005655 struct hlist_node *node2;
5656
5657 hlist_for_each_entry_safe(filter, node2,
5658 &pf->fdir_filter_list, fdir_node) {
5659 hlist_del(&filter->fdir_node);
5660 kfree(filter);
5661 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005662
Jacob Keller0e588de2017-02-06 14:38:50 -08005663 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5664 list_del(&pit_entry->list);
5665 kfree(pit_entry);
5666 }
5667 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5668
5669 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5670 list_del(&pit_entry->list);
5671 kfree(pit_entry);
5672 }
5673 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5674
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005675 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005676 pf->fd_tcp4_filter_cnt = 0;
5677 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005678 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005679 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005680
5681 /* Reprogram the default input set for TCP/IPv4 */
5682 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5683 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5684 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5685
5686 /* Reprogram the default input set for UDP/IPv4 */
5687 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5688 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5689 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5690
5691 /* Reprogram the default input set for SCTP/IPv4 */
5692 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5693 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5694 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5695
5696 /* Reprogram the default input set for Other/IPv4 */
5697 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5698 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005699}
5700
5701/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005702 * i40e_close - Disables a network interface
5703 * @netdev: network interface device structure
5704 *
5705 * The close entry point is called when an interface is de-activated
5706 * by the OS. The hardware is still under the driver's control, but
5707 * this netdev interface is disabled.
5708 *
5709 * Returns 0, this is not allowed to fail
5710 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005711int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005712{
5713 struct i40e_netdev_priv *np = netdev_priv(netdev);
5714 struct i40e_vsi *vsi = np->vsi;
5715
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005716 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005717
5718 return 0;
5719}
5720
5721/**
5722 * i40e_do_reset - Start a PF or Core Reset sequence
5723 * @pf: board private structure
5724 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005725 * @lock_acquired: indicates whether or not the lock has been acquired
5726 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005727 *
5728 * The essential difference in resets is that the PF Reset
5729 * doesn't clear the packet buffers, doesn't reset the PE
5730 * firmware, and doesn't bother the other PFs on the chip.
5731 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005732void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005733{
5734 u32 val;
5735
5736 WARN_ON(in_interrupt());
5737
Mitch Williams263fc482014-04-23 04:50:11 +00005738
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005739 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005740 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005741
5742 /* Request a Global Reset
5743 *
5744 * This will start the chip's countdown to the actual full
5745 * chip reset event, and a warning interrupt to be sent
5746 * to all PFs, including the requestor. Our handler
5747 * for the warning interrupt will deal with the shutdown
5748 * and recovery of the switch setup.
5749 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005750 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005751 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5752 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5753 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5754
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005755 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005756
5757 /* Request a Core Reset
5758 *
5759 * Same as Global Reset, except does *not* include the MAC/PHY
5760 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005761 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005762 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5763 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5764 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5765 i40e_flush(&pf->hw);
5766
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005767 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005768
5769 /* Request a PF Reset
5770 *
5771 * Resets only the PF-specific registers
5772 *
5773 * This goes directly to the tear-down and rebuild of
5774 * the switch, since we need to do all the recovery as
5775 * for the Core Reset.
5776 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005777 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005778 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005779
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005780 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005781 int v;
5782
5783 /* Find the VSI(s) that requested a re-init */
5784 dev_info(&pf->pdev->dev,
5785 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005786 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005787 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005788
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005789 if (vsi != NULL &&
5790 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5791 i40e_vsi_reinit_locked(pf->vsi[v]);
5792 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5793 }
5794 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005795 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005796 int v;
5797
5798 /* Find the VSI(s) that needs to be brought down */
5799 dev_info(&pf->pdev->dev, "VSI down requested\n");
5800 for (v = 0; v < pf->num_alloc_vsi; v++) {
5801 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005802
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005803 if (vsi != NULL &&
5804 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5805 set_bit(__I40E_DOWN, &vsi->state);
5806 i40e_down(vsi);
5807 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5808 }
5809 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005810 } else {
5811 dev_info(&pf->pdev->dev,
5812 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005813 }
5814}
5815
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005816#ifdef CONFIG_I40E_DCB
5817/**
5818 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5819 * @pf: board private structure
5820 * @old_cfg: current DCB config
5821 * @new_cfg: new DCB config
5822 **/
5823bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5824 struct i40e_dcbx_config *old_cfg,
5825 struct i40e_dcbx_config *new_cfg)
5826{
5827 bool need_reconfig = false;
5828
5829 /* Check if ETS configuration has changed */
5830 if (memcmp(&new_cfg->etscfg,
5831 &old_cfg->etscfg,
5832 sizeof(new_cfg->etscfg))) {
5833 /* If Priority Table has changed reconfig is needed */
5834 if (memcmp(&new_cfg->etscfg.prioritytable,
5835 &old_cfg->etscfg.prioritytable,
5836 sizeof(new_cfg->etscfg.prioritytable))) {
5837 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005838 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005839 }
5840
5841 if (memcmp(&new_cfg->etscfg.tcbwtable,
5842 &old_cfg->etscfg.tcbwtable,
5843 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005844 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005845
5846 if (memcmp(&new_cfg->etscfg.tsatable,
5847 &old_cfg->etscfg.tsatable,
5848 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005849 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005850 }
5851
5852 /* Check if PFC configuration has changed */
5853 if (memcmp(&new_cfg->pfc,
5854 &old_cfg->pfc,
5855 sizeof(new_cfg->pfc))) {
5856 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005857 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005858 }
5859
5860 /* Check if APP Table has changed */
5861 if (memcmp(&new_cfg->app,
5862 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005863 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005864 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005865 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005866 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005867
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005868 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005869 return need_reconfig;
5870}
5871
5872/**
5873 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5874 * @pf: board private structure
5875 * @e: event info posted on ARQ
5876 **/
5877static int i40e_handle_lldp_event(struct i40e_pf *pf,
5878 struct i40e_arq_event_info *e)
5879{
5880 struct i40e_aqc_lldp_get_mib *mib =
5881 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5882 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005883 struct i40e_dcbx_config tmp_dcbx_cfg;
5884 bool need_reconfig = false;
5885 int ret = 0;
5886 u8 type;
5887
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005888 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005889 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005890 return ret;
5891
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005892 /* Ignore if event is not for Nearest Bridge */
5893 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5894 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005895 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005896 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5897 return ret;
5898
5899 /* Check MIB Type and return if event for Remote MIB update */
5900 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005901 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005902 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005903 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5904 /* Update the remote cached instance and return */
5905 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5906 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5907 &hw->remote_dcbx_config);
5908 goto exit;
5909 }
5910
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005911 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005912 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005913
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005914 /* Reset the old DCBx configuration data */
5915 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005916 /* Get updated DCBX data from firmware */
5917 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005918 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005919 dev_info(&pf->pdev->dev,
5920 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5921 i40e_stat_str(&pf->hw, ret),
5922 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005923 goto exit;
5924 }
5925
5926 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005927 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5928 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005929 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005930 goto exit;
5931 }
5932
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005933 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5934 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005935
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005936 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005937
5938 if (!need_reconfig)
5939 goto exit;
5940
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005941 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005942 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005943 pf->flags |= I40E_FLAG_DCB_ENABLED;
5944 else
5945 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5946
Jacob Keller34807562017-04-13 04:45:53 -04005947 set_bit(__I40E_PORT_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005948 /* Reconfiguration needed quiesce all VSIs */
5949 i40e_pf_quiesce_all_vsi(pf);
5950
5951 /* Changes in configuration update VEB/VSI */
5952 i40e_dcb_reconfigure(pf);
5953
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005954 ret = i40e_resume_port_tx(pf);
5955
Jacob Keller34807562017-04-13 04:45:53 -04005956 clear_bit(__I40E_PORT_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005957 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005958 if (ret)
5959 goto exit;
5960
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005961 /* Wait for the PF's queues to be disabled */
5962 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005963 if (ret) {
5964 /* Schedule PF reset to recover */
5965 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5966 i40e_service_event_schedule(pf);
5967 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005968 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08005969 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
5970 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005971 }
5972
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005973exit:
5974 return ret;
5975}
5976#endif /* CONFIG_I40E_DCB */
5977
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005978/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005979 * i40e_do_reset_safe - Protected reset path for userland calls.
5980 * @pf: board private structure
5981 * @reset_flags: which reset is requested
5982 *
5983 **/
5984void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5985{
5986 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04005987 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005988 rtnl_unlock();
5989}
5990
5991/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005992 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5993 * @pf: board private structure
5994 * @e: event info posted on ARQ
5995 *
5996 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5997 * and VF queues
5998 **/
5999static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6000 struct i40e_arq_event_info *e)
6001{
6002 struct i40e_aqc_lan_overflow *data =
6003 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6004 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6005 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6006 struct i40e_hw *hw = &pf->hw;
6007 struct i40e_vf *vf;
6008 u16 vf_id;
6009
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006010 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6011 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006012
6013 /* Queue belongs to VF, find the VF and issue VF reset */
6014 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6015 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6016 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6017 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6018 vf_id -= hw->func_caps.vf_base_id;
6019 vf = &pf->vf[vf_id];
6020 i40e_vc_notify_vf_reset(vf);
6021 /* Allow VF to process pending reset notification */
6022 msleep(20);
6023 i40e_reset_vf(vf, false);
6024 }
6025}
6026
6027/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006028 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6029 * @pf: board private structure
6030 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006031u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006032{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006033 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006034
6035 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6036 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6037 return fcnt_prog;
6038}
6039
6040/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006041 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006042 * @pf: board private structure
6043 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006044u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006045{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006046 u32 val, fcnt_prog;
6047
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006048 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6049 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6050 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6051 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6052 return fcnt_prog;
6053}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006054
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006055/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006056 * i40e_get_global_fd_count - Get total FD filters programmed on device
6057 * @pf: board private structure
6058 **/
6059u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6060{
6061 u32 val, fcnt_prog;
6062
6063 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6064 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6065 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6066 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6067 return fcnt_prog;
6068}
6069
6070/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006071 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6072 * @pf: board private structure
6073 **/
6074void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6075{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006076 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006077 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006078 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006079
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006080 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6081 return;
6082
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006083 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6084 * to re-enable
6085 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006086 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006087 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006088 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6089 (pf->fd_add_err == 0) ||
6090 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006091 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006092 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6093 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006094 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6095 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 +00006096 }
6097 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006098
6099 /* Wait for some more space to be available to turn on ATR. We also
6100 * must check that no existing ntuple rules for TCP are in effect
6101 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006102 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6103 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006104 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006105 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006106 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006107 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006108 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 +00006109 }
6110 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006111
6112 /* if hw had a problem adding a filter, delete it */
6113 if (pf->fd_inv > 0) {
6114 hlist_for_each_entry_safe(filter, node,
6115 &pf->fdir_filter_list, fdir_node) {
6116 if (filter->fd_id == pf->fd_inv) {
6117 hlist_del(&filter->fdir_node);
6118 kfree(filter);
6119 pf->fdir_pf_active_filters--;
6120 }
6121 }
6122 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006123}
6124
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006125#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006126#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006127/**
6128 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6129 * @pf: board private structure
6130 **/
6131static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6132{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006133 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006134 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006135 bool disable_atr = false;
6136 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006137 int reg;
6138
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006139 if (!time_after(jiffies, pf->fd_flush_timestamp +
6140 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6141 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006142
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006143 /* If the flush is happening too quick and we have mostly SB rules we
6144 * should not re-enable ATR for some time.
6145 */
6146 min_flush_time = pf->fd_flush_timestamp +
6147 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6148 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006149
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006150 if (!(time_after(jiffies, min_flush_time)) &&
6151 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6152 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6153 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6154 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006155 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006156
6157 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006158 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006159 /* flush all filters */
6160 wr32(&pf->hw, I40E_PFQF_CTL_1,
6161 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6162 i40e_flush(&pf->hw);
6163 pf->fd_flush_cnt++;
6164 pf->fd_add_err = 0;
6165 do {
6166 /* Check FD flush status every 5-6msec */
6167 usleep_range(5000, 6000);
6168 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6169 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6170 break;
6171 } while (flush_wait_retry--);
6172 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6173 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6174 } else {
6175 /* replay sideband filters */
6176 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006177 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006178 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006179 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6180 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6181 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6182 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006183}
6184
6185/**
6186 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6187 * @pf: board private structure
6188 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006189u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006190{
6191 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6192}
6193
6194/* We can see up to 256 filter programming desc in transit if the filters are
6195 * being applied really fast; before we see the first
6196 * filter miss error on Rx queue 0. Accumulating enough error messages before
6197 * reacting will make sure we don't cause flush too often.
6198 */
6199#define I40E_MAX_FD_PROGRAM_ERROR 256
6200
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006201/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006202 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6203 * @pf: board private structure
6204 **/
6205static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6206{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006207
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006208 /* if interface is down do nothing */
6209 if (test_bit(__I40E_DOWN, &pf->state))
6210 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006211
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006212 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006213 i40e_fdir_flush_and_replay(pf);
6214
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006215 i40e_fdir_check_and_reenable(pf);
6216
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217}
6218
6219/**
6220 * i40e_vsi_link_event - notify VSI of a link event
6221 * @vsi: vsi to be notified
6222 * @link_up: link up or down
6223 **/
6224static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6225{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006226 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006227 return;
6228
6229 switch (vsi->type) {
6230 case I40E_VSI_MAIN:
6231 if (!vsi->netdev || !vsi->netdev_registered)
6232 break;
6233
6234 if (link_up) {
6235 netif_carrier_on(vsi->netdev);
6236 netif_tx_wake_all_queues(vsi->netdev);
6237 } else {
6238 netif_carrier_off(vsi->netdev);
6239 netif_tx_stop_all_queues(vsi->netdev);
6240 }
6241 break;
6242
6243 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006244 case I40E_VSI_VMDQ2:
6245 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006246 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006247 case I40E_VSI_MIRROR:
6248 default:
6249 /* there is no notification for other VSIs */
6250 break;
6251 }
6252}
6253
6254/**
6255 * i40e_veb_link_event - notify elements on the veb of a link event
6256 * @veb: veb to be notified
6257 * @link_up: link up or down
6258 **/
6259static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6260{
6261 struct i40e_pf *pf;
6262 int i;
6263
6264 if (!veb || !veb->pf)
6265 return;
6266 pf = veb->pf;
6267
6268 /* depth first... */
6269 for (i = 0; i < I40E_MAX_VEB; i++)
6270 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6271 i40e_veb_link_event(pf->veb[i], link_up);
6272
6273 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006274 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006275 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6276 i40e_vsi_link_event(pf->vsi[i], link_up);
6277}
6278
6279/**
6280 * i40e_link_event - Update netif_carrier status
6281 * @pf: board private structure
6282 **/
6283static void i40e_link_event(struct i40e_pf *pf)
6284{
Mitch Williams320684c2014-10-17 03:14:43 +00006285 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006286 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006287 i40e_status status;
6288 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006290 /* save off old link status information */
6291 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6292
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006293 /* set this to force the get_link_status call to refresh state */
6294 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006296 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006297
6298 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006299
6300 /* On success, disable temp link polling */
6301 if (status == I40E_SUCCESS) {
6302 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6303 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6304 } else {
6305 /* Enable link polling temporarily until i40e_get_link_status
6306 * returns I40E_SUCCESS
6307 */
6308 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006309 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6310 status);
6311 return;
6312 }
6313
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006314 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6315 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006316
6317 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006318 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006319 (test_bit(__I40E_DOWN, &vsi->state) ||
6320 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006321 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006322
6323 if (!test_bit(__I40E_DOWN, &vsi->state))
6324 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006325
6326 /* Notify the base of the switch tree connected to
6327 * the link. Floating VEBs are not notified.
6328 */
6329 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6330 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6331 else
Mitch Williams320684c2014-10-17 03:14:43 +00006332 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006333
6334 if (pf->vf)
6335 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006336
6337 if (pf->flags & I40E_FLAG_PTP)
6338 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006339}
6340
6341/**
Shannon Nelson21536712014-10-25 10:35:25 +00006342 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006343 * @pf: board private structure
6344 **/
6345static void i40e_watchdog_subtask(struct i40e_pf *pf)
6346{
6347 int i;
6348
6349 /* if interface is down do nothing */
6350 if (test_bit(__I40E_DOWN, &pf->state) ||
6351 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6352 return;
6353
Shannon Nelson21536712014-10-25 10:35:25 +00006354 /* make sure we don't do these things too often */
6355 if (time_before(jiffies, (pf->service_timer_previous +
6356 pf->service_timer_period)))
6357 return;
6358 pf->service_timer_previous = jiffies;
6359
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006360 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6361 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006362 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006364 /* Update the stats for active netdevs so the network stack
6365 * can look at updated numbers whenever it cares to
6366 */
Mitch Williams505682c2014-05-20 08:01:37 +00006367 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006368 if (pf->vsi[i] && pf->vsi[i]->netdev)
6369 i40e_update_stats(pf->vsi[i]);
6370
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006371 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6372 /* Update the stats for the active switching components */
6373 for (i = 0; i < I40E_MAX_VEB; i++)
6374 if (pf->veb[i])
6375 i40e_update_veb_stats(pf->veb[i]);
6376 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006377
6378 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006379}
6380
6381/**
6382 * i40e_reset_subtask - Set up for resetting the device and driver
6383 * @pf: board private structure
6384 **/
6385static void i40e_reset_subtask(struct i40e_pf *pf)
6386{
6387 u32 reset_flags = 0;
6388
6389 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006390 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6392 }
6393 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006394 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006395 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6396 }
6397 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006398 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006399 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6400 }
6401 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006402 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006403 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6404 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006405 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006406 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006407 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6408 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006409
6410 /* If there's a recovery already waiting, it takes
6411 * precedence before starting a new reset sequence.
6412 */
6413 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006414 i40e_prep_for_reset(pf, false);
6415 i40e_reset(pf);
6416 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006417 }
6418
6419 /* If we're already down or resetting, just bail */
6420 if (reset_flags &&
6421 !test_bit(__I40E_DOWN, &pf->state) &&
Maciej Sosin373149f2017-04-05 07:50:55 -04006422 !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
6423 rtnl_lock();
6424 i40e_do_reset(pf, reset_flags, true);
6425 rtnl_unlock();
6426 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006427}
6428
6429/**
6430 * i40e_handle_link_event - Handle link event
6431 * @pf: board private structure
6432 * @e: event info posted on ARQ
6433 **/
6434static void i40e_handle_link_event(struct i40e_pf *pf,
6435 struct i40e_arq_event_info *e)
6436{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006437 struct i40e_aqc_get_link_status *status =
6438 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006439
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006440 /* Do a new status request to re-enable LSE reporting
6441 * and load new status information into the hw struct
6442 * This completely ignores any state information
6443 * in the ARQ event info, instead choosing to always
6444 * issue the AQ update link status command.
6445 */
6446 i40e_link_event(pf);
6447
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006448 /* check for unqualified module, if link is down */
6449 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6450 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6451 (!(status->link_info & I40E_AQ_LINK_UP)))
6452 dev_err(&pf->pdev->dev,
6453 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454}
6455
6456/**
6457 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6458 * @pf: board private structure
6459 **/
6460static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6461{
6462 struct i40e_arq_event_info event;
6463 struct i40e_hw *hw = &pf->hw;
6464 u16 pending, i = 0;
6465 i40e_status ret;
6466 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006467 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006468 u32 val;
6469
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006470 /* Do not run clean AQ when PF reset fails */
6471 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6472 return;
6473
Shannon Nelson86df2422014-05-20 08:01:35 +00006474 /* check for error indications */
6475 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6476 oldval = val;
6477 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006478 if (hw->debug_mask & I40E_DEBUG_AQ)
6479 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006480 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6481 }
6482 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006483 if (hw->debug_mask & I40E_DEBUG_AQ)
6484 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006485 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006486 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006487 }
6488 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006489 if (hw->debug_mask & I40E_DEBUG_AQ)
6490 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006491 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6492 }
6493 if (oldval != val)
6494 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6495
6496 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6497 oldval = val;
6498 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006499 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6500 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006501 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6502 }
6503 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006504 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6505 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006506 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6507 }
6508 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006509 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6510 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006511 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6512 }
6513 if (oldval != val)
6514 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6515
Mitch Williams1001dc32014-11-11 20:02:19 +00006516 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6517 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518 if (!event.msg_buf)
6519 return;
6520
6521 do {
6522 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006523 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006524 break;
Mitch Williams56497972014-06-04 08:45:18 +00006525 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006526 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6527 break;
6528 }
6529
6530 opcode = le16_to_cpu(event.desc.opcode);
6531 switch (opcode) {
6532
6533 case i40e_aqc_opc_get_link_status:
6534 i40e_handle_link_event(pf, &event);
6535 break;
6536 case i40e_aqc_opc_send_msg_to_pf:
6537 ret = i40e_vc_process_vf_msg(pf,
6538 le16_to_cpu(event.desc.retval),
6539 le32_to_cpu(event.desc.cookie_high),
6540 le32_to_cpu(event.desc.cookie_low),
6541 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006542 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006543 break;
6544 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006545 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006546#ifdef CONFIG_I40E_DCB
6547 rtnl_lock();
6548 ret = i40e_handle_lldp_event(pf, &event);
6549 rtnl_unlock();
6550#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551 break;
6552 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006553 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006554 i40e_handle_lan_overflow_event(pf, &event);
6555 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006556 case i40e_aqc_opc_send_msg_to_peer:
6557 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6558 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006559 case i40e_aqc_opc_nvm_erase:
6560 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006561 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006562 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6563 "ARQ NVM operation 0x%04x completed\n",
6564 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006565 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006566 default:
6567 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006568 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006569 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006570 break;
6571 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006572 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006573
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006574 if (i < pf->adminq_work_limit)
6575 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6576
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006577 /* re-enable Admin queue interrupt cause */
6578 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6579 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6580 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6581 i40e_flush(hw);
6582
6583 kfree(event.msg_buf);
6584}
6585
6586/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006587 * i40e_verify_eeprom - make sure eeprom is good to use
6588 * @pf: board private structure
6589 **/
6590static void i40e_verify_eeprom(struct i40e_pf *pf)
6591{
6592 int err;
6593
6594 err = i40e_diag_eeprom_test(&pf->hw);
6595 if (err) {
6596 /* retry in case of garbage read */
6597 err = i40e_diag_eeprom_test(&pf->hw);
6598 if (err) {
6599 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6600 err);
6601 set_bit(__I40E_BAD_EEPROM, &pf->state);
6602 }
6603 }
6604
6605 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6606 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6607 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6608 }
6609}
6610
6611/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006612 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006613 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006614 *
6615 * enable switch loop back or die - no point in a return value
6616 **/
6617static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6618{
6619 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6620 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006621 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006622
6623 ctxt.seid = pf->main_vsi_seid;
6624 ctxt.pf_num = pf->hw.pf_id;
6625 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006626 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6627 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006628 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006629 "couldn't get PF vsi config, err %s aq_err %s\n",
6630 i40e_stat_str(&pf->hw, ret),
6631 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006632 return;
6633 }
6634 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6635 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6636 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6637
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006638 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6639 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006640 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006641 "update vsi switch failed, err %s aq_err %s\n",
6642 i40e_stat_str(&pf->hw, ret),
6643 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006644 }
6645}
6646
6647/**
6648 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006649 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006650 *
6651 * disable switch loop back or die - no point in a return value
6652 **/
6653static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6654{
6655 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6656 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006657 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006658
6659 ctxt.seid = pf->main_vsi_seid;
6660 ctxt.pf_num = pf->hw.pf_id;
6661 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006662 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6663 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006664 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006665 "couldn't get PF vsi config, err %s aq_err %s\n",
6666 i40e_stat_str(&pf->hw, ret),
6667 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006668 return;
6669 }
6670 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6671 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6672 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6673
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006674 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6675 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006676 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006677 "update vsi switch failed, err %s aq_err %s\n",
6678 i40e_stat_str(&pf->hw, ret),
6679 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006680 }
6681}
6682
6683/**
Neerav Parikh51616012015-02-06 08:52:14 +00006684 * i40e_config_bridge_mode - Configure the HW bridge mode
6685 * @veb: pointer to the bridge instance
6686 *
6687 * Configure the loop back mode for the LAN VSI that is downlink to the
6688 * specified HW bridge instance. It is expected this function is called
6689 * when a new HW bridge is instantiated.
6690 **/
6691static void i40e_config_bridge_mode(struct i40e_veb *veb)
6692{
6693 struct i40e_pf *pf = veb->pf;
6694
Shannon Nelson6dec1012015-09-28 14:12:30 -04006695 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6696 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6697 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006698 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6699 i40e_disable_pf_switch_lb(pf);
6700 else
6701 i40e_enable_pf_switch_lb(pf);
6702}
6703
6704/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6706 * @veb: pointer to the VEB instance
6707 *
6708 * This is a recursive function that first builds the attached VSIs then
6709 * recurses in to build the next layer of VEB. We track the connections
6710 * through our own index numbers because the seid's from the HW could
6711 * change across the reset.
6712 **/
6713static int i40e_reconstitute_veb(struct i40e_veb *veb)
6714{
6715 struct i40e_vsi *ctl_vsi = NULL;
6716 struct i40e_pf *pf = veb->pf;
6717 int v, veb_idx;
6718 int ret;
6719
6720 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006721 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006722 if (pf->vsi[v] &&
6723 pf->vsi[v]->veb_idx == veb->idx &&
6724 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6725 ctl_vsi = pf->vsi[v];
6726 break;
6727 }
6728 }
6729 if (!ctl_vsi) {
6730 dev_info(&pf->pdev->dev,
6731 "missing owner VSI for veb_idx %d\n", veb->idx);
6732 ret = -ENOENT;
6733 goto end_reconstitute;
6734 }
6735 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6736 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6737 ret = i40e_add_vsi(ctl_vsi);
6738 if (ret) {
6739 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006740 "rebuild of veb_idx %d owner VSI failed: %d\n",
6741 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006742 goto end_reconstitute;
6743 }
6744 i40e_vsi_reset_stats(ctl_vsi);
6745
6746 /* create the VEB in the switch and move the VSI onto the VEB */
6747 ret = i40e_add_veb(veb, ctl_vsi);
6748 if (ret)
6749 goto end_reconstitute;
6750
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006751 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6752 veb->bridge_mode = BRIDGE_MODE_VEB;
6753 else
6754 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006755 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006756
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006757 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006758 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006759 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6760 continue;
6761
6762 if (pf->vsi[v]->veb_idx == veb->idx) {
6763 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006765 vsi->uplink_seid = veb->seid;
6766 ret = i40e_add_vsi(vsi);
6767 if (ret) {
6768 dev_info(&pf->pdev->dev,
6769 "rebuild of vsi_idx %d failed: %d\n",
6770 v, ret);
6771 goto end_reconstitute;
6772 }
6773 i40e_vsi_reset_stats(vsi);
6774 }
6775 }
6776
6777 /* create any VEBs attached to this VEB - RECURSION */
6778 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6779 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6780 pf->veb[veb_idx]->uplink_seid = veb->seid;
6781 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6782 if (ret)
6783 break;
6784 }
6785 }
6786
6787end_reconstitute:
6788 return ret;
6789}
6790
6791/**
6792 * i40e_get_capabilities - get info about the HW
6793 * @pf: the PF struct
6794 **/
6795static int i40e_get_capabilities(struct i40e_pf *pf)
6796{
6797 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6798 u16 data_size;
6799 int buf_len;
6800 int err;
6801
6802 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6803 do {
6804 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6805 if (!cap_buf)
6806 return -ENOMEM;
6807
6808 /* this loads the data into the hw struct for us */
6809 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6810 &data_size,
6811 i40e_aqc_opc_list_func_capabilities,
6812 NULL);
6813 /* data loaded, buffer no longer needed */
6814 kfree(cap_buf);
6815
6816 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6817 /* retry with a larger buffer */
6818 buf_len = data_size;
6819 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6820 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006821 "capability discovery failed, err %s aq_err %s\n",
6822 i40e_stat_str(&pf->hw, err),
6823 i40e_aq_str(&pf->hw,
6824 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006825 return -ENODEV;
6826 }
6827 } while (err);
6828
6829 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6830 dev_info(&pf->pdev->dev,
6831 "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",
6832 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6833 pf->hw.func_caps.num_msix_vectors,
6834 pf->hw.func_caps.num_msix_vectors_vf,
6835 pf->hw.func_caps.fd_filters_guaranteed,
6836 pf->hw.func_caps.fd_filters_best_effort,
6837 pf->hw.func_caps.num_tx_qp,
6838 pf->hw.func_caps.num_vsis);
6839
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006840#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6841 + pf->hw.func_caps.num_vfs)
6842 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6843 dev_info(&pf->pdev->dev,
6844 "got num_vsis %d, setting num_vsis to %d\n",
6845 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6846 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6847 }
6848
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006849 return 0;
6850}
6851
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006852static int i40e_vsi_clear(struct i40e_vsi *vsi);
6853
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006855 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006856 * @pf: board private structure
6857 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006858static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006859{
6860 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006862 /* quick workaround for an NVM issue that leaves a critical register
6863 * uninitialized
6864 */
6865 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6866 static const u32 hkey[] = {
6867 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6868 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6869 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6870 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006871 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006872
6873 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6874 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6875 }
6876
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006877 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878 return;
6879
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006880 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006881 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006882
6883 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006884 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006885 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6886 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006887 if (!vsi) {
6888 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006889 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6890 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006891 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006892 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006893
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006894 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006895}
6896
6897/**
6898 * i40e_fdir_teardown - release the Flow Director resources
6899 * @pf: board private structure
6900 **/
6901static void i40e_fdir_teardown(struct i40e_pf *pf)
6902{
Alexander Duyck4b816442016-10-11 15:26:53 -07006903 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006904
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006905 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006906 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6907 if (vsi)
6908 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006909}
6910
6911/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006912 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006913 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04006914 * @lock_acquired: indicates whether or not the lock has been acquired
6915 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006916 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006917 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006918 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006919static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006920{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006921 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006922 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006923 u32 v;
6924
6925 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6926 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006927 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006928 if (i40e_check_asq_alive(&pf->hw))
6929 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006930
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006931 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006932
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006933 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04006934 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
6935 if (!lock_acquired)
6936 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04006938 if (!lock_acquired)
6939 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006940
Mitch Williams505682c2014-05-20 08:01:37 +00006941 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006942 if (pf->vsi[v])
6943 pf->vsi[v]->seid = 0;
6944 }
6945
6946 i40e_shutdown_adminq(&pf->hw);
6947
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006948 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006949 if (hw->hmc.hmc_obj) {
6950 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006951 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006952 dev_warn(&pf->pdev->dev,
6953 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006954 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006955}
6956
6957/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006958 * i40e_send_version - update firmware with driver version
6959 * @pf: PF struct
6960 */
6961static void i40e_send_version(struct i40e_pf *pf)
6962{
6963 struct i40e_driver_version dv;
6964
6965 dv.major_version = DRV_VERSION_MAJOR;
6966 dv.minor_version = DRV_VERSION_MINOR;
6967 dv.build_version = DRV_VERSION_BUILD;
6968 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006969 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006970 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6971}
6972
6973/**
Maciej Sosin373149f2017-04-05 07:50:55 -04006974 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
6975 * @pf: board private structure
6976 **/
6977static int i40e_reset(struct i40e_pf *pf)
6978{
6979 struct i40e_hw *hw = &pf->hw;
6980 i40e_status ret;
6981
6982 ret = i40e_pf_reset(hw);
6983 if (ret) {
6984 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
6985 set_bit(__I40E_RESET_FAILED, &pf->state);
6986 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6987 } else {
6988 pf->pfr_count++;
6989 }
6990 return ret;
6991}
6992
6993/**
6994 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006995 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006996 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04006997 * @lock_acquired: indicates whether or not the lock has been acquired
6998 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006999 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007000static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007001{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007002 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007003 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007004 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007005 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04007006 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007007
7008 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007009 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007010 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007011
7012 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7013 ret = i40e_init_adminq(&pf->hw);
7014 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007015 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7016 i40e_stat_str(&pf->hw, ret),
7017 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007018 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019 }
7020
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007021 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00007022 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007023 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007024
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007025 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007026 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007027 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007028 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007029
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007030 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08007031 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032 if (ret) {
7033 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7034 goto end_core_reset;
7035 }
7036 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7037 if (ret) {
7038 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7039 goto end_core_reset;
7040 }
7041
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007042#ifdef CONFIG_I40E_DCB
7043 ret = i40e_init_pf_dcb(pf);
7044 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007045 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7046 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7047 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007048 }
7049#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007050 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04007051 if (!lock_acquired)
7052 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007053 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007054 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04007055 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007057 /* The driver only wants link up/down and module qualification
7058 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007059 */
7060 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007061 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007062 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007063 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007064 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007065 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7066 i40e_stat_str(&pf->hw, ret),
7067 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007068
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007069 /* make sure our flow control settings are restored */
7070 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7071 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007072 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7073 i40e_stat_str(&pf->hw, ret),
7074 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007075
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007076 /* Rebuild the VSIs and VEBs that existed before reset.
7077 * They are still in our local switch element arrays, so only
7078 * need to rebuild the switch model in the HW.
7079 *
7080 * If there were VEBs but the reconstitution failed, we'll try
7081 * try to recover minimal use by getting the basic PF VSI working.
7082 */
7083 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007084 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007085 /* find the one VEB connected to the MAC, and find orphans */
7086 for (v = 0; v < I40E_MAX_VEB; v++) {
7087 if (!pf->veb[v])
7088 continue;
7089
7090 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7091 pf->veb[v]->uplink_seid == 0) {
7092 ret = i40e_reconstitute_veb(pf->veb[v]);
7093
7094 if (!ret)
7095 continue;
7096
7097 /* If Main VEB failed, we're in deep doodoo,
7098 * so give up rebuilding the switch and set up
7099 * for minimal rebuild of PF VSI.
7100 * If orphan failed, we'll report the error
7101 * but try to keep going.
7102 */
7103 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7104 dev_info(&pf->pdev->dev,
7105 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7106 ret);
7107 pf->vsi[pf->lan_vsi]->uplink_seid
7108 = pf->mac_seid;
7109 break;
7110 } else if (pf->veb[v]->uplink_seid == 0) {
7111 dev_info(&pf->pdev->dev,
7112 "rebuild of orphan VEB failed: %d\n",
7113 ret);
7114 }
7115 }
7116 }
7117 }
7118
7119 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007120 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007121 /* no VEB, so rebuild only the Main VSI */
7122 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7123 if (ret) {
7124 dev_info(&pf->pdev->dev,
7125 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007126 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007127 }
7128 }
7129
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007130 /* Reconfigure hardware for allowing smaller MSS in the case
7131 * of TSO, so that we avoid the MDD being fired and causing
7132 * a reset in the case of small MSS+TSO.
7133 */
7134#define I40E_REG_MSS 0x000E64DC
7135#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7136#define I40E_64BYTE_MSS 0x400000
7137 val = rd32(hw, I40E_REG_MSS);
7138 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7139 val &= ~I40E_REG_MSS_MIN_MASK;
7140 val |= I40E_64BYTE_MSS;
7141 wr32(hw, I40E_REG_MSS, val);
7142 }
7143
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007144 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007145 msleep(75);
7146 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7147 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007148 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7149 i40e_stat_str(&pf->hw, ret),
7150 i40e_aq_str(&pf->hw,
7151 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007153 /* reinit the misc interrupt */
7154 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7155 ret = i40e_setup_misc_vector(pf);
7156
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007157 /* Add a filter to drop all Flow control frames from any VSI from being
7158 * transmitted. By doing so we stop a malicious VF from sending out
7159 * PAUSE or PFC frames and potentially controlling traffic for other
7160 * PF/VF VSIs.
7161 * The FW can still send Flow control frames if enabled.
7162 */
7163 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7164 pf->main_vsi_seid);
7165
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007166 /* restart the VSIs that were rebuilt and running before the reset */
7167 i40e_pf_unquiesce_all_vsi(pf);
7168
Jacob Keller024b05f2017-04-13 04:45:46 -04007169 /* Release the RTNL lock before we start resetting VFs */
7170 if (!lock_acquired)
7171 rtnl_unlock();
7172
Jacob Kellere4b433f2017-04-13 04:45:52 -04007173 i40e_reset_all_vfs(pf, true);
Mitch Williams69f64b22014-02-13 03:48:46 -08007174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007175 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007176 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007177
Jacob Keller024b05f2017-04-13 04:45:46 -04007178 /* We've already released the lock, so don't do it again */
7179 goto end_core_reset;
7180
Maciej Sosin373149f2017-04-05 07:50:55 -04007181end_unlock:
Jacob Keller024b05f2017-04-13 04:45:46 -04007182 if (!lock_acquired)
7183 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007184end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007185 clear_bit(__I40E_RESET_FAILED, &pf->state);
7186clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007187 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7188}
7189
7190/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007191 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7192 * @pf: board private structure
7193 * @reinit: if the Main VSI needs to re-initialized.
7194 * @lock_acquired: indicates whether or not the lock has been acquired
7195 * before this function was called.
7196 **/
7197static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7198 bool lock_acquired)
7199{
7200 int ret;
7201 /* Now we wait for GRST to settle out.
7202 * We don't have to delete the VEBs or VSIs from the hw switch
7203 * because the reset will make them disappear.
7204 */
7205 ret = i40e_reset(pf);
7206 if (!ret)
7207 i40e_rebuild(pf, reinit, lock_acquired);
7208}
7209
7210/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007211 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007212 * @pf: board private structure
7213 *
7214 * Close up the VFs and other things in prep for a Core Reset,
7215 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007216 * @lock_acquired: indicates whether or not the lock has been acquired
7217 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007218 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007219static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007220{
Maciej Sosin373149f2017-04-05 07:50:55 -04007221 i40e_prep_for_reset(pf, lock_acquired);
7222 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007223}
7224
7225/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007226 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007227 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007228 *
7229 * Called from the MDD irq handler to identify possibly malicious vfs
7230 **/
7231static void i40e_handle_mdd_event(struct i40e_pf *pf)
7232{
7233 struct i40e_hw *hw = &pf->hw;
7234 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007235 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007236 struct i40e_vf *vf;
7237 u32 reg;
7238 int i;
7239
7240 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7241 return;
7242
7243 /* find what triggered the MDD event */
7244 reg = rd32(hw, I40E_GL_MDET_TX);
7245 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007246 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7247 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007248 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007249 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007250 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007251 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007252 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7253 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7254 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007255 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007256 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 +00007257 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007258 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7259 mdd_detected = true;
7260 }
7261 reg = rd32(hw, I40E_GL_MDET_RX);
7262 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007263 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7264 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007265 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007266 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007267 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7268 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7269 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007270 if (netif_msg_rx_err(pf))
7271 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7272 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007273 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7274 mdd_detected = true;
7275 }
7276
Neerav Parikhdf430b12014-06-04 01:23:15 +00007277 if (mdd_detected) {
7278 reg = rd32(hw, I40E_PF_MDET_TX);
7279 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7280 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007281 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007282 pf_mdd_detected = true;
7283 }
7284 reg = rd32(hw, I40E_PF_MDET_RX);
7285 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7286 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007287 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007288 pf_mdd_detected = true;
7289 }
7290 /* Queue belongs to the PF, initiate a reset */
7291 if (pf_mdd_detected) {
7292 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7293 i40e_service_event_schedule(pf);
7294 }
7295 }
7296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007297 /* see if one of the VFs needs its hand slapped */
7298 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7299 vf = &(pf->vf[i]);
7300 reg = rd32(hw, I40E_VP_MDET_TX(i));
7301 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7302 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7303 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007304 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7305 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007306 }
7307
7308 reg = rd32(hw, I40E_VP_MDET_RX(i));
7309 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7310 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7311 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007312 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7313 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007314 }
7315
7316 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7317 dev_info(&pf->pdev->dev,
7318 "Too many MDD events on VF %d, disabled\n", i);
7319 dev_info(&pf->pdev->dev,
7320 "Use PF Control I/F to re-enable the VF\n");
Jacob Keller6322e632017-04-13 04:45:54 -04007321 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007322 }
7323 }
7324
7325 /* re-enable mdd interrupt cause */
7326 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7327 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7328 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7329 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7330 i40e_flush(hw);
7331}
7332
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007333/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007334 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007335 * @pf: board private structure
7336 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007337static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007338{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007339 struct i40e_hw *hw = &pf->hw;
7340 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007341 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007342 int i;
7343
Singhai, Anjali6a899022015-12-14 12:21:18 -08007344 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007345 return;
7346
Singhai, Anjali6a899022015-12-14 12:21:18 -08007347 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007348
7349 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007350 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7351 pf->pending_udp_bitmap &= ~BIT_ULL(i);
Jacob Keller27826fd2017-04-19 09:25:50 -04007352 port = pf->udp_ports[i].port;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007353 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007354 ret = i40e_aq_add_udp_tunnel(hw, port,
7355 pf->udp_ports[i].type,
7356 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007357 else
7358 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007359
7360 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007361 dev_dbg(&pf->pdev->dev,
7362 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7363 pf->udp_ports[i].type ? "vxlan" : "geneve",
7364 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007365 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007366 i40e_stat_str(&pf->hw, ret),
7367 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007368 pf->hw.aq.asq_last_status));
Jacob Keller27826fd2017-04-19 09:25:50 -04007369 pf->udp_ports[i].port = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007370 }
7371 }
7372 }
7373}
7374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007375/**
7376 * i40e_service_task - Run the driver's async subtasks
7377 * @work: pointer to work_struct containing our data
7378 **/
7379static void i40e_service_task(struct work_struct *work)
7380{
7381 struct i40e_pf *pf = container_of(work,
7382 struct i40e_pf,
7383 service_task);
7384 unsigned long start_time = jiffies;
7385
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007386 /* don't bother with service tasks if a reset is in progress */
7387 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007388 return;
7389 }
7390
Mitch Williams91089032016-11-21 13:03:51 -08007391 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7392 return;
7393
Kiran Patilb03a8c12015-09-24 18:13:15 -04007394 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007395 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007396 i40e_reset_subtask(pf);
7397 i40e_handle_mdd_event(pf);
7398 i40e_vc_process_vflr_event(pf);
7399 i40e_watchdog_subtask(pf);
7400 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007401 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7402 /* Client subtask will reopen next time through. */
7403 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7404 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7405 } else {
7406 i40e_client_subtask(pf);
7407 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7408 i40e_notify_client_of_l2_param_changes(
7409 pf->vsi[pf->lan_vsi]);
7410 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7411 }
7412 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007413 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007414 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007415 i40e_clean_adminq_subtask(pf);
7416
Mitch Williams91089032016-11-21 13:03:51 -08007417 /* flush memory to make sure state is correct before next watchdog */
7418 smp_mb__before_atomic();
7419 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007420
7421 /* If the tasks have taken longer than one timer cycle or there
7422 * is more work to be done, reschedule the service task now
7423 * rather than wait for the timer to tick again.
7424 */
7425 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7426 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7427 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7428 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7429 i40e_service_event_schedule(pf);
7430}
7431
7432/**
7433 * i40e_service_timer - timer callback
7434 * @data: pointer to PF struct
7435 **/
7436static void i40e_service_timer(unsigned long data)
7437{
7438 struct i40e_pf *pf = (struct i40e_pf *)data;
7439
7440 mod_timer(&pf->service_timer,
7441 round_jiffies(jiffies + pf->service_timer_period));
7442 i40e_service_event_schedule(pf);
7443}
7444
7445/**
7446 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7447 * @vsi: the VSI being configured
7448 **/
7449static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7450{
7451 struct i40e_pf *pf = vsi->back;
7452
7453 switch (vsi->type) {
7454 case I40E_VSI_MAIN:
7455 vsi->alloc_queue_pairs = pf->num_lan_qps;
7456 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7457 I40E_REQ_DESCRIPTOR_MULTIPLE);
7458 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7459 vsi->num_q_vectors = pf->num_lan_msix;
7460 else
7461 vsi->num_q_vectors = 1;
7462
7463 break;
7464
7465 case I40E_VSI_FDIR:
7466 vsi->alloc_queue_pairs = 1;
7467 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7468 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007469 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007470 break;
7471
7472 case I40E_VSI_VMDQ2:
7473 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7474 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7475 I40E_REQ_DESCRIPTOR_MULTIPLE);
7476 vsi->num_q_vectors = pf->num_vmdq_msix;
7477 break;
7478
7479 case I40E_VSI_SRIOV:
7480 vsi->alloc_queue_pairs = pf->num_vf_qps;
7481 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7482 I40E_REQ_DESCRIPTOR_MULTIPLE);
7483 break;
7484
7485 default:
7486 WARN_ON(1);
7487 return -ENODATA;
7488 }
7489
7490 return 0;
7491}
7492
7493/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007494 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7495 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007496 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007497 *
7498 * On error: returns error code (negative)
7499 * On success: returns 0
7500 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007501static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007502{
7503 int size;
7504 int ret = 0;
7505
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007506 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007507 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7508 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7509 if (!vsi->tx_rings)
7510 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007511 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7512
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007513 if (alloc_qvectors) {
7514 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007515 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007516 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7517 if (!vsi->q_vectors) {
7518 ret = -ENOMEM;
7519 goto err_vectors;
7520 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007521 }
7522 return ret;
7523
7524err_vectors:
7525 kfree(vsi->tx_rings);
7526 return ret;
7527}
7528
7529/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007530 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7531 * @pf: board private structure
7532 * @type: type of VSI
7533 *
7534 * On error: returns error code (negative)
7535 * On success: returns vsi index in PF (positive)
7536 **/
7537static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7538{
7539 int ret = -ENODEV;
7540 struct i40e_vsi *vsi;
7541 int vsi_idx;
7542 int i;
7543
7544 /* Need to protect the allocation of the VSIs at the PF level */
7545 mutex_lock(&pf->switch_mutex);
7546
7547 /* VSI list may be fragmented if VSI creation/destruction has
7548 * been happening. We can afford to do a quick scan to look
7549 * for any free VSIs in the list.
7550 *
7551 * find next empty vsi slot, looping back around if necessary
7552 */
7553 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007554 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007555 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007556 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007557 i = 0;
7558 while (i < pf->next_vsi && pf->vsi[i])
7559 i++;
7560 }
7561
Mitch Williams505682c2014-05-20 08:01:37 +00007562 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007563 vsi_idx = i; /* Found one! */
7564 } else {
7565 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007566 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007567 }
7568 pf->next_vsi = ++i;
7569
7570 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7571 if (!vsi) {
7572 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007573 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007574 }
7575 vsi->type = type;
7576 vsi->back = pf;
7577 set_bit(__I40E_DOWN, &vsi->state);
7578 vsi->flags = 0;
7579 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007580 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007581 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7582 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007583 vsi->netdev_registered = false;
7584 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007585 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007586 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007587
Alexander Duyck9f65e152013-09-28 06:00:58 +00007588 ret = i40e_set_num_rings_in_vsi(vsi);
7589 if (ret)
7590 goto err_rings;
7591
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007592 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007593 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007594 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007596 /* Setup default MSIX irq handler for VSI */
7597 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7598
Kiran Patil21659032015-09-30 14:09:03 -04007599 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007600 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007601 pf->vsi[vsi_idx] = vsi;
7602 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007603 goto unlock_pf;
7604
Alexander Duyck9f65e152013-09-28 06:00:58 +00007605err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007606 pf->next_vsi = i - 1;
7607 kfree(vsi);
7608unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007609 mutex_unlock(&pf->switch_mutex);
7610 return ret;
7611}
7612
7613/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007614 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7615 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007616 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007617 *
7618 * On error: returns error code (negative)
7619 * On success: returns 0
7620 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007621static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007622{
7623 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007624 if (free_qvectors) {
7625 kfree(vsi->q_vectors);
7626 vsi->q_vectors = NULL;
7627 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007628 kfree(vsi->tx_rings);
7629 vsi->tx_rings = NULL;
7630 vsi->rx_rings = NULL;
7631}
7632
7633/**
Helin Zhang28c58692015-10-26 19:44:27 -04007634 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7635 * and lookup table
7636 * @vsi: Pointer to VSI structure
7637 */
7638static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7639{
7640 if (!vsi)
7641 return;
7642
7643 kfree(vsi->rss_hkey_user);
7644 vsi->rss_hkey_user = NULL;
7645
7646 kfree(vsi->rss_lut_user);
7647 vsi->rss_lut_user = NULL;
7648}
7649
7650/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007651 * i40e_vsi_clear - Deallocate the VSI provided
7652 * @vsi: the VSI being un-configured
7653 **/
7654static int i40e_vsi_clear(struct i40e_vsi *vsi)
7655{
7656 struct i40e_pf *pf;
7657
7658 if (!vsi)
7659 return 0;
7660
7661 if (!vsi->back)
7662 goto free_vsi;
7663 pf = vsi->back;
7664
7665 mutex_lock(&pf->switch_mutex);
7666 if (!pf->vsi[vsi->idx]) {
7667 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7668 vsi->idx, vsi->idx, vsi, vsi->type);
7669 goto unlock_vsi;
7670 }
7671
7672 if (pf->vsi[vsi->idx] != vsi) {
7673 dev_err(&pf->pdev->dev,
7674 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7675 pf->vsi[vsi->idx]->idx,
7676 pf->vsi[vsi->idx],
7677 pf->vsi[vsi->idx]->type,
7678 vsi->idx, vsi, vsi->type);
7679 goto unlock_vsi;
7680 }
7681
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007682 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007683 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7684 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7685
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007686 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007687 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007689 pf->vsi[vsi->idx] = NULL;
7690 if (vsi->idx < pf->next_vsi)
7691 pf->next_vsi = vsi->idx;
7692
7693unlock_vsi:
7694 mutex_unlock(&pf->switch_mutex);
7695free_vsi:
7696 kfree(vsi);
7697
7698 return 0;
7699}
7700
7701/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007702 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7703 * @vsi: the VSI being cleaned
7704 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007705static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007706{
7707 int i;
7708
Greg Rose8e9dca52013-12-18 13:45:53 +00007709 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007710 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007711 kfree_rcu(vsi->tx_rings[i], rcu);
7712 vsi->tx_rings[i] = NULL;
7713 vsi->rx_rings[i] = NULL;
7714 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007715 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007716}
7717
7718/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007719 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7720 * @vsi: the VSI being configured
7721 **/
7722static int i40e_alloc_rings(struct i40e_vsi *vsi)
7723{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007724 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007725 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007726 int i;
7727
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007728 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007729 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007730 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007731 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7732 if (!tx_ring)
7733 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007734
7735 tx_ring->queue_index = i;
7736 tx_ring->reg_idx = vsi->base_queue + i;
7737 tx_ring->ring_active = false;
7738 tx_ring->vsi = vsi;
7739 tx_ring->netdev = vsi->netdev;
7740 tx_ring->dev = &pf->pdev->dev;
7741 tx_ring->count = vsi->num_desc;
7742 tx_ring->size = 0;
7743 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007744 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7745 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007746 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007747 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748
Alexander Duyck9f65e152013-09-28 06:00:58 +00007749 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007750 rx_ring->queue_index = i;
7751 rx_ring->reg_idx = vsi->base_queue + i;
7752 rx_ring->ring_active = false;
7753 rx_ring->vsi = vsi;
7754 rx_ring->netdev = vsi->netdev;
7755 rx_ring->dev = &pf->pdev->dev;
7756 rx_ring->count = vsi->num_desc;
7757 rx_ring->size = 0;
7758 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007759 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007760 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007761 }
7762
7763 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007764
7765err_out:
7766 i40e_vsi_clear_rings(vsi);
7767 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007768}
7769
7770/**
7771 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7772 * @pf: board private structure
7773 * @vectors: the number of MSI-X vectors to request
7774 *
7775 * Returns the number of vectors reserved, or error
7776 **/
7777static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7778{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007779 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7780 I40E_MIN_MSIX, vectors);
7781 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007782 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007783 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007784 vectors = 0;
7785 }
7786
7787 return vectors;
7788}
7789
7790/**
7791 * i40e_init_msix - Setup the MSIX capability
7792 * @pf: board private structure
7793 *
7794 * Work with the OS to set up the MSIX vectors needed.
7795 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007796 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007797 **/
7798static int i40e_init_msix(struct i40e_pf *pf)
7799{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007800 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007801 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007802 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007804 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007805 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007806
7807 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7808 return -ENODEV;
7809
7810 /* The number of vectors we'll request will be comprised of:
7811 * - Add 1 for "other" cause for Admin Queue events, etc.
7812 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007813 * - Queues being used for RSS.
7814 * We don't need as many as max_rss_size vectors.
7815 * use rss_size instead in the calculation since that
7816 * is governed by number of cpus in the system.
7817 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007818 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007819 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007820 * Once we count this up, try the request.
7821 *
7822 * If we can't get what we want, we'll simplify to nearly nothing
7823 * and try again. If that still fails, we punt.
7824 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007825 vectors_left = hw->func_caps.num_msix_vectors;
7826 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007827
Shannon Nelson1e200e42015-02-27 09:15:24 +00007828 /* reserve one vector for miscellaneous handler */
7829 if (vectors_left) {
7830 v_budget++;
7831 vectors_left--;
7832 }
7833
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007834 /* reserve some vectors for the main PF traffic queues. Initially we
7835 * only reserve at most 50% of the available vectors, in the case that
7836 * the number of online CPUs is large. This ensures that we can enable
7837 * extra features as well. Once we've enabled the other features, we
7838 * will use any remaining vectors to reach as close as we can to the
7839 * number of online CPUs.
7840 */
7841 cpus = num_online_cpus();
7842 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007843 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007844
7845 /* reserve one vector for sideband flow director */
7846 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7847 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007848 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007849 v_budget++;
7850 vectors_left--;
7851 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007852 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007853 }
7854 }
John W Linville83840e42015-01-14 03:06:28 +00007855
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007856 /* can we reserve enough for iWARP? */
7857 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007858 iwarp_requested = pf->num_iwarp_msix;
7859
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007860 if (!vectors_left)
7861 pf->num_iwarp_msix = 0;
7862 else if (vectors_left < pf->num_iwarp_msix)
7863 pf->num_iwarp_msix = 1;
7864 v_budget += pf->num_iwarp_msix;
7865 vectors_left -= pf->num_iwarp_msix;
7866 }
7867
Shannon Nelson1e200e42015-02-27 09:15:24 +00007868 /* any vectors left over go for VMDq support */
7869 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7870 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7871 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7872
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007873 if (!vectors_left) {
7874 pf->num_vmdq_msix = 0;
7875 pf->num_vmdq_qps = 0;
7876 } else {
7877 /* if we're short on vectors for what's desired, we limit
7878 * the queues per vmdq. If this is still more than are
7879 * available, the user will need to change the number of
7880 * queues/vectors used by the PF later with the ethtool
7881 * channels command
7882 */
7883 if (vmdq_vecs < vmdq_vecs_wanted)
7884 pf->num_vmdq_qps = 1;
7885 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007886
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007887 v_budget += vmdq_vecs;
7888 vectors_left -= vmdq_vecs;
7889 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007890 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007891
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007892 /* On systems with a large number of SMP cores, we previously limited
7893 * the number of vectors for num_lan_msix to be at most 50% of the
7894 * available vectors, to allow for other features. Now, we add back
7895 * the remaining vectors. However, we ensure that the total
7896 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7897 * calculate the number of vectors we can add without going over the
7898 * cap of CPUs. For systems with a small number of CPUs this will be
7899 * zero.
7900 */
7901 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7902 pf->num_lan_msix += extra_vectors;
7903 vectors_left -= extra_vectors;
7904
7905 WARN(vectors_left < 0,
7906 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7907
7908 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007909 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7910 GFP_KERNEL);
7911 if (!pf->msix_entries)
7912 return -ENOMEM;
7913
7914 for (i = 0; i < v_budget; i++)
7915 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007916 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007917
Shannon Nelson3b444392015-02-26 16:15:57 +00007918 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007919 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7920 kfree(pf->msix_entries);
7921 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007922 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007923 return -ENODEV;
7924
Shannon Nelson3b444392015-02-26 16:15:57 +00007925 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007926 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007927 pf->num_vmdq_vsis = 0;
7928 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007929 pf->num_lan_qps = 1;
7930 pf->num_lan_msix = 1;
7931
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007932 } else if (!vectors_left) {
7933 /* If we have limited resources, we will start with no vectors
7934 * for the special features and then allocate vectors to some
7935 * of these features based on the policy and at the end disable
7936 * the features that did not get any vectors.
7937 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007938 int vec;
7939
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007940 dev_info(&pf->pdev->dev,
7941 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007942 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007943 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007944
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007945 /* Scale vector usage down */
7946 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007947 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007948 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007949
7950 /* partition out the remaining vectors */
7951 switch (vec) {
7952 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007953 pf->num_lan_msix = 1;
7954 break;
7955 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007956 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7957 pf->num_lan_msix = 1;
7958 pf->num_iwarp_msix = 1;
7959 } else {
7960 pf->num_lan_msix = 2;
7961 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007962 break;
7963 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007964 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7965 pf->num_iwarp_msix = min_t(int, (vec / 3),
7966 iwarp_requested);
7967 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7968 I40E_DEFAULT_NUM_VMDQ_VSI);
7969 } else {
7970 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7971 I40E_DEFAULT_NUM_VMDQ_VSI);
7972 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007973 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7974 pf->num_fdsb_msix = 1;
7975 vec--;
7976 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007977 pf->num_lan_msix = min_t(int,
7978 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7979 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007980 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981 break;
7982 }
7983 }
7984
Stefan Assmannabd97a92016-09-19 13:37:51 +02007985 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7986 (pf->num_fdsb_msix == 0)) {
7987 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7988 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7989 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007990 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7991 (pf->num_vmdq_msix == 0)) {
7992 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7993 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7994 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007995
7996 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7997 (pf->num_iwarp_msix == 0)) {
7998 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7999 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8000 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008001 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8002 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8003 pf->num_lan_msix,
8004 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8005 pf->num_fdsb_msix,
8006 pf->num_iwarp_msix);
8007
Shannon Nelson3b444392015-02-26 16:15:57 +00008008 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008009}
8010
8011/**
Greg Rose90e04072014-03-06 08:59:57 +00008012 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008013 * @vsi: the VSI being configured
8014 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008015 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008016 *
8017 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8018 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008019static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008020{
8021 struct i40e_q_vector *q_vector;
8022
8023 /* allocate q_vector */
8024 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8025 if (!q_vector)
8026 return -ENOMEM;
8027
8028 q_vector->vsi = vsi;
8029 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008030 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
8031
Alexander Duyck493fb302013-09-28 07:01:44 +00008032 if (vsi->netdev)
8033 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008034 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008035
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008036 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8037 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8038
Alexander Duyck493fb302013-09-28 07:01:44 +00008039 /* tie q_vector and vsi together */
8040 vsi->q_vectors[v_idx] = q_vector;
8041
8042 return 0;
8043}
8044
8045/**
Greg Rose90e04072014-03-06 08:59:57 +00008046 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008047 * @vsi: the VSI being configured
8048 *
8049 * We allocate one q_vector per queue interrupt. If allocation fails we
8050 * return -ENOMEM.
8051 **/
Greg Rose90e04072014-03-06 08:59:57 +00008052static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008053{
8054 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008055 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008056
8057 /* if not MSIX, give the one vector only to the LAN VSI */
8058 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8059 num_q_vectors = vsi->num_q_vectors;
8060 else if (vsi == pf->vsi[pf->lan_vsi])
8061 num_q_vectors = 1;
8062 else
8063 return -EINVAL;
8064
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008065 current_cpu = cpumask_first(cpu_online_mask);
8066
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008067 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008068 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008069 if (err)
8070 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008071 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8072 if (unlikely(current_cpu >= nr_cpu_ids))
8073 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008074 }
8075
8076 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008077
8078err_out:
8079 while (v_idx--)
8080 i40e_free_q_vector(vsi, v_idx);
8081
8082 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008083}
8084
8085/**
8086 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8087 * @pf: board private structure to initialize
8088 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008089static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008090{
Shannon Nelson3b444392015-02-26 16:15:57 +00008091 int vectors = 0;
8092 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008093
8094 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008095 vectors = i40e_init_msix(pf);
8096 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008097 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008098 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008099 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008100 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008101 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008102 I40E_FLAG_SRIOV_ENABLED |
8103 I40E_FLAG_FD_SB_ENABLED |
8104 I40E_FLAG_FD_ATR_ENABLED |
8105 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008106
8107 /* rework the queue expectations without MSIX */
8108 i40e_determine_queue_usage(pf);
8109 }
8110 }
8111
8112 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8113 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008114 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008115 vectors = pci_enable_msi(pf->pdev);
8116 if (vectors < 0) {
8117 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8118 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008119 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8120 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008121 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008122 }
8123
Shannon Nelson958a3e32013-09-28 07:13:28 +00008124 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008125 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008126
Shannon Nelson3b444392015-02-26 16:15:57 +00008127 /* set up vector assignment tracking */
8128 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8129 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008130 if (!pf->irq_pile) {
8131 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8132 return -ENOMEM;
8133 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008134 pf->irq_pile->num_entries = vectors;
8135 pf->irq_pile->search_hint = 0;
8136
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008137 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008138 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008139
8140 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008141}
8142
8143/**
8144 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8145 * @pf: board private structure
8146 *
8147 * This sets up the handler for MSIX 0, which is used to manage the
8148 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8149 * when in MSI or Legacy interrupt mode.
8150 **/
8151static int i40e_setup_misc_vector(struct i40e_pf *pf)
8152{
8153 struct i40e_hw *hw = &pf->hw;
8154 int err = 0;
8155
8156 /* Only request the irq if this is the first time through, and
8157 * not when we're rebuilding after a Reset
8158 */
8159 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8160 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008161 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008162 if (err) {
8163 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008164 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008165 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008166 return -EFAULT;
8167 }
8168 }
8169
Jacob Kellerab437b52014-12-14 01:55:08 +00008170 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008171
8172 /* associate no queues to the misc vector */
8173 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8174 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8175
8176 i40e_flush(hw);
8177
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008178 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008179
8180 return err;
8181}
8182
8183/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008184 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8185 * @vsi: vsi structure
8186 * @seed: RSS hash seed
8187 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008188static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8189 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008190{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008191 struct i40e_pf *pf = vsi->back;
8192 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008193 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008194
Jacob Keller776b2e12016-07-19 16:23:30 -07008195 if (seed) {
8196 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8197 (struct i40e_aqc_get_set_rss_key_data *)seed;
8198 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8199 if (ret) {
8200 dev_info(&pf->pdev->dev,
8201 "Cannot set RSS key, err %s aq_err %s\n",
8202 i40e_stat_str(hw, ret),
8203 i40e_aq_str(hw, hw->aq.asq_last_status));
8204 return ret;
8205 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008206 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008207 if (lut) {
8208 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008209
Jacob Keller776b2e12016-07-19 16:23:30 -07008210 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8211 if (ret) {
8212 dev_info(&pf->pdev->dev,
8213 "Cannot set RSS lut, err %s aq_err %s\n",
8214 i40e_stat_str(hw, ret),
8215 i40e_aq_str(hw, hw->aq.asq_last_status));
8216 return ret;
8217 }
8218 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008219 return ret;
8220}
8221
8222/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008223 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8224 * @vsi: Pointer to vsi structure
8225 * @seed: Buffter to store the hash keys
8226 * @lut: Buffer to store the lookup table entries
8227 * @lut_size: Size of buffer to store the lookup table entries
8228 *
8229 * Return 0 on success, negative on failure
8230 */
8231static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8232 u8 *lut, u16 lut_size)
8233{
8234 struct i40e_pf *pf = vsi->back;
8235 struct i40e_hw *hw = &pf->hw;
8236 int ret = 0;
8237
8238 if (seed) {
8239 ret = i40e_aq_get_rss_key(hw, vsi->id,
8240 (struct i40e_aqc_get_set_rss_key_data *)seed);
8241 if (ret) {
8242 dev_info(&pf->pdev->dev,
8243 "Cannot get RSS key, err %s aq_err %s\n",
8244 i40e_stat_str(&pf->hw, ret),
8245 i40e_aq_str(&pf->hw,
8246 pf->hw.aq.asq_last_status));
8247 return ret;
8248 }
8249 }
8250
8251 if (lut) {
8252 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8253
8254 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8255 if (ret) {
8256 dev_info(&pf->pdev->dev,
8257 "Cannot get RSS lut, err %s aq_err %s\n",
8258 i40e_stat_str(&pf->hw, ret),
8259 i40e_aq_str(&pf->hw,
8260 pf->hw.aq.asq_last_status));
8261 return ret;
8262 }
8263 }
8264
8265 return ret;
8266}
8267
8268/**
Jacob Keller0582b962016-07-19 16:23:29 -07008269 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8270 * @vsi: VSI structure
8271 **/
8272static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8273{
8274 u8 seed[I40E_HKEY_ARRAY_SIZE];
8275 struct i40e_pf *pf = vsi->back;
8276 u8 *lut;
8277 int ret;
8278
8279 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8280 return 0;
8281
Jacob Keller552b9962016-07-19 16:23:31 -07008282 if (!vsi->rss_size)
8283 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8284 vsi->num_queue_pairs);
8285 if (!vsi->rss_size)
8286 return -EINVAL;
8287
Jacob Keller0582b962016-07-19 16:23:29 -07008288 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8289 if (!lut)
8290 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008291 /* Use the user configured hash keys and lookup table if there is one,
8292 * otherwise use default
8293 */
8294 if (vsi->rss_lut_user)
8295 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8296 else
8297 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8298 if (vsi->rss_hkey_user)
8299 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8300 else
8301 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008302 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8303 kfree(lut);
8304
8305 return ret;
8306}
8307
8308/**
Helin Zhang043dd652015-10-21 19:56:23 -04008309 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008310 * @vsi: Pointer to vsi structure
8311 * @seed: RSS hash seed
8312 * @lut: Lookup table
8313 * @lut_size: Lookup table size
8314 *
8315 * Returns 0 on success, negative on failure
8316 **/
8317static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8318 const u8 *lut, u16 lut_size)
8319{
8320 struct i40e_pf *pf = vsi->back;
8321 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008322 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008323 u8 i;
8324
8325 /* Fill out hash function seed */
8326 if (seed) {
8327 u32 *seed_dw = (u32 *)seed;
8328
Mitch Williamsc4e18682016-04-12 08:30:40 -07008329 if (vsi->type == I40E_VSI_MAIN) {
8330 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008331 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008332 } else if (vsi->type == I40E_VSI_SRIOV) {
8333 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008334 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008335 } else {
8336 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8337 }
Helin Zhange69ff812015-10-21 19:56:22 -04008338 }
8339
8340 if (lut) {
8341 u32 *lut_dw = (u32 *)lut;
8342
Mitch Williamsc4e18682016-04-12 08:30:40 -07008343 if (vsi->type == I40E_VSI_MAIN) {
8344 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8345 return -EINVAL;
8346 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8347 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8348 } else if (vsi->type == I40E_VSI_SRIOV) {
8349 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8350 return -EINVAL;
8351 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008352 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008353 } else {
8354 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8355 }
Helin Zhange69ff812015-10-21 19:56:22 -04008356 }
8357 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008358
8359 return 0;
8360}
8361
8362/**
Helin Zhang043dd652015-10-21 19:56:23 -04008363 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8364 * @vsi: Pointer to VSI structure
8365 * @seed: Buffer to store the keys
8366 * @lut: Buffer to store the lookup table entries
8367 * @lut_size: Size of buffer to store the lookup table entries
8368 *
8369 * Returns 0 on success, negative on failure
8370 */
8371static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8372 u8 *lut, u16 lut_size)
8373{
8374 struct i40e_pf *pf = vsi->back;
8375 struct i40e_hw *hw = &pf->hw;
8376 u16 i;
8377
8378 if (seed) {
8379 u32 *seed_dw = (u32 *)seed;
8380
8381 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008382 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008383 }
8384 if (lut) {
8385 u32 *lut_dw = (u32 *)lut;
8386
8387 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8388 return -EINVAL;
8389 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8390 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8391 }
8392
8393 return 0;
8394}
8395
8396/**
8397 * i40e_config_rss - Configure RSS keys and lut
8398 * @vsi: Pointer to VSI structure
8399 * @seed: RSS hash seed
8400 * @lut: Lookup table
8401 * @lut_size: Lookup table size
8402 *
8403 * Returns 0 on success, negative on failure
8404 */
8405int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8406{
8407 struct i40e_pf *pf = vsi->back;
8408
8409 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8410 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8411 else
8412 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8413}
8414
8415/**
8416 * i40e_get_rss - Get RSS keys and lut
8417 * @vsi: Pointer to VSI structure
8418 * @seed: Buffer to store the keys
8419 * @lut: Buffer to store the lookup table entries
8420 * lut_size: Size of buffer to store the lookup table entries
8421 *
8422 * Returns 0 on success, negative on failure
8423 */
8424int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8425{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008426 struct i40e_pf *pf = vsi->back;
8427
8428 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8429 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8430 else
8431 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008432}
8433
8434/**
Helin Zhange69ff812015-10-21 19:56:22 -04008435 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8436 * @pf: Pointer to board private structure
8437 * @lut: Lookup table
8438 * @rss_table_size: Lookup table size
8439 * @rss_size: Range of queue number for hashing
8440 */
Alan Bradyf1582352016-08-24 11:33:46 -07008441void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8442 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008443{
Helin Zhange69ff812015-10-21 19:56:22 -04008444 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008445
Helin Zhange69ff812015-10-21 19:56:22 -04008446 for (i = 0; i < rss_table_size; i++)
8447 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008448}
8449
8450/**
Helin Zhang043dd652015-10-21 19:56:23 -04008451 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008452 * @pf: board private structure
8453 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008454static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008455{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008456 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008457 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008458 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008459 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008460 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008461 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008462 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008463
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008464 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008465 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8466 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008467 hena |= i40e_pf_get_default_rss_hena(pf);
8468
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008469 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8470 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008471
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008472 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008473 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008474 reg_val = (pf->rss_table_size == 512) ?
8475 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8476 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008477 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008478
Helin Zhang28c58692015-10-26 19:44:27 -04008479 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008480 if (!vsi->rss_size) {
8481 u16 qcount;
8482
8483 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8484 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8485 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008486 if (!vsi->rss_size)
8487 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008488
Helin Zhange69ff812015-10-21 19:56:22 -04008489 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8490 if (!lut)
8491 return -ENOMEM;
8492
Helin Zhang28c58692015-10-26 19:44:27 -04008493 /* Use user configured lut if there is one, otherwise use default */
8494 if (vsi->rss_lut_user)
8495 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8496 else
8497 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008498
Helin Zhang28c58692015-10-26 19:44:27 -04008499 /* Use user configured hash key if there is one, otherwise
8500 * use default.
8501 */
8502 if (vsi->rss_hkey_user)
8503 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8504 else
8505 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008506 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008507 kfree(lut);
8508
8509 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008510}
8511
8512/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008513 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8514 * @pf: board private structure
8515 * @queue_count: the requested queue count for rss.
8516 *
8517 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8518 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008519 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008520 **/
8521int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8522{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008523 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8524 int new_rss_size;
8525
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008526 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8527 return 0;
8528
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008529 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008530
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008531 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008532 u16 qcount;
8533
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008534 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008535 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008536
Helin Zhangacd65442015-10-26 19:44:28 -04008537 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008538
Maciej Sosin373149f2017-04-05 07:50:55 -04008539 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008540
8541 /* Discard the user configured hash keys and lut, if less
8542 * queues are enabled.
8543 */
8544 if (queue_count < vsi->rss_size) {
8545 i40e_clear_rss_config_user(vsi);
8546 dev_dbg(&pf->pdev->dev,
8547 "discard user configured hash keys and lut\n");
8548 }
8549
8550 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008551 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8552 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008553
Helin Zhang043dd652015-10-21 19:56:23 -04008554 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008555 }
Lihong Yang12815052016-09-27 11:28:48 -07008556 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8557 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008558 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008559}
8560
8561/**
Greg Rosef4492db2015-02-06 08:52:12 +00008562 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8563 * @pf: board private structure
8564 **/
8565i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8566{
8567 i40e_status status;
8568 bool min_valid, max_valid;
8569 u32 max_bw, min_bw;
8570
8571 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8572 &min_valid, &max_valid);
8573
8574 if (!status) {
8575 if (min_valid)
8576 pf->npar_min_bw = min_bw;
8577 if (max_valid)
8578 pf->npar_max_bw = max_bw;
8579 }
8580
8581 return status;
8582}
8583
8584/**
8585 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8586 * @pf: board private structure
8587 **/
8588i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8589{
8590 struct i40e_aqc_configure_partition_bw_data bw_data;
8591 i40e_status status;
8592
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008593 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008594 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008595 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8596 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8597
8598 /* Set the new bandwidths */
8599 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8600
8601 return status;
8602}
8603
8604/**
8605 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8606 * @pf: board private structure
8607 **/
8608i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8609{
8610 /* Commit temporary BW setting to permanent NVM image */
8611 enum i40e_admin_queue_err last_aq_status;
8612 i40e_status ret;
8613 u16 nvm_word;
8614
8615 if (pf->hw.partition_id != 1) {
8616 dev_info(&pf->pdev->dev,
8617 "Commit BW only works on partition 1! This is partition %d",
8618 pf->hw.partition_id);
8619 ret = I40E_NOT_SUPPORTED;
8620 goto bw_commit_out;
8621 }
8622
8623 /* Acquire NVM for read access */
8624 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8625 last_aq_status = pf->hw.aq.asq_last_status;
8626 if (ret) {
8627 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008628 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8629 i40e_stat_str(&pf->hw, ret),
8630 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008631 goto bw_commit_out;
8632 }
8633
8634 /* Read word 0x10 of NVM - SW compatibility word 1 */
8635 ret = i40e_aq_read_nvm(&pf->hw,
8636 I40E_SR_NVM_CONTROL_WORD,
8637 0x10, sizeof(nvm_word), &nvm_word,
8638 false, NULL);
8639 /* Save off last admin queue command status before releasing
8640 * the NVM
8641 */
8642 last_aq_status = pf->hw.aq.asq_last_status;
8643 i40e_release_nvm(&pf->hw);
8644 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008645 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8646 i40e_stat_str(&pf->hw, ret),
8647 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008648 goto bw_commit_out;
8649 }
8650
8651 /* Wait a bit for NVM release to complete */
8652 msleep(50);
8653
8654 /* Acquire NVM for write access */
8655 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8656 last_aq_status = pf->hw.aq.asq_last_status;
8657 if (ret) {
8658 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008659 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8660 i40e_stat_str(&pf->hw, ret),
8661 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008662 goto bw_commit_out;
8663 }
8664 /* Write it back out unchanged to initiate update NVM,
8665 * which will force a write of the shadow (alt) RAM to
8666 * the NVM - thus storing the bandwidth values permanently.
8667 */
8668 ret = i40e_aq_update_nvm(&pf->hw,
8669 I40E_SR_NVM_CONTROL_WORD,
8670 0x10, sizeof(nvm_word),
8671 &nvm_word, true, NULL);
8672 /* Save off last admin queue command status before releasing
8673 * the NVM
8674 */
8675 last_aq_status = pf->hw.aq.asq_last_status;
8676 i40e_release_nvm(&pf->hw);
8677 if (ret)
8678 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008679 "BW settings NOT SAVED, err %s aq_err %s\n",
8680 i40e_stat_str(&pf->hw, ret),
8681 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008682bw_commit_out:
8683
8684 return ret;
8685}
8686
8687/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008688 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8689 * @pf: board private structure to initialize
8690 *
8691 * i40e_sw_init initializes the Adapter private data structure.
8692 * Fields are initialized based on PCI device information and
8693 * OS network device settings (MTU size).
8694 **/
8695static int i40e_sw_init(struct i40e_pf *pf)
8696{
8697 int err = 0;
8698 int size;
8699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008700 /* Set default capability flags */
8701 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8702 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008703 I40E_FLAG_MSIX_ENABLED;
8704
Mitch Williamsca99eb92014-04-04 04:43:07 +00008705 /* Set default ITR */
8706 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8707 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8708
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008709 /* Depending on PF configurations, it is possible that the RSS
8710 * maximum might end up larger than the available queues
8711 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008712 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008713 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008714 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008715 pf->rss_size_max = min_t(int, pf->rss_size_max,
8716 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008717 if (pf->hw.func_caps.rss) {
8718 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008719 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8720 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008721 }
8722
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008723 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008724 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008725 pf->flags |= I40E_FLAG_MFP_ENABLED;
8726 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008727 if (i40e_get_npar_bw_setting(pf))
8728 dev_warn(&pf->pdev->dev,
8729 "Could not get NPAR bw settings\n");
8730 else
8731 dev_info(&pf->pdev->dev,
8732 "Min BW = %8.8x, Max BW = %8.8x\n",
8733 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008734 }
8735
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008736 /* FW/NVM is not yet fixed in this regard */
8737 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8738 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8739 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8740 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008741 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8742 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008743 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008744 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008745 else
8746 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008747 pf->fdir_pf_filter_count =
8748 pf->hw.func_caps.fd_filters_guaranteed;
8749 pf->hw.fdir_shared_filter_count =
8750 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008751 }
8752
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008753 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008754 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008755 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008756 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008757 /* No DCB support for FW < v4.33 */
8758 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8759 }
8760
8761 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008762 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008763 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8764 (pf->hw.aq.fw_maj_ver < 4)))
8765 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8766
8767 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008768 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008769 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8770 (pf->hw.aq.fw_maj_ver >= 5)))
8771 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008772
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008773 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008774 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008775 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008776 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008777 }
8778
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008779 if (pf->hw.func_caps.iwarp) {
8780 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8781 /* IWARP needs one extra vector for CQP just like MISC.*/
8782 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8783 }
8784
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008785#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008786 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008787 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8788 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8789 pf->num_req_vfs = min_t(int,
8790 pf->hw.func_caps.num_vfs,
8791 I40E_MAX_VF_COUNT);
8792 }
8793#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008794 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008795 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8796 | I40E_FLAG_128_QP_RSS_CAPABLE
8797 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8798 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8799 | I40E_FLAG_WB_ON_ITR_CAPABLE
8800 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8801 | I40E_FLAG_NO_PCI_LINK_CHECK
8802 | I40E_FLAG_USE_SET_LLDP_MIB
8803 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8804 | I40E_FLAG_PTP_L4_CAPABLE
8805 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008806 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8807 ((pf->hw.aq.api_maj_ver == 1) &&
8808 (pf->hw.aq.api_min_ver > 4))) {
8809 /* Supported in FW API version higher than 1.4 */
8810 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008811 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008812 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008813 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008814 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008815
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008816 pf->eeprom_version = 0xDEAD;
8817 pf->lan_veb = I40E_NO_VEB;
8818 pf->lan_vsi = I40E_NO_VSI;
8819
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008820 /* By default FW has this off for performance reasons */
8821 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8822
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008823 /* set up queue assignment tracking */
8824 size = sizeof(struct i40e_lump_tracking)
8825 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8826 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8827 if (!pf->qp_pile) {
8828 err = -ENOMEM;
8829 goto sw_init_done;
8830 }
8831 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8832 pf->qp_pile->search_hint = 0;
8833
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008834 pf->tx_timeout_recovery_level = 1;
8835
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008836 mutex_init(&pf->switch_mutex);
8837
Greg Rosec668a122015-02-26 16:10:39 +00008838 /* If NPAR is enabled nudge the Tx scheduler */
8839 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8840 i40e_set_npar_bw_setting(pf);
8841
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008842sw_init_done:
8843 return err;
8844}
8845
8846/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008847 * i40e_set_ntuple - set the ntuple feature flag and take action
8848 * @pf: board private structure to initialize
8849 * @features: the feature set that the stack is suggesting
8850 *
8851 * returns a bool to indicate if reset needs to happen
8852 **/
8853bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8854{
8855 bool need_reset = false;
8856
8857 /* Check if Flow Director n-tuple support was enabled or disabled. If
8858 * the state changed, we need to reset.
8859 */
8860 if (features & NETIF_F_NTUPLE) {
8861 /* Enable filters and mark for reset */
8862 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8863 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008864 /* enable FD_SB only if there is MSI-X vector */
8865 if (pf->num_fdsb_msix > 0)
8866 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008867 } else {
8868 /* turn off filters, mark for reset and clear SW filter list */
8869 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8870 need_reset = true;
8871 i40e_fdir_filter_exit(pf);
8872 }
8873 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008874 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008875 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008876 pf->fd_add_err = 0;
8877 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008878 /* if ATR was auto disabled it can be re-enabled. */
8879 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008880 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8881 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008882 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8883 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8884 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008885 }
8886 return need_reset;
8887}
8888
8889/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008890 * i40e_clear_rss_lut - clear the rx hash lookup table
8891 * @vsi: the VSI being configured
8892 **/
8893static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8894{
8895 struct i40e_pf *pf = vsi->back;
8896 struct i40e_hw *hw = &pf->hw;
8897 u16 vf_id = vsi->vf_id;
8898 u8 i;
8899
8900 if (vsi->type == I40E_VSI_MAIN) {
8901 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8902 wr32(hw, I40E_PFQF_HLUT(i), 0);
8903 } else if (vsi->type == I40E_VSI_SRIOV) {
8904 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8905 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8906 } else {
8907 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8908 }
8909}
8910
8911/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008912 * i40e_set_features - set the netdev feature flags
8913 * @netdev: ptr to the netdev being adjusted
8914 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04008915 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008916 **/
8917static int i40e_set_features(struct net_device *netdev,
8918 netdev_features_t features)
8919{
8920 struct i40e_netdev_priv *np = netdev_priv(netdev);
8921 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008922 struct i40e_pf *pf = vsi->back;
8923 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008924
Alan Bradyd8ec9862016-07-27 12:02:38 -07008925 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8926 i40e_pf_config_rss(pf);
8927 else if (!(features & NETIF_F_RXHASH) &&
8928 netdev->features & NETIF_F_RXHASH)
8929 i40e_clear_rss_lut(vsi);
8930
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008931 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8932 i40e_vlan_stripping_enable(vsi);
8933 else
8934 i40e_vlan_stripping_disable(vsi);
8935
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008936 need_reset = i40e_set_ntuple(pf, features);
8937
8938 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04008939 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008941 return 0;
8942}
8943
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008944/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008945 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008946 * @pf: board private structure
8947 * @port: The UDP port to look up
8948 *
8949 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8950 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008951static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008952{
8953 u8 i;
8954
8955 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Jacob Keller27826fd2017-04-19 09:25:50 -04008956 if (pf->udp_ports[i].port == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008957 return i;
8958 }
8959
8960 return i;
8961}
8962
8963/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008964 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008965 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008966 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008967 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008968static void i40e_udp_tunnel_add(struct net_device *netdev,
8969 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008970{
8971 struct i40e_netdev_priv *np = netdev_priv(netdev);
8972 struct i40e_vsi *vsi = np->vsi;
8973 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008974 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008975 u8 next_idx;
8976 u8 idx;
8977
Singhai, Anjali6a899022015-12-14 12:21:18 -08008978 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008979
8980 /* Check if port already exists */
8981 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008982 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008983 return;
8984 }
8985
8986 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008987 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008988
8989 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008990 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008991 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008992 return;
8993 }
8994
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008995 switch (ti->type) {
8996 case UDP_TUNNEL_TYPE_VXLAN:
8997 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8998 break;
8999 case UDP_TUNNEL_TYPE_GENEVE:
9000 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
9001 return;
9002 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9003 break;
9004 default:
9005 return;
9006 }
9007
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009008 /* New port: add it and mark its index in the bitmap */
Jacob Keller27826fd2017-04-19 09:25:50 -04009009 pf->udp_ports[next_idx].port = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009010 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9011 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009012}
9013
9014/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009015 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009016 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009017 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009018 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009019static void i40e_udp_tunnel_del(struct net_device *netdev,
9020 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009021{
9022 struct i40e_netdev_priv *np = netdev_priv(netdev);
9023 struct i40e_vsi *vsi = np->vsi;
9024 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009025 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009026 u8 idx;
9027
Singhai, Anjali6a899022015-12-14 12:21:18 -08009028 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009029
9030 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009031 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9032 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009033
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009034 switch (ti->type) {
9035 case UDP_TUNNEL_TYPE_VXLAN:
9036 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9037 goto not_found;
9038 break;
9039 case UDP_TUNNEL_TYPE_GENEVE:
9040 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9041 goto not_found;
9042 break;
9043 default:
9044 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009045 }
9046
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009047 /* if port exists, set it to 0 (mark for deletion)
9048 * and make it pending
9049 */
Jacob Keller27826fd2017-04-19 09:25:50 -04009050 pf->udp_ports[idx].port = 0;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009051 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009052 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9053
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009054 return;
9055not_found:
9056 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009057 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009058}
9059
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009060static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009061 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009062{
9063 struct i40e_netdev_priv *np = netdev_priv(netdev);
9064 struct i40e_pf *pf = np->vsi->back;
9065 struct i40e_hw *hw = &pf->hw;
9066
9067 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9068 return -EOPNOTSUPP;
9069
9070 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9071 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9072
9073 return 0;
9074}
9075
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009076/**
9077 * i40e_ndo_fdb_add - add an entry to the hardware database
9078 * @ndm: the input from the stack
9079 * @tb: pointer to array of nladdr (unused)
9080 * @dev: the net device pointer
9081 * @addr: the MAC address entry being added
9082 * @flags: instructions from stack about fdb operation
9083 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009084static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9085 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009086 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009087 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009088{
9089 struct i40e_netdev_priv *np = netdev_priv(dev);
9090 struct i40e_pf *pf = np->vsi->back;
9091 int err = 0;
9092
9093 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9094 return -EOPNOTSUPP;
9095
Or Gerlitz65891fe2014-12-14 18:19:05 +02009096 if (vid) {
9097 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9098 return -EINVAL;
9099 }
9100
Greg Rose4ba0dea2014-03-06 08:59:55 +00009101 /* Hardware does not support aging addresses so if a
9102 * ndm_state is given only allow permanent addresses
9103 */
9104 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9105 netdev_info(dev, "FDB only supports static addresses\n");
9106 return -EINVAL;
9107 }
9108
9109 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9110 err = dev_uc_add_excl(dev, addr);
9111 else if (is_multicast_ether_addr(addr))
9112 err = dev_mc_add_excl(dev, addr);
9113 else
9114 err = -EINVAL;
9115
9116 /* Only return duplicate errors if NLM_F_EXCL is set */
9117 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9118 err = 0;
9119
9120 return err;
9121}
9122
Neerav Parikh51616012015-02-06 08:52:14 +00009123/**
9124 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9125 * @dev: the netdev being configured
9126 * @nlh: RTNL message
9127 *
9128 * Inserts a new hardware bridge if not already created and
9129 * enables the bridging mode requested (VEB or VEPA). If the
9130 * hardware bridge has already been inserted and the request
9131 * is to change the mode then that requires a PF reset to
9132 * allow rebuild of the components with required hardware
9133 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009134 *
9135 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009136 **/
9137static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009138 struct nlmsghdr *nlh,
9139 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009140{
9141 struct i40e_netdev_priv *np = netdev_priv(dev);
9142 struct i40e_vsi *vsi = np->vsi;
9143 struct i40e_pf *pf = vsi->back;
9144 struct i40e_veb *veb = NULL;
9145 struct nlattr *attr, *br_spec;
9146 int i, rem;
9147
9148 /* Only for PF VSI for now */
9149 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9150 return -EOPNOTSUPP;
9151
9152 /* Find the HW bridge for PF VSI */
9153 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9154 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9155 veb = pf->veb[i];
9156 }
9157
9158 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9159
9160 nla_for_each_nested(attr, br_spec, rem) {
9161 __u16 mode;
9162
9163 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9164 continue;
9165
9166 mode = nla_get_u16(attr);
9167 if ((mode != BRIDGE_MODE_VEPA) &&
9168 (mode != BRIDGE_MODE_VEB))
9169 return -EINVAL;
9170
9171 /* Insert a new HW bridge */
9172 if (!veb) {
9173 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9174 vsi->tc_config.enabled_tc);
9175 if (veb) {
9176 veb->bridge_mode = mode;
9177 i40e_config_bridge_mode(veb);
9178 } else {
9179 /* No Bridge HW offload available */
9180 return -ENOENT;
9181 }
9182 break;
9183 } else if (mode != veb->bridge_mode) {
9184 /* Existing HW bridge but different mode needs reset */
9185 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009186 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9187 if (mode == BRIDGE_MODE_VEB)
9188 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9189 else
9190 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009191 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9192 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009193 break;
9194 }
9195 }
9196
9197 return 0;
9198}
9199
9200/**
9201 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9202 * @skb: skb buff
9203 * @pid: process id
9204 * @seq: RTNL message seq #
9205 * @dev: the netdev being configured
9206 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009207 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009208 *
9209 * Return the mode in which the hardware bridge is operating in
9210 * i.e VEB or VEPA.
9211 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009212static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9213 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009214 u32 __always_unused filter_mask,
9215 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009216{
9217 struct i40e_netdev_priv *np = netdev_priv(dev);
9218 struct i40e_vsi *vsi = np->vsi;
9219 struct i40e_pf *pf = vsi->back;
9220 struct i40e_veb *veb = NULL;
9221 int i;
9222
9223 /* Only for PF VSI for now */
9224 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9225 return -EOPNOTSUPP;
9226
9227 /* Find the HW bridge for the PF VSI */
9228 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9229 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9230 veb = pf->veb[i];
9231 }
9232
9233 if (!veb)
9234 return 0;
9235
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009236 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009237 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009238}
Neerav Parikh51616012015-02-06 08:52:14 +00009239
Joe Stringerf44a75e2015-04-14 17:09:14 -07009240/**
9241 * i40e_features_check - Validate encapsulated packet conforms to limits
9242 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009243 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009244 * @features: Offload features that the stack believes apply
9245 **/
9246static netdev_features_t i40e_features_check(struct sk_buff *skb,
9247 struct net_device *dev,
9248 netdev_features_t features)
9249{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009250 size_t len;
9251
9252 /* No point in doing any of this if neither checksum nor GSO are
9253 * being requested for this frame. We can rule out both by just
9254 * checking for CHECKSUM_PARTIAL
9255 */
9256 if (skb->ip_summed != CHECKSUM_PARTIAL)
9257 return features;
9258
9259 /* We cannot support GSO if the MSS is going to be less than
9260 * 64 bytes. If it is then we need to drop support for GSO.
9261 */
9262 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9263 features &= ~NETIF_F_GSO_MASK;
9264
9265 /* MACLEN can support at most 63 words */
9266 len = skb_network_header(skb) - skb->data;
9267 if (len & ~(63 * 2))
9268 goto out_err;
9269
9270 /* IPLEN and EIPLEN can support at most 127 dwords */
9271 len = skb_transport_header(skb) - skb_network_header(skb);
9272 if (len & ~(127 * 4))
9273 goto out_err;
9274
9275 if (skb->encapsulation) {
9276 /* L4TUNLEN can support 127 words */
9277 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9278 if (len & ~(127 * 2))
9279 goto out_err;
9280
9281 /* IPLEN can support at most 127 dwords */
9282 len = skb_inner_transport_header(skb) -
9283 skb_inner_network_header(skb);
9284 if (len & ~(127 * 4))
9285 goto out_err;
9286 }
9287
9288 /* No need to validate L4LEN as TCP is the only protocol with a
9289 * a flexible value and we support all possible values supported
9290 * by TCP, which is at most 15 dwords
9291 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009292
9293 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009294out_err:
9295 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009296}
9297
Shannon Nelson37a29732015-02-27 09:15:19 +00009298static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009299 .ndo_open = i40e_open,
9300 .ndo_stop = i40e_close,
9301 .ndo_start_xmit = i40e_lan_xmit_frame,
9302 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9303 .ndo_set_rx_mode = i40e_set_rx_mode,
9304 .ndo_validate_addr = eth_validate_addr,
9305 .ndo_set_mac_address = i40e_set_mac,
9306 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009307 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009308 .ndo_tx_timeout = i40e_tx_timeout,
9309 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9310 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9311#ifdef CONFIG_NET_POLL_CONTROLLER
9312 .ndo_poll_controller = i40e_netpoll,
9313#endif
John Fastabende4c67342016-02-16 21:16:15 -08009314 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009315 .ndo_set_features = i40e_set_features,
9316 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9317 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009318 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009319 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009320 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009321 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009322 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009323 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9324 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009325 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009326 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009327 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009328 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9329 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009330};
9331
9332/**
9333 * i40e_config_netdev - Setup the netdev flags
9334 * @vsi: the VSI being configured
9335 *
9336 * Returns 0 on success, negative value on failure
9337 **/
9338static int i40e_config_netdev(struct i40e_vsi *vsi)
9339{
9340 struct i40e_pf *pf = vsi->back;
9341 struct i40e_hw *hw = &pf->hw;
9342 struct i40e_netdev_priv *np;
9343 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009344 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009345 u8 mac_addr[ETH_ALEN];
9346 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009347 netdev_features_t hw_enc_features;
9348 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009349
9350 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009351 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009352 if (!netdev)
9353 return -ENOMEM;
9354
9355 vsi->netdev = netdev;
9356 np = netdev_priv(netdev);
9357 np->vsi = vsi;
9358
Preethi Banalabacd75c2017-03-27 14:43:18 -07009359 hw_enc_features = NETIF_F_SG |
9360 NETIF_F_IP_CSUM |
9361 NETIF_F_IPV6_CSUM |
9362 NETIF_F_HIGHDMA |
9363 NETIF_F_SOFT_FEATURES |
9364 NETIF_F_TSO |
9365 NETIF_F_TSO_ECN |
9366 NETIF_F_TSO6 |
9367 NETIF_F_GSO_GRE |
9368 NETIF_F_GSO_GRE_CSUM |
9369 NETIF_F_GSO_PARTIAL |
9370 NETIF_F_GSO_UDP_TUNNEL |
9371 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9372 NETIF_F_SCTP_CRC |
9373 NETIF_F_RXHASH |
9374 NETIF_F_RXCSUM |
9375 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009376
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009377 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009378 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9379
9380 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009381
Preethi Banalabacd75c2017-03-27 14:43:18 -07009382 netdev->hw_enc_features |= hw_enc_features;
9383
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009384 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009385 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009386
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009387 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009388 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009389 hw_features = hw_enc_features |
9390 NETIF_F_HW_VLAN_CTAG_TX |
9391 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009392
Preethi Banalabacd75c2017-03-27 14:43:18 -07009393 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009394
Preethi Banalabacd75c2017-03-27 14:43:18 -07009395 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009396 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009397
9398 if (vsi->type == I40E_VSI_MAIN) {
9399 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009400 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009401 /* The following steps are necessary for two reasons. First,
9402 * some older NVM configurations load a default MAC-VLAN
9403 * filter that will accept any tagged packet, and we want to
9404 * replace this with a normal filter. Additionally, it is
9405 * possible our MAC address was provided by the platform using
9406 * Open Firmware or similar.
9407 *
9408 * Thus, we need to remove the default filter and install one
9409 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009410 */
9411 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009412 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009413 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009414 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009415 } else {
9416 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9417 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9418 pf->vsi[pf->lan_vsi]->netdev->name);
9419 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009420
Jacob Keller278e7d02016-10-05 09:30:37 -07009421 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009422 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009423 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009424 }
Kiran Patil21659032015-09-30 14:09:03 -04009425
Jacob Keller435c0842016-11-08 13:05:10 -08009426 /* Add the broadcast filter so that we initially will receive
9427 * broadcast packets. Note that when a new VLAN is first added the
9428 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9429 * specific filters as part of transitioning into "vlan" operation.
9430 * When more VLANs are added, the driver will copy each existing MAC
9431 * filter and add it for the new VLAN.
9432 *
9433 * Broadcast filters are handled specially by
9434 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9435 * promiscuous bit instead of adding this directly as a MAC/VLAN
9436 * filter. The subtask will update the correct broadcast promiscuous
9437 * bits as VLANs become active or inactive.
9438 */
9439 eth_broadcast_addr(broadcast);
9440 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009441 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009442 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9443
Greg Rose9a173902014-05-22 06:32:02 +00009444 ether_addr_copy(netdev->dev_addr, mac_addr);
9445 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447 netdev->priv_flags |= IFF_UNICAST_FLT;
9448 netdev->priv_flags |= IFF_SUPP_NOFCS;
9449 /* Setup netdev TC information */
9450 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9451
9452 netdev->netdev_ops = &i40e_netdev_ops;
9453 netdev->watchdog_timeo = 5 * HZ;
9454 i40e_set_ethtool_ops(netdev);
9455
Jarod Wilson91c527a2016-10-17 15:54:05 -04009456 /* MTU range: 68 - 9706 */
9457 netdev->min_mtu = ETH_MIN_MTU;
9458 netdev->max_mtu = I40E_MAX_RXBUFFER -
9459 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9460
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009461 return 0;
9462}
9463
9464/**
9465 * i40e_vsi_delete - Delete a VSI from the switch
9466 * @vsi: the VSI being removed
9467 *
9468 * Returns 0 on success, negative value on failure
9469 **/
9470static void i40e_vsi_delete(struct i40e_vsi *vsi)
9471{
9472 /* remove default VSI is not allowed */
9473 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9474 return;
9475
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009476 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009477}
9478
9479/**
Neerav Parikh51616012015-02-06 08:52:14 +00009480 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9481 * @vsi: the VSI being queried
9482 *
9483 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9484 **/
9485int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9486{
9487 struct i40e_veb *veb;
9488 struct i40e_pf *pf = vsi->back;
9489
9490 /* Uplink is not a bridge so default to VEB */
9491 if (vsi->veb_idx == I40E_NO_VEB)
9492 return 1;
9493
9494 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009495 if (!veb) {
9496 dev_info(&pf->pdev->dev,
9497 "There is no veb associated with the bridge\n");
9498 return -ENOENT;
9499 }
Neerav Parikh51616012015-02-06 08:52:14 +00009500
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009501 /* Uplink is a bridge in VEPA mode */
9502 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9503 return 0;
9504 } else {
9505 /* Uplink is a bridge in VEB mode */
9506 return 1;
9507 }
9508
9509 /* VEPA is now default bridge, so return 0 */
9510 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009511}
9512
9513/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009514 * i40e_add_vsi - Add a VSI to the switch
9515 * @vsi: the VSI being configured
9516 *
9517 * This initializes a VSI context depending on the VSI type to be added and
9518 * passes it down to the add_vsi aq command.
9519 **/
9520static int i40e_add_vsi(struct i40e_vsi *vsi)
9521{
9522 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009523 struct i40e_pf *pf = vsi->back;
9524 struct i40e_hw *hw = &pf->hw;
9525 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009526 struct i40e_mac_filter *f;
9527 struct hlist_node *h;
9528 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530 u8 enabled_tc = 0x1; /* TC0 enabled */
9531 int f_count = 0;
9532
9533 memset(&ctxt, 0, sizeof(ctxt));
9534 switch (vsi->type) {
9535 case I40E_VSI_MAIN:
9536 /* The PF's main VSI is already setup as part of the
9537 * device initialization, so we'll not bother with
9538 * the add_vsi call, but we will retrieve the current
9539 * VSI context.
9540 */
9541 ctxt.seid = pf->main_vsi_seid;
9542 ctxt.pf_num = pf->hw.pf_id;
9543 ctxt.vf_num = 0;
9544 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9545 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9546 if (ret) {
9547 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009548 "couldn't get PF vsi config, err %s aq_err %s\n",
9549 i40e_stat_str(&pf->hw, ret),
9550 i40e_aq_str(&pf->hw,
9551 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009552 return -ENOENT;
9553 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009554 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009555 vsi->info.valid_sections = 0;
9556
9557 vsi->seid = ctxt.seid;
9558 vsi->id = ctxt.vsi_number;
9559
9560 enabled_tc = i40e_pf_get_tc_map(pf);
9561
9562 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009563 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9564 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009565 memset(&ctxt, 0, sizeof(ctxt));
9566 ctxt.seid = pf->main_vsi_seid;
9567 ctxt.pf_num = pf->hw.pf_id;
9568 ctxt.vf_num = 0;
9569 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9570 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9571 if (ret) {
9572 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009573 "update vsi failed, err %s aq_err %s\n",
9574 i40e_stat_str(&pf->hw, ret),
9575 i40e_aq_str(&pf->hw,
9576 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009577 ret = -ENOENT;
9578 goto err;
9579 }
9580 /* update the local VSI info queue map */
9581 i40e_vsi_update_queue_map(vsi, &ctxt);
9582 vsi->info.valid_sections = 0;
9583 } else {
9584 /* Default/Main VSI is only enabled for TC0
9585 * reconfigure it to enable all TCs that are
9586 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009587 * For MFP case the iSCSI PF would use this
9588 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009589 */
9590 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9591 if (ret) {
9592 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009593 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9594 enabled_tc,
9595 i40e_stat_str(&pf->hw, ret),
9596 i40e_aq_str(&pf->hw,
9597 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009598 ret = -ENOENT;
9599 }
9600 }
9601 break;
9602
9603 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009604 ctxt.pf_num = hw->pf_id;
9605 ctxt.vf_num = 0;
9606 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009607 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009608 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009609 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9610 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009611 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009612 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009613 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009614 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009615 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009616 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 break;
9618
9619 case I40E_VSI_VMDQ2:
9620 ctxt.pf_num = hw->pf_id;
9621 ctxt.vf_num = 0;
9622 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009623 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009624 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009626 /* This VSI is connected to VEB so the switch_id
9627 * should be set to zero by default.
9628 */
Neerav Parikh51616012015-02-06 08:52:14 +00009629 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9630 ctxt.info.valid_sections |=
9631 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9632 ctxt.info.switch_id =
9633 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9634 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009635
9636 /* Setup the VSI tx/rx queue map for TC0 only for now */
9637 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9638 break;
9639
9640 case I40E_VSI_SRIOV:
9641 ctxt.pf_num = hw->pf_id;
9642 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9643 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009644 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009645 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009647 /* This VSI is connected to VEB so the switch_id
9648 * should be set to zero by default.
9649 */
Neerav Parikh51616012015-02-06 08:52:14 +00009650 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9651 ctxt.info.valid_sections |=
9652 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9653 ctxt.info.switch_id =
9654 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9655 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009656
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009657 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9658 ctxt.info.valid_sections |=
9659 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9660 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009661 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9662 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009663 }
9664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009665 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9666 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009667 if (pf->vf[vsi->vf_id].spoofchk) {
9668 ctxt.info.valid_sections |=
9669 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9670 ctxt.info.sec_flags |=
9671 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9672 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9673 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009674 /* Setup the VSI tx/rx queue map for TC0 only for now */
9675 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9676 break;
9677
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009678 case I40E_VSI_IWARP:
9679 /* send down message to iWARP */
9680 break;
9681
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009682 default:
9683 return -ENODEV;
9684 }
9685
9686 if (vsi->type != I40E_VSI_MAIN) {
9687 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9688 if (ret) {
9689 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009690 "add vsi failed, err %s aq_err %s\n",
9691 i40e_stat_str(&pf->hw, ret),
9692 i40e_aq_str(&pf->hw,
9693 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009694 ret = -ENOENT;
9695 goto err;
9696 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009697 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009698 vsi->info.valid_sections = 0;
9699 vsi->seid = ctxt.seid;
9700 vsi->id = ctxt.vsi_number;
9701 }
9702
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009703 vsi->active_filters = 0;
9704 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009705 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009706 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009707 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009708 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009709 f_count++;
9710 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009711 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009713 if (f_count) {
9714 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9715 pf->flags |= I40E_FLAG_FILTER_SYNC;
9716 }
9717
9718 /* Update VSI BW information */
9719 ret = i40e_vsi_get_bw_info(vsi);
9720 if (ret) {
9721 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009722 "couldn't get vsi bw info, err %s aq_err %s\n",
9723 i40e_stat_str(&pf->hw, ret),
9724 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009725 /* VSI is already added so not tearing that up */
9726 ret = 0;
9727 }
9728
9729err:
9730 return ret;
9731}
9732
9733/**
9734 * i40e_vsi_release - Delete a VSI and free its resources
9735 * @vsi: the VSI being removed
9736 *
9737 * Returns 0 on success or < 0 on error
9738 **/
9739int i40e_vsi_release(struct i40e_vsi *vsi)
9740{
Jacob Keller278e7d02016-10-05 09:30:37 -07009741 struct i40e_mac_filter *f;
9742 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009743 struct i40e_veb *veb = NULL;
9744 struct i40e_pf *pf;
9745 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009746 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009747
9748 pf = vsi->back;
9749
9750 /* release of a VEB-owner or last VSI is not allowed */
9751 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9752 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9753 vsi->seid, vsi->uplink_seid);
9754 return -ENODEV;
9755 }
9756 if (vsi == pf->vsi[pf->lan_vsi] &&
9757 !test_bit(__I40E_DOWN, &pf->state)) {
9758 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9759 return -ENODEV;
9760 }
9761
9762 uplink_seid = vsi->uplink_seid;
9763 if (vsi->type != I40E_VSI_SRIOV) {
9764 if (vsi->netdev_registered) {
9765 vsi->netdev_registered = false;
9766 if (vsi->netdev) {
9767 /* results in a call to i40e_close() */
9768 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009769 }
9770 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009771 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009772 }
9773 i40e_vsi_disable_irq(vsi);
9774 }
9775
Jacob Keller278e7d02016-10-05 09:30:37 -07009776 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009777
9778 /* clear the sync flag on all filters */
9779 if (vsi->netdev) {
9780 __dev_uc_unsync(vsi->netdev, NULL);
9781 __dev_mc_unsync(vsi->netdev, NULL);
9782 }
9783
9784 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009785 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009786 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009787
Jacob Keller278e7d02016-10-05 09:30:37 -07009788 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009789
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009790 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009791
9792 i40e_vsi_delete(vsi);
9793 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009794 if (vsi->netdev) {
9795 free_netdev(vsi->netdev);
9796 vsi->netdev = NULL;
9797 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009798 i40e_vsi_clear_rings(vsi);
9799 i40e_vsi_clear(vsi);
9800
9801 /* If this was the last thing on the VEB, except for the
9802 * controlling VSI, remove the VEB, which puts the controlling
9803 * VSI onto the next level down in the switch.
9804 *
9805 * Well, okay, there's one more exception here: don't remove
9806 * the orphan VEBs yet. We'll wait for an explicit remove request
9807 * from up the network stack.
9808 */
Mitch Williams505682c2014-05-20 08:01:37 +00009809 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009810 if (pf->vsi[i] &&
9811 pf->vsi[i]->uplink_seid == uplink_seid &&
9812 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9813 n++; /* count the VSIs */
9814 }
9815 }
9816 for (i = 0; i < I40E_MAX_VEB; i++) {
9817 if (!pf->veb[i])
9818 continue;
9819 if (pf->veb[i]->uplink_seid == uplink_seid)
9820 n++; /* count the VEBs */
9821 if (pf->veb[i]->seid == uplink_seid)
9822 veb = pf->veb[i];
9823 }
9824 if (n == 0 && veb && veb->uplink_seid != 0)
9825 i40e_veb_release(veb);
9826
9827 return 0;
9828}
9829
9830/**
9831 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9832 * @vsi: ptr to the VSI
9833 *
9834 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9835 * corresponding SW VSI structure and initializes num_queue_pairs for the
9836 * newly allocated VSI.
9837 *
9838 * Returns 0 on success or negative on failure
9839 **/
9840static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9841{
9842 int ret = -ENOENT;
9843 struct i40e_pf *pf = vsi->back;
9844
Alexander Duyck493fb302013-09-28 07:01:44 +00009845 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009846 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9847 vsi->seid);
9848 return -EEXIST;
9849 }
9850
9851 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009852 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009853 vsi->seid, vsi->base_vector);
9854 return -EEXIST;
9855 }
9856
Greg Rose90e04072014-03-06 08:59:57 +00009857 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009858 if (ret) {
9859 dev_info(&pf->pdev->dev,
9860 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9861 vsi->num_q_vectors, vsi->seid, ret);
9862 vsi->num_q_vectors = 0;
9863 goto vector_setup_out;
9864 }
9865
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009866 /* In Legacy mode, we do not have to get any other vector since we
9867 * piggyback on the misc/ICR0 for queue interrupts.
9868 */
9869 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9870 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009871 if (vsi->num_q_vectors)
9872 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9873 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009874 if (vsi->base_vector < 0) {
9875 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009876 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9877 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009878 i40e_vsi_free_q_vectors(vsi);
9879 ret = -ENOENT;
9880 goto vector_setup_out;
9881 }
9882
9883vector_setup_out:
9884 return ret;
9885}
9886
9887/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009888 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9889 * @vsi: pointer to the vsi.
9890 *
9891 * This re-allocates a vsi's queue resources.
9892 *
9893 * Returns pointer to the successfully allocated and configured VSI sw struct
9894 * on success, otherwise returns NULL on failure.
9895 **/
9896static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9897{
John Underwoodf5340392016-02-18 09:19:24 -08009898 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009899 u8 enabled_tc;
9900 int ret;
9901
John Underwoodf5340392016-02-18 09:19:24 -08009902 if (!vsi)
9903 return NULL;
9904
9905 pf = vsi->back;
9906
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009907 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9908 i40e_vsi_clear_rings(vsi);
9909
9910 i40e_vsi_free_arrays(vsi, false);
9911 i40e_set_num_rings_in_vsi(vsi);
9912 ret = i40e_vsi_alloc_arrays(vsi, false);
9913 if (ret)
9914 goto err_vsi;
9915
9916 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9917 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009918 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009919 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009920 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009921 goto err_vsi;
9922 }
9923 vsi->base_queue = ret;
9924
9925 /* Update the FW view of the VSI. Force a reset of TC and queue
9926 * layout configurations.
9927 */
9928 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9929 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9930 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9931 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009932 if (vsi->type == I40E_VSI_MAIN)
9933 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009934
9935 /* assign it some queues */
9936 ret = i40e_alloc_rings(vsi);
9937 if (ret)
9938 goto err_rings;
9939
9940 /* map all of the rings to the q_vectors */
9941 i40e_vsi_map_rings_to_vectors(vsi);
9942 return vsi;
9943
9944err_rings:
9945 i40e_vsi_free_q_vectors(vsi);
9946 if (vsi->netdev_registered) {
9947 vsi->netdev_registered = false;
9948 unregister_netdev(vsi->netdev);
9949 free_netdev(vsi->netdev);
9950 vsi->netdev = NULL;
9951 }
9952 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9953err_vsi:
9954 i40e_vsi_clear(vsi);
9955 return NULL;
9956}
9957
9958/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009959 * i40e_vsi_setup - Set up a VSI by a given type
9960 * @pf: board private structure
9961 * @type: VSI type
9962 * @uplink_seid: the switch element to link to
9963 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9964 *
9965 * This allocates the sw VSI structure and its queue resources, then add a VSI
9966 * to the identified VEB.
9967 *
9968 * Returns pointer to the successfully allocated and configure VSI sw struct on
9969 * success, otherwise returns NULL on failure.
9970 **/
9971struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9972 u16 uplink_seid, u32 param1)
9973{
9974 struct i40e_vsi *vsi = NULL;
9975 struct i40e_veb *veb = NULL;
9976 int ret, i;
9977 int v_idx;
9978
9979 /* The requested uplink_seid must be either
9980 * - the PF's port seid
9981 * no VEB is needed because this is the PF
9982 * or this is a Flow Director special case VSI
9983 * - seid of an existing VEB
9984 * - seid of a VSI that owns an existing VEB
9985 * - seid of a VSI that doesn't own a VEB
9986 * a new VEB is created and the VSI becomes the owner
9987 * - seid of the PF VSI, which is what creates the first VEB
9988 * this is a special case of the previous
9989 *
9990 * Find which uplink_seid we were given and create a new VEB if needed
9991 */
9992 for (i = 0; i < I40E_MAX_VEB; i++) {
9993 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9994 veb = pf->veb[i];
9995 break;
9996 }
9997 }
9998
9999 if (!veb && uplink_seid != pf->mac_seid) {
10000
Mitch Williams505682c2014-05-20 08:01:37 +000010001 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010002 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10003 vsi = pf->vsi[i];
10004 break;
10005 }
10006 }
10007 if (!vsi) {
10008 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10009 uplink_seid);
10010 return NULL;
10011 }
10012
10013 if (vsi->uplink_seid == pf->mac_seid)
10014 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10015 vsi->tc_config.enabled_tc);
10016 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10017 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10018 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010019 if (veb) {
10020 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10021 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010022 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010023 return NULL;
10024 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010025 /* We come up by default in VEPA mode if SRIOV is not
10026 * already enabled, in which case we can't force VEPA
10027 * mode.
10028 */
10029 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10030 veb->bridge_mode = BRIDGE_MODE_VEPA;
10031 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10032 }
Neerav Parikh51616012015-02-06 08:52:14 +000010033 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010034 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010035 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10036 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10037 veb = pf->veb[i];
10038 }
10039 if (!veb) {
10040 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10041 return NULL;
10042 }
10043
10044 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10045 uplink_seid = veb->seid;
10046 }
10047
10048 /* get vsi sw struct */
10049 v_idx = i40e_vsi_mem_alloc(pf, type);
10050 if (v_idx < 0)
10051 goto err_alloc;
10052 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010053 if (!vsi)
10054 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010055 vsi->type = type;
10056 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10057
10058 if (type == I40E_VSI_MAIN)
10059 pf->lan_vsi = v_idx;
10060 else if (type == I40E_VSI_SRIOV)
10061 vsi->vf_id = param1;
10062 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010063 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
10064 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010065 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010066 dev_info(&pf->pdev->dev,
10067 "failed to get tracking for %d queues for VSI %d err=%d\n",
10068 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010069 goto err_vsi;
10070 }
10071 vsi->base_queue = ret;
10072
10073 /* get a VSI from the hardware */
10074 vsi->uplink_seid = uplink_seid;
10075 ret = i40e_add_vsi(vsi);
10076 if (ret)
10077 goto err_vsi;
10078
10079 switch (vsi->type) {
10080 /* setup the netdev if needed */
10081 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010082 /* Apply relevant filters if a platform-specific mac
10083 * address was selected.
10084 */
10085 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10086 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10087 if (ret) {
10088 dev_warn(&pf->pdev->dev,
10089 "could not set up macaddr; err %d\n",
10090 ret);
10091 }
10092 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010093 case I40E_VSI_VMDQ2:
10094 ret = i40e_config_netdev(vsi);
10095 if (ret)
10096 goto err_netdev;
10097 ret = register_netdev(vsi->netdev);
10098 if (ret)
10099 goto err_netdev;
10100 vsi->netdev_registered = true;
10101 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010102#ifdef CONFIG_I40E_DCB
10103 /* Setup DCB netlink interface */
10104 i40e_dcbnl_setup(vsi);
10105#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010106 /* fall through */
10107
10108 case I40E_VSI_FDIR:
10109 /* set up vectors and rings if needed */
10110 ret = i40e_vsi_setup_vectors(vsi);
10111 if (ret)
10112 goto err_msix;
10113
10114 ret = i40e_alloc_rings(vsi);
10115 if (ret)
10116 goto err_rings;
10117
10118 /* map all of the rings to the q_vectors */
10119 i40e_vsi_map_rings_to_vectors(vsi);
10120
10121 i40e_vsi_reset_stats(vsi);
10122 break;
10123
10124 default:
10125 /* no netdev or rings for the other VSI types */
10126 break;
10127 }
10128
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010129 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10130 (vsi->type == I40E_VSI_VMDQ2)) {
10131 ret = i40e_vsi_config_rss(vsi);
10132 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010133 return vsi;
10134
10135err_rings:
10136 i40e_vsi_free_q_vectors(vsi);
10137err_msix:
10138 if (vsi->netdev_registered) {
10139 vsi->netdev_registered = false;
10140 unregister_netdev(vsi->netdev);
10141 free_netdev(vsi->netdev);
10142 vsi->netdev = NULL;
10143 }
10144err_netdev:
10145 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10146err_vsi:
10147 i40e_vsi_clear(vsi);
10148err_alloc:
10149 return NULL;
10150}
10151
10152/**
10153 * i40e_veb_get_bw_info - Query VEB BW information
10154 * @veb: the veb to query
10155 *
10156 * Query the Tx scheduler BW configuration data for given VEB
10157 **/
10158static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10159{
10160 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10161 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10162 struct i40e_pf *pf = veb->pf;
10163 struct i40e_hw *hw = &pf->hw;
10164 u32 tc_bw_max;
10165 int ret = 0;
10166 int i;
10167
10168 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10169 &bw_data, NULL);
10170 if (ret) {
10171 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010172 "query veb bw config failed, err %s aq_err %s\n",
10173 i40e_stat_str(&pf->hw, ret),
10174 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010175 goto out;
10176 }
10177
10178 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10179 &ets_data, NULL);
10180 if (ret) {
10181 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010182 "query veb bw ets config failed, err %s aq_err %s\n",
10183 i40e_stat_str(&pf->hw, ret),
10184 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010185 goto out;
10186 }
10187
10188 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10189 veb->bw_max_quanta = ets_data.tc_bw_max;
10190 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010191 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010192 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10193 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10194 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10195 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10196 veb->bw_tc_limit_credits[i] =
10197 le16_to_cpu(bw_data.tc_bw_limits[i]);
10198 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10199 }
10200
10201out:
10202 return ret;
10203}
10204
10205/**
10206 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10207 * @pf: board private structure
10208 *
10209 * On error: returns error code (negative)
10210 * On success: returns vsi index in PF (positive)
10211 **/
10212static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10213{
10214 int ret = -ENOENT;
10215 struct i40e_veb *veb;
10216 int i;
10217
10218 /* Need to protect the allocation of switch elements at the PF level */
10219 mutex_lock(&pf->switch_mutex);
10220
10221 /* VEB list may be fragmented if VEB creation/destruction has
10222 * been happening. We can afford to do a quick scan to look
10223 * for any free slots in the list.
10224 *
10225 * find next empty veb slot, looping back around if necessary
10226 */
10227 i = 0;
10228 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10229 i++;
10230 if (i >= I40E_MAX_VEB) {
10231 ret = -ENOMEM;
10232 goto err_alloc_veb; /* out of VEB slots! */
10233 }
10234
10235 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10236 if (!veb) {
10237 ret = -ENOMEM;
10238 goto err_alloc_veb;
10239 }
10240 veb->pf = pf;
10241 veb->idx = i;
10242 veb->enabled_tc = 1;
10243
10244 pf->veb[i] = veb;
10245 ret = i;
10246err_alloc_veb:
10247 mutex_unlock(&pf->switch_mutex);
10248 return ret;
10249}
10250
10251/**
10252 * i40e_switch_branch_release - Delete a branch of the switch tree
10253 * @branch: where to start deleting
10254 *
10255 * This uses recursion to find the tips of the branch to be
10256 * removed, deleting until we get back to and can delete this VEB.
10257 **/
10258static void i40e_switch_branch_release(struct i40e_veb *branch)
10259{
10260 struct i40e_pf *pf = branch->pf;
10261 u16 branch_seid = branch->seid;
10262 u16 veb_idx = branch->idx;
10263 int i;
10264
10265 /* release any VEBs on this VEB - RECURSION */
10266 for (i = 0; i < I40E_MAX_VEB; i++) {
10267 if (!pf->veb[i])
10268 continue;
10269 if (pf->veb[i]->uplink_seid == branch->seid)
10270 i40e_switch_branch_release(pf->veb[i]);
10271 }
10272
10273 /* Release the VSIs on this VEB, but not the owner VSI.
10274 *
10275 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10276 * the VEB itself, so don't use (*branch) after this loop.
10277 */
Mitch Williams505682c2014-05-20 08:01:37 +000010278 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010279 if (!pf->vsi[i])
10280 continue;
10281 if (pf->vsi[i]->uplink_seid == branch_seid &&
10282 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10283 i40e_vsi_release(pf->vsi[i]);
10284 }
10285 }
10286
10287 /* There's one corner case where the VEB might not have been
10288 * removed, so double check it here and remove it if needed.
10289 * This case happens if the veb was created from the debugfs
10290 * commands and no VSIs were added to it.
10291 */
10292 if (pf->veb[veb_idx])
10293 i40e_veb_release(pf->veb[veb_idx]);
10294}
10295
10296/**
10297 * i40e_veb_clear - remove veb struct
10298 * @veb: the veb to remove
10299 **/
10300static void i40e_veb_clear(struct i40e_veb *veb)
10301{
10302 if (!veb)
10303 return;
10304
10305 if (veb->pf) {
10306 struct i40e_pf *pf = veb->pf;
10307
10308 mutex_lock(&pf->switch_mutex);
10309 if (pf->veb[veb->idx] == veb)
10310 pf->veb[veb->idx] = NULL;
10311 mutex_unlock(&pf->switch_mutex);
10312 }
10313
10314 kfree(veb);
10315}
10316
10317/**
10318 * i40e_veb_release - Delete a VEB and free its resources
10319 * @veb: the VEB being removed
10320 **/
10321void i40e_veb_release(struct i40e_veb *veb)
10322{
10323 struct i40e_vsi *vsi = NULL;
10324 struct i40e_pf *pf;
10325 int i, n = 0;
10326
10327 pf = veb->pf;
10328
10329 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010330 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010331 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10332 n++;
10333 vsi = pf->vsi[i];
10334 }
10335 }
10336 if (n != 1) {
10337 dev_info(&pf->pdev->dev,
10338 "can't remove VEB %d with %d VSIs left\n",
10339 veb->seid, n);
10340 return;
10341 }
10342
10343 /* move the remaining VSI to uplink veb */
10344 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10345 if (veb->uplink_seid) {
10346 vsi->uplink_seid = veb->uplink_seid;
10347 if (veb->uplink_seid == pf->mac_seid)
10348 vsi->veb_idx = I40E_NO_VEB;
10349 else
10350 vsi->veb_idx = veb->veb_idx;
10351 } else {
10352 /* floating VEB */
10353 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10354 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10355 }
10356
10357 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10358 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010359}
10360
10361/**
10362 * i40e_add_veb - create the VEB in the switch
10363 * @veb: the VEB to be instantiated
10364 * @vsi: the controlling VSI
10365 **/
10366static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10367{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010368 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010369 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010370 int ret;
10371
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010372 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010373 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010374 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010375
10376 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010377 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010378 dev_info(&pf->pdev->dev,
10379 "couldn't add VEB, err %s aq_err %s\n",
10380 i40e_stat_str(&pf->hw, ret),
10381 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010382 return -EPERM;
10383 }
10384
10385 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010386 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010387 &veb->stats_idx, NULL, NULL, NULL);
10388 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010389 dev_info(&pf->pdev->dev,
10390 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10391 i40e_stat_str(&pf->hw, ret),
10392 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010393 return -EPERM;
10394 }
10395 ret = i40e_veb_get_bw_info(veb);
10396 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010397 dev_info(&pf->pdev->dev,
10398 "couldn't get VEB bw info, err %s aq_err %s\n",
10399 i40e_stat_str(&pf->hw, ret),
10400 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10401 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010402 return -ENOENT;
10403 }
10404
10405 vsi->uplink_seid = veb->seid;
10406 vsi->veb_idx = veb->idx;
10407 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10408
10409 return 0;
10410}
10411
10412/**
10413 * i40e_veb_setup - Set up a VEB
10414 * @pf: board private structure
10415 * @flags: VEB setup flags
10416 * @uplink_seid: the switch element to link to
10417 * @vsi_seid: the initial VSI seid
10418 * @enabled_tc: Enabled TC bit-map
10419 *
10420 * This allocates the sw VEB structure and links it into the switch
10421 * It is possible and legal for this to be a duplicate of an already
10422 * existing VEB. It is also possible for both uplink and vsi seids
10423 * to be zero, in order to create a floating VEB.
10424 *
10425 * Returns pointer to the successfully allocated VEB sw struct on
10426 * success, otherwise returns NULL on failure.
10427 **/
10428struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10429 u16 uplink_seid, u16 vsi_seid,
10430 u8 enabled_tc)
10431{
10432 struct i40e_veb *veb, *uplink_veb = NULL;
10433 int vsi_idx, veb_idx;
10434 int ret;
10435
10436 /* if one seid is 0, the other must be 0 to create a floating relay */
10437 if ((uplink_seid == 0 || vsi_seid == 0) &&
10438 (uplink_seid + vsi_seid != 0)) {
10439 dev_info(&pf->pdev->dev,
10440 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10441 uplink_seid, vsi_seid);
10442 return NULL;
10443 }
10444
10445 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010446 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010447 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10448 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010449 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010450 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10451 vsi_seid);
10452 return NULL;
10453 }
10454
10455 if (uplink_seid && uplink_seid != pf->mac_seid) {
10456 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10457 if (pf->veb[veb_idx] &&
10458 pf->veb[veb_idx]->seid == uplink_seid) {
10459 uplink_veb = pf->veb[veb_idx];
10460 break;
10461 }
10462 }
10463 if (!uplink_veb) {
10464 dev_info(&pf->pdev->dev,
10465 "uplink seid %d not found\n", uplink_seid);
10466 return NULL;
10467 }
10468 }
10469
10470 /* get veb sw struct */
10471 veb_idx = i40e_veb_mem_alloc(pf);
10472 if (veb_idx < 0)
10473 goto err_alloc;
10474 veb = pf->veb[veb_idx];
10475 veb->flags = flags;
10476 veb->uplink_seid = uplink_seid;
10477 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10478 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10479
10480 /* create the VEB in the switch */
10481 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10482 if (ret)
10483 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010484 if (vsi_idx == pf->lan_vsi)
10485 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010486
10487 return veb;
10488
10489err_veb:
10490 i40e_veb_clear(veb);
10491err_alloc:
10492 return NULL;
10493}
10494
10495/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010496 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010497 * @pf: board private structure
10498 * @ele: element we are building info from
10499 * @num_reported: total number of elements
10500 * @printconfig: should we print the contents
10501 *
10502 * helper function to assist in extracting a few useful SEID values.
10503 **/
10504static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10505 struct i40e_aqc_switch_config_element_resp *ele,
10506 u16 num_reported, bool printconfig)
10507{
10508 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10509 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10510 u8 element_type = ele->element_type;
10511 u16 seid = le16_to_cpu(ele->seid);
10512
10513 if (printconfig)
10514 dev_info(&pf->pdev->dev,
10515 "type=%d seid=%d uplink=%d downlink=%d\n",
10516 element_type, seid, uplink_seid, downlink_seid);
10517
10518 switch (element_type) {
10519 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10520 pf->mac_seid = seid;
10521 break;
10522 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10523 /* Main VEB? */
10524 if (uplink_seid != pf->mac_seid)
10525 break;
10526 if (pf->lan_veb == I40E_NO_VEB) {
10527 int v;
10528
10529 /* find existing or else empty VEB */
10530 for (v = 0; v < I40E_MAX_VEB; v++) {
10531 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10532 pf->lan_veb = v;
10533 break;
10534 }
10535 }
10536 if (pf->lan_veb == I40E_NO_VEB) {
10537 v = i40e_veb_mem_alloc(pf);
10538 if (v < 0)
10539 break;
10540 pf->lan_veb = v;
10541 }
10542 }
10543
10544 pf->veb[pf->lan_veb]->seid = seid;
10545 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10546 pf->veb[pf->lan_veb]->pf = pf;
10547 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10548 break;
10549 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10550 if (num_reported != 1)
10551 break;
10552 /* This is immediately after a reset so we can assume this is
10553 * the PF's VSI
10554 */
10555 pf->mac_seid = uplink_seid;
10556 pf->pf_seid = downlink_seid;
10557 pf->main_vsi_seid = seid;
10558 if (printconfig)
10559 dev_info(&pf->pdev->dev,
10560 "pf_seid=%d main_vsi_seid=%d\n",
10561 pf->pf_seid, pf->main_vsi_seid);
10562 break;
10563 case I40E_SWITCH_ELEMENT_TYPE_PF:
10564 case I40E_SWITCH_ELEMENT_TYPE_VF:
10565 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10566 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10567 case I40E_SWITCH_ELEMENT_TYPE_PE:
10568 case I40E_SWITCH_ELEMENT_TYPE_PA:
10569 /* ignore these for now */
10570 break;
10571 default:
10572 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10573 element_type, seid);
10574 break;
10575 }
10576}
10577
10578/**
10579 * i40e_fetch_switch_configuration - Get switch config from firmware
10580 * @pf: board private structure
10581 * @printconfig: should we print the contents
10582 *
10583 * Get the current switch configuration from the device and
10584 * extract a few useful SEID values.
10585 **/
10586int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10587{
10588 struct i40e_aqc_get_switch_config_resp *sw_config;
10589 u16 next_seid = 0;
10590 int ret = 0;
10591 u8 *aq_buf;
10592 int i;
10593
10594 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10595 if (!aq_buf)
10596 return -ENOMEM;
10597
10598 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10599 do {
10600 u16 num_reported, num_total;
10601
10602 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10603 I40E_AQ_LARGE_BUF,
10604 &next_seid, NULL);
10605 if (ret) {
10606 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010607 "get switch config failed err %s aq_err %s\n",
10608 i40e_stat_str(&pf->hw, ret),
10609 i40e_aq_str(&pf->hw,
10610 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010611 kfree(aq_buf);
10612 return -ENOENT;
10613 }
10614
10615 num_reported = le16_to_cpu(sw_config->header.num_reported);
10616 num_total = le16_to_cpu(sw_config->header.num_total);
10617
10618 if (printconfig)
10619 dev_info(&pf->pdev->dev,
10620 "header: %d reported %d total\n",
10621 num_reported, num_total);
10622
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010623 for (i = 0; i < num_reported; i++) {
10624 struct i40e_aqc_switch_config_element_resp *ele =
10625 &sw_config->element[i];
10626
10627 i40e_setup_pf_switch_element(pf, ele, num_reported,
10628 printconfig);
10629 }
10630 } while (next_seid != 0);
10631
10632 kfree(aq_buf);
10633 return ret;
10634}
10635
10636/**
10637 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10638 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010639 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010640 *
10641 * Returns 0 on success, negative value on failure
10642 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010643static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010644{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010645 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010646 int ret;
10647
10648 /* find out what's out there already */
10649 ret = i40e_fetch_switch_configuration(pf, false);
10650 if (ret) {
10651 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010652 "couldn't fetch switch config, err %s aq_err %s\n",
10653 i40e_stat_str(&pf->hw, ret),
10654 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010655 return ret;
10656 }
10657 i40e_pf_reset_stats(pf);
10658
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010659 /* set the switch config bit for the whole device to
10660 * support limited promisc or true promisc
10661 * when user requests promisc. The default is limited
10662 * promisc.
10663 */
10664
10665 if ((pf->hw.pf_id == 0) &&
10666 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10667 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10668
10669 if (pf->hw.pf_id == 0) {
10670 u16 valid_flags;
10671
10672 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10673 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10674 NULL);
10675 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10676 dev_info(&pf->pdev->dev,
10677 "couldn't set switch config bits, err %s aq_err %s\n",
10678 i40e_stat_str(&pf->hw, ret),
10679 i40e_aq_str(&pf->hw,
10680 pf->hw.aq.asq_last_status));
10681 /* not a fatal problem, just keep going */
10682 }
10683 }
10684
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010685 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010686 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010687 struct i40e_vsi *vsi = NULL;
10688 u16 uplink_seid;
10689
10690 /* Set up the PF VSI associated with the PF's main VSI
10691 * that is already in the HW switch
10692 */
10693 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10694 uplink_seid = pf->veb[pf->lan_veb]->seid;
10695 else
10696 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010697 if (pf->lan_vsi == I40E_NO_VSI)
10698 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10699 else if (reinit)
10700 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010701 if (!vsi) {
10702 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10703 i40e_fdir_teardown(pf);
10704 return -EAGAIN;
10705 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010706 } else {
10707 /* force a reset of TC and queue layout configurations */
10708 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010710 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10711 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10712 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10713 }
10714 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10715
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010716 i40e_fdir_sb_setup(pf);
10717
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010718 /* Setup static PF queue filter control settings */
10719 ret = i40e_setup_pf_filter_control(pf);
10720 if (ret) {
10721 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10722 ret);
10723 /* Failure here should not stop continuing other steps */
10724 }
10725
10726 /* enable RSS in the HW, even for only one queue, as the stack can use
10727 * the hash
10728 */
10729 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010730 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010731
10732 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010733 i40e_link_event(pf);
10734
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010735 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010736 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10737 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010738
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010739 i40e_ptp_init(pf);
10740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010741 return ret;
10742}
10743
10744/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010745 * i40e_determine_queue_usage - Work out queue distribution
10746 * @pf: board private structure
10747 **/
10748static void i40e_determine_queue_usage(struct i40e_pf *pf)
10749{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010750 int queues_left;
10751
10752 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010753
10754 /* Find the max queues to be put into basic use. We'll always be
10755 * using TC0, whether or not DCB is running, and TC0 will get the
10756 * big RSS set.
10757 */
10758 queues_left = pf->hw.func_caps.num_tx_qp;
10759
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010760 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010761 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010762 /* one qp for PF, no queues for anything else */
10763 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010764 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010765
10766 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010767 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010768 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010769 I40E_FLAG_FD_SB_ENABLED |
10770 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010771 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010772 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010773 I40E_FLAG_SRIOV_ENABLED |
10774 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010775 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10776 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010777 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010778 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010779 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010780 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010781 queues_left -= pf->num_lan_qps;
10782
10783 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010784 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000010785 I40E_FLAG_FD_SB_ENABLED |
10786 I40E_FLAG_FD_ATR_ENABLED |
10787 I40E_FLAG_DCB_ENABLED |
10788 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010789 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010790 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010791 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010792 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010793 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10794 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010795 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10796 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010797 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10798 num_online_cpus());
10799 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10800 pf->hw.func_caps.num_tx_qp);
10801
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010802 queues_left -= pf->num_lan_qps;
10803 }
10804
10805 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10806 if (queues_left > 1) {
10807 queues_left -= 1; /* save 1 queue for FD */
10808 } else {
10809 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10810 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10811 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010812 }
10813
10814 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10815 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010816 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10817 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010818 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10819 }
10820
10821 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10822 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10823 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10824 (queues_left / pf->num_vmdq_qps));
10825 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10826 }
10827
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010828 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010829 dev_dbg(&pf->pdev->dev,
10830 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10831 pf->hw.func_caps.num_tx_qp,
10832 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010833 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10834 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10835 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010836}
10837
10838/**
10839 * i40e_setup_pf_filter_control - Setup PF static filter control
10840 * @pf: PF to be setup
10841 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010842 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010843 * settings. If PE/FCoE are enabled then it will also set the per PF
10844 * based filter sizes required for them. It also enables Flow director,
10845 * ethertype and macvlan type filter settings for the pf.
10846 *
10847 * Returns 0 on success, negative on failure
10848 **/
10849static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10850{
10851 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10852
10853 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10854
10855 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010856 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010857 settings->enable_fdir = true;
10858
10859 /* Ethtype and MACVLAN filters enabled for PF */
10860 settings->enable_ethtype = true;
10861 settings->enable_macvlan = true;
10862
10863 if (i40e_set_filter_control(&pf->hw, settings))
10864 return -ENOENT;
10865
10866 return 0;
10867}
10868
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010869#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010870#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010871static void i40e_print_features(struct i40e_pf *pf)
10872{
10873 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010874 char *buf;
10875 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010876
Joe Perches3b195842015-12-03 04:20:57 -080010877 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10878 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010879 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010880
Joe Perches3b195842015-12-03 04:20:57 -080010881 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010882#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010883 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010884#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010885 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010886 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010887 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010888 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010889 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010890 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010891 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010892 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010893 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10894 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010895 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010896 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010897 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010898 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010899 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010900 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010901 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010902 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010903 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010904 else
Joe Perches3b195842015-12-03 04:20:57 -080010905 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010906
Joe Perches3b195842015-12-03 04:20:57 -080010907 dev_info(&pf->pdev->dev, "%s\n", buf);
10908 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010909 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010910}
10911
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010912/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010913 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010914 * @pdev: PCI device information struct
10915 * @pf: board private structure
10916 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010917 * Look up the MAC address for the device. First we'll try
10918 * eth_platform_get_mac_address, which will check Open Firmware, or arch
10919 * specific fallback. Otherwise, we'll default to the stored value in
10920 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010921 **/
10922static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10923{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010924 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
10925 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010926}
10927
10928/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010929 * i40e_probe - Device initialization routine
10930 * @pdev: PCI device information struct
10931 * @ent: entry in i40e_pci_tbl
10932 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010933 * i40e_probe initializes a PF identified by a pci_dev structure.
10934 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010935 * and a hardware reset occur.
10936 *
10937 * Returns 0 on success, negative on failure
10938 **/
10939static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10940{
Catherine Sullivane8278452015-02-06 08:52:08 +000010941 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010942 struct i40e_pf *pf;
10943 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010944 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010945 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010946 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010947 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010948 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010949 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010950 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010951
10952 err = pci_enable_device_mem(pdev);
10953 if (err)
10954 return err;
10955
10956 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010957 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010958 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010959 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10960 if (err) {
10961 dev_err(&pdev->dev,
10962 "DMA configuration failed: 0x%x\n", err);
10963 goto err_dma;
10964 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010965 }
10966
10967 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010968 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010969 if (err) {
10970 dev_info(&pdev->dev,
10971 "pci_request_selected_regions failed %d\n", err);
10972 goto err_pci_reg;
10973 }
10974
10975 pci_enable_pcie_error_reporting(pdev);
10976 pci_set_master(pdev);
10977
10978 /* Now that we have a PCI connection, we need to do the
10979 * low level device setup. This is primarily setting up
10980 * the Admin Queue structures and then querying for the
10981 * device's current profile information.
10982 */
10983 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10984 if (!pf) {
10985 err = -ENOMEM;
10986 goto err_pf_alloc;
10987 }
10988 pf->next_vsi = 0;
10989 pf->pdev = pdev;
10990 set_bit(__I40E_DOWN, &pf->state);
10991
10992 hw = &pf->hw;
10993 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010994
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010995 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10996 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010997
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010998 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010999 if (!hw->hw_addr) {
11000 err = -EIO;
11001 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11002 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011003 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011004 goto err_ioremap;
11005 }
11006 hw->vendor_id = pdev->vendor;
11007 hw->device_id = pdev->device;
11008 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11009 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11010 hw->subsystem_device_id = pdev->subsystem_device;
11011 hw->bus.device = PCI_SLOT(pdev->devfn);
11012 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011013 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011014 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011015
Jacob Keller0e588de2017-02-06 14:38:50 -080011016 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11017 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11018
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011019 /* set up the locks for the AQ, do this only once in probe
11020 * and destroy them only once in remove
11021 */
11022 mutex_init(&hw->aq.asq_mutex);
11023 mutex_init(&hw->aq.arq_mutex);
11024
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011025 pf->msg_enable = netif_msg_init(debug,
11026 NETIF_MSG_DRV |
11027 NETIF_MSG_PROBE |
11028 NETIF_MSG_LINK);
11029 if (debug < -1)
11030 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011031
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011032 /* do a special CORER for clearing PXE mode once at init */
11033 if (hw->revision_id == 0 &&
11034 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11035 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11036 i40e_flush(hw);
11037 msleep(200);
11038 pf->corer_count++;
11039
11040 i40e_clear_pxe_mode(hw);
11041 }
11042
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011044 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011045 err = i40e_pf_reset(hw);
11046 if (err) {
11047 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11048 goto err_pf_reset;
11049 }
11050 pf->pfr_count++;
11051
11052 hw->aq.num_arq_entries = I40E_AQ_LEN;
11053 hw->aq.num_asq_entries = I40E_AQ_LEN;
11054 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11055 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11056 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011057
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011058 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011059 "%s-%s:misc",
11060 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011061
11062 err = i40e_init_shared_code(hw);
11063 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011064 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11065 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011066 goto err_pf_reset;
11067 }
11068
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011069 /* set up a default setting for link flow control */
11070 pf->hw.fc.requested_mode = I40E_FC_NONE;
11071
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011072 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011073 if (err) {
11074 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11075 dev_info(&pdev->dev,
11076 "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");
11077 else
11078 dev_info(&pdev->dev,
11079 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11080
11081 goto err_pf_reset;
11082 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011083
Shannon Nelson6dec1012015-09-28 14:12:30 -040011084 /* provide nvm, fw, api versions */
11085 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11086 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11087 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11088 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011089
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011090 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11091 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011092 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011093 "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");
11094 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11095 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011096 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011097 "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 +000011098
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011099 i40e_verify_eeprom(pf);
11100
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011101 /* Rev 0 hardware was never productized */
11102 if (hw->revision_id < 1)
11103 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");
11104
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011105 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011106 err = i40e_get_capabilities(pf);
11107 if (err)
11108 goto err_adminq_setup;
11109
11110 err = i40e_sw_init(pf);
11111 if (err) {
11112 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11113 goto err_sw_init;
11114 }
11115
11116 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011117 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011118 if (err) {
11119 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11120 goto err_init_lan_hmc;
11121 }
11122
11123 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11124 if (err) {
11125 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11126 err = -ENOENT;
11127 goto err_configure_lan_hmc;
11128 }
11129
Neerav Parikhb686ece2014-12-14 01:55:11 +000011130 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11131 * Ignore error return codes because if it was already disabled via
11132 * hardware settings this will fail
11133 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011134 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011135 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11136 i40e_aq_stop_lldp(hw, true, NULL);
11137 }
11138
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011139 /* allow a platform config to override the HW addr */
11140 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011141
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011142 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011143 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11144 err = -EIO;
11145 goto err_mac_addr;
11146 }
11147 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011148 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011149 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11150 if (is_valid_ether_addr(hw->mac.port_addr))
11151 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011152
11153 pci_set_drvdata(pdev, pf);
11154 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011155#ifdef CONFIG_I40E_DCB
11156 err = i40e_init_pf_dcb(pf);
11157 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011158 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011159 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011160 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011161 }
11162#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011163
11164 /* set up periodic task facility */
11165 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11166 pf->service_timer_period = HZ;
11167
11168 INIT_WORK(&pf->service_task, i40e_service_task);
11169 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011170
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011171 /* NVM bit on means WoL disabled for the port */
11172 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011173 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011174 pf->wol_en = false;
11175 else
11176 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011177 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011179 /* set up the main switch operations */
11180 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011181 err = i40e_init_interrupt_scheme(pf);
11182 if (err)
11183 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011184
Mitch Williams505682c2014-05-20 08:01:37 +000011185 /* The number of VSIs reported by the FW is the minimum guaranteed
11186 * to us; HW supports far more and we share the remaining pool with
11187 * the other PFs. We allocate space for more than the guarantee with
11188 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011189 */
Mitch Williams505682c2014-05-20 08:01:37 +000011190 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11191 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11192 else
11193 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11194
11195 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011196 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11197 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011198 if (!pf->vsi) {
11199 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011200 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011202
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011203#ifdef CONFIG_PCI_IOV
11204 /* prep for VF support */
11205 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11206 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11207 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11208 if (pci_num_vf(pdev))
11209 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11210 }
11211#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011212 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011213 if (err) {
11214 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11215 goto err_vsis;
11216 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011217
11218 /* Make sure flow control is set according to current settings */
11219 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11220 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11221 dev_dbg(&pf->pdev->dev,
11222 "Set fc with err %s aq_err %s on get_phy_cap\n",
11223 i40e_stat_str(hw, err),
11224 i40e_aq_str(hw, hw->aq.asq_last_status));
11225 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11226 dev_dbg(&pf->pdev->dev,
11227 "Set fc with err %s aq_err %s on set_phy_config\n",
11228 i40e_stat_str(hw, err),
11229 i40e_aq_str(hw, hw->aq.asq_last_status));
11230 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11231 dev_dbg(&pf->pdev->dev,
11232 "Set fc with err %s aq_err %s on get_link_info\n",
11233 i40e_stat_str(hw, err),
11234 i40e_aq_str(hw, hw->aq.asq_last_status));
11235
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011236 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011237 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011238 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11239 i40e_vsi_open(pf->vsi[i]);
11240 break;
11241 }
11242 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011243
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011244 /* The driver only wants link up/down and module qualification
11245 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011246 */
11247 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011248 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011249 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011250 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011251 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011252 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11253 i40e_stat_str(&pf->hw, err),
11254 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011255
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011256 /* Reconfigure hardware for allowing smaller MSS in the case
11257 * of TSO, so that we avoid the MDD being fired and causing
11258 * a reset in the case of small MSS+TSO.
11259 */
11260 val = rd32(hw, I40E_REG_MSS);
11261 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11262 val &= ~I40E_REG_MSS_MIN_MASK;
11263 val |= I40E_64BYTE_MSS;
11264 wr32(hw, I40E_REG_MSS, val);
11265 }
11266
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011267 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011268 msleep(75);
11269 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11270 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011271 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11272 i40e_stat_str(&pf->hw, err),
11273 i40e_aq_str(&pf->hw,
11274 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011275 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011276 /* The main driver is (mostly) up and happy. We need to set this state
11277 * before setting up the misc vector or we get a race and the vector
11278 * ends up disabled forever.
11279 */
11280 clear_bit(__I40E_DOWN, &pf->state);
11281
11282 /* In case of MSIX we are going to setup the misc vector right here
11283 * to handle admin queue events etc. In case of legacy and MSI
11284 * the misc functionality and queue processing is combined in
11285 * the same vector and that gets setup at open.
11286 */
11287 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11288 err = i40e_setup_misc_vector(pf);
11289 if (err) {
11290 dev_info(&pdev->dev,
11291 "setup of misc vector failed: %d\n", err);
11292 goto err_vsis;
11293 }
11294 }
11295
Greg Rosedf805f62014-04-04 04:43:16 +000011296#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011297 /* prep for VF support */
11298 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011299 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11300 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011301 /* disable link interrupts for VFs */
11302 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11303 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11304 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11305 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011306
11307 if (pci_num_vf(pdev)) {
11308 dev_info(&pdev->dev,
11309 "Active VFs found, allocating resources.\n");
11310 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11311 if (err)
11312 dev_info(&pdev->dev,
11313 "Error %d allocating resources for existing VFs\n",
11314 err);
11315 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011316 }
Greg Rosedf805f62014-04-04 04:43:16 +000011317#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011318
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011319 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11320 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11321 pf->num_iwarp_msix,
11322 I40E_IWARP_IRQ_PILE_ID);
11323 if (pf->iwarp_base_vector < 0) {
11324 dev_info(&pdev->dev,
11325 "failed to get tracking for %d vectors for IWARP err=%d\n",
11326 pf->num_iwarp_msix, pf->iwarp_base_vector);
11327 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11328 }
11329 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011330
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011331 i40e_dbg_pf_init(pf);
11332
11333 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011334 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011335
11336 /* since everything's happy, start the service_task timer */
11337 mod_timer(&pf->service_timer,
11338 round_jiffies(jiffies + pf->service_timer_period));
11339
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011340 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011341 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11342 err = i40e_lan_add_device(pf);
11343 if (err)
11344 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11345 err);
11346 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011347
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011348#define PCI_SPEED_SIZE 8
11349#define PCI_WIDTH_SIZE 8
11350 /* Devices on the IOSF bus do not have this information
11351 * and will report PCI Gen 1 x 1 by default so don't bother
11352 * checking them.
11353 */
11354 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11355 char speed[PCI_SPEED_SIZE] = "Unknown";
11356 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011357
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011358 /* Get the negotiated link width and speed from PCI config
11359 * space
11360 */
11361 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11362 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011363
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011364 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011365
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011366 switch (hw->bus.speed) {
11367 case i40e_bus_speed_8000:
11368 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11369 case i40e_bus_speed_5000:
11370 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11371 case i40e_bus_speed_2500:
11372 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11373 default:
11374 break;
11375 }
11376 switch (hw->bus.width) {
11377 case i40e_bus_width_pcie_x8:
11378 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11379 case i40e_bus_width_pcie_x4:
11380 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11381 case i40e_bus_width_pcie_x2:
11382 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11383 case i40e_bus_width_pcie_x1:
11384 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11385 default:
11386 break;
11387 }
11388
11389 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11390 speed, width);
11391
11392 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11393 hw->bus.speed < i40e_bus_speed_8000) {
11394 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11395 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11396 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011397 }
11398
Catherine Sullivane8278452015-02-06 08:52:08 +000011399 /* get the requested speeds from the fw */
11400 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11401 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011402 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11403 i40e_stat_str(&pf->hw, err),
11404 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011405 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11406
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011407 /* get the supported phy types from the fw */
11408 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11409 if (err)
11410 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11411 i40e_stat_str(&pf->hw, err),
11412 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011413
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011414 /* Add a filter to drop all Flow control frames from any VSI from being
11415 * transmitted. By doing so we stop a malicious VF from sending out
11416 * PAUSE or PFC frames and potentially controlling traffic for other
11417 * PF/VF VSIs.
11418 * The FW can still send Flow control frames if enabled.
11419 */
11420 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11421 pf->main_vsi_seid);
11422
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011423 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011424 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11425 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011426 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11427 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011428 /* print a string summarizing features */
11429 i40e_print_features(pf);
11430
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011431 return 0;
11432
11433 /* Unwind what we've done if something failed in the setup */
11434err_vsis:
11435 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011436 i40e_clear_interrupt_scheme(pf);
11437 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011438err_switch_setup:
11439 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011440 del_timer_sync(&pf->service_timer);
11441err_mac_addr:
11442err_configure_lan_hmc:
11443 (void)i40e_shutdown_lan_hmc(hw);
11444err_init_lan_hmc:
11445 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446err_sw_init:
11447err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011448err_pf_reset:
11449 iounmap(hw->hw_addr);
11450err_ioremap:
11451 kfree(pf);
11452err_pf_alloc:
11453 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011454 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011455err_pci_reg:
11456err_dma:
11457 pci_disable_device(pdev);
11458 return err;
11459}
11460
11461/**
11462 * i40e_remove - Device removal routine
11463 * @pdev: PCI device information struct
11464 *
11465 * i40e_remove is called by the PCI subsystem to alert the driver
11466 * that is should release a PCI device. This could be caused by a
11467 * Hot-Plug event, or because the driver is going to be removed from
11468 * memory.
11469 **/
11470static void i40e_remove(struct pci_dev *pdev)
11471{
11472 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011473 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011474 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011475 int i;
11476
11477 i40e_dbg_pf_exit(pf);
11478
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011479 i40e_ptp_stop(pf);
11480
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011481 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011482 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11483 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011485 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011486 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011487 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011488 if (pf->service_timer.data)
11489 del_timer_sync(&pf->service_timer);
11490 if (pf->service_task.func)
11491 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011492
Mitch Williams921c4672017-03-30 00:46:08 -070011493 /* Client close must be called explicitly here because the timer
11494 * has been stopped.
11495 */
11496 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11497
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011498 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11499 i40e_free_vfs(pf);
11500 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11501 }
11502
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011503 i40e_fdir_teardown(pf);
11504
11505 /* If there is a switch structure or any orphans, remove them.
11506 * This will leave only the PF's VSI remaining.
11507 */
11508 for (i = 0; i < I40E_MAX_VEB; i++) {
11509 if (!pf->veb[i])
11510 continue;
11511
11512 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11513 pf->veb[i]->uplink_seid == 0)
11514 i40e_switch_branch_release(pf->veb[i]);
11515 }
11516
11517 /* Now we can shutdown the PF's VSI, just before we kill
11518 * adminq and hmc.
11519 */
11520 if (pf->vsi[pf->lan_vsi])
11521 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11522
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011523 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011524 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11525 ret_code = i40e_lan_del_device(pf);
11526 if (ret_code)
11527 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11528 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011529 }
11530
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011531 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011532 if (hw->hmc.hmc_obj) {
11533 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011534 if (ret_code)
11535 dev_warn(&pdev->dev,
11536 "Failed to destroy the HMC resources: %d\n",
11537 ret_code);
11538 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011539
11540 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011541 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011542
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011543 /* destroy the locks only once, here */
11544 mutex_destroy(&hw->aq.arq_mutex);
11545 mutex_destroy(&hw->aq.asq_mutex);
11546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011547 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11548 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011549 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011550 if (pf->vsi[i]) {
11551 i40e_vsi_clear_rings(pf->vsi[i]);
11552 i40e_vsi_clear(pf->vsi[i]);
11553 pf->vsi[i] = NULL;
11554 }
11555 }
11556
11557 for (i = 0; i < I40E_MAX_VEB; i++) {
11558 kfree(pf->veb[i]);
11559 pf->veb[i] = NULL;
11560 }
11561
11562 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011563 kfree(pf->vsi);
11564
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011565 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011566 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011567 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011568
11569 pci_disable_pcie_error_reporting(pdev);
11570 pci_disable_device(pdev);
11571}
11572
11573/**
11574 * i40e_pci_error_detected - warning that something funky happened in PCI land
11575 * @pdev: PCI device information struct
11576 *
11577 * Called to warn that something happened and the error handling steps
11578 * are in progress. Allows the driver to quiesce things, be ready for
11579 * remediation.
11580 **/
11581static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11582 enum pci_channel_state error)
11583{
11584 struct i40e_pf *pf = pci_get_drvdata(pdev);
11585
11586 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11587
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011588 if (!pf) {
11589 dev_info(&pdev->dev,
11590 "Cannot recover - error happened during device probe\n");
11591 return PCI_ERS_RESULT_DISCONNECT;
11592 }
11593
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011594 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011595 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11596 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011597 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011598 rtnl_unlock();
11599 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011600
11601 /* Request a slot reset */
11602 return PCI_ERS_RESULT_NEED_RESET;
11603}
11604
11605/**
11606 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11607 * @pdev: PCI device information struct
11608 *
11609 * Called to find if the driver can work with the device now that
11610 * the pci slot has been reset. If a basic connection seems good
11611 * (registers are readable and have sane content) then return a
11612 * happy little PCI_ERS_RESULT_xxx.
11613 **/
11614static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11615{
11616 struct i40e_pf *pf = pci_get_drvdata(pdev);
11617 pci_ers_result_t result;
11618 int err;
11619 u32 reg;
11620
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011621 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011622 if (pci_enable_device_mem(pdev)) {
11623 dev_info(&pdev->dev,
11624 "Cannot re-enable PCI device after reset.\n");
11625 result = PCI_ERS_RESULT_DISCONNECT;
11626 } else {
11627 pci_set_master(pdev);
11628 pci_restore_state(pdev);
11629 pci_save_state(pdev);
11630 pci_wake_from_d3(pdev, false);
11631
11632 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11633 if (reg == 0)
11634 result = PCI_ERS_RESULT_RECOVERED;
11635 else
11636 result = PCI_ERS_RESULT_DISCONNECT;
11637 }
11638
11639 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11640 if (err) {
11641 dev_info(&pdev->dev,
11642 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11643 err);
11644 /* non-fatal, continue */
11645 }
11646
11647 return result;
11648}
11649
11650/**
11651 * i40e_pci_error_resume - restart operations after PCI error recovery
11652 * @pdev: PCI device information struct
11653 *
11654 * Called to allow the driver to bring things back up after PCI error
11655 * and/or reset recovery has finished.
11656 **/
11657static void i40e_pci_error_resume(struct pci_dev *pdev)
11658{
11659 struct i40e_pf *pf = pci_get_drvdata(pdev);
11660
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011661 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011662 if (test_bit(__I40E_SUSPENDED, &pf->state))
11663 return;
11664
11665 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011666 i40e_handle_reset_warning(pf, true);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011667 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011668}
11669
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011670/**
Joshua Hay1d680052016-12-12 15:44:08 -080011671 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11672 * using the mac_address_write admin q function
11673 * @pf: pointer to i40e_pf struct
11674 **/
11675static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11676{
11677 struct i40e_hw *hw = &pf->hw;
11678 i40e_status ret;
11679 u8 mac_addr[6];
11680 u16 flags = 0;
11681
11682 /* Get current MAC address in case it's an LAA */
11683 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11684 ether_addr_copy(mac_addr,
11685 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11686 } else {
11687 dev_err(&pf->pdev->dev,
11688 "Failed to retrieve MAC address; using default\n");
11689 ether_addr_copy(mac_addr, hw->mac.addr);
11690 }
11691
11692 /* The FW expects the mac address write cmd to first be called with
11693 * one of these flags before calling it again with the multicast
11694 * enable flags.
11695 */
11696 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11697
11698 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11699 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11700
11701 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11702 if (ret) {
11703 dev_err(&pf->pdev->dev,
11704 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11705 return;
11706 }
11707
11708 flags = I40E_AQC_MC_MAG_EN
11709 | I40E_AQC_WOL_PRESERVE_ON_PFR
11710 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11711 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11712 if (ret)
11713 dev_err(&pf->pdev->dev,
11714 "Failed to enable Multicast Magic Packet wake up\n");
11715}
11716
11717/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011718 * i40e_shutdown - PCI callback for shutting down
11719 * @pdev: PCI device information struct
11720 **/
11721static void i40e_shutdown(struct pci_dev *pdev)
11722{
11723 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011724 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011725
11726 set_bit(__I40E_SUSPENDED, &pf->state);
11727 set_bit(__I40E_DOWN, &pf->state);
11728 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011729 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011730 rtnl_unlock();
11731
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011732 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11733 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11734
Catherine Sullivan02b42492015-07-10 19:35:59 -040011735 del_timer_sync(&pf->service_timer);
11736 cancel_work_sync(&pf->service_task);
11737 i40e_fdir_teardown(pf);
11738
Mitch Williams921c4672017-03-30 00:46:08 -070011739 /* Client close must be called explicitly here because the timer
11740 * has been stopped.
11741 */
11742 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11743
Joshua Hay1d680052016-12-12 15:44:08 -080011744 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11745 i40e_enable_mc_magic_wake(pf);
11746
Catherine Sullivan02b42492015-07-10 19:35:59 -040011747 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011748 i40e_prep_for_reset(pf, true);
Catherine Sullivan02b42492015-07-10 19:35:59 -040011749 rtnl_unlock();
11750
11751 wr32(hw, I40E_PFPM_APM,
11752 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11753 wr32(hw, I40E_PFPM_WUFC,
11754 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11755
Shannon Nelsone1477582015-02-21 06:44:33 +000011756 i40e_clear_interrupt_scheme(pf);
11757
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011758 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011759 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011760 pci_set_power_state(pdev, PCI_D3hot);
11761 }
11762}
11763
11764#ifdef CONFIG_PM
11765/**
11766 * i40e_suspend - PCI callback for moving to D3
11767 * @pdev: PCI device information struct
11768 **/
11769static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11770{
11771 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011772 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011773 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011774
11775 set_bit(__I40E_SUSPENDED, &pf->state);
11776 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011777
Joshua Hay1d680052016-12-12 15:44:08 -080011778 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11779 i40e_enable_mc_magic_wake(pf);
11780
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011781 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011782 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011783 rtnl_unlock();
11784
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011785 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11786 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11787
Greg Roseb33d3b72016-05-16 10:26:37 -070011788 i40e_stop_misc_vector(pf);
11789
Greg Rose059ff692016-05-16 10:26:38 -070011790 retval = pci_save_state(pdev);
11791 if (retval)
11792 return retval;
11793
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011794 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011795 pci_set_power_state(pdev, PCI_D3hot);
11796
Greg Rose059ff692016-05-16 10:26:38 -070011797 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011798}
11799
11800/**
11801 * i40e_resume - PCI callback for waking up from D3
11802 * @pdev: PCI device information struct
11803 **/
11804static int i40e_resume(struct pci_dev *pdev)
11805{
11806 struct i40e_pf *pf = pci_get_drvdata(pdev);
11807 u32 err;
11808
11809 pci_set_power_state(pdev, PCI_D0);
11810 pci_restore_state(pdev);
11811 /* pci_restore_state() clears dev->state_saves, so
11812 * call pci_save_state() again to restore it.
11813 */
11814 pci_save_state(pdev);
11815
11816 err = pci_enable_device_mem(pdev);
11817 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011818 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011819 return err;
11820 }
11821 pci_set_master(pdev);
11822
11823 /* no wakeup events while running */
11824 pci_wake_from_d3(pdev, false);
11825
11826 /* handling the reset will rebuild the device state */
11827 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11828 clear_bit(__I40E_DOWN, &pf->state);
11829 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011830 i40e_reset_and_rebuild(pf, false, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011831 rtnl_unlock();
11832 }
11833
11834 return 0;
11835}
11836
11837#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011838static const struct pci_error_handlers i40e_err_handler = {
11839 .error_detected = i40e_pci_error_detected,
11840 .slot_reset = i40e_pci_error_slot_reset,
11841 .resume = i40e_pci_error_resume,
11842};
11843
11844static struct pci_driver i40e_driver = {
11845 .name = i40e_driver_name,
11846 .id_table = i40e_pci_tbl,
11847 .probe = i40e_probe,
11848 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011849#ifdef CONFIG_PM
11850 .suspend = i40e_suspend,
11851 .resume = i40e_resume,
11852#endif
11853 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011854 .err_handler = &i40e_err_handler,
11855 .sriov_configure = i40e_pci_sriov_configure,
11856};
11857
11858/**
11859 * i40e_init_module - Driver registration routine
11860 *
11861 * i40e_init_module is the first routine called when the driver is
11862 * loaded. All it does is register with the PCI subsystem.
11863 **/
11864static int __init i40e_init_module(void)
11865{
11866 pr_info("%s: %s - version %s\n", i40e_driver_name,
11867 i40e_driver_string, i40e_driver_version_str);
11868 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011869
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011870 /* we will see if single thread per module is enough for now,
11871 * it can't be any worse than using the system workqueue which
11872 * was already single threaded
11873 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011874 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11875 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011876 if (!i40e_wq) {
11877 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11878 return -ENOMEM;
11879 }
11880
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011881 i40e_dbg_init();
11882 return pci_register_driver(&i40e_driver);
11883}
11884module_init(i40e_init_module);
11885
11886/**
11887 * i40e_exit_module - Driver exit cleanup routine
11888 *
11889 * i40e_exit_module is called just before the driver is removed
11890 * from memory.
11891 **/
11892static void __exit i40e_exit_module(void)
11893{
11894 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011895 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011896 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011897}
11898module_exit(i40e_exit_module);