blob: 806fd56bf0fd1b0fb6d297d7d6be62f3d647e4ff [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 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700412#ifdef I40E_FCOE
413struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
414 struct net_device *netdev,
415 struct rtnl_link_stats64 *stats)
416#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000417static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
418 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000419 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700420#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000421{
422 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000423 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000424 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000425 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
426 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000427
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000428 if (test_bit(__I40E_DOWN, &vsi->state))
429 return stats;
430
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800431 if (!vsi->tx_rings)
432 return stats;
433
Alexander Duyck980e9b12013-09-28 06:01:03 +0000434 rcu_read_lock();
435 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000436 u64 bytes, packets;
437 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000438
Alexander Duyck980e9b12013-09-28 06:01:03 +0000439 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
440 if (!tx_ring)
441 continue;
442
443 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700444 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000445 packets = tx_ring->stats.packets;
446 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700447 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000448
449 stats->tx_packets += packets;
450 stats->tx_bytes += bytes;
451 rx_ring = &tx_ring[1];
452
453 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700454 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 packets = rx_ring->stats.packets;
456 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700457 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458
459 stats->rx_packets += packets;
460 stats->rx_bytes += bytes;
461 }
462 rcu_read_unlock();
463
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000464 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000465 stats->multicast = vsi_stats->multicast;
466 stats->tx_errors = vsi_stats->tx_errors;
467 stats->tx_dropped = vsi_stats->tx_dropped;
468 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400469 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000470 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
471 stats->rx_length_errors = vsi_stats->rx_length_errors;
472
473 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000474}
475
476/**
477 * i40e_vsi_reset_stats - Resets all stats of the given vsi
478 * @vsi: the VSI to have its stats reset
479 **/
480void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
481{
482 struct rtnl_link_stats64 *ns;
483 int i;
484
485 if (!vsi)
486 return;
487
488 ns = i40e_get_vsi_stats_struct(vsi);
489 memset(ns, 0, sizeof(*ns));
490 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
491 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
492 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000493 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000494 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400495 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000496 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400497 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000498 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400499 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000500 sizeof(vsi->tx_rings[i]->stats));
501 memset(&vsi->tx_rings[i]->tx_stats, 0,
502 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000503 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000504 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000505 vsi->stat_offsets_loaded = false;
506}
507
508/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000509 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000510 * @pf: the PF to be reset
511 **/
512void i40e_pf_reset_stats(struct i40e_pf *pf)
513{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000514 int i;
515
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 memset(&pf->stats, 0, sizeof(pf->stats));
517 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
518 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000519
520 for (i = 0; i < I40E_MAX_VEB; i++) {
521 if (pf->veb[i]) {
522 memset(&pf->veb[i]->stats, 0,
523 sizeof(pf->veb[i]->stats));
524 memset(&pf->veb[i]->stats_offsets, 0,
525 sizeof(pf->veb[i]->stats_offsets));
526 pf->veb[i]->stat_offsets_loaded = false;
527 }
528 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700529 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000530}
531
532/**
533 * i40e_stat_update48 - read and update a 48 bit stat from the chip
534 * @hw: ptr to the hardware info
535 * @hireg: the high 32 bit reg to read
536 * @loreg: the low 32 bit reg to read
537 * @offset_loaded: has the initial offset been loaded yet
538 * @offset: ptr to current offset value
539 * @stat: ptr to the stat
540 *
541 * Since the device stats are not reset at PFReset, they likely will not
542 * be zeroed when the driver starts. We'll save the first values read
543 * and use them as offsets to be subtracted from the raw values in order
544 * to report stats that count from zero. In the process, we also manage
545 * the potential roll-over.
546 **/
547static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
548 bool offset_loaded, u64 *offset, u64 *stat)
549{
550 u64 new_data;
551
Shannon Nelsonab600852014-01-17 15:36:39 -0800552 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000553 new_data = rd32(hw, loreg);
554 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
555 } else {
556 new_data = rd64(hw, loreg);
557 }
558 if (!offset_loaded)
559 *offset = new_data;
560 if (likely(new_data >= *offset))
561 *stat = new_data - *offset;
562 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400563 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000564 *stat &= 0xFFFFFFFFFFFFULL;
565}
566
567/**
568 * i40e_stat_update32 - read and update a 32 bit stat from the chip
569 * @hw: ptr to the hardware info
570 * @reg: the hw reg to read
571 * @offset_loaded: has the initial offset been loaded yet
572 * @offset: ptr to current offset value
573 * @stat: ptr to the stat
574 **/
575static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
576 bool offset_loaded, u64 *offset, u64 *stat)
577{
578 u32 new_data;
579
580 new_data = rd32(hw, reg);
581 if (!offset_loaded)
582 *offset = new_data;
583 if (likely(new_data >= *offset))
584 *stat = (u32)(new_data - *offset);
585 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400586 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000587}
588
589/**
590 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
591 * @vsi: the VSI to be updated
592 **/
593void i40e_update_eth_stats(struct i40e_vsi *vsi)
594{
595 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
596 struct i40e_pf *pf = vsi->back;
597 struct i40e_hw *hw = &pf->hw;
598 struct i40e_eth_stats *oes;
599 struct i40e_eth_stats *es; /* device's eth stats */
600
601 es = &vsi->eth_stats;
602 oes = &vsi->eth_stats_offsets;
603
604 /* Gather up the stats that the hw collects */
605 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
606 vsi->stat_offsets_loaded,
607 &oes->tx_errors, &es->tx_errors);
608 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000611 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
612 vsi->stat_offsets_loaded,
613 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
614 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
615 vsi->stat_offsets_loaded,
616 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000617
618 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
619 I40E_GLV_GORCL(stat_idx),
620 vsi->stat_offsets_loaded,
621 &oes->rx_bytes, &es->rx_bytes);
622 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
623 I40E_GLV_UPRCL(stat_idx),
624 vsi->stat_offsets_loaded,
625 &oes->rx_unicast, &es->rx_unicast);
626 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
627 I40E_GLV_MPRCL(stat_idx),
628 vsi->stat_offsets_loaded,
629 &oes->rx_multicast, &es->rx_multicast);
630 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
631 I40E_GLV_BPRCL(stat_idx),
632 vsi->stat_offsets_loaded,
633 &oes->rx_broadcast, &es->rx_broadcast);
634
635 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
636 I40E_GLV_GOTCL(stat_idx),
637 vsi->stat_offsets_loaded,
638 &oes->tx_bytes, &es->tx_bytes);
639 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
640 I40E_GLV_UPTCL(stat_idx),
641 vsi->stat_offsets_loaded,
642 &oes->tx_unicast, &es->tx_unicast);
643 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
644 I40E_GLV_MPTCL(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->tx_multicast, &es->tx_multicast);
647 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
648 I40E_GLV_BPTCL(stat_idx),
649 vsi->stat_offsets_loaded,
650 &oes->tx_broadcast, &es->tx_broadcast);
651 vsi->stat_offsets_loaded = true;
652}
653
654/**
655 * i40e_update_veb_stats - Update Switch component statistics
656 * @veb: the VEB being updated
657 **/
658static void i40e_update_veb_stats(struct i40e_veb *veb)
659{
660 struct i40e_pf *pf = veb->pf;
661 struct i40e_hw *hw = &pf->hw;
662 struct i40e_eth_stats *oes;
663 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400664 struct i40e_veb_tc_stats *veb_oes;
665 struct i40e_veb_tc_stats *veb_es;
666 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000667
668 idx = veb->stats_idx;
669 es = &veb->stats;
670 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400671 veb_es = &veb->tc_stats;
672 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000673
674 /* Gather up the stats that the hw collects */
675 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
676 veb->stat_offsets_loaded,
677 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000678 if (hw->revision_id > 0)
679 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
680 veb->stat_offsets_loaded,
681 &oes->rx_unknown_protocol,
682 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000683 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
684 veb->stat_offsets_loaded,
685 &oes->rx_bytes, &es->rx_bytes);
686 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
687 veb->stat_offsets_loaded,
688 &oes->rx_unicast, &es->rx_unicast);
689 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
690 veb->stat_offsets_loaded,
691 &oes->rx_multicast, &es->rx_multicast);
692 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
693 veb->stat_offsets_loaded,
694 &oes->rx_broadcast, &es->rx_broadcast);
695
696 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->tx_bytes, &es->tx_bytes);
699 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->tx_unicast, &es->tx_unicast);
702 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_multicast, &es->tx_multicast);
705 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400708 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
709 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
710 I40E_GLVEBTC_RPCL(i, idx),
711 veb->stat_offsets_loaded,
712 &veb_oes->tc_rx_packets[i],
713 &veb_es->tc_rx_packets[i]);
714 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
715 I40E_GLVEBTC_RBCL(i, idx),
716 veb->stat_offsets_loaded,
717 &veb_oes->tc_rx_bytes[i],
718 &veb_es->tc_rx_bytes[i]);
719 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
720 I40E_GLVEBTC_TPCL(i, idx),
721 veb->stat_offsets_loaded,
722 &veb_oes->tc_tx_packets[i],
723 &veb_es->tc_tx_packets[i]);
724 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
725 I40E_GLVEBTC_TBCL(i, idx),
726 veb->stat_offsets_loaded,
727 &veb_oes->tc_tx_bytes[i],
728 &veb_es->tc_tx_bytes[i]);
729 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000730 veb->stat_offsets_loaded = true;
731}
732
Vasu Dev38e00432014-08-01 13:27:03 -0700733#ifdef I40E_FCOE
734/**
735 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
736 * @vsi: the VSI that is capable of doing FCoE
737 **/
738static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
739{
740 struct i40e_pf *pf = vsi->back;
741 struct i40e_hw *hw = &pf->hw;
742 struct i40e_fcoe_stats *ofs;
743 struct i40e_fcoe_stats *fs; /* device's eth stats */
744 int idx;
745
746 if (vsi->type != I40E_VSI_FCOE)
747 return;
748
Kiran Patil4147e2c2016-01-15 14:33:14 -0800749 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700750 fs = &vsi->fcoe_stats;
751 ofs = &vsi->fcoe_stats_offsets;
752
753 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
754 vsi->fcoe_stat_offsets_loaded,
755 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
756 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
757 vsi->fcoe_stat_offsets_loaded,
758 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
759 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
760 vsi->fcoe_stat_offsets_loaded,
761 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
762 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
763 vsi->fcoe_stat_offsets_loaded,
764 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
765 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
768 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
771 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
772 vsi->fcoe_stat_offsets_loaded,
773 &ofs->fcoe_last_error, &fs->fcoe_last_error);
774 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
775 vsi->fcoe_stat_offsets_loaded,
776 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
777
778 vsi->fcoe_stat_offsets_loaded = true;
779}
780
781#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000782/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000783 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 * @vsi: the VSI to be updated
785 *
786 * There are a few instances where we store the same stat in a
787 * couple of different structs. This is partly because we have
788 * the netdev stats that need to be filled out, which is slightly
789 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000790 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000791 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000792static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000793{
794 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 struct rtnl_link_stats64 *ons;
796 struct rtnl_link_stats64 *ns; /* netdev stats */
797 struct i40e_eth_stats *oes;
798 struct i40e_eth_stats *es; /* device's eth stats */
799 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800800 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000801 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000802 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000803 u64 bytes, packets;
804 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400805 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400806 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000807 u64 rx_p, rx_b;
808 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000809 u16 q;
810
811 if (test_bit(__I40E_DOWN, &vsi->state) ||
812 test_bit(__I40E_CONFIG_BUSY, &pf->state))
813 return;
814
815 ns = i40e_get_vsi_stats_struct(vsi);
816 ons = &vsi->net_stats_offsets;
817 es = &vsi->eth_stats;
818 oes = &vsi->eth_stats_offsets;
819
820 /* Gather up the netdev and vsi stats that the driver collects
821 * on the fly during packet processing
822 */
823 rx_b = rx_p = 0;
824 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400825 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800826 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827 rx_page = 0;
828 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000830 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000831 /* locate Tx ring */
832 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833
Alexander Duyck980e9b12013-09-28 06:01:03 +0000834 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700835 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000836 packets = p->stats.packets;
837 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700838 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000839 tx_b += bytes;
840 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000841 tx_restart += p->tx_stats.restart_queue;
842 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400843 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400844 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800845 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000846
847 /* Rx queue is part of the same block as Tx queue */
848 p = &p[1];
849 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700850 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000851 packets = p->stats.packets;
852 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700853 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000854 rx_b += bytes;
855 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000856 rx_buf += p->rx_stats.alloc_buff_failed;
857 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000859 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000860 vsi->tx_restart = tx_restart;
861 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400862 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400863 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800864 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865 vsi->rx_page_failed = rx_page;
866 vsi->rx_buf_failed = rx_buf;
867
868 ns->rx_packets = rx_p;
869 ns->rx_bytes = rx_b;
870 ns->tx_packets = tx_p;
871 ns->tx_bytes = tx_b;
872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000873 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000874 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000875 ons->tx_errors = oes->tx_errors;
876 ns->tx_errors = es->tx_errors;
877 ons->multicast = oes->rx_multicast;
878 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000879 ons->rx_dropped = oes->rx_discards;
880 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000881 ons->tx_dropped = oes->tx_discards;
882 ns->tx_dropped = es->tx_discards;
883
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000884 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000885 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000886 ns->rx_crc_errors = pf->stats.crc_errors;
887 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
888 ns->rx_length_errors = pf->stats.rx_length_errors;
889 }
890}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000893 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000894 * @pf: the PF to be updated
895 **/
896static void i40e_update_pf_stats(struct i40e_pf *pf)
897{
898 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
899 struct i40e_hw_port_stats *nsd = &pf->stats;
900 struct i40e_hw *hw = &pf->hw;
901 u32 val;
902 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000904 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
905 I40E_GLPRT_GORCL(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
908 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
909 I40E_GLPRT_GOTCL(hw->port),
910 pf->stat_offsets_loaded,
911 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
912 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->eth.rx_discards,
915 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000916 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
917 I40E_GLPRT_UPRCL(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->eth.rx_unicast,
920 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
922 I40E_GLPRT_MPRCL(hw->port),
923 pf->stat_offsets_loaded,
924 &osd->eth.rx_multicast,
925 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000926 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
927 I40E_GLPRT_BPRCL(hw->port),
928 pf->stat_offsets_loaded,
929 &osd->eth.rx_broadcast,
930 &nsd->eth.rx_broadcast);
931 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
932 I40E_GLPRT_UPTCL(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->eth.tx_unicast,
935 &nsd->eth.tx_unicast);
936 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
937 I40E_GLPRT_MPTCL(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->eth.tx_multicast,
940 &nsd->eth.tx_multicast);
941 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
942 I40E_GLPRT_BPTCL(hw->port),
943 pf->stat_offsets_loaded,
944 &osd->eth.tx_broadcast,
945 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000946
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000947 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
948 pf->stat_offsets_loaded,
949 &osd->tx_dropped_link_down,
950 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000951
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000952 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
953 pf->stat_offsets_loaded,
954 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000955
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000956 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
957 pf->stat_offsets_loaded,
958 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000959
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000960 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->mac_local_faults,
963 &nsd->mac_local_faults);
964 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->mac_remote_faults,
967 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000968
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000969 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->rx_length_errors,
972 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000973
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000974 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->link_xon_rx, &nsd->link_xon_rx);
977 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800980 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000983 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000986
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000987 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800988 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
989 pf->stat_offsets_loaded,
990 &osd->priority_xoff_rx[i],
991 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000993 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000994 &osd->priority_xon_rx[i],
995 &nsd->priority_xon_rx[i]);
996 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000997 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000998 &osd->priority_xon_tx[i],
999 &nsd->priority_xon_tx[i]);
1000 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001001 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001002 &osd->priority_xoff_tx[i],
1003 &nsd->priority_xoff_tx[i]);
1004 i40e_stat_update32(hw,
1005 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001006 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001007 &osd->priority_xon_2_xoff[i],
1008 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001009 }
1010
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001011 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1012 I40E_GLPRT_PRC64L(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->rx_size_64, &nsd->rx_size_64);
1015 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1016 I40E_GLPRT_PRC127L(hw->port),
1017 pf->stat_offsets_loaded,
1018 &osd->rx_size_127, &nsd->rx_size_127);
1019 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1020 I40E_GLPRT_PRC255L(hw->port),
1021 pf->stat_offsets_loaded,
1022 &osd->rx_size_255, &nsd->rx_size_255);
1023 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1024 I40E_GLPRT_PRC511L(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->rx_size_511, &nsd->rx_size_511);
1027 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1028 I40E_GLPRT_PRC1023L(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->rx_size_1023, &nsd->rx_size_1023);
1031 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1032 I40E_GLPRT_PRC1522L(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->rx_size_1522, &nsd->rx_size_1522);
1035 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1036 I40E_GLPRT_PRC9522L(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->rx_size_big, &nsd->rx_size_big);
1039
1040 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1041 I40E_GLPRT_PTC64L(hw->port),
1042 pf->stat_offsets_loaded,
1043 &osd->tx_size_64, &nsd->tx_size_64);
1044 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1045 I40E_GLPRT_PTC127L(hw->port),
1046 pf->stat_offsets_loaded,
1047 &osd->tx_size_127, &nsd->tx_size_127);
1048 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1049 I40E_GLPRT_PTC255L(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->tx_size_255, &nsd->tx_size_255);
1052 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1053 I40E_GLPRT_PTC511L(hw->port),
1054 pf->stat_offsets_loaded,
1055 &osd->tx_size_511, &nsd->tx_size_511);
1056 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1057 I40E_GLPRT_PTC1023L(hw->port),
1058 pf->stat_offsets_loaded,
1059 &osd->tx_size_1023, &nsd->tx_size_1023);
1060 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1061 I40E_GLPRT_PTC1522L(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->tx_size_1522, &nsd->tx_size_1522);
1064 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1065 I40E_GLPRT_PTC9522L(hw->port),
1066 pf->stat_offsets_loaded,
1067 &osd->tx_size_big, &nsd->tx_size_big);
1068
1069 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->rx_undersize, &nsd->rx_undersize);
1072 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->rx_fragments, &nsd->rx_fragments);
1075 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->rx_oversize, &nsd->rx_oversize);
1078 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->rx_jabber, &nsd->rx_jabber);
1081
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001082 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001083 i40e_stat_update32(hw,
1084 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001085 pf->stat_offsets_loaded,
1086 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001087 i40e_stat_update32(hw,
1088 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001089 pf->stat_offsets_loaded,
1090 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001091 i40e_stat_update32(hw,
1092 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1093 pf->stat_offsets_loaded,
1094 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001095
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001096 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1097 nsd->tx_lpi_status =
1098 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1099 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1100 nsd->rx_lpi_status =
1101 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1102 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1103 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1104 pf->stat_offsets_loaded,
1105 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1106 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1107 pf->stat_offsets_loaded,
1108 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1109
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001110 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1111 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1112 nsd->fd_sb_status = true;
1113 else
1114 nsd->fd_sb_status = false;
1115
1116 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1117 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1118 nsd->fd_atr_status = true;
1119 else
1120 nsd->fd_atr_status = false;
1121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122 pf->stat_offsets_loaded = true;
1123}
1124
1125/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001126 * i40e_update_stats - Update the various statistics counters.
1127 * @vsi: the VSI to be updated
1128 *
1129 * Update the various stats for this VSI and its related entities.
1130 **/
1131void i40e_update_stats(struct i40e_vsi *vsi)
1132{
1133 struct i40e_pf *pf = vsi->back;
1134
1135 if (vsi == pf->vsi[pf->lan_vsi])
1136 i40e_update_pf_stats(pf);
1137
1138 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001139#ifdef I40E_FCOE
1140 i40e_update_fcoe_stats(vsi);
1141#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001142}
1143
1144/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001145 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1146 * @vsi: the VSI to be searched
1147 * @macaddr: the MAC address
1148 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001149 *
1150 * Returns ptr to the filter object or NULL
1151 **/
1152static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001153 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001154{
1155 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001156 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157
1158 if (!vsi || !macaddr)
1159 return NULL;
1160
Jacob Keller278e7d02016-10-05 09:30:37 -07001161 key = i40e_addr_to_hkey(macaddr);
1162 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001163 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001164 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001165 return f;
1166 }
1167 return NULL;
1168}
1169
1170/**
1171 * i40e_find_mac - Find a mac addr in the macvlan filters list
1172 * @vsi: the VSI to be searched
1173 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001174 *
1175 * Returns the first filter with the provided MAC address or NULL if
1176 * MAC address was not found
1177 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001178struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001179{
1180 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001181 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182
1183 if (!vsi || !macaddr)
1184 return NULL;
1185
Jacob Keller278e7d02016-10-05 09:30:37 -07001186 key = i40e_addr_to_hkey(macaddr);
1187 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001188 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001189 return f;
1190 }
1191 return NULL;
1192}
1193
1194/**
1195 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1196 * @vsi: the VSI to be searched
1197 *
1198 * Returns true if VSI is in vlan mode or false otherwise
1199 **/
1200bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1201{
Jacob Kellercbebb852016-10-05 09:30:40 -07001202 /* If we have a PVID, always operate in VLAN mode */
1203 if (vsi->info.pvid)
1204 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001205
Jacob Kellercbebb852016-10-05 09:30:40 -07001206 /* We need to operate in VLAN mode whenever we have any filters with
1207 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1208 * time, incurring search cost repeatedly. However, we can notice two
1209 * things:
1210 *
1211 * 1) the only place where we can gain a VLAN filter is in
1212 * i40e_add_filter.
1213 *
1214 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001215 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001216 *
1217 * Thus, we can simply use a boolean value, has_vlan_filters which we
1218 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1219 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001220 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001221 * filters here and can perform the check at the same time. This
1222 * results in avoiding embedding a loop for VLAN mode inside another
1223 * loop over all the filters, and should maintain correctness as noted
1224 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001225 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001226 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001227}
1228
1229/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001230 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1231 * @vsi: the PF Main VSI - inappropriate for any other VSI
1232 * @macaddr: the MAC address
1233 *
1234 * Remove whatever filter the firmware set up so the driver can manage
1235 * its own filtering intelligently.
1236 **/
1237static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1238{
1239 struct i40e_aqc_remove_macvlan_element_data element;
1240 struct i40e_pf *pf = vsi->back;
1241
1242 /* Only appropriate for the PF main VSI */
1243 if (vsi->type != I40E_VSI_MAIN)
1244 return;
1245
1246 memset(&element, 0, sizeof(element));
1247 ether_addr_copy(element.mac_addr, macaddr);
1248 element.vlan_tag = 0;
1249 /* Ignore error returns, some firmware does it this way... */
1250 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1251 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1252
1253 memset(&element, 0, sizeof(element));
1254 ether_addr_copy(element.mac_addr, macaddr);
1255 element.vlan_tag = 0;
1256 /* ...and some firmware does it this way. */
1257 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1258 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1259 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1260}
1261
1262/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001263 * i40e_add_filter - Add a mac/vlan filter to the VSI
1264 * @vsi: the VSI to be searched
1265 * @macaddr: the MAC address
1266 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001267 *
1268 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001269 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001270 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001271 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001272 **/
1273struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001274 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001275{
1276 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001277 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001278
1279 if (!vsi || !macaddr)
1280 return NULL;
1281
Jacob Keller1bc87e82016-10-05 09:30:31 -07001282 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001283 if (!f) {
1284 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1285 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001286 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001287
Jacob Kellercbebb852016-10-05 09:30:40 -07001288 /* Update the boolean indicating if we need to function in
1289 * VLAN mode.
1290 */
1291 if (vlan >= 0)
1292 vsi->has_vlan_filter = true;
1293
Greg Rose9a173902014-05-22 06:32:02 +00001294 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001295 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001296 /* If we're in overflow promisc mode, set the state directly
1297 * to failed, so we don't bother to try sending the filter
1298 * to the hardware.
1299 */
1300 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1301 f->state = I40E_FILTER_FAILED;
1302 else
1303 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001304 INIT_HLIST_NODE(&f->hlist);
1305
1306 key = i40e_addr_to_hkey(macaddr);
1307 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001309 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1310 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1311 }
1312
Jacob Keller1bc87e82016-10-05 09:30:31 -07001313 /* If we're asked to add a filter that has been marked for removal, it
1314 * is safe to simply restore it to active state. __i40e_del_filter
1315 * will have simply deleted any filters which were previously marked
1316 * NEW or FAILED, so if it is currently marked REMOVE it must have
1317 * previously been ACTIVE. Since we haven't yet run the sync filters
1318 * task, just restore this filter to the ACTIVE state so that the
1319 * sync task leaves it in place
1320 */
1321 if (f->state == I40E_FILTER_REMOVE)
1322 f->state = I40E_FILTER_ACTIVE;
1323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001324 return f;
1325}
1326
1327/**
Jacob Keller290d2552016-10-05 09:30:36 -07001328 * __i40e_del_filter - Remove a specific filter from the VSI
1329 * @vsi: VSI to remove from
1330 * @f: the filter to remove from the list
1331 *
1332 * This function should be called instead of i40e_del_filter only if you know
1333 * the exact filter you will remove already, such as via i40e_find_filter or
1334 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001335 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001336 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001337 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001338 * ANOTHER NOTE: This function MUST be called from within the context of
1339 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1340 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001342static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001343{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001344 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345 return;
1346
Jacob Keller1bc87e82016-10-05 09:30:31 -07001347 if ((f->state == I40E_FILTER_FAILED) ||
1348 (f->state == I40E_FILTER_NEW)) {
1349 /* this one never got added by the FW. Just remove it,
1350 * no need to sync anything.
1351 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001352 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001353 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001355 f->state = I40E_FILTER_REMOVE;
1356 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1357 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001358 }
1359}
1360
1361/**
Jacob Keller290d2552016-10-05 09:30:36 -07001362 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1363 * @vsi: the VSI to be searched
1364 * @macaddr: the MAC address
1365 * @vlan: the VLAN
1366 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001367 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001368 * being held.
1369 * ANOTHER NOTE: This function MUST be called from within the context of
1370 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1371 * instead of list_for_each_entry().
1372 **/
1373void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1374{
1375 struct i40e_mac_filter *f;
1376
1377 if (!vsi || !macaddr)
1378 return;
1379
1380 f = i40e_find_filter(vsi, macaddr, vlan);
1381 __i40e_del_filter(vsi, f);
1382}
1383
1384/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001385 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1386 * @vsi: the VSI to be searched
1387 * @macaddr: the mac address to be filtered
1388 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001389 * Goes through all the macvlan filters and adds a macvlan filter for each
1390 * unique vlan that already exists. If a PVID has been assigned, instead only
1391 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001392 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001393 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001394 **/
1395struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1396 const u8 *macaddr)
1397{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001398 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001399 struct hlist_node *h;
1400 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001401
1402 if (vsi->info.pvid)
1403 return i40e_add_filter(vsi, macaddr,
1404 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001405
Jacob Keller278e7d02016-10-05 09:30:37 -07001406 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001407 if (f->state == I40E_FILTER_REMOVE)
1408 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001409 add = i40e_add_filter(vsi, macaddr, f->vlan);
1410 if (!add)
1411 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001412 }
1413
Jacob Keller5feb3d72016-10-05 09:30:34 -07001414 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001415}
1416
1417/**
1418 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1419 * @vsi: the VSI to be searched
1420 * @macaddr: the mac address to be removed
1421 *
1422 * Removes a given MAC address from a VSI, regardless of VLAN
1423 *
1424 * Returns 0 for success, or error
1425 **/
1426int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1427{
Jacob Keller278e7d02016-10-05 09:30:37 -07001428 struct i40e_mac_filter *f;
1429 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001430 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001432
Jacob Keller278e7d02016-10-05 09:30:37 -07001433 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1434 "Missing mac_filter_hash_lock\n");
1435 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001436 if (ether_addr_equal(macaddr, f->macaddr)) {
1437 __i40e_del_filter(vsi, f);
1438 found = true;
1439 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001440 }
Jacob Keller290d2552016-10-05 09:30:36 -07001441
1442 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001443 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001444 else
1445 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001446}
1447
1448/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449 * i40e_set_mac - NDO callback to set mac address
1450 * @netdev: network interface device structure
1451 * @p: pointer to an address structure
1452 *
1453 * Returns 0 on success, negative on failure
1454 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001455#ifdef I40E_FCOE
1456int i40e_set_mac(struct net_device *netdev, void *p)
1457#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001458static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001459#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460{
1461 struct i40e_netdev_priv *np = netdev_priv(netdev);
1462 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001463 struct i40e_pf *pf = vsi->back;
1464 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001465 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001466
1467 if (!is_valid_ether_addr(addr->sa_data))
1468 return -EADDRNOTAVAIL;
1469
Shannon Nelson30650cc2014-07-29 04:01:50 +00001470 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1471 netdev_info(netdev, "already using mac address %pM\n",
1472 addr->sa_data);
1473 return 0;
1474 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001475
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001476 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1477 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1478 return -EADDRNOTAVAIL;
1479
Shannon Nelson30650cc2014-07-29 04:01:50 +00001480 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1481 netdev_info(netdev, "returning to hw mac address %pM\n",
1482 hw->mac.addr);
1483 else
1484 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1485
Jacob Keller278e7d02016-10-05 09:30:37 -07001486 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001487 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1488 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001489 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001490 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001491 if (vsi->type == I40E_VSI_MAIN) {
1492 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001493
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001494 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001495 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001496 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001497 if (ret)
1498 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1499 i40e_stat_str(hw, ret),
1500 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001501 }
1502
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001503 /* schedule our worker thread which will take care of
1504 * applying the new filter changes
1505 */
1506 i40e_service_event_schedule(vsi->back);
1507 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001508}
1509
1510/**
1511 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1512 * @vsi: the VSI being setup
1513 * @ctxt: VSI context structure
1514 * @enabled_tc: Enabled TCs bitmap
1515 * @is_add: True if called before Add VSI
1516 *
1517 * Setup VSI queue mapping for enabled traffic classes.
1518 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001519#ifdef I40E_FCOE
1520void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1521 struct i40e_vsi_context *ctxt,
1522 u8 enabled_tc,
1523 bool is_add)
1524#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001525static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1526 struct i40e_vsi_context *ctxt,
1527 u8 enabled_tc,
1528 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001529#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001530{
1531 struct i40e_pf *pf = vsi->back;
1532 u16 sections = 0;
1533 u8 netdev_tc = 0;
1534 u16 numtc = 0;
1535 u16 qcount;
1536 u8 offset;
1537 u16 qmap;
1538 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001539 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001540
1541 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1542 offset = 0;
1543
1544 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1545 /* Find numtc from enabled TC bitmap */
1546 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001547 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001548 numtc++;
1549 }
1550 if (!numtc) {
1551 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1552 numtc = 1;
1553 }
1554 } else {
1555 /* At least TC0 is enabled in case of non-DCB case */
1556 numtc = 1;
1557 }
1558
1559 vsi->tc_config.numtc = numtc;
1560 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001561 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001562 qcount = vsi->alloc_queue_pairs;
1563
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001564 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001565 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566
1567 /* Setup queue offset/count for all TCs for given VSI */
1568 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1569 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001570 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001571 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 int pow, num_qps;
1573
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001574 switch (vsi->type) {
1575 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001576 qcount = min_t(int, pf->alloc_rss_size,
1577 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001579#ifdef I40E_FCOE
1580 case I40E_VSI_FCOE:
1581 qcount = num_tc_qps;
1582 break;
1583#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001584 case I40E_VSI_FDIR:
1585 case I40E_VSI_SRIOV:
1586 case I40E_VSI_VMDQ2:
1587 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001588 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589 WARN_ON(i != 0);
1590 break;
1591 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001592 vsi->tc_config.tc_info[i].qoffset = offset;
1593 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001594
Shannon Nelson1e200e42015-02-27 09:15:24 +00001595 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001596 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001597 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001598 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001599 pow++;
1600 num_qps >>= 1;
1601 }
1602
1603 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1604 qmap =
1605 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1606 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1607
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001608 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609 } else {
1610 /* TC is not enabled so set the offset to
1611 * default queue and allocate one queue
1612 * for the given TC.
1613 */
1614 vsi->tc_config.tc_info[i].qoffset = 0;
1615 vsi->tc_config.tc_info[i].qcount = 1;
1616 vsi->tc_config.tc_info[i].netdev_tc = 0;
1617
1618 qmap = 0;
1619 }
1620 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1621 }
1622
1623 /* Set actual Tx/Rx queue pairs */
1624 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001625 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1626 if (vsi->req_queue_pairs > 0)
1627 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001628 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001629 vsi->num_queue_pairs = pf->num_lan_msix;
1630 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001631
1632 /* Scheduler section valid can only be set for ADD VSI */
1633 if (is_add) {
1634 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1635
1636 ctxt->info.up_enable_bits = enabled_tc;
1637 }
1638 if (vsi->type == I40E_VSI_SRIOV) {
1639 ctxt->info.mapping_flags |=
1640 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1641 for (i = 0; i < vsi->num_queue_pairs; i++)
1642 ctxt->info.queue_mapping[i] =
1643 cpu_to_le16(vsi->base_queue + i);
1644 } else {
1645 ctxt->info.mapping_flags |=
1646 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1647 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1648 }
1649 ctxt->info.valid_sections |= cpu_to_le16(sections);
1650}
1651
1652/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001653 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1654 * @netdev: the netdevice
1655 * @addr: address to add
1656 *
1657 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1658 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1659 */
1660static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1661{
1662 struct i40e_netdev_priv *np = netdev_priv(netdev);
1663 struct i40e_vsi *vsi = np->vsi;
1664 struct i40e_mac_filter *f;
1665
1666 if (i40e_is_vsi_in_vlan(vsi))
1667 f = i40e_put_mac_in_vlan(vsi, addr);
1668 else
1669 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1670
1671 if (f)
1672 return 0;
1673 else
1674 return -ENOMEM;
1675}
1676
1677/**
1678 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1679 * @netdev: the netdevice
1680 * @addr: address to add
1681 *
1682 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1683 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1684 */
1685static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1686{
1687 struct i40e_netdev_priv *np = netdev_priv(netdev);
1688 struct i40e_vsi *vsi = np->vsi;
1689
1690 if (i40e_is_vsi_in_vlan(vsi))
1691 i40e_del_mac_all_vlan(vsi, addr);
1692 else
1693 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1694
1695 return 0;
1696}
1697
1698/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 * i40e_set_rx_mode - NDO callback to set the netdev filters
1700 * @netdev: network interface device structure
1701 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001702#ifdef I40E_FCOE
1703void i40e_set_rx_mode(struct net_device *netdev)
1704#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001705static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001706#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001707{
1708 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001710
Jacob Keller278e7d02016-10-05 09:30:37 -07001711 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001712
Jacob Keller6622f5c2016-10-05 09:30:32 -07001713 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1714 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001715
Jacob Keller278e7d02016-10-05 09:30:37 -07001716 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001717
1718 /* check for other flag changes */
1719 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1720 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1721 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1722 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001723
1724 /* schedule our worker thread which will take care of
1725 * applying the new filter changes
1726 */
1727 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001728}
1729
1730/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001731 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1732 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001733 * @from: Pointer to list which contains MAC filter entries - changes to
1734 * those entries needs to be undone.
1735 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001736 * MAC filter entries from list were slated to be sent to firmware, either for
1737 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001738 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001739static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1740 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001741{
Jacob Keller278e7d02016-10-05 09:30:37 -07001742 struct i40e_mac_filter *f;
1743 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001744
Jacob Keller278e7d02016-10-05 09:30:37 -07001745 hlist_for_each_entry_safe(f, h, from, hlist) {
1746 u64 key = i40e_addr_to_hkey(f->macaddr);
1747
Kiran Patil21659032015-09-30 14:09:03 -04001748 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001749 hlist_del(&f->hlist);
1750 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001751 }
1752}
1753
1754/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001755 * i40e_update_filter_state - Update filter state based on return data
1756 * from firmware
1757 * @count: Number of filters added
1758 * @add_list: return data from fw
1759 * @head: pointer to first filter in current batch
1760 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001761 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001762 * MAC filter entries from list were slated to be added to device. Returns
1763 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001764 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001765static int
1766i40e_update_filter_state(int count,
1767 struct i40e_aqc_add_macvlan_element_data *add_list,
1768 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001769{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001770 int retval = 0;
1771 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001772
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001773
1774 if (!aq_err) {
1775 retval = count;
1776 /* Everything's good, mark all filters active. */
1777 for (i = 0; i < count ; i++) {
1778 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001779 add_head = hlist_entry(add_head->hlist.next,
1780 typeof(struct i40e_mac_filter),
1781 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001782 }
1783 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1784 /* Device ran out of filter space. Check the return value
1785 * for each filter to see which ones are active.
1786 */
1787 for (i = 0; i < count ; i++) {
1788 if (add_list[i].match_method ==
1789 I40E_AQC_MM_ERR_NO_RES) {
1790 add_head->state = I40E_FILTER_FAILED;
1791 } else {
1792 add_head->state = I40E_FILTER_ACTIVE;
1793 retval++;
1794 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001795 add_head = hlist_entry(add_head->hlist.next,
1796 typeof(struct i40e_mac_filter),
1797 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001798 }
1799 } else {
1800 /* Some other horrible thing happened, fail all filters */
1801 retval = 0;
1802 for (i = 0; i < count ; i++) {
1803 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001804 add_head = hlist_entry(add_head->hlist.next,
1805 typeof(struct i40e_mac_filter),
1806 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001807 }
Kiran Patil21659032015-09-30 14:09:03 -04001808 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001809 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001810}
1811
1812/**
Jacob Keller00936312016-10-05 09:30:41 -07001813 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1814 * @vsi: ptr to the VSI
1815 * @vsi_name: name to display in messages
1816 * @list: the list of filters to send to firmware
1817 * @num_del: the number of filters to delete
1818 * @retval: Set to -EIO on failure to delete
1819 *
1820 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1821 * *retval instead of a return value so that success does not force ret_val to
1822 * be set to 0. This ensures that a sequence of calls to this function
1823 * preserve the previous value of *retval on successful delete.
1824 */
1825static
1826void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1827 struct i40e_aqc_remove_macvlan_element_data *list,
1828 int num_del, int *retval)
1829{
1830 struct i40e_hw *hw = &vsi->back->hw;
1831 i40e_status aq_ret;
1832 int aq_err;
1833
1834 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1835 aq_err = hw->aq.asq_last_status;
1836
1837 /* Explicitly ignore and do not report when firmware returns ENOENT */
1838 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1839 *retval = -EIO;
1840 dev_info(&vsi->back->pdev->dev,
1841 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1842 vsi_name, i40e_stat_str(hw, aq_ret),
1843 i40e_aq_str(hw, aq_err));
1844 }
1845}
1846
1847/**
1848 * i40e_aqc_add_filters - Request firmware to add a set of filters
1849 * @vsi: ptr to the VSI
1850 * @vsi_name: name to display in messages
1851 * @list: the list of filters to send to firmware
1852 * @add_head: Position in the add hlist
1853 * @num_add: the number of filters to add
1854 * @promisc_change: set to true on exit if promiscuous mode was forced on
1855 *
1856 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1857 * promisc_changed to true if the firmware has run out of space for more
1858 * filters.
1859 */
1860static
1861void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1862 struct i40e_aqc_add_macvlan_element_data *list,
1863 struct i40e_mac_filter *add_head,
1864 int num_add, bool *promisc_changed)
1865{
1866 struct i40e_hw *hw = &vsi->back->hw;
1867 i40e_status aq_ret;
1868 int aq_err, fcnt;
1869
1870 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
1871 aq_err = hw->aq.asq_last_status;
1872 fcnt = i40e_update_filter_state(num_add, list, add_head, aq_ret);
1873 vsi->active_filters += fcnt;
1874
1875 if (fcnt != num_add) {
1876 *promisc_changed = true;
1877 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
1878 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
1879 dev_warn(&vsi->back->pdev->dev,
1880 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1881 i40e_aq_str(hw, aq_err),
1882 vsi_name);
1883 }
1884}
1885
1886/**
Jacob Keller435c0842016-11-08 13:05:10 -08001887 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1888 * @vsi: pointer to the VSI
1889 * @f: filter data
1890 *
1891 * This function sets or clears the promiscuous broadcast flags for VLAN
1892 * filters in order to properly receive broadcast frames. Assumes that only
1893 * broadcast filters are passed.
1894 **/
1895static
1896void i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1897 struct i40e_mac_filter *f)
1898{
1899 bool enable = f->state == I40E_FILTER_NEW;
1900 struct i40e_hw *hw = &vsi->back->hw;
1901 i40e_status aq_ret;
1902
1903 if (f->vlan == I40E_VLAN_ANY) {
1904 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1905 vsi->seid,
1906 enable,
1907 NULL);
1908 } else {
1909 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1910 vsi->seid,
1911 enable,
1912 f->vlan,
1913 NULL);
1914 }
1915
1916 if (aq_ret) {
1917 dev_warn(&vsi->back->pdev->dev,
1918 "Error %s setting broadcast promiscuous mode on %s\n",
1919 i40e_aq_str(hw, hw->aq.asq_last_status),
1920 vsi_name);
1921 f->state = I40E_FILTER_FAILED;
1922 } else if (enable) {
1923 f->state = I40E_FILTER_ACTIVE;
1924 }
1925}
1926
1927/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001928 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1929 * @vsi: ptr to the VSI
1930 *
1931 * Push any outstanding VSI filter changes through the AdminQ.
1932 *
1933 * Returns 0 or error value
1934 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001935int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001936{
Jacob Keller278e7d02016-10-05 09:30:37 -07001937 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07001938 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001939 struct i40e_hw *hw = &vsi->back->hw;
Alan Brady84f5ca62016-10-05 09:30:39 -07001940 unsigned int vlan_any_filters = 0;
1941 unsigned int non_vlan_filters = 0;
1942 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001943 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001944 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001945 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001946 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001947 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001948 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001949 struct i40e_pf *pf;
1950 int num_add = 0;
1951 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001952 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001953 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001954 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07001955 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001956
1957 /* empty array typed pointers, kcalloc later */
1958 struct i40e_aqc_add_macvlan_element_data *add_list;
1959 struct i40e_aqc_remove_macvlan_element_data *del_list;
1960
1961 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1962 usleep_range(1000, 2000);
1963 pf = vsi->back;
1964
1965 if (vsi->netdev) {
1966 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1967 vsi->current_netdev_flags = vsi->netdev->flags;
1968 }
1969
Jacob Keller278e7d02016-10-05 09:30:37 -07001970 INIT_HLIST_HEAD(&tmp_add_list);
1971 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001972
Shannon Nelson2d1de822016-05-16 10:26:44 -07001973 if (vsi->type == I40E_VSI_SRIOV)
1974 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1975 else if (vsi->type != I40E_VSI_MAIN)
1976 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1977
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001978 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1979 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1980
Jacob Keller278e7d02016-10-05 09:30:37 -07001981 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001982 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001983 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001984 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001985 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001986 hash_del(&f->hlist);
1987 hlist_add_head(&f->hlist, &tmp_del_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001988 vsi->active_filters--;
Alan Brady84f5ca62016-10-05 09:30:39 -07001989
1990 /* Avoid counting removed filters */
1991 continue;
Kiran Patil21659032015-09-30 14:09:03 -04001992 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001993 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001994 hash_del(&f->hlist);
1995 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001996 }
Alan Brady84f5ca62016-10-05 09:30:39 -07001997
1998 /* Count the number of each type of filter we have
1999 * remaining, ignoring any filters we're about to
2000 * delete.
2001 */
2002 if (f->vlan > 0)
2003 vlan_filters++;
2004 else if (!f->vlan)
2005 non_vlan_filters++;
2006 else
2007 vlan_any_filters++;
2008 }
2009
2010 /* We should never have VLAN=-1 filters at the same time as we
2011 * have either VLAN=0 or VLAN>0 filters, so warn about this
2012 * case here to help catch any issues.
2013 */
2014 WARN_ON(vlan_any_filters && (vlan_filters + non_vlan_filters));
2015
2016 /* If we only have VLAN=0 filters remaining, and don't have
2017 * any other VLAN filters, we need to convert these VLAN=0
2018 * filters into VLAN=-1 (I40E_VLAN_ANY) so that we operate
2019 * correctly in non-VLAN mode and receive all traffic tagged
2020 * or untagged.
2021 */
2022 if (non_vlan_filters && !vlan_filters) {
2023 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
2024 hlist) {
2025 /* Only replace VLAN=0 filters */
2026 if (f->vlan)
2027 continue;
2028
2029 /* Allocate a replacement element */
2030 add_head = kzalloc(sizeof(*add_head),
2031 GFP_KERNEL);
2032 if (!add_head)
2033 goto err_no_memory_locked;
2034
2035 /* Copy the filter, with new state and VLAN */
2036 *add_head = *f;
2037 add_head->state = I40E_FILTER_NEW;
2038 add_head->vlan = I40E_VLAN_ANY;
2039
2040 /* Move the replacement to the add list */
2041 INIT_HLIST_NODE(&add_head->hlist);
2042 hlist_add_head(&add_head->hlist,
2043 &tmp_add_list);
2044
2045 /* Move the original to the delete list */
2046 f->state = I40E_FILTER_REMOVE;
2047 hash_del(&f->hlist);
2048 hlist_add_head(&f->hlist, &tmp_del_list);
2049 vsi->active_filters--;
2050 }
2051
2052 /* Also update any filters on the tmp_add list */
2053 hlist_for_each_entry(f, &tmp_add_list, hlist) {
2054 if (!f->vlan)
2055 f->vlan = I40E_VLAN_ANY;
2056 }
2057 add_head = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002058 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002059 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002060 }
2061
2062 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002063 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002064 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002065 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002066 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002067 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002068 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002069 if (!del_list)
2070 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002071
Jacob Keller278e7d02016-10-05 09:30:37 -07002072 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073 cmd_flags = 0;
2074
Jacob Keller435c0842016-11-08 13:05:10 -08002075 /* handle broadcast filters by updating the broadcast
2076 * promiscuous flag instead of deleting a MAC filter.
2077 */
2078 if (is_broadcast_ether_addr(f->macaddr)) {
2079 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2080
2081 hlist_del(&f->hlist);
2082 kfree(f);
2083 continue;
2084 }
2085
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002086 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002087 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002088 if (f->vlan == I40E_VLAN_ANY) {
2089 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002090 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002091 } else {
2092 del_list[num_del].vlan_tag =
2093 cpu_to_le16((u16)(f->vlan));
2094 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002095
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002096 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2097 del_list[num_del].flags = cmd_flags;
2098 num_del++;
2099
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002100 /* flush a full buffer */
2101 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002102 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2103 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002104 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002105 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002106 }
Kiran Patil21659032015-09-30 14:09:03 -04002107 /* Release memory for MAC filter entries which were
2108 * synced up with HW.
2109 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002110 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002111 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002112 }
Kiran Patil21659032015-09-30 14:09:03 -04002113
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002115 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2116 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002117 }
2118
2119 kfree(del_list);
2120 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002121 }
2122
Jacob Kellercbebb852016-10-05 09:30:40 -07002123 /* After finishing notifying firmware of the deleted filters, update
2124 * the cached value of vsi->has_vlan_filter. Note that we are safe to
2125 * use just !!vlan_filters here because if we only have VLAN=0 (that
2126 * is, non_vlan_filters) these will all be converted to VLAN=-1 in the
2127 * logic above already so this value would still be correct.
2128 */
2129 vsi->has_vlan_filter = !!vlan_filters;
2130
Jacob Keller278e7d02016-10-05 09:30:37 -07002131 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002132 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002133 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002134 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002135 list_size = filter_list_len *
2136 sizeof(struct i40e_aqc_add_macvlan_element_data);
2137 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002138 if (!add_list)
2139 goto err_no_memory;
2140
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002141 num_add = 0;
Jacob Keller435c0842016-11-08 13:05:10 -08002142 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002143 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2144 &vsi->state)) {
2145 f->state = I40E_FILTER_FAILED;
2146 continue;
2147 }
Jacob Keller435c0842016-11-08 13:05:10 -08002148
2149 /* handle broadcast filters by updating the broadcast
2150 * promiscuous flag instead of adding a MAC filter.
2151 */
2152 if (is_broadcast_ether_addr(f->macaddr)) {
2153 u64 key = i40e_addr_to_hkey(f->macaddr);
2154 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2155
2156 hlist_del(&f->hlist);
2157 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2158 continue;
2159 }
2160
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002162 if (num_add == 0)
2163 add_head = f;
2164 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002165 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002166 if (f->vlan == I40E_VLAN_ANY) {
2167 add_list[num_add].vlan_tag = 0;
2168 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2169 } else {
2170 add_list[num_add].vlan_tag =
2171 cpu_to_le16((u16)(f->vlan));
2172 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002173 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002174 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002175 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2176 num_add++;
2177
2178 /* flush a full buffer */
2179 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002180 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2181 add_head, num_add,
2182 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002183 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002184 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002185 }
2186 }
2187 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002188 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2189 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002190 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002191 /* Now move all of the filters from the temp add list back to
2192 * the VSI's list.
2193 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002194 spin_lock_bh(&vsi->mac_filter_hash_lock);
2195 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2196 u64 key = i40e_addr_to_hkey(f->macaddr);
2197
2198 hlist_del(&f->hlist);
2199 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002200 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002201 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002202 kfree(add_list);
2203 add_list = NULL;
2204 }
2205
2206 /* Check to see if we can drop out of overflow promiscuous mode. */
2207 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2208 (vsi->active_filters < vsi->promisc_threshold)) {
2209 int failed_count = 0;
2210 /* See if we have any failed filters. We can't drop out of
2211 * promiscuous until these have all been deleted.
2212 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002213 spin_lock_bh(&vsi->mac_filter_hash_lock);
2214 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002215 if (f->state == I40E_FILTER_FAILED)
2216 failed_count++;
2217 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002218 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002219 if (!failed_count) {
2220 dev_info(&pf->pdev->dev,
2221 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2222 vsi_name);
2223 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2224 promisc_changed = true;
2225 vsi->promisc_threshold = 0;
2226 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002227 }
2228
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002229 /* if the VF is not trusted do not do promisc */
2230 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2231 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2232 goto out;
2233 }
2234
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002235 /* check for changes in promiscuous modes */
2236 if (changed_flags & IFF_ALLMULTI) {
2237 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002239 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002240 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2241 vsi->seid,
2242 cur_multipromisc,
2243 NULL);
2244 if (aq_ret) {
2245 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002246 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002247 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002248 "set multi promisc failed on %s, err %s aq_err %s\n",
2249 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002250 i40e_stat_str(hw, aq_ret),
2251 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002252 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002254 if ((changed_flags & IFF_PROMISC) ||
2255 (promisc_changed &&
2256 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002258
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002259 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2260 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2261 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002262 if ((vsi->type == I40E_VSI_MAIN) &&
2263 (pf->lan_veb != I40E_NO_VEB) &&
2264 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002265 /* set defport ON for Main VSI instead of true promisc
2266 * this way we will get all unicast/multicast and VLAN
2267 * promisc behavior but will not get VF or VMDq traffic
2268 * replicated on the Main VSI.
2269 */
2270 if (pf->cur_promisc != cur_promisc) {
2271 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002272 if (cur_promisc)
2273 aq_ret =
2274 i40e_aq_set_default_vsi(hw,
2275 vsi->seid,
2276 NULL);
2277 else
2278 aq_ret =
2279 i40e_aq_clear_default_vsi(hw,
2280 vsi->seid,
2281 NULL);
2282 if (aq_ret) {
2283 retval = i40e_aq_rc_to_posix(aq_ret,
2284 hw->aq.asq_last_status);
2285 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002286 "Set default VSI failed on %s, err %s, aq_err %s\n",
2287 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002288 i40e_stat_str(hw, aq_ret),
2289 i40e_aq_str(hw,
2290 hw->aq.asq_last_status));
2291 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002292 }
2293 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002294 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002295 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002296 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002297 cur_promisc, NULL,
2298 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002299 if (aq_ret) {
2300 retval =
2301 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002302 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002303 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002304 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2305 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002306 i40e_stat_str(hw, aq_ret),
2307 i40e_aq_str(hw,
2308 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002309 }
2310 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002311 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002312 vsi->seid,
2313 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002314 if (aq_ret) {
2315 retval =
2316 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002317 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002318 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002319 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2320 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002321 i40e_stat_str(hw, aq_ret),
2322 i40e_aq_str(hw,
2323 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002324 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002325 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002326 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2327 vsi->seid,
2328 cur_promisc, NULL);
2329 if (aq_ret) {
2330 retval = i40e_aq_rc_to_posix(aq_ret,
2331 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002332 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002333 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002334 i40e_stat_str(hw, aq_ret),
2335 i40e_aq_str(hw,
2336 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002337 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002338 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002339out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002340 /* if something went wrong then set the changed flag so we try again */
2341 if (retval)
2342 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2343
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002344 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002345 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002346
2347err_no_memory:
2348 /* Restore elements on the temporary add and delete lists */
2349 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002350err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002351 i40e_undo_filter_entries(vsi, &tmp_del_list);
2352 i40e_undo_filter_entries(vsi, &tmp_add_list);
2353 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2354
2355 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2356 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2357 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002358}
2359
2360/**
2361 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2362 * @pf: board private structure
2363 **/
2364static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2365{
2366 int v;
2367
2368 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2369 return;
2370 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2371
Mitch Williams505682c2014-05-20 08:01:37 +00002372 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002373 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002374 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2375 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2376
2377 if (ret) {
2378 /* come back and try again later */
2379 pf->flags |= I40E_FLAG_FILTER_SYNC;
2380 break;
2381 }
2382 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002383 }
2384}
2385
2386/**
2387 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2388 * @netdev: network interface device structure
2389 * @new_mtu: new value for maximum frame size
2390 *
2391 * Returns 0 on success, negative on failure
2392 **/
2393static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2394{
2395 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002396 struct i40e_vsi *vsi = np->vsi;
2397
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398 netdev_info(netdev, "changing MTU from %d to %d\n",
2399 netdev->mtu, new_mtu);
2400 netdev->mtu = new_mtu;
2401 if (netif_running(netdev))
2402 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002403 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002404 return 0;
2405}
2406
2407/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002408 * i40e_ioctl - Access the hwtstamp interface
2409 * @netdev: network interface device structure
2410 * @ifr: interface request data
2411 * @cmd: ioctl command
2412 **/
2413int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2414{
2415 struct i40e_netdev_priv *np = netdev_priv(netdev);
2416 struct i40e_pf *pf = np->vsi->back;
2417
2418 switch (cmd) {
2419 case SIOCGHWTSTAMP:
2420 return i40e_ptp_get_ts_config(pf, ifr);
2421 case SIOCSHWTSTAMP:
2422 return i40e_ptp_set_ts_config(pf, ifr);
2423 default:
2424 return -EOPNOTSUPP;
2425 }
2426}
2427
2428/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2430 * @vsi: the vsi being adjusted
2431 **/
2432void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2433{
2434 struct i40e_vsi_context ctxt;
2435 i40e_status ret;
2436
2437 if ((vsi->info.valid_sections &
2438 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2439 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2440 return; /* already enabled */
2441
2442 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2443 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2444 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2445
2446 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002447 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002448 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2449 if (ret) {
2450 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002451 "update vlan stripping failed, err %s aq_err %s\n",
2452 i40e_stat_str(&vsi->back->hw, ret),
2453 i40e_aq_str(&vsi->back->hw,
2454 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002455 }
2456}
2457
2458/**
2459 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2460 * @vsi: the vsi being adjusted
2461 **/
2462void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2463{
2464 struct i40e_vsi_context ctxt;
2465 i40e_status ret;
2466
2467 if ((vsi->info.valid_sections &
2468 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2469 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2470 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2471 return; /* already disabled */
2472
2473 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2474 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2475 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2476
2477 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002478 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002479 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2480 if (ret) {
2481 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002482 "update vlan stripping failed, err %s aq_err %s\n",
2483 i40e_stat_str(&vsi->back->hw, ret),
2484 i40e_aq_str(&vsi->back->hw,
2485 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002486 }
2487}
2488
2489/**
2490 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2491 * @netdev: network interface to be adjusted
2492 * @features: netdev features to test if VLAN offload is enabled or not
2493 **/
2494static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2495{
2496 struct i40e_netdev_priv *np = netdev_priv(netdev);
2497 struct i40e_vsi *vsi = np->vsi;
2498
2499 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2500 i40e_vlan_stripping_enable(vsi);
2501 else
2502 i40e_vlan_stripping_disable(vsi);
2503}
2504
2505/**
2506 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2507 * @vsi: the vsi being configured
2508 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2509 **/
2510int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2511{
Jacob Keller278e7d02016-10-05 09:30:37 -07002512 struct i40e_mac_filter *f, *add_f, *del_f;
2513 struct hlist_node *h;
2514 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002515
Kiran Patil21659032015-09-30 14:09:03 -04002516 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002517 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002518
Jacob Keller278e7d02016-10-05 09:30:37 -07002519 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002520 if (f->state == I40E_FILTER_REMOVE)
2521 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002522 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002523 if (!add_f) {
2524 dev_info(&vsi->back->pdev->dev,
2525 "Could not add vlan filter %d for %pM\n",
2526 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002527 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002528 return -ENOMEM;
2529 }
2530 }
2531
Jacob Keller3c7cbd42016-11-08 13:05:17 -08002532 /* When we add a new VLAN filter, we need to make sure that all existing
2533 * filters which are marked as vid=-1 (I40E_VLAN_ANY) are converted to
2534 * vid=0. The simplest way is just search for all filters marked as
2535 * vid=-1 and replace them with vid=0. This converts all filters that
2536 * were marked to receive all traffic (tagged or untagged) into
2537 * filters to receive only untagged traffic, so that we don't receive
2538 * tagged traffic for VLANs which we have not configured.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002539 */
Greg Rose8d82a7c2014-01-13 16:13:04 -08002540 if (vid > 0 && !vsi->info.pvid) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002541 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002542 if (f->state == I40E_FILTER_REMOVE)
2543 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002544 del_f = i40e_find_filter(vsi, f->macaddr,
2545 I40E_VLAN_ANY);
2546 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002547 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002548 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002549 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002550 if (!add_f) {
2551 dev_info(&vsi->back->pdev->dev,
2552 "Could not add filter 0 for %pM\n",
2553 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002554 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002555 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002556 }
2557 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002558 }
2559
Jacob Keller278e7d02016-10-05 09:30:37 -07002560 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002561
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002562 /* schedule our worker thread which will take care of
2563 * applying the new filter changes
2564 */
2565 i40e_service_event_schedule(vsi->back);
2566 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002567}
2568
2569/**
2570 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2571 * @vsi: the vsi being configured
2572 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2573 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07002574void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002575{
Alan Brady84f5ca62016-10-05 09:30:39 -07002576 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002577 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002578 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002579
Kiran Patil21659032015-09-30 14:09:03 -04002580 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002581 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002582
Jacob Keller278e7d02016-10-05 09:30:37 -07002583 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002584 if (f->vlan == vid)
2585 __i40e_del_filter(vsi, f);
2586 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002587
Jacob Keller278e7d02016-10-05 09:30:37 -07002588 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002589
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002590 /* schedule our worker thread which will take care of
2591 * applying the new filter changes
2592 */
2593 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002594}
2595
2596/**
2597 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2598 * @netdev: network interface to be adjusted
2599 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002600 *
2601 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002602 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002603#ifdef I40E_FCOE
2604int i40e_vlan_rx_add_vid(struct net_device *netdev,
2605 __always_unused __be16 proto, u16 vid)
2606#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002607static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2608 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002609#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002610{
2611 struct i40e_netdev_priv *np = netdev_priv(netdev);
2612 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002613 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002614
Jacob Keller6a1127852016-10-25 16:08:49 -07002615 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002616 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002617
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002618 /* If the network stack called us with vid = 0 then
2619 * it is asking to receive priority tagged packets with
2620 * vlan id 0. Our HW receives them by default when configured
2621 * to receive untagged packets so there is no need to add an
2622 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002623 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002624 if (vid)
2625 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002626
Jacob Keller6a1127852016-10-25 16:08:49 -07002627 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002628 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002629
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002630 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002631}
2632
2633/**
2634 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2635 * @netdev: network interface to be adjusted
2636 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002637 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002638 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002640#ifdef I40E_FCOE
2641int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2642 __always_unused __be16 proto, u16 vid)
2643#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002644static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2645 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002646#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002647{
2648 struct i40e_netdev_priv *np = netdev_priv(netdev);
2649 struct i40e_vsi *vsi = np->vsi;
2650
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651 /* return code is ignored as there is nothing a user
2652 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002653 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002654 */
2655 i40e_vsi_kill_vlan(vsi, vid);
2656
2657 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002658
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002659 return 0;
2660}
2661
2662/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002663 * i40e_macaddr_init - explicitly write the mac address filters
2664 *
2665 * @vsi: pointer to the vsi
2666 * @macaddr: the MAC address
2667 *
2668 * This is needed when the macaddr has been obtained by other
2669 * means than the default, e.g., from Open Firmware or IDPROM.
2670 * Returns 0 on success, negative on failure
2671 **/
2672static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2673{
2674 int ret;
2675 struct i40e_aqc_add_macvlan_element_data element;
2676
2677 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2678 I40E_AQC_WRITE_TYPE_LAA_WOL,
2679 macaddr, NULL);
2680 if (ret) {
2681 dev_info(&vsi->back->pdev->dev,
2682 "Addr change for VSI failed: %d\n", ret);
2683 return -EADDRNOTAVAIL;
2684 }
2685
2686 memset(&element, 0, sizeof(element));
2687 ether_addr_copy(element.mac_addr, macaddr);
2688 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2689 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2690 if (ret) {
2691 dev_info(&vsi->back->pdev->dev,
2692 "add filter failed err %s aq_err %s\n",
2693 i40e_stat_str(&vsi->back->hw, ret),
2694 i40e_aq_str(&vsi->back->hw,
2695 vsi->back->hw.aq.asq_last_status));
2696 }
2697 return ret;
2698}
2699
2700/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002701 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2702 * @vsi: the vsi being brought back up
2703 **/
2704static void i40e_restore_vlan(struct i40e_vsi *vsi)
2705{
2706 u16 vid;
2707
2708 if (!vsi->netdev)
2709 return;
2710
2711 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2712
2713 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2714 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2715 vid);
2716}
2717
2718/**
2719 * i40e_vsi_add_pvid - Add pvid for the VSI
2720 * @vsi: the vsi being adjusted
2721 * @vid: the vlan id to set as a PVID
2722 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002723int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002724{
2725 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002726 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727
2728 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2729 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002730 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2731 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002732 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002733
2734 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002735 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002736 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2737 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002739 "add pvid failed, err %s aq_err %s\n",
2740 i40e_stat_str(&vsi->back->hw, ret),
2741 i40e_aq_str(&vsi->back->hw,
2742 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002743 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002744 }
2745
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002746 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002747}
2748
2749/**
2750 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2751 * @vsi: the vsi being adjusted
2752 *
2753 * Just use the vlan_rx_register() service to put it back to normal
2754 **/
2755void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2756{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002757 i40e_vlan_stripping_disable(vsi);
2758
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002759 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002760}
2761
2762/**
2763 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2764 * @vsi: ptr to the VSI
2765 *
2766 * If this function returns with an error, then it's possible one or
2767 * more of the rings is populated (while the rest are not). It is the
2768 * callers duty to clean those orphaned rings.
2769 *
2770 * Return 0 on success, negative on failure
2771 **/
2772static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2773{
2774 int i, err = 0;
2775
2776 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002777 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002778
2779 return err;
2780}
2781
2782/**
2783 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2784 * @vsi: ptr to the VSI
2785 *
2786 * Free VSI's transmit software resources
2787 **/
2788static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2789{
2790 int i;
2791
Greg Rose8e9dca52013-12-18 13:45:53 +00002792 if (!vsi->tx_rings)
2793 return;
2794
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002795 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002796 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002797 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002798}
2799
2800/**
2801 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2802 * @vsi: ptr to the VSI
2803 *
2804 * If this function returns with an error, then it's possible one or
2805 * more of the rings is populated (while the rest are not). It is the
2806 * callers duty to clean those orphaned rings.
2807 *
2808 * Return 0 on success, negative on failure
2809 **/
2810static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2811{
2812 int i, err = 0;
2813
2814 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002815 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002816#ifdef I40E_FCOE
2817 i40e_fcoe_setup_ddp_resources(vsi);
2818#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002819 return err;
2820}
2821
2822/**
2823 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2824 * @vsi: ptr to the VSI
2825 *
2826 * Free all receive software resources
2827 **/
2828static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2829{
2830 int i;
2831
Greg Rose8e9dca52013-12-18 13:45:53 +00002832 if (!vsi->rx_rings)
2833 return;
2834
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002835 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002836 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002837 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002838#ifdef I40E_FCOE
2839 i40e_fcoe_free_ddp_resources(vsi);
2840#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002841}
2842
2843/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002844 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2845 * @ring: The Tx ring to configure
2846 *
2847 * This enables/disables XPS for a given Tx descriptor ring
2848 * based on the TCs enabled for the VSI that ring belongs to.
2849 **/
2850static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2851{
2852 struct i40e_vsi *vsi = ring->vsi;
2853 cpumask_var_t mask;
2854
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002855 if (!ring->q_vector || !ring->netdev)
2856 return;
2857
2858 /* Single TC mode enable XPS */
2859 if (vsi->tc_config.numtc <= 1) {
2860 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002861 netif_set_xps_queue(ring->netdev,
2862 &ring->q_vector->affinity_mask,
2863 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002864 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2865 /* Disable XPS to allow selection based on TC */
2866 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2867 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2868 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002869 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002870
2871 /* schedule our worker thread which will take care of
2872 * applying the new filter changes
2873 */
2874 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002875}
2876
2877/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002878 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2879 * @ring: The Tx ring to configure
2880 *
2881 * Configure the Tx descriptor ring in the HMC context.
2882 **/
2883static int i40e_configure_tx_ring(struct i40e_ring *ring)
2884{
2885 struct i40e_vsi *vsi = ring->vsi;
2886 u16 pf_q = vsi->base_queue + ring->queue_index;
2887 struct i40e_hw *hw = &vsi->back->hw;
2888 struct i40e_hmc_obj_txq tx_ctx;
2889 i40e_status err = 0;
2890 u32 qtx_ctl = 0;
2891
2892 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002893 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002894 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2895 ring->atr_count = 0;
2896 } else {
2897 ring->atr_sample_rate = 0;
2898 }
2899
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002900 /* configure XPS */
2901 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002902
2903 /* clear the context structure first */
2904 memset(&tx_ctx, 0, sizeof(tx_ctx));
2905
2906 tx_ctx.new_context = 1;
2907 tx_ctx.base = (ring->dma / 128);
2908 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002909 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2910 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002911#ifdef I40E_FCOE
2912 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2913#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002914 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002915 /* FDIR VSI tx ring can still use RS bit and writebacks */
2916 if (vsi->type != I40E_VSI_FDIR)
2917 tx_ctx.head_wb_ena = 1;
2918 tx_ctx.head_wb_addr = ring->dma +
2919 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002920
2921 /* As part of VSI creation/update, FW allocates certain
2922 * Tx arbitration queue sets for each TC enabled for
2923 * the VSI. The FW returns the handles to these queue
2924 * sets as part of the response buffer to Add VSI,
2925 * Update VSI, etc. AQ commands. It is expected that
2926 * these queue set handles be associated with the Tx
2927 * queues by the driver as part of the TX queue context
2928 * initialization. This has to be done regardless of
2929 * DCB as by default everything is mapped to TC0.
2930 */
2931 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2932 tx_ctx.rdylist_act = 0;
2933
2934 /* clear the context in the HMC */
2935 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2936 if (err) {
2937 dev_info(&vsi->back->pdev->dev,
2938 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2939 ring->queue_index, pf_q, err);
2940 return -ENOMEM;
2941 }
2942
2943 /* set the context in the HMC */
2944 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2945 if (err) {
2946 dev_info(&vsi->back->pdev->dev,
2947 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2948 ring->queue_index, pf_q, err);
2949 return -ENOMEM;
2950 }
2951
2952 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002953 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002954 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002955 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2956 I40E_QTX_CTL_VFVM_INDX_MASK;
2957 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002958 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002959 }
2960
Shannon Nelson13fd9772013-09-28 07:14:19 +00002961 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2962 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002963 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2964 i40e_flush(hw);
2965
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002966 /* cache tail off for easier writes later */
2967 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2968
2969 return 0;
2970}
2971
2972/**
2973 * i40e_configure_rx_ring - Configure a receive ring context
2974 * @ring: The Rx ring to configure
2975 *
2976 * Configure the Rx descriptor ring in the HMC context.
2977 **/
2978static int i40e_configure_rx_ring(struct i40e_ring *ring)
2979{
2980 struct i40e_vsi *vsi = ring->vsi;
2981 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2982 u16 pf_q = vsi->base_queue + ring->queue_index;
2983 struct i40e_hw *hw = &vsi->back->hw;
2984 struct i40e_hmc_obj_rxq rx_ctx;
2985 i40e_status err = 0;
2986
2987 ring->state = 0;
2988
2989 /* clear the context structure first */
2990 memset(&rx_ctx, 0, sizeof(rx_ctx));
2991
2992 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002993
2994 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002995
2996 rx_ctx.base = (ring->dma / 128);
2997 rx_ctx.qlen = ring->count;
2998
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002999 /* use 32 byte descriptors */
3000 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003001
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003002 /* descriptor type is always zero
3003 * rx_ctx.dtype = 0;
3004 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003005 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003007 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003008 if (hw->revision_id == 0)
3009 rx_ctx.lrxqthresh = 0;
3010 else
3011 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003012 rx_ctx.crcstrip = 1;
3013 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003014 /* this controls whether VLAN is stripped from inner headers */
3015 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003016#ifdef I40E_FCOE
3017 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3018#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003019 /* set the prefena field to 1 because the manual says to */
3020 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003021
3022 /* clear the context in the HMC */
3023 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3024 if (err) {
3025 dev_info(&vsi->back->pdev->dev,
3026 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3027 ring->queue_index, pf_q, err);
3028 return -ENOMEM;
3029 }
3030
3031 /* set the context in the HMC */
3032 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3033 if (err) {
3034 dev_info(&vsi->back->pdev->dev,
3035 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3036 ring->queue_index, pf_q, err);
3037 return -ENOMEM;
3038 }
3039
3040 /* cache tail for quicker writes, and clear the reg before use */
3041 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3042 writel(0, ring->tail);
3043
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003044 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003045
3046 return 0;
3047}
3048
3049/**
3050 * i40e_vsi_configure_tx - Configure the VSI for Tx
3051 * @vsi: VSI structure describing this set of rings and resources
3052 *
3053 * Configure the Tx VSI for operation.
3054 **/
3055static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3056{
3057 int err = 0;
3058 u16 i;
3059
Alexander Duyck9f65e152013-09-28 06:00:58 +00003060 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3061 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003062
3063 return err;
3064}
3065
3066/**
3067 * i40e_vsi_configure_rx - Configure the VSI for Rx
3068 * @vsi: the VSI being configured
3069 *
3070 * Configure the Rx VSI for operation.
3071 **/
3072static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3073{
3074 int err = 0;
3075 u16 i;
3076
3077 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3078 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3079 + ETH_FCS_LEN + VLAN_HLEN;
3080 else
3081 vsi->max_frame = I40E_RXBUFFER_2048;
3082
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003083 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084
Vasu Dev38e00432014-08-01 13:27:03 -07003085#ifdef I40E_FCOE
3086 /* setup rx buffer for FCoE */
3087 if ((vsi->type == I40E_VSI_FCOE) &&
3088 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003089 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3090 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003091 }
3092
3093#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003094 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003095 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003096 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003097
3098 /* set up individual rings */
3099 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003100 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003101
3102 return err;
3103}
3104
3105/**
3106 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3107 * @vsi: ptr to the VSI
3108 **/
3109static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3110{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003111 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003112 u16 qoffset, qcount;
3113 int i, n;
3114
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003115 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3116 /* Reset the TC information */
3117 for (i = 0; i < vsi->num_queue_pairs; i++) {
3118 rx_ring = vsi->rx_rings[i];
3119 tx_ring = vsi->tx_rings[i];
3120 rx_ring->dcb_tc = 0;
3121 tx_ring->dcb_tc = 0;
3122 }
3123 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003124
3125 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003126 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127 continue;
3128
3129 qoffset = vsi->tc_config.tc_info[n].qoffset;
3130 qcount = vsi->tc_config.tc_info[n].qcount;
3131 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003132 rx_ring = vsi->rx_rings[i];
3133 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003134 rx_ring->dcb_tc = n;
3135 tx_ring->dcb_tc = n;
3136 }
3137 }
3138}
3139
3140/**
3141 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3142 * @vsi: ptr to the VSI
3143 **/
3144static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3145{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003146 struct i40e_pf *pf = vsi->back;
3147 int err;
3148
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003149 if (vsi->netdev)
3150 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003151
3152 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3153 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3154 if (err) {
3155 dev_warn(&pf->pdev->dev,
3156 "could not set up macaddr; err %d\n", err);
3157 }
3158 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159}
3160
3161/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003162 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3163 * @vsi: Pointer to the targeted VSI
3164 *
3165 * This function replays the hlist on the hw where all the SB Flow Director
3166 * filters were saved.
3167 **/
3168static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3169{
3170 struct i40e_fdir_filter *filter;
3171 struct i40e_pf *pf = vsi->back;
3172 struct hlist_node *node;
3173
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003174 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3175 return;
3176
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003177 hlist_for_each_entry_safe(filter, node,
3178 &pf->fdir_filter_list, fdir_node) {
3179 i40e_add_del_fdir(vsi, filter, true);
3180 }
3181}
3182
3183/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184 * i40e_vsi_configure - Set up the VSI for action
3185 * @vsi: the VSI being configured
3186 **/
3187static int i40e_vsi_configure(struct i40e_vsi *vsi)
3188{
3189 int err;
3190
3191 i40e_set_vsi_rx_mode(vsi);
3192 i40e_restore_vlan(vsi);
3193 i40e_vsi_config_dcb_rings(vsi);
3194 err = i40e_vsi_configure_tx(vsi);
3195 if (!err)
3196 err = i40e_vsi_configure_rx(vsi);
3197
3198 return err;
3199}
3200
3201/**
3202 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3203 * @vsi: the VSI being configured
3204 **/
3205static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3206{
3207 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208 struct i40e_hw *hw = &pf->hw;
3209 u16 vector;
3210 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211 u32 qp;
3212
3213 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3214 * and PFINT_LNKLSTn registers, e.g.:
3215 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3216 */
3217 qp = vsi->base_queue;
3218 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003219 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003220 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3221
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003222 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003223 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003224 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3225 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3226 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003227 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003228 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3229 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3230 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003231 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3232 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233
3234 /* Linked list for the queuepairs assigned to this vector */
3235 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3236 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003237 u32 val;
3238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003239 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3240 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3241 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3242 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3243 (I40E_QUEUE_TYPE_TX
3244 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3245
3246 wr32(hw, I40E_QINT_RQCTL(qp), val);
3247
3248 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3249 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3250 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3251 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3252 (I40E_QUEUE_TYPE_RX
3253 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3254
3255 /* Terminate the linked list */
3256 if (q == (q_vector->num_ringpairs - 1))
3257 val |= (I40E_QUEUE_END_OF_LIST
3258 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3259
3260 wr32(hw, I40E_QINT_TQCTL(qp), val);
3261 qp++;
3262 }
3263 }
3264
3265 i40e_flush(hw);
3266}
3267
3268/**
3269 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3270 * @hw: ptr to the hardware info
3271 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003272static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273{
Jacob Kellerab437b52014-12-14 01:55:08 +00003274 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003275 u32 val;
3276
3277 /* clear things first */
3278 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3279 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3280
3281 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3282 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3283 I40E_PFINT_ICR0_ENA_GRST_MASK |
3284 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3285 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003286 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3287 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3288 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3289
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003290 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3291 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3292
Jacob Kellerab437b52014-12-14 01:55:08 +00003293 if (pf->flags & I40E_FLAG_PTP)
3294 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3295
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3297
3298 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003299 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3300 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 /* OTHER_ITR_IDX = 0 */
3303 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3304}
3305
3306/**
3307 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3308 * @vsi: the VSI being configured
3309 **/
3310static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3311{
Alexander Duyck493fb302013-09-28 07:01:44 +00003312 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003313 struct i40e_pf *pf = vsi->back;
3314 struct i40e_hw *hw = &pf->hw;
3315 u32 val;
3316
3317 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003318 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003319 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3321 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003322 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3324 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3325
Jacob Kellerab437b52014-12-14 01:55:08 +00003326 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327
3328 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3329 wr32(hw, I40E_PFINT_LNKLST0, 0);
3330
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003331 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3333 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3334 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3335
3336 wr32(hw, I40E_QINT_RQCTL(0), val);
3337
3338 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3339 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3340 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3341
3342 wr32(hw, I40E_QINT_TQCTL(0), val);
3343 i40e_flush(hw);
3344}
3345
3346/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003347 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3348 * @pf: board private structure
3349 **/
3350void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3351{
3352 struct i40e_hw *hw = &pf->hw;
3353
3354 wr32(hw, I40E_PFINT_DYN_CTL0,
3355 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3356 i40e_flush(hw);
3357}
3358
3359/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003360 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3361 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003362 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003363 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003364void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365{
3366 struct i40e_hw *hw = &pf->hw;
3367 u32 val;
3368
3369 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003370 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3372
3373 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3374 i40e_flush(hw);
3375}
3376
3377/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003378 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3379 * @irq: interrupt number
3380 * @data: pointer to a q_vector
3381 **/
3382static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3383{
3384 struct i40e_q_vector *q_vector = data;
3385
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003386 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003387 return IRQ_HANDLED;
3388
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003389 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003390
3391 return IRQ_HANDLED;
3392}
3393
3394/**
Alan Brady96db7762016-09-14 16:24:38 -07003395 * i40e_irq_affinity_notify - Callback for affinity changes
3396 * @notify: context as to what irq was changed
3397 * @mask: the new affinity mask
3398 *
3399 * This is a callback function used by the irq_set_affinity_notifier function
3400 * so that we may register to receive changes to the irq affinity masks.
3401 **/
3402static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3403 const cpumask_t *mask)
3404{
3405 struct i40e_q_vector *q_vector =
3406 container_of(notify, struct i40e_q_vector, affinity_notify);
3407
3408 q_vector->affinity_mask = *mask;
3409}
3410
3411/**
3412 * i40e_irq_affinity_release - Callback for affinity notifier release
3413 * @ref: internal core kernel usage
3414 *
3415 * This is a callback function used by the irq_set_affinity_notifier function
3416 * to inform the current notification subscriber that they will no longer
3417 * receive notifications.
3418 **/
3419static void i40e_irq_affinity_release(struct kref *ref) {}
3420
3421/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003422 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3423 * @vsi: the VSI being configured
3424 * @basename: name for the vector
3425 *
3426 * Allocates MSI-X vectors and requests interrupts from the kernel.
3427 **/
3428static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3429{
3430 int q_vectors = vsi->num_q_vectors;
3431 struct i40e_pf *pf = vsi->back;
3432 int base = vsi->base_vector;
3433 int rx_int_idx = 0;
3434 int tx_int_idx = 0;
3435 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003436 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003437
3438 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003439 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003440
Alan Brady96db7762016-09-14 16:24:38 -07003441 irq_num = pf->msix_entries[base + vector].vector;
3442
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003443 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003444 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3445 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3446 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003447 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003448 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3449 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003450 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003451 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3452 "%s-%s-%d", basename, "tx", tx_int_idx++);
3453 } else {
3454 /* skip this unused q_vector */
3455 continue;
3456 }
Alan Brady96db7762016-09-14 16:24:38 -07003457 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003458 vsi->irq_handler,
3459 0,
3460 q_vector->name,
3461 q_vector);
3462 if (err) {
3463 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003464 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465 goto free_queue_irqs;
3466 }
Alan Brady96db7762016-09-14 16:24:38 -07003467
3468 /* register for affinity change notifications */
3469 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3470 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3471 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003472 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003473 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003474 }
3475
Shannon Nelson63741842014-04-23 04:50:16 +00003476 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 return 0;
3478
3479free_queue_irqs:
3480 while (vector) {
3481 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003482 irq_num = pf->msix_entries[base + vector].vector;
3483 irq_set_affinity_notifier(irq_num, NULL);
3484 irq_set_affinity_hint(irq_num, NULL);
3485 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 }
3487 return err;
3488}
3489
3490/**
3491 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3492 * @vsi: the VSI being un-configured
3493 **/
3494static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3495{
3496 struct i40e_pf *pf = vsi->back;
3497 struct i40e_hw *hw = &pf->hw;
3498 int base = vsi->base_vector;
3499 int i;
3500
3501 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003502 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3503 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003504 }
3505
3506 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3507 for (i = vsi->base_vector;
3508 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3509 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3510
3511 i40e_flush(hw);
3512 for (i = 0; i < vsi->num_q_vectors; i++)
3513 synchronize_irq(pf->msix_entries[i + base].vector);
3514 } else {
3515 /* Legacy and MSI mode - this stops all interrupt handling */
3516 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3517 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3518 i40e_flush(hw);
3519 synchronize_irq(pf->pdev->irq);
3520 }
3521}
3522
3523/**
3524 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3525 * @vsi: the VSI being configured
3526 **/
3527static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3528{
3529 struct i40e_pf *pf = vsi->back;
3530 int i;
3531
3532 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003533 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003534 i40e_irq_dynamic_enable(vsi, i);
3535 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003536 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 }
3538
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003539 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540 return 0;
3541}
3542
3543/**
3544 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3545 * @pf: board private structure
3546 **/
3547static void i40e_stop_misc_vector(struct i40e_pf *pf)
3548{
3549 /* Disable ICR 0 */
3550 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3551 i40e_flush(&pf->hw);
3552}
3553
3554/**
3555 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3556 * @irq: interrupt number
3557 * @data: pointer to a q_vector
3558 *
3559 * This is the handler used for all MSI/Legacy interrupts, and deals
3560 * with both queue and non-queue interrupts. This is also used in
3561 * MSIX mode to handle the non-queue interrupts.
3562 **/
3563static irqreturn_t i40e_intr(int irq, void *data)
3564{
3565 struct i40e_pf *pf = (struct i40e_pf *)data;
3566 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003567 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003568 u32 icr0, icr0_remaining;
3569 u32 val, ena_mask;
3570
3571 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003572 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573
Shannon Nelson116a57d2013-09-28 07:13:59 +00003574 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3575 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003576 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003577
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003578 /* if interrupt but no bits showing, must be SWINT */
3579 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3580 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3581 pf->sw_int_count++;
3582
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003583 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3584 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3585 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3586 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003587 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003588 }
3589
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003590 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3591 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003592 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3593 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003594
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003595 /* We do not have a way to disarm Queue causes while leaving
3596 * interrupt enabled for all other causes, ideally
3597 * interrupt should be disabled while we are in NAPI but
3598 * this is not a performance path and napi_schedule()
3599 * can deal with rescheduling.
3600 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003601 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003602 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003603 }
3604
3605 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3606 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3607 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003608 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003609 }
3610
3611 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3612 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3613 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3614 }
3615
3616 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3617 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3618 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3619 }
3620
3621 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3622 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3623 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3624 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3625 val = rd32(hw, I40E_GLGEN_RSTAT);
3626 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3627 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003628 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003629 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003630 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003631 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003632 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003633 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003634 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003635 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003636 }
3637
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003638 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3639 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3640 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003641 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3642 rd32(hw, I40E_PFHMC_ERRORINFO),
3643 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003644 }
3645
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003646 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3647 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3648
3649 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003650 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003651 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003652 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003653 }
3654
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003655 /* If a critical error is pending we have no choice but to reset the
3656 * device.
3657 * Report and mask out any remaining unexpected interrupts.
3658 */
3659 icr0_remaining = icr0 & ena_mask;
3660 if (icr0_remaining) {
3661 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3662 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003663 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003665 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003666 dev_info(&pf->pdev->dev, "device will be reset\n");
3667 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3668 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003669 }
3670 ena_mask &= ~icr0_remaining;
3671 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003672 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003674enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 /* re-enable interrupt causes */
3676 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 if (!test_bit(__I40E_DOWN, &pf->state)) {
3678 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003679 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003680 }
3681
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003682 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683}
3684
3685/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003686 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3687 * @tx_ring: tx ring to clean
3688 * @budget: how many cleans we're allowed
3689 *
3690 * Returns true if there's any budget left (e.g. the clean is finished)
3691 **/
3692static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3693{
3694 struct i40e_vsi *vsi = tx_ring->vsi;
3695 u16 i = tx_ring->next_to_clean;
3696 struct i40e_tx_buffer *tx_buf;
3697 struct i40e_tx_desc *tx_desc;
3698
3699 tx_buf = &tx_ring->tx_bi[i];
3700 tx_desc = I40E_TX_DESC(tx_ring, i);
3701 i -= tx_ring->count;
3702
3703 do {
3704 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3705
3706 /* if next_to_watch is not set then there is no work pending */
3707 if (!eop_desc)
3708 break;
3709
3710 /* prevent any other reads prior to eop_desc */
3711 read_barrier_depends();
3712
3713 /* if the descriptor isn't done, no work yet to do */
3714 if (!(eop_desc->cmd_type_offset_bsz &
3715 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3716 break;
3717
3718 /* clear next_to_watch to prevent false hangs */
3719 tx_buf->next_to_watch = NULL;
3720
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003721 tx_desc->buffer_addr = 0;
3722 tx_desc->cmd_type_offset_bsz = 0;
3723 /* move past filter desc */
3724 tx_buf++;
3725 tx_desc++;
3726 i++;
3727 if (unlikely(!i)) {
3728 i -= tx_ring->count;
3729 tx_buf = tx_ring->tx_bi;
3730 tx_desc = I40E_TX_DESC(tx_ring, 0);
3731 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003732 /* unmap skb header data */
3733 dma_unmap_single(tx_ring->dev,
3734 dma_unmap_addr(tx_buf, dma),
3735 dma_unmap_len(tx_buf, len),
3736 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003737 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3738 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003739
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003740 tx_buf->raw_buf = NULL;
3741 tx_buf->tx_flags = 0;
3742 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003743 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003744 tx_desc->buffer_addr = 0;
3745 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003746
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003747 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003748 tx_buf++;
3749 tx_desc++;
3750 i++;
3751 if (unlikely(!i)) {
3752 i -= tx_ring->count;
3753 tx_buf = tx_ring->tx_bi;
3754 tx_desc = I40E_TX_DESC(tx_ring, 0);
3755 }
3756
3757 /* update budget accounting */
3758 budget--;
3759 } while (likely(budget));
3760
3761 i += tx_ring->count;
3762 tx_ring->next_to_clean = i;
3763
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003764 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003765 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003766
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003767 return budget > 0;
3768}
3769
3770/**
3771 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3772 * @irq: interrupt number
3773 * @data: pointer to a q_vector
3774 **/
3775static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3776{
3777 struct i40e_q_vector *q_vector = data;
3778 struct i40e_vsi *vsi;
3779
3780 if (!q_vector->tx.ring)
3781 return IRQ_HANDLED;
3782
3783 vsi = q_vector->tx.ring->vsi;
3784 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3785
3786 return IRQ_HANDLED;
3787}
3788
3789/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003790 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003791 * @vsi: the VSI being configured
3792 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003793 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003794 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003795static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003796{
Alexander Duyck493fb302013-09-28 07:01:44 +00003797 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003798 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3799 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800
3801 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003802 tx_ring->next = q_vector->tx.ring;
3803 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003804 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003805
3806 rx_ring->q_vector = q_vector;
3807 rx_ring->next = q_vector->rx.ring;
3808 q_vector->rx.ring = rx_ring;
3809 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003810}
3811
3812/**
3813 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3814 * @vsi: the VSI being configured
3815 *
3816 * This function maps descriptor rings to the queue-specific vectors
3817 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3818 * one vector per queue pair, but on a constrained vector budget, we
3819 * group the queue pairs as "efficiently" as possible.
3820 **/
3821static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3822{
3823 int qp_remaining = vsi->num_queue_pairs;
3824 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003825 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003826 int v_start = 0;
3827 int qp_idx = 0;
3828
3829 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3830 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003831 * It is also important to go through all the vectors available to be
3832 * sure that if we don't use all the vectors, that the remaining vectors
3833 * are cleared. This is especially important when decreasing the
3834 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003835 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003836 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003837 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3838
3839 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3840
3841 q_vector->num_ringpairs = num_ringpairs;
3842
3843 q_vector->rx.count = 0;
3844 q_vector->tx.count = 0;
3845 q_vector->rx.ring = NULL;
3846 q_vector->tx.ring = NULL;
3847
3848 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003849 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003850 qp_idx++;
3851 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003852 }
3853 }
3854}
3855
3856/**
3857 * i40e_vsi_request_irq - Request IRQ from the OS
3858 * @vsi: the VSI being configured
3859 * @basename: name for the vector
3860 **/
3861static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3862{
3863 struct i40e_pf *pf = vsi->back;
3864 int err;
3865
3866 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3867 err = i40e_vsi_request_irq_msix(vsi, basename);
3868 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3869 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003870 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003871 else
3872 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003873 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874
3875 if (err)
3876 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3877
3878 return err;
3879}
3880
3881#ifdef CONFIG_NET_POLL_CONTROLLER
3882/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003883 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003884 * @netdev: network interface device structure
3885 *
3886 * This is used by netconsole to send skbs without having to re-enable
3887 * interrupts. It's not called while the normal interrupt routine is executing.
3888 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003889#ifdef I40E_FCOE
3890void i40e_netpoll(struct net_device *netdev)
3891#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003892static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003893#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003894{
3895 struct i40e_netdev_priv *np = netdev_priv(netdev);
3896 struct i40e_vsi *vsi = np->vsi;
3897 struct i40e_pf *pf = vsi->back;
3898 int i;
3899
3900 /* if interface is down do nothing */
3901 if (test_bit(__I40E_DOWN, &vsi->state))
3902 return;
3903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003904 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3905 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003906 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003907 } else {
3908 i40e_intr(pf->pdev->irq, netdev);
3909 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003910}
3911#endif
3912
3913/**
Neerav Parikh23527302014-06-03 23:50:15 +00003914 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3915 * @pf: the PF being configured
3916 * @pf_q: the PF queue
3917 * @enable: enable or disable state of the queue
3918 *
3919 * This routine will wait for the given Tx queue of the PF to reach the
3920 * enabled or disabled state.
3921 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3922 * multiple retries; else will return 0 in case of success.
3923 **/
3924static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3925{
3926 int i;
3927 u32 tx_reg;
3928
3929 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3930 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3931 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3932 break;
3933
Neerav Parikhf98a2002014-09-13 07:40:44 +00003934 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003935 }
3936 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3937 return -ETIMEDOUT;
3938
3939 return 0;
3940}
3941
3942/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003943 * i40e_vsi_control_tx - Start or stop a VSI's rings
3944 * @vsi: the VSI being configured
3945 * @enable: start or stop the rings
3946 **/
3947static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3948{
3949 struct i40e_pf *pf = vsi->back;
3950 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003951 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003952 u32 tx_reg;
3953
3954 pf_q = vsi->base_queue;
3955 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003956
3957 /* warn the TX unit of coming changes */
3958 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3959 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003960 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003961
Mitch Williams6c5ef622014-02-20 19:29:16 -08003962 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003963 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003964 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3965 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3966 break;
3967 usleep_range(1000, 2000);
3968 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003969 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003970 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003971 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003972
3973 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003974 if (enable) {
3975 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003976 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003977 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003978 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003979 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003980
3981 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003982 /* No waiting for the Tx queue to disable */
3983 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3984 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003985
3986 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003987 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3988 if (ret) {
3989 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003990 "VSI seid %d Tx ring %d %sable timeout\n",
3991 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003992 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003993 }
3994 }
3995
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003996 if (hw->revision_id == 0)
3997 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003998 return ret;
3999}
4000
4001/**
4002 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4003 * @pf: the PF being configured
4004 * @pf_q: the PF queue
4005 * @enable: enable or disable state of the queue
4006 *
4007 * This routine will wait for the given Rx queue of the PF to reach the
4008 * enabled or disabled state.
4009 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4010 * multiple retries; else will return 0 in case of success.
4011 **/
4012static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4013{
4014 int i;
4015 u32 rx_reg;
4016
4017 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4018 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4019 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4020 break;
4021
Neerav Parikhf98a2002014-09-13 07:40:44 +00004022 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004023 }
4024 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4025 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004026
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004027 return 0;
4028}
4029
4030/**
4031 * i40e_vsi_control_rx - Start or stop a VSI's rings
4032 * @vsi: the VSI being configured
4033 * @enable: start or stop the rings
4034 **/
4035static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4036{
4037 struct i40e_pf *pf = vsi->back;
4038 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004039 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004040 u32 rx_reg;
4041
4042 pf_q = vsi->base_queue;
4043 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004044 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004045 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004046 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4047 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4048 break;
4049 usleep_range(1000, 2000);
4050 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004051
Catherine Sullivan7c122002014-03-14 07:32:29 +00004052 /* Skip if the queue is already in the requested state */
4053 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4054 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004055
4056 /* turn on/off the queue */
4057 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004058 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004059 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004060 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004061 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004062 /* No waiting for the Tx queue to disable */
4063 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4064 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004065
4066 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004067 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4068 if (ret) {
4069 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004070 "VSI seid %d Rx ring %d %sable timeout\n",
4071 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004072 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004073 }
4074 }
4075
Neerav Parikh23527302014-06-03 23:50:15 +00004076 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004077}
4078
4079/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004080 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004081 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004082 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004083int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004084{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004085 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004086
4087 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004088 ret = i40e_vsi_control_rx(vsi, true);
4089 if (ret)
4090 return ret;
4091 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004092
4093 return ret;
4094}
4095
4096/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004097 * i40e_vsi_stop_rings - Stop a VSI's rings
4098 * @vsi: the VSI being configured
4099 **/
4100void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4101{
4102 /* do rx first for enable and last for disable
4103 * Ignore return value, we need to shutdown whatever we can
4104 */
4105 i40e_vsi_control_tx(vsi, false);
4106 i40e_vsi_control_rx(vsi, false);
4107}
4108
4109/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004110 * i40e_vsi_free_irq - Free the irq association with the OS
4111 * @vsi: the VSI being configured
4112 **/
4113static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4114{
4115 struct i40e_pf *pf = vsi->back;
4116 struct i40e_hw *hw = &pf->hw;
4117 int base = vsi->base_vector;
4118 u32 val, qp;
4119 int i;
4120
4121 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4122 if (!vsi->q_vectors)
4123 return;
4124
Shannon Nelson63741842014-04-23 04:50:16 +00004125 if (!vsi->irqs_ready)
4126 return;
4127
4128 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004129 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004130 int irq_num;
4131 u16 vector;
4132
4133 vector = i + base;
4134 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135
4136 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004137 if (!vsi->q_vectors[i] ||
4138 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004139 continue;
4140
Alan Brady96db7762016-09-14 16:24:38 -07004141 /* clear the affinity notifier in the IRQ descriptor */
4142 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004143 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004144 irq_set_affinity_hint(irq_num, NULL);
4145 synchronize_irq(irq_num);
4146 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004147
4148 /* Tear down the interrupt queue link list
4149 *
4150 * We know that they come in pairs and always
4151 * the Rx first, then the Tx. To clear the
4152 * link list, stick the EOL value into the
4153 * next_q field of the registers.
4154 */
4155 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4156 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4157 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4158 val |= I40E_QUEUE_END_OF_LIST
4159 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4160 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4161
4162 while (qp != I40E_QUEUE_END_OF_LIST) {
4163 u32 next;
4164
4165 val = rd32(hw, I40E_QINT_RQCTL(qp));
4166
4167 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4168 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4169 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4170 I40E_QINT_RQCTL_INTEVENT_MASK);
4171
4172 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4173 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4174
4175 wr32(hw, I40E_QINT_RQCTL(qp), val);
4176
4177 val = rd32(hw, I40E_QINT_TQCTL(qp));
4178
4179 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4180 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4181
4182 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4183 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4184 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4185 I40E_QINT_TQCTL_INTEVENT_MASK);
4186
4187 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4188 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4189
4190 wr32(hw, I40E_QINT_TQCTL(qp), val);
4191 qp = next;
4192 }
4193 }
4194 } else {
4195 free_irq(pf->pdev->irq, pf);
4196
4197 val = rd32(hw, I40E_PFINT_LNKLST0);
4198 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4199 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4200 val |= I40E_QUEUE_END_OF_LIST
4201 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4202 wr32(hw, I40E_PFINT_LNKLST0, val);
4203
4204 val = rd32(hw, I40E_QINT_RQCTL(qp));
4205 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4206 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4207 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4208 I40E_QINT_RQCTL_INTEVENT_MASK);
4209
4210 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4211 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4212
4213 wr32(hw, I40E_QINT_RQCTL(qp), val);
4214
4215 val = rd32(hw, I40E_QINT_TQCTL(qp));
4216
4217 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4218 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4219 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4220 I40E_QINT_TQCTL_INTEVENT_MASK);
4221
4222 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4223 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4224
4225 wr32(hw, I40E_QINT_TQCTL(qp), val);
4226 }
4227}
4228
4229/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004230 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4231 * @vsi: the VSI being configured
4232 * @v_idx: Index of vector to be freed
4233 *
4234 * This function frees the memory allocated to the q_vector. In addition if
4235 * NAPI is enabled it will delete any references to the NAPI struct prior
4236 * to freeing the q_vector.
4237 **/
4238static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4239{
4240 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004241 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004242
4243 if (!q_vector)
4244 return;
4245
4246 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004247 i40e_for_each_ring(ring, q_vector->tx)
4248 ring->q_vector = NULL;
4249
4250 i40e_for_each_ring(ring, q_vector->rx)
4251 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004252
4253 /* only VSI w/ an associated netdev is set up w/ NAPI */
4254 if (vsi->netdev)
4255 netif_napi_del(&q_vector->napi);
4256
4257 vsi->q_vectors[v_idx] = NULL;
4258
4259 kfree_rcu(q_vector, rcu);
4260}
4261
4262/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004263 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4264 * @vsi: the VSI being un-configured
4265 *
4266 * This frees the memory allocated to the q_vectors and
4267 * deletes references to the NAPI struct.
4268 **/
4269static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4270{
4271 int v_idx;
4272
Alexander Duyck493fb302013-09-28 07:01:44 +00004273 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4274 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004275}
4276
4277/**
4278 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4279 * @pf: board private structure
4280 **/
4281static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4282{
4283 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4284 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4285 pci_disable_msix(pf->pdev);
4286 kfree(pf->msix_entries);
4287 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004288 kfree(pf->irq_pile);
4289 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004290 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4291 pci_disable_msi(pf->pdev);
4292 }
4293 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4294}
4295
4296/**
4297 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4298 * @pf: board private structure
4299 *
4300 * We go through and clear interrupt specific resources and reset the structure
4301 * to pre-load conditions
4302 **/
4303static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4304{
4305 int i;
4306
Shannon Nelsone1477582015-02-21 06:44:33 +00004307 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004308 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004309 synchronize_irq(pf->msix_entries[0].vector);
4310 free_irq(pf->msix_entries[0].vector, pf);
4311 }
4312
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004313 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4314 I40E_IWARP_IRQ_PILE_ID);
4315
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004316 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004317 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004318 if (pf->vsi[i])
4319 i40e_vsi_free_q_vectors(pf->vsi[i]);
4320 i40e_reset_interrupt_capability(pf);
4321}
4322
4323/**
4324 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4325 * @vsi: the VSI being configured
4326 **/
4327static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4328{
4329 int q_idx;
4330
4331 if (!vsi->netdev)
4332 return;
4333
4334 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004335 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004336}
4337
4338/**
4339 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4340 * @vsi: the VSI being configured
4341 **/
4342static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4343{
4344 int q_idx;
4345
4346 if (!vsi->netdev)
4347 return;
4348
4349 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004350 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004351}
4352
4353/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004354 * i40e_vsi_close - Shut down a VSI
4355 * @vsi: the vsi to be quelled
4356 **/
4357static void i40e_vsi_close(struct i40e_vsi *vsi)
4358{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004359 bool reset = false;
4360
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004361 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4362 i40e_down(vsi);
4363 i40e_vsi_free_irq(vsi);
4364 i40e_vsi_free_tx_resources(vsi);
4365 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004366 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004367 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4368 reset = true;
4369 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004370}
4371
4372/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004373 * i40e_quiesce_vsi - Pause a given VSI
4374 * @vsi: the VSI being paused
4375 **/
4376static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4377{
4378 if (test_bit(__I40E_DOWN, &vsi->state))
4379 return;
4380
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004381 /* No need to disable FCoE VSI when Tx suspended */
4382 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4383 vsi->type == I40E_VSI_FCOE) {
4384 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004385 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004386 return;
4387 }
4388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004389 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004390 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004391 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004392 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004393 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004394}
4395
4396/**
4397 * i40e_unquiesce_vsi - Resume a given VSI
4398 * @vsi: the VSI being resumed
4399 **/
4400static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4401{
4402 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4403 return;
4404
4405 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4406 if (vsi->netdev && netif_running(vsi->netdev))
4407 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4408 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004409 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004410}
4411
4412/**
4413 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4414 * @pf: the PF
4415 **/
4416static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4417{
4418 int v;
4419
Mitch Williams505682c2014-05-20 08:01:37 +00004420 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004421 if (pf->vsi[v])
4422 i40e_quiesce_vsi(pf->vsi[v]);
4423 }
4424}
4425
4426/**
4427 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4428 * @pf: the PF
4429 **/
4430static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4431{
4432 int v;
4433
Mitch Williams505682c2014-05-20 08:01:37 +00004434 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004435 if (pf->vsi[v])
4436 i40e_unquiesce_vsi(pf->vsi[v]);
4437 }
4438}
4439
Neerav Parikh69129dc2014-11-12 00:18:46 +00004440#ifdef CONFIG_I40E_DCB
4441/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004442 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004443 * @vsi: the VSI being configured
4444 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004445 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004446 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004447static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004448{
4449 struct i40e_pf *pf = vsi->back;
4450 int i, pf_q, ret;
4451
4452 pf_q = vsi->base_queue;
4453 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4454 /* Check and wait for the disable status of the queue */
4455 ret = i40e_pf_txq_wait(pf, pf_q, false);
4456 if (ret) {
4457 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004458 "VSI seid %d Tx ring %d disable timeout\n",
4459 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004460 return ret;
4461 }
4462 }
4463
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004464 pf_q = vsi->base_queue;
4465 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4466 /* Check and wait for the disable status of the queue */
4467 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4468 if (ret) {
4469 dev_info(&pf->pdev->dev,
4470 "VSI seid %d Rx ring %d disable timeout\n",
4471 vsi->seid, pf_q);
4472 return ret;
4473 }
4474 }
4475
Neerav Parikh69129dc2014-11-12 00:18:46 +00004476 return 0;
4477}
4478
4479/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004480 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004481 * @pf: the PF
4482 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004483 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004484 * VSIs that are managed by this PF.
4485 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004486static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004487{
4488 int v, ret = 0;
4489
4490 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004491 /* No need to wait for FCoE VSI queues */
4492 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004493 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004494 if (ret)
4495 break;
4496 }
4497 }
4498
4499 return ret;
4500}
4501
4502#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004503
4504/**
4505 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4506 * @q_idx: TX queue number
4507 * @vsi: Pointer to VSI struct
4508 *
4509 * This function checks specified queue for given VSI. Detects hung condition.
4510 * Sets hung bit since it is two step process. Before next run of service task
4511 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4512 * hung condition remain unchanged and during subsequent run, this function
4513 * issues SW interrupt to recover from hung condition.
4514 **/
4515static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4516{
4517 struct i40e_ring *tx_ring = NULL;
4518 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004519 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004520 int i;
4521
4522 pf = vsi->back;
4523
4524 /* now that we have an index, find the tx_ring struct */
4525 for (i = 0; i < vsi->num_queue_pairs; i++) {
4526 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4527 if (q_idx == vsi->tx_rings[i]->queue_index) {
4528 tx_ring = vsi->tx_rings[i];
4529 break;
4530 }
4531 }
4532 }
4533
4534 if (!tx_ring)
4535 return;
4536
4537 /* Read interrupt register */
4538 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4539 val = rd32(&pf->hw,
4540 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4541 tx_ring->vsi->base_vector - 1));
4542 else
4543 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4544
4545 head = i40e_get_head(tx_ring);
4546
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004547 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004548
Kiran Patil9c6c1252015-11-06 15:26:02 -08004549 /* HW is done executing descriptors, updated HEAD write back,
4550 * but SW hasn't processed those descriptors. If interrupt is
4551 * not generated from this point ON, it could result into
4552 * dev_watchdog detecting timeout on those netdev_queue,
4553 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004554 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004555 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004556 /* NAPI Poll didn't run and clear since it was set */
4557 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4558 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004559 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",
4560 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004561 tx_ring->next_to_clean, head,
4562 tx_ring->next_to_use,
4563 readl(tx_ring->tail));
4564 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4565 vsi->seid, q_idx, val);
4566 i40e_force_wb(vsi, tx_ring->q_vector);
4567 } else {
4568 /* First Chance - detected possible hung */
4569 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4570 &tx_ring->q_vector->hung_detected);
4571 }
4572 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004573
4574 /* This is the case where we have interrupts missing,
4575 * so the tx_pending in HW will most likely be 0, but we
4576 * will have tx_pending in SW since the WB happened but the
4577 * interrupt got lost.
4578 */
4579 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4580 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4581 if (napi_reschedule(&tx_ring->q_vector->napi))
4582 tx_ring->tx_stats.tx_lost_interrupt++;
4583 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004584}
4585
4586/**
4587 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4588 * @pf: pointer to PF struct
4589 *
4590 * LAN VSI has netdev and netdev has TX queues. This function is to check
4591 * each of those TX queues if they are hung, trigger recovery by issuing
4592 * SW interrupt.
4593 **/
4594static void i40e_detect_recover_hung(struct i40e_pf *pf)
4595{
4596 struct net_device *netdev;
4597 struct i40e_vsi *vsi;
4598 int i;
4599
4600 /* Only for LAN VSI */
4601 vsi = pf->vsi[pf->lan_vsi];
4602
4603 if (!vsi)
4604 return;
4605
4606 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4607 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4608 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4609 return;
4610
4611 /* Make sure type is MAIN VSI */
4612 if (vsi->type != I40E_VSI_MAIN)
4613 return;
4614
4615 netdev = vsi->netdev;
4616 if (!netdev)
4617 return;
4618
4619 /* Bail out if netif_carrier is not OK */
4620 if (!netif_carrier_ok(netdev))
4621 return;
4622
4623 /* Go thru' TX queues for netdev */
4624 for (i = 0; i < netdev->num_tx_queues; i++) {
4625 struct netdev_queue *q;
4626
4627 q = netdev_get_tx_queue(netdev, i);
4628 if (q)
4629 i40e_detect_recover_hung_queue(i, vsi);
4630 }
4631}
4632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004633/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004634 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004635 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004636 *
4637 * Get TC map for ISCSI PF type that will include iSCSI TC
4638 * and LAN TC.
4639 **/
4640static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4641{
4642 struct i40e_dcb_app_priority_table app;
4643 struct i40e_hw *hw = &pf->hw;
4644 u8 enabled_tc = 1; /* TC0 is always enabled */
4645 u8 tc, i;
4646 /* Get the iSCSI APP TLV */
4647 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4648
4649 for (i = 0; i < dcbcfg->numapps; i++) {
4650 app = dcbcfg->app[i];
4651 if (app.selector == I40E_APP_SEL_TCPIP &&
4652 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4653 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004654 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004655 break;
4656 }
4657 }
4658
4659 return enabled_tc;
4660}
4661
4662/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004663 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4664 * @dcbcfg: the corresponding DCBx configuration structure
4665 *
4666 * Return the number of TCs from given DCBx configuration
4667 **/
4668static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4669{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004670 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004671 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004672 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004673
4674 /* Scan the ETS Config Priority Table to find
4675 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004676 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004677 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004678 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4679 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4680
4681 /* Now scan the bitmask to check for
4682 * contiguous TCs starting with TC0
4683 */
4684 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4685 if (num_tc & BIT(i)) {
4686 if (!tc_unused) {
4687 ret++;
4688 } else {
4689 pr_err("Non-contiguous TC - Disabling DCB\n");
4690 return 1;
4691 }
4692 } else {
4693 tc_unused = 1;
4694 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004695 }
4696
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004697 /* There is always at least TC0 */
4698 if (!ret)
4699 ret = 1;
4700
4701 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004702}
4703
4704/**
4705 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4706 * @dcbcfg: the corresponding DCBx configuration structure
4707 *
4708 * Query the current DCB configuration and return the number of
4709 * traffic classes enabled from the given DCBX config
4710 **/
4711static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4712{
4713 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4714 u8 enabled_tc = 1;
4715 u8 i;
4716
4717 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004718 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004719
4720 return enabled_tc;
4721}
4722
4723/**
4724 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4725 * @pf: PF being queried
4726 *
4727 * Return number of traffic classes enabled for the given PF
4728 **/
4729static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4730{
4731 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004732 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004733 u8 num_tc = 0;
4734 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4735
4736 /* If DCB is not enabled then always in single TC */
4737 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4738 return 1;
4739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004740 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004741 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4742 return i40e_dcb_get_num_tc(dcbcfg);
4743
4744 /* MFP mode return count of enabled TCs for this PF */
4745 if (pf->hw.func_caps.iscsi)
4746 enabled_tc = i40e_get_iscsi_tc_map(pf);
4747 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004748 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004749
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004750 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004751 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004752 num_tc++;
4753 }
4754 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004755}
4756
4757/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4759 * @pf: PF being queried
4760 *
4761 * Return a bitmap for enabled traffic classes for this PF.
4762 **/
4763static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4764{
4765 /* If DCB is not enabled for this PF then just return default TC */
4766 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004767 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004768
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004769 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004770 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4771 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4772
Neerav Parikhfc51de92015-02-24 06:58:53 +00004773 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004774 if (pf->hw.func_caps.iscsi)
4775 return i40e_get_iscsi_tc_map(pf);
4776 else
David Ertmanea6acb72016-09-20 07:10:50 -07004777 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778}
4779
4780/**
4781 * i40e_vsi_get_bw_info - Query VSI BW Information
4782 * @vsi: the VSI being queried
4783 *
4784 * Returns 0 on success, negative value on failure
4785 **/
4786static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4787{
4788 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4789 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4790 struct i40e_pf *pf = vsi->back;
4791 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004792 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004793 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004794 int i;
4795
4796 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004797 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4798 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004799 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004800 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4801 i40e_stat_str(&pf->hw, ret),
4802 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004803 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004804 }
4805
4806 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004807 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4808 NULL);
4809 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004810 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004811 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4812 i40e_stat_str(&pf->hw, ret),
4813 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004814 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004815 }
4816
4817 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4818 dev_info(&pf->pdev->dev,
4819 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4820 bw_config.tc_valid_bits,
4821 bw_ets_config.tc_valid_bits);
4822 /* Still continuing */
4823 }
4824
4825 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4826 vsi->bw_max_quanta = bw_config.max_bw;
4827 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4828 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4829 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4830 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4831 vsi->bw_ets_limit_credits[i] =
4832 le16_to_cpu(bw_ets_config.credits[i]);
4833 /* 3 bits out of 4 for each TC */
4834 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4835 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004836
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004837 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838}
4839
4840/**
4841 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4842 * @vsi: the VSI being configured
4843 * @enabled_tc: TC bitmap
4844 * @bw_credits: BW shared credits per TC
4845 *
4846 * Returns 0 on success, negative value on failure
4847 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004848static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 u8 *bw_share)
4850{
4851 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004852 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004853 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004854
4855 bw_data.tc_valid_bits = enabled_tc;
4856 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4857 bw_data.tc_bw_credits[i] = bw_share[i];
4858
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004859 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4860 NULL);
4861 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004862 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004863 "AQ command Config VSI BW allocation per TC failed = %d\n",
4864 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004865 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004866 }
4867
4868 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4869 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4870
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004871 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004872}
4873
4874/**
4875 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4876 * @vsi: the VSI being configured
4877 * @enabled_tc: TC map to be enabled
4878 *
4879 **/
4880static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4881{
4882 struct net_device *netdev = vsi->netdev;
4883 struct i40e_pf *pf = vsi->back;
4884 struct i40e_hw *hw = &pf->hw;
4885 u8 netdev_tc = 0;
4886 int i;
4887 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4888
4889 if (!netdev)
4890 return;
4891
4892 if (!enabled_tc) {
4893 netdev_reset_tc(netdev);
4894 return;
4895 }
4896
4897 /* Set up actual enabled TCs on the VSI */
4898 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4899 return;
4900
4901 /* set per TC queues for the VSI */
4902 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4903 /* Only set TC queues for enabled tcs
4904 *
4905 * e.g. For a VSI that has TC0 and TC3 enabled the
4906 * enabled_tc bitmap would be 0x00001001; the driver
4907 * will set the numtc for netdev as 2 that will be
4908 * referenced by the netdev layer as TC 0 and 1.
4909 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004910 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004911 netdev_set_tc_queue(netdev,
4912 vsi->tc_config.tc_info[i].netdev_tc,
4913 vsi->tc_config.tc_info[i].qcount,
4914 vsi->tc_config.tc_info[i].qoffset);
4915 }
4916
4917 /* Assign UP2TC map for the VSI */
4918 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4919 /* Get the actual TC# for the UP */
4920 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4921 /* Get the mapped netdev TC# for the UP */
4922 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4923 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4924 }
4925}
4926
4927/**
4928 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4929 * @vsi: the VSI being configured
4930 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4931 **/
4932static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4933 struct i40e_vsi_context *ctxt)
4934{
4935 /* copy just the sections touched not the entire info
4936 * since not all sections are valid as returned by
4937 * update vsi params
4938 */
4939 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4940 memcpy(&vsi->info.queue_mapping,
4941 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4942 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4943 sizeof(vsi->info.tc_mapping));
4944}
4945
4946/**
4947 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4948 * @vsi: VSI to be configured
4949 * @enabled_tc: TC bitmap
4950 *
4951 * This configures a particular VSI for TCs that are mapped to the
4952 * given TC bitmap. It uses default bandwidth share for TCs across
4953 * VSIs to configure TC for a particular VSI.
4954 *
4955 * NOTE:
4956 * It is expected that the VSI queues have been quisced before calling
4957 * this function.
4958 **/
4959static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4960{
4961 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4962 struct i40e_vsi_context ctxt;
4963 int ret = 0;
4964 int i;
4965
4966 /* Check if enabled_tc is same as existing or new TCs */
4967 if (vsi->tc_config.enabled_tc == enabled_tc)
4968 return ret;
4969
4970 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4971 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004972 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004973 bw_share[i] = 1;
4974 }
4975
4976 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4977 if (ret) {
4978 dev_info(&vsi->back->pdev->dev,
4979 "Failed configuring TC map %d for VSI %d\n",
4980 enabled_tc, vsi->seid);
4981 goto out;
4982 }
4983
4984 /* Update Queue Pairs Mapping for currently enabled UPs */
4985 ctxt.seid = vsi->seid;
4986 ctxt.pf_num = vsi->back->hw.pf_id;
4987 ctxt.vf_num = 0;
4988 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004989 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004990 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4991
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004992 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4993 ctxt.info.valid_sections |=
4994 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4995 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4996 }
4997
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004998 /* Update the VSI after updating the VSI queue-mapping information */
4999 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5000 if (ret) {
5001 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005002 "Update vsi tc config failed, err %s aq_err %s\n",
5003 i40e_stat_str(&vsi->back->hw, ret),
5004 i40e_aq_str(&vsi->back->hw,
5005 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005006 goto out;
5007 }
5008 /* update the local VSI info with updated queue map */
5009 i40e_vsi_update_queue_map(vsi, &ctxt);
5010 vsi->info.valid_sections = 0;
5011
5012 /* Update current VSI BW information */
5013 ret = i40e_vsi_get_bw_info(vsi);
5014 if (ret) {
5015 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005016 "Failed updating vsi bw info, err %s aq_err %s\n",
5017 i40e_stat_str(&vsi->back->hw, ret),
5018 i40e_aq_str(&vsi->back->hw,
5019 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005020 goto out;
5021 }
5022
5023 /* Update the netdev TC setup */
5024 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5025out:
5026 return ret;
5027}
5028
5029/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005030 * i40e_veb_config_tc - Configure TCs for given VEB
5031 * @veb: given VEB
5032 * @enabled_tc: TC bitmap
5033 *
5034 * Configures given TC bitmap for VEB (switching) element
5035 **/
5036int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5037{
5038 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5039 struct i40e_pf *pf = veb->pf;
5040 int ret = 0;
5041 int i;
5042
5043 /* No TCs or already enabled TCs just return */
5044 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5045 return ret;
5046
5047 bw_data.tc_valid_bits = enabled_tc;
5048 /* bw_data.absolute_credits is not set (relative) */
5049
5050 /* Enable ETS TCs with equal BW Share for now */
5051 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005052 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005053 bw_data.tc_bw_share_credits[i] = 1;
5054 }
5055
5056 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5057 &bw_data, NULL);
5058 if (ret) {
5059 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005060 "VEB bw config failed, err %s aq_err %s\n",
5061 i40e_stat_str(&pf->hw, ret),
5062 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005063 goto out;
5064 }
5065
5066 /* Update the BW information */
5067 ret = i40e_veb_get_bw_info(veb);
5068 if (ret) {
5069 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005070 "Failed getting veb bw config, err %s aq_err %s\n",
5071 i40e_stat_str(&pf->hw, ret),
5072 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005073 }
5074
5075out:
5076 return ret;
5077}
5078
5079#ifdef CONFIG_I40E_DCB
5080/**
5081 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5082 * @pf: PF struct
5083 *
5084 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5085 * the caller would've quiesce all the VSIs before calling
5086 * this function
5087 **/
5088static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5089{
5090 u8 tc_map = 0;
5091 int ret;
5092 u8 v;
5093
5094 /* Enable the TCs available on PF to all VEBs */
5095 tc_map = i40e_pf_get_tc_map(pf);
5096 for (v = 0; v < I40E_MAX_VEB; v++) {
5097 if (!pf->veb[v])
5098 continue;
5099 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5100 if (ret) {
5101 dev_info(&pf->pdev->dev,
5102 "Failed configuring TC for VEB seid=%d\n",
5103 pf->veb[v]->seid);
5104 /* Will try to configure as many components */
5105 }
5106 }
5107
5108 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005109 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005110 if (!pf->vsi[v])
5111 continue;
5112
5113 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005114#ifdef I40E_FCOE
5115 * - For FCoE VSI only enable the TC configured
5116 * as per the APP TLV
5117#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005118 * - For all others keep them at TC0 for now
5119 */
5120 if (v == pf->lan_vsi)
5121 tc_map = i40e_pf_get_tc_map(pf);
5122 else
David Ertmanea6acb72016-09-20 07:10:50 -07005123 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005124#ifdef I40E_FCOE
5125 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5126 tc_map = i40e_get_fcoe_tc_map(pf);
5127#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005128
5129 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5130 if (ret) {
5131 dev_info(&pf->pdev->dev,
5132 "Failed configuring TC for VSI seid=%d\n",
5133 pf->vsi[v]->seid);
5134 /* Will try to configure as many components */
5135 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005136 /* Re-configure VSI vectors based on updated TC map */
5137 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005138 if (pf->vsi[v]->netdev)
5139 i40e_dcbnl_set_all(pf->vsi[v]);
5140 }
5141 }
5142}
5143
5144/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005145 * i40e_resume_port_tx - Resume port Tx
5146 * @pf: PF struct
5147 *
5148 * Resume a port's Tx and issue a PF reset in case of failure to
5149 * resume.
5150 **/
5151static int i40e_resume_port_tx(struct i40e_pf *pf)
5152{
5153 struct i40e_hw *hw = &pf->hw;
5154 int ret;
5155
5156 ret = i40e_aq_resume_port_tx(hw, NULL);
5157 if (ret) {
5158 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005159 "Resume Port Tx failed, err %s aq_err %s\n",
5160 i40e_stat_str(&pf->hw, ret),
5161 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005162 /* Schedule PF reset to recover */
5163 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5164 i40e_service_event_schedule(pf);
5165 }
5166
5167 return ret;
5168}
5169
5170/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005171 * i40e_init_pf_dcb - Initialize DCB configuration
5172 * @pf: PF being configured
5173 *
5174 * Query the current DCB configuration and cache it
5175 * in the hardware structure
5176 **/
5177static int i40e_init_pf_dcb(struct i40e_pf *pf)
5178{
5179 struct i40e_hw *hw = &pf->hw;
5180 int err = 0;
5181
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005182 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005183 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005184 goto out;
5185
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005186 /* Get the initial DCB configuration */
5187 err = i40e_init_dcb(hw);
5188 if (!err) {
5189 /* Device/Function is not DCBX capable */
5190 if ((!hw->func_caps.dcb) ||
5191 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5192 dev_info(&pf->pdev->dev,
5193 "DCBX offload is not supported or is disabled for this PF.\n");
5194
5195 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5196 goto out;
5197
5198 } else {
5199 /* When status is not DISABLED then DCBX in FW */
5200 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5201 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005202
5203 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005204 /* Enable DCB tagging only when more than one TC
5205 * or explicitly disable if only one TC
5206 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005207 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5208 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005209 else
5210 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005211 dev_dbg(&pf->pdev->dev,
5212 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005213 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005214 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005215 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005216 "Query for DCB configuration failed, err %s aq_err %s\n",
5217 i40e_stat_str(&pf->hw, err),
5218 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005219 }
5220
5221out:
5222 return err;
5223}
5224#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005225#define SPEED_SIZE 14
5226#define FC_SIZE 8
5227/**
5228 * i40e_print_link_message - print link up or down
5229 * @vsi: the VSI for which link needs a message
5230 */
Matt Jaredc156f852015-08-27 11:42:39 -04005231void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005232{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005233 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005234 char *speed = "Unknown";
5235 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005236
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005237 new_speed = vsi->back->hw.phy.link_info.link_speed;
5238
5239 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005240 return;
5241 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005242 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005243 if (!isup) {
5244 netdev_info(vsi->netdev, "NIC Link is Down\n");
5245 return;
5246 }
5247
Greg Rose148c2d82014-12-11 07:06:27 +00005248 /* Warn user if link speed on NPAR enabled partition is not at
5249 * least 10GB
5250 */
5251 if (vsi->back->hw.func_caps.npar_enable &&
5252 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5253 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5254 netdev_warn(vsi->netdev,
5255 "The partition detected link speed that is less than 10Gbps\n");
5256
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005257 switch (vsi->back->hw.phy.link_info.link_speed) {
5258 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005259 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005260 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005261 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005262 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005263 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005264 case I40E_LINK_SPEED_25GB:
5265 speed = "25 G";
5266 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005267 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005268 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005269 break;
5270 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005271 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005272 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005273 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005274 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005275 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005276 default:
5277 break;
5278 }
5279
5280 switch (vsi->back->hw.fc.current_mode) {
5281 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005282 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005283 break;
5284 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005285 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005286 break;
5287 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005288 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005289 break;
5290 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005291 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005292 break;
5293 }
5294
Shannon Nelsona9165492015-09-03 17:19:00 -04005295 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005296 speed, fc);
5297}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005298
5299/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005300 * i40e_up_complete - Finish the last steps of bringing up a connection
5301 * @vsi: the VSI being configured
5302 **/
5303static int i40e_up_complete(struct i40e_vsi *vsi)
5304{
5305 struct i40e_pf *pf = vsi->back;
5306 int err;
5307
5308 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5309 i40e_vsi_configure_msix(vsi);
5310 else
5311 i40e_configure_msi_and_legacy(vsi);
5312
5313 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005314 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005315 if (err)
5316 return err;
5317
5318 clear_bit(__I40E_DOWN, &vsi->state);
5319 i40e_napi_enable_all(vsi);
5320 i40e_vsi_enable_irq(vsi);
5321
5322 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5323 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005324 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005325 netif_tx_start_all_queues(vsi->netdev);
5326 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005327 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005328 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005329 /* need to check for qualified module here*/
5330 if ((pf->hw.phy.link_info.link_info &
5331 I40E_AQ_MEDIA_AVAILABLE) &&
5332 (!(pf->hw.phy.link_info.an_info &
5333 I40E_AQ_QUALIFIED_MODULE)))
5334 netdev_err(vsi->netdev,
5335 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005336 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005337
5338 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005339 if (vsi->type == I40E_VSI_FDIR) {
5340 /* reset fd counters */
5341 pf->fd_add_err = pf->fd_atr_cnt = 0;
5342 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005343 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005344 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5345 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005346 pf->fd_tcp_rule = 0;
5347 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005348 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005349 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005350
5351 /* On the next run of the service_task, notify any clients of the new
5352 * opened netdev
5353 */
5354 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005355 i40e_service_event_schedule(pf);
5356
5357 return 0;
5358}
5359
5360/**
5361 * i40e_vsi_reinit_locked - Reset the VSI
5362 * @vsi: the VSI being configured
5363 *
5364 * Rebuild the ring structs after some configuration
5365 * has changed, e.g. MTU size.
5366 **/
5367static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5368{
5369 struct i40e_pf *pf = vsi->back;
5370
5371 WARN_ON(in_interrupt());
5372 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5373 usleep_range(1000, 2000);
5374 i40e_down(vsi);
5375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376 i40e_up(vsi);
5377 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5378}
5379
5380/**
5381 * i40e_up - Bring the connection back up after being down
5382 * @vsi: the VSI being configured
5383 **/
5384int i40e_up(struct i40e_vsi *vsi)
5385{
5386 int err;
5387
5388 err = i40e_vsi_configure(vsi);
5389 if (!err)
5390 err = i40e_up_complete(vsi);
5391
5392 return err;
5393}
5394
5395/**
5396 * i40e_down - Shutdown the connection processing
5397 * @vsi: the VSI being stopped
5398 **/
5399void i40e_down(struct i40e_vsi *vsi)
5400{
5401 int i;
5402
5403 /* It is assumed that the caller of this function
5404 * sets the vsi->state __I40E_DOWN bit.
5405 */
5406 if (vsi->netdev) {
5407 netif_carrier_off(vsi->netdev);
5408 netif_tx_disable(vsi->netdev);
5409 }
5410 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005411 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005412 i40e_napi_disable_all(vsi);
5413
5414 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005415 i40e_clean_tx_ring(vsi->tx_rings[i]);
5416 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005417 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005418
5419 i40e_notify_client_of_netdev_close(vsi, false);
5420
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005421}
5422
5423/**
5424 * i40e_setup_tc - configure multiple traffic classes
5425 * @netdev: net device to configure
5426 * @tc: number of traffic classes to enable
5427 **/
5428static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5429{
5430 struct i40e_netdev_priv *np = netdev_priv(netdev);
5431 struct i40e_vsi *vsi = np->vsi;
5432 struct i40e_pf *pf = vsi->back;
5433 u8 enabled_tc = 0;
5434 int ret = -EINVAL;
5435 int i;
5436
5437 /* Check if DCB enabled to continue */
5438 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5439 netdev_info(netdev, "DCB is not enabled for adapter\n");
5440 goto exit;
5441 }
5442
5443 /* Check if MFP enabled */
5444 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5445 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5446 goto exit;
5447 }
5448
5449 /* Check whether tc count is within enabled limit */
5450 if (tc > i40e_pf_get_num_tc(pf)) {
5451 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5452 goto exit;
5453 }
5454
5455 /* Generate TC map for number of tc requested */
5456 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005457 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005458
5459 /* Requesting same TC configuration as already enabled */
5460 if (enabled_tc == vsi->tc_config.enabled_tc)
5461 return 0;
5462
5463 /* Quiesce VSI queues */
5464 i40e_quiesce_vsi(vsi);
5465
5466 /* Configure VSI for enabled TCs */
5467 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5468 if (ret) {
5469 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5470 vsi->seid);
5471 goto exit;
5472 }
5473
5474 /* Unquiesce VSI */
5475 i40e_unquiesce_vsi(vsi);
5476
5477exit:
5478 return ret;
5479}
5480
John Fastabende4c67342016-02-16 21:16:15 -08005481#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005482int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5483 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005484#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005485static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5486 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005487#endif
5488{
John Fastabend16e5cc62016-02-16 21:16:43 -08005489 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005490 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005491 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005492}
5493
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005494/**
5495 * i40e_open - Called when a network interface is made active
5496 * @netdev: network interface device structure
5497 *
5498 * The open entry point is called when a network interface is made
5499 * active by the system (IFF_UP). At this point all resources needed
5500 * for transmit and receive operations are allocated, the interrupt
5501 * handler is registered with the OS, the netdev watchdog subtask is
5502 * enabled, and the stack is notified that the interface is ready.
5503 *
5504 * Returns 0 on success, negative value on failure
5505 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005506int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005507{
5508 struct i40e_netdev_priv *np = netdev_priv(netdev);
5509 struct i40e_vsi *vsi = np->vsi;
5510 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005511 int err;
5512
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005513 /* disallow open during test or if eeprom is broken */
5514 if (test_bit(__I40E_TESTING, &pf->state) ||
5515 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005516 return -EBUSY;
5517
5518 netif_carrier_off(netdev);
5519
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005520 err = i40e_vsi_open(vsi);
5521 if (err)
5522 return err;
5523
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005524 /* configure global TSO hardware offload settings */
5525 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5526 TCP_FLAG_FIN) >> 16);
5527 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5528 TCP_FLAG_FIN |
5529 TCP_FLAG_CWR) >> 16);
5530 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5531
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005532 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005533
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005534 return 0;
5535}
5536
5537/**
5538 * i40e_vsi_open -
5539 * @vsi: the VSI to open
5540 *
5541 * Finish initialization of the VSI.
5542 *
5543 * Returns 0 on success, negative value on failure
5544 **/
5545int i40e_vsi_open(struct i40e_vsi *vsi)
5546{
5547 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005548 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005549 int err;
5550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 /* allocate descriptors */
5552 err = i40e_vsi_setup_tx_resources(vsi);
5553 if (err)
5554 goto err_setup_tx;
5555 err = i40e_vsi_setup_rx_resources(vsi);
5556 if (err)
5557 goto err_setup_rx;
5558
5559 err = i40e_vsi_configure(vsi);
5560 if (err)
5561 goto err_setup_rx;
5562
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005563 if (vsi->netdev) {
5564 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5565 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5566 err = i40e_vsi_request_irq(vsi, int_name);
5567 if (err)
5568 goto err_setup_rx;
5569
5570 /* Notify the stack of the actual queue counts. */
5571 err = netif_set_real_num_tx_queues(vsi->netdev,
5572 vsi->num_queue_pairs);
5573 if (err)
5574 goto err_set_queues;
5575
5576 err = netif_set_real_num_rx_queues(vsi->netdev,
5577 vsi->num_queue_pairs);
5578 if (err)
5579 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005580
5581 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005582 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005583 dev_driver_string(&pf->pdev->dev),
5584 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005585 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005586
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005587 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005588 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005589 goto err_setup_rx;
5590 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005591
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005592 err = i40e_up_complete(vsi);
5593 if (err)
5594 goto err_up_complete;
5595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005596 return 0;
5597
5598err_up_complete:
5599 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005600err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005601 i40e_vsi_free_irq(vsi);
5602err_setup_rx:
5603 i40e_vsi_free_rx_resources(vsi);
5604err_setup_tx:
5605 i40e_vsi_free_tx_resources(vsi);
5606 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005607 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005608
5609 return err;
5610}
5611
5612/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005613 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005614 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005615 *
5616 * This function destroys the hlist where all the Flow Director
5617 * filters were saved.
5618 **/
5619static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5620{
5621 struct i40e_fdir_filter *filter;
5622 struct hlist_node *node2;
5623
5624 hlist_for_each_entry_safe(filter, node2,
5625 &pf->fdir_filter_list, fdir_node) {
5626 hlist_del(&filter->fdir_node);
5627 kfree(filter);
5628 }
5629 pf->fdir_pf_active_filters = 0;
5630}
5631
5632/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005633 * i40e_close - Disables a network interface
5634 * @netdev: network interface device structure
5635 *
5636 * The close entry point is called when an interface is de-activated
5637 * by the OS. The hardware is still under the driver's control, but
5638 * this netdev interface is disabled.
5639 *
5640 * Returns 0, this is not allowed to fail
5641 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005642int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005643{
5644 struct i40e_netdev_priv *np = netdev_priv(netdev);
5645 struct i40e_vsi *vsi = np->vsi;
5646
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005647 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005648
5649 return 0;
5650}
5651
5652/**
5653 * i40e_do_reset - Start a PF or Core Reset sequence
5654 * @pf: board private structure
5655 * @reset_flags: which reset is requested
5656 *
5657 * The essential difference in resets is that the PF Reset
5658 * doesn't clear the packet buffers, doesn't reset the PE
5659 * firmware, and doesn't bother the other PFs on the chip.
5660 **/
5661void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5662{
5663 u32 val;
5664
5665 WARN_ON(in_interrupt());
5666
Mitch Williams263fc482014-04-23 04:50:11 +00005667
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005668 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005669 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005670
5671 /* Request a Global Reset
5672 *
5673 * This will start the chip's countdown to the actual full
5674 * chip reset event, and a warning interrupt to be sent
5675 * to all PFs, including the requestor. Our handler
5676 * for the warning interrupt will deal with the shutdown
5677 * and recovery of the switch setup.
5678 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005679 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005680 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5681 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5682 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5683
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005684 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005685
5686 /* Request a Core Reset
5687 *
5688 * Same as Global Reset, except does *not* include the MAC/PHY
5689 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005690 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5692 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5693 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5694 i40e_flush(&pf->hw);
5695
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005696 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005697
5698 /* Request a PF Reset
5699 *
5700 * Resets only the PF-specific registers
5701 *
5702 * This goes directly to the tear-down and rebuild of
5703 * the switch, since we need to do all the recovery as
5704 * for the Core Reset.
5705 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005706 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005707 i40e_handle_reset_warning(pf);
5708
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005709 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005710 int v;
5711
5712 /* Find the VSI(s) that requested a re-init */
5713 dev_info(&pf->pdev->dev,
5714 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005715 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005716 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005717
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005718 if (vsi != NULL &&
5719 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5720 i40e_vsi_reinit_locked(pf->vsi[v]);
5721 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5722 }
5723 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005724 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005725 int v;
5726
5727 /* Find the VSI(s) that needs to be brought down */
5728 dev_info(&pf->pdev->dev, "VSI down requested\n");
5729 for (v = 0; v < pf->num_alloc_vsi; v++) {
5730 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005731
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005732 if (vsi != NULL &&
5733 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5734 set_bit(__I40E_DOWN, &vsi->state);
5735 i40e_down(vsi);
5736 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5737 }
5738 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005739 } else {
5740 dev_info(&pf->pdev->dev,
5741 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742 }
5743}
5744
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005745#ifdef CONFIG_I40E_DCB
5746/**
5747 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5748 * @pf: board private structure
5749 * @old_cfg: current DCB config
5750 * @new_cfg: new DCB config
5751 **/
5752bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5753 struct i40e_dcbx_config *old_cfg,
5754 struct i40e_dcbx_config *new_cfg)
5755{
5756 bool need_reconfig = false;
5757
5758 /* Check if ETS configuration has changed */
5759 if (memcmp(&new_cfg->etscfg,
5760 &old_cfg->etscfg,
5761 sizeof(new_cfg->etscfg))) {
5762 /* If Priority Table has changed reconfig is needed */
5763 if (memcmp(&new_cfg->etscfg.prioritytable,
5764 &old_cfg->etscfg.prioritytable,
5765 sizeof(new_cfg->etscfg.prioritytable))) {
5766 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005767 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005768 }
5769
5770 if (memcmp(&new_cfg->etscfg.tcbwtable,
5771 &old_cfg->etscfg.tcbwtable,
5772 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005773 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005774
5775 if (memcmp(&new_cfg->etscfg.tsatable,
5776 &old_cfg->etscfg.tsatable,
5777 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005778 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005779 }
5780
5781 /* Check if PFC configuration has changed */
5782 if (memcmp(&new_cfg->pfc,
5783 &old_cfg->pfc,
5784 sizeof(new_cfg->pfc))) {
5785 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005786 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005787 }
5788
5789 /* Check if APP Table has changed */
5790 if (memcmp(&new_cfg->app,
5791 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005792 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005793 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005794 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005795 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005796
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005797 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005798 return need_reconfig;
5799}
5800
5801/**
5802 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5803 * @pf: board private structure
5804 * @e: event info posted on ARQ
5805 **/
5806static int i40e_handle_lldp_event(struct i40e_pf *pf,
5807 struct i40e_arq_event_info *e)
5808{
5809 struct i40e_aqc_lldp_get_mib *mib =
5810 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5811 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005812 struct i40e_dcbx_config tmp_dcbx_cfg;
5813 bool need_reconfig = false;
5814 int ret = 0;
5815 u8 type;
5816
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005817 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005818 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005819 return ret;
5820
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005821 /* Ignore if event is not for Nearest Bridge */
5822 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5823 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005824 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005825 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5826 return ret;
5827
5828 /* Check MIB Type and return if event for Remote MIB update */
5829 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005830 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005831 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005832 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5833 /* Update the remote cached instance and return */
5834 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5835 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5836 &hw->remote_dcbx_config);
5837 goto exit;
5838 }
5839
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005840 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005841 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005842
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005843 /* Reset the old DCBx configuration data */
5844 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005845 /* Get updated DCBX data from firmware */
5846 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005847 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005848 dev_info(&pf->pdev->dev,
5849 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5850 i40e_stat_str(&pf->hw, ret),
5851 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005852 goto exit;
5853 }
5854
5855 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005856 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5857 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005858 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005859 goto exit;
5860 }
5861
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005862 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5863 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005864
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005865 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005866
5867 if (!need_reconfig)
5868 goto exit;
5869
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005870 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005871 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005872 pf->flags |= I40E_FLAG_DCB_ENABLED;
5873 else
5874 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5875
Neerav Parikh69129dc2014-11-12 00:18:46 +00005876 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005877 /* Reconfiguration needed quiesce all VSIs */
5878 i40e_pf_quiesce_all_vsi(pf);
5879
5880 /* Changes in configuration update VEB/VSI */
5881 i40e_dcb_reconfigure(pf);
5882
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005883 ret = i40e_resume_port_tx(pf);
5884
Neerav Parikh69129dc2014-11-12 00:18:46 +00005885 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005886 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005887 if (ret)
5888 goto exit;
5889
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005890 /* Wait for the PF's queues to be disabled */
5891 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005892 if (ret) {
5893 /* Schedule PF reset to recover */
5894 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5895 i40e_service_event_schedule(pf);
5896 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005897 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005898 /* Notify the client for the DCB changes */
5899 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005900 }
5901
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005902exit:
5903 return ret;
5904}
5905#endif /* CONFIG_I40E_DCB */
5906
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005907/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005908 * i40e_do_reset_safe - Protected reset path for userland calls.
5909 * @pf: board private structure
5910 * @reset_flags: which reset is requested
5911 *
5912 **/
5913void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5914{
5915 rtnl_lock();
5916 i40e_do_reset(pf, reset_flags);
5917 rtnl_unlock();
5918}
5919
5920/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005921 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5922 * @pf: board private structure
5923 * @e: event info posted on ARQ
5924 *
5925 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5926 * and VF queues
5927 **/
5928static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5929 struct i40e_arq_event_info *e)
5930{
5931 struct i40e_aqc_lan_overflow *data =
5932 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5933 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5934 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5935 struct i40e_hw *hw = &pf->hw;
5936 struct i40e_vf *vf;
5937 u16 vf_id;
5938
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005939 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5940 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005941
5942 /* Queue belongs to VF, find the VF and issue VF reset */
5943 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5944 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5945 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5946 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5947 vf_id -= hw->func_caps.vf_base_id;
5948 vf = &pf->vf[vf_id];
5949 i40e_vc_notify_vf_reset(vf);
5950 /* Allow VF to process pending reset notification */
5951 msleep(20);
5952 i40e_reset_vf(vf, false);
5953 }
5954}
5955
5956/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005957 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5958 * @pf: board private structure
5959 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005960u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005961{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005962 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005963
5964 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5965 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5966 return fcnt_prog;
5967}
5968
5969/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005970 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005971 * @pf: board private structure
5972 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005973u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005974{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005975 u32 val, fcnt_prog;
5976
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005977 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5978 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5979 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5980 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5981 return fcnt_prog;
5982}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005983
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005984/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005985 * i40e_get_global_fd_count - Get total FD filters programmed on device
5986 * @pf: board private structure
5987 **/
5988u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5989{
5990 u32 val, fcnt_prog;
5991
5992 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5993 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5994 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5995 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5996 return fcnt_prog;
5997}
5998
5999/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006000 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6001 * @pf: board private structure
6002 **/
6003void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6004{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006005 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006006 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006007 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006008
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006009 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6010 return;
6011
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006012 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6013 * to re-enable
6014 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006015 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006016 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006017 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6018 (pf->fd_add_err == 0) ||
6019 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006020 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6021 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6022 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006023 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6024 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 +00006025 }
6026 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006027
6028 /* Wait for some more space to be available to turn on ATR. We also
6029 * must check that no existing ntuple rules for TCP are in effect
6030 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006031 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6032 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07006033 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6034 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006035 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006036 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006037 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 +00006038 }
6039 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006040
6041 /* if hw had a problem adding a filter, delete it */
6042 if (pf->fd_inv > 0) {
6043 hlist_for_each_entry_safe(filter, node,
6044 &pf->fdir_filter_list, fdir_node) {
6045 if (filter->fd_id == pf->fd_inv) {
6046 hlist_del(&filter->fdir_node);
6047 kfree(filter);
6048 pf->fdir_pf_active_filters--;
6049 }
6050 }
6051 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006052}
6053
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006054#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006055#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006056/**
6057 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6058 * @pf: board private structure
6059 **/
6060static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6061{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006062 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006063 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006064 bool disable_atr = false;
6065 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006066 int reg;
6067
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006068 if (!time_after(jiffies, pf->fd_flush_timestamp +
6069 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6070 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006071
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006072 /* If the flush is happening too quick and we have mostly SB rules we
6073 * should not re-enable ATR for some time.
6074 */
6075 min_flush_time = pf->fd_flush_timestamp +
6076 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6077 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006078
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006079 if (!(time_after(jiffies, min_flush_time)) &&
6080 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6081 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6082 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6083 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006084 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006085
6086 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006087 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006088 /* flush all filters */
6089 wr32(&pf->hw, I40E_PFQF_CTL_1,
6090 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6091 i40e_flush(&pf->hw);
6092 pf->fd_flush_cnt++;
6093 pf->fd_add_err = 0;
6094 do {
6095 /* Check FD flush status every 5-6msec */
6096 usleep_range(5000, 6000);
6097 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6098 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6099 break;
6100 } while (flush_wait_retry--);
6101 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6102 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6103 } else {
6104 /* replay sideband filters */
6105 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6106 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006107 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006108 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6109 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6110 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6111 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006112}
6113
6114/**
6115 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6116 * @pf: board private structure
6117 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006118u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006119{
6120 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6121}
6122
6123/* We can see up to 256 filter programming desc in transit if the filters are
6124 * being applied really fast; before we see the first
6125 * filter miss error on Rx queue 0. Accumulating enough error messages before
6126 * reacting will make sure we don't cause flush too often.
6127 */
6128#define I40E_MAX_FD_PROGRAM_ERROR 256
6129
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006130/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006131 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6132 * @pf: board private structure
6133 **/
6134static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6135{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006137 /* if interface is down do nothing */
6138 if (test_bit(__I40E_DOWN, &pf->state))
6139 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006140
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006141 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006142 i40e_fdir_flush_and_replay(pf);
6143
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006144 i40e_fdir_check_and_reenable(pf);
6145
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006146}
6147
6148/**
6149 * i40e_vsi_link_event - notify VSI of a link event
6150 * @vsi: vsi to be notified
6151 * @link_up: link up or down
6152 **/
6153static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6154{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006155 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006156 return;
6157
6158 switch (vsi->type) {
6159 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006160#ifdef I40E_FCOE
6161 case I40E_VSI_FCOE:
6162#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163 if (!vsi->netdev || !vsi->netdev_registered)
6164 break;
6165
6166 if (link_up) {
6167 netif_carrier_on(vsi->netdev);
6168 netif_tx_wake_all_queues(vsi->netdev);
6169 } else {
6170 netif_carrier_off(vsi->netdev);
6171 netif_tx_stop_all_queues(vsi->netdev);
6172 }
6173 break;
6174
6175 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006176 case I40E_VSI_VMDQ2:
6177 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006178 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006179 case I40E_VSI_MIRROR:
6180 default:
6181 /* there is no notification for other VSIs */
6182 break;
6183 }
6184}
6185
6186/**
6187 * i40e_veb_link_event - notify elements on the veb of a link event
6188 * @veb: veb to be notified
6189 * @link_up: link up or down
6190 **/
6191static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6192{
6193 struct i40e_pf *pf;
6194 int i;
6195
6196 if (!veb || !veb->pf)
6197 return;
6198 pf = veb->pf;
6199
6200 /* depth first... */
6201 for (i = 0; i < I40E_MAX_VEB; i++)
6202 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6203 i40e_veb_link_event(pf->veb[i], link_up);
6204
6205 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006206 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006207 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6208 i40e_vsi_link_event(pf->vsi[i], link_up);
6209}
6210
6211/**
6212 * i40e_link_event - Update netif_carrier status
6213 * @pf: board private structure
6214 **/
6215static void i40e_link_event(struct i40e_pf *pf)
6216{
Mitch Williams320684c2014-10-17 03:14:43 +00006217 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006218 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006219 i40e_status status;
6220 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006221
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006222 /* save off old link status information */
6223 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6224
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006225 /* set this to force the get_link_status call to refresh state */
6226 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006227
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006228 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006229
6230 status = i40e_get_link_status(&pf->hw, &new_link);
6231 if (status) {
6232 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6233 status);
6234 return;
6235 }
6236
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006237 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6238 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006239
6240 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006241 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006242 (test_bit(__I40E_DOWN, &vsi->state) ||
6243 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006244 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006245
6246 if (!test_bit(__I40E_DOWN, &vsi->state))
6247 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006248
6249 /* Notify the base of the switch tree connected to
6250 * the link. Floating VEBs are not notified.
6251 */
6252 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6253 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6254 else
Mitch Williams320684c2014-10-17 03:14:43 +00006255 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006256
6257 if (pf->vf)
6258 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006259
6260 if (pf->flags & I40E_FLAG_PTP)
6261 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006262}
6263
6264/**
Shannon Nelson21536712014-10-25 10:35:25 +00006265 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006266 * @pf: board private structure
6267 **/
6268static void i40e_watchdog_subtask(struct i40e_pf *pf)
6269{
6270 int i;
6271
6272 /* if interface is down do nothing */
6273 if (test_bit(__I40E_DOWN, &pf->state) ||
6274 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6275 return;
6276
Shannon Nelson21536712014-10-25 10:35:25 +00006277 /* make sure we don't do these things too often */
6278 if (time_before(jiffies, (pf->service_timer_previous +
6279 pf->service_timer_period)))
6280 return;
6281 pf->service_timer_previous = jiffies;
6282
Shannon Nelson9ac77262015-08-27 11:42:40 -04006283 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6284 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006285
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006286 /* Update the stats for active netdevs so the network stack
6287 * can look at updated numbers whenever it cares to
6288 */
Mitch Williams505682c2014-05-20 08:01:37 +00006289 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006290 if (pf->vsi[i] && pf->vsi[i]->netdev)
6291 i40e_update_stats(pf->vsi[i]);
6292
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006293 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6294 /* Update the stats for the active switching components */
6295 for (i = 0; i < I40E_MAX_VEB; i++)
6296 if (pf->veb[i])
6297 i40e_update_veb_stats(pf->veb[i]);
6298 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006299
6300 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006301}
6302
6303/**
6304 * i40e_reset_subtask - Set up for resetting the device and driver
6305 * @pf: board private structure
6306 **/
6307static void i40e_reset_subtask(struct i40e_pf *pf)
6308{
6309 u32 reset_flags = 0;
6310
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006311 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006312 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006313 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6315 }
6316 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006317 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6319 }
6320 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006321 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6323 }
6324 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006325 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006326 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6327 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006328 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006329 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006330 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6331 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006332
6333 /* If there's a recovery already waiting, it takes
6334 * precedence before starting a new reset sequence.
6335 */
6336 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6337 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006338 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006339 }
6340
6341 /* If we're already down or resetting, just bail */
6342 if (reset_flags &&
6343 !test_bit(__I40E_DOWN, &pf->state) &&
6344 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6345 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006346
6347unlock:
6348 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006349}
6350
6351/**
6352 * i40e_handle_link_event - Handle link event
6353 * @pf: board private structure
6354 * @e: event info posted on ARQ
6355 **/
6356static void i40e_handle_link_event(struct i40e_pf *pf,
6357 struct i40e_arq_event_info *e)
6358{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006359 struct i40e_aqc_get_link_status *status =
6360 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006362 /* Do a new status request to re-enable LSE reporting
6363 * and load new status information into the hw struct
6364 * This completely ignores any state information
6365 * in the ARQ event info, instead choosing to always
6366 * issue the AQ update link status command.
6367 */
6368 i40e_link_event(pf);
6369
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006370 /* check for unqualified module, if link is down */
6371 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6372 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6373 (!(status->link_info & I40E_AQ_LINK_UP)))
6374 dev_err(&pf->pdev->dev,
6375 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006376}
6377
6378/**
6379 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6380 * @pf: board private structure
6381 **/
6382static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6383{
6384 struct i40e_arq_event_info event;
6385 struct i40e_hw *hw = &pf->hw;
6386 u16 pending, i = 0;
6387 i40e_status ret;
6388 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006389 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006390 u32 val;
6391
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006392 /* Do not run clean AQ when PF reset fails */
6393 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6394 return;
6395
Shannon Nelson86df2422014-05-20 08:01:35 +00006396 /* check for error indications */
6397 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6398 oldval = val;
6399 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006400 if (hw->debug_mask & I40E_DEBUG_AQ)
6401 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006402 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6403 }
6404 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006405 if (hw->debug_mask & I40E_DEBUG_AQ)
6406 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006407 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006408 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006409 }
6410 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006411 if (hw->debug_mask & I40E_DEBUG_AQ)
6412 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006413 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6414 }
6415 if (oldval != val)
6416 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6417
6418 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6419 oldval = val;
6420 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006421 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6422 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006423 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6424 }
6425 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006426 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6427 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006428 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6429 }
6430 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006431 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6432 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006433 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6434 }
6435 if (oldval != val)
6436 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6437
Mitch Williams1001dc32014-11-11 20:02:19 +00006438 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6439 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006440 if (!event.msg_buf)
6441 return;
6442
6443 do {
6444 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006445 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006446 break;
Mitch Williams56497972014-06-04 08:45:18 +00006447 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006448 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6449 break;
6450 }
6451
6452 opcode = le16_to_cpu(event.desc.opcode);
6453 switch (opcode) {
6454
6455 case i40e_aqc_opc_get_link_status:
6456 i40e_handle_link_event(pf, &event);
6457 break;
6458 case i40e_aqc_opc_send_msg_to_pf:
6459 ret = i40e_vc_process_vf_msg(pf,
6460 le16_to_cpu(event.desc.retval),
6461 le32_to_cpu(event.desc.cookie_high),
6462 le32_to_cpu(event.desc.cookie_low),
6463 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006464 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006465 break;
6466 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006467 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006468#ifdef CONFIG_I40E_DCB
6469 rtnl_lock();
6470 ret = i40e_handle_lldp_event(pf, &event);
6471 rtnl_unlock();
6472#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006473 break;
6474 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006475 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006476 i40e_handle_lan_overflow_event(pf, &event);
6477 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006478 case i40e_aqc_opc_send_msg_to_peer:
6479 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6480 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006481 case i40e_aqc_opc_nvm_erase:
6482 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006483 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006484 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6485 "ARQ NVM operation 0x%04x completed\n",
6486 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006487 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006488 default:
6489 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006490 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006491 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006492 break;
6493 }
6494 } while (pending && (i++ < pf->adminq_work_limit));
6495
6496 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6497 /* re-enable Admin queue interrupt cause */
6498 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6499 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6500 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6501 i40e_flush(hw);
6502
6503 kfree(event.msg_buf);
6504}
6505
6506/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006507 * i40e_verify_eeprom - make sure eeprom is good to use
6508 * @pf: board private structure
6509 **/
6510static void i40e_verify_eeprom(struct i40e_pf *pf)
6511{
6512 int err;
6513
6514 err = i40e_diag_eeprom_test(&pf->hw);
6515 if (err) {
6516 /* retry in case of garbage read */
6517 err = i40e_diag_eeprom_test(&pf->hw);
6518 if (err) {
6519 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6520 err);
6521 set_bit(__I40E_BAD_EEPROM, &pf->state);
6522 }
6523 }
6524
6525 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6526 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6527 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6528 }
6529}
6530
6531/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006532 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006533 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006534 *
6535 * enable switch loop back or die - no point in a return value
6536 **/
6537static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6538{
6539 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6540 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006541 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006542
6543 ctxt.seid = pf->main_vsi_seid;
6544 ctxt.pf_num = pf->hw.pf_id;
6545 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006546 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6547 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006548 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006549 "couldn't get PF vsi config, err %s aq_err %s\n",
6550 i40e_stat_str(&pf->hw, ret),
6551 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006552 return;
6553 }
6554 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6555 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6556 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6557
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006558 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6559 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006560 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006561 "update vsi switch failed, err %s aq_err %s\n",
6562 i40e_stat_str(&pf->hw, ret),
6563 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006564 }
6565}
6566
6567/**
6568 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006569 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006570 *
6571 * disable switch loop back or die - no point in a return value
6572 **/
6573static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6574{
6575 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6576 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006577 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006578
6579 ctxt.seid = pf->main_vsi_seid;
6580 ctxt.pf_num = pf->hw.pf_id;
6581 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006582 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6583 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006584 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006585 "couldn't get PF vsi config, err %s aq_err %s\n",
6586 i40e_stat_str(&pf->hw, ret),
6587 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006588 return;
6589 }
6590 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6591 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6592 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6593
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006594 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6595 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006596 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006597 "update vsi switch failed, err %s aq_err %s\n",
6598 i40e_stat_str(&pf->hw, ret),
6599 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006600 }
6601}
6602
6603/**
Neerav Parikh51616012015-02-06 08:52:14 +00006604 * i40e_config_bridge_mode - Configure the HW bridge mode
6605 * @veb: pointer to the bridge instance
6606 *
6607 * Configure the loop back mode for the LAN VSI that is downlink to the
6608 * specified HW bridge instance. It is expected this function is called
6609 * when a new HW bridge is instantiated.
6610 **/
6611static void i40e_config_bridge_mode(struct i40e_veb *veb)
6612{
6613 struct i40e_pf *pf = veb->pf;
6614
Shannon Nelson6dec1012015-09-28 14:12:30 -04006615 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6616 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6617 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006618 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6619 i40e_disable_pf_switch_lb(pf);
6620 else
6621 i40e_enable_pf_switch_lb(pf);
6622}
6623
6624/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006625 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6626 * @veb: pointer to the VEB instance
6627 *
6628 * This is a recursive function that first builds the attached VSIs then
6629 * recurses in to build the next layer of VEB. We track the connections
6630 * through our own index numbers because the seid's from the HW could
6631 * change across the reset.
6632 **/
6633static int i40e_reconstitute_veb(struct i40e_veb *veb)
6634{
6635 struct i40e_vsi *ctl_vsi = NULL;
6636 struct i40e_pf *pf = veb->pf;
6637 int v, veb_idx;
6638 int ret;
6639
6640 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006641 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006642 if (pf->vsi[v] &&
6643 pf->vsi[v]->veb_idx == veb->idx &&
6644 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6645 ctl_vsi = pf->vsi[v];
6646 break;
6647 }
6648 }
6649 if (!ctl_vsi) {
6650 dev_info(&pf->pdev->dev,
6651 "missing owner VSI for veb_idx %d\n", veb->idx);
6652 ret = -ENOENT;
6653 goto end_reconstitute;
6654 }
6655 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6656 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6657 ret = i40e_add_vsi(ctl_vsi);
6658 if (ret) {
6659 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006660 "rebuild of veb_idx %d owner VSI failed: %d\n",
6661 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006662 goto end_reconstitute;
6663 }
6664 i40e_vsi_reset_stats(ctl_vsi);
6665
6666 /* create the VEB in the switch and move the VSI onto the VEB */
6667 ret = i40e_add_veb(veb, ctl_vsi);
6668 if (ret)
6669 goto end_reconstitute;
6670
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006671 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6672 veb->bridge_mode = BRIDGE_MODE_VEB;
6673 else
6674 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006675 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006676
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006677 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006678 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006679 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6680 continue;
6681
6682 if (pf->vsi[v]->veb_idx == veb->idx) {
6683 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006684
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 vsi->uplink_seid = veb->seid;
6686 ret = i40e_add_vsi(vsi);
6687 if (ret) {
6688 dev_info(&pf->pdev->dev,
6689 "rebuild of vsi_idx %d failed: %d\n",
6690 v, ret);
6691 goto end_reconstitute;
6692 }
6693 i40e_vsi_reset_stats(vsi);
6694 }
6695 }
6696
6697 /* create any VEBs attached to this VEB - RECURSION */
6698 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6699 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6700 pf->veb[veb_idx]->uplink_seid = veb->seid;
6701 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6702 if (ret)
6703 break;
6704 }
6705 }
6706
6707end_reconstitute:
6708 return ret;
6709}
6710
6711/**
6712 * i40e_get_capabilities - get info about the HW
6713 * @pf: the PF struct
6714 **/
6715static int i40e_get_capabilities(struct i40e_pf *pf)
6716{
6717 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6718 u16 data_size;
6719 int buf_len;
6720 int err;
6721
6722 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6723 do {
6724 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6725 if (!cap_buf)
6726 return -ENOMEM;
6727
6728 /* this loads the data into the hw struct for us */
6729 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6730 &data_size,
6731 i40e_aqc_opc_list_func_capabilities,
6732 NULL);
6733 /* data loaded, buffer no longer needed */
6734 kfree(cap_buf);
6735
6736 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6737 /* retry with a larger buffer */
6738 buf_len = data_size;
6739 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6740 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006741 "capability discovery failed, err %s aq_err %s\n",
6742 i40e_stat_str(&pf->hw, err),
6743 i40e_aq_str(&pf->hw,
6744 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006745 return -ENODEV;
6746 }
6747 } while (err);
6748
6749 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6750 dev_info(&pf->pdev->dev,
6751 "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",
6752 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6753 pf->hw.func_caps.num_msix_vectors,
6754 pf->hw.func_caps.num_msix_vectors_vf,
6755 pf->hw.func_caps.fd_filters_guaranteed,
6756 pf->hw.func_caps.fd_filters_best_effort,
6757 pf->hw.func_caps.num_tx_qp,
6758 pf->hw.func_caps.num_vsis);
6759
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006760#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6761 + pf->hw.func_caps.num_vfs)
6762 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6763 dev_info(&pf->pdev->dev,
6764 "got num_vsis %d, setting num_vsis to %d\n",
6765 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6766 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6767 }
6768
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006769 return 0;
6770}
6771
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006772static int i40e_vsi_clear(struct i40e_vsi *vsi);
6773
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006774/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006775 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006776 * @pf: board private structure
6777 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006778static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779{
6780 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006782 /* quick workaround for an NVM issue that leaves a critical register
6783 * uninitialized
6784 */
6785 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6786 static const u32 hkey[] = {
6787 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6788 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6789 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6790 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006791 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006792
6793 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6794 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6795 }
6796
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006797 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006798 return;
6799
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006800 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006801 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006802
6803 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006805 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6806 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006807 if (!vsi) {
6808 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006809 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6810 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006811 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006812 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006813
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006814 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815}
6816
6817/**
6818 * i40e_fdir_teardown - release the Flow Director resources
6819 * @pf: board private structure
6820 **/
6821static void i40e_fdir_teardown(struct i40e_pf *pf)
6822{
Alexander Duyck4b816442016-10-11 15:26:53 -07006823 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006824
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006825 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006826 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6827 if (vsi)
6828 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006829}
6830
6831/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006832 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833 * @pf: board private structure
6834 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006835 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006836 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006837static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006839 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006840 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006841 u32 v;
6842
6843 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6844 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006845 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006846 if (i40e_check_asq_alive(&pf->hw))
6847 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006849 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006850
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006851 /* quiesce the VSIs and their queues that are not already DOWN */
6852 i40e_pf_quiesce_all_vsi(pf);
6853
Mitch Williams505682c2014-05-20 08:01:37 +00006854 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855 if (pf->vsi[v])
6856 pf->vsi[v]->seid = 0;
6857 }
6858
6859 i40e_shutdown_adminq(&pf->hw);
6860
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006861 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006862 if (hw->hmc.hmc_obj) {
6863 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006864 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006865 dev_warn(&pf->pdev->dev,
6866 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006867 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006868}
6869
6870/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006871 * i40e_send_version - update firmware with driver version
6872 * @pf: PF struct
6873 */
6874static void i40e_send_version(struct i40e_pf *pf)
6875{
6876 struct i40e_driver_version dv;
6877
6878 dv.major_version = DRV_VERSION_MAJOR;
6879 dv.minor_version = DRV_VERSION_MINOR;
6880 dv.build_version = DRV_VERSION_BUILD;
6881 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006882 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006883 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6884}
6885
6886/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006887 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006888 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006889 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006890 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006891static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006892{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006893 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006894 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006895 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006896 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006897 u32 v;
6898
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006899 /* Now we wait for GRST to settle out.
6900 * We don't have to delete the VEBs or VSIs from the hw switch
6901 * because the reset will make them disappear.
6902 */
6903 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006904 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006905 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006906 set_bit(__I40E_RESET_FAILED, &pf->state);
6907 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006908 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006909 pf->pfr_count++;
6910
6911 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006912 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006913 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914
6915 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6916 ret = i40e_init_adminq(&pf->hw);
6917 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006918 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6919 i40e_stat_str(&pf->hw, ret),
6920 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006921 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006922 }
6923
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006924 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006925 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006926 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006927
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006928 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006929 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006930 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006931 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006932
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006933 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6934 hw->func_caps.num_rx_qp,
6935 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6936 if (ret) {
6937 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6938 goto end_core_reset;
6939 }
6940 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6941 if (ret) {
6942 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6943 goto end_core_reset;
6944 }
6945
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006946#ifdef CONFIG_I40E_DCB
6947 ret = i40e_init_pf_dcb(pf);
6948 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006949 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6950 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6951 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006952 }
6953#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006954#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006955 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006956
Vasu Dev38e00432014-08-01 13:27:03 -07006957#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006959 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960 if (ret)
6961 goto end_core_reset;
6962
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006963 /* The driver only wants link up/down and module qualification
6964 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006965 */
6966 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006967 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006968 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006969 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006970 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006971 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6972 i40e_stat_str(&pf->hw, ret),
6973 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006974
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006975 /* make sure our flow control settings are restored */
6976 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6977 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006978 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6979 i40e_stat_str(&pf->hw, ret),
6980 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006981
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006982 /* Rebuild the VSIs and VEBs that existed before reset.
6983 * They are still in our local switch element arrays, so only
6984 * need to rebuild the switch model in the HW.
6985 *
6986 * If there were VEBs but the reconstitution failed, we'll try
6987 * try to recover minimal use by getting the basic PF VSI working.
6988 */
6989 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006990 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006991 /* find the one VEB connected to the MAC, and find orphans */
6992 for (v = 0; v < I40E_MAX_VEB; v++) {
6993 if (!pf->veb[v])
6994 continue;
6995
6996 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6997 pf->veb[v]->uplink_seid == 0) {
6998 ret = i40e_reconstitute_veb(pf->veb[v]);
6999
7000 if (!ret)
7001 continue;
7002
7003 /* If Main VEB failed, we're in deep doodoo,
7004 * so give up rebuilding the switch and set up
7005 * for minimal rebuild of PF VSI.
7006 * If orphan failed, we'll report the error
7007 * but try to keep going.
7008 */
7009 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7010 dev_info(&pf->pdev->dev,
7011 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7012 ret);
7013 pf->vsi[pf->lan_vsi]->uplink_seid
7014 = pf->mac_seid;
7015 break;
7016 } else if (pf->veb[v]->uplink_seid == 0) {
7017 dev_info(&pf->pdev->dev,
7018 "rebuild of orphan VEB failed: %d\n",
7019 ret);
7020 }
7021 }
7022 }
7023 }
7024
7025 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007026 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007027 /* no VEB, so rebuild only the Main VSI */
7028 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7029 if (ret) {
7030 dev_info(&pf->pdev->dev,
7031 "rebuild of Main VSI failed: %d\n", ret);
7032 goto end_core_reset;
7033 }
7034 }
7035
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007036 /* Reconfigure hardware for allowing smaller MSS in the case
7037 * of TSO, so that we avoid the MDD being fired and causing
7038 * a reset in the case of small MSS+TSO.
7039 */
7040#define I40E_REG_MSS 0x000E64DC
7041#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7042#define I40E_64BYTE_MSS 0x400000
7043 val = rd32(hw, I40E_REG_MSS);
7044 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7045 val &= ~I40E_REG_MSS_MIN_MASK;
7046 val |= I40E_64BYTE_MSS;
7047 wr32(hw, I40E_REG_MSS, val);
7048 }
7049
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007050 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007051 msleep(75);
7052 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7053 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007054 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7055 i40e_stat_str(&pf->hw, ret),
7056 i40e_aq_str(&pf->hw,
7057 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007058 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007059 /* reinit the misc interrupt */
7060 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7061 ret = i40e_setup_misc_vector(pf);
7062
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007063 /* Add a filter to drop all Flow control frames from any VSI from being
7064 * transmitted. By doing so we stop a malicious VF from sending out
7065 * PAUSE or PFC frames and potentially controlling traffic for other
7066 * PF/VF VSIs.
7067 * The FW can still send Flow control frames if enabled.
7068 */
7069 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7070 pf->main_vsi_seid);
7071
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007072 /* restart the VSIs that were rebuilt and running before the reset */
7073 i40e_pf_unquiesce_all_vsi(pf);
7074
Mitch Williams69f64b22014-02-13 03:48:46 -08007075 if (pf->num_alloc_vfs) {
7076 for (v = 0; v < pf->num_alloc_vfs; v++)
7077 i40e_reset_vf(&pf->vf[v], true);
7078 }
7079
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007080 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007081 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007082
7083end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007084 clear_bit(__I40E_RESET_FAILED, &pf->state);
7085clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007086 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7087}
7088
7089/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007090 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007091 * @pf: board private structure
7092 *
7093 * Close up the VFs and other things in prep for a Core Reset,
7094 * then get ready to rebuild the world.
7095 **/
7096static void i40e_handle_reset_warning(struct i40e_pf *pf)
7097{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007098 i40e_prep_for_reset(pf);
7099 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007100}
7101
7102/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007103 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007104 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007105 *
7106 * Called from the MDD irq handler to identify possibly malicious vfs
7107 **/
7108static void i40e_handle_mdd_event(struct i40e_pf *pf)
7109{
7110 struct i40e_hw *hw = &pf->hw;
7111 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007112 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007113 struct i40e_vf *vf;
7114 u32 reg;
7115 int i;
7116
7117 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7118 return;
7119
7120 /* find what triggered the MDD event */
7121 reg = rd32(hw, I40E_GL_MDET_TX);
7122 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007123 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7124 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007125 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007126 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007127 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007128 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007129 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7130 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7131 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007132 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007133 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 +00007134 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007135 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7136 mdd_detected = true;
7137 }
7138 reg = rd32(hw, I40E_GL_MDET_RX);
7139 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007140 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7141 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007142 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007143 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007144 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7145 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7146 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007147 if (netif_msg_rx_err(pf))
7148 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7149 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007150 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7151 mdd_detected = true;
7152 }
7153
Neerav Parikhdf430b12014-06-04 01:23:15 +00007154 if (mdd_detected) {
7155 reg = rd32(hw, I40E_PF_MDET_TX);
7156 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7157 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007158 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007159 pf_mdd_detected = true;
7160 }
7161 reg = rd32(hw, I40E_PF_MDET_RX);
7162 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7163 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007164 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007165 pf_mdd_detected = true;
7166 }
7167 /* Queue belongs to the PF, initiate a reset */
7168 if (pf_mdd_detected) {
7169 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7170 i40e_service_event_schedule(pf);
7171 }
7172 }
7173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007174 /* see if one of the VFs needs its hand slapped */
7175 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7176 vf = &(pf->vf[i]);
7177 reg = rd32(hw, I40E_VP_MDET_TX(i));
7178 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7179 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7180 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007181 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7182 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007183 }
7184
7185 reg = rd32(hw, I40E_VP_MDET_RX(i));
7186 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7187 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7188 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007189 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7190 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007191 }
7192
7193 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7194 dev_info(&pf->pdev->dev,
7195 "Too many MDD events on VF %d, disabled\n", i);
7196 dev_info(&pf->pdev->dev,
7197 "Use PF Control I/F to re-enable the VF\n");
7198 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7199 }
7200 }
7201
7202 /* re-enable mdd interrupt cause */
7203 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7204 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7205 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7206 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7207 i40e_flush(hw);
7208}
7209
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007210/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007211 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007212 * @pf: board private structure
7213 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007214static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007215{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007216 struct i40e_hw *hw = &pf->hw;
7217 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007218 __be16 port;
7219 int i;
7220
Singhai, Anjali6a899022015-12-14 12:21:18 -08007221 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007222 return;
7223
Singhai, Anjali6a899022015-12-14 12:21:18 -08007224 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007225
7226 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007227 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7228 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7229 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007230 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007231 ret = i40e_aq_add_udp_tunnel(hw, port,
7232 pf->udp_ports[i].type,
7233 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007234 else
7235 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007236
7237 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007238 dev_dbg(&pf->pdev->dev,
7239 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7240 pf->udp_ports[i].type ? "vxlan" : "geneve",
7241 port ? "add" : "delete",
7242 ntohs(port), i,
7243 i40e_stat_str(&pf->hw, ret),
7244 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007245 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007246 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007247 }
7248 }
7249 }
7250}
7251
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007252/**
7253 * i40e_service_task - Run the driver's async subtasks
7254 * @work: pointer to work_struct containing our data
7255 **/
7256static void i40e_service_task(struct work_struct *work)
7257{
7258 struct i40e_pf *pf = container_of(work,
7259 struct i40e_pf,
7260 service_task);
7261 unsigned long start_time = jiffies;
7262
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007263 /* don't bother with service tasks if a reset is in progress */
7264 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007265 return;
7266 }
7267
Mitch Williams91089032016-11-21 13:03:51 -08007268 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7269 return;
7270
Kiran Patilb03a8c12015-09-24 18:13:15 -04007271 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007272 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007273 i40e_reset_subtask(pf);
7274 i40e_handle_mdd_event(pf);
7275 i40e_vc_process_vflr_event(pf);
7276 i40e_watchdog_subtask(pf);
7277 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007278 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007279 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007280 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007281 i40e_clean_adminq_subtask(pf);
7282
Mitch Williams91089032016-11-21 13:03:51 -08007283 /* flush memory to make sure state is correct before next watchdog */
7284 smp_mb__before_atomic();
7285 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007286
7287 /* If the tasks have taken longer than one timer cycle or there
7288 * is more work to be done, reschedule the service task now
7289 * rather than wait for the timer to tick again.
7290 */
7291 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7292 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7293 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7294 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7295 i40e_service_event_schedule(pf);
7296}
7297
7298/**
7299 * i40e_service_timer - timer callback
7300 * @data: pointer to PF struct
7301 **/
7302static void i40e_service_timer(unsigned long data)
7303{
7304 struct i40e_pf *pf = (struct i40e_pf *)data;
7305
7306 mod_timer(&pf->service_timer,
7307 round_jiffies(jiffies + pf->service_timer_period));
7308 i40e_service_event_schedule(pf);
7309}
7310
7311/**
7312 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7313 * @vsi: the VSI being configured
7314 **/
7315static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7316{
7317 struct i40e_pf *pf = vsi->back;
7318
7319 switch (vsi->type) {
7320 case I40E_VSI_MAIN:
7321 vsi->alloc_queue_pairs = pf->num_lan_qps;
7322 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7323 I40E_REQ_DESCRIPTOR_MULTIPLE);
7324 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7325 vsi->num_q_vectors = pf->num_lan_msix;
7326 else
7327 vsi->num_q_vectors = 1;
7328
7329 break;
7330
7331 case I40E_VSI_FDIR:
7332 vsi->alloc_queue_pairs = 1;
7333 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7334 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007335 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336 break;
7337
7338 case I40E_VSI_VMDQ2:
7339 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7340 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7341 I40E_REQ_DESCRIPTOR_MULTIPLE);
7342 vsi->num_q_vectors = pf->num_vmdq_msix;
7343 break;
7344
7345 case I40E_VSI_SRIOV:
7346 vsi->alloc_queue_pairs = pf->num_vf_qps;
7347 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7348 I40E_REQ_DESCRIPTOR_MULTIPLE);
7349 break;
7350
Vasu Dev38e00432014-08-01 13:27:03 -07007351#ifdef I40E_FCOE
7352 case I40E_VSI_FCOE:
7353 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7354 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7355 I40E_REQ_DESCRIPTOR_MULTIPLE);
7356 vsi->num_q_vectors = pf->num_fcoe_msix;
7357 break;
7358
7359#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007360 default:
7361 WARN_ON(1);
7362 return -ENODATA;
7363 }
7364
7365 return 0;
7366}
7367
7368/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007369 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7370 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007371 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007372 *
7373 * On error: returns error code (negative)
7374 * On success: returns 0
7375 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007376static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007377{
7378 int size;
7379 int ret = 0;
7380
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007381 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007382 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7383 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7384 if (!vsi->tx_rings)
7385 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007386 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7387
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007388 if (alloc_qvectors) {
7389 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007390 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007391 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7392 if (!vsi->q_vectors) {
7393 ret = -ENOMEM;
7394 goto err_vectors;
7395 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007396 }
7397 return ret;
7398
7399err_vectors:
7400 kfree(vsi->tx_rings);
7401 return ret;
7402}
7403
7404/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007405 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7406 * @pf: board private structure
7407 * @type: type of VSI
7408 *
7409 * On error: returns error code (negative)
7410 * On success: returns vsi index in PF (positive)
7411 **/
7412static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7413{
7414 int ret = -ENODEV;
7415 struct i40e_vsi *vsi;
7416 int vsi_idx;
7417 int i;
7418
7419 /* Need to protect the allocation of the VSIs at the PF level */
7420 mutex_lock(&pf->switch_mutex);
7421
7422 /* VSI list may be fragmented if VSI creation/destruction has
7423 * been happening. We can afford to do a quick scan to look
7424 * for any free VSIs in the list.
7425 *
7426 * find next empty vsi slot, looping back around if necessary
7427 */
7428 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007429 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007430 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007431 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007432 i = 0;
7433 while (i < pf->next_vsi && pf->vsi[i])
7434 i++;
7435 }
7436
Mitch Williams505682c2014-05-20 08:01:37 +00007437 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007438 vsi_idx = i; /* Found one! */
7439 } else {
7440 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007441 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007442 }
7443 pf->next_vsi = ++i;
7444
7445 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7446 if (!vsi) {
7447 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007448 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007449 }
7450 vsi->type = type;
7451 vsi->back = pf;
7452 set_bit(__I40E_DOWN, &vsi->state);
7453 vsi->flags = 0;
7454 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007455 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007456 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7457 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007458 vsi->netdev_registered = false;
7459 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007460 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007461 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007462
Alexander Duyck9f65e152013-09-28 06:00:58 +00007463 ret = i40e_set_num_rings_in_vsi(vsi);
7464 if (ret)
7465 goto err_rings;
7466
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007467 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007468 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007469 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 /* Setup default MSIX irq handler for VSI */
7472 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7473
Kiran Patil21659032015-09-30 14:09:03 -04007474 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007475 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007476 pf->vsi[vsi_idx] = vsi;
7477 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007478 goto unlock_pf;
7479
Alexander Duyck9f65e152013-09-28 06:00:58 +00007480err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007481 pf->next_vsi = i - 1;
7482 kfree(vsi);
7483unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007484 mutex_unlock(&pf->switch_mutex);
7485 return ret;
7486}
7487
7488/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007489 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7490 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007491 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007492 *
7493 * On error: returns error code (negative)
7494 * On success: returns 0
7495 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007496static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007497{
7498 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007499 if (free_qvectors) {
7500 kfree(vsi->q_vectors);
7501 vsi->q_vectors = NULL;
7502 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007503 kfree(vsi->tx_rings);
7504 vsi->tx_rings = NULL;
7505 vsi->rx_rings = NULL;
7506}
7507
7508/**
Helin Zhang28c58692015-10-26 19:44:27 -04007509 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7510 * and lookup table
7511 * @vsi: Pointer to VSI structure
7512 */
7513static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7514{
7515 if (!vsi)
7516 return;
7517
7518 kfree(vsi->rss_hkey_user);
7519 vsi->rss_hkey_user = NULL;
7520
7521 kfree(vsi->rss_lut_user);
7522 vsi->rss_lut_user = NULL;
7523}
7524
7525/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007526 * i40e_vsi_clear - Deallocate the VSI provided
7527 * @vsi: the VSI being un-configured
7528 **/
7529static int i40e_vsi_clear(struct i40e_vsi *vsi)
7530{
7531 struct i40e_pf *pf;
7532
7533 if (!vsi)
7534 return 0;
7535
7536 if (!vsi->back)
7537 goto free_vsi;
7538 pf = vsi->back;
7539
7540 mutex_lock(&pf->switch_mutex);
7541 if (!pf->vsi[vsi->idx]) {
7542 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7543 vsi->idx, vsi->idx, vsi, vsi->type);
7544 goto unlock_vsi;
7545 }
7546
7547 if (pf->vsi[vsi->idx] != vsi) {
7548 dev_err(&pf->pdev->dev,
7549 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7550 pf->vsi[vsi->idx]->idx,
7551 pf->vsi[vsi->idx],
7552 pf->vsi[vsi->idx]->type,
7553 vsi->idx, vsi, vsi->type);
7554 goto unlock_vsi;
7555 }
7556
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007557 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007558 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7559 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7560
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007561 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007562 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007563
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007564 pf->vsi[vsi->idx] = NULL;
7565 if (vsi->idx < pf->next_vsi)
7566 pf->next_vsi = vsi->idx;
7567
7568unlock_vsi:
7569 mutex_unlock(&pf->switch_mutex);
7570free_vsi:
7571 kfree(vsi);
7572
7573 return 0;
7574}
7575
7576/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007577 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7578 * @vsi: the VSI being cleaned
7579 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007580static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007581{
7582 int i;
7583
Greg Rose8e9dca52013-12-18 13:45:53 +00007584 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007585 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007586 kfree_rcu(vsi->tx_rings[i], rcu);
7587 vsi->tx_rings[i] = NULL;
7588 vsi->rx_rings[i] = NULL;
7589 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007590 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007591}
7592
7593/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007594 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7595 * @vsi: the VSI being configured
7596 **/
7597static int i40e_alloc_rings(struct i40e_vsi *vsi)
7598{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007599 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007600 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007601 int i;
7602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007603 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007604 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007605 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007606 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7607 if (!tx_ring)
7608 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007609
7610 tx_ring->queue_index = i;
7611 tx_ring->reg_idx = vsi->base_queue + i;
7612 tx_ring->ring_active = false;
7613 tx_ring->vsi = vsi;
7614 tx_ring->netdev = vsi->netdev;
7615 tx_ring->dev = &pf->pdev->dev;
7616 tx_ring->count = vsi->num_desc;
7617 tx_ring->size = 0;
7618 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007619 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7620 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007621 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007622 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007623
Alexander Duyck9f65e152013-09-28 06:00:58 +00007624 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007625 rx_ring->queue_index = i;
7626 rx_ring->reg_idx = vsi->base_queue + i;
7627 rx_ring->ring_active = false;
7628 rx_ring->vsi = vsi;
7629 rx_ring->netdev = vsi->netdev;
7630 rx_ring->dev = &pf->pdev->dev;
7631 rx_ring->count = vsi->num_desc;
7632 rx_ring->size = 0;
7633 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007634 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007635 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007636 }
7637
7638 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007639
7640err_out:
7641 i40e_vsi_clear_rings(vsi);
7642 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007643}
7644
7645/**
7646 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7647 * @pf: board private structure
7648 * @vectors: the number of MSI-X vectors to request
7649 *
7650 * Returns the number of vectors reserved, or error
7651 **/
7652static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7653{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007654 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7655 I40E_MIN_MSIX, vectors);
7656 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007657 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007658 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007659 vectors = 0;
7660 }
7661
7662 return vectors;
7663}
7664
7665/**
7666 * i40e_init_msix - Setup the MSIX capability
7667 * @pf: board private structure
7668 *
7669 * Work with the OS to set up the MSIX vectors needed.
7670 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007671 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007672 **/
7673static int i40e_init_msix(struct i40e_pf *pf)
7674{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007675 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007676 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007677 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007678 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007679 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007680
7681 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7682 return -ENODEV;
7683
7684 /* The number of vectors we'll request will be comprised of:
7685 * - Add 1 for "other" cause for Admin Queue events, etc.
7686 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007687 * - Queues being used for RSS.
7688 * We don't need as many as max_rss_size vectors.
7689 * use rss_size instead in the calculation since that
7690 * is governed by number of cpus in the system.
7691 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007693 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007694#ifdef I40E_FCOE
7695 * - The number of FCOE qps.
7696#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007697 * Once we count this up, try the request.
7698 *
7699 * If we can't get what we want, we'll simplify to nearly nothing
7700 * and try again. If that still fails, we punt.
7701 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007702 vectors_left = hw->func_caps.num_msix_vectors;
7703 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007704
Shannon Nelson1e200e42015-02-27 09:15:24 +00007705 /* reserve one vector for miscellaneous handler */
7706 if (vectors_left) {
7707 v_budget++;
7708 vectors_left--;
7709 }
7710
7711 /* reserve vectors for the main PF traffic queues */
7712 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7713 vectors_left -= pf->num_lan_msix;
7714 v_budget += pf->num_lan_msix;
7715
7716 /* reserve one vector for sideband flow director */
7717 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7718 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007719 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007720 v_budget++;
7721 vectors_left--;
7722 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007723 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007724 }
7725 }
John W Linville83840e42015-01-14 03:06:28 +00007726
Vasu Dev38e00432014-08-01 13:27:03 -07007727#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007728 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007729 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007730 if (!vectors_left)
7731 pf->num_fcoe_msix = 0;
7732 else if (vectors_left >= pf->num_fcoe_qps)
7733 pf->num_fcoe_msix = pf->num_fcoe_qps;
7734 else
7735 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007736 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007737 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007738 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007739
Vasu Dev38e00432014-08-01 13:27:03 -07007740#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007741 /* can we reserve enough for iWARP? */
7742 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007743 iwarp_requested = pf->num_iwarp_msix;
7744
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007745 if (!vectors_left)
7746 pf->num_iwarp_msix = 0;
7747 else if (vectors_left < pf->num_iwarp_msix)
7748 pf->num_iwarp_msix = 1;
7749 v_budget += pf->num_iwarp_msix;
7750 vectors_left -= pf->num_iwarp_msix;
7751 }
7752
Shannon Nelson1e200e42015-02-27 09:15:24 +00007753 /* any vectors left over go for VMDq support */
7754 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7755 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7756 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7757
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007758 if (!vectors_left) {
7759 pf->num_vmdq_msix = 0;
7760 pf->num_vmdq_qps = 0;
7761 } else {
7762 /* if we're short on vectors for what's desired, we limit
7763 * the queues per vmdq. If this is still more than are
7764 * available, the user will need to change the number of
7765 * queues/vectors used by the PF later with the ethtool
7766 * channels command
7767 */
7768 if (vmdq_vecs < vmdq_vecs_wanted)
7769 pf->num_vmdq_qps = 1;
7770 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007771
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007772 v_budget += vmdq_vecs;
7773 vectors_left -= vmdq_vecs;
7774 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007775 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007776
7777 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7778 GFP_KERNEL);
7779 if (!pf->msix_entries)
7780 return -ENOMEM;
7781
7782 for (i = 0; i < v_budget; i++)
7783 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007784 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007785
Shannon Nelson3b444392015-02-26 16:15:57 +00007786 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007787 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7788 kfree(pf->msix_entries);
7789 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007790 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007791 return -ENODEV;
7792
Shannon Nelson3b444392015-02-26 16:15:57 +00007793 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007794 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007795 pf->num_vmdq_vsis = 0;
7796 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007797 pf->num_lan_qps = 1;
7798 pf->num_lan_msix = 1;
7799
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007800 } else if (!vectors_left) {
7801 /* If we have limited resources, we will start with no vectors
7802 * for the special features and then allocate vectors to some
7803 * of these features based on the policy and at the end disable
7804 * the features that did not get any vectors.
7805 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007806 int vec;
7807
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007808 dev_info(&pf->pdev->dev,
7809 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007810 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007811 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007812
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007813 /* Scale vector usage down */
7814 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007815 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007816 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007817#ifdef I40E_FCOE
7818 pf->num_fcoe_qps = 0;
7819 pf->num_fcoe_msix = 0;
7820#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007821
7822 /* partition out the remaining vectors */
7823 switch (vec) {
7824 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007825 pf->num_lan_msix = 1;
7826 break;
7827 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007828 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7829 pf->num_lan_msix = 1;
7830 pf->num_iwarp_msix = 1;
7831 } else {
7832 pf->num_lan_msix = 2;
7833 }
Vasu Dev38e00432014-08-01 13:27:03 -07007834#ifdef I40E_FCOE
7835 /* give one vector to FCoE */
7836 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7837 pf->num_lan_msix = 1;
7838 pf->num_fcoe_msix = 1;
7839 }
Vasu Dev38e00432014-08-01 13:27:03 -07007840#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007841 break;
7842 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007843 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7844 pf->num_iwarp_msix = min_t(int, (vec / 3),
7845 iwarp_requested);
7846 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7847 I40E_DEFAULT_NUM_VMDQ_VSI);
7848 } else {
7849 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7850 I40E_DEFAULT_NUM_VMDQ_VSI);
7851 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007852 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7853 pf->num_fdsb_msix = 1;
7854 vec--;
7855 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007856 pf->num_lan_msix = min_t(int,
7857 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7858 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007859 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007860#ifdef I40E_FCOE
7861 /* give one vector to FCoE */
7862 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7863 pf->num_fcoe_msix = 1;
7864 vec--;
7865 }
7866#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007867 break;
7868 }
7869 }
7870
Stefan Assmannabd97a92016-09-19 13:37:51 +02007871 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7872 (pf->num_fdsb_msix == 0)) {
7873 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7874 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7875 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007876 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7877 (pf->num_vmdq_msix == 0)) {
7878 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7879 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7880 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007881
7882 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7883 (pf->num_iwarp_msix == 0)) {
7884 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7885 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7886 }
Vasu Dev38e00432014-08-01 13:27:03 -07007887#ifdef I40E_FCOE
7888
7889 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7890 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7891 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7892 }
7893#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007894 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7895 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7896 pf->num_lan_msix,
7897 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7898 pf->num_fdsb_msix,
7899 pf->num_iwarp_msix);
7900
Shannon Nelson3b444392015-02-26 16:15:57 +00007901 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007902}
7903
7904/**
Greg Rose90e04072014-03-06 08:59:57 +00007905 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007906 * @vsi: the VSI being configured
7907 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007908 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007909 *
7910 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7911 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007912static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007913{
7914 struct i40e_q_vector *q_vector;
7915
7916 /* allocate q_vector */
7917 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7918 if (!q_vector)
7919 return -ENOMEM;
7920
7921 q_vector->vsi = vsi;
7922 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007923 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7924
Alexander Duyck493fb302013-09-28 07:01:44 +00007925 if (vsi->netdev)
7926 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007927 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007928
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007929 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7930 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7931
Alexander Duyck493fb302013-09-28 07:01:44 +00007932 /* tie q_vector and vsi together */
7933 vsi->q_vectors[v_idx] = q_vector;
7934
7935 return 0;
7936}
7937
7938/**
Greg Rose90e04072014-03-06 08:59:57 +00007939 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007940 * @vsi: the VSI being configured
7941 *
7942 * We allocate one q_vector per queue interrupt. If allocation fails we
7943 * return -ENOMEM.
7944 **/
Greg Rose90e04072014-03-06 08:59:57 +00007945static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007946{
7947 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007948 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007949
7950 /* if not MSIX, give the one vector only to the LAN VSI */
7951 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7952 num_q_vectors = vsi->num_q_vectors;
7953 else if (vsi == pf->vsi[pf->lan_vsi])
7954 num_q_vectors = 1;
7955 else
7956 return -EINVAL;
7957
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007958 current_cpu = cpumask_first(cpu_online_mask);
7959
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007960 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007961 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007962 if (err)
7963 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007964 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7965 if (unlikely(current_cpu >= nr_cpu_ids))
7966 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007967 }
7968
7969 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007970
7971err_out:
7972 while (v_idx--)
7973 i40e_free_q_vector(vsi, v_idx);
7974
7975 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007976}
7977
7978/**
7979 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7980 * @pf: board private structure to initialize
7981 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007982static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983{
Shannon Nelson3b444392015-02-26 16:15:57 +00007984 int vectors = 0;
7985 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007986
7987 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007988 vectors = i40e_init_msix(pf);
7989 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007990 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007991 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007992#ifdef I40E_FCOE
7993 I40E_FLAG_FCOE_ENABLED |
7994#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007995 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007996 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007997 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007998 I40E_FLAG_SRIOV_ENABLED |
7999 I40E_FLAG_FD_SB_ENABLED |
8000 I40E_FLAG_FD_ATR_ENABLED |
8001 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008002
8003 /* rework the queue expectations without MSIX */
8004 i40e_determine_queue_usage(pf);
8005 }
8006 }
8007
8008 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8009 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008010 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008011 vectors = pci_enable_msi(pf->pdev);
8012 if (vectors < 0) {
8013 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8014 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008015 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8016 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008017 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008018 }
8019
Shannon Nelson958a3e32013-09-28 07:13:28 +00008020 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008021 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008022
Shannon Nelson3b444392015-02-26 16:15:57 +00008023 /* set up vector assignment tracking */
8024 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8025 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008026 if (!pf->irq_pile) {
8027 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8028 return -ENOMEM;
8029 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008030 pf->irq_pile->num_entries = vectors;
8031 pf->irq_pile->search_hint = 0;
8032
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008033 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008034 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008035
8036 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008037}
8038
8039/**
8040 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8041 * @pf: board private structure
8042 *
8043 * This sets up the handler for MSIX 0, which is used to manage the
8044 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8045 * when in MSI or Legacy interrupt mode.
8046 **/
8047static int i40e_setup_misc_vector(struct i40e_pf *pf)
8048{
8049 struct i40e_hw *hw = &pf->hw;
8050 int err = 0;
8051
8052 /* Only request the irq if this is the first time through, and
8053 * not when we're rebuilding after a Reset
8054 */
8055 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8056 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008057 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008058 if (err) {
8059 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008060 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008061 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008062 return -EFAULT;
8063 }
8064 }
8065
Jacob Kellerab437b52014-12-14 01:55:08 +00008066 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008067
8068 /* associate no queues to the misc vector */
8069 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8070 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8071
8072 i40e_flush(hw);
8073
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008074 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008075
8076 return err;
8077}
8078
8079/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008080 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8081 * @vsi: vsi structure
8082 * @seed: RSS hash seed
8083 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008084static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8085 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008086{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008087 struct i40e_pf *pf = vsi->back;
8088 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008089 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008090
Jacob Keller776b2e12016-07-19 16:23:30 -07008091 if (seed) {
8092 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8093 (struct i40e_aqc_get_set_rss_key_data *)seed;
8094 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8095 if (ret) {
8096 dev_info(&pf->pdev->dev,
8097 "Cannot set RSS key, err %s aq_err %s\n",
8098 i40e_stat_str(hw, ret),
8099 i40e_aq_str(hw, hw->aq.asq_last_status));
8100 return ret;
8101 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008102 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008103 if (lut) {
8104 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008105
Jacob Keller776b2e12016-07-19 16:23:30 -07008106 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8107 if (ret) {
8108 dev_info(&pf->pdev->dev,
8109 "Cannot set RSS lut, err %s aq_err %s\n",
8110 i40e_stat_str(hw, ret),
8111 i40e_aq_str(hw, hw->aq.asq_last_status));
8112 return ret;
8113 }
8114 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008115 return ret;
8116}
8117
8118/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008119 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8120 * @vsi: Pointer to vsi structure
8121 * @seed: Buffter to store the hash keys
8122 * @lut: Buffer to store the lookup table entries
8123 * @lut_size: Size of buffer to store the lookup table entries
8124 *
8125 * Return 0 on success, negative on failure
8126 */
8127static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8128 u8 *lut, u16 lut_size)
8129{
8130 struct i40e_pf *pf = vsi->back;
8131 struct i40e_hw *hw = &pf->hw;
8132 int ret = 0;
8133
8134 if (seed) {
8135 ret = i40e_aq_get_rss_key(hw, vsi->id,
8136 (struct i40e_aqc_get_set_rss_key_data *)seed);
8137 if (ret) {
8138 dev_info(&pf->pdev->dev,
8139 "Cannot get RSS key, err %s aq_err %s\n",
8140 i40e_stat_str(&pf->hw, ret),
8141 i40e_aq_str(&pf->hw,
8142 pf->hw.aq.asq_last_status));
8143 return ret;
8144 }
8145 }
8146
8147 if (lut) {
8148 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8149
8150 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8151 if (ret) {
8152 dev_info(&pf->pdev->dev,
8153 "Cannot get RSS lut, err %s aq_err %s\n",
8154 i40e_stat_str(&pf->hw, ret),
8155 i40e_aq_str(&pf->hw,
8156 pf->hw.aq.asq_last_status));
8157 return ret;
8158 }
8159 }
8160
8161 return ret;
8162}
8163
8164/**
Jacob Keller0582b962016-07-19 16:23:29 -07008165 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8166 * @vsi: VSI structure
8167 **/
8168static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8169{
8170 u8 seed[I40E_HKEY_ARRAY_SIZE];
8171 struct i40e_pf *pf = vsi->back;
8172 u8 *lut;
8173 int ret;
8174
8175 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8176 return 0;
8177
Jacob Keller552b9962016-07-19 16:23:31 -07008178 if (!vsi->rss_size)
8179 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8180 vsi->num_queue_pairs);
8181 if (!vsi->rss_size)
8182 return -EINVAL;
8183
Jacob Keller0582b962016-07-19 16:23:29 -07008184 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8185 if (!lut)
8186 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008187 /* Use the user configured hash keys and lookup table if there is one,
8188 * otherwise use default
8189 */
8190 if (vsi->rss_lut_user)
8191 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8192 else
8193 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8194 if (vsi->rss_hkey_user)
8195 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8196 else
8197 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008198 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8199 kfree(lut);
8200
8201 return ret;
8202}
8203
8204/**
Helin Zhang043dd652015-10-21 19:56:23 -04008205 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008206 * @vsi: Pointer to vsi structure
8207 * @seed: RSS hash seed
8208 * @lut: Lookup table
8209 * @lut_size: Lookup table size
8210 *
8211 * Returns 0 on success, negative on failure
8212 **/
8213static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8214 const u8 *lut, u16 lut_size)
8215{
8216 struct i40e_pf *pf = vsi->back;
8217 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008218 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008219 u8 i;
8220
8221 /* Fill out hash function seed */
8222 if (seed) {
8223 u32 *seed_dw = (u32 *)seed;
8224
Mitch Williamsc4e18682016-04-12 08:30:40 -07008225 if (vsi->type == I40E_VSI_MAIN) {
8226 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8227 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8228 seed_dw[i]);
8229 } else if (vsi->type == I40E_VSI_SRIOV) {
8230 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8231 i40e_write_rx_ctl(hw,
8232 I40E_VFQF_HKEY1(i, vf_id),
8233 seed_dw[i]);
8234 } else {
8235 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8236 }
Helin Zhange69ff812015-10-21 19:56:22 -04008237 }
8238
8239 if (lut) {
8240 u32 *lut_dw = (u32 *)lut;
8241
Mitch Williamsc4e18682016-04-12 08:30:40 -07008242 if (vsi->type == I40E_VSI_MAIN) {
8243 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8244 return -EINVAL;
8245 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8246 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8247 } else if (vsi->type == I40E_VSI_SRIOV) {
8248 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8249 return -EINVAL;
8250 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8251 i40e_write_rx_ctl(hw,
8252 I40E_VFQF_HLUT1(i, vf_id),
8253 lut_dw[i]);
8254 } else {
8255 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8256 }
Helin Zhange69ff812015-10-21 19:56:22 -04008257 }
8258 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008259
8260 return 0;
8261}
8262
8263/**
Helin Zhang043dd652015-10-21 19:56:23 -04008264 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8265 * @vsi: Pointer to VSI structure
8266 * @seed: Buffer to store the keys
8267 * @lut: Buffer to store the lookup table entries
8268 * @lut_size: Size of buffer to store the lookup table entries
8269 *
8270 * Returns 0 on success, negative on failure
8271 */
8272static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8273 u8 *lut, u16 lut_size)
8274{
8275 struct i40e_pf *pf = vsi->back;
8276 struct i40e_hw *hw = &pf->hw;
8277 u16 i;
8278
8279 if (seed) {
8280 u32 *seed_dw = (u32 *)seed;
8281
8282 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008283 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008284 }
8285 if (lut) {
8286 u32 *lut_dw = (u32 *)lut;
8287
8288 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8289 return -EINVAL;
8290 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8291 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8292 }
8293
8294 return 0;
8295}
8296
8297/**
8298 * i40e_config_rss - Configure RSS keys and lut
8299 * @vsi: Pointer to VSI structure
8300 * @seed: RSS hash seed
8301 * @lut: Lookup table
8302 * @lut_size: Lookup table size
8303 *
8304 * Returns 0 on success, negative on failure
8305 */
8306int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8307{
8308 struct i40e_pf *pf = vsi->back;
8309
8310 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8311 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8312 else
8313 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8314}
8315
8316/**
8317 * i40e_get_rss - Get RSS keys and lut
8318 * @vsi: Pointer to VSI structure
8319 * @seed: Buffer to store the keys
8320 * @lut: Buffer to store the lookup table entries
8321 * lut_size: Size of buffer to store the lookup table entries
8322 *
8323 * Returns 0 on success, negative on failure
8324 */
8325int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8326{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008327 struct i40e_pf *pf = vsi->back;
8328
8329 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8330 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8331 else
8332 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008333}
8334
8335/**
Helin Zhange69ff812015-10-21 19:56:22 -04008336 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8337 * @pf: Pointer to board private structure
8338 * @lut: Lookup table
8339 * @rss_table_size: Lookup table size
8340 * @rss_size: Range of queue number for hashing
8341 */
Alan Bradyf1582352016-08-24 11:33:46 -07008342void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8343 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008344{
Helin Zhange69ff812015-10-21 19:56:22 -04008345 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008346
Helin Zhange69ff812015-10-21 19:56:22 -04008347 for (i = 0; i < rss_table_size; i++)
8348 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008349}
8350
8351/**
Helin Zhang043dd652015-10-21 19:56:23 -04008352 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008353 * @pf: board private structure
8354 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008355static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008356{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008357 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008358 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008359 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008360 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008361 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008362 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008363 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008366 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8367 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008368 hena |= i40e_pf_get_default_rss_hena(pf);
8369
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008370 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8371 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008372
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008373 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008374 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008375 reg_val = (pf->rss_table_size == 512) ?
8376 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8377 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008378 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008379
Helin Zhang28c58692015-10-26 19:44:27 -04008380 /* Determine the RSS size of the VSI */
8381 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008382 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8383 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008384 if (!vsi->rss_size)
8385 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008386
Helin Zhange69ff812015-10-21 19:56:22 -04008387 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8388 if (!lut)
8389 return -ENOMEM;
8390
Helin Zhang28c58692015-10-26 19:44:27 -04008391 /* Use user configured lut if there is one, otherwise use default */
8392 if (vsi->rss_lut_user)
8393 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8394 else
8395 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008396
Helin Zhang28c58692015-10-26 19:44:27 -04008397 /* Use user configured hash key if there is one, otherwise
8398 * use default.
8399 */
8400 if (vsi->rss_hkey_user)
8401 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8402 else
8403 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008404 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008405 kfree(lut);
8406
8407 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008408}
8409
8410/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008411 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8412 * @pf: board private structure
8413 * @queue_count: the requested queue count for rss.
8414 *
8415 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8416 * count which may be different from the requested queue count.
8417 **/
8418int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8419{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008420 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8421 int new_rss_size;
8422
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008423 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8424 return 0;
8425
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008426 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008427
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008428 if (queue_count != vsi->num_queue_pairs) {
8429 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008430 i40e_prep_for_reset(pf);
8431
Helin Zhangacd65442015-10-26 19:44:28 -04008432 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008433
8434 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008435
8436 /* Discard the user configured hash keys and lut, if less
8437 * queues are enabled.
8438 */
8439 if (queue_count < vsi->rss_size) {
8440 i40e_clear_rss_config_user(vsi);
8441 dev_dbg(&pf->pdev->dev,
8442 "discard user configured hash keys and lut\n");
8443 }
8444
8445 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008446 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8447 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008448
Helin Zhang043dd652015-10-21 19:56:23 -04008449 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008450 }
Lihong Yang12815052016-09-27 11:28:48 -07008451 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8452 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008453 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008454}
8455
8456/**
Greg Rosef4492db2015-02-06 08:52:12 +00008457 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8458 * @pf: board private structure
8459 **/
8460i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8461{
8462 i40e_status status;
8463 bool min_valid, max_valid;
8464 u32 max_bw, min_bw;
8465
8466 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8467 &min_valid, &max_valid);
8468
8469 if (!status) {
8470 if (min_valid)
8471 pf->npar_min_bw = min_bw;
8472 if (max_valid)
8473 pf->npar_max_bw = max_bw;
8474 }
8475
8476 return status;
8477}
8478
8479/**
8480 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8481 * @pf: board private structure
8482 **/
8483i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8484{
8485 struct i40e_aqc_configure_partition_bw_data bw_data;
8486 i40e_status status;
8487
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008488 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008489 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008490 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8491 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8492
8493 /* Set the new bandwidths */
8494 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8495
8496 return status;
8497}
8498
8499/**
8500 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8501 * @pf: board private structure
8502 **/
8503i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8504{
8505 /* Commit temporary BW setting to permanent NVM image */
8506 enum i40e_admin_queue_err last_aq_status;
8507 i40e_status ret;
8508 u16 nvm_word;
8509
8510 if (pf->hw.partition_id != 1) {
8511 dev_info(&pf->pdev->dev,
8512 "Commit BW only works on partition 1! This is partition %d",
8513 pf->hw.partition_id);
8514 ret = I40E_NOT_SUPPORTED;
8515 goto bw_commit_out;
8516 }
8517
8518 /* Acquire NVM for read access */
8519 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8520 last_aq_status = pf->hw.aq.asq_last_status;
8521 if (ret) {
8522 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008523 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8524 i40e_stat_str(&pf->hw, ret),
8525 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008526 goto bw_commit_out;
8527 }
8528
8529 /* Read word 0x10 of NVM - SW compatibility word 1 */
8530 ret = i40e_aq_read_nvm(&pf->hw,
8531 I40E_SR_NVM_CONTROL_WORD,
8532 0x10, sizeof(nvm_word), &nvm_word,
8533 false, NULL);
8534 /* Save off last admin queue command status before releasing
8535 * the NVM
8536 */
8537 last_aq_status = pf->hw.aq.asq_last_status;
8538 i40e_release_nvm(&pf->hw);
8539 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008540 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8541 i40e_stat_str(&pf->hw, ret),
8542 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008543 goto bw_commit_out;
8544 }
8545
8546 /* Wait a bit for NVM release to complete */
8547 msleep(50);
8548
8549 /* Acquire NVM for write access */
8550 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8551 last_aq_status = pf->hw.aq.asq_last_status;
8552 if (ret) {
8553 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008554 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8555 i40e_stat_str(&pf->hw, ret),
8556 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008557 goto bw_commit_out;
8558 }
8559 /* Write it back out unchanged to initiate update NVM,
8560 * which will force a write of the shadow (alt) RAM to
8561 * the NVM - thus storing the bandwidth values permanently.
8562 */
8563 ret = i40e_aq_update_nvm(&pf->hw,
8564 I40E_SR_NVM_CONTROL_WORD,
8565 0x10, sizeof(nvm_word),
8566 &nvm_word, true, NULL);
8567 /* Save off last admin queue command status before releasing
8568 * the NVM
8569 */
8570 last_aq_status = pf->hw.aq.asq_last_status;
8571 i40e_release_nvm(&pf->hw);
8572 if (ret)
8573 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008574 "BW settings NOT SAVED, err %s aq_err %s\n",
8575 i40e_stat_str(&pf->hw, ret),
8576 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008577bw_commit_out:
8578
8579 return ret;
8580}
8581
8582/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008583 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8584 * @pf: board private structure to initialize
8585 *
8586 * i40e_sw_init initializes the Adapter private data structure.
8587 * Fields are initialized based on PCI device information and
8588 * OS network device settings (MTU size).
8589 **/
8590static int i40e_sw_init(struct i40e_pf *pf)
8591{
8592 int err = 0;
8593 int size;
8594
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008595 /* Set default capability flags */
8596 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8597 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008598 I40E_FLAG_MSIX_ENABLED;
8599
Mitch Williamsca99eb92014-04-04 04:43:07 +00008600 /* Set default ITR */
8601 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8602 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8603
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008604 /* Depending on PF configurations, it is possible that the RSS
8605 * maximum might end up larger than the available queues
8606 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008607 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008608 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008609 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008610 pf->rss_size_max = min_t(int, pf->rss_size_max,
8611 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008612 if (pf->hw.func_caps.rss) {
8613 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008614 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8615 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008616 }
8617
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008618 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008619 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008620 pf->flags |= I40E_FLAG_MFP_ENABLED;
8621 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008622 if (i40e_get_npar_bw_setting(pf))
8623 dev_warn(&pf->pdev->dev,
8624 "Could not get NPAR bw settings\n");
8625 else
8626 dev_info(&pf->pdev->dev,
8627 "Min BW = %8.8x, Max BW = %8.8x\n",
8628 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008629 }
8630
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008631 /* FW/NVM is not yet fixed in this regard */
8632 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8633 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8634 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8635 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008636 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8637 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008638 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008639 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008640 else
8641 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008642 pf->fdir_pf_filter_count =
8643 pf->hw.func_caps.fd_filters_guaranteed;
8644 pf->hw.fdir_shared_filter_count =
8645 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008646 }
8647
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008648 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008649 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008650 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008651 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008652 /* No DCB support for FW < v4.33 */
8653 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8654 }
8655
8656 /* Disable FW LLDP if FW < v4.3 */
8657 if (i40e_is_mac_710(&pf->hw) &&
8658 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8659 (pf->hw.aq.fw_maj_ver < 4)))
8660 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8661
8662 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8663 if (i40e_is_mac_710(&pf->hw) &&
8664 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8665 (pf->hw.aq.fw_maj_ver >= 5)))
8666 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008667
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008668 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008669 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008670 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008671 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008672 }
8673
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008674 if (pf->hw.func_caps.iwarp) {
8675 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8676 /* IWARP needs one extra vector for CQP just like MISC.*/
8677 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8678 }
8679
Vasu Dev38e00432014-08-01 13:27:03 -07008680#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008681 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008682
8683#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008684#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008685 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008686 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8687 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8688 pf->num_req_vfs = min_t(int,
8689 pf->hw.func_caps.num_vfs,
8690 I40E_MAX_VF_COUNT);
8691 }
8692#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008693 if (pf->hw.mac.type == I40E_MAC_X722) {
8694 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8695 I40E_FLAG_128_QP_RSS_CAPABLE |
8696 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8697 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8698 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008699 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008700 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008701 I40E_FLAG_USE_SET_LLDP_MIB |
Jacob Keller1e28e862016-11-11 12:39:25 -08008702 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE |
8703 I40E_FLAG_PTP_L4_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008704 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8705 ((pf->hw.aq.api_maj_ver == 1) &&
8706 (pf->hw.aq.api_min_ver > 4))) {
8707 /* Supported in FW API version higher than 1.4 */
8708 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008709 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8710 } else {
8711 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008712 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008714 pf->eeprom_version = 0xDEAD;
8715 pf->lan_veb = I40E_NO_VEB;
8716 pf->lan_vsi = I40E_NO_VSI;
8717
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008718 /* By default FW has this off for performance reasons */
8719 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8720
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008721 /* set up queue assignment tracking */
8722 size = sizeof(struct i40e_lump_tracking)
8723 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8724 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8725 if (!pf->qp_pile) {
8726 err = -ENOMEM;
8727 goto sw_init_done;
8728 }
8729 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8730 pf->qp_pile->search_hint = 0;
8731
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008732 pf->tx_timeout_recovery_level = 1;
8733
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008734 mutex_init(&pf->switch_mutex);
8735
Greg Rosec668a122015-02-26 16:10:39 +00008736 /* If NPAR is enabled nudge the Tx scheduler */
8737 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8738 i40e_set_npar_bw_setting(pf);
8739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008740sw_init_done:
8741 return err;
8742}
8743
8744/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008745 * i40e_set_ntuple - set the ntuple feature flag and take action
8746 * @pf: board private structure to initialize
8747 * @features: the feature set that the stack is suggesting
8748 *
8749 * returns a bool to indicate if reset needs to happen
8750 **/
8751bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8752{
8753 bool need_reset = false;
8754
8755 /* Check if Flow Director n-tuple support was enabled or disabled. If
8756 * the state changed, we need to reset.
8757 */
8758 if (features & NETIF_F_NTUPLE) {
8759 /* Enable filters and mark for reset */
8760 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8761 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008762 /* enable FD_SB only if there is MSI-X vector */
8763 if (pf->num_fdsb_msix > 0)
8764 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008765 } else {
8766 /* turn off filters, mark for reset and clear SW filter list */
8767 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8768 need_reset = true;
8769 i40e_fdir_filter_exit(pf);
8770 }
8771 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008772 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008773 /* reset fd counters */
8774 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8775 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008776 /* if ATR was auto disabled it can be re-enabled. */
8777 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008778 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008779 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008780 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8781 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8782 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008783 }
8784 return need_reset;
8785}
8786
8787/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008788 * i40e_clear_rss_lut - clear the rx hash lookup table
8789 * @vsi: the VSI being configured
8790 **/
8791static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8792{
8793 struct i40e_pf *pf = vsi->back;
8794 struct i40e_hw *hw = &pf->hw;
8795 u16 vf_id = vsi->vf_id;
8796 u8 i;
8797
8798 if (vsi->type == I40E_VSI_MAIN) {
8799 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8800 wr32(hw, I40E_PFQF_HLUT(i), 0);
8801 } else if (vsi->type == I40E_VSI_SRIOV) {
8802 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8803 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8804 } else {
8805 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8806 }
8807}
8808
8809/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008810 * i40e_set_features - set the netdev feature flags
8811 * @netdev: ptr to the netdev being adjusted
8812 * @features: the feature set that the stack is suggesting
8813 **/
8814static int i40e_set_features(struct net_device *netdev,
8815 netdev_features_t features)
8816{
8817 struct i40e_netdev_priv *np = netdev_priv(netdev);
8818 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008819 struct i40e_pf *pf = vsi->back;
8820 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008821
Alan Bradyd8ec9862016-07-27 12:02:38 -07008822 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8823 i40e_pf_config_rss(pf);
8824 else if (!(features & NETIF_F_RXHASH) &&
8825 netdev->features & NETIF_F_RXHASH)
8826 i40e_clear_rss_lut(vsi);
8827
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008828 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8829 i40e_vlan_stripping_enable(vsi);
8830 else
8831 i40e_vlan_stripping_disable(vsi);
8832
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008833 need_reset = i40e_set_ntuple(pf, features);
8834
8835 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008836 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008838 return 0;
8839}
8840
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008841/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008842 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008843 * @pf: board private structure
8844 * @port: The UDP port to look up
8845 *
8846 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8847 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008848static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008849{
8850 u8 i;
8851
8852 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008853 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008854 return i;
8855 }
8856
8857 return i;
8858}
8859
8860/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008861 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008862 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008863 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008864 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008865static void i40e_udp_tunnel_add(struct net_device *netdev,
8866 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008867{
8868 struct i40e_netdev_priv *np = netdev_priv(netdev);
8869 struct i40e_vsi *vsi = np->vsi;
8870 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008871 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008872 u8 next_idx;
8873 u8 idx;
8874
Singhai, Anjali6a899022015-12-14 12:21:18 -08008875 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008876
8877 /* Check if port already exists */
8878 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008879 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008880 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008881 return;
8882 }
8883
8884 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008885 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008886
8887 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008888 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008889 ntohs(port));
8890 return;
8891 }
8892
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008893 switch (ti->type) {
8894 case UDP_TUNNEL_TYPE_VXLAN:
8895 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8896 break;
8897 case UDP_TUNNEL_TYPE_GENEVE:
8898 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8899 return;
8900 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8901 break;
8902 default:
8903 return;
8904 }
8905
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008906 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008907 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008908 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8909 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008910}
8911
8912/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008913 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008914 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008915 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008916 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008917static void i40e_udp_tunnel_del(struct net_device *netdev,
8918 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008919{
8920 struct i40e_netdev_priv *np = netdev_priv(netdev);
8921 struct i40e_vsi *vsi = np->vsi;
8922 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008923 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008924 u8 idx;
8925
Singhai, Anjali6a899022015-12-14 12:21:18 -08008926 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008927
8928 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008929 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8930 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008931
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008932 switch (ti->type) {
8933 case UDP_TUNNEL_TYPE_VXLAN:
8934 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8935 goto not_found;
8936 break;
8937 case UDP_TUNNEL_TYPE_GENEVE:
8938 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8939 goto not_found;
8940 break;
8941 default:
8942 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008943 }
8944
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008945 /* if port exists, set it to 0 (mark for deletion)
8946 * and make it pending
8947 */
8948 pf->udp_ports[idx].index = 0;
8949 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008950 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8951
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008952 return;
8953not_found:
8954 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8955 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008956}
8957
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008958static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008959 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008960{
8961 struct i40e_netdev_priv *np = netdev_priv(netdev);
8962 struct i40e_pf *pf = np->vsi->back;
8963 struct i40e_hw *hw = &pf->hw;
8964
8965 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8966 return -EOPNOTSUPP;
8967
8968 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8969 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8970
8971 return 0;
8972}
8973
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008974/**
8975 * i40e_ndo_fdb_add - add an entry to the hardware database
8976 * @ndm: the input from the stack
8977 * @tb: pointer to array of nladdr (unused)
8978 * @dev: the net device pointer
8979 * @addr: the MAC address entry being added
8980 * @flags: instructions from stack about fdb operation
8981 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008982static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8983 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008984 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008985 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008986{
8987 struct i40e_netdev_priv *np = netdev_priv(dev);
8988 struct i40e_pf *pf = np->vsi->back;
8989 int err = 0;
8990
8991 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8992 return -EOPNOTSUPP;
8993
Or Gerlitz65891fe2014-12-14 18:19:05 +02008994 if (vid) {
8995 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8996 return -EINVAL;
8997 }
8998
Greg Rose4ba0dea2014-03-06 08:59:55 +00008999 /* Hardware does not support aging addresses so if a
9000 * ndm_state is given only allow permanent addresses
9001 */
9002 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9003 netdev_info(dev, "FDB only supports static addresses\n");
9004 return -EINVAL;
9005 }
9006
9007 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9008 err = dev_uc_add_excl(dev, addr);
9009 else if (is_multicast_ether_addr(addr))
9010 err = dev_mc_add_excl(dev, addr);
9011 else
9012 err = -EINVAL;
9013
9014 /* Only return duplicate errors if NLM_F_EXCL is set */
9015 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9016 err = 0;
9017
9018 return err;
9019}
9020
Neerav Parikh51616012015-02-06 08:52:14 +00009021/**
9022 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9023 * @dev: the netdev being configured
9024 * @nlh: RTNL message
9025 *
9026 * Inserts a new hardware bridge if not already created and
9027 * enables the bridging mode requested (VEB or VEPA). If the
9028 * hardware bridge has already been inserted and the request
9029 * is to change the mode then that requires a PF reset to
9030 * allow rebuild of the components with required hardware
9031 * bridge mode enabled.
9032 **/
9033static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009034 struct nlmsghdr *nlh,
9035 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009036{
9037 struct i40e_netdev_priv *np = netdev_priv(dev);
9038 struct i40e_vsi *vsi = np->vsi;
9039 struct i40e_pf *pf = vsi->back;
9040 struct i40e_veb *veb = NULL;
9041 struct nlattr *attr, *br_spec;
9042 int i, rem;
9043
9044 /* Only for PF VSI for now */
9045 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9046 return -EOPNOTSUPP;
9047
9048 /* Find the HW bridge for PF VSI */
9049 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9050 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9051 veb = pf->veb[i];
9052 }
9053
9054 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9055
9056 nla_for_each_nested(attr, br_spec, rem) {
9057 __u16 mode;
9058
9059 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9060 continue;
9061
9062 mode = nla_get_u16(attr);
9063 if ((mode != BRIDGE_MODE_VEPA) &&
9064 (mode != BRIDGE_MODE_VEB))
9065 return -EINVAL;
9066
9067 /* Insert a new HW bridge */
9068 if (!veb) {
9069 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9070 vsi->tc_config.enabled_tc);
9071 if (veb) {
9072 veb->bridge_mode = mode;
9073 i40e_config_bridge_mode(veb);
9074 } else {
9075 /* No Bridge HW offload available */
9076 return -ENOENT;
9077 }
9078 break;
9079 } else if (mode != veb->bridge_mode) {
9080 /* Existing HW bridge but different mode needs reset */
9081 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009082 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9083 if (mode == BRIDGE_MODE_VEB)
9084 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9085 else
9086 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9087 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009088 break;
9089 }
9090 }
9091
9092 return 0;
9093}
9094
9095/**
9096 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9097 * @skb: skb buff
9098 * @pid: process id
9099 * @seq: RTNL message seq #
9100 * @dev: the netdev being configured
9101 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009102 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009103 *
9104 * Return the mode in which the hardware bridge is operating in
9105 * i.e VEB or VEPA.
9106 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009107static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9108 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009109 u32 __always_unused filter_mask,
9110 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009111{
9112 struct i40e_netdev_priv *np = netdev_priv(dev);
9113 struct i40e_vsi *vsi = np->vsi;
9114 struct i40e_pf *pf = vsi->back;
9115 struct i40e_veb *veb = NULL;
9116 int i;
9117
9118 /* Only for PF VSI for now */
9119 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9120 return -EOPNOTSUPP;
9121
9122 /* Find the HW bridge for the PF VSI */
9123 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9124 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9125 veb = pf->veb[i];
9126 }
9127
9128 if (!veb)
9129 return 0;
9130
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009131 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009132 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009133}
Neerav Parikh51616012015-02-06 08:52:14 +00009134
Joe Stringerf44a75e2015-04-14 17:09:14 -07009135/**
9136 * i40e_features_check - Validate encapsulated packet conforms to limits
9137 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009138 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009139 * @features: Offload features that the stack believes apply
9140 **/
9141static netdev_features_t i40e_features_check(struct sk_buff *skb,
9142 struct net_device *dev,
9143 netdev_features_t features)
9144{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009145 size_t len;
9146
9147 /* No point in doing any of this if neither checksum nor GSO are
9148 * being requested for this frame. We can rule out both by just
9149 * checking for CHECKSUM_PARTIAL
9150 */
9151 if (skb->ip_summed != CHECKSUM_PARTIAL)
9152 return features;
9153
9154 /* We cannot support GSO if the MSS is going to be less than
9155 * 64 bytes. If it is then we need to drop support for GSO.
9156 */
9157 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9158 features &= ~NETIF_F_GSO_MASK;
9159
9160 /* MACLEN can support at most 63 words */
9161 len = skb_network_header(skb) - skb->data;
9162 if (len & ~(63 * 2))
9163 goto out_err;
9164
9165 /* IPLEN and EIPLEN can support at most 127 dwords */
9166 len = skb_transport_header(skb) - skb_network_header(skb);
9167 if (len & ~(127 * 4))
9168 goto out_err;
9169
9170 if (skb->encapsulation) {
9171 /* L4TUNLEN can support 127 words */
9172 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9173 if (len & ~(127 * 2))
9174 goto out_err;
9175
9176 /* IPLEN can support at most 127 dwords */
9177 len = skb_inner_transport_header(skb) -
9178 skb_inner_network_header(skb);
9179 if (len & ~(127 * 4))
9180 goto out_err;
9181 }
9182
9183 /* No need to validate L4LEN as TCP is the only protocol with a
9184 * a flexible value and we support all possible values supported
9185 * by TCP, which is at most 15 dwords
9186 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009187
9188 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009189out_err:
9190 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009191}
9192
Shannon Nelson37a29732015-02-27 09:15:19 +00009193static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009194 .ndo_open = i40e_open,
9195 .ndo_stop = i40e_close,
9196 .ndo_start_xmit = i40e_lan_xmit_frame,
9197 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9198 .ndo_set_rx_mode = i40e_set_rx_mode,
9199 .ndo_validate_addr = eth_validate_addr,
9200 .ndo_set_mac_address = i40e_set_mac,
9201 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009202 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009203 .ndo_tx_timeout = i40e_tx_timeout,
9204 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9205 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9206#ifdef CONFIG_NET_POLL_CONTROLLER
9207 .ndo_poll_controller = i40e_netpoll,
9208#endif
John Fastabende4c67342016-02-16 21:16:15 -08009209 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009210#ifdef I40E_FCOE
9211 .ndo_fcoe_enable = i40e_fcoe_enable,
9212 .ndo_fcoe_disable = i40e_fcoe_disable,
9213#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009214 .ndo_set_features = i40e_set_features,
9215 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9216 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009217 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009218 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009219 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009220 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009221 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009222 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9223 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009224 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009225 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009226 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009227 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9228 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009229};
9230
9231/**
9232 * i40e_config_netdev - Setup the netdev flags
9233 * @vsi: the VSI being configured
9234 *
9235 * Returns 0 on success, negative value on failure
9236 **/
9237static int i40e_config_netdev(struct i40e_vsi *vsi)
9238{
9239 struct i40e_pf *pf = vsi->back;
9240 struct i40e_hw *hw = &pf->hw;
9241 struct i40e_netdev_priv *np;
9242 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009243 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009244 u8 mac_addr[ETH_ALEN];
9245 int etherdev_size;
9246
9247 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009248 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009249 if (!netdev)
9250 return -ENOMEM;
9251
9252 vsi->netdev = netdev;
9253 np = netdev_priv(netdev);
9254 np->vsi = vsi;
9255
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009256 netdev->hw_enc_features |= NETIF_F_SG |
9257 NETIF_F_IP_CSUM |
9258 NETIF_F_IPV6_CSUM |
9259 NETIF_F_HIGHDMA |
9260 NETIF_F_SOFT_FEATURES |
9261 NETIF_F_TSO |
9262 NETIF_F_TSO_ECN |
9263 NETIF_F_TSO6 |
9264 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009265 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009266 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009267 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009268 NETIF_F_GSO_UDP_TUNNEL |
9269 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009270 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009271 NETIF_F_SCTP_CRC |
9272 NETIF_F_RXHASH |
9273 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009274 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009275
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009276 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009277 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9278
9279 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009280
9281 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009282 netdev->vlan_features |= netdev->hw_enc_features |
9283 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009284
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009285 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009286 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009287
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009288 netdev->hw_features |= netdev->hw_enc_features |
9289 NETIF_F_HW_VLAN_CTAG_TX |
9290 NETIF_F_HW_VLAN_CTAG_RX;
9291
9292 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009293 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009294
9295 if (vsi->type == I40E_VSI_MAIN) {
9296 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009297 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009298 /* The following steps are necessary to prevent reception
9299 * of tagged packets - some older NVM configurations load a
9300 * default a MAC-VLAN filter that accepts any tagged packet
9301 * which must be replaced by a normal filter.
9302 */
9303 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009304 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009305 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009306 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009307 } else {
9308 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9309 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9310 pf->vsi[pf->lan_vsi]->netdev->name);
9311 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009312
Jacob Keller278e7d02016-10-05 09:30:37 -07009313 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009314 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009315 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009316 }
Kiran Patil21659032015-09-30 14:09:03 -04009317
Jacob Keller435c0842016-11-08 13:05:10 -08009318 /* Add the broadcast filter so that we initially will receive
9319 * broadcast packets. Note that when a new VLAN is first added the
9320 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9321 * specific filters as part of transitioning into "vlan" operation.
9322 * When more VLANs are added, the driver will copy each existing MAC
9323 * filter and add it for the new VLAN.
9324 *
9325 * Broadcast filters are handled specially by
9326 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9327 * promiscuous bit instead of adding this directly as a MAC/VLAN
9328 * filter. The subtask will update the correct broadcast promiscuous
9329 * bits as VLANs become active or inactive.
9330 */
9331 eth_broadcast_addr(broadcast);
9332 spin_lock_bh(&vsi->mac_filter_hash_lock);
9333 i40e_add_filter(vsi, broadcast, I40E_VLAN_ANY);
9334 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9335
Greg Rose9a173902014-05-22 06:32:02 +00009336 ether_addr_copy(netdev->dev_addr, mac_addr);
9337 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009338
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009339 netdev->priv_flags |= IFF_UNICAST_FLT;
9340 netdev->priv_flags |= IFF_SUPP_NOFCS;
9341 /* Setup netdev TC information */
9342 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9343
9344 netdev->netdev_ops = &i40e_netdev_ops;
9345 netdev->watchdog_timeo = 5 * HZ;
9346 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009347#ifdef I40E_FCOE
9348 i40e_fcoe_config_netdev(netdev, vsi);
9349#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009350
Jarod Wilson91c527a2016-10-17 15:54:05 -04009351 /* MTU range: 68 - 9706 */
9352 netdev->min_mtu = ETH_MIN_MTU;
9353 netdev->max_mtu = I40E_MAX_RXBUFFER -
9354 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9355
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009356 return 0;
9357}
9358
9359/**
9360 * i40e_vsi_delete - Delete a VSI from the switch
9361 * @vsi: the VSI being removed
9362 *
9363 * Returns 0 on success, negative value on failure
9364 **/
9365static void i40e_vsi_delete(struct i40e_vsi *vsi)
9366{
9367 /* remove default VSI is not allowed */
9368 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9369 return;
9370
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009371 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009372}
9373
9374/**
Neerav Parikh51616012015-02-06 08:52:14 +00009375 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9376 * @vsi: the VSI being queried
9377 *
9378 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9379 **/
9380int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9381{
9382 struct i40e_veb *veb;
9383 struct i40e_pf *pf = vsi->back;
9384
9385 /* Uplink is not a bridge so default to VEB */
9386 if (vsi->veb_idx == I40E_NO_VEB)
9387 return 1;
9388
9389 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009390 if (!veb) {
9391 dev_info(&pf->pdev->dev,
9392 "There is no veb associated with the bridge\n");
9393 return -ENOENT;
9394 }
Neerav Parikh51616012015-02-06 08:52:14 +00009395
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009396 /* Uplink is a bridge in VEPA mode */
9397 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9398 return 0;
9399 } else {
9400 /* Uplink is a bridge in VEB mode */
9401 return 1;
9402 }
9403
9404 /* VEPA is now default bridge, so return 0 */
9405 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009406}
9407
9408/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009409 * i40e_add_vsi - Add a VSI to the switch
9410 * @vsi: the VSI being configured
9411 *
9412 * This initializes a VSI context depending on the VSI type to be added and
9413 * passes it down to the add_vsi aq command.
9414 **/
9415static int i40e_add_vsi(struct i40e_vsi *vsi)
9416{
9417 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009418 struct i40e_pf *pf = vsi->back;
9419 struct i40e_hw *hw = &pf->hw;
9420 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009421 struct i40e_mac_filter *f;
9422 struct hlist_node *h;
9423 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009424
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009425 u8 enabled_tc = 0x1; /* TC0 enabled */
9426 int f_count = 0;
9427
9428 memset(&ctxt, 0, sizeof(ctxt));
9429 switch (vsi->type) {
9430 case I40E_VSI_MAIN:
9431 /* The PF's main VSI is already setup as part of the
9432 * device initialization, so we'll not bother with
9433 * the add_vsi call, but we will retrieve the current
9434 * VSI context.
9435 */
9436 ctxt.seid = pf->main_vsi_seid;
9437 ctxt.pf_num = pf->hw.pf_id;
9438 ctxt.vf_num = 0;
9439 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9440 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9441 if (ret) {
9442 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009443 "couldn't get PF vsi config, err %s aq_err %s\n",
9444 i40e_stat_str(&pf->hw, ret),
9445 i40e_aq_str(&pf->hw,
9446 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447 return -ENOENT;
9448 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009449 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009450 vsi->info.valid_sections = 0;
9451
9452 vsi->seid = ctxt.seid;
9453 vsi->id = ctxt.vsi_number;
9454
9455 enabled_tc = i40e_pf_get_tc_map(pf);
9456
9457 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009458 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9459 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009460 memset(&ctxt, 0, sizeof(ctxt));
9461 ctxt.seid = pf->main_vsi_seid;
9462 ctxt.pf_num = pf->hw.pf_id;
9463 ctxt.vf_num = 0;
9464 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9465 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9466 if (ret) {
9467 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009468 "update vsi failed, err %s aq_err %s\n",
9469 i40e_stat_str(&pf->hw, ret),
9470 i40e_aq_str(&pf->hw,
9471 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009472 ret = -ENOENT;
9473 goto err;
9474 }
9475 /* update the local VSI info queue map */
9476 i40e_vsi_update_queue_map(vsi, &ctxt);
9477 vsi->info.valid_sections = 0;
9478 } else {
9479 /* Default/Main VSI is only enabled for TC0
9480 * reconfigure it to enable all TCs that are
9481 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009482 * For MFP case the iSCSI PF would use this
9483 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009484 */
9485 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9486 if (ret) {
9487 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009488 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9489 enabled_tc,
9490 i40e_stat_str(&pf->hw, ret),
9491 i40e_aq_str(&pf->hw,
9492 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009493 ret = -ENOENT;
9494 }
9495 }
9496 break;
9497
9498 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009499 ctxt.pf_num = hw->pf_id;
9500 ctxt.vf_num = 0;
9501 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009502 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009503 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009504 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9505 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009506 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009507 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009508 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009509 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009510 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009511 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009512 break;
9513
9514 case I40E_VSI_VMDQ2:
9515 ctxt.pf_num = hw->pf_id;
9516 ctxt.vf_num = 0;
9517 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009518 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009519 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9520
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009521 /* This VSI is connected to VEB so the switch_id
9522 * should be set to zero by default.
9523 */
Neerav Parikh51616012015-02-06 08:52:14 +00009524 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9525 ctxt.info.valid_sections |=
9526 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9527 ctxt.info.switch_id =
9528 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9529 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530
9531 /* Setup the VSI tx/rx queue map for TC0 only for now */
9532 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9533 break;
9534
9535 case I40E_VSI_SRIOV:
9536 ctxt.pf_num = hw->pf_id;
9537 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9538 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009539 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009540 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009542 /* This VSI is connected to VEB so the switch_id
9543 * should be set to zero by default.
9544 */
Neerav Parikh51616012015-02-06 08:52:14 +00009545 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9546 ctxt.info.valid_sections |=
9547 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9548 ctxt.info.switch_id =
9549 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9550 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009551
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009552 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9553 ctxt.info.valid_sections |=
9554 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9555 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009556 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9557 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009558 }
9559
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009560 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9561 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009562 if (pf->vf[vsi->vf_id].spoofchk) {
9563 ctxt.info.valid_sections |=
9564 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9565 ctxt.info.sec_flags |=
9566 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9567 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9568 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009569 /* Setup the VSI tx/rx queue map for TC0 only for now */
9570 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9571 break;
9572
Vasu Dev38e00432014-08-01 13:27:03 -07009573#ifdef I40E_FCOE
9574 case I40E_VSI_FCOE:
9575 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9576 if (ret) {
9577 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9578 return ret;
9579 }
9580 break;
9581
9582#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009583 case I40E_VSI_IWARP:
9584 /* send down message to iWARP */
9585 break;
9586
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009587 default:
9588 return -ENODEV;
9589 }
9590
9591 if (vsi->type != I40E_VSI_MAIN) {
9592 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9593 if (ret) {
9594 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009595 "add vsi failed, err %s aq_err %s\n",
9596 i40e_stat_str(&pf->hw, ret),
9597 i40e_aq_str(&pf->hw,
9598 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009599 ret = -ENOENT;
9600 goto err;
9601 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009602 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009603 vsi->info.valid_sections = 0;
9604 vsi->seid = ctxt.seid;
9605 vsi->id = ctxt.vsi_number;
9606 }
9607
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009608 vsi->active_filters = 0;
9609 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009610 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009611 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009612 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009613 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009614 f_count++;
9615 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009616 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009617
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009618 if (f_count) {
9619 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9620 pf->flags |= I40E_FLAG_FILTER_SYNC;
9621 }
9622
9623 /* Update VSI BW information */
9624 ret = i40e_vsi_get_bw_info(vsi);
9625 if (ret) {
9626 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009627 "couldn't get vsi bw info, err %s aq_err %s\n",
9628 i40e_stat_str(&pf->hw, ret),
9629 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009630 /* VSI is already added so not tearing that up */
9631 ret = 0;
9632 }
9633
9634err:
9635 return ret;
9636}
9637
9638/**
9639 * i40e_vsi_release - Delete a VSI and free its resources
9640 * @vsi: the VSI being removed
9641 *
9642 * Returns 0 on success or < 0 on error
9643 **/
9644int i40e_vsi_release(struct i40e_vsi *vsi)
9645{
Jacob Keller278e7d02016-10-05 09:30:37 -07009646 struct i40e_mac_filter *f;
9647 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009648 struct i40e_veb *veb = NULL;
9649 struct i40e_pf *pf;
9650 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009651 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009652
9653 pf = vsi->back;
9654
9655 /* release of a VEB-owner or last VSI is not allowed */
9656 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9657 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9658 vsi->seid, vsi->uplink_seid);
9659 return -ENODEV;
9660 }
9661 if (vsi == pf->vsi[pf->lan_vsi] &&
9662 !test_bit(__I40E_DOWN, &pf->state)) {
9663 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9664 return -ENODEV;
9665 }
9666
9667 uplink_seid = vsi->uplink_seid;
9668 if (vsi->type != I40E_VSI_SRIOV) {
9669 if (vsi->netdev_registered) {
9670 vsi->netdev_registered = false;
9671 if (vsi->netdev) {
9672 /* results in a call to i40e_close() */
9673 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009674 }
9675 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009676 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009677 }
9678 i40e_vsi_disable_irq(vsi);
9679 }
9680
Jacob Keller278e7d02016-10-05 09:30:37 -07009681 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009682
9683 /* clear the sync flag on all filters */
9684 if (vsi->netdev) {
9685 __dev_uc_unsync(vsi->netdev, NULL);
9686 __dev_mc_unsync(vsi->netdev, NULL);
9687 }
9688
9689 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009690 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009691 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009692
Jacob Keller278e7d02016-10-05 09:30:37 -07009693 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009694
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009695 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009696
9697 i40e_vsi_delete(vsi);
9698 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009699 if (vsi->netdev) {
9700 free_netdev(vsi->netdev);
9701 vsi->netdev = NULL;
9702 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009703 i40e_vsi_clear_rings(vsi);
9704 i40e_vsi_clear(vsi);
9705
9706 /* If this was the last thing on the VEB, except for the
9707 * controlling VSI, remove the VEB, which puts the controlling
9708 * VSI onto the next level down in the switch.
9709 *
9710 * Well, okay, there's one more exception here: don't remove
9711 * the orphan VEBs yet. We'll wait for an explicit remove request
9712 * from up the network stack.
9713 */
Mitch Williams505682c2014-05-20 08:01:37 +00009714 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009715 if (pf->vsi[i] &&
9716 pf->vsi[i]->uplink_seid == uplink_seid &&
9717 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9718 n++; /* count the VSIs */
9719 }
9720 }
9721 for (i = 0; i < I40E_MAX_VEB; i++) {
9722 if (!pf->veb[i])
9723 continue;
9724 if (pf->veb[i]->uplink_seid == uplink_seid)
9725 n++; /* count the VEBs */
9726 if (pf->veb[i]->seid == uplink_seid)
9727 veb = pf->veb[i];
9728 }
9729 if (n == 0 && veb && veb->uplink_seid != 0)
9730 i40e_veb_release(veb);
9731
9732 return 0;
9733}
9734
9735/**
9736 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9737 * @vsi: ptr to the VSI
9738 *
9739 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9740 * corresponding SW VSI structure and initializes num_queue_pairs for the
9741 * newly allocated VSI.
9742 *
9743 * Returns 0 on success or negative on failure
9744 **/
9745static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9746{
9747 int ret = -ENOENT;
9748 struct i40e_pf *pf = vsi->back;
9749
Alexander Duyck493fb302013-09-28 07:01:44 +00009750 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009751 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9752 vsi->seid);
9753 return -EEXIST;
9754 }
9755
9756 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009757 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009758 vsi->seid, vsi->base_vector);
9759 return -EEXIST;
9760 }
9761
Greg Rose90e04072014-03-06 08:59:57 +00009762 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009763 if (ret) {
9764 dev_info(&pf->pdev->dev,
9765 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9766 vsi->num_q_vectors, vsi->seid, ret);
9767 vsi->num_q_vectors = 0;
9768 goto vector_setup_out;
9769 }
9770
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009771 /* In Legacy mode, we do not have to get any other vector since we
9772 * piggyback on the misc/ICR0 for queue interrupts.
9773 */
9774 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9775 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009776 if (vsi->num_q_vectors)
9777 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9778 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009779 if (vsi->base_vector < 0) {
9780 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009781 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9782 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009783 i40e_vsi_free_q_vectors(vsi);
9784 ret = -ENOENT;
9785 goto vector_setup_out;
9786 }
9787
9788vector_setup_out:
9789 return ret;
9790}
9791
9792/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009793 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9794 * @vsi: pointer to the vsi.
9795 *
9796 * This re-allocates a vsi's queue resources.
9797 *
9798 * Returns pointer to the successfully allocated and configured VSI sw struct
9799 * on success, otherwise returns NULL on failure.
9800 **/
9801static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9802{
John Underwoodf5340392016-02-18 09:19:24 -08009803 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009804 u8 enabled_tc;
9805 int ret;
9806
John Underwoodf5340392016-02-18 09:19:24 -08009807 if (!vsi)
9808 return NULL;
9809
9810 pf = vsi->back;
9811
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009812 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9813 i40e_vsi_clear_rings(vsi);
9814
9815 i40e_vsi_free_arrays(vsi, false);
9816 i40e_set_num_rings_in_vsi(vsi);
9817 ret = i40e_vsi_alloc_arrays(vsi, false);
9818 if (ret)
9819 goto err_vsi;
9820
9821 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9822 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009823 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009824 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009825 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009826 goto err_vsi;
9827 }
9828 vsi->base_queue = ret;
9829
9830 /* Update the FW view of the VSI. Force a reset of TC and queue
9831 * layout configurations.
9832 */
9833 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9834 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9835 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9836 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009837 if (vsi->type == I40E_VSI_MAIN)
9838 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009839
9840 /* assign it some queues */
9841 ret = i40e_alloc_rings(vsi);
9842 if (ret)
9843 goto err_rings;
9844
9845 /* map all of the rings to the q_vectors */
9846 i40e_vsi_map_rings_to_vectors(vsi);
9847 return vsi;
9848
9849err_rings:
9850 i40e_vsi_free_q_vectors(vsi);
9851 if (vsi->netdev_registered) {
9852 vsi->netdev_registered = false;
9853 unregister_netdev(vsi->netdev);
9854 free_netdev(vsi->netdev);
9855 vsi->netdev = NULL;
9856 }
9857 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9858err_vsi:
9859 i40e_vsi_clear(vsi);
9860 return NULL;
9861}
9862
9863/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009864 * i40e_vsi_setup - Set up a VSI by a given type
9865 * @pf: board private structure
9866 * @type: VSI type
9867 * @uplink_seid: the switch element to link to
9868 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9869 *
9870 * This allocates the sw VSI structure and its queue resources, then add a VSI
9871 * to the identified VEB.
9872 *
9873 * Returns pointer to the successfully allocated and configure VSI sw struct on
9874 * success, otherwise returns NULL on failure.
9875 **/
9876struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9877 u16 uplink_seid, u32 param1)
9878{
9879 struct i40e_vsi *vsi = NULL;
9880 struct i40e_veb *veb = NULL;
9881 int ret, i;
9882 int v_idx;
9883
9884 /* The requested uplink_seid must be either
9885 * - the PF's port seid
9886 * no VEB is needed because this is the PF
9887 * or this is a Flow Director special case VSI
9888 * - seid of an existing VEB
9889 * - seid of a VSI that owns an existing VEB
9890 * - seid of a VSI that doesn't own a VEB
9891 * a new VEB is created and the VSI becomes the owner
9892 * - seid of the PF VSI, which is what creates the first VEB
9893 * this is a special case of the previous
9894 *
9895 * Find which uplink_seid we were given and create a new VEB if needed
9896 */
9897 for (i = 0; i < I40E_MAX_VEB; i++) {
9898 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9899 veb = pf->veb[i];
9900 break;
9901 }
9902 }
9903
9904 if (!veb && uplink_seid != pf->mac_seid) {
9905
Mitch Williams505682c2014-05-20 08:01:37 +00009906 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009907 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9908 vsi = pf->vsi[i];
9909 break;
9910 }
9911 }
9912 if (!vsi) {
9913 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9914 uplink_seid);
9915 return NULL;
9916 }
9917
9918 if (vsi->uplink_seid == pf->mac_seid)
9919 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9920 vsi->tc_config.enabled_tc);
9921 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9922 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9923 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009924 if (veb) {
9925 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9926 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009927 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009928 return NULL;
9929 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009930 /* We come up by default in VEPA mode if SRIOV is not
9931 * already enabled, in which case we can't force VEPA
9932 * mode.
9933 */
9934 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9935 veb->bridge_mode = BRIDGE_MODE_VEPA;
9936 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9937 }
Neerav Parikh51616012015-02-06 08:52:14 +00009938 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009939 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009940 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9941 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9942 veb = pf->veb[i];
9943 }
9944 if (!veb) {
9945 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9946 return NULL;
9947 }
9948
9949 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9950 uplink_seid = veb->seid;
9951 }
9952
9953 /* get vsi sw struct */
9954 v_idx = i40e_vsi_mem_alloc(pf, type);
9955 if (v_idx < 0)
9956 goto err_alloc;
9957 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009958 if (!vsi)
9959 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009960 vsi->type = type;
9961 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9962
9963 if (type == I40E_VSI_MAIN)
9964 pf->lan_vsi = v_idx;
9965 else if (type == I40E_VSI_SRIOV)
9966 vsi->vf_id = param1;
9967 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009968 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9969 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009971 dev_info(&pf->pdev->dev,
9972 "failed to get tracking for %d queues for VSI %d err=%d\n",
9973 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009974 goto err_vsi;
9975 }
9976 vsi->base_queue = ret;
9977
9978 /* get a VSI from the hardware */
9979 vsi->uplink_seid = uplink_seid;
9980 ret = i40e_add_vsi(vsi);
9981 if (ret)
9982 goto err_vsi;
9983
9984 switch (vsi->type) {
9985 /* setup the netdev if needed */
9986 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009987 /* Apply relevant filters if a platform-specific mac
9988 * address was selected.
9989 */
9990 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9991 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9992 if (ret) {
9993 dev_warn(&pf->pdev->dev,
9994 "could not set up macaddr; err %d\n",
9995 ret);
9996 }
9997 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009998 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009999 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010000 ret = i40e_config_netdev(vsi);
10001 if (ret)
10002 goto err_netdev;
10003 ret = register_netdev(vsi->netdev);
10004 if (ret)
10005 goto err_netdev;
10006 vsi->netdev_registered = true;
10007 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010008#ifdef CONFIG_I40E_DCB
10009 /* Setup DCB netlink interface */
10010 i40e_dcbnl_setup(vsi);
10011#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010012 /* fall through */
10013
10014 case I40E_VSI_FDIR:
10015 /* set up vectors and rings if needed */
10016 ret = i40e_vsi_setup_vectors(vsi);
10017 if (ret)
10018 goto err_msix;
10019
10020 ret = i40e_alloc_rings(vsi);
10021 if (ret)
10022 goto err_rings;
10023
10024 /* map all of the rings to the q_vectors */
10025 i40e_vsi_map_rings_to_vectors(vsi);
10026
10027 i40e_vsi_reset_stats(vsi);
10028 break;
10029
10030 default:
10031 /* no netdev or rings for the other VSI types */
10032 break;
10033 }
10034
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010035 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10036 (vsi->type == I40E_VSI_VMDQ2)) {
10037 ret = i40e_vsi_config_rss(vsi);
10038 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010039 return vsi;
10040
10041err_rings:
10042 i40e_vsi_free_q_vectors(vsi);
10043err_msix:
10044 if (vsi->netdev_registered) {
10045 vsi->netdev_registered = false;
10046 unregister_netdev(vsi->netdev);
10047 free_netdev(vsi->netdev);
10048 vsi->netdev = NULL;
10049 }
10050err_netdev:
10051 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10052err_vsi:
10053 i40e_vsi_clear(vsi);
10054err_alloc:
10055 return NULL;
10056}
10057
10058/**
10059 * i40e_veb_get_bw_info - Query VEB BW information
10060 * @veb: the veb to query
10061 *
10062 * Query the Tx scheduler BW configuration data for given VEB
10063 **/
10064static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10065{
10066 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10067 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10068 struct i40e_pf *pf = veb->pf;
10069 struct i40e_hw *hw = &pf->hw;
10070 u32 tc_bw_max;
10071 int ret = 0;
10072 int i;
10073
10074 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10075 &bw_data, NULL);
10076 if (ret) {
10077 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010078 "query veb bw config failed, err %s aq_err %s\n",
10079 i40e_stat_str(&pf->hw, ret),
10080 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010081 goto out;
10082 }
10083
10084 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10085 &ets_data, NULL);
10086 if (ret) {
10087 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010088 "query veb bw ets config failed, err %s aq_err %s\n",
10089 i40e_stat_str(&pf->hw, ret),
10090 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010091 goto out;
10092 }
10093
10094 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10095 veb->bw_max_quanta = ets_data.tc_bw_max;
10096 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010097 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010098 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10099 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10100 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10101 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10102 veb->bw_tc_limit_credits[i] =
10103 le16_to_cpu(bw_data.tc_bw_limits[i]);
10104 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10105 }
10106
10107out:
10108 return ret;
10109}
10110
10111/**
10112 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10113 * @pf: board private structure
10114 *
10115 * On error: returns error code (negative)
10116 * On success: returns vsi index in PF (positive)
10117 **/
10118static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10119{
10120 int ret = -ENOENT;
10121 struct i40e_veb *veb;
10122 int i;
10123
10124 /* Need to protect the allocation of switch elements at the PF level */
10125 mutex_lock(&pf->switch_mutex);
10126
10127 /* VEB list may be fragmented if VEB creation/destruction has
10128 * been happening. We can afford to do a quick scan to look
10129 * for any free slots in the list.
10130 *
10131 * find next empty veb slot, looping back around if necessary
10132 */
10133 i = 0;
10134 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10135 i++;
10136 if (i >= I40E_MAX_VEB) {
10137 ret = -ENOMEM;
10138 goto err_alloc_veb; /* out of VEB slots! */
10139 }
10140
10141 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10142 if (!veb) {
10143 ret = -ENOMEM;
10144 goto err_alloc_veb;
10145 }
10146 veb->pf = pf;
10147 veb->idx = i;
10148 veb->enabled_tc = 1;
10149
10150 pf->veb[i] = veb;
10151 ret = i;
10152err_alloc_veb:
10153 mutex_unlock(&pf->switch_mutex);
10154 return ret;
10155}
10156
10157/**
10158 * i40e_switch_branch_release - Delete a branch of the switch tree
10159 * @branch: where to start deleting
10160 *
10161 * This uses recursion to find the tips of the branch to be
10162 * removed, deleting until we get back to and can delete this VEB.
10163 **/
10164static void i40e_switch_branch_release(struct i40e_veb *branch)
10165{
10166 struct i40e_pf *pf = branch->pf;
10167 u16 branch_seid = branch->seid;
10168 u16 veb_idx = branch->idx;
10169 int i;
10170
10171 /* release any VEBs on this VEB - RECURSION */
10172 for (i = 0; i < I40E_MAX_VEB; i++) {
10173 if (!pf->veb[i])
10174 continue;
10175 if (pf->veb[i]->uplink_seid == branch->seid)
10176 i40e_switch_branch_release(pf->veb[i]);
10177 }
10178
10179 /* Release the VSIs on this VEB, but not the owner VSI.
10180 *
10181 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10182 * the VEB itself, so don't use (*branch) after this loop.
10183 */
Mitch Williams505682c2014-05-20 08:01:37 +000010184 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010185 if (!pf->vsi[i])
10186 continue;
10187 if (pf->vsi[i]->uplink_seid == branch_seid &&
10188 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10189 i40e_vsi_release(pf->vsi[i]);
10190 }
10191 }
10192
10193 /* There's one corner case where the VEB might not have been
10194 * removed, so double check it here and remove it if needed.
10195 * This case happens if the veb was created from the debugfs
10196 * commands and no VSIs were added to it.
10197 */
10198 if (pf->veb[veb_idx])
10199 i40e_veb_release(pf->veb[veb_idx]);
10200}
10201
10202/**
10203 * i40e_veb_clear - remove veb struct
10204 * @veb: the veb to remove
10205 **/
10206static void i40e_veb_clear(struct i40e_veb *veb)
10207{
10208 if (!veb)
10209 return;
10210
10211 if (veb->pf) {
10212 struct i40e_pf *pf = veb->pf;
10213
10214 mutex_lock(&pf->switch_mutex);
10215 if (pf->veb[veb->idx] == veb)
10216 pf->veb[veb->idx] = NULL;
10217 mutex_unlock(&pf->switch_mutex);
10218 }
10219
10220 kfree(veb);
10221}
10222
10223/**
10224 * i40e_veb_release - Delete a VEB and free its resources
10225 * @veb: the VEB being removed
10226 **/
10227void i40e_veb_release(struct i40e_veb *veb)
10228{
10229 struct i40e_vsi *vsi = NULL;
10230 struct i40e_pf *pf;
10231 int i, n = 0;
10232
10233 pf = veb->pf;
10234
10235 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010236 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010237 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10238 n++;
10239 vsi = pf->vsi[i];
10240 }
10241 }
10242 if (n != 1) {
10243 dev_info(&pf->pdev->dev,
10244 "can't remove VEB %d with %d VSIs left\n",
10245 veb->seid, n);
10246 return;
10247 }
10248
10249 /* move the remaining VSI to uplink veb */
10250 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10251 if (veb->uplink_seid) {
10252 vsi->uplink_seid = veb->uplink_seid;
10253 if (veb->uplink_seid == pf->mac_seid)
10254 vsi->veb_idx = I40E_NO_VEB;
10255 else
10256 vsi->veb_idx = veb->veb_idx;
10257 } else {
10258 /* floating VEB */
10259 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10260 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10261 }
10262
10263 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10264 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010265}
10266
10267/**
10268 * i40e_add_veb - create the VEB in the switch
10269 * @veb: the VEB to be instantiated
10270 * @vsi: the controlling VSI
10271 **/
10272static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10273{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010274 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010275 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010276 int ret;
10277
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010278 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010279 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010280 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010281
10282 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010283 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010284 dev_info(&pf->pdev->dev,
10285 "couldn't add VEB, err %s aq_err %s\n",
10286 i40e_stat_str(&pf->hw, ret),
10287 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010288 return -EPERM;
10289 }
10290
10291 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010292 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010293 &veb->stats_idx, NULL, NULL, NULL);
10294 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010295 dev_info(&pf->pdev->dev,
10296 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10297 i40e_stat_str(&pf->hw, ret),
10298 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010299 return -EPERM;
10300 }
10301 ret = i40e_veb_get_bw_info(veb);
10302 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010303 dev_info(&pf->pdev->dev,
10304 "couldn't get VEB bw info, err %s aq_err %s\n",
10305 i40e_stat_str(&pf->hw, ret),
10306 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10307 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010308 return -ENOENT;
10309 }
10310
10311 vsi->uplink_seid = veb->seid;
10312 vsi->veb_idx = veb->idx;
10313 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10314
10315 return 0;
10316}
10317
10318/**
10319 * i40e_veb_setup - Set up a VEB
10320 * @pf: board private structure
10321 * @flags: VEB setup flags
10322 * @uplink_seid: the switch element to link to
10323 * @vsi_seid: the initial VSI seid
10324 * @enabled_tc: Enabled TC bit-map
10325 *
10326 * This allocates the sw VEB structure and links it into the switch
10327 * It is possible and legal for this to be a duplicate of an already
10328 * existing VEB. It is also possible for both uplink and vsi seids
10329 * to be zero, in order to create a floating VEB.
10330 *
10331 * Returns pointer to the successfully allocated VEB sw struct on
10332 * success, otherwise returns NULL on failure.
10333 **/
10334struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10335 u16 uplink_seid, u16 vsi_seid,
10336 u8 enabled_tc)
10337{
10338 struct i40e_veb *veb, *uplink_veb = NULL;
10339 int vsi_idx, veb_idx;
10340 int ret;
10341
10342 /* if one seid is 0, the other must be 0 to create a floating relay */
10343 if ((uplink_seid == 0 || vsi_seid == 0) &&
10344 (uplink_seid + vsi_seid != 0)) {
10345 dev_info(&pf->pdev->dev,
10346 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10347 uplink_seid, vsi_seid);
10348 return NULL;
10349 }
10350
10351 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010352 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010353 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10354 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010355 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010356 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10357 vsi_seid);
10358 return NULL;
10359 }
10360
10361 if (uplink_seid && uplink_seid != pf->mac_seid) {
10362 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10363 if (pf->veb[veb_idx] &&
10364 pf->veb[veb_idx]->seid == uplink_seid) {
10365 uplink_veb = pf->veb[veb_idx];
10366 break;
10367 }
10368 }
10369 if (!uplink_veb) {
10370 dev_info(&pf->pdev->dev,
10371 "uplink seid %d not found\n", uplink_seid);
10372 return NULL;
10373 }
10374 }
10375
10376 /* get veb sw struct */
10377 veb_idx = i40e_veb_mem_alloc(pf);
10378 if (veb_idx < 0)
10379 goto err_alloc;
10380 veb = pf->veb[veb_idx];
10381 veb->flags = flags;
10382 veb->uplink_seid = uplink_seid;
10383 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10384 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10385
10386 /* create the VEB in the switch */
10387 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10388 if (ret)
10389 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010390 if (vsi_idx == pf->lan_vsi)
10391 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010392
10393 return veb;
10394
10395err_veb:
10396 i40e_veb_clear(veb);
10397err_alloc:
10398 return NULL;
10399}
10400
10401/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010402 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010403 * @pf: board private structure
10404 * @ele: element we are building info from
10405 * @num_reported: total number of elements
10406 * @printconfig: should we print the contents
10407 *
10408 * helper function to assist in extracting a few useful SEID values.
10409 **/
10410static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10411 struct i40e_aqc_switch_config_element_resp *ele,
10412 u16 num_reported, bool printconfig)
10413{
10414 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10415 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10416 u8 element_type = ele->element_type;
10417 u16 seid = le16_to_cpu(ele->seid);
10418
10419 if (printconfig)
10420 dev_info(&pf->pdev->dev,
10421 "type=%d seid=%d uplink=%d downlink=%d\n",
10422 element_type, seid, uplink_seid, downlink_seid);
10423
10424 switch (element_type) {
10425 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10426 pf->mac_seid = seid;
10427 break;
10428 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10429 /* Main VEB? */
10430 if (uplink_seid != pf->mac_seid)
10431 break;
10432 if (pf->lan_veb == I40E_NO_VEB) {
10433 int v;
10434
10435 /* find existing or else empty VEB */
10436 for (v = 0; v < I40E_MAX_VEB; v++) {
10437 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10438 pf->lan_veb = v;
10439 break;
10440 }
10441 }
10442 if (pf->lan_veb == I40E_NO_VEB) {
10443 v = i40e_veb_mem_alloc(pf);
10444 if (v < 0)
10445 break;
10446 pf->lan_veb = v;
10447 }
10448 }
10449
10450 pf->veb[pf->lan_veb]->seid = seid;
10451 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10452 pf->veb[pf->lan_veb]->pf = pf;
10453 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10454 break;
10455 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10456 if (num_reported != 1)
10457 break;
10458 /* This is immediately after a reset so we can assume this is
10459 * the PF's VSI
10460 */
10461 pf->mac_seid = uplink_seid;
10462 pf->pf_seid = downlink_seid;
10463 pf->main_vsi_seid = seid;
10464 if (printconfig)
10465 dev_info(&pf->pdev->dev,
10466 "pf_seid=%d main_vsi_seid=%d\n",
10467 pf->pf_seid, pf->main_vsi_seid);
10468 break;
10469 case I40E_SWITCH_ELEMENT_TYPE_PF:
10470 case I40E_SWITCH_ELEMENT_TYPE_VF:
10471 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10472 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10473 case I40E_SWITCH_ELEMENT_TYPE_PE:
10474 case I40E_SWITCH_ELEMENT_TYPE_PA:
10475 /* ignore these for now */
10476 break;
10477 default:
10478 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10479 element_type, seid);
10480 break;
10481 }
10482}
10483
10484/**
10485 * i40e_fetch_switch_configuration - Get switch config from firmware
10486 * @pf: board private structure
10487 * @printconfig: should we print the contents
10488 *
10489 * Get the current switch configuration from the device and
10490 * extract a few useful SEID values.
10491 **/
10492int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10493{
10494 struct i40e_aqc_get_switch_config_resp *sw_config;
10495 u16 next_seid = 0;
10496 int ret = 0;
10497 u8 *aq_buf;
10498 int i;
10499
10500 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10501 if (!aq_buf)
10502 return -ENOMEM;
10503
10504 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10505 do {
10506 u16 num_reported, num_total;
10507
10508 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10509 I40E_AQ_LARGE_BUF,
10510 &next_seid, NULL);
10511 if (ret) {
10512 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010513 "get switch config failed err %s aq_err %s\n",
10514 i40e_stat_str(&pf->hw, ret),
10515 i40e_aq_str(&pf->hw,
10516 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010517 kfree(aq_buf);
10518 return -ENOENT;
10519 }
10520
10521 num_reported = le16_to_cpu(sw_config->header.num_reported);
10522 num_total = le16_to_cpu(sw_config->header.num_total);
10523
10524 if (printconfig)
10525 dev_info(&pf->pdev->dev,
10526 "header: %d reported %d total\n",
10527 num_reported, num_total);
10528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010529 for (i = 0; i < num_reported; i++) {
10530 struct i40e_aqc_switch_config_element_resp *ele =
10531 &sw_config->element[i];
10532
10533 i40e_setup_pf_switch_element(pf, ele, num_reported,
10534 printconfig);
10535 }
10536 } while (next_seid != 0);
10537
10538 kfree(aq_buf);
10539 return ret;
10540}
10541
10542/**
10543 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10544 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010545 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010546 *
10547 * Returns 0 on success, negative value on failure
10548 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010549static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010550{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010551 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010552 int ret;
10553
10554 /* find out what's out there already */
10555 ret = i40e_fetch_switch_configuration(pf, false);
10556 if (ret) {
10557 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010558 "couldn't fetch switch config, err %s aq_err %s\n",
10559 i40e_stat_str(&pf->hw, ret),
10560 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010561 return ret;
10562 }
10563 i40e_pf_reset_stats(pf);
10564
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010565 /* set the switch config bit for the whole device to
10566 * support limited promisc or true promisc
10567 * when user requests promisc. The default is limited
10568 * promisc.
10569 */
10570
10571 if ((pf->hw.pf_id == 0) &&
10572 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10573 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10574
10575 if (pf->hw.pf_id == 0) {
10576 u16 valid_flags;
10577
10578 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10579 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10580 NULL);
10581 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10582 dev_info(&pf->pdev->dev,
10583 "couldn't set switch config bits, err %s aq_err %s\n",
10584 i40e_stat_str(&pf->hw, ret),
10585 i40e_aq_str(&pf->hw,
10586 pf->hw.aq.asq_last_status));
10587 /* not a fatal problem, just keep going */
10588 }
10589 }
10590
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010591 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010592 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010593 struct i40e_vsi *vsi = NULL;
10594 u16 uplink_seid;
10595
10596 /* Set up the PF VSI associated with the PF's main VSI
10597 * that is already in the HW switch
10598 */
10599 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10600 uplink_seid = pf->veb[pf->lan_veb]->seid;
10601 else
10602 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010603 if (pf->lan_vsi == I40E_NO_VSI)
10604 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10605 else if (reinit)
10606 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010607 if (!vsi) {
10608 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10609 i40e_fdir_teardown(pf);
10610 return -EAGAIN;
10611 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010612 } else {
10613 /* force a reset of TC and queue layout configurations */
10614 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010616 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10617 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10618 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10619 }
10620 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10621
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010622 i40e_fdir_sb_setup(pf);
10623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010624 /* Setup static PF queue filter control settings */
10625 ret = i40e_setup_pf_filter_control(pf);
10626 if (ret) {
10627 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10628 ret);
10629 /* Failure here should not stop continuing other steps */
10630 }
10631
10632 /* enable RSS in the HW, even for only one queue, as the stack can use
10633 * the hash
10634 */
10635 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010636 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010637
10638 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010639 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010640 i40e_link_event(pf);
10641
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010642 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010643 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10644 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010645
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010646 i40e_ptp_init(pf);
10647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010648 return ret;
10649}
10650
10651/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010652 * i40e_determine_queue_usage - Work out queue distribution
10653 * @pf: board private structure
10654 **/
10655static void i40e_determine_queue_usage(struct i40e_pf *pf)
10656{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010657 int queues_left;
10658
10659 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010660#ifdef I40E_FCOE
10661 pf->num_fcoe_qps = 0;
10662#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010663
10664 /* Find the max queues to be put into basic use. We'll always be
10665 * using TC0, whether or not DCB is running, and TC0 will get the
10666 * big RSS set.
10667 */
10668 queues_left = pf->hw.func_caps.num_tx_qp;
10669
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010670 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010671 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010672 /* one qp for PF, no queues for anything else */
10673 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010674 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010675
10676 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010677 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010678 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010679#ifdef I40E_FCOE
10680 I40E_FLAG_FCOE_ENABLED |
10681#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010682 I40E_FLAG_FD_SB_ENABLED |
10683 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010684 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010685 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010686 I40E_FLAG_SRIOV_ENABLED |
10687 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010688 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10689 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010690 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010691 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010692 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010693 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010694 queues_left -= pf->num_lan_qps;
10695
10696 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010697 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010698#ifdef I40E_FCOE
10699 I40E_FLAG_FCOE_ENABLED |
10700#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010701 I40E_FLAG_FD_SB_ENABLED |
10702 I40E_FLAG_FD_ATR_ENABLED |
10703 I40E_FLAG_DCB_ENABLED |
10704 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010705 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010706 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010707 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010708 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010709 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10710 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010711 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10712 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010713 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10714 num_online_cpus());
10715 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10716 pf->hw.func_caps.num_tx_qp);
10717
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010718 queues_left -= pf->num_lan_qps;
10719 }
10720
Vasu Dev38e00432014-08-01 13:27:03 -070010721#ifdef I40E_FCOE
10722 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10723 if (I40E_DEFAULT_FCOE <= queues_left) {
10724 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10725 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10726 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10727 } else {
10728 pf->num_fcoe_qps = 0;
10729 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10730 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10731 }
10732
10733 queues_left -= pf->num_fcoe_qps;
10734 }
10735
10736#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010737 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10738 if (queues_left > 1) {
10739 queues_left -= 1; /* save 1 queue for FD */
10740 } else {
10741 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10742 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10743 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010744 }
10745
10746 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10747 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010748 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10749 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010750 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10751 }
10752
10753 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10754 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10755 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10756 (queues_left / pf->num_vmdq_qps));
10757 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10758 }
10759
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010760 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010761 dev_dbg(&pf->pdev->dev,
10762 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10763 pf->hw.func_caps.num_tx_qp,
10764 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010765 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10766 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10767 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010768#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010769 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010770#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010771}
10772
10773/**
10774 * i40e_setup_pf_filter_control - Setup PF static filter control
10775 * @pf: PF to be setup
10776 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010777 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010778 * settings. If PE/FCoE are enabled then it will also set the per PF
10779 * based filter sizes required for them. It also enables Flow director,
10780 * ethertype and macvlan type filter settings for the pf.
10781 *
10782 * Returns 0 on success, negative on failure
10783 **/
10784static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10785{
10786 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10787
10788 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10789
10790 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010791 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010792 settings->enable_fdir = true;
10793
10794 /* Ethtype and MACVLAN filters enabled for PF */
10795 settings->enable_ethtype = true;
10796 settings->enable_macvlan = true;
10797
10798 if (i40e_set_filter_control(&pf->hw, settings))
10799 return -ENOENT;
10800
10801 return 0;
10802}
10803
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010804#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010805#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010806static void i40e_print_features(struct i40e_pf *pf)
10807{
10808 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010809 char *buf;
10810 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010811
Joe Perches3b195842015-12-03 04:20:57 -080010812 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10813 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010814 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010815
Joe Perches3b195842015-12-03 04:20:57 -080010816 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010817#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010818 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010819#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010820 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010821 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010822 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010823 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010824 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010825 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010826 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010827 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010828 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10829 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010830 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010831 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010832 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010833 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010834 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010835 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010836 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010837#ifdef I40E_FCOE
10838 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010839 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010840#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010841 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010842 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010843 else
Joe Perches3b195842015-12-03 04:20:57 -080010844 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010845
Joe Perches3b195842015-12-03 04:20:57 -080010846 dev_info(&pf->pdev->dev, "%s\n", buf);
10847 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010848 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010849}
10850
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010851/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010852 * i40e_get_platform_mac_addr - get platform-specific MAC address
10853 *
10854 * @pdev: PCI device information struct
10855 * @pf: board private structure
10856 *
10857 * Look up the MAC address in Open Firmware on systems that support it,
10858 * and use IDPROM on SPARC if no OF address is found. On return, the
10859 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10860 * has been selected.
10861 **/
10862static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10863{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010864 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010865 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010866 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010867}
10868
10869/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010870 * i40e_probe - Device initialization routine
10871 * @pdev: PCI device information struct
10872 * @ent: entry in i40e_pci_tbl
10873 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010874 * i40e_probe initializes a PF identified by a pci_dev structure.
10875 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010876 * and a hardware reset occur.
10877 *
10878 * Returns 0 on success, negative on failure
10879 **/
10880static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10881{
Catherine Sullivane8278452015-02-06 08:52:08 +000010882 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010883 struct i40e_pf *pf;
10884 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010885 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010886 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010887 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010888 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010889 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010890 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010891 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010892
10893 err = pci_enable_device_mem(pdev);
10894 if (err)
10895 return err;
10896
10897 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010898 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010899 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010900 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10901 if (err) {
10902 dev_err(&pdev->dev,
10903 "DMA configuration failed: 0x%x\n", err);
10904 goto err_dma;
10905 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010906 }
10907
10908 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010909 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010910 if (err) {
10911 dev_info(&pdev->dev,
10912 "pci_request_selected_regions failed %d\n", err);
10913 goto err_pci_reg;
10914 }
10915
10916 pci_enable_pcie_error_reporting(pdev);
10917 pci_set_master(pdev);
10918
10919 /* Now that we have a PCI connection, we need to do the
10920 * low level device setup. This is primarily setting up
10921 * the Admin Queue structures and then querying for the
10922 * device's current profile information.
10923 */
10924 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10925 if (!pf) {
10926 err = -ENOMEM;
10927 goto err_pf_alloc;
10928 }
10929 pf->next_vsi = 0;
10930 pf->pdev = pdev;
10931 set_bit(__I40E_DOWN, &pf->state);
10932
10933 hw = &pf->hw;
10934 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010935
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010936 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10937 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010938
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010939 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010940 if (!hw->hw_addr) {
10941 err = -EIO;
10942 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10943 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010944 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010945 goto err_ioremap;
10946 }
10947 hw->vendor_id = pdev->vendor;
10948 hw->device_id = pdev->device;
10949 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10950 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10951 hw->subsystem_device_id = pdev->subsystem_device;
10952 hw->bus.device = PCI_SLOT(pdev->devfn);
10953 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010954 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010955
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010956 /* set up the locks for the AQ, do this only once in probe
10957 * and destroy them only once in remove
10958 */
10959 mutex_init(&hw->aq.asq_mutex);
10960 mutex_init(&hw->aq.arq_mutex);
10961
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010962 pf->msg_enable = netif_msg_init(debug,
10963 NETIF_MSG_DRV |
10964 NETIF_MSG_PROBE |
10965 NETIF_MSG_LINK);
10966 if (debug < -1)
10967 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010968
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010969 /* do a special CORER for clearing PXE mode once at init */
10970 if (hw->revision_id == 0 &&
10971 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10972 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10973 i40e_flush(hw);
10974 msleep(200);
10975 pf->corer_count++;
10976
10977 i40e_clear_pxe_mode(hw);
10978 }
10979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010980 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010981 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010982 err = i40e_pf_reset(hw);
10983 if (err) {
10984 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10985 goto err_pf_reset;
10986 }
10987 pf->pfr_count++;
10988
10989 hw->aq.num_arq_entries = I40E_AQ_LEN;
10990 hw->aq.num_asq_entries = I40E_AQ_LEN;
10991 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10992 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10993 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010994
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010995 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010996 "%s-%s:misc",
10997 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010998
10999 err = i40e_init_shared_code(hw);
11000 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011001 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11002 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011003 goto err_pf_reset;
11004 }
11005
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011006 /* set up a default setting for link flow control */
11007 pf->hw.fc.requested_mode = I40E_FC_NONE;
11008
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011009 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011010 if (err) {
11011 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11012 dev_info(&pdev->dev,
11013 "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");
11014 else
11015 dev_info(&pdev->dev,
11016 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11017
11018 goto err_pf_reset;
11019 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011020
Shannon Nelson6dec1012015-09-28 14:12:30 -040011021 /* provide nvm, fw, api versions */
11022 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11023 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11024 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11025 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011026
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011027 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11028 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011029 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011030 "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");
11031 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11032 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011033 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011034 "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 +000011035
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011036 i40e_verify_eeprom(pf);
11037
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011038 /* Rev 0 hardware was never productized */
11039 if (hw->revision_id < 1)
11040 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");
11041
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011042 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 err = i40e_get_capabilities(pf);
11044 if (err)
11045 goto err_adminq_setup;
11046
11047 err = i40e_sw_init(pf);
11048 if (err) {
11049 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11050 goto err_sw_init;
11051 }
11052
11053 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11054 hw->func_caps.num_rx_qp,
11055 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11056 if (err) {
11057 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11058 goto err_init_lan_hmc;
11059 }
11060
11061 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11062 if (err) {
11063 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11064 err = -ENOENT;
11065 goto err_configure_lan_hmc;
11066 }
11067
Neerav Parikhb686ece2014-12-14 01:55:11 +000011068 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11069 * Ignore error return codes because if it was already disabled via
11070 * hardware settings this will fail
11071 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011072 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011073 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11074 i40e_aq_stop_lldp(hw, true, NULL);
11075 }
11076
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011077 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011078 /* allow a platform config to override the HW addr */
11079 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011080 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011081 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11082 err = -EIO;
11083 goto err_mac_addr;
11084 }
11085 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011086 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011087 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11088 if (is_valid_ether_addr(hw->mac.port_addr))
11089 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011090#ifdef I40E_FCOE
11091 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11092 if (err)
11093 dev_info(&pdev->dev,
11094 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11095 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11096 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11097 hw->mac.san_addr);
11098 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11099 }
11100 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11101#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011102
11103 pci_set_drvdata(pdev, pf);
11104 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011105#ifdef CONFIG_I40E_DCB
11106 err = i40e_init_pf_dcb(pf);
11107 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011108 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011109 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011110 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011111 }
11112#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113
11114 /* set up periodic task facility */
11115 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11116 pf->service_timer_period = HZ;
11117
11118 INIT_WORK(&pf->service_task, i40e_service_task);
11119 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11120 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011121
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011122 /* NVM bit on means WoL disabled for the port */
11123 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011124 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011125 pf->wol_en = false;
11126 else
11127 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011128 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11129
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011130 /* set up the main switch operations */
11131 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011132 err = i40e_init_interrupt_scheme(pf);
11133 if (err)
11134 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011135
Mitch Williams505682c2014-05-20 08:01:37 +000011136 /* The number of VSIs reported by the FW is the minimum guaranteed
11137 * to us; HW supports far more and we share the remaining pool with
11138 * the other PFs. We allocate space for more than the guarantee with
11139 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011140 */
Mitch Williams505682c2014-05-20 08:01:37 +000011141 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11142 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11143 else
11144 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11145
11146 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011147 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11148 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011149 if (!pf->vsi) {
11150 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011151 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011153
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011154#ifdef CONFIG_PCI_IOV
11155 /* prep for VF support */
11156 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11157 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11158 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11159 if (pci_num_vf(pdev))
11160 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11161 }
11162#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011163 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011164 if (err) {
11165 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11166 goto err_vsis;
11167 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011168
11169 /* Make sure flow control is set according to current settings */
11170 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11171 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11172 dev_dbg(&pf->pdev->dev,
11173 "Set fc with err %s aq_err %s on get_phy_cap\n",
11174 i40e_stat_str(hw, err),
11175 i40e_aq_str(hw, hw->aq.asq_last_status));
11176 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11177 dev_dbg(&pf->pdev->dev,
11178 "Set fc with err %s aq_err %s on set_phy_config\n",
11179 i40e_stat_str(hw, err),
11180 i40e_aq_str(hw, hw->aq.asq_last_status));
11181 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11182 dev_dbg(&pf->pdev->dev,
11183 "Set fc with err %s aq_err %s on get_link_info\n",
11184 i40e_stat_str(hw, err),
11185 i40e_aq_str(hw, hw->aq.asq_last_status));
11186
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011187 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011188 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011189 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11190 i40e_vsi_open(pf->vsi[i]);
11191 break;
11192 }
11193 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011194
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011195 /* The driver only wants link up/down and module qualification
11196 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011197 */
11198 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011199 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011200 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011201 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011202 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011203 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11204 i40e_stat_str(&pf->hw, err),
11205 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011206
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011207 /* Reconfigure hardware for allowing smaller MSS in the case
11208 * of TSO, so that we avoid the MDD being fired and causing
11209 * a reset in the case of small MSS+TSO.
11210 */
11211 val = rd32(hw, I40E_REG_MSS);
11212 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11213 val &= ~I40E_REG_MSS_MIN_MASK;
11214 val |= I40E_64BYTE_MSS;
11215 wr32(hw, I40E_REG_MSS, val);
11216 }
11217
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011218 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011219 msleep(75);
11220 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11221 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011222 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11223 i40e_stat_str(&pf->hw, err),
11224 i40e_aq_str(&pf->hw,
11225 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011226 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011227 /* The main driver is (mostly) up and happy. We need to set this state
11228 * before setting up the misc vector or we get a race and the vector
11229 * ends up disabled forever.
11230 */
11231 clear_bit(__I40E_DOWN, &pf->state);
11232
11233 /* In case of MSIX we are going to setup the misc vector right here
11234 * to handle admin queue events etc. In case of legacy and MSI
11235 * the misc functionality and queue processing is combined in
11236 * the same vector and that gets setup at open.
11237 */
11238 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11239 err = i40e_setup_misc_vector(pf);
11240 if (err) {
11241 dev_info(&pdev->dev,
11242 "setup of misc vector failed: %d\n", err);
11243 goto err_vsis;
11244 }
11245 }
11246
Greg Rosedf805f62014-04-04 04:43:16 +000011247#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011248 /* prep for VF support */
11249 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011250 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11251 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011252 /* disable link interrupts for VFs */
11253 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11254 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11255 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11256 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011257
11258 if (pci_num_vf(pdev)) {
11259 dev_info(&pdev->dev,
11260 "Active VFs found, allocating resources.\n");
11261 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11262 if (err)
11263 dev_info(&pdev->dev,
11264 "Error %d allocating resources for existing VFs\n",
11265 err);
11266 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011267 }
Greg Rosedf805f62014-04-04 04:43:16 +000011268#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011269
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011270 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11271 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11272 pf->num_iwarp_msix,
11273 I40E_IWARP_IRQ_PILE_ID);
11274 if (pf->iwarp_base_vector < 0) {
11275 dev_info(&pdev->dev,
11276 "failed to get tracking for %d vectors for IWARP err=%d\n",
11277 pf->num_iwarp_msix, pf->iwarp_base_vector);
11278 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11279 }
11280 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011282 i40e_dbg_pf_init(pf);
11283
11284 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011285 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011286
11287 /* since everything's happy, start the service_task timer */
11288 mod_timer(&pf->service_timer,
11289 round_jiffies(jiffies + pf->service_timer_period));
11290
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011291 /* add this PF to client device list and launch a client service task */
11292 err = i40e_lan_add_device(pf);
11293 if (err)
11294 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11295 err);
11296
Vasu Dev38e00432014-08-01 13:27:03 -070011297#ifdef I40E_FCOE
11298 /* create FCoE interface */
11299 i40e_fcoe_vsi_setup(pf);
11300
11301#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011302#define PCI_SPEED_SIZE 8
11303#define PCI_WIDTH_SIZE 8
11304 /* Devices on the IOSF bus do not have this information
11305 * and will report PCI Gen 1 x 1 by default so don't bother
11306 * checking them.
11307 */
11308 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11309 char speed[PCI_SPEED_SIZE] = "Unknown";
11310 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011311
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011312 /* Get the negotiated link width and speed from PCI config
11313 * space
11314 */
11315 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11316 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011317
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011318 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011319
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011320 switch (hw->bus.speed) {
11321 case i40e_bus_speed_8000:
11322 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11323 case i40e_bus_speed_5000:
11324 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11325 case i40e_bus_speed_2500:
11326 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11327 default:
11328 break;
11329 }
11330 switch (hw->bus.width) {
11331 case i40e_bus_width_pcie_x8:
11332 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11333 case i40e_bus_width_pcie_x4:
11334 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11335 case i40e_bus_width_pcie_x2:
11336 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11337 case i40e_bus_width_pcie_x1:
11338 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11339 default:
11340 break;
11341 }
11342
11343 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11344 speed, width);
11345
11346 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11347 hw->bus.speed < i40e_bus_speed_8000) {
11348 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11349 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11350 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011351 }
11352
Catherine Sullivane8278452015-02-06 08:52:08 +000011353 /* get the requested speeds from the fw */
11354 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11355 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011356 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11357 i40e_stat_str(&pf->hw, err),
11358 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011359 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11360
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011361 /* get the supported phy types from the fw */
11362 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11363 if (err)
11364 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11365 i40e_stat_str(&pf->hw, err),
11366 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011367
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011368 /* Add a filter to drop all Flow control frames from any VSI from being
11369 * transmitted. By doing so we stop a malicious VF from sending out
11370 * PAUSE or PFC frames and potentially controlling traffic for other
11371 * PF/VF VSIs.
11372 * The FW can still send Flow control frames if enabled.
11373 */
11374 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11375 pf->main_vsi_seid);
11376
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011377 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011378 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11379 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011380 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11381 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011382 /* print a string summarizing features */
11383 i40e_print_features(pf);
11384
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011385 return 0;
11386
11387 /* Unwind what we've done if something failed in the setup */
11388err_vsis:
11389 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011390 i40e_clear_interrupt_scheme(pf);
11391 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011392err_switch_setup:
11393 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011394 del_timer_sync(&pf->service_timer);
11395err_mac_addr:
11396err_configure_lan_hmc:
11397 (void)i40e_shutdown_lan_hmc(hw);
11398err_init_lan_hmc:
11399 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011400err_sw_init:
11401err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011402err_pf_reset:
11403 iounmap(hw->hw_addr);
11404err_ioremap:
11405 kfree(pf);
11406err_pf_alloc:
11407 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011408 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011409err_pci_reg:
11410err_dma:
11411 pci_disable_device(pdev);
11412 return err;
11413}
11414
11415/**
11416 * i40e_remove - Device removal routine
11417 * @pdev: PCI device information struct
11418 *
11419 * i40e_remove is called by the PCI subsystem to alert the driver
11420 * that is should release a PCI device. This could be caused by a
11421 * Hot-Plug event, or because the driver is going to be removed from
11422 * memory.
11423 **/
11424static void i40e_remove(struct pci_dev *pdev)
11425{
11426 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011427 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011428 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011429 int i;
11430
11431 i40e_dbg_pf_exit(pf);
11432
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011433 i40e_ptp_stop(pf);
11434
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011435 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011436 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11437 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011438
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011439 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011440 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011441 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011442 if (pf->service_timer.data)
11443 del_timer_sync(&pf->service_timer);
11444 if (pf->service_task.func)
11445 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011447 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11448 i40e_free_vfs(pf);
11449 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11450 }
11451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011452 i40e_fdir_teardown(pf);
11453
11454 /* If there is a switch structure or any orphans, remove them.
11455 * This will leave only the PF's VSI remaining.
11456 */
11457 for (i = 0; i < I40E_MAX_VEB; i++) {
11458 if (!pf->veb[i])
11459 continue;
11460
11461 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11462 pf->veb[i]->uplink_seid == 0)
11463 i40e_switch_branch_release(pf->veb[i]);
11464 }
11465
11466 /* Now we can shutdown the PF's VSI, just before we kill
11467 * adminq and hmc.
11468 */
11469 if (pf->vsi[pf->lan_vsi])
11470 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11471
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011472 /* remove attached clients */
11473 ret_code = i40e_lan_del_device(pf);
11474 if (ret_code) {
11475 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11476 ret_code);
11477 }
11478
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011479 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011480 if (hw->hmc.hmc_obj) {
11481 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011482 if (ret_code)
11483 dev_warn(&pdev->dev,
11484 "Failed to destroy the HMC resources: %d\n",
11485 ret_code);
11486 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011487
11488 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011489 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011490
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011491 /* destroy the locks only once, here */
11492 mutex_destroy(&hw->aq.arq_mutex);
11493 mutex_destroy(&hw->aq.asq_mutex);
11494
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011495 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11496 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011497 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011498 if (pf->vsi[i]) {
11499 i40e_vsi_clear_rings(pf->vsi[i]);
11500 i40e_vsi_clear(pf->vsi[i]);
11501 pf->vsi[i] = NULL;
11502 }
11503 }
11504
11505 for (i = 0; i < I40E_MAX_VEB; i++) {
11506 kfree(pf->veb[i]);
11507 pf->veb[i] = NULL;
11508 }
11509
11510 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011511 kfree(pf->vsi);
11512
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011513 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011514 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011515 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011516
11517 pci_disable_pcie_error_reporting(pdev);
11518 pci_disable_device(pdev);
11519}
11520
11521/**
11522 * i40e_pci_error_detected - warning that something funky happened in PCI land
11523 * @pdev: PCI device information struct
11524 *
11525 * Called to warn that something happened and the error handling steps
11526 * are in progress. Allows the driver to quiesce things, be ready for
11527 * remediation.
11528 **/
11529static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11530 enum pci_channel_state error)
11531{
11532 struct i40e_pf *pf = pci_get_drvdata(pdev);
11533
11534 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11535
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011536 if (!pf) {
11537 dev_info(&pdev->dev,
11538 "Cannot recover - error happened during device probe\n");
11539 return PCI_ERS_RESULT_DISCONNECT;
11540 }
11541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011542 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011543 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11544 rtnl_lock();
11545 i40e_prep_for_reset(pf);
11546 rtnl_unlock();
11547 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011548
11549 /* Request a slot reset */
11550 return PCI_ERS_RESULT_NEED_RESET;
11551}
11552
11553/**
11554 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11555 * @pdev: PCI device information struct
11556 *
11557 * Called to find if the driver can work with the device now that
11558 * the pci slot has been reset. If a basic connection seems good
11559 * (registers are readable and have sane content) then return a
11560 * happy little PCI_ERS_RESULT_xxx.
11561 **/
11562static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11563{
11564 struct i40e_pf *pf = pci_get_drvdata(pdev);
11565 pci_ers_result_t result;
11566 int err;
11567 u32 reg;
11568
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011569 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011570 if (pci_enable_device_mem(pdev)) {
11571 dev_info(&pdev->dev,
11572 "Cannot re-enable PCI device after reset.\n");
11573 result = PCI_ERS_RESULT_DISCONNECT;
11574 } else {
11575 pci_set_master(pdev);
11576 pci_restore_state(pdev);
11577 pci_save_state(pdev);
11578 pci_wake_from_d3(pdev, false);
11579
11580 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11581 if (reg == 0)
11582 result = PCI_ERS_RESULT_RECOVERED;
11583 else
11584 result = PCI_ERS_RESULT_DISCONNECT;
11585 }
11586
11587 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11588 if (err) {
11589 dev_info(&pdev->dev,
11590 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11591 err);
11592 /* non-fatal, continue */
11593 }
11594
11595 return result;
11596}
11597
11598/**
11599 * i40e_pci_error_resume - restart operations after PCI error recovery
11600 * @pdev: PCI device information struct
11601 *
11602 * Called to allow the driver to bring things back up after PCI error
11603 * and/or reset recovery has finished.
11604 **/
11605static void i40e_pci_error_resume(struct pci_dev *pdev)
11606{
11607 struct i40e_pf *pf = pci_get_drvdata(pdev);
11608
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011609 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011610 if (test_bit(__I40E_SUSPENDED, &pf->state))
11611 return;
11612
11613 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011614 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011615 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011616}
11617
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011618/**
11619 * i40e_shutdown - PCI callback for shutting down
11620 * @pdev: PCI device information struct
11621 **/
11622static void i40e_shutdown(struct pci_dev *pdev)
11623{
11624 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011625 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011626
11627 set_bit(__I40E_SUSPENDED, &pf->state);
11628 set_bit(__I40E_DOWN, &pf->state);
11629 rtnl_lock();
11630 i40e_prep_for_reset(pf);
11631 rtnl_unlock();
11632
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011633 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11634 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11635
Catherine Sullivan02b42492015-07-10 19:35:59 -040011636 del_timer_sync(&pf->service_timer);
11637 cancel_work_sync(&pf->service_task);
11638 i40e_fdir_teardown(pf);
11639
11640 rtnl_lock();
11641 i40e_prep_for_reset(pf);
11642 rtnl_unlock();
11643
11644 wr32(hw, I40E_PFPM_APM,
11645 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11646 wr32(hw, I40E_PFPM_WUFC,
11647 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11648
Shannon Nelsone1477582015-02-21 06:44:33 +000011649 i40e_clear_interrupt_scheme(pf);
11650
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011651 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011652 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011653 pci_set_power_state(pdev, PCI_D3hot);
11654 }
11655}
11656
11657#ifdef CONFIG_PM
11658/**
11659 * i40e_suspend - PCI callback for moving to D3
11660 * @pdev: PCI device information struct
11661 **/
11662static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11663{
11664 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011665 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011666 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011667
11668 set_bit(__I40E_SUSPENDED, &pf->state);
11669 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011670
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011671 rtnl_lock();
11672 i40e_prep_for_reset(pf);
11673 rtnl_unlock();
11674
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011675 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11676 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11677
Greg Roseb33d3b72016-05-16 10:26:37 -070011678 i40e_stop_misc_vector(pf);
11679
Greg Rose059ff692016-05-16 10:26:38 -070011680 retval = pci_save_state(pdev);
11681 if (retval)
11682 return retval;
11683
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011684 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011685 pci_set_power_state(pdev, PCI_D3hot);
11686
Greg Rose059ff692016-05-16 10:26:38 -070011687 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011688}
11689
11690/**
11691 * i40e_resume - PCI callback for waking up from D3
11692 * @pdev: PCI device information struct
11693 **/
11694static int i40e_resume(struct pci_dev *pdev)
11695{
11696 struct i40e_pf *pf = pci_get_drvdata(pdev);
11697 u32 err;
11698
11699 pci_set_power_state(pdev, PCI_D0);
11700 pci_restore_state(pdev);
11701 /* pci_restore_state() clears dev->state_saves, so
11702 * call pci_save_state() again to restore it.
11703 */
11704 pci_save_state(pdev);
11705
11706 err = pci_enable_device_mem(pdev);
11707 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011708 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011709 return err;
11710 }
11711 pci_set_master(pdev);
11712
11713 /* no wakeup events while running */
11714 pci_wake_from_d3(pdev, false);
11715
11716 /* handling the reset will rebuild the device state */
11717 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11718 clear_bit(__I40E_DOWN, &pf->state);
11719 rtnl_lock();
11720 i40e_reset_and_rebuild(pf, false);
11721 rtnl_unlock();
11722 }
11723
11724 return 0;
11725}
11726
11727#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011728static const struct pci_error_handlers i40e_err_handler = {
11729 .error_detected = i40e_pci_error_detected,
11730 .slot_reset = i40e_pci_error_slot_reset,
11731 .resume = i40e_pci_error_resume,
11732};
11733
11734static struct pci_driver i40e_driver = {
11735 .name = i40e_driver_name,
11736 .id_table = i40e_pci_tbl,
11737 .probe = i40e_probe,
11738 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011739#ifdef CONFIG_PM
11740 .suspend = i40e_suspend,
11741 .resume = i40e_resume,
11742#endif
11743 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011744 .err_handler = &i40e_err_handler,
11745 .sriov_configure = i40e_pci_sriov_configure,
11746};
11747
11748/**
11749 * i40e_init_module - Driver registration routine
11750 *
11751 * i40e_init_module is the first routine called when the driver is
11752 * loaded. All it does is register with the PCI subsystem.
11753 **/
11754static int __init i40e_init_module(void)
11755{
11756 pr_info("%s: %s - version %s\n", i40e_driver_name,
11757 i40e_driver_string, i40e_driver_version_str);
11758 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011759
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011760 /* we will see if single thread per module is enough for now,
11761 * it can't be any worse than using the system workqueue which
11762 * was already single threaded
11763 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011764 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11765 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011766 if (!i40e_wq) {
11767 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11768 return -ENOMEM;
11769 }
11770
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011771 i40e_dbg_init();
11772 return pci_register_driver(&i40e_driver);
11773}
11774module_init(i40e_init_module);
11775
11776/**
11777 * i40e_exit_module - Driver exit cleanup routine
11778 *
11779 * i40e_exit_module is called just before the driver is removed
11780 * from memory.
11781 **/
11782static void __exit i40e_exit_module(void)
11783{
11784 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011785 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011786 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011787}
11788module_exit(i40e_exit_module);