blob: 0b4adccd26117466de3f65a6074438e52317377f [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070034#include <net/udp_tunnel.h>
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000035
36const char i40e_driver_name[] = "i40e";
37static const char i40e_driver_string[] =
38 "Intel(R) Ethernet Connection XL710 Network Driver";
39
40#define DRV_KERN "-k"
41
Catherine Sullivane8e724d2014-07-10 07:58:26 +000042#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070043#define DRV_VERSION_MINOR 6
Bimmy Pujari36023862016-11-08 13:05:19 -080044#define DRV_VERSION_BUILD 25
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000045#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080049static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000056static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080060static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080061static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020070static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080071 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080072 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080073 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
88 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000091 /* required last entry */
92 {0, }
93};
94MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95
96#define I40E_MAX_VF_COUNT 128
97static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040098module_param(debug, uint, 0);
99MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100
101MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103MODULE_LICENSE("GPL");
104MODULE_VERSION(DRV_VERSION);
105
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800106static struct workqueue_struct *i40e_wq;
107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000108/**
109 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
110 * @hw: pointer to the HW structure
111 * @mem: ptr to mem struct to fill out
112 * @size: size of memory requested
113 * @alignment: what to align the allocation to
114 **/
115int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
116 u64 size, u32 alignment)
117{
118 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
119
120 mem->size = ALIGN(size, alignment);
121 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
122 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000123 if (!mem->va)
124 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000125
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000126 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000127}
128
129/**
130 * i40e_free_dma_mem_d - OS specific memory free for shared code
131 * @hw: pointer to the HW structure
132 * @mem: ptr to mem struct to free
133 **/
134int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
135{
136 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137
138 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
139 mem->va = NULL;
140 mem->pa = 0;
141 mem->size = 0;
142
143 return 0;
144}
145
146/**
147 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to fill out
150 * @size: size of memory requested
151 **/
152int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
153 u32 size)
154{
155 mem->size = size;
156 mem->va = kzalloc(size, GFP_KERNEL);
157
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000158 if (!mem->va)
159 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000160
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000161 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000162}
163
164/**
165 * i40e_free_virt_mem_d - OS specific memory free for shared code
166 * @hw: pointer to the HW structure
167 * @mem: ptr to mem struct to free
168 **/
169int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
170{
171 /* it's ok to kfree a NULL pointer */
172 kfree(mem->va);
173 mem->va = NULL;
174 mem->size = 0;
175
176 return 0;
177}
178
179/**
180 * i40e_get_lump - find a lump of free generic resource
181 * @pf: board private structure
182 * @pile: the pile of resource to search
183 * @needed: the number of items needed
184 * @id: an owner id to stick on the items assigned
185 *
186 * Returns the base item index of the lump, or negative for error
187 *
188 * The search_hint trick and lack of advanced fit-finding only work
189 * because we're highly likely to have all the same size lump requests.
190 * Linear search time and any fragmentation should be minimal.
191 **/
192static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
193 u16 needed, u16 id)
194{
195 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000196 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000197
198 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
199 dev_info(&pf->pdev->dev,
200 "param err: pile=%p needed=%d id=0x%04x\n",
201 pile, needed, id);
202 return -EINVAL;
203 }
204
205 /* start the linear search with an imperfect hint */
206 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000207 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000208 /* skip already allocated entries */
209 if (pile->list[i] & I40E_PILE_VALID_BIT) {
210 i++;
211 continue;
212 }
213
214 /* do we have enough in this lump? */
215 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
216 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
217 break;
218 }
219
220 if (j == needed) {
221 /* there was enough, so assign it to the requestor */
222 for (j = 0; j < needed; j++)
223 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
224 ret = i;
225 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000226 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000227 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400228
229 /* not enough, so skip over it and continue looking */
230 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000231 }
232
233 return ret;
234}
235
236/**
237 * i40e_put_lump - return a lump of generic resource
238 * @pile: the pile of resource to search
239 * @index: the base item index
240 * @id: the owner id of the items assigned
241 *
242 * Returns the count of items in the lump
243 **/
244static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
245{
246 int valid_id = (id | I40E_PILE_VALID_BIT);
247 int count = 0;
248 int i;
249
250 if (!pile || index >= pile->num_entries)
251 return -EINVAL;
252
253 for (i = index;
254 i < pile->num_entries && pile->list[i] == valid_id;
255 i++) {
256 pile->list[i] = 0;
257 count++;
258 }
259
260 if (count && index < pile->search_hint)
261 pile->search_hint = index;
262
263 return count;
264}
265
266/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700267 * i40e_find_vsi_from_id - searches for the vsi with the given id
268 * @pf - the pf structure to search for the vsi
269 * @id - id of the vsi it is searching for
270 **/
271struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
272{
273 int i;
274
275 for (i = 0; i < pf->num_alloc_vsi; i++)
276 if (pf->vsi[i] && (pf->vsi[i]->id == id))
277 return pf->vsi[i];
278
279 return NULL;
280}
281
282/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000283 * i40e_service_event_schedule - Schedule the service task to wake up
284 * @pf: board private structure
285 *
286 * If not already scheduled, this puts the task into the work queue
287 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600288void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000289{
290 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800291 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800292 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000293}
294
295/**
296 * i40e_tx_timeout - Respond to a Tx Hang
297 * @netdev: network interface device structure
298 *
299 * If any port has noticed a Tx timeout, it is likely that the whole
300 * device is munged, not just the one netdev port, so go for the full
301 * reset.
302 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700303#ifdef I40E_FCOE
304void i40e_tx_timeout(struct net_device *netdev)
305#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000306static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700307#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000308{
309 struct i40e_netdev_priv *np = netdev_priv(netdev);
310 struct i40e_vsi *vsi = np->vsi;
311 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400312 struct i40e_ring *tx_ring = NULL;
313 unsigned int i, hung_queue = 0;
314 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000315
316 pf->tx_timeout_count++;
317
Kiran Patilb03a8c12015-09-24 18:13:15 -0400318 /* find the stopped queue the same way the stack does */
319 for (i = 0; i < netdev->num_tx_queues; i++) {
320 struct netdev_queue *q;
321 unsigned long trans_start;
322
323 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200324 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400325 if (netif_xmit_stopped(q) &&
326 time_after(jiffies,
327 (trans_start + netdev->watchdog_timeo))) {
328 hung_queue = i;
329 break;
330 }
331 }
332
333 if (i == netdev->num_tx_queues) {
334 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
335 } else {
336 /* now that we have an index, find the tx_ring struct */
337 for (i = 0; i < vsi->num_queue_pairs; i++) {
338 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 if (hung_queue ==
340 vsi->tx_rings[i]->queue_index) {
341 tx_ring = vsi->tx_rings[i];
342 break;
343 }
344 }
345 }
346 }
347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000348 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400349 pf->tx_timeout_recovery_level = 1; /* reset after some time */
350 else if (time_before(jiffies,
351 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
352 return; /* don't do any new action before the next timeout */
353
354 if (tx_ring) {
355 head = i40e_get_head(tx_ring);
356 /* Read interrupt register */
357 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 val = rd32(&pf->hw,
359 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 tx_ring->vsi->base_vector - 1));
361 else
362 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363
364 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
365 vsi->seid, hung_queue, tx_ring->next_to_clean,
366 head, tx_ring->next_to_use,
367 readl(tx_ring->tail), val);
368 }
369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000370 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400371 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000373
374 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 case 1:
376 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
377 break;
378 case 2:
379 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
380 break;
381 case 3:
382 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
383 break;
384 default:
385 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000386 break;
387 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389 i40e_service_event_schedule(pf);
390 pf->tx_timeout_recovery_level++;
391}
392
393/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 * i40e_get_vsi_stats_struct - Get System Network Statistics
395 * @vsi: the VSI we care about
396 *
397 * Returns the address of the device statistics structure.
398 * The statistics are actually updated from the service task.
399 **/
400struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401{
402 return &vsi->net_stats;
403}
404
405/**
406 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
407 * @netdev: network interface device structure
408 *
409 * Returns the address of the device statistics structure.
410 * The statistics are actually updated from the service task.
411 **/
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800412#ifndef I40E_FCOE
413static
Vasu Dev38e00432014-08-01 13:27:03 -0700414#endif
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800415void i40e_get_netdev_stats_struct(struct net_device *netdev,
416 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000417{
418 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000419 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000420 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000421 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
422 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000423
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000424 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800425 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000426
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800427 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800428 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800429
Alexander Duyck980e9b12013-09-28 06:01:03 +0000430 rcu_read_lock();
431 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000432 u64 bytes, packets;
433 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000434
Alexander Duyck980e9b12013-09-28 06:01:03 +0000435 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
436 if (!tx_ring)
437 continue;
438
439 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700440 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000441 packets = tx_ring->stats.packets;
442 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700443 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444
445 stats->tx_packets += packets;
446 stats->tx_bytes += bytes;
447 rx_ring = &tx_ring[1];
448
449 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700450 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000451 packets = rx_ring->stats.packets;
452 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700453 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000454
455 stats->rx_packets += packets;
456 stats->rx_bytes += bytes;
457 }
458 rcu_read_unlock();
459
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000460 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000461 stats->multicast = vsi_stats->multicast;
462 stats->tx_errors = vsi_stats->tx_errors;
463 stats->tx_dropped = vsi_stats->tx_dropped;
464 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400465 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000466 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
467 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000468}
469
470/**
471 * i40e_vsi_reset_stats - Resets all stats of the given vsi
472 * @vsi: the VSI to have its stats reset
473 **/
474void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
475{
476 struct rtnl_link_stats64 *ns;
477 int i;
478
479 if (!vsi)
480 return;
481
482 ns = i40e_get_vsi_stats_struct(vsi);
483 memset(ns, 0, sizeof(*ns));
484 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
485 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
486 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000487 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000488 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400489 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000490 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400491 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000492 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400493 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000494 sizeof(vsi->tx_rings[i]->stats));
495 memset(&vsi->tx_rings[i]->tx_stats, 0,
496 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000497 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000498 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000499 vsi->stat_offsets_loaded = false;
500}
501
502/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000503 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000504 * @pf: the PF to be reset
505 **/
506void i40e_pf_reset_stats(struct i40e_pf *pf)
507{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000508 int i;
509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000510 memset(&pf->stats, 0, sizeof(pf->stats));
511 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
512 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000513
514 for (i = 0; i < I40E_MAX_VEB; i++) {
515 if (pf->veb[i]) {
516 memset(&pf->veb[i]->stats, 0,
517 sizeof(pf->veb[i]->stats));
518 memset(&pf->veb[i]->stats_offsets, 0,
519 sizeof(pf->veb[i]->stats_offsets));
520 pf->veb[i]->stat_offsets_loaded = false;
521 }
522 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700523 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000524}
525
526/**
527 * i40e_stat_update48 - read and update a 48 bit stat from the chip
528 * @hw: ptr to the hardware info
529 * @hireg: the high 32 bit reg to read
530 * @loreg: the low 32 bit reg to read
531 * @offset_loaded: has the initial offset been loaded yet
532 * @offset: ptr to current offset value
533 * @stat: ptr to the stat
534 *
535 * Since the device stats are not reset at PFReset, they likely will not
536 * be zeroed when the driver starts. We'll save the first values read
537 * and use them as offsets to be subtracted from the raw values in order
538 * to report stats that count from zero. In the process, we also manage
539 * the potential roll-over.
540 **/
541static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
542 bool offset_loaded, u64 *offset, u64 *stat)
543{
544 u64 new_data;
545
Shannon Nelsonab600852014-01-17 15:36:39 -0800546 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000547 new_data = rd32(hw, loreg);
548 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
549 } else {
550 new_data = rd64(hw, loreg);
551 }
552 if (!offset_loaded)
553 *offset = new_data;
554 if (likely(new_data >= *offset))
555 *stat = new_data - *offset;
556 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400557 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000558 *stat &= 0xFFFFFFFFFFFFULL;
559}
560
561/**
562 * i40e_stat_update32 - read and update a 32 bit stat from the chip
563 * @hw: ptr to the hardware info
564 * @reg: the hw reg to read
565 * @offset_loaded: has the initial offset been loaded yet
566 * @offset: ptr to current offset value
567 * @stat: ptr to the stat
568 **/
569static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
570 bool offset_loaded, u64 *offset, u64 *stat)
571{
572 u32 new_data;
573
574 new_data = rd32(hw, reg);
575 if (!offset_loaded)
576 *offset = new_data;
577 if (likely(new_data >= *offset))
578 *stat = (u32)(new_data - *offset);
579 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400580 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000581}
582
583/**
584 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
585 * @vsi: the VSI to be updated
586 **/
587void i40e_update_eth_stats(struct i40e_vsi *vsi)
588{
589 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
590 struct i40e_pf *pf = vsi->back;
591 struct i40e_hw *hw = &pf->hw;
592 struct i40e_eth_stats *oes;
593 struct i40e_eth_stats *es; /* device's eth stats */
594
595 es = &vsi->eth_stats;
596 oes = &vsi->eth_stats_offsets;
597
598 /* Gather up the stats that the hw collects */
599 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
600 vsi->stat_offsets_loaded,
601 &oes->tx_errors, &es->tx_errors);
602 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
603 vsi->stat_offsets_loaded,
604 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000605 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
606 vsi->stat_offsets_loaded,
607 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
608 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000611
612 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
613 I40E_GLV_GORCL(stat_idx),
614 vsi->stat_offsets_loaded,
615 &oes->rx_bytes, &es->rx_bytes);
616 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
617 I40E_GLV_UPRCL(stat_idx),
618 vsi->stat_offsets_loaded,
619 &oes->rx_unicast, &es->rx_unicast);
620 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
621 I40E_GLV_MPRCL(stat_idx),
622 vsi->stat_offsets_loaded,
623 &oes->rx_multicast, &es->rx_multicast);
624 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
625 I40E_GLV_BPRCL(stat_idx),
626 vsi->stat_offsets_loaded,
627 &oes->rx_broadcast, &es->rx_broadcast);
628
629 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
630 I40E_GLV_GOTCL(stat_idx),
631 vsi->stat_offsets_loaded,
632 &oes->tx_bytes, &es->tx_bytes);
633 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
634 I40E_GLV_UPTCL(stat_idx),
635 vsi->stat_offsets_loaded,
636 &oes->tx_unicast, &es->tx_unicast);
637 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
638 I40E_GLV_MPTCL(stat_idx),
639 vsi->stat_offsets_loaded,
640 &oes->tx_multicast, &es->tx_multicast);
641 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
642 I40E_GLV_BPTCL(stat_idx),
643 vsi->stat_offsets_loaded,
644 &oes->tx_broadcast, &es->tx_broadcast);
645 vsi->stat_offsets_loaded = true;
646}
647
648/**
649 * i40e_update_veb_stats - Update Switch component statistics
650 * @veb: the VEB being updated
651 **/
652static void i40e_update_veb_stats(struct i40e_veb *veb)
653{
654 struct i40e_pf *pf = veb->pf;
655 struct i40e_hw *hw = &pf->hw;
656 struct i40e_eth_stats *oes;
657 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400658 struct i40e_veb_tc_stats *veb_oes;
659 struct i40e_veb_tc_stats *veb_es;
660 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000661
662 idx = veb->stats_idx;
663 es = &veb->stats;
664 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400665 veb_es = &veb->tc_stats;
666 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000667
668 /* Gather up the stats that the hw collects */
669 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
670 veb->stat_offsets_loaded,
671 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000672 if (hw->revision_id > 0)
673 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
674 veb->stat_offsets_loaded,
675 &oes->rx_unknown_protocol,
676 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000677 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
678 veb->stat_offsets_loaded,
679 &oes->rx_bytes, &es->rx_bytes);
680 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
681 veb->stat_offsets_loaded,
682 &oes->rx_unicast, &es->rx_unicast);
683 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
684 veb->stat_offsets_loaded,
685 &oes->rx_multicast, &es->rx_multicast);
686 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
687 veb->stat_offsets_loaded,
688 &oes->rx_broadcast, &es->rx_broadcast);
689
690 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
691 veb->stat_offsets_loaded,
692 &oes->tx_bytes, &es->tx_bytes);
693 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
694 veb->stat_offsets_loaded,
695 &oes->tx_unicast, &es->tx_unicast);
696 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->tx_multicast, &es->tx_multicast);
699 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400702 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
703 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
704 I40E_GLVEBTC_RPCL(i, idx),
705 veb->stat_offsets_loaded,
706 &veb_oes->tc_rx_packets[i],
707 &veb_es->tc_rx_packets[i]);
708 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
709 I40E_GLVEBTC_RBCL(i, idx),
710 veb->stat_offsets_loaded,
711 &veb_oes->tc_rx_bytes[i],
712 &veb_es->tc_rx_bytes[i]);
713 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
714 I40E_GLVEBTC_TPCL(i, idx),
715 veb->stat_offsets_loaded,
716 &veb_oes->tc_tx_packets[i],
717 &veb_es->tc_tx_packets[i]);
718 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
719 I40E_GLVEBTC_TBCL(i, idx),
720 veb->stat_offsets_loaded,
721 &veb_oes->tc_tx_bytes[i],
722 &veb_es->tc_tx_bytes[i]);
723 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000724 veb->stat_offsets_loaded = true;
725}
726
Vasu Dev38e00432014-08-01 13:27:03 -0700727#ifdef I40E_FCOE
728/**
729 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
730 * @vsi: the VSI that is capable of doing FCoE
731 **/
732static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
733{
734 struct i40e_pf *pf = vsi->back;
735 struct i40e_hw *hw = &pf->hw;
736 struct i40e_fcoe_stats *ofs;
737 struct i40e_fcoe_stats *fs; /* device's eth stats */
738 int idx;
739
740 if (vsi->type != I40E_VSI_FCOE)
741 return;
742
Kiran Patil4147e2c2016-01-15 14:33:14 -0800743 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700744 fs = &vsi->fcoe_stats;
745 ofs = &vsi->fcoe_stats_offsets;
746
747 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
748 vsi->fcoe_stat_offsets_loaded,
749 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
750 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
751 vsi->fcoe_stat_offsets_loaded,
752 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
753 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
754 vsi->fcoe_stat_offsets_loaded,
755 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
756 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
757 vsi->fcoe_stat_offsets_loaded,
758 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
759 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
760 vsi->fcoe_stat_offsets_loaded,
761 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
762 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
763 vsi->fcoe_stat_offsets_loaded,
764 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
765 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->fcoe_last_error, &fs->fcoe_last_error);
768 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
771
772 vsi->fcoe_stat_offsets_loaded = true;
773}
774
775#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000776/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000777 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000778 * @vsi: the VSI to be updated
779 *
780 * There are a few instances where we store the same stat in a
781 * couple of different structs. This is partly because we have
782 * the netdev stats that need to be filled out, which is slightly
783 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000784 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000785 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000786static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000787{
788 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000789 struct rtnl_link_stats64 *ons;
790 struct rtnl_link_stats64 *ns; /* netdev stats */
791 struct i40e_eth_stats *oes;
792 struct i40e_eth_stats *es; /* device's eth stats */
793 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800794 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000795 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000796 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000797 u64 bytes, packets;
798 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400799 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400800 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 u64 rx_p, rx_b;
802 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000803 u16 q;
804
805 if (test_bit(__I40E_DOWN, &vsi->state) ||
806 test_bit(__I40E_CONFIG_BUSY, &pf->state))
807 return;
808
809 ns = i40e_get_vsi_stats_struct(vsi);
810 ons = &vsi->net_stats_offsets;
811 es = &vsi->eth_stats;
812 oes = &vsi->eth_stats_offsets;
813
814 /* Gather up the netdev and vsi stats that the driver collects
815 * on the fly during packet processing
816 */
817 rx_b = rx_p = 0;
818 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400819 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800820 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000821 rx_page = 0;
822 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000823 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000824 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000825 /* locate Tx ring */
826 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827
Alexander Duyck980e9b12013-09-28 06:01:03 +0000828 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700829 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000830 packets = p->stats.packets;
831 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700832 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000833 tx_b += bytes;
834 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000835 tx_restart += p->tx_stats.restart_queue;
836 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400837 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400838 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800839 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000840
841 /* Rx queue is part of the same block as Tx queue */
842 p = &p[1];
843 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700844 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000845 packets = p->stats.packets;
846 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700847 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000848 rx_b += bytes;
849 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000850 rx_buf += p->rx_stats.alloc_buff_failed;
851 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000852 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000853 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000854 vsi->tx_restart = tx_restart;
855 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400856 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400857 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800858 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 vsi->rx_page_failed = rx_page;
860 vsi->rx_buf_failed = rx_buf;
861
862 ns->rx_packets = rx_p;
863 ns->rx_bytes = rx_b;
864 ns->tx_packets = tx_p;
865 ns->tx_bytes = tx_b;
866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000867 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000868 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000869 ons->tx_errors = oes->tx_errors;
870 ns->tx_errors = es->tx_errors;
871 ons->multicast = oes->rx_multicast;
872 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000873 ons->rx_dropped = oes->rx_discards;
874 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000875 ons->tx_dropped = oes->tx_discards;
876 ns->tx_dropped = es->tx_discards;
877
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000878 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000879 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000880 ns->rx_crc_errors = pf->stats.crc_errors;
881 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
882 ns->rx_length_errors = pf->stats.rx_length_errors;
883 }
884}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000885
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000886/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000887 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000888 * @pf: the PF to be updated
889 **/
890static void i40e_update_pf_stats(struct i40e_pf *pf)
891{
892 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
893 struct i40e_hw_port_stats *nsd = &pf->stats;
894 struct i40e_hw *hw = &pf->hw;
895 u32 val;
896 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000897
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000898 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
899 I40E_GLPRT_GORCL(hw->port),
900 pf->stat_offsets_loaded,
901 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
902 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
903 I40E_GLPRT_GOTCL(hw->port),
904 pf->stat_offsets_loaded,
905 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
906 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->eth.rx_discards,
909 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000910 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
911 I40E_GLPRT_UPRCL(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.rx_unicast,
914 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000915 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
916 I40E_GLPRT_MPRCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.rx_multicast,
919 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000920 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
921 I40E_GLPRT_BPRCL(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->eth.rx_broadcast,
924 &nsd->eth.rx_broadcast);
925 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
926 I40E_GLPRT_UPTCL(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->eth.tx_unicast,
929 &nsd->eth.tx_unicast);
930 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
931 I40E_GLPRT_MPTCL(hw->port),
932 pf->stat_offsets_loaded,
933 &osd->eth.tx_multicast,
934 &nsd->eth.tx_multicast);
935 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
936 I40E_GLPRT_BPTCL(hw->port),
937 pf->stat_offsets_loaded,
938 &osd->eth.tx_broadcast,
939 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000940
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000941 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->tx_dropped_link_down,
944 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000945
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000946 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000949
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000950 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000953
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000954 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
955 pf->stat_offsets_loaded,
956 &osd->mac_local_faults,
957 &nsd->mac_local_faults);
958 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->mac_remote_faults,
961 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000962
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000963 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->rx_length_errors,
966 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
969 pf->stat_offsets_loaded,
970 &osd->link_xon_rx, &nsd->link_xon_rx);
971 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
972 pf->stat_offsets_loaded,
973 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800974 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000977 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000980
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000981 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800982 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
983 pf->stat_offsets_loaded,
984 &osd->priority_xoff_rx[i],
985 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000986 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000987 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000988 &osd->priority_xon_rx[i],
989 &nsd->priority_xon_rx[i]);
990 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000991 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 &osd->priority_xon_tx[i],
993 &nsd->priority_xon_tx[i]);
994 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000995 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000996 &osd->priority_xoff_tx[i],
997 &nsd->priority_xoff_tx[i]);
998 i40e_stat_update32(hw,
999 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001000 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001001 &osd->priority_xon_2_xoff[i],
1002 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001003 }
1004
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001005 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1006 I40E_GLPRT_PRC64L(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_size_64, &nsd->rx_size_64);
1009 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1010 I40E_GLPRT_PRC127L(hw->port),
1011 pf->stat_offsets_loaded,
1012 &osd->rx_size_127, &nsd->rx_size_127);
1013 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1014 I40E_GLPRT_PRC255L(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->rx_size_255, &nsd->rx_size_255);
1017 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1018 I40E_GLPRT_PRC511L(hw->port),
1019 pf->stat_offsets_loaded,
1020 &osd->rx_size_511, &nsd->rx_size_511);
1021 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1022 I40E_GLPRT_PRC1023L(hw->port),
1023 pf->stat_offsets_loaded,
1024 &osd->rx_size_1023, &nsd->rx_size_1023);
1025 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1026 I40E_GLPRT_PRC1522L(hw->port),
1027 pf->stat_offsets_loaded,
1028 &osd->rx_size_1522, &nsd->rx_size_1522);
1029 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1030 I40E_GLPRT_PRC9522L(hw->port),
1031 pf->stat_offsets_loaded,
1032 &osd->rx_size_big, &nsd->rx_size_big);
1033
1034 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1035 I40E_GLPRT_PTC64L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->tx_size_64, &nsd->tx_size_64);
1038 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1039 I40E_GLPRT_PTC127L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->tx_size_127, &nsd->tx_size_127);
1042 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1043 I40E_GLPRT_PTC255L(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->tx_size_255, &nsd->tx_size_255);
1046 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1047 I40E_GLPRT_PTC511L(hw->port),
1048 pf->stat_offsets_loaded,
1049 &osd->tx_size_511, &nsd->tx_size_511);
1050 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1051 I40E_GLPRT_PTC1023L(hw->port),
1052 pf->stat_offsets_loaded,
1053 &osd->tx_size_1023, &nsd->tx_size_1023);
1054 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1055 I40E_GLPRT_PTC1522L(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->tx_size_1522, &nsd->tx_size_1522);
1058 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1059 I40E_GLPRT_PTC9522L(hw->port),
1060 pf->stat_offsets_loaded,
1061 &osd->tx_size_big, &nsd->tx_size_big);
1062
1063 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1064 pf->stat_offsets_loaded,
1065 &osd->rx_undersize, &nsd->rx_undersize);
1066 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1067 pf->stat_offsets_loaded,
1068 &osd->rx_fragments, &nsd->rx_fragments);
1069 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->rx_oversize, &nsd->rx_oversize);
1072 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->rx_jabber, &nsd->rx_jabber);
1075
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001076 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001077 i40e_stat_update32(hw,
1078 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001079 pf->stat_offsets_loaded,
1080 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001081 i40e_stat_update32(hw,
1082 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001083 pf->stat_offsets_loaded,
1084 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001085 i40e_stat_update32(hw,
1086 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1087 pf->stat_offsets_loaded,
1088 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001089
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001090 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1091 nsd->tx_lpi_status =
1092 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1093 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1094 nsd->rx_lpi_status =
1095 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1096 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1097 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1098 pf->stat_offsets_loaded,
1099 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1100 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1101 pf->stat_offsets_loaded,
1102 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1103
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001104 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1105 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1106 nsd->fd_sb_status = true;
1107 else
1108 nsd->fd_sb_status = false;
1109
1110 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1111 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1112 nsd->fd_atr_status = true;
1113 else
1114 nsd->fd_atr_status = false;
1115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001116 pf->stat_offsets_loaded = true;
1117}
1118
1119/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001120 * i40e_update_stats - Update the various statistics counters.
1121 * @vsi: the VSI to be updated
1122 *
1123 * Update the various stats for this VSI and its related entities.
1124 **/
1125void i40e_update_stats(struct i40e_vsi *vsi)
1126{
1127 struct i40e_pf *pf = vsi->back;
1128
1129 if (vsi == pf->vsi[pf->lan_vsi])
1130 i40e_update_pf_stats(pf);
1131
1132 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001133#ifdef I40E_FCOE
1134 i40e_update_fcoe_stats(vsi);
1135#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001136}
1137
1138/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001139 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1140 * @vsi: the VSI to be searched
1141 * @macaddr: the MAC address
1142 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001143 *
1144 * Returns ptr to the filter object or NULL
1145 **/
1146static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001147 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001148{
1149 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001150 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001151
1152 if (!vsi || !macaddr)
1153 return NULL;
1154
Jacob Keller278e7d02016-10-05 09:30:37 -07001155 key = i40e_addr_to_hkey(macaddr);
1156 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001158 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001159 return f;
1160 }
1161 return NULL;
1162}
1163
1164/**
1165 * i40e_find_mac - Find a mac addr in the macvlan filters list
1166 * @vsi: the VSI to be searched
1167 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001168 *
1169 * Returns the first filter with the provided MAC address or NULL if
1170 * MAC address was not found
1171 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001172struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173{
1174 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001175 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001176
1177 if (!vsi || !macaddr)
1178 return NULL;
1179
Jacob Keller278e7d02016-10-05 09:30:37 -07001180 key = i40e_addr_to_hkey(macaddr);
1181 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001182 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001183 return f;
1184 }
1185 return NULL;
1186}
1187
1188/**
1189 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1190 * @vsi: the VSI to be searched
1191 *
1192 * Returns true if VSI is in vlan mode or false otherwise
1193 **/
1194bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1195{
Jacob Kellercbebb852016-10-05 09:30:40 -07001196 /* If we have a PVID, always operate in VLAN mode */
1197 if (vsi->info.pvid)
1198 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001199
Jacob Kellercbebb852016-10-05 09:30:40 -07001200 /* We need to operate in VLAN mode whenever we have any filters with
1201 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1202 * time, incurring search cost repeatedly. However, we can notice two
1203 * things:
1204 *
1205 * 1) the only place where we can gain a VLAN filter is in
1206 * i40e_add_filter.
1207 *
1208 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001209 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001210 *
1211 * Thus, we can simply use a boolean value, has_vlan_filters which we
1212 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1213 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001214 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001215 * filters here and can perform the check at the same time. This
1216 * results in avoiding embedding a loop for VLAN mode inside another
1217 * loop over all the filters, and should maintain correctness as noted
1218 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001219 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001220 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001221}
1222
1223/**
Jacob Keller489a3262016-11-11 12:39:31 -08001224 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1225 * @vsi: the VSI to configure
1226 * @tmp_add_list: list of filters ready to be added
1227 * @tmp_del_list: list of filters ready to be deleted
1228 * @vlan_filters: the number of active VLAN filters
1229 *
1230 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1231 * behave as expected. If we have any active VLAN filters remaining or about
1232 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1233 * so that they only match against untagged traffic. If we no longer have any
1234 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1235 * so that they match against both tagged and untagged traffic. In this way,
1236 * we ensure that we correctly receive the desired traffic. This ensures that
1237 * when we have an active VLAN we will receive only untagged traffic and
1238 * traffic matching active VLANs. If we have no active VLANs then we will
1239 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1240 *
1241 * Finally, in a similar fashion, this function also corrects filters when
1242 * there is an active PVID assigned to this VSI.
1243 *
1244 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1245 *
1246 * This function is only expected to be called from within
1247 * i40e_sync_vsi_filters.
1248 *
1249 * NOTE: This function expects to be called while under the
1250 * mac_filter_hash_lock
1251 */
1252static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1253 struct hlist_head *tmp_add_list,
1254 struct hlist_head *tmp_del_list,
1255 int vlan_filters)
1256{
1257 struct i40e_mac_filter *f, *add_head;
1258 struct hlist_node *h;
1259 int bkt, new_vlan;
1260
1261 /* To determine if a particular filter needs to be replaced we
1262 * have the three following conditions:
1263 *
1264 * a) if we have a PVID assigned, then all filters which are
1265 * not marked as VLAN=PVID must be replaced with filters that
1266 * are.
1267 * b) otherwise, if we have any active VLANS, all filters
1268 * which are marked as VLAN=-1 must be replaced with
1269 * filters marked as VLAN=0
1270 * c) finally, if we do not have any active VLANS, all filters
1271 * which are marked as VLAN=0 must be replaced with filters
1272 * marked as VLAN=-1
1273 */
1274
1275 /* Update the filters about to be added in place */
1276 hlist_for_each_entry(f, tmp_add_list, hlist) {
1277 if (vsi->info.pvid && f->vlan != vsi->info.pvid)
1278 f->vlan = vsi->info.pvid;
1279 else if (vlan_filters && f->vlan == I40E_VLAN_ANY)
1280 f->vlan = 0;
1281 else if (!vlan_filters && f->vlan == 0)
1282 f->vlan = I40E_VLAN_ANY;
1283 }
1284
1285 /* Update the remaining active filters */
1286 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1287 /* Combine the checks for whether a filter needs to be changed
1288 * and then determine the new VLAN inside the if block, in
1289 * order to avoid duplicating code for adding the new filter
1290 * then deleting the old filter.
1291 */
1292 if ((vsi->info.pvid && f->vlan != vsi->info.pvid) ||
1293 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1294 (!vlan_filters && f->vlan == 0)) {
1295 /* Determine the new vlan we will be adding */
1296 if (vsi->info.pvid)
1297 new_vlan = vsi->info.pvid;
1298 else if (vlan_filters)
1299 new_vlan = 0;
1300 else
1301 new_vlan = I40E_VLAN_ANY;
1302
1303 /* Create the new filter */
1304 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1305 if (!add_head)
1306 return -ENOMEM;
1307
1308 /* Put the replacement filter into the add list */
1309 hash_del(&add_head->hlist);
1310 hlist_add_head(&add_head->hlist, tmp_add_list);
1311
1312 /* Put the original filter into the delete list */
1313 f->state = I40E_FILTER_REMOVE;
1314 hash_del(&f->hlist);
1315 hlist_add_head(&f->hlist, tmp_del_list);
1316 }
1317 }
1318
1319 vsi->has_vlan_filter = !!vlan_filters;
1320
1321 return 0;
1322}
1323
1324/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001325 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1326 * @vsi: the PF Main VSI - inappropriate for any other VSI
1327 * @macaddr: the MAC address
1328 *
1329 * Remove whatever filter the firmware set up so the driver can manage
1330 * its own filtering intelligently.
1331 **/
1332static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1333{
1334 struct i40e_aqc_remove_macvlan_element_data element;
1335 struct i40e_pf *pf = vsi->back;
1336
1337 /* Only appropriate for the PF main VSI */
1338 if (vsi->type != I40E_VSI_MAIN)
1339 return;
1340
1341 memset(&element, 0, sizeof(element));
1342 ether_addr_copy(element.mac_addr, macaddr);
1343 element.vlan_tag = 0;
1344 /* Ignore error returns, some firmware does it this way... */
1345 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1346 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1347
1348 memset(&element, 0, sizeof(element));
1349 ether_addr_copy(element.mac_addr, macaddr);
1350 element.vlan_tag = 0;
1351 /* ...and some firmware does it this way. */
1352 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1353 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1354 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1355}
1356
1357/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001358 * i40e_add_filter - Add a mac/vlan filter to the VSI
1359 * @vsi: the VSI to be searched
1360 * @macaddr: the MAC address
1361 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001362 *
1363 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001364 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001365 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001366 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001367 **/
1368struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001369 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001370{
1371 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001372 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001373
1374 if (!vsi || !macaddr)
1375 return NULL;
1376
Jacob Keller1bc87e82016-10-05 09:30:31 -07001377 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378 if (!f) {
1379 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1380 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001381 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001382
Jacob Kellercbebb852016-10-05 09:30:40 -07001383 /* Update the boolean indicating if we need to function in
1384 * VLAN mode.
1385 */
1386 if (vlan >= 0)
1387 vsi->has_vlan_filter = true;
1388
Greg Rose9a173902014-05-22 06:32:02 +00001389 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001390 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001391 /* If we're in overflow promisc mode, set the state directly
1392 * to failed, so we don't bother to try sending the filter
1393 * to the hardware.
1394 */
1395 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1396 f->state = I40E_FILTER_FAILED;
1397 else
1398 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001399 INIT_HLIST_NODE(&f->hlist);
1400
1401 key = i40e_addr_to_hkey(macaddr);
1402 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001404 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1405 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1406 }
1407
Jacob Keller1bc87e82016-10-05 09:30:31 -07001408 /* If we're asked to add a filter that has been marked for removal, it
1409 * is safe to simply restore it to active state. __i40e_del_filter
1410 * will have simply deleted any filters which were previously marked
1411 * NEW or FAILED, so if it is currently marked REMOVE it must have
1412 * previously been ACTIVE. Since we haven't yet run the sync filters
1413 * task, just restore this filter to the ACTIVE state so that the
1414 * sync task leaves it in place
1415 */
1416 if (f->state == I40E_FILTER_REMOVE)
1417 f->state = I40E_FILTER_ACTIVE;
1418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001419 return f;
1420}
1421
1422/**
Jacob Keller290d2552016-10-05 09:30:36 -07001423 * __i40e_del_filter - Remove a specific filter from the VSI
1424 * @vsi: VSI to remove from
1425 * @f: the filter to remove from the list
1426 *
1427 * This function should be called instead of i40e_del_filter only if you know
1428 * the exact filter you will remove already, such as via i40e_find_filter or
1429 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001430 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001432 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001433 * ANOTHER NOTE: This function MUST be called from within the context of
1434 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1435 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001436 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001437static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001438{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001439 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001440 return;
1441
Jacob Keller1bc87e82016-10-05 09:30:31 -07001442 if ((f->state == I40E_FILTER_FAILED) ||
1443 (f->state == I40E_FILTER_NEW)) {
1444 /* this one never got added by the FW. Just remove it,
1445 * no need to sync anything.
1446 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001447 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001448 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001450 f->state = I40E_FILTER_REMOVE;
1451 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1452 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001453 }
1454}
1455
1456/**
Jacob Keller290d2552016-10-05 09:30:36 -07001457 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1458 * @vsi: the VSI to be searched
1459 * @macaddr: the MAC address
1460 * @vlan: the VLAN
1461 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001462 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001463 * being held.
1464 * ANOTHER NOTE: This function MUST be called from within the context of
1465 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1466 * instead of list_for_each_entry().
1467 **/
1468void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1469{
1470 struct i40e_mac_filter *f;
1471
1472 if (!vsi || !macaddr)
1473 return;
1474
1475 f = i40e_find_filter(vsi, macaddr, vlan);
1476 __i40e_del_filter(vsi, f);
1477}
1478
1479/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001480 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1481 * @vsi: the VSI to be searched
1482 * @macaddr: the mac address to be filtered
1483 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001484 * Goes through all the macvlan filters and adds a macvlan filter for each
1485 * unique vlan that already exists. If a PVID has been assigned, instead only
1486 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001487 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001488 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001489 **/
1490struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1491 const u8 *macaddr)
1492{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001493 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001494 struct hlist_node *h;
1495 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001496
1497 if (vsi->info.pvid)
1498 return i40e_add_filter(vsi, macaddr,
1499 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001500
Jacob Keller278e7d02016-10-05 09:30:37 -07001501 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001502 if (f->state == I40E_FILTER_REMOVE)
1503 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001504 add = i40e_add_filter(vsi, macaddr, f->vlan);
1505 if (!add)
1506 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001507 }
1508
Jacob Keller5feb3d72016-10-05 09:30:34 -07001509 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001510}
1511
1512/**
1513 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1514 * @vsi: the VSI to be searched
1515 * @macaddr: the mac address to be removed
1516 *
1517 * Removes a given MAC address from a VSI, regardless of VLAN
1518 *
1519 * Returns 0 for success, or error
1520 **/
1521int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1522{
Jacob Keller278e7d02016-10-05 09:30:37 -07001523 struct i40e_mac_filter *f;
1524 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001525 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001526 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001527
Jacob Keller278e7d02016-10-05 09:30:37 -07001528 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1529 "Missing mac_filter_hash_lock\n");
1530 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001531 if (ether_addr_equal(macaddr, f->macaddr)) {
1532 __i40e_del_filter(vsi, f);
1533 found = true;
1534 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001535 }
Jacob Keller290d2552016-10-05 09:30:36 -07001536
1537 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001538 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001539 else
1540 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001541}
1542
1543/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001544 * i40e_set_mac - NDO callback to set mac address
1545 * @netdev: network interface device structure
1546 * @p: pointer to an address structure
1547 *
1548 * Returns 0 on success, negative on failure
1549 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001550#ifdef I40E_FCOE
1551int i40e_set_mac(struct net_device *netdev, void *p)
1552#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001553static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001554#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555{
1556 struct i40e_netdev_priv *np = netdev_priv(netdev);
1557 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001558 struct i40e_pf *pf = vsi->back;
1559 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001560 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561
1562 if (!is_valid_ether_addr(addr->sa_data))
1563 return -EADDRNOTAVAIL;
1564
Shannon Nelson30650cc2014-07-29 04:01:50 +00001565 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1566 netdev_info(netdev, "already using mac address %pM\n",
1567 addr->sa_data);
1568 return 0;
1569 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001570
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001571 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1572 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1573 return -EADDRNOTAVAIL;
1574
Shannon Nelson30650cc2014-07-29 04:01:50 +00001575 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1576 netdev_info(netdev, "returning to hw mac address %pM\n",
1577 hw->mac.addr);
1578 else
1579 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1580
Jacob Keller278e7d02016-10-05 09:30:37 -07001581 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001582 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1583 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001584 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001585 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001586 if (vsi->type == I40E_VSI_MAIN) {
1587 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001590 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001591 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001592 if (ret)
1593 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1594 i40e_stat_str(hw, ret),
1595 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001596 }
1597
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001598 /* schedule our worker thread which will take care of
1599 * applying the new filter changes
1600 */
1601 i40e_service_event_schedule(vsi->back);
1602 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001603}
1604
1605/**
1606 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1607 * @vsi: the VSI being setup
1608 * @ctxt: VSI context structure
1609 * @enabled_tc: Enabled TCs bitmap
1610 * @is_add: True if called before Add VSI
1611 *
1612 * Setup VSI queue mapping for enabled traffic classes.
1613 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001614#ifdef I40E_FCOE
1615void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1616 struct i40e_vsi_context *ctxt,
1617 u8 enabled_tc,
1618 bool is_add)
1619#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001620static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1621 struct i40e_vsi_context *ctxt,
1622 u8 enabled_tc,
1623 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001624#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625{
1626 struct i40e_pf *pf = vsi->back;
1627 u16 sections = 0;
1628 u8 netdev_tc = 0;
1629 u16 numtc = 0;
1630 u16 qcount;
1631 u8 offset;
1632 u16 qmap;
1633 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001634 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635
1636 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1637 offset = 0;
1638
1639 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1640 /* Find numtc from enabled TC bitmap */
1641 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001642 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001643 numtc++;
1644 }
1645 if (!numtc) {
1646 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1647 numtc = 1;
1648 }
1649 } else {
1650 /* At least TC0 is enabled in case of non-DCB case */
1651 numtc = 1;
1652 }
1653
1654 vsi->tc_config.numtc = numtc;
1655 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001656 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001657 qcount = vsi->alloc_queue_pairs;
1658
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001659 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001660 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001661
1662 /* Setup queue offset/count for all TCs for given VSI */
1663 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1664 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001665 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001666 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001667 int pow, num_qps;
1668
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001669 switch (vsi->type) {
1670 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001671 qcount = min_t(int, pf->alloc_rss_size,
1672 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001673 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001674#ifdef I40E_FCOE
1675 case I40E_VSI_FCOE:
1676 qcount = num_tc_qps;
1677 break;
1678#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001679 case I40E_VSI_FDIR:
1680 case I40E_VSI_SRIOV:
1681 case I40E_VSI_VMDQ2:
1682 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001683 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001684 WARN_ON(i != 0);
1685 break;
1686 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001687 vsi->tc_config.tc_info[i].qoffset = offset;
1688 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001689
Shannon Nelson1e200e42015-02-27 09:15:24 +00001690 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001691 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001692 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001693 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001694 pow++;
1695 num_qps >>= 1;
1696 }
1697
1698 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1699 qmap =
1700 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1701 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1702
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001703 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001704 } else {
1705 /* TC is not enabled so set the offset to
1706 * default queue and allocate one queue
1707 * for the given TC.
1708 */
1709 vsi->tc_config.tc_info[i].qoffset = 0;
1710 vsi->tc_config.tc_info[i].qcount = 1;
1711 vsi->tc_config.tc_info[i].netdev_tc = 0;
1712
1713 qmap = 0;
1714 }
1715 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1716 }
1717
1718 /* Set actual Tx/Rx queue pairs */
1719 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001720 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1721 if (vsi->req_queue_pairs > 0)
1722 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001723 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001724 vsi->num_queue_pairs = pf->num_lan_msix;
1725 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001726
1727 /* Scheduler section valid can only be set for ADD VSI */
1728 if (is_add) {
1729 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1730
1731 ctxt->info.up_enable_bits = enabled_tc;
1732 }
1733 if (vsi->type == I40E_VSI_SRIOV) {
1734 ctxt->info.mapping_flags |=
1735 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1736 for (i = 0; i < vsi->num_queue_pairs; i++)
1737 ctxt->info.queue_mapping[i] =
1738 cpu_to_le16(vsi->base_queue + i);
1739 } else {
1740 ctxt->info.mapping_flags |=
1741 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1742 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1743 }
1744 ctxt->info.valid_sections |= cpu_to_le16(sections);
1745}
1746
1747/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001748 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1749 * @netdev: the netdevice
1750 * @addr: address to add
1751 *
1752 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1753 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1754 */
1755static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1756{
1757 struct i40e_netdev_priv *np = netdev_priv(netdev);
1758 struct i40e_vsi *vsi = np->vsi;
1759 struct i40e_mac_filter *f;
1760
1761 if (i40e_is_vsi_in_vlan(vsi))
1762 f = i40e_put_mac_in_vlan(vsi, addr);
1763 else
1764 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1765
1766 if (f)
1767 return 0;
1768 else
1769 return -ENOMEM;
1770}
1771
1772/**
1773 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1774 * @netdev: the netdevice
1775 * @addr: address to add
1776 *
1777 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1778 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1779 */
1780static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1781{
1782 struct i40e_netdev_priv *np = netdev_priv(netdev);
1783 struct i40e_vsi *vsi = np->vsi;
1784
1785 if (i40e_is_vsi_in_vlan(vsi))
1786 i40e_del_mac_all_vlan(vsi, addr);
1787 else
1788 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1789
1790 return 0;
1791}
1792
1793/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001794 * i40e_set_rx_mode - NDO callback to set the netdev filters
1795 * @netdev: network interface device structure
1796 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001797#ifdef I40E_FCOE
1798void i40e_set_rx_mode(struct net_device *netdev)
1799#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001800static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001801#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001802{
1803 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001804 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001805
Jacob Keller278e7d02016-10-05 09:30:37 -07001806 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001807
Jacob Keller6622f5c2016-10-05 09:30:32 -07001808 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1809 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001810
Jacob Keller278e7d02016-10-05 09:30:37 -07001811 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001812
1813 /* check for other flag changes */
1814 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1815 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1816 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1817 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001818
1819 /* schedule our worker thread which will take care of
1820 * applying the new filter changes
1821 */
1822 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001823}
1824
1825/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001826 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1827 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001828 * @from: Pointer to list which contains MAC filter entries - changes to
1829 * those entries needs to be undone.
1830 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001831 * MAC filter entries from list were slated to be sent to firmware, either for
1832 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001833 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001834static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1835 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001836{
Jacob Keller278e7d02016-10-05 09:30:37 -07001837 struct i40e_mac_filter *f;
1838 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001839
Jacob Keller278e7d02016-10-05 09:30:37 -07001840 hlist_for_each_entry_safe(f, h, from, hlist) {
1841 u64 key = i40e_addr_to_hkey(f->macaddr);
1842
Kiran Patil21659032015-09-30 14:09:03 -04001843 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001844 hlist_del(&f->hlist);
1845 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001846 }
1847}
1848
1849/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001850 * i40e_update_filter_state - Update filter state based on return data
1851 * from firmware
1852 * @count: Number of filters added
1853 * @add_list: return data from fw
1854 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001855 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001856 * MAC filter entries from list were slated to be added to device. Returns
1857 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001858 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001859static int
1860i40e_update_filter_state(int count,
1861 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Kellerac9e2392016-11-11 12:39:27 -08001862 struct i40e_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001863{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001864 int retval = 0;
1865 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001866
Jacob Kellerac9e2392016-11-11 12:39:27 -08001867 for (i = 0; i < count; i++) {
1868 /* Always check status of each filter. We don't need to check
1869 * the firmware return status because we pre-set the filter
1870 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1871 * request to the adminq. Thus, if it no longer matches then
1872 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001873 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001874 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001876 } else {
1877 add_head->state = I40E_FILTER_ACTIVE;
1878 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001879 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001880
1881 add_head = hlist_entry(add_head->hlist.next,
1882 typeof(struct i40e_mac_filter),
1883 hlist);
Kiran Patil21659032015-09-30 14:09:03 -04001884 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001885
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001886 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001887}
1888
1889/**
Jacob Keller00936312016-10-05 09:30:41 -07001890 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1891 * @vsi: ptr to the VSI
1892 * @vsi_name: name to display in messages
1893 * @list: the list of filters to send to firmware
1894 * @num_del: the number of filters to delete
1895 * @retval: Set to -EIO on failure to delete
1896 *
1897 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1898 * *retval instead of a return value so that success does not force ret_val to
1899 * be set to 0. This ensures that a sequence of calls to this function
1900 * preserve the previous value of *retval on successful delete.
1901 */
1902static
1903void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1904 struct i40e_aqc_remove_macvlan_element_data *list,
1905 int num_del, int *retval)
1906{
1907 struct i40e_hw *hw = &vsi->back->hw;
1908 i40e_status aq_ret;
1909 int aq_err;
1910
1911 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1912 aq_err = hw->aq.asq_last_status;
1913
1914 /* Explicitly ignore and do not report when firmware returns ENOENT */
1915 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1916 *retval = -EIO;
1917 dev_info(&vsi->back->pdev->dev,
1918 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1919 vsi_name, i40e_stat_str(hw, aq_ret),
1920 i40e_aq_str(hw, aq_err));
1921 }
1922}
1923
1924/**
1925 * i40e_aqc_add_filters - Request firmware to add a set of filters
1926 * @vsi: ptr to the VSI
1927 * @vsi_name: name to display in messages
1928 * @list: the list of filters to send to firmware
1929 * @add_head: Position in the add hlist
1930 * @num_add: the number of filters to add
1931 * @promisc_change: set to true on exit if promiscuous mode was forced on
1932 *
1933 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1934 * promisc_changed to true if the firmware has run out of space for more
1935 * filters.
1936 */
1937static
1938void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1939 struct i40e_aqc_add_macvlan_element_data *list,
1940 struct i40e_mac_filter *add_head,
1941 int num_add, bool *promisc_changed)
1942{
1943 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001944 int aq_err, fcnt;
1945
Jacob Kellerac9e2392016-11-11 12:39:27 -08001946 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001947 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001948 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001949
1950 if (fcnt != num_add) {
1951 *promisc_changed = true;
1952 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001953 dev_warn(&vsi->back->pdev->dev,
1954 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1955 i40e_aq_str(hw, aq_err),
1956 vsi_name);
1957 }
1958}
1959
1960/**
Jacob Keller435c0842016-11-08 13:05:10 -08001961 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1962 * @vsi: pointer to the VSI
1963 * @f: filter data
1964 *
1965 * This function sets or clears the promiscuous broadcast flags for VLAN
1966 * filters in order to properly receive broadcast frames. Assumes that only
1967 * broadcast filters are passed.
1968 **/
1969static
1970void i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1971 struct i40e_mac_filter *f)
1972{
1973 bool enable = f->state == I40E_FILTER_NEW;
1974 struct i40e_hw *hw = &vsi->back->hw;
1975 i40e_status aq_ret;
1976
1977 if (f->vlan == I40E_VLAN_ANY) {
1978 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1979 vsi->seid,
1980 enable,
1981 NULL);
1982 } else {
1983 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1984 vsi->seid,
1985 enable,
1986 f->vlan,
1987 NULL);
1988 }
1989
1990 if (aq_ret) {
1991 dev_warn(&vsi->back->pdev->dev,
1992 "Error %s setting broadcast promiscuous mode on %s\n",
1993 i40e_aq_str(hw, hw->aq.asq_last_status),
1994 vsi_name);
1995 f->state = I40E_FILTER_FAILED;
1996 } else if (enable) {
1997 f->state = I40E_FILTER_ACTIVE;
1998 }
1999}
2000
2001/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002002 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2003 * @vsi: ptr to the VSI
2004 *
2005 * Push any outstanding VSI filter changes through the AdminQ.
2006 *
2007 * Returns 0 or error value
2008 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002009int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002010{
Jacob Keller278e7d02016-10-05 09:30:37 -07002011 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07002012 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002013 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002014 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002015 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002016 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002017 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002019 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002020 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002021 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002022 struct i40e_pf *pf;
2023 int num_add = 0;
2024 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002025 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002026 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002027 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002028 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002029
2030 /* empty array typed pointers, kcalloc later */
2031 struct i40e_aqc_add_macvlan_element_data *add_list;
2032 struct i40e_aqc_remove_macvlan_element_data *del_list;
2033
2034 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2035 usleep_range(1000, 2000);
2036 pf = vsi->back;
2037
2038 if (vsi->netdev) {
2039 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2040 vsi->current_netdev_flags = vsi->netdev->flags;
2041 }
2042
Jacob Keller278e7d02016-10-05 09:30:37 -07002043 INIT_HLIST_HEAD(&tmp_add_list);
2044 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002045
Shannon Nelson2d1de822016-05-16 10:26:44 -07002046 if (vsi->type == I40E_VSI_SRIOV)
2047 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2048 else if (vsi->type != I40E_VSI_MAIN)
2049 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2050
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002051 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2052 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2053
Jacob Keller278e7d02016-10-05 09:30:37 -07002054 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002055 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002056 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002057 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002058 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002059 hash_del(&f->hlist);
2060 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002061
2062 /* Avoid counting removed filters */
2063 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002064 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002065 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002066 hash_del(&f->hlist);
2067 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002068 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002069
Jacob Keller489a3262016-11-11 12:39:31 -08002070 /* Count the number of active (current and new) VLAN
2071 * filters we have now. Does not count filters which
2072 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002073 */
2074 if (f->vlan > 0)
2075 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002076 }
2077
Jacob Keller489a3262016-11-11 12:39:31 -08002078 retval = i40e_correct_mac_vlan_filters(vsi,
2079 &tmp_add_list,
2080 &tmp_del_list,
2081 vlan_filters);
2082 if (retval)
2083 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002084
Jacob Keller278e7d02016-10-05 09:30:37 -07002085 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002086 }
2087
2088 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002089 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002090 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002091 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002092 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002093 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002094 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002095 if (!del_list)
2096 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002097
Jacob Keller278e7d02016-10-05 09:30:37 -07002098 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002099 cmd_flags = 0;
2100
Jacob Keller435c0842016-11-08 13:05:10 -08002101 /* handle broadcast filters by updating the broadcast
2102 * promiscuous flag instead of deleting a MAC filter.
2103 */
2104 if (is_broadcast_ether_addr(f->macaddr)) {
2105 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2106
2107 hlist_del(&f->hlist);
2108 kfree(f);
2109 continue;
2110 }
2111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002112 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002113 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002114 if (f->vlan == I40E_VLAN_ANY) {
2115 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002116 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002117 } else {
2118 del_list[num_del].vlan_tag =
2119 cpu_to_le16((u16)(f->vlan));
2120 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002122 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2123 del_list[num_del].flags = cmd_flags;
2124 num_del++;
2125
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002126 /* flush a full buffer */
2127 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002128 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2129 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002130 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002131 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002132 }
Kiran Patil21659032015-09-30 14:09:03 -04002133 /* Release memory for MAC filter entries which were
2134 * synced up with HW.
2135 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002136 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002137 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002138 }
Kiran Patil21659032015-09-30 14:09:03 -04002139
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002140 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002141 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2142 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002143 }
2144
2145 kfree(del_list);
2146 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002147 }
2148
Jacob Keller278e7d02016-10-05 09:30:37 -07002149 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002150 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002151 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002152 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002153 list_size = filter_list_len *
2154 sizeof(struct i40e_aqc_add_macvlan_element_data);
2155 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002156 if (!add_list)
2157 goto err_no_memory;
2158
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002159 num_add = 0;
Jacob Keller435c0842016-11-08 13:05:10 -08002160 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002161 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2162 &vsi->state)) {
2163 f->state = I40E_FILTER_FAILED;
2164 continue;
2165 }
Jacob Keller435c0842016-11-08 13:05:10 -08002166
2167 /* handle broadcast filters by updating the broadcast
2168 * promiscuous flag instead of adding a MAC filter.
2169 */
2170 if (is_broadcast_ether_addr(f->macaddr)) {
2171 u64 key = i40e_addr_to_hkey(f->macaddr);
2172 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2173
2174 hlist_del(&f->hlist);
2175 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2176 continue;
2177 }
2178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002179 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002180 if (num_add == 0)
2181 add_head = f;
2182 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002183 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002184 if (f->vlan == I40E_VLAN_ANY) {
2185 add_list[num_add].vlan_tag = 0;
2186 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2187 } else {
2188 add_list[num_add].vlan_tag =
2189 cpu_to_le16((u16)(f->vlan));
2190 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002191 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002192 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002193 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002194 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2196 num_add++;
2197
2198 /* flush a full buffer */
2199 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002200 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2201 add_head, num_add,
2202 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002203 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002204 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 }
2206 }
2207 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002208 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2209 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002210 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002211 /* Now move all of the filters from the temp add list back to
2212 * the VSI's list.
2213 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002214 spin_lock_bh(&vsi->mac_filter_hash_lock);
2215 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2216 u64 key = i40e_addr_to_hkey(f->macaddr);
2217
2218 hlist_del(&f->hlist);
2219 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002220 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002221 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002222 kfree(add_list);
2223 add_list = NULL;
2224 }
2225
Jacob Keller38326212016-11-11 12:39:26 -08002226 /* Determine the number of active and failed filters. */
2227 spin_lock_bh(&vsi->mac_filter_hash_lock);
2228 vsi->active_filters = 0;
2229 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2230 if (f->state == I40E_FILTER_ACTIVE)
2231 vsi->active_filters++;
2232 else if (f->state == I40E_FILTER_FAILED)
2233 failed_filters++;
2234 }
2235 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2236
2237 /* If promiscuous mode has changed, we need to calculate a new
2238 * threshold for when we are safe to exit
2239 */
2240 if (promisc_changed)
2241 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2242
2243 /* Check if we are able to exit overflow promiscuous mode. We can
2244 * safely exit if we didn't just enter, we no longer have any failed
2245 * filters, and we have reduced filters below the threshold value.
2246 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002247 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002248 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002249 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002250 dev_info(&pf->pdev->dev,
2251 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2252 vsi_name);
2253 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2254 promisc_changed = true;
2255 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002256 }
2257
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002258 /* if the VF is not trusted do not do promisc */
2259 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2260 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2261 goto out;
2262 }
2263
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002264 /* check for changes in promiscuous modes */
2265 if (changed_flags & IFF_ALLMULTI) {
2266 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002269 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2270 vsi->seid,
2271 cur_multipromisc,
2272 NULL);
2273 if (aq_ret) {
2274 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002275 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002276 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002277 "set multi promisc failed on %s, err %s aq_err %s\n",
2278 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002279 i40e_stat_str(hw, aq_ret),
2280 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002281 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002282 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002283 if ((changed_flags & IFF_PROMISC) ||
2284 (promisc_changed &&
2285 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002286 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002288 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2289 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2290 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002291 if ((vsi->type == I40E_VSI_MAIN) &&
2292 (pf->lan_veb != I40E_NO_VEB) &&
2293 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002294 /* set defport ON for Main VSI instead of true promisc
2295 * this way we will get all unicast/multicast and VLAN
2296 * promisc behavior but will not get VF or VMDq traffic
2297 * replicated on the Main VSI.
2298 */
2299 if (pf->cur_promisc != cur_promisc) {
2300 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002301 if (cur_promisc)
2302 aq_ret =
2303 i40e_aq_set_default_vsi(hw,
2304 vsi->seid,
2305 NULL);
2306 else
2307 aq_ret =
2308 i40e_aq_clear_default_vsi(hw,
2309 vsi->seid,
2310 NULL);
2311 if (aq_ret) {
2312 retval = i40e_aq_rc_to_posix(aq_ret,
2313 hw->aq.asq_last_status);
2314 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002315 "Set default VSI failed on %s, err %s, aq_err %s\n",
2316 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002317 i40e_stat_str(hw, aq_ret),
2318 i40e_aq_str(hw,
2319 hw->aq.asq_last_status));
2320 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002321 }
2322 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002323 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002324 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002325 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002326 cur_promisc, NULL,
2327 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002328 if (aq_ret) {
2329 retval =
2330 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002331 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002332 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002333 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2334 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002335 i40e_stat_str(hw, aq_ret),
2336 i40e_aq_str(hw,
2337 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002338 }
2339 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002340 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002341 vsi->seid,
2342 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002343 if (aq_ret) {
2344 retval =
2345 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002346 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002347 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002348 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2349 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002350 i40e_stat_str(hw, aq_ret),
2351 i40e_aq_str(hw,
2352 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002353 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002354 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002355 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2356 vsi->seid,
2357 cur_promisc, NULL);
2358 if (aq_ret) {
2359 retval = i40e_aq_rc_to_posix(aq_ret,
2360 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002361 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002362 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002363 i40e_stat_str(hw, aq_ret),
2364 i40e_aq_str(hw,
2365 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002366 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002367 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002368out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002369 /* if something went wrong then set the changed flag so we try again */
2370 if (retval)
2371 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2372
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002373 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002374 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002375
2376err_no_memory:
2377 /* Restore elements on the temporary add and delete lists */
2378 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002379err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002380 i40e_undo_filter_entries(vsi, &tmp_del_list);
2381 i40e_undo_filter_entries(vsi, &tmp_add_list);
2382 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2383
2384 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2385 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2386 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002387}
2388
2389/**
2390 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2391 * @pf: board private structure
2392 **/
2393static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2394{
2395 int v;
2396
2397 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2398 return;
2399 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2400
Mitch Williams505682c2014-05-20 08:01:37 +00002401 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002402 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002403 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2404 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2405
2406 if (ret) {
2407 /* come back and try again later */
2408 pf->flags |= I40E_FLAG_FILTER_SYNC;
2409 break;
2410 }
2411 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002412 }
2413}
2414
2415/**
2416 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2417 * @netdev: network interface device structure
2418 * @new_mtu: new value for maximum frame size
2419 *
2420 * Returns 0 on success, negative on failure
2421 **/
2422static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2423{
2424 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002425 struct i40e_vsi *vsi = np->vsi;
2426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002427 netdev_info(netdev, "changing MTU from %d to %d\n",
2428 netdev->mtu, new_mtu);
2429 netdev->mtu = new_mtu;
2430 if (netif_running(netdev))
2431 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002432 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433 return 0;
2434}
2435
2436/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002437 * i40e_ioctl - Access the hwtstamp interface
2438 * @netdev: network interface device structure
2439 * @ifr: interface request data
2440 * @cmd: ioctl command
2441 **/
2442int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2443{
2444 struct i40e_netdev_priv *np = netdev_priv(netdev);
2445 struct i40e_pf *pf = np->vsi->back;
2446
2447 switch (cmd) {
2448 case SIOCGHWTSTAMP:
2449 return i40e_ptp_get_ts_config(pf, ifr);
2450 case SIOCSHWTSTAMP:
2451 return i40e_ptp_set_ts_config(pf, ifr);
2452 default:
2453 return -EOPNOTSUPP;
2454 }
2455}
2456
2457/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002458 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2459 * @vsi: the vsi being adjusted
2460 **/
2461void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2462{
2463 struct i40e_vsi_context ctxt;
2464 i40e_status ret;
2465
2466 if ((vsi->info.valid_sections &
2467 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2468 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2469 return; /* already enabled */
2470
2471 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2472 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2473 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2474
2475 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002476 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002477 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2478 if (ret) {
2479 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002480 "update vlan stripping failed, err %s aq_err %s\n",
2481 i40e_stat_str(&vsi->back->hw, ret),
2482 i40e_aq_str(&vsi->back->hw,
2483 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002484 }
2485}
2486
2487/**
2488 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2489 * @vsi: the vsi being adjusted
2490 **/
2491void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2492{
2493 struct i40e_vsi_context ctxt;
2494 i40e_status ret;
2495
2496 if ((vsi->info.valid_sections &
2497 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2498 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2499 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2500 return; /* already disabled */
2501
2502 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2503 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2504 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2505
2506 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002507 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002508 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2509 if (ret) {
2510 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002511 "update vlan stripping failed, err %s aq_err %s\n",
2512 i40e_stat_str(&vsi->back->hw, ret),
2513 i40e_aq_str(&vsi->back->hw,
2514 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002515 }
2516}
2517
2518/**
2519 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2520 * @netdev: network interface to be adjusted
2521 * @features: netdev features to test if VLAN offload is enabled or not
2522 **/
2523static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2524{
2525 struct i40e_netdev_priv *np = netdev_priv(netdev);
2526 struct i40e_vsi *vsi = np->vsi;
2527
2528 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2529 i40e_vlan_stripping_enable(vsi);
2530 else
2531 i40e_vlan_stripping_disable(vsi);
2532}
2533
2534/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002535 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536 * @vsi: the vsi being configured
2537 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002538 *
2539 * This is a helper function for adding a new MAC/VLAN filter with the
2540 * specified VLAN for each existing MAC address already in the hash table.
2541 * This function does *not* perform any accounting to update filters based on
2542 * VLAN mode.
2543 *
2544 * NOTE: this function expects to be called while under the
2545 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002546 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002547int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002548{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002549 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002550 struct hlist_node *h;
2551 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002552
Jacob Keller278e7d02016-10-05 09:30:37 -07002553 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002554 if (f->state == I40E_FILTER_REMOVE)
2555 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002556 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557 if (!add_f) {
2558 dev_info(&vsi->back->pdev->dev,
2559 "Could not add vlan filter %d for %pM\n",
2560 vid, f->macaddr);
2561 return -ENOMEM;
2562 }
2563 }
2564
Jacob Keller490a4ad2016-11-11 12:39:29 -08002565 return 0;
2566}
2567
2568/**
2569 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2570 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002571 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002572 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002573int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002574{
Jacob Keller489a3262016-11-11 12:39:31 -08002575 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002576
Jacob Kellerf94484b2016-12-07 14:05:34 -08002577 if (!vid || vsi->info.pvid)
2578 return -EINVAL;
2579
Jacob Keller490a4ad2016-11-11 12:39:29 -08002580 /* Locked once because all functions invoked below iterates list*/
2581 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002582 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002583 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002584 if (err)
2585 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002586
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002587 /* schedule our worker thread which will take care of
2588 * applying the new filter changes
2589 */
2590 i40e_service_event_schedule(vsi->back);
2591 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592}
2593
2594/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002595 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002596 * @vsi: the vsi being configured
2597 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002598 *
2599 * This function should be used to remove all VLAN filters which match the
2600 * given VID. It does not schedule the service event and does not take the
2601 * mac_filter_hash_lock so it may be combined with other operations under
2602 * a single invocation of the mac_filter_hash_lock.
2603 *
2604 * NOTE: this function expects to be called while under the
2605 * mac_filter_hash_lock
2606 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002607void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608{
Alan Brady84f5ca62016-10-05 09:30:39 -07002609 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002610 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002611 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002612
Jacob Keller278e7d02016-10-05 09:30:37 -07002613 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002614 if (f->vlan == vid)
2615 __i40e_del_filter(vsi, f);
2616 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002617}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002618
Jacob Keller490a4ad2016-11-11 12:39:29 -08002619/**
2620 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2621 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002622 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002623 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002624void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002625{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002626 if (!vid || vsi->info.pvid)
2627 return;
2628
Jacob Keller490a4ad2016-11-11 12:39:29 -08002629 spin_lock_bh(&vsi->mac_filter_hash_lock);
2630 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002631 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002632
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002633 /* schedule our worker thread which will take care of
2634 * applying the new filter changes
2635 */
2636 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002637}
2638
2639/**
2640 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2641 * @netdev: network interface to be adjusted
2642 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002643 *
2644 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002645 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002646#ifdef I40E_FCOE
2647int i40e_vlan_rx_add_vid(struct net_device *netdev,
2648 __always_unused __be16 proto, u16 vid)
2649#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002650static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2651 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002652#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002653{
2654 struct i40e_netdev_priv *np = netdev_priv(netdev);
2655 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002656 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002657
Jacob Keller6a1127852016-10-25 16:08:49 -07002658 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002659 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002661 /* If the network stack called us with vid = 0 then
2662 * it is asking to receive priority tagged packets with
2663 * vlan id 0. Our HW receives them by default when configured
2664 * to receive untagged packets so there is no need to add an
2665 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002667 if (vid)
2668 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002669
Jacob Keller6a1127852016-10-25 16:08:49 -07002670 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002671 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002672
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002673 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002674}
2675
2676/**
2677 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2678 * @netdev: network interface to be adjusted
2679 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002680 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002681 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002682 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002683#ifdef I40E_FCOE
2684int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2685 __always_unused __be16 proto, u16 vid)
2686#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002687static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2688 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002689#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690{
2691 struct i40e_netdev_priv *np = netdev_priv(netdev);
2692 struct i40e_vsi *vsi = np->vsi;
2693
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002694 /* return code is ignored as there is nothing a user
2695 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002696 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002697 */
2698 i40e_vsi_kill_vlan(vsi, vid);
2699
2700 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002701
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002702 return 0;
2703}
2704
2705/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002706 * i40e_macaddr_init - explicitly write the mac address filters
2707 *
2708 * @vsi: pointer to the vsi
2709 * @macaddr: the MAC address
2710 *
2711 * This is needed when the macaddr has been obtained by other
2712 * means than the default, e.g., from Open Firmware or IDPROM.
2713 * Returns 0 on success, negative on failure
2714 **/
2715static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2716{
2717 int ret;
2718 struct i40e_aqc_add_macvlan_element_data element;
2719
2720 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2721 I40E_AQC_WRITE_TYPE_LAA_WOL,
2722 macaddr, NULL);
2723 if (ret) {
2724 dev_info(&vsi->back->pdev->dev,
2725 "Addr change for VSI failed: %d\n", ret);
2726 return -EADDRNOTAVAIL;
2727 }
2728
2729 memset(&element, 0, sizeof(element));
2730 ether_addr_copy(element.mac_addr, macaddr);
2731 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2732 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2733 if (ret) {
2734 dev_info(&vsi->back->pdev->dev,
2735 "add filter failed err %s aq_err %s\n",
2736 i40e_stat_str(&vsi->back->hw, ret),
2737 i40e_aq_str(&vsi->back->hw,
2738 vsi->back->hw.aq.asq_last_status));
2739 }
2740 return ret;
2741}
2742
2743/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002744 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2745 * @vsi: the vsi being brought back up
2746 **/
2747static void i40e_restore_vlan(struct i40e_vsi *vsi)
2748{
2749 u16 vid;
2750
2751 if (!vsi->netdev)
2752 return;
2753
2754 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2755
2756 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2757 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2758 vid);
2759}
2760
2761/**
2762 * i40e_vsi_add_pvid - Add pvid for the VSI
2763 * @vsi: the vsi being adjusted
2764 * @vid: the vlan id to set as a PVID
2765 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002766int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002767{
2768 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002769 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002770
2771 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2772 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002773 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2774 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002775 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002776
2777 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002778 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002779 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2780 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002781 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002782 "add pvid failed, err %s aq_err %s\n",
2783 i40e_stat_str(&vsi->back->hw, ret),
2784 i40e_aq_str(&vsi->back->hw,
2785 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002786 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002787 }
2788
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002789 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002790}
2791
2792/**
2793 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2794 * @vsi: the vsi being adjusted
2795 *
2796 * Just use the vlan_rx_register() service to put it back to normal
2797 **/
2798void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2799{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002800 i40e_vlan_stripping_disable(vsi);
2801
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002802 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002803}
2804
2805/**
2806 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2807 * @vsi: ptr to the VSI
2808 *
2809 * If this function returns with an error, then it's possible one or
2810 * more of the rings is populated (while the rest are not). It is the
2811 * callers duty to clean those orphaned rings.
2812 *
2813 * Return 0 on success, negative on failure
2814 **/
2815static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2816{
2817 int i, err = 0;
2818
2819 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002820 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002821
2822 return err;
2823}
2824
2825/**
2826 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2827 * @vsi: ptr to the VSI
2828 *
2829 * Free VSI's transmit software resources
2830 **/
2831static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2832{
2833 int i;
2834
Greg Rose8e9dca52013-12-18 13:45:53 +00002835 if (!vsi->tx_rings)
2836 return;
2837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002838 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002839 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002840 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002841}
2842
2843/**
2844 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2845 * @vsi: ptr to the VSI
2846 *
2847 * If this function returns with an error, then it's possible one or
2848 * more of the rings is populated (while the rest are not). It is the
2849 * callers duty to clean those orphaned rings.
2850 *
2851 * Return 0 on success, negative on failure
2852 **/
2853static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2854{
2855 int i, err = 0;
2856
2857 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002858 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002859#ifdef I40E_FCOE
2860 i40e_fcoe_setup_ddp_resources(vsi);
2861#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002862 return err;
2863}
2864
2865/**
2866 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2867 * @vsi: ptr to the VSI
2868 *
2869 * Free all receive software resources
2870 **/
2871static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2872{
2873 int i;
2874
Greg Rose8e9dca52013-12-18 13:45:53 +00002875 if (!vsi->rx_rings)
2876 return;
2877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002878 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002879 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002880 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002881#ifdef I40E_FCOE
2882 i40e_fcoe_free_ddp_resources(vsi);
2883#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002884}
2885
2886/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002887 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2888 * @ring: The Tx ring to configure
2889 *
2890 * This enables/disables XPS for a given Tx descriptor ring
2891 * based on the TCs enabled for the VSI that ring belongs to.
2892 **/
2893static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2894{
2895 struct i40e_vsi *vsi = ring->vsi;
2896 cpumask_var_t mask;
2897
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002898 if (!ring->q_vector || !ring->netdev)
2899 return;
2900
2901 /* Single TC mode enable XPS */
2902 if (vsi->tc_config.numtc <= 1) {
2903 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002904 netif_set_xps_queue(ring->netdev,
2905 &ring->q_vector->affinity_mask,
2906 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002907 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2908 /* Disable XPS to allow selection based on TC */
2909 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2910 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2911 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002912 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002913
2914 /* schedule our worker thread which will take care of
2915 * applying the new filter changes
2916 */
2917 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002918}
2919
2920/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002921 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2922 * @ring: The Tx ring to configure
2923 *
2924 * Configure the Tx descriptor ring in the HMC context.
2925 **/
2926static int i40e_configure_tx_ring(struct i40e_ring *ring)
2927{
2928 struct i40e_vsi *vsi = ring->vsi;
2929 u16 pf_q = vsi->base_queue + ring->queue_index;
2930 struct i40e_hw *hw = &vsi->back->hw;
2931 struct i40e_hmc_obj_txq tx_ctx;
2932 i40e_status err = 0;
2933 u32 qtx_ctl = 0;
2934
2935 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002936 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002937 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2938 ring->atr_count = 0;
2939 } else {
2940 ring->atr_sample_rate = 0;
2941 }
2942
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002943 /* configure XPS */
2944 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945
2946 /* clear the context structure first */
2947 memset(&tx_ctx, 0, sizeof(tx_ctx));
2948
2949 tx_ctx.new_context = 1;
2950 tx_ctx.base = (ring->dma / 128);
2951 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002952 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2953 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002954#ifdef I40E_FCOE
2955 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2956#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002957 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002958 /* FDIR VSI tx ring can still use RS bit and writebacks */
2959 if (vsi->type != I40E_VSI_FDIR)
2960 tx_ctx.head_wb_ena = 1;
2961 tx_ctx.head_wb_addr = ring->dma +
2962 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002963
2964 /* As part of VSI creation/update, FW allocates certain
2965 * Tx arbitration queue sets for each TC enabled for
2966 * the VSI. The FW returns the handles to these queue
2967 * sets as part of the response buffer to Add VSI,
2968 * Update VSI, etc. AQ commands. It is expected that
2969 * these queue set handles be associated with the Tx
2970 * queues by the driver as part of the TX queue context
2971 * initialization. This has to be done regardless of
2972 * DCB as by default everything is mapped to TC0.
2973 */
2974 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2975 tx_ctx.rdylist_act = 0;
2976
2977 /* clear the context in the HMC */
2978 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2979 if (err) {
2980 dev_info(&vsi->back->pdev->dev,
2981 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2982 ring->queue_index, pf_q, err);
2983 return -ENOMEM;
2984 }
2985
2986 /* set the context in the HMC */
2987 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2988 if (err) {
2989 dev_info(&vsi->back->pdev->dev,
2990 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2991 ring->queue_index, pf_q, err);
2992 return -ENOMEM;
2993 }
2994
2995 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002996 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002997 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002998 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2999 I40E_QTX_CTL_VFVM_INDX_MASK;
3000 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003001 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003002 }
3003
Shannon Nelson13fd9772013-09-28 07:14:19 +00003004 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3005 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3007 i40e_flush(hw);
3008
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003009 /* cache tail off for easier writes later */
3010 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3011
3012 return 0;
3013}
3014
3015/**
3016 * i40e_configure_rx_ring - Configure a receive ring context
3017 * @ring: The Rx ring to configure
3018 *
3019 * Configure the Rx descriptor ring in the HMC context.
3020 **/
3021static int i40e_configure_rx_ring(struct i40e_ring *ring)
3022{
3023 struct i40e_vsi *vsi = ring->vsi;
3024 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3025 u16 pf_q = vsi->base_queue + ring->queue_index;
3026 struct i40e_hw *hw = &vsi->back->hw;
3027 struct i40e_hmc_obj_rxq rx_ctx;
3028 i40e_status err = 0;
3029
3030 ring->state = 0;
3031
3032 /* clear the context structure first */
3033 memset(&rx_ctx, 0, sizeof(rx_ctx));
3034
3035 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003036
3037 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003038
3039 rx_ctx.base = (ring->dma / 128);
3040 rx_ctx.qlen = ring->count;
3041
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003042 /* use 32 byte descriptors */
3043 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003044
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003045 /* descriptor type is always zero
3046 * rx_ctx.dtype = 0;
3047 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003048 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003049
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003050 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003051 if (hw->revision_id == 0)
3052 rx_ctx.lrxqthresh = 0;
3053 else
3054 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003055 rx_ctx.crcstrip = 1;
3056 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003057 /* this controls whether VLAN is stripped from inner headers */
3058 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003059#ifdef I40E_FCOE
3060 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3061#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003062 /* set the prefena field to 1 because the manual says to */
3063 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003064
3065 /* clear the context in the HMC */
3066 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3067 if (err) {
3068 dev_info(&vsi->back->pdev->dev,
3069 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3070 ring->queue_index, pf_q, err);
3071 return -ENOMEM;
3072 }
3073
3074 /* set the context in the HMC */
3075 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3076 if (err) {
3077 dev_info(&vsi->back->pdev->dev,
3078 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3079 ring->queue_index, pf_q, err);
3080 return -ENOMEM;
3081 }
3082
3083 /* cache tail for quicker writes, and clear the reg before use */
3084 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3085 writel(0, ring->tail);
3086
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003087 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003088
3089 return 0;
3090}
3091
3092/**
3093 * i40e_vsi_configure_tx - Configure the VSI for Tx
3094 * @vsi: VSI structure describing this set of rings and resources
3095 *
3096 * Configure the Tx VSI for operation.
3097 **/
3098static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3099{
3100 int err = 0;
3101 u16 i;
3102
Alexander Duyck9f65e152013-09-28 06:00:58 +00003103 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3104 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003105
3106 return err;
3107}
3108
3109/**
3110 * i40e_vsi_configure_rx - Configure the VSI for Rx
3111 * @vsi: the VSI being configured
3112 *
3113 * Configure the Rx VSI for operation.
3114 **/
3115static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3116{
3117 int err = 0;
3118 u16 i;
3119
3120 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3121 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3122 + ETH_FCS_LEN + VLAN_HLEN;
3123 else
3124 vsi->max_frame = I40E_RXBUFFER_2048;
3125
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003126 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127
Vasu Dev38e00432014-08-01 13:27:03 -07003128#ifdef I40E_FCOE
3129 /* setup rx buffer for FCoE */
3130 if ((vsi->type == I40E_VSI_FCOE) &&
3131 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003132 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3133 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003134 }
3135
3136#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003137 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003138 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003139 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003140
3141 /* set up individual rings */
3142 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003143 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003144
3145 return err;
3146}
3147
3148/**
3149 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3150 * @vsi: ptr to the VSI
3151 **/
3152static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3153{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003154 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003155 u16 qoffset, qcount;
3156 int i, n;
3157
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003158 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3159 /* Reset the TC information */
3160 for (i = 0; i < vsi->num_queue_pairs; i++) {
3161 rx_ring = vsi->rx_rings[i];
3162 tx_ring = vsi->tx_rings[i];
3163 rx_ring->dcb_tc = 0;
3164 tx_ring->dcb_tc = 0;
3165 }
3166 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003167
3168 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003169 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003170 continue;
3171
3172 qoffset = vsi->tc_config.tc_info[n].qoffset;
3173 qcount = vsi->tc_config.tc_info[n].qcount;
3174 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003175 rx_ring = vsi->rx_rings[i];
3176 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003177 rx_ring->dcb_tc = n;
3178 tx_ring->dcb_tc = n;
3179 }
3180 }
3181}
3182
3183/**
3184 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3185 * @vsi: ptr to the VSI
3186 **/
3187static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3188{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003189 struct i40e_pf *pf = vsi->back;
3190 int err;
3191
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003192 if (vsi->netdev)
3193 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003194
3195 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3196 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3197 if (err) {
3198 dev_warn(&pf->pdev->dev,
3199 "could not set up macaddr; err %d\n", err);
3200 }
3201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003202}
3203
3204/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003205 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3206 * @vsi: Pointer to the targeted VSI
3207 *
3208 * This function replays the hlist on the hw where all the SB Flow Director
3209 * filters were saved.
3210 **/
3211static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3212{
3213 struct i40e_fdir_filter *filter;
3214 struct i40e_pf *pf = vsi->back;
3215 struct hlist_node *node;
3216
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003217 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3218 return;
3219
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003220 hlist_for_each_entry_safe(filter, node,
3221 &pf->fdir_filter_list, fdir_node) {
3222 i40e_add_del_fdir(vsi, filter, true);
3223 }
3224}
3225
3226/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003227 * i40e_vsi_configure - Set up the VSI for action
3228 * @vsi: the VSI being configured
3229 **/
3230static int i40e_vsi_configure(struct i40e_vsi *vsi)
3231{
3232 int err;
3233
3234 i40e_set_vsi_rx_mode(vsi);
3235 i40e_restore_vlan(vsi);
3236 i40e_vsi_config_dcb_rings(vsi);
3237 err = i40e_vsi_configure_tx(vsi);
3238 if (!err)
3239 err = i40e_vsi_configure_rx(vsi);
3240
3241 return err;
3242}
3243
3244/**
3245 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3246 * @vsi: the VSI being configured
3247 **/
3248static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3249{
3250 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003251 struct i40e_hw *hw = &pf->hw;
3252 u16 vector;
3253 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254 u32 qp;
3255
3256 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3257 * and PFINT_LNKLSTn registers, e.g.:
3258 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3259 */
3260 qp = vsi->base_queue;
3261 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003262 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003263 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3264
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003265 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003266 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003267 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3268 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3269 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003270 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3272 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3273 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003274 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3275 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003276
3277 /* Linked list for the queuepairs assigned to this vector */
3278 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3279 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003280 u32 val;
3281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3283 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3284 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3285 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3286 (I40E_QUEUE_TYPE_TX
3287 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3288
3289 wr32(hw, I40E_QINT_RQCTL(qp), val);
3290
3291 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3292 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3293 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3294 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3295 (I40E_QUEUE_TYPE_RX
3296 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3297
3298 /* Terminate the linked list */
3299 if (q == (q_vector->num_ringpairs - 1))
3300 val |= (I40E_QUEUE_END_OF_LIST
3301 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3302
3303 wr32(hw, I40E_QINT_TQCTL(qp), val);
3304 qp++;
3305 }
3306 }
3307
3308 i40e_flush(hw);
3309}
3310
3311/**
3312 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3313 * @hw: ptr to the hardware info
3314 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003315static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003316{
Jacob Kellerab437b52014-12-14 01:55:08 +00003317 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003318 u32 val;
3319
3320 /* clear things first */
3321 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3322 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3323
3324 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3325 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3326 I40E_PFINT_ICR0_ENA_GRST_MASK |
3327 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3328 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3330 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3331 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3332
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003333 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3334 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3335
Jacob Kellerab437b52014-12-14 01:55:08 +00003336 if (pf->flags & I40E_FLAG_PTP)
3337 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3338
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003339 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3340
3341 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003342 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3343 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003344
3345 /* OTHER_ITR_IDX = 0 */
3346 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3347}
3348
3349/**
3350 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3351 * @vsi: the VSI being configured
3352 **/
3353static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3354{
Alexander Duyck493fb302013-09-28 07:01:44 +00003355 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003356 struct i40e_pf *pf = vsi->back;
3357 struct i40e_hw *hw = &pf->hw;
3358 u32 val;
3359
3360 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003361 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003362 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003363 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3364 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003365 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003366 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3367 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3368
Jacob Kellerab437b52014-12-14 01:55:08 +00003369 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003370
3371 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3372 wr32(hw, I40E_PFINT_LNKLST0, 0);
3373
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003374 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003375 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3376 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3377 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3378
3379 wr32(hw, I40E_QINT_RQCTL(0), val);
3380
3381 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3382 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3383 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3384
3385 wr32(hw, I40E_QINT_TQCTL(0), val);
3386 i40e_flush(hw);
3387}
3388
3389/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003390 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3391 * @pf: board private structure
3392 **/
3393void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3394{
3395 struct i40e_hw *hw = &pf->hw;
3396
3397 wr32(hw, I40E_PFINT_DYN_CTL0,
3398 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3399 i40e_flush(hw);
3400}
3401
3402/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003403 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3404 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003405 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003406 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003407void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003408{
3409 struct i40e_hw *hw = &pf->hw;
3410 u32 val;
3411
3412 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003413 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003414 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3415
3416 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3417 i40e_flush(hw);
3418}
3419
3420/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003421 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3422 * @irq: interrupt number
3423 * @data: pointer to a q_vector
3424 **/
3425static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3426{
3427 struct i40e_q_vector *q_vector = data;
3428
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003429 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003430 return IRQ_HANDLED;
3431
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003432 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003433
3434 return IRQ_HANDLED;
3435}
3436
3437/**
Alan Brady96db7762016-09-14 16:24:38 -07003438 * i40e_irq_affinity_notify - Callback for affinity changes
3439 * @notify: context as to what irq was changed
3440 * @mask: the new affinity mask
3441 *
3442 * This is a callback function used by the irq_set_affinity_notifier function
3443 * so that we may register to receive changes to the irq affinity masks.
3444 **/
3445static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3446 const cpumask_t *mask)
3447{
3448 struct i40e_q_vector *q_vector =
3449 container_of(notify, struct i40e_q_vector, affinity_notify);
3450
3451 q_vector->affinity_mask = *mask;
3452}
3453
3454/**
3455 * i40e_irq_affinity_release - Callback for affinity notifier release
3456 * @ref: internal core kernel usage
3457 *
3458 * This is a callback function used by the irq_set_affinity_notifier function
3459 * to inform the current notification subscriber that they will no longer
3460 * receive notifications.
3461 **/
3462static void i40e_irq_affinity_release(struct kref *ref) {}
3463
3464/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3466 * @vsi: the VSI being configured
3467 * @basename: name for the vector
3468 *
3469 * Allocates MSI-X vectors and requests interrupts from the kernel.
3470 **/
3471static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3472{
3473 int q_vectors = vsi->num_q_vectors;
3474 struct i40e_pf *pf = vsi->back;
3475 int base = vsi->base_vector;
3476 int rx_int_idx = 0;
3477 int tx_int_idx = 0;
3478 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003479 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003480
3481 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003482 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003483
Alan Brady96db7762016-09-14 16:24:38 -07003484 irq_num = pf->msix_entries[base + vector].vector;
3485
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003486 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3488 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3489 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003490 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003491 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3492 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003493 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003494 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3495 "%s-%s-%d", basename, "tx", tx_int_idx++);
3496 } else {
3497 /* skip this unused q_vector */
3498 continue;
3499 }
Alan Brady96db7762016-09-14 16:24:38 -07003500 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003501 vsi->irq_handler,
3502 0,
3503 q_vector->name,
3504 q_vector);
3505 if (err) {
3506 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003507 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003508 goto free_queue_irqs;
3509 }
Alan Brady96db7762016-09-14 16:24:38 -07003510
3511 /* register for affinity change notifications */
3512 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3513 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3514 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003515 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003516 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003517 }
3518
Shannon Nelson63741842014-04-23 04:50:16 +00003519 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003520 return 0;
3521
3522free_queue_irqs:
3523 while (vector) {
3524 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003525 irq_num = pf->msix_entries[base + vector].vector;
3526 irq_set_affinity_notifier(irq_num, NULL);
3527 irq_set_affinity_hint(irq_num, NULL);
3528 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003529 }
3530 return err;
3531}
3532
3533/**
3534 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3535 * @vsi: the VSI being un-configured
3536 **/
3537static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3538{
3539 struct i40e_pf *pf = vsi->back;
3540 struct i40e_hw *hw = &pf->hw;
3541 int base = vsi->base_vector;
3542 int i;
3543
3544 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003545 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3546 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003547 }
3548
3549 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3550 for (i = vsi->base_vector;
3551 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3552 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3553
3554 i40e_flush(hw);
3555 for (i = 0; i < vsi->num_q_vectors; i++)
3556 synchronize_irq(pf->msix_entries[i + base].vector);
3557 } else {
3558 /* Legacy and MSI mode - this stops all interrupt handling */
3559 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3560 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3561 i40e_flush(hw);
3562 synchronize_irq(pf->pdev->irq);
3563 }
3564}
3565
3566/**
3567 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3568 * @vsi: the VSI being configured
3569 **/
3570static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3571{
3572 struct i40e_pf *pf = vsi->back;
3573 int i;
3574
3575 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003576 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003577 i40e_irq_dynamic_enable(vsi, i);
3578 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003579 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003580 }
3581
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003582 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003583 return 0;
3584}
3585
3586/**
3587 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3588 * @pf: board private structure
3589 **/
3590static void i40e_stop_misc_vector(struct i40e_pf *pf)
3591{
3592 /* Disable ICR 0 */
3593 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3594 i40e_flush(&pf->hw);
3595}
3596
3597/**
3598 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3599 * @irq: interrupt number
3600 * @data: pointer to a q_vector
3601 *
3602 * This is the handler used for all MSI/Legacy interrupts, and deals
3603 * with both queue and non-queue interrupts. This is also used in
3604 * MSIX mode to handle the non-queue interrupts.
3605 **/
3606static irqreturn_t i40e_intr(int irq, void *data)
3607{
3608 struct i40e_pf *pf = (struct i40e_pf *)data;
3609 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003610 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003611 u32 icr0, icr0_remaining;
3612 u32 val, ena_mask;
3613
3614 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003615 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003616
Shannon Nelson116a57d2013-09-28 07:13:59 +00003617 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3618 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003619 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003620
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003621 /* if interrupt but no bits showing, must be SWINT */
3622 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3623 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3624 pf->sw_int_count++;
3625
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003626 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3627 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3628 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3629 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003630 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003631 }
3632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003633 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3634 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003635 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3636 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003637
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003638 /* We do not have a way to disarm Queue causes while leaving
3639 * interrupt enabled for all other causes, ideally
3640 * interrupt should be disabled while we are in NAPI but
3641 * this is not a performance path and napi_schedule()
3642 * can deal with rescheduling.
3643 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003644 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003645 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003646 }
3647
3648 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3649 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3650 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003651 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003652 }
3653
3654 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3655 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3656 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3657 }
3658
3659 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3660 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3661 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3662 }
3663
3664 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3665 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3666 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3667 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3668 val = rd32(hw, I40E_GLGEN_RSTAT);
3669 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3670 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003671 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003673 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003674 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003675 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003677 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003678 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003679 }
3680
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003681 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3682 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3683 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003684 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3685 rd32(hw, I40E_PFHMC_ERRORINFO),
3686 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003687 }
3688
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003689 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3690 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3691
3692 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003693 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003694 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003695 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003696 }
3697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003698 /* If a critical error is pending we have no choice but to reset the
3699 * device.
3700 * Report and mask out any remaining unexpected interrupts.
3701 */
3702 icr0_remaining = icr0 & ena_mask;
3703 if (icr0_remaining) {
3704 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3705 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003706 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003707 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003708 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003709 dev_info(&pf->pdev->dev, "device will be reset\n");
3710 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3711 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712 }
3713 ena_mask &= ~icr0_remaining;
3714 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003715 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003716
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003717enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718 /* re-enable interrupt causes */
3719 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003720 if (!test_bit(__I40E_DOWN, &pf->state)) {
3721 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003722 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003723 }
3724
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003725 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003726}
3727
3728/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003729 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3730 * @tx_ring: tx ring to clean
3731 * @budget: how many cleans we're allowed
3732 *
3733 * Returns true if there's any budget left (e.g. the clean is finished)
3734 **/
3735static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3736{
3737 struct i40e_vsi *vsi = tx_ring->vsi;
3738 u16 i = tx_ring->next_to_clean;
3739 struct i40e_tx_buffer *tx_buf;
3740 struct i40e_tx_desc *tx_desc;
3741
3742 tx_buf = &tx_ring->tx_bi[i];
3743 tx_desc = I40E_TX_DESC(tx_ring, i);
3744 i -= tx_ring->count;
3745
3746 do {
3747 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3748
3749 /* if next_to_watch is not set then there is no work pending */
3750 if (!eop_desc)
3751 break;
3752
3753 /* prevent any other reads prior to eop_desc */
3754 read_barrier_depends();
3755
3756 /* if the descriptor isn't done, no work yet to do */
3757 if (!(eop_desc->cmd_type_offset_bsz &
3758 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3759 break;
3760
3761 /* clear next_to_watch to prevent false hangs */
3762 tx_buf->next_to_watch = NULL;
3763
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003764 tx_desc->buffer_addr = 0;
3765 tx_desc->cmd_type_offset_bsz = 0;
3766 /* move past filter desc */
3767 tx_buf++;
3768 tx_desc++;
3769 i++;
3770 if (unlikely(!i)) {
3771 i -= tx_ring->count;
3772 tx_buf = tx_ring->tx_bi;
3773 tx_desc = I40E_TX_DESC(tx_ring, 0);
3774 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003775 /* unmap skb header data */
3776 dma_unmap_single(tx_ring->dev,
3777 dma_unmap_addr(tx_buf, dma),
3778 dma_unmap_len(tx_buf, len),
3779 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003780 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3781 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003782
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003783 tx_buf->raw_buf = NULL;
3784 tx_buf->tx_flags = 0;
3785 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003786 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003787 tx_desc->buffer_addr = 0;
3788 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003789
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003790 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003791 tx_buf++;
3792 tx_desc++;
3793 i++;
3794 if (unlikely(!i)) {
3795 i -= tx_ring->count;
3796 tx_buf = tx_ring->tx_bi;
3797 tx_desc = I40E_TX_DESC(tx_ring, 0);
3798 }
3799
3800 /* update budget accounting */
3801 budget--;
3802 } while (likely(budget));
3803
3804 i += tx_ring->count;
3805 tx_ring->next_to_clean = i;
3806
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003807 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003808 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003809
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003810 return budget > 0;
3811}
3812
3813/**
3814 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3815 * @irq: interrupt number
3816 * @data: pointer to a q_vector
3817 **/
3818static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3819{
3820 struct i40e_q_vector *q_vector = data;
3821 struct i40e_vsi *vsi;
3822
3823 if (!q_vector->tx.ring)
3824 return IRQ_HANDLED;
3825
3826 vsi = q_vector->tx.ring->vsi;
3827 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3828
3829 return IRQ_HANDLED;
3830}
3831
3832/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003833 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003834 * @vsi: the VSI being configured
3835 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003836 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003837 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003838static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003839{
Alexander Duyck493fb302013-09-28 07:01:44 +00003840 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003841 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3842 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003843
3844 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003845 tx_ring->next = q_vector->tx.ring;
3846 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003847 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003848
3849 rx_ring->q_vector = q_vector;
3850 rx_ring->next = q_vector->rx.ring;
3851 q_vector->rx.ring = rx_ring;
3852 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003853}
3854
3855/**
3856 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3857 * @vsi: the VSI being configured
3858 *
3859 * This function maps descriptor rings to the queue-specific vectors
3860 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3861 * one vector per queue pair, but on a constrained vector budget, we
3862 * group the queue pairs as "efficiently" as possible.
3863 **/
3864static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3865{
3866 int qp_remaining = vsi->num_queue_pairs;
3867 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003868 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003869 int v_start = 0;
3870 int qp_idx = 0;
3871
3872 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3873 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003874 * It is also important to go through all the vectors available to be
3875 * sure that if we don't use all the vectors, that the remaining vectors
3876 * are cleared. This is especially important when decreasing the
3877 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003878 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003879 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003880 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3881
3882 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3883
3884 q_vector->num_ringpairs = num_ringpairs;
3885
3886 q_vector->rx.count = 0;
3887 q_vector->tx.count = 0;
3888 q_vector->rx.ring = NULL;
3889 q_vector->tx.ring = NULL;
3890
3891 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003892 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003893 qp_idx++;
3894 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003895 }
3896 }
3897}
3898
3899/**
3900 * i40e_vsi_request_irq - Request IRQ from the OS
3901 * @vsi: the VSI being configured
3902 * @basename: name for the vector
3903 **/
3904static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3905{
3906 struct i40e_pf *pf = vsi->back;
3907 int err;
3908
3909 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3910 err = i40e_vsi_request_irq_msix(vsi, basename);
3911 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3912 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003913 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003914 else
3915 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003916 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003917
3918 if (err)
3919 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3920
3921 return err;
3922}
3923
3924#ifdef CONFIG_NET_POLL_CONTROLLER
3925/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003926 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003927 * @netdev: network interface device structure
3928 *
3929 * This is used by netconsole to send skbs without having to re-enable
3930 * interrupts. It's not called while the normal interrupt routine is executing.
3931 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003932#ifdef I40E_FCOE
3933void i40e_netpoll(struct net_device *netdev)
3934#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003935static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003936#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003937{
3938 struct i40e_netdev_priv *np = netdev_priv(netdev);
3939 struct i40e_vsi *vsi = np->vsi;
3940 struct i40e_pf *pf = vsi->back;
3941 int i;
3942
3943 /* if interface is down do nothing */
3944 if (test_bit(__I40E_DOWN, &vsi->state))
3945 return;
3946
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3948 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003949 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003950 } else {
3951 i40e_intr(pf->pdev->irq, netdev);
3952 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003953}
3954#endif
3955
3956/**
Neerav Parikh23527302014-06-03 23:50:15 +00003957 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3958 * @pf: the PF being configured
3959 * @pf_q: the PF queue
3960 * @enable: enable or disable state of the queue
3961 *
3962 * This routine will wait for the given Tx queue of the PF to reach the
3963 * enabled or disabled state.
3964 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3965 * multiple retries; else will return 0 in case of success.
3966 **/
3967static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3968{
3969 int i;
3970 u32 tx_reg;
3971
3972 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3973 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3974 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3975 break;
3976
Neerav Parikhf98a2002014-09-13 07:40:44 +00003977 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003978 }
3979 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3980 return -ETIMEDOUT;
3981
3982 return 0;
3983}
3984
3985/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003986 * i40e_vsi_control_tx - Start or stop a VSI's rings
3987 * @vsi: the VSI being configured
3988 * @enable: start or stop the rings
3989 **/
3990static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3991{
3992 struct i40e_pf *pf = vsi->back;
3993 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003994 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003995 u32 tx_reg;
3996
3997 pf_q = vsi->base_queue;
3998 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003999
4000 /* warn the TX unit of coming changes */
4001 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4002 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00004003 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00004004
Mitch Williams6c5ef622014-02-20 19:29:16 -08004005 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004006 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004007 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4008 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4009 break;
4010 usleep_range(1000, 2000);
4011 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00004012 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00004013 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00004014 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004015
4016 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004017 if (enable) {
4018 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08004019 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004020 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004021 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004022 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004023
4024 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004025 /* No waiting for the Tx queue to disable */
4026 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4027 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004028
4029 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004030 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4031 if (ret) {
4032 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004033 "VSI seid %d Tx ring %d %sable timeout\n",
4034 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004035 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004036 }
4037 }
4038
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004039 if (hw->revision_id == 0)
4040 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00004041 return ret;
4042}
4043
4044/**
4045 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4046 * @pf: the PF being configured
4047 * @pf_q: the PF queue
4048 * @enable: enable or disable state of the queue
4049 *
4050 * This routine will wait for the given Rx queue of the PF to reach the
4051 * enabled or disabled state.
4052 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4053 * multiple retries; else will return 0 in case of success.
4054 **/
4055static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4056{
4057 int i;
4058 u32 rx_reg;
4059
4060 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4061 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4062 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4063 break;
4064
Neerav Parikhf98a2002014-09-13 07:40:44 +00004065 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004066 }
4067 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4068 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004069
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004070 return 0;
4071}
4072
4073/**
4074 * i40e_vsi_control_rx - Start or stop a VSI's rings
4075 * @vsi: the VSI being configured
4076 * @enable: start or stop the rings
4077 **/
4078static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4079{
4080 struct i40e_pf *pf = vsi->back;
4081 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004082 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004083 u32 rx_reg;
4084
4085 pf_q = vsi->base_queue;
4086 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004087 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004088 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004089 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4090 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4091 break;
4092 usleep_range(1000, 2000);
4093 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004094
Catherine Sullivan7c122002014-03-14 07:32:29 +00004095 /* Skip if the queue is already in the requested state */
4096 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4097 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004098
4099 /* turn on/off the queue */
4100 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004101 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004102 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004103 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004104 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004105 /* No waiting for the Tx queue to disable */
4106 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4107 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004108
4109 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004110 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4111 if (ret) {
4112 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004113 "VSI seid %d Rx ring %d %sable timeout\n",
4114 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004115 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004116 }
4117 }
4118
Neerav Parikh23527302014-06-03 23:50:15 +00004119 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004120}
4121
4122/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004123 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004124 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004125 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004126int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004127{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004128 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004129
4130 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004131 ret = i40e_vsi_control_rx(vsi, true);
4132 if (ret)
4133 return ret;
4134 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135
4136 return ret;
4137}
4138
4139/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004140 * i40e_vsi_stop_rings - Stop a VSI's rings
4141 * @vsi: the VSI being configured
4142 **/
4143void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4144{
4145 /* do rx first for enable and last for disable
4146 * Ignore return value, we need to shutdown whatever we can
4147 */
4148 i40e_vsi_control_tx(vsi, false);
4149 i40e_vsi_control_rx(vsi, false);
4150}
4151
4152/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004153 * i40e_vsi_free_irq - Free the irq association with the OS
4154 * @vsi: the VSI being configured
4155 **/
4156static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4157{
4158 struct i40e_pf *pf = vsi->back;
4159 struct i40e_hw *hw = &pf->hw;
4160 int base = vsi->base_vector;
4161 u32 val, qp;
4162 int i;
4163
4164 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4165 if (!vsi->q_vectors)
4166 return;
4167
Shannon Nelson63741842014-04-23 04:50:16 +00004168 if (!vsi->irqs_ready)
4169 return;
4170
4171 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004172 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004173 int irq_num;
4174 u16 vector;
4175
4176 vector = i + base;
4177 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004178
4179 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004180 if (!vsi->q_vectors[i] ||
4181 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004182 continue;
4183
Alan Brady96db7762016-09-14 16:24:38 -07004184 /* clear the affinity notifier in the IRQ descriptor */
4185 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004186 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004187 irq_set_affinity_hint(irq_num, NULL);
4188 synchronize_irq(irq_num);
4189 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004190
4191 /* Tear down the interrupt queue link list
4192 *
4193 * We know that they come in pairs and always
4194 * the Rx first, then the Tx. To clear the
4195 * link list, stick the EOL value into the
4196 * next_q field of the registers.
4197 */
4198 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4199 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4200 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4201 val |= I40E_QUEUE_END_OF_LIST
4202 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4203 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4204
4205 while (qp != I40E_QUEUE_END_OF_LIST) {
4206 u32 next;
4207
4208 val = rd32(hw, I40E_QINT_RQCTL(qp));
4209
4210 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4211 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4212 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4213 I40E_QINT_RQCTL_INTEVENT_MASK);
4214
4215 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4216 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4217
4218 wr32(hw, I40E_QINT_RQCTL(qp), val);
4219
4220 val = rd32(hw, I40E_QINT_TQCTL(qp));
4221
4222 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4223 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4224
4225 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4226 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4227 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4228 I40E_QINT_TQCTL_INTEVENT_MASK);
4229
4230 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4231 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4232
4233 wr32(hw, I40E_QINT_TQCTL(qp), val);
4234 qp = next;
4235 }
4236 }
4237 } else {
4238 free_irq(pf->pdev->irq, pf);
4239
4240 val = rd32(hw, I40E_PFINT_LNKLST0);
4241 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4242 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4243 val |= I40E_QUEUE_END_OF_LIST
4244 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4245 wr32(hw, I40E_PFINT_LNKLST0, val);
4246
4247 val = rd32(hw, I40E_QINT_RQCTL(qp));
4248 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4249 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4250 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4251 I40E_QINT_RQCTL_INTEVENT_MASK);
4252
4253 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4254 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4255
4256 wr32(hw, I40E_QINT_RQCTL(qp), val);
4257
4258 val = rd32(hw, I40E_QINT_TQCTL(qp));
4259
4260 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4261 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4262 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4263 I40E_QINT_TQCTL_INTEVENT_MASK);
4264
4265 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4266 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4267
4268 wr32(hw, I40E_QINT_TQCTL(qp), val);
4269 }
4270}
4271
4272/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004273 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4274 * @vsi: the VSI being configured
4275 * @v_idx: Index of vector to be freed
4276 *
4277 * This function frees the memory allocated to the q_vector. In addition if
4278 * NAPI is enabled it will delete any references to the NAPI struct prior
4279 * to freeing the q_vector.
4280 **/
4281static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4282{
4283 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004284 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004285
4286 if (!q_vector)
4287 return;
4288
4289 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004290 i40e_for_each_ring(ring, q_vector->tx)
4291 ring->q_vector = NULL;
4292
4293 i40e_for_each_ring(ring, q_vector->rx)
4294 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004295
4296 /* only VSI w/ an associated netdev is set up w/ NAPI */
4297 if (vsi->netdev)
4298 netif_napi_del(&q_vector->napi);
4299
4300 vsi->q_vectors[v_idx] = NULL;
4301
4302 kfree_rcu(q_vector, rcu);
4303}
4304
4305/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004306 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4307 * @vsi: the VSI being un-configured
4308 *
4309 * This frees the memory allocated to the q_vectors and
4310 * deletes references to the NAPI struct.
4311 **/
4312static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4313{
4314 int v_idx;
4315
Alexander Duyck493fb302013-09-28 07:01:44 +00004316 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4317 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004318}
4319
4320/**
4321 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4322 * @pf: board private structure
4323 **/
4324static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4325{
4326 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4327 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4328 pci_disable_msix(pf->pdev);
4329 kfree(pf->msix_entries);
4330 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004331 kfree(pf->irq_pile);
4332 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004333 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4334 pci_disable_msi(pf->pdev);
4335 }
4336 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4337}
4338
4339/**
4340 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4341 * @pf: board private structure
4342 *
4343 * We go through and clear interrupt specific resources and reset the structure
4344 * to pre-load conditions
4345 **/
4346static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4347{
4348 int i;
4349
Shannon Nelsone1477582015-02-21 06:44:33 +00004350 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004351 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004352 synchronize_irq(pf->msix_entries[0].vector);
4353 free_irq(pf->msix_entries[0].vector, pf);
4354 }
4355
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004356 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4357 I40E_IWARP_IRQ_PILE_ID);
4358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004359 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004360 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004361 if (pf->vsi[i])
4362 i40e_vsi_free_q_vectors(pf->vsi[i]);
4363 i40e_reset_interrupt_capability(pf);
4364}
4365
4366/**
4367 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4368 * @vsi: the VSI being configured
4369 **/
4370static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4371{
4372 int q_idx;
4373
4374 if (!vsi->netdev)
4375 return;
4376
4377 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004378 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004379}
4380
4381/**
4382 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4383 * @vsi: the VSI being configured
4384 **/
4385static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4386{
4387 int q_idx;
4388
4389 if (!vsi->netdev)
4390 return;
4391
4392 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004393 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004394}
4395
4396/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004397 * i40e_vsi_close - Shut down a VSI
4398 * @vsi: the vsi to be quelled
4399 **/
4400static void i40e_vsi_close(struct i40e_vsi *vsi)
4401{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004402 bool reset = false;
4403
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004404 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4405 i40e_down(vsi);
4406 i40e_vsi_free_irq(vsi);
4407 i40e_vsi_free_tx_resources(vsi);
4408 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004409 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004410 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4411 reset = true;
4412 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004413}
4414
4415/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416 * i40e_quiesce_vsi - Pause a given VSI
4417 * @vsi: the VSI being paused
4418 **/
4419static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4420{
4421 if (test_bit(__I40E_DOWN, &vsi->state))
4422 return;
4423
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004424 /* No need to disable FCoE VSI when Tx suspended */
4425 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4426 vsi->type == I40E_VSI_FCOE) {
4427 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004428 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004429 return;
4430 }
4431
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004432 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004433 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004434 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004435 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004436 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004437}
4438
4439/**
4440 * i40e_unquiesce_vsi - Resume a given VSI
4441 * @vsi: the VSI being resumed
4442 **/
4443static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4444{
4445 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4446 return;
4447
4448 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4449 if (vsi->netdev && netif_running(vsi->netdev))
4450 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4451 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004452 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004453}
4454
4455/**
4456 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4457 * @pf: the PF
4458 **/
4459static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4460{
4461 int v;
4462
Mitch Williams505682c2014-05-20 08:01:37 +00004463 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004464 if (pf->vsi[v])
4465 i40e_quiesce_vsi(pf->vsi[v]);
4466 }
4467}
4468
4469/**
4470 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4471 * @pf: the PF
4472 **/
4473static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4474{
4475 int v;
4476
Mitch Williams505682c2014-05-20 08:01:37 +00004477 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004478 if (pf->vsi[v])
4479 i40e_unquiesce_vsi(pf->vsi[v]);
4480 }
4481}
4482
Neerav Parikh69129dc2014-11-12 00:18:46 +00004483#ifdef CONFIG_I40E_DCB
4484/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004485 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004486 * @vsi: the VSI being configured
4487 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004488 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004489 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004490static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004491{
4492 struct i40e_pf *pf = vsi->back;
4493 int i, pf_q, ret;
4494
4495 pf_q = vsi->base_queue;
4496 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4497 /* Check and wait for the disable status of the queue */
4498 ret = i40e_pf_txq_wait(pf, pf_q, false);
4499 if (ret) {
4500 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004501 "VSI seid %d Tx ring %d disable timeout\n",
4502 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004503 return ret;
4504 }
4505 }
4506
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004507 pf_q = vsi->base_queue;
4508 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4509 /* Check and wait for the disable status of the queue */
4510 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4511 if (ret) {
4512 dev_info(&pf->pdev->dev,
4513 "VSI seid %d Rx ring %d disable timeout\n",
4514 vsi->seid, pf_q);
4515 return ret;
4516 }
4517 }
4518
Neerav Parikh69129dc2014-11-12 00:18:46 +00004519 return 0;
4520}
4521
4522/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004523 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004524 * @pf: the PF
4525 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004526 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004527 * VSIs that are managed by this PF.
4528 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004529static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004530{
4531 int v, ret = 0;
4532
4533 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004534 /* No need to wait for FCoE VSI queues */
4535 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004536 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004537 if (ret)
4538 break;
4539 }
4540 }
4541
4542 return ret;
4543}
4544
4545#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004546
4547/**
4548 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4549 * @q_idx: TX queue number
4550 * @vsi: Pointer to VSI struct
4551 *
4552 * This function checks specified queue for given VSI. Detects hung condition.
4553 * Sets hung bit since it is two step process. Before next run of service task
4554 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4555 * hung condition remain unchanged and during subsequent run, this function
4556 * issues SW interrupt to recover from hung condition.
4557 **/
4558static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4559{
4560 struct i40e_ring *tx_ring = NULL;
4561 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004562 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004563 int i;
4564
4565 pf = vsi->back;
4566
4567 /* now that we have an index, find the tx_ring struct */
4568 for (i = 0; i < vsi->num_queue_pairs; i++) {
4569 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4570 if (q_idx == vsi->tx_rings[i]->queue_index) {
4571 tx_ring = vsi->tx_rings[i];
4572 break;
4573 }
4574 }
4575 }
4576
4577 if (!tx_ring)
4578 return;
4579
4580 /* Read interrupt register */
4581 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4582 val = rd32(&pf->hw,
4583 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4584 tx_ring->vsi->base_vector - 1));
4585 else
4586 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4587
4588 head = i40e_get_head(tx_ring);
4589
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004590 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004591
Kiran Patil9c6c1252015-11-06 15:26:02 -08004592 /* HW is done executing descriptors, updated HEAD write back,
4593 * but SW hasn't processed those descriptors. If interrupt is
4594 * not generated from this point ON, it could result into
4595 * dev_watchdog detecting timeout on those netdev_queue,
4596 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004597 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004598 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004599 /* NAPI Poll didn't run and clear since it was set */
4600 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4601 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004602 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",
4603 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004604 tx_ring->next_to_clean, head,
4605 tx_ring->next_to_use,
4606 readl(tx_ring->tail));
4607 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4608 vsi->seid, q_idx, val);
4609 i40e_force_wb(vsi, tx_ring->q_vector);
4610 } else {
4611 /* First Chance - detected possible hung */
4612 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4613 &tx_ring->q_vector->hung_detected);
4614 }
4615 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004616
4617 /* This is the case where we have interrupts missing,
4618 * so the tx_pending in HW will most likely be 0, but we
4619 * will have tx_pending in SW since the WB happened but the
4620 * interrupt got lost.
4621 */
4622 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4623 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4624 if (napi_reschedule(&tx_ring->q_vector->napi))
4625 tx_ring->tx_stats.tx_lost_interrupt++;
4626 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004627}
4628
4629/**
4630 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4631 * @pf: pointer to PF struct
4632 *
4633 * LAN VSI has netdev and netdev has TX queues. This function is to check
4634 * each of those TX queues if they are hung, trigger recovery by issuing
4635 * SW interrupt.
4636 **/
4637static void i40e_detect_recover_hung(struct i40e_pf *pf)
4638{
4639 struct net_device *netdev;
4640 struct i40e_vsi *vsi;
4641 int i;
4642
4643 /* Only for LAN VSI */
4644 vsi = pf->vsi[pf->lan_vsi];
4645
4646 if (!vsi)
4647 return;
4648
4649 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4650 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4651 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4652 return;
4653
4654 /* Make sure type is MAIN VSI */
4655 if (vsi->type != I40E_VSI_MAIN)
4656 return;
4657
4658 netdev = vsi->netdev;
4659 if (!netdev)
4660 return;
4661
4662 /* Bail out if netif_carrier is not OK */
4663 if (!netif_carrier_ok(netdev))
4664 return;
4665
4666 /* Go thru' TX queues for netdev */
4667 for (i = 0; i < netdev->num_tx_queues; i++) {
4668 struct netdev_queue *q;
4669
4670 q = netdev_get_tx_queue(netdev, i);
4671 if (q)
4672 i40e_detect_recover_hung_queue(i, vsi);
4673 }
4674}
4675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004676/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004677 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004678 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004679 *
4680 * Get TC map for ISCSI PF type that will include iSCSI TC
4681 * and LAN TC.
4682 **/
4683static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4684{
4685 struct i40e_dcb_app_priority_table app;
4686 struct i40e_hw *hw = &pf->hw;
4687 u8 enabled_tc = 1; /* TC0 is always enabled */
4688 u8 tc, i;
4689 /* Get the iSCSI APP TLV */
4690 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4691
4692 for (i = 0; i < dcbcfg->numapps; i++) {
4693 app = dcbcfg->app[i];
4694 if (app.selector == I40E_APP_SEL_TCPIP &&
4695 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4696 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004697 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004698 break;
4699 }
4700 }
4701
4702 return enabled_tc;
4703}
4704
4705/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004706 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4707 * @dcbcfg: the corresponding DCBx configuration structure
4708 *
4709 * Return the number of TCs from given DCBx configuration
4710 **/
4711static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4712{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004713 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004714 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004715 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004716
4717 /* Scan the ETS Config Priority Table to find
4718 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004719 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004720 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004721 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4722 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4723
4724 /* Now scan the bitmask to check for
4725 * contiguous TCs starting with TC0
4726 */
4727 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4728 if (num_tc & BIT(i)) {
4729 if (!tc_unused) {
4730 ret++;
4731 } else {
4732 pr_err("Non-contiguous TC - Disabling DCB\n");
4733 return 1;
4734 }
4735 } else {
4736 tc_unused = 1;
4737 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004738 }
4739
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004740 /* There is always at least TC0 */
4741 if (!ret)
4742 ret = 1;
4743
4744 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004745}
4746
4747/**
4748 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4749 * @dcbcfg: the corresponding DCBx configuration structure
4750 *
4751 * Query the current DCB configuration and return the number of
4752 * traffic classes enabled from the given DCBX config
4753 **/
4754static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4755{
4756 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4757 u8 enabled_tc = 1;
4758 u8 i;
4759
4760 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004761 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004762
4763 return enabled_tc;
4764}
4765
4766/**
4767 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4768 * @pf: PF being queried
4769 *
4770 * Return number of traffic classes enabled for the given PF
4771 **/
4772static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4773{
4774 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004775 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004776 u8 num_tc = 0;
4777 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4778
4779 /* If DCB is not enabled then always in single TC */
4780 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4781 return 1;
4782
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004783 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004784 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4785 return i40e_dcb_get_num_tc(dcbcfg);
4786
4787 /* MFP mode return count of enabled TCs for this PF */
4788 if (pf->hw.func_caps.iscsi)
4789 enabled_tc = i40e_get_iscsi_tc_map(pf);
4790 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004791 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004792
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004793 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004794 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004795 num_tc++;
4796 }
4797 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004798}
4799
4800/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004801 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4802 * @pf: PF being queried
4803 *
4804 * Return a bitmap for enabled traffic classes for this PF.
4805 **/
4806static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4807{
4808 /* If DCB is not enabled for this PF then just return default TC */
4809 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004810 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004812 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004813 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4814 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4815
Neerav Parikhfc51de92015-02-24 06:58:53 +00004816 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004817 if (pf->hw.func_caps.iscsi)
4818 return i40e_get_iscsi_tc_map(pf);
4819 else
David Ertmanea6acb72016-09-20 07:10:50 -07004820 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004821}
4822
4823/**
4824 * i40e_vsi_get_bw_info - Query VSI BW Information
4825 * @vsi: the VSI being queried
4826 *
4827 * Returns 0 on success, negative value on failure
4828 **/
4829static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4830{
4831 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4832 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4833 struct i40e_pf *pf = vsi->back;
4834 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004835 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004836 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004837 int i;
4838
4839 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004840 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4841 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004842 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004843 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4844 i40e_stat_str(&pf->hw, ret),
4845 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004846 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004847 }
4848
4849 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004850 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4851 NULL);
4852 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004853 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004854 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4855 i40e_stat_str(&pf->hw, ret),
4856 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004857 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004858 }
4859
4860 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4861 dev_info(&pf->pdev->dev,
4862 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4863 bw_config.tc_valid_bits,
4864 bw_ets_config.tc_valid_bits);
4865 /* Still continuing */
4866 }
4867
4868 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4869 vsi->bw_max_quanta = bw_config.max_bw;
4870 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4871 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4872 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4873 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4874 vsi->bw_ets_limit_credits[i] =
4875 le16_to_cpu(bw_ets_config.credits[i]);
4876 /* 3 bits out of 4 for each TC */
4877 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4878 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004879
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004880 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004881}
4882
4883/**
4884 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4885 * @vsi: the VSI being configured
4886 * @enabled_tc: TC bitmap
4887 * @bw_credits: BW shared credits per TC
4888 *
4889 * Returns 0 on success, negative value on failure
4890 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004891static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004892 u8 *bw_share)
4893{
4894 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004895 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004896 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004897
4898 bw_data.tc_valid_bits = enabled_tc;
4899 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4900 bw_data.tc_bw_credits[i] = bw_share[i];
4901
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004902 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4903 NULL);
4904 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004905 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004906 "AQ command Config VSI BW allocation per TC failed = %d\n",
4907 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004908 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909 }
4910
4911 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4912 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4913
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004914 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004915}
4916
4917/**
4918 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4919 * @vsi: the VSI being configured
4920 * @enabled_tc: TC map to be enabled
4921 *
4922 **/
4923static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4924{
4925 struct net_device *netdev = vsi->netdev;
4926 struct i40e_pf *pf = vsi->back;
4927 struct i40e_hw *hw = &pf->hw;
4928 u8 netdev_tc = 0;
4929 int i;
4930 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4931
4932 if (!netdev)
4933 return;
4934
4935 if (!enabled_tc) {
4936 netdev_reset_tc(netdev);
4937 return;
4938 }
4939
4940 /* Set up actual enabled TCs on the VSI */
4941 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4942 return;
4943
4944 /* set per TC queues for the VSI */
4945 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4946 /* Only set TC queues for enabled tcs
4947 *
4948 * e.g. For a VSI that has TC0 and TC3 enabled the
4949 * enabled_tc bitmap would be 0x00001001; the driver
4950 * will set the numtc for netdev as 2 that will be
4951 * referenced by the netdev layer as TC 0 and 1.
4952 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004953 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004954 netdev_set_tc_queue(netdev,
4955 vsi->tc_config.tc_info[i].netdev_tc,
4956 vsi->tc_config.tc_info[i].qcount,
4957 vsi->tc_config.tc_info[i].qoffset);
4958 }
4959
4960 /* Assign UP2TC map for the VSI */
4961 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4962 /* Get the actual TC# for the UP */
4963 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4964 /* Get the mapped netdev TC# for the UP */
4965 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4966 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4967 }
4968}
4969
4970/**
4971 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4972 * @vsi: the VSI being configured
4973 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4974 **/
4975static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4976 struct i40e_vsi_context *ctxt)
4977{
4978 /* copy just the sections touched not the entire info
4979 * since not all sections are valid as returned by
4980 * update vsi params
4981 */
4982 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4983 memcpy(&vsi->info.queue_mapping,
4984 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4985 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4986 sizeof(vsi->info.tc_mapping));
4987}
4988
4989/**
4990 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4991 * @vsi: VSI to be configured
4992 * @enabled_tc: TC bitmap
4993 *
4994 * This configures a particular VSI for TCs that are mapped to the
4995 * given TC bitmap. It uses default bandwidth share for TCs across
4996 * VSIs to configure TC for a particular VSI.
4997 *
4998 * NOTE:
4999 * It is expected that the VSI queues have been quisced before calling
5000 * this function.
5001 **/
5002static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5003{
5004 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5005 struct i40e_vsi_context ctxt;
5006 int ret = 0;
5007 int i;
5008
5009 /* Check if enabled_tc is same as existing or new TCs */
5010 if (vsi->tc_config.enabled_tc == enabled_tc)
5011 return ret;
5012
5013 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5014 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005015 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005016 bw_share[i] = 1;
5017 }
5018
5019 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5020 if (ret) {
5021 dev_info(&vsi->back->pdev->dev,
5022 "Failed configuring TC map %d for VSI %d\n",
5023 enabled_tc, vsi->seid);
5024 goto out;
5025 }
5026
5027 /* Update Queue Pairs Mapping for currently enabled UPs */
5028 ctxt.seid = vsi->seid;
5029 ctxt.pf_num = vsi->back->hw.pf_id;
5030 ctxt.vf_num = 0;
5031 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005032 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005033 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5034
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005035 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5036 ctxt.info.valid_sections |=
5037 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5038 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5039 }
5040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005041 /* Update the VSI after updating the VSI queue-mapping information */
5042 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5043 if (ret) {
5044 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005045 "Update vsi tc config failed, err %s aq_err %s\n",
5046 i40e_stat_str(&vsi->back->hw, ret),
5047 i40e_aq_str(&vsi->back->hw,
5048 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005049 goto out;
5050 }
5051 /* update the local VSI info with updated queue map */
5052 i40e_vsi_update_queue_map(vsi, &ctxt);
5053 vsi->info.valid_sections = 0;
5054
5055 /* Update current VSI BW information */
5056 ret = i40e_vsi_get_bw_info(vsi);
5057 if (ret) {
5058 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005059 "Failed updating vsi bw info, err %s aq_err %s\n",
5060 i40e_stat_str(&vsi->back->hw, ret),
5061 i40e_aq_str(&vsi->back->hw,
5062 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005063 goto out;
5064 }
5065
5066 /* Update the netdev TC setup */
5067 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5068out:
5069 return ret;
5070}
5071
5072/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005073 * i40e_veb_config_tc - Configure TCs for given VEB
5074 * @veb: given VEB
5075 * @enabled_tc: TC bitmap
5076 *
5077 * Configures given TC bitmap for VEB (switching) element
5078 **/
5079int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5080{
5081 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5082 struct i40e_pf *pf = veb->pf;
5083 int ret = 0;
5084 int i;
5085
5086 /* No TCs or already enabled TCs just return */
5087 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5088 return ret;
5089
5090 bw_data.tc_valid_bits = enabled_tc;
5091 /* bw_data.absolute_credits is not set (relative) */
5092
5093 /* Enable ETS TCs with equal BW Share for now */
5094 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005095 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005096 bw_data.tc_bw_share_credits[i] = 1;
5097 }
5098
5099 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5100 &bw_data, NULL);
5101 if (ret) {
5102 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005103 "VEB bw config failed, err %s aq_err %s\n",
5104 i40e_stat_str(&pf->hw, ret),
5105 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005106 goto out;
5107 }
5108
5109 /* Update the BW information */
5110 ret = i40e_veb_get_bw_info(veb);
5111 if (ret) {
5112 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005113 "Failed getting veb bw config, err %s aq_err %s\n",
5114 i40e_stat_str(&pf->hw, ret),
5115 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005116 }
5117
5118out:
5119 return ret;
5120}
5121
5122#ifdef CONFIG_I40E_DCB
5123/**
5124 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5125 * @pf: PF struct
5126 *
5127 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5128 * the caller would've quiesce all the VSIs before calling
5129 * this function
5130 **/
5131static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5132{
5133 u8 tc_map = 0;
5134 int ret;
5135 u8 v;
5136
5137 /* Enable the TCs available on PF to all VEBs */
5138 tc_map = i40e_pf_get_tc_map(pf);
5139 for (v = 0; v < I40E_MAX_VEB; v++) {
5140 if (!pf->veb[v])
5141 continue;
5142 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5143 if (ret) {
5144 dev_info(&pf->pdev->dev,
5145 "Failed configuring TC for VEB seid=%d\n",
5146 pf->veb[v]->seid);
5147 /* Will try to configure as many components */
5148 }
5149 }
5150
5151 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005152 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005153 if (!pf->vsi[v])
5154 continue;
5155
5156 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005157#ifdef I40E_FCOE
5158 * - For FCoE VSI only enable the TC configured
5159 * as per the APP TLV
5160#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005161 * - For all others keep them at TC0 for now
5162 */
5163 if (v == pf->lan_vsi)
5164 tc_map = i40e_pf_get_tc_map(pf);
5165 else
David Ertmanea6acb72016-09-20 07:10:50 -07005166 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005167#ifdef I40E_FCOE
5168 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5169 tc_map = i40e_get_fcoe_tc_map(pf);
5170#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005171
5172 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5173 if (ret) {
5174 dev_info(&pf->pdev->dev,
5175 "Failed configuring TC for VSI seid=%d\n",
5176 pf->vsi[v]->seid);
5177 /* Will try to configure as many components */
5178 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005179 /* Re-configure VSI vectors based on updated TC map */
5180 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005181 if (pf->vsi[v]->netdev)
5182 i40e_dcbnl_set_all(pf->vsi[v]);
5183 }
5184 }
5185}
5186
5187/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005188 * i40e_resume_port_tx - Resume port Tx
5189 * @pf: PF struct
5190 *
5191 * Resume a port's Tx and issue a PF reset in case of failure to
5192 * resume.
5193 **/
5194static int i40e_resume_port_tx(struct i40e_pf *pf)
5195{
5196 struct i40e_hw *hw = &pf->hw;
5197 int ret;
5198
5199 ret = i40e_aq_resume_port_tx(hw, NULL);
5200 if (ret) {
5201 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005202 "Resume Port Tx failed, err %s aq_err %s\n",
5203 i40e_stat_str(&pf->hw, ret),
5204 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005205 /* Schedule PF reset to recover */
5206 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5207 i40e_service_event_schedule(pf);
5208 }
5209
5210 return ret;
5211}
5212
5213/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005214 * i40e_init_pf_dcb - Initialize DCB configuration
5215 * @pf: PF being configured
5216 *
5217 * Query the current DCB configuration and cache it
5218 * in the hardware structure
5219 **/
5220static int i40e_init_pf_dcb(struct i40e_pf *pf)
5221{
5222 struct i40e_hw *hw = &pf->hw;
5223 int err = 0;
5224
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005225 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005226 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005227 goto out;
5228
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005229 /* Get the initial DCB configuration */
5230 err = i40e_init_dcb(hw);
5231 if (!err) {
5232 /* Device/Function is not DCBX capable */
5233 if ((!hw->func_caps.dcb) ||
5234 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5235 dev_info(&pf->pdev->dev,
5236 "DCBX offload is not supported or is disabled for this PF.\n");
5237
5238 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5239 goto out;
5240
5241 } else {
5242 /* When status is not DISABLED then DCBX in FW */
5243 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5244 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005245
5246 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005247 /* Enable DCB tagging only when more than one TC
5248 * or explicitly disable if only one TC
5249 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005250 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5251 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005252 else
5253 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005254 dev_dbg(&pf->pdev->dev,
5255 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005256 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005257 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005258 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005259 "Query for DCB configuration failed, err %s aq_err %s\n",
5260 i40e_stat_str(&pf->hw, err),
5261 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005262 }
5263
5264out:
5265 return err;
5266}
5267#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005268#define SPEED_SIZE 14
5269#define FC_SIZE 8
5270/**
5271 * i40e_print_link_message - print link up or down
5272 * @vsi: the VSI for which link needs a message
5273 */
Matt Jaredc156f852015-08-27 11:42:39 -04005274void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005275{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005276 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005277 char *speed = "Unknown";
5278 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005279
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005280 new_speed = vsi->back->hw.phy.link_info.link_speed;
5281
5282 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005283 return;
5284 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005285 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005286 if (!isup) {
5287 netdev_info(vsi->netdev, "NIC Link is Down\n");
5288 return;
5289 }
5290
Greg Rose148c2d82014-12-11 07:06:27 +00005291 /* Warn user if link speed on NPAR enabled partition is not at
5292 * least 10GB
5293 */
5294 if (vsi->back->hw.func_caps.npar_enable &&
5295 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5296 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5297 netdev_warn(vsi->netdev,
5298 "The partition detected link speed that is less than 10Gbps\n");
5299
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005300 switch (vsi->back->hw.phy.link_info.link_speed) {
5301 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005302 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005303 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005304 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005305 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005306 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005307 case I40E_LINK_SPEED_25GB:
5308 speed = "25 G";
5309 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005310 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005311 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005312 break;
5313 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005314 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005315 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005316 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005317 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005318 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005319 default:
5320 break;
5321 }
5322
5323 switch (vsi->back->hw.fc.current_mode) {
5324 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005325 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005326 break;
5327 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005328 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005329 break;
5330 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005331 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005332 break;
5333 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005334 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005335 break;
5336 }
5337
Shannon Nelsona9165492015-09-03 17:19:00 -04005338 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005339 speed, fc);
5340}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005341
5342/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005343 * i40e_up_complete - Finish the last steps of bringing up a connection
5344 * @vsi: the VSI being configured
5345 **/
5346static int i40e_up_complete(struct i40e_vsi *vsi)
5347{
5348 struct i40e_pf *pf = vsi->back;
5349 int err;
5350
5351 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5352 i40e_vsi_configure_msix(vsi);
5353 else
5354 i40e_configure_msi_and_legacy(vsi);
5355
5356 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005357 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005358 if (err)
5359 return err;
5360
5361 clear_bit(__I40E_DOWN, &vsi->state);
5362 i40e_napi_enable_all(vsi);
5363 i40e_vsi_enable_irq(vsi);
5364
5365 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5366 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005367 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005368 netif_tx_start_all_queues(vsi->netdev);
5369 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005370 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005371 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005372 /* need to check for qualified module here*/
5373 if ((pf->hw.phy.link_info.link_info &
5374 I40E_AQ_MEDIA_AVAILABLE) &&
5375 (!(pf->hw.phy.link_info.an_info &
5376 I40E_AQ_QUALIFIED_MODULE)))
5377 netdev_err(vsi->netdev,
5378 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005379 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005380
5381 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005382 if (vsi->type == I40E_VSI_FDIR) {
5383 /* reset fd counters */
5384 pf->fd_add_err = pf->fd_atr_cnt = 0;
5385 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005386 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005387 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5388 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005389 pf->fd_tcp_rule = 0;
5390 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005391 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005392 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005393
5394 /* On the next run of the service_task, notify any clients of the new
5395 * opened netdev
5396 */
5397 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005398 i40e_service_event_schedule(pf);
5399
5400 return 0;
5401}
5402
5403/**
5404 * i40e_vsi_reinit_locked - Reset the VSI
5405 * @vsi: the VSI being configured
5406 *
5407 * Rebuild the ring structs after some configuration
5408 * has changed, e.g. MTU size.
5409 **/
5410static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5411{
5412 struct i40e_pf *pf = vsi->back;
5413
5414 WARN_ON(in_interrupt());
5415 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5416 usleep_range(1000, 2000);
5417 i40e_down(vsi);
5418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005419 i40e_up(vsi);
5420 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5421}
5422
5423/**
5424 * i40e_up - Bring the connection back up after being down
5425 * @vsi: the VSI being configured
5426 **/
5427int i40e_up(struct i40e_vsi *vsi)
5428{
5429 int err;
5430
5431 err = i40e_vsi_configure(vsi);
5432 if (!err)
5433 err = i40e_up_complete(vsi);
5434
5435 return err;
5436}
5437
5438/**
5439 * i40e_down - Shutdown the connection processing
5440 * @vsi: the VSI being stopped
5441 **/
5442void i40e_down(struct i40e_vsi *vsi)
5443{
5444 int i;
5445
5446 /* It is assumed that the caller of this function
5447 * sets the vsi->state __I40E_DOWN bit.
5448 */
5449 if (vsi->netdev) {
5450 netif_carrier_off(vsi->netdev);
5451 netif_tx_disable(vsi->netdev);
5452 }
5453 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005454 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005455 i40e_napi_disable_all(vsi);
5456
5457 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005458 i40e_clean_tx_ring(vsi->tx_rings[i]);
5459 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005460 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005461
5462 i40e_notify_client_of_netdev_close(vsi, false);
5463
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005464}
5465
5466/**
5467 * i40e_setup_tc - configure multiple traffic classes
5468 * @netdev: net device to configure
5469 * @tc: number of traffic classes to enable
5470 **/
5471static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5472{
5473 struct i40e_netdev_priv *np = netdev_priv(netdev);
5474 struct i40e_vsi *vsi = np->vsi;
5475 struct i40e_pf *pf = vsi->back;
5476 u8 enabled_tc = 0;
5477 int ret = -EINVAL;
5478 int i;
5479
5480 /* Check if DCB enabled to continue */
5481 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5482 netdev_info(netdev, "DCB is not enabled for adapter\n");
5483 goto exit;
5484 }
5485
5486 /* Check if MFP enabled */
5487 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5488 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5489 goto exit;
5490 }
5491
5492 /* Check whether tc count is within enabled limit */
5493 if (tc > i40e_pf_get_num_tc(pf)) {
5494 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5495 goto exit;
5496 }
5497
5498 /* Generate TC map for number of tc requested */
5499 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005500 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005501
5502 /* Requesting same TC configuration as already enabled */
5503 if (enabled_tc == vsi->tc_config.enabled_tc)
5504 return 0;
5505
5506 /* Quiesce VSI queues */
5507 i40e_quiesce_vsi(vsi);
5508
5509 /* Configure VSI for enabled TCs */
5510 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5511 if (ret) {
5512 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5513 vsi->seid);
5514 goto exit;
5515 }
5516
5517 /* Unquiesce VSI */
5518 i40e_unquiesce_vsi(vsi);
5519
5520exit:
5521 return ret;
5522}
5523
John Fastabende4c67342016-02-16 21:16:15 -08005524#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005525int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5526 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005527#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005528static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5529 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005530#endif
5531{
John Fastabend16e5cc62016-02-16 21:16:43 -08005532 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005533 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005534 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005535}
5536
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005537/**
5538 * i40e_open - Called when a network interface is made active
5539 * @netdev: network interface device structure
5540 *
5541 * The open entry point is called when a network interface is made
5542 * active by the system (IFF_UP). At this point all resources needed
5543 * for transmit and receive operations are allocated, the interrupt
5544 * handler is registered with the OS, the netdev watchdog subtask is
5545 * enabled, and the stack is notified that the interface is ready.
5546 *
5547 * Returns 0 on success, negative value on failure
5548 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005549int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005550{
5551 struct i40e_netdev_priv *np = netdev_priv(netdev);
5552 struct i40e_vsi *vsi = np->vsi;
5553 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005554 int err;
5555
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005556 /* disallow open during test or if eeprom is broken */
5557 if (test_bit(__I40E_TESTING, &pf->state) ||
5558 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005559 return -EBUSY;
5560
5561 netif_carrier_off(netdev);
5562
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005563 err = i40e_vsi_open(vsi);
5564 if (err)
5565 return err;
5566
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005567 /* configure global TSO hardware offload settings */
5568 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5569 TCP_FLAG_FIN) >> 16);
5570 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5571 TCP_FLAG_FIN |
5572 TCP_FLAG_CWR) >> 16);
5573 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5574
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005575 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005576
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005577 return 0;
5578}
5579
5580/**
5581 * i40e_vsi_open -
5582 * @vsi: the VSI to open
5583 *
5584 * Finish initialization of the VSI.
5585 *
5586 * Returns 0 on success, negative value on failure
5587 **/
5588int i40e_vsi_open(struct i40e_vsi *vsi)
5589{
5590 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005591 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005592 int err;
5593
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005594 /* allocate descriptors */
5595 err = i40e_vsi_setup_tx_resources(vsi);
5596 if (err)
5597 goto err_setup_tx;
5598 err = i40e_vsi_setup_rx_resources(vsi);
5599 if (err)
5600 goto err_setup_rx;
5601
5602 err = i40e_vsi_configure(vsi);
5603 if (err)
5604 goto err_setup_rx;
5605
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005606 if (vsi->netdev) {
5607 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5608 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5609 err = i40e_vsi_request_irq(vsi, int_name);
5610 if (err)
5611 goto err_setup_rx;
5612
5613 /* Notify the stack of the actual queue counts. */
5614 err = netif_set_real_num_tx_queues(vsi->netdev,
5615 vsi->num_queue_pairs);
5616 if (err)
5617 goto err_set_queues;
5618
5619 err = netif_set_real_num_rx_queues(vsi->netdev,
5620 vsi->num_queue_pairs);
5621 if (err)
5622 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005623
5624 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005625 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005626 dev_driver_string(&pf->pdev->dev),
5627 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005628 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005629
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005630 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005631 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005632 goto err_setup_rx;
5633 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005634
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005635 err = i40e_up_complete(vsi);
5636 if (err)
5637 goto err_up_complete;
5638
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005639 return 0;
5640
5641err_up_complete:
5642 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005643err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005644 i40e_vsi_free_irq(vsi);
5645err_setup_rx:
5646 i40e_vsi_free_rx_resources(vsi);
5647err_setup_tx:
5648 i40e_vsi_free_tx_resources(vsi);
5649 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005650 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005651
5652 return err;
5653}
5654
5655/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005656 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005657 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005658 *
5659 * This function destroys the hlist where all the Flow Director
5660 * filters were saved.
5661 **/
5662static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5663{
5664 struct i40e_fdir_filter *filter;
5665 struct hlist_node *node2;
5666
5667 hlist_for_each_entry_safe(filter, node2,
5668 &pf->fdir_filter_list, fdir_node) {
5669 hlist_del(&filter->fdir_node);
5670 kfree(filter);
5671 }
5672 pf->fdir_pf_active_filters = 0;
5673}
5674
5675/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005676 * i40e_close - Disables a network interface
5677 * @netdev: network interface device structure
5678 *
5679 * The close entry point is called when an interface is de-activated
5680 * by the OS. The hardware is still under the driver's control, but
5681 * this netdev interface is disabled.
5682 *
5683 * Returns 0, this is not allowed to fail
5684 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005685int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005686{
5687 struct i40e_netdev_priv *np = netdev_priv(netdev);
5688 struct i40e_vsi *vsi = np->vsi;
5689
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005690 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691
5692 return 0;
5693}
5694
5695/**
5696 * i40e_do_reset - Start a PF or Core Reset sequence
5697 * @pf: board private structure
5698 * @reset_flags: which reset is requested
5699 *
5700 * The essential difference in resets is that the PF Reset
5701 * doesn't clear the packet buffers, doesn't reset the PE
5702 * firmware, and doesn't bother the other PFs on the chip.
5703 **/
5704void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5705{
5706 u32 val;
5707
5708 WARN_ON(in_interrupt());
5709
Mitch Williams263fc482014-04-23 04:50:11 +00005710
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005711 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005712 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005713
5714 /* Request a Global Reset
5715 *
5716 * This will start the chip's countdown to the actual full
5717 * chip reset event, and a warning interrupt to be sent
5718 * to all PFs, including the requestor. Our handler
5719 * for the warning interrupt will deal with the shutdown
5720 * and recovery of the switch setup.
5721 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005722 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005723 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5724 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5725 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5726
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005727 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005728
5729 /* Request a Core Reset
5730 *
5731 * Same as Global Reset, except does *not* include the MAC/PHY
5732 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005733 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005734 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5735 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5736 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5737 i40e_flush(&pf->hw);
5738
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005739 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005740
5741 /* Request a PF Reset
5742 *
5743 * Resets only the PF-specific registers
5744 *
5745 * This goes directly to the tear-down and rebuild of
5746 * the switch, since we need to do all the recovery as
5747 * for the Core Reset.
5748 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005749 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005750 i40e_handle_reset_warning(pf);
5751
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005752 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005753 int v;
5754
5755 /* Find the VSI(s) that requested a re-init */
5756 dev_info(&pf->pdev->dev,
5757 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005758 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005759 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005761 if (vsi != NULL &&
5762 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5763 i40e_vsi_reinit_locked(pf->vsi[v]);
5764 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5765 }
5766 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005767 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005768 int v;
5769
5770 /* Find the VSI(s) that needs to be brought down */
5771 dev_info(&pf->pdev->dev, "VSI down requested\n");
5772 for (v = 0; v < pf->num_alloc_vsi; v++) {
5773 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005774
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005775 if (vsi != NULL &&
5776 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5777 set_bit(__I40E_DOWN, &vsi->state);
5778 i40e_down(vsi);
5779 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5780 }
5781 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005782 } else {
5783 dev_info(&pf->pdev->dev,
5784 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005785 }
5786}
5787
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005788#ifdef CONFIG_I40E_DCB
5789/**
5790 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5791 * @pf: board private structure
5792 * @old_cfg: current DCB config
5793 * @new_cfg: new DCB config
5794 **/
5795bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5796 struct i40e_dcbx_config *old_cfg,
5797 struct i40e_dcbx_config *new_cfg)
5798{
5799 bool need_reconfig = false;
5800
5801 /* Check if ETS configuration has changed */
5802 if (memcmp(&new_cfg->etscfg,
5803 &old_cfg->etscfg,
5804 sizeof(new_cfg->etscfg))) {
5805 /* If Priority Table has changed reconfig is needed */
5806 if (memcmp(&new_cfg->etscfg.prioritytable,
5807 &old_cfg->etscfg.prioritytable,
5808 sizeof(new_cfg->etscfg.prioritytable))) {
5809 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005810 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005811 }
5812
5813 if (memcmp(&new_cfg->etscfg.tcbwtable,
5814 &old_cfg->etscfg.tcbwtable,
5815 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005816 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005817
5818 if (memcmp(&new_cfg->etscfg.tsatable,
5819 &old_cfg->etscfg.tsatable,
5820 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005821 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005822 }
5823
5824 /* Check if PFC configuration has changed */
5825 if (memcmp(&new_cfg->pfc,
5826 &old_cfg->pfc,
5827 sizeof(new_cfg->pfc))) {
5828 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005829 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005830 }
5831
5832 /* Check if APP Table has changed */
5833 if (memcmp(&new_cfg->app,
5834 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005835 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005836 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005837 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005838 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005839
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005840 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005841 return need_reconfig;
5842}
5843
5844/**
5845 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5846 * @pf: board private structure
5847 * @e: event info posted on ARQ
5848 **/
5849static int i40e_handle_lldp_event(struct i40e_pf *pf,
5850 struct i40e_arq_event_info *e)
5851{
5852 struct i40e_aqc_lldp_get_mib *mib =
5853 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5854 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005855 struct i40e_dcbx_config tmp_dcbx_cfg;
5856 bool need_reconfig = false;
5857 int ret = 0;
5858 u8 type;
5859
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005860 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005861 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005862 return ret;
5863
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005864 /* Ignore if event is not for Nearest Bridge */
5865 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5866 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005867 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005868 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5869 return ret;
5870
5871 /* Check MIB Type and return if event for Remote MIB update */
5872 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005873 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005874 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005875 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5876 /* Update the remote cached instance and return */
5877 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5878 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5879 &hw->remote_dcbx_config);
5880 goto exit;
5881 }
5882
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005883 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005884 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005885
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005886 /* Reset the old DCBx configuration data */
5887 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005888 /* Get updated DCBX data from firmware */
5889 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005890 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005891 dev_info(&pf->pdev->dev,
5892 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5893 i40e_stat_str(&pf->hw, ret),
5894 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005895 goto exit;
5896 }
5897
5898 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005899 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5900 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005901 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005902 goto exit;
5903 }
5904
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005905 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5906 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005907
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005908 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005909
5910 if (!need_reconfig)
5911 goto exit;
5912
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005913 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005914 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005915 pf->flags |= I40E_FLAG_DCB_ENABLED;
5916 else
5917 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5918
Neerav Parikh69129dc2014-11-12 00:18:46 +00005919 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005920 /* Reconfiguration needed quiesce all VSIs */
5921 i40e_pf_quiesce_all_vsi(pf);
5922
5923 /* Changes in configuration update VEB/VSI */
5924 i40e_dcb_reconfigure(pf);
5925
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005926 ret = i40e_resume_port_tx(pf);
5927
Neerav Parikh69129dc2014-11-12 00:18:46 +00005928 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005929 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005930 if (ret)
5931 goto exit;
5932
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005933 /* Wait for the PF's queues to be disabled */
5934 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005935 if (ret) {
5936 /* Schedule PF reset to recover */
5937 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5938 i40e_service_event_schedule(pf);
5939 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005940 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005941 /* Notify the client for the DCB changes */
5942 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005943 }
5944
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005945exit:
5946 return ret;
5947}
5948#endif /* CONFIG_I40E_DCB */
5949
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005950/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005951 * i40e_do_reset_safe - Protected reset path for userland calls.
5952 * @pf: board private structure
5953 * @reset_flags: which reset is requested
5954 *
5955 **/
5956void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5957{
5958 rtnl_lock();
5959 i40e_do_reset(pf, reset_flags);
5960 rtnl_unlock();
5961}
5962
5963/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005964 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5965 * @pf: board private structure
5966 * @e: event info posted on ARQ
5967 *
5968 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5969 * and VF queues
5970 **/
5971static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5972 struct i40e_arq_event_info *e)
5973{
5974 struct i40e_aqc_lan_overflow *data =
5975 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5976 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5977 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5978 struct i40e_hw *hw = &pf->hw;
5979 struct i40e_vf *vf;
5980 u16 vf_id;
5981
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005982 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5983 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005984
5985 /* Queue belongs to VF, find the VF and issue VF reset */
5986 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5987 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5988 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5989 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5990 vf_id -= hw->func_caps.vf_base_id;
5991 vf = &pf->vf[vf_id];
5992 i40e_vc_notify_vf_reset(vf);
5993 /* Allow VF to process pending reset notification */
5994 msleep(20);
5995 i40e_reset_vf(vf, false);
5996 }
5997}
5998
5999/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006000 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6001 * @pf: board private structure
6002 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006003u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006004{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006005 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006006
6007 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6008 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6009 return fcnt_prog;
6010}
6011
6012/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006013 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006014 * @pf: board private structure
6015 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006016u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006017{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006018 u32 val, fcnt_prog;
6019
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006020 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6021 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6022 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6023 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6024 return fcnt_prog;
6025}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006026
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006027/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006028 * i40e_get_global_fd_count - Get total FD filters programmed on device
6029 * @pf: board private structure
6030 **/
6031u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6032{
6033 u32 val, fcnt_prog;
6034
6035 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6036 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6037 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6038 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6039 return fcnt_prog;
6040}
6041
6042/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006043 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6044 * @pf: board private structure
6045 **/
6046void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6047{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006048 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006049 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006050 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006051
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006052 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6053 return;
6054
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006055 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6056 * to re-enable
6057 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006058 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006059 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006060 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6061 (pf->fd_add_err == 0) ||
6062 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006063 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6064 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6065 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006066 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6067 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 +00006068 }
6069 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006070
6071 /* Wait for some more space to be available to turn on ATR. We also
6072 * must check that no existing ntuple rules for TCP are in effect
6073 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006074 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6075 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07006076 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6077 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006078 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006079 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006080 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 +00006081 }
6082 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006083
6084 /* if hw had a problem adding a filter, delete it */
6085 if (pf->fd_inv > 0) {
6086 hlist_for_each_entry_safe(filter, node,
6087 &pf->fdir_filter_list, fdir_node) {
6088 if (filter->fd_id == pf->fd_inv) {
6089 hlist_del(&filter->fdir_node);
6090 kfree(filter);
6091 pf->fdir_pf_active_filters--;
6092 }
6093 }
6094 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006095}
6096
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006097#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006098#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006099/**
6100 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6101 * @pf: board private structure
6102 **/
6103static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6104{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006105 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006106 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006107 bool disable_atr = false;
6108 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006109 int reg;
6110
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006111 if (!time_after(jiffies, pf->fd_flush_timestamp +
6112 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6113 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006114
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006115 /* If the flush is happening too quick and we have mostly SB rules we
6116 * should not re-enable ATR for some time.
6117 */
6118 min_flush_time = pf->fd_flush_timestamp +
6119 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6120 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006121
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006122 if (!(time_after(jiffies, min_flush_time)) &&
6123 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6124 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6125 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6126 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006127 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006128
6129 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006130 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006131 /* flush all filters */
6132 wr32(&pf->hw, I40E_PFQF_CTL_1,
6133 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6134 i40e_flush(&pf->hw);
6135 pf->fd_flush_cnt++;
6136 pf->fd_add_err = 0;
6137 do {
6138 /* Check FD flush status every 5-6msec */
6139 usleep_range(5000, 6000);
6140 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6141 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6142 break;
6143 } while (flush_wait_retry--);
6144 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6145 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6146 } else {
6147 /* replay sideband filters */
6148 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6149 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006150 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006151 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6152 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6153 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6154 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006155}
6156
6157/**
6158 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6159 * @pf: board private structure
6160 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006161u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006162{
6163 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6164}
6165
6166/* We can see up to 256 filter programming desc in transit if the filters are
6167 * being applied really fast; before we see the first
6168 * filter miss error on Rx queue 0. Accumulating enough error messages before
6169 * reacting will make sure we don't cause flush too often.
6170 */
6171#define I40E_MAX_FD_PROGRAM_ERROR 256
6172
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006173/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006174 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6175 * @pf: board private structure
6176 **/
6177static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6178{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006179
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006180 /* if interface is down do nothing */
6181 if (test_bit(__I40E_DOWN, &pf->state))
6182 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006183
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006184 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006185 i40e_fdir_flush_and_replay(pf);
6186
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006187 i40e_fdir_check_and_reenable(pf);
6188
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006189}
6190
6191/**
6192 * i40e_vsi_link_event - notify VSI of a link event
6193 * @vsi: vsi to be notified
6194 * @link_up: link up or down
6195 **/
6196static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6197{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006198 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006199 return;
6200
6201 switch (vsi->type) {
6202 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006203#ifdef I40E_FCOE
6204 case I40E_VSI_FCOE:
6205#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206 if (!vsi->netdev || !vsi->netdev_registered)
6207 break;
6208
6209 if (link_up) {
6210 netif_carrier_on(vsi->netdev);
6211 netif_tx_wake_all_queues(vsi->netdev);
6212 } else {
6213 netif_carrier_off(vsi->netdev);
6214 netif_tx_stop_all_queues(vsi->netdev);
6215 }
6216 break;
6217
6218 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006219 case I40E_VSI_VMDQ2:
6220 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006221 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006222 case I40E_VSI_MIRROR:
6223 default:
6224 /* there is no notification for other VSIs */
6225 break;
6226 }
6227}
6228
6229/**
6230 * i40e_veb_link_event - notify elements on the veb of a link event
6231 * @veb: veb to be notified
6232 * @link_up: link up or down
6233 **/
6234static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6235{
6236 struct i40e_pf *pf;
6237 int i;
6238
6239 if (!veb || !veb->pf)
6240 return;
6241 pf = veb->pf;
6242
6243 /* depth first... */
6244 for (i = 0; i < I40E_MAX_VEB; i++)
6245 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6246 i40e_veb_link_event(pf->veb[i], link_up);
6247
6248 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006249 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6251 i40e_vsi_link_event(pf->vsi[i], link_up);
6252}
6253
6254/**
6255 * i40e_link_event - Update netif_carrier status
6256 * @pf: board private structure
6257 **/
6258static void i40e_link_event(struct i40e_pf *pf)
6259{
Mitch Williams320684c2014-10-17 03:14:43 +00006260 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006261 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006262 i40e_status status;
6263 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006264
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006265 /* save off old link status information */
6266 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6267
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006268 /* set this to force the get_link_status call to refresh state */
6269 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006270
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006271 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006272
6273 status = i40e_get_link_status(&pf->hw, &new_link);
6274 if (status) {
6275 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6276 status);
6277 return;
6278 }
6279
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006280 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6281 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006282
6283 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006284 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006285 (test_bit(__I40E_DOWN, &vsi->state) ||
6286 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006288
6289 if (!test_bit(__I40E_DOWN, &vsi->state))
6290 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006291
6292 /* Notify the base of the switch tree connected to
6293 * the link. Floating VEBs are not notified.
6294 */
6295 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6296 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6297 else
Mitch Williams320684c2014-10-17 03:14:43 +00006298 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006299
6300 if (pf->vf)
6301 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006302
6303 if (pf->flags & I40E_FLAG_PTP)
6304 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006305}
6306
6307/**
Shannon Nelson21536712014-10-25 10:35:25 +00006308 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006309 * @pf: board private structure
6310 **/
6311static void i40e_watchdog_subtask(struct i40e_pf *pf)
6312{
6313 int i;
6314
6315 /* if interface is down do nothing */
6316 if (test_bit(__I40E_DOWN, &pf->state) ||
6317 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6318 return;
6319
Shannon Nelson21536712014-10-25 10:35:25 +00006320 /* make sure we don't do these things too often */
6321 if (time_before(jiffies, (pf->service_timer_previous +
6322 pf->service_timer_period)))
6323 return;
6324 pf->service_timer_previous = jiffies;
6325
Shannon Nelson9ac77262015-08-27 11:42:40 -04006326 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6327 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006328
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006329 /* Update the stats for active netdevs so the network stack
6330 * can look at updated numbers whenever it cares to
6331 */
Mitch Williams505682c2014-05-20 08:01:37 +00006332 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006333 if (pf->vsi[i] && pf->vsi[i]->netdev)
6334 i40e_update_stats(pf->vsi[i]);
6335
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006336 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6337 /* Update the stats for the active switching components */
6338 for (i = 0; i < I40E_MAX_VEB; i++)
6339 if (pf->veb[i])
6340 i40e_update_veb_stats(pf->veb[i]);
6341 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006342
6343 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006344}
6345
6346/**
6347 * i40e_reset_subtask - Set up for resetting the device and driver
6348 * @pf: board private structure
6349 **/
6350static void i40e_reset_subtask(struct i40e_pf *pf)
6351{
6352 u32 reset_flags = 0;
6353
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006354 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006355 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006356 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6358 }
6359 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006360 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6362 }
6363 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006364 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6366 }
6367 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006368 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006369 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6370 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006371 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006372 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006373 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6374 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006375
6376 /* If there's a recovery already waiting, it takes
6377 * precedence before starting a new reset sequence.
6378 */
6379 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6380 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006381 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382 }
6383
6384 /* If we're already down or resetting, just bail */
6385 if (reset_flags &&
6386 !test_bit(__I40E_DOWN, &pf->state) &&
6387 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6388 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006389
6390unlock:
6391 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006392}
6393
6394/**
6395 * i40e_handle_link_event - Handle link event
6396 * @pf: board private structure
6397 * @e: event info posted on ARQ
6398 **/
6399static void i40e_handle_link_event(struct i40e_pf *pf,
6400 struct i40e_arq_event_info *e)
6401{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006402 struct i40e_aqc_get_link_status *status =
6403 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006404
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006405 /* Do a new status request to re-enable LSE reporting
6406 * and load new status information into the hw struct
6407 * This completely ignores any state information
6408 * in the ARQ event info, instead choosing to always
6409 * issue the AQ update link status command.
6410 */
6411 i40e_link_event(pf);
6412
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006413 /* check for unqualified module, if link is down */
6414 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6415 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6416 (!(status->link_info & I40E_AQ_LINK_UP)))
6417 dev_err(&pf->pdev->dev,
6418 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006419}
6420
6421/**
6422 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6423 * @pf: board private structure
6424 **/
6425static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6426{
6427 struct i40e_arq_event_info event;
6428 struct i40e_hw *hw = &pf->hw;
6429 u16 pending, i = 0;
6430 i40e_status ret;
6431 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006432 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006433 u32 val;
6434
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006435 /* Do not run clean AQ when PF reset fails */
6436 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6437 return;
6438
Shannon Nelson86df2422014-05-20 08:01:35 +00006439 /* check for error indications */
6440 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6441 oldval = val;
6442 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006443 if (hw->debug_mask & I40E_DEBUG_AQ)
6444 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006445 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6446 }
6447 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006448 if (hw->debug_mask & I40E_DEBUG_AQ)
6449 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006450 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006451 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006452 }
6453 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006454 if (hw->debug_mask & I40E_DEBUG_AQ)
6455 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006456 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6457 }
6458 if (oldval != val)
6459 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6460
6461 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6462 oldval = val;
6463 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006464 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6465 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006466 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6467 }
6468 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006469 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6470 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006471 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6472 }
6473 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006474 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6475 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006476 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6477 }
6478 if (oldval != val)
6479 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6480
Mitch Williams1001dc32014-11-11 20:02:19 +00006481 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6482 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006483 if (!event.msg_buf)
6484 return;
6485
6486 do {
6487 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006488 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006489 break;
Mitch Williams56497972014-06-04 08:45:18 +00006490 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006491 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6492 break;
6493 }
6494
6495 opcode = le16_to_cpu(event.desc.opcode);
6496 switch (opcode) {
6497
6498 case i40e_aqc_opc_get_link_status:
6499 i40e_handle_link_event(pf, &event);
6500 break;
6501 case i40e_aqc_opc_send_msg_to_pf:
6502 ret = i40e_vc_process_vf_msg(pf,
6503 le16_to_cpu(event.desc.retval),
6504 le32_to_cpu(event.desc.cookie_high),
6505 le32_to_cpu(event.desc.cookie_low),
6506 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006507 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006508 break;
6509 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006510 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006511#ifdef CONFIG_I40E_DCB
6512 rtnl_lock();
6513 ret = i40e_handle_lldp_event(pf, &event);
6514 rtnl_unlock();
6515#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 break;
6517 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006518 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006519 i40e_handle_lan_overflow_event(pf, &event);
6520 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006521 case i40e_aqc_opc_send_msg_to_peer:
6522 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6523 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006524 case i40e_aqc_opc_nvm_erase:
6525 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006526 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006527 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6528 "ARQ NVM operation 0x%04x completed\n",
6529 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006530 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006531 default:
6532 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006533 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006534 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006535 break;
6536 }
6537 } while (pending && (i++ < pf->adminq_work_limit));
6538
6539 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6540 /* re-enable Admin queue interrupt cause */
6541 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6542 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6543 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6544 i40e_flush(hw);
6545
6546 kfree(event.msg_buf);
6547}
6548
6549/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006550 * i40e_verify_eeprom - make sure eeprom is good to use
6551 * @pf: board private structure
6552 **/
6553static void i40e_verify_eeprom(struct i40e_pf *pf)
6554{
6555 int err;
6556
6557 err = i40e_diag_eeprom_test(&pf->hw);
6558 if (err) {
6559 /* retry in case of garbage read */
6560 err = i40e_diag_eeprom_test(&pf->hw);
6561 if (err) {
6562 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6563 err);
6564 set_bit(__I40E_BAD_EEPROM, &pf->state);
6565 }
6566 }
6567
6568 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6569 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6570 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6571 }
6572}
6573
6574/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006575 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006576 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006577 *
6578 * enable switch loop back or die - no point in a return value
6579 **/
6580static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6581{
6582 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6583 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006584 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006585
6586 ctxt.seid = pf->main_vsi_seid;
6587 ctxt.pf_num = pf->hw.pf_id;
6588 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006589 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6590 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006591 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006592 "couldn't get PF vsi config, err %s aq_err %s\n",
6593 i40e_stat_str(&pf->hw, ret),
6594 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006595 return;
6596 }
6597 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6598 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6599 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6600
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006601 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6602 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006603 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006604 "update vsi switch failed, err %s aq_err %s\n",
6605 i40e_stat_str(&pf->hw, ret),
6606 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006607 }
6608}
6609
6610/**
6611 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006612 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006613 *
6614 * disable switch loop back or die - no point in a return value
6615 **/
6616static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6617{
6618 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6619 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006620 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006621
6622 ctxt.seid = pf->main_vsi_seid;
6623 ctxt.pf_num = pf->hw.pf_id;
6624 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006625 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6626 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006627 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006628 "couldn't get PF vsi config, err %s aq_err %s\n",
6629 i40e_stat_str(&pf->hw, ret),
6630 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006631 return;
6632 }
6633 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6634 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6635 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6636
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006637 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6638 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006639 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006640 "update vsi switch failed, err %s aq_err %s\n",
6641 i40e_stat_str(&pf->hw, ret),
6642 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006643 }
6644}
6645
6646/**
Neerav Parikh51616012015-02-06 08:52:14 +00006647 * i40e_config_bridge_mode - Configure the HW bridge mode
6648 * @veb: pointer to the bridge instance
6649 *
6650 * Configure the loop back mode for the LAN VSI that is downlink to the
6651 * specified HW bridge instance. It is expected this function is called
6652 * when a new HW bridge is instantiated.
6653 **/
6654static void i40e_config_bridge_mode(struct i40e_veb *veb)
6655{
6656 struct i40e_pf *pf = veb->pf;
6657
Shannon Nelson6dec1012015-09-28 14:12:30 -04006658 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6659 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6660 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006661 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6662 i40e_disable_pf_switch_lb(pf);
6663 else
6664 i40e_enable_pf_switch_lb(pf);
6665}
6666
6667/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006668 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6669 * @veb: pointer to the VEB instance
6670 *
6671 * This is a recursive function that first builds the attached VSIs then
6672 * recurses in to build the next layer of VEB. We track the connections
6673 * through our own index numbers because the seid's from the HW could
6674 * change across the reset.
6675 **/
6676static int i40e_reconstitute_veb(struct i40e_veb *veb)
6677{
6678 struct i40e_vsi *ctl_vsi = NULL;
6679 struct i40e_pf *pf = veb->pf;
6680 int v, veb_idx;
6681 int ret;
6682
6683 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006684 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 if (pf->vsi[v] &&
6686 pf->vsi[v]->veb_idx == veb->idx &&
6687 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6688 ctl_vsi = pf->vsi[v];
6689 break;
6690 }
6691 }
6692 if (!ctl_vsi) {
6693 dev_info(&pf->pdev->dev,
6694 "missing owner VSI for veb_idx %d\n", veb->idx);
6695 ret = -ENOENT;
6696 goto end_reconstitute;
6697 }
6698 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6699 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6700 ret = i40e_add_vsi(ctl_vsi);
6701 if (ret) {
6702 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006703 "rebuild of veb_idx %d owner VSI failed: %d\n",
6704 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 goto end_reconstitute;
6706 }
6707 i40e_vsi_reset_stats(ctl_vsi);
6708
6709 /* create the VEB in the switch and move the VSI onto the VEB */
6710 ret = i40e_add_veb(veb, ctl_vsi);
6711 if (ret)
6712 goto end_reconstitute;
6713
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006714 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6715 veb->bridge_mode = BRIDGE_MODE_VEB;
6716 else
6717 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006718 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006720 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006721 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006722 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6723 continue;
6724
6725 if (pf->vsi[v]->veb_idx == veb->idx) {
6726 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006727
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006728 vsi->uplink_seid = veb->seid;
6729 ret = i40e_add_vsi(vsi);
6730 if (ret) {
6731 dev_info(&pf->pdev->dev,
6732 "rebuild of vsi_idx %d failed: %d\n",
6733 v, ret);
6734 goto end_reconstitute;
6735 }
6736 i40e_vsi_reset_stats(vsi);
6737 }
6738 }
6739
6740 /* create any VEBs attached to this VEB - RECURSION */
6741 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6742 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6743 pf->veb[veb_idx]->uplink_seid = veb->seid;
6744 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6745 if (ret)
6746 break;
6747 }
6748 }
6749
6750end_reconstitute:
6751 return ret;
6752}
6753
6754/**
6755 * i40e_get_capabilities - get info about the HW
6756 * @pf: the PF struct
6757 **/
6758static int i40e_get_capabilities(struct i40e_pf *pf)
6759{
6760 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6761 u16 data_size;
6762 int buf_len;
6763 int err;
6764
6765 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6766 do {
6767 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6768 if (!cap_buf)
6769 return -ENOMEM;
6770
6771 /* this loads the data into the hw struct for us */
6772 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6773 &data_size,
6774 i40e_aqc_opc_list_func_capabilities,
6775 NULL);
6776 /* data loaded, buffer no longer needed */
6777 kfree(cap_buf);
6778
6779 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6780 /* retry with a larger buffer */
6781 buf_len = data_size;
6782 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6783 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006784 "capability discovery failed, err %s aq_err %s\n",
6785 i40e_stat_str(&pf->hw, err),
6786 i40e_aq_str(&pf->hw,
6787 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006788 return -ENODEV;
6789 }
6790 } while (err);
6791
6792 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6793 dev_info(&pf->pdev->dev,
6794 "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",
6795 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6796 pf->hw.func_caps.num_msix_vectors,
6797 pf->hw.func_caps.num_msix_vectors_vf,
6798 pf->hw.func_caps.fd_filters_guaranteed,
6799 pf->hw.func_caps.fd_filters_best_effort,
6800 pf->hw.func_caps.num_tx_qp,
6801 pf->hw.func_caps.num_vsis);
6802
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006803#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6804 + pf->hw.func_caps.num_vfs)
6805 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6806 dev_info(&pf->pdev->dev,
6807 "got num_vsis %d, setting num_vsis to %d\n",
6808 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6809 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6810 }
6811
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006812 return 0;
6813}
6814
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006815static int i40e_vsi_clear(struct i40e_vsi *vsi);
6816
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006817/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006818 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006819 * @pf: board private structure
6820 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006821static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006822{
6823 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006824
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006825 /* quick workaround for an NVM issue that leaves a critical register
6826 * uninitialized
6827 */
6828 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6829 static const u32 hkey[] = {
6830 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6831 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6832 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6833 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006834 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006835
6836 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6837 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6838 }
6839
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006840 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006841 return;
6842
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006843 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006844 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006845
6846 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006847 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006848 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6849 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006850 if (!vsi) {
6851 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006852 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6853 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006856
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006857 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006858}
6859
6860/**
6861 * i40e_fdir_teardown - release the Flow Director resources
6862 * @pf: board private structure
6863 **/
6864static void i40e_fdir_teardown(struct i40e_pf *pf)
6865{
Alexander Duyck4b816442016-10-11 15:26:53 -07006866 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006867
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006868 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006869 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6870 if (vsi)
6871 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872}
6873
6874/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006875 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006876 * @pf: board private structure
6877 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006878 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006879 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006880static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006881{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006882 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006883 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006884 u32 v;
6885
6886 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6887 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006888 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006889 if (i40e_check_asq_alive(&pf->hw))
6890 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006891
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006892 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006893
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006894 /* quiesce the VSIs and their queues that are not already DOWN */
6895 i40e_pf_quiesce_all_vsi(pf);
6896
Mitch Williams505682c2014-05-20 08:01:37 +00006897 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006898 if (pf->vsi[v])
6899 pf->vsi[v]->seid = 0;
6900 }
6901
6902 i40e_shutdown_adminq(&pf->hw);
6903
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006904 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006905 if (hw->hmc.hmc_obj) {
6906 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006907 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006908 dev_warn(&pf->pdev->dev,
6909 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006910 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006911}
6912
6913/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006914 * i40e_send_version - update firmware with driver version
6915 * @pf: PF struct
6916 */
6917static void i40e_send_version(struct i40e_pf *pf)
6918{
6919 struct i40e_driver_version dv;
6920
6921 dv.major_version = DRV_VERSION_MAJOR;
6922 dv.minor_version = DRV_VERSION_MINOR;
6923 dv.build_version = DRV_VERSION_BUILD;
6924 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006925 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006926 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6927}
6928
6929/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006930 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006931 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006932 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006933 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006934static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006935{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006936 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006937 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006938 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006939 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006940 u32 v;
6941
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006942 /* Now we wait for GRST to settle out.
6943 * We don't have to delete the VEBs or VSIs from the hw switch
6944 * because the reset will make them disappear.
6945 */
6946 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006947 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006949 set_bit(__I40E_RESET_FAILED, &pf->state);
6950 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006951 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006952 pf->pfr_count++;
6953
6954 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006955 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006956 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006957
6958 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6959 ret = i40e_init_adminq(&pf->hw);
6960 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006961 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6962 i40e_stat_str(&pf->hw, ret),
6963 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006964 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006965 }
6966
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006967 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006968 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006969 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006970
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006971 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006972 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006973 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006974 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006975
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006976 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6977 hw->func_caps.num_rx_qp,
6978 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6979 if (ret) {
6980 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6981 goto end_core_reset;
6982 }
6983 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6984 if (ret) {
6985 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6986 goto end_core_reset;
6987 }
6988
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006989#ifdef CONFIG_I40E_DCB
6990 ret = i40e_init_pf_dcb(pf);
6991 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006992 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6993 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6994 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006995 }
6996#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006997#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006998 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006999
Vasu Dev38e00432014-08-01 13:27:03 -07007000#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007001 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007002 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007003 if (ret)
7004 goto end_core_reset;
7005
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007006 /* The driver only wants link up/down and module qualification
7007 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007008 */
7009 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007010 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007011 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007012 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007013 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007014 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7015 i40e_stat_str(&pf->hw, ret),
7016 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007017
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007018 /* make sure our flow control settings are restored */
7019 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7020 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007021 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7022 i40e_stat_str(&pf->hw, ret),
7023 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007024
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007025 /* Rebuild the VSIs and VEBs that existed before reset.
7026 * They are still in our local switch element arrays, so only
7027 * need to rebuild the switch model in the HW.
7028 *
7029 * If there were VEBs but the reconstitution failed, we'll try
7030 * try to recover minimal use by getting the basic PF VSI working.
7031 */
7032 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007033 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007034 /* find the one VEB connected to the MAC, and find orphans */
7035 for (v = 0; v < I40E_MAX_VEB; v++) {
7036 if (!pf->veb[v])
7037 continue;
7038
7039 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7040 pf->veb[v]->uplink_seid == 0) {
7041 ret = i40e_reconstitute_veb(pf->veb[v]);
7042
7043 if (!ret)
7044 continue;
7045
7046 /* If Main VEB failed, we're in deep doodoo,
7047 * so give up rebuilding the switch and set up
7048 * for minimal rebuild of PF VSI.
7049 * If orphan failed, we'll report the error
7050 * but try to keep going.
7051 */
7052 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7053 dev_info(&pf->pdev->dev,
7054 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7055 ret);
7056 pf->vsi[pf->lan_vsi]->uplink_seid
7057 = pf->mac_seid;
7058 break;
7059 } else if (pf->veb[v]->uplink_seid == 0) {
7060 dev_info(&pf->pdev->dev,
7061 "rebuild of orphan VEB failed: %d\n",
7062 ret);
7063 }
7064 }
7065 }
7066 }
7067
7068 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007069 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007070 /* no VEB, so rebuild only the Main VSI */
7071 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7072 if (ret) {
7073 dev_info(&pf->pdev->dev,
7074 "rebuild of Main VSI failed: %d\n", ret);
7075 goto end_core_reset;
7076 }
7077 }
7078
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007079 /* Reconfigure hardware for allowing smaller MSS in the case
7080 * of TSO, so that we avoid the MDD being fired and causing
7081 * a reset in the case of small MSS+TSO.
7082 */
7083#define I40E_REG_MSS 0x000E64DC
7084#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7085#define I40E_64BYTE_MSS 0x400000
7086 val = rd32(hw, I40E_REG_MSS);
7087 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7088 val &= ~I40E_REG_MSS_MIN_MASK;
7089 val |= I40E_64BYTE_MSS;
7090 wr32(hw, I40E_REG_MSS, val);
7091 }
7092
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007093 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007094 msleep(75);
7095 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7096 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007097 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7098 i40e_stat_str(&pf->hw, ret),
7099 i40e_aq_str(&pf->hw,
7100 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007101 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007102 /* reinit the misc interrupt */
7103 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7104 ret = i40e_setup_misc_vector(pf);
7105
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007106 /* Add a filter to drop all Flow control frames from any VSI from being
7107 * transmitted. By doing so we stop a malicious VF from sending out
7108 * PAUSE or PFC frames and potentially controlling traffic for other
7109 * PF/VF VSIs.
7110 * The FW can still send Flow control frames if enabled.
7111 */
7112 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7113 pf->main_vsi_seid);
7114
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007115 /* restart the VSIs that were rebuilt and running before the reset */
7116 i40e_pf_unquiesce_all_vsi(pf);
7117
Mitch Williams69f64b22014-02-13 03:48:46 -08007118 if (pf->num_alloc_vfs) {
7119 for (v = 0; v < pf->num_alloc_vfs; v++)
7120 i40e_reset_vf(&pf->vf[v], true);
7121 }
7122
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007123 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007124 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007125
7126end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007127 clear_bit(__I40E_RESET_FAILED, &pf->state);
7128clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007129 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7130}
7131
7132/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007133 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007134 * @pf: board private structure
7135 *
7136 * Close up the VFs and other things in prep for a Core Reset,
7137 * then get ready to rebuild the world.
7138 **/
7139static void i40e_handle_reset_warning(struct i40e_pf *pf)
7140{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007141 i40e_prep_for_reset(pf);
7142 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007143}
7144
7145/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007146 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007147 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007148 *
7149 * Called from the MDD irq handler to identify possibly malicious vfs
7150 **/
7151static void i40e_handle_mdd_event(struct i40e_pf *pf)
7152{
7153 struct i40e_hw *hw = &pf->hw;
7154 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007155 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007156 struct i40e_vf *vf;
7157 u32 reg;
7158 int i;
7159
7160 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7161 return;
7162
7163 /* find what triggered the MDD event */
7164 reg = rd32(hw, I40E_GL_MDET_TX);
7165 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007166 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7167 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007168 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007169 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007170 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007171 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007172 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7173 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7174 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007175 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007176 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 +00007177 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007178 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7179 mdd_detected = true;
7180 }
7181 reg = rd32(hw, I40E_GL_MDET_RX);
7182 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007183 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7184 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007185 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007186 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007187 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7188 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7189 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007190 if (netif_msg_rx_err(pf))
7191 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7192 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007193 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7194 mdd_detected = true;
7195 }
7196
Neerav Parikhdf430b12014-06-04 01:23:15 +00007197 if (mdd_detected) {
7198 reg = rd32(hw, I40E_PF_MDET_TX);
7199 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7200 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007201 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007202 pf_mdd_detected = true;
7203 }
7204 reg = rd32(hw, I40E_PF_MDET_RX);
7205 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7206 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007207 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007208 pf_mdd_detected = true;
7209 }
7210 /* Queue belongs to the PF, initiate a reset */
7211 if (pf_mdd_detected) {
7212 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7213 i40e_service_event_schedule(pf);
7214 }
7215 }
7216
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007217 /* see if one of the VFs needs its hand slapped */
7218 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7219 vf = &(pf->vf[i]);
7220 reg = rd32(hw, I40E_VP_MDET_TX(i));
7221 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7222 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7223 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007224 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7225 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007226 }
7227
7228 reg = rd32(hw, I40E_VP_MDET_RX(i));
7229 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7230 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7231 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007232 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7233 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007234 }
7235
7236 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7237 dev_info(&pf->pdev->dev,
7238 "Too many MDD events on VF %d, disabled\n", i);
7239 dev_info(&pf->pdev->dev,
7240 "Use PF Control I/F to re-enable the VF\n");
7241 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7242 }
7243 }
7244
7245 /* re-enable mdd interrupt cause */
7246 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7247 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7248 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7249 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7250 i40e_flush(hw);
7251}
7252
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007253/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007254 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007255 * @pf: board private structure
7256 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007257static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007258{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007259 struct i40e_hw *hw = &pf->hw;
7260 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007261 __be16 port;
7262 int i;
7263
Singhai, Anjali6a899022015-12-14 12:21:18 -08007264 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007265 return;
7266
Singhai, Anjali6a899022015-12-14 12:21:18 -08007267 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007268
7269 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007270 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7271 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7272 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007273 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007274 ret = i40e_aq_add_udp_tunnel(hw, port,
7275 pf->udp_ports[i].type,
7276 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007277 else
7278 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007279
7280 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007281 dev_dbg(&pf->pdev->dev,
7282 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7283 pf->udp_ports[i].type ? "vxlan" : "geneve",
7284 port ? "add" : "delete",
7285 ntohs(port), i,
7286 i40e_stat_str(&pf->hw, ret),
7287 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007288 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007289 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007290 }
7291 }
7292 }
7293}
7294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007295/**
7296 * i40e_service_task - Run the driver's async subtasks
7297 * @work: pointer to work_struct containing our data
7298 **/
7299static void i40e_service_task(struct work_struct *work)
7300{
7301 struct i40e_pf *pf = container_of(work,
7302 struct i40e_pf,
7303 service_task);
7304 unsigned long start_time = jiffies;
7305
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007306 /* don't bother with service tasks if a reset is in progress */
7307 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007308 return;
7309 }
7310
Mitch Williams91089032016-11-21 13:03:51 -08007311 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7312 return;
7313
Kiran Patilb03a8c12015-09-24 18:13:15 -04007314 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007315 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007316 i40e_reset_subtask(pf);
7317 i40e_handle_mdd_event(pf);
7318 i40e_vc_process_vflr_event(pf);
7319 i40e_watchdog_subtask(pf);
7320 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007321 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007322 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007323 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007324 i40e_clean_adminq_subtask(pf);
7325
Mitch Williams91089032016-11-21 13:03:51 -08007326 /* flush memory to make sure state is correct before next watchdog */
7327 smp_mb__before_atomic();
7328 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007329
7330 /* If the tasks have taken longer than one timer cycle or there
7331 * is more work to be done, reschedule the service task now
7332 * rather than wait for the timer to tick again.
7333 */
7334 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7335 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7336 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7337 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7338 i40e_service_event_schedule(pf);
7339}
7340
7341/**
7342 * i40e_service_timer - timer callback
7343 * @data: pointer to PF struct
7344 **/
7345static void i40e_service_timer(unsigned long data)
7346{
7347 struct i40e_pf *pf = (struct i40e_pf *)data;
7348
7349 mod_timer(&pf->service_timer,
7350 round_jiffies(jiffies + pf->service_timer_period));
7351 i40e_service_event_schedule(pf);
7352}
7353
7354/**
7355 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7356 * @vsi: the VSI being configured
7357 **/
7358static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7359{
7360 struct i40e_pf *pf = vsi->back;
7361
7362 switch (vsi->type) {
7363 case I40E_VSI_MAIN:
7364 vsi->alloc_queue_pairs = pf->num_lan_qps;
7365 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7366 I40E_REQ_DESCRIPTOR_MULTIPLE);
7367 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7368 vsi->num_q_vectors = pf->num_lan_msix;
7369 else
7370 vsi->num_q_vectors = 1;
7371
7372 break;
7373
7374 case I40E_VSI_FDIR:
7375 vsi->alloc_queue_pairs = 1;
7376 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7377 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007378 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007379 break;
7380
7381 case I40E_VSI_VMDQ2:
7382 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7383 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7384 I40E_REQ_DESCRIPTOR_MULTIPLE);
7385 vsi->num_q_vectors = pf->num_vmdq_msix;
7386 break;
7387
7388 case I40E_VSI_SRIOV:
7389 vsi->alloc_queue_pairs = pf->num_vf_qps;
7390 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7391 I40E_REQ_DESCRIPTOR_MULTIPLE);
7392 break;
7393
Vasu Dev38e00432014-08-01 13:27:03 -07007394#ifdef I40E_FCOE
7395 case I40E_VSI_FCOE:
7396 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7397 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7398 I40E_REQ_DESCRIPTOR_MULTIPLE);
7399 vsi->num_q_vectors = pf->num_fcoe_msix;
7400 break;
7401
7402#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007403 default:
7404 WARN_ON(1);
7405 return -ENODATA;
7406 }
7407
7408 return 0;
7409}
7410
7411/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007412 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7413 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007414 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007415 *
7416 * On error: returns error code (negative)
7417 * On success: returns 0
7418 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007419static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007420{
7421 int size;
7422 int ret = 0;
7423
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007424 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007425 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7426 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7427 if (!vsi->tx_rings)
7428 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007429 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7430
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007431 if (alloc_qvectors) {
7432 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007433 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007434 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7435 if (!vsi->q_vectors) {
7436 ret = -ENOMEM;
7437 goto err_vectors;
7438 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007439 }
7440 return ret;
7441
7442err_vectors:
7443 kfree(vsi->tx_rings);
7444 return ret;
7445}
7446
7447/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007448 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7449 * @pf: board private structure
7450 * @type: type of VSI
7451 *
7452 * On error: returns error code (negative)
7453 * On success: returns vsi index in PF (positive)
7454 **/
7455static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7456{
7457 int ret = -ENODEV;
7458 struct i40e_vsi *vsi;
7459 int vsi_idx;
7460 int i;
7461
7462 /* Need to protect the allocation of the VSIs at the PF level */
7463 mutex_lock(&pf->switch_mutex);
7464
7465 /* VSI list may be fragmented if VSI creation/destruction has
7466 * been happening. We can afford to do a quick scan to look
7467 * for any free VSIs in the list.
7468 *
7469 * find next empty vsi slot, looping back around if necessary
7470 */
7471 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007472 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007473 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007474 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007475 i = 0;
7476 while (i < pf->next_vsi && pf->vsi[i])
7477 i++;
7478 }
7479
Mitch Williams505682c2014-05-20 08:01:37 +00007480 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481 vsi_idx = i; /* Found one! */
7482 } else {
7483 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007484 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007485 }
7486 pf->next_vsi = ++i;
7487
7488 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7489 if (!vsi) {
7490 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007491 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007492 }
7493 vsi->type = type;
7494 vsi->back = pf;
7495 set_bit(__I40E_DOWN, &vsi->state);
7496 vsi->flags = 0;
7497 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007498 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007499 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7500 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501 vsi->netdev_registered = false;
7502 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007503 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007504 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007505
Alexander Duyck9f65e152013-09-28 06:00:58 +00007506 ret = i40e_set_num_rings_in_vsi(vsi);
7507 if (ret)
7508 goto err_rings;
7509
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007510 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007511 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007512 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007513
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007514 /* Setup default MSIX irq handler for VSI */
7515 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7516
Kiran Patil21659032015-09-30 14:09:03 -04007517 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007518 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007519 pf->vsi[vsi_idx] = vsi;
7520 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007521 goto unlock_pf;
7522
Alexander Duyck9f65e152013-09-28 06:00:58 +00007523err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007524 pf->next_vsi = i - 1;
7525 kfree(vsi);
7526unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007527 mutex_unlock(&pf->switch_mutex);
7528 return ret;
7529}
7530
7531/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007532 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7533 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007534 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007535 *
7536 * On error: returns error code (negative)
7537 * On success: returns 0
7538 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007539static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007540{
7541 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007542 if (free_qvectors) {
7543 kfree(vsi->q_vectors);
7544 vsi->q_vectors = NULL;
7545 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007546 kfree(vsi->tx_rings);
7547 vsi->tx_rings = NULL;
7548 vsi->rx_rings = NULL;
7549}
7550
7551/**
Helin Zhang28c58692015-10-26 19:44:27 -04007552 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7553 * and lookup table
7554 * @vsi: Pointer to VSI structure
7555 */
7556static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7557{
7558 if (!vsi)
7559 return;
7560
7561 kfree(vsi->rss_hkey_user);
7562 vsi->rss_hkey_user = NULL;
7563
7564 kfree(vsi->rss_lut_user);
7565 vsi->rss_lut_user = NULL;
7566}
7567
7568/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007569 * i40e_vsi_clear - Deallocate the VSI provided
7570 * @vsi: the VSI being un-configured
7571 **/
7572static int i40e_vsi_clear(struct i40e_vsi *vsi)
7573{
7574 struct i40e_pf *pf;
7575
7576 if (!vsi)
7577 return 0;
7578
7579 if (!vsi->back)
7580 goto free_vsi;
7581 pf = vsi->back;
7582
7583 mutex_lock(&pf->switch_mutex);
7584 if (!pf->vsi[vsi->idx]) {
7585 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7586 vsi->idx, vsi->idx, vsi, vsi->type);
7587 goto unlock_vsi;
7588 }
7589
7590 if (pf->vsi[vsi->idx] != vsi) {
7591 dev_err(&pf->pdev->dev,
7592 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7593 pf->vsi[vsi->idx]->idx,
7594 pf->vsi[vsi->idx],
7595 pf->vsi[vsi->idx]->type,
7596 vsi->idx, vsi, vsi->type);
7597 goto unlock_vsi;
7598 }
7599
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007600 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007601 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7602 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7603
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007604 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007605 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007607 pf->vsi[vsi->idx] = NULL;
7608 if (vsi->idx < pf->next_vsi)
7609 pf->next_vsi = vsi->idx;
7610
7611unlock_vsi:
7612 mutex_unlock(&pf->switch_mutex);
7613free_vsi:
7614 kfree(vsi);
7615
7616 return 0;
7617}
7618
7619/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007620 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7621 * @vsi: the VSI being cleaned
7622 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007623static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007624{
7625 int i;
7626
Greg Rose8e9dca52013-12-18 13:45:53 +00007627 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007628 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007629 kfree_rcu(vsi->tx_rings[i], rcu);
7630 vsi->tx_rings[i] = NULL;
7631 vsi->rx_rings[i] = NULL;
7632 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007633 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007634}
7635
7636/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007637 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7638 * @vsi: the VSI being configured
7639 **/
7640static int i40e_alloc_rings(struct i40e_vsi *vsi)
7641{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007642 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007643 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007644 int i;
7645
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007646 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007647 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007648 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007649 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7650 if (!tx_ring)
7651 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007652
7653 tx_ring->queue_index = i;
7654 tx_ring->reg_idx = vsi->base_queue + i;
7655 tx_ring->ring_active = false;
7656 tx_ring->vsi = vsi;
7657 tx_ring->netdev = vsi->netdev;
7658 tx_ring->dev = &pf->pdev->dev;
7659 tx_ring->count = vsi->num_desc;
7660 tx_ring->size = 0;
7661 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007662 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7663 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007664 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007665 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007666
Alexander Duyck9f65e152013-09-28 06:00:58 +00007667 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007668 rx_ring->queue_index = i;
7669 rx_ring->reg_idx = vsi->base_queue + i;
7670 rx_ring->ring_active = false;
7671 rx_ring->vsi = vsi;
7672 rx_ring->netdev = vsi->netdev;
7673 rx_ring->dev = &pf->pdev->dev;
7674 rx_ring->count = vsi->num_desc;
7675 rx_ring->size = 0;
7676 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007677 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007678 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007679 }
7680
7681 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007682
7683err_out:
7684 i40e_vsi_clear_rings(vsi);
7685 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007686}
7687
7688/**
7689 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7690 * @pf: board private structure
7691 * @vectors: the number of MSI-X vectors to request
7692 *
7693 * Returns the number of vectors reserved, or error
7694 **/
7695static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7696{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007697 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7698 I40E_MIN_MSIX, vectors);
7699 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007700 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007701 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007702 vectors = 0;
7703 }
7704
7705 return vectors;
7706}
7707
7708/**
7709 * i40e_init_msix - Setup the MSIX capability
7710 * @pf: board private structure
7711 *
7712 * Work with the OS to set up the MSIX vectors needed.
7713 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007714 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007715 **/
7716static int i40e_init_msix(struct i40e_pf *pf)
7717{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007718 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007719 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007720 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007721 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007722 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007723
7724 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7725 return -ENODEV;
7726
7727 /* The number of vectors we'll request will be comprised of:
7728 * - Add 1 for "other" cause for Admin Queue events, etc.
7729 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007730 * - Queues being used for RSS.
7731 * We don't need as many as max_rss_size vectors.
7732 * use rss_size instead in the calculation since that
7733 * is governed by number of cpus in the system.
7734 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007735 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007736 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007737#ifdef I40E_FCOE
7738 * - The number of FCOE qps.
7739#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007740 * Once we count this up, try the request.
7741 *
7742 * If we can't get what we want, we'll simplify to nearly nothing
7743 * and try again. If that still fails, we punt.
7744 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007745 vectors_left = hw->func_caps.num_msix_vectors;
7746 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007747
Shannon Nelson1e200e42015-02-27 09:15:24 +00007748 /* reserve one vector for miscellaneous handler */
7749 if (vectors_left) {
7750 v_budget++;
7751 vectors_left--;
7752 }
7753
7754 /* reserve vectors for the main PF traffic queues */
7755 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7756 vectors_left -= pf->num_lan_msix;
7757 v_budget += pf->num_lan_msix;
7758
7759 /* reserve one vector for sideband flow director */
7760 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7761 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007762 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007763 v_budget++;
7764 vectors_left--;
7765 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007766 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007767 }
7768 }
John W Linville83840e42015-01-14 03:06:28 +00007769
Vasu Dev38e00432014-08-01 13:27:03 -07007770#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007771 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007772 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007773 if (!vectors_left)
7774 pf->num_fcoe_msix = 0;
7775 else if (vectors_left >= pf->num_fcoe_qps)
7776 pf->num_fcoe_msix = pf->num_fcoe_qps;
7777 else
7778 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007779 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007780 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007781 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007782
Vasu Dev38e00432014-08-01 13:27:03 -07007783#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007784 /* can we reserve enough for iWARP? */
7785 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007786 iwarp_requested = pf->num_iwarp_msix;
7787
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007788 if (!vectors_left)
7789 pf->num_iwarp_msix = 0;
7790 else if (vectors_left < pf->num_iwarp_msix)
7791 pf->num_iwarp_msix = 1;
7792 v_budget += pf->num_iwarp_msix;
7793 vectors_left -= pf->num_iwarp_msix;
7794 }
7795
Shannon Nelson1e200e42015-02-27 09:15:24 +00007796 /* any vectors left over go for VMDq support */
7797 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7798 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7799 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7800
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007801 if (!vectors_left) {
7802 pf->num_vmdq_msix = 0;
7803 pf->num_vmdq_qps = 0;
7804 } else {
7805 /* if we're short on vectors for what's desired, we limit
7806 * the queues per vmdq. If this is still more than are
7807 * available, the user will need to change the number of
7808 * queues/vectors used by the PF later with the ethtool
7809 * channels command
7810 */
7811 if (vmdq_vecs < vmdq_vecs_wanted)
7812 pf->num_vmdq_qps = 1;
7813 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007814
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007815 v_budget += vmdq_vecs;
7816 vectors_left -= vmdq_vecs;
7817 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007818 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007819
7820 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7821 GFP_KERNEL);
7822 if (!pf->msix_entries)
7823 return -ENOMEM;
7824
7825 for (i = 0; i < v_budget; i++)
7826 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007827 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007828
Shannon Nelson3b444392015-02-26 16:15:57 +00007829 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007830 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7831 kfree(pf->msix_entries);
7832 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007833 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007834 return -ENODEV;
7835
Shannon Nelson3b444392015-02-26 16:15:57 +00007836 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007838 pf->num_vmdq_vsis = 0;
7839 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007840 pf->num_lan_qps = 1;
7841 pf->num_lan_msix = 1;
7842
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007843 } else if (!vectors_left) {
7844 /* If we have limited resources, we will start with no vectors
7845 * for the special features and then allocate vectors to some
7846 * of these features based on the policy and at the end disable
7847 * the features that did not get any vectors.
7848 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007849 int vec;
7850
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007851 dev_info(&pf->pdev->dev,
7852 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007853 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007854 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007856 /* Scale vector usage down */
7857 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007858 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007859 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007860#ifdef I40E_FCOE
7861 pf->num_fcoe_qps = 0;
7862 pf->num_fcoe_msix = 0;
7863#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007864
7865 /* partition out the remaining vectors */
7866 switch (vec) {
7867 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007868 pf->num_lan_msix = 1;
7869 break;
7870 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007871 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7872 pf->num_lan_msix = 1;
7873 pf->num_iwarp_msix = 1;
7874 } else {
7875 pf->num_lan_msix = 2;
7876 }
Vasu Dev38e00432014-08-01 13:27:03 -07007877#ifdef I40E_FCOE
7878 /* give one vector to FCoE */
7879 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7880 pf->num_lan_msix = 1;
7881 pf->num_fcoe_msix = 1;
7882 }
Vasu Dev38e00432014-08-01 13:27:03 -07007883#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007884 break;
7885 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007886 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7887 pf->num_iwarp_msix = min_t(int, (vec / 3),
7888 iwarp_requested);
7889 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7890 I40E_DEFAULT_NUM_VMDQ_VSI);
7891 } else {
7892 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7893 I40E_DEFAULT_NUM_VMDQ_VSI);
7894 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007895 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7896 pf->num_fdsb_msix = 1;
7897 vec--;
7898 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007899 pf->num_lan_msix = min_t(int,
7900 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7901 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007902 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007903#ifdef I40E_FCOE
7904 /* give one vector to FCoE */
7905 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7906 pf->num_fcoe_msix = 1;
7907 vec--;
7908 }
7909#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007910 break;
7911 }
7912 }
7913
Stefan Assmannabd97a92016-09-19 13:37:51 +02007914 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7915 (pf->num_fdsb_msix == 0)) {
7916 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7917 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7918 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007919 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7920 (pf->num_vmdq_msix == 0)) {
7921 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7922 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7923 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007924
7925 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7926 (pf->num_iwarp_msix == 0)) {
7927 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7928 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7929 }
Vasu Dev38e00432014-08-01 13:27:03 -07007930#ifdef I40E_FCOE
7931
7932 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7933 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7934 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7935 }
7936#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007937 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7938 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7939 pf->num_lan_msix,
7940 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7941 pf->num_fdsb_msix,
7942 pf->num_iwarp_msix);
7943
Shannon Nelson3b444392015-02-26 16:15:57 +00007944 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007945}
7946
7947/**
Greg Rose90e04072014-03-06 08:59:57 +00007948 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007949 * @vsi: the VSI being configured
7950 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007951 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007952 *
7953 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7954 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007955static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007956{
7957 struct i40e_q_vector *q_vector;
7958
7959 /* allocate q_vector */
7960 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7961 if (!q_vector)
7962 return -ENOMEM;
7963
7964 q_vector->vsi = vsi;
7965 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007966 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7967
Alexander Duyck493fb302013-09-28 07:01:44 +00007968 if (vsi->netdev)
7969 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007970 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007971
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007972 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7973 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7974
Alexander Duyck493fb302013-09-28 07:01:44 +00007975 /* tie q_vector and vsi together */
7976 vsi->q_vectors[v_idx] = q_vector;
7977
7978 return 0;
7979}
7980
7981/**
Greg Rose90e04072014-03-06 08:59:57 +00007982 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983 * @vsi: the VSI being configured
7984 *
7985 * We allocate one q_vector per queue interrupt. If allocation fails we
7986 * return -ENOMEM.
7987 **/
Greg Rose90e04072014-03-06 08:59:57 +00007988static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007989{
7990 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007991 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007992
7993 /* if not MSIX, give the one vector only to the LAN VSI */
7994 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7995 num_q_vectors = vsi->num_q_vectors;
7996 else if (vsi == pf->vsi[pf->lan_vsi])
7997 num_q_vectors = 1;
7998 else
7999 return -EINVAL;
8000
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008001 current_cpu = cpumask_first(cpu_online_mask);
8002
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008003 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008004 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008005 if (err)
8006 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008007 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8008 if (unlikely(current_cpu >= nr_cpu_ids))
8009 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008010 }
8011
8012 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008013
8014err_out:
8015 while (v_idx--)
8016 i40e_free_q_vector(vsi, v_idx);
8017
8018 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008019}
8020
8021/**
8022 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8023 * @pf: board private structure to initialize
8024 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008025static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008026{
Shannon Nelson3b444392015-02-26 16:15:57 +00008027 int vectors = 0;
8028 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008029
8030 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008031 vectors = i40e_init_msix(pf);
8032 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008033 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008034 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07008035#ifdef I40E_FCOE
8036 I40E_FLAG_FCOE_ENABLED |
8037#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008038 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008039 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008040 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008041 I40E_FLAG_SRIOV_ENABLED |
8042 I40E_FLAG_FD_SB_ENABLED |
8043 I40E_FLAG_FD_ATR_ENABLED |
8044 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008045
8046 /* rework the queue expectations without MSIX */
8047 i40e_determine_queue_usage(pf);
8048 }
8049 }
8050
8051 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8052 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008053 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008054 vectors = pci_enable_msi(pf->pdev);
8055 if (vectors < 0) {
8056 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8057 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008058 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8059 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008060 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008061 }
8062
Shannon Nelson958a3e32013-09-28 07:13:28 +00008063 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008064 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008065
Shannon Nelson3b444392015-02-26 16:15:57 +00008066 /* set up vector assignment tracking */
8067 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8068 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008069 if (!pf->irq_pile) {
8070 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8071 return -ENOMEM;
8072 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008073 pf->irq_pile->num_entries = vectors;
8074 pf->irq_pile->search_hint = 0;
8075
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008076 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008077 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008078
8079 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080}
8081
8082/**
8083 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8084 * @pf: board private structure
8085 *
8086 * This sets up the handler for MSIX 0, which is used to manage the
8087 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8088 * when in MSI or Legacy interrupt mode.
8089 **/
8090static int i40e_setup_misc_vector(struct i40e_pf *pf)
8091{
8092 struct i40e_hw *hw = &pf->hw;
8093 int err = 0;
8094
8095 /* Only request the irq if this is the first time through, and
8096 * not when we're rebuilding after a Reset
8097 */
8098 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8099 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008100 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008101 if (err) {
8102 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008103 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008104 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008105 return -EFAULT;
8106 }
8107 }
8108
Jacob Kellerab437b52014-12-14 01:55:08 +00008109 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008110
8111 /* associate no queues to the misc vector */
8112 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8113 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8114
8115 i40e_flush(hw);
8116
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008117 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008118
8119 return err;
8120}
8121
8122/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008123 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8124 * @vsi: vsi structure
8125 * @seed: RSS hash seed
8126 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008127static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8128 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008129{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008130 struct i40e_pf *pf = vsi->back;
8131 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008132 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008133
Jacob Keller776b2e12016-07-19 16:23:30 -07008134 if (seed) {
8135 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8136 (struct i40e_aqc_get_set_rss_key_data *)seed;
8137 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8138 if (ret) {
8139 dev_info(&pf->pdev->dev,
8140 "Cannot set RSS key, err %s aq_err %s\n",
8141 i40e_stat_str(hw, ret),
8142 i40e_aq_str(hw, hw->aq.asq_last_status));
8143 return ret;
8144 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008145 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008146 if (lut) {
8147 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008148
Jacob Keller776b2e12016-07-19 16:23:30 -07008149 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8150 if (ret) {
8151 dev_info(&pf->pdev->dev,
8152 "Cannot set RSS lut, err %s aq_err %s\n",
8153 i40e_stat_str(hw, ret),
8154 i40e_aq_str(hw, hw->aq.asq_last_status));
8155 return ret;
8156 }
8157 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008158 return ret;
8159}
8160
8161/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008162 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8163 * @vsi: Pointer to vsi structure
8164 * @seed: Buffter to store the hash keys
8165 * @lut: Buffer to store the lookup table entries
8166 * @lut_size: Size of buffer to store the lookup table entries
8167 *
8168 * Return 0 on success, negative on failure
8169 */
8170static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8171 u8 *lut, u16 lut_size)
8172{
8173 struct i40e_pf *pf = vsi->back;
8174 struct i40e_hw *hw = &pf->hw;
8175 int ret = 0;
8176
8177 if (seed) {
8178 ret = i40e_aq_get_rss_key(hw, vsi->id,
8179 (struct i40e_aqc_get_set_rss_key_data *)seed);
8180 if (ret) {
8181 dev_info(&pf->pdev->dev,
8182 "Cannot get RSS key, err %s aq_err %s\n",
8183 i40e_stat_str(&pf->hw, ret),
8184 i40e_aq_str(&pf->hw,
8185 pf->hw.aq.asq_last_status));
8186 return ret;
8187 }
8188 }
8189
8190 if (lut) {
8191 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8192
8193 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8194 if (ret) {
8195 dev_info(&pf->pdev->dev,
8196 "Cannot get RSS lut, err %s aq_err %s\n",
8197 i40e_stat_str(&pf->hw, ret),
8198 i40e_aq_str(&pf->hw,
8199 pf->hw.aq.asq_last_status));
8200 return ret;
8201 }
8202 }
8203
8204 return ret;
8205}
8206
8207/**
Jacob Keller0582b962016-07-19 16:23:29 -07008208 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8209 * @vsi: VSI structure
8210 **/
8211static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8212{
8213 u8 seed[I40E_HKEY_ARRAY_SIZE];
8214 struct i40e_pf *pf = vsi->back;
8215 u8 *lut;
8216 int ret;
8217
8218 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8219 return 0;
8220
Jacob Keller552b9962016-07-19 16:23:31 -07008221 if (!vsi->rss_size)
8222 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8223 vsi->num_queue_pairs);
8224 if (!vsi->rss_size)
8225 return -EINVAL;
8226
Jacob Keller0582b962016-07-19 16:23:29 -07008227 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8228 if (!lut)
8229 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008230 /* Use the user configured hash keys and lookup table if there is one,
8231 * otherwise use default
8232 */
8233 if (vsi->rss_lut_user)
8234 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8235 else
8236 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8237 if (vsi->rss_hkey_user)
8238 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8239 else
8240 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008241 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8242 kfree(lut);
8243
8244 return ret;
8245}
8246
8247/**
Helin Zhang043dd652015-10-21 19:56:23 -04008248 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008249 * @vsi: Pointer to vsi structure
8250 * @seed: RSS hash seed
8251 * @lut: Lookup table
8252 * @lut_size: Lookup table size
8253 *
8254 * Returns 0 on success, negative on failure
8255 **/
8256static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8257 const u8 *lut, u16 lut_size)
8258{
8259 struct i40e_pf *pf = vsi->back;
8260 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008261 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008262 u8 i;
8263
8264 /* Fill out hash function seed */
8265 if (seed) {
8266 u32 *seed_dw = (u32 *)seed;
8267
Mitch Williamsc4e18682016-04-12 08:30:40 -07008268 if (vsi->type == I40E_VSI_MAIN) {
8269 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8270 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8271 seed_dw[i]);
8272 } else if (vsi->type == I40E_VSI_SRIOV) {
8273 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8274 i40e_write_rx_ctl(hw,
8275 I40E_VFQF_HKEY1(i, vf_id),
8276 seed_dw[i]);
8277 } else {
8278 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8279 }
Helin Zhange69ff812015-10-21 19:56:22 -04008280 }
8281
8282 if (lut) {
8283 u32 *lut_dw = (u32 *)lut;
8284
Mitch Williamsc4e18682016-04-12 08:30:40 -07008285 if (vsi->type == I40E_VSI_MAIN) {
8286 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8287 return -EINVAL;
8288 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8289 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8290 } else if (vsi->type == I40E_VSI_SRIOV) {
8291 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8292 return -EINVAL;
8293 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8294 i40e_write_rx_ctl(hw,
8295 I40E_VFQF_HLUT1(i, vf_id),
8296 lut_dw[i]);
8297 } else {
8298 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8299 }
Helin Zhange69ff812015-10-21 19:56:22 -04008300 }
8301 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008302
8303 return 0;
8304}
8305
8306/**
Helin Zhang043dd652015-10-21 19:56:23 -04008307 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8308 * @vsi: Pointer to VSI structure
8309 * @seed: Buffer to store the keys
8310 * @lut: Buffer to store the lookup table entries
8311 * @lut_size: Size of buffer to store the lookup table entries
8312 *
8313 * Returns 0 on success, negative on failure
8314 */
8315static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8316 u8 *lut, u16 lut_size)
8317{
8318 struct i40e_pf *pf = vsi->back;
8319 struct i40e_hw *hw = &pf->hw;
8320 u16 i;
8321
8322 if (seed) {
8323 u32 *seed_dw = (u32 *)seed;
8324
8325 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008326 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008327 }
8328 if (lut) {
8329 u32 *lut_dw = (u32 *)lut;
8330
8331 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8332 return -EINVAL;
8333 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8334 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8335 }
8336
8337 return 0;
8338}
8339
8340/**
8341 * i40e_config_rss - Configure RSS keys and lut
8342 * @vsi: Pointer to VSI structure
8343 * @seed: RSS hash seed
8344 * @lut: Lookup table
8345 * @lut_size: Lookup table size
8346 *
8347 * Returns 0 on success, negative on failure
8348 */
8349int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8350{
8351 struct i40e_pf *pf = vsi->back;
8352
8353 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8354 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8355 else
8356 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8357}
8358
8359/**
8360 * i40e_get_rss - Get RSS keys and lut
8361 * @vsi: Pointer to VSI structure
8362 * @seed: Buffer to store the keys
8363 * @lut: Buffer to store the lookup table entries
8364 * lut_size: Size of buffer to store the lookup table entries
8365 *
8366 * Returns 0 on success, negative on failure
8367 */
8368int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8369{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008370 struct i40e_pf *pf = vsi->back;
8371
8372 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8373 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8374 else
8375 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008376}
8377
8378/**
Helin Zhange69ff812015-10-21 19:56:22 -04008379 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8380 * @pf: Pointer to board private structure
8381 * @lut: Lookup table
8382 * @rss_table_size: Lookup table size
8383 * @rss_size: Range of queue number for hashing
8384 */
Alan Bradyf1582352016-08-24 11:33:46 -07008385void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8386 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008387{
Helin Zhange69ff812015-10-21 19:56:22 -04008388 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008389
Helin Zhange69ff812015-10-21 19:56:22 -04008390 for (i = 0; i < rss_table_size; i++)
8391 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008392}
8393
8394/**
Helin Zhang043dd652015-10-21 19:56:23 -04008395 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008396 * @pf: board private structure
8397 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008398static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008399{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008400 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008401 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008402 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008403 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008404 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008405 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008406 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008407
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008408 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008409 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8410 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008411 hena |= i40e_pf_get_default_rss_hena(pf);
8412
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008413 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8414 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008415
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008416 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008417 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008418 reg_val = (pf->rss_table_size == 512) ?
8419 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8420 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008421 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008422
Helin Zhang28c58692015-10-26 19:44:27 -04008423 /* Determine the RSS size of the VSI */
8424 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008425 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8426 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008427 if (!vsi->rss_size)
8428 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008429
Helin Zhange69ff812015-10-21 19:56:22 -04008430 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8431 if (!lut)
8432 return -ENOMEM;
8433
Helin Zhang28c58692015-10-26 19:44:27 -04008434 /* Use user configured lut if there is one, otherwise use default */
8435 if (vsi->rss_lut_user)
8436 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8437 else
8438 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008439
Helin Zhang28c58692015-10-26 19:44:27 -04008440 /* Use user configured hash key if there is one, otherwise
8441 * use default.
8442 */
8443 if (vsi->rss_hkey_user)
8444 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8445 else
8446 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008447 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008448 kfree(lut);
8449
8450 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008451}
8452
8453/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008454 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8455 * @pf: board private structure
8456 * @queue_count: the requested queue count for rss.
8457 *
8458 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8459 * count which may be different from the requested queue count.
8460 **/
8461int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8462{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008463 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8464 int new_rss_size;
8465
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008466 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8467 return 0;
8468
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008469 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008470
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008471 if (queue_count != vsi->num_queue_pairs) {
8472 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008473 i40e_prep_for_reset(pf);
8474
Helin Zhangacd65442015-10-26 19:44:28 -04008475 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008476
8477 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008478
8479 /* Discard the user configured hash keys and lut, if less
8480 * queues are enabled.
8481 */
8482 if (queue_count < vsi->rss_size) {
8483 i40e_clear_rss_config_user(vsi);
8484 dev_dbg(&pf->pdev->dev,
8485 "discard user configured hash keys and lut\n");
8486 }
8487
8488 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008489 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8490 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008491
Helin Zhang043dd652015-10-21 19:56:23 -04008492 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008493 }
Lihong Yang12815052016-09-27 11:28:48 -07008494 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8495 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008496 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008497}
8498
8499/**
Greg Rosef4492db2015-02-06 08:52:12 +00008500 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8501 * @pf: board private structure
8502 **/
8503i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8504{
8505 i40e_status status;
8506 bool min_valid, max_valid;
8507 u32 max_bw, min_bw;
8508
8509 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8510 &min_valid, &max_valid);
8511
8512 if (!status) {
8513 if (min_valid)
8514 pf->npar_min_bw = min_bw;
8515 if (max_valid)
8516 pf->npar_max_bw = max_bw;
8517 }
8518
8519 return status;
8520}
8521
8522/**
8523 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8524 * @pf: board private structure
8525 **/
8526i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8527{
8528 struct i40e_aqc_configure_partition_bw_data bw_data;
8529 i40e_status status;
8530
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008531 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008532 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008533 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8534 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8535
8536 /* Set the new bandwidths */
8537 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8538
8539 return status;
8540}
8541
8542/**
8543 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8544 * @pf: board private structure
8545 **/
8546i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8547{
8548 /* Commit temporary BW setting to permanent NVM image */
8549 enum i40e_admin_queue_err last_aq_status;
8550 i40e_status ret;
8551 u16 nvm_word;
8552
8553 if (pf->hw.partition_id != 1) {
8554 dev_info(&pf->pdev->dev,
8555 "Commit BW only works on partition 1! This is partition %d",
8556 pf->hw.partition_id);
8557 ret = I40E_NOT_SUPPORTED;
8558 goto bw_commit_out;
8559 }
8560
8561 /* Acquire NVM for read access */
8562 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8563 last_aq_status = pf->hw.aq.asq_last_status;
8564 if (ret) {
8565 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008566 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8567 i40e_stat_str(&pf->hw, ret),
8568 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008569 goto bw_commit_out;
8570 }
8571
8572 /* Read word 0x10 of NVM - SW compatibility word 1 */
8573 ret = i40e_aq_read_nvm(&pf->hw,
8574 I40E_SR_NVM_CONTROL_WORD,
8575 0x10, sizeof(nvm_word), &nvm_word,
8576 false, NULL);
8577 /* Save off last admin queue command status before releasing
8578 * the NVM
8579 */
8580 last_aq_status = pf->hw.aq.asq_last_status;
8581 i40e_release_nvm(&pf->hw);
8582 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008583 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8584 i40e_stat_str(&pf->hw, ret),
8585 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008586 goto bw_commit_out;
8587 }
8588
8589 /* Wait a bit for NVM release to complete */
8590 msleep(50);
8591
8592 /* Acquire NVM for write access */
8593 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8594 last_aq_status = pf->hw.aq.asq_last_status;
8595 if (ret) {
8596 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008597 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8598 i40e_stat_str(&pf->hw, ret),
8599 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008600 goto bw_commit_out;
8601 }
8602 /* Write it back out unchanged to initiate update NVM,
8603 * which will force a write of the shadow (alt) RAM to
8604 * the NVM - thus storing the bandwidth values permanently.
8605 */
8606 ret = i40e_aq_update_nvm(&pf->hw,
8607 I40E_SR_NVM_CONTROL_WORD,
8608 0x10, sizeof(nvm_word),
8609 &nvm_word, true, NULL);
8610 /* Save off last admin queue command status before releasing
8611 * the NVM
8612 */
8613 last_aq_status = pf->hw.aq.asq_last_status;
8614 i40e_release_nvm(&pf->hw);
8615 if (ret)
8616 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008617 "BW settings NOT SAVED, err %s aq_err %s\n",
8618 i40e_stat_str(&pf->hw, ret),
8619 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008620bw_commit_out:
8621
8622 return ret;
8623}
8624
8625/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008626 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8627 * @pf: board private structure to initialize
8628 *
8629 * i40e_sw_init initializes the Adapter private data structure.
8630 * Fields are initialized based on PCI device information and
8631 * OS network device settings (MTU size).
8632 **/
8633static int i40e_sw_init(struct i40e_pf *pf)
8634{
8635 int err = 0;
8636 int size;
8637
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008638 /* Set default capability flags */
8639 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8640 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008641 I40E_FLAG_MSIX_ENABLED;
8642
Mitch Williamsca99eb92014-04-04 04:43:07 +00008643 /* Set default ITR */
8644 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8645 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8646
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008647 /* Depending on PF configurations, it is possible that the RSS
8648 * maximum might end up larger than the available queues
8649 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008650 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008651 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008652 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008653 pf->rss_size_max = min_t(int, pf->rss_size_max,
8654 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008655 if (pf->hw.func_caps.rss) {
8656 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008657 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8658 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008659 }
8660
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008661 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008662 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008663 pf->flags |= I40E_FLAG_MFP_ENABLED;
8664 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008665 if (i40e_get_npar_bw_setting(pf))
8666 dev_warn(&pf->pdev->dev,
8667 "Could not get NPAR bw settings\n");
8668 else
8669 dev_info(&pf->pdev->dev,
8670 "Min BW = %8.8x, Max BW = %8.8x\n",
8671 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008672 }
8673
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008674 /* FW/NVM is not yet fixed in this regard */
8675 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8676 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8677 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8678 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008679 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8680 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008681 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008682 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008683 else
8684 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008685 pf->fdir_pf_filter_count =
8686 pf->hw.func_caps.fd_filters_guaranteed;
8687 pf->hw.fdir_shared_filter_count =
8688 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008689 }
8690
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008691 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008692 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008693 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008694 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008695 /* No DCB support for FW < v4.33 */
8696 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8697 }
8698
8699 /* Disable FW LLDP if FW < v4.3 */
8700 if (i40e_is_mac_710(&pf->hw) &&
8701 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8702 (pf->hw.aq.fw_maj_ver < 4)))
8703 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8704
8705 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8706 if (i40e_is_mac_710(&pf->hw) &&
8707 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8708 (pf->hw.aq.fw_maj_ver >= 5)))
8709 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008710
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008711 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008712 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008713 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008714 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008715 }
8716
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008717 if (pf->hw.func_caps.iwarp) {
8718 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8719 /* IWARP needs one extra vector for CQP just like MISC.*/
8720 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8721 }
8722
Vasu Dev38e00432014-08-01 13:27:03 -07008723#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008724 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008725
8726#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008727#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008728 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008729 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8730 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8731 pf->num_req_vfs = min_t(int,
8732 pf->hw.func_caps.num_vfs,
8733 I40E_MAX_VF_COUNT);
8734 }
8735#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008736 if (pf->hw.mac.type == I40E_MAC_X722) {
8737 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8738 I40E_FLAG_128_QP_RSS_CAPABLE |
8739 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8740 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8741 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008742 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008743 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008744 I40E_FLAG_USE_SET_LLDP_MIB |
Jacob Keller1e28e862016-11-11 12:39:25 -08008745 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE |
8746 I40E_FLAG_PTP_L4_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008747 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8748 ((pf->hw.aq.api_maj_ver == 1) &&
8749 (pf->hw.aq.api_min_ver > 4))) {
8750 /* Supported in FW API version higher than 1.4 */
8751 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008752 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8753 } else {
8754 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008755 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008756
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008757 pf->eeprom_version = 0xDEAD;
8758 pf->lan_veb = I40E_NO_VEB;
8759 pf->lan_vsi = I40E_NO_VSI;
8760
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008761 /* By default FW has this off for performance reasons */
8762 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008764 /* set up queue assignment tracking */
8765 size = sizeof(struct i40e_lump_tracking)
8766 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8767 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8768 if (!pf->qp_pile) {
8769 err = -ENOMEM;
8770 goto sw_init_done;
8771 }
8772 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8773 pf->qp_pile->search_hint = 0;
8774
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008775 pf->tx_timeout_recovery_level = 1;
8776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008777 mutex_init(&pf->switch_mutex);
8778
Greg Rosec668a122015-02-26 16:10:39 +00008779 /* If NPAR is enabled nudge the Tx scheduler */
8780 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8781 i40e_set_npar_bw_setting(pf);
8782
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008783sw_init_done:
8784 return err;
8785}
8786
8787/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008788 * i40e_set_ntuple - set the ntuple feature flag and take action
8789 * @pf: board private structure to initialize
8790 * @features: the feature set that the stack is suggesting
8791 *
8792 * returns a bool to indicate if reset needs to happen
8793 **/
8794bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8795{
8796 bool need_reset = false;
8797
8798 /* Check if Flow Director n-tuple support was enabled or disabled. If
8799 * the state changed, we need to reset.
8800 */
8801 if (features & NETIF_F_NTUPLE) {
8802 /* Enable filters and mark for reset */
8803 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8804 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008805 /* enable FD_SB only if there is MSI-X vector */
8806 if (pf->num_fdsb_msix > 0)
8807 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008808 } else {
8809 /* turn off filters, mark for reset and clear SW filter list */
8810 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8811 need_reset = true;
8812 i40e_fdir_filter_exit(pf);
8813 }
8814 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008815 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008816 /* reset fd counters */
8817 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8818 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008819 /* if ATR was auto disabled it can be re-enabled. */
8820 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008821 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008822 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008823 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8824 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8825 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008826 }
8827 return need_reset;
8828}
8829
8830/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008831 * i40e_clear_rss_lut - clear the rx hash lookup table
8832 * @vsi: the VSI being configured
8833 **/
8834static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8835{
8836 struct i40e_pf *pf = vsi->back;
8837 struct i40e_hw *hw = &pf->hw;
8838 u16 vf_id = vsi->vf_id;
8839 u8 i;
8840
8841 if (vsi->type == I40E_VSI_MAIN) {
8842 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8843 wr32(hw, I40E_PFQF_HLUT(i), 0);
8844 } else if (vsi->type == I40E_VSI_SRIOV) {
8845 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8846 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8847 } else {
8848 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8849 }
8850}
8851
8852/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008853 * i40e_set_features - set the netdev feature flags
8854 * @netdev: ptr to the netdev being adjusted
8855 * @features: the feature set that the stack is suggesting
8856 **/
8857static int i40e_set_features(struct net_device *netdev,
8858 netdev_features_t features)
8859{
8860 struct i40e_netdev_priv *np = netdev_priv(netdev);
8861 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008862 struct i40e_pf *pf = vsi->back;
8863 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008864
Alan Bradyd8ec9862016-07-27 12:02:38 -07008865 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8866 i40e_pf_config_rss(pf);
8867 else if (!(features & NETIF_F_RXHASH) &&
8868 netdev->features & NETIF_F_RXHASH)
8869 i40e_clear_rss_lut(vsi);
8870
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008871 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8872 i40e_vlan_stripping_enable(vsi);
8873 else
8874 i40e_vlan_stripping_disable(vsi);
8875
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008876 need_reset = i40e_set_ntuple(pf, features);
8877
8878 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008879 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008880
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008881 return 0;
8882}
8883
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008884/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008885 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008886 * @pf: board private structure
8887 * @port: The UDP port to look up
8888 *
8889 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8890 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008891static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008892{
8893 u8 i;
8894
8895 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008896 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008897 return i;
8898 }
8899
8900 return i;
8901}
8902
8903/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008904 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008905 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008906 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008907 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008908static void i40e_udp_tunnel_add(struct net_device *netdev,
8909 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008910{
8911 struct i40e_netdev_priv *np = netdev_priv(netdev);
8912 struct i40e_vsi *vsi = np->vsi;
8913 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008914 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008915 u8 next_idx;
8916 u8 idx;
8917
Singhai, Anjali6a899022015-12-14 12:21:18 -08008918 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008919
8920 /* Check if port already exists */
8921 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008922 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008923 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008924 return;
8925 }
8926
8927 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008928 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008929
8930 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008931 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008932 ntohs(port));
8933 return;
8934 }
8935
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008936 switch (ti->type) {
8937 case UDP_TUNNEL_TYPE_VXLAN:
8938 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8939 break;
8940 case UDP_TUNNEL_TYPE_GENEVE:
8941 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8942 return;
8943 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8944 break;
8945 default:
8946 return;
8947 }
8948
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008949 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008950 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008951 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8952 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008953}
8954
8955/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008956 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008957 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008958 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008959 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008960static void i40e_udp_tunnel_del(struct net_device *netdev,
8961 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008962{
8963 struct i40e_netdev_priv *np = netdev_priv(netdev);
8964 struct i40e_vsi *vsi = np->vsi;
8965 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008966 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008967 u8 idx;
8968
Singhai, Anjali6a899022015-12-14 12:21:18 -08008969 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008970
8971 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008972 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8973 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008974
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008975 switch (ti->type) {
8976 case UDP_TUNNEL_TYPE_VXLAN:
8977 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8978 goto not_found;
8979 break;
8980 case UDP_TUNNEL_TYPE_GENEVE:
8981 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8982 goto not_found;
8983 break;
8984 default:
8985 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008986 }
8987
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008988 /* if port exists, set it to 0 (mark for deletion)
8989 * and make it pending
8990 */
8991 pf->udp_ports[idx].index = 0;
8992 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008993 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8994
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008995 return;
8996not_found:
8997 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8998 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008999}
9000
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009001static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009002 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009003{
9004 struct i40e_netdev_priv *np = netdev_priv(netdev);
9005 struct i40e_pf *pf = np->vsi->back;
9006 struct i40e_hw *hw = &pf->hw;
9007
9008 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9009 return -EOPNOTSUPP;
9010
9011 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9012 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9013
9014 return 0;
9015}
9016
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009017/**
9018 * i40e_ndo_fdb_add - add an entry to the hardware database
9019 * @ndm: the input from the stack
9020 * @tb: pointer to array of nladdr (unused)
9021 * @dev: the net device pointer
9022 * @addr: the MAC address entry being added
9023 * @flags: instructions from stack about fdb operation
9024 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009025static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9026 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009027 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009028 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009029{
9030 struct i40e_netdev_priv *np = netdev_priv(dev);
9031 struct i40e_pf *pf = np->vsi->back;
9032 int err = 0;
9033
9034 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9035 return -EOPNOTSUPP;
9036
Or Gerlitz65891fe2014-12-14 18:19:05 +02009037 if (vid) {
9038 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9039 return -EINVAL;
9040 }
9041
Greg Rose4ba0dea2014-03-06 08:59:55 +00009042 /* Hardware does not support aging addresses so if a
9043 * ndm_state is given only allow permanent addresses
9044 */
9045 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9046 netdev_info(dev, "FDB only supports static addresses\n");
9047 return -EINVAL;
9048 }
9049
9050 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9051 err = dev_uc_add_excl(dev, addr);
9052 else if (is_multicast_ether_addr(addr))
9053 err = dev_mc_add_excl(dev, addr);
9054 else
9055 err = -EINVAL;
9056
9057 /* Only return duplicate errors if NLM_F_EXCL is set */
9058 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9059 err = 0;
9060
9061 return err;
9062}
9063
Neerav Parikh51616012015-02-06 08:52:14 +00009064/**
9065 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9066 * @dev: the netdev being configured
9067 * @nlh: RTNL message
9068 *
9069 * Inserts a new hardware bridge if not already created and
9070 * enables the bridging mode requested (VEB or VEPA). If the
9071 * hardware bridge has already been inserted and the request
9072 * is to change the mode then that requires a PF reset to
9073 * allow rebuild of the components with required hardware
9074 * bridge mode enabled.
9075 **/
9076static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009077 struct nlmsghdr *nlh,
9078 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009079{
9080 struct i40e_netdev_priv *np = netdev_priv(dev);
9081 struct i40e_vsi *vsi = np->vsi;
9082 struct i40e_pf *pf = vsi->back;
9083 struct i40e_veb *veb = NULL;
9084 struct nlattr *attr, *br_spec;
9085 int i, rem;
9086
9087 /* Only for PF VSI for now */
9088 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9089 return -EOPNOTSUPP;
9090
9091 /* Find the HW bridge for PF VSI */
9092 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9093 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9094 veb = pf->veb[i];
9095 }
9096
9097 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9098
9099 nla_for_each_nested(attr, br_spec, rem) {
9100 __u16 mode;
9101
9102 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9103 continue;
9104
9105 mode = nla_get_u16(attr);
9106 if ((mode != BRIDGE_MODE_VEPA) &&
9107 (mode != BRIDGE_MODE_VEB))
9108 return -EINVAL;
9109
9110 /* Insert a new HW bridge */
9111 if (!veb) {
9112 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9113 vsi->tc_config.enabled_tc);
9114 if (veb) {
9115 veb->bridge_mode = mode;
9116 i40e_config_bridge_mode(veb);
9117 } else {
9118 /* No Bridge HW offload available */
9119 return -ENOENT;
9120 }
9121 break;
9122 } else if (mode != veb->bridge_mode) {
9123 /* Existing HW bridge but different mode needs reset */
9124 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009125 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9126 if (mode == BRIDGE_MODE_VEB)
9127 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9128 else
9129 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9130 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009131 break;
9132 }
9133 }
9134
9135 return 0;
9136}
9137
9138/**
9139 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9140 * @skb: skb buff
9141 * @pid: process id
9142 * @seq: RTNL message seq #
9143 * @dev: the netdev being configured
9144 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009145 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009146 *
9147 * Return the mode in which the hardware bridge is operating in
9148 * i.e VEB or VEPA.
9149 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009150static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9151 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009152 u32 __always_unused filter_mask,
9153 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009154{
9155 struct i40e_netdev_priv *np = netdev_priv(dev);
9156 struct i40e_vsi *vsi = np->vsi;
9157 struct i40e_pf *pf = vsi->back;
9158 struct i40e_veb *veb = NULL;
9159 int i;
9160
9161 /* Only for PF VSI for now */
9162 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9163 return -EOPNOTSUPP;
9164
9165 /* Find the HW bridge for the PF VSI */
9166 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9167 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9168 veb = pf->veb[i];
9169 }
9170
9171 if (!veb)
9172 return 0;
9173
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009174 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009175 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009176}
Neerav Parikh51616012015-02-06 08:52:14 +00009177
Joe Stringerf44a75e2015-04-14 17:09:14 -07009178/**
9179 * i40e_features_check - Validate encapsulated packet conforms to limits
9180 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009181 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009182 * @features: Offload features that the stack believes apply
9183 **/
9184static netdev_features_t i40e_features_check(struct sk_buff *skb,
9185 struct net_device *dev,
9186 netdev_features_t features)
9187{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009188 size_t len;
9189
9190 /* No point in doing any of this if neither checksum nor GSO are
9191 * being requested for this frame. We can rule out both by just
9192 * checking for CHECKSUM_PARTIAL
9193 */
9194 if (skb->ip_summed != CHECKSUM_PARTIAL)
9195 return features;
9196
9197 /* We cannot support GSO if the MSS is going to be less than
9198 * 64 bytes. If it is then we need to drop support for GSO.
9199 */
9200 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9201 features &= ~NETIF_F_GSO_MASK;
9202
9203 /* MACLEN can support at most 63 words */
9204 len = skb_network_header(skb) - skb->data;
9205 if (len & ~(63 * 2))
9206 goto out_err;
9207
9208 /* IPLEN and EIPLEN can support at most 127 dwords */
9209 len = skb_transport_header(skb) - skb_network_header(skb);
9210 if (len & ~(127 * 4))
9211 goto out_err;
9212
9213 if (skb->encapsulation) {
9214 /* L4TUNLEN can support 127 words */
9215 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9216 if (len & ~(127 * 2))
9217 goto out_err;
9218
9219 /* IPLEN can support at most 127 dwords */
9220 len = skb_inner_transport_header(skb) -
9221 skb_inner_network_header(skb);
9222 if (len & ~(127 * 4))
9223 goto out_err;
9224 }
9225
9226 /* No need to validate L4LEN as TCP is the only protocol with a
9227 * a flexible value and we support all possible values supported
9228 * by TCP, which is at most 15 dwords
9229 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009230
9231 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009232out_err:
9233 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009234}
9235
Shannon Nelson37a29732015-02-27 09:15:19 +00009236static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009237 .ndo_open = i40e_open,
9238 .ndo_stop = i40e_close,
9239 .ndo_start_xmit = i40e_lan_xmit_frame,
9240 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9241 .ndo_set_rx_mode = i40e_set_rx_mode,
9242 .ndo_validate_addr = eth_validate_addr,
9243 .ndo_set_mac_address = i40e_set_mac,
9244 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009245 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009246 .ndo_tx_timeout = i40e_tx_timeout,
9247 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9248 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9249#ifdef CONFIG_NET_POLL_CONTROLLER
9250 .ndo_poll_controller = i40e_netpoll,
9251#endif
John Fastabende4c67342016-02-16 21:16:15 -08009252 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009253#ifdef I40E_FCOE
9254 .ndo_fcoe_enable = i40e_fcoe_enable,
9255 .ndo_fcoe_disable = i40e_fcoe_disable,
9256#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009257 .ndo_set_features = i40e_set_features,
9258 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9259 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009260 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009261 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009262 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009263 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009264 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009265 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9266 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009267 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009268 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009269 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009270 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9271 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009272};
9273
9274/**
9275 * i40e_config_netdev - Setup the netdev flags
9276 * @vsi: the VSI being configured
9277 *
9278 * Returns 0 on success, negative value on failure
9279 **/
9280static int i40e_config_netdev(struct i40e_vsi *vsi)
9281{
9282 struct i40e_pf *pf = vsi->back;
9283 struct i40e_hw *hw = &pf->hw;
9284 struct i40e_netdev_priv *np;
9285 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009286 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009287 u8 mac_addr[ETH_ALEN];
9288 int etherdev_size;
9289
9290 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009291 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009292 if (!netdev)
9293 return -ENOMEM;
9294
9295 vsi->netdev = netdev;
9296 np = netdev_priv(netdev);
9297 np->vsi = vsi;
9298
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009299 netdev->hw_enc_features |= NETIF_F_SG |
9300 NETIF_F_IP_CSUM |
9301 NETIF_F_IPV6_CSUM |
9302 NETIF_F_HIGHDMA |
9303 NETIF_F_SOFT_FEATURES |
9304 NETIF_F_TSO |
9305 NETIF_F_TSO_ECN |
9306 NETIF_F_TSO6 |
9307 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009308 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009309 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009310 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009311 NETIF_F_GSO_UDP_TUNNEL |
9312 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009313 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009314 NETIF_F_SCTP_CRC |
9315 NETIF_F_RXHASH |
9316 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009317 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009318
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009319 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009320 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9321
9322 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009323
9324 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009325 netdev->vlan_features |= netdev->hw_enc_features |
9326 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009327
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009328 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009329 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009330
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009331 netdev->hw_features |= netdev->hw_enc_features |
9332 NETIF_F_HW_VLAN_CTAG_TX |
9333 NETIF_F_HW_VLAN_CTAG_RX;
9334
9335 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009336 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009337
9338 if (vsi->type == I40E_VSI_MAIN) {
9339 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009340 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009341 /* The following steps are necessary to prevent reception
9342 * of tagged packets - some older NVM configurations load a
9343 * default a MAC-VLAN filter that accepts any tagged packet
9344 * which must be replaced by a normal filter.
9345 */
9346 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009347 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009348 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009349 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009350 } else {
9351 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9352 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9353 pf->vsi[pf->lan_vsi]->netdev->name);
9354 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009355
Jacob Keller278e7d02016-10-05 09:30:37 -07009356 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009357 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009358 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009359 }
Kiran Patil21659032015-09-30 14:09:03 -04009360
Jacob Keller435c0842016-11-08 13:05:10 -08009361 /* Add the broadcast filter so that we initially will receive
9362 * broadcast packets. Note that when a new VLAN is first added the
9363 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9364 * specific filters as part of transitioning into "vlan" operation.
9365 * When more VLANs are added, the driver will copy each existing MAC
9366 * filter and add it for the new VLAN.
9367 *
9368 * Broadcast filters are handled specially by
9369 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9370 * promiscuous bit instead of adding this directly as a MAC/VLAN
9371 * filter. The subtask will update the correct broadcast promiscuous
9372 * bits as VLANs become active or inactive.
9373 */
9374 eth_broadcast_addr(broadcast);
9375 spin_lock_bh(&vsi->mac_filter_hash_lock);
9376 i40e_add_filter(vsi, broadcast, I40E_VLAN_ANY);
9377 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9378
Greg Rose9a173902014-05-22 06:32:02 +00009379 ether_addr_copy(netdev->dev_addr, mac_addr);
9380 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009382 netdev->priv_flags |= IFF_UNICAST_FLT;
9383 netdev->priv_flags |= IFF_SUPP_NOFCS;
9384 /* Setup netdev TC information */
9385 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9386
9387 netdev->netdev_ops = &i40e_netdev_ops;
9388 netdev->watchdog_timeo = 5 * HZ;
9389 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009390#ifdef I40E_FCOE
9391 i40e_fcoe_config_netdev(netdev, vsi);
9392#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009393
Jarod Wilson91c527a2016-10-17 15:54:05 -04009394 /* MTU range: 68 - 9706 */
9395 netdev->min_mtu = ETH_MIN_MTU;
9396 netdev->max_mtu = I40E_MAX_RXBUFFER -
9397 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9398
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009399 return 0;
9400}
9401
9402/**
9403 * i40e_vsi_delete - Delete a VSI from the switch
9404 * @vsi: the VSI being removed
9405 *
9406 * Returns 0 on success, negative value on failure
9407 **/
9408static void i40e_vsi_delete(struct i40e_vsi *vsi)
9409{
9410 /* remove default VSI is not allowed */
9411 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9412 return;
9413
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009414 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009415}
9416
9417/**
Neerav Parikh51616012015-02-06 08:52:14 +00009418 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9419 * @vsi: the VSI being queried
9420 *
9421 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9422 **/
9423int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9424{
9425 struct i40e_veb *veb;
9426 struct i40e_pf *pf = vsi->back;
9427
9428 /* Uplink is not a bridge so default to VEB */
9429 if (vsi->veb_idx == I40E_NO_VEB)
9430 return 1;
9431
9432 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009433 if (!veb) {
9434 dev_info(&pf->pdev->dev,
9435 "There is no veb associated with the bridge\n");
9436 return -ENOENT;
9437 }
Neerav Parikh51616012015-02-06 08:52:14 +00009438
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009439 /* Uplink is a bridge in VEPA mode */
9440 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9441 return 0;
9442 } else {
9443 /* Uplink is a bridge in VEB mode */
9444 return 1;
9445 }
9446
9447 /* VEPA is now default bridge, so return 0 */
9448 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009449}
9450
9451/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009452 * i40e_add_vsi - Add a VSI to the switch
9453 * @vsi: the VSI being configured
9454 *
9455 * This initializes a VSI context depending on the VSI type to be added and
9456 * passes it down to the add_vsi aq command.
9457 **/
9458static int i40e_add_vsi(struct i40e_vsi *vsi)
9459{
9460 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009461 struct i40e_pf *pf = vsi->back;
9462 struct i40e_hw *hw = &pf->hw;
9463 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009464 struct i40e_mac_filter *f;
9465 struct hlist_node *h;
9466 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009467
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009468 u8 enabled_tc = 0x1; /* TC0 enabled */
9469 int f_count = 0;
9470
9471 memset(&ctxt, 0, sizeof(ctxt));
9472 switch (vsi->type) {
9473 case I40E_VSI_MAIN:
9474 /* The PF's main VSI is already setup as part of the
9475 * device initialization, so we'll not bother with
9476 * the add_vsi call, but we will retrieve the current
9477 * VSI context.
9478 */
9479 ctxt.seid = pf->main_vsi_seid;
9480 ctxt.pf_num = pf->hw.pf_id;
9481 ctxt.vf_num = 0;
9482 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9483 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9484 if (ret) {
9485 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009486 "couldn't get PF vsi config, err %s aq_err %s\n",
9487 i40e_stat_str(&pf->hw, ret),
9488 i40e_aq_str(&pf->hw,
9489 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009490 return -ENOENT;
9491 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009492 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009493 vsi->info.valid_sections = 0;
9494
9495 vsi->seid = ctxt.seid;
9496 vsi->id = ctxt.vsi_number;
9497
9498 enabled_tc = i40e_pf_get_tc_map(pf);
9499
9500 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009501 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9502 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009503 memset(&ctxt, 0, sizeof(ctxt));
9504 ctxt.seid = pf->main_vsi_seid;
9505 ctxt.pf_num = pf->hw.pf_id;
9506 ctxt.vf_num = 0;
9507 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9508 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9509 if (ret) {
9510 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009511 "update vsi failed, err %s aq_err %s\n",
9512 i40e_stat_str(&pf->hw, ret),
9513 i40e_aq_str(&pf->hw,
9514 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009515 ret = -ENOENT;
9516 goto err;
9517 }
9518 /* update the local VSI info queue map */
9519 i40e_vsi_update_queue_map(vsi, &ctxt);
9520 vsi->info.valid_sections = 0;
9521 } else {
9522 /* Default/Main VSI is only enabled for TC0
9523 * reconfigure it to enable all TCs that are
9524 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009525 * For MFP case the iSCSI PF would use this
9526 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009527 */
9528 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9529 if (ret) {
9530 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009531 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9532 enabled_tc,
9533 i40e_stat_str(&pf->hw, ret),
9534 i40e_aq_str(&pf->hw,
9535 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009536 ret = -ENOENT;
9537 }
9538 }
9539 break;
9540
9541 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009542 ctxt.pf_num = hw->pf_id;
9543 ctxt.vf_num = 0;
9544 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009545 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009546 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009547 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9548 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009549 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009550 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009551 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009552 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009553 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009554 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009555 break;
9556
9557 case I40E_VSI_VMDQ2:
9558 ctxt.pf_num = hw->pf_id;
9559 ctxt.vf_num = 0;
9560 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009561 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009562 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9563
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009564 /* This VSI is connected to VEB so the switch_id
9565 * should be set to zero by default.
9566 */
Neerav Parikh51616012015-02-06 08:52:14 +00009567 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9568 ctxt.info.valid_sections |=
9569 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9570 ctxt.info.switch_id =
9571 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9572 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009573
9574 /* Setup the VSI tx/rx queue map for TC0 only for now */
9575 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9576 break;
9577
9578 case I40E_VSI_SRIOV:
9579 ctxt.pf_num = hw->pf_id;
9580 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9581 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009582 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009583 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9584
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009585 /* This VSI is connected to VEB so the switch_id
9586 * should be set to zero by default.
9587 */
Neerav Parikh51616012015-02-06 08:52:14 +00009588 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9589 ctxt.info.valid_sections |=
9590 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9591 ctxt.info.switch_id =
9592 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9593 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009594
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009595 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9596 ctxt.info.valid_sections |=
9597 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9598 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009599 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9600 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009601 }
9602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009603 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9604 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009605 if (pf->vf[vsi->vf_id].spoofchk) {
9606 ctxt.info.valid_sections |=
9607 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9608 ctxt.info.sec_flags |=
9609 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9610 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9611 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009612 /* Setup the VSI tx/rx queue map for TC0 only for now */
9613 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9614 break;
9615
Vasu Dev38e00432014-08-01 13:27:03 -07009616#ifdef I40E_FCOE
9617 case I40E_VSI_FCOE:
9618 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9619 if (ret) {
9620 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9621 return ret;
9622 }
9623 break;
9624
9625#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009626 case I40E_VSI_IWARP:
9627 /* send down message to iWARP */
9628 break;
9629
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009630 default:
9631 return -ENODEV;
9632 }
9633
9634 if (vsi->type != I40E_VSI_MAIN) {
9635 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9636 if (ret) {
9637 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009638 "add vsi failed, err %s aq_err %s\n",
9639 i40e_stat_str(&pf->hw, ret),
9640 i40e_aq_str(&pf->hw,
9641 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009642 ret = -ENOENT;
9643 goto err;
9644 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009645 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009646 vsi->info.valid_sections = 0;
9647 vsi->seid = ctxt.seid;
9648 vsi->id = ctxt.vsi_number;
9649 }
9650
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009651 vsi->active_filters = 0;
9652 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009653 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009654 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009655 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009656 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009657 f_count++;
9658 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009659 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009660
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009661 if (f_count) {
9662 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9663 pf->flags |= I40E_FLAG_FILTER_SYNC;
9664 }
9665
9666 /* Update VSI BW information */
9667 ret = i40e_vsi_get_bw_info(vsi);
9668 if (ret) {
9669 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009670 "couldn't get vsi bw info, err %s aq_err %s\n",
9671 i40e_stat_str(&pf->hw, ret),
9672 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009673 /* VSI is already added so not tearing that up */
9674 ret = 0;
9675 }
9676
9677err:
9678 return ret;
9679}
9680
9681/**
9682 * i40e_vsi_release - Delete a VSI and free its resources
9683 * @vsi: the VSI being removed
9684 *
9685 * Returns 0 on success or < 0 on error
9686 **/
9687int i40e_vsi_release(struct i40e_vsi *vsi)
9688{
Jacob Keller278e7d02016-10-05 09:30:37 -07009689 struct i40e_mac_filter *f;
9690 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009691 struct i40e_veb *veb = NULL;
9692 struct i40e_pf *pf;
9693 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009694 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009695
9696 pf = vsi->back;
9697
9698 /* release of a VEB-owner or last VSI is not allowed */
9699 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9700 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9701 vsi->seid, vsi->uplink_seid);
9702 return -ENODEV;
9703 }
9704 if (vsi == pf->vsi[pf->lan_vsi] &&
9705 !test_bit(__I40E_DOWN, &pf->state)) {
9706 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9707 return -ENODEV;
9708 }
9709
9710 uplink_seid = vsi->uplink_seid;
9711 if (vsi->type != I40E_VSI_SRIOV) {
9712 if (vsi->netdev_registered) {
9713 vsi->netdev_registered = false;
9714 if (vsi->netdev) {
9715 /* results in a call to i40e_close() */
9716 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009717 }
9718 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009719 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009720 }
9721 i40e_vsi_disable_irq(vsi);
9722 }
9723
Jacob Keller278e7d02016-10-05 09:30:37 -07009724 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009725
9726 /* clear the sync flag on all filters */
9727 if (vsi->netdev) {
9728 __dev_uc_unsync(vsi->netdev, NULL);
9729 __dev_mc_unsync(vsi->netdev, NULL);
9730 }
9731
9732 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009733 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009734 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009735
Jacob Keller278e7d02016-10-05 09:30:37 -07009736 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009737
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009738 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009739
9740 i40e_vsi_delete(vsi);
9741 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009742 if (vsi->netdev) {
9743 free_netdev(vsi->netdev);
9744 vsi->netdev = NULL;
9745 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009746 i40e_vsi_clear_rings(vsi);
9747 i40e_vsi_clear(vsi);
9748
9749 /* If this was the last thing on the VEB, except for the
9750 * controlling VSI, remove the VEB, which puts the controlling
9751 * VSI onto the next level down in the switch.
9752 *
9753 * Well, okay, there's one more exception here: don't remove
9754 * the orphan VEBs yet. We'll wait for an explicit remove request
9755 * from up the network stack.
9756 */
Mitch Williams505682c2014-05-20 08:01:37 +00009757 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009758 if (pf->vsi[i] &&
9759 pf->vsi[i]->uplink_seid == uplink_seid &&
9760 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9761 n++; /* count the VSIs */
9762 }
9763 }
9764 for (i = 0; i < I40E_MAX_VEB; i++) {
9765 if (!pf->veb[i])
9766 continue;
9767 if (pf->veb[i]->uplink_seid == uplink_seid)
9768 n++; /* count the VEBs */
9769 if (pf->veb[i]->seid == uplink_seid)
9770 veb = pf->veb[i];
9771 }
9772 if (n == 0 && veb && veb->uplink_seid != 0)
9773 i40e_veb_release(veb);
9774
9775 return 0;
9776}
9777
9778/**
9779 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9780 * @vsi: ptr to the VSI
9781 *
9782 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9783 * corresponding SW VSI structure and initializes num_queue_pairs for the
9784 * newly allocated VSI.
9785 *
9786 * Returns 0 on success or negative on failure
9787 **/
9788static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9789{
9790 int ret = -ENOENT;
9791 struct i40e_pf *pf = vsi->back;
9792
Alexander Duyck493fb302013-09-28 07:01:44 +00009793 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009794 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9795 vsi->seid);
9796 return -EEXIST;
9797 }
9798
9799 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009800 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009801 vsi->seid, vsi->base_vector);
9802 return -EEXIST;
9803 }
9804
Greg Rose90e04072014-03-06 08:59:57 +00009805 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009806 if (ret) {
9807 dev_info(&pf->pdev->dev,
9808 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9809 vsi->num_q_vectors, vsi->seid, ret);
9810 vsi->num_q_vectors = 0;
9811 goto vector_setup_out;
9812 }
9813
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009814 /* In Legacy mode, we do not have to get any other vector since we
9815 * piggyback on the misc/ICR0 for queue interrupts.
9816 */
9817 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9818 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009819 if (vsi->num_q_vectors)
9820 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9821 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009822 if (vsi->base_vector < 0) {
9823 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009824 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9825 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 i40e_vsi_free_q_vectors(vsi);
9827 ret = -ENOENT;
9828 goto vector_setup_out;
9829 }
9830
9831vector_setup_out:
9832 return ret;
9833}
9834
9835/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009836 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9837 * @vsi: pointer to the vsi.
9838 *
9839 * This re-allocates a vsi's queue resources.
9840 *
9841 * Returns pointer to the successfully allocated and configured VSI sw struct
9842 * on success, otherwise returns NULL on failure.
9843 **/
9844static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9845{
John Underwoodf5340392016-02-18 09:19:24 -08009846 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009847 u8 enabled_tc;
9848 int ret;
9849
John Underwoodf5340392016-02-18 09:19:24 -08009850 if (!vsi)
9851 return NULL;
9852
9853 pf = vsi->back;
9854
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009855 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9856 i40e_vsi_clear_rings(vsi);
9857
9858 i40e_vsi_free_arrays(vsi, false);
9859 i40e_set_num_rings_in_vsi(vsi);
9860 ret = i40e_vsi_alloc_arrays(vsi, false);
9861 if (ret)
9862 goto err_vsi;
9863
9864 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9865 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009866 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009867 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009868 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009869 goto err_vsi;
9870 }
9871 vsi->base_queue = ret;
9872
9873 /* Update the FW view of the VSI. Force a reset of TC and queue
9874 * layout configurations.
9875 */
9876 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9877 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9878 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9879 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009880 if (vsi->type == I40E_VSI_MAIN)
9881 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009882
9883 /* assign it some queues */
9884 ret = i40e_alloc_rings(vsi);
9885 if (ret)
9886 goto err_rings;
9887
9888 /* map all of the rings to the q_vectors */
9889 i40e_vsi_map_rings_to_vectors(vsi);
9890 return vsi;
9891
9892err_rings:
9893 i40e_vsi_free_q_vectors(vsi);
9894 if (vsi->netdev_registered) {
9895 vsi->netdev_registered = false;
9896 unregister_netdev(vsi->netdev);
9897 free_netdev(vsi->netdev);
9898 vsi->netdev = NULL;
9899 }
9900 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9901err_vsi:
9902 i40e_vsi_clear(vsi);
9903 return NULL;
9904}
9905
9906/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009907 * i40e_vsi_setup - Set up a VSI by a given type
9908 * @pf: board private structure
9909 * @type: VSI type
9910 * @uplink_seid: the switch element to link to
9911 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9912 *
9913 * This allocates the sw VSI structure and its queue resources, then add a VSI
9914 * to the identified VEB.
9915 *
9916 * Returns pointer to the successfully allocated and configure VSI sw struct on
9917 * success, otherwise returns NULL on failure.
9918 **/
9919struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9920 u16 uplink_seid, u32 param1)
9921{
9922 struct i40e_vsi *vsi = NULL;
9923 struct i40e_veb *veb = NULL;
9924 int ret, i;
9925 int v_idx;
9926
9927 /* The requested uplink_seid must be either
9928 * - the PF's port seid
9929 * no VEB is needed because this is the PF
9930 * or this is a Flow Director special case VSI
9931 * - seid of an existing VEB
9932 * - seid of a VSI that owns an existing VEB
9933 * - seid of a VSI that doesn't own a VEB
9934 * a new VEB is created and the VSI becomes the owner
9935 * - seid of the PF VSI, which is what creates the first VEB
9936 * this is a special case of the previous
9937 *
9938 * Find which uplink_seid we were given and create a new VEB if needed
9939 */
9940 for (i = 0; i < I40E_MAX_VEB; i++) {
9941 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9942 veb = pf->veb[i];
9943 break;
9944 }
9945 }
9946
9947 if (!veb && uplink_seid != pf->mac_seid) {
9948
Mitch Williams505682c2014-05-20 08:01:37 +00009949 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009950 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9951 vsi = pf->vsi[i];
9952 break;
9953 }
9954 }
9955 if (!vsi) {
9956 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9957 uplink_seid);
9958 return NULL;
9959 }
9960
9961 if (vsi->uplink_seid == pf->mac_seid)
9962 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9963 vsi->tc_config.enabled_tc);
9964 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9965 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9966 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009967 if (veb) {
9968 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9969 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009970 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009971 return NULL;
9972 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009973 /* We come up by default in VEPA mode if SRIOV is not
9974 * already enabled, in which case we can't force VEPA
9975 * mode.
9976 */
9977 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9978 veb->bridge_mode = BRIDGE_MODE_VEPA;
9979 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9980 }
Neerav Parikh51616012015-02-06 08:52:14 +00009981 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009982 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009983 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9984 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9985 veb = pf->veb[i];
9986 }
9987 if (!veb) {
9988 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9989 return NULL;
9990 }
9991
9992 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9993 uplink_seid = veb->seid;
9994 }
9995
9996 /* get vsi sw struct */
9997 v_idx = i40e_vsi_mem_alloc(pf, type);
9998 if (v_idx < 0)
9999 goto err_alloc;
10000 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010001 if (!vsi)
10002 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010003 vsi->type = type;
10004 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10005
10006 if (type == I40E_VSI_MAIN)
10007 pf->lan_vsi = v_idx;
10008 else if (type == I40E_VSI_SRIOV)
10009 vsi->vf_id = param1;
10010 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010011 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
10012 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010013 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010014 dev_info(&pf->pdev->dev,
10015 "failed to get tracking for %d queues for VSI %d err=%d\n",
10016 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010017 goto err_vsi;
10018 }
10019 vsi->base_queue = ret;
10020
10021 /* get a VSI from the hardware */
10022 vsi->uplink_seid = uplink_seid;
10023 ret = i40e_add_vsi(vsi);
10024 if (ret)
10025 goto err_vsi;
10026
10027 switch (vsi->type) {
10028 /* setup the netdev if needed */
10029 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010030 /* Apply relevant filters if a platform-specific mac
10031 * address was selected.
10032 */
10033 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10034 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10035 if (ret) {
10036 dev_warn(&pf->pdev->dev,
10037 "could not set up macaddr; err %d\n",
10038 ret);
10039 }
10040 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010041 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -070010042 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010043 ret = i40e_config_netdev(vsi);
10044 if (ret)
10045 goto err_netdev;
10046 ret = register_netdev(vsi->netdev);
10047 if (ret)
10048 goto err_netdev;
10049 vsi->netdev_registered = true;
10050 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010051#ifdef CONFIG_I40E_DCB
10052 /* Setup DCB netlink interface */
10053 i40e_dcbnl_setup(vsi);
10054#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010055 /* fall through */
10056
10057 case I40E_VSI_FDIR:
10058 /* set up vectors and rings if needed */
10059 ret = i40e_vsi_setup_vectors(vsi);
10060 if (ret)
10061 goto err_msix;
10062
10063 ret = i40e_alloc_rings(vsi);
10064 if (ret)
10065 goto err_rings;
10066
10067 /* map all of the rings to the q_vectors */
10068 i40e_vsi_map_rings_to_vectors(vsi);
10069
10070 i40e_vsi_reset_stats(vsi);
10071 break;
10072
10073 default:
10074 /* no netdev or rings for the other VSI types */
10075 break;
10076 }
10077
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010078 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10079 (vsi->type == I40E_VSI_VMDQ2)) {
10080 ret = i40e_vsi_config_rss(vsi);
10081 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010082 return vsi;
10083
10084err_rings:
10085 i40e_vsi_free_q_vectors(vsi);
10086err_msix:
10087 if (vsi->netdev_registered) {
10088 vsi->netdev_registered = false;
10089 unregister_netdev(vsi->netdev);
10090 free_netdev(vsi->netdev);
10091 vsi->netdev = NULL;
10092 }
10093err_netdev:
10094 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10095err_vsi:
10096 i40e_vsi_clear(vsi);
10097err_alloc:
10098 return NULL;
10099}
10100
10101/**
10102 * i40e_veb_get_bw_info - Query VEB BW information
10103 * @veb: the veb to query
10104 *
10105 * Query the Tx scheduler BW configuration data for given VEB
10106 **/
10107static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10108{
10109 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10110 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10111 struct i40e_pf *pf = veb->pf;
10112 struct i40e_hw *hw = &pf->hw;
10113 u32 tc_bw_max;
10114 int ret = 0;
10115 int i;
10116
10117 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10118 &bw_data, NULL);
10119 if (ret) {
10120 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010121 "query veb bw config failed, err %s aq_err %s\n",
10122 i40e_stat_str(&pf->hw, ret),
10123 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010124 goto out;
10125 }
10126
10127 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10128 &ets_data, NULL);
10129 if (ret) {
10130 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010131 "query veb bw ets config failed, err %s aq_err %s\n",
10132 i40e_stat_str(&pf->hw, ret),
10133 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010134 goto out;
10135 }
10136
10137 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10138 veb->bw_max_quanta = ets_data.tc_bw_max;
10139 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010140 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010141 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10142 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10143 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10144 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10145 veb->bw_tc_limit_credits[i] =
10146 le16_to_cpu(bw_data.tc_bw_limits[i]);
10147 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10148 }
10149
10150out:
10151 return ret;
10152}
10153
10154/**
10155 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10156 * @pf: board private structure
10157 *
10158 * On error: returns error code (negative)
10159 * On success: returns vsi index in PF (positive)
10160 **/
10161static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10162{
10163 int ret = -ENOENT;
10164 struct i40e_veb *veb;
10165 int i;
10166
10167 /* Need to protect the allocation of switch elements at the PF level */
10168 mutex_lock(&pf->switch_mutex);
10169
10170 /* VEB list may be fragmented if VEB creation/destruction has
10171 * been happening. We can afford to do a quick scan to look
10172 * for any free slots in the list.
10173 *
10174 * find next empty veb slot, looping back around if necessary
10175 */
10176 i = 0;
10177 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10178 i++;
10179 if (i >= I40E_MAX_VEB) {
10180 ret = -ENOMEM;
10181 goto err_alloc_veb; /* out of VEB slots! */
10182 }
10183
10184 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10185 if (!veb) {
10186 ret = -ENOMEM;
10187 goto err_alloc_veb;
10188 }
10189 veb->pf = pf;
10190 veb->idx = i;
10191 veb->enabled_tc = 1;
10192
10193 pf->veb[i] = veb;
10194 ret = i;
10195err_alloc_veb:
10196 mutex_unlock(&pf->switch_mutex);
10197 return ret;
10198}
10199
10200/**
10201 * i40e_switch_branch_release - Delete a branch of the switch tree
10202 * @branch: where to start deleting
10203 *
10204 * This uses recursion to find the tips of the branch to be
10205 * removed, deleting until we get back to and can delete this VEB.
10206 **/
10207static void i40e_switch_branch_release(struct i40e_veb *branch)
10208{
10209 struct i40e_pf *pf = branch->pf;
10210 u16 branch_seid = branch->seid;
10211 u16 veb_idx = branch->idx;
10212 int i;
10213
10214 /* release any VEBs on this VEB - RECURSION */
10215 for (i = 0; i < I40E_MAX_VEB; i++) {
10216 if (!pf->veb[i])
10217 continue;
10218 if (pf->veb[i]->uplink_seid == branch->seid)
10219 i40e_switch_branch_release(pf->veb[i]);
10220 }
10221
10222 /* Release the VSIs on this VEB, but not the owner VSI.
10223 *
10224 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10225 * the VEB itself, so don't use (*branch) after this loop.
10226 */
Mitch Williams505682c2014-05-20 08:01:37 +000010227 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010228 if (!pf->vsi[i])
10229 continue;
10230 if (pf->vsi[i]->uplink_seid == branch_seid &&
10231 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10232 i40e_vsi_release(pf->vsi[i]);
10233 }
10234 }
10235
10236 /* There's one corner case where the VEB might not have been
10237 * removed, so double check it here and remove it if needed.
10238 * This case happens if the veb was created from the debugfs
10239 * commands and no VSIs were added to it.
10240 */
10241 if (pf->veb[veb_idx])
10242 i40e_veb_release(pf->veb[veb_idx]);
10243}
10244
10245/**
10246 * i40e_veb_clear - remove veb struct
10247 * @veb: the veb to remove
10248 **/
10249static void i40e_veb_clear(struct i40e_veb *veb)
10250{
10251 if (!veb)
10252 return;
10253
10254 if (veb->pf) {
10255 struct i40e_pf *pf = veb->pf;
10256
10257 mutex_lock(&pf->switch_mutex);
10258 if (pf->veb[veb->idx] == veb)
10259 pf->veb[veb->idx] = NULL;
10260 mutex_unlock(&pf->switch_mutex);
10261 }
10262
10263 kfree(veb);
10264}
10265
10266/**
10267 * i40e_veb_release - Delete a VEB and free its resources
10268 * @veb: the VEB being removed
10269 **/
10270void i40e_veb_release(struct i40e_veb *veb)
10271{
10272 struct i40e_vsi *vsi = NULL;
10273 struct i40e_pf *pf;
10274 int i, n = 0;
10275
10276 pf = veb->pf;
10277
10278 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010279 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010280 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10281 n++;
10282 vsi = pf->vsi[i];
10283 }
10284 }
10285 if (n != 1) {
10286 dev_info(&pf->pdev->dev,
10287 "can't remove VEB %d with %d VSIs left\n",
10288 veb->seid, n);
10289 return;
10290 }
10291
10292 /* move the remaining VSI to uplink veb */
10293 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10294 if (veb->uplink_seid) {
10295 vsi->uplink_seid = veb->uplink_seid;
10296 if (veb->uplink_seid == pf->mac_seid)
10297 vsi->veb_idx = I40E_NO_VEB;
10298 else
10299 vsi->veb_idx = veb->veb_idx;
10300 } else {
10301 /* floating VEB */
10302 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10303 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10304 }
10305
10306 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10307 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010308}
10309
10310/**
10311 * i40e_add_veb - create the VEB in the switch
10312 * @veb: the VEB to be instantiated
10313 * @vsi: the controlling VSI
10314 **/
10315static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10316{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010317 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010318 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010319 int ret;
10320
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010321 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010322 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010323 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010324
10325 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010326 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010327 dev_info(&pf->pdev->dev,
10328 "couldn't add VEB, err %s aq_err %s\n",
10329 i40e_stat_str(&pf->hw, ret),
10330 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010331 return -EPERM;
10332 }
10333
10334 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010335 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010336 &veb->stats_idx, NULL, NULL, NULL);
10337 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010338 dev_info(&pf->pdev->dev,
10339 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10340 i40e_stat_str(&pf->hw, ret),
10341 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010342 return -EPERM;
10343 }
10344 ret = i40e_veb_get_bw_info(veb);
10345 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010346 dev_info(&pf->pdev->dev,
10347 "couldn't get VEB bw info, err %s aq_err %s\n",
10348 i40e_stat_str(&pf->hw, ret),
10349 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10350 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010351 return -ENOENT;
10352 }
10353
10354 vsi->uplink_seid = veb->seid;
10355 vsi->veb_idx = veb->idx;
10356 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10357
10358 return 0;
10359}
10360
10361/**
10362 * i40e_veb_setup - Set up a VEB
10363 * @pf: board private structure
10364 * @flags: VEB setup flags
10365 * @uplink_seid: the switch element to link to
10366 * @vsi_seid: the initial VSI seid
10367 * @enabled_tc: Enabled TC bit-map
10368 *
10369 * This allocates the sw VEB structure and links it into the switch
10370 * It is possible and legal for this to be a duplicate of an already
10371 * existing VEB. It is also possible for both uplink and vsi seids
10372 * to be zero, in order to create a floating VEB.
10373 *
10374 * Returns pointer to the successfully allocated VEB sw struct on
10375 * success, otherwise returns NULL on failure.
10376 **/
10377struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10378 u16 uplink_seid, u16 vsi_seid,
10379 u8 enabled_tc)
10380{
10381 struct i40e_veb *veb, *uplink_veb = NULL;
10382 int vsi_idx, veb_idx;
10383 int ret;
10384
10385 /* if one seid is 0, the other must be 0 to create a floating relay */
10386 if ((uplink_seid == 0 || vsi_seid == 0) &&
10387 (uplink_seid + vsi_seid != 0)) {
10388 dev_info(&pf->pdev->dev,
10389 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10390 uplink_seid, vsi_seid);
10391 return NULL;
10392 }
10393
10394 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010395 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010396 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10397 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010398 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010399 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10400 vsi_seid);
10401 return NULL;
10402 }
10403
10404 if (uplink_seid && uplink_seid != pf->mac_seid) {
10405 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10406 if (pf->veb[veb_idx] &&
10407 pf->veb[veb_idx]->seid == uplink_seid) {
10408 uplink_veb = pf->veb[veb_idx];
10409 break;
10410 }
10411 }
10412 if (!uplink_veb) {
10413 dev_info(&pf->pdev->dev,
10414 "uplink seid %d not found\n", uplink_seid);
10415 return NULL;
10416 }
10417 }
10418
10419 /* get veb sw struct */
10420 veb_idx = i40e_veb_mem_alloc(pf);
10421 if (veb_idx < 0)
10422 goto err_alloc;
10423 veb = pf->veb[veb_idx];
10424 veb->flags = flags;
10425 veb->uplink_seid = uplink_seid;
10426 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10427 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10428
10429 /* create the VEB in the switch */
10430 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10431 if (ret)
10432 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010433 if (vsi_idx == pf->lan_vsi)
10434 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010435
10436 return veb;
10437
10438err_veb:
10439 i40e_veb_clear(veb);
10440err_alloc:
10441 return NULL;
10442}
10443
10444/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010445 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010446 * @pf: board private structure
10447 * @ele: element we are building info from
10448 * @num_reported: total number of elements
10449 * @printconfig: should we print the contents
10450 *
10451 * helper function to assist in extracting a few useful SEID values.
10452 **/
10453static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10454 struct i40e_aqc_switch_config_element_resp *ele,
10455 u16 num_reported, bool printconfig)
10456{
10457 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10458 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10459 u8 element_type = ele->element_type;
10460 u16 seid = le16_to_cpu(ele->seid);
10461
10462 if (printconfig)
10463 dev_info(&pf->pdev->dev,
10464 "type=%d seid=%d uplink=%d downlink=%d\n",
10465 element_type, seid, uplink_seid, downlink_seid);
10466
10467 switch (element_type) {
10468 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10469 pf->mac_seid = seid;
10470 break;
10471 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10472 /* Main VEB? */
10473 if (uplink_seid != pf->mac_seid)
10474 break;
10475 if (pf->lan_veb == I40E_NO_VEB) {
10476 int v;
10477
10478 /* find existing or else empty VEB */
10479 for (v = 0; v < I40E_MAX_VEB; v++) {
10480 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10481 pf->lan_veb = v;
10482 break;
10483 }
10484 }
10485 if (pf->lan_veb == I40E_NO_VEB) {
10486 v = i40e_veb_mem_alloc(pf);
10487 if (v < 0)
10488 break;
10489 pf->lan_veb = v;
10490 }
10491 }
10492
10493 pf->veb[pf->lan_veb]->seid = seid;
10494 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10495 pf->veb[pf->lan_veb]->pf = pf;
10496 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10497 break;
10498 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10499 if (num_reported != 1)
10500 break;
10501 /* This is immediately after a reset so we can assume this is
10502 * the PF's VSI
10503 */
10504 pf->mac_seid = uplink_seid;
10505 pf->pf_seid = downlink_seid;
10506 pf->main_vsi_seid = seid;
10507 if (printconfig)
10508 dev_info(&pf->pdev->dev,
10509 "pf_seid=%d main_vsi_seid=%d\n",
10510 pf->pf_seid, pf->main_vsi_seid);
10511 break;
10512 case I40E_SWITCH_ELEMENT_TYPE_PF:
10513 case I40E_SWITCH_ELEMENT_TYPE_VF:
10514 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10515 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10516 case I40E_SWITCH_ELEMENT_TYPE_PE:
10517 case I40E_SWITCH_ELEMENT_TYPE_PA:
10518 /* ignore these for now */
10519 break;
10520 default:
10521 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10522 element_type, seid);
10523 break;
10524 }
10525}
10526
10527/**
10528 * i40e_fetch_switch_configuration - Get switch config from firmware
10529 * @pf: board private structure
10530 * @printconfig: should we print the contents
10531 *
10532 * Get the current switch configuration from the device and
10533 * extract a few useful SEID values.
10534 **/
10535int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10536{
10537 struct i40e_aqc_get_switch_config_resp *sw_config;
10538 u16 next_seid = 0;
10539 int ret = 0;
10540 u8 *aq_buf;
10541 int i;
10542
10543 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10544 if (!aq_buf)
10545 return -ENOMEM;
10546
10547 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10548 do {
10549 u16 num_reported, num_total;
10550
10551 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10552 I40E_AQ_LARGE_BUF,
10553 &next_seid, NULL);
10554 if (ret) {
10555 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010556 "get switch config failed err %s aq_err %s\n",
10557 i40e_stat_str(&pf->hw, ret),
10558 i40e_aq_str(&pf->hw,
10559 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010560 kfree(aq_buf);
10561 return -ENOENT;
10562 }
10563
10564 num_reported = le16_to_cpu(sw_config->header.num_reported);
10565 num_total = le16_to_cpu(sw_config->header.num_total);
10566
10567 if (printconfig)
10568 dev_info(&pf->pdev->dev,
10569 "header: %d reported %d total\n",
10570 num_reported, num_total);
10571
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010572 for (i = 0; i < num_reported; i++) {
10573 struct i40e_aqc_switch_config_element_resp *ele =
10574 &sw_config->element[i];
10575
10576 i40e_setup_pf_switch_element(pf, ele, num_reported,
10577 printconfig);
10578 }
10579 } while (next_seid != 0);
10580
10581 kfree(aq_buf);
10582 return ret;
10583}
10584
10585/**
10586 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10587 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010588 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010589 *
10590 * Returns 0 on success, negative value on failure
10591 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010592static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010593{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010594 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010595 int ret;
10596
10597 /* find out what's out there already */
10598 ret = i40e_fetch_switch_configuration(pf, false);
10599 if (ret) {
10600 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010601 "couldn't fetch switch config, err %s aq_err %s\n",
10602 i40e_stat_str(&pf->hw, ret),
10603 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010604 return ret;
10605 }
10606 i40e_pf_reset_stats(pf);
10607
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010608 /* set the switch config bit for the whole device to
10609 * support limited promisc or true promisc
10610 * when user requests promisc. The default is limited
10611 * promisc.
10612 */
10613
10614 if ((pf->hw.pf_id == 0) &&
10615 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10616 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10617
10618 if (pf->hw.pf_id == 0) {
10619 u16 valid_flags;
10620
10621 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10622 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10623 NULL);
10624 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10625 dev_info(&pf->pdev->dev,
10626 "couldn't set switch config bits, err %s aq_err %s\n",
10627 i40e_stat_str(&pf->hw, ret),
10628 i40e_aq_str(&pf->hw,
10629 pf->hw.aq.asq_last_status));
10630 /* not a fatal problem, just keep going */
10631 }
10632 }
10633
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010634 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010635 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010636 struct i40e_vsi *vsi = NULL;
10637 u16 uplink_seid;
10638
10639 /* Set up the PF VSI associated with the PF's main VSI
10640 * that is already in the HW switch
10641 */
10642 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10643 uplink_seid = pf->veb[pf->lan_veb]->seid;
10644 else
10645 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010646 if (pf->lan_vsi == I40E_NO_VSI)
10647 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10648 else if (reinit)
10649 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010650 if (!vsi) {
10651 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10652 i40e_fdir_teardown(pf);
10653 return -EAGAIN;
10654 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010655 } else {
10656 /* force a reset of TC and queue layout configurations */
10657 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010658
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010659 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10660 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10661 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10662 }
10663 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10664
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010665 i40e_fdir_sb_setup(pf);
10666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010667 /* Setup static PF queue filter control settings */
10668 ret = i40e_setup_pf_filter_control(pf);
10669 if (ret) {
10670 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10671 ret);
10672 /* Failure here should not stop continuing other steps */
10673 }
10674
10675 /* enable RSS in the HW, even for only one queue, as the stack can use
10676 * the hash
10677 */
10678 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010679 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010680
10681 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010682 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010683 i40e_link_event(pf);
10684
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010685 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010686 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10687 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010688
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010689 i40e_ptp_init(pf);
10690
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010691 return ret;
10692}
10693
10694/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010695 * i40e_determine_queue_usage - Work out queue distribution
10696 * @pf: board private structure
10697 **/
10698static void i40e_determine_queue_usage(struct i40e_pf *pf)
10699{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010700 int queues_left;
10701
10702 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010703#ifdef I40E_FCOE
10704 pf->num_fcoe_qps = 0;
10705#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010706
10707 /* Find the max queues to be put into basic use. We'll always be
10708 * using TC0, whether or not DCB is running, and TC0 will get the
10709 * big RSS set.
10710 */
10711 queues_left = pf->hw.func_caps.num_tx_qp;
10712
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010713 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010714 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010715 /* one qp for PF, no queues for anything else */
10716 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010717 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010718
10719 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010720 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010721 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010722#ifdef I40E_FCOE
10723 I40E_FLAG_FCOE_ENABLED |
10724#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010725 I40E_FLAG_FD_SB_ENABLED |
10726 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010727 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010728 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010729 I40E_FLAG_SRIOV_ENABLED |
10730 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010731 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10732 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010733 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010734 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010735 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010736 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010737 queues_left -= pf->num_lan_qps;
10738
10739 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010740 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010741#ifdef I40E_FCOE
10742 I40E_FLAG_FCOE_ENABLED |
10743#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010744 I40E_FLAG_FD_SB_ENABLED |
10745 I40E_FLAG_FD_ATR_ENABLED |
10746 I40E_FLAG_DCB_ENABLED |
10747 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010748 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010749 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010750 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010751 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010752 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10753 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010754 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10755 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010756 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10757 num_online_cpus());
10758 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10759 pf->hw.func_caps.num_tx_qp);
10760
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010761 queues_left -= pf->num_lan_qps;
10762 }
10763
Vasu Dev38e00432014-08-01 13:27:03 -070010764#ifdef I40E_FCOE
10765 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10766 if (I40E_DEFAULT_FCOE <= queues_left) {
10767 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10768 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10769 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10770 } else {
10771 pf->num_fcoe_qps = 0;
10772 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10773 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10774 }
10775
10776 queues_left -= pf->num_fcoe_qps;
10777 }
10778
10779#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010780 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10781 if (queues_left > 1) {
10782 queues_left -= 1; /* save 1 queue for FD */
10783 } else {
10784 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10785 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10786 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010787 }
10788
10789 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10790 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010791 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10792 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010793 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10794 }
10795
10796 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10797 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10798 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10799 (queues_left / pf->num_vmdq_qps));
10800 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10801 }
10802
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010803 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010804 dev_dbg(&pf->pdev->dev,
10805 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10806 pf->hw.func_caps.num_tx_qp,
10807 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010808 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10809 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10810 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010811#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010812 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010813#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010814}
10815
10816/**
10817 * i40e_setup_pf_filter_control - Setup PF static filter control
10818 * @pf: PF to be setup
10819 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010820 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010821 * settings. If PE/FCoE are enabled then it will also set the per PF
10822 * based filter sizes required for them. It also enables Flow director,
10823 * ethertype and macvlan type filter settings for the pf.
10824 *
10825 * Returns 0 on success, negative on failure
10826 **/
10827static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10828{
10829 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10830
10831 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10832
10833 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010834 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010835 settings->enable_fdir = true;
10836
10837 /* Ethtype and MACVLAN filters enabled for PF */
10838 settings->enable_ethtype = true;
10839 settings->enable_macvlan = true;
10840
10841 if (i40e_set_filter_control(&pf->hw, settings))
10842 return -ENOENT;
10843
10844 return 0;
10845}
10846
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010847#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010848#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010849static void i40e_print_features(struct i40e_pf *pf)
10850{
10851 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010852 char *buf;
10853 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010854
Joe Perches3b195842015-12-03 04:20:57 -080010855 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10856 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010857 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010858
Joe Perches3b195842015-12-03 04:20:57 -080010859 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010860#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010861 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010862#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010863 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010864 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010865 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010866 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010867 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010868 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010869 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010870 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010871 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10872 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010873 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010874 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010875 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010876 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010877 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010878 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010879 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010880#ifdef I40E_FCOE
10881 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010882 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010883#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010884 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010885 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010886 else
Joe Perches3b195842015-12-03 04:20:57 -080010887 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010888
Joe Perches3b195842015-12-03 04:20:57 -080010889 dev_info(&pf->pdev->dev, "%s\n", buf);
10890 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010891 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010892}
10893
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010894/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010895 * i40e_get_platform_mac_addr - get platform-specific MAC address
10896 *
10897 * @pdev: PCI device information struct
10898 * @pf: board private structure
10899 *
10900 * Look up the MAC address in Open Firmware on systems that support it,
10901 * and use IDPROM on SPARC if no OF address is found. On return, the
10902 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10903 * has been selected.
10904 **/
10905static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10906{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010907 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010908 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010909 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010910}
10911
10912/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010913 * i40e_probe - Device initialization routine
10914 * @pdev: PCI device information struct
10915 * @ent: entry in i40e_pci_tbl
10916 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010917 * i40e_probe initializes a PF identified by a pci_dev structure.
10918 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010919 * and a hardware reset occur.
10920 *
10921 * Returns 0 on success, negative on failure
10922 **/
10923static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10924{
Catherine Sullivane8278452015-02-06 08:52:08 +000010925 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010926 struct i40e_pf *pf;
10927 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010928 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010929 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010930 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010931 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010932 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010933 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010934 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010935
10936 err = pci_enable_device_mem(pdev);
10937 if (err)
10938 return err;
10939
10940 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010941 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010942 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010943 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10944 if (err) {
10945 dev_err(&pdev->dev,
10946 "DMA configuration failed: 0x%x\n", err);
10947 goto err_dma;
10948 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010949 }
10950
10951 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010952 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010953 if (err) {
10954 dev_info(&pdev->dev,
10955 "pci_request_selected_regions failed %d\n", err);
10956 goto err_pci_reg;
10957 }
10958
10959 pci_enable_pcie_error_reporting(pdev);
10960 pci_set_master(pdev);
10961
10962 /* Now that we have a PCI connection, we need to do the
10963 * low level device setup. This is primarily setting up
10964 * the Admin Queue structures and then querying for the
10965 * device's current profile information.
10966 */
10967 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10968 if (!pf) {
10969 err = -ENOMEM;
10970 goto err_pf_alloc;
10971 }
10972 pf->next_vsi = 0;
10973 pf->pdev = pdev;
10974 set_bit(__I40E_DOWN, &pf->state);
10975
10976 hw = &pf->hw;
10977 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010978
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010979 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10980 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010981
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010982 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010983 if (!hw->hw_addr) {
10984 err = -EIO;
10985 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10986 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010987 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010988 goto err_ioremap;
10989 }
10990 hw->vendor_id = pdev->vendor;
10991 hw->device_id = pdev->device;
10992 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10993 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10994 hw->subsystem_device_id = pdev->subsystem_device;
10995 hw->bus.device = PCI_SLOT(pdev->devfn);
10996 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010997 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010998
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010999 /* set up the locks for the AQ, do this only once in probe
11000 * and destroy them only once in remove
11001 */
11002 mutex_init(&hw->aq.asq_mutex);
11003 mutex_init(&hw->aq.arq_mutex);
11004
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011005 pf->msg_enable = netif_msg_init(debug,
11006 NETIF_MSG_DRV |
11007 NETIF_MSG_PROBE |
11008 NETIF_MSG_LINK);
11009 if (debug < -1)
11010 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011011
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011012 /* do a special CORER for clearing PXE mode once at init */
11013 if (hw->revision_id == 0 &&
11014 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11015 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11016 i40e_flush(hw);
11017 msleep(200);
11018 pf->corer_count++;
11019
11020 i40e_clear_pxe_mode(hw);
11021 }
11022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011023 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011024 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011025 err = i40e_pf_reset(hw);
11026 if (err) {
11027 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11028 goto err_pf_reset;
11029 }
11030 pf->pfr_count++;
11031
11032 hw->aq.num_arq_entries = I40E_AQ_LEN;
11033 hw->aq.num_asq_entries = I40E_AQ_LEN;
11034 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11035 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11036 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011037
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011038 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011039 "%s-%s:misc",
11040 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011041
11042 err = i40e_init_shared_code(hw);
11043 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011044 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11045 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011046 goto err_pf_reset;
11047 }
11048
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011049 /* set up a default setting for link flow control */
11050 pf->hw.fc.requested_mode = I40E_FC_NONE;
11051
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011052 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011053 if (err) {
11054 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11055 dev_info(&pdev->dev,
11056 "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");
11057 else
11058 dev_info(&pdev->dev,
11059 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11060
11061 goto err_pf_reset;
11062 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011063
Shannon Nelson6dec1012015-09-28 14:12:30 -040011064 /* provide nvm, fw, api versions */
11065 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11066 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11067 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11068 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011069
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011070 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11071 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011072 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011073 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
11074 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11075 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011076 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011077 "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 +000011078
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011079 i40e_verify_eeprom(pf);
11080
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011081 /* Rev 0 hardware was never productized */
11082 if (hw->revision_id < 1)
11083 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");
11084
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011085 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011086 err = i40e_get_capabilities(pf);
11087 if (err)
11088 goto err_adminq_setup;
11089
11090 err = i40e_sw_init(pf);
11091 if (err) {
11092 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11093 goto err_sw_init;
11094 }
11095
11096 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11097 hw->func_caps.num_rx_qp,
11098 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11099 if (err) {
11100 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11101 goto err_init_lan_hmc;
11102 }
11103
11104 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11105 if (err) {
11106 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11107 err = -ENOENT;
11108 goto err_configure_lan_hmc;
11109 }
11110
Neerav Parikhb686ece2014-12-14 01:55:11 +000011111 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11112 * Ignore error return codes because if it was already disabled via
11113 * hardware settings this will fail
11114 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011115 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011116 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11117 i40e_aq_stop_lldp(hw, true, NULL);
11118 }
11119
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011120 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011121 /* allow a platform config to override the HW addr */
11122 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011123 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011124 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11125 err = -EIO;
11126 goto err_mac_addr;
11127 }
11128 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011129 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011130 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11131 if (is_valid_ether_addr(hw->mac.port_addr))
11132 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011133#ifdef I40E_FCOE
11134 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11135 if (err)
11136 dev_info(&pdev->dev,
11137 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11138 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11139 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11140 hw->mac.san_addr);
11141 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11142 }
11143 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11144#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011145
11146 pci_set_drvdata(pdev, pf);
11147 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011148#ifdef CONFIG_I40E_DCB
11149 err = i40e_init_pf_dcb(pf);
11150 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011151 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011152 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011153 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011154 }
11155#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011156
11157 /* set up periodic task facility */
11158 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11159 pf->service_timer_period = HZ;
11160
11161 INIT_WORK(&pf->service_task, i40e_service_task);
11162 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11163 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011164
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011165 /* NVM bit on means WoL disabled for the port */
11166 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011167 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011168 pf->wol_en = false;
11169 else
11170 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011171 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11172
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011173 /* set up the main switch operations */
11174 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011175 err = i40e_init_interrupt_scheme(pf);
11176 if (err)
11177 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011178
Mitch Williams505682c2014-05-20 08:01:37 +000011179 /* The number of VSIs reported by the FW is the minimum guaranteed
11180 * to us; HW supports far more and we share the remaining pool with
11181 * the other PFs. We allocate space for more than the guarantee with
11182 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011183 */
Mitch Williams505682c2014-05-20 08:01:37 +000011184 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11185 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11186 else
11187 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11188
11189 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011190 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11191 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011192 if (!pf->vsi) {
11193 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011194 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011195 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011196
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011197#ifdef CONFIG_PCI_IOV
11198 /* prep for VF support */
11199 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11200 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11201 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11202 if (pci_num_vf(pdev))
11203 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11204 }
11205#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011206 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011207 if (err) {
11208 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11209 goto err_vsis;
11210 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011211
11212 /* Make sure flow control is set according to current settings */
11213 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11214 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11215 dev_dbg(&pf->pdev->dev,
11216 "Set fc with err %s aq_err %s on get_phy_cap\n",
11217 i40e_stat_str(hw, err),
11218 i40e_aq_str(hw, hw->aq.asq_last_status));
11219 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11220 dev_dbg(&pf->pdev->dev,
11221 "Set fc with err %s aq_err %s on set_phy_config\n",
11222 i40e_stat_str(hw, err),
11223 i40e_aq_str(hw, hw->aq.asq_last_status));
11224 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11225 dev_dbg(&pf->pdev->dev,
11226 "Set fc with err %s aq_err %s on get_link_info\n",
11227 i40e_stat_str(hw, err),
11228 i40e_aq_str(hw, hw->aq.asq_last_status));
11229
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011230 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011231 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011232 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11233 i40e_vsi_open(pf->vsi[i]);
11234 break;
11235 }
11236 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011237
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011238 /* The driver only wants link up/down and module qualification
11239 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011240 */
11241 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011242 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011243 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011244 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011245 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011246 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11247 i40e_stat_str(&pf->hw, err),
11248 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011249
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011250 /* Reconfigure hardware for allowing smaller MSS in the case
11251 * of TSO, so that we avoid the MDD being fired and causing
11252 * a reset in the case of small MSS+TSO.
11253 */
11254 val = rd32(hw, I40E_REG_MSS);
11255 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11256 val &= ~I40E_REG_MSS_MIN_MASK;
11257 val |= I40E_64BYTE_MSS;
11258 wr32(hw, I40E_REG_MSS, val);
11259 }
11260
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011261 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011262 msleep(75);
11263 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11264 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011265 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11266 i40e_stat_str(&pf->hw, err),
11267 i40e_aq_str(&pf->hw,
11268 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011269 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011270 /* The main driver is (mostly) up and happy. We need to set this state
11271 * before setting up the misc vector or we get a race and the vector
11272 * ends up disabled forever.
11273 */
11274 clear_bit(__I40E_DOWN, &pf->state);
11275
11276 /* In case of MSIX we are going to setup the misc vector right here
11277 * to handle admin queue events etc. In case of legacy and MSI
11278 * the misc functionality and queue processing is combined in
11279 * the same vector and that gets setup at open.
11280 */
11281 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11282 err = i40e_setup_misc_vector(pf);
11283 if (err) {
11284 dev_info(&pdev->dev,
11285 "setup of misc vector failed: %d\n", err);
11286 goto err_vsis;
11287 }
11288 }
11289
Greg Rosedf805f62014-04-04 04:43:16 +000011290#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011291 /* prep for VF support */
11292 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011293 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11294 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011295 /* disable link interrupts for VFs */
11296 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11297 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11298 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11299 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011300
11301 if (pci_num_vf(pdev)) {
11302 dev_info(&pdev->dev,
11303 "Active VFs found, allocating resources.\n");
11304 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11305 if (err)
11306 dev_info(&pdev->dev,
11307 "Error %d allocating resources for existing VFs\n",
11308 err);
11309 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011310 }
Greg Rosedf805f62014-04-04 04:43:16 +000011311#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011312
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011313 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11314 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11315 pf->num_iwarp_msix,
11316 I40E_IWARP_IRQ_PILE_ID);
11317 if (pf->iwarp_base_vector < 0) {
11318 dev_info(&pdev->dev,
11319 "failed to get tracking for %d vectors for IWARP err=%d\n",
11320 pf->num_iwarp_msix, pf->iwarp_base_vector);
11321 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11322 }
11323 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011325 i40e_dbg_pf_init(pf);
11326
11327 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011328 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011329
11330 /* since everything's happy, start the service_task timer */
11331 mod_timer(&pf->service_timer,
11332 round_jiffies(jiffies + pf->service_timer_period));
11333
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011334 /* add this PF to client device list and launch a client service task */
11335 err = i40e_lan_add_device(pf);
11336 if (err)
11337 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11338 err);
11339
Vasu Dev38e00432014-08-01 13:27:03 -070011340#ifdef I40E_FCOE
11341 /* create FCoE interface */
11342 i40e_fcoe_vsi_setup(pf);
11343
11344#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011345#define PCI_SPEED_SIZE 8
11346#define PCI_WIDTH_SIZE 8
11347 /* Devices on the IOSF bus do not have this information
11348 * and will report PCI Gen 1 x 1 by default so don't bother
11349 * checking them.
11350 */
11351 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11352 char speed[PCI_SPEED_SIZE] = "Unknown";
11353 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011354
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011355 /* Get the negotiated link width and speed from PCI config
11356 * space
11357 */
11358 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11359 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011360
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011361 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011362
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011363 switch (hw->bus.speed) {
11364 case i40e_bus_speed_8000:
11365 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11366 case i40e_bus_speed_5000:
11367 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11368 case i40e_bus_speed_2500:
11369 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11370 default:
11371 break;
11372 }
11373 switch (hw->bus.width) {
11374 case i40e_bus_width_pcie_x8:
11375 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11376 case i40e_bus_width_pcie_x4:
11377 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11378 case i40e_bus_width_pcie_x2:
11379 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11380 case i40e_bus_width_pcie_x1:
11381 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11382 default:
11383 break;
11384 }
11385
11386 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11387 speed, width);
11388
11389 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11390 hw->bus.speed < i40e_bus_speed_8000) {
11391 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11392 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11393 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011394 }
11395
Catherine Sullivane8278452015-02-06 08:52:08 +000011396 /* get the requested speeds from the fw */
11397 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11398 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011399 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11400 i40e_stat_str(&pf->hw, err),
11401 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011402 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11403
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011404 /* get the supported phy types from the fw */
11405 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11406 if (err)
11407 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11408 i40e_stat_str(&pf->hw, err),
11409 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011410
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011411 /* Add a filter to drop all Flow control frames from any VSI from being
11412 * transmitted. By doing so we stop a malicious VF from sending out
11413 * PAUSE or PFC frames and potentially controlling traffic for other
11414 * PF/VF VSIs.
11415 * The FW can still send Flow control frames if enabled.
11416 */
11417 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11418 pf->main_vsi_seid);
11419
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011420 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011421 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11422 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011423 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11424 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011425 /* print a string summarizing features */
11426 i40e_print_features(pf);
11427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011428 return 0;
11429
11430 /* Unwind what we've done if something failed in the setup */
11431err_vsis:
11432 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011433 i40e_clear_interrupt_scheme(pf);
11434 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011435err_switch_setup:
11436 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011437 del_timer_sync(&pf->service_timer);
11438err_mac_addr:
11439err_configure_lan_hmc:
11440 (void)i40e_shutdown_lan_hmc(hw);
11441err_init_lan_hmc:
11442 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011443err_sw_init:
11444err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011445err_pf_reset:
11446 iounmap(hw->hw_addr);
11447err_ioremap:
11448 kfree(pf);
11449err_pf_alloc:
11450 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011451 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011452err_pci_reg:
11453err_dma:
11454 pci_disable_device(pdev);
11455 return err;
11456}
11457
11458/**
11459 * i40e_remove - Device removal routine
11460 * @pdev: PCI device information struct
11461 *
11462 * i40e_remove is called by the PCI subsystem to alert the driver
11463 * that is should release a PCI device. This could be caused by a
11464 * Hot-Plug event, or because the driver is going to be removed from
11465 * memory.
11466 **/
11467static void i40e_remove(struct pci_dev *pdev)
11468{
11469 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011470 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011471 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011472 int i;
11473
11474 i40e_dbg_pf_exit(pf);
11475
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011476 i40e_ptp_stop(pf);
11477
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011478 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011479 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11480 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011481
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011482 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011483 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011484 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011485 if (pf->service_timer.data)
11486 del_timer_sync(&pf->service_timer);
11487 if (pf->service_task.func)
11488 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011489
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011490 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11491 i40e_free_vfs(pf);
11492 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11493 }
11494
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011495 i40e_fdir_teardown(pf);
11496
11497 /* If there is a switch structure or any orphans, remove them.
11498 * This will leave only the PF's VSI remaining.
11499 */
11500 for (i = 0; i < I40E_MAX_VEB; i++) {
11501 if (!pf->veb[i])
11502 continue;
11503
11504 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11505 pf->veb[i]->uplink_seid == 0)
11506 i40e_switch_branch_release(pf->veb[i]);
11507 }
11508
11509 /* Now we can shutdown the PF's VSI, just before we kill
11510 * adminq and hmc.
11511 */
11512 if (pf->vsi[pf->lan_vsi])
11513 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11514
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011515 /* remove attached clients */
11516 ret_code = i40e_lan_del_device(pf);
11517 if (ret_code) {
11518 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11519 ret_code);
11520 }
11521
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011522 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011523 if (hw->hmc.hmc_obj) {
11524 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011525 if (ret_code)
11526 dev_warn(&pdev->dev,
11527 "Failed to destroy the HMC resources: %d\n",
11528 ret_code);
11529 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011530
11531 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011532 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011533
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011534 /* destroy the locks only once, here */
11535 mutex_destroy(&hw->aq.arq_mutex);
11536 mutex_destroy(&hw->aq.asq_mutex);
11537
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011538 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11539 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011540 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011541 if (pf->vsi[i]) {
11542 i40e_vsi_clear_rings(pf->vsi[i]);
11543 i40e_vsi_clear(pf->vsi[i]);
11544 pf->vsi[i] = NULL;
11545 }
11546 }
11547
11548 for (i = 0; i < I40E_MAX_VEB; i++) {
11549 kfree(pf->veb[i]);
11550 pf->veb[i] = NULL;
11551 }
11552
11553 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011554 kfree(pf->vsi);
11555
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011556 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011557 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011558 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011559
11560 pci_disable_pcie_error_reporting(pdev);
11561 pci_disable_device(pdev);
11562}
11563
11564/**
11565 * i40e_pci_error_detected - warning that something funky happened in PCI land
11566 * @pdev: PCI device information struct
11567 *
11568 * Called to warn that something happened and the error handling steps
11569 * are in progress. Allows the driver to quiesce things, be ready for
11570 * remediation.
11571 **/
11572static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11573 enum pci_channel_state error)
11574{
11575 struct i40e_pf *pf = pci_get_drvdata(pdev);
11576
11577 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11578
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011579 if (!pf) {
11580 dev_info(&pdev->dev,
11581 "Cannot recover - error happened during device probe\n");
11582 return PCI_ERS_RESULT_DISCONNECT;
11583 }
11584
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011585 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011586 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11587 rtnl_lock();
11588 i40e_prep_for_reset(pf);
11589 rtnl_unlock();
11590 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011591
11592 /* Request a slot reset */
11593 return PCI_ERS_RESULT_NEED_RESET;
11594}
11595
11596/**
11597 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11598 * @pdev: PCI device information struct
11599 *
11600 * Called to find if the driver can work with the device now that
11601 * the pci slot has been reset. If a basic connection seems good
11602 * (registers are readable and have sane content) then return a
11603 * happy little PCI_ERS_RESULT_xxx.
11604 **/
11605static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11606{
11607 struct i40e_pf *pf = pci_get_drvdata(pdev);
11608 pci_ers_result_t result;
11609 int err;
11610 u32 reg;
11611
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011612 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011613 if (pci_enable_device_mem(pdev)) {
11614 dev_info(&pdev->dev,
11615 "Cannot re-enable PCI device after reset.\n");
11616 result = PCI_ERS_RESULT_DISCONNECT;
11617 } else {
11618 pci_set_master(pdev);
11619 pci_restore_state(pdev);
11620 pci_save_state(pdev);
11621 pci_wake_from_d3(pdev, false);
11622
11623 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11624 if (reg == 0)
11625 result = PCI_ERS_RESULT_RECOVERED;
11626 else
11627 result = PCI_ERS_RESULT_DISCONNECT;
11628 }
11629
11630 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11631 if (err) {
11632 dev_info(&pdev->dev,
11633 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11634 err);
11635 /* non-fatal, continue */
11636 }
11637
11638 return result;
11639}
11640
11641/**
11642 * i40e_pci_error_resume - restart operations after PCI error recovery
11643 * @pdev: PCI device information struct
11644 *
11645 * Called to allow the driver to bring things back up after PCI error
11646 * and/or reset recovery has finished.
11647 **/
11648static void i40e_pci_error_resume(struct pci_dev *pdev)
11649{
11650 struct i40e_pf *pf = pci_get_drvdata(pdev);
11651
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011652 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011653 if (test_bit(__I40E_SUSPENDED, &pf->state))
11654 return;
11655
11656 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011657 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011658 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011659}
11660
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011661/**
11662 * i40e_shutdown - PCI callback for shutting down
11663 * @pdev: PCI device information struct
11664 **/
11665static void i40e_shutdown(struct pci_dev *pdev)
11666{
11667 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011668 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011669
11670 set_bit(__I40E_SUSPENDED, &pf->state);
11671 set_bit(__I40E_DOWN, &pf->state);
11672 rtnl_lock();
11673 i40e_prep_for_reset(pf);
11674 rtnl_unlock();
11675
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011676 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11677 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11678
Catherine Sullivan02b42492015-07-10 19:35:59 -040011679 del_timer_sync(&pf->service_timer);
11680 cancel_work_sync(&pf->service_task);
11681 i40e_fdir_teardown(pf);
11682
11683 rtnl_lock();
11684 i40e_prep_for_reset(pf);
11685 rtnl_unlock();
11686
11687 wr32(hw, I40E_PFPM_APM,
11688 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11689 wr32(hw, I40E_PFPM_WUFC,
11690 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11691
Shannon Nelsone1477582015-02-21 06:44:33 +000011692 i40e_clear_interrupt_scheme(pf);
11693
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011694 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011695 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011696 pci_set_power_state(pdev, PCI_D3hot);
11697 }
11698}
11699
11700#ifdef CONFIG_PM
11701/**
11702 * i40e_suspend - PCI callback for moving to D3
11703 * @pdev: PCI device information struct
11704 **/
11705static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11706{
11707 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011708 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011709 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011710
11711 set_bit(__I40E_SUSPENDED, &pf->state);
11712 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011713
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011714 rtnl_lock();
11715 i40e_prep_for_reset(pf);
11716 rtnl_unlock();
11717
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011718 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11719 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11720
Greg Roseb33d3b72016-05-16 10:26:37 -070011721 i40e_stop_misc_vector(pf);
11722
Greg Rose059ff692016-05-16 10:26:38 -070011723 retval = pci_save_state(pdev);
11724 if (retval)
11725 return retval;
11726
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011727 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011728 pci_set_power_state(pdev, PCI_D3hot);
11729
Greg Rose059ff692016-05-16 10:26:38 -070011730 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011731}
11732
11733/**
11734 * i40e_resume - PCI callback for waking up from D3
11735 * @pdev: PCI device information struct
11736 **/
11737static int i40e_resume(struct pci_dev *pdev)
11738{
11739 struct i40e_pf *pf = pci_get_drvdata(pdev);
11740 u32 err;
11741
11742 pci_set_power_state(pdev, PCI_D0);
11743 pci_restore_state(pdev);
11744 /* pci_restore_state() clears dev->state_saves, so
11745 * call pci_save_state() again to restore it.
11746 */
11747 pci_save_state(pdev);
11748
11749 err = pci_enable_device_mem(pdev);
11750 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011751 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011752 return err;
11753 }
11754 pci_set_master(pdev);
11755
11756 /* no wakeup events while running */
11757 pci_wake_from_d3(pdev, false);
11758
11759 /* handling the reset will rebuild the device state */
11760 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11761 clear_bit(__I40E_DOWN, &pf->state);
11762 rtnl_lock();
11763 i40e_reset_and_rebuild(pf, false);
11764 rtnl_unlock();
11765 }
11766
11767 return 0;
11768}
11769
11770#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011771static const struct pci_error_handlers i40e_err_handler = {
11772 .error_detected = i40e_pci_error_detected,
11773 .slot_reset = i40e_pci_error_slot_reset,
11774 .resume = i40e_pci_error_resume,
11775};
11776
11777static struct pci_driver i40e_driver = {
11778 .name = i40e_driver_name,
11779 .id_table = i40e_pci_tbl,
11780 .probe = i40e_probe,
11781 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011782#ifdef CONFIG_PM
11783 .suspend = i40e_suspend,
11784 .resume = i40e_resume,
11785#endif
11786 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011787 .err_handler = &i40e_err_handler,
11788 .sriov_configure = i40e_pci_sriov_configure,
11789};
11790
11791/**
11792 * i40e_init_module - Driver registration routine
11793 *
11794 * i40e_init_module is the first routine called when the driver is
11795 * loaded. All it does is register with the PCI subsystem.
11796 **/
11797static int __init i40e_init_module(void)
11798{
11799 pr_info("%s: %s - version %s\n", i40e_driver_name,
11800 i40e_driver_string, i40e_driver_version_str);
11801 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011802
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011803 /* we will see if single thread per module is enough for now,
11804 * it can't be any worse than using the system workqueue which
11805 * was already single threaded
11806 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011807 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11808 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011809 if (!i40e_wq) {
11810 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11811 return -ENOMEM;
11812 }
11813
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011814 i40e_dbg_init();
11815 return pci_register_driver(&i40e_driver);
11816}
11817module_init(i40e_init_module);
11818
11819/**
11820 * i40e_exit_module - Driver exit cleanup routine
11821 *
11822 * i40e_exit_module is called just before the driver is removed
11823 * from memory.
11824 **/
11825static void __exit i40e_exit_module(void)
11826{
11827 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011828 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011829 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011830}
11831module_exit(i40e_exit_module);