blob: cabd72854274e611bb8d3b62133b3d2e6e521959 [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>
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000035
36const char i40e_driver_name[] = "i40e";
37static const char i40e_driver_string[] =
38 "Intel(R) Ethernet Connection XL710 Network Driver";
39
40#define DRV_KERN "-k"
41
Catherine Sullivane8e724d2014-07-10 07:58:26 +000042#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070043#define DRV_VERSION_MINOR 6
Bimmy Pujari36023862016-11-08 13:05:19 -080044#define DRV_VERSION_BUILD 25
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000045#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080049static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000056static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080060static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080061static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020070static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080071 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080072 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080073 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
88 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000091 /* required last entry */
92 {0, }
93};
94MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95
96#define I40E_MAX_VF_COUNT 128
97static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040098module_param(debug, uint, 0);
99MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100
101MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103MODULE_LICENSE("GPL");
104MODULE_VERSION(DRV_VERSION);
105
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800106static struct workqueue_struct *i40e_wq;
107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000108/**
109 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
110 * @hw: pointer to the HW structure
111 * @mem: ptr to mem struct to fill out
112 * @size: size of memory requested
113 * @alignment: what to align the allocation to
114 **/
115int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
116 u64 size, u32 alignment)
117{
118 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
119
120 mem->size = ALIGN(size, alignment);
121 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
122 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000123 if (!mem->va)
124 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000125
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000126 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000127}
128
129/**
130 * i40e_free_dma_mem_d - OS specific memory free for shared code
131 * @hw: pointer to the HW structure
132 * @mem: ptr to mem struct to free
133 **/
134int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
135{
136 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137
138 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
139 mem->va = NULL;
140 mem->pa = 0;
141 mem->size = 0;
142
143 return 0;
144}
145
146/**
147 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to fill out
150 * @size: size of memory requested
151 **/
152int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
153 u32 size)
154{
155 mem->size = size;
156 mem->va = kzalloc(size, GFP_KERNEL);
157
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000158 if (!mem->va)
159 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000160
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000161 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000162}
163
164/**
165 * i40e_free_virt_mem_d - OS specific memory free for shared code
166 * @hw: pointer to the HW structure
167 * @mem: ptr to mem struct to free
168 **/
169int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
170{
171 /* it's ok to kfree a NULL pointer */
172 kfree(mem->va);
173 mem->va = NULL;
174 mem->size = 0;
175
176 return 0;
177}
178
179/**
180 * i40e_get_lump - find a lump of free generic resource
181 * @pf: board private structure
182 * @pile: the pile of resource to search
183 * @needed: the number of items needed
184 * @id: an owner id to stick on the items assigned
185 *
186 * Returns the base item index of the lump, or negative for error
187 *
188 * The search_hint trick and lack of advanced fit-finding only work
189 * because we're highly likely to have all the same size lump requests.
190 * Linear search time and any fragmentation should be minimal.
191 **/
192static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
193 u16 needed, u16 id)
194{
195 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000196 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000197
198 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
199 dev_info(&pf->pdev->dev,
200 "param err: pile=%p needed=%d id=0x%04x\n",
201 pile, needed, id);
202 return -EINVAL;
203 }
204
205 /* start the linear search with an imperfect hint */
206 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000207 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000208 /* skip already allocated entries */
209 if (pile->list[i] & I40E_PILE_VALID_BIT) {
210 i++;
211 continue;
212 }
213
214 /* do we have enough in this lump? */
215 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
216 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
217 break;
218 }
219
220 if (j == needed) {
221 /* there was enough, so assign it to the requestor */
222 for (j = 0; j < needed; j++)
223 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
224 ret = i;
225 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000226 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000227 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400228
229 /* not enough, so skip over it and continue looking */
230 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000231 }
232
233 return ret;
234}
235
236/**
237 * i40e_put_lump - return a lump of generic resource
238 * @pile: the pile of resource to search
239 * @index: the base item index
240 * @id: the owner id of the items assigned
241 *
242 * Returns the count of items in the lump
243 **/
244static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
245{
246 int valid_id = (id | I40E_PILE_VALID_BIT);
247 int count = 0;
248 int i;
249
250 if (!pile || index >= pile->num_entries)
251 return -EINVAL;
252
253 for (i = index;
254 i < pile->num_entries && pile->list[i] == valid_id;
255 i++) {
256 pile->list[i] = 0;
257 count++;
258 }
259
260 if (count && index < pile->search_hint)
261 pile->search_hint = index;
262
263 return count;
264}
265
266/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700267 * i40e_find_vsi_from_id - searches for the vsi with the given id
268 * @pf - the pf structure to search for the vsi
269 * @id - id of the vsi it is searching for
270 **/
271struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
272{
273 int i;
274
275 for (i = 0; i < pf->num_alloc_vsi; i++)
276 if (pf->vsi[i] && (pf->vsi[i]->id == id))
277 return pf->vsi[i];
278
279 return NULL;
280}
281
282/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000283 * i40e_service_event_schedule - Schedule the service task to wake up
284 * @pf: board private structure
285 *
286 * If not already scheduled, this puts the task into the work queue
287 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600288void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000289{
290 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800291 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800292 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000293}
294
295/**
296 * i40e_tx_timeout - Respond to a Tx Hang
297 * @netdev: network interface device structure
298 *
299 * If any port has noticed a Tx timeout, it is likely that the whole
300 * device is munged, not just the one netdev port, so go for the full
301 * reset.
302 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700303#ifdef I40E_FCOE
304void i40e_tx_timeout(struct net_device *netdev)
305#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000306static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700307#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000308{
309 struct i40e_netdev_priv *np = netdev_priv(netdev);
310 struct i40e_vsi *vsi = np->vsi;
311 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400312 struct i40e_ring *tx_ring = NULL;
313 unsigned int i, hung_queue = 0;
314 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000315
316 pf->tx_timeout_count++;
317
Kiran Patilb03a8c12015-09-24 18:13:15 -0400318 /* find the stopped queue the same way the stack does */
319 for (i = 0; i < netdev->num_tx_queues; i++) {
320 struct netdev_queue *q;
321 unsigned long trans_start;
322
323 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200324 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400325 if (netif_xmit_stopped(q) &&
326 time_after(jiffies,
327 (trans_start + netdev->watchdog_timeo))) {
328 hung_queue = i;
329 break;
330 }
331 }
332
333 if (i == netdev->num_tx_queues) {
334 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
335 } else {
336 /* now that we have an index, find the tx_ring struct */
337 for (i = 0; i < vsi->num_queue_pairs; i++) {
338 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 if (hung_queue ==
340 vsi->tx_rings[i]->queue_index) {
341 tx_ring = vsi->tx_rings[i];
342 break;
343 }
344 }
345 }
346 }
347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000348 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400349 pf->tx_timeout_recovery_level = 1; /* reset after some time */
350 else if (time_before(jiffies,
351 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
352 return; /* don't do any new action before the next timeout */
353
354 if (tx_ring) {
355 head = i40e_get_head(tx_ring);
356 /* Read interrupt register */
357 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 val = rd32(&pf->hw,
359 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 tx_ring->vsi->base_vector - 1));
361 else
362 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363
364 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",
365 vsi->seid, hung_queue, tx_ring->next_to_clean,
366 head, tx_ring->next_to_use,
367 readl(tx_ring->tail), val);
368 }
369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000370 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400371 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000373
374 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 case 1:
376 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
377 break;
378 case 2:
379 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
380 break;
381 case 3:
382 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
383 break;
384 default:
385 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000386 break;
387 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389 i40e_service_event_schedule(pf);
390 pf->tx_timeout_recovery_level++;
391}
392
393/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 * i40e_get_vsi_stats_struct - Get System Network Statistics
395 * @vsi: the VSI we care about
396 *
397 * Returns the address of the device statistics structure.
398 * The statistics are actually updated from the service task.
399 **/
400struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401{
402 return &vsi->net_stats;
403}
404
405/**
406 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
407 * @netdev: network interface device structure
408 *
409 * Returns the address of the device statistics structure.
410 * The statistics are actually updated from the service task.
411 **/
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800412#ifndef I40E_FCOE
413static
Vasu Dev38e00432014-08-01 13:27:03 -0700414#endif
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800415void i40e_get_netdev_stats_struct(struct net_device *netdev,
416 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000417{
418 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000419 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000420 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000421 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
422 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000423
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000424 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800425 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000426
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800427 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800428 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800429
Alexander Duyck980e9b12013-09-28 06:01:03 +0000430 rcu_read_lock();
431 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000432 u64 bytes, packets;
433 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000434
Alexander Duyck980e9b12013-09-28 06:01:03 +0000435 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
436 if (!tx_ring)
437 continue;
438
439 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700440 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000441 packets = tx_ring->stats.packets;
442 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700443 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444
445 stats->tx_packets += packets;
446 stats->tx_bytes += bytes;
447 rx_ring = &tx_ring[1];
448
449 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700450 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000451 packets = rx_ring->stats.packets;
452 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700453 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000454
455 stats->rx_packets += packets;
456 stats->rx_bytes += bytes;
457 }
458 rcu_read_unlock();
459
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000460 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000461 stats->multicast = vsi_stats->multicast;
462 stats->tx_errors = vsi_stats->tx_errors;
463 stats->tx_dropped = vsi_stats->tx_dropped;
464 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400465 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000466 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
467 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000468}
469
470/**
471 * i40e_vsi_reset_stats - Resets all stats of the given vsi
472 * @vsi: the VSI to have its stats reset
473 **/
474void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
475{
476 struct rtnl_link_stats64 *ns;
477 int i;
478
479 if (!vsi)
480 return;
481
482 ns = i40e_get_vsi_stats_struct(vsi);
483 memset(ns, 0, sizeof(*ns));
484 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
485 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
486 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000487 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000488 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400489 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000490 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400491 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000492 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400493 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000494 sizeof(vsi->tx_rings[i]->stats));
495 memset(&vsi->tx_rings[i]->tx_stats, 0,
496 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000497 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000498 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000499 vsi->stat_offsets_loaded = false;
500}
501
502/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000503 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000504 * @pf: the PF to be reset
505 **/
506void i40e_pf_reset_stats(struct i40e_pf *pf)
507{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000508 int i;
509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000510 memset(&pf->stats, 0, sizeof(pf->stats));
511 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
512 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000513
514 for (i = 0; i < I40E_MAX_VEB; i++) {
515 if (pf->veb[i]) {
516 memset(&pf->veb[i]->stats, 0,
517 sizeof(pf->veb[i]->stats));
518 memset(&pf->veb[i]->stats_offsets, 0,
519 sizeof(pf->veb[i]->stats_offsets));
520 pf->veb[i]->stat_offsets_loaded = false;
521 }
522 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700523 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000524}
525
526/**
527 * i40e_stat_update48 - read and update a 48 bit stat from the chip
528 * @hw: ptr to the hardware info
529 * @hireg: the high 32 bit reg to read
530 * @loreg: the low 32 bit reg to read
531 * @offset_loaded: has the initial offset been loaded yet
532 * @offset: ptr to current offset value
533 * @stat: ptr to the stat
534 *
535 * Since the device stats are not reset at PFReset, they likely will not
536 * be zeroed when the driver starts. We'll save the first values read
537 * and use them as offsets to be subtracted from the raw values in order
538 * to report stats that count from zero. In the process, we also manage
539 * the potential roll-over.
540 **/
541static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
542 bool offset_loaded, u64 *offset, u64 *stat)
543{
544 u64 new_data;
545
Shannon Nelsonab600852014-01-17 15:36:39 -0800546 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000547 new_data = rd32(hw, loreg);
548 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
549 } else {
550 new_data = rd64(hw, loreg);
551 }
552 if (!offset_loaded)
553 *offset = new_data;
554 if (likely(new_data >= *offset))
555 *stat = new_data - *offset;
556 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400557 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000558 *stat &= 0xFFFFFFFFFFFFULL;
559}
560
561/**
562 * i40e_stat_update32 - read and update a 32 bit stat from the chip
563 * @hw: ptr to the hardware info
564 * @reg: the hw reg to read
565 * @offset_loaded: has the initial offset been loaded yet
566 * @offset: ptr to current offset value
567 * @stat: ptr to the stat
568 **/
569static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
570 bool offset_loaded, u64 *offset, u64 *stat)
571{
572 u32 new_data;
573
574 new_data = rd32(hw, reg);
575 if (!offset_loaded)
576 *offset = new_data;
577 if (likely(new_data >= *offset))
578 *stat = (u32)(new_data - *offset);
579 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400580 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000581}
582
583/**
584 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
585 * @vsi: the VSI to be updated
586 **/
587void i40e_update_eth_stats(struct i40e_vsi *vsi)
588{
589 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
590 struct i40e_pf *pf = vsi->back;
591 struct i40e_hw *hw = &pf->hw;
592 struct i40e_eth_stats *oes;
593 struct i40e_eth_stats *es; /* device's eth stats */
594
595 es = &vsi->eth_stats;
596 oes = &vsi->eth_stats_offsets;
597
598 /* Gather up the stats that the hw collects */
599 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
600 vsi->stat_offsets_loaded,
601 &oes->tx_errors, &es->tx_errors);
602 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
603 vsi->stat_offsets_loaded,
604 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000605 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
606 vsi->stat_offsets_loaded,
607 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
608 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000611
612 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
613 I40E_GLV_GORCL(stat_idx),
614 vsi->stat_offsets_loaded,
615 &oes->rx_bytes, &es->rx_bytes);
616 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
617 I40E_GLV_UPRCL(stat_idx),
618 vsi->stat_offsets_loaded,
619 &oes->rx_unicast, &es->rx_unicast);
620 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
621 I40E_GLV_MPRCL(stat_idx),
622 vsi->stat_offsets_loaded,
623 &oes->rx_multicast, &es->rx_multicast);
624 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
625 I40E_GLV_BPRCL(stat_idx),
626 vsi->stat_offsets_loaded,
627 &oes->rx_broadcast, &es->rx_broadcast);
628
629 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
630 I40E_GLV_GOTCL(stat_idx),
631 vsi->stat_offsets_loaded,
632 &oes->tx_bytes, &es->tx_bytes);
633 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
634 I40E_GLV_UPTCL(stat_idx),
635 vsi->stat_offsets_loaded,
636 &oes->tx_unicast, &es->tx_unicast);
637 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
638 I40E_GLV_MPTCL(stat_idx),
639 vsi->stat_offsets_loaded,
640 &oes->tx_multicast, &es->tx_multicast);
641 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
642 I40E_GLV_BPTCL(stat_idx),
643 vsi->stat_offsets_loaded,
644 &oes->tx_broadcast, &es->tx_broadcast);
645 vsi->stat_offsets_loaded = true;
646}
647
648/**
649 * i40e_update_veb_stats - Update Switch component statistics
650 * @veb: the VEB being updated
651 **/
652static void i40e_update_veb_stats(struct i40e_veb *veb)
653{
654 struct i40e_pf *pf = veb->pf;
655 struct i40e_hw *hw = &pf->hw;
656 struct i40e_eth_stats *oes;
657 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400658 struct i40e_veb_tc_stats *veb_oes;
659 struct i40e_veb_tc_stats *veb_es;
660 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000661
662 idx = veb->stats_idx;
663 es = &veb->stats;
664 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400665 veb_es = &veb->tc_stats;
666 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000667
668 /* Gather up the stats that the hw collects */
669 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
670 veb->stat_offsets_loaded,
671 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000672 if (hw->revision_id > 0)
673 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
674 veb->stat_offsets_loaded,
675 &oes->rx_unknown_protocol,
676 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000677 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
678 veb->stat_offsets_loaded,
679 &oes->rx_bytes, &es->rx_bytes);
680 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
681 veb->stat_offsets_loaded,
682 &oes->rx_unicast, &es->rx_unicast);
683 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
684 veb->stat_offsets_loaded,
685 &oes->rx_multicast, &es->rx_multicast);
686 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
687 veb->stat_offsets_loaded,
688 &oes->rx_broadcast, &es->rx_broadcast);
689
690 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
691 veb->stat_offsets_loaded,
692 &oes->tx_bytes, &es->tx_bytes);
693 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
694 veb->stat_offsets_loaded,
695 &oes->tx_unicast, &es->tx_unicast);
696 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->tx_multicast, &es->tx_multicast);
699 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400702 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
703 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
704 I40E_GLVEBTC_RPCL(i, idx),
705 veb->stat_offsets_loaded,
706 &veb_oes->tc_rx_packets[i],
707 &veb_es->tc_rx_packets[i]);
708 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
709 I40E_GLVEBTC_RBCL(i, idx),
710 veb->stat_offsets_loaded,
711 &veb_oes->tc_rx_bytes[i],
712 &veb_es->tc_rx_bytes[i]);
713 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
714 I40E_GLVEBTC_TPCL(i, idx),
715 veb->stat_offsets_loaded,
716 &veb_oes->tc_tx_packets[i],
717 &veb_es->tc_tx_packets[i]);
718 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
719 I40E_GLVEBTC_TBCL(i, idx),
720 veb->stat_offsets_loaded,
721 &veb_oes->tc_tx_bytes[i],
722 &veb_es->tc_tx_bytes[i]);
723 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000724 veb->stat_offsets_loaded = true;
725}
726
Vasu Dev38e00432014-08-01 13:27:03 -0700727#ifdef I40E_FCOE
728/**
729 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
730 * @vsi: the VSI that is capable of doing FCoE
731 **/
732static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
733{
734 struct i40e_pf *pf = vsi->back;
735 struct i40e_hw *hw = &pf->hw;
736 struct i40e_fcoe_stats *ofs;
737 struct i40e_fcoe_stats *fs; /* device's eth stats */
738 int idx;
739
740 if (vsi->type != I40E_VSI_FCOE)
741 return;
742
Kiran Patil4147e2c2016-01-15 14:33:14 -0800743 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700744 fs = &vsi->fcoe_stats;
745 ofs = &vsi->fcoe_stats_offsets;
746
747 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
748 vsi->fcoe_stat_offsets_loaded,
749 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
750 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
751 vsi->fcoe_stat_offsets_loaded,
752 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
753 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
754 vsi->fcoe_stat_offsets_loaded,
755 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
756 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
757 vsi->fcoe_stat_offsets_loaded,
758 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
759 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
760 vsi->fcoe_stat_offsets_loaded,
761 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
762 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
763 vsi->fcoe_stat_offsets_loaded,
764 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
765 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->fcoe_last_error, &fs->fcoe_last_error);
768 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
771
772 vsi->fcoe_stat_offsets_loaded = true;
773}
774
775#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000776/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000777 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000778 * @vsi: the VSI to be updated
779 *
780 * There are a few instances where we store the same stat in a
781 * couple of different structs. This is partly because we have
782 * the netdev stats that need to be filled out, which is slightly
783 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000784 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000785 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000786static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000787{
788 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000789 struct rtnl_link_stats64 *ons;
790 struct rtnl_link_stats64 *ns; /* netdev stats */
791 struct i40e_eth_stats *oes;
792 struct i40e_eth_stats *es; /* device's eth stats */
793 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800794 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000795 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000796 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000797 u64 bytes, packets;
798 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400799 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400800 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 u64 rx_p, rx_b;
802 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 u16 q;
804
805 if (test_bit(__I40E_DOWN, &vsi->state) ||
806 test_bit(__I40E_CONFIG_BUSY, &pf->state))
807 return;
808
809 ns = i40e_get_vsi_stats_struct(vsi);
810 ons = &vsi->net_stats_offsets;
811 es = &vsi->eth_stats;
812 oes = &vsi->eth_stats_offsets;
813
814 /* Gather up the netdev and vsi stats that the driver collects
815 * on the fly during packet processing
816 */
817 rx_b = rx_p = 0;
818 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400819 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800820 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000821 rx_page = 0;
822 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000823 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000824 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000825 /* locate Tx ring */
826 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827
Alexander Duyck980e9b12013-09-28 06:01:03 +0000828 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700829 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000830 packets = p->stats.packets;
831 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700832 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000833 tx_b += bytes;
834 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 tx_restart += p->tx_stats.restart_queue;
836 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400837 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400838 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800839 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000840
841 /* Rx queue is part of the same block as Tx queue */
842 p = &p[1];
843 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700844 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000845 packets = p->stats.packets;
846 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700847 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000848 rx_b += bytes;
849 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000850 rx_buf += p->rx_stats.alloc_buff_failed;
851 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000852 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000853 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000854 vsi->tx_restart = tx_restart;
855 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400856 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400857 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800858 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 vsi->rx_page_failed = rx_page;
860 vsi->rx_buf_failed = rx_buf;
861
862 ns->rx_packets = rx_p;
863 ns->rx_bytes = rx_b;
864 ns->tx_packets = tx_p;
865 ns->tx_bytes = tx_b;
866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000867 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000869 ons->tx_errors = oes->tx_errors;
870 ns->tx_errors = es->tx_errors;
871 ons->multicast = oes->rx_multicast;
872 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000873 ons->rx_dropped = oes->rx_discards;
874 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000875 ons->tx_dropped = oes->tx_discards;
876 ns->tx_dropped = es->tx_discards;
877
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000878 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000879 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000880 ns->rx_crc_errors = pf->stats.crc_errors;
881 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
882 ns->rx_length_errors = pf->stats.rx_length_errors;
883 }
884}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000885
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000886/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000887 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000888 * @pf: the PF to be updated
889 **/
890static void i40e_update_pf_stats(struct i40e_pf *pf)
891{
892 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
893 struct i40e_hw_port_stats *nsd = &pf->stats;
894 struct i40e_hw *hw = &pf->hw;
895 u32 val;
896 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000897
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000898 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
899 I40E_GLPRT_GORCL(hw->port),
900 pf->stat_offsets_loaded,
901 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
902 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
903 I40E_GLPRT_GOTCL(hw->port),
904 pf->stat_offsets_loaded,
905 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
906 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->eth.rx_discards,
909 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000910 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
911 I40E_GLPRT_UPRCL(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.rx_unicast,
914 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000915 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
916 I40E_GLPRT_MPRCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.rx_multicast,
919 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000920 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
921 I40E_GLPRT_BPRCL(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->eth.rx_broadcast,
924 &nsd->eth.rx_broadcast);
925 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
926 I40E_GLPRT_UPTCL(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->eth.tx_unicast,
929 &nsd->eth.tx_unicast);
930 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
931 I40E_GLPRT_MPTCL(hw->port),
932 pf->stat_offsets_loaded,
933 &osd->eth.tx_multicast,
934 &nsd->eth.tx_multicast);
935 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
936 I40E_GLPRT_BPTCL(hw->port),
937 pf->stat_offsets_loaded,
938 &osd->eth.tx_broadcast,
939 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000940
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000941 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->tx_dropped_link_down,
944 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000945
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000946 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000949
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000950 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000953
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000954 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->mac_local_faults,
957 &nsd->mac_local_faults);
958 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->mac_remote_faults,
961 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000962
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000963 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->rx_length_errors,
966 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
969 pf->stat_offsets_loaded,
970 &osd->link_xon_rx, &nsd->link_xon_rx);
971 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
972 pf->stat_offsets_loaded,
973 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800974 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000977 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000980
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000981 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800982 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
983 pf->stat_offsets_loaded,
984 &osd->priority_xoff_rx[i],
985 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000986 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000987 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000988 &osd->priority_xon_rx[i],
989 &nsd->priority_xon_rx[i]);
990 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000991 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 &osd->priority_xon_tx[i],
993 &nsd->priority_xon_tx[i]);
994 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000995 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000996 &osd->priority_xoff_tx[i],
997 &nsd->priority_xoff_tx[i]);
998 i40e_stat_update32(hw,
999 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001000 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001001 &osd->priority_xon_2_xoff[i],
1002 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001003 }
1004
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001005 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1006 I40E_GLPRT_PRC64L(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_size_64, &nsd->rx_size_64);
1009 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1010 I40E_GLPRT_PRC127L(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->rx_size_127, &nsd->rx_size_127);
1013 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1014 I40E_GLPRT_PRC255L(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->rx_size_255, &nsd->rx_size_255);
1017 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1018 I40E_GLPRT_PRC511L(hw->port),
1019 pf->stat_offsets_loaded,
1020 &osd->rx_size_511, &nsd->rx_size_511);
1021 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1022 I40E_GLPRT_PRC1023L(hw->port),
1023 pf->stat_offsets_loaded,
1024 &osd->rx_size_1023, &nsd->rx_size_1023);
1025 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1026 I40E_GLPRT_PRC1522L(hw->port),
1027 pf->stat_offsets_loaded,
1028 &osd->rx_size_1522, &nsd->rx_size_1522);
1029 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1030 I40E_GLPRT_PRC9522L(hw->port),
1031 pf->stat_offsets_loaded,
1032 &osd->rx_size_big, &nsd->rx_size_big);
1033
1034 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1035 I40E_GLPRT_PTC64L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->tx_size_64, &nsd->tx_size_64);
1038 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1039 I40E_GLPRT_PTC127L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->tx_size_127, &nsd->tx_size_127);
1042 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1043 I40E_GLPRT_PTC255L(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->tx_size_255, &nsd->tx_size_255);
1046 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1047 I40E_GLPRT_PTC511L(hw->port),
1048 pf->stat_offsets_loaded,
1049 &osd->tx_size_511, &nsd->tx_size_511);
1050 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1051 I40E_GLPRT_PTC1023L(hw->port),
1052 pf->stat_offsets_loaded,
1053 &osd->tx_size_1023, &nsd->tx_size_1023);
1054 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1055 I40E_GLPRT_PTC1522L(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->tx_size_1522, &nsd->tx_size_1522);
1058 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1059 I40E_GLPRT_PTC9522L(hw->port),
1060 pf->stat_offsets_loaded,
1061 &osd->tx_size_big, &nsd->tx_size_big);
1062
1063 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1064 pf->stat_offsets_loaded,
1065 &osd->rx_undersize, &nsd->rx_undersize);
1066 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1067 pf->stat_offsets_loaded,
1068 &osd->rx_fragments, &nsd->rx_fragments);
1069 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->rx_oversize, &nsd->rx_oversize);
1072 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->rx_jabber, &nsd->rx_jabber);
1075
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001076 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001077 i40e_stat_update32(hw,
1078 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001079 pf->stat_offsets_loaded,
1080 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001081 i40e_stat_update32(hw,
1082 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001083 pf->stat_offsets_loaded,
1084 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001085 i40e_stat_update32(hw,
1086 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1087 pf->stat_offsets_loaded,
1088 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001089
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001090 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1091 nsd->tx_lpi_status =
1092 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1093 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1094 nsd->rx_lpi_status =
1095 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1096 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1097 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1098 pf->stat_offsets_loaded,
1099 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1100 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1101 pf->stat_offsets_loaded,
1102 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1103
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001104 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1105 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1106 nsd->fd_sb_status = true;
1107 else
1108 nsd->fd_sb_status = false;
1109
1110 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1111 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1112 nsd->fd_atr_status = true;
1113 else
1114 nsd->fd_atr_status = false;
1115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001116 pf->stat_offsets_loaded = true;
1117}
1118
1119/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001120 * i40e_update_stats - Update the various statistics counters.
1121 * @vsi: the VSI to be updated
1122 *
1123 * Update the various stats for this VSI and its related entities.
1124 **/
1125void i40e_update_stats(struct i40e_vsi *vsi)
1126{
1127 struct i40e_pf *pf = vsi->back;
1128
1129 if (vsi == pf->vsi[pf->lan_vsi])
1130 i40e_update_pf_stats(pf);
1131
1132 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001133#ifdef I40E_FCOE
1134 i40e_update_fcoe_stats(vsi);
1135#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001136}
1137
1138/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001139 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1140 * @vsi: the VSI to be searched
1141 * @macaddr: the MAC address
1142 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001143 *
1144 * Returns ptr to the filter object or NULL
1145 **/
1146static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001147 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001148{
1149 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001150 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001151
1152 if (!vsi || !macaddr)
1153 return NULL;
1154
Jacob Keller278e7d02016-10-05 09:30:37 -07001155 key = i40e_addr_to_hkey(macaddr);
1156 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001158 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001159 return f;
1160 }
1161 return NULL;
1162}
1163
1164/**
1165 * i40e_find_mac - Find a mac addr in the macvlan filters list
1166 * @vsi: the VSI to be searched
1167 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001168 *
1169 * Returns the first filter with the provided MAC address or NULL if
1170 * MAC address was not found
1171 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001172struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173{
1174 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001175 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001176
1177 if (!vsi || !macaddr)
1178 return NULL;
1179
Jacob Keller278e7d02016-10-05 09:30:37 -07001180 key = i40e_addr_to_hkey(macaddr);
1181 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001182 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001183 return f;
1184 }
1185 return NULL;
1186}
1187
1188/**
1189 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1190 * @vsi: the VSI to be searched
1191 *
1192 * Returns true if VSI is in vlan mode or false otherwise
1193 **/
1194bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1195{
Jacob Kellercbebb852016-10-05 09:30:40 -07001196 /* If we have a PVID, always operate in VLAN mode */
1197 if (vsi->info.pvid)
1198 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001199
Jacob Kellercbebb852016-10-05 09:30:40 -07001200 /* We need to operate in VLAN mode whenever we have any filters with
1201 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1202 * time, incurring search cost repeatedly. However, we can notice two
1203 * things:
1204 *
1205 * 1) the only place where we can gain a VLAN filter is in
1206 * i40e_add_filter.
1207 *
1208 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001209 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001210 *
1211 * Thus, we can simply use a boolean value, has_vlan_filters which we
1212 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1213 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001214 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001215 * filters here and can perform the check at the same time. This
1216 * results in avoiding embedding a loop for VLAN mode inside another
1217 * loop over all the filters, and should maintain correctness as noted
1218 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001219 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001220 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001221}
1222
1223/**
Jacob Keller489a3262016-11-11 12:39:31 -08001224 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1225 * @vsi: the VSI to configure
1226 * @tmp_add_list: list of filters ready to be added
1227 * @tmp_del_list: list of filters ready to be deleted
1228 * @vlan_filters: the number of active VLAN filters
1229 *
1230 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1231 * behave as expected. If we have any active VLAN filters remaining or about
1232 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1233 * so that they only match against untagged traffic. If we no longer have any
1234 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1235 * so that they match against both tagged and untagged traffic. In this way,
1236 * we ensure that we correctly receive the desired traffic. This ensures that
1237 * when we have an active VLAN we will receive only untagged traffic and
1238 * traffic matching active VLANs. If we have no active VLANs then we will
1239 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1240 *
1241 * Finally, in a similar fashion, this function also corrects filters when
1242 * there is an active PVID assigned to this VSI.
1243 *
1244 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1245 *
1246 * This function is only expected to be called from within
1247 * i40e_sync_vsi_filters.
1248 *
1249 * NOTE: This function expects to be called while under the
1250 * mac_filter_hash_lock
1251 */
1252static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1253 struct hlist_head *tmp_add_list,
1254 struct hlist_head *tmp_del_list,
1255 int vlan_filters)
1256{
1257 struct i40e_mac_filter *f, *add_head;
1258 struct hlist_node *h;
1259 int bkt, new_vlan;
1260
1261 /* To determine if a particular filter needs to be replaced we
1262 * have the three following conditions:
1263 *
1264 * a) if we have a PVID assigned, then all filters which are
1265 * not marked as VLAN=PVID must be replaced with filters that
1266 * are.
1267 * b) otherwise, if we have any active VLANS, all filters
1268 * which are marked as VLAN=-1 must be replaced with
1269 * filters marked as VLAN=0
1270 * c) finally, if we do not have any active VLANS, all filters
1271 * which are marked as VLAN=0 must be replaced with filters
1272 * marked as VLAN=-1
1273 */
1274
1275 /* Update the filters about to be added in place */
1276 hlist_for_each_entry(f, tmp_add_list, hlist) {
1277 if (vsi->info.pvid && f->vlan != vsi->info.pvid)
1278 f->vlan = vsi->info.pvid;
1279 else if (vlan_filters && f->vlan == I40E_VLAN_ANY)
1280 f->vlan = 0;
1281 else if (!vlan_filters && f->vlan == 0)
1282 f->vlan = I40E_VLAN_ANY;
1283 }
1284
1285 /* Update the remaining active filters */
1286 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1287 /* Combine the checks for whether a filter needs to be changed
1288 * and then determine the new VLAN inside the if block, in
1289 * order to avoid duplicating code for adding the new filter
1290 * then deleting the old filter.
1291 */
1292 if ((vsi->info.pvid && f->vlan != vsi->info.pvid) ||
1293 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1294 (!vlan_filters && f->vlan == 0)) {
1295 /* Determine the new vlan we will be adding */
1296 if (vsi->info.pvid)
1297 new_vlan = vsi->info.pvid;
1298 else if (vlan_filters)
1299 new_vlan = 0;
1300 else
1301 new_vlan = I40E_VLAN_ANY;
1302
1303 /* Create the new filter */
1304 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1305 if (!add_head)
1306 return -ENOMEM;
1307
1308 /* Put the replacement filter into the add list */
1309 hash_del(&add_head->hlist);
1310 hlist_add_head(&add_head->hlist, tmp_add_list);
1311
1312 /* Put the original filter into the delete list */
1313 f->state = I40E_FILTER_REMOVE;
1314 hash_del(&f->hlist);
1315 hlist_add_head(&f->hlist, tmp_del_list);
1316 }
1317 }
1318
1319 vsi->has_vlan_filter = !!vlan_filters;
1320
1321 return 0;
1322}
1323
1324/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001325 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1326 * @vsi: the PF Main VSI - inappropriate for any other VSI
1327 * @macaddr: the MAC address
1328 *
1329 * Remove whatever filter the firmware set up so the driver can manage
1330 * its own filtering intelligently.
1331 **/
1332static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1333{
1334 struct i40e_aqc_remove_macvlan_element_data element;
1335 struct i40e_pf *pf = vsi->back;
1336
1337 /* Only appropriate for the PF main VSI */
1338 if (vsi->type != I40E_VSI_MAIN)
1339 return;
1340
1341 memset(&element, 0, sizeof(element));
1342 ether_addr_copy(element.mac_addr, macaddr);
1343 element.vlan_tag = 0;
1344 /* Ignore error returns, some firmware does it this way... */
1345 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1346 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1347
1348 memset(&element, 0, sizeof(element));
1349 ether_addr_copy(element.mac_addr, macaddr);
1350 element.vlan_tag = 0;
1351 /* ...and some firmware does it this way. */
1352 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1353 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1354 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1355}
1356
1357/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001358 * i40e_add_filter - Add a mac/vlan filter to the VSI
1359 * @vsi: the VSI to be searched
1360 * @macaddr: the MAC address
1361 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001362 *
1363 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001364 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001365 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001366 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001367 **/
1368struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001369 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001370{
1371 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001372 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373
1374 if (!vsi || !macaddr)
1375 return NULL;
1376
Jacob Keller1bc87e82016-10-05 09:30:31 -07001377 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378 if (!f) {
1379 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1380 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001381 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001382
Jacob Kellercbebb852016-10-05 09:30:40 -07001383 /* Update the boolean indicating if we need to function in
1384 * VLAN mode.
1385 */
1386 if (vlan >= 0)
1387 vsi->has_vlan_filter = true;
1388
Greg Rose9a173902014-05-22 06:32:02 +00001389 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001390 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001391 /* If we're in overflow promisc mode, set the state directly
1392 * to failed, so we don't bother to try sending the filter
1393 * to the hardware.
1394 */
1395 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1396 f->state = I40E_FILTER_FAILED;
1397 else
1398 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001399 INIT_HLIST_NODE(&f->hlist);
1400
1401 key = i40e_addr_to_hkey(macaddr);
1402 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001404 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1405 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1406 }
1407
Jacob Keller1bc87e82016-10-05 09:30:31 -07001408 /* If we're asked to add a filter that has been marked for removal, it
1409 * is safe to simply restore it to active state. __i40e_del_filter
1410 * will have simply deleted any filters which were previously marked
1411 * NEW or FAILED, so if it is currently marked REMOVE it must have
1412 * previously been ACTIVE. Since we haven't yet run the sync filters
1413 * task, just restore this filter to the ACTIVE state so that the
1414 * sync task leaves it in place
1415 */
1416 if (f->state == I40E_FILTER_REMOVE)
1417 f->state = I40E_FILTER_ACTIVE;
1418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 return f;
1420}
1421
1422/**
Jacob Keller290d2552016-10-05 09:30:36 -07001423 * __i40e_del_filter - Remove a specific filter from the VSI
1424 * @vsi: VSI to remove from
1425 * @f: the filter to remove from the list
1426 *
1427 * This function should be called instead of i40e_del_filter only if you know
1428 * the exact filter you will remove already, such as via i40e_find_filter or
1429 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001430 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001432 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001433 * ANOTHER NOTE: This function MUST be called from within the context of
1434 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1435 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001436 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001437void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001438{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001439 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001440 return;
1441
Jacob Keller1bc87e82016-10-05 09:30:31 -07001442 if ((f->state == I40E_FILTER_FAILED) ||
1443 (f->state == I40E_FILTER_NEW)) {
1444 /* this one never got added by the FW. Just remove it,
1445 * no need to sync anything.
1446 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001447 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001448 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001450 f->state = I40E_FILTER_REMOVE;
1451 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1452 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001453 }
1454}
1455
1456/**
Jacob Keller290d2552016-10-05 09:30:36 -07001457 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1458 * @vsi: the VSI to be searched
1459 * @macaddr: the MAC address
1460 * @vlan: the VLAN
1461 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001462 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001463 * being held.
1464 * ANOTHER NOTE: This function MUST be called from within the context of
1465 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1466 * instead of list_for_each_entry().
1467 **/
1468void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1469{
1470 struct i40e_mac_filter *f;
1471
1472 if (!vsi || !macaddr)
1473 return;
1474
1475 f = i40e_find_filter(vsi, macaddr, vlan);
1476 __i40e_del_filter(vsi, f);
1477}
1478
1479/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001480 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001481 * @vsi: the VSI to be searched
1482 * @macaddr: the mac address to be filtered
1483 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001484 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1485 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001486 * unique vlan that already exists. If a PVID has been assigned, instead only
1487 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001488 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001489 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001490 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001491struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1492 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001493{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001494 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001495 struct hlist_node *h;
1496 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001497
1498 if (vsi->info.pvid)
1499 return i40e_add_filter(vsi, macaddr,
1500 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001501
Jacob Keller7aaf95362016-11-11 12:39:33 -08001502 if (!i40e_is_vsi_in_vlan(vsi))
1503 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1504
Jacob Keller278e7d02016-10-05 09:30:37 -07001505 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001506 if (f->state == I40E_FILTER_REMOVE)
1507 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001508 add = i40e_add_filter(vsi, macaddr, f->vlan);
1509 if (!add)
1510 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001511 }
1512
Jacob Keller5feb3d72016-10-05 09:30:34 -07001513 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001514}
1515
1516/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001517 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001518 * @vsi: the VSI to be searched
1519 * @macaddr: the mac address to be removed
1520 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001521 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1522 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001523 *
1524 * Returns 0 for success, or error
1525 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001526int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001527{
Jacob Keller278e7d02016-10-05 09:30:37 -07001528 struct i40e_mac_filter *f;
1529 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001530 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001531 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001532
Jacob Keller278e7d02016-10-05 09:30:37 -07001533 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1534 "Missing mac_filter_hash_lock\n");
1535 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001536 if (ether_addr_equal(macaddr, f->macaddr)) {
1537 __i40e_del_filter(vsi, f);
1538 found = true;
1539 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001540 }
Jacob Keller290d2552016-10-05 09:30:36 -07001541
1542 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001543 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001544 else
1545 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001546}
1547
1548/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549 * i40e_set_mac - NDO callback to set mac address
1550 * @netdev: network interface device structure
1551 * @p: pointer to an address structure
1552 *
1553 * Returns 0 on success, negative on failure
1554 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001555#ifdef I40E_FCOE
1556int i40e_set_mac(struct net_device *netdev, void *p)
1557#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001558static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001559#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560{
1561 struct i40e_netdev_priv *np = netdev_priv(netdev);
1562 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001563 struct i40e_pf *pf = vsi->back;
1564 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001565 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566
1567 if (!is_valid_ether_addr(addr->sa_data))
1568 return -EADDRNOTAVAIL;
1569
Shannon Nelson30650cc2014-07-29 04:01:50 +00001570 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1571 netdev_info(netdev, "already using mac address %pM\n",
1572 addr->sa_data);
1573 return 0;
1574 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001576 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1577 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1578 return -EADDRNOTAVAIL;
1579
Shannon Nelson30650cc2014-07-29 04:01:50 +00001580 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1581 netdev_info(netdev, "returning to hw mac address %pM\n",
1582 hw->mac.addr);
1583 else
1584 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1585
Jacob Keller278e7d02016-10-05 09:30:37 -07001586 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001587 i40e_del_mac_filter(vsi, netdev->dev_addr);
1588 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001589 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001590 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001591 if (vsi->type == I40E_VSI_MAIN) {
1592 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001593
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001594 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001595 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001596 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001597 if (ret)
1598 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1599 i40e_stat_str(hw, ret),
1600 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 }
1602
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001603 /* schedule our worker thread which will take care of
1604 * applying the new filter changes
1605 */
1606 i40e_service_event_schedule(vsi->back);
1607 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001608}
1609
1610/**
1611 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1612 * @vsi: the VSI being setup
1613 * @ctxt: VSI context structure
1614 * @enabled_tc: Enabled TCs bitmap
1615 * @is_add: True if called before Add VSI
1616 *
1617 * Setup VSI queue mapping for enabled traffic classes.
1618 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001619#ifdef I40E_FCOE
1620void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1621 struct i40e_vsi_context *ctxt,
1622 u8 enabled_tc,
1623 bool is_add)
1624#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1626 struct i40e_vsi_context *ctxt,
1627 u8 enabled_tc,
1628 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001629#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001630{
1631 struct i40e_pf *pf = vsi->back;
1632 u16 sections = 0;
1633 u8 netdev_tc = 0;
1634 u16 numtc = 0;
1635 u16 qcount;
1636 u8 offset;
1637 u16 qmap;
1638 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001639 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640
1641 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1642 offset = 0;
1643
1644 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1645 /* Find numtc from enabled TC bitmap */
1646 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001647 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001648 numtc++;
1649 }
1650 if (!numtc) {
1651 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1652 numtc = 1;
1653 }
1654 } else {
1655 /* At least TC0 is enabled in case of non-DCB case */
1656 numtc = 1;
1657 }
1658
1659 vsi->tc_config.numtc = numtc;
1660 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001661 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001662 qcount = vsi->alloc_queue_pairs;
1663
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001664 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001665 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666
1667 /* Setup queue offset/count for all TCs for given VSI */
1668 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1669 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001670 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001671 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672 int pow, num_qps;
1673
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001674 switch (vsi->type) {
1675 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001676 qcount = min_t(int, pf->alloc_rss_size,
1677 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001678 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001679#ifdef I40E_FCOE
1680 case I40E_VSI_FCOE:
1681 qcount = num_tc_qps;
1682 break;
1683#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001684 case I40E_VSI_FDIR:
1685 case I40E_VSI_SRIOV:
1686 case I40E_VSI_VMDQ2:
1687 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001688 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001689 WARN_ON(i != 0);
1690 break;
1691 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001692 vsi->tc_config.tc_info[i].qoffset = offset;
1693 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001694
Shannon Nelson1e200e42015-02-27 09:15:24 +00001695 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001696 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001697 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001698 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 pow++;
1700 num_qps >>= 1;
1701 }
1702
1703 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1704 qmap =
1705 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1706 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1707
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001708 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709 } else {
1710 /* TC is not enabled so set the offset to
1711 * default queue and allocate one queue
1712 * for the given TC.
1713 */
1714 vsi->tc_config.tc_info[i].qoffset = 0;
1715 vsi->tc_config.tc_info[i].qcount = 1;
1716 vsi->tc_config.tc_info[i].netdev_tc = 0;
1717
1718 qmap = 0;
1719 }
1720 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1721 }
1722
1723 /* Set actual Tx/Rx queue pairs */
1724 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001725 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1726 if (vsi->req_queue_pairs > 0)
1727 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001728 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001729 vsi->num_queue_pairs = pf->num_lan_msix;
1730 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001731
1732 /* Scheduler section valid can only be set for ADD VSI */
1733 if (is_add) {
1734 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1735
1736 ctxt->info.up_enable_bits = enabled_tc;
1737 }
1738 if (vsi->type == I40E_VSI_SRIOV) {
1739 ctxt->info.mapping_flags |=
1740 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1741 for (i = 0; i < vsi->num_queue_pairs; i++)
1742 ctxt->info.queue_mapping[i] =
1743 cpu_to_le16(vsi->base_queue + i);
1744 } else {
1745 ctxt->info.mapping_flags |=
1746 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1747 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1748 }
1749 ctxt->info.valid_sections |= cpu_to_le16(sections);
1750}
1751
1752/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001753 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1754 * @netdev: the netdevice
1755 * @addr: address to add
1756 *
1757 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1758 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1759 */
1760static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1761{
1762 struct i40e_netdev_priv *np = netdev_priv(netdev);
1763 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001764
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001765 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001766 return 0;
1767 else
1768 return -ENOMEM;
1769}
1770
1771/**
1772 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1773 * @netdev: the netdevice
1774 * @addr: address to add
1775 *
1776 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1777 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1778 */
1779static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1780{
1781 struct i40e_netdev_priv *np = netdev_priv(netdev);
1782 struct i40e_vsi *vsi = np->vsi;
1783
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001784 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001785
1786 return 0;
1787}
1788
1789/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001790 * i40e_set_rx_mode - NDO callback to set the netdev filters
1791 * @netdev: network interface device structure
1792 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001793#ifdef I40E_FCOE
1794void i40e_set_rx_mode(struct net_device *netdev)
1795#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001796static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001797#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001798{
1799 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001800 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001801
Jacob Keller278e7d02016-10-05 09:30:37 -07001802 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001803
Jacob Keller6622f5c2016-10-05 09:30:32 -07001804 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1805 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001806
Jacob Keller278e7d02016-10-05 09:30:37 -07001807 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001808
1809 /* check for other flag changes */
1810 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1811 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1812 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1813 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001814
1815 /* schedule our worker thread which will take care of
1816 * applying the new filter changes
1817 */
1818 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001819}
1820
1821/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001822 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1823 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001824 * @from: Pointer to list which contains MAC filter entries - changes to
1825 * those entries needs to be undone.
1826 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001827 * MAC filter entries from list were slated to be sent to firmware, either for
1828 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001829 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001830static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1831 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001832{
Jacob Keller278e7d02016-10-05 09:30:37 -07001833 struct i40e_mac_filter *f;
1834 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001835
Jacob Keller278e7d02016-10-05 09:30:37 -07001836 hlist_for_each_entry_safe(f, h, from, hlist) {
1837 u64 key = i40e_addr_to_hkey(f->macaddr);
1838
Kiran Patil21659032015-09-30 14:09:03 -04001839 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001840 hlist_del(&f->hlist);
1841 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001842 }
1843}
1844
1845/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001846 * i40e_update_filter_state - Update filter state based on return data
1847 * from firmware
1848 * @count: Number of filters added
1849 * @add_list: return data from fw
1850 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001851 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001852 * MAC filter entries from list were slated to be added to device. Returns
1853 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001854 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001855static int
1856i40e_update_filter_state(int count,
1857 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Kellerac9e2392016-11-11 12:39:27 -08001858 struct i40e_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001859{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001860 int retval = 0;
1861 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001862
Jacob Kellerac9e2392016-11-11 12:39:27 -08001863 for (i = 0; i < count; i++) {
1864 /* Always check status of each filter. We don't need to check
1865 * the firmware return status because we pre-set the filter
1866 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1867 * request to the adminq. Thus, if it no longer matches then
1868 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001869 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001870 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001871 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001872 } else {
1873 add_head->state = I40E_FILTER_ACTIVE;
1874 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001876
1877 add_head = hlist_entry(add_head->hlist.next,
1878 typeof(struct i40e_mac_filter),
1879 hlist);
Kiran Patil21659032015-09-30 14:09:03 -04001880 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001881
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001882 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001883}
1884
1885/**
Jacob Keller00936312016-10-05 09:30:41 -07001886 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1887 * @vsi: ptr to the VSI
1888 * @vsi_name: name to display in messages
1889 * @list: the list of filters to send to firmware
1890 * @num_del: the number of filters to delete
1891 * @retval: Set to -EIO on failure to delete
1892 *
1893 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1894 * *retval instead of a return value so that success does not force ret_val to
1895 * be set to 0. This ensures that a sequence of calls to this function
1896 * preserve the previous value of *retval on successful delete.
1897 */
1898static
1899void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1900 struct i40e_aqc_remove_macvlan_element_data *list,
1901 int num_del, int *retval)
1902{
1903 struct i40e_hw *hw = &vsi->back->hw;
1904 i40e_status aq_ret;
1905 int aq_err;
1906
1907 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1908 aq_err = hw->aq.asq_last_status;
1909
1910 /* Explicitly ignore and do not report when firmware returns ENOENT */
1911 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1912 *retval = -EIO;
1913 dev_info(&vsi->back->pdev->dev,
1914 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1915 vsi_name, i40e_stat_str(hw, aq_ret),
1916 i40e_aq_str(hw, aq_err));
1917 }
1918}
1919
1920/**
1921 * i40e_aqc_add_filters - Request firmware to add a set of filters
1922 * @vsi: ptr to the VSI
1923 * @vsi_name: name to display in messages
1924 * @list: the list of filters to send to firmware
1925 * @add_head: Position in the add hlist
1926 * @num_add: the number of filters to add
1927 * @promisc_change: set to true on exit if promiscuous mode was forced on
1928 *
1929 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1930 * promisc_changed to true if the firmware has run out of space for more
1931 * filters.
1932 */
1933static
1934void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1935 struct i40e_aqc_add_macvlan_element_data *list,
1936 struct i40e_mac_filter *add_head,
1937 int num_add, bool *promisc_changed)
1938{
1939 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001940 int aq_err, fcnt;
1941
Jacob Kellerac9e2392016-11-11 12:39:27 -08001942 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001943 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001944 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001945
1946 if (fcnt != num_add) {
1947 *promisc_changed = true;
1948 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001949 dev_warn(&vsi->back->pdev->dev,
1950 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1951 i40e_aq_str(hw, aq_err),
1952 vsi_name);
1953 }
1954}
1955
1956/**
Jacob Keller435c0842016-11-08 13:05:10 -08001957 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1958 * @vsi: pointer to the VSI
1959 * @f: filter data
1960 *
1961 * This function sets or clears the promiscuous broadcast flags for VLAN
1962 * filters in order to properly receive broadcast frames. Assumes that only
1963 * broadcast filters are passed.
1964 **/
1965static
1966void i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1967 struct i40e_mac_filter *f)
1968{
1969 bool enable = f->state == I40E_FILTER_NEW;
1970 struct i40e_hw *hw = &vsi->back->hw;
1971 i40e_status aq_ret;
1972
1973 if (f->vlan == I40E_VLAN_ANY) {
1974 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1975 vsi->seid,
1976 enable,
1977 NULL);
1978 } else {
1979 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1980 vsi->seid,
1981 enable,
1982 f->vlan,
1983 NULL);
1984 }
1985
1986 if (aq_ret) {
1987 dev_warn(&vsi->back->pdev->dev,
1988 "Error %s setting broadcast promiscuous mode on %s\n",
1989 i40e_aq_str(hw, hw->aq.asq_last_status),
1990 vsi_name);
1991 f->state = I40E_FILTER_FAILED;
1992 } else if (enable) {
1993 f->state = I40E_FILTER_ACTIVE;
1994 }
1995}
1996
1997/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001998 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1999 * @vsi: ptr to the VSI
2000 *
2001 * Push any outstanding VSI filter changes through the AdminQ.
2002 *
2003 * Returns 0 or error value
2004 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002005int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002006{
Jacob Keller278e7d02016-10-05 09:30:37 -07002007 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07002008 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002009 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002010 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002011 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002012 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002013 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002014 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002015 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002016 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002017 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 struct i40e_pf *pf;
2019 int num_add = 0;
2020 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002021 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002022 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002023 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002024 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002025
2026 /* empty array typed pointers, kcalloc later */
2027 struct i40e_aqc_add_macvlan_element_data *add_list;
2028 struct i40e_aqc_remove_macvlan_element_data *del_list;
2029
2030 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2031 usleep_range(1000, 2000);
2032 pf = vsi->back;
2033
2034 if (vsi->netdev) {
2035 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2036 vsi->current_netdev_flags = vsi->netdev->flags;
2037 }
2038
Jacob Keller278e7d02016-10-05 09:30:37 -07002039 INIT_HLIST_HEAD(&tmp_add_list);
2040 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002041
Shannon Nelson2d1de822016-05-16 10:26:44 -07002042 if (vsi->type == I40E_VSI_SRIOV)
2043 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2044 else if (vsi->type != I40E_VSI_MAIN)
2045 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2046
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002047 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2048 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2049
Jacob Keller278e7d02016-10-05 09:30:37 -07002050 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002051 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002052 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002053 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002054 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002055 hash_del(&f->hlist);
2056 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002057
2058 /* Avoid counting removed filters */
2059 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002060 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002061 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002062 hash_del(&f->hlist);
2063 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002064 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002065
Jacob Keller489a3262016-11-11 12:39:31 -08002066 /* Count the number of active (current and new) VLAN
2067 * filters we have now. Does not count filters which
2068 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002069 */
2070 if (f->vlan > 0)
2071 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002072 }
2073
Jacob Keller489a3262016-11-11 12:39:31 -08002074 retval = i40e_correct_mac_vlan_filters(vsi,
2075 &tmp_add_list,
2076 &tmp_del_list,
2077 vlan_filters);
2078 if (retval)
2079 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002080
Jacob Keller278e7d02016-10-05 09:30:37 -07002081 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002082 }
2083
2084 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002085 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002086 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002087 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002088 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002089 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002090 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002091 if (!del_list)
2092 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002093
Jacob Keller278e7d02016-10-05 09:30:37 -07002094 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002095 cmd_flags = 0;
2096
Jacob Keller435c0842016-11-08 13:05:10 -08002097 /* handle broadcast filters by updating the broadcast
2098 * promiscuous flag instead of deleting a MAC filter.
2099 */
2100 if (is_broadcast_ether_addr(f->macaddr)) {
2101 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2102
2103 hlist_del(&f->hlist);
2104 kfree(f);
2105 continue;
2106 }
2107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002109 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002110 if (f->vlan == I40E_VLAN_ANY) {
2111 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002112 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002113 } else {
2114 del_list[num_del].vlan_tag =
2115 cpu_to_le16((u16)(f->vlan));
2116 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002117
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002118 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2119 del_list[num_del].flags = cmd_flags;
2120 num_del++;
2121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002122 /* flush a full buffer */
2123 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002124 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2125 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002126 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002127 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002128 }
Kiran Patil21659032015-09-30 14:09:03 -04002129 /* Release memory for MAC filter entries which were
2130 * synced up with HW.
2131 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002132 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002133 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002134 }
Kiran Patil21659032015-09-30 14:09:03 -04002135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002136 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002137 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2138 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002139 }
2140
2141 kfree(del_list);
2142 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002143 }
2144
Jacob Keller278e7d02016-10-05 09:30:37 -07002145 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002146 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002147 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002148 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002149 list_size = filter_list_len *
2150 sizeof(struct i40e_aqc_add_macvlan_element_data);
2151 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002152 if (!add_list)
2153 goto err_no_memory;
2154
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002155 num_add = 0;
Jacob Keller435c0842016-11-08 13:05:10 -08002156 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002157 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2158 &vsi->state)) {
2159 f->state = I40E_FILTER_FAILED;
2160 continue;
2161 }
Jacob Keller435c0842016-11-08 13:05:10 -08002162
2163 /* handle broadcast filters by updating the broadcast
2164 * promiscuous flag instead of adding a MAC filter.
2165 */
2166 if (is_broadcast_ether_addr(f->macaddr)) {
2167 u64 key = i40e_addr_to_hkey(f->macaddr);
2168 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2169
2170 hlist_del(&f->hlist);
2171 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2172 continue;
2173 }
2174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002175 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002176 if (num_add == 0)
2177 add_head = f;
2178 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002179 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002180 if (f->vlan == I40E_VLAN_ANY) {
2181 add_list[num_add].vlan_tag = 0;
2182 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2183 } else {
2184 add_list[num_add].vlan_tag =
2185 cpu_to_le16((u16)(f->vlan));
2186 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002188 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002189 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002190 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002191 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2192 num_add++;
2193
2194 /* flush a full buffer */
2195 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002196 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2197 add_head, num_add,
2198 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002199 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002200 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002201 }
2202 }
2203 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002204 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2205 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002206 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002207 /* Now move all of the filters from the temp add list back to
2208 * the VSI's list.
2209 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002210 spin_lock_bh(&vsi->mac_filter_hash_lock);
2211 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2212 u64 key = i40e_addr_to_hkey(f->macaddr);
2213
2214 hlist_del(&f->hlist);
2215 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002216 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002217 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002218 kfree(add_list);
2219 add_list = NULL;
2220 }
2221
Jacob Keller38326212016-11-11 12:39:26 -08002222 /* Determine the number of active and failed filters. */
2223 spin_lock_bh(&vsi->mac_filter_hash_lock);
2224 vsi->active_filters = 0;
2225 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2226 if (f->state == I40E_FILTER_ACTIVE)
2227 vsi->active_filters++;
2228 else if (f->state == I40E_FILTER_FAILED)
2229 failed_filters++;
2230 }
2231 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2232
2233 /* If promiscuous mode has changed, we need to calculate a new
2234 * threshold for when we are safe to exit
2235 */
2236 if (promisc_changed)
2237 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2238
2239 /* Check if we are able to exit overflow promiscuous mode. We can
2240 * safely exit if we didn't just enter, we no longer have any failed
2241 * filters, and we have reduced filters below the threshold value.
2242 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002243 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002244 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002245 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002246 dev_info(&pf->pdev->dev,
2247 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2248 vsi_name);
2249 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2250 promisc_changed = true;
2251 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002252 }
2253
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002254 /* if the VF is not trusted do not do promisc */
2255 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2256 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2257 goto out;
2258 }
2259
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002260 /* check for changes in promiscuous modes */
2261 if (changed_flags & IFF_ALLMULTI) {
2262 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002263
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002264 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002265 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2266 vsi->seid,
2267 cur_multipromisc,
2268 NULL);
2269 if (aq_ret) {
2270 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002271 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002272 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002273 "set multi promisc failed on %s, err %s aq_err %s\n",
2274 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002275 i40e_stat_str(hw, aq_ret),
2276 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002277 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002278 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002279 if ((changed_flags & IFF_PROMISC) ||
2280 (promisc_changed &&
2281 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002282 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002284 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2285 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2286 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002287 if ((vsi->type == I40E_VSI_MAIN) &&
2288 (pf->lan_veb != I40E_NO_VEB) &&
2289 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002290 /* set defport ON for Main VSI instead of true promisc
2291 * this way we will get all unicast/multicast and VLAN
2292 * promisc behavior but will not get VF or VMDq traffic
2293 * replicated on the Main VSI.
2294 */
2295 if (pf->cur_promisc != cur_promisc) {
2296 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002297 if (cur_promisc)
2298 aq_ret =
2299 i40e_aq_set_default_vsi(hw,
2300 vsi->seid,
2301 NULL);
2302 else
2303 aq_ret =
2304 i40e_aq_clear_default_vsi(hw,
2305 vsi->seid,
2306 NULL);
2307 if (aq_ret) {
2308 retval = i40e_aq_rc_to_posix(aq_ret,
2309 hw->aq.asq_last_status);
2310 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002311 "Set default VSI failed on %s, err %s, aq_err %s\n",
2312 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002313 i40e_stat_str(hw, aq_ret),
2314 i40e_aq_str(hw,
2315 hw->aq.asq_last_status));
2316 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002317 }
2318 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002319 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002320 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002321 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002322 cur_promisc, NULL,
2323 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002324 if (aq_ret) {
2325 retval =
2326 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002327 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002328 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002329 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2330 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002331 i40e_stat_str(hw, aq_ret),
2332 i40e_aq_str(hw,
2333 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002334 }
2335 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002336 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002337 vsi->seid,
2338 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002339 if (aq_ret) {
2340 retval =
2341 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002342 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002343 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002344 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2345 vsi_name,
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 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002350 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002351 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2352 vsi->seid,
2353 cur_promisc, NULL);
2354 if (aq_ret) {
2355 retval = i40e_aq_rc_to_posix(aq_ret,
2356 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002357 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002358 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002359 i40e_stat_str(hw, aq_ret),
2360 i40e_aq_str(hw,
2361 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002362 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002363 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002364out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002365 /* if something went wrong then set the changed flag so we try again */
2366 if (retval)
2367 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002369 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002370 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002371
2372err_no_memory:
2373 /* Restore elements on the temporary add and delete lists */
2374 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002375err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002376 i40e_undo_filter_entries(vsi, &tmp_del_list);
2377 i40e_undo_filter_entries(vsi, &tmp_add_list);
2378 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2379
2380 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2381 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2382 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002383}
2384
2385/**
2386 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2387 * @pf: board private structure
2388 **/
2389static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2390{
2391 int v;
2392
2393 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2394 return;
2395 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2396
Mitch Williams505682c2014-05-20 08:01:37 +00002397 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002399 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2400 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2401
2402 if (ret) {
2403 /* come back and try again later */
2404 pf->flags |= I40E_FLAG_FILTER_SYNC;
2405 break;
2406 }
2407 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002408 }
2409}
2410
2411/**
2412 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2413 * @netdev: network interface device structure
2414 * @new_mtu: new value for maximum frame size
2415 *
2416 * Returns 0 on success, negative on failure
2417 **/
2418static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2419{
2420 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002421 struct i40e_vsi *vsi = np->vsi;
2422
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002423 netdev_info(netdev, "changing MTU from %d to %d\n",
2424 netdev->mtu, new_mtu);
2425 netdev->mtu = new_mtu;
2426 if (netif_running(netdev))
2427 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002428 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429 return 0;
2430}
2431
2432/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002433 * i40e_ioctl - Access the hwtstamp interface
2434 * @netdev: network interface device structure
2435 * @ifr: interface request data
2436 * @cmd: ioctl command
2437 **/
2438int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2439{
2440 struct i40e_netdev_priv *np = netdev_priv(netdev);
2441 struct i40e_pf *pf = np->vsi->back;
2442
2443 switch (cmd) {
2444 case SIOCGHWTSTAMP:
2445 return i40e_ptp_get_ts_config(pf, ifr);
2446 case SIOCSHWTSTAMP:
2447 return i40e_ptp_set_ts_config(pf, ifr);
2448 default:
2449 return -EOPNOTSUPP;
2450 }
2451}
2452
2453/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002454 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2455 * @vsi: the vsi being adjusted
2456 **/
2457void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2458{
2459 struct i40e_vsi_context ctxt;
2460 i40e_status ret;
2461
2462 if ((vsi->info.valid_sections &
2463 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2464 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2465 return; /* already enabled */
2466
2467 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2468 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2469 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2470
2471 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002472 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002473 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2474 if (ret) {
2475 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002476 "update vlan stripping failed, err %s aq_err %s\n",
2477 i40e_stat_str(&vsi->back->hw, ret),
2478 i40e_aq_str(&vsi->back->hw,
2479 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002480 }
2481}
2482
2483/**
2484 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2485 * @vsi: the vsi being adjusted
2486 **/
2487void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2488{
2489 struct i40e_vsi_context ctxt;
2490 i40e_status ret;
2491
2492 if ((vsi->info.valid_sections &
2493 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2494 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2495 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2496 return; /* already disabled */
2497
2498 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2499 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2500 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2501
2502 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002503 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002504 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2505 if (ret) {
2506 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002507 "update vlan stripping failed, err %s aq_err %s\n",
2508 i40e_stat_str(&vsi->back->hw, ret),
2509 i40e_aq_str(&vsi->back->hw,
2510 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002511 }
2512}
2513
2514/**
2515 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2516 * @netdev: network interface to be adjusted
2517 * @features: netdev features to test if VLAN offload is enabled or not
2518 **/
2519static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2520{
2521 struct i40e_netdev_priv *np = netdev_priv(netdev);
2522 struct i40e_vsi *vsi = np->vsi;
2523
2524 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2525 i40e_vlan_stripping_enable(vsi);
2526 else
2527 i40e_vlan_stripping_disable(vsi);
2528}
2529
2530/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002531 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532 * @vsi: the vsi being configured
2533 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002534 *
2535 * This is a helper function for adding a new MAC/VLAN filter with the
2536 * specified VLAN for each existing MAC address already in the hash table.
2537 * This function does *not* perform any accounting to update filters based on
2538 * VLAN mode.
2539 *
2540 * NOTE: this function expects to be called while under the
2541 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002542 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002543int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002544{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002545 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002546 struct hlist_node *h;
2547 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002548
Jacob Keller278e7d02016-10-05 09:30:37 -07002549 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002550 if (f->state == I40E_FILTER_REMOVE)
2551 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002552 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002553 if (!add_f) {
2554 dev_info(&vsi->back->pdev->dev,
2555 "Could not add vlan filter %d for %pM\n",
2556 vid, f->macaddr);
2557 return -ENOMEM;
2558 }
2559 }
2560
Jacob Keller490a4ad2016-11-11 12:39:29 -08002561 return 0;
2562}
2563
2564/**
2565 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2566 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002567 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002568 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002569int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002570{
Jacob Keller489a3262016-11-11 12:39:31 -08002571 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002572
Jacob Kellerf94484b2016-12-07 14:05:34 -08002573 if (!vid || vsi->info.pvid)
2574 return -EINVAL;
2575
Jacob Keller490a4ad2016-11-11 12:39:29 -08002576 /* Locked once because all functions invoked below iterates list*/
2577 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002578 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002579 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002580 if (err)
2581 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002582
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002583 /* schedule our worker thread which will take care of
2584 * applying the new filter changes
2585 */
2586 i40e_service_event_schedule(vsi->back);
2587 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002588}
2589
2590/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002591 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592 * @vsi: the vsi being configured
2593 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002594 *
2595 * This function should be used to remove all VLAN filters which match the
2596 * given VID. It does not schedule the service event and does not take the
2597 * mac_filter_hash_lock so it may be combined with other operations under
2598 * a single invocation of the mac_filter_hash_lock.
2599 *
2600 * NOTE: this function expects to be called while under the
2601 * mac_filter_hash_lock
2602 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002603void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002604{
Alan Brady84f5ca62016-10-05 09:30:39 -07002605 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002606 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002607 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608
Jacob Keller278e7d02016-10-05 09:30:37 -07002609 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002610 if (f->vlan == vid)
2611 __i40e_del_filter(vsi, f);
2612 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002613}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002614
Jacob Keller490a4ad2016-11-11 12:39:29 -08002615/**
2616 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2617 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002618 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002619 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002620void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002621{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002622 if (!vid || vsi->info.pvid)
2623 return;
2624
Jacob Keller490a4ad2016-11-11 12:39:29 -08002625 spin_lock_bh(&vsi->mac_filter_hash_lock);
2626 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002627 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002628
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002629 /* schedule our worker thread which will take care of
2630 * applying the new filter changes
2631 */
2632 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002633}
2634
2635/**
2636 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2637 * @netdev: network interface to be adjusted
2638 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002639 *
2640 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002641 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002642#ifdef I40E_FCOE
2643int i40e_vlan_rx_add_vid(struct net_device *netdev,
2644 __always_unused __be16 proto, u16 vid)
2645#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002646static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2647 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002648#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649{
2650 struct i40e_netdev_priv *np = netdev_priv(netdev);
2651 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002652 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002653
Jacob Keller6a1127852016-10-25 16:08:49 -07002654 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002655 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002656
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002657 /* If the network stack called us with vid = 0 then
2658 * it is asking to receive priority tagged packets with
2659 * vlan id 0. Our HW receives them by default when configured
2660 * to receive untagged packets so there is no need to add an
2661 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002662 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002663 if (vid)
2664 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002665
Jacob Keller6a1127852016-10-25 16:08:49 -07002666 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002667 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002668
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002669 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002670}
2671
2672/**
2673 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2674 * @netdev: network interface to be adjusted
2675 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002676 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002677 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002678 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002679#ifdef I40E_FCOE
2680int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2681 __always_unused __be16 proto, u16 vid)
2682#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002683static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2684 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002685#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002686{
2687 struct i40e_netdev_priv *np = netdev_priv(netdev);
2688 struct i40e_vsi *vsi = np->vsi;
2689
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690 /* return code is ignored as there is nothing a user
2691 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002692 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002693 */
2694 i40e_vsi_kill_vlan(vsi, vid);
2695
2696 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002698 return 0;
2699}
2700
2701/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002702 * i40e_macaddr_init - explicitly write the mac address filters
2703 *
2704 * @vsi: pointer to the vsi
2705 * @macaddr: the MAC address
2706 *
2707 * This is needed when the macaddr has been obtained by other
2708 * means than the default, e.g., from Open Firmware or IDPROM.
2709 * Returns 0 on success, negative on failure
2710 **/
2711static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2712{
2713 int ret;
2714 struct i40e_aqc_add_macvlan_element_data element;
2715
2716 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2717 I40E_AQC_WRITE_TYPE_LAA_WOL,
2718 macaddr, NULL);
2719 if (ret) {
2720 dev_info(&vsi->back->pdev->dev,
2721 "Addr change for VSI failed: %d\n", ret);
2722 return -EADDRNOTAVAIL;
2723 }
2724
2725 memset(&element, 0, sizeof(element));
2726 ether_addr_copy(element.mac_addr, macaddr);
2727 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2728 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2729 if (ret) {
2730 dev_info(&vsi->back->pdev->dev,
2731 "add filter failed err %s aq_err %s\n",
2732 i40e_stat_str(&vsi->back->hw, ret),
2733 i40e_aq_str(&vsi->back->hw,
2734 vsi->back->hw.aq.asq_last_status));
2735 }
2736 return ret;
2737}
2738
2739/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002740 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2741 * @vsi: the vsi being brought back up
2742 **/
2743static void i40e_restore_vlan(struct i40e_vsi *vsi)
2744{
2745 u16 vid;
2746
2747 if (!vsi->netdev)
2748 return;
2749
2750 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2751
2752 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2753 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2754 vid);
2755}
2756
2757/**
2758 * i40e_vsi_add_pvid - Add pvid for the VSI
2759 * @vsi: the vsi being adjusted
2760 * @vid: the vlan id to set as a PVID
2761 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002762int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763{
2764 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002765 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002766
2767 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2768 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002769 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2770 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002771 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772
2773 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002774 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002775 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2776 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002777 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002778 "add pvid failed, err %s aq_err %s\n",
2779 i40e_stat_str(&vsi->back->hw, ret),
2780 i40e_aq_str(&vsi->back->hw,
2781 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002782 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002783 }
2784
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002785 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002786}
2787
2788/**
2789 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2790 * @vsi: the vsi being adjusted
2791 *
2792 * Just use the vlan_rx_register() service to put it back to normal
2793 **/
2794void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2795{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002796 i40e_vlan_stripping_disable(vsi);
2797
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002798 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002799}
2800
2801/**
2802 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2803 * @vsi: ptr to the VSI
2804 *
2805 * If this function returns with an error, then it's possible one or
2806 * more of the rings is populated (while the rest are not). It is the
2807 * callers duty to clean those orphaned rings.
2808 *
2809 * Return 0 on success, negative on failure
2810 **/
2811static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2812{
2813 int i, err = 0;
2814
2815 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002816 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002817
2818 return err;
2819}
2820
2821/**
2822 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2823 * @vsi: ptr to the VSI
2824 *
2825 * Free VSI's transmit software resources
2826 **/
2827static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2828{
2829 int i;
2830
Greg Rose8e9dca52013-12-18 13:45:53 +00002831 if (!vsi->tx_rings)
2832 return;
2833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002834 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002835 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002836 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002837}
2838
2839/**
2840 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2841 * @vsi: ptr to the VSI
2842 *
2843 * If this function returns with an error, then it's possible one or
2844 * more of the rings is populated (while the rest are not). It is the
2845 * callers duty to clean those orphaned rings.
2846 *
2847 * Return 0 on success, negative on failure
2848 **/
2849static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2850{
2851 int i, err = 0;
2852
2853 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002854 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002855#ifdef I40E_FCOE
2856 i40e_fcoe_setup_ddp_resources(vsi);
2857#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002858 return err;
2859}
2860
2861/**
2862 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2863 * @vsi: ptr to the VSI
2864 *
2865 * Free all receive software resources
2866 **/
2867static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2868{
2869 int i;
2870
Greg Rose8e9dca52013-12-18 13:45:53 +00002871 if (!vsi->rx_rings)
2872 return;
2873
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002874 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002875 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002876 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002877#ifdef I40E_FCOE
2878 i40e_fcoe_free_ddp_resources(vsi);
2879#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002880}
2881
2882/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002883 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2884 * @ring: The Tx ring to configure
2885 *
2886 * This enables/disables XPS for a given Tx descriptor ring
2887 * based on the TCs enabled for the VSI that ring belongs to.
2888 **/
2889static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2890{
2891 struct i40e_vsi *vsi = ring->vsi;
2892 cpumask_var_t mask;
2893
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002894 if (!ring->q_vector || !ring->netdev)
2895 return;
2896
2897 /* Single TC mode enable XPS */
2898 if (vsi->tc_config.numtc <= 1) {
2899 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002900 netif_set_xps_queue(ring->netdev,
2901 &ring->q_vector->affinity_mask,
2902 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002903 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2904 /* Disable XPS to allow selection based on TC */
2905 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2906 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2907 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002908 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002909
2910 /* schedule our worker thread which will take care of
2911 * applying the new filter changes
2912 */
2913 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002914}
2915
2916/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002917 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2918 * @ring: The Tx ring to configure
2919 *
2920 * Configure the Tx descriptor ring in the HMC context.
2921 **/
2922static int i40e_configure_tx_ring(struct i40e_ring *ring)
2923{
2924 struct i40e_vsi *vsi = ring->vsi;
2925 u16 pf_q = vsi->base_queue + ring->queue_index;
2926 struct i40e_hw *hw = &vsi->back->hw;
2927 struct i40e_hmc_obj_txq tx_ctx;
2928 i40e_status err = 0;
2929 u32 qtx_ctl = 0;
2930
2931 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002932 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002933 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2934 ring->atr_count = 0;
2935 } else {
2936 ring->atr_sample_rate = 0;
2937 }
2938
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002939 /* configure XPS */
2940 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002941
2942 /* clear the context structure first */
2943 memset(&tx_ctx, 0, sizeof(tx_ctx));
2944
2945 tx_ctx.new_context = 1;
2946 tx_ctx.base = (ring->dma / 128);
2947 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002948 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2949 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002950#ifdef I40E_FCOE
2951 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2952#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002953 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002954 /* FDIR VSI tx ring can still use RS bit and writebacks */
2955 if (vsi->type != I40E_VSI_FDIR)
2956 tx_ctx.head_wb_ena = 1;
2957 tx_ctx.head_wb_addr = ring->dma +
2958 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002959
2960 /* As part of VSI creation/update, FW allocates certain
2961 * Tx arbitration queue sets for each TC enabled for
2962 * the VSI. The FW returns the handles to these queue
2963 * sets as part of the response buffer to Add VSI,
2964 * Update VSI, etc. AQ commands. It is expected that
2965 * these queue set handles be associated with the Tx
2966 * queues by the driver as part of the TX queue context
2967 * initialization. This has to be done regardless of
2968 * DCB as by default everything is mapped to TC0.
2969 */
2970 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2971 tx_ctx.rdylist_act = 0;
2972
2973 /* clear the context in the HMC */
2974 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2975 if (err) {
2976 dev_info(&vsi->back->pdev->dev,
2977 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2978 ring->queue_index, pf_q, err);
2979 return -ENOMEM;
2980 }
2981
2982 /* set the context in the HMC */
2983 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2984 if (err) {
2985 dev_info(&vsi->back->pdev->dev,
2986 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2987 ring->queue_index, pf_q, err);
2988 return -ENOMEM;
2989 }
2990
2991 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002992 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002993 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002994 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2995 I40E_QTX_CTL_VFVM_INDX_MASK;
2996 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002997 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002998 }
2999
Shannon Nelson13fd977492013-09-28 07:14:19 +00003000 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3001 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003002 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3003 i40e_flush(hw);
3004
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003005 /* cache tail off for easier writes later */
3006 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3007
3008 return 0;
3009}
3010
3011/**
3012 * i40e_configure_rx_ring - Configure a receive ring context
3013 * @ring: The Rx ring to configure
3014 *
3015 * Configure the Rx descriptor ring in the HMC context.
3016 **/
3017static int i40e_configure_rx_ring(struct i40e_ring *ring)
3018{
3019 struct i40e_vsi *vsi = ring->vsi;
3020 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3021 u16 pf_q = vsi->base_queue + ring->queue_index;
3022 struct i40e_hw *hw = &vsi->back->hw;
3023 struct i40e_hmc_obj_rxq rx_ctx;
3024 i40e_status err = 0;
3025
3026 ring->state = 0;
3027
3028 /* clear the context structure first */
3029 memset(&rx_ctx, 0, sizeof(rx_ctx));
3030
3031 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032
3033 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003034
3035 rx_ctx.base = (ring->dma / 128);
3036 rx_ctx.qlen = ring->count;
3037
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003038 /* use 32 byte descriptors */
3039 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003040
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003041 /* descriptor type is always zero
3042 * rx_ctx.dtype = 0;
3043 */
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003044 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003045
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003046 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003047 if (hw->revision_id == 0)
3048 rx_ctx.lrxqthresh = 0;
3049 else
3050 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003051 rx_ctx.crcstrip = 1;
3052 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003053 /* this controls whether VLAN is stripped from inner headers */
3054 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003055#ifdef I40E_FCOE
3056 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3057#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003058 /* set the prefena field to 1 because the manual says to */
3059 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003060
3061 /* clear the context in the HMC */
3062 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3063 if (err) {
3064 dev_info(&vsi->back->pdev->dev,
3065 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3066 ring->queue_index, pf_q, err);
3067 return -ENOMEM;
3068 }
3069
3070 /* set the context in the HMC */
3071 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3072 if (err) {
3073 dev_info(&vsi->back->pdev->dev,
3074 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3075 ring->queue_index, pf_q, err);
3076 return -ENOMEM;
3077 }
3078
3079 /* cache tail for quicker writes, and clear the reg before use */
3080 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3081 writel(0, ring->tail);
3082
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003083 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084
3085 return 0;
3086}
3087
3088/**
3089 * i40e_vsi_configure_tx - Configure the VSI for Tx
3090 * @vsi: VSI structure describing this set of rings and resources
3091 *
3092 * Configure the Tx VSI for operation.
3093 **/
3094static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3095{
3096 int err = 0;
3097 u16 i;
3098
Alexander Duyck9f65e152013-09-28 06:00:58 +00003099 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3100 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003101
3102 return err;
3103}
3104
3105/**
3106 * i40e_vsi_configure_rx - Configure the VSI for Rx
3107 * @vsi: the VSI being configured
3108 *
3109 * Configure the Rx VSI for operation.
3110 **/
3111static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3112{
3113 int err = 0;
3114 u16 i;
3115
3116 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3117 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3118 + ETH_FCS_LEN + VLAN_HLEN;
3119 else
3120 vsi->max_frame = I40E_RXBUFFER_2048;
3121
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003122 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003123
Vasu Dev38e00432014-08-01 13:27:03 -07003124#ifdef I40E_FCOE
3125 /* setup rx buffer for FCoE */
3126 if ((vsi->type == I40E_VSI_FCOE) &&
3127 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003128 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3129 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003130 }
3131
3132#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003133 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003134 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003135 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003136
3137 /* set up individual rings */
3138 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003139 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003140
3141 return err;
3142}
3143
3144/**
3145 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3146 * @vsi: ptr to the VSI
3147 **/
3148static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3149{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003150 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003151 u16 qoffset, qcount;
3152 int i, n;
3153
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003154 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3155 /* Reset the TC information */
3156 for (i = 0; i < vsi->num_queue_pairs; i++) {
3157 rx_ring = vsi->rx_rings[i];
3158 tx_ring = vsi->tx_rings[i];
3159 rx_ring->dcb_tc = 0;
3160 tx_ring->dcb_tc = 0;
3161 }
3162 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003163
3164 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003165 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003166 continue;
3167
3168 qoffset = vsi->tc_config.tc_info[n].qoffset;
3169 qcount = vsi->tc_config.tc_info[n].qcount;
3170 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003171 rx_ring = vsi->rx_rings[i];
3172 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003173 rx_ring->dcb_tc = n;
3174 tx_ring->dcb_tc = n;
3175 }
3176 }
3177}
3178
3179/**
3180 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3181 * @vsi: ptr to the VSI
3182 **/
3183static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3184{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003185 struct i40e_pf *pf = vsi->back;
3186 int err;
3187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003188 if (vsi->netdev)
3189 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003190
3191 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3192 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3193 if (err) {
3194 dev_warn(&pf->pdev->dev,
3195 "could not set up macaddr; err %d\n", err);
3196 }
3197 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003198}
3199
3200/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003201 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3202 * @vsi: Pointer to the targeted VSI
3203 *
3204 * This function replays the hlist on the hw where all the SB Flow Director
3205 * filters were saved.
3206 **/
3207static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3208{
3209 struct i40e_fdir_filter *filter;
3210 struct i40e_pf *pf = vsi->back;
3211 struct hlist_node *node;
3212
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003213 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3214 return;
3215
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003216 hlist_for_each_entry_safe(filter, node,
3217 &pf->fdir_filter_list, fdir_node) {
3218 i40e_add_del_fdir(vsi, filter, true);
3219 }
3220}
3221
3222/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003223 * i40e_vsi_configure - Set up the VSI for action
3224 * @vsi: the VSI being configured
3225 **/
3226static int i40e_vsi_configure(struct i40e_vsi *vsi)
3227{
3228 int err;
3229
3230 i40e_set_vsi_rx_mode(vsi);
3231 i40e_restore_vlan(vsi);
3232 i40e_vsi_config_dcb_rings(vsi);
3233 err = i40e_vsi_configure_tx(vsi);
3234 if (!err)
3235 err = i40e_vsi_configure_rx(vsi);
3236
3237 return err;
3238}
3239
3240/**
3241 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3242 * @vsi: the VSI being configured
3243 **/
3244static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3245{
3246 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003247 struct i40e_hw *hw = &pf->hw;
3248 u16 vector;
3249 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003250 u32 qp;
3251
3252 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3253 * and PFINT_LNKLSTn registers, e.g.:
3254 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3255 */
3256 qp = vsi->base_queue;
3257 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003258 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003259 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3260
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003261 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003262 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003263 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3264 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3265 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003266 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3268 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3269 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003270 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3271 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003272
3273 /* Linked list for the queuepairs assigned to this vector */
3274 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3275 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003276 u32 val;
3277
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003278 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3279 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3280 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3281 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3282 (I40E_QUEUE_TYPE_TX
3283 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3284
3285 wr32(hw, I40E_QINT_RQCTL(qp), val);
3286
3287 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3288 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3289 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3290 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3291 (I40E_QUEUE_TYPE_RX
3292 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3293
3294 /* Terminate the linked list */
3295 if (q == (q_vector->num_ringpairs - 1))
3296 val |= (I40E_QUEUE_END_OF_LIST
3297 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3298
3299 wr32(hw, I40E_QINT_TQCTL(qp), val);
3300 qp++;
3301 }
3302 }
3303
3304 i40e_flush(hw);
3305}
3306
3307/**
3308 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3309 * @hw: ptr to the hardware info
3310 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003311static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003312{
Jacob Kellerab437b52014-12-14 01:55:08 +00003313 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003314 u32 val;
3315
3316 /* clear things first */
3317 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3318 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3319
3320 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3321 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3322 I40E_PFINT_ICR0_ENA_GRST_MASK |
3323 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3324 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3326 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3327 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3328
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003329 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3330 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3331
Jacob Kellerab437b52014-12-14 01:55:08 +00003332 if (pf->flags & I40E_FLAG_PTP)
3333 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3334
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003335 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3336
3337 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003338 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3339 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003340
3341 /* OTHER_ITR_IDX = 0 */
3342 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3343}
3344
3345/**
3346 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3347 * @vsi: the VSI being configured
3348 **/
3349static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3350{
Alexander Duyck493fb302013-09-28 07:01:44 +00003351 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003352 struct i40e_pf *pf = vsi->back;
3353 struct i40e_hw *hw = &pf->hw;
3354 u32 val;
3355
3356 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003357 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003358 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003359 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3360 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003361 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003362 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3363 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3364
Jacob Kellerab437b52014-12-14 01:55:08 +00003365 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003366
3367 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3368 wr32(hw, I40E_PFINT_LNKLST0, 0);
3369
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003370 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3372 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3373 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3374
3375 wr32(hw, I40E_QINT_RQCTL(0), val);
3376
3377 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3378 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3379 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3380
3381 wr32(hw, I40E_QINT_TQCTL(0), val);
3382 i40e_flush(hw);
3383}
3384
3385/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003386 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3387 * @pf: board private structure
3388 **/
3389void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3390{
3391 struct i40e_hw *hw = &pf->hw;
3392
3393 wr32(hw, I40E_PFINT_DYN_CTL0,
3394 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3395 i40e_flush(hw);
3396}
3397
3398/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003399 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3400 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003401 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003402 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003403void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003404{
3405 struct i40e_hw *hw = &pf->hw;
3406 u32 val;
3407
3408 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003409 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003410 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3411
3412 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3413 i40e_flush(hw);
3414}
3415
3416/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003417 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3418 * @irq: interrupt number
3419 * @data: pointer to a q_vector
3420 **/
3421static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3422{
3423 struct i40e_q_vector *q_vector = data;
3424
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003425 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003426 return IRQ_HANDLED;
3427
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003428 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003429
3430 return IRQ_HANDLED;
3431}
3432
3433/**
Alan Brady96db7762016-09-14 16:24:38 -07003434 * i40e_irq_affinity_notify - Callback for affinity changes
3435 * @notify: context as to what irq was changed
3436 * @mask: the new affinity mask
3437 *
3438 * This is a callback function used by the irq_set_affinity_notifier function
3439 * so that we may register to receive changes to the irq affinity masks.
3440 **/
3441static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3442 const cpumask_t *mask)
3443{
3444 struct i40e_q_vector *q_vector =
3445 container_of(notify, struct i40e_q_vector, affinity_notify);
3446
3447 q_vector->affinity_mask = *mask;
3448}
3449
3450/**
3451 * i40e_irq_affinity_release - Callback for affinity notifier release
3452 * @ref: internal core kernel usage
3453 *
3454 * This is a callback function used by the irq_set_affinity_notifier function
3455 * to inform the current notification subscriber that they will no longer
3456 * receive notifications.
3457 **/
3458static void i40e_irq_affinity_release(struct kref *ref) {}
3459
3460/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3462 * @vsi: the VSI being configured
3463 * @basename: name for the vector
3464 *
3465 * Allocates MSI-X vectors and requests interrupts from the kernel.
3466 **/
3467static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3468{
3469 int q_vectors = vsi->num_q_vectors;
3470 struct i40e_pf *pf = vsi->back;
3471 int base = vsi->base_vector;
3472 int rx_int_idx = 0;
3473 int tx_int_idx = 0;
3474 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003475 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003476
3477 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003478 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003479
Alan Brady96db7762016-09-14 16:24:38 -07003480 irq_num = pf->msix_entries[base + vector].vector;
3481
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003482 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003483 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3484 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3485 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003486 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3488 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003489 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003490 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3491 "%s-%s-%d", basename, "tx", tx_int_idx++);
3492 } else {
3493 /* skip this unused q_vector */
3494 continue;
3495 }
Alan Brady96db7762016-09-14 16:24:38 -07003496 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003497 vsi->irq_handler,
3498 0,
3499 q_vector->name,
3500 q_vector);
3501 if (err) {
3502 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003503 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003504 goto free_queue_irqs;
3505 }
Alan Brady96db7762016-09-14 16:24:38 -07003506
3507 /* register for affinity change notifications */
3508 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3509 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3510 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003511 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003512 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003513 }
3514
Shannon Nelson63741842014-04-23 04:50:16 +00003515 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003516 return 0;
3517
3518free_queue_irqs:
3519 while (vector) {
3520 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003521 irq_num = pf->msix_entries[base + vector].vector;
3522 irq_set_affinity_notifier(irq_num, NULL);
3523 irq_set_affinity_hint(irq_num, NULL);
3524 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003525 }
3526 return err;
3527}
3528
3529/**
3530 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3531 * @vsi: the VSI being un-configured
3532 **/
3533static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3534{
3535 struct i40e_pf *pf = vsi->back;
3536 struct i40e_hw *hw = &pf->hw;
3537 int base = vsi->base_vector;
3538 int i;
3539
3540 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003541 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3542 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003543 }
3544
3545 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3546 for (i = vsi->base_vector;
3547 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3548 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3549
3550 i40e_flush(hw);
3551 for (i = 0; i < vsi->num_q_vectors; i++)
3552 synchronize_irq(pf->msix_entries[i + base].vector);
3553 } else {
3554 /* Legacy and MSI mode - this stops all interrupt handling */
3555 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3556 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3557 i40e_flush(hw);
3558 synchronize_irq(pf->pdev->irq);
3559 }
3560}
3561
3562/**
3563 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3564 * @vsi: the VSI being configured
3565 **/
3566static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3567{
3568 struct i40e_pf *pf = vsi->back;
3569 int i;
3570
3571 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003572 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573 i40e_irq_dynamic_enable(vsi, i);
3574 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003575 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003576 }
3577
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003578 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003579 return 0;
3580}
3581
3582/**
3583 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3584 * @pf: board private structure
3585 **/
3586static void i40e_stop_misc_vector(struct i40e_pf *pf)
3587{
3588 /* Disable ICR 0 */
3589 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3590 i40e_flush(&pf->hw);
3591}
3592
3593/**
3594 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3595 * @irq: interrupt number
3596 * @data: pointer to a q_vector
3597 *
3598 * This is the handler used for all MSI/Legacy interrupts, and deals
3599 * with both queue and non-queue interrupts. This is also used in
3600 * MSIX mode to handle the non-queue interrupts.
3601 **/
3602static irqreturn_t i40e_intr(int irq, void *data)
3603{
3604 struct i40e_pf *pf = (struct i40e_pf *)data;
3605 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003606 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003607 u32 icr0, icr0_remaining;
3608 u32 val, ena_mask;
3609
3610 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003611 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003612
Shannon Nelson116a57d2013-09-28 07:13:59 +00003613 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3614 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003615 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003616
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003617 /* if interrupt but no bits showing, must be SWINT */
3618 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3619 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3620 pf->sw_int_count++;
3621
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003622 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3623 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3624 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3625 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003626 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003627 }
3628
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003629 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3630 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003631 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3632 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003633
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003634 /* We do not have a way to disarm Queue causes while leaving
3635 * interrupt enabled for all other causes, ideally
3636 * interrupt should be disabled while we are in NAPI but
3637 * this is not a performance path and napi_schedule()
3638 * can deal with rescheduling.
3639 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003640 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003641 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003642 }
3643
3644 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3645 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3646 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003647 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003648 }
3649
3650 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3651 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3652 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3653 }
3654
3655 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3656 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3657 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3658 }
3659
3660 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3661 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3662 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3663 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3664 val = rd32(hw, I40E_GLGEN_RSTAT);
3665 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3666 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003667 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003668 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003669 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003671 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003673 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003674 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 }
3676
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003677 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3678 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3679 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003680 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3681 rd32(hw, I40E_PFHMC_ERRORINFO),
3682 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003683 }
3684
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003685 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3686 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3687
3688 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003689 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003690 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003691 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003692 }
3693
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003694 /* If a critical error is pending we have no choice but to reset the
3695 * device.
3696 * Report and mask out any remaining unexpected interrupts.
3697 */
3698 icr0_remaining = icr0 & ena_mask;
3699 if (icr0_remaining) {
3700 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3701 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003702 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003703 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003704 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003705 dev_info(&pf->pdev->dev, "device will be reset\n");
3706 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3707 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003708 }
3709 ena_mask &= ~icr0_remaining;
3710 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003711 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003713enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003714 /* re-enable interrupt causes */
3715 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003716 if (!test_bit(__I40E_DOWN, &pf->state)) {
3717 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003718 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003719 }
3720
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003721 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003722}
3723
3724/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003725 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3726 * @tx_ring: tx ring to clean
3727 * @budget: how many cleans we're allowed
3728 *
3729 * Returns true if there's any budget left (e.g. the clean is finished)
3730 **/
3731static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3732{
3733 struct i40e_vsi *vsi = tx_ring->vsi;
3734 u16 i = tx_ring->next_to_clean;
3735 struct i40e_tx_buffer *tx_buf;
3736 struct i40e_tx_desc *tx_desc;
3737
3738 tx_buf = &tx_ring->tx_bi[i];
3739 tx_desc = I40E_TX_DESC(tx_ring, i);
3740 i -= tx_ring->count;
3741
3742 do {
3743 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3744
3745 /* if next_to_watch is not set then there is no work pending */
3746 if (!eop_desc)
3747 break;
3748
3749 /* prevent any other reads prior to eop_desc */
3750 read_barrier_depends();
3751
3752 /* if the descriptor isn't done, no work yet to do */
3753 if (!(eop_desc->cmd_type_offset_bsz &
3754 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3755 break;
3756
3757 /* clear next_to_watch to prevent false hangs */
3758 tx_buf->next_to_watch = NULL;
3759
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003760 tx_desc->buffer_addr = 0;
3761 tx_desc->cmd_type_offset_bsz = 0;
3762 /* move past filter desc */
3763 tx_buf++;
3764 tx_desc++;
3765 i++;
3766 if (unlikely(!i)) {
3767 i -= tx_ring->count;
3768 tx_buf = tx_ring->tx_bi;
3769 tx_desc = I40E_TX_DESC(tx_ring, 0);
3770 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003771 /* unmap skb header data */
3772 dma_unmap_single(tx_ring->dev,
3773 dma_unmap_addr(tx_buf, dma),
3774 dma_unmap_len(tx_buf, len),
3775 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003776 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3777 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003778
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003779 tx_buf->raw_buf = NULL;
3780 tx_buf->tx_flags = 0;
3781 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003782 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003783 tx_desc->buffer_addr = 0;
3784 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003785
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003786 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003787 tx_buf++;
3788 tx_desc++;
3789 i++;
3790 if (unlikely(!i)) {
3791 i -= tx_ring->count;
3792 tx_buf = tx_ring->tx_bi;
3793 tx_desc = I40E_TX_DESC(tx_ring, 0);
3794 }
3795
3796 /* update budget accounting */
3797 budget--;
3798 } while (likely(budget));
3799
3800 i += tx_ring->count;
3801 tx_ring->next_to_clean = i;
3802
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003803 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003804 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003805
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003806 return budget > 0;
3807}
3808
3809/**
3810 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3811 * @irq: interrupt number
3812 * @data: pointer to a q_vector
3813 **/
3814static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3815{
3816 struct i40e_q_vector *q_vector = data;
3817 struct i40e_vsi *vsi;
3818
3819 if (!q_vector->tx.ring)
3820 return IRQ_HANDLED;
3821
3822 vsi = q_vector->tx.ring->vsi;
3823 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3824
3825 return IRQ_HANDLED;
3826}
3827
3828/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003829 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003830 * @vsi: the VSI being configured
3831 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003832 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003833 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003834static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003835{
Alexander Duyck493fb302013-09-28 07:01:44 +00003836 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003837 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3838 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003839
3840 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003841 tx_ring->next = q_vector->tx.ring;
3842 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003843 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003844
3845 rx_ring->q_vector = q_vector;
3846 rx_ring->next = q_vector->rx.ring;
3847 q_vector->rx.ring = rx_ring;
3848 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003849}
3850
3851/**
3852 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3853 * @vsi: the VSI being configured
3854 *
3855 * This function maps descriptor rings to the queue-specific vectors
3856 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3857 * one vector per queue pair, but on a constrained vector budget, we
3858 * group the queue pairs as "efficiently" as possible.
3859 **/
3860static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3861{
3862 int qp_remaining = vsi->num_queue_pairs;
3863 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003864 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003865 int v_start = 0;
3866 int qp_idx = 0;
3867
3868 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3869 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003870 * It is also important to go through all the vectors available to be
3871 * sure that if we don't use all the vectors, that the remaining vectors
3872 * are cleared. This is especially important when decreasing the
3873 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003875 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003876 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3877
3878 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3879
3880 q_vector->num_ringpairs = num_ringpairs;
3881
3882 q_vector->rx.count = 0;
3883 q_vector->tx.count = 0;
3884 q_vector->rx.ring = NULL;
3885 q_vector->tx.ring = NULL;
3886
3887 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003888 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003889 qp_idx++;
3890 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003891 }
3892 }
3893}
3894
3895/**
3896 * i40e_vsi_request_irq - Request IRQ from the OS
3897 * @vsi: the VSI being configured
3898 * @basename: name for the vector
3899 **/
3900static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3901{
3902 struct i40e_pf *pf = vsi->back;
3903 int err;
3904
3905 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3906 err = i40e_vsi_request_irq_msix(vsi, basename);
3907 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3908 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003909 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003910 else
3911 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003912 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913
3914 if (err)
3915 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3916
3917 return err;
3918}
3919
3920#ifdef CONFIG_NET_POLL_CONTROLLER
3921/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003922 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003923 * @netdev: network interface device structure
3924 *
3925 * This is used by netconsole to send skbs without having to re-enable
3926 * interrupts. It's not called while the normal interrupt routine is executing.
3927 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003928#ifdef I40E_FCOE
3929void i40e_netpoll(struct net_device *netdev)
3930#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003931static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003932#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003933{
3934 struct i40e_netdev_priv *np = netdev_priv(netdev);
3935 struct i40e_vsi *vsi = np->vsi;
3936 struct i40e_pf *pf = vsi->back;
3937 int i;
3938
3939 /* if interface is down do nothing */
3940 if (test_bit(__I40E_DOWN, &vsi->state))
3941 return;
3942
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003943 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3944 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003945 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003946 } else {
3947 i40e_intr(pf->pdev->irq, netdev);
3948 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003949}
3950#endif
3951
3952/**
Neerav Parikh23527302014-06-03 23:50:15 +00003953 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3954 * @pf: the PF being configured
3955 * @pf_q: the PF queue
3956 * @enable: enable or disable state of the queue
3957 *
3958 * This routine will wait for the given Tx queue of the PF to reach the
3959 * enabled or disabled state.
3960 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3961 * multiple retries; else will return 0 in case of success.
3962 **/
3963static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3964{
3965 int i;
3966 u32 tx_reg;
3967
3968 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3969 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3970 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3971 break;
3972
Neerav Parikhf98a2002014-09-13 07:40:44 +00003973 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003974 }
3975 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3976 return -ETIMEDOUT;
3977
3978 return 0;
3979}
3980
3981/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003982 * i40e_vsi_control_tx - Start or stop a VSI's rings
3983 * @vsi: the VSI being configured
3984 * @enable: start or stop the rings
3985 **/
3986static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3987{
3988 struct i40e_pf *pf = vsi->back;
3989 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003990 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003991 u32 tx_reg;
3992
3993 pf_q = vsi->base_queue;
3994 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003995
3996 /* warn the TX unit of coming changes */
3997 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3998 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003999 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00004000
Mitch Williams6c5ef622014-02-20 19:29:16 -08004001 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004002 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004003 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4004 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4005 break;
4006 usleep_range(1000, 2000);
4007 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00004008 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00004009 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00004010 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004011
4012 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004013 if (enable) {
4014 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08004015 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004016 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004017 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004018 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019
4020 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004021 /* No waiting for the Tx queue to disable */
4022 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4023 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004024
4025 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004026 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4027 if (ret) {
4028 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004029 "VSI seid %d Tx ring %d %sable timeout\n",
4030 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004031 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004032 }
4033 }
4034
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004035 if (hw->revision_id == 0)
4036 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00004037 return ret;
4038}
4039
4040/**
4041 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4042 * @pf: the PF being configured
4043 * @pf_q: the PF queue
4044 * @enable: enable or disable state of the queue
4045 *
4046 * This routine will wait for the given Rx queue of the PF to reach the
4047 * enabled or disabled state.
4048 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4049 * multiple retries; else will return 0 in case of success.
4050 **/
4051static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4052{
4053 int i;
4054 u32 rx_reg;
4055
4056 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4057 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4058 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4059 break;
4060
Neerav Parikhf98a2002014-09-13 07:40:44 +00004061 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004062 }
4063 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4064 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004065
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004066 return 0;
4067}
4068
4069/**
4070 * i40e_vsi_control_rx - Start or stop a VSI's rings
4071 * @vsi: the VSI being configured
4072 * @enable: start or stop the rings
4073 **/
4074static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4075{
4076 struct i40e_pf *pf = vsi->back;
4077 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004078 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004079 u32 rx_reg;
4080
4081 pf_q = vsi->base_queue;
4082 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004083 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004084 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004085 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4086 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4087 break;
4088 usleep_range(1000, 2000);
4089 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004090
Catherine Sullivan7c1220072014-03-14 07:32:29 +00004091 /* Skip if the queue is already in the requested state */
4092 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4093 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004094
4095 /* turn on/off the queue */
4096 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004097 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004098 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004099 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004100 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004101 /* No waiting for the Tx queue to disable */
4102 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4103 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004104
4105 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004106 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4107 if (ret) {
4108 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004109 "VSI seid %d Rx ring %d %sable timeout\n",
4110 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004111 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004112 }
4113 }
4114
Neerav Parikh23527302014-06-03 23:50:15 +00004115 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004116}
4117
4118/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004119 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004120 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004121 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004122int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004123{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004124 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004125
4126 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004127 ret = i40e_vsi_control_rx(vsi, true);
4128 if (ret)
4129 return ret;
4130 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004131
4132 return ret;
4133}
4134
4135/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004136 * i40e_vsi_stop_rings - Stop a VSI's rings
4137 * @vsi: the VSI being configured
4138 **/
4139void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4140{
4141 /* do rx first for enable and last for disable
4142 * Ignore return value, we need to shutdown whatever we can
4143 */
4144 i40e_vsi_control_tx(vsi, false);
4145 i40e_vsi_control_rx(vsi, false);
4146}
4147
4148/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004149 * i40e_vsi_free_irq - Free the irq association with the OS
4150 * @vsi: the VSI being configured
4151 **/
4152static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4153{
4154 struct i40e_pf *pf = vsi->back;
4155 struct i40e_hw *hw = &pf->hw;
4156 int base = vsi->base_vector;
4157 u32 val, qp;
4158 int i;
4159
4160 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4161 if (!vsi->q_vectors)
4162 return;
4163
Shannon Nelson63741842014-04-23 04:50:16 +00004164 if (!vsi->irqs_ready)
4165 return;
4166
4167 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004168 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004169 int irq_num;
4170 u16 vector;
4171
4172 vector = i + base;
4173 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004174
4175 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004176 if (!vsi->q_vectors[i] ||
4177 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004178 continue;
4179
Alan Brady96db7762016-09-14 16:24:38 -07004180 /* clear the affinity notifier in the IRQ descriptor */
4181 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004182 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004183 irq_set_affinity_hint(irq_num, NULL);
4184 synchronize_irq(irq_num);
4185 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004186
4187 /* Tear down the interrupt queue link list
4188 *
4189 * We know that they come in pairs and always
4190 * the Rx first, then the Tx. To clear the
4191 * link list, stick the EOL value into the
4192 * next_q field of the registers.
4193 */
4194 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4195 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4196 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4197 val |= I40E_QUEUE_END_OF_LIST
4198 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4199 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4200
4201 while (qp != I40E_QUEUE_END_OF_LIST) {
4202 u32 next;
4203
4204 val = rd32(hw, I40E_QINT_RQCTL(qp));
4205
4206 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4207 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4208 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4209 I40E_QINT_RQCTL_INTEVENT_MASK);
4210
4211 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4212 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4213
4214 wr32(hw, I40E_QINT_RQCTL(qp), val);
4215
4216 val = rd32(hw, I40E_QINT_TQCTL(qp));
4217
4218 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4219 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4220
4221 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4222 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4223 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4224 I40E_QINT_TQCTL_INTEVENT_MASK);
4225
4226 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4227 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4228
4229 wr32(hw, I40E_QINT_TQCTL(qp), val);
4230 qp = next;
4231 }
4232 }
4233 } else {
4234 free_irq(pf->pdev->irq, pf);
4235
4236 val = rd32(hw, I40E_PFINT_LNKLST0);
4237 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4238 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4239 val |= I40E_QUEUE_END_OF_LIST
4240 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4241 wr32(hw, I40E_PFINT_LNKLST0, val);
4242
4243 val = rd32(hw, I40E_QINT_RQCTL(qp));
4244 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4245 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4246 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4247 I40E_QINT_RQCTL_INTEVENT_MASK);
4248
4249 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4250 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4251
4252 wr32(hw, I40E_QINT_RQCTL(qp), val);
4253
4254 val = rd32(hw, I40E_QINT_TQCTL(qp));
4255
4256 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4257 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4258 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4259 I40E_QINT_TQCTL_INTEVENT_MASK);
4260
4261 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4262 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4263
4264 wr32(hw, I40E_QINT_TQCTL(qp), val);
4265 }
4266}
4267
4268/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004269 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4270 * @vsi: the VSI being configured
4271 * @v_idx: Index of vector to be freed
4272 *
4273 * This function frees the memory allocated to the q_vector. In addition if
4274 * NAPI is enabled it will delete any references to the NAPI struct prior
4275 * to freeing the q_vector.
4276 **/
4277static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4278{
4279 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004280 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004281
4282 if (!q_vector)
4283 return;
4284
4285 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004286 i40e_for_each_ring(ring, q_vector->tx)
4287 ring->q_vector = NULL;
4288
4289 i40e_for_each_ring(ring, q_vector->rx)
4290 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004291
4292 /* only VSI w/ an associated netdev is set up w/ NAPI */
4293 if (vsi->netdev)
4294 netif_napi_del(&q_vector->napi);
4295
4296 vsi->q_vectors[v_idx] = NULL;
4297
4298 kfree_rcu(q_vector, rcu);
4299}
4300
4301/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004302 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4303 * @vsi: the VSI being un-configured
4304 *
4305 * This frees the memory allocated to the q_vectors and
4306 * deletes references to the NAPI struct.
4307 **/
4308static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4309{
4310 int v_idx;
4311
Alexander Duyck493fb302013-09-28 07:01:44 +00004312 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4313 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004314}
4315
4316/**
4317 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4318 * @pf: board private structure
4319 **/
4320static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4321{
4322 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4323 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4324 pci_disable_msix(pf->pdev);
4325 kfree(pf->msix_entries);
4326 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004327 kfree(pf->irq_pile);
4328 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004329 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4330 pci_disable_msi(pf->pdev);
4331 }
4332 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4333}
4334
4335/**
4336 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4337 * @pf: board private structure
4338 *
4339 * We go through and clear interrupt specific resources and reset the structure
4340 * to pre-load conditions
4341 **/
4342static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4343{
4344 int i;
4345
Shannon Nelsone1477582015-02-21 06:44:33 +00004346 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004347 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004348 synchronize_irq(pf->msix_entries[0].vector);
4349 free_irq(pf->msix_entries[0].vector, pf);
4350 }
4351
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004352 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4353 I40E_IWARP_IRQ_PILE_ID);
4354
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004355 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004356 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004357 if (pf->vsi[i])
4358 i40e_vsi_free_q_vectors(pf->vsi[i]);
4359 i40e_reset_interrupt_capability(pf);
4360}
4361
4362/**
4363 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4364 * @vsi: the VSI being configured
4365 **/
4366static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4367{
4368 int q_idx;
4369
4370 if (!vsi->netdev)
4371 return;
4372
4373 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004374 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004375}
4376
4377/**
4378 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4379 * @vsi: the VSI being configured
4380 **/
4381static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4382{
4383 int q_idx;
4384
4385 if (!vsi->netdev)
4386 return;
4387
4388 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004389 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004390}
4391
4392/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004393 * i40e_vsi_close - Shut down a VSI
4394 * @vsi: the vsi to be quelled
4395 **/
4396static void i40e_vsi_close(struct i40e_vsi *vsi)
4397{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004398 bool reset = false;
4399
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004400 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4401 i40e_down(vsi);
4402 i40e_vsi_free_irq(vsi);
4403 i40e_vsi_free_tx_resources(vsi);
4404 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004405 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004406 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4407 reset = true;
4408 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004409}
4410
4411/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004412 * i40e_quiesce_vsi - Pause a given VSI
4413 * @vsi: the VSI being paused
4414 **/
4415static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4416{
4417 if (test_bit(__I40E_DOWN, &vsi->state))
4418 return;
4419
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004420 /* No need to disable FCoE VSI when Tx suspended */
4421 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4422 vsi->type == I40E_VSI_FCOE) {
4423 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004424 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004425 return;
4426 }
4427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004428 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004429 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004430 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004431 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004432 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004433}
4434
4435/**
4436 * i40e_unquiesce_vsi - Resume a given VSI
4437 * @vsi: the VSI being resumed
4438 **/
4439static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4440{
4441 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4442 return;
4443
4444 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4445 if (vsi->netdev && netif_running(vsi->netdev))
4446 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4447 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004448 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004449}
4450
4451/**
4452 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4453 * @pf: the PF
4454 **/
4455static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4456{
4457 int v;
4458
Mitch Williams505682c2014-05-20 08:01:37 +00004459 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004460 if (pf->vsi[v])
4461 i40e_quiesce_vsi(pf->vsi[v]);
4462 }
4463}
4464
4465/**
4466 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4467 * @pf: the PF
4468 **/
4469static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4470{
4471 int v;
4472
Mitch Williams505682c2014-05-20 08:01:37 +00004473 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004474 if (pf->vsi[v])
4475 i40e_unquiesce_vsi(pf->vsi[v]);
4476 }
4477}
4478
Neerav Parikh69129dc2014-11-12 00:18:46 +00004479#ifdef CONFIG_I40E_DCB
4480/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004481 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004482 * @vsi: the VSI being configured
4483 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004484 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004485 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004486static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004487{
4488 struct i40e_pf *pf = vsi->back;
4489 int i, pf_q, ret;
4490
4491 pf_q = vsi->base_queue;
4492 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4493 /* Check and wait for the disable status of the queue */
4494 ret = i40e_pf_txq_wait(pf, pf_q, false);
4495 if (ret) {
4496 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004497 "VSI seid %d Tx ring %d disable timeout\n",
4498 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004499 return ret;
4500 }
4501 }
4502
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004503 pf_q = vsi->base_queue;
4504 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4505 /* Check and wait for the disable status of the queue */
4506 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4507 if (ret) {
4508 dev_info(&pf->pdev->dev,
4509 "VSI seid %d Rx ring %d disable timeout\n",
4510 vsi->seid, pf_q);
4511 return ret;
4512 }
4513 }
4514
Neerav Parikh69129dc2014-11-12 00:18:46 +00004515 return 0;
4516}
4517
4518/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004519 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004520 * @pf: the PF
4521 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004522 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004523 * VSIs that are managed by this PF.
4524 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004525static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004526{
4527 int v, ret = 0;
4528
4529 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004530 /* No need to wait for FCoE VSI queues */
4531 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004532 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004533 if (ret)
4534 break;
4535 }
4536 }
4537
4538 return ret;
4539}
4540
4541#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004542
4543/**
4544 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4545 * @q_idx: TX queue number
4546 * @vsi: Pointer to VSI struct
4547 *
4548 * This function checks specified queue for given VSI. Detects hung condition.
4549 * Sets hung bit since it is two step process. Before next run of service task
4550 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4551 * hung condition remain unchanged and during subsequent run, this function
4552 * issues SW interrupt to recover from hung condition.
4553 **/
4554static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4555{
4556 struct i40e_ring *tx_ring = NULL;
4557 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004558 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004559 int i;
4560
4561 pf = vsi->back;
4562
4563 /* now that we have an index, find the tx_ring struct */
4564 for (i = 0; i < vsi->num_queue_pairs; i++) {
4565 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4566 if (q_idx == vsi->tx_rings[i]->queue_index) {
4567 tx_ring = vsi->tx_rings[i];
4568 break;
4569 }
4570 }
4571 }
4572
4573 if (!tx_ring)
4574 return;
4575
4576 /* Read interrupt register */
4577 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4578 val = rd32(&pf->hw,
4579 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4580 tx_ring->vsi->base_vector - 1));
4581 else
4582 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4583
4584 head = i40e_get_head(tx_ring);
4585
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004586 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004587
Kiran Patil9c6c1252015-11-06 15:26:02 -08004588 /* HW is done executing descriptors, updated HEAD write back,
4589 * but SW hasn't processed those descriptors. If interrupt is
4590 * not generated from this point ON, it could result into
4591 * dev_watchdog detecting timeout on those netdev_queue,
4592 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004593 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004594 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004595 /* NAPI Poll didn't run and clear since it was set */
4596 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4597 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004598 netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
4599 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004600 tx_ring->next_to_clean, head,
4601 tx_ring->next_to_use,
4602 readl(tx_ring->tail));
4603 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4604 vsi->seid, q_idx, val);
4605 i40e_force_wb(vsi, tx_ring->q_vector);
4606 } else {
4607 /* First Chance - detected possible hung */
4608 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4609 &tx_ring->q_vector->hung_detected);
4610 }
4611 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004612
4613 /* This is the case where we have interrupts missing,
4614 * so the tx_pending in HW will most likely be 0, but we
4615 * will have tx_pending in SW since the WB happened but the
4616 * interrupt got lost.
4617 */
4618 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4619 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4620 if (napi_reschedule(&tx_ring->q_vector->napi))
4621 tx_ring->tx_stats.tx_lost_interrupt++;
4622 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004623}
4624
4625/**
4626 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4627 * @pf: pointer to PF struct
4628 *
4629 * LAN VSI has netdev and netdev has TX queues. This function is to check
4630 * each of those TX queues if they are hung, trigger recovery by issuing
4631 * SW interrupt.
4632 **/
4633static void i40e_detect_recover_hung(struct i40e_pf *pf)
4634{
4635 struct net_device *netdev;
4636 struct i40e_vsi *vsi;
4637 int i;
4638
4639 /* Only for LAN VSI */
4640 vsi = pf->vsi[pf->lan_vsi];
4641
4642 if (!vsi)
4643 return;
4644
4645 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4646 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4647 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4648 return;
4649
4650 /* Make sure type is MAIN VSI */
4651 if (vsi->type != I40E_VSI_MAIN)
4652 return;
4653
4654 netdev = vsi->netdev;
4655 if (!netdev)
4656 return;
4657
4658 /* Bail out if netif_carrier is not OK */
4659 if (!netif_carrier_ok(netdev))
4660 return;
4661
4662 /* Go thru' TX queues for netdev */
4663 for (i = 0; i < netdev->num_tx_queues; i++) {
4664 struct netdev_queue *q;
4665
4666 q = netdev_get_tx_queue(netdev, i);
4667 if (q)
4668 i40e_detect_recover_hung_queue(i, vsi);
4669 }
4670}
4671
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004672/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004673 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004674 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004675 *
4676 * Get TC map for ISCSI PF type that will include iSCSI TC
4677 * and LAN TC.
4678 **/
4679static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4680{
4681 struct i40e_dcb_app_priority_table app;
4682 struct i40e_hw *hw = &pf->hw;
4683 u8 enabled_tc = 1; /* TC0 is always enabled */
4684 u8 tc, i;
4685 /* Get the iSCSI APP TLV */
4686 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4687
4688 for (i = 0; i < dcbcfg->numapps; i++) {
4689 app = dcbcfg->app[i];
4690 if (app.selector == I40E_APP_SEL_TCPIP &&
4691 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4692 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004693 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004694 break;
4695 }
4696 }
4697
4698 return enabled_tc;
4699}
4700
4701/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004702 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4703 * @dcbcfg: the corresponding DCBx configuration structure
4704 *
4705 * Return the number of TCs from given DCBx configuration
4706 **/
4707static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4708{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004709 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004710 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004711 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004712
4713 /* Scan the ETS Config Priority Table to find
4714 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004715 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004716 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004717 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4718 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4719
4720 /* Now scan the bitmask to check for
4721 * contiguous TCs starting with TC0
4722 */
4723 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4724 if (num_tc & BIT(i)) {
4725 if (!tc_unused) {
4726 ret++;
4727 } else {
4728 pr_err("Non-contiguous TC - Disabling DCB\n");
4729 return 1;
4730 }
4731 } else {
4732 tc_unused = 1;
4733 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004734 }
4735
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004736 /* There is always at least TC0 */
4737 if (!ret)
4738 ret = 1;
4739
4740 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004741}
4742
4743/**
4744 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4745 * @dcbcfg: the corresponding DCBx configuration structure
4746 *
4747 * Query the current DCB configuration and return the number of
4748 * traffic classes enabled from the given DCBX config
4749 **/
4750static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4751{
4752 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4753 u8 enabled_tc = 1;
4754 u8 i;
4755
4756 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004757 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758
4759 return enabled_tc;
4760}
4761
4762/**
4763 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4764 * @pf: PF being queried
4765 *
4766 * Return number of traffic classes enabled for the given PF
4767 **/
4768static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4769{
4770 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004771 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004772 u8 num_tc = 0;
4773 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4774
4775 /* If DCB is not enabled then always in single TC */
4776 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4777 return 1;
4778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004779 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004780 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4781 return i40e_dcb_get_num_tc(dcbcfg);
4782
4783 /* MFP mode return count of enabled TCs for this PF */
4784 if (pf->hw.func_caps.iscsi)
4785 enabled_tc = i40e_get_iscsi_tc_map(pf);
4786 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004787 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004788
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004789 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004790 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004791 num_tc++;
4792 }
4793 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004794}
4795
4796/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004797 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4798 * @pf: PF being queried
4799 *
4800 * Return a bitmap for enabled traffic classes for this PF.
4801 **/
4802static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4803{
4804 /* If DCB is not enabled for this PF then just return default TC */
4805 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004806 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004807
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004808 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004809 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4810 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4811
Neerav Parikhfc51de92015-02-24 06:58:53 +00004812 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004813 if (pf->hw.func_caps.iscsi)
4814 return i40e_get_iscsi_tc_map(pf);
4815 else
David Ertmanea6acb72016-09-20 07:10:50 -07004816 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004817}
4818
4819/**
4820 * i40e_vsi_get_bw_info - Query VSI BW Information
4821 * @vsi: the VSI being queried
4822 *
4823 * Returns 0 on success, negative value on failure
4824 **/
4825static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4826{
4827 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4828 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4829 struct i40e_pf *pf = vsi->back;
4830 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004831 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004832 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833 int i;
4834
4835 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004836 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4837 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004839 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4840 i40e_stat_str(&pf->hw, ret),
4841 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004842 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004843 }
4844
4845 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004846 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4847 NULL);
4848 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004850 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4851 i40e_stat_str(&pf->hw, ret),
4852 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004853 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004854 }
4855
4856 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4857 dev_info(&pf->pdev->dev,
4858 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4859 bw_config.tc_valid_bits,
4860 bw_ets_config.tc_valid_bits);
4861 /* Still continuing */
4862 }
4863
4864 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4865 vsi->bw_max_quanta = bw_config.max_bw;
4866 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4867 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4868 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4869 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4870 vsi->bw_ets_limit_credits[i] =
4871 le16_to_cpu(bw_ets_config.credits[i]);
4872 /* 3 bits out of 4 for each TC */
4873 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4874 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004875
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004876 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004877}
4878
4879/**
4880 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4881 * @vsi: the VSI being configured
4882 * @enabled_tc: TC bitmap
4883 * @bw_credits: BW shared credits per TC
4884 *
4885 * Returns 0 on success, negative value on failure
4886 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004887static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004888 u8 *bw_share)
4889{
4890 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004891 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004892 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004893
4894 bw_data.tc_valid_bits = enabled_tc;
4895 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4896 bw_data.tc_bw_credits[i] = bw_share[i];
4897
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004898 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4899 NULL);
4900 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004901 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004902 "AQ command Config VSI BW allocation per TC failed = %d\n",
4903 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004904 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004905 }
4906
4907 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4908 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4909
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004910 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004911}
4912
4913/**
4914 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4915 * @vsi: the VSI being configured
4916 * @enabled_tc: TC map to be enabled
4917 *
4918 **/
4919static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4920{
4921 struct net_device *netdev = vsi->netdev;
4922 struct i40e_pf *pf = vsi->back;
4923 struct i40e_hw *hw = &pf->hw;
4924 u8 netdev_tc = 0;
4925 int i;
4926 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4927
4928 if (!netdev)
4929 return;
4930
4931 if (!enabled_tc) {
4932 netdev_reset_tc(netdev);
4933 return;
4934 }
4935
4936 /* Set up actual enabled TCs on the VSI */
4937 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4938 return;
4939
4940 /* set per TC queues for the VSI */
4941 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4942 /* Only set TC queues for enabled tcs
4943 *
4944 * e.g. For a VSI that has TC0 and TC3 enabled the
4945 * enabled_tc bitmap would be 0x00001001; the driver
4946 * will set the numtc for netdev as 2 that will be
4947 * referenced by the netdev layer as TC 0 and 1.
4948 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004949 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004950 netdev_set_tc_queue(netdev,
4951 vsi->tc_config.tc_info[i].netdev_tc,
4952 vsi->tc_config.tc_info[i].qcount,
4953 vsi->tc_config.tc_info[i].qoffset);
4954 }
4955
4956 /* Assign UP2TC map for the VSI */
4957 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4958 /* Get the actual TC# for the UP */
4959 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4960 /* Get the mapped netdev TC# for the UP */
4961 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4962 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4963 }
4964}
4965
4966/**
4967 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4968 * @vsi: the VSI being configured
4969 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4970 **/
4971static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4972 struct i40e_vsi_context *ctxt)
4973{
4974 /* copy just the sections touched not the entire info
4975 * since not all sections are valid as returned by
4976 * update vsi params
4977 */
4978 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4979 memcpy(&vsi->info.queue_mapping,
4980 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4981 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4982 sizeof(vsi->info.tc_mapping));
4983}
4984
4985/**
4986 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4987 * @vsi: VSI to be configured
4988 * @enabled_tc: TC bitmap
4989 *
4990 * This configures a particular VSI for TCs that are mapped to the
4991 * given TC bitmap. It uses default bandwidth share for TCs across
4992 * VSIs to configure TC for a particular VSI.
4993 *
4994 * NOTE:
4995 * It is expected that the VSI queues have been quisced before calling
4996 * this function.
4997 **/
4998static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4999{
5000 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5001 struct i40e_vsi_context ctxt;
5002 int ret = 0;
5003 int i;
5004
5005 /* Check if enabled_tc is same as existing or new TCs */
5006 if (vsi->tc_config.enabled_tc == enabled_tc)
5007 return ret;
5008
5009 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5010 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005011 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005012 bw_share[i] = 1;
5013 }
5014
5015 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5016 if (ret) {
5017 dev_info(&vsi->back->pdev->dev,
5018 "Failed configuring TC map %d for VSI %d\n",
5019 enabled_tc, vsi->seid);
5020 goto out;
5021 }
5022
5023 /* Update Queue Pairs Mapping for currently enabled UPs */
5024 ctxt.seid = vsi->seid;
5025 ctxt.pf_num = vsi->back->hw.pf_id;
5026 ctxt.vf_num = 0;
5027 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005028 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005029 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5030
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005031 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5032 ctxt.info.valid_sections |=
5033 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5034 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5035 }
5036
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005037 /* Update the VSI after updating the VSI queue-mapping information */
5038 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5039 if (ret) {
5040 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005041 "Update vsi tc config failed, err %s aq_err %s\n",
5042 i40e_stat_str(&vsi->back->hw, ret),
5043 i40e_aq_str(&vsi->back->hw,
5044 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005045 goto out;
5046 }
5047 /* update the local VSI info with updated queue map */
5048 i40e_vsi_update_queue_map(vsi, &ctxt);
5049 vsi->info.valid_sections = 0;
5050
5051 /* Update current VSI BW information */
5052 ret = i40e_vsi_get_bw_info(vsi);
5053 if (ret) {
5054 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005055 "Failed updating vsi bw info, err %s aq_err %s\n",
5056 i40e_stat_str(&vsi->back->hw, ret),
5057 i40e_aq_str(&vsi->back->hw,
5058 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005059 goto out;
5060 }
5061
5062 /* Update the netdev TC setup */
5063 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5064out:
5065 return ret;
5066}
5067
5068/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005069 * i40e_veb_config_tc - Configure TCs for given VEB
5070 * @veb: given VEB
5071 * @enabled_tc: TC bitmap
5072 *
5073 * Configures given TC bitmap for VEB (switching) element
5074 **/
5075int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5076{
5077 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5078 struct i40e_pf *pf = veb->pf;
5079 int ret = 0;
5080 int i;
5081
5082 /* No TCs or already enabled TCs just return */
5083 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5084 return ret;
5085
5086 bw_data.tc_valid_bits = enabled_tc;
5087 /* bw_data.absolute_credits is not set (relative) */
5088
5089 /* Enable ETS TCs with equal BW Share for now */
5090 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005091 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005092 bw_data.tc_bw_share_credits[i] = 1;
5093 }
5094
5095 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5096 &bw_data, NULL);
5097 if (ret) {
5098 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005099 "VEB bw config failed, err %s aq_err %s\n",
5100 i40e_stat_str(&pf->hw, ret),
5101 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005102 goto out;
5103 }
5104
5105 /* Update the BW information */
5106 ret = i40e_veb_get_bw_info(veb);
5107 if (ret) {
5108 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005109 "Failed getting veb bw config, err %s aq_err %s\n",
5110 i40e_stat_str(&pf->hw, ret),
5111 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005112 }
5113
5114out:
5115 return ret;
5116}
5117
5118#ifdef CONFIG_I40E_DCB
5119/**
5120 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5121 * @pf: PF struct
5122 *
5123 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5124 * the caller would've quiesce all the VSIs before calling
5125 * this function
5126 **/
5127static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5128{
5129 u8 tc_map = 0;
5130 int ret;
5131 u8 v;
5132
5133 /* Enable the TCs available on PF to all VEBs */
5134 tc_map = i40e_pf_get_tc_map(pf);
5135 for (v = 0; v < I40E_MAX_VEB; v++) {
5136 if (!pf->veb[v])
5137 continue;
5138 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5139 if (ret) {
5140 dev_info(&pf->pdev->dev,
5141 "Failed configuring TC for VEB seid=%d\n",
5142 pf->veb[v]->seid);
5143 /* Will try to configure as many components */
5144 }
5145 }
5146
5147 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005148 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005149 if (!pf->vsi[v])
5150 continue;
5151
5152 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005153#ifdef I40E_FCOE
5154 * - For FCoE VSI only enable the TC configured
5155 * as per the APP TLV
5156#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005157 * - For all others keep them at TC0 for now
5158 */
5159 if (v == pf->lan_vsi)
5160 tc_map = i40e_pf_get_tc_map(pf);
5161 else
David Ertmanea6acb72016-09-20 07:10:50 -07005162 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005163#ifdef I40E_FCOE
5164 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5165 tc_map = i40e_get_fcoe_tc_map(pf);
5166#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005167
5168 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5169 if (ret) {
5170 dev_info(&pf->pdev->dev,
5171 "Failed configuring TC for VSI seid=%d\n",
5172 pf->vsi[v]->seid);
5173 /* Will try to configure as many components */
5174 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005175 /* Re-configure VSI vectors based on updated TC map */
5176 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005177 if (pf->vsi[v]->netdev)
5178 i40e_dcbnl_set_all(pf->vsi[v]);
5179 }
5180 }
5181}
5182
5183/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005184 * i40e_resume_port_tx - Resume port Tx
5185 * @pf: PF struct
5186 *
5187 * Resume a port's Tx and issue a PF reset in case of failure to
5188 * resume.
5189 **/
5190static int i40e_resume_port_tx(struct i40e_pf *pf)
5191{
5192 struct i40e_hw *hw = &pf->hw;
5193 int ret;
5194
5195 ret = i40e_aq_resume_port_tx(hw, NULL);
5196 if (ret) {
5197 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005198 "Resume Port Tx failed, err %s aq_err %s\n",
5199 i40e_stat_str(&pf->hw, ret),
5200 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005201 /* Schedule PF reset to recover */
5202 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5203 i40e_service_event_schedule(pf);
5204 }
5205
5206 return ret;
5207}
5208
5209/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005210 * i40e_init_pf_dcb - Initialize DCB configuration
5211 * @pf: PF being configured
5212 *
5213 * Query the current DCB configuration and cache it
5214 * in the hardware structure
5215 **/
5216static int i40e_init_pf_dcb(struct i40e_pf *pf)
5217{
5218 struct i40e_hw *hw = &pf->hw;
5219 int err = 0;
5220
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005221 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005222 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005223 goto out;
5224
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005225 /* Get the initial DCB configuration */
5226 err = i40e_init_dcb(hw);
5227 if (!err) {
5228 /* Device/Function is not DCBX capable */
5229 if ((!hw->func_caps.dcb) ||
5230 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5231 dev_info(&pf->pdev->dev,
5232 "DCBX offload is not supported or is disabled for this PF.\n");
5233
5234 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5235 goto out;
5236
5237 } else {
5238 /* When status is not DISABLED then DCBX in FW */
5239 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5240 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005241
5242 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005243 /* Enable DCB tagging only when more than one TC
5244 * or explicitly disable if only one TC
5245 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005246 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5247 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005248 else
5249 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005250 dev_dbg(&pf->pdev->dev,
5251 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005252 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005253 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005254 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005255 "Query for DCB configuration failed, err %s aq_err %s\n",
5256 i40e_stat_str(&pf->hw, err),
5257 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005258 }
5259
5260out:
5261 return err;
5262}
5263#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005264#define SPEED_SIZE 14
5265#define FC_SIZE 8
5266/**
5267 * i40e_print_link_message - print link up or down
5268 * @vsi: the VSI for which link needs a message
5269 */
Matt Jaredc156f852015-08-27 11:42:39 -04005270void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005271{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005272 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005273 char *speed = "Unknown";
5274 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005275
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005276 new_speed = vsi->back->hw.phy.link_info.link_speed;
5277
5278 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005279 return;
5280 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005281 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005282 if (!isup) {
5283 netdev_info(vsi->netdev, "NIC Link is Down\n");
5284 return;
5285 }
5286
Greg Rose148c2d82014-12-11 07:06:27 +00005287 /* Warn user if link speed on NPAR enabled partition is not at
5288 * least 10GB
5289 */
5290 if (vsi->back->hw.func_caps.npar_enable &&
5291 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5292 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5293 netdev_warn(vsi->netdev,
5294 "The partition detected link speed that is less than 10Gbps\n");
5295
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005296 switch (vsi->back->hw.phy.link_info.link_speed) {
5297 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005298 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005299 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005300 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005301 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005302 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005303 case I40E_LINK_SPEED_25GB:
5304 speed = "25 G";
5305 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005306 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005307 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005308 break;
5309 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005310 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005311 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005312 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005313 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005314 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005315 default:
5316 break;
5317 }
5318
5319 switch (vsi->back->hw.fc.current_mode) {
5320 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005321 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005322 break;
5323 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005324 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005325 break;
5326 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005327 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005328 break;
5329 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005330 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005331 break;
5332 }
5333
Shannon Nelsona9165492015-09-03 17:19:00 -04005334 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005335 speed, fc);
5336}
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 */
5380 pf->fd_add_err = pf->fd_atr_cnt = 0;
5381 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005382 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005383 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5384 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005385 pf->fd_tcp_rule = 0;
5386 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005387 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005388 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005389
5390 /* On the next run of the service_task, notify any clients of the new
5391 * opened netdev
5392 */
5393 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005394 i40e_service_event_schedule(pf);
5395
5396 return 0;
5397}
5398
5399/**
5400 * i40e_vsi_reinit_locked - Reset the VSI
5401 * @vsi: the VSI being configured
5402 *
5403 * Rebuild the ring structs after some configuration
5404 * has changed, e.g. MTU size.
5405 **/
5406static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5407{
5408 struct i40e_pf *pf = vsi->back;
5409
5410 WARN_ON(in_interrupt());
5411 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5412 usleep_range(1000, 2000);
5413 i40e_down(vsi);
5414
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005415 i40e_up(vsi);
5416 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5417}
5418
5419/**
5420 * i40e_up - Bring the connection back up after being down
5421 * @vsi: the VSI being configured
5422 **/
5423int i40e_up(struct i40e_vsi *vsi)
5424{
5425 int err;
5426
5427 err = i40e_vsi_configure(vsi);
5428 if (!err)
5429 err = i40e_up_complete(vsi);
5430
5431 return err;
5432}
5433
5434/**
5435 * i40e_down - Shutdown the connection processing
5436 * @vsi: the VSI being stopped
5437 **/
5438void i40e_down(struct i40e_vsi *vsi)
5439{
5440 int i;
5441
5442 /* It is assumed that the caller of this function
5443 * sets the vsi->state __I40E_DOWN bit.
5444 */
5445 if (vsi->netdev) {
5446 netif_carrier_off(vsi->netdev);
5447 netif_tx_disable(vsi->netdev);
5448 }
5449 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005450 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005451 i40e_napi_disable_all(vsi);
5452
5453 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005454 i40e_clean_tx_ring(vsi->tx_rings[i]);
5455 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005457
5458 i40e_notify_client_of_netdev_close(vsi, false);
5459
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005460}
5461
5462/**
5463 * i40e_setup_tc - configure multiple traffic classes
5464 * @netdev: net device to configure
5465 * @tc: number of traffic classes to enable
5466 **/
5467static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5468{
5469 struct i40e_netdev_priv *np = netdev_priv(netdev);
5470 struct i40e_vsi *vsi = np->vsi;
5471 struct i40e_pf *pf = vsi->back;
5472 u8 enabled_tc = 0;
5473 int ret = -EINVAL;
5474 int i;
5475
5476 /* Check if DCB enabled to continue */
5477 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5478 netdev_info(netdev, "DCB is not enabled for adapter\n");
5479 goto exit;
5480 }
5481
5482 /* Check if MFP enabled */
5483 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5484 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5485 goto exit;
5486 }
5487
5488 /* Check whether tc count is within enabled limit */
5489 if (tc > i40e_pf_get_num_tc(pf)) {
5490 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5491 goto exit;
5492 }
5493
5494 /* Generate TC map for number of tc requested */
5495 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005496 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005497
5498 /* Requesting same TC configuration as already enabled */
5499 if (enabled_tc == vsi->tc_config.enabled_tc)
5500 return 0;
5501
5502 /* Quiesce VSI queues */
5503 i40e_quiesce_vsi(vsi);
5504
5505 /* Configure VSI for enabled TCs */
5506 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5507 if (ret) {
5508 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5509 vsi->seid);
5510 goto exit;
5511 }
5512
5513 /* Unquiesce VSI */
5514 i40e_unquiesce_vsi(vsi);
5515
5516exit:
5517 return ret;
5518}
5519
John Fastabende4c67342016-02-16 21:16:15 -08005520#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005521int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5522 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005523#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005524static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5525 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005526#endif
5527{
John Fastabend16e5cc62016-02-16 21:16:43 -08005528 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005529 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005530 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005531}
5532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005533/**
5534 * i40e_open - Called when a network interface is made active
5535 * @netdev: network interface device structure
5536 *
5537 * The open entry point is called when a network interface is made
5538 * active by the system (IFF_UP). At this point all resources needed
5539 * for transmit and receive operations are allocated, the interrupt
5540 * handler is registered with the OS, the netdev watchdog subtask is
5541 * enabled, and the stack is notified that the interface is ready.
5542 *
5543 * Returns 0 on success, negative value on failure
5544 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005545int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546{
5547 struct i40e_netdev_priv *np = netdev_priv(netdev);
5548 struct i40e_vsi *vsi = np->vsi;
5549 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005550 int err;
5551
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005552 /* disallow open during test or if eeprom is broken */
5553 if (test_bit(__I40E_TESTING, &pf->state) ||
5554 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005555 return -EBUSY;
5556
5557 netif_carrier_off(netdev);
5558
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005559 err = i40e_vsi_open(vsi);
5560 if (err)
5561 return err;
5562
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005563 /* configure global TSO hardware offload settings */
5564 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5565 TCP_FLAG_FIN) >> 16);
5566 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5567 TCP_FLAG_FIN |
5568 TCP_FLAG_CWR) >> 16);
5569 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5570
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005571 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005572
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005573 return 0;
5574}
5575
5576/**
5577 * i40e_vsi_open -
5578 * @vsi: the VSI to open
5579 *
5580 * Finish initialization of the VSI.
5581 *
5582 * Returns 0 on success, negative value on failure
5583 **/
5584int i40e_vsi_open(struct i40e_vsi *vsi)
5585{
5586 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005587 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005588 int err;
5589
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005590 /* allocate descriptors */
5591 err = i40e_vsi_setup_tx_resources(vsi);
5592 if (err)
5593 goto err_setup_tx;
5594 err = i40e_vsi_setup_rx_resources(vsi);
5595 if (err)
5596 goto err_setup_rx;
5597
5598 err = i40e_vsi_configure(vsi);
5599 if (err)
5600 goto err_setup_rx;
5601
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005602 if (vsi->netdev) {
5603 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5604 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5605 err = i40e_vsi_request_irq(vsi, int_name);
5606 if (err)
5607 goto err_setup_rx;
5608
5609 /* Notify the stack of the actual queue counts. */
5610 err = netif_set_real_num_tx_queues(vsi->netdev,
5611 vsi->num_queue_pairs);
5612 if (err)
5613 goto err_set_queues;
5614
5615 err = netif_set_real_num_rx_queues(vsi->netdev,
5616 vsi->num_queue_pairs);
5617 if (err)
5618 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005619
5620 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005621 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005622 dev_driver_string(&pf->pdev->dev),
5623 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005624 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005625
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005626 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005627 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005628 goto err_setup_rx;
5629 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005630
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005631 err = i40e_up_complete(vsi);
5632 if (err)
5633 goto err_up_complete;
5634
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005635 return 0;
5636
5637err_up_complete:
5638 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005639err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005640 i40e_vsi_free_irq(vsi);
5641err_setup_rx:
5642 i40e_vsi_free_rx_resources(vsi);
5643err_setup_tx:
5644 i40e_vsi_free_tx_resources(vsi);
5645 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005646 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005647
5648 return err;
5649}
5650
5651/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005652 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005653 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005654 *
5655 * This function destroys the hlist where all the Flow Director
5656 * filters were saved.
5657 **/
5658static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5659{
5660 struct i40e_fdir_filter *filter;
5661 struct hlist_node *node2;
5662
5663 hlist_for_each_entry_safe(filter, node2,
5664 &pf->fdir_filter_list, fdir_node) {
5665 hlist_del(&filter->fdir_node);
5666 kfree(filter);
5667 }
5668 pf->fdir_pf_active_filters = 0;
5669}
5670
5671/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005672 * i40e_close - Disables a network interface
5673 * @netdev: network interface device structure
5674 *
5675 * The close entry point is called when an interface is de-activated
5676 * by the OS. The hardware is still under the driver's control, but
5677 * this netdev interface is disabled.
5678 *
5679 * Returns 0, this is not allowed to fail
5680 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005681int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005682{
5683 struct i40e_netdev_priv *np = netdev_priv(netdev);
5684 struct i40e_vsi *vsi = np->vsi;
5685
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005686 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005687
5688 return 0;
5689}
5690
5691/**
5692 * i40e_do_reset - Start a PF or Core Reset sequence
5693 * @pf: board private structure
5694 * @reset_flags: which reset is requested
5695 *
5696 * The essential difference in resets is that the PF Reset
5697 * doesn't clear the packet buffers, doesn't reset the PE
5698 * firmware, and doesn't bother the other PFs on the chip.
5699 **/
5700void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5701{
5702 u32 val;
5703
5704 WARN_ON(in_interrupt());
5705
Mitch Williams263fc482014-04-23 04:50:11 +00005706
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005707 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005708 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005709
5710 /* Request a Global Reset
5711 *
5712 * This will start the chip's countdown to the actual full
5713 * chip reset event, and a warning interrupt to be sent
5714 * to all PFs, including the requestor. Our handler
5715 * for the warning interrupt will deal with the shutdown
5716 * and recovery of the switch setup.
5717 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005718 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005719 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5720 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5721 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5722
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005723 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005724
5725 /* Request a Core Reset
5726 *
5727 * Same as Global Reset, except does *not* include the MAC/PHY
5728 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005729 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005730 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5731 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5732 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5733 i40e_flush(&pf->hw);
5734
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005735 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005736
5737 /* Request a PF Reset
5738 *
5739 * Resets only the PF-specific registers
5740 *
5741 * This goes directly to the tear-down and rebuild of
5742 * the switch, since we need to do all the recovery as
5743 * for the Core Reset.
5744 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005745 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005746 i40e_handle_reset_warning(pf);
5747
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005748 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005749 int v;
5750
5751 /* Find the VSI(s) that requested a re-init */
5752 dev_info(&pf->pdev->dev,
5753 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005754 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005755 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005756
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005757 if (vsi != NULL &&
5758 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5759 i40e_vsi_reinit_locked(pf->vsi[v]);
5760 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5761 }
5762 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005763 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005764 int v;
5765
5766 /* Find the VSI(s) that needs to be brought down */
5767 dev_info(&pf->pdev->dev, "VSI down requested\n");
5768 for (v = 0; v < pf->num_alloc_vsi; v++) {
5769 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005770
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005771 if (vsi != NULL &&
5772 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5773 set_bit(__I40E_DOWN, &vsi->state);
5774 i40e_down(vsi);
5775 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5776 }
5777 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005778 } else {
5779 dev_info(&pf->pdev->dev,
5780 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005781 }
5782}
5783
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005784#ifdef CONFIG_I40E_DCB
5785/**
5786 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5787 * @pf: board private structure
5788 * @old_cfg: current DCB config
5789 * @new_cfg: new DCB config
5790 **/
5791bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5792 struct i40e_dcbx_config *old_cfg,
5793 struct i40e_dcbx_config *new_cfg)
5794{
5795 bool need_reconfig = false;
5796
5797 /* Check if ETS configuration has changed */
5798 if (memcmp(&new_cfg->etscfg,
5799 &old_cfg->etscfg,
5800 sizeof(new_cfg->etscfg))) {
5801 /* If Priority Table has changed reconfig is needed */
5802 if (memcmp(&new_cfg->etscfg.prioritytable,
5803 &old_cfg->etscfg.prioritytable,
5804 sizeof(new_cfg->etscfg.prioritytable))) {
5805 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005806 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005807 }
5808
5809 if (memcmp(&new_cfg->etscfg.tcbwtable,
5810 &old_cfg->etscfg.tcbwtable,
5811 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005812 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005813
5814 if (memcmp(&new_cfg->etscfg.tsatable,
5815 &old_cfg->etscfg.tsatable,
5816 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005817 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005818 }
5819
5820 /* Check if PFC configuration has changed */
5821 if (memcmp(&new_cfg->pfc,
5822 &old_cfg->pfc,
5823 sizeof(new_cfg->pfc))) {
5824 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005825 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005826 }
5827
5828 /* Check if APP Table has changed */
5829 if (memcmp(&new_cfg->app,
5830 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005831 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005832 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005833 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005834 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005835
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005836 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005837 return need_reconfig;
5838}
5839
5840/**
5841 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5842 * @pf: board private structure
5843 * @e: event info posted on ARQ
5844 **/
5845static int i40e_handle_lldp_event(struct i40e_pf *pf,
5846 struct i40e_arq_event_info *e)
5847{
5848 struct i40e_aqc_lldp_get_mib *mib =
5849 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5850 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005851 struct i40e_dcbx_config tmp_dcbx_cfg;
5852 bool need_reconfig = false;
5853 int ret = 0;
5854 u8 type;
5855
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005856 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005857 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005858 return ret;
5859
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005860 /* Ignore if event is not for Nearest Bridge */
5861 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5862 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005863 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005864 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5865 return ret;
5866
5867 /* Check MIB Type and return if event for Remote MIB update */
5868 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005869 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005870 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005871 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5872 /* Update the remote cached instance and return */
5873 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5874 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5875 &hw->remote_dcbx_config);
5876 goto exit;
5877 }
5878
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005879 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005880 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005881
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005882 /* Reset the old DCBx configuration data */
5883 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005884 /* Get updated DCBX data from firmware */
5885 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005886 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005887 dev_info(&pf->pdev->dev,
5888 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5889 i40e_stat_str(&pf->hw, ret),
5890 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005891 goto exit;
5892 }
5893
5894 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005895 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5896 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005897 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005898 goto exit;
5899 }
5900
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005901 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5902 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005903
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005904 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005905
5906 if (!need_reconfig)
5907 goto exit;
5908
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005909 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005910 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005911 pf->flags |= I40E_FLAG_DCB_ENABLED;
5912 else
5913 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5914
Neerav Parikh69129dc2014-11-12 00:18:46 +00005915 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005916 /* Reconfiguration needed quiesce all VSIs */
5917 i40e_pf_quiesce_all_vsi(pf);
5918
5919 /* Changes in configuration update VEB/VSI */
5920 i40e_dcb_reconfigure(pf);
5921
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005922 ret = i40e_resume_port_tx(pf);
5923
Neerav Parikh69129dc2014-11-12 00:18:46 +00005924 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005925 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005926 if (ret)
5927 goto exit;
5928
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005929 /* Wait for the PF's queues to be disabled */
5930 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005931 if (ret) {
5932 /* Schedule PF reset to recover */
5933 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5934 i40e_service_event_schedule(pf);
5935 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005936 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005937 /* Notify the client for the DCB changes */
5938 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005939 }
5940
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005941exit:
5942 return ret;
5943}
5944#endif /* CONFIG_I40E_DCB */
5945
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005946/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005947 * i40e_do_reset_safe - Protected reset path for userland calls.
5948 * @pf: board private structure
5949 * @reset_flags: which reset is requested
5950 *
5951 **/
5952void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5953{
5954 rtnl_lock();
5955 i40e_do_reset(pf, reset_flags);
5956 rtnl_unlock();
5957}
5958
5959/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005960 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5961 * @pf: board private structure
5962 * @e: event info posted on ARQ
5963 *
5964 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5965 * and VF queues
5966 **/
5967static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5968 struct i40e_arq_event_info *e)
5969{
5970 struct i40e_aqc_lan_overflow *data =
5971 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5972 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5973 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5974 struct i40e_hw *hw = &pf->hw;
5975 struct i40e_vf *vf;
5976 u16 vf_id;
5977
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005978 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5979 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005980
5981 /* Queue belongs to VF, find the VF and issue VF reset */
5982 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5983 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5984 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5985 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5986 vf_id -= hw->func_caps.vf_base_id;
5987 vf = &pf->vf[vf_id];
5988 i40e_vc_notify_vf_reset(vf);
5989 /* Allow VF to process pending reset notification */
5990 msleep(20);
5991 i40e_reset_vf(vf, false);
5992 }
5993}
5994
5995/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005996 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5997 * @pf: board private structure
5998 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005999u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006000{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006001 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006002
6003 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6004 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6005 return fcnt_prog;
6006}
6007
6008/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006009 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006010 * @pf: board private structure
6011 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006012u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006013{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006014 u32 val, fcnt_prog;
6015
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006016 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6017 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6018 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6019 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6020 return fcnt_prog;
6021}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006022
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006023/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006024 * i40e_get_global_fd_count - Get total FD filters programmed on device
6025 * @pf: board private structure
6026 **/
6027u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6028{
6029 u32 val, fcnt_prog;
6030
6031 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6032 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6033 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6034 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6035 return fcnt_prog;
6036}
6037
6038/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006039 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6040 * @pf: board private structure
6041 **/
6042void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6043{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006044 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006045 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006046 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006047
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006048 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6049 return;
6050
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006051 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6052 * to re-enable
6053 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006054 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006055 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006056 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6057 (pf->fd_add_err == 0) ||
6058 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006059 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6060 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6061 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006062 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6063 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 +00006064 }
6065 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006066
6067 /* Wait for some more space to be available to turn on ATR. We also
6068 * must check that no existing ntuple rules for TCP are in effect
6069 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006070 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6071 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07006072 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6073 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006074 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006075 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006076 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 +00006077 }
6078 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006079
6080 /* if hw had a problem adding a filter, delete it */
6081 if (pf->fd_inv > 0) {
6082 hlist_for_each_entry_safe(filter, node,
6083 &pf->fdir_filter_list, fdir_node) {
6084 if (filter->fd_id == pf->fd_inv) {
6085 hlist_del(&filter->fdir_node);
6086 kfree(filter);
6087 pf->fdir_pf_active_filters--;
6088 }
6089 }
6090 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006091}
6092
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006093#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006094#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006095/**
6096 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6097 * @pf: board private structure
6098 **/
6099static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6100{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006101 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006102 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006103 bool disable_atr = false;
6104 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006105 int reg;
6106
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006107 if (!time_after(jiffies, pf->fd_flush_timestamp +
6108 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6109 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006110
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006111 /* If the flush is happening too quick and we have mostly SB rules we
6112 * should not re-enable ATR for some time.
6113 */
6114 min_flush_time = pf->fd_flush_timestamp +
6115 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6116 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006117
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006118 if (!(time_after(jiffies, min_flush_time)) &&
6119 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6120 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6121 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6122 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006123 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006124
6125 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006126 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006127 /* flush all filters */
6128 wr32(&pf->hw, I40E_PFQF_CTL_1,
6129 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6130 i40e_flush(&pf->hw);
6131 pf->fd_flush_cnt++;
6132 pf->fd_add_err = 0;
6133 do {
6134 /* Check FD flush status every 5-6msec */
6135 usleep_range(5000, 6000);
6136 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6137 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6138 break;
6139 } while (flush_wait_retry--);
6140 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6141 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6142 } else {
6143 /* replay sideband filters */
6144 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6145 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006146 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006147 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6148 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6149 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6150 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006151}
6152
6153/**
6154 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6155 * @pf: board private structure
6156 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006157u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006158{
6159 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6160}
6161
6162/* We can see up to 256 filter programming desc in transit if the filters are
6163 * being applied really fast; before we see the first
6164 * filter miss error on Rx queue 0. Accumulating enough error messages before
6165 * reacting will make sure we don't cause flush too often.
6166 */
6167#define I40E_MAX_FD_PROGRAM_ERROR 256
6168
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006169/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006170 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6171 * @pf: board private structure
6172 **/
6173static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6174{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006175
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006176 /* if interface is down do nothing */
6177 if (test_bit(__I40E_DOWN, &pf->state))
6178 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006179
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006180 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006181 i40e_fdir_flush_and_replay(pf);
6182
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006183 i40e_fdir_check_and_reenable(pf);
6184
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006185}
6186
6187/**
6188 * i40e_vsi_link_event - notify VSI of a link event
6189 * @vsi: vsi to be notified
6190 * @link_up: link up or down
6191 **/
6192static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6193{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006194 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006195 return;
6196
6197 switch (vsi->type) {
6198 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006199#ifdef I40E_FCOE
6200 case I40E_VSI_FCOE:
6201#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006202 if (!vsi->netdev || !vsi->netdev_registered)
6203 break;
6204
6205 if (link_up) {
6206 netif_carrier_on(vsi->netdev);
6207 netif_tx_wake_all_queues(vsi->netdev);
6208 } else {
6209 netif_carrier_off(vsi->netdev);
6210 netif_tx_stop_all_queues(vsi->netdev);
6211 }
6212 break;
6213
6214 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006215 case I40E_VSI_VMDQ2:
6216 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006217 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006218 case I40E_VSI_MIRROR:
6219 default:
6220 /* there is no notification for other VSIs */
6221 break;
6222 }
6223}
6224
6225/**
6226 * i40e_veb_link_event - notify elements on the veb of a link event
6227 * @veb: veb to be notified
6228 * @link_up: link up or down
6229 **/
6230static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6231{
6232 struct i40e_pf *pf;
6233 int i;
6234
6235 if (!veb || !veb->pf)
6236 return;
6237 pf = veb->pf;
6238
6239 /* depth first... */
6240 for (i = 0; i < I40E_MAX_VEB; i++)
6241 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6242 i40e_veb_link_event(pf->veb[i], link_up);
6243
6244 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006245 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006246 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6247 i40e_vsi_link_event(pf->vsi[i], link_up);
6248}
6249
6250/**
6251 * i40e_link_event - Update netif_carrier status
6252 * @pf: board private structure
6253 **/
6254static void i40e_link_event(struct i40e_pf *pf)
6255{
Mitch Williams320684c2014-10-17 03:14:43 +00006256 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006257 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006258 i40e_status status;
6259 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006260
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006261 /* save off old link status information */
6262 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6263
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006264 /* set this to force the get_link_status call to refresh state */
6265 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006266
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006267 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006268
6269 status = i40e_get_link_status(&pf->hw, &new_link);
6270 if (status) {
6271 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6272 status);
6273 return;
6274 }
6275
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006276 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6277 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006278
6279 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006280 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006281 (test_bit(__I40E_DOWN, &vsi->state) ||
6282 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006283 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006284
6285 if (!test_bit(__I40E_DOWN, &vsi->state))
6286 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287
6288 /* Notify the base of the switch tree connected to
6289 * the link. Floating VEBs are not notified.
6290 */
6291 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6292 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6293 else
Mitch Williams320684c2014-10-17 03:14:43 +00006294 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295
6296 if (pf->vf)
6297 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006298
6299 if (pf->flags & I40E_FLAG_PTP)
6300 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006301}
6302
6303/**
Shannon Nelson21536712014-10-25 10:35:25 +00006304 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006305 * @pf: board private structure
6306 **/
6307static void i40e_watchdog_subtask(struct i40e_pf *pf)
6308{
6309 int i;
6310
6311 /* if interface is down do nothing */
6312 if (test_bit(__I40E_DOWN, &pf->state) ||
6313 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6314 return;
6315
Shannon Nelson21536712014-10-25 10:35:25 +00006316 /* make sure we don't do these things too often */
6317 if (time_before(jiffies, (pf->service_timer_previous +
6318 pf->service_timer_period)))
6319 return;
6320 pf->service_timer_previous = jiffies;
6321
Shannon Nelson9ac77262015-08-27 11:42:40 -04006322 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6323 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006325 /* Update the stats for active netdevs so the network stack
6326 * can look at updated numbers whenever it cares to
6327 */
Mitch Williams505682c2014-05-20 08:01:37 +00006328 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006329 if (pf->vsi[i] && pf->vsi[i]->netdev)
6330 i40e_update_stats(pf->vsi[i]);
6331
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006332 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6333 /* Update the stats for the active switching components */
6334 for (i = 0; i < I40E_MAX_VEB; i++)
6335 if (pf->veb[i])
6336 i40e_update_veb_stats(pf->veb[i]);
6337 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006338
6339 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006340}
6341
6342/**
6343 * i40e_reset_subtask - Set up for resetting the device and driver
6344 * @pf: board private structure
6345 **/
6346static void i40e_reset_subtask(struct i40e_pf *pf)
6347{
6348 u32 reset_flags = 0;
6349
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006350 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006351 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006352 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006353 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6354 }
6355 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006356 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6358 }
6359 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006360 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6362 }
6363 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006364 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6366 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006367 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006368 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006369 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6370 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006371
6372 /* If there's a recovery already waiting, it takes
6373 * precedence before starting a new reset sequence.
6374 */
6375 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6376 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006377 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006378 }
6379
6380 /* If we're already down or resetting, just bail */
6381 if (reset_flags &&
6382 !test_bit(__I40E_DOWN, &pf->state) &&
6383 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6384 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006385
6386unlock:
6387 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006388}
6389
6390/**
6391 * i40e_handle_link_event - Handle link event
6392 * @pf: board private structure
6393 * @e: event info posted on ARQ
6394 **/
6395static void i40e_handle_link_event(struct i40e_pf *pf,
6396 struct i40e_arq_event_info *e)
6397{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006398 struct i40e_aqc_get_link_status *status =
6399 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006400
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006401 /* Do a new status request to re-enable LSE reporting
6402 * and load new status information into the hw struct
6403 * This completely ignores any state information
6404 * in the ARQ event info, instead choosing to always
6405 * issue the AQ update link status command.
6406 */
6407 i40e_link_event(pf);
6408
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006409 /* check for unqualified module, if link is down */
6410 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6411 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6412 (!(status->link_info & I40E_AQ_LINK_UP)))
6413 dev_err(&pf->pdev->dev,
6414 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006415}
6416
6417/**
6418 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6419 * @pf: board private structure
6420 **/
6421static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6422{
6423 struct i40e_arq_event_info event;
6424 struct i40e_hw *hw = &pf->hw;
6425 u16 pending, i = 0;
6426 i40e_status ret;
6427 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006428 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006429 u32 val;
6430
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006431 /* Do not run clean AQ when PF reset fails */
6432 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6433 return;
6434
Shannon Nelson86df2422014-05-20 08:01:35 +00006435 /* check for error indications */
6436 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6437 oldval = val;
6438 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006439 if (hw->debug_mask & I40E_DEBUG_AQ)
6440 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006441 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6442 }
6443 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006444 if (hw->debug_mask & I40E_DEBUG_AQ)
6445 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006446 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006447 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006448 }
6449 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006450 if (hw->debug_mask & I40E_DEBUG_AQ)
6451 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006452 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6453 }
6454 if (oldval != val)
6455 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6456
6457 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6458 oldval = val;
6459 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006460 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6461 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006462 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6463 }
6464 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006465 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6466 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006467 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6468 }
6469 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006470 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6471 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006472 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6473 }
6474 if (oldval != val)
6475 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6476
Mitch Williams1001dc32014-11-11 20:02:19 +00006477 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6478 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006479 if (!event.msg_buf)
6480 return;
6481
6482 do {
6483 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006484 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006485 break;
Mitch Williams56497972014-06-04 08:45:18 +00006486 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006487 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6488 break;
6489 }
6490
6491 opcode = le16_to_cpu(event.desc.opcode);
6492 switch (opcode) {
6493
6494 case i40e_aqc_opc_get_link_status:
6495 i40e_handle_link_event(pf, &event);
6496 break;
6497 case i40e_aqc_opc_send_msg_to_pf:
6498 ret = i40e_vc_process_vf_msg(pf,
6499 le16_to_cpu(event.desc.retval),
6500 le32_to_cpu(event.desc.cookie_high),
6501 le32_to_cpu(event.desc.cookie_low),
6502 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006503 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006504 break;
6505 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006506 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006507#ifdef CONFIG_I40E_DCB
6508 rtnl_lock();
6509 ret = i40e_handle_lldp_event(pf, &event);
6510 rtnl_unlock();
6511#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006512 break;
6513 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006514 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006515 i40e_handle_lan_overflow_event(pf, &event);
6516 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006517 case i40e_aqc_opc_send_msg_to_peer:
6518 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6519 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006520 case i40e_aqc_opc_nvm_erase:
6521 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006522 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006523 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6524 "ARQ NVM operation 0x%04x completed\n",
6525 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006526 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006527 default:
6528 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006529 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006530 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006531 break;
6532 }
6533 } while (pending && (i++ < pf->adminq_work_limit));
6534
6535 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6536 /* re-enable Admin queue interrupt cause */
6537 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6538 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6539 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6540 i40e_flush(hw);
6541
6542 kfree(event.msg_buf);
6543}
6544
6545/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006546 * i40e_verify_eeprom - make sure eeprom is good to use
6547 * @pf: board private structure
6548 **/
6549static void i40e_verify_eeprom(struct i40e_pf *pf)
6550{
6551 int err;
6552
6553 err = i40e_diag_eeprom_test(&pf->hw);
6554 if (err) {
6555 /* retry in case of garbage read */
6556 err = i40e_diag_eeprom_test(&pf->hw);
6557 if (err) {
6558 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6559 err);
6560 set_bit(__I40E_BAD_EEPROM, &pf->state);
6561 }
6562 }
6563
6564 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6565 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6566 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6567 }
6568}
6569
6570/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006571 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006572 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006573 *
6574 * enable switch loop back or die - no point in a return value
6575 **/
6576static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6577{
6578 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6579 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006580 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006581
6582 ctxt.seid = pf->main_vsi_seid;
6583 ctxt.pf_num = pf->hw.pf_id;
6584 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006585 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6586 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006587 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006588 "couldn't get PF vsi config, err %s aq_err %s\n",
6589 i40e_stat_str(&pf->hw, ret),
6590 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006591 return;
6592 }
6593 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6594 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6595 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6596
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006597 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6598 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006599 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006600 "update vsi switch failed, err %s aq_err %s\n",
6601 i40e_stat_str(&pf->hw, ret),
6602 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006603 }
6604}
6605
6606/**
6607 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006608 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006609 *
6610 * disable switch loop back or die - no point in a return value
6611 **/
6612static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6613{
6614 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6615 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006616 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006617
6618 ctxt.seid = pf->main_vsi_seid;
6619 ctxt.pf_num = pf->hw.pf_id;
6620 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006621 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6622 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006623 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006624 "couldn't get PF vsi config, err %s aq_err %s\n",
6625 i40e_stat_str(&pf->hw, ret),
6626 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006627 return;
6628 }
6629 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6630 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6631 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6632
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006633 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6634 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006635 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006636 "update vsi switch failed, err %s aq_err %s\n",
6637 i40e_stat_str(&pf->hw, ret),
6638 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006639 }
6640}
6641
6642/**
Neerav Parikh51616012015-02-06 08:52:14 +00006643 * i40e_config_bridge_mode - Configure the HW bridge mode
6644 * @veb: pointer to the bridge instance
6645 *
6646 * Configure the loop back mode for the LAN VSI that is downlink to the
6647 * specified HW bridge instance. It is expected this function is called
6648 * when a new HW bridge is instantiated.
6649 **/
6650static void i40e_config_bridge_mode(struct i40e_veb *veb)
6651{
6652 struct i40e_pf *pf = veb->pf;
6653
Shannon Nelson6dec1012015-09-28 14:12:30 -04006654 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6655 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6656 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006657 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6658 i40e_disable_pf_switch_lb(pf);
6659 else
6660 i40e_enable_pf_switch_lb(pf);
6661}
6662
6663/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006664 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6665 * @veb: pointer to the VEB instance
6666 *
6667 * This is a recursive function that first builds the attached VSIs then
6668 * recurses in to build the next layer of VEB. We track the connections
6669 * through our own index numbers because the seid's from the HW could
6670 * change across the reset.
6671 **/
6672static int i40e_reconstitute_veb(struct i40e_veb *veb)
6673{
6674 struct i40e_vsi *ctl_vsi = NULL;
6675 struct i40e_pf *pf = veb->pf;
6676 int v, veb_idx;
6677 int ret;
6678
6679 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006680 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006681 if (pf->vsi[v] &&
6682 pf->vsi[v]->veb_idx == veb->idx &&
6683 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6684 ctl_vsi = pf->vsi[v];
6685 break;
6686 }
6687 }
6688 if (!ctl_vsi) {
6689 dev_info(&pf->pdev->dev,
6690 "missing owner VSI for veb_idx %d\n", veb->idx);
6691 ret = -ENOENT;
6692 goto end_reconstitute;
6693 }
6694 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6695 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6696 ret = i40e_add_vsi(ctl_vsi);
6697 if (ret) {
6698 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006699 "rebuild of veb_idx %d owner VSI failed: %d\n",
6700 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 goto end_reconstitute;
6702 }
6703 i40e_vsi_reset_stats(ctl_vsi);
6704
6705 /* create the VEB in the switch and move the VSI onto the VEB */
6706 ret = i40e_add_veb(veb, ctl_vsi);
6707 if (ret)
6708 goto end_reconstitute;
6709
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006710 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6711 veb->bridge_mode = BRIDGE_MODE_VEB;
6712 else
6713 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006714 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006715
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006716 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006717 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006718 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6719 continue;
6720
6721 if (pf->vsi[v]->veb_idx == veb->idx) {
6722 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006724 vsi->uplink_seid = veb->seid;
6725 ret = i40e_add_vsi(vsi);
6726 if (ret) {
6727 dev_info(&pf->pdev->dev,
6728 "rebuild of vsi_idx %d failed: %d\n",
6729 v, ret);
6730 goto end_reconstitute;
6731 }
6732 i40e_vsi_reset_stats(vsi);
6733 }
6734 }
6735
6736 /* create any VEBs attached to this VEB - RECURSION */
6737 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6738 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6739 pf->veb[veb_idx]->uplink_seid = veb->seid;
6740 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6741 if (ret)
6742 break;
6743 }
6744 }
6745
6746end_reconstitute:
6747 return ret;
6748}
6749
6750/**
6751 * i40e_get_capabilities - get info about the HW
6752 * @pf: the PF struct
6753 **/
6754static int i40e_get_capabilities(struct i40e_pf *pf)
6755{
6756 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6757 u16 data_size;
6758 int buf_len;
6759 int err;
6760
6761 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6762 do {
6763 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6764 if (!cap_buf)
6765 return -ENOMEM;
6766
6767 /* this loads the data into the hw struct for us */
6768 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6769 &data_size,
6770 i40e_aqc_opc_list_func_capabilities,
6771 NULL);
6772 /* data loaded, buffer no longer needed */
6773 kfree(cap_buf);
6774
6775 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6776 /* retry with a larger buffer */
6777 buf_len = data_size;
6778 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6779 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006780 "capability discovery failed, err %s aq_err %s\n",
6781 i40e_stat_str(&pf->hw, err),
6782 i40e_aq_str(&pf->hw,
6783 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006784 return -ENODEV;
6785 }
6786 } while (err);
6787
6788 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6789 dev_info(&pf->pdev->dev,
6790 "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",
6791 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6792 pf->hw.func_caps.num_msix_vectors,
6793 pf->hw.func_caps.num_msix_vectors_vf,
6794 pf->hw.func_caps.fd_filters_guaranteed,
6795 pf->hw.func_caps.fd_filters_best_effort,
6796 pf->hw.func_caps.num_tx_qp,
6797 pf->hw.func_caps.num_vsis);
6798
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006799#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6800 + pf->hw.func_caps.num_vfs)
6801 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6802 dev_info(&pf->pdev->dev,
6803 "got num_vsis %d, setting num_vsis to %d\n",
6804 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6805 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6806 }
6807
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006808 return 0;
6809}
6810
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006811static int i40e_vsi_clear(struct i40e_vsi *vsi);
6812
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006813/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006814 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815 * @pf: board private structure
6816 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006817static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006818{
6819 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006821 /* quick workaround for an NVM issue that leaves a critical register
6822 * uninitialized
6823 */
6824 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6825 static const u32 hkey[] = {
6826 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6827 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6828 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6829 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006830 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006831
6832 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6833 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6834 }
6835
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006836 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006837 return;
6838
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006839 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006840 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006841
6842 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006843 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006844 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6845 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 if (!vsi) {
6847 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006848 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6849 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006850 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006851 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006852
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006853 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854}
6855
6856/**
6857 * i40e_fdir_teardown - release the Flow Director resources
6858 * @pf: board private structure
6859 **/
6860static void i40e_fdir_teardown(struct i40e_pf *pf)
6861{
Alexander Duyck4b816442016-10-11 15:26:53 -07006862 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006863
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006864 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006865 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6866 if (vsi)
6867 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006868}
6869
6870/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006871 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872 * @pf: board private structure
6873 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006874 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006875 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006876static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006877{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006879 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006880 u32 v;
6881
6882 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6883 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006884 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006885 if (i40e_check_asq_alive(&pf->hw))
6886 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006887
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006888 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006889
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006890 /* quiesce the VSIs and their queues that are not already DOWN */
6891 i40e_pf_quiesce_all_vsi(pf);
6892
Mitch Williams505682c2014-05-20 08:01:37 +00006893 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006894 if (pf->vsi[v])
6895 pf->vsi[v]->seid = 0;
6896 }
6897
6898 i40e_shutdown_adminq(&pf->hw);
6899
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006900 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006901 if (hw->hmc.hmc_obj) {
6902 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006903 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006904 dev_warn(&pf->pdev->dev,
6905 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006906 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006907}
6908
6909/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006910 * i40e_send_version - update firmware with driver version
6911 * @pf: PF struct
6912 */
6913static void i40e_send_version(struct i40e_pf *pf)
6914{
6915 struct i40e_driver_version dv;
6916
6917 dv.major_version = DRV_VERSION_MAJOR;
6918 dv.minor_version = DRV_VERSION_MINOR;
6919 dv.build_version = DRV_VERSION_BUILD;
6920 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006921 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006922 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6923}
6924
6925/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006926 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006927 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006928 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006929 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006930static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006931{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006932 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006933 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006934 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006935 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006936 u32 v;
6937
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938 /* Now we wait for GRST to settle out.
6939 * We don't have to delete the VEBs or VSIs from the hw switch
6940 * because the reset will make them disappear.
6941 */
6942 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006943 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006944 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006945 set_bit(__I40E_RESET_FAILED, &pf->state);
6946 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006947 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948 pf->pfr_count++;
6949
6950 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006951 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006952 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006953
6954 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6955 ret = i40e_init_adminq(&pf->hw);
6956 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006957 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6958 i40e_stat_str(&pf->hw, ret),
6959 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006960 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006961 }
6962
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006963 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006964 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006965 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006966
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006967 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006968 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006969 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006972 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6973 hw->func_caps.num_rx_qp,
6974 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6975 if (ret) {
6976 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6977 goto end_core_reset;
6978 }
6979 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6980 if (ret) {
6981 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6982 goto end_core_reset;
6983 }
6984
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006985#ifdef CONFIG_I40E_DCB
6986 ret = i40e_init_pf_dcb(pf);
6987 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006988 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6989 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6990 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006991 }
6992#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006993#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006994 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006995
Vasu Dev38e00432014-08-01 13:27:03 -07006996#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006998 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999 if (ret)
7000 goto end_core_reset;
7001
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007002 /* The driver only wants link up/down and module qualification
7003 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007004 */
7005 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007006 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007007 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007008 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007009 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007010 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7011 i40e_stat_str(&pf->hw, ret),
7012 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007013
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007014 /* make sure our flow control settings are restored */
7015 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7016 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007017 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7018 i40e_stat_str(&pf->hw, ret),
7019 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007020
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007021 /* Rebuild the VSIs and VEBs that existed before reset.
7022 * They are still in our local switch element arrays, so only
7023 * need to rebuild the switch model in the HW.
7024 *
7025 * If there were VEBs but the reconstitution failed, we'll try
7026 * try to recover minimal use by getting the basic PF VSI working.
7027 */
7028 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007029 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007030 /* find the one VEB connected to the MAC, and find orphans */
7031 for (v = 0; v < I40E_MAX_VEB; v++) {
7032 if (!pf->veb[v])
7033 continue;
7034
7035 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7036 pf->veb[v]->uplink_seid == 0) {
7037 ret = i40e_reconstitute_veb(pf->veb[v]);
7038
7039 if (!ret)
7040 continue;
7041
7042 /* If Main VEB failed, we're in deep doodoo,
7043 * so give up rebuilding the switch and set up
7044 * for minimal rebuild of PF VSI.
7045 * If orphan failed, we'll report the error
7046 * but try to keep going.
7047 */
7048 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7049 dev_info(&pf->pdev->dev,
7050 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7051 ret);
7052 pf->vsi[pf->lan_vsi]->uplink_seid
7053 = pf->mac_seid;
7054 break;
7055 } else if (pf->veb[v]->uplink_seid == 0) {
7056 dev_info(&pf->pdev->dev,
7057 "rebuild of orphan VEB failed: %d\n",
7058 ret);
7059 }
7060 }
7061 }
7062 }
7063
7064 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007065 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007066 /* no VEB, so rebuild only the Main VSI */
7067 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7068 if (ret) {
7069 dev_info(&pf->pdev->dev,
7070 "rebuild of Main VSI failed: %d\n", ret);
7071 goto end_core_reset;
7072 }
7073 }
7074
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007075 /* Reconfigure hardware for allowing smaller MSS in the case
7076 * of TSO, so that we avoid the MDD being fired and causing
7077 * a reset in the case of small MSS+TSO.
7078 */
7079#define I40E_REG_MSS 0x000E64DC
7080#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7081#define I40E_64BYTE_MSS 0x400000
7082 val = rd32(hw, I40E_REG_MSS);
7083 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7084 val &= ~I40E_REG_MSS_MIN_MASK;
7085 val |= I40E_64BYTE_MSS;
7086 wr32(hw, I40E_REG_MSS, val);
7087 }
7088
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007089 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007090 msleep(75);
7091 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7092 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007093 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7094 i40e_stat_str(&pf->hw, ret),
7095 i40e_aq_str(&pf->hw,
7096 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007097 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007098 /* reinit the misc interrupt */
7099 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7100 ret = i40e_setup_misc_vector(pf);
7101
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007102 /* Add a filter to drop all Flow control frames from any VSI from being
7103 * transmitted. By doing so we stop a malicious VF from sending out
7104 * PAUSE or PFC frames and potentially controlling traffic for other
7105 * PF/VF VSIs.
7106 * The FW can still send Flow control frames if enabled.
7107 */
7108 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7109 pf->main_vsi_seid);
7110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007111 /* restart the VSIs that were rebuilt and running before the reset */
7112 i40e_pf_unquiesce_all_vsi(pf);
7113
Mitch Williams69f64b22014-02-13 03:48:46 -08007114 if (pf->num_alloc_vfs) {
7115 for (v = 0; v < pf->num_alloc_vfs; v++)
7116 i40e_reset_vf(&pf->vf[v], true);
7117 }
7118
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007120 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007121
7122end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007123 clear_bit(__I40E_RESET_FAILED, &pf->state);
7124clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007125 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7126}
7127
7128/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007129 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007130 * @pf: board private structure
7131 *
7132 * Close up the VFs and other things in prep for a Core Reset,
7133 * then get ready to rebuild the world.
7134 **/
7135static void i40e_handle_reset_warning(struct i40e_pf *pf)
7136{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007137 i40e_prep_for_reset(pf);
7138 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007139}
7140
7141/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007142 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007143 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007144 *
7145 * Called from the MDD irq handler to identify possibly malicious vfs
7146 **/
7147static void i40e_handle_mdd_event(struct i40e_pf *pf)
7148{
7149 struct i40e_hw *hw = &pf->hw;
7150 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007151 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007152 struct i40e_vf *vf;
7153 u32 reg;
7154 int i;
7155
7156 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7157 return;
7158
7159 /* find what triggered the MDD event */
7160 reg = rd32(hw, I40E_GL_MDET_TX);
7161 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007162 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7163 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007164 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007165 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007166 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007167 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007168 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7169 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7170 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007171 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007172 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 +00007173 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7175 mdd_detected = true;
7176 }
7177 reg = rd32(hw, I40E_GL_MDET_RX);
7178 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007179 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7180 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007181 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007182 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007183 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7184 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7185 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007186 if (netif_msg_rx_err(pf))
7187 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7188 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007189 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7190 mdd_detected = true;
7191 }
7192
Neerav Parikhdf430b12014-06-04 01:23:15 +00007193 if (mdd_detected) {
7194 reg = rd32(hw, I40E_PF_MDET_TX);
7195 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7196 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007197 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007198 pf_mdd_detected = true;
7199 }
7200 reg = rd32(hw, I40E_PF_MDET_RX);
7201 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7202 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007203 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007204 pf_mdd_detected = true;
7205 }
7206 /* Queue belongs to the PF, initiate a reset */
7207 if (pf_mdd_detected) {
7208 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7209 i40e_service_event_schedule(pf);
7210 }
7211 }
7212
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007213 /* see if one of the VFs needs its hand slapped */
7214 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7215 vf = &(pf->vf[i]);
7216 reg = rd32(hw, I40E_VP_MDET_TX(i));
7217 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7218 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7219 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007220 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7221 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007222 }
7223
7224 reg = rd32(hw, I40E_VP_MDET_RX(i));
7225 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7226 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7227 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007228 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7229 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007230 }
7231
7232 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7233 dev_info(&pf->pdev->dev,
7234 "Too many MDD events on VF %d, disabled\n", i);
7235 dev_info(&pf->pdev->dev,
7236 "Use PF Control I/F to re-enable the VF\n");
7237 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7238 }
7239 }
7240
7241 /* re-enable mdd interrupt cause */
7242 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7243 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7244 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7245 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7246 i40e_flush(hw);
7247}
7248
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007249/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007250 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007251 * @pf: board private structure
7252 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007253static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007254{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007255 struct i40e_hw *hw = &pf->hw;
7256 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007257 __be16 port;
7258 int i;
7259
Singhai, Anjali6a899022015-12-14 12:21:18 -08007260 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007261 return;
7262
Singhai, Anjali6a899022015-12-14 12:21:18 -08007263 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007264
7265 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007266 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7267 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7268 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007269 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007270 ret = i40e_aq_add_udp_tunnel(hw, port,
7271 pf->udp_ports[i].type,
7272 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007273 else
7274 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007275
7276 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007277 dev_dbg(&pf->pdev->dev,
7278 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7279 pf->udp_ports[i].type ? "vxlan" : "geneve",
7280 port ? "add" : "delete",
7281 ntohs(port), i,
7282 i40e_stat_str(&pf->hw, ret),
7283 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007284 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007285 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007286 }
7287 }
7288 }
7289}
7290
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007291/**
7292 * i40e_service_task - Run the driver's async subtasks
7293 * @work: pointer to work_struct containing our data
7294 **/
7295static void i40e_service_task(struct work_struct *work)
7296{
7297 struct i40e_pf *pf = container_of(work,
7298 struct i40e_pf,
7299 service_task);
7300 unsigned long start_time = jiffies;
7301
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007302 /* don't bother with service tasks if a reset is in progress */
7303 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007304 return;
7305 }
7306
Mitch Williams91089032016-11-21 13:03:51 -08007307 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7308 return;
7309
Kiran Patilb03a8c12015-09-24 18:13:15 -04007310 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007311 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007312 i40e_reset_subtask(pf);
7313 i40e_handle_mdd_event(pf);
7314 i40e_vc_process_vflr_event(pf);
7315 i40e_watchdog_subtask(pf);
7316 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007317 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007318 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007319 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007320 i40e_clean_adminq_subtask(pf);
7321
Mitch Williams91089032016-11-21 13:03:51 -08007322 /* flush memory to make sure state is correct before next watchdog */
7323 smp_mb__before_atomic();
7324 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007325
7326 /* If the tasks have taken longer than one timer cycle or there
7327 * is more work to be done, reschedule the service task now
7328 * rather than wait for the timer to tick again.
7329 */
7330 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7331 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7332 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7333 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7334 i40e_service_event_schedule(pf);
7335}
7336
7337/**
7338 * i40e_service_timer - timer callback
7339 * @data: pointer to PF struct
7340 **/
7341static void i40e_service_timer(unsigned long data)
7342{
7343 struct i40e_pf *pf = (struct i40e_pf *)data;
7344
7345 mod_timer(&pf->service_timer,
7346 round_jiffies(jiffies + pf->service_timer_period));
7347 i40e_service_event_schedule(pf);
7348}
7349
7350/**
7351 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7352 * @vsi: the VSI being configured
7353 **/
7354static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7355{
7356 struct i40e_pf *pf = vsi->back;
7357
7358 switch (vsi->type) {
7359 case I40E_VSI_MAIN:
7360 vsi->alloc_queue_pairs = pf->num_lan_qps;
7361 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7362 I40E_REQ_DESCRIPTOR_MULTIPLE);
7363 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7364 vsi->num_q_vectors = pf->num_lan_msix;
7365 else
7366 vsi->num_q_vectors = 1;
7367
7368 break;
7369
7370 case I40E_VSI_FDIR:
7371 vsi->alloc_queue_pairs = 1;
7372 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7373 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007374 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007375 break;
7376
7377 case I40E_VSI_VMDQ2:
7378 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7379 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7380 I40E_REQ_DESCRIPTOR_MULTIPLE);
7381 vsi->num_q_vectors = pf->num_vmdq_msix;
7382 break;
7383
7384 case I40E_VSI_SRIOV:
7385 vsi->alloc_queue_pairs = pf->num_vf_qps;
7386 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7387 I40E_REQ_DESCRIPTOR_MULTIPLE);
7388 break;
7389
Vasu Dev38e00432014-08-01 13:27:03 -07007390#ifdef I40E_FCOE
7391 case I40E_VSI_FCOE:
7392 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7393 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7394 I40E_REQ_DESCRIPTOR_MULTIPLE);
7395 vsi->num_q_vectors = pf->num_fcoe_msix;
7396 break;
7397
7398#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007399 default:
7400 WARN_ON(1);
7401 return -ENODATA;
7402 }
7403
7404 return 0;
7405}
7406
7407/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007408 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7409 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007410 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007411 *
7412 * On error: returns error code (negative)
7413 * On success: returns 0
7414 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007415static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007416{
7417 int size;
7418 int ret = 0;
7419
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007420 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007421 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7422 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7423 if (!vsi->tx_rings)
7424 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007425 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7426
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007427 if (alloc_qvectors) {
7428 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007429 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007430 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7431 if (!vsi->q_vectors) {
7432 ret = -ENOMEM;
7433 goto err_vectors;
7434 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007435 }
7436 return ret;
7437
7438err_vectors:
7439 kfree(vsi->tx_rings);
7440 return ret;
7441}
7442
7443/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007444 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7445 * @pf: board private structure
7446 * @type: type of VSI
7447 *
7448 * On error: returns error code (negative)
7449 * On success: returns vsi index in PF (positive)
7450 **/
7451static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7452{
7453 int ret = -ENODEV;
7454 struct i40e_vsi *vsi;
7455 int vsi_idx;
7456 int i;
7457
7458 /* Need to protect the allocation of the VSIs at the PF level */
7459 mutex_lock(&pf->switch_mutex);
7460
7461 /* VSI list may be fragmented if VSI creation/destruction has
7462 * been happening. We can afford to do a quick scan to look
7463 * for any free VSIs in the list.
7464 *
7465 * find next empty vsi slot, looping back around if necessary
7466 */
7467 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007468 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007469 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007470 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 i = 0;
7472 while (i < pf->next_vsi && pf->vsi[i])
7473 i++;
7474 }
7475
Mitch Williams505682c2014-05-20 08:01:37 +00007476 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007477 vsi_idx = i; /* Found one! */
7478 } else {
7479 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007480 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481 }
7482 pf->next_vsi = ++i;
7483
7484 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7485 if (!vsi) {
7486 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007487 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007488 }
7489 vsi->type = type;
7490 vsi->back = pf;
7491 set_bit(__I40E_DOWN, &vsi->state);
7492 vsi->flags = 0;
7493 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007494 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007495 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7496 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007497 vsi->netdev_registered = false;
7498 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007499 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007500 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501
Alexander Duyck9f65e152013-09-28 06:00:58 +00007502 ret = i40e_set_num_rings_in_vsi(vsi);
7503 if (ret)
7504 goto err_rings;
7505
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007506 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007507 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007508 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007510 /* Setup default MSIX irq handler for VSI */
7511 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7512
Kiran Patil21659032015-09-30 14:09:03 -04007513 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007514 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007515 pf->vsi[vsi_idx] = vsi;
7516 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007517 goto unlock_pf;
7518
Alexander Duyck9f65e152013-09-28 06:00:58 +00007519err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007520 pf->next_vsi = i - 1;
7521 kfree(vsi);
7522unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007523 mutex_unlock(&pf->switch_mutex);
7524 return ret;
7525}
7526
7527/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007528 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7529 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007530 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007531 *
7532 * On error: returns error code (negative)
7533 * On success: returns 0
7534 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007535static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007536{
7537 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007538 if (free_qvectors) {
7539 kfree(vsi->q_vectors);
7540 vsi->q_vectors = NULL;
7541 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007542 kfree(vsi->tx_rings);
7543 vsi->tx_rings = NULL;
7544 vsi->rx_rings = NULL;
7545}
7546
7547/**
Helin Zhang28c58692015-10-26 19:44:27 -04007548 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7549 * and lookup table
7550 * @vsi: Pointer to VSI structure
7551 */
7552static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7553{
7554 if (!vsi)
7555 return;
7556
7557 kfree(vsi->rss_hkey_user);
7558 vsi->rss_hkey_user = NULL;
7559
7560 kfree(vsi->rss_lut_user);
7561 vsi->rss_lut_user = NULL;
7562}
7563
7564/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007565 * i40e_vsi_clear - Deallocate the VSI provided
7566 * @vsi: the VSI being un-configured
7567 **/
7568static int i40e_vsi_clear(struct i40e_vsi *vsi)
7569{
7570 struct i40e_pf *pf;
7571
7572 if (!vsi)
7573 return 0;
7574
7575 if (!vsi->back)
7576 goto free_vsi;
7577 pf = vsi->back;
7578
7579 mutex_lock(&pf->switch_mutex);
7580 if (!pf->vsi[vsi->idx]) {
7581 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7582 vsi->idx, vsi->idx, vsi, vsi->type);
7583 goto unlock_vsi;
7584 }
7585
7586 if (pf->vsi[vsi->idx] != vsi) {
7587 dev_err(&pf->pdev->dev,
7588 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7589 pf->vsi[vsi->idx]->idx,
7590 pf->vsi[vsi->idx],
7591 pf->vsi[vsi->idx]->type,
7592 vsi->idx, vsi, vsi->type);
7593 goto unlock_vsi;
7594 }
7595
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007596 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007597 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7598 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7599
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007600 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007601 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007603 pf->vsi[vsi->idx] = NULL;
7604 if (vsi->idx < pf->next_vsi)
7605 pf->next_vsi = vsi->idx;
7606
7607unlock_vsi:
7608 mutex_unlock(&pf->switch_mutex);
7609free_vsi:
7610 kfree(vsi);
7611
7612 return 0;
7613}
7614
7615/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007616 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7617 * @vsi: the VSI being cleaned
7618 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007619static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007620{
7621 int i;
7622
Greg Rose8e9dca52013-12-18 13:45:53 +00007623 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007624 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007625 kfree_rcu(vsi->tx_rings[i], rcu);
7626 vsi->tx_rings[i] = NULL;
7627 vsi->rx_rings[i] = NULL;
7628 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007629 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007630}
7631
7632/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007633 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7634 * @vsi: the VSI being configured
7635 **/
7636static int i40e_alloc_rings(struct i40e_vsi *vsi)
7637{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007638 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007639 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007640 int i;
7641
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007642 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007643 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007644 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007645 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7646 if (!tx_ring)
7647 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007648
7649 tx_ring->queue_index = i;
7650 tx_ring->reg_idx = vsi->base_queue + i;
7651 tx_ring->ring_active = false;
7652 tx_ring->vsi = vsi;
7653 tx_ring->netdev = vsi->netdev;
7654 tx_ring->dev = &pf->pdev->dev;
7655 tx_ring->count = vsi->num_desc;
7656 tx_ring->size = 0;
7657 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007658 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7659 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007660 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007661 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007662
Alexander Duyck9f65e152013-09-28 06:00:58 +00007663 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007664 rx_ring->queue_index = i;
7665 rx_ring->reg_idx = vsi->base_queue + i;
7666 rx_ring->ring_active = false;
7667 rx_ring->vsi = vsi;
7668 rx_ring->netdev = vsi->netdev;
7669 rx_ring->dev = &pf->pdev->dev;
7670 rx_ring->count = vsi->num_desc;
7671 rx_ring->size = 0;
7672 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007673 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007674 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007675 }
7676
7677 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007678
7679err_out:
7680 i40e_vsi_clear_rings(vsi);
7681 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682}
7683
7684/**
7685 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7686 * @pf: board private structure
7687 * @vectors: the number of MSI-X vectors to request
7688 *
7689 * Returns the number of vectors reserved, or error
7690 **/
7691static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7692{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007693 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7694 I40E_MIN_MSIX, vectors);
7695 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007697 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 vectors = 0;
7699 }
7700
7701 return vectors;
7702}
7703
7704/**
7705 * i40e_init_msix - Setup the MSIX capability
7706 * @pf: board private structure
7707 *
7708 * Work with the OS to set up the MSIX vectors needed.
7709 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007710 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007711 **/
7712static int i40e_init_msix(struct i40e_pf *pf)
7713{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007715 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007716 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007717 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007718 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007719
7720 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7721 return -ENODEV;
7722
7723 /* The number of vectors we'll request will be comprised of:
7724 * - Add 1 for "other" cause for Admin Queue events, etc.
7725 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007726 * - Queues being used for RSS.
7727 * We don't need as many as max_rss_size vectors.
7728 * use rss_size instead in the calculation since that
7729 * is governed by number of cpus in the system.
7730 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007731 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007732 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007733#ifdef I40E_FCOE
7734 * - The number of FCOE qps.
7735#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007736 * Once we count this up, try the request.
7737 *
7738 * If we can't get what we want, we'll simplify to nearly nothing
7739 * and try again. If that still fails, we punt.
7740 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007741 vectors_left = hw->func_caps.num_msix_vectors;
7742 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743
Shannon Nelson1e200e42015-02-27 09:15:24 +00007744 /* reserve one vector for miscellaneous handler */
7745 if (vectors_left) {
7746 v_budget++;
7747 vectors_left--;
7748 }
7749
7750 /* reserve vectors for the main PF traffic queues */
7751 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7752 vectors_left -= pf->num_lan_msix;
7753 v_budget += pf->num_lan_msix;
7754
7755 /* reserve one vector for sideband flow director */
7756 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7757 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007758 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007759 v_budget++;
7760 vectors_left--;
7761 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007762 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007763 }
7764 }
John W Linville83840e42015-01-14 03:06:28 +00007765
Vasu Dev38e00432014-08-01 13:27:03 -07007766#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007767 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007768 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007769 if (!vectors_left)
7770 pf->num_fcoe_msix = 0;
7771 else if (vectors_left >= pf->num_fcoe_qps)
7772 pf->num_fcoe_msix = pf->num_fcoe_qps;
7773 else
7774 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007775 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007776 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007777 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007778
Vasu Dev38e00432014-08-01 13:27:03 -07007779#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007780 /* can we reserve enough for iWARP? */
7781 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007782 iwarp_requested = pf->num_iwarp_msix;
7783
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007784 if (!vectors_left)
7785 pf->num_iwarp_msix = 0;
7786 else if (vectors_left < pf->num_iwarp_msix)
7787 pf->num_iwarp_msix = 1;
7788 v_budget += pf->num_iwarp_msix;
7789 vectors_left -= pf->num_iwarp_msix;
7790 }
7791
Shannon Nelson1e200e42015-02-27 09:15:24 +00007792 /* any vectors left over go for VMDq support */
7793 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7794 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7795 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7796
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007797 if (!vectors_left) {
7798 pf->num_vmdq_msix = 0;
7799 pf->num_vmdq_qps = 0;
7800 } else {
7801 /* if we're short on vectors for what's desired, we limit
7802 * the queues per vmdq. If this is still more than are
7803 * available, the user will need to change the number of
7804 * queues/vectors used by the PF later with the ethtool
7805 * channels command
7806 */
7807 if (vmdq_vecs < vmdq_vecs_wanted)
7808 pf->num_vmdq_qps = 1;
7809 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007810
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007811 v_budget += vmdq_vecs;
7812 vectors_left -= vmdq_vecs;
7813 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007814 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007815
7816 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7817 GFP_KERNEL);
7818 if (!pf->msix_entries)
7819 return -ENOMEM;
7820
7821 for (i = 0; i < v_budget; i++)
7822 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007823 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007824
Shannon Nelson3b444392015-02-26 16:15:57 +00007825 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007826 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7827 kfree(pf->msix_entries);
7828 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007829 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007830 return -ENODEV;
7831
Shannon Nelson3b444392015-02-26 16:15:57 +00007832 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007833 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007834 pf->num_vmdq_vsis = 0;
7835 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007836 pf->num_lan_qps = 1;
7837 pf->num_lan_msix = 1;
7838
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007839 } else if (!vectors_left) {
7840 /* If we have limited resources, we will start with no vectors
7841 * for the special features and then allocate vectors to some
7842 * of these features based on the policy and at the end disable
7843 * the features that did not get any vectors.
7844 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007845 int vec;
7846
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007847 dev_info(&pf->pdev->dev,
7848 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007849 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007850 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007851
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007852 /* Scale vector usage down */
7853 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007854 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007855 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007856#ifdef I40E_FCOE
7857 pf->num_fcoe_qps = 0;
7858 pf->num_fcoe_msix = 0;
7859#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007860
7861 /* partition out the remaining vectors */
7862 switch (vec) {
7863 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007864 pf->num_lan_msix = 1;
7865 break;
7866 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007867 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7868 pf->num_lan_msix = 1;
7869 pf->num_iwarp_msix = 1;
7870 } else {
7871 pf->num_lan_msix = 2;
7872 }
Vasu Dev38e00432014-08-01 13:27:03 -07007873#ifdef I40E_FCOE
7874 /* give one vector to FCoE */
7875 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7876 pf->num_lan_msix = 1;
7877 pf->num_fcoe_msix = 1;
7878 }
Vasu Dev38e00432014-08-01 13:27:03 -07007879#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007880 break;
7881 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007882 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7883 pf->num_iwarp_msix = min_t(int, (vec / 3),
7884 iwarp_requested);
7885 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7886 I40E_DEFAULT_NUM_VMDQ_VSI);
7887 } else {
7888 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7889 I40E_DEFAULT_NUM_VMDQ_VSI);
7890 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007891 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7892 pf->num_fdsb_msix = 1;
7893 vec--;
7894 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007895 pf->num_lan_msix = min_t(int,
7896 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7897 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007898 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007899#ifdef I40E_FCOE
7900 /* give one vector to FCoE */
7901 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7902 pf->num_fcoe_msix = 1;
7903 vec--;
7904 }
7905#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007906 break;
7907 }
7908 }
7909
Stefan Assmannabd97a92016-09-19 13:37:51 +02007910 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7911 (pf->num_fdsb_msix == 0)) {
7912 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7913 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7914 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007915 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7916 (pf->num_vmdq_msix == 0)) {
7917 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7918 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7919 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007920
7921 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7922 (pf->num_iwarp_msix == 0)) {
7923 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7924 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7925 }
Vasu Dev38e00432014-08-01 13:27:03 -07007926#ifdef I40E_FCOE
7927
7928 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7929 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7930 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7931 }
7932#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007933 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7934 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7935 pf->num_lan_msix,
7936 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7937 pf->num_fdsb_msix,
7938 pf->num_iwarp_msix);
7939
Shannon Nelson3b444392015-02-26 16:15:57 +00007940 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007941}
7942
7943/**
Greg Rose90e04072014-03-06 08:59:57 +00007944 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007945 * @vsi: the VSI being configured
7946 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007947 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007948 *
7949 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7950 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007951static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007952{
7953 struct i40e_q_vector *q_vector;
7954
7955 /* allocate q_vector */
7956 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7957 if (!q_vector)
7958 return -ENOMEM;
7959
7960 q_vector->vsi = vsi;
7961 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007962 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7963
Alexander Duyck493fb302013-09-28 07:01:44 +00007964 if (vsi->netdev)
7965 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007966 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007967
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007968 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7969 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7970
Alexander Duyck493fb302013-09-28 07:01:44 +00007971 /* tie q_vector and vsi together */
7972 vsi->q_vectors[v_idx] = q_vector;
7973
7974 return 0;
7975}
7976
7977/**
Greg Rose90e04072014-03-06 08:59:57 +00007978 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007979 * @vsi: the VSI being configured
7980 *
7981 * We allocate one q_vector per queue interrupt. If allocation fails we
7982 * return -ENOMEM.
7983 **/
Greg Rose90e04072014-03-06 08:59:57 +00007984static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007985{
7986 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007987 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007988
7989 /* if not MSIX, give the one vector only to the LAN VSI */
7990 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7991 num_q_vectors = vsi->num_q_vectors;
7992 else if (vsi == pf->vsi[pf->lan_vsi])
7993 num_q_vectors = 1;
7994 else
7995 return -EINVAL;
7996
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007997 current_cpu = cpumask_first(cpu_online_mask);
7998
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007999 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008000 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008001 if (err)
8002 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008003 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8004 if (unlikely(current_cpu >= nr_cpu_ids))
8005 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008006 }
8007
8008 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008009
8010err_out:
8011 while (v_idx--)
8012 i40e_free_q_vector(vsi, v_idx);
8013
8014 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008015}
8016
8017/**
8018 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8019 * @pf: board private structure to initialize
8020 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008021static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008022{
Shannon Nelson3b444392015-02-26 16:15:57 +00008023 int vectors = 0;
8024 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008025
8026 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008027 vectors = i40e_init_msix(pf);
8028 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008029 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008030 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07008031#ifdef I40E_FCOE
8032 I40E_FLAG_FCOE_ENABLED |
8033#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008034 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008035 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008036 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008037 I40E_FLAG_SRIOV_ENABLED |
8038 I40E_FLAG_FD_SB_ENABLED |
8039 I40E_FLAG_FD_ATR_ENABLED |
8040 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008041
8042 /* rework the queue expectations without MSIX */
8043 i40e_determine_queue_usage(pf);
8044 }
8045 }
8046
8047 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8048 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008049 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008050 vectors = pci_enable_msi(pf->pdev);
8051 if (vectors < 0) {
8052 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8053 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008054 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8055 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008056 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008057 }
8058
Shannon Nelson958a3e32013-09-28 07:13:28 +00008059 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008060 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008061
Shannon Nelson3b444392015-02-26 16:15:57 +00008062 /* set up vector assignment tracking */
8063 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8064 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008065 if (!pf->irq_pile) {
8066 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8067 return -ENOMEM;
8068 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008069 pf->irq_pile->num_entries = vectors;
8070 pf->irq_pile->search_hint = 0;
8071
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008072 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008073 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008074
8075 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008076}
8077
8078/**
8079 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8080 * @pf: board private structure
8081 *
8082 * This sets up the handler for MSIX 0, which is used to manage the
8083 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8084 * when in MSI or Legacy interrupt mode.
8085 **/
8086static int i40e_setup_misc_vector(struct i40e_pf *pf)
8087{
8088 struct i40e_hw *hw = &pf->hw;
8089 int err = 0;
8090
8091 /* Only request the irq if this is the first time through, and
8092 * not when we're rebuilding after a Reset
8093 */
8094 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8095 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008096 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008097 if (err) {
8098 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008099 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008100 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008101 return -EFAULT;
8102 }
8103 }
8104
Jacob Kellerab437b52014-12-14 01:55:08 +00008105 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008106
8107 /* associate no queues to the misc vector */
8108 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8109 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8110
8111 i40e_flush(hw);
8112
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008113 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008114
8115 return err;
8116}
8117
8118/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008119 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8120 * @vsi: vsi structure
8121 * @seed: RSS hash seed
8122 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008123static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8124 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008125{
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008126 struct i40e_pf *pf = vsi->back;
8127 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008128 int ret = 0;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008129
Jacob Keller776b2e12016-07-19 16:23:30 -07008130 if (seed) {
8131 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8132 (struct i40e_aqc_get_set_rss_key_data *)seed;
8133 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8134 if (ret) {
8135 dev_info(&pf->pdev->dev,
8136 "Cannot set RSS key, err %s aq_err %s\n",
8137 i40e_stat_str(hw, ret),
8138 i40e_aq_str(hw, hw->aq.asq_last_status));
8139 return ret;
8140 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008141 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008142 if (lut) {
8143 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008144
Jacob Keller776b2e12016-07-19 16:23:30 -07008145 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8146 if (ret) {
8147 dev_info(&pf->pdev->dev,
8148 "Cannot set RSS lut, err %s aq_err %s\n",
8149 i40e_stat_str(hw, ret),
8150 i40e_aq_str(hw, hw->aq.asq_last_status));
8151 return ret;
8152 }
8153 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008154 return ret;
8155}
8156
8157/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008158 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8159 * @vsi: Pointer to vsi structure
8160 * @seed: Buffter to store the hash keys
8161 * @lut: Buffer to store the lookup table entries
8162 * @lut_size: Size of buffer to store the lookup table entries
8163 *
8164 * Return 0 on success, negative on failure
8165 */
8166static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8167 u8 *lut, u16 lut_size)
8168{
8169 struct i40e_pf *pf = vsi->back;
8170 struct i40e_hw *hw = &pf->hw;
8171 int ret = 0;
8172
8173 if (seed) {
8174 ret = i40e_aq_get_rss_key(hw, vsi->id,
8175 (struct i40e_aqc_get_set_rss_key_data *)seed);
8176 if (ret) {
8177 dev_info(&pf->pdev->dev,
8178 "Cannot get RSS key, err %s aq_err %s\n",
8179 i40e_stat_str(&pf->hw, ret),
8180 i40e_aq_str(&pf->hw,
8181 pf->hw.aq.asq_last_status));
8182 return ret;
8183 }
8184 }
8185
8186 if (lut) {
8187 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8188
8189 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8190 if (ret) {
8191 dev_info(&pf->pdev->dev,
8192 "Cannot get RSS lut, err %s aq_err %s\n",
8193 i40e_stat_str(&pf->hw, ret),
8194 i40e_aq_str(&pf->hw,
8195 pf->hw.aq.asq_last_status));
8196 return ret;
8197 }
8198 }
8199
8200 return ret;
8201}
8202
8203/**
Jacob Keller0582b962016-07-19 16:23:29 -07008204 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8205 * @vsi: VSI structure
8206 **/
8207static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8208{
8209 u8 seed[I40E_HKEY_ARRAY_SIZE];
8210 struct i40e_pf *pf = vsi->back;
8211 u8 *lut;
8212 int ret;
8213
8214 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8215 return 0;
8216
Jacob Keller552b9962016-07-19 16:23:31 -07008217 if (!vsi->rss_size)
8218 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8219 vsi->num_queue_pairs);
8220 if (!vsi->rss_size)
8221 return -EINVAL;
8222
Jacob Keller0582b962016-07-19 16:23:29 -07008223 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8224 if (!lut)
8225 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008226 /* Use the user configured hash keys and lookup table if there is one,
8227 * otherwise use default
8228 */
8229 if (vsi->rss_lut_user)
8230 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8231 else
8232 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8233 if (vsi->rss_hkey_user)
8234 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8235 else
8236 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008237 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8238 kfree(lut);
8239
8240 return ret;
8241}
8242
8243/**
Helin Zhang043dd652015-10-21 19:56:23 -04008244 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008245 * @vsi: Pointer to vsi structure
8246 * @seed: RSS hash seed
8247 * @lut: Lookup table
8248 * @lut_size: Lookup table size
8249 *
8250 * Returns 0 on success, negative on failure
8251 **/
8252static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8253 const u8 *lut, u16 lut_size)
8254{
8255 struct i40e_pf *pf = vsi->back;
8256 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008257 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008258 u8 i;
8259
8260 /* Fill out hash function seed */
8261 if (seed) {
8262 u32 *seed_dw = (u32 *)seed;
8263
Mitch Williamsc4e18682016-04-12 08:30:40 -07008264 if (vsi->type == I40E_VSI_MAIN) {
8265 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8266 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8267 seed_dw[i]);
8268 } else if (vsi->type == I40E_VSI_SRIOV) {
8269 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8270 i40e_write_rx_ctl(hw,
8271 I40E_VFQF_HKEY1(i, vf_id),
8272 seed_dw[i]);
8273 } else {
8274 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8275 }
Helin Zhange69ff812015-10-21 19:56:22 -04008276 }
8277
8278 if (lut) {
8279 u32 *lut_dw = (u32 *)lut;
8280
Mitch Williamsc4e18682016-04-12 08:30:40 -07008281 if (vsi->type == I40E_VSI_MAIN) {
8282 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8283 return -EINVAL;
8284 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8285 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8286 } else if (vsi->type == I40E_VSI_SRIOV) {
8287 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8288 return -EINVAL;
8289 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8290 i40e_write_rx_ctl(hw,
8291 I40E_VFQF_HLUT1(i, vf_id),
8292 lut_dw[i]);
8293 } else {
8294 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8295 }
Helin Zhange69ff812015-10-21 19:56:22 -04008296 }
8297 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008298
8299 return 0;
8300}
8301
8302/**
Helin Zhang043dd652015-10-21 19:56:23 -04008303 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8304 * @vsi: Pointer to VSI structure
8305 * @seed: Buffer to store the keys
8306 * @lut: Buffer to store the lookup table entries
8307 * @lut_size: Size of buffer to store the lookup table entries
8308 *
8309 * Returns 0 on success, negative on failure
8310 */
8311static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8312 u8 *lut, u16 lut_size)
8313{
8314 struct i40e_pf *pf = vsi->back;
8315 struct i40e_hw *hw = &pf->hw;
8316 u16 i;
8317
8318 if (seed) {
8319 u32 *seed_dw = (u32 *)seed;
8320
8321 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008322 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008323 }
8324 if (lut) {
8325 u32 *lut_dw = (u32 *)lut;
8326
8327 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8328 return -EINVAL;
8329 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8330 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8331 }
8332
8333 return 0;
8334}
8335
8336/**
8337 * i40e_config_rss - Configure RSS keys and lut
8338 * @vsi: Pointer to VSI structure
8339 * @seed: RSS hash seed
8340 * @lut: Lookup table
8341 * @lut_size: Lookup table size
8342 *
8343 * Returns 0 on success, negative on failure
8344 */
8345int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8346{
8347 struct i40e_pf *pf = vsi->back;
8348
8349 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8350 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8351 else
8352 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8353}
8354
8355/**
8356 * i40e_get_rss - Get RSS keys and lut
8357 * @vsi: Pointer to VSI structure
8358 * @seed: Buffer to store the keys
8359 * @lut: Buffer to store the lookup table entries
8360 * lut_size: Size of buffer to store the lookup table entries
8361 *
8362 * Returns 0 on success, negative on failure
8363 */
8364int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8365{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008366 struct i40e_pf *pf = vsi->back;
8367
8368 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8369 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8370 else
8371 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008372}
8373
8374/**
Helin Zhange69ff812015-10-21 19:56:22 -04008375 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8376 * @pf: Pointer to board private structure
8377 * @lut: Lookup table
8378 * @rss_table_size: Lookup table size
8379 * @rss_size: Range of queue number for hashing
8380 */
Alan Bradyf1582352016-08-24 11:33:46 -07008381void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8382 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008383{
Helin Zhange69ff812015-10-21 19:56:22 -04008384 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008385
Helin Zhange69ff812015-10-21 19:56:22 -04008386 for (i = 0; i < rss_table_size; i++)
8387 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008388}
8389
8390/**
Helin Zhang043dd652015-10-21 19:56:23 -04008391 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008392 * @pf: board private structure
8393 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008394static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008395{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008396 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008397 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008398 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008399 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008400 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008401 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008402 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008404 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008405 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8406 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008407 hena |= i40e_pf_get_default_rss_hena(pf);
8408
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008409 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8410 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008411
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008412 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008413 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008414 reg_val = (pf->rss_table_size == 512) ?
8415 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8416 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008417 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008418
Helin Zhang28c58692015-10-26 19:44:27 -04008419 /* Determine the RSS size of the VSI */
8420 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008421 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8422 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008423 if (!vsi->rss_size)
8424 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008425
Helin Zhange69ff812015-10-21 19:56:22 -04008426 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8427 if (!lut)
8428 return -ENOMEM;
8429
Helin Zhang28c58692015-10-26 19:44:27 -04008430 /* Use user configured lut if there is one, otherwise use default */
8431 if (vsi->rss_lut_user)
8432 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8433 else
8434 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008435
Helin Zhang28c58692015-10-26 19:44:27 -04008436 /* Use user configured hash key if there is one, otherwise
8437 * use default.
8438 */
8439 if (vsi->rss_hkey_user)
8440 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8441 else
8442 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008443 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008444 kfree(lut);
8445
8446 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008447}
8448
8449/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008450 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8451 * @pf: board private structure
8452 * @queue_count: the requested queue count for rss.
8453 *
8454 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8455 * count which may be different from the requested queue count.
8456 **/
8457int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8458{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008459 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8460 int new_rss_size;
8461
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008462 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8463 return 0;
8464
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008465 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008466
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008467 if (queue_count != vsi->num_queue_pairs) {
8468 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008469 i40e_prep_for_reset(pf);
8470
Helin Zhangacd65442015-10-26 19:44:28 -04008471 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008472
8473 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008474
8475 /* Discard the user configured hash keys and lut, if less
8476 * queues are enabled.
8477 */
8478 if (queue_count < vsi->rss_size) {
8479 i40e_clear_rss_config_user(vsi);
8480 dev_dbg(&pf->pdev->dev,
8481 "discard user configured hash keys and lut\n");
8482 }
8483
8484 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008485 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8486 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008487
Helin Zhang043dd652015-10-21 19:56:23 -04008488 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008489 }
Lihong Yang12815052016-09-27 11:28:48 -07008490 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8491 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008492 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008493}
8494
8495/**
Greg Rosef4492db2015-02-06 08:52:12 +00008496 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8497 * @pf: board private structure
8498 **/
8499i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8500{
8501 i40e_status status;
8502 bool min_valid, max_valid;
8503 u32 max_bw, min_bw;
8504
8505 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8506 &min_valid, &max_valid);
8507
8508 if (!status) {
8509 if (min_valid)
8510 pf->npar_min_bw = min_bw;
8511 if (max_valid)
8512 pf->npar_max_bw = max_bw;
8513 }
8514
8515 return status;
8516}
8517
8518/**
8519 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8520 * @pf: board private structure
8521 **/
8522i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8523{
8524 struct i40e_aqc_configure_partition_bw_data bw_data;
8525 i40e_status status;
8526
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008527 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008528 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008529 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8530 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8531
8532 /* Set the new bandwidths */
8533 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8534
8535 return status;
8536}
8537
8538/**
8539 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8540 * @pf: board private structure
8541 **/
8542i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8543{
8544 /* Commit temporary BW setting to permanent NVM image */
8545 enum i40e_admin_queue_err last_aq_status;
8546 i40e_status ret;
8547 u16 nvm_word;
8548
8549 if (pf->hw.partition_id != 1) {
8550 dev_info(&pf->pdev->dev,
8551 "Commit BW only works on partition 1! This is partition %d",
8552 pf->hw.partition_id);
8553 ret = I40E_NOT_SUPPORTED;
8554 goto bw_commit_out;
8555 }
8556
8557 /* Acquire NVM for read access */
8558 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8559 last_aq_status = pf->hw.aq.asq_last_status;
8560 if (ret) {
8561 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008562 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8563 i40e_stat_str(&pf->hw, ret),
8564 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008565 goto bw_commit_out;
8566 }
8567
8568 /* Read word 0x10 of NVM - SW compatibility word 1 */
8569 ret = i40e_aq_read_nvm(&pf->hw,
8570 I40E_SR_NVM_CONTROL_WORD,
8571 0x10, sizeof(nvm_word), &nvm_word,
8572 false, NULL);
8573 /* Save off last admin queue command status before releasing
8574 * the NVM
8575 */
8576 last_aq_status = pf->hw.aq.asq_last_status;
8577 i40e_release_nvm(&pf->hw);
8578 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008579 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8580 i40e_stat_str(&pf->hw, ret),
8581 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008582 goto bw_commit_out;
8583 }
8584
8585 /* Wait a bit for NVM release to complete */
8586 msleep(50);
8587
8588 /* Acquire NVM for write access */
8589 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8590 last_aq_status = pf->hw.aq.asq_last_status;
8591 if (ret) {
8592 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008593 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8594 i40e_stat_str(&pf->hw, ret),
8595 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008596 goto bw_commit_out;
8597 }
8598 /* Write it back out unchanged to initiate update NVM,
8599 * which will force a write of the shadow (alt) RAM to
8600 * the NVM - thus storing the bandwidth values permanently.
8601 */
8602 ret = i40e_aq_update_nvm(&pf->hw,
8603 I40E_SR_NVM_CONTROL_WORD,
8604 0x10, sizeof(nvm_word),
8605 &nvm_word, true, NULL);
8606 /* Save off last admin queue command status before releasing
8607 * the NVM
8608 */
8609 last_aq_status = pf->hw.aq.asq_last_status;
8610 i40e_release_nvm(&pf->hw);
8611 if (ret)
8612 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008613 "BW settings NOT SAVED, err %s aq_err %s\n",
8614 i40e_stat_str(&pf->hw, ret),
8615 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008616bw_commit_out:
8617
8618 return ret;
8619}
8620
8621/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008622 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8623 * @pf: board private structure to initialize
8624 *
8625 * i40e_sw_init initializes the Adapter private data structure.
8626 * Fields are initialized based on PCI device information and
8627 * OS network device settings (MTU size).
8628 **/
8629static int i40e_sw_init(struct i40e_pf *pf)
8630{
8631 int err = 0;
8632 int size;
8633
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008634 /* Set default capability flags */
8635 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8636 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008637 I40E_FLAG_MSIX_ENABLED;
8638
Mitch Williamsca99eb92014-04-04 04:43:07 +00008639 /* Set default ITR */
8640 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8641 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8642
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008643 /* Depending on PF configurations, it is possible that the RSS
8644 * maximum might end up larger than the available queues
8645 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008646 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008647 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008648 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008649 pf->rss_size_max = min_t(int, pf->rss_size_max,
8650 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008651 if (pf->hw.func_caps.rss) {
8652 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008653 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8654 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008655 }
8656
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008657 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008658 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008659 pf->flags |= I40E_FLAG_MFP_ENABLED;
8660 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008661 if (i40e_get_npar_bw_setting(pf))
8662 dev_warn(&pf->pdev->dev,
8663 "Could not get NPAR bw settings\n");
8664 else
8665 dev_info(&pf->pdev->dev,
8666 "Min BW = %8.8x, Max BW = %8.8x\n",
8667 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008668 }
8669
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008670 /* FW/NVM is not yet fixed in this regard */
8671 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8672 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8673 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8674 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008675 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8676 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008677 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008678 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008679 else
8680 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008681 pf->fdir_pf_filter_count =
8682 pf->hw.func_caps.fd_filters_guaranteed;
8683 pf->hw.fdir_shared_filter_count =
8684 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008685 }
8686
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008687 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008688 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008689 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008690 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008691 /* No DCB support for FW < v4.33 */
8692 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8693 }
8694
8695 /* Disable FW LLDP if FW < v4.3 */
8696 if (i40e_is_mac_710(&pf->hw) &&
8697 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8698 (pf->hw.aq.fw_maj_ver < 4)))
8699 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8700
8701 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8702 if (i40e_is_mac_710(&pf->hw) &&
8703 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8704 (pf->hw.aq.fw_maj_ver >= 5)))
8705 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008706
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008707 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008708 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008709 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008710 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008711 }
8712
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008713 if (pf->hw.func_caps.iwarp) {
8714 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8715 /* IWARP needs one extra vector for CQP just like MISC.*/
8716 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8717 }
8718
Vasu Dev38e00432014-08-01 13:27:03 -07008719#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008720 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008721
8722#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008723#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008724 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008725 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8726 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8727 pf->num_req_vfs = min_t(int,
8728 pf->hw.func_caps.num_vfs,
8729 I40E_MAX_VF_COUNT);
8730 }
8731#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008732 if (pf->hw.mac.type == I40E_MAC_X722) {
8733 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8734 I40E_FLAG_128_QP_RSS_CAPABLE |
8735 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8736 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8737 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008738 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008739 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008740 I40E_FLAG_USE_SET_LLDP_MIB |
Jacob Keller1e28e862016-11-11 12:39:25 -08008741 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE |
8742 I40E_FLAG_PTP_L4_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008743 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8744 ((pf->hw.aq.api_maj_ver == 1) &&
8745 (pf->hw.aq.api_min_ver > 4))) {
8746 /* Supported in FW API version higher than 1.4 */
8747 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008748 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8749 } else {
8750 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008751 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008753 pf->eeprom_version = 0xDEAD;
8754 pf->lan_veb = I40E_NO_VEB;
8755 pf->lan_vsi = I40E_NO_VSI;
8756
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008757 /* By default FW has this off for performance reasons */
8758 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8759
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008760 /* set up queue assignment tracking */
8761 size = sizeof(struct i40e_lump_tracking)
8762 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8763 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8764 if (!pf->qp_pile) {
8765 err = -ENOMEM;
8766 goto sw_init_done;
8767 }
8768 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8769 pf->qp_pile->search_hint = 0;
8770
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008771 pf->tx_timeout_recovery_level = 1;
8772
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008773 mutex_init(&pf->switch_mutex);
8774
Greg Rosec668a122015-02-26 16:10:39 +00008775 /* If NPAR is enabled nudge the Tx scheduler */
8776 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8777 i40e_set_npar_bw_setting(pf);
8778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008779sw_init_done:
8780 return err;
8781}
8782
8783/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008784 * i40e_set_ntuple - set the ntuple feature flag and take action
8785 * @pf: board private structure to initialize
8786 * @features: the feature set that the stack is suggesting
8787 *
8788 * returns a bool to indicate if reset needs to happen
8789 **/
8790bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8791{
8792 bool need_reset = false;
8793
8794 /* Check if Flow Director n-tuple support was enabled or disabled. If
8795 * the state changed, we need to reset.
8796 */
8797 if (features & NETIF_F_NTUPLE) {
8798 /* Enable filters and mark for reset */
8799 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8800 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008801 /* enable FD_SB only if there is MSI-X vector */
8802 if (pf->num_fdsb_msix > 0)
8803 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008804 } else {
8805 /* turn off filters, mark for reset and clear SW filter list */
8806 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8807 need_reset = true;
8808 i40e_fdir_filter_exit(pf);
8809 }
8810 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008811 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008812 /* reset fd counters */
8813 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8814 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008815 /* if ATR was auto disabled it can be re-enabled. */
8816 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008817 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008818 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008819 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8820 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8821 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008822 }
8823 return need_reset;
8824}
8825
8826/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008827 * i40e_clear_rss_lut - clear the rx hash lookup table
8828 * @vsi: the VSI being configured
8829 **/
8830static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8831{
8832 struct i40e_pf *pf = vsi->back;
8833 struct i40e_hw *hw = &pf->hw;
8834 u16 vf_id = vsi->vf_id;
8835 u8 i;
8836
8837 if (vsi->type == I40E_VSI_MAIN) {
8838 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8839 wr32(hw, I40E_PFQF_HLUT(i), 0);
8840 } else if (vsi->type == I40E_VSI_SRIOV) {
8841 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8842 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8843 } else {
8844 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8845 }
8846}
8847
8848/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008849 * i40e_set_features - set the netdev feature flags
8850 * @netdev: ptr to the netdev being adjusted
8851 * @features: the feature set that the stack is suggesting
8852 **/
8853static int i40e_set_features(struct net_device *netdev,
8854 netdev_features_t features)
8855{
8856 struct i40e_netdev_priv *np = netdev_priv(netdev);
8857 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008858 struct i40e_pf *pf = vsi->back;
8859 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008860
Alan Bradyd8ec9862016-07-27 12:02:38 -07008861 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8862 i40e_pf_config_rss(pf);
8863 else if (!(features & NETIF_F_RXHASH) &&
8864 netdev->features & NETIF_F_RXHASH)
8865 i40e_clear_rss_lut(vsi);
8866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008867 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8868 i40e_vlan_stripping_enable(vsi);
8869 else
8870 i40e_vlan_stripping_disable(vsi);
8871
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008872 need_reset = i40e_set_ntuple(pf, features);
8873
8874 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008875 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008877 return 0;
8878}
8879
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008880/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008881 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008882 * @pf: board private structure
8883 * @port: The UDP port to look up
8884 *
8885 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8886 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008887static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008888{
8889 u8 i;
8890
8891 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008892 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008893 return i;
8894 }
8895
8896 return i;
8897}
8898
8899/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008900 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008901 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008902 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008903 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008904static void i40e_udp_tunnel_add(struct net_device *netdev,
8905 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008906{
8907 struct i40e_netdev_priv *np = netdev_priv(netdev);
8908 struct i40e_vsi *vsi = np->vsi;
8909 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008910 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008911 u8 next_idx;
8912 u8 idx;
8913
Singhai, Anjali6a899022015-12-14 12:21:18 -08008914 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008915
8916 /* Check if port already exists */
8917 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008918 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008919 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008920 return;
8921 }
8922
8923 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008924 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008925
8926 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008927 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008928 ntohs(port));
8929 return;
8930 }
8931
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008932 switch (ti->type) {
8933 case UDP_TUNNEL_TYPE_VXLAN:
8934 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8935 break;
8936 case UDP_TUNNEL_TYPE_GENEVE:
8937 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8938 return;
8939 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8940 break;
8941 default:
8942 return;
8943 }
8944
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008945 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008946 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008947 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8948 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008949}
8950
8951/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008952 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008953 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008954 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008955 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008956static void i40e_udp_tunnel_del(struct net_device *netdev,
8957 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008958{
8959 struct i40e_netdev_priv *np = netdev_priv(netdev);
8960 struct i40e_vsi *vsi = np->vsi;
8961 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008962 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008963 u8 idx;
8964
Singhai, Anjali6a899022015-12-14 12:21:18 -08008965 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008966
8967 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008968 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8969 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008970
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008971 switch (ti->type) {
8972 case UDP_TUNNEL_TYPE_VXLAN:
8973 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8974 goto not_found;
8975 break;
8976 case UDP_TUNNEL_TYPE_GENEVE:
8977 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8978 goto not_found;
8979 break;
8980 default:
8981 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008982 }
8983
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008984 /* if port exists, set it to 0 (mark for deletion)
8985 * and make it pending
8986 */
8987 pf->udp_ports[idx].index = 0;
8988 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008989 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8990
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008991 return;
8992not_found:
8993 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8994 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008995}
8996
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008997static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008998 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008999{
9000 struct i40e_netdev_priv *np = netdev_priv(netdev);
9001 struct i40e_pf *pf = np->vsi->back;
9002 struct i40e_hw *hw = &pf->hw;
9003
9004 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9005 return -EOPNOTSUPP;
9006
9007 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9008 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9009
9010 return 0;
9011}
9012
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009013/**
9014 * i40e_ndo_fdb_add - add an entry to the hardware database
9015 * @ndm: the input from the stack
9016 * @tb: pointer to array of nladdr (unused)
9017 * @dev: the net device pointer
9018 * @addr: the MAC address entry being added
9019 * @flags: instructions from stack about fdb operation
9020 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009021static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9022 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009023 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009024 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009025{
9026 struct i40e_netdev_priv *np = netdev_priv(dev);
9027 struct i40e_pf *pf = np->vsi->back;
9028 int err = 0;
9029
9030 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9031 return -EOPNOTSUPP;
9032
Or Gerlitz65891fe2014-12-14 18:19:05 +02009033 if (vid) {
9034 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9035 return -EINVAL;
9036 }
9037
Greg Rose4ba0dea2014-03-06 08:59:55 +00009038 /* Hardware does not support aging addresses so if a
9039 * ndm_state is given only allow permanent addresses
9040 */
9041 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9042 netdev_info(dev, "FDB only supports static addresses\n");
9043 return -EINVAL;
9044 }
9045
9046 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9047 err = dev_uc_add_excl(dev, addr);
9048 else if (is_multicast_ether_addr(addr))
9049 err = dev_mc_add_excl(dev, addr);
9050 else
9051 err = -EINVAL;
9052
9053 /* Only return duplicate errors if NLM_F_EXCL is set */
9054 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9055 err = 0;
9056
9057 return err;
9058}
9059
Neerav Parikh51616012015-02-06 08:52:14 +00009060/**
9061 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9062 * @dev: the netdev being configured
9063 * @nlh: RTNL message
9064 *
9065 * Inserts a new hardware bridge if not already created and
9066 * enables the bridging mode requested (VEB or VEPA). If the
9067 * hardware bridge has already been inserted and the request
9068 * is to change the mode then that requires a PF reset to
9069 * allow rebuild of the components with required hardware
9070 * bridge mode enabled.
9071 **/
9072static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009073 struct nlmsghdr *nlh,
9074 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009075{
9076 struct i40e_netdev_priv *np = netdev_priv(dev);
9077 struct i40e_vsi *vsi = np->vsi;
9078 struct i40e_pf *pf = vsi->back;
9079 struct i40e_veb *veb = NULL;
9080 struct nlattr *attr, *br_spec;
9081 int i, rem;
9082
9083 /* Only for PF VSI for now */
9084 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9085 return -EOPNOTSUPP;
9086
9087 /* Find the HW bridge for PF VSI */
9088 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9089 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9090 veb = pf->veb[i];
9091 }
9092
9093 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9094
9095 nla_for_each_nested(attr, br_spec, rem) {
9096 __u16 mode;
9097
9098 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9099 continue;
9100
9101 mode = nla_get_u16(attr);
9102 if ((mode != BRIDGE_MODE_VEPA) &&
9103 (mode != BRIDGE_MODE_VEB))
9104 return -EINVAL;
9105
9106 /* Insert a new HW bridge */
9107 if (!veb) {
9108 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9109 vsi->tc_config.enabled_tc);
9110 if (veb) {
9111 veb->bridge_mode = mode;
9112 i40e_config_bridge_mode(veb);
9113 } else {
9114 /* No Bridge HW offload available */
9115 return -ENOENT;
9116 }
9117 break;
9118 } else if (mode != veb->bridge_mode) {
9119 /* Existing HW bridge but different mode needs reset */
9120 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009121 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9122 if (mode == BRIDGE_MODE_VEB)
9123 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9124 else
9125 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9126 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009127 break;
9128 }
9129 }
9130
9131 return 0;
9132}
9133
9134/**
9135 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9136 * @skb: skb buff
9137 * @pid: process id
9138 * @seq: RTNL message seq #
9139 * @dev: the netdev being configured
9140 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009141 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009142 *
9143 * Return the mode in which the hardware bridge is operating in
9144 * i.e VEB or VEPA.
9145 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009146static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9147 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009148 u32 __always_unused filter_mask,
9149 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009150{
9151 struct i40e_netdev_priv *np = netdev_priv(dev);
9152 struct i40e_vsi *vsi = np->vsi;
9153 struct i40e_pf *pf = vsi->back;
9154 struct i40e_veb *veb = NULL;
9155 int i;
9156
9157 /* Only for PF VSI for now */
9158 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9159 return -EOPNOTSUPP;
9160
9161 /* Find the HW bridge for the PF VSI */
9162 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9163 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9164 veb = pf->veb[i];
9165 }
9166
9167 if (!veb)
9168 return 0;
9169
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009170 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009171 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009172}
Neerav Parikh51616012015-02-06 08:52:14 +00009173
Joe Stringerf44a75e2015-04-14 17:09:14 -07009174/**
9175 * i40e_features_check - Validate encapsulated packet conforms to limits
9176 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009177 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009178 * @features: Offload features that the stack believes apply
9179 **/
9180static netdev_features_t i40e_features_check(struct sk_buff *skb,
9181 struct net_device *dev,
9182 netdev_features_t features)
9183{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009184 size_t len;
9185
9186 /* No point in doing any of this if neither checksum nor GSO are
9187 * being requested for this frame. We can rule out both by just
9188 * checking for CHECKSUM_PARTIAL
9189 */
9190 if (skb->ip_summed != CHECKSUM_PARTIAL)
9191 return features;
9192
9193 /* We cannot support GSO if the MSS is going to be less than
9194 * 64 bytes. If it is then we need to drop support for GSO.
9195 */
9196 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9197 features &= ~NETIF_F_GSO_MASK;
9198
9199 /* MACLEN can support at most 63 words */
9200 len = skb_network_header(skb) - skb->data;
9201 if (len & ~(63 * 2))
9202 goto out_err;
9203
9204 /* IPLEN and EIPLEN can support at most 127 dwords */
9205 len = skb_transport_header(skb) - skb_network_header(skb);
9206 if (len & ~(127 * 4))
9207 goto out_err;
9208
9209 if (skb->encapsulation) {
9210 /* L4TUNLEN can support 127 words */
9211 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9212 if (len & ~(127 * 2))
9213 goto out_err;
9214
9215 /* IPLEN can support at most 127 dwords */
9216 len = skb_inner_transport_header(skb) -
9217 skb_inner_network_header(skb);
9218 if (len & ~(127 * 4))
9219 goto out_err;
9220 }
9221
9222 /* No need to validate L4LEN as TCP is the only protocol with a
9223 * a flexible value and we support all possible values supported
9224 * by TCP, which is at most 15 dwords
9225 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009226
9227 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009228out_err:
9229 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009230}
9231
Shannon Nelson37a29732015-02-27 09:15:19 +00009232static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009233 .ndo_open = i40e_open,
9234 .ndo_stop = i40e_close,
9235 .ndo_start_xmit = i40e_lan_xmit_frame,
9236 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9237 .ndo_set_rx_mode = i40e_set_rx_mode,
9238 .ndo_validate_addr = eth_validate_addr,
9239 .ndo_set_mac_address = i40e_set_mac,
9240 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009241 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009242 .ndo_tx_timeout = i40e_tx_timeout,
9243 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9244 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9245#ifdef CONFIG_NET_POLL_CONTROLLER
9246 .ndo_poll_controller = i40e_netpoll,
9247#endif
John Fastabende4c67342016-02-16 21:16:15 -08009248 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009249#ifdef I40E_FCOE
9250 .ndo_fcoe_enable = i40e_fcoe_enable,
9251 .ndo_fcoe_disable = i40e_fcoe_disable,
9252#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009253 .ndo_set_features = i40e_set_features,
9254 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9255 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009256 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009257 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009258 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009259 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009260 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009261 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9262 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009263 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009264 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009265 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009266 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9267 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009268};
9269
9270/**
9271 * i40e_config_netdev - Setup the netdev flags
9272 * @vsi: the VSI being configured
9273 *
9274 * Returns 0 on success, negative value on failure
9275 **/
9276static int i40e_config_netdev(struct i40e_vsi *vsi)
9277{
9278 struct i40e_pf *pf = vsi->back;
9279 struct i40e_hw *hw = &pf->hw;
9280 struct i40e_netdev_priv *np;
9281 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009282 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009283 u8 mac_addr[ETH_ALEN];
9284 int etherdev_size;
9285
9286 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009287 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009288 if (!netdev)
9289 return -ENOMEM;
9290
9291 vsi->netdev = netdev;
9292 np = netdev_priv(netdev);
9293 np->vsi = vsi;
9294
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009295 netdev->hw_enc_features |= NETIF_F_SG |
9296 NETIF_F_IP_CSUM |
9297 NETIF_F_IPV6_CSUM |
9298 NETIF_F_HIGHDMA |
9299 NETIF_F_SOFT_FEATURES |
9300 NETIF_F_TSO |
9301 NETIF_F_TSO_ECN |
9302 NETIF_F_TSO6 |
9303 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009304 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009305 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009306 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009307 NETIF_F_GSO_UDP_TUNNEL |
9308 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009309 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009310 NETIF_F_SCTP_CRC |
9311 NETIF_F_RXHASH |
9312 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009313 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009314
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009315 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009316 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9317
9318 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009319
9320 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009321 netdev->vlan_features |= netdev->hw_enc_features |
9322 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009323
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009324 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009325 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009326
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009327 netdev->hw_features |= netdev->hw_enc_features |
9328 NETIF_F_HW_VLAN_CTAG_TX |
9329 NETIF_F_HW_VLAN_CTAG_RX;
9330
9331 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009332 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009333
9334 if (vsi->type == I40E_VSI_MAIN) {
9335 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009336 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009337 /* The following steps are necessary to prevent reception
9338 * of tagged packets - some older NVM configurations load a
9339 * default a MAC-VLAN filter that accepts any tagged packet
9340 * which must be replaced by a normal filter.
9341 */
9342 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009343 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009344 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009345 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009346 } else {
9347 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9348 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9349 pf->vsi[pf->lan_vsi]->netdev->name);
9350 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009351
Jacob Keller278e7d02016-10-05 09:30:37 -07009352 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009353 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009354 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009355 }
Kiran Patil21659032015-09-30 14:09:03 -04009356
Jacob Keller435c0842016-11-08 13:05:10 -08009357 /* Add the broadcast filter so that we initially will receive
9358 * broadcast packets. Note that when a new VLAN is first added the
9359 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9360 * specific filters as part of transitioning into "vlan" operation.
9361 * When more VLANs are added, the driver will copy each existing MAC
9362 * filter and add it for the new VLAN.
9363 *
9364 * Broadcast filters are handled specially by
9365 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9366 * promiscuous bit instead of adding this directly as a MAC/VLAN
9367 * filter. The subtask will update the correct broadcast promiscuous
9368 * bits as VLANs become active or inactive.
9369 */
9370 eth_broadcast_addr(broadcast);
9371 spin_lock_bh(&vsi->mac_filter_hash_lock);
9372 i40e_add_filter(vsi, broadcast, I40E_VLAN_ANY);
9373 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9374
Greg Rose9a173902014-05-22 06:32:02 +00009375 ether_addr_copy(netdev->dev_addr, mac_addr);
9376 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009378 netdev->priv_flags |= IFF_UNICAST_FLT;
9379 netdev->priv_flags |= IFF_SUPP_NOFCS;
9380 /* Setup netdev TC information */
9381 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9382
9383 netdev->netdev_ops = &i40e_netdev_ops;
9384 netdev->watchdog_timeo = 5 * HZ;
9385 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009386#ifdef I40E_FCOE
9387 i40e_fcoe_config_netdev(netdev, vsi);
9388#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009389
Jarod Wilson91c527a2016-10-17 15:54:05 -04009390 /* MTU range: 68 - 9706 */
9391 netdev->min_mtu = ETH_MIN_MTU;
9392 netdev->max_mtu = I40E_MAX_RXBUFFER -
9393 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009395 return 0;
9396}
9397
9398/**
9399 * i40e_vsi_delete - Delete a VSI from the switch
9400 * @vsi: the VSI being removed
9401 *
9402 * Returns 0 on success, negative value on failure
9403 **/
9404static void i40e_vsi_delete(struct i40e_vsi *vsi)
9405{
9406 /* remove default VSI is not allowed */
9407 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9408 return;
9409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009410 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009411}
9412
9413/**
Neerav Parikh51616012015-02-06 08:52:14 +00009414 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9415 * @vsi: the VSI being queried
9416 *
9417 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9418 **/
9419int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9420{
9421 struct i40e_veb *veb;
9422 struct i40e_pf *pf = vsi->back;
9423
9424 /* Uplink is not a bridge so default to VEB */
9425 if (vsi->veb_idx == I40E_NO_VEB)
9426 return 1;
9427
9428 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009429 if (!veb) {
9430 dev_info(&pf->pdev->dev,
9431 "There is no veb associated with the bridge\n");
9432 return -ENOENT;
9433 }
Neerav Parikh51616012015-02-06 08:52:14 +00009434
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009435 /* Uplink is a bridge in VEPA mode */
9436 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9437 return 0;
9438 } else {
9439 /* Uplink is a bridge in VEB mode */
9440 return 1;
9441 }
9442
9443 /* VEPA is now default bridge, so return 0 */
9444 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009445}
9446
9447/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009448 * i40e_add_vsi - Add a VSI to the switch
9449 * @vsi: the VSI being configured
9450 *
9451 * This initializes a VSI context depending on the VSI type to be added and
9452 * passes it down to the add_vsi aq command.
9453 **/
9454static int i40e_add_vsi(struct i40e_vsi *vsi)
9455{
9456 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009457 struct i40e_pf *pf = vsi->back;
9458 struct i40e_hw *hw = &pf->hw;
9459 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009460 struct i40e_mac_filter *f;
9461 struct hlist_node *h;
9462 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009463
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009464 u8 enabled_tc = 0x1; /* TC0 enabled */
9465 int f_count = 0;
9466
9467 memset(&ctxt, 0, sizeof(ctxt));
9468 switch (vsi->type) {
9469 case I40E_VSI_MAIN:
9470 /* The PF's main VSI is already setup as part of the
9471 * device initialization, so we'll not bother with
9472 * the add_vsi call, but we will retrieve the current
9473 * VSI context.
9474 */
9475 ctxt.seid = pf->main_vsi_seid;
9476 ctxt.pf_num = pf->hw.pf_id;
9477 ctxt.vf_num = 0;
9478 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9479 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9480 if (ret) {
9481 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009482 "couldn't get PF vsi config, err %s aq_err %s\n",
9483 i40e_stat_str(&pf->hw, ret),
9484 i40e_aq_str(&pf->hw,
9485 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009486 return -ENOENT;
9487 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009488 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009489 vsi->info.valid_sections = 0;
9490
9491 vsi->seid = ctxt.seid;
9492 vsi->id = ctxt.vsi_number;
9493
9494 enabled_tc = i40e_pf_get_tc_map(pf);
9495
9496 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009497 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9498 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009499 memset(&ctxt, 0, sizeof(ctxt));
9500 ctxt.seid = pf->main_vsi_seid;
9501 ctxt.pf_num = pf->hw.pf_id;
9502 ctxt.vf_num = 0;
9503 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9504 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9505 if (ret) {
9506 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009507 "update vsi failed, err %s aq_err %s\n",
9508 i40e_stat_str(&pf->hw, ret),
9509 i40e_aq_str(&pf->hw,
9510 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009511 ret = -ENOENT;
9512 goto err;
9513 }
9514 /* update the local VSI info queue map */
9515 i40e_vsi_update_queue_map(vsi, &ctxt);
9516 vsi->info.valid_sections = 0;
9517 } else {
9518 /* Default/Main VSI is only enabled for TC0
9519 * reconfigure it to enable all TCs that are
9520 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009521 * For MFP case the iSCSI PF would use this
9522 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009523 */
9524 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9525 if (ret) {
9526 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009527 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9528 enabled_tc,
9529 i40e_stat_str(&pf->hw, ret),
9530 i40e_aq_str(&pf->hw,
9531 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009532 ret = -ENOENT;
9533 }
9534 }
9535 break;
9536
9537 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009538 ctxt.pf_num = hw->pf_id;
9539 ctxt.vf_num = 0;
9540 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009541 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009542 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009543 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9544 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009545 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009546 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009547 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009548 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009549 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009550 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009551 break;
9552
9553 case I40E_VSI_VMDQ2:
9554 ctxt.pf_num = hw->pf_id;
9555 ctxt.vf_num = 0;
9556 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009557 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009558 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9559
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009560 /* This VSI is connected to VEB so the switch_id
9561 * should be set to zero by default.
9562 */
Neerav Parikh51616012015-02-06 08:52:14 +00009563 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9564 ctxt.info.valid_sections |=
9565 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9566 ctxt.info.switch_id =
9567 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9568 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009569
9570 /* Setup the VSI tx/rx queue map for TC0 only for now */
9571 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9572 break;
9573
9574 case I40E_VSI_SRIOV:
9575 ctxt.pf_num = hw->pf_id;
9576 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9577 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009578 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009579 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9580
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009581 /* This VSI is connected to VEB so the switch_id
9582 * should be set to zero by default.
9583 */
Neerav Parikh51616012015-02-06 08:52:14 +00009584 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9585 ctxt.info.valid_sections |=
9586 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9587 ctxt.info.switch_id =
9588 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9589 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009590
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009591 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9592 ctxt.info.valid_sections |=
9593 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9594 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009595 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9596 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009597 }
9598
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009599 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9600 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009601 if (pf->vf[vsi->vf_id].spoofchk) {
9602 ctxt.info.valid_sections |=
9603 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9604 ctxt.info.sec_flags |=
9605 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9606 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9607 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009608 /* Setup the VSI tx/rx queue map for TC0 only for now */
9609 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9610 break;
9611
Vasu Dev38e00432014-08-01 13:27:03 -07009612#ifdef I40E_FCOE
9613 case I40E_VSI_FCOE:
9614 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9615 if (ret) {
9616 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9617 return ret;
9618 }
9619 break;
9620
9621#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009622 case I40E_VSI_IWARP:
9623 /* send down message to iWARP */
9624 break;
9625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009626 default:
9627 return -ENODEV;
9628 }
9629
9630 if (vsi->type != I40E_VSI_MAIN) {
9631 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9632 if (ret) {
9633 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009634 "add vsi failed, err %s aq_err %s\n",
9635 i40e_stat_str(&pf->hw, ret),
9636 i40e_aq_str(&pf->hw,
9637 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009638 ret = -ENOENT;
9639 goto err;
9640 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009641 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009642 vsi->info.valid_sections = 0;
9643 vsi->seid = ctxt.seid;
9644 vsi->id = ctxt.vsi_number;
9645 }
9646
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009647 vsi->active_filters = 0;
9648 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009649 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009650 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009651 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009652 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009653 f_count++;
9654 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009655 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009656
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009657 if (f_count) {
9658 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9659 pf->flags |= I40E_FLAG_FILTER_SYNC;
9660 }
9661
9662 /* Update VSI BW information */
9663 ret = i40e_vsi_get_bw_info(vsi);
9664 if (ret) {
9665 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009666 "couldn't get vsi bw info, err %s aq_err %s\n",
9667 i40e_stat_str(&pf->hw, ret),
9668 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009669 /* VSI is already added so not tearing that up */
9670 ret = 0;
9671 }
9672
9673err:
9674 return ret;
9675}
9676
9677/**
9678 * i40e_vsi_release - Delete a VSI and free its resources
9679 * @vsi: the VSI being removed
9680 *
9681 * Returns 0 on success or < 0 on error
9682 **/
9683int i40e_vsi_release(struct i40e_vsi *vsi)
9684{
Jacob Keller278e7d02016-10-05 09:30:37 -07009685 struct i40e_mac_filter *f;
9686 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009687 struct i40e_veb *veb = NULL;
9688 struct i40e_pf *pf;
9689 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009690 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009691
9692 pf = vsi->back;
9693
9694 /* release of a VEB-owner or last VSI is not allowed */
9695 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9696 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9697 vsi->seid, vsi->uplink_seid);
9698 return -ENODEV;
9699 }
9700 if (vsi == pf->vsi[pf->lan_vsi] &&
9701 !test_bit(__I40E_DOWN, &pf->state)) {
9702 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9703 return -ENODEV;
9704 }
9705
9706 uplink_seid = vsi->uplink_seid;
9707 if (vsi->type != I40E_VSI_SRIOV) {
9708 if (vsi->netdev_registered) {
9709 vsi->netdev_registered = false;
9710 if (vsi->netdev) {
9711 /* results in a call to i40e_close() */
9712 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009713 }
9714 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009715 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009716 }
9717 i40e_vsi_disable_irq(vsi);
9718 }
9719
Jacob Keller278e7d02016-10-05 09:30:37 -07009720 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009721
9722 /* clear the sync flag on all filters */
9723 if (vsi->netdev) {
9724 __dev_uc_unsync(vsi->netdev, NULL);
9725 __dev_mc_unsync(vsi->netdev, NULL);
9726 }
9727
9728 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009729 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009730 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009731
Jacob Keller278e7d02016-10-05 09:30:37 -07009732 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009733
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009734 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009735
9736 i40e_vsi_delete(vsi);
9737 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009738 if (vsi->netdev) {
9739 free_netdev(vsi->netdev);
9740 vsi->netdev = NULL;
9741 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009742 i40e_vsi_clear_rings(vsi);
9743 i40e_vsi_clear(vsi);
9744
9745 /* If this was the last thing on the VEB, except for the
9746 * controlling VSI, remove the VEB, which puts the controlling
9747 * VSI onto the next level down in the switch.
9748 *
9749 * Well, okay, there's one more exception here: don't remove
9750 * the orphan VEBs yet. We'll wait for an explicit remove request
9751 * from up the network stack.
9752 */
Mitch Williams505682c2014-05-20 08:01:37 +00009753 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009754 if (pf->vsi[i] &&
9755 pf->vsi[i]->uplink_seid == uplink_seid &&
9756 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9757 n++; /* count the VSIs */
9758 }
9759 }
9760 for (i = 0; i < I40E_MAX_VEB; i++) {
9761 if (!pf->veb[i])
9762 continue;
9763 if (pf->veb[i]->uplink_seid == uplink_seid)
9764 n++; /* count the VEBs */
9765 if (pf->veb[i]->seid == uplink_seid)
9766 veb = pf->veb[i];
9767 }
9768 if (n == 0 && veb && veb->uplink_seid != 0)
9769 i40e_veb_release(veb);
9770
9771 return 0;
9772}
9773
9774/**
9775 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9776 * @vsi: ptr to the VSI
9777 *
9778 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9779 * corresponding SW VSI structure and initializes num_queue_pairs for the
9780 * newly allocated VSI.
9781 *
9782 * Returns 0 on success or negative on failure
9783 **/
9784static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9785{
9786 int ret = -ENOENT;
9787 struct i40e_pf *pf = vsi->back;
9788
Alexander Duyck493fb302013-09-28 07:01:44 +00009789 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009790 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9791 vsi->seid);
9792 return -EEXIST;
9793 }
9794
9795 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009796 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009797 vsi->seid, vsi->base_vector);
9798 return -EEXIST;
9799 }
9800
Greg Rose90e04072014-03-06 08:59:57 +00009801 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009802 if (ret) {
9803 dev_info(&pf->pdev->dev,
9804 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9805 vsi->num_q_vectors, vsi->seid, ret);
9806 vsi->num_q_vectors = 0;
9807 goto vector_setup_out;
9808 }
9809
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009810 /* In Legacy mode, we do not have to get any other vector since we
9811 * piggyback on the misc/ICR0 for queue interrupts.
9812 */
9813 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9814 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009815 if (vsi->num_q_vectors)
9816 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9817 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009818 if (vsi->base_vector < 0) {
9819 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009820 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9821 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009822 i40e_vsi_free_q_vectors(vsi);
9823 ret = -ENOENT;
9824 goto vector_setup_out;
9825 }
9826
9827vector_setup_out:
9828 return ret;
9829}
9830
9831/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009832 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9833 * @vsi: pointer to the vsi.
9834 *
9835 * This re-allocates a vsi's queue resources.
9836 *
9837 * Returns pointer to the successfully allocated and configured VSI sw struct
9838 * on success, otherwise returns NULL on failure.
9839 **/
9840static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9841{
John Underwoodf5340392016-02-18 09:19:24 -08009842 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009843 u8 enabled_tc;
9844 int ret;
9845
John Underwoodf5340392016-02-18 09:19:24 -08009846 if (!vsi)
9847 return NULL;
9848
9849 pf = vsi->back;
9850
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009851 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9852 i40e_vsi_clear_rings(vsi);
9853
9854 i40e_vsi_free_arrays(vsi, false);
9855 i40e_set_num_rings_in_vsi(vsi);
9856 ret = i40e_vsi_alloc_arrays(vsi, false);
9857 if (ret)
9858 goto err_vsi;
9859
9860 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9861 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009862 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009863 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009864 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009865 goto err_vsi;
9866 }
9867 vsi->base_queue = ret;
9868
9869 /* Update the FW view of the VSI. Force a reset of TC and queue
9870 * layout configurations.
9871 */
9872 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9873 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9874 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9875 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009876 if (vsi->type == I40E_VSI_MAIN)
9877 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009878
9879 /* assign it some queues */
9880 ret = i40e_alloc_rings(vsi);
9881 if (ret)
9882 goto err_rings;
9883
9884 /* map all of the rings to the q_vectors */
9885 i40e_vsi_map_rings_to_vectors(vsi);
9886 return vsi;
9887
9888err_rings:
9889 i40e_vsi_free_q_vectors(vsi);
9890 if (vsi->netdev_registered) {
9891 vsi->netdev_registered = false;
9892 unregister_netdev(vsi->netdev);
9893 free_netdev(vsi->netdev);
9894 vsi->netdev = NULL;
9895 }
9896 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9897err_vsi:
9898 i40e_vsi_clear(vsi);
9899 return NULL;
9900}
9901
9902/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009903 * i40e_vsi_setup - Set up a VSI by a given type
9904 * @pf: board private structure
9905 * @type: VSI type
9906 * @uplink_seid: the switch element to link to
9907 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9908 *
9909 * This allocates the sw VSI structure and its queue resources, then add a VSI
9910 * to the identified VEB.
9911 *
9912 * Returns pointer to the successfully allocated and configure VSI sw struct on
9913 * success, otherwise returns NULL on failure.
9914 **/
9915struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9916 u16 uplink_seid, u32 param1)
9917{
9918 struct i40e_vsi *vsi = NULL;
9919 struct i40e_veb *veb = NULL;
9920 int ret, i;
9921 int v_idx;
9922
9923 /* The requested uplink_seid must be either
9924 * - the PF's port seid
9925 * no VEB is needed because this is the PF
9926 * or this is a Flow Director special case VSI
9927 * - seid of an existing VEB
9928 * - seid of a VSI that owns an existing VEB
9929 * - seid of a VSI that doesn't own a VEB
9930 * a new VEB is created and the VSI becomes the owner
9931 * - seid of the PF VSI, which is what creates the first VEB
9932 * this is a special case of the previous
9933 *
9934 * Find which uplink_seid we were given and create a new VEB if needed
9935 */
9936 for (i = 0; i < I40E_MAX_VEB; i++) {
9937 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9938 veb = pf->veb[i];
9939 break;
9940 }
9941 }
9942
9943 if (!veb && uplink_seid != pf->mac_seid) {
9944
Mitch Williams505682c2014-05-20 08:01:37 +00009945 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009946 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9947 vsi = pf->vsi[i];
9948 break;
9949 }
9950 }
9951 if (!vsi) {
9952 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9953 uplink_seid);
9954 return NULL;
9955 }
9956
9957 if (vsi->uplink_seid == pf->mac_seid)
9958 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9959 vsi->tc_config.enabled_tc);
9960 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9961 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9962 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009963 if (veb) {
9964 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9965 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009966 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009967 return NULL;
9968 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009969 /* We come up by default in VEPA mode if SRIOV is not
9970 * already enabled, in which case we can't force VEPA
9971 * mode.
9972 */
9973 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9974 veb->bridge_mode = BRIDGE_MODE_VEPA;
9975 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9976 }
Neerav Parikh51616012015-02-06 08:52:14 +00009977 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009978 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009979 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9980 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9981 veb = pf->veb[i];
9982 }
9983 if (!veb) {
9984 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9985 return NULL;
9986 }
9987
9988 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9989 uplink_seid = veb->seid;
9990 }
9991
9992 /* get vsi sw struct */
9993 v_idx = i40e_vsi_mem_alloc(pf, type);
9994 if (v_idx < 0)
9995 goto err_alloc;
9996 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009997 if (!vsi)
9998 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009999 vsi->type = type;
10000 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10001
10002 if (type == I40E_VSI_MAIN)
10003 pf->lan_vsi = v_idx;
10004 else if (type == I40E_VSI_SRIOV)
10005 vsi->vf_id = param1;
10006 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010007 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
10008 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010009 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010010 dev_info(&pf->pdev->dev,
10011 "failed to get tracking for %d queues for VSI %d err=%d\n",
10012 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010013 goto err_vsi;
10014 }
10015 vsi->base_queue = ret;
10016
10017 /* get a VSI from the hardware */
10018 vsi->uplink_seid = uplink_seid;
10019 ret = i40e_add_vsi(vsi);
10020 if (ret)
10021 goto err_vsi;
10022
10023 switch (vsi->type) {
10024 /* setup the netdev if needed */
10025 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010026 /* Apply relevant filters if a platform-specific mac
10027 * address was selected.
10028 */
10029 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10030 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10031 if (ret) {
10032 dev_warn(&pf->pdev->dev,
10033 "could not set up macaddr; err %d\n",
10034 ret);
10035 }
10036 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -070010038 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010039 ret = i40e_config_netdev(vsi);
10040 if (ret)
10041 goto err_netdev;
10042 ret = register_netdev(vsi->netdev);
10043 if (ret)
10044 goto err_netdev;
10045 vsi->netdev_registered = true;
10046 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010047#ifdef CONFIG_I40E_DCB
10048 /* Setup DCB netlink interface */
10049 i40e_dcbnl_setup(vsi);
10050#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010051 /* fall through */
10052
10053 case I40E_VSI_FDIR:
10054 /* set up vectors and rings if needed */
10055 ret = i40e_vsi_setup_vectors(vsi);
10056 if (ret)
10057 goto err_msix;
10058
10059 ret = i40e_alloc_rings(vsi);
10060 if (ret)
10061 goto err_rings;
10062
10063 /* map all of the rings to the q_vectors */
10064 i40e_vsi_map_rings_to_vectors(vsi);
10065
10066 i40e_vsi_reset_stats(vsi);
10067 break;
10068
10069 default:
10070 /* no netdev or rings for the other VSI types */
10071 break;
10072 }
10073
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -040010074 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10075 (vsi->type == I40E_VSI_VMDQ2)) {
10076 ret = i40e_vsi_config_rss(vsi);
10077 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010078 return vsi;
10079
10080err_rings:
10081 i40e_vsi_free_q_vectors(vsi);
10082err_msix:
10083 if (vsi->netdev_registered) {
10084 vsi->netdev_registered = false;
10085 unregister_netdev(vsi->netdev);
10086 free_netdev(vsi->netdev);
10087 vsi->netdev = NULL;
10088 }
10089err_netdev:
10090 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10091err_vsi:
10092 i40e_vsi_clear(vsi);
10093err_alloc:
10094 return NULL;
10095}
10096
10097/**
10098 * i40e_veb_get_bw_info - Query VEB BW information
10099 * @veb: the veb to query
10100 *
10101 * Query the Tx scheduler BW configuration data for given VEB
10102 **/
10103static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10104{
10105 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10106 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10107 struct i40e_pf *pf = veb->pf;
10108 struct i40e_hw *hw = &pf->hw;
10109 u32 tc_bw_max;
10110 int ret = 0;
10111 int i;
10112
10113 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10114 &bw_data, NULL);
10115 if (ret) {
10116 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010117 "query veb bw config failed, err %s aq_err %s\n",
10118 i40e_stat_str(&pf->hw, ret),
10119 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010120 goto out;
10121 }
10122
10123 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10124 &ets_data, NULL);
10125 if (ret) {
10126 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010127 "query veb bw ets config failed, err %s aq_err %s\n",
10128 i40e_stat_str(&pf->hw, ret),
10129 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010130 goto out;
10131 }
10132
10133 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10134 veb->bw_max_quanta = ets_data.tc_bw_max;
10135 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010136 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010137 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10138 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10139 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10140 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10141 veb->bw_tc_limit_credits[i] =
10142 le16_to_cpu(bw_data.tc_bw_limits[i]);
10143 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10144 }
10145
10146out:
10147 return ret;
10148}
10149
10150/**
10151 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10152 * @pf: board private structure
10153 *
10154 * On error: returns error code (negative)
10155 * On success: returns vsi index in PF (positive)
10156 **/
10157static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10158{
10159 int ret = -ENOENT;
10160 struct i40e_veb *veb;
10161 int i;
10162
10163 /* Need to protect the allocation of switch elements at the PF level */
10164 mutex_lock(&pf->switch_mutex);
10165
10166 /* VEB list may be fragmented if VEB creation/destruction has
10167 * been happening. We can afford to do a quick scan to look
10168 * for any free slots in the list.
10169 *
10170 * find next empty veb slot, looping back around if necessary
10171 */
10172 i = 0;
10173 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10174 i++;
10175 if (i >= I40E_MAX_VEB) {
10176 ret = -ENOMEM;
10177 goto err_alloc_veb; /* out of VEB slots! */
10178 }
10179
10180 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10181 if (!veb) {
10182 ret = -ENOMEM;
10183 goto err_alloc_veb;
10184 }
10185 veb->pf = pf;
10186 veb->idx = i;
10187 veb->enabled_tc = 1;
10188
10189 pf->veb[i] = veb;
10190 ret = i;
10191err_alloc_veb:
10192 mutex_unlock(&pf->switch_mutex);
10193 return ret;
10194}
10195
10196/**
10197 * i40e_switch_branch_release - Delete a branch of the switch tree
10198 * @branch: where to start deleting
10199 *
10200 * This uses recursion to find the tips of the branch to be
10201 * removed, deleting until we get back to and can delete this VEB.
10202 **/
10203static void i40e_switch_branch_release(struct i40e_veb *branch)
10204{
10205 struct i40e_pf *pf = branch->pf;
10206 u16 branch_seid = branch->seid;
10207 u16 veb_idx = branch->idx;
10208 int i;
10209
10210 /* release any VEBs on this VEB - RECURSION */
10211 for (i = 0; i < I40E_MAX_VEB; i++) {
10212 if (!pf->veb[i])
10213 continue;
10214 if (pf->veb[i]->uplink_seid == branch->seid)
10215 i40e_switch_branch_release(pf->veb[i]);
10216 }
10217
10218 /* Release the VSIs on this VEB, but not the owner VSI.
10219 *
10220 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10221 * the VEB itself, so don't use (*branch) after this loop.
10222 */
Mitch Williams505682c2014-05-20 08:01:37 +000010223 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010224 if (!pf->vsi[i])
10225 continue;
10226 if (pf->vsi[i]->uplink_seid == branch_seid &&
10227 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10228 i40e_vsi_release(pf->vsi[i]);
10229 }
10230 }
10231
10232 /* There's one corner case where the VEB might not have been
10233 * removed, so double check it here and remove it if needed.
10234 * This case happens if the veb was created from the debugfs
10235 * commands and no VSIs were added to it.
10236 */
10237 if (pf->veb[veb_idx])
10238 i40e_veb_release(pf->veb[veb_idx]);
10239}
10240
10241/**
10242 * i40e_veb_clear - remove veb struct
10243 * @veb: the veb to remove
10244 **/
10245static void i40e_veb_clear(struct i40e_veb *veb)
10246{
10247 if (!veb)
10248 return;
10249
10250 if (veb->pf) {
10251 struct i40e_pf *pf = veb->pf;
10252
10253 mutex_lock(&pf->switch_mutex);
10254 if (pf->veb[veb->idx] == veb)
10255 pf->veb[veb->idx] = NULL;
10256 mutex_unlock(&pf->switch_mutex);
10257 }
10258
10259 kfree(veb);
10260}
10261
10262/**
10263 * i40e_veb_release - Delete a VEB and free its resources
10264 * @veb: the VEB being removed
10265 **/
10266void i40e_veb_release(struct i40e_veb *veb)
10267{
10268 struct i40e_vsi *vsi = NULL;
10269 struct i40e_pf *pf;
10270 int i, n = 0;
10271
10272 pf = veb->pf;
10273
10274 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010275 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010276 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10277 n++;
10278 vsi = pf->vsi[i];
10279 }
10280 }
10281 if (n != 1) {
10282 dev_info(&pf->pdev->dev,
10283 "can't remove VEB %d with %d VSIs left\n",
10284 veb->seid, n);
10285 return;
10286 }
10287
10288 /* move the remaining VSI to uplink veb */
10289 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10290 if (veb->uplink_seid) {
10291 vsi->uplink_seid = veb->uplink_seid;
10292 if (veb->uplink_seid == pf->mac_seid)
10293 vsi->veb_idx = I40E_NO_VEB;
10294 else
10295 vsi->veb_idx = veb->veb_idx;
10296 } else {
10297 /* floating VEB */
10298 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10299 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10300 }
10301
10302 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10303 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010304}
10305
10306/**
10307 * i40e_add_veb - create the VEB in the switch
10308 * @veb: the VEB to be instantiated
10309 * @vsi: the controlling VSI
10310 **/
10311static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10312{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010313 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010314 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010315 int ret;
10316
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010317 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010318 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010319 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010320
10321 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010322 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010323 dev_info(&pf->pdev->dev,
10324 "couldn't add VEB, err %s aq_err %s\n",
10325 i40e_stat_str(&pf->hw, ret),
10326 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010327 return -EPERM;
10328 }
10329
10330 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010331 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010332 &veb->stats_idx, NULL, NULL, NULL);
10333 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010334 dev_info(&pf->pdev->dev,
10335 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10336 i40e_stat_str(&pf->hw, ret),
10337 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010338 return -EPERM;
10339 }
10340 ret = i40e_veb_get_bw_info(veb);
10341 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010342 dev_info(&pf->pdev->dev,
10343 "couldn't get VEB bw info, err %s aq_err %s\n",
10344 i40e_stat_str(&pf->hw, ret),
10345 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10346 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010347 return -ENOENT;
10348 }
10349
10350 vsi->uplink_seid = veb->seid;
10351 vsi->veb_idx = veb->idx;
10352 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10353
10354 return 0;
10355}
10356
10357/**
10358 * i40e_veb_setup - Set up a VEB
10359 * @pf: board private structure
10360 * @flags: VEB setup flags
10361 * @uplink_seid: the switch element to link to
10362 * @vsi_seid: the initial VSI seid
10363 * @enabled_tc: Enabled TC bit-map
10364 *
10365 * This allocates the sw VEB structure and links it into the switch
10366 * It is possible and legal for this to be a duplicate of an already
10367 * existing VEB. It is also possible for both uplink and vsi seids
10368 * to be zero, in order to create a floating VEB.
10369 *
10370 * Returns pointer to the successfully allocated VEB sw struct on
10371 * success, otherwise returns NULL on failure.
10372 **/
10373struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10374 u16 uplink_seid, u16 vsi_seid,
10375 u8 enabled_tc)
10376{
10377 struct i40e_veb *veb, *uplink_veb = NULL;
10378 int vsi_idx, veb_idx;
10379 int ret;
10380
10381 /* if one seid is 0, the other must be 0 to create a floating relay */
10382 if ((uplink_seid == 0 || vsi_seid == 0) &&
10383 (uplink_seid + vsi_seid != 0)) {
10384 dev_info(&pf->pdev->dev,
10385 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10386 uplink_seid, vsi_seid);
10387 return NULL;
10388 }
10389
10390 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010391 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010392 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10393 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010394 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010395 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10396 vsi_seid);
10397 return NULL;
10398 }
10399
10400 if (uplink_seid && uplink_seid != pf->mac_seid) {
10401 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10402 if (pf->veb[veb_idx] &&
10403 pf->veb[veb_idx]->seid == uplink_seid) {
10404 uplink_veb = pf->veb[veb_idx];
10405 break;
10406 }
10407 }
10408 if (!uplink_veb) {
10409 dev_info(&pf->pdev->dev,
10410 "uplink seid %d not found\n", uplink_seid);
10411 return NULL;
10412 }
10413 }
10414
10415 /* get veb sw struct */
10416 veb_idx = i40e_veb_mem_alloc(pf);
10417 if (veb_idx < 0)
10418 goto err_alloc;
10419 veb = pf->veb[veb_idx];
10420 veb->flags = flags;
10421 veb->uplink_seid = uplink_seid;
10422 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10423 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10424
10425 /* create the VEB in the switch */
10426 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10427 if (ret)
10428 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010429 if (vsi_idx == pf->lan_vsi)
10430 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010431
10432 return veb;
10433
10434err_veb:
10435 i40e_veb_clear(veb);
10436err_alloc:
10437 return NULL;
10438}
10439
10440/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010441 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010442 * @pf: board private structure
10443 * @ele: element we are building info from
10444 * @num_reported: total number of elements
10445 * @printconfig: should we print the contents
10446 *
10447 * helper function to assist in extracting a few useful SEID values.
10448 **/
10449static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10450 struct i40e_aqc_switch_config_element_resp *ele,
10451 u16 num_reported, bool printconfig)
10452{
10453 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10454 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10455 u8 element_type = ele->element_type;
10456 u16 seid = le16_to_cpu(ele->seid);
10457
10458 if (printconfig)
10459 dev_info(&pf->pdev->dev,
10460 "type=%d seid=%d uplink=%d downlink=%d\n",
10461 element_type, seid, uplink_seid, downlink_seid);
10462
10463 switch (element_type) {
10464 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10465 pf->mac_seid = seid;
10466 break;
10467 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10468 /* Main VEB? */
10469 if (uplink_seid != pf->mac_seid)
10470 break;
10471 if (pf->lan_veb == I40E_NO_VEB) {
10472 int v;
10473
10474 /* find existing or else empty VEB */
10475 for (v = 0; v < I40E_MAX_VEB; v++) {
10476 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10477 pf->lan_veb = v;
10478 break;
10479 }
10480 }
10481 if (pf->lan_veb == I40E_NO_VEB) {
10482 v = i40e_veb_mem_alloc(pf);
10483 if (v < 0)
10484 break;
10485 pf->lan_veb = v;
10486 }
10487 }
10488
10489 pf->veb[pf->lan_veb]->seid = seid;
10490 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10491 pf->veb[pf->lan_veb]->pf = pf;
10492 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10493 break;
10494 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10495 if (num_reported != 1)
10496 break;
10497 /* This is immediately after a reset so we can assume this is
10498 * the PF's VSI
10499 */
10500 pf->mac_seid = uplink_seid;
10501 pf->pf_seid = downlink_seid;
10502 pf->main_vsi_seid = seid;
10503 if (printconfig)
10504 dev_info(&pf->pdev->dev,
10505 "pf_seid=%d main_vsi_seid=%d\n",
10506 pf->pf_seid, pf->main_vsi_seid);
10507 break;
10508 case I40E_SWITCH_ELEMENT_TYPE_PF:
10509 case I40E_SWITCH_ELEMENT_TYPE_VF:
10510 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10511 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10512 case I40E_SWITCH_ELEMENT_TYPE_PE:
10513 case I40E_SWITCH_ELEMENT_TYPE_PA:
10514 /* ignore these for now */
10515 break;
10516 default:
10517 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10518 element_type, seid);
10519 break;
10520 }
10521}
10522
10523/**
10524 * i40e_fetch_switch_configuration - Get switch config from firmware
10525 * @pf: board private structure
10526 * @printconfig: should we print the contents
10527 *
10528 * Get the current switch configuration from the device and
10529 * extract a few useful SEID values.
10530 **/
10531int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10532{
10533 struct i40e_aqc_get_switch_config_resp *sw_config;
10534 u16 next_seid = 0;
10535 int ret = 0;
10536 u8 *aq_buf;
10537 int i;
10538
10539 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10540 if (!aq_buf)
10541 return -ENOMEM;
10542
10543 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10544 do {
10545 u16 num_reported, num_total;
10546
10547 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10548 I40E_AQ_LARGE_BUF,
10549 &next_seid, NULL);
10550 if (ret) {
10551 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010552 "get switch config failed err %s aq_err %s\n",
10553 i40e_stat_str(&pf->hw, ret),
10554 i40e_aq_str(&pf->hw,
10555 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010556 kfree(aq_buf);
10557 return -ENOENT;
10558 }
10559
10560 num_reported = le16_to_cpu(sw_config->header.num_reported);
10561 num_total = le16_to_cpu(sw_config->header.num_total);
10562
10563 if (printconfig)
10564 dev_info(&pf->pdev->dev,
10565 "header: %d reported %d total\n",
10566 num_reported, num_total);
10567
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010568 for (i = 0; i < num_reported; i++) {
10569 struct i40e_aqc_switch_config_element_resp *ele =
10570 &sw_config->element[i];
10571
10572 i40e_setup_pf_switch_element(pf, ele, num_reported,
10573 printconfig);
10574 }
10575 } while (next_seid != 0);
10576
10577 kfree(aq_buf);
10578 return ret;
10579}
10580
10581/**
10582 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10583 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010584 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010585 *
10586 * Returns 0 on success, negative value on failure
10587 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010588static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010589{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010590 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010591 int ret;
10592
10593 /* find out what's out there already */
10594 ret = i40e_fetch_switch_configuration(pf, false);
10595 if (ret) {
10596 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010597 "couldn't fetch switch config, err %s aq_err %s\n",
10598 i40e_stat_str(&pf->hw, ret),
10599 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010600 return ret;
10601 }
10602 i40e_pf_reset_stats(pf);
10603
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010604 /* set the switch config bit for the whole device to
10605 * support limited promisc or true promisc
10606 * when user requests promisc. The default is limited
10607 * promisc.
10608 */
10609
10610 if ((pf->hw.pf_id == 0) &&
10611 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10612 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10613
10614 if (pf->hw.pf_id == 0) {
10615 u16 valid_flags;
10616
10617 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10618 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10619 NULL);
10620 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10621 dev_info(&pf->pdev->dev,
10622 "couldn't set switch config bits, err %s aq_err %s\n",
10623 i40e_stat_str(&pf->hw, ret),
10624 i40e_aq_str(&pf->hw,
10625 pf->hw.aq.asq_last_status));
10626 /* not a fatal problem, just keep going */
10627 }
10628 }
10629
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010630 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010631 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010632 struct i40e_vsi *vsi = NULL;
10633 u16 uplink_seid;
10634
10635 /* Set up the PF VSI associated with the PF's main VSI
10636 * that is already in the HW switch
10637 */
10638 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10639 uplink_seid = pf->veb[pf->lan_veb]->seid;
10640 else
10641 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010642 if (pf->lan_vsi == I40E_NO_VSI)
10643 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10644 else if (reinit)
10645 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010646 if (!vsi) {
10647 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10648 i40e_fdir_teardown(pf);
10649 return -EAGAIN;
10650 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010651 } else {
10652 /* force a reset of TC and queue layout configurations */
10653 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010654
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010655 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10656 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10657 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10658 }
10659 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10660
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010661 i40e_fdir_sb_setup(pf);
10662
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010663 /* Setup static PF queue filter control settings */
10664 ret = i40e_setup_pf_filter_control(pf);
10665 if (ret) {
10666 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10667 ret);
10668 /* Failure here should not stop continuing other steps */
10669 }
10670
10671 /* enable RSS in the HW, even for only one queue, as the stack can use
10672 * the hash
10673 */
10674 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010675 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010676
10677 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010678 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010679 i40e_link_event(pf);
10680
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010681 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010682 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10683 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010684
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010685 i40e_ptp_init(pf);
10686
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010687 return ret;
10688}
10689
10690/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010691 * i40e_determine_queue_usage - Work out queue distribution
10692 * @pf: board private structure
10693 **/
10694static void i40e_determine_queue_usage(struct i40e_pf *pf)
10695{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010696 int queues_left;
10697
10698 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010699#ifdef I40E_FCOE
10700 pf->num_fcoe_qps = 0;
10701#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010702
10703 /* Find the max queues to be put into basic use. We'll always be
10704 * using TC0, whether or not DCB is running, and TC0 will get the
10705 * big RSS set.
10706 */
10707 queues_left = pf->hw.func_caps.num_tx_qp;
10708
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010709 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010710 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010711 /* one qp for PF, no queues for anything else */
10712 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010713 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010714
10715 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010716 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010717 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010718#ifdef I40E_FCOE
10719 I40E_FLAG_FCOE_ENABLED |
10720#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010721 I40E_FLAG_FD_SB_ENABLED |
10722 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010723 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010724 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010725 I40E_FLAG_SRIOV_ENABLED |
10726 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010727 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10728 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010729 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010730 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010731 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010732 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010733 queues_left -= pf->num_lan_qps;
10734
10735 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010736 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010737#ifdef I40E_FCOE
10738 I40E_FLAG_FCOE_ENABLED |
10739#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010740 I40E_FLAG_FD_SB_ENABLED |
10741 I40E_FLAG_FD_ATR_ENABLED |
10742 I40E_FLAG_DCB_ENABLED |
10743 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010744 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010745 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010746 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010747 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010748 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10749 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010750 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10751 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010752 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10753 num_online_cpus());
10754 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10755 pf->hw.func_caps.num_tx_qp);
10756
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010757 queues_left -= pf->num_lan_qps;
10758 }
10759
Vasu Dev38e00432014-08-01 13:27:03 -070010760#ifdef I40E_FCOE
10761 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10762 if (I40E_DEFAULT_FCOE <= queues_left) {
10763 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10764 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10765 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10766 } else {
10767 pf->num_fcoe_qps = 0;
10768 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10769 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10770 }
10771
10772 queues_left -= pf->num_fcoe_qps;
10773 }
10774
10775#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010776 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10777 if (queues_left > 1) {
10778 queues_left -= 1; /* save 1 queue for FD */
10779 } else {
10780 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10781 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10782 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010783 }
10784
10785 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10786 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010787 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10788 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010789 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10790 }
10791
10792 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10793 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10794 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10795 (queues_left / pf->num_vmdq_qps));
10796 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10797 }
10798
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010799 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010800 dev_dbg(&pf->pdev->dev,
10801 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10802 pf->hw.func_caps.num_tx_qp,
10803 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010804 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10805 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10806 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010807#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010808 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010809#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010810}
10811
10812/**
10813 * i40e_setup_pf_filter_control - Setup PF static filter control
10814 * @pf: PF to be setup
10815 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010816 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010817 * settings. If PE/FCoE are enabled then it will also set the per PF
10818 * based filter sizes required for them. It also enables Flow director,
10819 * ethertype and macvlan type filter settings for the pf.
10820 *
10821 * Returns 0 on success, negative on failure
10822 **/
10823static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10824{
10825 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10826
10827 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10828
10829 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010830 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010831 settings->enable_fdir = true;
10832
10833 /* Ethtype and MACVLAN filters enabled for PF */
10834 settings->enable_ethtype = true;
10835 settings->enable_macvlan = true;
10836
10837 if (i40e_set_filter_control(&pf->hw, settings))
10838 return -ENOENT;
10839
10840 return 0;
10841}
10842
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010843#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010844#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010845static void i40e_print_features(struct i40e_pf *pf)
10846{
10847 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010848 char *buf;
10849 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010850
Joe Perches3b195842015-12-03 04:20:57 -080010851 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10852 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010853 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010854
Joe Perches3b195842015-12-03 04:20:57 -080010855 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010856#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010857 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010858#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010859 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010860 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010861 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010862 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010863 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010864 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010865 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010866 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010867 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10868 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010869 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010870 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010871 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010872 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010873 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010874 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010875 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010876#ifdef I40E_FCOE
10877 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010878 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010879#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010880 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010881 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010882 else
Joe Perches3b195842015-12-03 04:20:57 -080010883 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010884
Joe Perches3b195842015-12-03 04:20:57 -080010885 dev_info(&pf->pdev->dev, "%s\n", buf);
10886 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010887 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010888}
10889
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010890/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010891 * i40e_get_platform_mac_addr - get platform-specific MAC address
10892 *
10893 * @pdev: PCI device information struct
10894 * @pf: board private structure
10895 *
10896 * Look up the MAC address in Open Firmware on systems that support it,
10897 * and use IDPROM on SPARC if no OF address is found. On return, the
10898 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10899 * has been selected.
10900 **/
10901static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10902{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010903 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010904 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010905 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010906}
10907
10908/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010909 * i40e_probe - Device initialization routine
10910 * @pdev: PCI device information struct
10911 * @ent: entry in i40e_pci_tbl
10912 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010913 * i40e_probe initializes a PF identified by a pci_dev structure.
10914 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010915 * and a hardware reset occur.
10916 *
10917 * Returns 0 on success, negative on failure
10918 **/
10919static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10920{
Catherine Sullivane8278452015-02-06 08:52:08 +000010921 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010922 struct i40e_pf *pf;
10923 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010924 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010925 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010926 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010927 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010928 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010929 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010930 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010931
10932 err = pci_enable_device_mem(pdev);
10933 if (err)
10934 return err;
10935
10936 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010937 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010938 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010939 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10940 if (err) {
10941 dev_err(&pdev->dev,
10942 "DMA configuration failed: 0x%x\n", err);
10943 goto err_dma;
10944 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010945 }
10946
10947 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010948 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010949 if (err) {
10950 dev_info(&pdev->dev,
10951 "pci_request_selected_regions failed %d\n", err);
10952 goto err_pci_reg;
10953 }
10954
10955 pci_enable_pcie_error_reporting(pdev);
10956 pci_set_master(pdev);
10957
10958 /* Now that we have a PCI connection, we need to do the
10959 * low level device setup. This is primarily setting up
10960 * the Admin Queue structures and then querying for the
10961 * device's current profile information.
10962 */
10963 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10964 if (!pf) {
10965 err = -ENOMEM;
10966 goto err_pf_alloc;
10967 }
10968 pf->next_vsi = 0;
10969 pf->pdev = pdev;
10970 set_bit(__I40E_DOWN, &pf->state);
10971
10972 hw = &pf->hw;
10973 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010974
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010975 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10976 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010977
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010978 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010979 if (!hw->hw_addr) {
10980 err = -EIO;
10981 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10982 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010983 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010984 goto err_ioremap;
10985 }
10986 hw->vendor_id = pdev->vendor;
10987 hw->device_id = pdev->device;
10988 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10989 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10990 hw->subsystem_device_id = pdev->subsystem_device;
10991 hw->bus.device = PCI_SLOT(pdev->devfn);
10992 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010993 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010994
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010995 /* set up the locks for the AQ, do this only once in probe
10996 * and destroy them only once in remove
10997 */
10998 mutex_init(&hw->aq.asq_mutex);
10999 mutex_init(&hw->aq.arq_mutex);
11000
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011001 pf->msg_enable = netif_msg_init(debug,
11002 NETIF_MSG_DRV |
11003 NETIF_MSG_PROBE |
11004 NETIF_MSG_LINK);
11005 if (debug < -1)
11006 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011007
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011008 /* do a special CORER for clearing PXE mode once at init */
11009 if (hw->revision_id == 0 &&
11010 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11011 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11012 i40e_flush(hw);
11013 msleep(200);
11014 pf->corer_count++;
11015
11016 i40e_clear_pxe_mode(hw);
11017 }
11018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011019 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011020 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011021 err = i40e_pf_reset(hw);
11022 if (err) {
11023 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11024 goto err_pf_reset;
11025 }
11026 pf->pfr_count++;
11027
11028 hw->aq.num_arq_entries = I40E_AQ_LEN;
11029 hw->aq.num_asq_entries = I40E_AQ_LEN;
11030 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11031 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11032 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011033
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011034 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011035 "%s-%s:misc",
11036 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011037
11038 err = i40e_init_shared_code(hw);
11039 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011040 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11041 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011042 goto err_pf_reset;
11043 }
11044
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011045 /* set up a default setting for link flow control */
11046 pf->hw.fc.requested_mode = I40E_FC_NONE;
11047
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011048 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011049 if (err) {
11050 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11051 dev_info(&pdev->dev,
11052 "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");
11053 else
11054 dev_info(&pdev->dev,
11055 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11056
11057 goto err_pf_reset;
11058 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011059
Shannon Nelson6dec1012015-09-28 14:12:30 -040011060 /* provide nvm, fw, api versions */
11061 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11062 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11063 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11064 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011065
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011066 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11067 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011068 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011069 "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");
11070 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11071 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011072 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011073 "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 +000011074
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011075 i40e_verify_eeprom(pf);
11076
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011077 /* Rev 0 hardware was never productized */
11078 if (hw->revision_id < 1)
11079 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");
11080
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011081 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011082 err = i40e_get_capabilities(pf);
11083 if (err)
11084 goto err_adminq_setup;
11085
11086 err = i40e_sw_init(pf);
11087 if (err) {
11088 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11089 goto err_sw_init;
11090 }
11091
11092 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11093 hw->func_caps.num_rx_qp,
11094 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11095 if (err) {
11096 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11097 goto err_init_lan_hmc;
11098 }
11099
11100 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11101 if (err) {
11102 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11103 err = -ENOENT;
11104 goto err_configure_lan_hmc;
11105 }
11106
Neerav Parikhb686ece2014-12-14 01:55:11 +000011107 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11108 * Ignore error return codes because if it was already disabled via
11109 * hardware settings this will fail
11110 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011111 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011112 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11113 i40e_aq_stop_lldp(hw, true, NULL);
11114 }
11115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011116 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011117 /* allow a platform config to override the HW addr */
11118 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011119 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011120 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11121 err = -EIO;
11122 goto err_mac_addr;
11123 }
11124 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011125 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011126 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11127 if (is_valid_ether_addr(hw->mac.port_addr))
11128 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011129#ifdef I40E_FCOE
11130 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11131 if (err)
11132 dev_info(&pdev->dev,
11133 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11134 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11135 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11136 hw->mac.san_addr);
11137 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11138 }
11139 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11140#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011141
11142 pci_set_drvdata(pdev, pf);
11143 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011144#ifdef CONFIG_I40E_DCB
11145 err = i40e_init_pf_dcb(pf);
11146 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011147 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011148 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011149 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011150 }
11151#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011152
11153 /* set up periodic task facility */
11154 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11155 pf->service_timer_period = HZ;
11156
11157 INIT_WORK(&pf->service_task, i40e_service_task);
11158 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11159 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011160
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011161 /* NVM bit on means WoL disabled for the port */
11162 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011163 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011164 pf->wol_en = false;
11165 else
11166 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011167 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11168
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011169 /* set up the main switch operations */
11170 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011171 err = i40e_init_interrupt_scheme(pf);
11172 if (err)
11173 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011174
Mitch Williams505682c2014-05-20 08:01:37 +000011175 /* The number of VSIs reported by the FW is the minimum guaranteed
11176 * to us; HW supports far more and we share the remaining pool with
11177 * the other PFs. We allocate space for more than the guarantee with
11178 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011179 */
Mitch Williams505682c2014-05-20 08:01:37 +000011180 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11181 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11182 else
11183 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11184
11185 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011186 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11187 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011188 if (!pf->vsi) {
11189 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011190 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011191 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011192
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011193#ifdef CONFIG_PCI_IOV
11194 /* prep for VF support */
11195 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11196 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11197 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11198 if (pci_num_vf(pdev))
11199 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11200 }
11201#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011202 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011203 if (err) {
11204 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11205 goto err_vsis;
11206 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011207
11208 /* Make sure flow control is set according to current settings */
11209 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11210 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11211 dev_dbg(&pf->pdev->dev,
11212 "Set fc with err %s aq_err %s on get_phy_cap\n",
11213 i40e_stat_str(hw, err),
11214 i40e_aq_str(hw, hw->aq.asq_last_status));
11215 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11216 dev_dbg(&pf->pdev->dev,
11217 "Set fc with err %s aq_err %s on set_phy_config\n",
11218 i40e_stat_str(hw, err),
11219 i40e_aq_str(hw, hw->aq.asq_last_status));
11220 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11221 dev_dbg(&pf->pdev->dev,
11222 "Set fc with err %s aq_err %s on get_link_info\n",
11223 i40e_stat_str(hw, err),
11224 i40e_aq_str(hw, hw->aq.asq_last_status));
11225
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011226 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011227 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011228 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11229 i40e_vsi_open(pf->vsi[i]);
11230 break;
11231 }
11232 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011233
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011234 /* The driver only wants link up/down and module qualification
11235 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011236 */
11237 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011238 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011239 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011240 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011241 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011242 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11243 i40e_stat_str(&pf->hw, err),
11244 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011245
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011246 /* Reconfigure hardware for allowing smaller MSS in the case
11247 * of TSO, so that we avoid the MDD being fired and causing
11248 * a reset in the case of small MSS+TSO.
11249 */
11250 val = rd32(hw, I40E_REG_MSS);
11251 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11252 val &= ~I40E_REG_MSS_MIN_MASK;
11253 val |= I40E_64BYTE_MSS;
11254 wr32(hw, I40E_REG_MSS, val);
11255 }
11256
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011257 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011258 msleep(75);
11259 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11260 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011261 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11262 i40e_stat_str(&pf->hw, err),
11263 i40e_aq_str(&pf->hw,
11264 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011265 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011266 /* The main driver is (mostly) up and happy. We need to set this state
11267 * before setting up the misc vector or we get a race and the vector
11268 * ends up disabled forever.
11269 */
11270 clear_bit(__I40E_DOWN, &pf->state);
11271
11272 /* In case of MSIX we are going to setup the misc vector right here
11273 * to handle admin queue events etc. In case of legacy and MSI
11274 * the misc functionality and queue processing is combined in
11275 * the same vector and that gets setup at open.
11276 */
11277 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11278 err = i40e_setup_misc_vector(pf);
11279 if (err) {
11280 dev_info(&pdev->dev,
11281 "setup of misc vector failed: %d\n", err);
11282 goto err_vsis;
11283 }
11284 }
11285
Greg Rosedf805f62014-04-04 04:43:16 +000011286#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011287 /* prep for VF support */
11288 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011289 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11290 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011291 /* disable link interrupts for VFs */
11292 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11293 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11294 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11295 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011296
11297 if (pci_num_vf(pdev)) {
11298 dev_info(&pdev->dev,
11299 "Active VFs found, allocating resources.\n");
11300 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11301 if (err)
11302 dev_info(&pdev->dev,
11303 "Error %d allocating resources for existing VFs\n",
11304 err);
11305 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011306 }
Greg Rosedf805f62014-04-04 04:43:16 +000011307#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011308
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011309 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11310 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11311 pf->num_iwarp_msix,
11312 I40E_IWARP_IRQ_PILE_ID);
11313 if (pf->iwarp_base_vector < 0) {
11314 dev_info(&pdev->dev,
11315 "failed to get tracking for %d vectors for IWARP err=%d\n",
11316 pf->num_iwarp_msix, pf->iwarp_base_vector);
11317 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11318 }
11319 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011320
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011321 i40e_dbg_pf_init(pf);
11322
11323 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011324 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011325
11326 /* since everything's happy, start the service_task timer */
11327 mod_timer(&pf->service_timer,
11328 round_jiffies(jiffies + pf->service_timer_period));
11329
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011330 /* add this PF to client device list and launch a client service task */
11331 err = i40e_lan_add_device(pf);
11332 if (err)
11333 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11334 err);
11335
Vasu Dev38e00432014-08-01 13:27:03 -070011336#ifdef I40E_FCOE
11337 /* create FCoE interface */
11338 i40e_fcoe_vsi_setup(pf);
11339
11340#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011341#define PCI_SPEED_SIZE 8
11342#define PCI_WIDTH_SIZE 8
11343 /* Devices on the IOSF bus do not have this information
11344 * and will report PCI Gen 1 x 1 by default so don't bother
11345 * checking them.
11346 */
11347 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11348 char speed[PCI_SPEED_SIZE] = "Unknown";
11349 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011350
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011351 /* Get the negotiated link width and speed from PCI config
11352 * space
11353 */
11354 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11355 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011356
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011357 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011358
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011359 switch (hw->bus.speed) {
11360 case i40e_bus_speed_8000:
11361 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11362 case i40e_bus_speed_5000:
11363 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11364 case i40e_bus_speed_2500:
11365 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11366 default:
11367 break;
11368 }
11369 switch (hw->bus.width) {
11370 case i40e_bus_width_pcie_x8:
11371 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11372 case i40e_bus_width_pcie_x4:
11373 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11374 case i40e_bus_width_pcie_x2:
11375 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11376 case i40e_bus_width_pcie_x1:
11377 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11378 default:
11379 break;
11380 }
11381
11382 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11383 speed, width);
11384
11385 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11386 hw->bus.speed < i40e_bus_speed_8000) {
11387 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11388 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11389 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011390 }
11391
Catherine Sullivane8278452015-02-06 08:52:08 +000011392 /* get the requested speeds from the fw */
11393 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11394 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011395 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11396 i40e_stat_str(&pf->hw, err),
11397 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011398 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11399
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011400 /* get the supported phy types from the fw */
11401 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11402 if (err)
11403 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11404 i40e_stat_str(&pf->hw, err),
11405 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011406
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011407 /* Add a filter to drop all Flow control frames from any VSI from being
11408 * transmitted. By doing so we stop a malicious VF from sending out
11409 * PAUSE or PFC frames and potentially controlling traffic for other
11410 * PF/VF VSIs.
11411 * The FW can still send Flow control frames if enabled.
11412 */
11413 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11414 pf->main_vsi_seid);
11415
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011416 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011417 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11418 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011419 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11420 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011421 /* print a string summarizing features */
11422 i40e_print_features(pf);
11423
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011424 return 0;
11425
11426 /* Unwind what we've done if something failed in the setup */
11427err_vsis:
11428 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011429 i40e_clear_interrupt_scheme(pf);
11430 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011431err_switch_setup:
11432 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011433 del_timer_sync(&pf->service_timer);
11434err_mac_addr:
11435err_configure_lan_hmc:
11436 (void)i40e_shutdown_lan_hmc(hw);
11437err_init_lan_hmc:
11438 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011439err_sw_init:
11440err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011441err_pf_reset:
11442 iounmap(hw->hw_addr);
11443err_ioremap:
11444 kfree(pf);
11445err_pf_alloc:
11446 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011447 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011448err_pci_reg:
11449err_dma:
11450 pci_disable_device(pdev);
11451 return err;
11452}
11453
11454/**
11455 * i40e_remove - Device removal routine
11456 * @pdev: PCI device information struct
11457 *
11458 * i40e_remove is called by the PCI subsystem to alert the driver
11459 * that is should release a PCI device. This could be caused by a
11460 * Hot-Plug event, or because the driver is going to be removed from
11461 * memory.
11462 **/
11463static void i40e_remove(struct pci_dev *pdev)
11464{
11465 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011466 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011467 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011468 int i;
11469
11470 i40e_dbg_pf_exit(pf);
11471
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011472 i40e_ptp_stop(pf);
11473
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011474 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011475 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11476 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011477
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011478 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011479 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011480 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011481 if (pf->service_timer.data)
11482 del_timer_sync(&pf->service_timer);
11483 if (pf->service_task.func)
11484 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011485
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011486 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11487 i40e_free_vfs(pf);
11488 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11489 }
11490
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011491 i40e_fdir_teardown(pf);
11492
11493 /* If there is a switch structure or any orphans, remove them.
11494 * This will leave only the PF's VSI remaining.
11495 */
11496 for (i = 0; i < I40E_MAX_VEB; i++) {
11497 if (!pf->veb[i])
11498 continue;
11499
11500 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11501 pf->veb[i]->uplink_seid == 0)
11502 i40e_switch_branch_release(pf->veb[i]);
11503 }
11504
11505 /* Now we can shutdown the PF's VSI, just before we kill
11506 * adminq and hmc.
11507 */
11508 if (pf->vsi[pf->lan_vsi])
11509 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11510
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011511 /* remove attached clients */
11512 ret_code = i40e_lan_del_device(pf);
11513 if (ret_code) {
11514 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11515 ret_code);
11516 }
11517
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011518 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011519 if (hw->hmc.hmc_obj) {
11520 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011521 if (ret_code)
11522 dev_warn(&pdev->dev,
11523 "Failed to destroy the HMC resources: %d\n",
11524 ret_code);
11525 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011526
11527 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011528 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011529
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011530 /* destroy the locks only once, here */
11531 mutex_destroy(&hw->aq.arq_mutex);
11532 mutex_destroy(&hw->aq.asq_mutex);
11533
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011534 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11535 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011536 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011537 if (pf->vsi[i]) {
11538 i40e_vsi_clear_rings(pf->vsi[i]);
11539 i40e_vsi_clear(pf->vsi[i]);
11540 pf->vsi[i] = NULL;
11541 }
11542 }
11543
11544 for (i = 0; i < I40E_MAX_VEB; i++) {
11545 kfree(pf->veb[i]);
11546 pf->veb[i] = NULL;
11547 }
11548
11549 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011550 kfree(pf->vsi);
11551
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011552 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011553 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011554 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011555
11556 pci_disable_pcie_error_reporting(pdev);
11557 pci_disable_device(pdev);
11558}
11559
11560/**
11561 * i40e_pci_error_detected - warning that something funky happened in PCI land
11562 * @pdev: PCI device information struct
11563 *
11564 * Called to warn that something happened and the error handling steps
11565 * are in progress. Allows the driver to quiesce things, be ready for
11566 * remediation.
11567 **/
11568static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11569 enum pci_channel_state error)
11570{
11571 struct i40e_pf *pf = pci_get_drvdata(pdev);
11572
11573 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11574
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011575 if (!pf) {
11576 dev_info(&pdev->dev,
11577 "Cannot recover - error happened during device probe\n");
11578 return PCI_ERS_RESULT_DISCONNECT;
11579 }
11580
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011581 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011582 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11583 rtnl_lock();
11584 i40e_prep_for_reset(pf);
11585 rtnl_unlock();
11586 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011587
11588 /* Request a slot reset */
11589 return PCI_ERS_RESULT_NEED_RESET;
11590}
11591
11592/**
11593 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11594 * @pdev: PCI device information struct
11595 *
11596 * Called to find if the driver can work with the device now that
11597 * the pci slot has been reset. If a basic connection seems good
11598 * (registers are readable and have sane content) then return a
11599 * happy little PCI_ERS_RESULT_xxx.
11600 **/
11601static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11602{
11603 struct i40e_pf *pf = pci_get_drvdata(pdev);
11604 pci_ers_result_t result;
11605 int err;
11606 u32 reg;
11607
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011608 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011609 if (pci_enable_device_mem(pdev)) {
11610 dev_info(&pdev->dev,
11611 "Cannot re-enable PCI device after reset.\n");
11612 result = PCI_ERS_RESULT_DISCONNECT;
11613 } else {
11614 pci_set_master(pdev);
11615 pci_restore_state(pdev);
11616 pci_save_state(pdev);
11617 pci_wake_from_d3(pdev, false);
11618
11619 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11620 if (reg == 0)
11621 result = PCI_ERS_RESULT_RECOVERED;
11622 else
11623 result = PCI_ERS_RESULT_DISCONNECT;
11624 }
11625
11626 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11627 if (err) {
11628 dev_info(&pdev->dev,
11629 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11630 err);
11631 /* non-fatal, continue */
11632 }
11633
11634 return result;
11635}
11636
11637/**
11638 * i40e_pci_error_resume - restart operations after PCI error recovery
11639 * @pdev: PCI device information struct
11640 *
11641 * Called to allow the driver to bring things back up after PCI error
11642 * and/or reset recovery has finished.
11643 **/
11644static void i40e_pci_error_resume(struct pci_dev *pdev)
11645{
11646 struct i40e_pf *pf = pci_get_drvdata(pdev);
11647
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011648 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011649 if (test_bit(__I40E_SUSPENDED, &pf->state))
11650 return;
11651
11652 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011653 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011654 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011655}
11656
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011657/**
11658 * i40e_shutdown - PCI callback for shutting down
11659 * @pdev: PCI device information struct
11660 **/
11661static void i40e_shutdown(struct pci_dev *pdev)
11662{
11663 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011664 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011665
11666 set_bit(__I40E_SUSPENDED, &pf->state);
11667 set_bit(__I40E_DOWN, &pf->state);
11668 rtnl_lock();
11669 i40e_prep_for_reset(pf);
11670 rtnl_unlock();
11671
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011672 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11673 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11674
Catherine Sullivan02b42492015-07-10 19:35:59 -040011675 del_timer_sync(&pf->service_timer);
11676 cancel_work_sync(&pf->service_task);
11677 i40e_fdir_teardown(pf);
11678
11679 rtnl_lock();
11680 i40e_prep_for_reset(pf);
11681 rtnl_unlock();
11682
11683 wr32(hw, I40E_PFPM_APM,
11684 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11685 wr32(hw, I40E_PFPM_WUFC,
11686 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11687
Shannon Nelsone1477582015-02-21 06:44:33 +000011688 i40e_clear_interrupt_scheme(pf);
11689
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011690 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011691 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011692 pci_set_power_state(pdev, PCI_D3hot);
11693 }
11694}
11695
11696#ifdef CONFIG_PM
11697/**
11698 * i40e_suspend - PCI callback for moving to D3
11699 * @pdev: PCI device information struct
11700 **/
11701static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11702{
11703 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011704 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011705 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011706
11707 set_bit(__I40E_SUSPENDED, &pf->state);
11708 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011709
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011710 rtnl_lock();
11711 i40e_prep_for_reset(pf);
11712 rtnl_unlock();
11713
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011714 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11715 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11716
Greg Roseb33d3b72016-05-16 10:26:37 -070011717 i40e_stop_misc_vector(pf);
11718
Greg Rose059ff692016-05-16 10:26:38 -070011719 retval = pci_save_state(pdev);
11720 if (retval)
11721 return retval;
11722
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011723 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011724 pci_set_power_state(pdev, PCI_D3hot);
11725
Greg Rose059ff692016-05-16 10:26:38 -070011726 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011727}
11728
11729/**
11730 * i40e_resume - PCI callback for waking up from D3
11731 * @pdev: PCI device information struct
11732 **/
11733static int i40e_resume(struct pci_dev *pdev)
11734{
11735 struct i40e_pf *pf = pci_get_drvdata(pdev);
11736 u32 err;
11737
11738 pci_set_power_state(pdev, PCI_D0);
11739 pci_restore_state(pdev);
11740 /* pci_restore_state() clears dev->state_saves, so
11741 * call pci_save_state() again to restore it.
11742 */
11743 pci_save_state(pdev);
11744
11745 err = pci_enable_device_mem(pdev);
11746 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011747 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011748 return err;
11749 }
11750 pci_set_master(pdev);
11751
11752 /* no wakeup events while running */
11753 pci_wake_from_d3(pdev, false);
11754
11755 /* handling the reset will rebuild the device state */
11756 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11757 clear_bit(__I40E_DOWN, &pf->state);
11758 rtnl_lock();
11759 i40e_reset_and_rebuild(pf, false);
11760 rtnl_unlock();
11761 }
11762
11763 return 0;
11764}
11765
11766#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011767static const struct pci_error_handlers i40e_err_handler = {
11768 .error_detected = i40e_pci_error_detected,
11769 .slot_reset = i40e_pci_error_slot_reset,
11770 .resume = i40e_pci_error_resume,
11771};
11772
11773static struct pci_driver i40e_driver = {
11774 .name = i40e_driver_name,
11775 .id_table = i40e_pci_tbl,
11776 .probe = i40e_probe,
11777 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011778#ifdef CONFIG_PM
11779 .suspend = i40e_suspend,
11780 .resume = i40e_resume,
11781#endif
11782 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011783 .err_handler = &i40e_err_handler,
11784 .sriov_configure = i40e_pci_sriov_configure,
11785};
11786
11787/**
11788 * i40e_init_module - Driver registration routine
11789 *
11790 * i40e_init_module is the first routine called when the driver is
11791 * loaded. All it does is register with the PCI subsystem.
11792 **/
11793static int __init i40e_init_module(void)
11794{
11795 pr_info("%s: %s - version %s\n", i40e_driver_name,
11796 i40e_driver_string, i40e_driver_version_str);
11797 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011798
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011799 /* we will see if single thread per module is enough for now,
11800 * it can't be any worse than using the system workqueue which
11801 * was already single threaded
11802 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011803 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11804 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011805 if (!i40e_wq) {
11806 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11807 return -ENOMEM;
11808 }
11809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011810 i40e_dbg_init();
11811 return pci_register_driver(&i40e_driver);
11812}
11813module_init(i40e_init_module);
11814
11815/**
11816 * i40e_exit_module - Driver exit cleanup routine
11817 *
11818 * i40e_exit_module is called just before the driver is removed
11819 * from memory.
11820 **/
11821static void __exit i40e_exit_module(void)
11822{
11823 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011824 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011825 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011826}
11827module_exit(i40e_exit_module);