blob: 2ccf376adcfe1279bd6cac17e04b3914b81d4314 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070034#include <net/udp_tunnel.h>
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000035
36const char i40e_driver_name[] = "i40e";
37static const char i40e_driver_string[] =
38 "Intel(R) Ethernet Connection XL710 Network Driver";
39
40#define DRV_KERN "-k"
41
Catherine Sullivane8e724d2014-07-10 07:58:26 +000042#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070043#define DRV_VERSION_MINOR 6
Bimmy Pujari36023862016-11-08 13:05:19 -080044#define DRV_VERSION_BUILD 25
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000045#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080049static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000056static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080060static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080061static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020070static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080071 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080072 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080073 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
88 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000091 /* required last entry */
92 {0, }
93};
94MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95
96#define I40E_MAX_VF_COUNT 128
97static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040098module_param(debug, uint, 0);
99MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000100
101MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103MODULE_LICENSE("GPL");
104MODULE_VERSION(DRV_VERSION);
105
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800106static struct workqueue_struct *i40e_wq;
107
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000108/**
109 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
110 * @hw: pointer to the HW structure
111 * @mem: ptr to mem struct to fill out
112 * @size: size of memory requested
113 * @alignment: what to align the allocation to
114 **/
115int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
116 u64 size, u32 alignment)
117{
118 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
119
120 mem->size = ALIGN(size, alignment);
121 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
122 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000123 if (!mem->va)
124 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000125
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000126 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000127}
128
129/**
130 * i40e_free_dma_mem_d - OS specific memory free for shared code
131 * @hw: pointer to the HW structure
132 * @mem: ptr to mem struct to free
133 **/
134int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
135{
136 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
137
138 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
139 mem->va = NULL;
140 mem->pa = 0;
141 mem->size = 0;
142
143 return 0;
144}
145
146/**
147 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
148 * @hw: pointer to the HW structure
149 * @mem: ptr to mem struct to fill out
150 * @size: size of memory requested
151 **/
152int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
153 u32 size)
154{
155 mem->size = size;
156 mem->va = kzalloc(size, GFP_KERNEL);
157
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000158 if (!mem->va)
159 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000160
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000161 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000162}
163
164/**
165 * i40e_free_virt_mem_d - OS specific memory free for shared code
166 * @hw: pointer to the HW structure
167 * @mem: ptr to mem struct to free
168 **/
169int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
170{
171 /* it's ok to kfree a NULL pointer */
172 kfree(mem->va);
173 mem->va = NULL;
174 mem->size = 0;
175
176 return 0;
177}
178
179/**
180 * i40e_get_lump - find a lump of free generic resource
181 * @pf: board private structure
182 * @pile: the pile of resource to search
183 * @needed: the number of items needed
184 * @id: an owner id to stick on the items assigned
185 *
186 * Returns the base item index of the lump, or negative for error
187 *
188 * The search_hint trick and lack of advanced fit-finding only work
189 * because we're highly likely to have all the same size lump requests.
190 * Linear search time and any fragmentation should be minimal.
191 **/
192static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
193 u16 needed, u16 id)
194{
195 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000196 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000197
198 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
199 dev_info(&pf->pdev->dev,
200 "param err: pile=%p needed=%d id=0x%04x\n",
201 pile, needed, id);
202 return -EINVAL;
203 }
204
205 /* start the linear search with an imperfect hint */
206 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000207 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000208 /* skip already allocated entries */
209 if (pile->list[i] & I40E_PILE_VALID_BIT) {
210 i++;
211 continue;
212 }
213
214 /* do we have enough in this lump? */
215 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
216 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
217 break;
218 }
219
220 if (j == needed) {
221 /* there was enough, so assign it to the requestor */
222 for (j = 0; j < needed; j++)
223 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
224 ret = i;
225 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000226 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000227 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400228
229 /* not enough, so skip over it and continue looking */
230 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000231 }
232
233 return ret;
234}
235
236/**
237 * i40e_put_lump - return a lump of generic resource
238 * @pile: the pile of resource to search
239 * @index: the base item index
240 * @id: the owner id of the items assigned
241 *
242 * Returns the count of items in the lump
243 **/
244static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
245{
246 int valid_id = (id | I40E_PILE_VALID_BIT);
247 int count = 0;
248 int i;
249
250 if (!pile || index >= pile->num_entries)
251 return -EINVAL;
252
253 for (i = index;
254 i < pile->num_entries && pile->list[i] == valid_id;
255 i++) {
256 pile->list[i] = 0;
257 count++;
258 }
259
260 if (count && index < pile->search_hint)
261 pile->search_hint = index;
262
263 return count;
264}
265
266/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700267 * i40e_find_vsi_from_id - searches for the vsi with the given id
268 * @pf - the pf structure to search for the vsi
269 * @id - id of the vsi it is searching for
270 **/
271struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
272{
273 int i;
274
275 for (i = 0; i < pf->num_alloc_vsi; i++)
276 if (pf->vsi[i] && (pf->vsi[i]->id == id))
277 return pf->vsi[i];
278
279 return NULL;
280}
281
282/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000283 * i40e_service_event_schedule - Schedule the service task to wake up
284 * @pf: board private structure
285 *
286 * If not already scheduled, this puts the task into the work queue
287 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600288void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000289{
290 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800291 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800292 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000293}
294
295/**
296 * i40e_tx_timeout - Respond to a Tx Hang
297 * @netdev: network interface device structure
298 *
299 * If any port has noticed a Tx timeout, it is likely that the whole
300 * device is munged, not just the one netdev port, so go for the full
301 * reset.
302 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700303#ifdef I40E_FCOE
304void i40e_tx_timeout(struct net_device *netdev)
305#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000306static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700307#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000308{
309 struct i40e_netdev_priv *np = netdev_priv(netdev);
310 struct i40e_vsi *vsi = np->vsi;
311 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400312 struct i40e_ring *tx_ring = NULL;
313 unsigned int i, hung_queue = 0;
314 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000315
316 pf->tx_timeout_count++;
317
Kiran Patilb03a8c12015-09-24 18:13:15 -0400318 /* find the stopped queue the same way the stack does */
319 for (i = 0; i < netdev->num_tx_queues; i++) {
320 struct netdev_queue *q;
321 unsigned long trans_start;
322
323 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200324 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400325 if (netif_xmit_stopped(q) &&
326 time_after(jiffies,
327 (trans_start + netdev->watchdog_timeo))) {
328 hung_queue = i;
329 break;
330 }
331 }
332
333 if (i == netdev->num_tx_queues) {
334 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
335 } else {
336 /* now that we have an index, find the tx_ring struct */
337 for (i = 0; i < vsi->num_queue_pairs; i++) {
338 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 if (hung_queue ==
340 vsi->tx_rings[i]->queue_index) {
341 tx_ring = vsi->tx_rings[i];
342 break;
343 }
344 }
345 }
346 }
347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000348 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400349 pf->tx_timeout_recovery_level = 1; /* reset after some time */
350 else if (time_before(jiffies,
351 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
352 return; /* don't do any new action before the next timeout */
353
354 if (tx_ring) {
355 head = i40e_get_head(tx_ring);
356 /* Read interrupt register */
357 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 val = rd32(&pf->hw,
359 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 tx_ring->vsi->base_vector - 1));
361 else
362 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363
364 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
365 vsi->seid, hung_queue, tx_ring->next_to_clean,
366 head, tx_ring->next_to_use,
367 readl(tx_ring->tail), val);
368 }
369
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000370 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400371 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
372 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000373
374 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 case 1:
376 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
377 break;
378 case 2:
379 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
380 break;
381 case 3:
382 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
383 break;
384 default:
385 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000386 break;
387 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389 i40e_service_event_schedule(pf);
390 pf->tx_timeout_recovery_level++;
391}
392
393/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 * i40e_get_vsi_stats_struct - Get System Network Statistics
395 * @vsi: the VSI we care about
396 *
397 * Returns the address of the device statistics structure.
398 * The statistics are actually updated from the service task.
399 **/
400struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401{
402 return &vsi->net_stats;
403}
404
405/**
406 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
407 * @netdev: network interface device structure
408 *
409 * Returns the address of the device statistics structure.
410 * The statistics are actually updated from the service task.
411 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700412#ifdef I40E_FCOE
413struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
414 struct net_device *netdev,
415 struct rtnl_link_stats64 *stats)
416#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000417static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
418 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000419 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700420#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000421{
422 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000423 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000424 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000425 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
426 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000427
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000428 if (test_bit(__I40E_DOWN, &vsi->state))
429 return stats;
430
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800431 if (!vsi->tx_rings)
432 return stats;
433
Alexander Duyck980e9b12013-09-28 06:01:03 +0000434 rcu_read_lock();
435 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000436 u64 bytes, packets;
437 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000438
Alexander Duyck980e9b12013-09-28 06:01:03 +0000439 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
440 if (!tx_ring)
441 continue;
442
443 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700444 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000445 packets = tx_ring->stats.packets;
446 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700447 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000448
449 stats->tx_packets += packets;
450 stats->tx_bytes += bytes;
451 rx_ring = &tx_ring[1];
452
453 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700454 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 packets = rx_ring->stats.packets;
456 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700457 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458
459 stats->rx_packets += packets;
460 stats->rx_bytes += bytes;
461 }
462 rcu_read_unlock();
463
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000464 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000465 stats->multicast = vsi_stats->multicast;
466 stats->tx_errors = vsi_stats->tx_errors;
467 stats->tx_dropped = vsi_stats->tx_dropped;
468 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400469 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000470 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
471 stats->rx_length_errors = vsi_stats->rx_length_errors;
472
473 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000474}
475
476/**
477 * i40e_vsi_reset_stats - Resets all stats of the given vsi
478 * @vsi: the VSI to have its stats reset
479 **/
480void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
481{
482 struct rtnl_link_stats64 *ns;
483 int i;
484
485 if (!vsi)
486 return;
487
488 ns = i40e_get_vsi_stats_struct(vsi);
489 memset(ns, 0, sizeof(*ns));
490 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
491 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
492 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000493 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000494 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400495 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000496 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400497 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000498 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400499 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000500 sizeof(vsi->tx_rings[i]->stats));
501 memset(&vsi->tx_rings[i]->tx_stats, 0,
502 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000503 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000504 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000505 vsi->stat_offsets_loaded = false;
506}
507
508/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000509 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000510 * @pf: the PF to be reset
511 **/
512void i40e_pf_reset_stats(struct i40e_pf *pf)
513{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000514 int i;
515
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516 memset(&pf->stats, 0, sizeof(pf->stats));
517 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
518 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000519
520 for (i = 0; i < I40E_MAX_VEB; i++) {
521 if (pf->veb[i]) {
522 memset(&pf->veb[i]->stats, 0,
523 sizeof(pf->veb[i]->stats));
524 memset(&pf->veb[i]->stats_offsets, 0,
525 sizeof(pf->veb[i]->stats_offsets));
526 pf->veb[i]->stat_offsets_loaded = false;
527 }
528 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700529 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000530}
531
532/**
533 * i40e_stat_update48 - read and update a 48 bit stat from the chip
534 * @hw: ptr to the hardware info
535 * @hireg: the high 32 bit reg to read
536 * @loreg: the low 32 bit reg to read
537 * @offset_loaded: has the initial offset been loaded yet
538 * @offset: ptr to current offset value
539 * @stat: ptr to the stat
540 *
541 * Since the device stats are not reset at PFReset, they likely will not
542 * be zeroed when the driver starts. We'll save the first values read
543 * and use them as offsets to be subtracted from the raw values in order
544 * to report stats that count from zero. In the process, we also manage
545 * the potential roll-over.
546 **/
547static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
548 bool offset_loaded, u64 *offset, u64 *stat)
549{
550 u64 new_data;
551
Shannon Nelsonab600852014-01-17 15:36:39 -0800552 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000553 new_data = rd32(hw, loreg);
554 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
555 } else {
556 new_data = rd64(hw, loreg);
557 }
558 if (!offset_loaded)
559 *offset = new_data;
560 if (likely(new_data >= *offset))
561 *stat = new_data - *offset;
562 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400563 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000564 *stat &= 0xFFFFFFFFFFFFULL;
565}
566
567/**
568 * i40e_stat_update32 - read and update a 32 bit stat from the chip
569 * @hw: ptr to the hardware info
570 * @reg: the hw reg to read
571 * @offset_loaded: has the initial offset been loaded yet
572 * @offset: ptr to current offset value
573 * @stat: ptr to the stat
574 **/
575static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
576 bool offset_loaded, u64 *offset, u64 *stat)
577{
578 u32 new_data;
579
580 new_data = rd32(hw, reg);
581 if (!offset_loaded)
582 *offset = new_data;
583 if (likely(new_data >= *offset))
584 *stat = (u32)(new_data - *offset);
585 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400586 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000587}
588
589/**
590 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
591 * @vsi: the VSI to be updated
592 **/
593void i40e_update_eth_stats(struct i40e_vsi *vsi)
594{
595 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
596 struct i40e_pf *pf = vsi->back;
597 struct i40e_hw *hw = &pf->hw;
598 struct i40e_eth_stats *oes;
599 struct i40e_eth_stats *es; /* device's eth stats */
600
601 es = &vsi->eth_stats;
602 oes = &vsi->eth_stats_offsets;
603
604 /* Gather up the stats that the hw collects */
605 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
606 vsi->stat_offsets_loaded,
607 &oes->tx_errors, &es->tx_errors);
608 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000611 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
612 vsi->stat_offsets_loaded,
613 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
614 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
615 vsi->stat_offsets_loaded,
616 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000617
618 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
619 I40E_GLV_GORCL(stat_idx),
620 vsi->stat_offsets_loaded,
621 &oes->rx_bytes, &es->rx_bytes);
622 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
623 I40E_GLV_UPRCL(stat_idx),
624 vsi->stat_offsets_loaded,
625 &oes->rx_unicast, &es->rx_unicast);
626 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
627 I40E_GLV_MPRCL(stat_idx),
628 vsi->stat_offsets_loaded,
629 &oes->rx_multicast, &es->rx_multicast);
630 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
631 I40E_GLV_BPRCL(stat_idx),
632 vsi->stat_offsets_loaded,
633 &oes->rx_broadcast, &es->rx_broadcast);
634
635 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
636 I40E_GLV_GOTCL(stat_idx),
637 vsi->stat_offsets_loaded,
638 &oes->tx_bytes, &es->tx_bytes);
639 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
640 I40E_GLV_UPTCL(stat_idx),
641 vsi->stat_offsets_loaded,
642 &oes->tx_unicast, &es->tx_unicast);
643 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
644 I40E_GLV_MPTCL(stat_idx),
645 vsi->stat_offsets_loaded,
646 &oes->tx_multicast, &es->tx_multicast);
647 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
648 I40E_GLV_BPTCL(stat_idx),
649 vsi->stat_offsets_loaded,
650 &oes->tx_broadcast, &es->tx_broadcast);
651 vsi->stat_offsets_loaded = true;
652}
653
654/**
655 * i40e_update_veb_stats - Update Switch component statistics
656 * @veb: the VEB being updated
657 **/
658static void i40e_update_veb_stats(struct i40e_veb *veb)
659{
660 struct i40e_pf *pf = veb->pf;
661 struct i40e_hw *hw = &pf->hw;
662 struct i40e_eth_stats *oes;
663 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400664 struct i40e_veb_tc_stats *veb_oes;
665 struct i40e_veb_tc_stats *veb_es;
666 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000667
668 idx = veb->stats_idx;
669 es = &veb->stats;
670 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400671 veb_es = &veb->tc_stats;
672 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000673
674 /* Gather up the stats that the hw collects */
675 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
676 veb->stat_offsets_loaded,
677 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000678 if (hw->revision_id > 0)
679 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
680 veb->stat_offsets_loaded,
681 &oes->rx_unknown_protocol,
682 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000683 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
684 veb->stat_offsets_loaded,
685 &oes->rx_bytes, &es->rx_bytes);
686 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
687 veb->stat_offsets_loaded,
688 &oes->rx_unicast, &es->rx_unicast);
689 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
690 veb->stat_offsets_loaded,
691 &oes->rx_multicast, &es->rx_multicast);
692 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
693 veb->stat_offsets_loaded,
694 &oes->rx_broadcast, &es->rx_broadcast);
695
696 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
697 veb->stat_offsets_loaded,
698 &oes->tx_bytes, &es->tx_bytes);
699 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
700 veb->stat_offsets_loaded,
701 &oes->tx_unicast, &es->tx_unicast);
702 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_multicast, &es->tx_multicast);
705 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400708 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
709 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
710 I40E_GLVEBTC_RPCL(i, idx),
711 veb->stat_offsets_loaded,
712 &veb_oes->tc_rx_packets[i],
713 &veb_es->tc_rx_packets[i]);
714 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
715 I40E_GLVEBTC_RBCL(i, idx),
716 veb->stat_offsets_loaded,
717 &veb_oes->tc_rx_bytes[i],
718 &veb_es->tc_rx_bytes[i]);
719 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
720 I40E_GLVEBTC_TPCL(i, idx),
721 veb->stat_offsets_loaded,
722 &veb_oes->tc_tx_packets[i],
723 &veb_es->tc_tx_packets[i]);
724 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
725 I40E_GLVEBTC_TBCL(i, idx),
726 veb->stat_offsets_loaded,
727 &veb_oes->tc_tx_bytes[i],
728 &veb_es->tc_tx_bytes[i]);
729 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000730 veb->stat_offsets_loaded = true;
731}
732
Vasu Dev38e00432014-08-01 13:27:03 -0700733#ifdef I40E_FCOE
734/**
735 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
736 * @vsi: the VSI that is capable of doing FCoE
737 **/
738static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
739{
740 struct i40e_pf *pf = vsi->back;
741 struct i40e_hw *hw = &pf->hw;
742 struct i40e_fcoe_stats *ofs;
743 struct i40e_fcoe_stats *fs; /* device's eth stats */
744 int idx;
745
746 if (vsi->type != I40E_VSI_FCOE)
747 return;
748
Kiran Patil4147e2c2016-01-15 14:33:14 -0800749 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700750 fs = &vsi->fcoe_stats;
751 ofs = &vsi->fcoe_stats_offsets;
752
753 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
754 vsi->fcoe_stat_offsets_loaded,
755 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
756 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
757 vsi->fcoe_stat_offsets_loaded,
758 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
759 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
760 vsi->fcoe_stat_offsets_loaded,
761 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
762 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
763 vsi->fcoe_stat_offsets_loaded,
764 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
765 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
768 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
771 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
772 vsi->fcoe_stat_offsets_loaded,
773 &ofs->fcoe_last_error, &fs->fcoe_last_error);
774 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
775 vsi->fcoe_stat_offsets_loaded,
776 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
777
778 vsi->fcoe_stat_offsets_loaded = true;
779}
780
781#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000782/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000783 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 * @vsi: the VSI to be updated
785 *
786 * There are a few instances where we store the same stat in a
787 * couple of different structs. This is partly because we have
788 * the netdev stats that need to be filled out, which is slightly
789 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000790 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000791 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000792static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000793{
794 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 struct rtnl_link_stats64 *ons;
796 struct rtnl_link_stats64 *ns; /* netdev stats */
797 struct i40e_eth_stats *oes;
798 struct i40e_eth_stats *es; /* device's eth stats */
799 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800800 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000801 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000802 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000803 u64 bytes, packets;
804 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400805 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400806 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000807 u64 rx_p, rx_b;
808 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000809 u16 q;
810
811 if (test_bit(__I40E_DOWN, &vsi->state) ||
812 test_bit(__I40E_CONFIG_BUSY, &pf->state))
813 return;
814
815 ns = i40e_get_vsi_stats_struct(vsi);
816 ons = &vsi->net_stats_offsets;
817 es = &vsi->eth_stats;
818 oes = &vsi->eth_stats_offsets;
819
820 /* Gather up the netdev and vsi stats that the driver collects
821 * on the fly during packet processing
822 */
823 rx_b = rx_p = 0;
824 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400825 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800826 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827 rx_page = 0;
828 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000830 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000831 /* locate Tx ring */
832 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833
Alexander Duyck980e9b12013-09-28 06:01:03 +0000834 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700835 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000836 packets = p->stats.packets;
837 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700838 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000839 tx_b += bytes;
840 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000841 tx_restart += p->tx_stats.restart_queue;
842 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400843 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400844 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800845 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000846
847 /* Rx queue is part of the same block as Tx queue */
848 p = &p[1];
849 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700850 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000851 packets = p->stats.packets;
852 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700853 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000854 rx_b += bytes;
855 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000856 rx_buf += p->rx_stats.alloc_buff_failed;
857 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000859 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000860 vsi->tx_restart = tx_restart;
861 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400862 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400863 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800864 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000865 vsi->rx_page_failed = rx_page;
866 vsi->rx_buf_failed = rx_buf;
867
868 ns->rx_packets = rx_p;
869 ns->rx_bytes = rx_b;
870 ns->tx_packets = tx_p;
871 ns->tx_bytes = tx_b;
872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000873 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000874 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000875 ons->tx_errors = oes->tx_errors;
876 ns->tx_errors = es->tx_errors;
877 ons->multicast = oes->rx_multicast;
878 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000879 ons->rx_dropped = oes->rx_discards;
880 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000881 ons->tx_dropped = oes->tx_discards;
882 ns->tx_dropped = es->tx_discards;
883
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000884 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000885 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000886 ns->rx_crc_errors = pf->stats.crc_errors;
887 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
888 ns->rx_length_errors = pf->stats.rx_length_errors;
889 }
890}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000893 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000894 * @pf: the PF to be updated
895 **/
896static void i40e_update_pf_stats(struct i40e_pf *pf)
897{
898 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
899 struct i40e_hw_port_stats *nsd = &pf->stats;
900 struct i40e_hw *hw = &pf->hw;
901 u32 val;
902 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000904 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
905 I40E_GLPRT_GORCL(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
908 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
909 I40E_GLPRT_GOTCL(hw->port),
910 pf->stat_offsets_loaded,
911 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
912 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->eth.rx_discards,
915 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000916 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
917 I40E_GLPRT_UPRCL(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->eth.rx_unicast,
920 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000921 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
922 I40E_GLPRT_MPRCL(hw->port),
923 pf->stat_offsets_loaded,
924 &osd->eth.rx_multicast,
925 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000926 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
927 I40E_GLPRT_BPRCL(hw->port),
928 pf->stat_offsets_loaded,
929 &osd->eth.rx_broadcast,
930 &nsd->eth.rx_broadcast);
931 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
932 I40E_GLPRT_UPTCL(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->eth.tx_unicast,
935 &nsd->eth.tx_unicast);
936 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
937 I40E_GLPRT_MPTCL(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->eth.tx_multicast,
940 &nsd->eth.tx_multicast);
941 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
942 I40E_GLPRT_BPTCL(hw->port),
943 pf->stat_offsets_loaded,
944 &osd->eth.tx_broadcast,
945 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000946
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000947 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
948 pf->stat_offsets_loaded,
949 &osd->tx_dropped_link_down,
950 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000951
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000952 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
953 pf->stat_offsets_loaded,
954 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000955
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000956 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
957 pf->stat_offsets_loaded,
958 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000959
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000960 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->mac_local_faults,
963 &nsd->mac_local_faults);
964 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->mac_remote_faults,
967 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000968
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000969 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->rx_length_errors,
972 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000973
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000974 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->link_xon_rx, &nsd->link_xon_rx);
977 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800980 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000983 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000986
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000987 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800988 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
989 pf->stat_offsets_loaded,
990 &osd->priority_xoff_rx[i],
991 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000993 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000994 &osd->priority_xon_rx[i],
995 &nsd->priority_xon_rx[i]);
996 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000997 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000998 &osd->priority_xon_tx[i],
999 &nsd->priority_xon_tx[i]);
1000 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001001 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001002 &osd->priority_xoff_tx[i],
1003 &nsd->priority_xoff_tx[i]);
1004 i40e_stat_update32(hw,
1005 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001006 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001007 &osd->priority_xon_2_xoff[i],
1008 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001009 }
1010
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001011 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1012 I40E_GLPRT_PRC64L(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->rx_size_64, &nsd->rx_size_64);
1015 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1016 I40E_GLPRT_PRC127L(hw->port),
1017 pf->stat_offsets_loaded,
1018 &osd->rx_size_127, &nsd->rx_size_127);
1019 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1020 I40E_GLPRT_PRC255L(hw->port),
1021 pf->stat_offsets_loaded,
1022 &osd->rx_size_255, &nsd->rx_size_255);
1023 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1024 I40E_GLPRT_PRC511L(hw->port),
1025 pf->stat_offsets_loaded,
1026 &osd->rx_size_511, &nsd->rx_size_511);
1027 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1028 I40E_GLPRT_PRC1023L(hw->port),
1029 pf->stat_offsets_loaded,
1030 &osd->rx_size_1023, &nsd->rx_size_1023);
1031 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1032 I40E_GLPRT_PRC1522L(hw->port),
1033 pf->stat_offsets_loaded,
1034 &osd->rx_size_1522, &nsd->rx_size_1522);
1035 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1036 I40E_GLPRT_PRC9522L(hw->port),
1037 pf->stat_offsets_loaded,
1038 &osd->rx_size_big, &nsd->rx_size_big);
1039
1040 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1041 I40E_GLPRT_PTC64L(hw->port),
1042 pf->stat_offsets_loaded,
1043 &osd->tx_size_64, &nsd->tx_size_64);
1044 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1045 I40E_GLPRT_PTC127L(hw->port),
1046 pf->stat_offsets_loaded,
1047 &osd->tx_size_127, &nsd->tx_size_127);
1048 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1049 I40E_GLPRT_PTC255L(hw->port),
1050 pf->stat_offsets_loaded,
1051 &osd->tx_size_255, &nsd->tx_size_255);
1052 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1053 I40E_GLPRT_PTC511L(hw->port),
1054 pf->stat_offsets_loaded,
1055 &osd->tx_size_511, &nsd->tx_size_511);
1056 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1057 I40E_GLPRT_PTC1023L(hw->port),
1058 pf->stat_offsets_loaded,
1059 &osd->tx_size_1023, &nsd->tx_size_1023);
1060 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1061 I40E_GLPRT_PTC1522L(hw->port),
1062 pf->stat_offsets_loaded,
1063 &osd->tx_size_1522, &nsd->tx_size_1522);
1064 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1065 I40E_GLPRT_PTC9522L(hw->port),
1066 pf->stat_offsets_loaded,
1067 &osd->tx_size_big, &nsd->tx_size_big);
1068
1069 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1070 pf->stat_offsets_loaded,
1071 &osd->rx_undersize, &nsd->rx_undersize);
1072 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->rx_fragments, &nsd->rx_fragments);
1075 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->rx_oversize, &nsd->rx_oversize);
1078 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->rx_jabber, &nsd->rx_jabber);
1081
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001082 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001083 i40e_stat_update32(hw,
1084 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001085 pf->stat_offsets_loaded,
1086 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001087 i40e_stat_update32(hw,
1088 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001089 pf->stat_offsets_loaded,
1090 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001091 i40e_stat_update32(hw,
1092 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1093 pf->stat_offsets_loaded,
1094 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001095
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001096 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1097 nsd->tx_lpi_status =
1098 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1099 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1100 nsd->rx_lpi_status =
1101 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1102 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1103 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1104 pf->stat_offsets_loaded,
1105 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1106 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1107 pf->stat_offsets_loaded,
1108 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1109
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001110 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1111 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1112 nsd->fd_sb_status = true;
1113 else
1114 nsd->fd_sb_status = false;
1115
1116 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1117 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1118 nsd->fd_atr_status = true;
1119 else
1120 nsd->fd_atr_status = false;
1121
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122 pf->stat_offsets_loaded = true;
1123}
1124
1125/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001126 * i40e_update_stats - Update the various statistics counters.
1127 * @vsi: the VSI to be updated
1128 *
1129 * Update the various stats for this VSI and its related entities.
1130 **/
1131void i40e_update_stats(struct i40e_vsi *vsi)
1132{
1133 struct i40e_pf *pf = vsi->back;
1134
1135 if (vsi == pf->vsi[pf->lan_vsi])
1136 i40e_update_pf_stats(pf);
1137
1138 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001139#ifdef I40E_FCOE
1140 i40e_update_fcoe_stats(vsi);
1141#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001142}
1143
1144/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001145 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1146 * @vsi: the VSI to be searched
1147 * @macaddr: the MAC address
1148 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001149 *
1150 * Returns ptr to the filter object or NULL
1151 **/
1152static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001153 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001154{
1155 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001156 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001157
1158 if (!vsi || !macaddr)
1159 return NULL;
1160
Jacob Keller278e7d02016-10-05 09:30:37 -07001161 key = i40e_addr_to_hkey(macaddr);
1162 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001163 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001164 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001165 return f;
1166 }
1167 return NULL;
1168}
1169
1170/**
1171 * i40e_find_mac - Find a mac addr in the macvlan filters list
1172 * @vsi: the VSI to be searched
1173 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001174 *
1175 * Returns the first filter with the provided MAC address or NULL if
1176 * MAC address was not found
1177 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001178struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001179{
1180 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001181 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182
1183 if (!vsi || !macaddr)
1184 return NULL;
1185
Jacob Keller278e7d02016-10-05 09:30:37 -07001186 key = i40e_addr_to_hkey(macaddr);
1187 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001188 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001189 return f;
1190 }
1191 return NULL;
1192}
1193
1194/**
1195 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1196 * @vsi: the VSI to be searched
1197 *
1198 * Returns true if VSI is in vlan mode or false otherwise
1199 **/
1200bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1201{
Jacob Kellercbebb852016-10-05 09:30:40 -07001202 /* If we have a PVID, always operate in VLAN mode */
1203 if (vsi->info.pvid)
1204 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001205
Jacob Kellercbebb852016-10-05 09:30:40 -07001206 /* We need to operate in VLAN mode whenever we have any filters with
1207 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1208 * time, incurring search cost repeatedly. However, we can notice two
1209 * things:
1210 *
1211 * 1) the only place where we can gain a VLAN filter is in
1212 * i40e_add_filter.
1213 *
1214 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001215 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001216 *
1217 * Thus, we can simply use a boolean value, has_vlan_filters which we
1218 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1219 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001220 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001221 * filters here and can perform the check at the same time. This
1222 * results in avoiding embedding a loop for VLAN mode inside another
1223 * loop over all the filters, and should maintain correctness as noted
1224 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001225 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001226 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001227}
1228
1229/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001230 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1231 * @vsi: the PF Main VSI - inappropriate for any other VSI
1232 * @macaddr: the MAC address
1233 *
1234 * Remove whatever filter the firmware set up so the driver can manage
1235 * its own filtering intelligently.
1236 **/
1237static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1238{
1239 struct i40e_aqc_remove_macvlan_element_data element;
1240 struct i40e_pf *pf = vsi->back;
1241
1242 /* Only appropriate for the PF main VSI */
1243 if (vsi->type != I40E_VSI_MAIN)
1244 return;
1245
1246 memset(&element, 0, sizeof(element));
1247 ether_addr_copy(element.mac_addr, macaddr);
1248 element.vlan_tag = 0;
1249 /* Ignore error returns, some firmware does it this way... */
1250 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1251 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1252
1253 memset(&element, 0, sizeof(element));
1254 ether_addr_copy(element.mac_addr, macaddr);
1255 element.vlan_tag = 0;
1256 /* ...and some firmware does it this way. */
1257 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1258 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1259 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1260}
1261
1262/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001263 * i40e_add_filter - Add a mac/vlan filter to the VSI
1264 * @vsi: the VSI to be searched
1265 * @macaddr: the MAC address
1266 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001267 *
1268 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001269 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001270 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001271 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001272 **/
1273struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001274 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001275{
1276 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001277 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001278
1279 if (!vsi || !macaddr)
1280 return NULL;
1281
Jacob Keller1bc87e82016-10-05 09:30:31 -07001282 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001283 if (!f) {
1284 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1285 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001286 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001287
Jacob Kellercbebb852016-10-05 09:30:40 -07001288 /* Update the boolean indicating if we need to function in
1289 * VLAN mode.
1290 */
1291 if (vlan >= 0)
1292 vsi->has_vlan_filter = true;
1293
Greg Rose9a173902014-05-22 06:32:02 +00001294 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001295 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001296 /* If we're in overflow promisc mode, set the state directly
1297 * to failed, so we don't bother to try sending the filter
1298 * to the hardware.
1299 */
1300 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1301 f->state = I40E_FILTER_FAILED;
1302 else
1303 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001304 INIT_HLIST_NODE(&f->hlist);
1305
1306 key = i40e_addr_to_hkey(macaddr);
1307 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001309 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1310 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1311 }
1312
Jacob Keller1bc87e82016-10-05 09:30:31 -07001313 /* If we're asked to add a filter that has been marked for removal, it
1314 * is safe to simply restore it to active state. __i40e_del_filter
1315 * will have simply deleted any filters which were previously marked
1316 * NEW or FAILED, so if it is currently marked REMOVE it must have
1317 * previously been ACTIVE. Since we haven't yet run the sync filters
1318 * task, just restore this filter to the ACTIVE state so that the
1319 * sync task leaves it in place
1320 */
1321 if (f->state == I40E_FILTER_REMOVE)
1322 f->state = I40E_FILTER_ACTIVE;
1323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001324 return f;
1325}
1326
1327/**
Jacob Keller290d2552016-10-05 09:30:36 -07001328 * __i40e_del_filter - Remove a specific filter from the VSI
1329 * @vsi: VSI to remove from
1330 * @f: the filter to remove from the list
1331 *
1332 * This function should be called instead of i40e_del_filter only if you know
1333 * the exact filter you will remove already, such as via i40e_find_filter or
1334 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001335 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001336 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001337 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001338 * ANOTHER NOTE: This function MUST be called from within the context of
1339 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1340 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001342static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001343{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001344 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001345 return;
1346
Jacob Keller1bc87e82016-10-05 09:30:31 -07001347 if ((f->state == I40E_FILTER_FAILED) ||
1348 (f->state == I40E_FILTER_NEW)) {
1349 /* this one never got added by the FW. Just remove it,
1350 * no need to sync anything.
1351 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001352 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001353 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001355 f->state = I40E_FILTER_REMOVE;
1356 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1357 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001358 }
1359}
1360
1361/**
Jacob Keller290d2552016-10-05 09:30:36 -07001362 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1363 * @vsi: the VSI to be searched
1364 * @macaddr: the MAC address
1365 * @vlan: the VLAN
1366 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001367 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001368 * being held.
1369 * ANOTHER NOTE: This function MUST be called from within the context of
1370 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1371 * instead of list_for_each_entry().
1372 **/
1373void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1374{
1375 struct i40e_mac_filter *f;
1376
1377 if (!vsi || !macaddr)
1378 return;
1379
1380 f = i40e_find_filter(vsi, macaddr, vlan);
1381 __i40e_del_filter(vsi, f);
1382}
1383
1384/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001385 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1386 * @vsi: the VSI to be searched
1387 * @macaddr: the mac address to be filtered
1388 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001389 * Goes through all the macvlan filters and adds a macvlan filter for each
1390 * unique vlan that already exists. If a PVID has been assigned, instead only
1391 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001392 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001393 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001394 **/
1395struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1396 const u8 *macaddr)
1397{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001398 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001399 struct hlist_node *h;
1400 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001401
1402 if (vsi->info.pvid)
1403 return i40e_add_filter(vsi, macaddr,
1404 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001405
Jacob Keller278e7d02016-10-05 09:30:37 -07001406 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001407 if (f->state == I40E_FILTER_REMOVE)
1408 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001409 add = i40e_add_filter(vsi, macaddr, f->vlan);
1410 if (!add)
1411 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001412 }
1413
Jacob Keller5feb3d72016-10-05 09:30:34 -07001414 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001415}
1416
1417/**
1418 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1419 * @vsi: the VSI to be searched
1420 * @macaddr: the mac address to be removed
1421 *
1422 * Removes a given MAC address from a VSI, regardless of VLAN
1423 *
1424 * Returns 0 for success, or error
1425 **/
1426int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1427{
Jacob Keller278e7d02016-10-05 09:30:37 -07001428 struct i40e_mac_filter *f;
1429 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001430 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001431 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001432
Jacob Keller278e7d02016-10-05 09:30:37 -07001433 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1434 "Missing mac_filter_hash_lock\n");
1435 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001436 if (ether_addr_equal(macaddr, f->macaddr)) {
1437 __i40e_del_filter(vsi, f);
1438 found = true;
1439 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001440 }
Jacob Keller290d2552016-10-05 09:30:36 -07001441
1442 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001443 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001444 else
1445 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001446}
1447
1448/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449 * i40e_set_mac - NDO callback to set mac address
1450 * @netdev: network interface device structure
1451 * @p: pointer to an address structure
1452 *
1453 * Returns 0 on success, negative on failure
1454 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001455#ifdef I40E_FCOE
1456int i40e_set_mac(struct net_device *netdev, void *p)
1457#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001458static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001459#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460{
1461 struct i40e_netdev_priv *np = netdev_priv(netdev);
1462 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001463 struct i40e_pf *pf = vsi->back;
1464 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001465 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001466
1467 if (!is_valid_ether_addr(addr->sa_data))
1468 return -EADDRNOTAVAIL;
1469
Shannon Nelson30650cc2014-07-29 04:01:50 +00001470 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1471 netdev_info(netdev, "already using mac address %pM\n",
1472 addr->sa_data);
1473 return 0;
1474 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001475
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001476 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1477 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1478 return -EADDRNOTAVAIL;
1479
Shannon Nelson30650cc2014-07-29 04:01:50 +00001480 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1481 netdev_info(netdev, "returning to hw mac address %pM\n",
1482 hw->mac.addr);
1483 else
1484 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1485
Jacob Keller278e7d02016-10-05 09:30:37 -07001486 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001487 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1488 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001489 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001490 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001491 if (vsi->type == I40E_VSI_MAIN) {
1492 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001493
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001494 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001495 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001496 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001497 if (ret)
1498 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1499 i40e_stat_str(hw, ret),
1500 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001501 }
1502
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001503 /* schedule our worker thread which will take care of
1504 * applying the new filter changes
1505 */
1506 i40e_service_event_schedule(vsi->back);
1507 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001508}
1509
1510/**
1511 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1512 * @vsi: the VSI being setup
1513 * @ctxt: VSI context structure
1514 * @enabled_tc: Enabled TCs bitmap
1515 * @is_add: True if called before Add VSI
1516 *
1517 * Setup VSI queue mapping for enabled traffic classes.
1518 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001519#ifdef I40E_FCOE
1520void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1521 struct i40e_vsi_context *ctxt,
1522 u8 enabled_tc,
1523 bool is_add)
1524#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001525static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1526 struct i40e_vsi_context *ctxt,
1527 u8 enabled_tc,
1528 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001529#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001530{
1531 struct i40e_pf *pf = vsi->back;
1532 u16 sections = 0;
1533 u8 netdev_tc = 0;
1534 u16 numtc = 0;
1535 u16 qcount;
1536 u8 offset;
1537 u16 qmap;
1538 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001539 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001540
1541 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1542 offset = 0;
1543
1544 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1545 /* Find numtc from enabled TC bitmap */
1546 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001547 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001548 numtc++;
1549 }
1550 if (!numtc) {
1551 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1552 numtc = 1;
1553 }
1554 } else {
1555 /* At least TC0 is enabled in case of non-DCB case */
1556 numtc = 1;
1557 }
1558
1559 vsi->tc_config.numtc = numtc;
1560 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001561 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001562 qcount = vsi->alloc_queue_pairs;
1563
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001564 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001565 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566
1567 /* Setup queue offset/count for all TCs for given VSI */
1568 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1569 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001570 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001571 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 int pow, num_qps;
1573
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001574 switch (vsi->type) {
1575 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001576 qcount = min_t(int, pf->alloc_rss_size,
1577 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001578 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001579#ifdef I40E_FCOE
1580 case I40E_VSI_FCOE:
1581 qcount = num_tc_qps;
1582 break;
1583#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001584 case I40E_VSI_FDIR:
1585 case I40E_VSI_SRIOV:
1586 case I40E_VSI_VMDQ2:
1587 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001588 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001589 WARN_ON(i != 0);
1590 break;
1591 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001592 vsi->tc_config.tc_info[i].qoffset = offset;
1593 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001594
Shannon Nelson1e200e42015-02-27 09:15:24 +00001595 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001596 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001597 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001598 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001599 pow++;
1600 num_qps >>= 1;
1601 }
1602
1603 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1604 qmap =
1605 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1606 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1607
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001608 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609 } else {
1610 /* TC is not enabled so set the offset to
1611 * default queue and allocate one queue
1612 * for the given TC.
1613 */
1614 vsi->tc_config.tc_info[i].qoffset = 0;
1615 vsi->tc_config.tc_info[i].qcount = 1;
1616 vsi->tc_config.tc_info[i].netdev_tc = 0;
1617
1618 qmap = 0;
1619 }
1620 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1621 }
1622
1623 /* Set actual Tx/Rx queue pairs */
1624 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001625 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1626 if (vsi->req_queue_pairs > 0)
1627 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001628 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001629 vsi->num_queue_pairs = pf->num_lan_msix;
1630 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001631
1632 /* Scheduler section valid can only be set for ADD VSI */
1633 if (is_add) {
1634 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1635
1636 ctxt->info.up_enable_bits = enabled_tc;
1637 }
1638 if (vsi->type == I40E_VSI_SRIOV) {
1639 ctxt->info.mapping_flags |=
1640 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1641 for (i = 0; i < vsi->num_queue_pairs; i++)
1642 ctxt->info.queue_mapping[i] =
1643 cpu_to_le16(vsi->base_queue + i);
1644 } else {
1645 ctxt->info.mapping_flags |=
1646 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1647 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1648 }
1649 ctxt->info.valid_sections |= cpu_to_le16(sections);
1650}
1651
1652/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001653 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1654 * @netdev: the netdevice
1655 * @addr: address to add
1656 *
1657 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1658 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1659 */
1660static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1661{
1662 struct i40e_netdev_priv *np = netdev_priv(netdev);
1663 struct i40e_vsi *vsi = np->vsi;
1664 struct i40e_mac_filter *f;
1665
1666 if (i40e_is_vsi_in_vlan(vsi))
1667 f = i40e_put_mac_in_vlan(vsi, addr);
1668 else
1669 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1670
1671 if (f)
1672 return 0;
1673 else
1674 return -ENOMEM;
1675}
1676
1677/**
1678 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1679 * @netdev: the netdevice
1680 * @addr: address to add
1681 *
1682 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1683 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1684 */
1685static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1686{
1687 struct i40e_netdev_priv *np = netdev_priv(netdev);
1688 struct i40e_vsi *vsi = np->vsi;
1689
1690 if (i40e_is_vsi_in_vlan(vsi))
1691 i40e_del_mac_all_vlan(vsi, addr);
1692 else
1693 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1694
1695 return 0;
1696}
1697
1698/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 * i40e_set_rx_mode - NDO callback to set the netdev filters
1700 * @netdev: network interface device structure
1701 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001702#ifdef I40E_FCOE
1703void i40e_set_rx_mode(struct net_device *netdev)
1704#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001705static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001706#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001707{
1708 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001710
Jacob Keller278e7d02016-10-05 09:30:37 -07001711 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001712
Jacob Keller6622f5c2016-10-05 09:30:32 -07001713 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1714 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001715
Jacob Keller278e7d02016-10-05 09:30:37 -07001716 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001717
1718 /* check for other flag changes */
1719 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1720 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1721 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1722 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001723
1724 /* schedule our worker thread which will take care of
1725 * applying the new filter changes
1726 */
1727 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001728}
1729
1730/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001731 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1732 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001733 * @from: Pointer to list which contains MAC filter entries - changes to
1734 * those entries needs to be undone.
1735 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001736 * MAC filter entries from list were slated to be sent to firmware, either for
1737 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001738 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001739static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1740 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001741{
Jacob Keller278e7d02016-10-05 09:30:37 -07001742 struct i40e_mac_filter *f;
1743 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001744
Jacob Keller278e7d02016-10-05 09:30:37 -07001745 hlist_for_each_entry_safe(f, h, from, hlist) {
1746 u64 key = i40e_addr_to_hkey(f->macaddr);
1747
Kiran Patil21659032015-09-30 14:09:03 -04001748 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001749 hlist_del(&f->hlist);
1750 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001751 }
1752}
1753
1754/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001755 * i40e_update_filter_state - Update filter state based on return data
1756 * from firmware
1757 * @count: Number of filters added
1758 * @add_list: return data from fw
1759 * @head: pointer to first filter in current batch
1760 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001761 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001762 * MAC filter entries from list were slated to be added to device. Returns
1763 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001764 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001765static int
1766i40e_update_filter_state(int count,
1767 struct i40e_aqc_add_macvlan_element_data *add_list,
1768 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001769{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001770 int retval = 0;
1771 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001772
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001773
1774 if (!aq_err) {
1775 retval = count;
1776 /* Everything's good, mark all filters active. */
1777 for (i = 0; i < count ; i++) {
1778 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001779 add_head = hlist_entry(add_head->hlist.next,
1780 typeof(struct i40e_mac_filter),
1781 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001782 }
1783 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1784 /* Device ran out of filter space. Check the return value
1785 * for each filter to see which ones are active.
1786 */
1787 for (i = 0; i < count ; i++) {
1788 if (add_list[i].match_method ==
1789 I40E_AQC_MM_ERR_NO_RES) {
1790 add_head->state = I40E_FILTER_FAILED;
1791 } else {
1792 add_head->state = I40E_FILTER_ACTIVE;
1793 retval++;
1794 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001795 add_head = hlist_entry(add_head->hlist.next,
1796 typeof(struct i40e_mac_filter),
1797 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001798 }
1799 } else {
1800 /* Some other horrible thing happened, fail all filters */
1801 retval = 0;
1802 for (i = 0; i < count ; i++) {
1803 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001804 add_head = hlist_entry(add_head->hlist.next,
1805 typeof(struct i40e_mac_filter),
1806 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001807 }
Kiran Patil21659032015-09-30 14:09:03 -04001808 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001809 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001810}
1811
1812/**
Jacob Keller00936312016-10-05 09:30:41 -07001813 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1814 * @vsi: ptr to the VSI
1815 * @vsi_name: name to display in messages
1816 * @list: the list of filters to send to firmware
1817 * @num_del: the number of filters to delete
1818 * @retval: Set to -EIO on failure to delete
1819 *
1820 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1821 * *retval instead of a return value so that success does not force ret_val to
1822 * be set to 0. This ensures that a sequence of calls to this function
1823 * preserve the previous value of *retval on successful delete.
1824 */
1825static
1826void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1827 struct i40e_aqc_remove_macvlan_element_data *list,
1828 int num_del, int *retval)
1829{
1830 struct i40e_hw *hw = &vsi->back->hw;
1831 i40e_status aq_ret;
1832 int aq_err;
1833
1834 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1835 aq_err = hw->aq.asq_last_status;
1836
1837 /* Explicitly ignore and do not report when firmware returns ENOENT */
1838 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1839 *retval = -EIO;
1840 dev_info(&vsi->back->pdev->dev,
1841 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1842 vsi_name, i40e_stat_str(hw, aq_ret),
1843 i40e_aq_str(hw, aq_err));
1844 }
1845}
1846
1847/**
1848 * i40e_aqc_add_filters - Request firmware to add a set of filters
1849 * @vsi: ptr to the VSI
1850 * @vsi_name: name to display in messages
1851 * @list: the list of filters to send to firmware
1852 * @add_head: Position in the add hlist
1853 * @num_add: the number of filters to add
1854 * @promisc_change: set to true on exit if promiscuous mode was forced on
1855 *
1856 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1857 * promisc_changed to true if the firmware has run out of space for more
1858 * filters.
1859 */
1860static
1861void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1862 struct i40e_aqc_add_macvlan_element_data *list,
1863 struct i40e_mac_filter *add_head,
1864 int num_add, bool *promisc_changed)
1865{
1866 struct i40e_hw *hw = &vsi->back->hw;
1867 i40e_status aq_ret;
1868 int aq_err, fcnt;
1869
1870 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
1871 aq_err = hw->aq.asq_last_status;
1872 fcnt = i40e_update_filter_state(num_add, list, add_head, aq_ret);
Jacob Keller00936312016-10-05 09:30:41 -07001873
1874 if (fcnt != num_add) {
1875 *promisc_changed = true;
1876 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001877 dev_warn(&vsi->back->pdev->dev,
1878 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1879 i40e_aq_str(hw, aq_err),
1880 vsi_name);
1881 }
1882}
1883
1884/**
Jacob Keller435c0842016-11-08 13:05:10 -08001885 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1886 * @vsi: pointer to the VSI
1887 * @f: filter data
1888 *
1889 * This function sets or clears the promiscuous broadcast flags for VLAN
1890 * filters in order to properly receive broadcast frames. Assumes that only
1891 * broadcast filters are passed.
1892 **/
1893static
1894void i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1895 struct i40e_mac_filter *f)
1896{
1897 bool enable = f->state == I40E_FILTER_NEW;
1898 struct i40e_hw *hw = &vsi->back->hw;
1899 i40e_status aq_ret;
1900
1901 if (f->vlan == I40E_VLAN_ANY) {
1902 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1903 vsi->seid,
1904 enable,
1905 NULL);
1906 } else {
1907 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1908 vsi->seid,
1909 enable,
1910 f->vlan,
1911 NULL);
1912 }
1913
1914 if (aq_ret) {
1915 dev_warn(&vsi->back->pdev->dev,
1916 "Error %s setting broadcast promiscuous mode on %s\n",
1917 i40e_aq_str(hw, hw->aq.asq_last_status),
1918 vsi_name);
1919 f->state = I40E_FILTER_FAILED;
1920 } else if (enable) {
1921 f->state = I40E_FILTER_ACTIVE;
1922 }
1923}
1924
1925/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001926 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1927 * @vsi: ptr to the VSI
1928 *
1929 * Push any outstanding VSI filter changes through the AdminQ.
1930 *
1931 * Returns 0 or error value
1932 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001933int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001934{
Jacob Keller278e7d02016-10-05 09:30:37 -07001935 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07001936 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001937 struct i40e_hw *hw = &vsi->back->hw;
Alan Brady84f5ca62016-10-05 09:30:39 -07001938 unsigned int vlan_any_filters = 0;
1939 unsigned int non_vlan_filters = 0;
Jacob Keller38326212016-11-11 12:39:26 -08001940 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001941 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001942 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001943 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001944 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001945 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001946 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001947 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001948 struct i40e_pf *pf;
1949 int num_add = 0;
1950 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001951 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001952 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001953 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07001954 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001955
1956 /* empty array typed pointers, kcalloc later */
1957 struct i40e_aqc_add_macvlan_element_data *add_list;
1958 struct i40e_aqc_remove_macvlan_element_data *del_list;
1959
1960 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1961 usleep_range(1000, 2000);
1962 pf = vsi->back;
1963
1964 if (vsi->netdev) {
1965 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1966 vsi->current_netdev_flags = vsi->netdev->flags;
1967 }
1968
Jacob Keller278e7d02016-10-05 09:30:37 -07001969 INIT_HLIST_HEAD(&tmp_add_list);
1970 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001971
Shannon Nelson2d1de822016-05-16 10:26:44 -07001972 if (vsi->type == I40E_VSI_SRIOV)
1973 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1974 else if (vsi->type != I40E_VSI_MAIN)
1975 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1976
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001977 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1978 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1979
Jacob Keller278e7d02016-10-05 09:30:37 -07001980 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001981 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001982 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001983 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001984 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001985 hash_del(&f->hlist);
1986 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07001987
1988 /* Avoid counting removed filters */
1989 continue;
Kiran Patil21659032015-09-30 14:09:03 -04001990 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001991 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001992 hash_del(&f->hlist);
1993 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001994 }
Alan Brady84f5ca62016-10-05 09:30:39 -07001995
1996 /* Count the number of each type of filter we have
1997 * remaining, ignoring any filters we're about to
1998 * delete.
1999 */
2000 if (f->vlan > 0)
2001 vlan_filters++;
2002 else if (!f->vlan)
2003 non_vlan_filters++;
2004 else
2005 vlan_any_filters++;
2006 }
2007
2008 /* We should never have VLAN=-1 filters at the same time as we
2009 * have either VLAN=0 or VLAN>0 filters, so warn about this
2010 * case here to help catch any issues.
2011 */
2012 WARN_ON(vlan_any_filters && (vlan_filters + non_vlan_filters));
2013
2014 /* If we only have VLAN=0 filters remaining, and don't have
2015 * any other VLAN filters, we need to convert these VLAN=0
2016 * filters into VLAN=-1 (I40E_VLAN_ANY) so that we operate
2017 * correctly in non-VLAN mode and receive all traffic tagged
2018 * or untagged.
2019 */
2020 if (non_vlan_filters && !vlan_filters) {
2021 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
2022 hlist) {
2023 /* Only replace VLAN=0 filters */
2024 if (f->vlan)
2025 continue;
2026
2027 /* Allocate a replacement element */
2028 add_head = kzalloc(sizeof(*add_head),
2029 GFP_KERNEL);
2030 if (!add_head)
2031 goto err_no_memory_locked;
2032
2033 /* Copy the filter, with new state and VLAN */
2034 *add_head = *f;
2035 add_head->state = I40E_FILTER_NEW;
2036 add_head->vlan = I40E_VLAN_ANY;
2037
2038 /* Move the replacement to the add list */
2039 INIT_HLIST_NODE(&add_head->hlist);
2040 hlist_add_head(&add_head->hlist,
2041 &tmp_add_list);
2042
2043 /* Move the original to the delete list */
2044 f->state = I40E_FILTER_REMOVE;
2045 hash_del(&f->hlist);
2046 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002047 }
2048
2049 /* Also update any filters on the tmp_add list */
2050 hlist_for_each_entry(f, &tmp_add_list, hlist) {
2051 if (!f->vlan)
2052 f->vlan = I40E_VLAN_ANY;
2053 }
2054 add_head = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002055 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002056 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002057 }
2058
2059 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002060 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002061 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002062 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002063 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002064 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002065 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002066 if (!del_list)
2067 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002068
Jacob Keller278e7d02016-10-05 09:30:37 -07002069 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002070 cmd_flags = 0;
2071
Jacob Keller435c0842016-11-08 13:05:10 -08002072 /* handle broadcast filters by updating the broadcast
2073 * promiscuous flag instead of deleting a MAC filter.
2074 */
2075 if (is_broadcast_ether_addr(f->macaddr)) {
2076 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2077
2078 hlist_del(&f->hlist);
2079 kfree(f);
2080 continue;
2081 }
2082
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002083 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002084 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002085 if (f->vlan == I40E_VLAN_ANY) {
2086 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002087 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002088 } else {
2089 del_list[num_del].vlan_tag =
2090 cpu_to_le16((u16)(f->vlan));
2091 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002093 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2094 del_list[num_del].flags = cmd_flags;
2095 num_del++;
2096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097 /* flush a full buffer */
2098 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002099 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2100 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002101 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002102 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002103 }
Kiran Patil21659032015-09-30 14:09:03 -04002104 /* Release memory for MAC filter entries which were
2105 * synced up with HW.
2106 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002107 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002108 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002109 }
Kiran Patil21659032015-09-30 14:09:03 -04002110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002111 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002112 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2113 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114 }
2115
2116 kfree(del_list);
2117 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002118 }
2119
Jacob Kellercbebb852016-10-05 09:30:40 -07002120 /* After finishing notifying firmware of the deleted filters, update
2121 * the cached value of vsi->has_vlan_filter. Note that we are safe to
2122 * use just !!vlan_filters here because if we only have VLAN=0 (that
2123 * is, non_vlan_filters) these will all be converted to VLAN=-1 in the
2124 * logic above already so this value would still be correct.
2125 */
2126 vsi->has_vlan_filter = !!vlan_filters;
2127
Jacob Keller278e7d02016-10-05 09:30:37 -07002128 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002129 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002130 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002131 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002132 list_size = filter_list_len *
2133 sizeof(struct i40e_aqc_add_macvlan_element_data);
2134 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002135 if (!add_list)
2136 goto err_no_memory;
2137
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002138 num_add = 0;
Jacob Keller435c0842016-11-08 13:05:10 -08002139 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002140 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2141 &vsi->state)) {
2142 f->state = I40E_FILTER_FAILED;
2143 continue;
2144 }
Jacob Keller435c0842016-11-08 13:05:10 -08002145
2146 /* handle broadcast filters by updating the broadcast
2147 * promiscuous flag instead of adding a MAC filter.
2148 */
2149 if (is_broadcast_ether_addr(f->macaddr)) {
2150 u64 key = i40e_addr_to_hkey(f->macaddr);
2151 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2152
2153 hlist_del(&f->hlist);
2154 hash_add(vsi->mac_filter_hash, &f->hlist, key);
2155 continue;
2156 }
2157
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002158 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002159 if (num_add == 0)
2160 add_head = f;
2161 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002162 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002163 if (f->vlan == I40E_VLAN_ANY) {
2164 add_list[num_add].vlan_tag = 0;
2165 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2166 } else {
2167 add_list[num_add].vlan_tag =
2168 cpu_to_le16((u16)(f->vlan));
2169 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002170 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002171 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002172 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2173 num_add++;
2174
2175 /* flush a full buffer */
2176 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002177 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2178 add_head, num_add,
2179 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002180 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002182 }
2183 }
2184 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002185 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2186 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002188 /* Now move all of the filters from the temp add list back to
2189 * the VSI's list.
2190 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002191 spin_lock_bh(&vsi->mac_filter_hash_lock);
2192 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2193 u64 key = i40e_addr_to_hkey(f->macaddr);
2194
2195 hlist_del(&f->hlist);
2196 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002197 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002198 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002199 kfree(add_list);
2200 add_list = NULL;
2201 }
2202
Jacob Keller38326212016-11-11 12:39:26 -08002203 /* Determine the number of active and failed filters. */
2204 spin_lock_bh(&vsi->mac_filter_hash_lock);
2205 vsi->active_filters = 0;
2206 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2207 if (f->state == I40E_FILTER_ACTIVE)
2208 vsi->active_filters++;
2209 else if (f->state == I40E_FILTER_FAILED)
2210 failed_filters++;
2211 }
2212 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2213
2214 /* If promiscuous mode has changed, we need to calculate a new
2215 * threshold for when we are safe to exit
2216 */
2217 if (promisc_changed)
2218 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2219
2220 /* Check if we are able to exit overflow promiscuous mode. We can
2221 * safely exit if we didn't just enter, we no longer have any failed
2222 * filters, and we have reduced filters below the threshold value.
2223 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002224 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002225 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002226 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002227 dev_info(&pf->pdev->dev,
2228 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2229 vsi_name);
2230 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2231 promisc_changed = true;
2232 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233 }
2234
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002235 /* if the VF is not trusted do not do promisc */
2236 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2237 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2238 goto out;
2239 }
2240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 /* check for changes in promiscuous modes */
2242 if (changed_flags & IFF_ALLMULTI) {
2243 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002244
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002245 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002246 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2247 vsi->seid,
2248 cur_multipromisc,
2249 NULL);
2250 if (aq_ret) {
2251 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002252 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002254 "set multi promisc failed on %s, err %s aq_err %s\n",
2255 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002256 i40e_stat_str(hw, aq_ret),
2257 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002258 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002259 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002260 if ((changed_flags & IFF_PROMISC) ||
2261 (promisc_changed &&
2262 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002265 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2266 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2267 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002268 if ((vsi->type == I40E_VSI_MAIN) &&
2269 (pf->lan_veb != I40E_NO_VEB) &&
2270 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002271 /* set defport ON for Main VSI instead of true promisc
2272 * this way we will get all unicast/multicast and VLAN
2273 * promisc behavior but will not get VF or VMDq traffic
2274 * replicated on the Main VSI.
2275 */
2276 if (pf->cur_promisc != cur_promisc) {
2277 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002278 if (cur_promisc)
2279 aq_ret =
2280 i40e_aq_set_default_vsi(hw,
2281 vsi->seid,
2282 NULL);
2283 else
2284 aq_ret =
2285 i40e_aq_clear_default_vsi(hw,
2286 vsi->seid,
2287 NULL);
2288 if (aq_ret) {
2289 retval = i40e_aq_rc_to_posix(aq_ret,
2290 hw->aq.asq_last_status);
2291 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002292 "Set default VSI failed on %s, err %s, aq_err %s\n",
2293 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002294 i40e_stat_str(hw, aq_ret),
2295 i40e_aq_str(hw,
2296 hw->aq.asq_last_status));
2297 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002298 }
2299 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002300 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002301 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002302 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002303 cur_promisc, NULL,
2304 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002305 if (aq_ret) {
2306 retval =
2307 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002308 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002309 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002310 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2311 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002312 i40e_stat_str(hw, aq_ret),
2313 i40e_aq_str(hw,
2314 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002315 }
2316 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002317 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002318 vsi->seid,
2319 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002320 if (aq_ret) {
2321 retval =
2322 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002323 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002324 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002325 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2326 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002327 i40e_stat_str(hw, aq_ret),
2328 i40e_aq_str(hw,
2329 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002330 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002331 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002332 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2333 vsi->seid,
2334 cur_promisc, NULL);
2335 if (aq_ret) {
2336 retval = i40e_aq_rc_to_posix(aq_ret,
2337 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002338 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002339 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002340 i40e_stat_str(hw, aq_ret),
2341 i40e_aq_str(hw,
2342 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002343 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002344 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002345out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002346 /* if something went wrong then set the changed flag so we try again */
2347 if (retval)
2348 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2349
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002350 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002351 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002352
2353err_no_memory:
2354 /* Restore elements on the temporary add and delete lists */
2355 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002356err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002357 i40e_undo_filter_entries(vsi, &tmp_del_list);
2358 i40e_undo_filter_entries(vsi, &tmp_add_list);
2359 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2360
2361 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2362 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2363 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364}
2365
2366/**
2367 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2368 * @pf: board private structure
2369 **/
2370static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2371{
2372 int v;
2373
2374 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2375 return;
2376 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2377
Mitch Williams505682c2014-05-20 08:01:37 +00002378 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002379 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002380 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2381 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2382
2383 if (ret) {
2384 /* come back and try again later */
2385 pf->flags |= I40E_FLAG_FILTER_SYNC;
2386 break;
2387 }
2388 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002389 }
2390}
2391
2392/**
2393 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2394 * @netdev: network interface device structure
2395 * @new_mtu: new value for maximum frame size
2396 *
2397 * Returns 0 on success, negative on failure
2398 **/
2399static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2400{
2401 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002402 struct i40e_vsi *vsi = np->vsi;
2403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002404 netdev_info(netdev, "changing MTU from %d to %d\n",
2405 netdev->mtu, new_mtu);
2406 netdev->mtu = new_mtu;
2407 if (netif_running(netdev))
2408 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002409 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410 return 0;
2411}
2412
2413/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002414 * i40e_ioctl - Access the hwtstamp interface
2415 * @netdev: network interface device structure
2416 * @ifr: interface request data
2417 * @cmd: ioctl command
2418 **/
2419int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2420{
2421 struct i40e_netdev_priv *np = netdev_priv(netdev);
2422 struct i40e_pf *pf = np->vsi->back;
2423
2424 switch (cmd) {
2425 case SIOCGHWTSTAMP:
2426 return i40e_ptp_get_ts_config(pf, ifr);
2427 case SIOCSHWTSTAMP:
2428 return i40e_ptp_set_ts_config(pf, ifr);
2429 default:
2430 return -EOPNOTSUPP;
2431 }
2432}
2433
2434/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002435 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2436 * @vsi: the vsi being adjusted
2437 **/
2438void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2439{
2440 struct i40e_vsi_context ctxt;
2441 i40e_status ret;
2442
2443 if ((vsi->info.valid_sections &
2444 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2445 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2446 return; /* already enabled */
2447
2448 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2449 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2450 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2451
2452 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002453 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002454 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2455 if (ret) {
2456 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002457 "update vlan stripping failed, err %s aq_err %s\n",
2458 i40e_stat_str(&vsi->back->hw, ret),
2459 i40e_aq_str(&vsi->back->hw,
2460 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002461 }
2462}
2463
2464/**
2465 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2466 * @vsi: the vsi being adjusted
2467 **/
2468void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2469{
2470 struct i40e_vsi_context ctxt;
2471 i40e_status ret;
2472
2473 if ((vsi->info.valid_sections &
2474 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2475 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2476 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2477 return; /* already disabled */
2478
2479 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2480 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2481 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2482
2483 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002484 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2486 if (ret) {
2487 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002488 "update vlan stripping failed, err %s aq_err %s\n",
2489 i40e_stat_str(&vsi->back->hw, ret),
2490 i40e_aq_str(&vsi->back->hw,
2491 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492 }
2493}
2494
2495/**
2496 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2497 * @netdev: network interface to be adjusted
2498 * @features: netdev features to test if VLAN offload is enabled or not
2499 **/
2500static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2501{
2502 struct i40e_netdev_priv *np = netdev_priv(netdev);
2503 struct i40e_vsi *vsi = np->vsi;
2504
2505 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2506 i40e_vlan_stripping_enable(vsi);
2507 else
2508 i40e_vlan_stripping_disable(vsi);
2509}
2510
2511/**
2512 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2513 * @vsi: the vsi being configured
2514 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2515 **/
2516int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2517{
Jacob Keller278e7d02016-10-05 09:30:37 -07002518 struct i40e_mac_filter *f, *add_f, *del_f;
2519 struct hlist_node *h;
2520 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002521
Kiran Patil21659032015-09-30 14:09:03 -04002522 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002523 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002524
Jacob Keller278e7d02016-10-05 09:30:37 -07002525 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002526 if (f->state == I40E_FILTER_REMOVE)
2527 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002528 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 if (!add_f) {
2530 dev_info(&vsi->back->pdev->dev,
2531 "Could not add vlan filter %d for %pM\n",
2532 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002533 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002534 return -ENOMEM;
2535 }
2536 }
2537
Jacob Keller3c7cbd42016-11-08 13:05:17 -08002538 /* When we add a new VLAN filter, we need to make sure that all existing
2539 * filters which are marked as vid=-1 (I40E_VLAN_ANY) are converted to
2540 * vid=0. The simplest way is just search for all filters marked as
2541 * vid=-1 and replace them with vid=0. This converts all filters that
2542 * were marked to receive all traffic (tagged or untagged) into
2543 * filters to receive only untagged traffic, so that we don't receive
2544 * tagged traffic for VLANs which we have not configured.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002545 */
Greg Rose8d82a7c2014-01-13 16:13:04 -08002546 if (vid > 0 && !vsi->info.pvid) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002547 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002548 if (f->state == I40E_FILTER_REMOVE)
2549 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002550 del_f = i40e_find_filter(vsi, f->macaddr,
2551 I40E_VLAN_ANY);
2552 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002553 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002554 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002555 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002556 if (!add_f) {
2557 dev_info(&vsi->back->pdev->dev,
2558 "Could not add filter 0 for %pM\n",
2559 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002560 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002561 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002562 }
2563 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002564 }
2565
Jacob Keller278e7d02016-10-05 09:30:37 -07002566 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002567
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002568 /* schedule our worker thread which will take care of
2569 * applying the new filter changes
2570 */
2571 i40e_service_event_schedule(vsi->back);
2572 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002573}
2574
2575/**
2576 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2577 * @vsi: the vsi being configured
2578 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2579 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07002580void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002581{
Alan Brady84f5ca62016-10-05 09:30:39 -07002582 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002583 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002584 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002585
Kiran Patil21659032015-09-30 14:09:03 -04002586 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002587 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002588
Jacob Keller278e7d02016-10-05 09:30:37 -07002589 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002590 if (f->vlan == vid)
2591 __i40e_del_filter(vsi, f);
2592 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593
Jacob Keller278e7d02016-10-05 09:30:37 -07002594 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002595
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002596 /* schedule our worker thread which will take care of
2597 * applying the new filter changes
2598 */
2599 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002600}
2601
2602/**
2603 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2604 * @netdev: network interface to be adjusted
2605 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002606 *
2607 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002609#ifdef I40E_FCOE
2610int i40e_vlan_rx_add_vid(struct net_device *netdev,
2611 __always_unused __be16 proto, u16 vid)
2612#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002613static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2614 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002615#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002616{
2617 struct i40e_netdev_priv *np = netdev_priv(netdev);
2618 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002619 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002620
Jacob Keller6a1127852016-10-25 16:08:49 -07002621 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002622 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002623
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002624 /* If the network stack called us with vid = 0 then
2625 * it is asking to receive priority tagged packets with
2626 * vlan id 0. Our HW receives them by default when configured
2627 * to receive untagged packets so there is no need to add an
2628 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002629 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002630 if (vid)
2631 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002632
Jacob Keller6a1127852016-10-25 16:08:49 -07002633 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002634 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002636 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002637}
2638
2639/**
2640 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2641 * @netdev: network interface to be adjusted
2642 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002643 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002644 * net_device_ops implementation for removing 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_kill_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_kill_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;
2656
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002657 /* return code is ignored as there is nothing a user
2658 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002659 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660 */
2661 i40e_vsi_kill_vlan(vsi, vid);
2662
2663 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002665 return 0;
2666}
2667
2668/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002669 * i40e_macaddr_init - explicitly write the mac address filters
2670 *
2671 * @vsi: pointer to the vsi
2672 * @macaddr: the MAC address
2673 *
2674 * This is needed when the macaddr has been obtained by other
2675 * means than the default, e.g., from Open Firmware or IDPROM.
2676 * Returns 0 on success, negative on failure
2677 **/
2678static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2679{
2680 int ret;
2681 struct i40e_aqc_add_macvlan_element_data element;
2682
2683 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2684 I40E_AQC_WRITE_TYPE_LAA_WOL,
2685 macaddr, NULL);
2686 if (ret) {
2687 dev_info(&vsi->back->pdev->dev,
2688 "Addr change for VSI failed: %d\n", ret);
2689 return -EADDRNOTAVAIL;
2690 }
2691
2692 memset(&element, 0, sizeof(element));
2693 ether_addr_copy(element.mac_addr, macaddr);
2694 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2695 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2696 if (ret) {
2697 dev_info(&vsi->back->pdev->dev,
2698 "add filter failed err %s aq_err %s\n",
2699 i40e_stat_str(&vsi->back->hw, ret),
2700 i40e_aq_str(&vsi->back->hw,
2701 vsi->back->hw.aq.asq_last_status));
2702 }
2703 return ret;
2704}
2705
2706/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002707 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2708 * @vsi: the vsi being brought back up
2709 **/
2710static void i40e_restore_vlan(struct i40e_vsi *vsi)
2711{
2712 u16 vid;
2713
2714 if (!vsi->netdev)
2715 return;
2716
2717 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2718
2719 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2720 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2721 vid);
2722}
2723
2724/**
2725 * i40e_vsi_add_pvid - Add pvid for the VSI
2726 * @vsi: the vsi being adjusted
2727 * @vid: the vlan id to set as a PVID
2728 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002729int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002730{
2731 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002732 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002733
2734 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2735 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002736 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2737 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002738 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002739
2740 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002741 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002742 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2743 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002744 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002745 "add pvid failed, err %s aq_err %s\n",
2746 i40e_stat_str(&vsi->back->hw, ret),
2747 i40e_aq_str(&vsi->back->hw,
2748 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002749 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002750 }
2751
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002752 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002753}
2754
2755/**
2756 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2757 * @vsi: the vsi being adjusted
2758 *
2759 * Just use the vlan_rx_register() service to put it back to normal
2760 **/
2761void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2762{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002763 i40e_vlan_stripping_disable(vsi);
2764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002766}
2767
2768/**
2769 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2770 * @vsi: ptr to the VSI
2771 *
2772 * If this function returns with an error, then it's possible one or
2773 * more of the rings is populated (while the rest are not). It is the
2774 * callers duty to clean those orphaned rings.
2775 *
2776 * Return 0 on success, negative on failure
2777 **/
2778static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2779{
2780 int i, err = 0;
2781
2782 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002783 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002784
2785 return err;
2786}
2787
2788/**
2789 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2790 * @vsi: ptr to the VSI
2791 *
2792 * Free VSI's transmit software resources
2793 **/
2794static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2795{
2796 int i;
2797
Greg Rose8e9dca52013-12-18 13:45:53 +00002798 if (!vsi->tx_rings)
2799 return;
2800
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002801 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002802 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002803 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002804}
2805
2806/**
2807 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2808 * @vsi: ptr to the VSI
2809 *
2810 * If this function returns with an error, then it's possible one or
2811 * more of the rings is populated (while the rest are not). It is the
2812 * callers duty to clean those orphaned rings.
2813 *
2814 * Return 0 on success, negative on failure
2815 **/
2816static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2817{
2818 int i, err = 0;
2819
2820 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002821 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002822#ifdef I40E_FCOE
2823 i40e_fcoe_setup_ddp_resources(vsi);
2824#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002825 return err;
2826}
2827
2828/**
2829 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2830 * @vsi: ptr to the VSI
2831 *
2832 * Free all receive software resources
2833 **/
2834static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2835{
2836 int i;
2837
Greg Rose8e9dca52013-12-18 13:45:53 +00002838 if (!vsi->rx_rings)
2839 return;
2840
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002841 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002842 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002843 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002844#ifdef I40E_FCOE
2845 i40e_fcoe_free_ddp_resources(vsi);
2846#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002847}
2848
2849/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002850 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2851 * @ring: The Tx ring to configure
2852 *
2853 * This enables/disables XPS for a given Tx descriptor ring
2854 * based on the TCs enabled for the VSI that ring belongs to.
2855 **/
2856static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2857{
2858 struct i40e_vsi *vsi = ring->vsi;
2859 cpumask_var_t mask;
2860
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002861 if (!ring->q_vector || !ring->netdev)
2862 return;
2863
2864 /* Single TC mode enable XPS */
2865 if (vsi->tc_config.numtc <= 1) {
2866 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002867 netif_set_xps_queue(ring->netdev,
2868 &ring->q_vector->affinity_mask,
2869 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002870 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2871 /* Disable XPS to allow selection based on TC */
2872 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2873 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2874 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002875 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002876
2877 /* schedule our worker thread which will take care of
2878 * applying the new filter changes
2879 */
2880 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002881}
2882
2883/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002884 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2885 * @ring: The Tx ring to configure
2886 *
2887 * Configure the Tx descriptor ring in the HMC context.
2888 **/
2889static int i40e_configure_tx_ring(struct i40e_ring *ring)
2890{
2891 struct i40e_vsi *vsi = ring->vsi;
2892 u16 pf_q = vsi->base_queue + ring->queue_index;
2893 struct i40e_hw *hw = &vsi->back->hw;
2894 struct i40e_hmc_obj_txq tx_ctx;
2895 i40e_status err = 0;
2896 u32 qtx_ctl = 0;
2897
2898 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002899 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2901 ring->atr_count = 0;
2902 } else {
2903 ring->atr_sample_rate = 0;
2904 }
2905
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002906 /* configure XPS */
2907 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002908
2909 /* clear the context structure first */
2910 memset(&tx_ctx, 0, sizeof(tx_ctx));
2911
2912 tx_ctx.new_context = 1;
2913 tx_ctx.base = (ring->dma / 128);
2914 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002915 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2916 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002917#ifdef I40E_FCOE
2918 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2919#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002920 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002921 /* FDIR VSI tx ring can still use RS bit and writebacks */
2922 if (vsi->type != I40E_VSI_FDIR)
2923 tx_ctx.head_wb_ena = 1;
2924 tx_ctx.head_wb_addr = ring->dma +
2925 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002926
2927 /* As part of VSI creation/update, FW allocates certain
2928 * Tx arbitration queue sets for each TC enabled for
2929 * the VSI. The FW returns the handles to these queue
2930 * sets as part of the response buffer to Add VSI,
2931 * Update VSI, etc. AQ commands. It is expected that
2932 * these queue set handles be associated with the Tx
2933 * queues by the driver as part of the TX queue context
2934 * initialization. This has to be done regardless of
2935 * DCB as by default everything is mapped to TC0.
2936 */
2937 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2938 tx_ctx.rdylist_act = 0;
2939
2940 /* clear the context in the HMC */
2941 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2942 if (err) {
2943 dev_info(&vsi->back->pdev->dev,
2944 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2945 ring->queue_index, pf_q, err);
2946 return -ENOMEM;
2947 }
2948
2949 /* set the context in the HMC */
2950 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2951 if (err) {
2952 dev_info(&vsi->back->pdev->dev,
2953 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2954 ring->queue_index, pf_q, err);
2955 return -ENOMEM;
2956 }
2957
2958 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002959 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002960 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002961 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2962 I40E_QTX_CTL_VFVM_INDX_MASK;
2963 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002964 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002965 }
2966
Shannon Nelson13fd9772013-09-28 07:14:19 +00002967 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2968 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002969 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2970 i40e_flush(hw);
2971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002972 /* cache tail off for easier writes later */
2973 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2974
2975 return 0;
2976}
2977
2978/**
2979 * i40e_configure_rx_ring - Configure a receive ring context
2980 * @ring: The Rx ring to configure
2981 *
2982 * Configure the Rx descriptor ring in the HMC context.
2983 **/
2984static int i40e_configure_rx_ring(struct i40e_ring *ring)
2985{
2986 struct i40e_vsi *vsi = ring->vsi;
2987 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2988 u16 pf_q = vsi->base_queue + ring->queue_index;
2989 struct i40e_hw *hw = &vsi->back->hw;
2990 struct i40e_hmc_obj_rxq rx_ctx;
2991 i40e_status err = 0;
2992
2993 ring->state = 0;
2994
2995 /* clear the context structure first */
2996 memset(&rx_ctx, 0, sizeof(rx_ctx));
2997
2998 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002999
3000 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003001
3002 rx_ctx.base = (ring->dma / 128);
3003 rx_ctx.qlen = ring->count;
3004
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003005 /* use 32 byte descriptors */
3006 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003007
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003008 /* descriptor type is always zero
3009 * rx_ctx.dtype = 0;
3010 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003011 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003012
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003013 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003014 if (hw->revision_id == 0)
3015 rx_ctx.lrxqthresh = 0;
3016 else
3017 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003018 rx_ctx.crcstrip = 1;
3019 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003020 /* this controls whether VLAN is stripped from inner headers */
3021 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003022#ifdef I40E_FCOE
3023 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3024#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003025 /* set the prefena field to 1 because the manual says to */
3026 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027
3028 /* clear the context in the HMC */
3029 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3030 if (err) {
3031 dev_info(&vsi->back->pdev->dev,
3032 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3033 ring->queue_index, pf_q, err);
3034 return -ENOMEM;
3035 }
3036
3037 /* set the context in the HMC */
3038 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3039 if (err) {
3040 dev_info(&vsi->back->pdev->dev,
3041 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3042 ring->queue_index, pf_q, err);
3043 return -ENOMEM;
3044 }
3045
3046 /* cache tail for quicker writes, and clear the reg before use */
3047 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3048 writel(0, ring->tail);
3049
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003050 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003051
3052 return 0;
3053}
3054
3055/**
3056 * i40e_vsi_configure_tx - Configure the VSI for Tx
3057 * @vsi: VSI structure describing this set of rings and resources
3058 *
3059 * Configure the Tx VSI for operation.
3060 **/
3061static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3062{
3063 int err = 0;
3064 u16 i;
3065
Alexander Duyck9f65e152013-09-28 06:00:58 +00003066 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3067 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003068
3069 return err;
3070}
3071
3072/**
3073 * i40e_vsi_configure_rx - Configure the VSI for Rx
3074 * @vsi: the VSI being configured
3075 *
3076 * Configure the Rx VSI for operation.
3077 **/
3078static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3079{
3080 int err = 0;
3081 u16 i;
3082
3083 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3084 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3085 + ETH_FCS_LEN + VLAN_HLEN;
3086 else
3087 vsi->max_frame = I40E_RXBUFFER_2048;
3088
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003089 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003090
Vasu Dev38e00432014-08-01 13:27:03 -07003091#ifdef I40E_FCOE
3092 /* setup rx buffer for FCoE */
3093 if ((vsi->type == I40E_VSI_FCOE) &&
3094 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003095 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3096 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003097 }
3098
3099#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003100 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003101 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003102 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103
3104 /* set up individual rings */
3105 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003106 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003107
3108 return err;
3109}
3110
3111/**
3112 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3113 * @vsi: ptr to the VSI
3114 **/
3115static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3116{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003117 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003118 u16 qoffset, qcount;
3119 int i, n;
3120
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003121 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3122 /* Reset the TC information */
3123 for (i = 0; i < vsi->num_queue_pairs; i++) {
3124 rx_ring = vsi->rx_rings[i];
3125 tx_ring = vsi->tx_rings[i];
3126 rx_ring->dcb_tc = 0;
3127 tx_ring->dcb_tc = 0;
3128 }
3129 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130
3131 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003132 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003133 continue;
3134
3135 qoffset = vsi->tc_config.tc_info[n].qoffset;
3136 qcount = vsi->tc_config.tc_info[n].qcount;
3137 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003138 rx_ring = vsi->rx_rings[i];
3139 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003140 rx_ring->dcb_tc = n;
3141 tx_ring->dcb_tc = n;
3142 }
3143 }
3144}
3145
3146/**
3147 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3148 * @vsi: ptr to the VSI
3149 **/
3150static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3151{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003152 struct i40e_pf *pf = vsi->back;
3153 int err;
3154
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003155 if (vsi->netdev)
3156 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003157
3158 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3159 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3160 if (err) {
3161 dev_warn(&pf->pdev->dev,
3162 "could not set up macaddr; err %d\n", err);
3163 }
3164 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165}
3166
3167/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003168 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3169 * @vsi: Pointer to the targeted VSI
3170 *
3171 * This function replays the hlist on the hw where all the SB Flow Director
3172 * filters were saved.
3173 **/
3174static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3175{
3176 struct i40e_fdir_filter *filter;
3177 struct i40e_pf *pf = vsi->back;
3178 struct hlist_node *node;
3179
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003180 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3181 return;
3182
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003183 hlist_for_each_entry_safe(filter, node,
3184 &pf->fdir_filter_list, fdir_node) {
3185 i40e_add_del_fdir(vsi, filter, true);
3186 }
3187}
3188
3189/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003190 * i40e_vsi_configure - Set up the VSI for action
3191 * @vsi: the VSI being configured
3192 **/
3193static int i40e_vsi_configure(struct i40e_vsi *vsi)
3194{
3195 int err;
3196
3197 i40e_set_vsi_rx_mode(vsi);
3198 i40e_restore_vlan(vsi);
3199 i40e_vsi_config_dcb_rings(vsi);
3200 err = i40e_vsi_configure_tx(vsi);
3201 if (!err)
3202 err = i40e_vsi_configure_rx(vsi);
3203
3204 return err;
3205}
3206
3207/**
3208 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3209 * @vsi: the VSI being configured
3210 **/
3211static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3212{
3213 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003214 struct i40e_hw *hw = &pf->hw;
3215 u16 vector;
3216 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217 u32 qp;
3218
3219 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3220 * and PFINT_LNKLSTn registers, e.g.:
3221 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3222 */
3223 qp = vsi->base_queue;
3224 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003225 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003226 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3227
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003228 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003229 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003230 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3231 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3232 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003233 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003234 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3235 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3236 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003237 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3238 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003239
3240 /* Linked list for the queuepairs assigned to this vector */
3241 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3242 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003243 u32 val;
3244
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003245 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3246 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3247 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3248 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3249 (I40E_QUEUE_TYPE_TX
3250 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3251
3252 wr32(hw, I40E_QINT_RQCTL(qp), val);
3253
3254 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3255 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3256 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3257 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3258 (I40E_QUEUE_TYPE_RX
3259 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3260
3261 /* Terminate the linked list */
3262 if (q == (q_vector->num_ringpairs - 1))
3263 val |= (I40E_QUEUE_END_OF_LIST
3264 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3265
3266 wr32(hw, I40E_QINT_TQCTL(qp), val);
3267 qp++;
3268 }
3269 }
3270
3271 i40e_flush(hw);
3272}
3273
3274/**
3275 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3276 * @hw: ptr to the hardware info
3277 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003278static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003279{
Jacob Kellerab437b52014-12-14 01:55:08 +00003280 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003281 u32 val;
3282
3283 /* clear things first */
3284 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3285 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3286
3287 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3288 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3289 I40E_PFINT_ICR0_ENA_GRST_MASK |
3290 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3291 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3293 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3294 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3295
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003296 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3297 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3298
Jacob Kellerab437b52014-12-14 01:55:08 +00003299 if (pf->flags & I40E_FLAG_PTP)
3300 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3301
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003302 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3303
3304 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003305 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3306 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003307
3308 /* OTHER_ITR_IDX = 0 */
3309 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3310}
3311
3312/**
3313 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3314 * @vsi: the VSI being configured
3315 **/
3316static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3317{
Alexander Duyck493fb302013-09-28 07:01:44 +00003318 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003319 struct i40e_pf *pf = vsi->back;
3320 struct i40e_hw *hw = &pf->hw;
3321 u32 val;
3322
3323 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003324 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003325 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003326 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3327 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003328 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3330 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3331
Jacob Kellerab437b52014-12-14 01:55:08 +00003332 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003333
3334 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3335 wr32(hw, I40E_PFINT_LNKLST0, 0);
3336
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003337 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3339 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3340 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3341
3342 wr32(hw, I40E_QINT_RQCTL(0), val);
3343
3344 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3345 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3346 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3347
3348 wr32(hw, I40E_QINT_TQCTL(0), val);
3349 i40e_flush(hw);
3350}
3351
3352/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003353 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3354 * @pf: board private structure
3355 **/
3356void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3357{
3358 struct i40e_hw *hw = &pf->hw;
3359
3360 wr32(hw, I40E_PFINT_DYN_CTL0,
3361 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3362 i40e_flush(hw);
3363}
3364
3365/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003366 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3367 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003368 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003369 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003370void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371{
3372 struct i40e_hw *hw = &pf->hw;
3373 u32 val;
3374
3375 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003376 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003377 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3378
3379 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3380 i40e_flush(hw);
3381}
3382
3383/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003384 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3385 * @irq: interrupt number
3386 * @data: pointer to a q_vector
3387 **/
3388static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3389{
3390 struct i40e_q_vector *q_vector = data;
3391
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003392 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003393 return IRQ_HANDLED;
3394
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003395 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003396
3397 return IRQ_HANDLED;
3398}
3399
3400/**
Alan Brady96db7762016-09-14 16:24:38 -07003401 * i40e_irq_affinity_notify - Callback for affinity changes
3402 * @notify: context as to what irq was changed
3403 * @mask: the new affinity mask
3404 *
3405 * This is a callback function used by the irq_set_affinity_notifier function
3406 * so that we may register to receive changes to the irq affinity masks.
3407 **/
3408static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3409 const cpumask_t *mask)
3410{
3411 struct i40e_q_vector *q_vector =
3412 container_of(notify, struct i40e_q_vector, affinity_notify);
3413
3414 q_vector->affinity_mask = *mask;
3415}
3416
3417/**
3418 * i40e_irq_affinity_release - Callback for affinity notifier release
3419 * @ref: internal core kernel usage
3420 *
3421 * This is a callback function used by the irq_set_affinity_notifier function
3422 * to inform the current notification subscriber that they will no longer
3423 * receive notifications.
3424 **/
3425static void i40e_irq_affinity_release(struct kref *ref) {}
3426
3427/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003428 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3429 * @vsi: the VSI being configured
3430 * @basename: name for the vector
3431 *
3432 * Allocates MSI-X vectors and requests interrupts from the kernel.
3433 **/
3434static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3435{
3436 int q_vectors = vsi->num_q_vectors;
3437 struct i40e_pf *pf = vsi->back;
3438 int base = vsi->base_vector;
3439 int rx_int_idx = 0;
3440 int tx_int_idx = 0;
3441 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003442 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003443
3444 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003445 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446
Alan Brady96db7762016-09-14 16:24:38 -07003447 irq_num = pf->msix_entries[base + vector].vector;
3448
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003449 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3451 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3452 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003453 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003454 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3455 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003456 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003457 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3458 "%s-%s-%d", basename, "tx", tx_int_idx++);
3459 } else {
3460 /* skip this unused q_vector */
3461 continue;
3462 }
Alan Brady96db7762016-09-14 16:24:38 -07003463 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003464 vsi->irq_handler,
3465 0,
3466 q_vector->name,
3467 q_vector);
3468 if (err) {
3469 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003470 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471 goto free_queue_irqs;
3472 }
Alan Brady96db7762016-09-14 16:24:38 -07003473
3474 /* register for affinity change notifications */
3475 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3476 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3477 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003478 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003479 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003480 }
3481
Shannon Nelson63741842014-04-23 04:50:16 +00003482 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003483 return 0;
3484
3485free_queue_irqs:
3486 while (vector) {
3487 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003488 irq_num = pf->msix_entries[base + vector].vector;
3489 irq_set_affinity_notifier(irq_num, NULL);
3490 irq_set_affinity_hint(irq_num, NULL);
3491 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003492 }
3493 return err;
3494}
3495
3496/**
3497 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3498 * @vsi: the VSI being un-configured
3499 **/
3500static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3501{
3502 struct i40e_pf *pf = vsi->back;
3503 struct i40e_hw *hw = &pf->hw;
3504 int base = vsi->base_vector;
3505 int i;
3506
3507 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003508 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3509 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003510 }
3511
3512 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3513 for (i = vsi->base_vector;
3514 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3515 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3516
3517 i40e_flush(hw);
3518 for (i = 0; i < vsi->num_q_vectors; i++)
3519 synchronize_irq(pf->msix_entries[i + base].vector);
3520 } else {
3521 /* Legacy and MSI mode - this stops all interrupt handling */
3522 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3523 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3524 i40e_flush(hw);
3525 synchronize_irq(pf->pdev->irq);
3526 }
3527}
3528
3529/**
3530 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3531 * @vsi: the VSI being configured
3532 **/
3533static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3534{
3535 struct i40e_pf *pf = vsi->back;
3536 int i;
3537
3538 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003539 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540 i40e_irq_dynamic_enable(vsi, i);
3541 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003542 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003543 }
3544
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003545 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546 return 0;
3547}
3548
3549/**
3550 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3551 * @pf: board private structure
3552 **/
3553static void i40e_stop_misc_vector(struct i40e_pf *pf)
3554{
3555 /* Disable ICR 0 */
3556 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3557 i40e_flush(&pf->hw);
3558}
3559
3560/**
3561 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3562 * @irq: interrupt number
3563 * @data: pointer to a q_vector
3564 *
3565 * This is the handler used for all MSI/Legacy interrupts, and deals
3566 * with both queue and non-queue interrupts. This is also used in
3567 * MSIX mode to handle the non-queue interrupts.
3568 **/
3569static irqreturn_t i40e_intr(int irq, void *data)
3570{
3571 struct i40e_pf *pf = (struct i40e_pf *)data;
3572 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003573 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003574 u32 icr0, icr0_remaining;
3575 u32 val, ena_mask;
3576
3577 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003578 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003579
Shannon Nelson116a57d2013-09-28 07:13:59 +00003580 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3581 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003582 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003583
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003584 /* if interrupt but no bits showing, must be SWINT */
3585 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3586 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3587 pf->sw_int_count++;
3588
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003589 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3590 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3591 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3592 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003593 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003594 }
3595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003596 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3597 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003598 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3599 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003600
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003601 /* We do not have a way to disarm Queue causes while leaving
3602 * interrupt enabled for all other causes, ideally
3603 * interrupt should be disabled while we are in NAPI but
3604 * this is not a performance path and napi_schedule()
3605 * can deal with rescheduling.
3606 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003607 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003608 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003609 }
3610
3611 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3612 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3613 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003614 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003615 }
3616
3617 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3618 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3619 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3620 }
3621
3622 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3623 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3624 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3625 }
3626
3627 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3628 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3629 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3630 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3631 val = rd32(hw, I40E_GLGEN_RSTAT);
3632 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3633 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003634 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003635 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003636 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003637 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003638 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003639 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003640 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003641 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003642 }
3643
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003644 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3645 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3646 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003647 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3648 rd32(hw, I40E_PFHMC_ERRORINFO),
3649 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003650 }
3651
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003652 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3653 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3654
3655 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003656 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003657 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003658 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003659 }
3660
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003661 /* If a critical error is pending we have no choice but to reset the
3662 * device.
3663 * Report and mask out any remaining unexpected interrupts.
3664 */
3665 icr0_remaining = icr0 & ena_mask;
3666 if (icr0_remaining) {
3667 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3668 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003669 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003671 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003672 dev_info(&pf->pdev->dev, "device will be reset\n");
3673 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3674 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 }
3676 ena_mask &= ~icr0_remaining;
3677 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003678 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003679
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003680enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003681 /* re-enable interrupt causes */
3682 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683 if (!test_bit(__I40E_DOWN, &pf->state)) {
3684 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003685 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686 }
3687
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003688 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003689}
3690
3691/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003692 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3693 * @tx_ring: tx ring to clean
3694 * @budget: how many cleans we're allowed
3695 *
3696 * Returns true if there's any budget left (e.g. the clean is finished)
3697 **/
3698static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3699{
3700 struct i40e_vsi *vsi = tx_ring->vsi;
3701 u16 i = tx_ring->next_to_clean;
3702 struct i40e_tx_buffer *tx_buf;
3703 struct i40e_tx_desc *tx_desc;
3704
3705 tx_buf = &tx_ring->tx_bi[i];
3706 tx_desc = I40E_TX_DESC(tx_ring, i);
3707 i -= tx_ring->count;
3708
3709 do {
3710 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3711
3712 /* if next_to_watch is not set then there is no work pending */
3713 if (!eop_desc)
3714 break;
3715
3716 /* prevent any other reads prior to eop_desc */
3717 read_barrier_depends();
3718
3719 /* if the descriptor isn't done, no work yet to do */
3720 if (!(eop_desc->cmd_type_offset_bsz &
3721 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3722 break;
3723
3724 /* clear next_to_watch to prevent false hangs */
3725 tx_buf->next_to_watch = NULL;
3726
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003727 tx_desc->buffer_addr = 0;
3728 tx_desc->cmd_type_offset_bsz = 0;
3729 /* move past filter desc */
3730 tx_buf++;
3731 tx_desc++;
3732 i++;
3733 if (unlikely(!i)) {
3734 i -= tx_ring->count;
3735 tx_buf = tx_ring->tx_bi;
3736 tx_desc = I40E_TX_DESC(tx_ring, 0);
3737 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003738 /* unmap skb header data */
3739 dma_unmap_single(tx_ring->dev,
3740 dma_unmap_addr(tx_buf, dma),
3741 dma_unmap_len(tx_buf, len),
3742 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003743 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3744 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003745
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003746 tx_buf->raw_buf = NULL;
3747 tx_buf->tx_flags = 0;
3748 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003749 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003750 tx_desc->buffer_addr = 0;
3751 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003752
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003753 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003754 tx_buf++;
3755 tx_desc++;
3756 i++;
3757 if (unlikely(!i)) {
3758 i -= tx_ring->count;
3759 tx_buf = tx_ring->tx_bi;
3760 tx_desc = I40E_TX_DESC(tx_ring, 0);
3761 }
3762
3763 /* update budget accounting */
3764 budget--;
3765 } while (likely(budget));
3766
3767 i += tx_ring->count;
3768 tx_ring->next_to_clean = i;
3769
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003770 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003771 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003772
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003773 return budget > 0;
3774}
3775
3776/**
3777 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3778 * @irq: interrupt number
3779 * @data: pointer to a q_vector
3780 **/
3781static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3782{
3783 struct i40e_q_vector *q_vector = data;
3784 struct i40e_vsi *vsi;
3785
3786 if (!q_vector->tx.ring)
3787 return IRQ_HANDLED;
3788
3789 vsi = q_vector->tx.ring->vsi;
3790 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3791
3792 return IRQ_HANDLED;
3793}
3794
3795/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003796 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003797 * @vsi: the VSI being configured
3798 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003799 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003801static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003802{
Alexander Duyck493fb302013-09-28 07:01:44 +00003803 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003804 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3805 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003806
3807 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003808 tx_ring->next = q_vector->tx.ring;
3809 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003810 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003811
3812 rx_ring->q_vector = q_vector;
3813 rx_ring->next = q_vector->rx.ring;
3814 q_vector->rx.ring = rx_ring;
3815 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003816}
3817
3818/**
3819 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3820 * @vsi: the VSI being configured
3821 *
3822 * This function maps descriptor rings to the queue-specific vectors
3823 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3824 * one vector per queue pair, but on a constrained vector budget, we
3825 * group the queue pairs as "efficiently" as possible.
3826 **/
3827static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3828{
3829 int qp_remaining = vsi->num_queue_pairs;
3830 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003831 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003832 int v_start = 0;
3833 int qp_idx = 0;
3834
3835 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3836 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003837 * It is also important to go through all the vectors available to be
3838 * sure that if we don't use all the vectors, that the remaining vectors
3839 * are cleared. This is especially important when decreasing the
3840 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003841 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003842 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003843 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3844
3845 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3846
3847 q_vector->num_ringpairs = num_ringpairs;
3848
3849 q_vector->rx.count = 0;
3850 q_vector->tx.count = 0;
3851 q_vector->rx.ring = NULL;
3852 q_vector->tx.ring = NULL;
3853
3854 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003855 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003856 qp_idx++;
3857 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003858 }
3859 }
3860}
3861
3862/**
3863 * i40e_vsi_request_irq - Request IRQ from the OS
3864 * @vsi: the VSI being configured
3865 * @basename: name for the vector
3866 **/
3867static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3868{
3869 struct i40e_pf *pf = vsi->back;
3870 int err;
3871
3872 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3873 err = i40e_vsi_request_irq_msix(vsi, basename);
3874 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3875 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003876 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003877 else
3878 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003879 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003880
3881 if (err)
3882 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3883
3884 return err;
3885}
3886
3887#ifdef CONFIG_NET_POLL_CONTROLLER
3888/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003889 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003890 * @netdev: network interface device structure
3891 *
3892 * This is used by netconsole to send skbs without having to re-enable
3893 * interrupts. It's not called while the normal interrupt routine is executing.
3894 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003895#ifdef I40E_FCOE
3896void i40e_netpoll(struct net_device *netdev)
3897#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003898static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003899#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003900{
3901 struct i40e_netdev_priv *np = netdev_priv(netdev);
3902 struct i40e_vsi *vsi = np->vsi;
3903 struct i40e_pf *pf = vsi->back;
3904 int i;
3905
3906 /* if interface is down do nothing */
3907 if (test_bit(__I40E_DOWN, &vsi->state))
3908 return;
3909
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003910 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3911 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003912 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913 } else {
3914 i40e_intr(pf->pdev->irq, netdev);
3915 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003916}
3917#endif
3918
3919/**
Neerav Parikh23527302014-06-03 23:50:15 +00003920 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3921 * @pf: the PF being configured
3922 * @pf_q: the PF queue
3923 * @enable: enable or disable state of the queue
3924 *
3925 * This routine will wait for the given Tx queue of the PF to reach the
3926 * enabled or disabled state.
3927 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3928 * multiple retries; else will return 0 in case of success.
3929 **/
3930static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3931{
3932 int i;
3933 u32 tx_reg;
3934
3935 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3936 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3937 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3938 break;
3939
Neerav Parikhf98a2002014-09-13 07:40:44 +00003940 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003941 }
3942 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3943 return -ETIMEDOUT;
3944
3945 return 0;
3946}
3947
3948/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003949 * i40e_vsi_control_tx - Start or stop a VSI's rings
3950 * @vsi: the VSI being configured
3951 * @enable: start or stop the rings
3952 **/
3953static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3954{
3955 struct i40e_pf *pf = vsi->back;
3956 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003957 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003958 u32 tx_reg;
3959
3960 pf_q = vsi->base_queue;
3961 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003962
3963 /* warn the TX unit of coming changes */
3964 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3965 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003966 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003967
Mitch Williams6c5ef622014-02-20 19:29:16 -08003968 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003969 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003970 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3971 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3972 break;
3973 usleep_range(1000, 2000);
3974 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003975 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003976 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003977 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003978
3979 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003980 if (enable) {
3981 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003982 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003983 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003984 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003985 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003986
3987 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003988 /* No waiting for the Tx queue to disable */
3989 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3990 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003991
3992 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003993 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3994 if (ret) {
3995 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003996 "VSI seid %d Tx ring %d %sable timeout\n",
3997 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003998 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003999 }
4000 }
4001
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004002 if (hw->revision_id == 0)
4003 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00004004 return ret;
4005}
4006
4007/**
4008 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4009 * @pf: the PF being configured
4010 * @pf_q: the PF queue
4011 * @enable: enable or disable state of the queue
4012 *
4013 * This routine will wait for the given Rx queue of the PF to reach the
4014 * enabled or disabled state.
4015 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4016 * multiple retries; else will return 0 in case of success.
4017 **/
4018static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4019{
4020 int i;
4021 u32 rx_reg;
4022
4023 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4024 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4025 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4026 break;
4027
Neerav Parikhf98a2002014-09-13 07:40:44 +00004028 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004029 }
4030 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4031 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004032
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004033 return 0;
4034}
4035
4036/**
4037 * i40e_vsi_control_rx - Start or stop a VSI's rings
4038 * @vsi: the VSI being configured
4039 * @enable: start or stop the rings
4040 **/
4041static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4042{
4043 struct i40e_pf *pf = vsi->back;
4044 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004045 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004046 u32 rx_reg;
4047
4048 pf_q = vsi->base_queue;
4049 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004050 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004051 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004052 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4053 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4054 break;
4055 usleep_range(1000, 2000);
4056 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004057
Catherine Sullivan7c122002014-03-14 07:32:29 +00004058 /* Skip if the queue is already in the requested state */
4059 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4060 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004061
4062 /* turn on/off the queue */
4063 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004064 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004065 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004066 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004067 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004068 /* No waiting for the Tx queue to disable */
4069 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4070 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004071
4072 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004073 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4074 if (ret) {
4075 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004076 "VSI seid %d Rx ring %d %sable timeout\n",
4077 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004078 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004079 }
4080 }
4081
Neerav Parikh23527302014-06-03 23:50:15 +00004082 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004083}
4084
4085/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004086 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004087 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004088 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004089int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004090{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004091 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004092
4093 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004094 ret = i40e_vsi_control_rx(vsi, true);
4095 if (ret)
4096 return ret;
4097 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004098
4099 return ret;
4100}
4101
4102/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004103 * i40e_vsi_stop_rings - Stop a VSI's rings
4104 * @vsi: the VSI being configured
4105 **/
4106void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4107{
4108 /* do rx first for enable and last for disable
4109 * Ignore return value, we need to shutdown whatever we can
4110 */
4111 i40e_vsi_control_tx(vsi, false);
4112 i40e_vsi_control_rx(vsi, false);
4113}
4114
4115/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004116 * i40e_vsi_free_irq - Free the irq association with the OS
4117 * @vsi: the VSI being configured
4118 **/
4119static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4120{
4121 struct i40e_pf *pf = vsi->back;
4122 struct i40e_hw *hw = &pf->hw;
4123 int base = vsi->base_vector;
4124 u32 val, qp;
4125 int i;
4126
4127 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4128 if (!vsi->q_vectors)
4129 return;
4130
Shannon Nelson63741842014-04-23 04:50:16 +00004131 if (!vsi->irqs_ready)
4132 return;
4133
4134 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004136 int irq_num;
4137 u16 vector;
4138
4139 vector = i + base;
4140 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004141
4142 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004143 if (!vsi->q_vectors[i] ||
4144 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004145 continue;
4146
Alan Brady96db7762016-09-14 16:24:38 -07004147 /* clear the affinity notifier in the IRQ descriptor */
4148 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004149 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004150 irq_set_affinity_hint(irq_num, NULL);
4151 synchronize_irq(irq_num);
4152 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004153
4154 /* Tear down the interrupt queue link list
4155 *
4156 * We know that they come in pairs and always
4157 * the Rx first, then the Tx. To clear the
4158 * link list, stick the EOL value into the
4159 * next_q field of the registers.
4160 */
4161 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4162 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4163 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4164 val |= I40E_QUEUE_END_OF_LIST
4165 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4166 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4167
4168 while (qp != I40E_QUEUE_END_OF_LIST) {
4169 u32 next;
4170
4171 val = rd32(hw, I40E_QINT_RQCTL(qp));
4172
4173 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4174 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4175 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4176 I40E_QINT_RQCTL_INTEVENT_MASK);
4177
4178 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4179 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4180
4181 wr32(hw, I40E_QINT_RQCTL(qp), val);
4182
4183 val = rd32(hw, I40E_QINT_TQCTL(qp));
4184
4185 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4186 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4187
4188 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4189 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4190 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4191 I40E_QINT_TQCTL_INTEVENT_MASK);
4192
4193 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4194 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4195
4196 wr32(hw, I40E_QINT_TQCTL(qp), val);
4197 qp = next;
4198 }
4199 }
4200 } else {
4201 free_irq(pf->pdev->irq, pf);
4202
4203 val = rd32(hw, I40E_PFINT_LNKLST0);
4204 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4205 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4206 val |= I40E_QUEUE_END_OF_LIST
4207 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4208 wr32(hw, I40E_PFINT_LNKLST0, val);
4209
4210 val = rd32(hw, I40E_QINT_RQCTL(qp));
4211 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4212 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4213 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4214 I40E_QINT_RQCTL_INTEVENT_MASK);
4215
4216 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4217 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4218
4219 wr32(hw, I40E_QINT_RQCTL(qp), val);
4220
4221 val = rd32(hw, I40E_QINT_TQCTL(qp));
4222
4223 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4224 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4225 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4226 I40E_QINT_TQCTL_INTEVENT_MASK);
4227
4228 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4229 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4230
4231 wr32(hw, I40E_QINT_TQCTL(qp), val);
4232 }
4233}
4234
4235/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004236 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4237 * @vsi: the VSI being configured
4238 * @v_idx: Index of vector to be freed
4239 *
4240 * This function frees the memory allocated to the q_vector. In addition if
4241 * NAPI is enabled it will delete any references to the NAPI struct prior
4242 * to freeing the q_vector.
4243 **/
4244static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4245{
4246 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004247 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004248
4249 if (!q_vector)
4250 return;
4251
4252 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004253 i40e_for_each_ring(ring, q_vector->tx)
4254 ring->q_vector = NULL;
4255
4256 i40e_for_each_ring(ring, q_vector->rx)
4257 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004258
4259 /* only VSI w/ an associated netdev is set up w/ NAPI */
4260 if (vsi->netdev)
4261 netif_napi_del(&q_vector->napi);
4262
4263 vsi->q_vectors[v_idx] = NULL;
4264
4265 kfree_rcu(q_vector, rcu);
4266}
4267
4268/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004269 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4270 * @vsi: the VSI being un-configured
4271 *
4272 * This frees the memory allocated to the q_vectors and
4273 * deletes references to the NAPI struct.
4274 **/
4275static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4276{
4277 int v_idx;
4278
Alexander Duyck493fb302013-09-28 07:01:44 +00004279 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4280 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004281}
4282
4283/**
4284 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4285 * @pf: board private structure
4286 **/
4287static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4288{
4289 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4290 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4291 pci_disable_msix(pf->pdev);
4292 kfree(pf->msix_entries);
4293 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004294 kfree(pf->irq_pile);
4295 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004296 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4297 pci_disable_msi(pf->pdev);
4298 }
4299 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4300}
4301
4302/**
4303 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4304 * @pf: board private structure
4305 *
4306 * We go through and clear interrupt specific resources and reset the structure
4307 * to pre-load conditions
4308 **/
4309static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4310{
4311 int i;
4312
Shannon Nelsone1477582015-02-21 06:44:33 +00004313 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004314 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004315 synchronize_irq(pf->msix_entries[0].vector);
4316 free_irq(pf->msix_entries[0].vector, pf);
4317 }
4318
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004319 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4320 I40E_IWARP_IRQ_PILE_ID);
4321
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004322 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004323 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004324 if (pf->vsi[i])
4325 i40e_vsi_free_q_vectors(pf->vsi[i]);
4326 i40e_reset_interrupt_capability(pf);
4327}
4328
4329/**
4330 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4331 * @vsi: the VSI being configured
4332 **/
4333static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4334{
4335 int q_idx;
4336
4337 if (!vsi->netdev)
4338 return;
4339
4340 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004341 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004342}
4343
4344/**
4345 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4346 * @vsi: the VSI being configured
4347 **/
4348static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4349{
4350 int q_idx;
4351
4352 if (!vsi->netdev)
4353 return;
4354
4355 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004356 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004357}
4358
4359/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004360 * i40e_vsi_close - Shut down a VSI
4361 * @vsi: the vsi to be quelled
4362 **/
4363static void i40e_vsi_close(struct i40e_vsi *vsi)
4364{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004365 bool reset = false;
4366
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004367 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4368 i40e_down(vsi);
4369 i40e_vsi_free_irq(vsi);
4370 i40e_vsi_free_tx_resources(vsi);
4371 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004372 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004373 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4374 reset = true;
4375 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004376}
4377
4378/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004379 * i40e_quiesce_vsi - Pause a given VSI
4380 * @vsi: the VSI being paused
4381 **/
4382static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4383{
4384 if (test_bit(__I40E_DOWN, &vsi->state))
4385 return;
4386
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004387 /* No need to disable FCoE VSI when Tx suspended */
4388 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4389 vsi->type == I40E_VSI_FCOE) {
4390 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004391 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004392 return;
4393 }
4394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004395 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004396 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004397 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004398 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004399 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004400}
4401
4402/**
4403 * i40e_unquiesce_vsi - Resume a given VSI
4404 * @vsi: the VSI being resumed
4405 **/
4406static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4407{
4408 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4409 return;
4410
4411 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4412 if (vsi->netdev && netif_running(vsi->netdev))
4413 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4414 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004415 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004416}
4417
4418/**
4419 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4420 * @pf: the PF
4421 **/
4422static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4423{
4424 int v;
4425
Mitch Williams505682c2014-05-20 08:01:37 +00004426 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004427 if (pf->vsi[v])
4428 i40e_quiesce_vsi(pf->vsi[v]);
4429 }
4430}
4431
4432/**
4433 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4434 * @pf: the PF
4435 **/
4436static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4437{
4438 int v;
4439
Mitch Williams505682c2014-05-20 08:01:37 +00004440 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004441 if (pf->vsi[v])
4442 i40e_unquiesce_vsi(pf->vsi[v]);
4443 }
4444}
4445
Neerav Parikh69129dc2014-11-12 00:18:46 +00004446#ifdef CONFIG_I40E_DCB
4447/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004448 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004449 * @vsi: the VSI being configured
4450 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004451 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004452 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004453static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004454{
4455 struct i40e_pf *pf = vsi->back;
4456 int i, pf_q, ret;
4457
4458 pf_q = vsi->base_queue;
4459 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4460 /* Check and wait for the disable status of the queue */
4461 ret = i40e_pf_txq_wait(pf, pf_q, false);
4462 if (ret) {
4463 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004464 "VSI seid %d Tx ring %d disable timeout\n",
4465 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004466 return ret;
4467 }
4468 }
4469
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004470 pf_q = vsi->base_queue;
4471 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4472 /* Check and wait for the disable status of the queue */
4473 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4474 if (ret) {
4475 dev_info(&pf->pdev->dev,
4476 "VSI seid %d Rx ring %d disable timeout\n",
4477 vsi->seid, pf_q);
4478 return ret;
4479 }
4480 }
4481
Neerav Parikh69129dc2014-11-12 00:18:46 +00004482 return 0;
4483}
4484
4485/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004486 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004487 * @pf: the PF
4488 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004489 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004490 * VSIs that are managed by this PF.
4491 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004492static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004493{
4494 int v, ret = 0;
4495
4496 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004497 /* No need to wait for FCoE VSI queues */
4498 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004499 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004500 if (ret)
4501 break;
4502 }
4503 }
4504
4505 return ret;
4506}
4507
4508#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004509
4510/**
4511 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4512 * @q_idx: TX queue number
4513 * @vsi: Pointer to VSI struct
4514 *
4515 * This function checks specified queue for given VSI. Detects hung condition.
4516 * Sets hung bit since it is two step process. Before next run of service task
4517 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4518 * hung condition remain unchanged and during subsequent run, this function
4519 * issues SW interrupt to recover from hung condition.
4520 **/
4521static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4522{
4523 struct i40e_ring *tx_ring = NULL;
4524 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004525 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004526 int i;
4527
4528 pf = vsi->back;
4529
4530 /* now that we have an index, find the tx_ring struct */
4531 for (i = 0; i < vsi->num_queue_pairs; i++) {
4532 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4533 if (q_idx == vsi->tx_rings[i]->queue_index) {
4534 tx_ring = vsi->tx_rings[i];
4535 break;
4536 }
4537 }
4538 }
4539
4540 if (!tx_ring)
4541 return;
4542
4543 /* Read interrupt register */
4544 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4545 val = rd32(&pf->hw,
4546 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4547 tx_ring->vsi->base_vector - 1));
4548 else
4549 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4550
4551 head = i40e_get_head(tx_ring);
4552
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004553 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004554
Kiran Patil9c6c1252015-11-06 15:26:02 -08004555 /* HW is done executing descriptors, updated HEAD write back,
4556 * but SW hasn't processed those descriptors. If interrupt is
4557 * not generated from this point ON, it could result into
4558 * dev_watchdog detecting timeout on those netdev_queue,
4559 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004560 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004561 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004562 /* NAPI Poll didn't run and clear since it was set */
4563 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4564 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004565 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",
4566 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004567 tx_ring->next_to_clean, head,
4568 tx_ring->next_to_use,
4569 readl(tx_ring->tail));
4570 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4571 vsi->seid, q_idx, val);
4572 i40e_force_wb(vsi, tx_ring->q_vector);
4573 } else {
4574 /* First Chance - detected possible hung */
4575 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4576 &tx_ring->q_vector->hung_detected);
4577 }
4578 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004579
4580 /* This is the case where we have interrupts missing,
4581 * so the tx_pending in HW will most likely be 0, but we
4582 * will have tx_pending in SW since the WB happened but the
4583 * interrupt got lost.
4584 */
4585 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4586 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4587 if (napi_reschedule(&tx_ring->q_vector->napi))
4588 tx_ring->tx_stats.tx_lost_interrupt++;
4589 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004590}
4591
4592/**
4593 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4594 * @pf: pointer to PF struct
4595 *
4596 * LAN VSI has netdev and netdev has TX queues. This function is to check
4597 * each of those TX queues if they are hung, trigger recovery by issuing
4598 * SW interrupt.
4599 **/
4600static void i40e_detect_recover_hung(struct i40e_pf *pf)
4601{
4602 struct net_device *netdev;
4603 struct i40e_vsi *vsi;
4604 int i;
4605
4606 /* Only for LAN VSI */
4607 vsi = pf->vsi[pf->lan_vsi];
4608
4609 if (!vsi)
4610 return;
4611
4612 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4613 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4614 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4615 return;
4616
4617 /* Make sure type is MAIN VSI */
4618 if (vsi->type != I40E_VSI_MAIN)
4619 return;
4620
4621 netdev = vsi->netdev;
4622 if (!netdev)
4623 return;
4624
4625 /* Bail out if netif_carrier is not OK */
4626 if (!netif_carrier_ok(netdev))
4627 return;
4628
4629 /* Go thru' TX queues for netdev */
4630 for (i = 0; i < netdev->num_tx_queues; i++) {
4631 struct netdev_queue *q;
4632
4633 q = netdev_get_tx_queue(netdev, i);
4634 if (q)
4635 i40e_detect_recover_hung_queue(i, vsi);
4636 }
4637}
4638
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004639/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004640 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004641 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004642 *
4643 * Get TC map for ISCSI PF type that will include iSCSI TC
4644 * and LAN TC.
4645 **/
4646static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4647{
4648 struct i40e_dcb_app_priority_table app;
4649 struct i40e_hw *hw = &pf->hw;
4650 u8 enabled_tc = 1; /* TC0 is always enabled */
4651 u8 tc, i;
4652 /* Get the iSCSI APP TLV */
4653 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4654
4655 for (i = 0; i < dcbcfg->numapps; i++) {
4656 app = dcbcfg->app[i];
4657 if (app.selector == I40E_APP_SEL_TCPIP &&
4658 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4659 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004660 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004661 break;
4662 }
4663 }
4664
4665 return enabled_tc;
4666}
4667
4668/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004669 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4670 * @dcbcfg: the corresponding DCBx configuration structure
4671 *
4672 * Return the number of TCs from given DCBx configuration
4673 **/
4674static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4675{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004676 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004677 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004678 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004679
4680 /* Scan the ETS Config Priority Table to find
4681 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004682 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004683 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004684 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4685 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4686
4687 /* Now scan the bitmask to check for
4688 * contiguous TCs starting with TC0
4689 */
4690 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4691 if (num_tc & BIT(i)) {
4692 if (!tc_unused) {
4693 ret++;
4694 } else {
4695 pr_err("Non-contiguous TC - Disabling DCB\n");
4696 return 1;
4697 }
4698 } else {
4699 tc_unused = 1;
4700 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004701 }
4702
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004703 /* There is always at least TC0 */
4704 if (!ret)
4705 ret = 1;
4706
4707 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004708}
4709
4710/**
4711 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4712 * @dcbcfg: the corresponding DCBx configuration structure
4713 *
4714 * Query the current DCB configuration and return the number of
4715 * traffic classes enabled from the given DCBX config
4716 **/
4717static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4718{
4719 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4720 u8 enabled_tc = 1;
4721 u8 i;
4722
4723 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004724 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004725
4726 return enabled_tc;
4727}
4728
4729/**
4730 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4731 * @pf: PF being queried
4732 *
4733 * Return number of traffic classes enabled for the given PF
4734 **/
4735static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4736{
4737 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004738 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004739 u8 num_tc = 0;
4740 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4741
4742 /* If DCB is not enabled then always in single TC */
4743 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4744 return 1;
4745
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004746 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004747 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4748 return i40e_dcb_get_num_tc(dcbcfg);
4749
4750 /* MFP mode return count of enabled TCs for this PF */
4751 if (pf->hw.func_caps.iscsi)
4752 enabled_tc = i40e_get_iscsi_tc_map(pf);
4753 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004754 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004755
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004756 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004757 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004758 num_tc++;
4759 }
4760 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004761}
4762
4763/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004764 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4765 * @pf: PF being queried
4766 *
4767 * Return a bitmap for enabled traffic classes for this PF.
4768 **/
4769static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4770{
4771 /* If DCB is not enabled for this PF then just return default TC */
4772 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004773 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004775 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004776 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4777 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4778
Neerav Parikhfc51de92015-02-24 06:58:53 +00004779 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004780 if (pf->hw.func_caps.iscsi)
4781 return i40e_get_iscsi_tc_map(pf);
4782 else
David Ertmanea6acb72016-09-20 07:10:50 -07004783 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004784}
4785
4786/**
4787 * i40e_vsi_get_bw_info - Query VSI BW Information
4788 * @vsi: the VSI being queried
4789 *
4790 * Returns 0 on success, negative value on failure
4791 **/
4792static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4793{
4794 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4795 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4796 struct i40e_pf *pf = vsi->back;
4797 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004798 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004799 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004800 int i;
4801
4802 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004803 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4804 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004805 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004806 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4807 i40e_stat_str(&pf->hw, ret),
4808 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004809 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004810 }
4811
4812 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004813 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4814 NULL);
4815 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004816 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004817 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4818 i40e_stat_str(&pf->hw, ret),
4819 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004820 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004821 }
4822
4823 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4824 dev_info(&pf->pdev->dev,
4825 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4826 bw_config.tc_valid_bits,
4827 bw_ets_config.tc_valid_bits);
4828 /* Still continuing */
4829 }
4830
4831 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4832 vsi->bw_max_quanta = bw_config.max_bw;
4833 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4834 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4835 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4836 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4837 vsi->bw_ets_limit_credits[i] =
4838 le16_to_cpu(bw_ets_config.credits[i]);
4839 /* 3 bits out of 4 for each TC */
4840 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4841 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004842
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004843 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004844}
4845
4846/**
4847 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4848 * @vsi: the VSI being configured
4849 * @enabled_tc: TC bitmap
4850 * @bw_credits: BW shared credits per TC
4851 *
4852 * Returns 0 on success, negative value on failure
4853 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004854static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004855 u8 *bw_share)
4856{
4857 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004858 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004859 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004860
4861 bw_data.tc_valid_bits = enabled_tc;
4862 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4863 bw_data.tc_bw_credits[i] = bw_share[i];
4864
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004865 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4866 NULL);
4867 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004868 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004869 "AQ command Config VSI BW allocation per TC failed = %d\n",
4870 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004871 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004872 }
4873
4874 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4875 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4876
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004877 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004878}
4879
4880/**
4881 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4882 * @vsi: the VSI being configured
4883 * @enabled_tc: TC map to be enabled
4884 *
4885 **/
4886static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4887{
4888 struct net_device *netdev = vsi->netdev;
4889 struct i40e_pf *pf = vsi->back;
4890 struct i40e_hw *hw = &pf->hw;
4891 u8 netdev_tc = 0;
4892 int i;
4893 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4894
4895 if (!netdev)
4896 return;
4897
4898 if (!enabled_tc) {
4899 netdev_reset_tc(netdev);
4900 return;
4901 }
4902
4903 /* Set up actual enabled TCs on the VSI */
4904 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4905 return;
4906
4907 /* set per TC queues for the VSI */
4908 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4909 /* Only set TC queues for enabled tcs
4910 *
4911 * e.g. For a VSI that has TC0 and TC3 enabled the
4912 * enabled_tc bitmap would be 0x00001001; the driver
4913 * will set the numtc for netdev as 2 that will be
4914 * referenced by the netdev layer as TC 0 and 1.
4915 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004916 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004917 netdev_set_tc_queue(netdev,
4918 vsi->tc_config.tc_info[i].netdev_tc,
4919 vsi->tc_config.tc_info[i].qcount,
4920 vsi->tc_config.tc_info[i].qoffset);
4921 }
4922
4923 /* Assign UP2TC map for the VSI */
4924 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4925 /* Get the actual TC# for the UP */
4926 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4927 /* Get the mapped netdev TC# for the UP */
4928 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4929 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4930 }
4931}
4932
4933/**
4934 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4935 * @vsi: the VSI being configured
4936 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4937 **/
4938static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4939 struct i40e_vsi_context *ctxt)
4940{
4941 /* copy just the sections touched not the entire info
4942 * since not all sections are valid as returned by
4943 * update vsi params
4944 */
4945 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4946 memcpy(&vsi->info.queue_mapping,
4947 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4948 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4949 sizeof(vsi->info.tc_mapping));
4950}
4951
4952/**
4953 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4954 * @vsi: VSI to be configured
4955 * @enabled_tc: TC bitmap
4956 *
4957 * This configures a particular VSI for TCs that are mapped to the
4958 * given TC bitmap. It uses default bandwidth share for TCs across
4959 * VSIs to configure TC for a particular VSI.
4960 *
4961 * NOTE:
4962 * It is expected that the VSI queues have been quisced before calling
4963 * this function.
4964 **/
4965static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4966{
4967 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4968 struct i40e_vsi_context ctxt;
4969 int ret = 0;
4970 int i;
4971
4972 /* Check if enabled_tc is same as existing or new TCs */
4973 if (vsi->tc_config.enabled_tc == enabled_tc)
4974 return ret;
4975
4976 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4977 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004978 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004979 bw_share[i] = 1;
4980 }
4981
4982 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4983 if (ret) {
4984 dev_info(&vsi->back->pdev->dev,
4985 "Failed configuring TC map %d for VSI %d\n",
4986 enabled_tc, vsi->seid);
4987 goto out;
4988 }
4989
4990 /* Update Queue Pairs Mapping for currently enabled UPs */
4991 ctxt.seid = vsi->seid;
4992 ctxt.pf_num = vsi->back->hw.pf_id;
4993 ctxt.vf_num = 0;
4994 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004995 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004996 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4997
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004998 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4999 ctxt.info.valid_sections |=
5000 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5001 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5002 }
5003
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005004 /* Update the VSI after updating the VSI queue-mapping information */
5005 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5006 if (ret) {
5007 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005008 "Update vsi tc config failed, err %s aq_err %s\n",
5009 i40e_stat_str(&vsi->back->hw, ret),
5010 i40e_aq_str(&vsi->back->hw,
5011 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005012 goto out;
5013 }
5014 /* update the local VSI info with updated queue map */
5015 i40e_vsi_update_queue_map(vsi, &ctxt);
5016 vsi->info.valid_sections = 0;
5017
5018 /* Update current VSI BW information */
5019 ret = i40e_vsi_get_bw_info(vsi);
5020 if (ret) {
5021 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005022 "Failed updating vsi bw info, err %s aq_err %s\n",
5023 i40e_stat_str(&vsi->back->hw, ret),
5024 i40e_aq_str(&vsi->back->hw,
5025 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005026 goto out;
5027 }
5028
5029 /* Update the netdev TC setup */
5030 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5031out:
5032 return ret;
5033}
5034
5035/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005036 * i40e_veb_config_tc - Configure TCs for given VEB
5037 * @veb: given VEB
5038 * @enabled_tc: TC bitmap
5039 *
5040 * Configures given TC bitmap for VEB (switching) element
5041 **/
5042int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5043{
5044 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5045 struct i40e_pf *pf = veb->pf;
5046 int ret = 0;
5047 int i;
5048
5049 /* No TCs or already enabled TCs just return */
5050 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5051 return ret;
5052
5053 bw_data.tc_valid_bits = enabled_tc;
5054 /* bw_data.absolute_credits is not set (relative) */
5055
5056 /* Enable ETS TCs with equal BW Share for now */
5057 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005058 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005059 bw_data.tc_bw_share_credits[i] = 1;
5060 }
5061
5062 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5063 &bw_data, NULL);
5064 if (ret) {
5065 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005066 "VEB bw config failed, err %s aq_err %s\n",
5067 i40e_stat_str(&pf->hw, ret),
5068 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005069 goto out;
5070 }
5071
5072 /* Update the BW information */
5073 ret = i40e_veb_get_bw_info(veb);
5074 if (ret) {
5075 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005076 "Failed getting veb bw config, err %s aq_err %s\n",
5077 i40e_stat_str(&pf->hw, ret),
5078 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005079 }
5080
5081out:
5082 return ret;
5083}
5084
5085#ifdef CONFIG_I40E_DCB
5086/**
5087 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5088 * @pf: PF struct
5089 *
5090 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5091 * the caller would've quiesce all the VSIs before calling
5092 * this function
5093 **/
5094static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5095{
5096 u8 tc_map = 0;
5097 int ret;
5098 u8 v;
5099
5100 /* Enable the TCs available on PF to all VEBs */
5101 tc_map = i40e_pf_get_tc_map(pf);
5102 for (v = 0; v < I40E_MAX_VEB; v++) {
5103 if (!pf->veb[v])
5104 continue;
5105 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5106 if (ret) {
5107 dev_info(&pf->pdev->dev,
5108 "Failed configuring TC for VEB seid=%d\n",
5109 pf->veb[v]->seid);
5110 /* Will try to configure as many components */
5111 }
5112 }
5113
5114 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005115 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005116 if (!pf->vsi[v])
5117 continue;
5118
5119 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005120#ifdef I40E_FCOE
5121 * - For FCoE VSI only enable the TC configured
5122 * as per the APP TLV
5123#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005124 * - For all others keep them at TC0 for now
5125 */
5126 if (v == pf->lan_vsi)
5127 tc_map = i40e_pf_get_tc_map(pf);
5128 else
David Ertmanea6acb72016-09-20 07:10:50 -07005129 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005130#ifdef I40E_FCOE
5131 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5132 tc_map = i40e_get_fcoe_tc_map(pf);
5133#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005134
5135 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5136 if (ret) {
5137 dev_info(&pf->pdev->dev,
5138 "Failed configuring TC for VSI seid=%d\n",
5139 pf->vsi[v]->seid);
5140 /* Will try to configure as many components */
5141 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005142 /* Re-configure VSI vectors based on updated TC map */
5143 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005144 if (pf->vsi[v]->netdev)
5145 i40e_dcbnl_set_all(pf->vsi[v]);
5146 }
5147 }
5148}
5149
5150/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005151 * i40e_resume_port_tx - Resume port Tx
5152 * @pf: PF struct
5153 *
5154 * Resume a port's Tx and issue a PF reset in case of failure to
5155 * resume.
5156 **/
5157static int i40e_resume_port_tx(struct i40e_pf *pf)
5158{
5159 struct i40e_hw *hw = &pf->hw;
5160 int ret;
5161
5162 ret = i40e_aq_resume_port_tx(hw, NULL);
5163 if (ret) {
5164 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005165 "Resume Port Tx failed, err %s aq_err %s\n",
5166 i40e_stat_str(&pf->hw, ret),
5167 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005168 /* Schedule PF reset to recover */
5169 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5170 i40e_service_event_schedule(pf);
5171 }
5172
5173 return ret;
5174}
5175
5176/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005177 * i40e_init_pf_dcb - Initialize DCB configuration
5178 * @pf: PF being configured
5179 *
5180 * Query the current DCB configuration and cache it
5181 * in the hardware structure
5182 **/
5183static int i40e_init_pf_dcb(struct i40e_pf *pf)
5184{
5185 struct i40e_hw *hw = &pf->hw;
5186 int err = 0;
5187
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005188 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005189 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005190 goto out;
5191
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005192 /* Get the initial DCB configuration */
5193 err = i40e_init_dcb(hw);
5194 if (!err) {
5195 /* Device/Function is not DCBX capable */
5196 if ((!hw->func_caps.dcb) ||
5197 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5198 dev_info(&pf->pdev->dev,
5199 "DCBX offload is not supported or is disabled for this PF.\n");
5200
5201 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5202 goto out;
5203
5204 } else {
5205 /* When status is not DISABLED then DCBX in FW */
5206 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5207 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005208
5209 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005210 /* Enable DCB tagging only when more than one TC
5211 * or explicitly disable if only one TC
5212 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005213 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5214 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005215 else
5216 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005217 dev_dbg(&pf->pdev->dev,
5218 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005219 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005220 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005221 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005222 "Query for DCB configuration failed, err %s aq_err %s\n",
5223 i40e_stat_str(&pf->hw, err),
5224 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005225 }
5226
5227out:
5228 return err;
5229}
5230#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005231#define SPEED_SIZE 14
5232#define FC_SIZE 8
5233/**
5234 * i40e_print_link_message - print link up or down
5235 * @vsi: the VSI for which link needs a message
5236 */
Matt Jaredc156f852015-08-27 11:42:39 -04005237void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005238{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005239 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005240 char *speed = "Unknown";
5241 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005242
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005243 new_speed = vsi->back->hw.phy.link_info.link_speed;
5244
5245 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005246 return;
5247 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005248 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005249 if (!isup) {
5250 netdev_info(vsi->netdev, "NIC Link is Down\n");
5251 return;
5252 }
5253
Greg Rose148c2d82014-12-11 07:06:27 +00005254 /* Warn user if link speed on NPAR enabled partition is not at
5255 * least 10GB
5256 */
5257 if (vsi->back->hw.func_caps.npar_enable &&
5258 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5259 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5260 netdev_warn(vsi->netdev,
5261 "The partition detected link speed that is less than 10Gbps\n");
5262
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005263 switch (vsi->back->hw.phy.link_info.link_speed) {
5264 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005265 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005266 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005267 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005268 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005269 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005270 case I40E_LINK_SPEED_25GB:
5271 speed = "25 G";
5272 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005273 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005274 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005275 break;
5276 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005277 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005278 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005279 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005280 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005281 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005282 default:
5283 break;
5284 }
5285
5286 switch (vsi->back->hw.fc.current_mode) {
5287 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005288 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005289 break;
5290 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005291 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005292 break;
5293 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005294 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005295 break;
5296 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005297 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005298 break;
5299 }
5300
Shannon Nelsona9165492015-09-03 17:19:00 -04005301 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005302 speed, fc);
5303}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005304
5305/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005306 * i40e_up_complete - Finish the last steps of bringing up a connection
5307 * @vsi: the VSI being configured
5308 **/
5309static int i40e_up_complete(struct i40e_vsi *vsi)
5310{
5311 struct i40e_pf *pf = vsi->back;
5312 int err;
5313
5314 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5315 i40e_vsi_configure_msix(vsi);
5316 else
5317 i40e_configure_msi_and_legacy(vsi);
5318
5319 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005320 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005321 if (err)
5322 return err;
5323
5324 clear_bit(__I40E_DOWN, &vsi->state);
5325 i40e_napi_enable_all(vsi);
5326 i40e_vsi_enable_irq(vsi);
5327
5328 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5329 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005330 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005331 netif_tx_start_all_queues(vsi->netdev);
5332 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005333 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005334 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005335 /* need to check for qualified module here*/
5336 if ((pf->hw.phy.link_info.link_info &
5337 I40E_AQ_MEDIA_AVAILABLE) &&
5338 (!(pf->hw.phy.link_info.an_info &
5339 I40E_AQ_QUALIFIED_MODULE)))
5340 netdev_err(vsi->netdev,
5341 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005342 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005343
5344 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005345 if (vsi->type == I40E_VSI_FDIR) {
5346 /* reset fd counters */
5347 pf->fd_add_err = pf->fd_atr_cnt = 0;
5348 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005349 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005350 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5351 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005352 pf->fd_tcp_rule = 0;
5353 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005354 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005355 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005356
5357 /* On the next run of the service_task, notify any clients of the new
5358 * opened netdev
5359 */
5360 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005361 i40e_service_event_schedule(pf);
5362
5363 return 0;
5364}
5365
5366/**
5367 * i40e_vsi_reinit_locked - Reset the VSI
5368 * @vsi: the VSI being configured
5369 *
5370 * Rebuild the ring structs after some configuration
5371 * has changed, e.g. MTU size.
5372 **/
5373static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5374{
5375 struct i40e_pf *pf = vsi->back;
5376
5377 WARN_ON(in_interrupt());
5378 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5379 usleep_range(1000, 2000);
5380 i40e_down(vsi);
5381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005382 i40e_up(vsi);
5383 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5384}
5385
5386/**
5387 * i40e_up - Bring the connection back up after being down
5388 * @vsi: the VSI being configured
5389 **/
5390int i40e_up(struct i40e_vsi *vsi)
5391{
5392 int err;
5393
5394 err = i40e_vsi_configure(vsi);
5395 if (!err)
5396 err = i40e_up_complete(vsi);
5397
5398 return err;
5399}
5400
5401/**
5402 * i40e_down - Shutdown the connection processing
5403 * @vsi: the VSI being stopped
5404 **/
5405void i40e_down(struct i40e_vsi *vsi)
5406{
5407 int i;
5408
5409 /* It is assumed that the caller of this function
5410 * sets the vsi->state __I40E_DOWN bit.
5411 */
5412 if (vsi->netdev) {
5413 netif_carrier_off(vsi->netdev);
5414 netif_tx_disable(vsi->netdev);
5415 }
5416 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005417 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418 i40e_napi_disable_all(vsi);
5419
5420 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005421 i40e_clean_tx_ring(vsi->tx_rings[i]);
5422 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005423 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005424
5425 i40e_notify_client_of_netdev_close(vsi, false);
5426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005427}
5428
5429/**
5430 * i40e_setup_tc - configure multiple traffic classes
5431 * @netdev: net device to configure
5432 * @tc: number of traffic classes to enable
5433 **/
5434static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5435{
5436 struct i40e_netdev_priv *np = netdev_priv(netdev);
5437 struct i40e_vsi *vsi = np->vsi;
5438 struct i40e_pf *pf = vsi->back;
5439 u8 enabled_tc = 0;
5440 int ret = -EINVAL;
5441 int i;
5442
5443 /* Check if DCB enabled to continue */
5444 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5445 netdev_info(netdev, "DCB is not enabled for adapter\n");
5446 goto exit;
5447 }
5448
5449 /* Check if MFP enabled */
5450 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5451 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5452 goto exit;
5453 }
5454
5455 /* Check whether tc count is within enabled limit */
5456 if (tc > i40e_pf_get_num_tc(pf)) {
5457 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5458 goto exit;
5459 }
5460
5461 /* Generate TC map for number of tc requested */
5462 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005463 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005464
5465 /* Requesting same TC configuration as already enabled */
5466 if (enabled_tc == vsi->tc_config.enabled_tc)
5467 return 0;
5468
5469 /* Quiesce VSI queues */
5470 i40e_quiesce_vsi(vsi);
5471
5472 /* Configure VSI for enabled TCs */
5473 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5474 if (ret) {
5475 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5476 vsi->seid);
5477 goto exit;
5478 }
5479
5480 /* Unquiesce VSI */
5481 i40e_unquiesce_vsi(vsi);
5482
5483exit:
5484 return ret;
5485}
5486
John Fastabende4c67342016-02-16 21:16:15 -08005487#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005488int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5489 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005490#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005491static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5492 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005493#endif
5494{
John Fastabend16e5cc62016-02-16 21:16:43 -08005495 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005496 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005497 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005498}
5499
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005500/**
5501 * i40e_open - Called when a network interface is made active
5502 * @netdev: network interface device structure
5503 *
5504 * The open entry point is called when a network interface is made
5505 * active by the system (IFF_UP). At this point all resources needed
5506 * for transmit and receive operations are allocated, the interrupt
5507 * handler is registered with the OS, the netdev watchdog subtask is
5508 * enabled, and the stack is notified that the interface is ready.
5509 *
5510 * Returns 0 on success, negative value on failure
5511 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005512int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005513{
5514 struct i40e_netdev_priv *np = netdev_priv(netdev);
5515 struct i40e_vsi *vsi = np->vsi;
5516 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005517 int err;
5518
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005519 /* disallow open during test or if eeprom is broken */
5520 if (test_bit(__I40E_TESTING, &pf->state) ||
5521 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005522 return -EBUSY;
5523
5524 netif_carrier_off(netdev);
5525
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005526 err = i40e_vsi_open(vsi);
5527 if (err)
5528 return err;
5529
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005530 /* configure global TSO hardware offload settings */
5531 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5532 TCP_FLAG_FIN) >> 16);
5533 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5534 TCP_FLAG_FIN |
5535 TCP_FLAG_CWR) >> 16);
5536 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5537
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005538 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005539
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005540 return 0;
5541}
5542
5543/**
5544 * i40e_vsi_open -
5545 * @vsi: the VSI to open
5546 *
5547 * Finish initialization of the VSI.
5548 *
5549 * Returns 0 on success, negative value on failure
5550 **/
5551int i40e_vsi_open(struct i40e_vsi *vsi)
5552{
5553 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005554 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005555 int err;
5556
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557 /* allocate descriptors */
5558 err = i40e_vsi_setup_tx_resources(vsi);
5559 if (err)
5560 goto err_setup_tx;
5561 err = i40e_vsi_setup_rx_resources(vsi);
5562 if (err)
5563 goto err_setup_rx;
5564
5565 err = i40e_vsi_configure(vsi);
5566 if (err)
5567 goto err_setup_rx;
5568
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005569 if (vsi->netdev) {
5570 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5571 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5572 err = i40e_vsi_request_irq(vsi, int_name);
5573 if (err)
5574 goto err_setup_rx;
5575
5576 /* Notify the stack of the actual queue counts. */
5577 err = netif_set_real_num_tx_queues(vsi->netdev,
5578 vsi->num_queue_pairs);
5579 if (err)
5580 goto err_set_queues;
5581
5582 err = netif_set_real_num_rx_queues(vsi->netdev,
5583 vsi->num_queue_pairs);
5584 if (err)
5585 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005586
5587 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005588 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005589 dev_driver_string(&pf->pdev->dev),
5590 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005591 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005592
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005593 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005594 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005595 goto err_setup_rx;
5596 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005597
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005598 err = i40e_up_complete(vsi);
5599 if (err)
5600 goto err_up_complete;
5601
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005602 return 0;
5603
5604err_up_complete:
5605 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005606err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005607 i40e_vsi_free_irq(vsi);
5608err_setup_rx:
5609 i40e_vsi_free_rx_resources(vsi);
5610err_setup_tx:
5611 i40e_vsi_free_tx_resources(vsi);
5612 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005613 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005614
5615 return err;
5616}
5617
5618/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005619 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005620 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005621 *
5622 * This function destroys the hlist where all the Flow Director
5623 * filters were saved.
5624 **/
5625static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5626{
5627 struct i40e_fdir_filter *filter;
5628 struct hlist_node *node2;
5629
5630 hlist_for_each_entry_safe(filter, node2,
5631 &pf->fdir_filter_list, fdir_node) {
5632 hlist_del(&filter->fdir_node);
5633 kfree(filter);
5634 }
5635 pf->fdir_pf_active_filters = 0;
5636}
5637
5638/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005639 * i40e_close - Disables a network interface
5640 * @netdev: network interface device structure
5641 *
5642 * The close entry point is called when an interface is de-activated
5643 * by the OS. The hardware is still under the driver's control, but
5644 * this netdev interface is disabled.
5645 *
5646 * Returns 0, this is not allowed to fail
5647 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005648int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005649{
5650 struct i40e_netdev_priv *np = netdev_priv(netdev);
5651 struct i40e_vsi *vsi = np->vsi;
5652
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005653 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005654
5655 return 0;
5656}
5657
5658/**
5659 * i40e_do_reset - Start a PF or Core Reset sequence
5660 * @pf: board private structure
5661 * @reset_flags: which reset is requested
5662 *
5663 * The essential difference in resets is that the PF Reset
5664 * doesn't clear the packet buffers, doesn't reset the PE
5665 * firmware, and doesn't bother the other PFs on the chip.
5666 **/
5667void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5668{
5669 u32 val;
5670
5671 WARN_ON(in_interrupt());
5672
Mitch Williams263fc482014-04-23 04:50:11 +00005673
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005674 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005675 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005676
5677 /* Request a Global Reset
5678 *
5679 * This will start the chip's countdown to the actual full
5680 * chip reset event, and a warning interrupt to be sent
5681 * to all PFs, including the requestor. Our handler
5682 * for the warning interrupt will deal with the shutdown
5683 * and recovery of the switch setup.
5684 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005685 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005686 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5687 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5688 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5689
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005690 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691
5692 /* Request a Core Reset
5693 *
5694 * Same as Global Reset, except does *not* include the MAC/PHY
5695 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005696 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005697 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5698 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5699 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5700 i40e_flush(&pf->hw);
5701
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005702 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005703
5704 /* Request a PF Reset
5705 *
5706 * Resets only the PF-specific registers
5707 *
5708 * This goes directly to the tear-down and rebuild of
5709 * the switch, since we need to do all the recovery as
5710 * for the Core Reset.
5711 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005712 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005713 i40e_handle_reset_warning(pf);
5714
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005715 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005716 int v;
5717
5718 /* Find the VSI(s) that requested a re-init */
5719 dev_info(&pf->pdev->dev,
5720 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005721 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005722 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005724 if (vsi != NULL &&
5725 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5726 i40e_vsi_reinit_locked(pf->vsi[v]);
5727 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5728 }
5729 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005730 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005731 int v;
5732
5733 /* Find the VSI(s) that needs to be brought down */
5734 dev_info(&pf->pdev->dev, "VSI down requested\n");
5735 for (v = 0; v < pf->num_alloc_vsi; v++) {
5736 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005737
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005738 if (vsi != NULL &&
5739 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5740 set_bit(__I40E_DOWN, &vsi->state);
5741 i40e_down(vsi);
5742 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5743 }
5744 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005745 } else {
5746 dev_info(&pf->pdev->dev,
5747 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005748 }
5749}
5750
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005751#ifdef CONFIG_I40E_DCB
5752/**
5753 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5754 * @pf: board private structure
5755 * @old_cfg: current DCB config
5756 * @new_cfg: new DCB config
5757 **/
5758bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5759 struct i40e_dcbx_config *old_cfg,
5760 struct i40e_dcbx_config *new_cfg)
5761{
5762 bool need_reconfig = false;
5763
5764 /* Check if ETS configuration has changed */
5765 if (memcmp(&new_cfg->etscfg,
5766 &old_cfg->etscfg,
5767 sizeof(new_cfg->etscfg))) {
5768 /* If Priority Table has changed reconfig is needed */
5769 if (memcmp(&new_cfg->etscfg.prioritytable,
5770 &old_cfg->etscfg.prioritytable,
5771 sizeof(new_cfg->etscfg.prioritytable))) {
5772 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005773 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005774 }
5775
5776 if (memcmp(&new_cfg->etscfg.tcbwtable,
5777 &old_cfg->etscfg.tcbwtable,
5778 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005779 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005780
5781 if (memcmp(&new_cfg->etscfg.tsatable,
5782 &old_cfg->etscfg.tsatable,
5783 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005784 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005785 }
5786
5787 /* Check if PFC configuration has changed */
5788 if (memcmp(&new_cfg->pfc,
5789 &old_cfg->pfc,
5790 sizeof(new_cfg->pfc))) {
5791 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005792 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005793 }
5794
5795 /* Check if APP Table has changed */
5796 if (memcmp(&new_cfg->app,
5797 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005798 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005799 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005800 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005801 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005802
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005803 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005804 return need_reconfig;
5805}
5806
5807/**
5808 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5809 * @pf: board private structure
5810 * @e: event info posted on ARQ
5811 **/
5812static int i40e_handle_lldp_event(struct i40e_pf *pf,
5813 struct i40e_arq_event_info *e)
5814{
5815 struct i40e_aqc_lldp_get_mib *mib =
5816 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5817 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005818 struct i40e_dcbx_config tmp_dcbx_cfg;
5819 bool need_reconfig = false;
5820 int ret = 0;
5821 u8 type;
5822
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005823 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005824 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005825 return ret;
5826
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005827 /* Ignore if event is not for Nearest Bridge */
5828 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5829 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005830 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005831 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5832 return ret;
5833
5834 /* Check MIB Type and return if event for Remote MIB update */
5835 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005836 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005837 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005838 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5839 /* Update the remote cached instance and return */
5840 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5841 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5842 &hw->remote_dcbx_config);
5843 goto exit;
5844 }
5845
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005846 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005847 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005848
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005849 /* Reset the old DCBx configuration data */
5850 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005851 /* Get updated DCBX data from firmware */
5852 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005853 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005854 dev_info(&pf->pdev->dev,
5855 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5856 i40e_stat_str(&pf->hw, ret),
5857 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005858 goto exit;
5859 }
5860
5861 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005862 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5863 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005864 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005865 goto exit;
5866 }
5867
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005868 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5869 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005870
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005871 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005872
5873 if (!need_reconfig)
5874 goto exit;
5875
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005876 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005877 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005878 pf->flags |= I40E_FLAG_DCB_ENABLED;
5879 else
5880 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5881
Neerav Parikh69129dc2014-11-12 00:18:46 +00005882 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005883 /* Reconfiguration needed quiesce all VSIs */
5884 i40e_pf_quiesce_all_vsi(pf);
5885
5886 /* Changes in configuration update VEB/VSI */
5887 i40e_dcb_reconfigure(pf);
5888
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005889 ret = i40e_resume_port_tx(pf);
5890
Neerav Parikh69129dc2014-11-12 00:18:46 +00005891 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005892 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005893 if (ret)
5894 goto exit;
5895
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005896 /* Wait for the PF's queues to be disabled */
5897 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005898 if (ret) {
5899 /* Schedule PF reset to recover */
5900 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5901 i40e_service_event_schedule(pf);
5902 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005903 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005904 /* Notify the client for the DCB changes */
5905 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005906 }
5907
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005908exit:
5909 return ret;
5910}
5911#endif /* CONFIG_I40E_DCB */
5912
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005913/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005914 * i40e_do_reset_safe - Protected reset path for userland calls.
5915 * @pf: board private structure
5916 * @reset_flags: which reset is requested
5917 *
5918 **/
5919void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5920{
5921 rtnl_lock();
5922 i40e_do_reset(pf, reset_flags);
5923 rtnl_unlock();
5924}
5925
5926/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005927 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5928 * @pf: board private structure
5929 * @e: event info posted on ARQ
5930 *
5931 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5932 * and VF queues
5933 **/
5934static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5935 struct i40e_arq_event_info *e)
5936{
5937 struct i40e_aqc_lan_overflow *data =
5938 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5939 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5940 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5941 struct i40e_hw *hw = &pf->hw;
5942 struct i40e_vf *vf;
5943 u16 vf_id;
5944
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005945 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5946 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005947
5948 /* Queue belongs to VF, find the VF and issue VF reset */
5949 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5950 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5951 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5952 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5953 vf_id -= hw->func_caps.vf_base_id;
5954 vf = &pf->vf[vf_id];
5955 i40e_vc_notify_vf_reset(vf);
5956 /* Allow VF to process pending reset notification */
5957 msleep(20);
5958 i40e_reset_vf(vf, false);
5959 }
5960}
5961
5962/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005963 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5964 * @pf: board private structure
5965 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005966u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005967{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005968 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005969
5970 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5971 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5972 return fcnt_prog;
5973}
5974
5975/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005976 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005977 * @pf: board private structure
5978 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005979u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005980{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005981 u32 val, fcnt_prog;
5982
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005983 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5984 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5985 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5986 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5987 return fcnt_prog;
5988}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005989
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005990/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005991 * i40e_get_global_fd_count - Get total FD filters programmed on device
5992 * @pf: board private structure
5993 **/
5994u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5995{
5996 u32 val, fcnt_prog;
5997
5998 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5999 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6000 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6001 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6002 return fcnt_prog;
6003}
6004
6005/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006006 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6007 * @pf: board private structure
6008 **/
6009void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6010{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006011 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006012 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006013 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006014
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006015 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6016 return;
6017
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006018 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6019 * to re-enable
6020 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006021 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006022 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006023 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6024 (pf->fd_add_err == 0) ||
6025 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006026 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
6027 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
6028 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006029 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6030 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 +00006031 }
6032 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006033
6034 /* Wait for some more space to be available to turn on ATR. We also
6035 * must check that no existing ntuple rules for TCP are in effect
6036 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006037 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6038 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07006039 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
6040 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006041 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006042 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006043 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 +00006044 }
6045 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006046
6047 /* if hw had a problem adding a filter, delete it */
6048 if (pf->fd_inv > 0) {
6049 hlist_for_each_entry_safe(filter, node,
6050 &pf->fdir_filter_list, fdir_node) {
6051 if (filter->fd_id == pf->fd_inv) {
6052 hlist_del(&filter->fdir_node);
6053 kfree(filter);
6054 pf->fdir_pf_active_filters--;
6055 }
6056 }
6057 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006058}
6059
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006060#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006061#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006062/**
6063 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6064 * @pf: board private structure
6065 **/
6066static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6067{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006068 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006069 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006070 bool disable_atr = false;
6071 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006072 int reg;
6073
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006074 if (!time_after(jiffies, pf->fd_flush_timestamp +
6075 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6076 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006077
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006078 /* If the flush is happening too quick and we have mostly SB rules we
6079 * should not re-enable ATR for some time.
6080 */
6081 min_flush_time = pf->fd_flush_timestamp +
6082 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6083 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006084
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006085 if (!(time_after(jiffies, min_flush_time)) &&
6086 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6087 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6088 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6089 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006090 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006091
6092 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006093 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006094 /* flush all filters */
6095 wr32(&pf->hw, I40E_PFQF_CTL_1,
6096 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6097 i40e_flush(&pf->hw);
6098 pf->fd_flush_cnt++;
6099 pf->fd_add_err = 0;
6100 do {
6101 /* Check FD flush status every 5-6msec */
6102 usleep_range(5000, 6000);
6103 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6104 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6105 break;
6106 } while (flush_wait_retry--);
6107 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6108 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6109 } else {
6110 /* replay sideband filters */
6111 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6112 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006113 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006114 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6115 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6116 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6117 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006118}
6119
6120/**
6121 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6122 * @pf: board private structure
6123 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006124u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006125{
6126 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6127}
6128
6129/* We can see up to 256 filter programming desc in transit if the filters are
6130 * being applied really fast; before we see the first
6131 * filter miss error on Rx queue 0. Accumulating enough error messages before
6132 * reacting will make sure we don't cause flush too often.
6133 */
6134#define I40E_MAX_FD_PROGRAM_ERROR 256
6135
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006136/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006137 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6138 * @pf: board private structure
6139 **/
6140static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6141{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006142
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006143 /* if interface is down do nothing */
6144 if (test_bit(__I40E_DOWN, &pf->state))
6145 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006146
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006147 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006148 i40e_fdir_flush_and_replay(pf);
6149
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006150 i40e_fdir_check_and_reenable(pf);
6151
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006152}
6153
6154/**
6155 * i40e_vsi_link_event - notify VSI of a link event
6156 * @vsi: vsi to be notified
6157 * @link_up: link up or down
6158 **/
6159static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6160{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006161 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006162 return;
6163
6164 switch (vsi->type) {
6165 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006166#ifdef I40E_FCOE
6167 case I40E_VSI_FCOE:
6168#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006169 if (!vsi->netdev || !vsi->netdev_registered)
6170 break;
6171
6172 if (link_up) {
6173 netif_carrier_on(vsi->netdev);
6174 netif_tx_wake_all_queues(vsi->netdev);
6175 } else {
6176 netif_carrier_off(vsi->netdev);
6177 netif_tx_stop_all_queues(vsi->netdev);
6178 }
6179 break;
6180
6181 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006182 case I40E_VSI_VMDQ2:
6183 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006184 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006185 case I40E_VSI_MIRROR:
6186 default:
6187 /* there is no notification for other VSIs */
6188 break;
6189 }
6190}
6191
6192/**
6193 * i40e_veb_link_event - notify elements on the veb of a link event
6194 * @veb: veb to be notified
6195 * @link_up: link up or down
6196 **/
6197static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6198{
6199 struct i40e_pf *pf;
6200 int i;
6201
6202 if (!veb || !veb->pf)
6203 return;
6204 pf = veb->pf;
6205
6206 /* depth first... */
6207 for (i = 0; i < I40E_MAX_VEB; i++)
6208 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6209 i40e_veb_link_event(pf->veb[i], link_up);
6210
6211 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006212 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006213 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6214 i40e_vsi_link_event(pf->vsi[i], link_up);
6215}
6216
6217/**
6218 * i40e_link_event - Update netif_carrier status
6219 * @pf: board private structure
6220 **/
6221static void i40e_link_event(struct i40e_pf *pf)
6222{
Mitch Williams320684c2014-10-17 03:14:43 +00006223 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006224 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006225 i40e_status status;
6226 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006227
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006228 /* save off old link status information */
6229 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6230
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006231 /* set this to force the get_link_status call to refresh state */
6232 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006233
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006234 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006235
6236 status = i40e_get_link_status(&pf->hw, &new_link);
6237 if (status) {
6238 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6239 status);
6240 return;
6241 }
6242
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006243 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6244 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006245
6246 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006247 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006248 (test_bit(__I40E_DOWN, &vsi->state) ||
6249 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006251
6252 if (!test_bit(__I40E_DOWN, &vsi->state))
6253 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254
6255 /* Notify the base of the switch tree connected to
6256 * the link. Floating VEBs are not notified.
6257 */
6258 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6259 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6260 else
Mitch Williams320684c2014-10-17 03:14:43 +00006261 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006262
6263 if (pf->vf)
6264 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006265
6266 if (pf->flags & I40E_FLAG_PTP)
6267 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006268}
6269
6270/**
Shannon Nelson21536712014-10-25 10:35:25 +00006271 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006272 * @pf: board private structure
6273 **/
6274static void i40e_watchdog_subtask(struct i40e_pf *pf)
6275{
6276 int i;
6277
6278 /* if interface is down do nothing */
6279 if (test_bit(__I40E_DOWN, &pf->state) ||
6280 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6281 return;
6282
Shannon Nelson21536712014-10-25 10:35:25 +00006283 /* make sure we don't do these things too often */
6284 if (time_before(jiffies, (pf->service_timer_previous +
6285 pf->service_timer_period)))
6286 return;
6287 pf->service_timer_previous = jiffies;
6288
Shannon Nelson9ac77262015-08-27 11:42:40 -04006289 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6290 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006291
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292 /* Update the stats for active netdevs so the network stack
6293 * can look at updated numbers whenever it cares to
6294 */
Mitch Williams505682c2014-05-20 08:01:37 +00006295 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006296 if (pf->vsi[i] && pf->vsi[i]->netdev)
6297 i40e_update_stats(pf->vsi[i]);
6298
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006299 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6300 /* Update the stats for the active switching components */
6301 for (i = 0; i < I40E_MAX_VEB; i++)
6302 if (pf->veb[i])
6303 i40e_update_veb_stats(pf->veb[i]);
6304 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006305
6306 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006307}
6308
6309/**
6310 * i40e_reset_subtask - Set up for resetting the device and driver
6311 * @pf: board private structure
6312 **/
6313static void i40e_reset_subtask(struct i40e_pf *pf)
6314{
6315 u32 reset_flags = 0;
6316
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006317 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006319 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6321 }
6322 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006323 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006324 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6325 }
6326 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006327 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006328 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6329 }
6330 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006331 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006332 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6333 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006334 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006335 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006336 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6337 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006338
6339 /* If there's a recovery already waiting, it takes
6340 * precedence before starting a new reset sequence.
6341 */
6342 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6343 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006344 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006345 }
6346
6347 /* If we're already down or resetting, just bail */
6348 if (reset_flags &&
6349 !test_bit(__I40E_DOWN, &pf->state) &&
6350 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6351 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006352
6353unlock:
6354 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006355}
6356
6357/**
6358 * i40e_handle_link_event - Handle link event
6359 * @pf: board private structure
6360 * @e: event info posted on ARQ
6361 **/
6362static void i40e_handle_link_event(struct i40e_pf *pf,
6363 struct i40e_arq_event_info *e)
6364{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 struct i40e_aqc_get_link_status *status =
6366 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006368 /* Do a new status request to re-enable LSE reporting
6369 * and load new status information into the hw struct
6370 * This completely ignores any state information
6371 * in the ARQ event info, instead choosing to always
6372 * issue the AQ update link status command.
6373 */
6374 i40e_link_event(pf);
6375
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006376 /* check for unqualified module, if link is down */
6377 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6378 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6379 (!(status->link_info & I40E_AQ_LINK_UP)))
6380 dev_err(&pf->pdev->dev,
6381 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006382}
6383
6384/**
6385 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6386 * @pf: board private structure
6387 **/
6388static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6389{
6390 struct i40e_arq_event_info event;
6391 struct i40e_hw *hw = &pf->hw;
6392 u16 pending, i = 0;
6393 i40e_status ret;
6394 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006395 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006396 u32 val;
6397
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006398 /* Do not run clean AQ when PF reset fails */
6399 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6400 return;
6401
Shannon Nelson86df2422014-05-20 08:01:35 +00006402 /* check for error indications */
6403 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6404 oldval = val;
6405 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006406 if (hw->debug_mask & I40E_DEBUG_AQ)
6407 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006408 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6409 }
6410 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006411 if (hw->debug_mask & I40E_DEBUG_AQ)
6412 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006413 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006414 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006415 }
6416 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006417 if (hw->debug_mask & I40E_DEBUG_AQ)
6418 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006419 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6420 }
6421 if (oldval != val)
6422 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6423
6424 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6425 oldval = val;
6426 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006427 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6428 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006429 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6430 }
6431 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006432 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6433 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006434 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6435 }
6436 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006437 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6438 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006439 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6440 }
6441 if (oldval != val)
6442 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6443
Mitch Williams1001dc32014-11-11 20:02:19 +00006444 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6445 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006446 if (!event.msg_buf)
6447 return;
6448
6449 do {
6450 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006451 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006452 break;
Mitch Williams56497972014-06-04 08:45:18 +00006453 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006454 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6455 break;
6456 }
6457
6458 opcode = le16_to_cpu(event.desc.opcode);
6459 switch (opcode) {
6460
6461 case i40e_aqc_opc_get_link_status:
6462 i40e_handle_link_event(pf, &event);
6463 break;
6464 case i40e_aqc_opc_send_msg_to_pf:
6465 ret = i40e_vc_process_vf_msg(pf,
6466 le16_to_cpu(event.desc.retval),
6467 le32_to_cpu(event.desc.cookie_high),
6468 le32_to_cpu(event.desc.cookie_low),
6469 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006470 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006471 break;
6472 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006473 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006474#ifdef CONFIG_I40E_DCB
6475 rtnl_lock();
6476 ret = i40e_handle_lldp_event(pf, &event);
6477 rtnl_unlock();
6478#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006479 break;
6480 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006481 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006482 i40e_handle_lan_overflow_event(pf, &event);
6483 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006484 case i40e_aqc_opc_send_msg_to_peer:
6485 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6486 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006487 case i40e_aqc_opc_nvm_erase:
6488 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006489 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006490 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6491 "ARQ NVM operation 0x%04x completed\n",
6492 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006493 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006494 default:
6495 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006496 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006497 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 break;
6499 }
6500 } while (pending && (i++ < pf->adminq_work_limit));
6501
6502 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6503 /* re-enable Admin queue interrupt cause */
6504 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6505 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6506 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6507 i40e_flush(hw);
6508
6509 kfree(event.msg_buf);
6510}
6511
6512/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006513 * i40e_verify_eeprom - make sure eeprom is good to use
6514 * @pf: board private structure
6515 **/
6516static void i40e_verify_eeprom(struct i40e_pf *pf)
6517{
6518 int err;
6519
6520 err = i40e_diag_eeprom_test(&pf->hw);
6521 if (err) {
6522 /* retry in case of garbage read */
6523 err = i40e_diag_eeprom_test(&pf->hw);
6524 if (err) {
6525 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6526 err);
6527 set_bit(__I40E_BAD_EEPROM, &pf->state);
6528 }
6529 }
6530
6531 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6532 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6533 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6534 }
6535}
6536
6537/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006538 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006539 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006540 *
6541 * enable switch loop back or die - no point in a return value
6542 **/
6543static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6544{
6545 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6546 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006547 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006548
6549 ctxt.seid = pf->main_vsi_seid;
6550 ctxt.pf_num = pf->hw.pf_id;
6551 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006552 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6553 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006554 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006555 "couldn't get PF vsi config, err %s aq_err %s\n",
6556 i40e_stat_str(&pf->hw, ret),
6557 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006558 return;
6559 }
6560 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6561 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6562 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6563
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006564 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6565 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006566 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006567 "update vsi switch failed, err %s aq_err %s\n",
6568 i40e_stat_str(&pf->hw, ret),
6569 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006570 }
6571}
6572
6573/**
6574 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006575 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006576 *
6577 * disable switch loop back or die - no point in a return value
6578 **/
6579static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6580{
6581 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6582 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006583 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006584
6585 ctxt.seid = pf->main_vsi_seid;
6586 ctxt.pf_num = pf->hw.pf_id;
6587 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006588 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6589 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006590 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006591 "couldn't get PF vsi config, err %s aq_err %s\n",
6592 i40e_stat_str(&pf->hw, ret),
6593 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006594 return;
6595 }
6596 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6597 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6598 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6599
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006600 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6601 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006602 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006603 "update vsi switch failed, err %s aq_err %s\n",
6604 i40e_stat_str(&pf->hw, ret),
6605 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006606 }
6607}
6608
6609/**
Neerav Parikh51616012015-02-06 08:52:14 +00006610 * i40e_config_bridge_mode - Configure the HW bridge mode
6611 * @veb: pointer to the bridge instance
6612 *
6613 * Configure the loop back mode for the LAN VSI that is downlink to the
6614 * specified HW bridge instance. It is expected this function is called
6615 * when a new HW bridge is instantiated.
6616 **/
6617static void i40e_config_bridge_mode(struct i40e_veb *veb)
6618{
6619 struct i40e_pf *pf = veb->pf;
6620
Shannon Nelson6dec1012015-09-28 14:12:30 -04006621 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6622 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6623 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006624 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6625 i40e_disable_pf_switch_lb(pf);
6626 else
6627 i40e_enable_pf_switch_lb(pf);
6628}
6629
6630/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006631 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6632 * @veb: pointer to the VEB instance
6633 *
6634 * This is a recursive function that first builds the attached VSIs then
6635 * recurses in to build the next layer of VEB. We track the connections
6636 * through our own index numbers because the seid's from the HW could
6637 * change across the reset.
6638 **/
6639static int i40e_reconstitute_veb(struct i40e_veb *veb)
6640{
6641 struct i40e_vsi *ctl_vsi = NULL;
6642 struct i40e_pf *pf = veb->pf;
6643 int v, veb_idx;
6644 int ret;
6645
6646 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006647 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006648 if (pf->vsi[v] &&
6649 pf->vsi[v]->veb_idx == veb->idx &&
6650 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6651 ctl_vsi = pf->vsi[v];
6652 break;
6653 }
6654 }
6655 if (!ctl_vsi) {
6656 dev_info(&pf->pdev->dev,
6657 "missing owner VSI for veb_idx %d\n", veb->idx);
6658 ret = -ENOENT;
6659 goto end_reconstitute;
6660 }
6661 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6662 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6663 ret = i40e_add_vsi(ctl_vsi);
6664 if (ret) {
6665 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006666 "rebuild of veb_idx %d owner VSI failed: %d\n",
6667 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006668 goto end_reconstitute;
6669 }
6670 i40e_vsi_reset_stats(ctl_vsi);
6671
6672 /* create the VEB in the switch and move the VSI onto the VEB */
6673 ret = i40e_add_veb(veb, ctl_vsi);
6674 if (ret)
6675 goto end_reconstitute;
6676
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006677 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6678 veb->bridge_mode = BRIDGE_MODE_VEB;
6679 else
6680 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006681 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006682
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006683 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006684 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6686 continue;
6687
6688 if (pf->vsi[v]->veb_idx == veb->idx) {
6689 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006690
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 vsi->uplink_seid = veb->seid;
6692 ret = i40e_add_vsi(vsi);
6693 if (ret) {
6694 dev_info(&pf->pdev->dev,
6695 "rebuild of vsi_idx %d failed: %d\n",
6696 v, ret);
6697 goto end_reconstitute;
6698 }
6699 i40e_vsi_reset_stats(vsi);
6700 }
6701 }
6702
6703 /* create any VEBs attached to this VEB - RECURSION */
6704 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6705 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6706 pf->veb[veb_idx]->uplink_seid = veb->seid;
6707 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6708 if (ret)
6709 break;
6710 }
6711 }
6712
6713end_reconstitute:
6714 return ret;
6715}
6716
6717/**
6718 * i40e_get_capabilities - get info about the HW
6719 * @pf: the PF struct
6720 **/
6721static int i40e_get_capabilities(struct i40e_pf *pf)
6722{
6723 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6724 u16 data_size;
6725 int buf_len;
6726 int err;
6727
6728 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6729 do {
6730 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6731 if (!cap_buf)
6732 return -ENOMEM;
6733
6734 /* this loads the data into the hw struct for us */
6735 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6736 &data_size,
6737 i40e_aqc_opc_list_func_capabilities,
6738 NULL);
6739 /* data loaded, buffer no longer needed */
6740 kfree(cap_buf);
6741
6742 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6743 /* retry with a larger buffer */
6744 buf_len = data_size;
6745 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6746 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006747 "capability discovery failed, err %s aq_err %s\n",
6748 i40e_stat_str(&pf->hw, err),
6749 i40e_aq_str(&pf->hw,
6750 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006751 return -ENODEV;
6752 }
6753 } while (err);
6754
6755 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6756 dev_info(&pf->pdev->dev,
6757 "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",
6758 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6759 pf->hw.func_caps.num_msix_vectors,
6760 pf->hw.func_caps.num_msix_vectors_vf,
6761 pf->hw.func_caps.fd_filters_guaranteed,
6762 pf->hw.func_caps.fd_filters_best_effort,
6763 pf->hw.func_caps.num_tx_qp,
6764 pf->hw.func_caps.num_vsis);
6765
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006766#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6767 + pf->hw.func_caps.num_vfs)
6768 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6769 dev_info(&pf->pdev->dev,
6770 "got num_vsis %d, setting num_vsis to %d\n",
6771 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6772 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6773 }
6774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006775 return 0;
6776}
6777
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006778static int i40e_vsi_clear(struct i40e_vsi *vsi);
6779
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006780/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006781 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006782 * @pf: board private structure
6783 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006784static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006785{
6786 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006787
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006788 /* quick workaround for an NVM issue that leaves a critical register
6789 * uninitialized
6790 */
6791 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6792 static const u32 hkey[] = {
6793 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6794 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6795 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6796 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006797 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006798
6799 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6800 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6801 }
6802
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006803 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804 return;
6805
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006806 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006807 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006808
6809 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006810 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006811 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6812 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006813 if (!vsi) {
6814 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006815 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6816 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006817 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006818 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006819
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006820 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006821}
6822
6823/**
6824 * i40e_fdir_teardown - release the Flow Director resources
6825 * @pf: board private structure
6826 **/
6827static void i40e_fdir_teardown(struct i40e_pf *pf)
6828{
Alexander Duyck4b816442016-10-11 15:26:53 -07006829 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006830
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006831 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006832 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6833 if (vsi)
6834 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006835}
6836
6837/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006838 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006839 * @pf: board private structure
6840 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006841 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006842 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006843static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006844{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006845 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006846 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006847 u32 v;
6848
6849 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6850 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006851 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006852 if (i40e_check_asq_alive(&pf->hw))
6853 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006855 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006856
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006857 /* quiesce the VSIs and their queues that are not already DOWN */
6858 i40e_pf_quiesce_all_vsi(pf);
6859
Mitch Williams505682c2014-05-20 08:01:37 +00006860 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861 if (pf->vsi[v])
6862 pf->vsi[v]->seid = 0;
6863 }
6864
6865 i40e_shutdown_adminq(&pf->hw);
6866
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006867 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006868 if (hw->hmc.hmc_obj) {
6869 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006870 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006871 dev_warn(&pf->pdev->dev,
6872 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006873 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006874}
6875
6876/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006877 * i40e_send_version - update firmware with driver version
6878 * @pf: PF struct
6879 */
6880static void i40e_send_version(struct i40e_pf *pf)
6881{
6882 struct i40e_driver_version dv;
6883
6884 dv.major_version = DRV_VERSION_MAJOR;
6885 dv.minor_version = DRV_VERSION_MINOR;
6886 dv.build_version = DRV_VERSION_BUILD;
6887 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006888 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006889 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6890}
6891
6892/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006893 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006894 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006895 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006896 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006897static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006898{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006899 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006900 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006901 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006902 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006903 u32 v;
6904
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006905 /* Now we wait for GRST to settle out.
6906 * We don't have to delete the VEBs or VSIs from the hw switch
6907 * because the reset will make them disappear.
6908 */
6909 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006910 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006911 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006912 set_bit(__I40E_RESET_FAILED, &pf->state);
6913 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006914 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006915 pf->pfr_count++;
6916
6917 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006918 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006919 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006920
6921 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6922 ret = i40e_init_adminq(&pf->hw);
6923 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006924 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6925 i40e_stat_str(&pf->hw, ret),
6926 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006927 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006928 }
6929
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006930 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006931 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006932 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006933
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006934 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006936 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006939 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6940 hw->func_caps.num_rx_qp,
6941 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6942 if (ret) {
6943 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6944 goto end_core_reset;
6945 }
6946 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6947 if (ret) {
6948 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6949 goto end_core_reset;
6950 }
6951
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006952#ifdef CONFIG_I40E_DCB
6953 ret = i40e_init_pf_dcb(pf);
6954 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006955 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6956 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6957 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006958 }
6959#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006960#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006961 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006962
Vasu Dev38e00432014-08-01 13:27:03 -07006963#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006965 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006966 if (ret)
6967 goto end_core_reset;
6968
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006969 /* The driver only wants link up/down and module qualification
6970 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006971 */
6972 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006973 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006974 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006975 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006976 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006977 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6978 i40e_stat_str(&pf->hw, ret),
6979 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006980
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006981 /* make sure our flow control settings are restored */
6982 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6983 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006984 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6985 i40e_stat_str(&pf->hw, ret),
6986 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006987
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006988 /* Rebuild the VSIs and VEBs that existed before reset.
6989 * They are still in our local switch element arrays, so only
6990 * need to rebuild the switch model in the HW.
6991 *
6992 * If there were VEBs but the reconstitution failed, we'll try
6993 * try to recover minimal use by getting the basic PF VSI working.
6994 */
6995 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006996 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 /* find the one VEB connected to the MAC, and find orphans */
6998 for (v = 0; v < I40E_MAX_VEB; v++) {
6999 if (!pf->veb[v])
7000 continue;
7001
7002 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7003 pf->veb[v]->uplink_seid == 0) {
7004 ret = i40e_reconstitute_veb(pf->veb[v]);
7005
7006 if (!ret)
7007 continue;
7008
7009 /* If Main VEB failed, we're in deep doodoo,
7010 * so give up rebuilding the switch and set up
7011 * for minimal rebuild of PF VSI.
7012 * If orphan failed, we'll report the error
7013 * but try to keep going.
7014 */
7015 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7016 dev_info(&pf->pdev->dev,
7017 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7018 ret);
7019 pf->vsi[pf->lan_vsi]->uplink_seid
7020 = pf->mac_seid;
7021 break;
7022 } else if (pf->veb[v]->uplink_seid == 0) {
7023 dev_info(&pf->pdev->dev,
7024 "rebuild of orphan VEB failed: %d\n",
7025 ret);
7026 }
7027 }
7028 }
7029 }
7030
7031 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007032 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007033 /* no VEB, so rebuild only the Main VSI */
7034 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7035 if (ret) {
7036 dev_info(&pf->pdev->dev,
7037 "rebuild of Main VSI failed: %d\n", ret);
7038 goto end_core_reset;
7039 }
7040 }
7041
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007042 /* Reconfigure hardware for allowing smaller MSS in the case
7043 * of TSO, so that we avoid the MDD being fired and causing
7044 * a reset in the case of small MSS+TSO.
7045 */
7046#define I40E_REG_MSS 0x000E64DC
7047#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7048#define I40E_64BYTE_MSS 0x400000
7049 val = rd32(hw, I40E_REG_MSS);
7050 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7051 val &= ~I40E_REG_MSS_MIN_MASK;
7052 val |= I40E_64BYTE_MSS;
7053 wr32(hw, I40E_REG_MSS, val);
7054 }
7055
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007056 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007057 msleep(75);
7058 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7059 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007060 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7061 i40e_stat_str(&pf->hw, ret),
7062 i40e_aq_str(&pf->hw,
7063 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007064 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007065 /* reinit the misc interrupt */
7066 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7067 ret = i40e_setup_misc_vector(pf);
7068
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007069 /* Add a filter to drop all Flow control frames from any VSI from being
7070 * transmitted. By doing so we stop a malicious VF from sending out
7071 * PAUSE or PFC frames and potentially controlling traffic for other
7072 * PF/VF VSIs.
7073 * The FW can still send Flow control frames if enabled.
7074 */
7075 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7076 pf->main_vsi_seid);
7077
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007078 /* restart the VSIs that were rebuilt and running before the reset */
7079 i40e_pf_unquiesce_all_vsi(pf);
7080
Mitch Williams69f64b22014-02-13 03:48:46 -08007081 if (pf->num_alloc_vfs) {
7082 for (v = 0; v < pf->num_alloc_vfs; v++)
7083 i40e_reset_vf(&pf->vf[v], true);
7084 }
7085
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007086 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007087 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007088
7089end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007090 clear_bit(__I40E_RESET_FAILED, &pf->state);
7091clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007092 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7093}
7094
7095/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007096 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007097 * @pf: board private structure
7098 *
7099 * Close up the VFs and other things in prep for a Core Reset,
7100 * then get ready to rebuild the world.
7101 **/
7102static void i40e_handle_reset_warning(struct i40e_pf *pf)
7103{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007104 i40e_prep_for_reset(pf);
7105 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007106}
7107
7108/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007109 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007110 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007111 *
7112 * Called from the MDD irq handler to identify possibly malicious vfs
7113 **/
7114static void i40e_handle_mdd_event(struct i40e_pf *pf)
7115{
7116 struct i40e_hw *hw = &pf->hw;
7117 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007118 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119 struct i40e_vf *vf;
7120 u32 reg;
7121 int i;
7122
7123 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7124 return;
7125
7126 /* find what triggered the MDD event */
7127 reg = rd32(hw, I40E_GL_MDET_TX);
7128 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007129 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7130 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007131 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007132 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007133 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007134 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007135 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7136 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7137 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007138 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007139 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 +00007140 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007141 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7142 mdd_detected = true;
7143 }
7144 reg = rd32(hw, I40E_GL_MDET_RX);
7145 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007146 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7147 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007148 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007149 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007150 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7151 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7152 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007153 if (netif_msg_rx_err(pf))
7154 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7155 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007156 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7157 mdd_detected = true;
7158 }
7159
Neerav Parikhdf430b12014-06-04 01:23:15 +00007160 if (mdd_detected) {
7161 reg = rd32(hw, I40E_PF_MDET_TX);
7162 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7163 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007164 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007165 pf_mdd_detected = true;
7166 }
7167 reg = rd32(hw, I40E_PF_MDET_RX);
7168 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7169 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007170 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007171 pf_mdd_detected = true;
7172 }
7173 /* Queue belongs to the PF, initiate a reset */
7174 if (pf_mdd_detected) {
7175 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7176 i40e_service_event_schedule(pf);
7177 }
7178 }
7179
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007180 /* see if one of the VFs needs its hand slapped */
7181 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7182 vf = &(pf->vf[i]);
7183 reg = rd32(hw, I40E_VP_MDET_TX(i));
7184 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7185 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7186 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007187 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7188 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007189 }
7190
7191 reg = rd32(hw, I40E_VP_MDET_RX(i));
7192 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7193 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7194 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007195 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7196 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007197 }
7198
7199 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7200 dev_info(&pf->pdev->dev,
7201 "Too many MDD events on VF %d, disabled\n", i);
7202 dev_info(&pf->pdev->dev,
7203 "Use PF Control I/F to re-enable the VF\n");
7204 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7205 }
7206 }
7207
7208 /* re-enable mdd interrupt cause */
7209 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7210 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7211 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7212 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7213 i40e_flush(hw);
7214}
7215
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007216/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007217 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007218 * @pf: board private structure
7219 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007220static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007221{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007222 struct i40e_hw *hw = &pf->hw;
7223 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007224 __be16 port;
7225 int i;
7226
Singhai, Anjali6a899022015-12-14 12:21:18 -08007227 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007228 return;
7229
Singhai, Anjali6a899022015-12-14 12:21:18 -08007230 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007231
7232 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007233 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7234 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7235 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007236 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007237 ret = i40e_aq_add_udp_tunnel(hw, port,
7238 pf->udp_ports[i].type,
7239 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007240 else
7241 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007242
7243 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007244 dev_dbg(&pf->pdev->dev,
7245 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7246 pf->udp_ports[i].type ? "vxlan" : "geneve",
7247 port ? "add" : "delete",
7248 ntohs(port), i,
7249 i40e_stat_str(&pf->hw, ret),
7250 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007251 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007252 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007253 }
7254 }
7255 }
7256}
7257
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007258/**
7259 * i40e_service_task - Run the driver's async subtasks
7260 * @work: pointer to work_struct containing our data
7261 **/
7262static void i40e_service_task(struct work_struct *work)
7263{
7264 struct i40e_pf *pf = container_of(work,
7265 struct i40e_pf,
7266 service_task);
7267 unsigned long start_time = jiffies;
7268
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007269 /* don't bother with service tasks if a reset is in progress */
7270 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007271 return;
7272 }
7273
Mitch Williams91089032016-11-21 13:03:51 -08007274 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7275 return;
7276
Kiran Patilb03a8c12015-09-24 18:13:15 -04007277 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007278 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007279 i40e_reset_subtask(pf);
7280 i40e_handle_mdd_event(pf);
7281 i40e_vc_process_vflr_event(pf);
7282 i40e_watchdog_subtask(pf);
7283 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007284 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007285 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007286 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007287 i40e_clean_adminq_subtask(pf);
7288
Mitch Williams91089032016-11-21 13:03:51 -08007289 /* flush memory to make sure state is correct before next watchdog */
7290 smp_mb__before_atomic();
7291 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007292
7293 /* If the tasks have taken longer than one timer cycle or there
7294 * is more work to be done, reschedule the service task now
7295 * rather than wait for the timer to tick again.
7296 */
7297 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7298 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7299 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7300 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7301 i40e_service_event_schedule(pf);
7302}
7303
7304/**
7305 * i40e_service_timer - timer callback
7306 * @data: pointer to PF struct
7307 **/
7308static void i40e_service_timer(unsigned long data)
7309{
7310 struct i40e_pf *pf = (struct i40e_pf *)data;
7311
7312 mod_timer(&pf->service_timer,
7313 round_jiffies(jiffies + pf->service_timer_period));
7314 i40e_service_event_schedule(pf);
7315}
7316
7317/**
7318 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7319 * @vsi: the VSI being configured
7320 **/
7321static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7322{
7323 struct i40e_pf *pf = vsi->back;
7324
7325 switch (vsi->type) {
7326 case I40E_VSI_MAIN:
7327 vsi->alloc_queue_pairs = pf->num_lan_qps;
7328 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7329 I40E_REQ_DESCRIPTOR_MULTIPLE);
7330 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7331 vsi->num_q_vectors = pf->num_lan_msix;
7332 else
7333 vsi->num_q_vectors = 1;
7334
7335 break;
7336
7337 case I40E_VSI_FDIR:
7338 vsi->alloc_queue_pairs = 1;
7339 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7340 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007341 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007342 break;
7343
7344 case I40E_VSI_VMDQ2:
7345 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7346 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7347 I40E_REQ_DESCRIPTOR_MULTIPLE);
7348 vsi->num_q_vectors = pf->num_vmdq_msix;
7349 break;
7350
7351 case I40E_VSI_SRIOV:
7352 vsi->alloc_queue_pairs = pf->num_vf_qps;
7353 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7354 I40E_REQ_DESCRIPTOR_MULTIPLE);
7355 break;
7356
Vasu Dev38e00432014-08-01 13:27:03 -07007357#ifdef I40E_FCOE
7358 case I40E_VSI_FCOE:
7359 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7360 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7361 I40E_REQ_DESCRIPTOR_MULTIPLE);
7362 vsi->num_q_vectors = pf->num_fcoe_msix;
7363 break;
7364
7365#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007366 default:
7367 WARN_ON(1);
7368 return -ENODATA;
7369 }
7370
7371 return 0;
7372}
7373
7374/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007375 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7376 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007377 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007378 *
7379 * On error: returns error code (negative)
7380 * On success: returns 0
7381 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007382static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007383{
7384 int size;
7385 int ret = 0;
7386
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007387 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007388 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7389 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7390 if (!vsi->tx_rings)
7391 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007392 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7393
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007394 if (alloc_qvectors) {
7395 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007396 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007397 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7398 if (!vsi->q_vectors) {
7399 ret = -ENOMEM;
7400 goto err_vectors;
7401 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007402 }
7403 return ret;
7404
7405err_vectors:
7406 kfree(vsi->tx_rings);
7407 return ret;
7408}
7409
7410/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007411 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7412 * @pf: board private structure
7413 * @type: type of VSI
7414 *
7415 * On error: returns error code (negative)
7416 * On success: returns vsi index in PF (positive)
7417 **/
7418static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7419{
7420 int ret = -ENODEV;
7421 struct i40e_vsi *vsi;
7422 int vsi_idx;
7423 int i;
7424
7425 /* Need to protect the allocation of the VSIs at the PF level */
7426 mutex_lock(&pf->switch_mutex);
7427
7428 /* VSI list may be fragmented if VSI creation/destruction has
7429 * been happening. We can afford to do a quick scan to look
7430 * for any free VSIs in the list.
7431 *
7432 * find next empty vsi slot, looping back around if necessary
7433 */
7434 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007435 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007436 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007437 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007438 i = 0;
7439 while (i < pf->next_vsi && pf->vsi[i])
7440 i++;
7441 }
7442
Mitch Williams505682c2014-05-20 08:01:37 +00007443 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007444 vsi_idx = i; /* Found one! */
7445 } else {
7446 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007447 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007448 }
7449 pf->next_vsi = ++i;
7450
7451 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7452 if (!vsi) {
7453 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007454 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007455 }
7456 vsi->type = type;
7457 vsi->back = pf;
7458 set_bit(__I40E_DOWN, &vsi->state);
7459 vsi->flags = 0;
7460 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007461 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007462 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7463 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007464 vsi->netdev_registered = false;
7465 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007466 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007467 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007468
Alexander Duyck9f65e152013-09-28 06:00:58 +00007469 ret = i40e_set_num_rings_in_vsi(vsi);
7470 if (ret)
7471 goto err_rings;
7472
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007473 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007474 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007475 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007476
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007477 /* Setup default MSIX irq handler for VSI */
7478 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7479
Kiran Patil21659032015-09-30 14:09:03 -04007480 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007481 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007482 pf->vsi[vsi_idx] = vsi;
7483 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007484 goto unlock_pf;
7485
Alexander Duyck9f65e152013-09-28 06:00:58 +00007486err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007487 pf->next_vsi = i - 1;
7488 kfree(vsi);
7489unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007490 mutex_unlock(&pf->switch_mutex);
7491 return ret;
7492}
7493
7494/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007495 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7496 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007497 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007498 *
7499 * On error: returns error code (negative)
7500 * On success: returns 0
7501 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007502static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007503{
7504 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007505 if (free_qvectors) {
7506 kfree(vsi->q_vectors);
7507 vsi->q_vectors = NULL;
7508 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007509 kfree(vsi->tx_rings);
7510 vsi->tx_rings = NULL;
7511 vsi->rx_rings = NULL;
7512}
7513
7514/**
Helin Zhang28c58692015-10-26 19:44:27 -04007515 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7516 * and lookup table
7517 * @vsi: Pointer to VSI structure
7518 */
7519static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7520{
7521 if (!vsi)
7522 return;
7523
7524 kfree(vsi->rss_hkey_user);
7525 vsi->rss_hkey_user = NULL;
7526
7527 kfree(vsi->rss_lut_user);
7528 vsi->rss_lut_user = NULL;
7529}
7530
7531/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007532 * i40e_vsi_clear - Deallocate the VSI provided
7533 * @vsi: the VSI being un-configured
7534 **/
7535static int i40e_vsi_clear(struct i40e_vsi *vsi)
7536{
7537 struct i40e_pf *pf;
7538
7539 if (!vsi)
7540 return 0;
7541
7542 if (!vsi->back)
7543 goto free_vsi;
7544 pf = vsi->back;
7545
7546 mutex_lock(&pf->switch_mutex);
7547 if (!pf->vsi[vsi->idx]) {
7548 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7549 vsi->idx, vsi->idx, vsi, vsi->type);
7550 goto unlock_vsi;
7551 }
7552
7553 if (pf->vsi[vsi->idx] != vsi) {
7554 dev_err(&pf->pdev->dev,
7555 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7556 pf->vsi[vsi->idx]->idx,
7557 pf->vsi[vsi->idx],
7558 pf->vsi[vsi->idx]->type,
7559 vsi->idx, vsi, vsi->type);
7560 goto unlock_vsi;
7561 }
7562
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007563 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007564 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7565 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7566
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007567 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007568 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007570 pf->vsi[vsi->idx] = NULL;
7571 if (vsi->idx < pf->next_vsi)
7572 pf->next_vsi = vsi->idx;
7573
7574unlock_vsi:
7575 mutex_unlock(&pf->switch_mutex);
7576free_vsi:
7577 kfree(vsi);
7578
7579 return 0;
7580}
7581
7582/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007583 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7584 * @vsi: the VSI being cleaned
7585 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007586static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007587{
7588 int i;
7589
Greg Rose8e9dca52013-12-18 13:45:53 +00007590 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007591 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007592 kfree_rcu(vsi->tx_rings[i], rcu);
7593 vsi->tx_rings[i] = NULL;
7594 vsi->rx_rings[i] = NULL;
7595 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007596 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007597}
7598
7599/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007600 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7601 * @vsi: the VSI being configured
7602 **/
7603static int i40e_alloc_rings(struct i40e_vsi *vsi)
7604{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007605 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007606 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007607 int i;
7608
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007609 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007610 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007611 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007612 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7613 if (!tx_ring)
7614 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007615
7616 tx_ring->queue_index = i;
7617 tx_ring->reg_idx = vsi->base_queue + i;
7618 tx_ring->ring_active = false;
7619 tx_ring->vsi = vsi;
7620 tx_ring->netdev = vsi->netdev;
7621 tx_ring->dev = &pf->pdev->dev;
7622 tx_ring->count = vsi->num_desc;
7623 tx_ring->size = 0;
7624 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007625 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7626 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007627 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007628 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007629
Alexander Duyck9f65e152013-09-28 06:00:58 +00007630 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007631 rx_ring->queue_index = i;
7632 rx_ring->reg_idx = vsi->base_queue + i;
7633 rx_ring->ring_active = false;
7634 rx_ring->vsi = vsi;
7635 rx_ring->netdev = vsi->netdev;
7636 rx_ring->dev = &pf->pdev->dev;
7637 rx_ring->count = vsi->num_desc;
7638 rx_ring->size = 0;
7639 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007640 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007641 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007642 }
7643
7644 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007645
7646err_out:
7647 i40e_vsi_clear_rings(vsi);
7648 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007649}
7650
7651/**
7652 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7653 * @pf: board private structure
7654 * @vectors: the number of MSI-X vectors to request
7655 *
7656 * Returns the number of vectors reserved, or error
7657 **/
7658static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7659{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007660 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7661 I40E_MIN_MSIX, vectors);
7662 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007663 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007664 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007665 vectors = 0;
7666 }
7667
7668 return vectors;
7669}
7670
7671/**
7672 * i40e_init_msix - Setup the MSIX capability
7673 * @pf: board private structure
7674 *
7675 * Work with the OS to set up the MSIX vectors needed.
7676 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007677 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007678 **/
7679static int i40e_init_msix(struct i40e_pf *pf)
7680{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007681 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007682 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007683 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007684 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007685 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007686
7687 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7688 return -ENODEV;
7689
7690 /* The number of vectors we'll request will be comprised of:
7691 * - Add 1 for "other" cause for Admin Queue events, etc.
7692 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007693 * - Queues being used for RSS.
7694 * We don't need as many as max_rss_size vectors.
7695 * use rss_size instead in the calculation since that
7696 * is governed by number of cpus in the system.
7697 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007699 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007700#ifdef I40E_FCOE
7701 * - The number of FCOE qps.
7702#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007703 * Once we count this up, try the request.
7704 *
7705 * If we can't get what we want, we'll simplify to nearly nothing
7706 * and try again. If that still fails, we punt.
7707 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007708 vectors_left = hw->func_caps.num_msix_vectors;
7709 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710
Shannon Nelson1e200e42015-02-27 09:15:24 +00007711 /* reserve one vector for miscellaneous handler */
7712 if (vectors_left) {
7713 v_budget++;
7714 vectors_left--;
7715 }
7716
7717 /* reserve vectors for the main PF traffic queues */
7718 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7719 vectors_left -= pf->num_lan_msix;
7720 v_budget += pf->num_lan_msix;
7721
7722 /* reserve one vector for sideband flow director */
7723 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7724 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007725 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007726 v_budget++;
7727 vectors_left--;
7728 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007729 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007730 }
7731 }
John W Linville83840e42015-01-14 03:06:28 +00007732
Vasu Dev38e00432014-08-01 13:27:03 -07007733#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007734 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007735 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007736 if (!vectors_left)
7737 pf->num_fcoe_msix = 0;
7738 else if (vectors_left >= pf->num_fcoe_qps)
7739 pf->num_fcoe_msix = pf->num_fcoe_qps;
7740 else
7741 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007742 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007743 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007744 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007745
Vasu Dev38e00432014-08-01 13:27:03 -07007746#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007747 /* can we reserve enough for iWARP? */
7748 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007749 iwarp_requested = pf->num_iwarp_msix;
7750
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007751 if (!vectors_left)
7752 pf->num_iwarp_msix = 0;
7753 else if (vectors_left < pf->num_iwarp_msix)
7754 pf->num_iwarp_msix = 1;
7755 v_budget += pf->num_iwarp_msix;
7756 vectors_left -= pf->num_iwarp_msix;
7757 }
7758
Shannon Nelson1e200e42015-02-27 09:15:24 +00007759 /* any vectors left over go for VMDq support */
7760 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7761 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7762 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7763
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007764 if (!vectors_left) {
7765 pf->num_vmdq_msix = 0;
7766 pf->num_vmdq_qps = 0;
7767 } else {
7768 /* if we're short on vectors for what's desired, we limit
7769 * the queues per vmdq. If this is still more than are
7770 * available, the user will need to change the number of
7771 * queues/vectors used by the PF later with the ethtool
7772 * channels command
7773 */
7774 if (vmdq_vecs < vmdq_vecs_wanted)
7775 pf->num_vmdq_qps = 1;
7776 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007777
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007778 v_budget += vmdq_vecs;
7779 vectors_left -= vmdq_vecs;
7780 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007781 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007782
7783 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7784 GFP_KERNEL);
7785 if (!pf->msix_entries)
7786 return -ENOMEM;
7787
7788 for (i = 0; i < v_budget; i++)
7789 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007790 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007791
Shannon Nelson3b444392015-02-26 16:15:57 +00007792 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007793 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7794 kfree(pf->msix_entries);
7795 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007796 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007797 return -ENODEV;
7798
Shannon Nelson3b444392015-02-26 16:15:57 +00007799 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007800 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007801 pf->num_vmdq_vsis = 0;
7802 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803 pf->num_lan_qps = 1;
7804 pf->num_lan_msix = 1;
7805
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007806 } else if (!vectors_left) {
7807 /* If we have limited resources, we will start with no vectors
7808 * for the special features and then allocate vectors to some
7809 * of these features based on the policy and at the end disable
7810 * the features that did not get any vectors.
7811 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007812 int vec;
7813
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007814 dev_info(&pf->pdev->dev,
7815 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007816 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007817 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007818
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007819 /* Scale vector usage down */
7820 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007821 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007822 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007823#ifdef I40E_FCOE
7824 pf->num_fcoe_qps = 0;
7825 pf->num_fcoe_msix = 0;
7826#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007827
7828 /* partition out the remaining vectors */
7829 switch (vec) {
7830 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007831 pf->num_lan_msix = 1;
7832 break;
7833 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007834 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7835 pf->num_lan_msix = 1;
7836 pf->num_iwarp_msix = 1;
7837 } else {
7838 pf->num_lan_msix = 2;
7839 }
Vasu Dev38e00432014-08-01 13:27:03 -07007840#ifdef I40E_FCOE
7841 /* give one vector to FCoE */
7842 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7843 pf->num_lan_msix = 1;
7844 pf->num_fcoe_msix = 1;
7845 }
Vasu Dev38e00432014-08-01 13:27:03 -07007846#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007847 break;
7848 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007849 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7850 pf->num_iwarp_msix = min_t(int, (vec / 3),
7851 iwarp_requested);
7852 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7853 I40E_DEFAULT_NUM_VMDQ_VSI);
7854 } else {
7855 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7856 I40E_DEFAULT_NUM_VMDQ_VSI);
7857 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007858 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7859 pf->num_fdsb_msix = 1;
7860 vec--;
7861 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007862 pf->num_lan_msix = min_t(int,
7863 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7864 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007865 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007866#ifdef I40E_FCOE
7867 /* give one vector to FCoE */
7868 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7869 pf->num_fcoe_msix = 1;
7870 vec--;
7871 }
7872#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007873 break;
7874 }
7875 }
7876
Stefan Assmannabd97a92016-09-19 13:37:51 +02007877 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7878 (pf->num_fdsb_msix == 0)) {
7879 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7880 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7881 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007882 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7883 (pf->num_vmdq_msix == 0)) {
7884 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7885 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7886 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007887
7888 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7889 (pf->num_iwarp_msix == 0)) {
7890 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7891 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7892 }
Vasu Dev38e00432014-08-01 13:27:03 -07007893#ifdef I40E_FCOE
7894
7895 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7896 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7897 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7898 }
7899#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007900 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7901 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7902 pf->num_lan_msix,
7903 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7904 pf->num_fdsb_msix,
7905 pf->num_iwarp_msix);
7906
Shannon Nelson3b444392015-02-26 16:15:57 +00007907 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007908}
7909
7910/**
Greg Rose90e04072014-03-06 08:59:57 +00007911 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007912 * @vsi: the VSI being configured
7913 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007914 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007915 *
7916 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7917 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007918static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007919{
7920 struct i40e_q_vector *q_vector;
7921
7922 /* allocate q_vector */
7923 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7924 if (!q_vector)
7925 return -ENOMEM;
7926
7927 q_vector->vsi = vsi;
7928 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007929 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7930
Alexander Duyck493fb302013-09-28 07:01:44 +00007931 if (vsi->netdev)
7932 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007933 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007934
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007935 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7936 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7937
Alexander Duyck493fb302013-09-28 07:01:44 +00007938 /* tie q_vector and vsi together */
7939 vsi->q_vectors[v_idx] = q_vector;
7940
7941 return 0;
7942}
7943
7944/**
Greg Rose90e04072014-03-06 08:59:57 +00007945 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007946 * @vsi: the VSI being configured
7947 *
7948 * We allocate one q_vector per queue interrupt. If allocation fails we
7949 * return -ENOMEM.
7950 **/
Greg Rose90e04072014-03-06 08:59:57 +00007951static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007952{
7953 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007954 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007955
7956 /* if not MSIX, give the one vector only to the LAN VSI */
7957 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7958 num_q_vectors = vsi->num_q_vectors;
7959 else if (vsi == pf->vsi[pf->lan_vsi])
7960 num_q_vectors = 1;
7961 else
7962 return -EINVAL;
7963
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007964 current_cpu = cpumask_first(cpu_online_mask);
7965
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007966 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007967 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007968 if (err)
7969 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007970 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7971 if (unlikely(current_cpu >= nr_cpu_ids))
7972 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007973 }
7974
7975 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007976
7977err_out:
7978 while (v_idx--)
7979 i40e_free_q_vector(vsi, v_idx);
7980
7981 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007982}
7983
7984/**
7985 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7986 * @pf: board private structure to initialize
7987 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007988static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007989{
Shannon Nelson3b444392015-02-26 16:15:57 +00007990 int vectors = 0;
7991 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007992
7993 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007994 vectors = i40e_init_msix(pf);
7995 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007996 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007997 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007998#ifdef I40E_FCOE
7999 I40E_FLAG_FCOE_ENABLED |
8000#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008001 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008002 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008003 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008004 I40E_FLAG_SRIOV_ENABLED |
8005 I40E_FLAG_FD_SB_ENABLED |
8006 I40E_FLAG_FD_ATR_ENABLED |
8007 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008008
8009 /* rework the queue expectations without MSIX */
8010 i40e_determine_queue_usage(pf);
8011 }
8012 }
8013
8014 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8015 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008016 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008017 vectors = pci_enable_msi(pf->pdev);
8018 if (vectors < 0) {
8019 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8020 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008021 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8022 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008023 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008024 }
8025
Shannon Nelson958a3e32013-09-28 07:13:28 +00008026 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008027 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008028
Shannon Nelson3b444392015-02-26 16:15:57 +00008029 /* set up vector assignment tracking */
8030 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8031 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008032 if (!pf->irq_pile) {
8033 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8034 return -ENOMEM;
8035 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008036 pf->irq_pile->num_entries = vectors;
8037 pf->irq_pile->search_hint = 0;
8038
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008039 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008040 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008041
8042 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008043}
8044
8045/**
8046 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8047 * @pf: board private structure
8048 *
8049 * This sets up the handler for MSIX 0, which is used to manage the
8050 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8051 * when in MSI or Legacy interrupt mode.
8052 **/
8053static int i40e_setup_misc_vector(struct i40e_pf *pf)
8054{
8055 struct i40e_hw *hw = &pf->hw;
8056 int err = 0;
8057
8058 /* Only request the irq if this is the first time through, and
8059 * not when we're rebuilding after a Reset
8060 */
8061 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8062 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008063 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008064 if (err) {
8065 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008066 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008067 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008068 return -EFAULT;
8069 }
8070 }
8071
Jacob Kellerab437b52014-12-14 01:55:08 +00008072 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008073
8074 /* associate no queues to the misc vector */
8075 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8076 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8077
8078 i40e_flush(hw);
8079
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008080 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008081
8082 return err;
8083}
8084
8085/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008086 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8087 * @vsi: vsi structure
8088 * @seed: RSS hash seed
8089 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008090static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8091 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008092{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008093 struct i40e_pf *pf = vsi->back;
8094 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008095 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008096
Jacob Keller776b2e12016-07-19 16:23:30 -07008097 if (seed) {
8098 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8099 (struct i40e_aqc_get_set_rss_key_data *)seed;
8100 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8101 if (ret) {
8102 dev_info(&pf->pdev->dev,
8103 "Cannot set RSS key, err %s aq_err %s\n",
8104 i40e_stat_str(hw, ret),
8105 i40e_aq_str(hw, hw->aq.asq_last_status));
8106 return ret;
8107 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008108 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008109 if (lut) {
8110 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008111
Jacob Keller776b2e12016-07-19 16:23:30 -07008112 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8113 if (ret) {
8114 dev_info(&pf->pdev->dev,
8115 "Cannot set RSS lut, err %s aq_err %s\n",
8116 i40e_stat_str(hw, ret),
8117 i40e_aq_str(hw, hw->aq.asq_last_status));
8118 return ret;
8119 }
8120 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008121 return ret;
8122}
8123
8124/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008125 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8126 * @vsi: Pointer to vsi structure
8127 * @seed: Buffter to store the hash keys
8128 * @lut: Buffer to store the lookup table entries
8129 * @lut_size: Size of buffer to store the lookup table entries
8130 *
8131 * Return 0 on success, negative on failure
8132 */
8133static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8134 u8 *lut, u16 lut_size)
8135{
8136 struct i40e_pf *pf = vsi->back;
8137 struct i40e_hw *hw = &pf->hw;
8138 int ret = 0;
8139
8140 if (seed) {
8141 ret = i40e_aq_get_rss_key(hw, vsi->id,
8142 (struct i40e_aqc_get_set_rss_key_data *)seed);
8143 if (ret) {
8144 dev_info(&pf->pdev->dev,
8145 "Cannot get RSS key, err %s aq_err %s\n",
8146 i40e_stat_str(&pf->hw, ret),
8147 i40e_aq_str(&pf->hw,
8148 pf->hw.aq.asq_last_status));
8149 return ret;
8150 }
8151 }
8152
8153 if (lut) {
8154 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8155
8156 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8157 if (ret) {
8158 dev_info(&pf->pdev->dev,
8159 "Cannot get RSS lut, err %s aq_err %s\n",
8160 i40e_stat_str(&pf->hw, ret),
8161 i40e_aq_str(&pf->hw,
8162 pf->hw.aq.asq_last_status));
8163 return ret;
8164 }
8165 }
8166
8167 return ret;
8168}
8169
8170/**
Jacob Keller0582b962016-07-19 16:23:29 -07008171 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8172 * @vsi: VSI structure
8173 **/
8174static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8175{
8176 u8 seed[I40E_HKEY_ARRAY_SIZE];
8177 struct i40e_pf *pf = vsi->back;
8178 u8 *lut;
8179 int ret;
8180
8181 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8182 return 0;
8183
Jacob Keller552b9962016-07-19 16:23:31 -07008184 if (!vsi->rss_size)
8185 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8186 vsi->num_queue_pairs);
8187 if (!vsi->rss_size)
8188 return -EINVAL;
8189
Jacob Keller0582b962016-07-19 16:23:29 -07008190 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8191 if (!lut)
8192 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008193 /* Use the user configured hash keys and lookup table if there is one,
8194 * otherwise use default
8195 */
8196 if (vsi->rss_lut_user)
8197 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8198 else
8199 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8200 if (vsi->rss_hkey_user)
8201 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8202 else
8203 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008204 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8205 kfree(lut);
8206
8207 return ret;
8208}
8209
8210/**
Helin Zhang043dd652015-10-21 19:56:23 -04008211 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008212 * @vsi: Pointer to vsi structure
8213 * @seed: RSS hash seed
8214 * @lut: Lookup table
8215 * @lut_size: Lookup table size
8216 *
8217 * Returns 0 on success, negative on failure
8218 **/
8219static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8220 const u8 *lut, u16 lut_size)
8221{
8222 struct i40e_pf *pf = vsi->back;
8223 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008224 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008225 u8 i;
8226
8227 /* Fill out hash function seed */
8228 if (seed) {
8229 u32 *seed_dw = (u32 *)seed;
8230
Mitch Williamsc4e18682016-04-12 08:30:40 -07008231 if (vsi->type == I40E_VSI_MAIN) {
8232 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8233 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8234 seed_dw[i]);
8235 } else if (vsi->type == I40E_VSI_SRIOV) {
8236 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8237 i40e_write_rx_ctl(hw,
8238 I40E_VFQF_HKEY1(i, vf_id),
8239 seed_dw[i]);
8240 } else {
8241 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8242 }
Helin Zhange69ff812015-10-21 19:56:22 -04008243 }
8244
8245 if (lut) {
8246 u32 *lut_dw = (u32 *)lut;
8247
Mitch Williamsc4e18682016-04-12 08:30:40 -07008248 if (vsi->type == I40E_VSI_MAIN) {
8249 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8250 return -EINVAL;
8251 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8252 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8253 } else if (vsi->type == I40E_VSI_SRIOV) {
8254 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8255 return -EINVAL;
8256 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8257 i40e_write_rx_ctl(hw,
8258 I40E_VFQF_HLUT1(i, vf_id),
8259 lut_dw[i]);
8260 } else {
8261 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8262 }
Helin Zhange69ff812015-10-21 19:56:22 -04008263 }
8264 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008265
8266 return 0;
8267}
8268
8269/**
Helin Zhang043dd652015-10-21 19:56:23 -04008270 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8271 * @vsi: Pointer to VSI structure
8272 * @seed: Buffer to store the keys
8273 * @lut: Buffer to store the lookup table entries
8274 * @lut_size: Size of buffer to store the lookup table entries
8275 *
8276 * Returns 0 on success, negative on failure
8277 */
8278static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8279 u8 *lut, u16 lut_size)
8280{
8281 struct i40e_pf *pf = vsi->back;
8282 struct i40e_hw *hw = &pf->hw;
8283 u16 i;
8284
8285 if (seed) {
8286 u32 *seed_dw = (u32 *)seed;
8287
8288 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008289 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008290 }
8291 if (lut) {
8292 u32 *lut_dw = (u32 *)lut;
8293
8294 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8295 return -EINVAL;
8296 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8297 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8298 }
8299
8300 return 0;
8301}
8302
8303/**
8304 * i40e_config_rss - Configure RSS keys and lut
8305 * @vsi: Pointer to VSI structure
8306 * @seed: RSS hash seed
8307 * @lut: Lookup table
8308 * @lut_size: Lookup table size
8309 *
8310 * Returns 0 on success, negative on failure
8311 */
8312int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8313{
8314 struct i40e_pf *pf = vsi->back;
8315
8316 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8317 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8318 else
8319 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8320}
8321
8322/**
8323 * i40e_get_rss - Get RSS keys and lut
8324 * @vsi: Pointer to VSI structure
8325 * @seed: Buffer to store the keys
8326 * @lut: Buffer to store the lookup table entries
8327 * lut_size: Size of buffer to store the lookup table entries
8328 *
8329 * Returns 0 on success, negative on failure
8330 */
8331int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8332{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008333 struct i40e_pf *pf = vsi->back;
8334
8335 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8336 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8337 else
8338 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008339}
8340
8341/**
Helin Zhange69ff812015-10-21 19:56:22 -04008342 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8343 * @pf: Pointer to board private structure
8344 * @lut: Lookup table
8345 * @rss_table_size: Lookup table size
8346 * @rss_size: Range of queue number for hashing
8347 */
Alan Bradyf1582352016-08-24 11:33:46 -07008348void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8349 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008350{
Helin Zhange69ff812015-10-21 19:56:22 -04008351 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008352
Helin Zhange69ff812015-10-21 19:56:22 -04008353 for (i = 0; i < rss_table_size; i++)
8354 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008355}
8356
8357/**
Helin Zhang043dd652015-10-21 19:56:23 -04008358 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008359 * @pf: board private structure
8360 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008361static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008362{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008363 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008364 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008365 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008366 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008367 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008368 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008369 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008370
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008371 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008372 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8373 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008374 hena |= i40e_pf_get_default_rss_hena(pf);
8375
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008376 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8377 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008378
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008379 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008380 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008381 reg_val = (pf->rss_table_size == 512) ?
8382 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8383 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008384 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008385
Helin Zhang28c58692015-10-26 19:44:27 -04008386 /* Determine the RSS size of the VSI */
8387 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008388 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8389 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008390 if (!vsi->rss_size)
8391 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008392
Helin Zhange69ff812015-10-21 19:56:22 -04008393 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8394 if (!lut)
8395 return -ENOMEM;
8396
Helin Zhang28c58692015-10-26 19:44:27 -04008397 /* Use user configured lut if there is one, otherwise use default */
8398 if (vsi->rss_lut_user)
8399 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8400 else
8401 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008402
Helin Zhang28c58692015-10-26 19:44:27 -04008403 /* Use user configured hash key if there is one, otherwise
8404 * use default.
8405 */
8406 if (vsi->rss_hkey_user)
8407 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8408 else
8409 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008410 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008411 kfree(lut);
8412
8413 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008414}
8415
8416/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008417 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8418 * @pf: board private structure
8419 * @queue_count: the requested queue count for rss.
8420 *
8421 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8422 * count which may be different from the requested queue count.
8423 **/
8424int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8425{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008426 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8427 int new_rss_size;
8428
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008429 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8430 return 0;
8431
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008432 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008433
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008434 if (queue_count != vsi->num_queue_pairs) {
8435 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008436 i40e_prep_for_reset(pf);
8437
Helin Zhangacd65442015-10-26 19:44:28 -04008438 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008439
8440 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008441
8442 /* Discard the user configured hash keys and lut, if less
8443 * queues are enabled.
8444 */
8445 if (queue_count < vsi->rss_size) {
8446 i40e_clear_rss_config_user(vsi);
8447 dev_dbg(&pf->pdev->dev,
8448 "discard user configured hash keys and lut\n");
8449 }
8450
8451 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008452 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8453 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008454
Helin Zhang043dd652015-10-21 19:56:23 -04008455 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008456 }
Lihong Yang12815052016-09-27 11:28:48 -07008457 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8458 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008459 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008460}
8461
8462/**
Greg Rosef4492db2015-02-06 08:52:12 +00008463 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8464 * @pf: board private structure
8465 **/
8466i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8467{
8468 i40e_status status;
8469 bool min_valid, max_valid;
8470 u32 max_bw, min_bw;
8471
8472 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8473 &min_valid, &max_valid);
8474
8475 if (!status) {
8476 if (min_valid)
8477 pf->npar_min_bw = min_bw;
8478 if (max_valid)
8479 pf->npar_max_bw = max_bw;
8480 }
8481
8482 return status;
8483}
8484
8485/**
8486 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8487 * @pf: board private structure
8488 **/
8489i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8490{
8491 struct i40e_aqc_configure_partition_bw_data bw_data;
8492 i40e_status status;
8493
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008494 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008495 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008496 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8497 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8498
8499 /* Set the new bandwidths */
8500 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8501
8502 return status;
8503}
8504
8505/**
8506 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8507 * @pf: board private structure
8508 **/
8509i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8510{
8511 /* Commit temporary BW setting to permanent NVM image */
8512 enum i40e_admin_queue_err last_aq_status;
8513 i40e_status ret;
8514 u16 nvm_word;
8515
8516 if (pf->hw.partition_id != 1) {
8517 dev_info(&pf->pdev->dev,
8518 "Commit BW only works on partition 1! This is partition %d",
8519 pf->hw.partition_id);
8520 ret = I40E_NOT_SUPPORTED;
8521 goto bw_commit_out;
8522 }
8523
8524 /* Acquire NVM for read access */
8525 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8526 last_aq_status = pf->hw.aq.asq_last_status;
8527 if (ret) {
8528 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008529 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8530 i40e_stat_str(&pf->hw, ret),
8531 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008532 goto bw_commit_out;
8533 }
8534
8535 /* Read word 0x10 of NVM - SW compatibility word 1 */
8536 ret = i40e_aq_read_nvm(&pf->hw,
8537 I40E_SR_NVM_CONTROL_WORD,
8538 0x10, sizeof(nvm_word), &nvm_word,
8539 false, NULL);
8540 /* Save off last admin queue command status before releasing
8541 * the NVM
8542 */
8543 last_aq_status = pf->hw.aq.asq_last_status;
8544 i40e_release_nvm(&pf->hw);
8545 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008546 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8547 i40e_stat_str(&pf->hw, ret),
8548 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008549 goto bw_commit_out;
8550 }
8551
8552 /* Wait a bit for NVM release to complete */
8553 msleep(50);
8554
8555 /* Acquire NVM for write access */
8556 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8557 last_aq_status = pf->hw.aq.asq_last_status;
8558 if (ret) {
8559 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008560 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8561 i40e_stat_str(&pf->hw, ret),
8562 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008563 goto bw_commit_out;
8564 }
8565 /* Write it back out unchanged to initiate update NVM,
8566 * which will force a write of the shadow (alt) RAM to
8567 * the NVM - thus storing the bandwidth values permanently.
8568 */
8569 ret = i40e_aq_update_nvm(&pf->hw,
8570 I40E_SR_NVM_CONTROL_WORD,
8571 0x10, sizeof(nvm_word),
8572 &nvm_word, true, NULL);
8573 /* Save off last admin queue command status before releasing
8574 * the NVM
8575 */
8576 last_aq_status = pf->hw.aq.asq_last_status;
8577 i40e_release_nvm(&pf->hw);
8578 if (ret)
8579 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008580 "BW settings NOT SAVED, err %s aq_err %s\n",
8581 i40e_stat_str(&pf->hw, ret),
8582 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008583bw_commit_out:
8584
8585 return ret;
8586}
8587
8588/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008589 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8590 * @pf: board private structure to initialize
8591 *
8592 * i40e_sw_init initializes the Adapter private data structure.
8593 * Fields are initialized based on PCI device information and
8594 * OS network device settings (MTU size).
8595 **/
8596static int i40e_sw_init(struct i40e_pf *pf)
8597{
8598 int err = 0;
8599 int size;
8600
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008601 /* Set default capability flags */
8602 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8603 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008604 I40E_FLAG_MSIX_ENABLED;
8605
Mitch Williamsca99eb92014-04-04 04:43:07 +00008606 /* Set default ITR */
8607 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8608 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8609
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008610 /* Depending on PF configurations, it is possible that the RSS
8611 * maximum might end up larger than the available queues
8612 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008613 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008614 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008615 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008616 pf->rss_size_max = min_t(int, pf->rss_size_max,
8617 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008618 if (pf->hw.func_caps.rss) {
8619 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008620 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8621 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008622 }
8623
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008624 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008625 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008626 pf->flags |= I40E_FLAG_MFP_ENABLED;
8627 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008628 if (i40e_get_npar_bw_setting(pf))
8629 dev_warn(&pf->pdev->dev,
8630 "Could not get NPAR bw settings\n");
8631 else
8632 dev_info(&pf->pdev->dev,
8633 "Min BW = %8.8x, Max BW = %8.8x\n",
8634 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008635 }
8636
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008637 /* FW/NVM is not yet fixed in this regard */
8638 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8639 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8640 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8641 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008642 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8643 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008644 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008645 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008646 else
8647 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008648 pf->fdir_pf_filter_count =
8649 pf->hw.func_caps.fd_filters_guaranteed;
8650 pf->hw.fdir_shared_filter_count =
8651 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008652 }
8653
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008654 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008655 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008656 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008657 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008658 /* No DCB support for FW < v4.33 */
8659 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8660 }
8661
8662 /* Disable FW LLDP if FW < v4.3 */
8663 if (i40e_is_mac_710(&pf->hw) &&
8664 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8665 (pf->hw.aq.fw_maj_ver < 4)))
8666 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8667
8668 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8669 if (i40e_is_mac_710(&pf->hw) &&
8670 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8671 (pf->hw.aq.fw_maj_ver >= 5)))
8672 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008673
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008674 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008675 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008676 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008677 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008678 }
8679
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008680 if (pf->hw.func_caps.iwarp) {
8681 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8682 /* IWARP needs one extra vector for CQP just like MISC.*/
8683 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8684 }
8685
Vasu Dev38e00432014-08-01 13:27:03 -07008686#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008687 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008688
8689#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008690#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008691 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008692 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8693 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8694 pf->num_req_vfs = min_t(int,
8695 pf->hw.func_caps.num_vfs,
8696 I40E_MAX_VF_COUNT);
8697 }
8698#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008699 if (pf->hw.mac.type == I40E_MAC_X722) {
8700 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8701 I40E_FLAG_128_QP_RSS_CAPABLE |
8702 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8703 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8704 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008705 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008706 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008707 I40E_FLAG_USE_SET_LLDP_MIB |
Jacob Keller1e28e862016-11-11 12:39:25 -08008708 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE |
8709 I40E_FLAG_PTP_L4_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008710 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8711 ((pf->hw.aq.api_maj_ver == 1) &&
8712 (pf->hw.aq.api_min_ver > 4))) {
8713 /* Supported in FW API version higher than 1.4 */
8714 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008715 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8716 } else {
8717 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008718 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008720 pf->eeprom_version = 0xDEAD;
8721 pf->lan_veb = I40E_NO_VEB;
8722 pf->lan_vsi = I40E_NO_VSI;
8723
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008724 /* By default FW has this off for performance reasons */
8725 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008727 /* set up queue assignment tracking */
8728 size = sizeof(struct i40e_lump_tracking)
8729 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8730 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8731 if (!pf->qp_pile) {
8732 err = -ENOMEM;
8733 goto sw_init_done;
8734 }
8735 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8736 pf->qp_pile->search_hint = 0;
8737
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008738 pf->tx_timeout_recovery_level = 1;
8739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008740 mutex_init(&pf->switch_mutex);
8741
Greg Rosec668a122015-02-26 16:10:39 +00008742 /* If NPAR is enabled nudge the Tx scheduler */
8743 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8744 i40e_set_npar_bw_setting(pf);
8745
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008746sw_init_done:
8747 return err;
8748}
8749
8750/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008751 * i40e_set_ntuple - set the ntuple feature flag and take action
8752 * @pf: board private structure to initialize
8753 * @features: the feature set that the stack is suggesting
8754 *
8755 * returns a bool to indicate if reset needs to happen
8756 **/
8757bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8758{
8759 bool need_reset = false;
8760
8761 /* Check if Flow Director n-tuple support was enabled or disabled. If
8762 * the state changed, we need to reset.
8763 */
8764 if (features & NETIF_F_NTUPLE) {
8765 /* Enable filters and mark for reset */
8766 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8767 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008768 /* enable FD_SB only if there is MSI-X vector */
8769 if (pf->num_fdsb_msix > 0)
8770 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008771 } else {
8772 /* turn off filters, mark for reset and clear SW filter list */
8773 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8774 need_reset = true;
8775 i40e_fdir_filter_exit(pf);
8776 }
8777 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008778 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008779 /* reset fd counters */
8780 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8781 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008782 /* if ATR was auto disabled it can be re-enabled. */
8783 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008784 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008785 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008786 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8787 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8788 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008789 }
8790 return need_reset;
8791}
8792
8793/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008794 * i40e_clear_rss_lut - clear the rx hash lookup table
8795 * @vsi: the VSI being configured
8796 **/
8797static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8798{
8799 struct i40e_pf *pf = vsi->back;
8800 struct i40e_hw *hw = &pf->hw;
8801 u16 vf_id = vsi->vf_id;
8802 u8 i;
8803
8804 if (vsi->type == I40E_VSI_MAIN) {
8805 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8806 wr32(hw, I40E_PFQF_HLUT(i), 0);
8807 } else if (vsi->type == I40E_VSI_SRIOV) {
8808 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8809 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8810 } else {
8811 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8812 }
8813}
8814
8815/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008816 * i40e_set_features - set the netdev feature flags
8817 * @netdev: ptr to the netdev being adjusted
8818 * @features: the feature set that the stack is suggesting
8819 **/
8820static int i40e_set_features(struct net_device *netdev,
8821 netdev_features_t features)
8822{
8823 struct i40e_netdev_priv *np = netdev_priv(netdev);
8824 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008825 struct i40e_pf *pf = vsi->back;
8826 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008827
Alan Bradyd8ec9862016-07-27 12:02:38 -07008828 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8829 i40e_pf_config_rss(pf);
8830 else if (!(features & NETIF_F_RXHASH) &&
8831 netdev->features & NETIF_F_RXHASH)
8832 i40e_clear_rss_lut(vsi);
8833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008834 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8835 i40e_vlan_stripping_enable(vsi);
8836 else
8837 i40e_vlan_stripping_disable(vsi);
8838
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008839 need_reset = i40e_set_ntuple(pf, features);
8840
8841 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008842 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008843
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008844 return 0;
8845}
8846
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008847/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008848 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008849 * @pf: board private structure
8850 * @port: The UDP port to look up
8851 *
8852 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8853 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008854static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008855{
8856 u8 i;
8857
8858 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008859 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008860 return i;
8861 }
8862
8863 return i;
8864}
8865
8866/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008867 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008868 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008869 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008870 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008871static void i40e_udp_tunnel_add(struct net_device *netdev,
8872 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008873{
8874 struct i40e_netdev_priv *np = netdev_priv(netdev);
8875 struct i40e_vsi *vsi = np->vsi;
8876 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008877 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008878 u8 next_idx;
8879 u8 idx;
8880
Singhai, Anjali6a899022015-12-14 12:21:18 -08008881 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008882
8883 /* Check if port already exists */
8884 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008885 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008886 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008887 return;
8888 }
8889
8890 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008891 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008892
8893 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008894 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008895 ntohs(port));
8896 return;
8897 }
8898
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008899 switch (ti->type) {
8900 case UDP_TUNNEL_TYPE_VXLAN:
8901 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8902 break;
8903 case UDP_TUNNEL_TYPE_GENEVE:
8904 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8905 return;
8906 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8907 break;
8908 default:
8909 return;
8910 }
8911
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008912 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008913 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008914 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8915 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008916}
8917
8918/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008919 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008920 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008921 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008922 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008923static void i40e_udp_tunnel_del(struct net_device *netdev,
8924 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008925{
8926 struct i40e_netdev_priv *np = netdev_priv(netdev);
8927 struct i40e_vsi *vsi = np->vsi;
8928 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008929 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008930 u8 idx;
8931
Singhai, Anjali6a899022015-12-14 12:21:18 -08008932 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008933
8934 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008935 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8936 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008937
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008938 switch (ti->type) {
8939 case UDP_TUNNEL_TYPE_VXLAN:
8940 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8941 goto not_found;
8942 break;
8943 case UDP_TUNNEL_TYPE_GENEVE:
8944 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8945 goto not_found;
8946 break;
8947 default:
8948 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008949 }
8950
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008951 /* if port exists, set it to 0 (mark for deletion)
8952 * and make it pending
8953 */
8954 pf->udp_ports[idx].index = 0;
8955 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008956 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8957
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008958 return;
8959not_found:
8960 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8961 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008962}
8963
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008964static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008965 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008966{
8967 struct i40e_netdev_priv *np = netdev_priv(netdev);
8968 struct i40e_pf *pf = np->vsi->back;
8969 struct i40e_hw *hw = &pf->hw;
8970
8971 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8972 return -EOPNOTSUPP;
8973
8974 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8975 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8976
8977 return 0;
8978}
8979
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008980/**
8981 * i40e_ndo_fdb_add - add an entry to the hardware database
8982 * @ndm: the input from the stack
8983 * @tb: pointer to array of nladdr (unused)
8984 * @dev: the net device pointer
8985 * @addr: the MAC address entry being added
8986 * @flags: instructions from stack about fdb operation
8987 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008988static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8989 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008990 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008991 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008992{
8993 struct i40e_netdev_priv *np = netdev_priv(dev);
8994 struct i40e_pf *pf = np->vsi->back;
8995 int err = 0;
8996
8997 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8998 return -EOPNOTSUPP;
8999
Or Gerlitz65891fe2014-12-14 18:19:05 +02009000 if (vid) {
9001 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9002 return -EINVAL;
9003 }
9004
Greg Rose4ba0dea2014-03-06 08:59:55 +00009005 /* Hardware does not support aging addresses so if a
9006 * ndm_state is given only allow permanent addresses
9007 */
9008 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9009 netdev_info(dev, "FDB only supports static addresses\n");
9010 return -EINVAL;
9011 }
9012
9013 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9014 err = dev_uc_add_excl(dev, addr);
9015 else if (is_multicast_ether_addr(addr))
9016 err = dev_mc_add_excl(dev, addr);
9017 else
9018 err = -EINVAL;
9019
9020 /* Only return duplicate errors if NLM_F_EXCL is set */
9021 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9022 err = 0;
9023
9024 return err;
9025}
9026
Neerav Parikh51616012015-02-06 08:52:14 +00009027/**
9028 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9029 * @dev: the netdev being configured
9030 * @nlh: RTNL message
9031 *
9032 * Inserts a new hardware bridge if not already created and
9033 * enables the bridging mode requested (VEB or VEPA). If the
9034 * hardware bridge has already been inserted and the request
9035 * is to change the mode then that requires a PF reset to
9036 * allow rebuild of the components with required hardware
9037 * bridge mode enabled.
9038 **/
9039static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009040 struct nlmsghdr *nlh,
9041 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009042{
9043 struct i40e_netdev_priv *np = netdev_priv(dev);
9044 struct i40e_vsi *vsi = np->vsi;
9045 struct i40e_pf *pf = vsi->back;
9046 struct i40e_veb *veb = NULL;
9047 struct nlattr *attr, *br_spec;
9048 int i, rem;
9049
9050 /* Only for PF VSI for now */
9051 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9052 return -EOPNOTSUPP;
9053
9054 /* Find the HW bridge for PF VSI */
9055 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9056 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9057 veb = pf->veb[i];
9058 }
9059
9060 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9061
9062 nla_for_each_nested(attr, br_spec, rem) {
9063 __u16 mode;
9064
9065 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9066 continue;
9067
9068 mode = nla_get_u16(attr);
9069 if ((mode != BRIDGE_MODE_VEPA) &&
9070 (mode != BRIDGE_MODE_VEB))
9071 return -EINVAL;
9072
9073 /* Insert a new HW bridge */
9074 if (!veb) {
9075 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9076 vsi->tc_config.enabled_tc);
9077 if (veb) {
9078 veb->bridge_mode = mode;
9079 i40e_config_bridge_mode(veb);
9080 } else {
9081 /* No Bridge HW offload available */
9082 return -ENOENT;
9083 }
9084 break;
9085 } else if (mode != veb->bridge_mode) {
9086 /* Existing HW bridge but different mode needs reset */
9087 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009088 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9089 if (mode == BRIDGE_MODE_VEB)
9090 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9091 else
9092 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9093 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009094 break;
9095 }
9096 }
9097
9098 return 0;
9099}
9100
9101/**
9102 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9103 * @skb: skb buff
9104 * @pid: process id
9105 * @seq: RTNL message seq #
9106 * @dev: the netdev being configured
9107 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009108 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009109 *
9110 * Return the mode in which the hardware bridge is operating in
9111 * i.e VEB or VEPA.
9112 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009113static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9114 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009115 u32 __always_unused filter_mask,
9116 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009117{
9118 struct i40e_netdev_priv *np = netdev_priv(dev);
9119 struct i40e_vsi *vsi = np->vsi;
9120 struct i40e_pf *pf = vsi->back;
9121 struct i40e_veb *veb = NULL;
9122 int i;
9123
9124 /* Only for PF VSI for now */
9125 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9126 return -EOPNOTSUPP;
9127
9128 /* Find the HW bridge for the PF VSI */
9129 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9130 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9131 veb = pf->veb[i];
9132 }
9133
9134 if (!veb)
9135 return 0;
9136
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009137 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009138 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009139}
Neerav Parikh51616012015-02-06 08:52:14 +00009140
Joe Stringerf44a75e2015-04-14 17:09:14 -07009141/**
9142 * i40e_features_check - Validate encapsulated packet conforms to limits
9143 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009144 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009145 * @features: Offload features that the stack believes apply
9146 **/
9147static netdev_features_t i40e_features_check(struct sk_buff *skb,
9148 struct net_device *dev,
9149 netdev_features_t features)
9150{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009151 size_t len;
9152
9153 /* No point in doing any of this if neither checksum nor GSO are
9154 * being requested for this frame. We can rule out both by just
9155 * checking for CHECKSUM_PARTIAL
9156 */
9157 if (skb->ip_summed != CHECKSUM_PARTIAL)
9158 return features;
9159
9160 /* We cannot support GSO if the MSS is going to be less than
9161 * 64 bytes. If it is then we need to drop support for GSO.
9162 */
9163 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9164 features &= ~NETIF_F_GSO_MASK;
9165
9166 /* MACLEN can support at most 63 words */
9167 len = skb_network_header(skb) - skb->data;
9168 if (len & ~(63 * 2))
9169 goto out_err;
9170
9171 /* IPLEN and EIPLEN can support at most 127 dwords */
9172 len = skb_transport_header(skb) - skb_network_header(skb);
9173 if (len & ~(127 * 4))
9174 goto out_err;
9175
9176 if (skb->encapsulation) {
9177 /* L4TUNLEN can support 127 words */
9178 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9179 if (len & ~(127 * 2))
9180 goto out_err;
9181
9182 /* IPLEN can support at most 127 dwords */
9183 len = skb_inner_transport_header(skb) -
9184 skb_inner_network_header(skb);
9185 if (len & ~(127 * 4))
9186 goto out_err;
9187 }
9188
9189 /* No need to validate L4LEN as TCP is the only protocol with a
9190 * a flexible value and we support all possible values supported
9191 * by TCP, which is at most 15 dwords
9192 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009193
9194 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009195out_err:
9196 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009197}
9198
Shannon Nelson37a29732015-02-27 09:15:19 +00009199static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009200 .ndo_open = i40e_open,
9201 .ndo_stop = i40e_close,
9202 .ndo_start_xmit = i40e_lan_xmit_frame,
9203 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9204 .ndo_set_rx_mode = i40e_set_rx_mode,
9205 .ndo_validate_addr = eth_validate_addr,
9206 .ndo_set_mac_address = i40e_set_mac,
9207 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009208 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009209 .ndo_tx_timeout = i40e_tx_timeout,
9210 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9211 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9212#ifdef CONFIG_NET_POLL_CONTROLLER
9213 .ndo_poll_controller = i40e_netpoll,
9214#endif
John Fastabende4c67342016-02-16 21:16:15 -08009215 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009216#ifdef I40E_FCOE
9217 .ndo_fcoe_enable = i40e_fcoe_enable,
9218 .ndo_fcoe_disable = i40e_fcoe_disable,
9219#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009220 .ndo_set_features = i40e_set_features,
9221 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9222 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009223 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009224 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009225 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009226 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009227 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009228 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9229 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009230 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009231 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009232 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009233 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9234 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009235};
9236
9237/**
9238 * i40e_config_netdev - Setup the netdev flags
9239 * @vsi: the VSI being configured
9240 *
9241 * Returns 0 on success, negative value on failure
9242 **/
9243static int i40e_config_netdev(struct i40e_vsi *vsi)
9244{
9245 struct i40e_pf *pf = vsi->back;
9246 struct i40e_hw *hw = &pf->hw;
9247 struct i40e_netdev_priv *np;
9248 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009249 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009250 u8 mac_addr[ETH_ALEN];
9251 int etherdev_size;
9252
9253 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009254 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009255 if (!netdev)
9256 return -ENOMEM;
9257
9258 vsi->netdev = netdev;
9259 np = netdev_priv(netdev);
9260 np->vsi = vsi;
9261
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009262 netdev->hw_enc_features |= NETIF_F_SG |
9263 NETIF_F_IP_CSUM |
9264 NETIF_F_IPV6_CSUM |
9265 NETIF_F_HIGHDMA |
9266 NETIF_F_SOFT_FEATURES |
9267 NETIF_F_TSO |
9268 NETIF_F_TSO_ECN |
9269 NETIF_F_TSO6 |
9270 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009271 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009272 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009273 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009274 NETIF_F_GSO_UDP_TUNNEL |
9275 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009276 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009277 NETIF_F_SCTP_CRC |
9278 NETIF_F_RXHASH |
9279 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009280 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009281
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009282 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009283 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9284
9285 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009286
9287 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009288 netdev->vlan_features |= netdev->hw_enc_features |
9289 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009290
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009291 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009292 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009293
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009294 netdev->hw_features |= netdev->hw_enc_features |
9295 NETIF_F_HW_VLAN_CTAG_TX |
9296 NETIF_F_HW_VLAN_CTAG_RX;
9297
9298 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009299 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009300
9301 if (vsi->type == I40E_VSI_MAIN) {
9302 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009303 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009304 /* The following steps are necessary to prevent reception
9305 * of tagged packets - some older NVM configurations load a
9306 * default a MAC-VLAN filter that accepts any tagged packet
9307 * which must be replaced by a normal filter.
9308 */
9309 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009310 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009311 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009312 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009313 } else {
9314 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9315 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9316 pf->vsi[pf->lan_vsi]->netdev->name);
9317 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009318
Jacob Keller278e7d02016-10-05 09:30:37 -07009319 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009320 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009321 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009322 }
Kiran Patil21659032015-09-30 14:09:03 -04009323
Jacob Keller435c0842016-11-08 13:05:10 -08009324 /* Add the broadcast filter so that we initially will receive
9325 * broadcast packets. Note that when a new VLAN is first added the
9326 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9327 * specific filters as part of transitioning into "vlan" operation.
9328 * When more VLANs are added, the driver will copy each existing MAC
9329 * filter and add it for the new VLAN.
9330 *
9331 * Broadcast filters are handled specially by
9332 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9333 * promiscuous bit instead of adding this directly as a MAC/VLAN
9334 * filter. The subtask will update the correct broadcast promiscuous
9335 * bits as VLANs become active or inactive.
9336 */
9337 eth_broadcast_addr(broadcast);
9338 spin_lock_bh(&vsi->mac_filter_hash_lock);
9339 i40e_add_filter(vsi, broadcast, I40E_VLAN_ANY);
9340 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9341
Greg Rose9a173902014-05-22 06:32:02 +00009342 ether_addr_copy(netdev->dev_addr, mac_addr);
9343 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009344
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009345 netdev->priv_flags |= IFF_UNICAST_FLT;
9346 netdev->priv_flags |= IFF_SUPP_NOFCS;
9347 /* Setup netdev TC information */
9348 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9349
9350 netdev->netdev_ops = &i40e_netdev_ops;
9351 netdev->watchdog_timeo = 5 * HZ;
9352 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009353#ifdef I40E_FCOE
9354 i40e_fcoe_config_netdev(netdev, vsi);
9355#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009356
Jarod Wilson91c527a2016-10-17 15:54:05 -04009357 /* MTU range: 68 - 9706 */
9358 netdev->min_mtu = ETH_MIN_MTU;
9359 netdev->max_mtu = I40E_MAX_RXBUFFER -
9360 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9361
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009362 return 0;
9363}
9364
9365/**
9366 * i40e_vsi_delete - Delete a VSI from the switch
9367 * @vsi: the VSI being removed
9368 *
9369 * Returns 0 on success, negative value on failure
9370 **/
9371static void i40e_vsi_delete(struct i40e_vsi *vsi)
9372{
9373 /* remove default VSI is not allowed */
9374 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9375 return;
9376
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009377 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009378}
9379
9380/**
Neerav Parikh51616012015-02-06 08:52:14 +00009381 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9382 * @vsi: the VSI being queried
9383 *
9384 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9385 **/
9386int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9387{
9388 struct i40e_veb *veb;
9389 struct i40e_pf *pf = vsi->back;
9390
9391 /* Uplink is not a bridge so default to VEB */
9392 if (vsi->veb_idx == I40E_NO_VEB)
9393 return 1;
9394
9395 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009396 if (!veb) {
9397 dev_info(&pf->pdev->dev,
9398 "There is no veb associated with the bridge\n");
9399 return -ENOENT;
9400 }
Neerav Parikh51616012015-02-06 08:52:14 +00009401
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009402 /* Uplink is a bridge in VEPA mode */
9403 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9404 return 0;
9405 } else {
9406 /* Uplink is a bridge in VEB mode */
9407 return 1;
9408 }
9409
9410 /* VEPA is now default bridge, so return 0 */
9411 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009412}
9413
9414/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009415 * i40e_add_vsi - Add a VSI to the switch
9416 * @vsi: the VSI being configured
9417 *
9418 * This initializes a VSI context depending on the VSI type to be added and
9419 * passes it down to the add_vsi aq command.
9420 **/
9421static int i40e_add_vsi(struct i40e_vsi *vsi)
9422{
9423 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009424 struct i40e_pf *pf = vsi->back;
9425 struct i40e_hw *hw = &pf->hw;
9426 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009427 struct i40e_mac_filter *f;
9428 struct hlist_node *h;
9429 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009430
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009431 u8 enabled_tc = 0x1; /* TC0 enabled */
9432 int f_count = 0;
9433
9434 memset(&ctxt, 0, sizeof(ctxt));
9435 switch (vsi->type) {
9436 case I40E_VSI_MAIN:
9437 /* The PF's main VSI is already setup as part of the
9438 * device initialization, so we'll not bother with
9439 * the add_vsi call, but we will retrieve the current
9440 * VSI context.
9441 */
9442 ctxt.seid = pf->main_vsi_seid;
9443 ctxt.pf_num = pf->hw.pf_id;
9444 ctxt.vf_num = 0;
9445 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9446 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9447 if (ret) {
9448 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009449 "couldn't get PF vsi config, err %s aq_err %s\n",
9450 i40e_stat_str(&pf->hw, ret),
9451 i40e_aq_str(&pf->hw,
9452 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009453 return -ENOENT;
9454 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009455 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009456 vsi->info.valid_sections = 0;
9457
9458 vsi->seid = ctxt.seid;
9459 vsi->id = ctxt.vsi_number;
9460
9461 enabled_tc = i40e_pf_get_tc_map(pf);
9462
9463 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009464 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9465 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009466 memset(&ctxt, 0, sizeof(ctxt));
9467 ctxt.seid = pf->main_vsi_seid;
9468 ctxt.pf_num = pf->hw.pf_id;
9469 ctxt.vf_num = 0;
9470 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9471 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9472 if (ret) {
9473 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009474 "update vsi failed, err %s aq_err %s\n",
9475 i40e_stat_str(&pf->hw, ret),
9476 i40e_aq_str(&pf->hw,
9477 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009478 ret = -ENOENT;
9479 goto err;
9480 }
9481 /* update the local VSI info queue map */
9482 i40e_vsi_update_queue_map(vsi, &ctxt);
9483 vsi->info.valid_sections = 0;
9484 } else {
9485 /* Default/Main VSI is only enabled for TC0
9486 * reconfigure it to enable all TCs that are
9487 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009488 * For MFP case the iSCSI PF would use this
9489 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009490 */
9491 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9492 if (ret) {
9493 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009494 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9495 enabled_tc,
9496 i40e_stat_str(&pf->hw, ret),
9497 i40e_aq_str(&pf->hw,
9498 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009499 ret = -ENOENT;
9500 }
9501 }
9502 break;
9503
9504 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009505 ctxt.pf_num = hw->pf_id;
9506 ctxt.vf_num = 0;
9507 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009508 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009509 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009510 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9511 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009512 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009513 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009514 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009515 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009516 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009517 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009518 break;
9519
9520 case I40E_VSI_VMDQ2:
9521 ctxt.pf_num = hw->pf_id;
9522 ctxt.vf_num = 0;
9523 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009524 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009525 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009527 /* This VSI is connected to VEB so the switch_id
9528 * should be set to zero by default.
9529 */
Neerav Parikh51616012015-02-06 08:52:14 +00009530 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9531 ctxt.info.valid_sections |=
9532 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9533 ctxt.info.switch_id =
9534 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9535 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009536
9537 /* Setup the VSI tx/rx queue map for TC0 only for now */
9538 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9539 break;
9540
9541 case I40E_VSI_SRIOV:
9542 ctxt.pf_num = hw->pf_id;
9543 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9544 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009545 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009546 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9547
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009548 /* This VSI is connected to VEB so the switch_id
9549 * should be set to zero by default.
9550 */
Neerav Parikh51616012015-02-06 08:52:14 +00009551 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9552 ctxt.info.valid_sections |=
9553 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9554 ctxt.info.switch_id =
9555 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9556 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009557
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009558 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9559 ctxt.info.valid_sections |=
9560 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9561 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009562 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9563 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009564 }
9565
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009566 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9567 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009568 if (pf->vf[vsi->vf_id].spoofchk) {
9569 ctxt.info.valid_sections |=
9570 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9571 ctxt.info.sec_flags |=
9572 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9573 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9574 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009575 /* Setup the VSI tx/rx queue map for TC0 only for now */
9576 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9577 break;
9578
Vasu Dev38e00432014-08-01 13:27:03 -07009579#ifdef I40E_FCOE
9580 case I40E_VSI_FCOE:
9581 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9582 if (ret) {
9583 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9584 return ret;
9585 }
9586 break;
9587
9588#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009589 case I40E_VSI_IWARP:
9590 /* send down message to iWARP */
9591 break;
9592
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009593 default:
9594 return -ENODEV;
9595 }
9596
9597 if (vsi->type != I40E_VSI_MAIN) {
9598 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9599 if (ret) {
9600 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009601 "add vsi failed, err %s aq_err %s\n",
9602 i40e_stat_str(&pf->hw, ret),
9603 i40e_aq_str(&pf->hw,
9604 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009605 ret = -ENOENT;
9606 goto err;
9607 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009608 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009609 vsi->info.valid_sections = 0;
9610 vsi->seid = ctxt.seid;
9611 vsi->id = ctxt.vsi_number;
9612 }
9613
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009614 vsi->active_filters = 0;
9615 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009616 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009618 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009619 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009620 f_count++;
9621 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009622 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009624 if (f_count) {
9625 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9626 pf->flags |= I40E_FLAG_FILTER_SYNC;
9627 }
9628
9629 /* Update VSI BW information */
9630 ret = i40e_vsi_get_bw_info(vsi);
9631 if (ret) {
9632 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009633 "couldn't get vsi bw info, err %s aq_err %s\n",
9634 i40e_stat_str(&pf->hw, ret),
9635 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009636 /* VSI is already added so not tearing that up */
9637 ret = 0;
9638 }
9639
9640err:
9641 return ret;
9642}
9643
9644/**
9645 * i40e_vsi_release - Delete a VSI and free its resources
9646 * @vsi: the VSI being removed
9647 *
9648 * Returns 0 on success or < 0 on error
9649 **/
9650int i40e_vsi_release(struct i40e_vsi *vsi)
9651{
Jacob Keller278e7d02016-10-05 09:30:37 -07009652 struct i40e_mac_filter *f;
9653 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009654 struct i40e_veb *veb = NULL;
9655 struct i40e_pf *pf;
9656 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009657 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009658
9659 pf = vsi->back;
9660
9661 /* release of a VEB-owner or last VSI is not allowed */
9662 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9663 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9664 vsi->seid, vsi->uplink_seid);
9665 return -ENODEV;
9666 }
9667 if (vsi == pf->vsi[pf->lan_vsi] &&
9668 !test_bit(__I40E_DOWN, &pf->state)) {
9669 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9670 return -ENODEV;
9671 }
9672
9673 uplink_seid = vsi->uplink_seid;
9674 if (vsi->type != I40E_VSI_SRIOV) {
9675 if (vsi->netdev_registered) {
9676 vsi->netdev_registered = false;
9677 if (vsi->netdev) {
9678 /* results in a call to i40e_close() */
9679 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009680 }
9681 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009682 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009683 }
9684 i40e_vsi_disable_irq(vsi);
9685 }
9686
Jacob Keller278e7d02016-10-05 09:30:37 -07009687 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009688
9689 /* clear the sync flag on all filters */
9690 if (vsi->netdev) {
9691 __dev_uc_unsync(vsi->netdev, NULL);
9692 __dev_mc_unsync(vsi->netdev, NULL);
9693 }
9694
9695 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009696 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009697 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009698
Jacob Keller278e7d02016-10-05 09:30:37 -07009699 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009700
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009701 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009702
9703 i40e_vsi_delete(vsi);
9704 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009705 if (vsi->netdev) {
9706 free_netdev(vsi->netdev);
9707 vsi->netdev = NULL;
9708 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009709 i40e_vsi_clear_rings(vsi);
9710 i40e_vsi_clear(vsi);
9711
9712 /* If this was the last thing on the VEB, except for the
9713 * controlling VSI, remove the VEB, which puts the controlling
9714 * VSI onto the next level down in the switch.
9715 *
9716 * Well, okay, there's one more exception here: don't remove
9717 * the orphan VEBs yet. We'll wait for an explicit remove request
9718 * from up the network stack.
9719 */
Mitch Williams505682c2014-05-20 08:01:37 +00009720 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009721 if (pf->vsi[i] &&
9722 pf->vsi[i]->uplink_seid == uplink_seid &&
9723 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9724 n++; /* count the VSIs */
9725 }
9726 }
9727 for (i = 0; i < I40E_MAX_VEB; i++) {
9728 if (!pf->veb[i])
9729 continue;
9730 if (pf->veb[i]->uplink_seid == uplink_seid)
9731 n++; /* count the VEBs */
9732 if (pf->veb[i]->seid == uplink_seid)
9733 veb = pf->veb[i];
9734 }
9735 if (n == 0 && veb && veb->uplink_seid != 0)
9736 i40e_veb_release(veb);
9737
9738 return 0;
9739}
9740
9741/**
9742 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9743 * @vsi: ptr to the VSI
9744 *
9745 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9746 * corresponding SW VSI structure and initializes num_queue_pairs for the
9747 * newly allocated VSI.
9748 *
9749 * Returns 0 on success or negative on failure
9750 **/
9751static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9752{
9753 int ret = -ENOENT;
9754 struct i40e_pf *pf = vsi->back;
9755
Alexander Duyck493fb302013-09-28 07:01:44 +00009756 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009757 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9758 vsi->seid);
9759 return -EEXIST;
9760 }
9761
9762 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009763 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009764 vsi->seid, vsi->base_vector);
9765 return -EEXIST;
9766 }
9767
Greg Rose90e04072014-03-06 08:59:57 +00009768 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009769 if (ret) {
9770 dev_info(&pf->pdev->dev,
9771 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9772 vsi->num_q_vectors, vsi->seid, ret);
9773 vsi->num_q_vectors = 0;
9774 goto vector_setup_out;
9775 }
9776
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009777 /* In Legacy mode, we do not have to get any other vector since we
9778 * piggyback on the misc/ICR0 for queue interrupts.
9779 */
9780 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9781 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009782 if (vsi->num_q_vectors)
9783 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9784 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009785 if (vsi->base_vector < 0) {
9786 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009787 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9788 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009789 i40e_vsi_free_q_vectors(vsi);
9790 ret = -ENOENT;
9791 goto vector_setup_out;
9792 }
9793
9794vector_setup_out:
9795 return ret;
9796}
9797
9798/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009799 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9800 * @vsi: pointer to the vsi.
9801 *
9802 * This re-allocates a vsi's queue resources.
9803 *
9804 * Returns pointer to the successfully allocated and configured VSI sw struct
9805 * on success, otherwise returns NULL on failure.
9806 **/
9807static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9808{
John Underwoodf5340392016-02-18 09:19:24 -08009809 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009810 u8 enabled_tc;
9811 int ret;
9812
John Underwoodf5340392016-02-18 09:19:24 -08009813 if (!vsi)
9814 return NULL;
9815
9816 pf = vsi->back;
9817
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009818 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9819 i40e_vsi_clear_rings(vsi);
9820
9821 i40e_vsi_free_arrays(vsi, false);
9822 i40e_set_num_rings_in_vsi(vsi);
9823 ret = i40e_vsi_alloc_arrays(vsi, false);
9824 if (ret)
9825 goto err_vsi;
9826
9827 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9828 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009829 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009830 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009831 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009832 goto err_vsi;
9833 }
9834 vsi->base_queue = ret;
9835
9836 /* Update the FW view of the VSI. Force a reset of TC and queue
9837 * layout configurations.
9838 */
9839 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9840 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9841 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9842 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009843 if (vsi->type == I40E_VSI_MAIN)
9844 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009845
9846 /* assign it some queues */
9847 ret = i40e_alloc_rings(vsi);
9848 if (ret)
9849 goto err_rings;
9850
9851 /* map all of the rings to the q_vectors */
9852 i40e_vsi_map_rings_to_vectors(vsi);
9853 return vsi;
9854
9855err_rings:
9856 i40e_vsi_free_q_vectors(vsi);
9857 if (vsi->netdev_registered) {
9858 vsi->netdev_registered = false;
9859 unregister_netdev(vsi->netdev);
9860 free_netdev(vsi->netdev);
9861 vsi->netdev = NULL;
9862 }
9863 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9864err_vsi:
9865 i40e_vsi_clear(vsi);
9866 return NULL;
9867}
9868
9869/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009870 * i40e_vsi_setup - Set up a VSI by a given type
9871 * @pf: board private structure
9872 * @type: VSI type
9873 * @uplink_seid: the switch element to link to
9874 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9875 *
9876 * This allocates the sw VSI structure and its queue resources, then add a VSI
9877 * to the identified VEB.
9878 *
9879 * Returns pointer to the successfully allocated and configure VSI sw struct on
9880 * success, otherwise returns NULL on failure.
9881 **/
9882struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9883 u16 uplink_seid, u32 param1)
9884{
9885 struct i40e_vsi *vsi = NULL;
9886 struct i40e_veb *veb = NULL;
9887 int ret, i;
9888 int v_idx;
9889
9890 /* The requested uplink_seid must be either
9891 * - the PF's port seid
9892 * no VEB is needed because this is the PF
9893 * or this is a Flow Director special case VSI
9894 * - seid of an existing VEB
9895 * - seid of a VSI that owns an existing VEB
9896 * - seid of a VSI that doesn't own a VEB
9897 * a new VEB is created and the VSI becomes the owner
9898 * - seid of the PF VSI, which is what creates the first VEB
9899 * this is a special case of the previous
9900 *
9901 * Find which uplink_seid we were given and create a new VEB if needed
9902 */
9903 for (i = 0; i < I40E_MAX_VEB; i++) {
9904 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9905 veb = pf->veb[i];
9906 break;
9907 }
9908 }
9909
9910 if (!veb && uplink_seid != pf->mac_seid) {
9911
Mitch Williams505682c2014-05-20 08:01:37 +00009912 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009913 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9914 vsi = pf->vsi[i];
9915 break;
9916 }
9917 }
9918 if (!vsi) {
9919 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9920 uplink_seid);
9921 return NULL;
9922 }
9923
9924 if (vsi->uplink_seid == pf->mac_seid)
9925 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9926 vsi->tc_config.enabled_tc);
9927 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9928 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9929 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009930 if (veb) {
9931 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9932 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009933 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009934 return NULL;
9935 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009936 /* We come up by default in VEPA mode if SRIOV is not
9937 * already enabled, in which case we can't force VEPA
9938 * mode.
9939 */
9940 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9941 veb->bridge_mode = BRIDGE_MODE_VEPA;
9942 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9943 }
Neerav Parikh51616012015-02-06 08:52:14 +00009944 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009945 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009946 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9947 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9948 veb = pf->veb[i];
9949 }
9950 if (!veb) {
9951 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9952 return NULL;
9953 }
9954
9955 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9956 uplink_seid = veb->seid;
9957 }
9958
9959 /* get vsi sw struct */
9960 v_idx = i40e_vsi_mem_alloc(pf, type);
9961 if (v_idx < 0)
9962 goto err_alloc;
9963 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009964 if (!vsi)
9965 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009966 vsi->type = type;
9967 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9968
9969 if (type == I40E_VSI_MAIN)
9970 pf->lan_vsi = v_idx;
9971 else if (type == I40E_VSI_SRIOV)
9972 vsi->vf_id = param1;
9973 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009974 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9975 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009976 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009977 dev_info(&pf->pdev->dev,
9978 "failed to get tracking for %d queues for VSI %d err=%d\n",
9979 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009980 goto err_vsi;
9981 }
9982 vsi->base_queue = ret;
9983
9984 /* get a VSI from the hardware */
9985 vsi->uplink_seid = uplink_seid;
9986 ret = i40e_add_vsi(vsi);
9987 if (ret)
9988 goto err_vsi;
9989
9990 switch (vsi->type) {
9991 /* setup the netdev if needed */
9992 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009993 /* Apply relevant filters if a platform-specific mac
9994 * address was selected.
9995 */
9996 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9997 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9998 if (ret) {
9999 dev_warn(&pf->pdev->dev,
10000 "could not set up macaddr; err %d\n",
10001 ret);
10002 }
10003 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010004 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -070010005 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010006 ret = i40e_config_netdev(vsi);
10007 if (ret)
10008 goto err_netdev;
10009 ret = register_netdev(vsi->netdev);
10010 if (ret)
10011 goto err_netdev;
10012 vsi->netdev_registered = true;
10013 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010014#ifdef CONFIG_I40E_DCB
10015 /* Setup DCB netlink interface */
10016 i40e_dcbnl_setup(vsi);
10017#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010018 /* fall through */
10019
10020 case I40E_VSI_FDIR:
10021 /* set up vectors and rings if needed */
10022 ret = i40e_vsi_setup_vectors(vsi);
10023 if (ret)
10024 goto err_msix;
10025
10026 ret = i40e_alloc_rings(vsi);
10027 if (ret)
10028 goto err_rings;
10029
10030 /* map all of the rings to the q_vectors */
10031 i40e_vsi_map_rings_to_vectors(vsi);
10032
10033 i40e_vsi_reset_stats(vsi);
10034 break;
10035
10036 default:
10037 /* no netdev or rings for the other VSI types */
10038 break;
10039 }
10040
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010041 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10042 (vsi->type == I40E_VSI_VMDQ2)) {
10043 ret = i40e_vsi_config_rss(vsi);
10044 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010045 return vsi;
10046
10047err_rings:
10048 i40e_vsi_free_q_vectors(vsi);
10049err_msix:
10050 if (vsi->netdev_registered) {
10051 vsi->netdev_registered = false;
10052 unregister_netdev(vsi->netdev);
10053 free_netdev(vsi->netdev);
10054 vsi->netdev = NULL;
10055 }
10056err_netdev:
10057 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10058err_vsi:
10059 i40e_vsi_clear(vsi);
10060err_alloc:
10061 return NULL;
10062}
10063
10064/**
10065 * i40e_veb_get_bw_info - Query VEB BW information
10066 * @veb: the veb to query
10067 *
10068 * Query the Tx scheduler BW configuration data for given VEB
10069 **/
10070static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10071{
10072 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10073 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10074 struct i40e_pf *pf = veb->pf;
10075 struct i40e_hw *hw = &pf->hw;
10076 u32 tc_bw_max;
10077 int ret = 0;
10078 int i;
10079
10080 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10081 &bw_data, NULL);
10082 if (ret) {
10083 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010084 "query veb bw config failed, err %s aq_err %s\n",
10085 i40e_stat_str(&pf->hw, ret),
10086 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010087 goto out;
10088 }
10089
10090 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10091 &ets_data, NULL);
10092 if (ret) {
10093 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010094 "query veb bw ets config failed, err %s aq_err %s\n",
10095 i40e_stat_str(&pf->hw, ret),
10096 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010097 goto out;
10098 }
10099
10100 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10101 veb->bw_max_quanta = ets_data.tc_bw_max;
10102 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010103 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010104 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10105 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10106 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10107 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10108 veb->bw_tc_limit_credits[i] =
10109 le16_to_cpu(bw_data.tc_bw_limits[i]);
10110 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10111 }
10112
10113out:
10114 return ret;
10115}
10116
10117/**
10118 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10119 * @pf: board private structure
10120 *
10121 * On error: returns error code (negative)
10122 * On success: returns vsi index in PF (positive)
10123 **/
10124static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10125{
10126 int ret = -ENOENT;
10127 struct i40e_veb *veb;
10128 int i;
10129
10130 /* Need to protect the allocation of switch elements at the PF level */
10131 mutex_lock(&pf->switch_mutex);
10132
10133 /* VEB list may be fragmented if VEB creation/destruction has
10134 * been happening. We can afford to do a quick scan to look
10135 * for any free slots in the list.
10136 *
10137 * find next empty veb slot, looping back around if necessary
10138 */
10139 i = 0;
10140 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10141 i++;
10142 if (i >= I40E_MAX_VEB) {
10143 ret = -ENOMEM;
10144 goto err_alloc_veb; /* out of VEB slots! */
10145 }
10146
10147 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10148 if (!veb) {
10149 ret = -ENOMEM;
10150 goto err_alloc_veb;
10151 }
10152 veb->pf = pf;
10153 veb->idx = i;
10154 veb->enabled_tc = 1;
10155
10156 pf->veb[i] = veb;
10157 ret = i;
10158err_alloc_veb:
10159 mutex_unlock(&pf->switch_mutex);
10160 return ret;
10161}
10162
10163/**
10164 * i40e_switch_branch_release - Delete a branch of the switch tree
10165 * @branch: where to start deleting
10166 *
10167 * This uses recursion to find the tips of the branch to be
10168 * removed, deleting until we get back to and can delete this VEB.
10169 **/
10170static void i40e_switch_branch_release(struct i40e_veb *branch)
10171{
10172 struct i40e_pf *pf = branch->pf;
10173 u16 branch_seid = branch->seid;
10174 u16 veb_idx = branch->idx;
10175 int i;
10176
10177 /* release any VEBs on this VEB - RECURSION */
10178 for (i = 0; i < I40E_MAX_VEB; i++) {
10179 if (!pf->veb[i])
10180 continue;
10181 if (pf->veb[i]->uplink_seid == branch->seid)
10182 i40e_switch_branch_release(pf->veb[i]);
10183 }
10184
10185 /* Release the VSIs on this VEB, but not the owner VSI.
10186 *
10187 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10188 * the VEB itself, so don't use (*branch) after this loop.
10189 */
Mitch Williams505682c2014-05-20 08:01:37 +000010190 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010191 if (!pf->vsi[i])
10192 continue;
10193 if (pf->vsi[i]->uplink_seid == branch_seid &&
10194 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10195 i40e_vsi_release(pf->vsi[i]);
10196 }
10197 }
10198
10199 /* There's one corner case where the VEB might not have been
10200 * removed, so double check it here and remove it if needed.
10201 * This case happens if the veb was created from the debugfs
10202 * commands and no VSIs were added to it.
10203 */
10204 if (pf->veb[veb_idx])
10205 i40e_veb_release(pf->veb[veb_idx]);
10206}
10207
10208/**
10209 * i40e_veb_clear - remove veb struct
10210 * @veb: the veb to remove
10211 **/
10212static void i40e_veb_clear(struct i40e_veb *veb)
10213{
10214 if (!veb)
10215 return;
10216
10217 if (veb->pf) {
10218 struct i40e_pf *pf = veb->pf;
10219
10220 mutex_lock(&pf->switch_mutex);
10221 if (pf->veb[veb->idx] == veb)
10222 pf->veb[veb->idx] = NULL;
10223 mutex_unlock(&pf->switch_mutex);
10224 }
10225
10226 kfree(veb);
10227}
10228
10229/**
10230 * i40e_veb_release - Delete a VEB and free its resources
10231 * @veb: the VEB being removed
10232 **/
10233void i40e_veb_release(struct i40e_veb *veb)
10234{
10235 struct i40e_vsi *vsi = NULL;
10236 struct i40e_pf *pf;
10237 int i, n = 0;
10238
10239 pf = veb->pf;
10240
10241 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010242 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010243 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10244 n++;
10245 vsi = pf->vsi[i];
10246 }
10247 }
10248 if (n != 1) {
10249 dev_info(&pf->pdev->dev,
10250 "can't remove VEB %d with %d VSIs left\n",
10251 veb->seid, n);
10252 return;
10253 }
10254
10255 /* move the remaining VSI to uplink veb */
10256 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10257 if (veb->uplink_seid) {
10258 vsi->uplink_seid = veb->uplink_seid;
10259 if (veb->uplink_seid == pf->mac_seid)
10260 vsi->veb_idx = I40E_NO_VEB;
10261 else
10262 vsi->veb_idx = veb->veb_idx;
10263 } else {
10264 /* floating VEB */
10265 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10266 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10267 }
10268
10269 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10270 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010271}
10272
10273/**
10274 * i40e_add_veb - create the VEB in the switch
10275 * @veb: the VEB to be instantiated
10276 * @vsi: the controlling VSI
10277 **/
10278static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10279{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010280 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010281 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010282 int ret;
10283
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010284 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010285 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010286 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010287
10288 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010289 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010290 dev_info(&pf->pdev->dev,
10291 "couldn't add VEB, err %s aq_err %s\n",
10292 i40e_stat_str(&pf->hw, ret),
10293 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010294 return -EPERM;
10295 }
10296
10297 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010298 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010299 &veb->stats_idx, NULL, NULL, NULL);
10300 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010301 dev_info(&pf->pdev->dev,
10302 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10303 i40e_stat_str(&pf->hw, ret),
10304 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010305 return -EPERM;
10306 }
10307 ret = i40e_veb_get_bw_info(veb);
10308 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010309 dev_info(&pf->pdev->dev,
10310 "couldn't get VEB bw info, err %s aq_err %s\n",
10311 i40e_stat_str(&pf->hw, ret),
10312 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10313 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010314 return -ENOENT;
10315 }
10316
10317 vsi->uplink_seid = veb->seid;
10318 vsi->veb_idx = veb->idx;
10319 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10320
10321 return 0;
10322}
10323
10324/**
10325 * i40e_veb_setup - Set up a VEB
10326 * @pf: board private structure
10327 * @flags: VEB setup flags
10328 * @uplink_seid: the switch element to link to
10329 * @vsi_seid: the initial VSI seid
10330 * @enabled_tc: Enabled TC bit-map
10331 *
10332 * This allocates the sw VEB structure and links it into the switch
10333 * It is possible and legal for this to be a duplicate of an already
10334 * existing VEB. It is also possible for both uplink and vsi seids
10335 * to be zero, in order to create a floating VEB.
10336 *
10337 * Returns pointer to the successfully allocated VEB sw struct on
10338 * success, otherwise returns NULL on failure.
10339 **/
10340struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10341 u16 uplink_seid, u16 vsi_seid,
10342 u8 enabled_tc)
10343{
10344 struct i40e_veb *veb, *uplink_veb = NULL;
10345 int vsi_idx, veb_idx;
10346 int ret;
10347
10348 /* if one seid is 0, the other must be 0 to create a floating relay */
10349 if ((uplink_seid == 0 || vsi_seid == 0) &&
10350 (uplink_seid + vsi_seid != 0)) {
10351 dev_info(&pf->pdev->dev,
10352 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10353 uplink_seid, vsi_seid);
10354 return NULL;
10355 }
10356
10357 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010358 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010359 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10360 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010361 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010362 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10363 vsi_seid);
10364 return NULL;
10365 }
10366
10367 if (uplink_seid && uplink_seid != pf->mac_seid) {
10368 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10369 if (pf->veb[veb_idx] &&
10370 pf->veb[veb_idx]->seid == uplink_seid) {
10371 uplink_veb = pf->veb[veb_idx];
10372 break;
10373 }
10374 }
10375 if (!uplink_veb) {
10376 dev_info(&pf->pdev->dev,
10377 "uplink seid %d not found\n", uplink_seid);
10378 return NULL;
10379 }
10380 }
10381
10382 /* get veb sw struct */
10383 veb_idx = i40e_veb_mem_alloc(pf);
10384 if (veb_idx < 0)
10385 goto err_alloc;
10386 veb = pf->veb[veb_idx];
10387 veb->flags = flags;
10388 veb->uplink_seid = uplink_seid;
10389 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10390 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10391
10392 /* create the VEB in the switch */
10393 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10394 if (ret)
10395 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010396 if (vsi_idx == pf->lan_vsi)
10397 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010398
10399 return veb;
10400
10401err_veb:
10402 i40e_veb_clear(veb);
10403err_alloc:
10404 return NULL;
10405}
10406
10407/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010408 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010409 * @pf: board private structure
10410 * @ele: element we are building info from
10411 * @num_reported: total number of elements
10412 * @printconfig: should we print the contents
10413 *
10414 * helper function to assist in extracting a few useful SEID values.
10415 **/
10416static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10417 struct i40e_aqc_switch_config_element_resp *ele,
10418 u16 num_reported, bool printconfig)
10419{
10420 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10421 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10422 u8 element_type = ele->element_type;
10423 u16 seid = le16_to_cpu(ele->seid);
10424
10425 if (printconfig)
10426 dev_info(&pf->pdev->dev,
10427 "type=%d seid=%d uplink=%d downlink=%d\n",
10428 element_type, seid, uplink_seid, downlink_seid);
10429
10430 switch (element_type) {
10431 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10432 pf->mac_seid = seid;
10433 break;
10434 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10435 /* Main VEB? */
10436 if (uplink_seid != pf->mac_seid)
10437 break;
10438 if (pf->lan_veb == I40E_NO_VEB) {
10439 int v;
10440
10441 /* find existing or else empty VEB */
10442 for (v = 0; v < I40E_MAX_VEB; v++) {
10443 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10444 pf->lan_veb = v;
10445 break;
10446 }
10447 }
10448 if (pf->lan_veb == I40E_NO_VEB) {
10449 v = i40e_veb_mem_alloc(pf);
10450 if (v < 0)
10451 break;
10452 pf->lan_veb = v;
10453 }
10454 }
10455
10456 pf->veb[pf->lan_veb]->seid = seid;
10457 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10458 pf->veb[pf->lan_veb]->pf = pf;
10459 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10460 break;
10461 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10462 if (num_reported != 1)
10463 break;
10464 /* This is immediately after a reset so we can assume this is
10465 * the PF's VSI
10466 */
10467 pf->mac_seid = uplink_seid;
10468 pf->pf_seid = downlink_seid;
10469 pf->main_vsi_seid = seid;
10470 if (printconfig)
10471 dev_info(&pf->pdev->dev,
10472 "pf_seid=%d main_vsi_seid=%d\n",
10473 pf->pf_seid, pf->main_vsi_seid);
10474 break;
10475 case I40E_SWITCH_ELEMENT_TYPE_PF:
10476 case I40E_SWITCH_ELEMENT_TYPE_VF:
10477 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10478 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10479 case I40E_SWITCH_ELEMENT_TYPE_PE:
10480 case I40E_SWITCH_ELEMENT_TYPE_PA:
10481 /* ignore these for now */
10482 break;
10483 default:
10484 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10485 element_type, seid);
10486 break;
10487 }
10488}
10489
10490/**
10491 * i40e_fetch_switch_configuration - Get switch config from firmware
10492 * @pf: board private structure
10493 * @printconfig: should we print the contents
10494 *
10495 * Get the current switch configuration from the device and
10496 * extract a few useful SEID values.
10497 **/
10498int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10499{
10500 struct i40e_aqc_get_switch_config_resp *sw_config;
10501 u16 next_seid = 0;
10502 int ret = 0;
10503 u8 *aq_buf;
10504 int i;
10505
10506 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10507 if (!aq_buf)
10508 return -ENOMEM;
10509
10510 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10511 do {
10512 u16 num_reported, num_total;
10513
10514 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10515 I40E_AQ_LARGE_BUF,
10516 &next_seid, NULL);
10517 if (ret) {
10518 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010519 "get switch config failed err %s aq_err %s\n",
10520 i40e_stat_str(&pf->hw, ret),
10521 i40e_aq_str(&pf->hw,
10522 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010523 kfree(aq_buf);
10524 return -ENOENT;
10525 }
10526
10527 num_reported = le16_to_cpu(sw_config->header.num_reported);
10528 num_total = le16_to_cpu(sw_config->header.num_total);
10529
10530 if (printconfig)
10531 dev_info(&pf->pdev->dev,
10532 "header: %d reported %d total\n",
10533 num_reported, num_total);
10534
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010535 for (i = 0; i < num_reported; i++) {
10536 struct i40e_aqc_switch_config_element_resp *ele =
10537 &sw_config->element[i];
10538
10539 i40e_setup_pf_switch_element(pf, ele, num_reported,
10540 printconfig);
10541 }
10542 } while (next_seid != 0);
10543
10544 kfree(aq_buf);
10545 return ret;
10546}
10547
10548/**
10549 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10550 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010551 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010552 *
10553 * Returns 0 on success, negative value on failure
10554 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010555static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010556{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010557 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010558 int ret;
10559
10560 /* find out what's out there already */
10561 ret = i40e_fetch_switch_configuration(pf, false);
10562 if (ret) {
10563 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010564 "couldn't fetch switch config, err %s aq_err %s\n",
10565 i40e_stat_str(&pf->hw, ret),
10566 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010567 return ret;
10568 }
10569 i40e_pf_reset_stats(pf);
10570
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010571 /* set the switch config bit for the whole device to
10572 * support limited promisc or true promisc
10573 * when user requests promisc. The default is limited
10574 * promisc.
10575 */
10576
10577 if ((pf->hw.pf_id == 0) &&
10578 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10579 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10580
10581 if (pf->hw.pf_id == 0) {
10582 u16 valid_flags;
10583
10584 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10585 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10586 NULL);
10587 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10588 dev_info(&pf->pdev->dev,
10589 "couldn't set switch config bits, err %s aq_err %s\n",
10590 i40e_stat_str(&pf->hw, ret),
10591 i40e_aq_str(&pf->hw,
10592 pf->hw.aq.asq_last_status));
10593 /* not a fatal problem, just keep going */
10594 }
10595 }
10596
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010597 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010598 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010599 struct i40e_vsi *vsi = NULL;
10600 u16 uplink_seid;
10601
10602 /* Set up the PF VSI associated with the PF's main VSI
10603 * that is already in the HW switch
10604 */
10605 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10606 uplink_seid = pf->veb[pf->lan_veb]->seid;
10607 else
10608 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010609 if (pf->lan_vsi == I40E_NO_VSI)
10610 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10611 else if (reinit)
10612 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010613 if (!vsi) {
10614 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10615 i40e_fdir_teardown(pf);
10616 return -EAGAIN;
10617 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010618 } else {
10619 /* force a reset of TC and queue layout configurations */
10620 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010621
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010622 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10623 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10624 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10625 }
10626 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10627
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010628 i40e_fdir_sb_setup(pf);
10629
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010630 /* Setup static PF queue filter control settings */
10631 ret = i40e_setup_pf_filter_control(pf);
10632 if (ret) {
10633 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10634 ret);
10635 /* Failure here should not stop continuing other steps */
10636 }
10637
10638 /* enable RSS in the HW, even for only one queue, as the stack can use
10639 * the hash
10640 */
10641 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010642 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010643
10644 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010645 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010646 i40e_link_event(pf);
10647
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010648 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010649 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10650 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010651
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010652 i40e_ptp_init(pf);
10653
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010654 return ret;
10655}
10656
10657/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010658 * i40e_determine_queue_usage - Work out queue distribution
10659 * @pf: board private structure
10660 **/
10661static void i40e_determine_queue_usage(struct i40e_pf *pf)
10662{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010663 int queues_left;
10664
10665 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010666#ifdef I40E_FCOE
10667 pf->num_fcoe_qps = 0;
10668#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010669
10670 /* Find the max queues to be put into basic use. We'll always be
10671 * using TC0, whether or not DCB is running, and TC0 will get the
10672 * big RSS set.
10673 */
10674 queues_left = pf->hw.func_caps.num_tx_qp;
10675
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010676 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010677 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010678 /* one qp for PF, no queues for anything else */
10679 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010680 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010681
10682 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010683 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010684 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010685#ifdef I40E_FCOE
10686 I40E_FLAG_FCOE_ENABLED |
10687#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010688 I40E_FLAG_FD_SB_ENABLED |
10689 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010690 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010691 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010692 I40E_FLAG_SRIOV_ENABLED |
10693 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010694 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10695 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010696 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010697 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010698 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010699 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010700 queues_left -= pf->num_lan_qps;
10701
10702 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010703 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010704#ifdef I40E_FCOE
10705 I40E_FLAG_FCOE_ENABLED |
10706#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010707 I40E_FLAG_FD_SB_ENABLED |
10708 I40E_FLAG_FD_ATR_ENABLED |
10709 I40E_FLAG_DCB_ENABLED |
10710 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010711 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010712 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010713 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010714 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010715 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10716 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010717 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10718 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010719 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10720 num_online_cpus());
10721 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10722 pf->hw.func_caps.num_tx_qp);
10723
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010724 queues_left -= pf->num_lan_qps;
10725 }
10726
Vasu Dev38e00432014-08-01 13:27:03 -070010727#ifdef I40E_FCOE
10728 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10729 if (I40E_DEFAULT_FCOE <= queues_left) {
10730 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10731 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10732 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10733 } else {
10734 pf->num_fcoe_qps = 0;
10735 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10736 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10737 }
10738
10739 queues_left -= pf->num_fcoe_qps;
10740 }
10741
10742#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010743 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10744 if (queues_left > 1) {
10745 queues_left -= 1; /* save 1 queue for FD */
10746 } else {
10747 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10748 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10749 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010750 }
10751
10752 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10753 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010754 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10755 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010756 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10757 }
10758
10759 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10760 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10761 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10762 (queues_left / pf->num_vmdq_qps));
10763 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10764 }
10765
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010766 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010767 dev_dbg(&pf->pdev->dev,
10768 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10769 pf->hw.func_caps.num_tx_qp,
10770 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010771 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10772 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10773 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010774#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010775 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010776#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010777}
10778
10779/**
10780 * i40e_setup_pf_filter_control - Setup PF static filter control
10781 * @pf: PF to be setup
10782 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010783 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010784 * settings. If PE/FCoE are enabled then it will also set the per PF
10785 * based filter sizes required for them. It also enables Flow director,
10786 * ethertype and macvlan type filter settings for the pf.
10787 *
10788 * Returns 0 on success, negative on failure
10789 **/
10790static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10791{
10792 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10793
10794 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10795
10796 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010797 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010798 settings->enable_fdir = true;
10799
10800 /* Ethtype and MACVLAN filters enabled for PF */
10801 settings->enable_ethtype = true;
10802 settings->enable_macvlan = true;
10803
10804 if (i40e_set_filter_control(&pf->hw, settings))
10805 return -ENOENT;
10806
10807 return 0;
10808}
10809
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010810#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010811#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010812static void i40e_print_features(struct i40e_pf *pf)
10813{
10814 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010815 char *buf;
10816 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010817
Joe Perches3b195842015-12-03 04:20:57 -080010818 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10819 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010820 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010821
Joe Perches3b195842015-12-03 04:20:57 -080010822 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010823#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010824 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010825#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010826 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010827 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010828 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010829 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010830 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010831 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010832 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010833 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010834 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10835 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010836 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010837 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010838 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010839 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010840 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010841 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010842 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010843#ifdef I40E_FCOE
10844 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010845 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010846#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010847 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010848 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010849 else
Joe Perches3b195842015-12-03 04:20:57 -080010850 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010851
Joe Perches3b195842015-12-03 04:20:57 -080010852 dev_info(&pf->pdev->dev, "%s\n", buf);
10853 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010854 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010855}
10856
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010857/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010858 * i40e_get_platform_mac_addr - get platform-specific MAC address
10859 *
10860 * @pdev: PCI device information struct
10861 * @pf: board private structure
10862 *
10863 * Look up the MAC address in Open Firmware on systems that support it,
10864 * and use IDPROM on SPARC if no OF address is found. On return, the
10865 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10866 * has been selected.
10867 **/
10868static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10869{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010870 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010871 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010872 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010873}
10874
10875/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010876 * i40e_probe - Device initialization routine
10877 * @pdev: PCI device information struct
10878 * @ent: entry in i40e_pci_tbl
10879 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010880 * i40e_probe initializes a PF identified by a pci_dev structure.
10881 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010882 * and a hardware reset occur.
10883 *
10884 * Returns 0 on success, negative on failure
10885 **/
10886static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10887{
Catherine Sullivane8278452015-02-06 08:52:08 +000010888 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010889 struct i40e_pf *pf;
10890 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010891 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010892 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010893 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010894 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010895 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010896 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010897 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010898
10899 err = pci_enable_device_mem(pdev);
10900 if (err)
10901 return err;
10902
10903 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010904 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010905 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010906 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10907 if (err) {
10908 dev_err(&pdev->dev,
10909 "DMA configuration failed: 0x%x\n", err);
10910 goto err_dma;
10911 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010912 }
10913
10914 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010915 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010916 if (err) {
10917 dev_info(&pdev->dev,
10918 "pci_request_selected_regions failed %d\n", err);
10919 goto err_pci_reg;
10920 }
10921
10922 pci_enable_pcie_error_reporting(pdev);
10923 pci_set_master(pdev);
10924
10925 /* Now that we have a PCI connection, we need to do the
10926 * low level device setup. This is primarily setting up
10927 * the Admin Queue structures and then querying for the
10928 * device's current profile information.
10929 */
10930 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10931 if (!pf) {
10932 err = -ENOMEM;
10933 goto err_pf_alloc;
10934 }
10935 pf->next_vsi = 0;
10936 pf->pdev = pdev;
10937 set_bit(__I40E_DOWN, &pf->state);
10938
10939 hw = &pf->hw;
10940 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010941
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010942 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10943 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010944
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010945 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010946 if (!hw->hw_addr) {
10947 err = -EIO;
10948 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10949 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010950 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010951 goto err_ioremap;
10952 }
10953 hw->vendor_id = pdev->vendor;
10954 hw->device_id = pdev->device;
10955 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10956 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10957 hw->subsystem_device_id = pdev->subsystem_device;
10958 hw->bus.device = PCI_SLOT(pdev->devfn);
10959 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010960 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010961
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010962 /* set up the locks for the AQ, do this only once in probe
10963 * and destroy them only once in remove
10964 */
10965 mutex_init(&hw->aq.asq_mutex);
10966 mutex_init(&hw->aq.arq_mutex);
10967
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010968 pf->msg_enable = netif_msg_init(debug,
10969 NETIF_MSG_DRV |
10970 NETIF_MSG_PROBE |
10971 NETIF_MSG_LINK);
10972 if (debug < -1)
10973 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010974
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010975 /* do a special CORER for clearing PXE mode once at init */
10976 if (hw->revision_id == 0 &&
10977 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10978 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10979 i40e_flush(hw);
10980 msleep(200);
10981 pf->corer_count++;
10982
10983 i40e_clear_pxe_mode(hw);
10984 }
10985
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010986 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010987 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010988 err = i40e_pf_reset(hw);
10989 if (err) {
10990 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10991 goto err_pf_reset;
10992 }
10993 pf->pfr_count++;
10994
10995 hw->aq.num_arq_entries = I40E_AQ_LEN;
10996 hw->aq.num_asq_entries = I40E_AQ_LEN;
10997 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10998 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10999 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011000
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011001 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011002 "%s-%s:misc",
11003 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011004
11005 err = i40e_init_shared_code(hw);
11006 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011007 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11008 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011009 goto err_pf_reset;
11010 }
11011
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011012 /* set up a default setting for link flow control */
11013 pf->hw.fc.requested_mode = I40E_FC_NONE;
11014
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011015 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011016 if (err) {
11017 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11018 dev_info(&pdev->dev,
11019 "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");
11020 else
11021 dev_info(&pdev->dev,
11022 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11023
11024 goto err_pf_reset;
11025 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011026
Shannon Nelson6dec1012015-09-28 14:12:30 -040011027 /* provide nvm, fw, api versions */
11028 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11029 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11030 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11031 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011032
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011033 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11034 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011035 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011036 "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");
11037 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11038 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011039 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011040 "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 +000011041
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011042 i40e_verify_eeprom(pf);
11043
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011044 /* Rev 0 hardware was never productized */
11045 if (hw->revision_id < 1)
11046 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");
11047
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011048 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011049 err = i40e_get_capabilities(pf);
11050 if (err)
11051 goto err_adminq_setup;
11052
11053 err = i40e_sw_init(pf);
11054 if (err) {
11055 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11056 goto err_sw_init;
11057 }
11058
11059 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11060 hw->func_caps.num_rx_qp,
11061 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11062 if (err) {
11063 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11064 goto err_init_lan_hmc;
11065 }
11066
11067 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11068 if (err) {
11069 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11070 err = -ENOENT;
11071 goto err_configure_lan_hmc;
11072 }
11073
Neerav Parikhb686ece2014-12-14 01:55:11 +000011074 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11075 * Ignore error return codes because if it was already disabled via
11076 * hardware settings this will fail
11077 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011078 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011079 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11080 i40e_aq_stop_lldp(hw, true, NULL);
11081 }
11082
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011083 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011084 /* allow a platform config to override the HW addr */
11085 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011086 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011087 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11088 err = -EIO;
11089 goto err_mac_addr;
11090 }
11091 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011092 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011093 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11094 if (is_valid_ether_addr(hw->mac.port_addr))
11095 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011096#ifdef I40E_FCOE
11097 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11098 if (err)
11099 dev_info(&pdev->dev,
11100 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11101 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11102 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11103 hw->mac.san_addr);
11104 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11105 }
11106 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11107#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011108
11109 pci_set_drvdata(pdev, pf);
11110 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011111#ifdef CONFIG_I40E_DCB
11112 err = i40e_init_pf_dcb(pf);
11113 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011114 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011115 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011116 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011117 }
11118#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011119
11120 /* set up periodic task facility */
11121 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11122 pf->service_timer_period = HZ;
11123
11124 INIT_WORK(&pf->service_task, i40e_service_task);
11125 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11126 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011127
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011128 /* NVM bit on means WoL disabled for the port */
11129 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011130 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011131 pf->wol_en = false;
11132 else
11133 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011134 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136 /* set up the main switch operations */
11137 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011138 err = i40e_init_interrupt_scheme(pf);
11139 if (err)
11140 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011141
Mitch Williams505682c2014-05-20 08:01:37 +000011142 /* The number of VSIs reported by the FW is the minimum guaranteed
11143 * to us; HW supports far more and we share the remaining pool with
11144 * the other PFs. We allocate space for more than the guarantee with
11145 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011146 */
Mitch Williams505682c2014-05-20 08:01:37 +000011147 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11148 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11149 else
11150 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11151
11152 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011153 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11154 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011155 if (!pf->vsi) {
11156 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011157 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011158 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011159
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011160#ifdef CONFIG_PCI_IOV
11161 /* prep for VF support */
11162 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11163 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11164 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11165 if (pci_num_vf(pdev))
11166 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11167 }
11168#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011169 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011170 if (err) {
11171 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11172 goto err_vsis;
11173 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011174
11175 /* Make sure flow control is set according to current settings */
11176 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11177 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11178 dev_dbg(&pf->pdev->dev,
11179 "Set fc with err %s aq_err %s on get_phy_cap\n",
11180 i40e_stat_str(hw, err),
11181 i40e_aq_str(hw, hw->aq.asq_last_status));
11182 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11183 dev_dbg(&pf->pdev->dev,
11184 "Set fc with err %s aq_err %s on set_phy_config\n",
11185 i40e_stat_str(hw, err),
11186 i40e_aq_str(hw, hw->aq.asq_last_status));
11187 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11188 dev_dbg(&pf->pdev->dev,
11189 "Set fc with err %s aq_err %s on get_link_info\n",
11190 i40e_stat_str(hw, err),
11191 i40e_aq_str(hw, hw->aq.asq_last_status));
11192
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011193 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011194 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011195 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11196 i40e_vsi_open(pf->vsi[i]);
11197 break;
11198 }
11199 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011200
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011201 /* The driver only wants link up/down and module qualification
11202 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011203 */
11204 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011205 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011206 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011207 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011208 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011209 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11210 i40e_stat_str(&pf->hw, err),
11211 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011212
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011213 /* Reconfigure hardware for allowing smaller MSS in the case
11214 * of TSO, so that we avoid the MDD being fired and causing
11215 * a reset in the case of small MSS+TSO.
11216 */
11217 val = rd32(hw, I40E_REG_MSS);
11218 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11219 val &= ~I40E_REG_MSS_MIN_MASK;
11220 val |= I40E_64BYTE_MSS;
11221 wr32(hw, I40E_REG_MSS, val);
11222 }
11223
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011224 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011225 msleep(75);
11226 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11227 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011228 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11229 i40e_stat_str(&pf->hw, err),
11230 i40e_aq_str(&pf->hw,
11231 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011232 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011233 /* The main driver is (mostly) up and happy. We need to set this state
11234 * before setting up the misc vector or we get a race and the vector
11235 * ends up disabled forever.
11236 */
11237 clear_bit(__I40E_DOWN, &pf->state);
11238
11239 /* In case of MSIX we are going to setup the misc vector right here
11240 * to handle admin queue events etc. In case of legacy and MSI
11241 * the misc functionality and queue processing is combined in
11242 * the same vector and that gets setup at open.
11243 */
11244 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11245 err = i40e_setup_misc_vector(pf);
11246 if (err) {
11247 dev_info(&pdev->dev,
11248 "setup of misc vector failed: %d\n", err);
11249 goto err_vsis;
11250 }
11251 }
11252
Greg Rosedf805f62014-04-04 04:43:16 +000011253#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011254 /* prep for VF support */
11255 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011256 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11257 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011258 /* disable link interrupts for VFs */
11259 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11260 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11261 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11262 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011263
11264 if (pci_num_vf(pdev)) {
11265 dev_info(&pdev->dev,
11266 "Active VFs found, allocating resources.\n");
11267 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11268 if (err)
11269 dev_info(&pdev->dev,
11270 "Error %d allocating resources for existing VFs\n",
11271 err);
11272 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011273 }
Greg Rosedf805f62014-04-04 04:43:16 +000011274#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011275
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011276 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11277 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11278 pf->num_iwarp_msix,
11279 I40E_IWARP_IRQ_PILE_ID);
11280 if (pf->iwarp_base_vector < 0) {
11281 dev_info(&pdev->dev,
11282 "failed to get tracking for %d vectors for IWARP err=%d\n",
11283 pf->num_iwarp_msix, pf->iwarp_base_vector);
11284 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11285 }
11286 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011288 i40e_dbg_pf_init(pf);
11289
11290 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011291 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011292
11293 /* since everything's happy, start the service_task timer */
11294 mod_timer(&pf->service_timer,
11295 round_jiffies(jiffies + pf->service_timer_period));
11296
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011297 /* add this PF to client device list and launch a client service task */
11298 err = i40e_lan_add_device(pf);
11299 if (err)
11300 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11301 err);
11302
Vasu Dev38e00432014-08-01 13:27:03 -070011303#ifdef I40E_FCOE
11304 /* create FCoE interface */
11305 i40e_fcoe_vsi_setup(pf);
11306
11307#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011308#define PCI_SPEED_SIZE 8
11309#define PCI_WIDTH_SIZE 8
11310 /* Devices on the IOSF bus do not have this information
11311 * and will report PCI Gen 1 x 1 by default so don't bother
11312 * checking them.
11313 */
11314 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11315 char speed[PCI_SPEED_SIZE] = "Unknown";
11316 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011317
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011318 /* Get the negotiated link width and speed from PCI config
11319 * space
11320 */
11321 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11322 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011323
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011324 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011325
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011326 switch (hw->bus.speed) {
11327 case i40e_bus_speed_8000:
11328 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11329 case i40e_bus_speed_5000:
11330 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11331 case i40e_bus_speed_2500:
11332 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11333 default:
11334 break;
11335 }
11336 switch (hw->bus.width) {
11337 case i40e_bus_width_pcie_x8:
11338 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11339 case i40e_bus_width_pcie_x4:
11340 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11341 case i40e_bus_width_pcie_x2:
11342 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11343 case i40e_bus_width_pcie_x1:
11344 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11345 default:
11346 break;
11347 }
11348
11349 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11350 speed, width);
11351
11352 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11353 hw->bus.speed < i40e_bus_speed_8000) {
11354 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11355 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11356 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011357 }
11358
Catherine Sullivane8278452015-02-06 08:52:08 +000011359 /* get the requested speeds from the fw */
11360 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11361 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011362 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11363 i40e_stat_str(&pf->hw, err),
11364 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011365 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11366
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011367 /* get the supported phy types from the fw */
11368 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11369 if (err)
11370 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11371 i40e_stat_str(&pf->hw, err),
11372 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011373
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011374 /* Add a filter to drop all Flow control frames from any VSI from being
11375 * transmitted. By doing so we stop a malicious VF from sending out
11376 * PAUSE or PFC frames and potentially controlling traffic for other
11377 * PF/VF VSIs.
11378 * The FW can still send Flow control frames if enabled.
11379 */
11380 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11381 pf->main_vsi_seid);
11382
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011383 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011384 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11385 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011386 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11387 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011388 /* print a string summarizing features */
11389 i40e_print_features(pf);
11390
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011391 return 0;
11392
11393 /* Unwind what we've done if something failed in the setup */
11394err_vsis:
11395 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011396 i40e_clear_interrupt_scheme(pf);
11397 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011398err_switch_setup:
11399 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011400 del_timer_sync(&pf->service_timer);
11401err_mac_addr:
11402err_configure_lan_hmc:
11403 (void)i40e_shutdown_lan_hmc(hw);
11404err_init_lan_hmc:
11405 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011406err_sw_init:
11407err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011408err_pf_reset:
11409 iounmap(hw->hw_addr);
11410err_ioremap:
11411 kfree(pf);
11412err_pf_alloc:
11413 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011414 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011415err_pci_reg:
11416err_dma:
11417 pci_disable_device(pdev);
11418 return err;
11419}
11420
11421/**
11422 * i40e_remove - Device removal routine
11423 * @pdev: PCI device information struct
11424 *
11425 * i40e_remove is called by the PCI subsystem to alert the driver
11426 * that is should release a PCI device. This could be caused by a
11427 * Hot-Plug event, or because the driver is going to be removed from
11428 * memory.
11429 **/
11430static void i40e_remove(struct pci_dev *pdev)
11431{
11432 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011433 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011434 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011435 int i;
11436
11437 i40e_dbg_pf_exit(pf);
11438
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011439 i40e_ptp_stop(pf);
11440
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011441 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011442 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11443 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011444
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011445 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011446 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011447 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011448 if (pf->service_timer.data)
11449 del_timer_sync(&pf->service_timer);
11450 if (pf->service_task.func)
11451 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011452
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011453 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11454 i40e_free_vfs(pf);
11455 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11456 }
11457
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011458 i40e_fdir_teardown(pf);
11459
11460 /* If there is a switch structure or any orphans, remove them.
11461 * This will leave only the PF's VSI remaining.
11462 */
11463 for (i = 0; i < I40E_MAX_VEB; i++) {
11464 if (!pf->veb[i])
11465 continue;
11466
11467 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11468 pf->veb[i]->uplink_seid == 0)
11469 i40e_switch_branch_release(pf->veb[i]);
11470 }
11471
11472 /* Now we can shutdown the PF's VSI, just before we kill
11473 * adminq and hmc.
11474 */
11475 if (pf->vsi[pf->lan_vsi])
11476 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11477
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011478 /* remove attached clients */
11479 ret_code = i40e_lan_del_device(pf);
11480 if (ret_code) {
11481 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11482 ret_code);
11483 }
11484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011485 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011486 if (hw->hmc.hmc_obj) {
11487 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011488 if (ret_code)
11489 dev_warn(&pdev->dev,
11490 "Failed to destroy the HMC resources: %d\n",
11491 ret_code);
11492 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011493
11494 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011495 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011496
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011497 /* destroy the locks only once, here */
11498 mutex_destroy(&hw->aq.arq_mutex);
11499 mutex_destroy(&hw->aq.asq_mutex);
11500
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011501 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11502 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011503 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011504 if (pf->vsi[i]) {
11505 i40e_vsi_clear_rings(pf->vsi[i]);
11506 i40e_vsi_clear(pf->vsi[i]);
11507 pf->vsi[i] = NULL;
11508 }
11509 }
11510
11511 for (i = 0; i < I40E_MAX_VEB; i++) {
11512 kfree(pf->veb[i]);
11513 pf->veb[i] = NULL;
11514 }
11515
11516 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011517 kfree(pf->vsi);
11518
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011519 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011520 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011521 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011522
11523 pci_disable_pcie_error_reporting(pdev);
11524 pci_disable_device(pdev);
11525}
11526
11527/**
11528 * i40e_pci_error_detected - warning that something funky happened in PCI land
11529 * @pdev: PCI device information struct
11530 *
11531 * Called to warn that something happened and the error handling steps
11532 * are in progress. Allows the driver to quiesce things, be ready for
11533 * remediation.
11534 **/
11535static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11536 enum pci_channel_state error)
11537{
11538 struct i40e_pf *pf = pci_get_drvdata(pdev);
11539
11540 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11541
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011542 if (!pf) {
11543 dev_info(&pdev->dev,
11544 "Cannot recover - error happened during device probe\n");
11545 return PCI_ERS_RESULT_DISCONNECT;
11546 }
11547
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011548 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011549 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11550 rtnl_lock();
11551 i40e_prep_for_reset(pf);
11552 rtnl_unlock();
11553 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011554
11555 /* Request a slot reset */
11556 return PCI_ERS_RESULT_NEED_RESET;
11557}
11558
11559/**
11560 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11561 * @pdev: PCI device information struct
11562 *
11563 * Called to find if the driver can work with the device now that
11564 * the pci slot has been reset. If a basic connection seems good
11565 * (registers are readable and have sane content) then return a
11566 * happy little PCI_ERS_RESULT_xxx.
11567 **/
11568static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11569{
11570 struct i40e_pf *pf = pci_get_drvdata(pdev);
11571 pci_ers_result_t result;
11572 int err;
11573 u32 reg;
11574
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011575 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011576 if (pci_enable_device_mem(pdev)) {
11577 dev_info(&pdev->dev,
11578 "Cannot re-enable PCI device after reset.\n");
11579 result = PCI_ERS_RESULT_DISCONNECT;
11580 } else {
11581 pci_set_master(pdev);
11582 pci_restore_state(pdev);
11583 pci_save_state(pdev);
11584 pci_wake_from_d3(pdev, false);
11585
11586 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11587 if (reg == 0)
11588 result = PCI_ERS_RESULT_RECOVERED;
11589 else
11590 result = PCI_ERS_RESULT_DISCONNECT;
11591 }
11592
11593 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11594 if (err) {
11595 dev_info(&pdev->dev,
11596 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11597 err);
11598 /* non-fatal, continue */
11599 }
11600
11601 return result;
11602}
11603
11604/**
11605 * i40e_pci_error_resume - restart operations after PCI error recovery
11606 * @pdev: PCI device information struct
11607 *
11608 * Called to allow the driver to bring things back up after PCI error
11609 * and/or reset recovery has finished.
11610 **/
11611static void i40e_pci_error_resume(struct pci_dev *pdev)
11612{
11613 struct i40e_pf *pf = pci_get_drvdata(pdev);
11614
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011615 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011616 if (test_bit(__I40E_SUSPENDED, &pf->state))
11617 return;
11618
11619 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011620 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011621 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011622}
11623
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011624/**
11625 * i40e_shutdown - PCI callback for shutting down
11626 * @pdev: PCI device information struct
11627 **/
11628static void i40e_shutdown(struct pci_dev *pdev)
11629{
11630 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011631 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011632
11633 set_bit(__I40E_SUSPENDED, &pf->state);
11634 set_bit(__I40E_DOWN, &pf->state);
11635 rtnl_lock();
11636 i40e_prep_for_reset(pf);
11637 rtnl_unlock();
11638
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011639 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11640 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11641
Catherine Sullivan02b42492015-07-10 19:35:59 -040011642 del_timer_sync(&pf->service_timer);
11643 cancel_work_sync(&pf->service_task);
11644 i40e_fdir_teardown(pf);
11645
11646 rtnl_lock();
11647 i40e_prep_for_reset(pf);
11648 rtnl_unlock();
11649
11650 wr32(hw, I40E_PFPM_APM,
11651 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11652 wr32(hw, I40E_PFPM_WUFC,
11653 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11654
Shannon Nelsone1477582015-02-21 06:44:33 +000011655 i40e_clear_interrupt_scheme(pf);
11656
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011657 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011658 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011659 pci_set_power_state(pdev, PCI_D3hot);
11660 }
11661}
11662
11663#ifdef CONFIG_PM
11664/**
11665 * i40e_suspend - PCI callback for moving to D3
11666 * @pdev: PCI device information struct
11667 **/
11668static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11669{
11670 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011671 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011672 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011673
11674 set_bit(__I40E_SUSPENDED, &pf->state);
11675 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011676
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011677 rtnl_lock();
11678 i40e_prep_for_reset(pf);
11679 rtnl_unlock();
11680
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011681 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11682 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11683
Greg Roseb33d3b72016-05-16 10:26:37 -070011684 i40e_stop_misc_vector(pf);
11685
Greg Rose059ff692016-05-16 10:26:38 -070011686 retval = pci_save_state(pdev);
11687 if (retval)
11688 return retval;
11689
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011690 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011691 pci_set_power_state(pdev, PCI_D3hot);
11692
Greg Rose059ff692016-05-16 10:26:38 -070011693 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011694}
11695
11696/**
11697 * i40e_resume - PCI callback for waking up from D3
11698 * @pdev: PCI device information struct
11699 **/
11700static int i40e_resume(struct pci_dev *pdev)
11701{
11702 struct i40e_pf *pf = pci_get_drvdata(pdev);
11703 u32 err;
11704
11705 pci_set_power_state(pdev, PCI_D0);
11706 pci_restore_state(pdev);
11707 /* pci_restore_state() clears dev->state_saves, so
11708 * call pci_save_state() again to restore it.
11709 */
11710 pci_save_state(pdev);
11711
11712 err = pci_enable_device_mem(pdev);
11713 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011714 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011715 return err;
11716 }
11717 pci_set_master(pdev);
11718
11719 /* no wakeup events while running */
11720 pci_wake_from_d3(pdev, false);
11721
11722 /* handling the reset will rebuild the device state */
11723 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11724 clear_bit(__I40E_DOWN, &pf->state);
11725 rtnl_lock();
11726 i40e_reset_and_rebuild(pf, false);
11727 rtnl_unlock();
11728 }
11729
11730 return 0;
11731}
11732
11733#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011734static const struct pci_error_handlers i40e_err_handler = {
11735 .error_detected = i40e_pci_error_detected,
11736 .slot_reset = i40e_pci_error_slot_reset,
11737 .resume = i40e_pci_error_resume,
11738};
11739
11740static struct pci_driver i40e_driver = {
11741 .name = i40e_driver_name,
11742 .id_table = i40e_pci_tbl,
11743 .probe = i40e_probe,
11744 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011745#ifdef CONFIG_PM
11746 .suspend = i40e_suspend,
11747 .resume = i40e_resume,
11748#endif
11749 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011750 .err_handler = &i40e_err_handler,
11751 .sriov_configure = i40e_pci_sriov_configure,
11752};
11753
11754/**
11755 * i40e_init_module - Driver registration routine
11756 *
11757 * i40e_init_module is the first routine called when the driver is
11758 * loaded. All it does is register with the PCI subsystem.
11759 **/
11760static int __init i40e_init_module(void)
11761{
11762 pr_info("%s: %s - version %s\n", i40e_driver_name,
11763 i40e_driver_string, i40e_driver_version_str);
11764 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011765
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011766 /* we will see if single thread per module is enough for now,
11767 * it can't be any worse than using the system workqueue which
11768 * was already single threaded
11769 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011770 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11771 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011772 if (!i40e_wq) {
11773 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11774 return -ENOMEM;
11775 }
11776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011777 i40e_dbg_init();
11778 return pci_register_driver(&i40e_driver);
11779}
11780module_init(i40e_init_module);
11781
11782/**
11783 * i40e_exit_module - Driver exit cleanup routine
11784 *
11785 * i40e_exit_module is called just before the driver is removed
11786 * from memory.
11787 **/
11788static void __exit i40e_exit_module(void)
11789{
11790 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011791 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011792 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011793}
11794module_exit(i40e_exit_module);