blob: 1d8febd721ac55fa934ec391cbd8089d249fc162 [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
Bimmy Pujari15990832017-01-30 12:29:37 -080042#define DRV_VERSION_MAJOR 2
43#define DRV_VERSION_MINOR 1
44#define DRV_VERSION_BUILD 7
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},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000090 /* required last entry */
91 {0, }
92};
93MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94
95#define I40E_MAX_VF_COUNT 128
96static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040097module_param(debug, uint, 0);
98MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000099
100MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102MODULE_LICENSE("GPL");
103MODULE_VERSION(DRV_VERSION);
104
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800105static struct workqueue_struct *i40e_wq;
106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000107/**
108 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
109 * @hw: pointer to the HW structure
110 * @mem: ptr to mem struct to fill out
111 * @size: size of memory requested
112 * @alignment: what to align the allocation to
113 **/
114int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
115 u64 size, u32 alignment)
116{
117 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118
119 mem->size = ALIGN(size, alignment);
120 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
121 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000122 if (!mem->va)
123 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000124
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000125 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000126}
127
128/**
129 * i40e_free_dma_mem_d - OS specific memory free for shared code
130 * @hw: pointer to the HW structure
131 * @mem: ptr to mem struct to free
132 **/
133int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
134{
135 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
136
137 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
138 mem->va = NULL;
139 mem->pa = 0;
140 mem->size = 0;
141
142 return 0;
143}
144
145/**
146 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
147 * @hw: pointer to the HW structure
148 * @mem: ptr to mem struct to fill out
149 * @size: size of memory requested
150 **/
151int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
152 u32 size)
153{
154 mem->size = size;
155 mem->va = kzalloc(size, GFP_KERNEL);
156
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000157 if (!mem->va)
158 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000159
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000160 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000161}
162
163/**
164 * i40e_free_virt_mem_d - OS specific memory free for shared code
165 * @hw: pointer to the HW structure
166 * @mem: ptr to mem struct to free
167 **/
168int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
169{
170 /* it's ok to kfree a NULL pointer */
171 kfree(mem->va);
172 mem->va = NULL;
173 mem->size = 0;
174
175 return 0;
176}
177
178/**
179 * i40e_get_lump - find a lump of free generic resource
180 * @pf: board private structure
181 * @pile: the pile of resource to search
182 * @needed: the number of items needed
183 * @id: an owner id to stick on the items assigned
184 *
185 * Returns the base item index of the lump, or negative for error
186 *
187 * The search_hint trick and lack of advanced fit-finding only work
188 * because we're highly likely to have all the same size lump requests.
189 * Linear search time and any fragmentation should be minimal.
190 **/
191static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
192 u16 needed, u16 id)
193{
194 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000195 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000196
197 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
198 dev_info(&pf->pdev->dev,
199 "param err: pile=%p needed=%d id=0x%04x\n",
200 pile, needed, id);
201 return -EINVAL;
202 }
203
204 /* start the linear search with an imperfect hint */
205 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000206 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000207 /* skip already allocated entries */
208 if (pile->list[i] & I40E_PILE_VALID_BIT) {
209 i++;
210 continue;
211 }
212
213 /* do we have enough in this lump? */
214 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
215 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
216 break;
217 }
218
219 if (j == needed) {
220 /* there was enough, so assign it to the requestor */
221 for (j = 0; j < needed; j++)
222 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
223 ret = i;
224 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000225 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000226 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400227
228 /* not enough, so skip over it and continue looking */
229 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000230 }
231
232 return ret;
233}
234
235/**
236 * i40e_put_lump - return a lump of generic resource
237 * @pile: the pile of resource to search
238 * @index: the base item index
239 * @id: the owner id of the items assigned
240 *
241 * Returns the count of items in the lump
242 **/
243static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
244{
245 int valid_id = (id | I40E_PILE_VALID_BIT);
246 int count = 0;
247 int i;
248
249 if (!pile || index >= pile->num_entries)
250 return -EINVAL;
251
252 for (i = index;
253 i < pile->num_entries && pile->list[i] == valid_id;
254 i++) {
255 pile->list[i] = 0;
256 count++;
257 }
258
259 if (count && index < pile->search_hint)
260 pile->search_hint = index;
261
262 return count;
263}
264
265/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700266 * i40e_find_vsi_from_id - searches for the vsi with the given id
267 * @pf - the pf structure to search for the vsi
268 * @id - id of the vsi it is searching for
269 **/
270struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
271{
272 int i;
273
274 for (i = 0; i < pf->num_alloc_vsi; i++)
275 if (pf->vsi[i] && (pf->vsi[i]->id == id))
276 return pf->vsi[i];
277
278 return NULL;
279}
280
281/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000282 * i40e_service_event_schedule - Schedule the service task to wake up
283 * @pf: board private structure
284 *
285 * If not already scheduled, this puts the task into the work queue
286 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600287void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000288{
289 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800290 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800291 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292}
293
294/**
295 * i40e_tx_timeout - Respond to a Tx Hang
296 * @netdev: network interface device structure
297 *
298 * If any port has noticed a Tx timeout, it is likely that the whole
299 * device is munged, not just the one netdev port, so go for the full
300 * reset.
301 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700302#ifdef I40E_FCOE
303void i40e_tx_timeout(struct net_device *netdev)
304#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000305static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700306#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000307{
308 struct i40e_netdev_priv *np = netdev_priv(netdev);
309 struct i40e_vsi *vsi = np->vsi;
310 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400311 struct i40e_ring *tx_ring = NULL;
312 unsigned int i, hung_queue = 0;
313 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000314
315 pf->tx_timeout_count++;
316
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 /* find the stopped queue the same way the stack does */
318 for (i = 0; i < netdev->num_tx_queues; i++) {
319 struct netdev_queue *q;
320 unsigned long trans_start;
321
322 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200323 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400324 if (netif_xmit_stopped(q) &&
325 time_after(jiffies,
326 (trans_start + netdev->watchdog_timeo))) {
327 hung_queue = i;
328 break;
329 }
330 }
331
332 if (i == netdev->num_tx_queues) {
333 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
334 } else {
335 /* now that we have an index, find the tx_ring struct */
336 for (i = 0; i < vsi->num_queue_pairs; i++) {
337 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 if (hung_queue ==
339 vsi->tx_rings[i]->queue_index) {
340 tx_ring = vsi->tx_rings[i];
341 break;
342 }
343 }
344 }
345 }
346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000347 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400348 pf->tx_timeout_recovery_level = 1; /* reset after some time */
349 else if (time_before(jiffies,
350 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 return; /* don't do any new action before the next timeout */
352
353 if (tx_ring) {
354 head = i40e_get_head(tx_ring);
355 /* Read interrupt register */
356 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
357 val = rd32(&pf->hw,
358 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
359 tx_ring->vsi->base_vector - 1));
360 else
361 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
362
363 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",
364 vsi->seid, hung_queue, tx_ring->next_to_clean,
365 head, tx_ring->next_to_use,
366 readl(tx_ring->tail), val);
367 }
368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000369 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400370 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
371 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
373 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000374 case 1:
375 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
376 break;
377 case 2:
378 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
379 break;
380 case 3:
381 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
382 break;
383 default:
384 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 i40e_service_event_schedule(pf);
389 pf->tx_timeout_recovery_level++;
390}
391
392/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000393 * i40e_get_vsi_stats_struct - Get System Network Statistics
394 * @vsi: the VSI we care about
395 *
396 * Returns the address of the device statistics structure.
397 * The statistics are actually updated from the service task.
398 **/
399struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
400{
401 return &vsi->net_stats;
402}
403
404/**
405 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
406 * @netdev: network interface device structure
407 *
408 * Returns the address of the device statistics structure.
409 * The statistics are actually updated from the service task.
410 **/
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800411#ifndef I40E_FCOE
412static
Vasu Dev38e00432014-08-01 13:27:03 -0700413#endif
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800414void i40e_get_netdev_stats_struct(struct net_device *netdev,
415 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000416{
417 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000418 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000419 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000420 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
421 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000422
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000423 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800424 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000425
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800426 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800427 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800428
Alexander Duyck980e9b12013-09-28 06:01:03 +0000429 rcu_read_lock();
430 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000431 u64 bytes, packets;
432 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000433
Alexander Duyck980e9b12013-09-28 06:01:03 +0000434 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
435 if (!tx_ring)
436 continue;
437
438 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700439 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000440 packets = tx_ring->stats.packets;
441 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700442 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000443
444 stats->tx_packets += packets;
445 stats->tx_bytes += bytes;
446 rx_ring = &tx_ring[1];
447
448 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700449 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000450 packets = rx_ring->stats.packets;
451 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700452 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453
454 stats->rx_packets += packets;
455 stats->rx_bytes += bytes;
456 }
457 rcu_read_unlock();
458
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000459 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000460 stats->multicast = vsi_stats->multicast;
461 stats->tx_errors = vsi_stats->tx_errors;
462 stats->tx_dropped = vsi_stats->tx_dropped;
463 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400464 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000465 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
466 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000467}
468
469/**
470 * i40e_vsi_reset_stats - Resets all stats of the given vsi
471 * @vsi: the VSI to have its stats reset
472 **/
473void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
474{
475 struct rtnl_link_stats64 *ns;
476 int i;
477
478 if (!vsi)
479 return;
480
481 ns = i40e_get_vsi_stats_struct(vsi);
482 memset(ns, 0, sizeof(*ns));
483 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
484 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
485 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000486 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000487 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400488 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000489 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400490 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000491 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400492 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000493 sizeof(vsi->tx_rings[i]->stats));
494 memset(&vsi->tx_rings[i]->tx_stats, 0,
495 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000496 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000497 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000498 vsi->stat_offsets_loaded = false;
499}
500
501/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000502 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000503 * @pf: the PF to be reset
504 **/
505void i40e_pf_reset_stats(struct i40e_pf *pf)
506{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000507 int i;
508
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000509 memset(&pf->stats, 0, sizeof(pf->stats));
510 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
511 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000512
513 for (i = 0; i < I40E_MAX_VEB; i++) {
514 if (pf->veb[i]) {
515 memset(&pf->veb[i]->stats, 0,
516 sizeof(pf->veb[i]->stats));
517 memset(&pf->veb[i]->stats_offsets, 0,
518 sizeof(pf->veb[i]->stats_offsets));
519 pf->veb[i]->stat_offsets_loaded = false;
520 }
521 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700522 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523}
524
525/**
526 * i40e_stat_update48 - read and update a 48 bit stat from the chip
527 * @hw: ptr to the hardware info
528 * @hireg: the high 32 bit reg to read
529 * @loreg: the low 32 bit reg to read
530 * @offset_loaded: has the initial offset been loaded yet
531 * @offset: ptr to current offset value
532 * @stat: ptr to the stat
533 *
534 * Since the device stats are not reset at PFReset, they likely will not
535 * be zeroed when the driver starts. We'll save the first values read
536 * and use them as offsets to be subtracted from the raw values in order
537 * to report stats that count from zero. In the process, we also manage
538 * the potential roll-over.
539 **/
540static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
541 bool offset_loaded, u64 *offset, u64 *stat)
542{
543 u64 new_data;
544
Shannon Nelsonab600852014-01-17 15:36:39 -0800545 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000546 new_data = rd32(hw, loreg);
547 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
548 } else {
549 new_data = rd64(hw, loreg);
550 }
551 if (!offset_loaded)
552 *offset = new_data;
553 if (likely(new_data >= *offset))
554 *stat = new_data - *offset;
555 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400556 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000557 *stat &= 0xFFFFFFFFFFFFULL;
558}
559
560/**
561 * i40e_stat_update32 - read and update a 32 bit stat from the chip
562 * @hw: ptr to the hardware info
563 * @reg: the hw reg to read
564 * @offset_loaded: has the initial offset been loaded yet
565 * @offset: ptr to current offset value
566 * @stat: ptr to the stat
567 **/
568static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
569 bool offset_loaded, u64 *offset, u64 *stat)
570{
571 u32 new_data;
572
573 new_data = rd32(hw, reg);
574 if (!offset_loaded)
575 *offset = new_data;
576 if (likely(new_data >= *offset))
577 *stat = (u32)(new_data - *offset);
578 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400579 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000580}
581
582/**
583 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
584 * @vsi: the VSI to be updated
585 **/
586void i40e_update_eth_stats(struct i40e_vsi *vsi)
587{
588 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
589 struct i40e_pf *pf = vsi->back;
590 struct i40e_hw *hw = &pf->hw;
591 struct i40e_eth_stats *oes;
592 struct i40e_eth_stats *es; /* device's eth stats */
593
594 es = &vsi->eth_stats;
595 oes = &vsi->eth_stats_offsets;
596
597 /* Gather up the stats that the hw collects */
598 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
599 vsi->stat_offsets_loaded,
600 &oes->tx_errors, &es->tx_errors);
601 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
602 vsi->stat_offsets_loaded,
603 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000604 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
605 vsi->stat_offsets_loaded,
606 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
607 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
608 vsi->stat_offsets_loaded,
609 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000610
611 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
612 I40E_GLV_GORCL(stat_idx),
613 vsi->stat_offsets_loaded,
614 &oes->rx_bytes, &es->rx_bytes);
615 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
616 I40E_GLV_UPRCL(stat_idx),
617 vsi->stat_offsets_loaded,
618 &oes->rx_unicast, &es->rx_unicast);
619 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
620 I40E_GLV_MPRCL(stat_idx),
621 vsi->stat_offsets_loaded,
622 &oes->rx_multicast, &es->rx_multicast);
623 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
624 I40E_GLV_BPRCL(stat_idx),
625 vsi->stat_offsets_loaded,
626 &oes->rx_broadcast, &es->rx_broadcast);
627
628 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
629 I40E_GLV_GOTCL(stat_idx),
630 vsi->stat_offsets_loaded,
631 &oes->tx_bytes, &es->tx_bytes);
632 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
633 I40E_GLV_UPTCL(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->tx_unicast, &es->tx_unicast);
636 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
637 I40E_GLV_MPTCL(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->tx_multicast, &es->tx_multicast);
640 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
641 I40E_GLV_BPTCL(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->tx_broadcast, &es->tx_broadcast);
644 vsi->stat_offsets_loaded = true;
645}
646
647/**
648 * i40e_update_veb_stats - Update Switch component statistics
649 * @veb: the VEB being updated
650 **/
651static void i40e_update_veb_stats(struct i40e_veb *veb)
652{
653 struct i40e_pf *pf = veb->pf;
654 struct i40e_hw *hw = &pf->hw;
655 struct i40e_eth_stats *oes;
656 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400657 struct i40e_veb_tc_stats *veb_oes;
658 struct i40e_veb_tc_stats *veb_es;
659 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000660
661 idx = veb->stats_idx;
662 es = &veb->stats;
663 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400664 veb_es = &veb->tc_stats;
665 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000666
667 /* Gather up the stats that the hw collects */
668 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
669 veb->stat_offsets_loaded,
670 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000671 if (hw->revision_id > 0)
672 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
673 veb->stat_offsets_loaded,
674 &oes->rx_unknown_protocol,
675 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000676 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
677 veb->stat_offsets_loaded,
678 &oes->rx_bytes, &es->rx_bytes);
679 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
680 veb->stat_offsets_loaded,
681 &oes->rx_unicast, &es->rx_unicast);
682 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
683 veb->stat_offsets_loaded,
684 &oes->rx_multicast, &es->rx_multicast);
685 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
686 veb->stat_offsets_loaded,
687 &oes->rx_broadcast, &es->rx_broadcast);
688
689 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
690 veb->stat_offsets_loaded,
691 &oes->tx_bytes, &es->tx_bytes);
692 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
693 veb->stat_offsets_loaded,
694 &oes->tx_unicast, &es->tx_unicast);
695 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
696 veb->stat_offsets_loaded,
697 &oes->tx_multicast, &es->tx_multicast);
698 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400701 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
702 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
703 I40E_GLVEBTC_RPCL(i, idx),
704 veb->stat_offsets_loaded,
705 &veb_oes->tc_rx_packets[i],
706 &veb_es->tc_rx_packets[i]);
707 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
708 I40E_GLVEBTC_RBCL(i, idx),
709 veb->stat_offsets_loaded,
710 &veb_oes->tc_rx_bytes[i],
711 &veb_es->tc_rx_bytes[i]);
712 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
713 I40E_GLVEBTC_TPCL(i, idx),
714 veb->stat_offsets_loaded,
715 &veb_oes->tc_tx_packets[i],
716 &veb_es->tc_tx_packets[i]);
717 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
718 I40E_GLVEBTC_TBCL(i, idx),
719 veb->stat_offsets_loaded,
720 &veb_oes->tc_tx_bytes[i],
721 &veb_es->tc_tx_bytes[i]);
722 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000723 veb->stat_offsets_loaded = true;
724}
725
Vasu Dev38e00432014-08-01 13:27:03 -0700726#ifdef I40E_FCOE
727/**
728 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
729 * @vsi: the VSI that is capable of doing FCoE
730 **/
731static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
732{
733 struct i40e_pf *pf = vsi->back;
734 struct i40e_hw *hw = &pf->hw;
735 struct i40e_fcoe_stats *ofs;
736 struct i40e_fcoe_stats *fs; /* device's eth stats */
737 int idx;
738
739 if (vsi->type != I40E_VSI_FCOE)
740 return;
741
Kiran Patil4147e2c2016-01-15 14:33:14 -0800742 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700743 fs = &vsi->fcoe_stats;
744 ofs = &vsi->fcoe_stats_offsets;
745
746 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
747 vsi->fcoe_stat_offsets_loaded,
748 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
749 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
750 vsi->fcoe_stat_offsets_loaded,
751 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
752 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
753 vsi->fcoe_stat_offsets_loaded,
754 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
755 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
756 vsi->fcoe_stat_offsets_loaded,
757 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
758 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
759 vsi->fcoe_stat_offsets_loaded,
760 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
761 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
762 vsi->fcoe_stat_offsets_loaded,
763 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
764 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
765 vsi->fcoe_stat_offsets_loaded,
766 &ofs->fcoe_last_error, &fs->fcoe_last_error);
767 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
768 vsi->fcoe_stat_offsets_loaded,
769 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
770
771 vsi->fcoe_stat_offsets_loaded = true;
772}
773
774#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000775/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000776 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000777 * @vsi: the VSI to be updated
778 *
779 * There are a few instances where we store the same stat in a
780 * couple of different structs. This is partly because we have
781 * the netdev stats that need to be filled out, which is slightly
782 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000783 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000784 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000785static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000786{
787 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000788 struct rtnl_link_stats64 *ons;
789 struct rtnl_link_stats64 *ns; /* netdev stats */
790 struct i40e_eth_stats *oes;
791 struct i40e_eth_stats *es; /* device's eth stats */
792 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800793 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000794 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000796 u64 bytes, packets;
797 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400798 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400799 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000800 u64 rx_p, rx_b;
801 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000802 u16 q;
803
804 if (test_bit(__I40E_DOWN, &vsi->state) ||
805 test_bit(__I40E_CONFIG_BUSY, &pf->state))
806 return;
807
808 ns = i40e_get_vsi_stats_struct(vsi);
809 ons = &vsi->net_stats_offsets;
810 es = &vsi->eth_stats;
811 oes = &vsi->eth_stats_offsets;
812
813 /* Gather up the netdev and vsi stats that the driver collects
814 * on the fly during packet processing
815 */
816 rx_b = rx_p = 0;
817 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400818 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800819 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000820 rx_page = 0;
821 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000822 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000823 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000824 /* locate Tx ring */
825 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826
Alexander Duyck980e9b12013-09-28 06:01:03 +0000827 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700828 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000829 packets = p->stats.packets;
830 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700831 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000832 tx_b += bytes;
833 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000834 tx_restart += p->tx_stats.restart_queue;
835 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400836 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400837 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800838 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000839
840 /* Rx queue is part of the same block as Tx queue */
841 p = &p[1];
842 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700843 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000844 packets = p->stats.packets;
845 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700846 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000847 rx_b += bytes;
848 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000849 rx_buf += p->rx_stats.alloc_buff_failed;
850 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000851 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000852 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000853 vsi->tx_restart = tx_restart;
854 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400855 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400856 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800857 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000858 vsi->rx_page_failed = rx_page;
859 vsi->rx_buf_failed = rx_buf;
860
861 ns->rx_packets = rx_p;
862 ns->rx_bytes = rx_b;
863 ns->tx_packets = tx_p;
864 ns->tx_bytes = tx_b;
865
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000866 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000867 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000868 ons->tx_errors = oes->tx_errors;
869 ns->tx_errors = es->tx_errors;
870 ons->multicast = oes->rx_multicast;
871 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000872 ons->rx_dropped = oes->rx_discards;
873 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874 ons->tx_dropped = oes->tx_discards;
875 ns->tx_dropped = es->tx_discards;
876
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000877 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000878 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000879 ns->rx_crc_errors = pf->stats.crc_errors;
880 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
881 ns->rx_length_errors = pf->stats.rx_length_errors;
882 }
883}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000885/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000886 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000887 * @pf: the PF to be updated
888 **/
889static void i40e_update_pf_stats(struct i40e_pf *pf)
890{
891 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
892 struct i40e_hw_port_stats *nsd = &pf->stats;
893 struct i40e_hw *hw = &pf->hw;
894 u32 val;
895 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000896
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000897 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
898 I40E_GLPRT_GORCL(hw->port),
899 pf->stat_offsets_loaded,
900 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
901 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
902 I40E_GLPRT_GOTCL(hw->port),
903 pf->stat_offsets_loaded,
904 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
905 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->eth.rx_discards,
908 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000909 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
910 I40E_GLPRT_UPRCL(hw->port),
911 pf->stat_offsets_loaded,
912 &osd->eth.rx_unicast,
913 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000914 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
915 I40E_GLPRT_MPRCL(hw->port),
916 pf->stat_offsets_loaded,
917 &osd->eth.rx_multicast,
918 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000919 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
920 I40E_GLPRT_BPRCL(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->eth.rx_broadcast,
923 &nsd->eth.rx_broadcast);
924 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
925 I40E_GLPRT_UPTCL(hw->port),
926 pf->stat_offsets_loaded,
927 &osd->eth.tx_unicast,
928 &nsd->eth.tx_unicast);
929 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
930 I40E_GLPRT_MPTCL(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->eth.tx_multicast,
933 &nsd->eth.tx_multicast);
934 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
935 I40E_GLPRT_BPTCL(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->eth.tx_broadcast,
938 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000939
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000940 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->tx_dropped_link_down,
943 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000944
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000945 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000948
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000949 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000952
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000953 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->mac_local_faults,
956 &nsd->mac_local_faults);
957 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->mac_remote_faults,
960 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000961
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000962 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->rx_length_errors,
965 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000966
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000967 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->link_xon_rx, &nsd->link_xon_rx);
970 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800973 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000976 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000979
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000980 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800981 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
982 pf->stat_offsets_loaded,
983 &osd->priority_xoff_rx[i],
984 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000986 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000987 &osd->priority_xon_rx[i],
988 &nsd->priority_xon_rx[i]);
989 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000990 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000991 &osd->priority_xon_tx[i],
992 &nsd->priority_xon_tx[i]);
993 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000994 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000995 &osd->priority_xoff_tx[i],
996 &nsd->priority_xoff_tx[i]);
997 i40e_stat_update32(hw,
998 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000999 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001000 &osd->priority_xon_2_xoff[i],
1001 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001002 }
1003
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001004 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1005 I40E_GLPRT_PRC64L(hw->port),
1006 pf->stat_offsets_loaded,
1007 &osd->rx_size_64, &nsd->rx_size_64);
1008 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1009 I40E_GLPRT_PRC127L(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->rx_size_127, &nsd->rx_size_127);
1012 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1013 I40E_GLPRT_PRC255L(hw->port),
1014 pf->stat_offsets_loaded,
1015 &osd->rx_size_255, &nsd->rx_size_255);
1016 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1017 I40E_GLPRT_PRC511L(hw->port),
1018 pf->stat_offsets_loaded,
1019 &osd->rx_size_511, &nsd->rx_size_511);
1020 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1021 I40E_GLPRT_PRC1023L(hw->port),
1022 pf->stat_offsets_loaded,
1023 &osd->rx_size_1023, &nsd->rx_size_1023);
1024 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1025 I40E_GLPRT_PRC1522L(hw->port),
1026 pf->stat_offsets_loaded,
1027 &osd->rx_size_1522, &nsd->rx_size_1522);
1028 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1029 I40E_GLPRT_PRC9522L(hw->port),
1030 pf->stat_offsets_loaded,
1031 &osd->rx_size_big, &nsd->rx_size_big);
1032
1033 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1034 I40E_GLPRT_PTC64L(hw->port),
1035 pf->stat_offsets_loaded,
1036 &osd->tx_size_64, &nsd->tx_size_64);
1037 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1038 I40E_GLPRT_PTC127L(hw->port),
1039 pf->stat_offsets_loaded,
1040 &osd->tx_size_127, &nsd->tx_size_127);
1041 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1042 I40E_GLPRT_PTC255L(hw->port),
1043 pf->stat_offsets_loaded,
1044 &osd->tx_size_255, &nsd->tx_size_255);
1045 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1046 I40E_GLPRT_PTC511L(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->tx_size_511, &nsd->tx_size_511);
1049 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1050 I40E_GLPRT_PTC1023L(hw->port),
1051 pf->stat_offsets_loaded,
1052 &osd->tx_size_1023, &nsd->tx_size_1023);
1053 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1054 I40E_GLPRT_PTC1522L(hw->port),
1055 pf->stat_offsets_loaded,
1056 &osd->tx_size_1522, &nsd->tx_size_1522);
1057 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1058 I40E_GLPRT_PTC9522L(hw->port),
1059 pf->stat_offsets_loaded,
1060 &osd->tx_size_big, &nsd->tx_size_big);
1061
1062 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1063 pf->stat_offsets_loaded,
1064 &osd->rx_undersize, &nsd->rx_undersize);
1065 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1066 pf->stat_offsets_loaded,
1067 &osd->rx_fragments, &nsd->rx_fragments);
1068 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1069 pf->stat_offsets_loaded,
1070 &osd->rx_oversize, &nsd->rx_oversize);
1071 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1072 pf->stat_offsets_loaded,
1073 &osd->rx_jabber, &nsd->rx_jabber);
1074
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001075 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001076 i40e_stat_update32(hw,
1077 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001078 pf->stat_offsets_loaded,
1079 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001080 i40e_stat_update32(hw,
1081 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001082 pf->stat_offsets_loaded,
1083 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001084 i40e_stat_update32(hw,
1085 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1086 pf->stat_offsets_loaded,
1087 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001088
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001089 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1090 nsd->tx_lpi_status =
1091 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1092 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1093 nsd->rx_lpi_status =
1094 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1095 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1096 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1097 pf->stat_offsets_loaded,
1098 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1099 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1100 pf->stat_offsets_loaded,
1101 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1102
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001103 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001104 !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001105 nsd->fd_sb_status = true;
1106 else
1107 nsd->fd_sb_status = false;
1108
1109 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001110 !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001111 nsd->fd_atr_status = true;
1112 else
1113 nsd->fd_atr_status = false;
1114
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001115 pf->stat_offsets_loaded = true;
1116}
1117
1118/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001119 * i40e_update_stats - Update the various statistics counters.
1120 * @vsi: the VSI to be updated
1121 *
1122 * Update the various stats for this VSI and its related entities.
1123 **/
1124void i40e_update_stats(struct i40e_vsi *vsi)
1125{
1126 struct i40e_pf *pf = vsi->back;
1127
1128 if (vsi == pf->vsi[pf->lan_vsi])
1129 i40e_update_pf_stats(pf);
1130
1131 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001132#ifdef I40E_FCOE
1133 i40e_update_fcoe_stats(vsi);
1134#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001135}
1136
1137/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001138 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1139 * @vsi: the VSI to be searched
1140 * @macaddr: the MAC address
1141 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001142 *
1143 * Returns ptr to the filter object or NULL
1144 **/
1145static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001146 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001147{
1148 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001149 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001150
1151 if (!vsi || !macaddr)
1152 return NULL;
1153
Jacob Keller278e7d02016-10-05 09:30:37 -07001154 key = i40e_addr_to_hkey(macaddr);
1155 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001157 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001158 return f;
1159 }
1160 return NULL;
1161}
1162
1163/**
1164 * i40e_find_mac - Find a mac addr in the macvlan filters list
1165 * @vsi: the VSI to be searched
1166 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001167 *
1168 * Returns the first filter with the provided MAC address or NULL if
1169 * MAC address was not found
1170 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001171struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001172{
1173 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001174 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001175
1176 if (!vsi || !macaddr)
1177 return NULL;
1178
Jacob Keller278e7d02016-10-05 09:30:37 -07001179 key = i40e_addr_to_hkey(macaddr);
1180 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001181 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001182 return f;
1183 }
1184 return NULL;
1185}
1186
1187/**
1188 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1189 * @vsi: the VSI to be searched
1190 *
1191 * Returns true if VSI is in vlan mode or false otherwise
1192 **/
1193bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1194{
Jacob Kellercbebb852016-10-05 09:30:40 -07001195 /* If we have a PVID, always operate in VLAN mode */
1196 if (vsi->info.pvid)
1197 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001198
Jacob Kellercbebb852016-10-05 09:30:40 -07001199 /* We need to operate in VLAN mode whenever we have any filters with
1200 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1201 * time, incurring search cost repeatedly. However, we can notice two
1202 * things:
1203 *
1204 * 1) the only place where we can gain a VLAN filter is in
1205 * i40e_add_filter.
1206 *
1207 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001208 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001209 *
1210 * Thus, we can simply use a boolean value, has_vlan_filters which we
1211 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1212 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001213 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001214 * filters here and can perform the check at the same time. This
1215 * results in avoiding embedding a loop for VLAN mode inside another
1216 * loop over all the filters, and should maintain correctness as noted
1217 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001218 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001219 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001220}
1221
1222/**
Jacob Keller489a3262016-11-11 12:39:31 -08001223 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1224 * @vsi: the VSI to configure
1225 * @tmp_add_list: list of filters ready to be added
1226 * @tmp_del_list: list of filters ready to be deleted
1227 * @vlan_filters: the number of active VLAN filters
1228 *
1229 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1230 * behave as expected. If we have any active VLAN filters remaining or about
1231 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1232 * so that they only match against untagged traffic. If we no longer have any
1233 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1234 * so that they match against both tagged and untagged traffic. In this way,
1235 * we ensure that we correctly receive the desired traffic. This ensures that
1236 * when we have an active VLAN we will receive only untagged traffic and
1237 * traffic matching active VLANs. If we have no active VLANs then we will
1238 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1239 *
1240 * Finally, in a similar fashion, this function also corrects filters when
1241 * there is an active PVID assigned to this VSI.
1242 *
1243 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1244 *
1245 * This function is only expected to be called from within
1246 * i40e_sync_vsi_filters.
1247 *
1248 * NOTE: This function expects to be called while under the
1249 * mac_filter_hash_lock
1250 */
1251static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1252 struct hlist_head *tmp_add_list,
1253 struct hlist_head *tmp_del_list,
1254 int vlan_filters)
1255{
Jacob Keller5cb25902016-12-12 15:44:15 -08001256 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001257 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001258 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001259 struct hlist_node *h;
1260 int bkt, new_vlan;
1261
1262 /* To determine if a particular filter needs to be replaced we
1263 * have the three following conditions:
1264 *
1265 * a) if we have a PVID assigned, then all filters which are
1266 * not marked as VLAN=PVID must be replaced with filters that
1267 * are.
1268 * b) otherwise, if we have any active VLANS, all filters
1269 * which are marked as VLAN=-1 must be replaced with
1270 * filters marked as VLAN=0
1271 * c) finally, if we do not have any active VLANS, all filters
1272 * which are marked as VLAN=0 must be replaced with filters
1273 * marked as VLAN=-1
1274 */
1275
1276 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001277 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001278 if (pvid && new->f->vlan != pvid)
1279 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001280 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1281 new->f->vlan = 0;
1282 else if (!vlan_filters && new->f->vlan == 0)
1283 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001284 }
1285
1286 /* Update the remaining active filters */
1287 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1288 /* Combine the checks for whether a filter needs to be changed
1289 * and then determine the new VLAN inside the if block, in
1290 * order to avoid duplicating code for adding the new filter
1291 * then deleting the old filter.
1292 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001293 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001294 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1295 (!vlan_filters && f->vlan == 0)) {
1296 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001297 if (pvid)
1298 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001299 else if (vlan_filters)
1300 new_vlan = 0;
1301 else
1302 new_vlan = I40E_VLAN_ANY;
1303
1304 /* Create the new filter */
1305 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1306 if (!add_head)
1307 return -ENOMEM;
1308
Jacob Keller671889e2016-12-02 12:33:00 -08001309 /* Create a temporary i40e_new_mac_filter */
1310 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1311 if (!new)
1312 return -ENOMEM;
1313
1314 new->f = add_head;
1315 new->state = add_head->state;
1316
1317 /* Add the new filter to the tmp list */
1318 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001319
1320 /* Put the original filter into the delete list */
1321 f->state = I40E_FILTER_REMOVE;
1322 hash_del(&f->hlist);
1323 hlist_add_head(&f->hlist, tmp_del_list);
1324 }
1325 }
1326
1327 vsi->has_vlan_filter = !!vlan_filters;
1328
1329 return 0;
1330}
1331
1332/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001333 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1334 * @vsi: the PF Main VSI - inappropriate for any other VSI
1335 * @macaddr: the MAC address
1336 *
1337 * Remove whatever filter the firmware set up so the driver can manage
1338 * its own filtering intelligently.
1339 **/
1340static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1341{
1342 struct i40e_aqc_remove_macvlan_element_data element;
1343 struct i40e_pf *pf = vsi->back;
1344
1345 /* Only appropriate for the PF main VSI */
1346 if (vsi->type != I40E_VSI_MAIN)
1347 return;
1348
1349 memset(&element, 0, sizeof(element));
1350 ether_addr_copy(element.mac_addr, macaddr);
1351 element.vlan_tag = 0;
1352 /* Ignore error returns, some firmware does it this way... */
1353 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1354 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1355
1356 memset(&element, 0, sizeof(element));
1357 ether_addr_copy(element.mac_addr, macaddr);
1358 element.vlan_tag = 0;
1359 /* ...and some firmware does it this way. */
1360 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1361 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1362 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1363}
1364
1365/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001366 * i40e_add_filter - Add a mac/vlan filter to the VSI
1367 * @vsi: the VSI to be searched
1368 * @macaddr: the MAC address
1369 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001370 *
1371 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001372 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001373 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001374 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001375 **/
1376struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001377 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001378{
1379 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001380 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001381
1382 if (!vsi || !macaddr)
1383 return NULL;
1384
Jacob Keller1bc87e82016-10-05 09:30:31 -07001385 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001386 if (!f) {
1387 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1388 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001389 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001390
Jacob Kellercbebb852016-10-05 09:30:40 -07001391 /* Update the boolean indicating if we need to function in
1392 * VLAN mode.
1393 */
1394 if (vlan >= 0)
1395 vsi->has_vlan_filter = true;
1396
Greg Rose9a173902014-05-22 06:32:02 +00001397 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001398 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001399 /* If we're in overflow promisc mode, set the state directly
1400 * to failed, so we don't bother to try sending the filter
1401 * to the hardware.
1402 */
1403 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1404 f->state = I40E_FILTER_FAILED;
1405 else
1406 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001407 INIT_HLIST_NODE(&f->hlist);
1408
1409 key = i40e_addr_to_hkey(macaddr);
1410 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001411
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001412 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1413 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1414 }
1415
Jacob Keller1bc87e82016-10-05 09:30:31 -07001416 /* If we're asked to add a filter that has been marked for removal, it
1417 * is safe to simply restore it to active state. __i40e_del_filter
1418 * will have simply deleted any filters which were previously marked
1419 * NEW or FAILED, so if it is currently marked REMOVE it must have
1420 * previously been ACTIVE. Since we haven't yet run the sync filters
1421 * task, just restore this filter to the ACTIVE state so that the
1422 * sync task leaves it in place
1423 */
1424 if (f->state == I40E_FILTER_REMOVE)
1425 f->state = I40E_FILTER_ACTIVE;
1426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001427 return f;
1428}
1429
1430/**
Jacob Keller290d2552016-10-05 09:30:36 -07001431 * __i40e_del_filter - Remove a specific filter from the VSI
1432 * @vsi: VSI to remove from
1433 * @f: the filter to remove from the list
1434 *
1435 * This function should be called instead of i40e_del_filter only if you know
1436 * the exact filter you will remove already, such as via i40e_find_filter or
1437 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001438 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001439 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001440 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001441 * ANOTHER NOTE: This function MUST be called from within the context of
1442 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1443 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001444 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001445void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001446{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001447 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001448 return;
1449
Alan Bradya410c822016-12-12 15:44:07 -08001450 /* If the filter was never added to firmware then we can just delete it
1451 * directly and we don't want to set the status to remove or else an
1452 * admin queue command will unnecessarily fire.
1453 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001454 if ((f->state == I40E_FILTER_FAILED) ||
1455 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001456 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001457 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001458 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001459 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460 }
Alan Bradya410c822016-12-12 15:44:07 -08001461
1462 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1463 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001464}
1465
1466/**
Jacob Keller290d2552016-10-05 09:30:36 -07001467 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1468 * @vsi: the VSI to be searched
1469 * @macaddr: the MAC address
1470 * @vlan: the VLAN
1471 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001472 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001473 * being held.
1474 * ANOTHER NOTE: This function MUST be called from within the context of
1475 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1476 * instead of list_for_each_entry().
1477 **/
1478void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1479{
1480 struct i40e_mac_filter *f;
1481
1482 if (!vsi || !macaddr)
1483 return;
1484
1485 f = i40e_find_filter(vsi, macaddr, vlan);
1486 __i40e_del_filter(vsi, f);
1487}
1488
1489/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001490 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001491 * @vsi: the VSI to be searched
1492 * @macaddr: the mac address to be filtered
1493 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001494 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1495 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001496 * unique vlan that already exists. If a PVID has been assigned, instead only
1497 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001498 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001499 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001500 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001501struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1502 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001503{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001504 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001505 struct hlist_node *h;
1506 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001507
1508 if (vsi->info.pvid)
1509 return i40e_add_filter(vsi, macaddr,
1510 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001511
Jacob Keller7aaf95362016-11-11 12:39:33 -08001512 if (!i40e_is_vsi_in_vlan(vsi))
1513 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1514
Jacob Keller278e7d02016-10-05 09:30:37 -07001515 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001516 if (f->state == I40E_FILTER_REMOVE)
1517 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001518 add = i40e_add_filter(vsi, macaddr, f->vlan);
1519 if (!add)
1520 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001521 }
1522
Jacob Keller5feb3d72016-10-05 09:30:34 -07001523 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001524}
1525
1526/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001527 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001528 * @vsi: the VSI to be searched
1529 * @macaddr: the mac address to be removed
1530 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001531 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1532 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001533 *
1534 * Returns 0 for success, or error
1535 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001536int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001537{
Jacob Keller278e7d02016-10-05 09:30:37 -07001538 struct i40e_mac_filter *f;
1539 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001540 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001541 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001542
Jacob Keller278e7d02016-10-05 09:30:37 -07001543 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1544 "Missing mac_filter_hash_lock\n");
1545 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001546 if (ether_addr_equal(macaddr, f->macaddr)) {
1547 __i40e_del_filter(vsi, f);
1548 found = true;
1549 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001550 }
Jacob Keller290d2552016-10-05 09:30:36 -07001551
1552 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001553 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001554 else
1555 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001556}
1557
1558/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001559 * i40e_set_mac - NDO callback to set mac address
1560 * @netdev: network interface device structure
1561 * @p: pointer to an address structure
1562 *
1563 * Returns 0 on success, negative on failure
1564 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001565#ifdef I40E_FCOE
1566int i40e_set_mac(struct net_device *netdev, void *p)
1567#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001568static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001569#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001570{
1571 struct i40e_netdev_priv *np = netdev_priv(netdev);
1572 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001573 struct i40e_pf *pf = vsi->back;
1574 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001575 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001576
1577 if (!is_valid_ether_addr(addr->sa_data))
1578 return -EADDRNOTAVAIL;
1579
Shannon Nelson30650cc2014-07-29 04:01:50 +00001580 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1581 netdev_info(netdev, "already using mac address %pM\n",
1582 addr->sa_data);
1583 return 0;
1584 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001585
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001586 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1587 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1588 return -EADDRNOTAVAIL;
1589
Shannon Nelson30650cc2014-07-29 04:01:50 +00001590 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1591 netdev_info(netdev, "returning to hw mac address %pM\n",
1592 hw->mac.addr);
1593 else
1594 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1595
Jacob Keller278e7d02016-10-05 09:30:37 -07001596 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001597 i40e_del_mac_filter(vsi, netdev->dev_addr);
1598 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001599 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001600 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001601 if (vsi->type == I40E_VSI_MAIN) {
1602 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001603
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001605 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001607 if (ret)
1608 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1609 i40e_stat_str(hw, ret),
1610 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001611 }
1612
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001613 /* schedule our worker thread which will take care of
1614 * applying the new filter changes
1615 */
1616 i40e_service_event_schedule(vsi->back);
1617 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001618}
1619
1620/**
1621 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1622 * @vsi: the VSI being setup
1623 * @ctxt: VSI context structure
1624 * @enabled_tc: Enabled TCs bitmap
1625 * @is_add: True if called before Add VSI
1626 *
1627 * Setup VSI queue mapping for enabled traffic classes.
1628 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001629#ifdef I40E_FCOE
1630void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1631 struct i40e_vsi_context *ctxt,
1632 u8 enabled_tc,
1633 bool is_add)
1634#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001635static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1636 struct i40e_vsi_context *ctxt,
1637 u8 enabled_tc,
1638 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001639#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640{
1641 struct i40e_pf *pf = vsi->back;
1642 u16 sections = 0;
1643 u8 netdev_tc = 0;
1644 u16 numtc = 0;
1645 u16 qcount;
1646 u8 offset;
1647 u16 qmap;
1648 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001649 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001650
1651 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1652 offset = 0;
1653
1654 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1655 /* Find numtc from enabled TC bitmap */
1656 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001657 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001658 numtc++;
1659 }
1660 if (!numtc) {
1661 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1662 numtc = 1;
1663 }
1664 } else {
1665 /* At least TC0 is enabled in case of non-DCB case */
1666 numtc = 1;
1667 }
1668
1669 vsi->tc_config.numtc = numtc;
1670 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001671 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001672 qcount = vsi->alloc_queue_pairs;
1673
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001674 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001675 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001676
1677 /* Setup queue offset/count for all TCs for given VSI */
1678 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1679 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001680 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001681 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001682 int pow, num_qps;
1683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001684 switch (vsi->type) {
1685 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001686 qcount = min_t(int, pf->alloc_rss_size,
1687 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001688 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001689#ifdef I40E_FCOE
1690 case I40E_VSI_FCOE:
1691 qcount = num_tc_qps;
1692 break;
1693#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001694 case I40E_VSI_FDIR:
1695 case I40E_VSI_SRIOV:
1696 case I40E_VSI_VMDQ2:
1697 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001698 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001699 WARN_ON(i != 0);
1700 break;
1701 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001702 vsi->tc_config.tc_info[i].qoffset = offset;
1703 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001704
Shannon Nelson1e200e42015-02-27 09:15:24 +00001705 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001706 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001707 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001708 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709 pow++;
1710 num_qps >>= 1;
1711 }
1712
1713 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1714 qmap =
1715 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1716 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1717
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001718 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001719 } else {
1720 /* TC is not enabled so set the offset to
1721 * default queue and allocate one queue
1722 * for the given TC.
1723 */
1724 vsi->tc_config.tc_info[i].qoffset = 0;
1725 vsi->tc_config.tc_info[i].qcount = 1;
1726 vsi->tc_config.tc_info[i].netdev_tc = 0;
1727
1728 qmap = 0;
1729 }
1730 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1731 }
1732
1733 /* Set actual Tx/Rx queue pairs */
1734 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001735 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1736 if (vsi->req_queue_pairs > 0)
1737 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001738 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001739 vsi->num_queue_pairs = pf->num_lan_msix;
1740 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001741
1742 /* Scheduler section valid can only be set for ADD VSI */
1743 if (is_add) {
1744 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1745
1746 ctxt->info.up_enable_bits = enabled_tc;
1747 }
1748 if (vsi->type == I40E_VSI_SRIOV) {
1749 ctxt->info.mapping_flags |=
1750 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1751 for (i = 0; i < vsi->num_queue_pairs; i++)
1752 ctxt->info.queue_mapping[i] =
1753 cpu_to_le16(vsi->base_queue + i);
1754 } else {
1755 ctxt->info.mapping_flags |=
1756 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1757 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1758 }
1759 ctxt->info.valid_sections |= cpu_to_le16(sections);
1760}
1761
1762/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001763 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1764 * @netdev: the netdevice
1765 * @addr: address to add
1766 *
1767 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1768 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1769 */
1770static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1771{
1772 struct i40e_netdev_priv *np = netdev_priv(netdev);
1773 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001774
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001775 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001776 return 0;
1777 else
1778 return -ENOMEM;
1779}
1780
1781/**
1782 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1783 * @netdev: the netdevice
1784 * @addr: address to add
1785 *
1786 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1787 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1788 */
1789static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1790{
1791 struct i40e_netdev_priv *np = netdev_priv(netdev);
1792 struct i40e_vsi *vsi = np->vsi;
1793
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001794 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001795
1796 return 0;
1797}
1798
1799/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001800 * i40e_set_rx_mode - NDO callback to set the netdev filters
1801 * @netdev: network interface device structure
1802 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001803#ifdef I40E_FCOE
1804void i40e_set_rx_mode(struct net_device *netdev)
1805#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001806static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001807#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001808{
1809 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001810 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001811
Jacob Keller278e7d02016-10-05 09:30:37 -07001812 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001813
Jacob Keller6622f5c2016-10-05 09:30:32 -07001814 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1815 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001816
Jacob Keller278e7d02016-10-05 09:30:37 -07001817 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001818
1819 /* check for other flag changes */
1820 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1821 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1822 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1823 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001824
1825 /* schedule our worker thread which will take care of
1826 * applying the new filter changes
1827 */
1828 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001829}
1830
1831/**
Jacob Keller671889e2016-12-02 12:33:00 -08001832 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001833 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001834 * @from: Pointer to list which contains MAC filter entries - changes to
1835 * those entries needs to be undone.
1836 *
Jacob Keller671889e2016-12-02 12:33:00 -08001837 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001838 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001839static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1840 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001841{
Jacob Keller278e7d02016-10-05 09:30:37 -07001842 struct i40e_mac_filter *f;
1843 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001844
Jacob Keller278e7d02016-10-05 09:30:37 -07001845 hlist_for_each_entry_safe(f, h, from, hlist) {
1846 u64 key = i40e_addr_to_hkey(f->macaddr);
1847
Kiran Patil21659032015-09-30 14:09:03 -04001848 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001849 hlist_del(&f->hlist);
1850 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001851 }
1852}
1853
1854/**
Jacob Keller671889e2016-12-02 12:33:00 -08001855 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1856 * @vsi: Pointer to vsi struct
1857 * @from: Pointer to list which contains MAC filter entries - changes to
1858 * those entries needs to be undone.
1859 *
1860 * MAC filter entries from this list were slated for addition.
1861 **/
1862static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1863 struct hlist_head *from)
1864{
1865 struct i40e_new_mac_filter *new;
1866 struct hlist_node *h;
1867
1868 hlist_for_each_entry_safe(new, h, from, hlist) {
1869 /* We can simply free the wrapper structure */
1870 hlist_del(&new->hlist);
1871 kfree(new);
1872 }
1873}
1874
1875/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001876 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001877 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001878 *
1879 * Returns the next non-broadcast filter in the list. Required so that we
1880 * ignore broadcast filters within the list, since these are not handled via
1881 * the normal firmware update path.
1882 */
Jacob Keller671889e2016-12-02 12:33:00 -08001883static
1884struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001885{
Jacob Keller584a8872017-03-07 15:17:52 -08001886 hlist_for_each_entry_continue(next, hlist) {
1887 if (!is_broadcast_ether_addr(next->f->macaddr))
1888 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001889 }
1890
Jacob Keller584a8872017-03-07 15:17:52 -08001891 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001892}
1893
1894/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001895 * i40e_update_filter_state - Update filter state based on return data
1896 * from firmware
1897 * @count: Number of filters added
1898 * @add_list: return data from fw
1899 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001900 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001901 * MAC filter entries from list were slated to be added to device. Returns
1902 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001903 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001904static int
1905i40e_update_filter_state(int count,
1906 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001907 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001908{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001909 int retval = 0;
1910 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001911
Jacob Kellerac9e2392016-11-11 12:39:27 -08001912 for (i = 0; i < count; i++) {
1913 /* Always check status of each filter. We don't need to check
1914 * the firmware return status because we pre-set the filter
1915 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1916 * request to the adminq. Thus, if it no longer matches then
1917 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001918 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001919 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001920 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001921 } else {
1922 add_head->state = I40E_FILTER_ACTIVE;
1923 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001924 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001925
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001926 add_head = i40e_next_filter(add_head);
1927 if (!add_head)
1928 break;
Kiran Patil21659032015-09-30 14:09:03 -04001929 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001930
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001931 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001932}
1933
1934/**
Jacob Keller00936312016-10-05 09:30:41 -07001935 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1936 * @vsi: ptr to the VSI
1937 * @vsi_name: name to display in messages
1938 * @list: the list of filters to send to firmware
1939 * @num_del: the number of filters to delete
1940 * @retval: Set to -EIO on failure to delete
1941 *
1942 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1943 * *retval instead of a return value so that success does not force ret_val to
1944 * be set to 0. This ensures that a sequence of calls to this function
1945 * preserve the previous value of *retval on successful delete.
1946 */
1947static
1948void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1949 struct i40e_aqc_remove_macvlan_element_data *list,
1950 int num_del, int *retval)
1951{
1952 struct i40e_hw *hw = &vsi->back->hw;
1953 i40e_status aq_ret;
1954 int aq_err;
1955
1956 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1957 aq_err = hw->aq.asq_last_status;
1958
1959 /* Explicitly ignore and do not report when firmware returns ENOENT */
1960 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1961 *retval = -EIO;
1962 dev_info(&vsi->back->pdev->dev,
1963 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1964 vsi_name, i40e_stat_str(hw, aq_ret),
1965 i40e_aq_str(hw, aq_err));
1966 }
1967}
1968
1969/**
1970 * i40e_aqc_add_filters - Request firmware to add a set of filters
1971 * @vsi: ptr to the VSI
1972 * @vsi_name: name to display in messages
1973 * @list: the list of filters to send to firmware
1974 * @add_head: Position in the add hlist
1975 * @num_add: the number of filters to add
1976 * @promisc_change: set to true on exit if promiscuous mode was forced on
1977 *
1978 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1979 * promisc_changed to true if the firmware has run out of space for more
1980 * filters.
1981 */
1982static
1983void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1984 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001985 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001986 int num_add, bool *promisc_changed)
1987{
1988 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001989 int aq_err, fcnt;
1990
Jacob Kellerac9e2392016-11-11 12:39:27 -08001991 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001992 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001993 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001994
1995 if (fcnt != num_add) {
1996 *promisc_changed = true;
1997 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001998 dev_warn(&vsi->back->pdev->dev,
1999 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2000 i40e_aq_str(hw, aq_err),
2001 vsi_name);
2002 }
2003}
2004
2005/**
Jacob Keller435c0842016-11-08 13:05:10 -08002006 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2007 * @vsi: pointer to the VSI
2008 * @f: filter data
2009 *
2010 * This function sets or clears the promiscuous broadcast flags for VLAN
2011 * filters in order to properly receive broadcast frames. Assumes that only
2012 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08002013 *
2014 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08002015 **/
Jacob Keller671889e2016-12-02 12:33:00 -08002016static i40e_status
2017i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2018 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08002019{
2020 bool enable = f->state == I40E_FILTER_NEW;
2021 struct i40e_hw *hw = &vsi->back->hw;
2022 i40e_status aq_ret;
2023
2024 if (f->vlan == I40E_VLAN_ANY) {
2025 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2026 vsi->seid,
2027 enable,
2028 NULL);
2029 } else {
2030 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2031 vsi->seid,
2032 enable,
2033 f->vlan,
2034 NULL);
2035 }
2036
Jacob Keller671889e2016-12-02 12:33:00 -08002037 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08002038 dev_warn(&vsi->back->pdev->dev,
2039 "Error %s setting broadcast promiscuous mode on %s\n",
2040 i40e_aq_str(hw, hw->aq.asq_last_status),
2041 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08002042
2043 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08002044}
2045
2046/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002047 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2048 * @vsi: ptr to the VSI
2049 *
2050 * Push any outstanding VSI filter changes through the AdminQ.
2051 *
2052 * Returns 0 or error value
2053 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002054int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002055{
Jacob Keller278e7d02016-10-05 09:30:37 -07002056 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002057 struct i40e_mac_filter *f;
2058 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002059 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002060 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002061 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002062 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002063 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002064 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002065 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002066 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002067 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002068 struct i40e_pf *pf;
2069 int num_add = 0;
2070 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002071 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002072 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002073 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002074 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075
2076 /* empty array typed pointers, kcalloc later */
2077 struct i40e_aqc_add_macvlan_element_data *add_list;
2078 struct i40e_aqc_remove_macvlan_element_data *del_list;
2079
2080 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2081 usleep_range(1000, 2000);
2082 pf = vsi->back;
2083
2084 if (vsi->netdev) {
2085 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2086 vsi->current_netdev_flags = vsi->netdev->flags;
2087 }
2088
Jacob Keller278e7d02016-10-05 09:30:37 -07002089 INIT_HLIST_HEAD(&tmp_add_list);
2090 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002091
Shannon Nelson2d1de822016-05-16 10:26:44 -07002092 if (vsi->type == I40E_VSI_SRIOV)
2093 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2094 else if (vsi->type != I40E_VSI_MAIN)
2095 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2098 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2099
Jacob Keller278e7d02016-10-05 09:30:37 -07002100 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002101 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002102 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002103 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002104 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002105 hash_del(&f->hlist);
2106 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002107
2108 /* Avoid counting removed filters */
2109 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002110 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002111 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002112 /* Create a temporary i40e_new_mac_filter */
2113 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2114 if (!new)
2115 goto err_no_memory_locked;
2116
2117 /* Store pointer to the real filter */
2118 new->f = f;
2119 new->state = f->state;
2120
2121 /* Add it to the hash list */
2122 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002123 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002124
Jacob Keller489a3262016-11-11 12:39:31 -08002125 /* Count the number of active (current and new) VLAN
2126 * filters we have now. Does not count filters which
2127 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002128 */
2129 if (f->vlan > 0)
2130 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002131 }
2132
Jacob Keller489a3262016-11-11 12:39:31 -08002133 retval = i40e_correct_mac_vlan_filters(vsi,
2134 &tmp_add_list,
2135 &tmp_del_list,
2136 vlan_filters);
2137 if (retval)
2138 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002139
Jacob Keller278e7d02016-10-05 09:30:37 -07002140 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002141 }
2142
2143 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002144 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002145 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002146 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002147 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002148 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002149 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002150 if (!del_list)
2151 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002152
Jacob Keller278e7d02016-10-05 09:30:37 -07002153 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002154 cmd_flags = 0;
2155
Jacob Keller435c0842016-11-08 13:05:10 -08002156 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002157 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002158 */
2159 if (is_broadcast_ether_addr(f->macaddr)) {
2160 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2161
2162 hlist_del(&f->hlist);
2163 kfree(f);
2164 continue;
2165 }
2166
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002167 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002168 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002169 if (f->vlan == I40E_VLAN_ANY) {
2170 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002171 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002172 } else {
2173 del_list[num_del].vlan_tag =
2174 cpu_to_le16((u16)(f->vlan));
2175 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002177 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2178 del_list[num_del].flags = cmd_flags;
2179 num_del++;
2180
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181 /* flush a full buffer */
2182 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002183 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2184 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002185 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002186 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187 }
Kiran Patil21659032015-09-30 14:09:03 -04002188 /* Release memory for MAC filter entries which were
2189 * synced up with HW.
2190 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002191 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002192 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002193 }
Kiran Patil21659032015-09-30 14:09:03 -04002194
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002196 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2197 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002198 }
2199
2200 kfree(del_list);
2201 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002202 }
2203
Jacob Keller278e7d02016-10-05 09:30:37 -07002204 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002205 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002206 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002207 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002208 list_size = filter_list_len *
2209 sizeof(struct i40e_aqc_add_macvlan_element_data);
2210 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002211 if (!add_list)
2212 goto err_no_memory;
2213
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002214 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002215 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002216 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2217 &vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002218 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002219 continue;
2220 }
Jacob Keller435c0842016-11-08 13:05:10 -08002221
2222 /* handle broadcast filters by updating the broadcast
2223 * promiscuous flag instead of adding a MAC filter.
2224 */
Jacob Keller671889e2016-12-02 12:33:00 -08002225 if (is_broadcast_ether_addr(new->f->macaddr)) {
2226 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2227 new->f))
2228 new->state = I40E_FILTER_FAILED;
2229 else
2230 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002231 continue;
2232 }
2233
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002234 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002235 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002236 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002237 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002238 ether_addr_copy(add_list[num_add].mac_addr,
2239 new->f->macaddr);
2240 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002241 add_list[num_add].vlan_tag = 0;
2242 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2243 } else {
2244 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002245 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002246 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002247 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002248 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002249 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002250 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002251 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2252 num_add++;
2253
2254 /* flush a full buffer */
2255 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002256 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2257 add_head, num_add,
2258 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002259 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002260 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002261 }
2262 }
2263 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002264 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2265 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002266 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002267 /* Now move all of the filters from the temp add list back to
2268 * the VSI's list.
2269 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002270 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002271 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2272 /* Only update the state if we're still NEW */
2273 if (new->f->state == I40E_FILTER_NEW)
2274 new->f->state = new->state;
2275 hlist_del(&new->hlist);
2276 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002277 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002278 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002279 kfree(add_list);
2280 add_list = NULL;
2281 }
2282
Jacob Keller38326212016-11-11 12:39:26 -08002283 /* Determine the number of active and failed filters. */
2284 spin_lock_bh(&vsi->mac_filter_hash_lock);
2285 vsi->active_filters = 0;
2286 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2287 if (f->state == I40E_FILTER_ACTIVE)
2288 vsi->active_filters++;
2289 else if (f->state == I40E_FILTER_FAILED)
2290 failed_filters++;
2291 }
2292 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2293
2294 /* If promiscuous mode has changed, we need to calculate a new
2295 * threshold for when we are safe to exit
2296 */
2297 if (promisc_changed)
2298 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2299
2300 /* Check if we are able to exit overflow promiscuous mode. We can
2301 * safely exit if we didn't just enter, we no longer have any failed
2302 * filters, and we have reduced filters below the threshold value.
2303 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002304 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002305 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002306 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002307 dev_info(&pf->pdev->dev,
2308 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2309 vsi_name);
2310 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2311 promisc_changed = true;
2312 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002313 }
2314
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002315 /* if the VF is not trusted do not do promisc */
2316 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2317 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2318 goto out;
2319 }
2320
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002321 /* check for changes in promiscuous modes */
2322 if (changed_flags & IFF_ALLMULTI) {
2323 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002325 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002326 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2327 vsi->seid,
2328 cur_multipromisc,
2329 NULL);
2330 if (aq_ret) {
2331 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002332 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002333 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002334 "set multi promisc failed on %s, err %s aq_err %s\n",
2335 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002336 i40e_stat_str(hw, aq_ret),
2337 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002338 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002339 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002340 if ((changed_flags & IFF_PROMISC) ||
2341 (promisc_changed &&
2342 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002343 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002344
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002345 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2346 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2347 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002348 if ((vsi->type == I40E_VSI_MAIN) &&
2349 (pf->lan_veb != I40E_NO_VEB) &&
2350 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002351 /* set defport ON for Main VSI instead of true promisc
2352 * this way we will get all unicast/multicast and VLAN
2353 * promisc behavior but will not get VF or VMDq traffic
2354 * replicated on the Main VSI.
2355 */
2356 if (pf->cur_promisc != cur_promisc) {
2357 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002358 if (cur_promisc)
2359 aq_ret =
2360 i40e_aq_set_default_vsi(hw,
2361 vsi->seid,
2362 NULL);
2363 else
2364 aq_ret =
2365 i40e_aq_clear_default_vsi(hw,
2366 vsi->seid,
2367 NULL);
2368 if (aq_ret) {
2369 retval = i40e_aq_rc_to_posix(aq_ret,
2370 hw->aq.asq_last_status);
2371 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002372 "Set default VSI failed on %s, err %s, aq_err %s\n",
2373 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002374 i40e_stat_str(hw, aq_ret),
2375 i40e_aq_str(hw,
2376 hw->aq.asq_last_status));
2377 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002378 }
2379 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002380 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002381 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002382 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002383 cur_promisc, NULL,
2384 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002385 if (aq_ret) {
2386 retval =
2387 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002388 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002389 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002390 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2391 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002392 i40e_stat_str(hw, aq_ret),
2393 i40e_aq_str(hw,
2394 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002395 }
2396 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002397 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002398 vsi->seid,
2399 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002400 if (aq_ret) {
2401 retval =
2402 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002403 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002404 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002405 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2406 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002407 i40e_stat_str(hw, aq_ret),
2408 i40e_aq_str(hw,
2409 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002410 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002411 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002412 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2413 vsi->seid,
2414 cur_promisc, NULL);
2415 if (aq_ret) {
2416 retval = i40e_aq_rc_to_posix(aq_ret,
2417 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002418 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002419 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002420 i40e_stat_str(hw, aq_ret),
2421 i40e_aq_str(hw,
2422 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002423 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002424 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002425out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002426 /* if something went wrong then set the changed flag so we try again */
2427 if (retval)
2428 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002430 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002431 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002432
2433err_no_memory:
2434 /* Restore elements on the temporary add and delete lists */
2435 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002436err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002437 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2438 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002439 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2440
2441 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2442 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2443 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002444}
2445
2446/**
2447 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2448 * @pf: board private structure
2449 **/
2450static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2451{
2452 int v;
2453
2454 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2455 return;
2456 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2457
Mitch Williams505682c2014-05-20 08:01:37 +00002458 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002459 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002460 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2461 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2462
2463 if (ret) {
2464 /* come back and try again later */
2465 pf->flags |= I40E_FLAG_FILTER_SYNC;
2466 break;
2467 }
2468 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002469 }
2470}
2471
2472/**
2473 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2474 * @netdev: network interface device structure
2475 * @new_mtu: new value for maximum frame size
2476 *
2477 * Returns 0 on success, negative on failure
2478 **/
2479static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2480{
2481 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002482 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002483 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485 netdev_info(netdev, "changing MTU from %d to %d\n",
2486 netdev->mtu, new_mtu);
2487 netdev->mtu = new_mtu;
2488 if (netif_running(netdev))
2489 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002490 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2491 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492 return 0;
2493}
2494
2495/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002496 * i40e_ioctl - Access the hwtstamp interface
2497 * @netdev: network interface device structure
2498 * @ifr: interface request data
2499 * @cmd: ioctl command
2500 **/
2501int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2502{
2503 struct i40e_netdev_priv *np = netdev_priv(netdev);
2504 struct i40e_pf *pf = np->vsi->back;
2505
2506 switch (cmd) {
2507 case SIOCGHWTSTAMP:
2508 return i40e_ptp_get_ts_config(pf, ifr);
2509 case SIOCSHWTSTAMP:
2510 return i40e_ptp_set_ts_config(pf, ifr);
2511 default:
2512 return -EOPNOTSUPP;
2513 }
2514}
2515
2516/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002517 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2518 * @vsi: the vsi being adjusted
2519 **/
2520void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2521{
2522 struct i40e_vsi_context ctxt;
2523 i40e_status ret;
2524
2525 if ((vsi->info.valid_sections &
2526 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2527 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2528 return; /* already enabled */
2529
2530 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2531 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2532 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2533
2534 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002535 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2537 if (ret) {
2538 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002539 "update vlan stripping failed, err %s aq_err %s\n",
2540 i40e_stat_str(&vsi->back->hw, ret),
2541 i40e_aq_str(&vsi->back->hw,
2542 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002543 }
2544}
2545
2546/**
2547 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2548 * @vsi: the vsi being adjusted
2549 **/
2550void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2551{
2552 struct i40e_vsi_context ctxt;
2553 i40e_status ret;
2554
2555 if ((vsi->info.valid_sections &
2556 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2557 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2558 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2559 return; /* already disabled */
2560
2561 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2562 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2563 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2564
2565 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002566 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002567 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2568 if (ret) {
2569 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002570 "update vlan stripping failed, err %s aq_err %s\n",
2571 i40e_stat_str(&vsi->back->hw, ret),
2572 i40e_aq_str(&vsi->back->hw,
2573 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002574 }
2575}
2576
2577/**
2578 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2579 * @netdev: network interface to be adjusted
2580 * @features: netdev features to test if VLAN offload is enabled or not
2581 **/
2582static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2583{
2584 struct i40e_netdev_priv *np = netdev_priv(netdev);
2585 struct i40e_vsi *vsi = np->vsi;
2586
2587 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2588 i40e_vlan_stripping_enable(vsi);
2589 else
2590 i40e_vlan_stripping_disable(vsi);
2591}
2592
2593/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002594 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002595 * @vsi: the vsi being configured
2596 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002597 *
2598 * This is a helper function for adding a new MAC/VLAN filter with the
2599 * specified VLAN for each existing MAC address already in the hash table.
2600 * This function does *not* perform any accounting to update filters based on
2601 * VLAN mode.
2602 *
2603 * NOTE: this function expects to be called while under the
2604 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002605 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002606int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002607{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002608 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002609 struct hlist_node *h;
2610 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002611
Jacob Keller278e7d02016-10-05 09:30:37 -07002612 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002613 if (f->state == I40E_FILTER_REMOVE)
2614 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002615 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002616 if (!add_f) {
2617 dev_info(&vsi->back->pdev->dev,
2618 "Could not add vlan filter %d for %pM\n",
2619 vid, f->macaddr);
2620 return -ENOMEM;
2621 }
2622 }
2623
Jacob Keller490a4ad2016-11-11 12:39:29 -08002624 return 0;
2625}
2626
2627/**
2628 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2629 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002630 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002631 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002632int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002633{
Jacob Keller489a3262016-11-11 12:39:31 -08002634 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002635
Jacob Kellerf94484b2016-12-07 14:05:34 -08002636 if (!vid || vsi->info.pvid)
2637 return -EINVAL;
2638
Jacob Keller490a4ad2016-11-11 12:39:29 -08002639 /* Locked once because all functions invoked below iterates list*/
2640 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002641 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002642 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002643 if (err)
2644 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002645
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002646 /* schedule our worker thread which will take care of
2647 * applying the new filter changes
2648 */
2649 i40e_service_event_schedule(vsi->back);
2650 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651}
2652
2653/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002654 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002655 * @vsi: the vsi being configured
2656 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002657 *
2658 * This function should be used to remove all VLAN filters which match the
2659 * given VID. It does not schedule the service event and does not take the
2660 * mac_filter_hash_lock so it may be combined with other operations under
2661 * a single invocation of the mac_filter_hash_lock.
2662 *
2663 * NOTE: this function expects to be called while under the
2664 * mac_filter_hash_lock
2665 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002666void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002667{
Alan Brady84f5ca62016-10-05 09:30:39 -07002668 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002669 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002670 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002671
Jacob Keller278e7d02016-10-05 09:30:37 -07002672 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002673 if (f->vlan == vid)
2674 __i40e_del_filter(vsi, f);
2675 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002676}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002677
Jacob Keller490a4ad2016-11-11 12:39:29 -08002678/**
2679 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2680 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002681 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002682 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002683void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002684{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002685 if (!vid || vsi->info.pvid)
2686 return;
2687
Jacob Keller490a4ad2016-11-11 12:39:29 -08002688 spin_lock_bh(&vsi->mac_filter_hash_lock);
2689 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002690 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002691
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002692 /* schedule our worker thread which will take care of
2693 * applying the new filter changes
2694 */
2695 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002696}
2697
2698/**
2699 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2700 * @netdev: network interface to be adjusted
2701 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002702 *
2703 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002704 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002705#ifdef I40E_FCOE
2706int i40e_vlan_rx_add_vid(struct net_device *netdev,
2707 __always_unused __be16 proto, u16 vid)
2708#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002709static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2710 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002711#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002712{
2713 struct i40e_netdev_priv *np = netdev_priv(netdev);
2714 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002715 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002716
Jacob Keller6a1127852016-10-25 16:08:49 -07002717 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002718 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002720 /* If the network stack called us with vid = 0 then
2721 * it is asking to receive priority tagged packets with
2722 * vlan id 0. Our HW receives them by default when configured
2723 * to receive untagged packets so there is no need to add an
2724 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002725 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002726 if (vid)
2727 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002728
Jacob Keller6a1127852016-10-25 16:08:49 -07002729 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002730 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002731
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002732 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002733}
2734
2735/**
2736 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2737 * @netdev: network interface to be adjusted
2738 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002739 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002740 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002741 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002742#ifdef I40E_FCOE
2743int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2744 __always_unused __be16 proto, u16 vid)
2745#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002746static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2747 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002748#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002749{
2750 struct i40e_netdev_priv *np = netdev_priv(netdev);
2751 struct i40e_vsi *vsi = np->vsi;
2752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002753 /* return code is ignored as there is nothing a user
2754 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002755 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756 */
2757 i40e_vsi_kill_vlan(vsi, vid);
2758
2759 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002761 return 0;
2762}
2763
2764/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002765 * i40e_macaddr_init - explicitly write the mac address filters
2766 *
2767 * @vsi: pointer to the vsi
2768 * @macaddr: the MAC address
2769 *
2770 * This is needed when the macaddr has been obtained by other
2771 * means than the default, e.g., from Open Firmware or IDPROM.
2772 * Returns 0 on success, negative on failure
2773 **/
2774static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2775{
2776 int ret;
2777 struct i40e_aqc_add_macvlan_element_data element;
2778
2779 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2780 I40E_AQC_WRITE_TYPE_LAA_WOL,
2781 macaddr, NULL);
2782 if (ret) {
2783 dev_info(&vsi->back->pdev->dev,
2784 "Addr change for VSI failed: %d\n", ret);
2785 return -EADDRNOTAVAIL;
2786 }
2787
2788 memset(&element, 0, sizeof(element));
2789 ether_addr_copy(element.mac_addr, macaddr);
2790 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2791 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2792 if (ret) {
2793 dev_info(&vsi->back->pdev->dev,
2794 "add filter failed err %s aq_err %s\n",
2795 i40e_stat_str(&vsi->back->hw, ret),
2796 i40e_aq_str(&vsi->back->hw,
2797 vsi->back->hw.aq.asq_last_status));
2798 }
2799 return ret;
2800}
2801
2802/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002803 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2804 * @vsi: the vsi being brought back up
2805 **/
2806static void i40e_restore_vlan(struct i40e_vsi *vsi)
2807{
2808 u16 vid;
2809
2810 if (!vsi->netdev)
2811 return;
2812
2813 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2814
2815 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2816 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2817 vid);
2818}
2819
2820/**
2821 * i40e_vsi_add_pvid - Add pvid for the VSI
2822 * @vsi: the vsi being adjusted
2823 * @vid: the vlan id to set as a PVID
2824 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002825int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002826{
2827 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002828 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002829
2830 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2831 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002832 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2833 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002834 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002835
2836 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002837 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002838 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2839 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002840 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002841 "add pvid failed, err %s aq_err %s\n",
2842 i40e_stat_str(&vsi->back->hw, ret),
2843 i40e_aq_str(&vsi->back->hw,
2844 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002845 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002846 }
2847
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002848 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002849}
2850
2851/**
2852 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2853 * @vsi: the vsi being adjusted
2854 *
2855 * Just use the vlan_rx_register() service to put it back to normal
2856 **/
2857void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2858{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002859 i40e_vlan_stripping_disable(vsi);
2860
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002861 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002862}
2863
2864/**
2865 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2866 * @vsi: ptr to the VSI
2867 *
2868 * If this function returns with an error, then it's possible one or
2869 * more of the rings is populated (while the rest are not). It is the
2870 * callers duty to clean those orphaned rings.
2871 *
2872 * Return 0 on success, negative on failure
2873 **/
2874static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2875{
2876 int i, err = 0;
2877
2878 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002879 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002880
2881 return err;
2882}
2883
2884/**
2885 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2886 * @vsi: ptr to the VSI
2887 *
2888 * Free VSI's transmit software resources
2889 **/
2890static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2891{
2892 int i;
2893
Greg Rose8e9dca52013-12-18 13:45:53 +00002894 if (!vsi->tx_rings)
2895 return;
2896
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002897 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002898 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002899 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900}
2901
2902/**
2903 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2904 * @vsi: ptr to the VSI
2905 *
2906 * If this function returns with an error, then it's possible one or
2907 * more of the rings is populated (while the rest are not). It is the
2908 * callers duty to clean those orphaned rings.
2909 *
2910 * Return 0 on success, negative on failure
2911 **/
2912static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2913{
2914 int i, err = 0;
2915
2916 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002917 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002918#ifdef I40E_FCOE
2919 i40e_fcoe_setup_ddp_resources(vsi);
2920#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002921 return err;
2922}
2923
2924/**
2925 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2926 * @vsi: ptr to the VSI
2927 *
2928 * Free all receive software resources
2929 **/
2930static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2931{
2932 int i;
2933
Greg Rose8e9dca52013-12-18 13:45:53 +00002934 if (!vsi->rx_rings)
2935 return;
2936
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002937 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002938 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002939 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002940#ifdef I40E_FCOE
2941 i40e_fcoe_free_ddp_resources(vsi);
2942#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002943}
2944
2945/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002946 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2947 * @ring: The Tx ring to configure
2948 *
2949 * This enables/disables XPS for a given Tx descriptor ring
2950 * based on the TCs enabled for the VSI that ring belongs to.
2951 **/
2952static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2953{
2954 struct i40e_vsi *vsi = ring->vsi;
2955 cpumask_var_t mask;
2956
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002957 if (!ring->q_vector || !ring->netdev)
2958 return;
2959
2960 /* Single TC mode enable XPS */
2961 if (vsi->tc_config.numtc <= 1) {
2962 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002963 netif_set_xps_queue(ring->netdev,
2964 &ring->q_vector->affinity_mask,
2965 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002966 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2967 /* Disable XPS to allow selection based on TC */
2968 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2969 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2970 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002971 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002972
2973 /* schedule our worker thread which will take care of
2974 * applying the new filter changes
2975 */
2976 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002977}
2978
2979/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002980 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2981 * @ring: The Tx ring to configure
2982 *
2983 * Configure the Tx descriptor ring in the HMC context.
2984 **/
2985static int i40e_configure_tx_ring(struct i40e_ring *ring)
2986{
2987 struct i40e_vsi *vsi = ring->vsi;
2988 u16 pf_q = vsi->base_queue + ring->queue_index;
2989 struct i40e_hw *hw = &vsi->back->hw;
2990 struct i40e_hmc_obj_txq tx_ctx;
2991 i40e_status err = 0;
2992 u32 qtx_ctl = 0;
2993
2994 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002995 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002996 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2997 ring->atr_count = 0;
2998 } else {
2999 ring->atr_sample_rate = 0;
3000 }
3001
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003002 /* configure XPS */
3003 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003004
3005 /* clear the context structure first */
3006 memset(&tx_ctx, 0, sizeof(tx_ctx));
3007
3008 tx_ctx.new_context = 1;
3009 tx_ctx.base = (ring->dma / 128);
3010 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003011 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3012 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07003013#ifdef I40E_FCOE
3014 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3015#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003016 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00003017 /* FDIR VSI tx ring can still use RS bit and writebacks */
3018 if (vsi->type != I40E_VSI_FDIR)
3019 tx_ctx.head_wb_ena = 1;
3020 tx_ctx.head_wb_addr = ring->dma +
3021 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022
3023 /* As part of VSI creation/update, FW allocates certain
3024 * Tx arbitration queue sets for each TC enabled for
3025 * the VSI. The FW returns the handles to these queue
3026 * sets as part of the response buffer to Add VSI,
3027 * Update VSI, etc. AQ commands. It is expected that
3028 * these queue set handles be associated with the Tx
3029 * queues by the driver as part of the TX queue context
3030 * initialization. This has to be done regardless of
3031 * DCB as by default everything is mapped to TC0.
3032 */
3033 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3034 tx_ctx.rdylist_act = 0;
3035
3036 /* clear the context in the HMC */
3037 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3038 if (err) {
3039 dev_info(&vsi->back->pdev->dev,
3040 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3041 ring->queue_index, pf_q, err);
3042 return -ENOMEM;
3043 }
3044
3045 /* set the context in the HMC */
3046 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3047 if (err) {
3048 dev_info(&vsi->back->pdev->dev,
3049 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3050 ring->queue_index, pf_q, err);
3051 return -ENOMEM;
3052 }
3053
3054 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00003055 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003056 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003057 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3058 I40E_QTX_CTL_VFVM_INDX_MASK;
3059 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003060 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003061 }
3062
Shannon Nelson13fd9772013-09-28 07:14:19 +00003063 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3064 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003065 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3066 i40e_flush(hw);
3067
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003068 /* cache tail off for easier writes later */
3069 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3070
3071 return 0;
3072}
3073
3074/**
3075 * i40e_configure_rx_ring - Configure a receive ring context
3076 * @ring: The Rx ring to configure
3077 *
3078 * Configure the Rx descriptor ring in the HMC context.
3079 **/
3080static int i40e_configure_rx_ring(struct i40e_ring *ring)
3081{
3082 struct i40e_vsi *vsi = ring->vsi;
3083 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3084 u16 pf_q = vsi->base_queue + ring->queue_index;
3085 struct i40e_hw *hw = &vsi->back->hw;
3086 struct i40e_hmc_obj_rxq rx_ctx;
3087 i40e_status err = 0;
3088
3089 ring->state = 0;
3090
3091 /* clear the context structure first */
3092 memset(&rx_ctx, 0, sizeof(rx_ctx));
3093
3094 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003095
3096 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003097
3098 rx_ctx.base = (ring->dma / 128);
3099 rx_ctx.qlen = ring->count;
3100
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003101 /* use 32 byte descriptors */
3102 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003104 /* descriptor type is always zero
3105 * rx_ctx.dtype = 0;
3106 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003107 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003108
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003109 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003110 if (hw->revision_id == 0)
3111 rx_ctx.lrxqthresh = 0;
3112 else
3113 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003114 rx_ctx.crcstrip = 1;
3115 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003116 /* this controls whether VLAN is stripped from inner headers */
3117 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003118#ifdef I40E_FCOE
3119 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3120#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003121 /* set the prefena field to 1 because the manual says to */
3122 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003123
3124 /* clear the context in the HMC */
3125 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3126 if (err) {
3127 dev_info(&vsi->back->pdev->dev,
3128 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3129 ring->queue_index, pf_q, err);
3130 return -ENOMEM;
3131 }
3132
3133 /* set the context in the HMC */
3134 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3135 if (err) {
3136 dev_info(&vsi->back->pdev->dev,
3137 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3138 ring->queue_index, pf_q, err);
3139 return -ENOMEM;
3140 }
3141
3142 /* cache tail for quicker writes, and clear the reg before use */
3143 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3144 writel(0, ring->tail);
3145
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003146 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003147
3148 return 0;
3149}
3150
3151/**
3152 * i40e_vsi_configure_tx - Configure the VSI for Tx
3153 * @vsi: VSI structure describing this set of rings and resources
3154 *
3155 * Configure the Tx VSI for operation.
3156 **/
3157static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3158{
3159 int err = 0;
3160 u16 i;
3161
Alexander Duyck9f65e152013-09-28 06:00:58 +00003162 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3163 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003164
3165 return err;
3166}
3167
3168/**
3169 * i40e_vsi_configure_rx - Configure the VSI for Rx
3170 * @vsi: the VSI being configured
3171 *
3172 * Configure the Rx VSI for operation.
3173 **/
3174static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3175{
3176 int err = 0;
3177 u16 i;
3178
3179 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3180 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3181 + ETH_FCS_LEN + VLAN_HLEN;
3182 else
3183 vsi->max_frame = I40E_RXBUFFER_2048;
3184
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003185 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003186
Vasu Dev38e00432014-08-01 13:27:03 -07003187#ifdef I40E_FCOE
3188 /* setup rx buffer for FCoE */
3189 if ((vsi->type == I40E_VSI_FCOE) &&
3190 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003191 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3192 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003193 }
3194
3195#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003196 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003197 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003198 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003199
3200 /* set up individual rings */
3201 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003202 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003203
3204 return err;
3205}
3206
3207/**
3208 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3209 * @vsi: ptr to the VSI
3210 **/
3211static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3212{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003213 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003214 u16 qoffset, qcount;
3215 int i, n;
3216
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003217 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3218 /* Reset the TC information */
3219 for (i = 0; i < vsi->num_queue_pairs; i++) {
3220 rx_ring = vsi->rx_rings[i];
3221 tx_ring = vsi->tx_rings[i];
3222 rx_ring->dcb_tc = 0;
3223 tx_ring->dcb_tc = 0;
3224 }
3225 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226
3227 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003228 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003229 continue;
3230
3231 qoffset = vsi->tc_config.tc_info[n].qoffset;
3232 qcount = vsi->tc_config.tc_info[n].qcount;
3233 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003234 rx_ring = vsi->rx_rings[i];
3235 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 rx_ring->dcb_tc = n;
3237 tx_ring->dcb_tc = n;
3238 }
3239 }
3240}
3241
3242/**
3243 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3244 * @vsi: ptr to the VSI
3245 **/
3246static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3247{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003248 struct i40e_pf *pf = vsi->back;
3249 int err;
3250
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003251 if (vsi->netdev)
3252 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003253
3254 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3255 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3256 if (err) {
3257 dev_warn(&pf->pdev->dev,
3258 "could not set up macaddr; err %d\n", err);
3259 }
3260 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003261}
3262
3263/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003264 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3265 * @vsi: Pointer to the targeted VSI
3266 *
3267 * This function replays the hlist on the hw where all the SB Flow Director
3268 * filters were saved.
3269 **/
3270static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3271{
3272 struct i40e_fdir_filter *filter;
3273 struct i40e_pf *pf = vsi->back;
3274 struct hlist_node *node;
3275
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003276 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3277 return;
3278
Jacob Keller6d069422017-02-06 14:38:44 -08003279 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003280 pf->fd_tcp4_filter_cnt = 0;
3281 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003282 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003283 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003284
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003285 hlist_for_each_entry_safe(filter, node,
3286 &pf->fdir_filter_list, fdir_node) {
3287 i40e_add_del_fdir(vsi, filter, true);
3288 }
3289}
3290
3291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292 * i40e_vsi_configure - Set up the VSI for action
3293 * @vsi: the VSI being configured
3294 **/
3295static int i40e_vsi_configure(struct i40e_vsi *vsi)
3296{
3297 int err;
3298
3299 i40e_set_vsi_rx_mode(vsi);
3300 i40e_restore_vlan(vsi);
3301 i40e_vsi_config_dcb_rings(vsi);
3302 err = i40e_vsi_configure_tx(vsi);
3303 if (!err)
3304 err = i40e_vsi_configure_rx(vsi);
3305
3306 return err;
3307}
3308
3309/**
3310 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3311 * @vsi: the VSI being configured
3312 **/
3313static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3314{
3315 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003316 struct i40e_hw *hw = &pf->hw;
3317 u16 vector;
3318 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003319 u32 qp;
3320
3321 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3322 * and PFINT_LNKLSTn registers, e.g.:
3323 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3324 */
3325 qp = vsi->base_queue;
3326 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003327 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003328 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3329
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003330 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003331 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3333 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3334 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003335 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003336 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3337 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3338 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003339 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003340 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003341
3342 /* Linked list for the queuepairs assigned to this vector */
3343 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3344 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003345 u32 val;
3346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3348 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3349 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3350 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3351 (I40E_QUEUE_TYPE_TX
3352 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3353
3354 wr32(hw, I40E_QINT_RQCTL(qp), val);
3355
3356 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3357 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3358 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3359 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3360 (I40E_QUEUE_TYPE_RX
3361 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3362
3363 /* Terminate the linked list */
3364 if (q == (q_vector->num_ringpairs - 1))
3365 val |= (I40E_QUEUE_END_OF_LIST
3366 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3367
3368 wr32(hw, I40E_QINT_TQCTL(qp), val);
3369 qp++;
3370 }
3371 }
3372
3373 i40e_flush(hw);
3374}
3375
3376/**
3377 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3378 * @hw: ptr to the hardware info
3379 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003380static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003381{
Jacob Kellerab437b52014-12-14 01:55:08 +00003382 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003383 u32 val;
3384
3385 /* clear things first */
3386 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3387 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3388
3389 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3390 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3391 I40E_PFINT_ICR0_ENA_GRST_MASK |
3392 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3393 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003394 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3395 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3396 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3397
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003398 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3399 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3400
Jacob Kellerab437b52014-12-14 01:55:08 +00003401 if (pf->flags & I40E_FLAG_PTP)
3402 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003404 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3405
3406 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003407 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3408 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003409
3410 /* OTHER_ITR_IDX = 0 */
3411 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3412}
3413
3414/**
3415 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3416 * @vsi: the VSI being configured
3417 **/
3418static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3419{
Alexander Duyck493fb302013-09-28 07:01:44 +00003420 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003421 struct i40e_pf *pf = vsi->back;
3422 struct i40e_hw *hw = &pf->hw;
3423 u32 val;
3424
3425 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003426 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003427 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003428 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3429 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003430 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003431 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3432 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3433
Jacob Kellerab437b52014-12-14 01:55:08 +00003434 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435
3436 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3437 wr32(hw, I40E_PFINT_LNKLST0, 0);
3438
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003439 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003440 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3441 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3442 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3443
3444 wr32(hw, I40E_QINT_RQCTL(0), val);
3445
3446 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3447 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3448 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3449
3450 wr32(hw, I40E_QINT_TQCTL(0), val);
3451 i40e_flush(hw);
3452}
3453
3454/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003455 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3456 * @pf: board private structure
3457 **/
3458void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3459{
3460 struct i40e_hw *hw = &pf->hw;
3461
3462 wr32(hw, I40E_PFINT_DYN_CTL0,
3463 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3464 i40e_flush(hw);
3465}
3466
3467/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003468 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3469 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003470 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003472void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003473{
3474 struct i40e_hw *hw = &pf->hw;
3475 u32 val;
3476
3477 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003478 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003479 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3480
3481 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3482 i40e_flush(hw);
3483}
3484
3485/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3487 * @irq: interrupt number
3488 * @data: pointer to a q_vector
3489 **/
3490static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3491{
3492 struct i40e_q_vector *q_vector = data;
3493
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003494 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003495 return IRQ_HANDLED;
3496
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003497 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003498
3499 return IRQ_HANDLED;
3500}
3501
3502/**
Alan Brady96db7762016-09-14 16:24:38 -07003503 * i40e_irq_affinity_notify - Callback for affinity changes
3504 * @notify: context as to what irq was changed
3505 * @mask: the new affinity mask
3506 *
3507 * This is a callback function used by the irq_set_affinity_notifier function
3508 * so that we may register to receive changes to the irq affinity masks.
3509 **/
3510static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3511 const cpumask_t *mask)
3512{
3513 struct i40e_q_vector *q_vector =
3514 container_of(notify, struct i40e_q_vector, affinity_notify);
3515
3516 q_vector->affinity_mask = *mask;
3517}
3518
3519/**
3520 * i40e_irq_affinity_release - Callback for affinity notifier release
3521 * @ref: internal core kernel usage
3522 *
3523 * This is a callback function used by the irq_set_affinity_notifier function
3524 * to inform the current notification subscriber that they will no longer
3525 * receive notifications.
3526 **/
3527static void i40e_irq_affinity_release(struct kref *ref) {}
3528
3529/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003530 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3531 * @vsi: the VSI being configured
3532 * @basename: name for the vector
3533 *
3534 * Allocates MSI-X vectors and requests interrupts from the kernel.
3535 **/
3536static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3537{
3538 int q_vectors = vsi->num_q_vectors;
3539 struct i40e_pf *pf = vsi->back;
3540 int base = vsi->base_vector;
3541 int rx_int_idx = 0;
3542 int tx_int_idx = 0;
3543 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003544 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003545
3546 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003547 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003548
Alan Brady96db7762016-09-14 16:24:38 -07003549 irq_num = pf->msix_entries[base + vector].vector;
3550
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003551 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003552 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3553 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3554 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003555 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003556 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3557 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003558 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003559 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3560 "%s-%s-%d", basename, "tx", tx_int_idx++);
3561 } else {
3562 /* skip this unused q_vector */
3563 continue;
3564 }
Alan Brady96db7762016-09-14 16:24:38 -07003565 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 vsi->irq_handler,
3567 0,
3568 q_vector->name,
3569 q_vector);
3570 if (err) {
3571 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003572 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573 goto free_queue_irqs;
3574 }
Alan Brady96db7762016-09-14 16:24:38 -07003575
3576 /* register for affinity change notifications */
3577 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3578 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3579 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003580 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003581 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003582 }
3583
Shannon Nelson63741842014-04-23 04:50:16 +00003584 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003585 return 0;
3586
3587free_queue_irqs:
3588 while (vector) {
3589 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003590 irq_num = pf->msix_entries[base + vector].vector;
3591 irq_set_affinity_notifier(irq_num, NULL);
3592 irq_set_affinity_hint(irq_num, NULL);
3593 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003594 }
3595 return err;
3596}
3597
3598/**
3599 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3600 * @vsi: the VSI being un-configured
3601 **/
3602static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3603{
3604 struct i40e_pf *pf = vsi->back;
3605 struct i40e_hw *hw = &pf->hw;
3606 int base = vsi->base_vector;
3607 int i;
3608
3609 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003610 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3611 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003612 }
3613
3614 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3615 for (i = vsi->base_vector;
3616 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3617 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3618
3619 i40e_flush(hw);
3620 for (i = 0; i < vsi->num_q_vectors; i++)
3621 synchronize_irq(pf->msix_entries[i + base].vector);
3622 } else {
3623 /* Legacy and MSI mode - this stops all interrupt handling */
3624 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3625 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3626 i40e_flush(hw);
3627 synchronize_irq(pf->pdev->irq);
3628 }
3629}
3630
3631/**
3632 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3633 * @vsi: the VSI being configured
3634 **/
3635static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3636{
3637 struct i40e_pf *pf = vsi->back;
3638 int i;
3639
3640 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003641 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003642 i40e_irq_dynamic_enable(vsi, i);
3643 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003644 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003645 }
3646
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003647 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003648 return 0;
3649}
3650
3651/**
3652 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3653 * @pf: board private structure
3654 **/
3655static void i40e_stop_misc_vector(struct i40e_pf *pf)
3656{
3657 /* Disable ICR 0 */
3658 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3659 i40e_flush(&pf->hw);
3660}
3661
3662/**
3663 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3664 * @irq: interrupt number
3665 * @data: pointer to a q_vector
3666 *
3667 * This is the handler used for all MSI/Legacy interrupts, and deals
3668 * with both queue and non-queue interrupts. This is also used in
3669 * MSIX mode to handle the non-queue interrupts.
3670 **/
3671static irqreturn_t i40e_intr(int irq, void *data)
3672{
3673 struct i40e_pf *pf = (struct i40e_pf *)data;
3674 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003675 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676 u32 icr0, icr0_remaining;
3677 u32 val, ena_mask;
3678
3679 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003680 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003681
Shannon Nelson116a57d2013-09-28 07:13:59 +00003682 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3683 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003684 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003685
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003686 /* if interrupt but no bits showing, must be SWINT */
3687 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3688 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3689 pf->sw_int_count++;
3690
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003691 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3692 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3693 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3694 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003695 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003696 }
3697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003698 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3699 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003700 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3701 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003702
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003703 /* We do not have a way to disarm Queue causes while leaving
3704 * interrupt enabled for all other causes, ideally
3705 * interrupt should be disabled while we are in NAPI but
3706 * this is not a performance path and napi_schedule()
3707 * can deal with rescheduling.
3708 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003709 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003710 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003711 }
3712
3713 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3714 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3715 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003716 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003717 }
3718
3719 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3720 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3721 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3722 }
3723
3724 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3725 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3726 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3727 }
3728
3729 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3730 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3731 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3732 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3733 val = rd32(hw, I40E_GLGEN_RSTAT);
3734 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3735 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003736 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003738 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003739 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003740 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003741 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003742 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003743 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744 }
3745
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003746 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3747 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3748 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003749 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3750 rd32(hw, I40E_PFHMC_ERRORINFO),
3751 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003752 }
3753
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003754 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3755 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3756
3757 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003758 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003759 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003760 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003761 }
3762
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003763 /* If a critical error is pending we have no choice but to reset the
3764 * device.
3765 * Report and mask out any remaining unexpected interrupts.
3766 */
3767 icr0_remaining = icr0 & ena_mask;
3768 if (icr0_remaining) {
3769 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3770 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003771 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003772 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003773 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003774 dev_info(&pf->pdev->dev, "device will be reset\n");
3775 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3776 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003777 }
3778 ena_mask &= ~icr0_remaining;
3779 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003780 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003781
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003782enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783 /* re-enable interrupt causes */
3784 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003785 if (!test_bit(__I40E_DOWN, &pf->state)) {
3786 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003787 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003788 }
3789
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003790 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003791}
3792
3793/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003794 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3795 * @tx_ring: tx ring to clean
3796 * @budget: how many cleans we're allowed
3797 *
3798 * Returns true if there's any budget left (e.g. the clean is finished)
3799 **/
3800static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3801{
3802 struct i40e_vsi *vsi = tx_ring->vsi;
3803 u16 i = tx_ring->next_to_clean;
3804 struct i40e_tx_buffer *tx_buf;
3805 struct i40e_tx_desc *tx_desc;
3806
3807 tx_buf = &tx_ring->tx_bi[i];
3808 tx_desc = I40E_TX_DESC(tx_ring, i);
3809 i -= tx_ring->count;
3810
3811 do {
3812 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3813
3814 /* if next_to_watch is not set then there is no work pending */
3815 if (!eop_desc)
3816 break;
3817
3818 /* prevent any other reads prior to eop_desc */
3819 read_barrier_depends();
3820
3821 /* if the descriptor isn't done, no work yet to do */
3822 if (!(eop_desc->cmd_type_offset_bsz &
3823 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3824 break;
3825
3826 /* clear next_to_watch to prevent false hangs */
3827 tx_buf->next_to_watch = NULL;
3828
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003829 tx_desc->buffer_addr = 0;
3830 tx_desc->cmd_type_offset_bsz = 0;
3831 /* move past filter desc */
3832 tx_buf++;
3833 tx_desc++;
3834 i++;
3835 if (unlikely(!i)) {
3836 i -= tx_ring->count;
3837 tx_buf = tx_ring->tx_bi;
3838 tx_desc = I40E_TX_DESC(tx_ring, 0);
3839 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003840 /* unmap skb header data */
3841 dma_unmap_single(tx_ring->dev,
3842 dma_unmap_addr(tx_buf, dma),
3843 dma_unmap_len(tx_buf, len),
3844 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003845 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3846 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003847
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003848 tx_buf->raw_buf = NULL;
3849 tx_buf->tx_flags = 0;
3850 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003851 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003852 tx_desc->buffer_addr = 0;
3853 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003854
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003855 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003856 tx_buf++;
3857 tx_desc++;
3858 i++;
3859 if (unlikely(!i)) {
3860 i -= tx_ring->count;
3861 tx_buf = tx_ring->tx_bi;
3862 tx_desc = I40E_TX_DESC(tx_ring, 0);
3863 }
3864
3865 /* update budget accounting */
3866 budget--;
3867 } while (likely(budget));
3868
3869 i += tx_ring->count;
3870 tx_ring->next_to_clean = i;
3871
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003872 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003873 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003874
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003875 return budget > 0;
3876}
3877
3878/**
3879 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3880 * @irq: interrupt number
3881 * @data: pointer to a q_vector
3882 **/
3883static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3884{
3885 struct i40e_q_vector *q_vector = data;
3886 struct i40e_vsi *vsi;
3887
3888 if (!q_vector->tx.ring)
3889 return IRQ_HANDLED;
3890
3891 vsi = q_vector->tx.ring->vsi;
3892 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3893
3894 return IRQ_HANDLED;
3895}
3896
3897/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003898 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003899 * @vsi: the VSI being configured
3900 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003901 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003903static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003904{
Alexander Duyck493fb302013-09-28 07:01:44 +00003905 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003906 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3907 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003908
3909 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003910 tx_ring->next = q_vector->tx.ring;
3911 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003912 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003913
3914 rx_ring->q_vector = q_vector;
3915 rx_ring->next = q_vector->rx.ring;
3916 q_vector->rx.ring = rx_ring;
3917 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003918}
3919
3920/**
3921 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3922 * @vsi: the VSI being configured
3923 *
3924 * This function maps descriptor rings to the queue-specific vectors
3925 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3926 * one vector per queue pair, but on a constrained vector budget, we
3927 * group the queue pairs as "efficiently" as possible.
3928 **/
3929static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3930{
3931 int qp_remaining = vsi->num_queue_pairs;
3932 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003933 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003934 int v_start = 0;
3935 int qp_idx = 0;
3936
3937 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3938 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003939 * It is also important to go through all the vectors available to be
3940 * sure that if we don't use all the vectors, that the remaining vectors
3941 * are cleared. This is especially important when decreasing the
3942 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003943 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003944 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003945 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3946
3947 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3948
3949 q_vector->num_ringpairs = num_ringpairs;
3950
3951 q_vector->rx.count = 0;
3952 q_vector->tx.count = 0;
3953 q_vector->rx.ring = NULL;
3954 q_vector->tx.ring = NULL;
3955
3956 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003957 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003958 qp_idx++;
3959 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003960 }
3961 }
3962}
3963
3964/**
3965 * i40e_vsi_request_irq - Request IRQ from the OS
3966 * @vsi: the VSI being configured
3967 * @basename: name for the vector
3968 **/
3969static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3970{
3971 struct i40e_pf *pf = vsi->back;
3972 int err;
3973
3974 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3975 err = i40e_vsi_request_irq_msix(vsi, basename);
3976 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3977 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003978 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003979 else
3980 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003981 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003982
3983 if (err)
3984 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3985
3986 return err;
3987}
3988
3989#ifdef CONFIG_NET_POLL_CONTROLLER
3990/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003991 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003992 * @netdev: network interface device structure
3993 *
3994 * This is used by netconsole to send skbs without having to re-enable
3995 * interrupts. It's not called while the normal interrupt routine is executing.
3996 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003997#ifdef I40E_FCOE
3998void i40e_netpoll(struct net_device *netdev)
3999#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004000static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07004001#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004002{
4003 struct i40e_netdev_priv *np = netdev_priv(netdev);
4004 struct i40e_vsi *vsi = np->vsi;
4005 struct i40e_pf *pf = vsi->back;
4006 int i;
4007
4008 /* if interface is down do nothing */
4009 if (test_bit(__I40E_DOWN, &vsi->state))
4010 return;
4011
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004012 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4013 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004014 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004015 } else {
4016 i40e_intr(pf->pdev->irq, netdev);
4017 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004018}
4019#endif
4020
4021/**
Neerav Parikh23527302014-06-03 23:50:15 +00004022 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4023 * @pf: the PF being configured
4024 * @pf_q: the PF queue
4025 * @enable: enable or disable state of the queue
4026 *
4027 * This routine will wait for the given Tx queue of the PF to reach the
4028 * enabled or disabled state.
4029 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4030 * multiple retries; else will return 0 in case of success.
4031 **/
4032static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4033{
4034 int i;
4035 u32 tx_reg;
4036
4037 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4038 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4039 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4040 break;
4041
Neerav Parikhf98a2002014-09-13 07:40:44 +00004042 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004043 }
4044 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4045 return -ETIMEDOUT;
4046
4047 return 0;
4048}
4049
4050/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004051 * i40e_vsi_control_tx - Start or stop a VSI's rings
4052 * @vsi: the VSI being configured
4053 * @enable: start or stop the rings
4054 **/
4055static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4056{
4057 struct i40e_pf *pf = vsi->back;
4058 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004059 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004060 u32 tx_reg;
4061
4062 pf_q = vsi->base_queue;
4063 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00004064
4065 /* warn the TX unit of coming changes */
4066 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4067 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00004068 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00004069
Mitch Williams6c5ef622014-02-20 19:29:16 -08004070 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004071 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004072 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4073 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4074 break;
4075 usleep_range(1000, 2000);
4076 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00004077 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00004078 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00004079 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004080
4081 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004082 if (enable) {
4083 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08004084 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004085 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004086 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004087 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004088
4089 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004090 /* No waiting for the Tx queue to disable */
4091 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4092 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004093
4094 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004095 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4096 if (ret) {
4097 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004098 "VSI seid %d Tx ring %d %sable timeout\n",
4099 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004100 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004101 }
4102 }
4103
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004104 if (hw->revision_id == 0)
4105 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00004106 return ret;
4107}
4108
4109/**
4110 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4111 * @pf: the PF being configured
4112 * @pf_q: the PF queue
4113 * @enable: enable or disable state of the queue
4114 *
4115 * This routine will wait for the given Rx queue of the PF to reach the
4116 * enabled or disabled state.
4117 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4118 * multiple retries; else will return 0 in case of success.
4119 **/
4120static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4121{
4122 int i;
4123 u32 rx_reg;
4124
4125 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4126 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4127 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4128 break;
4129
Neerav Parikhf98a2002014-09-13 07:40:44 +00004130 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004131 }
4132 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4133 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004134
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135 return 0;
4136}
4137
4138/**
4139 * i40e_vsi_control_rx - Start or stop a VSI's rings
4140 * @vsi: the VSI being configured
4141 * @enable: start or stop the rings
4142 **/
4143static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4144{
4145 struct i40e_pf *pf = vsi->back;
4146 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004147 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004148 u32 rx_reg;
4149
4150 pf_q = vsi->base_queue;
4151 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004152 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004153 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004154 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4155 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4156 break;
4157 usleep_range(1000, 2000);
4158 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004159
Catherine Sullivan7c122002014-03-14 07:32:29 +00004160 /* Skip if the queue is already in the requested state */
4161 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4162 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004163
4164 /* turn on/off the queue */
4165 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004166 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004167 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004168 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004169 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004170 /* No waiting for the Tx queue to disable */
4171 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4172 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004173
4174 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004175 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4176 if (ret) {
4177 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004178 "VSI seid %d Rx ring %d %sable timeout\n",
4179 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004180 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004181 }
4182 }
4183
Neerav Parikh23527302014-06-03 23:50:15 +00004184 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004185}
4186
4187/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004188 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004189 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004190 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004191int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004192{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004193 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004194
4195 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004196 ret = i40e_vsi_control_rx(vsi, true);
4197 if (ret)
4198 return ret;
4199 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004200
4201 return ret;
4202}
4203
4204/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004205 * i40e_vsi_stop_rings - Stop a VSI's rings
4206 * @vsi: the VSI being configured
4207 **/
4208void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4209{
4210 /* do rx first for enable and last for disable
4211 * Ignore return value, we need to shutdown whatever we can
4212 */
4213 i40e_vsi_control_tx(vsi, false);
4214 i40e_vsi_control_rx(vsi, false);
4215}
4216
4217/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004218 * i40e_vsi_free_irq - Free the irq association with the OS
4219 * @vsi: the VSI being configured
4220 **/
4221static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4222{
4223 struct i40e_pf *pf = vsi->back;
4224 struct i40e_hw *hw = &pf->hw;
4225 int base = vsi->base_vector;
4226 u32 val, qp;
4227 int i;
4228
4229 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4230 if (!vsi->q_vectors)
4231 return;
4232
Shannon Nelson63741842014-04-23 04:50:16 +00004233 if (!vsi->irqs_ready)
4234 return;
4235
4236 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004237 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004238 int irq_num;
4239 u16 vector;
4240
4241 vector = i + base;
4242 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004243
4244 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004245 if (!vsi->q_vectors[i] ||
4246 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004247 continue;
4248
Alan Brady96db7762016-09-14 16:24:38 -07004249 /* clear the affinity notifier in the IRQ descriptor */
4250 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004251 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004252 irq_set_affinity_hint(irq_num, NULL);
4253 synchronize_irq(irq_num);
4254 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004255
4256 /* Tear down the interrupt queue link list
4257 *
4258 * We know that they come in pairs and always
4259 * the Rx first, then the Tx. To clear the
4260 * link list, stick the EOL value into the
4261 * next_q field of the registers.
4262 */
4263 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4264 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4265 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4266 val |= I40E_QUEUE_END_OF_LIST
4267 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4268 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4269
4270 while (qp != I40E_QUEUE_END_OF_LIST) {
4271 u32 next;
4272
4273 val = rd32(hw, I40E_QINT_RQCTL(qp));
4274
4275 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4276 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4277 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4278 I40E_QINT_RQCTL_INTEVENT_MASK);
4279
4280 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4281 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4282
4283 wr32(hw, I40E_QINT_RQCTL(qp), val);
4284
4285 val = rd32(hw, I40E_QINT_TQCTL(qp));
4286
4287 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4288 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4289
4290 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4291 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4292 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4293 I40E_QINT_TQCTL_INTEVENT_MASK);
4294
4295 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4296 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4297
4298 wr32(hw, I40E_QINT_TQCTL(qp), val);
4299 qp = next;
4300 }
4301 }
4302 } else {
4303 free_irq(pf->pdev->irq, pf);
4304
4305 val = rd32(hw, I40E_PFINT_LNKLST0);
4306 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4307 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4308 val |= I40E_QUEUE_END_OF_LIST
4309 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4310 wr32(hw, I40E_PFINT_LNKLST0, val);
4311
4312 val = rd32(hw, I40E_QINT_RQCTL(qp));
4313 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4314 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4315 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4316 I40E_QINT_RQCTL_INTEVENT_MASK);
4317
4318 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4319 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4320
4321 wr32(hw, I40E_QINT_RQCTL(qp), val);
4322
4323 val = rd32(hw, I40E_QINT_TQCTL(qp));
4324
4325 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4326 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4327 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4328 I40E_QINT_TQCTL_INTEVENT_MASK);
4329
4330 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4331 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4332
4333 wr32(hw, I40E_QINT_TQCTL(qp), val);
4334 }
4335}
4336
4337/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004338 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4339 * @vsi: the VSI being configured
4340 * @v_idx: Index of vector to be freed
4341 *
4342 * This function frees the memory allocated to the q_vector. In addition if
4343 * NAPI is enabled it will delete any references to the NAPI struct prior
4344 * to freeing the q_vector.
4345 **/
4346static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4347{
4348 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004349 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004350
4351 if (!q_vector)
4352 return;
4353
4354 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004355 i40e_for_each_ring(ring, q_vector->tx)
4356 ring->q_vector = NULL;
4357
4358 i40e_for_each_ring(ring, q_vector->rx)
4359 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004360
4361 /* only VSI w/ an associated netdev is set up w/ NAPI */
4362 if (vsi->netdev)
4363 netif_napi_del(&q_vector->napi);
4364
4365 vsi->q_vectors[v_idx] = NULL;
4366
4367 kfree_rcu(q_vector, rcu);
4368}
4369
4370/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004371 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4372 * @vsi: the VSI being un-configured
4373 *
4374 * This frees the memory allocated to the q_vectors and
4375 * deletes references to the NAPI struct.
4376 **/
4377static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4378{
4379 int v_idx;
4380
Alexander Duyck493fb302013-09-28 07:01:44 +00004381 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4382 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004383}
4384
4385/**
4386 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4387 * @pf: board private structure
4388 **/
4389static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4390{
4391 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4392 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4393 pci_disable_msix(pf->pdev);
4394 kfree(pf->msix_entries);
4395 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004396 kfree(pf->irq_pile);
4397 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004398 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4399 pci_disable_msi(pf->pdev);
4400 }
4401 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4402}
4403
4404/**
4405 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4406 * @pf: board private structure
4407 *
4408 * We go through and clear interrupt specific resources and reset the structure
4409 * to pre-load conditions
4410 **/
4411static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4412{
4413 int i;
4414
Shannon Nelsone1477582015-02-21 06:44:33 +00004415 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004416 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004417 synchronize_irq(pf->msix_entries[0].vector);
4418 free_irq(pf->msix_entries[0].vector, pf);
4419 }
4420
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004421 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4422 I40E_IWARP_IRQ_PILE_ID);
4423
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004424 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004425 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004426 if (pf->vsi[i])
4427 i40e_vsi_free_q_vectors(pf->vsi[i]);
4428 i40e_reset_interrupt_capability(pf);
4429}
4430
4431/**
4432 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4433 * @vsi: the VSI being configured
4434 **/
4435static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4436{
4437 int q_idx;
4438
4439 if (!vsi->netdev)
4440 return;
4441
4442 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004443 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004444}
4445
4446/**
4447 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4448 * @vsi: the VSI being configured
4449 **/
4450static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4451{
4452 int q_idx;
4453
4454 if (!vsi->netdev)
4455 return;
4456
4457 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004458 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004459}
4460
4461/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004462 * i40e_vsi_close - Shut down a VSI
4463 * @vsi: the vsi to be quelled
4464 **/
4465static void i40e_vsi_close(struct i40e_vsi *vsi)
4466{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004467 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004468 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4469 i40e_down(vsi);
4470 i40e_vsi_free_irq(vsi);
4471 i40e_vsi_free_tx_resources(vsi);
4472 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004473 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004474 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4475 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4476 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004477}
4478
4479/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004480 * i40e_quiesce_vsi - Pause a given VSI
4481 * @vsi: the VSI being paused
4482 **/
4483static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4484{
4485 if (test_bit(__I40E_DOWN, &vsi->state))
4486 return;
4487
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004488 /* No need to disable FCoE VSI when Tx suspended */
4489 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4490 vsi->type == I40E_VSI_FCOE) {
4491 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004492 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004493 return;
4494 }
4495
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004496 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004497 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004498 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004499 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004500 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004501}
4502
4503/**
4504 * i40e_unquiesce_vsi - Resume a given VSI
4505 * @vsi: the VSI being resumed
4506 **/
4507static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4508{
4509 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4510 return;
4511
4512 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4513 if (vsi->netdev && netif_running(vsi->netdev))
4514 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4515 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004516 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004517}
4518
4519/**
4520 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4521 * @pf: the PF
4522 **/
4523static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4524{
4525 int v;
4526
Mitch Williams505682c2014-05-20 08:01:37 +00004527 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004528 if (pf->vsi[v])
4529 i40e_quiesce_vsi(pf->vsi[v]);
4530 }
4531}
4532
4533/**
4534 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4535 * @pf: the PF
4536 **/
4537static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4538{
4539 int v;
4540
Mitch Williams505682c2014-05-20 08:01:37 +00004541 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004542 if (pf->vsi[v])
4543 i40e_unquiesce_vsi(pf->vsi[v]);
4544 }
4545}
4546
Neerav Parikh69129dc2014-11-12 00:18:46 +00004547#ifdef CONFIG_I40E_DCB
4548/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004549 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004550 * @vsi: the VSI being configured
4551 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004552 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004553 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004554static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004555{
4556 struct i40e_pf *pf = vsi->back;
4557 int i, pf_q, ret;
4558
4559 pf_q = vsi->base_queue;
4560 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4561 /* Check and wait for the disable status of the queue */
4562 ret = i40e_pf_txq_wait(pf, pf_q, false);
4563 if (ret) {
4564 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004565 "VSI seid %d Tx ring %d disable timeout\n",
4566 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004567 return ret;
4568 }
4569 }
4570
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004571 pf_q = vsi->base_queue;
4572 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4573 /* Check and wait for the disable status of the queue */
4574 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4575 if (ret) {
4576 dev_info(&pf->pdev->dev,
4577 "VSI seid %d Rx ring %d disable timeout\n",
4578 vsi->seid, pf_q);
4579 return ret;
4580 }
4581 }
4582
Neerav Parikh69129dc2014-11-12 00:18:46 +00004583 return 0;
4584}
4585
4586/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004587 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004588 * @pf: the PF
4589 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004590 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004591 * VSIs that are managed by this PF.
4592 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004593static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004594{
4595 int v, ret = 0;
4596
4597 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004598 /* No need to wait for FCoE VSI queues */
4599 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004600 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004601 if (ret)
4602 break;
4603 }
4604 }
4605
4606 return ret;
4607}
4608
4609#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004610
4611/**
4612 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4613 * @q_idx: TX queue number
4614 * @vsi: Pointer to VSI struct
4615 *
4616 * This function checks specified queue for given VSI. Detects hung condition.
4617 * Sets hung bit since it is two step process. Before next run of service task
4618 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4619 * hung condition remain unchanged and during subsequent run, this function
4620 * issues SW interrupt to recover from hung condition.
4621 **/
4622static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4623{
4624 struct i40e_ring *tx_ring = NULL;
4625 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004626 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004627 int i;
4628
4629 pf = vsi->back;
4630
4631 /* now that we have an index, find the tx_ring struct */
4632 for (i = 0; i < vsi->num_queue_pairs; i++) {
4633 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4634 if (q_idx == vsi->tx_rings[i]->queue_index) {
4635 tx_ring = vsi->tx_rings[i];
4636 break;
4637 }
4638 }
4639 }
4640
4641 if (!tx_ring)
4642 return;
4643
4644 /* Read interrupt register */
4645 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4646 val = rd32(&pf->hw,
4647 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4648 tx_ring->vsi->base_vector - 1));
4649 else
4650 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4651
4652 head = i40e_get_head(tx_ring);
4653
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004654 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004655
Kiran Patil9c6c1252015-11-06 15:26:02 -08004656 /* HW is done executing descriptors, updated HEAD write back,
4657 * but SW hasn't processed those descriptors. If interrupt is
4658 * not generated from this point ON, it could result into
4659 * dev_watchdog detecting timeout on those netdev_queue,
4660 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004661 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004662 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004663 /* NAPI Poll didn't run and clear since it was set */
4664 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4665 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004666 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",
4667 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004668 tx_ring->next_to_clean, head,
4669 tx_ring->next_to_use,
4670 readl(tx_ring->tail));
4671 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4672 vsi->seid, q_idx, val);
4673 i40e_force_wb(vsi, tx_ring->q_vector);
4674 } else {
4675 /* First Chance - detected possible hung */
4676 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4677 &tx_ring->q_vector->hung_detected);
4678 }
4679 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004680
4681 /* This is the case where we have interrupts missing,
4682 * so the tx_pending in HW will most likely be 0, but we
4683 * will have tx_pending in SW since the WB happened but the
4684 * interrupt got lost.
4685 */
4686 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4687 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004688 local_bh_disable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004689 if (napi_reschedule(&tx_ring->q_vector->napi))
4690 tx_ring->tx_stats.tx_lost_interrupt++;
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004691 local_bh_enable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004692 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004693}
4694
4695/**
4696 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4697 * @pf: pointer to PF struct
4698 *
4699 * LAN VSI has netdev and netdev has TX queues. This function is to check
4700 * each of those TX queues if they are hung, trigger recovery by issuing
4701 * SW interrupt.
4702 **/
4703static void i40e_detect_recover_hung(struct i40e_pf *pf)
4704{
4705 struct net_device *netdev;
4706 struct i40e_vsi *vsi;
4707 int i;
4708
4709 /* Only for LAN VSI */
4710 vsi = pf->vsi[pf->lan_vsi];
4711
4712 if (!vsi)
4713 return;
4714
4715 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4716 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4717 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4718 return;
4719
4720 /* Make sure type is MAIN VSI */
4721 if (vsi->type != I40E_VSI_MAIN)
4722 return;
4723
4724 netdev = vsi->netdev;
4725 if (!netdev)
4726 return;
4727
4728 /* Bail out if netif_carrier is not OK */
4729 if (!netif_carrier_ok(netdev))
4730 return;
4731
4732 /* Go thru' TX queues for netdev */
4733 for (i = 0; i < netdev->num_tx_queues; i++) {
4734 struct netdev_queue *q;
4735
4736 q = netdev_get_tx_queue(netdev, i);
4737 if (q)
4738 i40e_detect_recover_hung_queue(i, vsi);
4739 }
4740}
4741
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004742/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004743 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004744 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004745 *
4746 * Get TC map for ISCSI PF type that will include iSCSI TC
4747 * and LAN TC.
4748 **/
4749static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4750{
4751 struct i40e_dcb_app_priority_table app;
4752 struct i40e_hw *hw = &pf->hw;
4753 u8 enabled_tc = 1; /* TC0 is always enabled */
4754 u8 tc, i;
4755 /* Get the iSCSI APP TLV */
4756 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4757
4758 for (i = 0; i < dcbcfg->numapps; i++) {
4759 app = dcbcfg->app[i];
4760 if (app.selector == I40E_APP_SEL_TCPIP &&
4761 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4762 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004763 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004764 break;
4765 }
4766 }
4767
4768 return enabled_tc;
4769}
4770
4771/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004772 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4773 * @dcbcfg: the corresponding DCBx configuration structure
4774 *
4775 * Return the number of TCs from given DCBx configuration
4776 **/
4777static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4778{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004779 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004780 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004781 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004782
4783 /* Scan the ETS Config Priority Table to find
4784 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004785 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004786 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004787 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4788 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4789
4790 /* Now scan the bitmask to check for
4791 * contiguous TCs starting with TC0
4792 */
4793 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4794 if (num_tc & BIT(i)) {
4795 if (!tc_unused) {
4796 ret++;
4797 } else {
4798 pr_err("Non-contiguous TC - Disabling DCB\n");
4799 return 1;
4800 }
4801 } else {
4802 tc_unused = 1;
4803 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004804 }
4805
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004806 /* There is always at least TC0 */
4807 if (!ret)
4808 ret = 1;
4809
4810 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811}
4812
4813/**
4814 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4815 * @dcbcfg: the corresponding DCBx configuration structure
4816 *
4817 * Query the current DCB configuration and return the number of
4818 * traffic classes enabled from the given DCBX config
4819 **/
4820static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4821{
4822 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4823 u8 enabled_tc = 1;
4824 u8 i;
4825
4826 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004827 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004828
4829 return enabled_tc;
4830}
4831
4832/**
4833 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4834 * @pf: PF being queried
4835 *
4836 * Return number of traffic classes enabled for the given PF
4837 **/
4838static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4839{
4840 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004841 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004842 u8 num_tc = 0;
4843 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4844
4845 /* If DCB is not enabled then always in single TC */
4846 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4847 return 1;
4848
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004850 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4851 return i40e_dcb_get_num_tc(dcbcfg);
4852
4853 /* MFP mode return count of enabled TCs for this PF */
4854 if (pf->hw.func_caps.iscsi)
4855 enabled_tc = i40e_get_iscsi_tc_map(pf);
4856 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004857 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004858
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004859 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004860 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004861 num_tc++;
4862 }
4863 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004864}
4865
4866/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004867 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4868 * @pf: PF being queried
4869 *
4870 * Return a bitmap for enabled traffic classes for this PF.
4871 **/
4872static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4873{
4874 /* If DCB is not enabled for this PF then just return default TC */
4875 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004876 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004877
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004878 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004879 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4880 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4881
Neerav Parikhfc51de92015-02-24 06:58:53 +00004882 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004883 if (pf->hw.func_caps.iscsi)
4884 return i40e_get_iscsi_tc_map(pf);
4885 else
David Ertmanea6acb72016-09-20 07:10:50 -07004886 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004887}
4888
4889/**
4890 * i40e_vsi_get_bw_info - Query VSI BW Information
4891 * @vsi: the VSI being queried
4892 *
4893 * Returns 0 on success, negative value on failure
4894 **/
4895static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4896{
4897 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4898 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4899 struct i40e_pf *pf = vsi->back;
4900 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004901 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004902 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004903 int i;
4904
4905 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004906 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4907 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004908 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004909 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4910 i40e_stat_str(&pf->hw, ret),
4911 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004912 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004913 }
4914
4915 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004916 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4917 NULL);
4918 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004919 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004920 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4921 i40e_stat_str(&pf->hw, ret),
4922 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004923 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004924 }
4925
4926 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4927 dev_info(&pf->pdev->dev,
4928 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4929 bw_config.tc_valid_bits,
4930 bw_ets_config.tc_valid_bits);
4931 /* Still continuing */
4932 }
4933
4934 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4935 vsi->bw_max_quanta = bw_config.max_bw;
4936 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4937 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4938 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4939 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4940 vsi->bw_ets_limit_credits[i] =
4941 le16_to_cpu(bw_ets_config.credits[i]);
4942 /* 3 bits out of 4 for each TC */
4943 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4944 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004945
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004946 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004947}
4948
4949/**
4950 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4951 * @vsi: the VSI being configured
4952 * @enabled_tc: TC bitmap
4953 * @bw_credits: BW shared credits per TC
4954 *
4955 * Returns 0 on success, negative value on failure
4956 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004957static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004958 u8 *bw_share)
4959{
4960 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004961 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004962 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004963
4964 bw_data.tc_valid_bits = enabled_tc;
4965 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4966 bw_data.tc_bw_credits[i] = bw_share[i];
4967
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004968 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4969 NULL);
4970 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004971 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004972 "AQ command Config VSI BW allocation per TC failed = %d\n",
4973 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004974 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004975 }
4976
4977 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4978 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4979
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004980 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004981}
4982
4983/**
4984 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4985 * @vsi: the VSI being configured
4986 * @enabled_tc: TC map to be enabled
4987 *
4988 **/
4989static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4990{
4991 struct net_device *netdev = vsi->netdev;
4992 struct i40e_pf *pf = vsi->back;
4993 struct i40e_hw *hw = &pf->hw;
4994 u8 netdev_tc = 0;
4995 int i;
4996 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4997
4998 if (!netdev)
4999 return;
5000
5001 if (!enabled_tc) {
5002 netdev_reset_tc(netdev);
5003 return;
5004 }
5005
5006 /* Set up actual enabled TCs on the VSI */
5007 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5008 return;
5009
5010 /* set per TC queues for the VSI */
5011 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5012 /* Only set TC queues for enabled tcs
5013 *
5014 * e.g. For a VSI that has TC0 and TC3 enabled the
5015 * enabled_tc bitmap would be 0x00001001; the driver
5016 * will set the numtc for netdev as 2 that will be
5017 * referenced by the netdev layer as TC 0 and 1.
5018 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005019 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005020 netdev_set_tc_queue(netdev,
5021 vsi->tc_config.tc_info[i].netdev_tc,
5022 vsi->tc_config.tc_info[i].qcount,
5023 vsi->tc_config.tc_info[i].qoffset);
5024 }
5025
5026 /* Assign UP2TC map for the VSI */
5027 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5028 /* Get the actual TC# for the UP */
5029 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5030 /* Get the mapped netdev TC# for the UP */
5031 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5032 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5033 }
5034}
5035
5036/**
5037 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5038 * @vsi: the VSI being configured
5039 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5040 **/
5041static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5042 struct i40e_vsi_context *ctxt)
5043{
5044 /* copy just the sections touched not the entire info
5045 * since not all sections are valid as returned by
5046 * update vsi params
5047 */
5048 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5049 memcpy(&vsi->info.queue_mapping,
5050 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5051 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5052 sizeof(vsi->info.tc_mapping));
5053}
5054
5055/**
5056 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5057 * @vsi: VSI to be configured
5058 * @enabled_tc: TC bitmap
5059 *
5060 * This configures a particular VSI for TCs that are mapped to the
5061 * given TC bitmap. It uses default bandwidth share for TCs across
5062 * VSIs to configure TC for a particular VSI.
5063 *
5064 * NOTE:
5065 * It is expected that the VSI queues have been quisced before calling
5066 * this function.
5067 **/
5068static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5069{
5070 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5071 struct i40e_vsi_context ctxt;
5072 int ret = 0;
5073 int i;
5074
5075 /* Check if enabled_tc is same as existing or new TCs */
5076 if (vsi->tc_config.enabled_tc == enabled_tc)
5077 return ret;
5078
5079 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5080 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005081 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005082 bw_share[i] = 1;
5083 }
5084
5085 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5086 if (ret) {
5087 dev_info(&vsi->back->pdev->dev,
5088 "Failed configuring TC map %d for VSI %d\n",
5089 enabled_tc, vsi->seid);
5090 goto out;
5091 }
5092
5093 /* Update Queue Pairs Mapping for currently enabled UPs */
5094 ctxt.seid = vsi->seid;
5095 ctxt.pf_num = vsi->back->hw.pf_id;
5096 ctxt.vf_num = 0;
5097 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005098 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005099 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5100
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005101 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5102 ctxt.info.valid_sections |=
5103 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5104 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5105 }
5106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005107 /* Update the VSI after updating the VSI queue-mapping information */
5108 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5109 if (ret) {
5110 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005111 "Update vsi tc config failed, err %s aq_err %s\n",
5112 i40e_stat_str(&vsi->back->hw, ret),
5113 i40e_aq_str(&vsi->back->hw,
5114 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005115 goto out;
5116 }
5117 /* update the local VSI info with updated queue map */
5118 i40e_vsi_update_queue_map(vsi, &ctxt);
5119 vsi->info.valid_sections = 0;
5120
5121 /* Update current VSI BW information */
5122 ret = i40e_vsi_get_bw_info(vsi);
5123 if (ret) {
5124 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005125 "Failed updating vsi bw info, err %s aq_err %s\n",
5126 i40e_stat_str(&vsi->back->hw, ret),
5127 i40e_aq_str(&vsi->back->hw,
5128 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005129 goto out;
5130 }
5131
5132 /* Update the netdev TC setup */
5133 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5134out:
5135 return ret;
5136}
5137
5138/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005139 * i40e_veb_config_tc - Configure TCs for given VEB
5140 * @veb: given VEB
5141 * @enabled_tc: TC bitmap
5142 *
5143 * Configures given TC bitmap for VEB (switching) element
5144 **/
5145int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5146{
5147 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5148 struct i40e_pf *pf = veb->pf;
5149 int ret = 0;
5150 int i;
5151
5152 /* No TCs or already enabled TCs just return */
5153 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5154 return ret;
5155
5156 bw_data.tc_valid_bits = enabled_tc;
5157 /* bw_data.absolute_credits is not set (relative) */
5158
5159 /* Enable ETS TCs with equal BW Share for now */
5160 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005161 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005162 bw_data.tc_bw_share_credits[i] = 1;
5163 }
5164
5165 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5166 &bw_data, NULL);
5167 if (ret) {
5168 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005169 "VEB bw config failed, err %s aq_err %s\n",
5170 i40e_stat_str(&pf->hw, ret),
5171 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005172 goto out;
5173 }
5174
5175 /* Update the BW information */
5176 ret = i40e_veb_get_bw_info(veb);
5177 if (ret) {
5178 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005179 "Failed getting veb bw config, err %s aq_err %s\n",
5180 i40e_stat_str(&pf->hw, ret),
5181 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005182 }
5183
5184out:
5185 return ret;
5186}
5187
5188#ifdef CONFIG_I40E_DCB
5189/**
5190 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5191 * @pf: PF struct
5192 *
5193 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5194 * the caller would've quiesce all the VSIs before calling
5195 * this function
5196 **/
5197static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5198{
5199 u8 tc_map = 0;
5200 int ret;
5201 u8 v;
5202
5203 /* Enable the TCs available on PF to all VEBs */
5204 tc_map = i40e_pf_get_tc_map(pf);
5205 for (v = 0; v < I40E_MAX_VEB; v++) {
5206 if (!pf->veb[v])
5207 continue;
5208 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5209 if (ret) {
5210 dev_info(&pf->pdev->dev,
5211 "Failed configuring TC for VEB seid=%d\n",
5212 pf->veb[v]->seid);
5213 /* Will try to configure as many components */
5214 }
5215 }
5216
5217 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005218 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005219 if (!pf->vsi[v])
5220 continue;
5221
5222 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005223#ifdef I40E_FCOE
5224 * - For FCoE VSI only enable the TC configured
5225 * as per the APP TLV
5226#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005227 * - For all others keep them at TC0 for now
5228 */
5229 if (v == pf->lan_vsi)
5230 tc_map = i40e_pf_get_tc_map(pf);
5231 else
David Ertmanea6acb72016-09-20 07:10:50 -07005232 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005233#ifdef I40E_FCOE
5234 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5235 tc_map = i40e_get_fcoe_tc_map(pf);
5236#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005237
5238 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5239 if (ret) {
5240 dev_info(&pf->pdev->dev,
5241 "Failed configuring TC for VSI seid=%d\n",
5242 pf->vsi[v]->seid);
5243 /* Will try to configure as many components */
5244 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005245 /* Re-configure VSI vectors based on updated TC map */
5246 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005247 if (pf->vsi[v]->netdev)
5248 i40e_dcbnl_set_all(pf->vsi[v]);
5249 }
5250 }
5251}
5252
5253/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005254 * i40e_resume_port_tx - Resume port Tx
5255 * @pf: PF struct
5256 *
5257 * Resume a port's Tx and issue a PF reset in case of failure to
5258 * resume.
5259 **/
5260static int i40e_resume_port_tx(struct i40e_pf *pf)
5261{
5262 struct i40e_hw *hw = &pf->hw;
5263 int ret;
5264
5265 ret = i40e_aq_resume_port_tx(hw, NULL);
5266 if (ret) {
5267 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005268 "Resume Port Tx failed, err %s aq_err %s\n",
5269 i40e_stat_str(&pf->hw, ret),
5270 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005271 /* Schedule PF reset to recover */
5272 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5273 i40e_service_event_schedule(pf);
5274 }
5275
5276 return ret;
5277}
5278
5279/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005280 * i40e_init_pf_dcb - Initialize DCB configuration
5281 * @pf: PF being configured
5282 *
5283 * Query the current DCB configuration and cache it
5284 * in the hardware structure
5285 **/
5286static int i40e_init_pf_dcb(struct i40e_pf *pf)
5287{
5288 struct i40e_hw *hw = &pf->hw;
5289 int err = 0;
5290
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005291 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005292 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005293 goto out;
5294
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005295 /* Get the initial DCB configuration */
5296 err = i40e_init_dcb(hw);
5297 if (!err) {
5298 /* Device/Function is not DCBX capable */
5299 if ((!hw->func_caps.dcb) ||
5300 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5301 dev_info(&pf->pdev->dev,
5302 "DCBX offload is not supported or is disabled for this PF.\n");
5303
5304 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5305 goto out;
5306
5307 } else {
5308 /* When status is not DISABLED then DCBX in FW */
5309 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5310 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005311
5312 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005313 /* Enable DCB tagging only when more than one TC
5314 * or explicitly disable if only one TC
5315 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005316 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5317 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005318 else
5319 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005320 dev_dbg(&pf->pdev->dev,
5321 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005322 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005323 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005324 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005325 "Query for DCB configuration failed, err %s aq_err %s\n",
5326 i40e_stat_str(&pf->hw, err),
5327 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005328 }
5329
5330out:
5331 return err;
5332}
5333#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005334#define SPEED_SIZE 14
5335#define FC_SIZE 8
5336/**
5337 * i40e_print_link_message - print link up or down
5338 * @vsi: the VSI for which link needs a message
5339 */
Matt Jaredc156f852015-08-27 11:42:39 -04005340void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005341{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005342 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005343 char *speed = "Unknown";
5344 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005345 char *fec = "";
5346 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005347
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005348 new_speed = vsi->back->hw.phy.link_info.link_speed;
5349
5350 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005351 return;
5352 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005353 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005354 if (!isup) {
5355 netdev_info(vsi->netdev, "NIC Link is Down\n");
5356 return;
5357 }
5358
Greg Rose148c2d82014-12-11 07:06:27 +00005359 /* Warn user if link speed on NPAR enabled partition is not at
5360 * least 10GB
5361 */
5362 if (vsi->back->hw.func_caps.npar_enable &&
5363 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5364 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5365 netdev_warn(vsi->netdev,
5366 "The partition detected link speed that is less than 10Gbps\n");
5367
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005368 switch (vsi->back->hw.phy.link_info.link_speed) {
5369 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005370 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005371 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005372 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005373 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005374 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005375 case I40E_LINK_SPEED_25GB:
5376 speed = "25 G";
5377 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005378 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005379 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005380 break;
5381 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005382 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005383 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005384 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005385 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005386 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005387 default:
5388 break;
5389 }
5390
5391 switch (vsi->back->hw.fc.current_mode) {
5392 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005393 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005394 break;
5395 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005396 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005397 break;
5398 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005399 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005400 break;
5401 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005402 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005403 break;
5404 }
5405
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005406 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5407 fec = ", FEC: None";
5408 an = ", Autoneg: False";
5409
5410 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5411 an = ", Autoneg: True";
5412
5413 if (vsi->back->hw.phy.link_info.fec_info &
5414 I40E_AQ_CONFIG_FEC_KR_ENA)
5415 fec = ", FEC: CL74 FC-FEC/BASE-R";
5416 else if (vsi->back->hw.phy.link_info.fec_info &
5417 I40E_AQ_CONFIG_FEC_RS_ENA)
5418 fec = ", FEC: CL108 RS-FEC";
5419 }
5420
5421 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5422 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005423}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005424
5425/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005426 * i40e_up_complete - Finish the last steps of bringing up a connection
5427 * @vsi: the VSI being configured
5428 **/
5429static int i40e_up_complete(struct i40e_vsi *vsi)
5430{
5431 struct i40e_pf *pf = vsi->back;
5432 int err;
5433
5434 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5435 i40e_vsi_configure_msix(vsi);
5436 else
5437 i40e_configure_msi_and_legacy(vsi);
5438
5439 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005440 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005441 if (err)
5442 return err;
5443
5444 clear_bit(__I40E_DOWN, &vsi->state);
5445 i40e_napi_enable_all(vsi);
5446 i40e_vsi_enable_irq(vsi);
5447
5448 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5449 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005450 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005451 netif_tx_start_all_queues(vsi->netdev);
5452 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005453 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005454 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005455 /* need to check for qualified module here*/
5456 if ((pf->hw.phy.link_info.link_info &
5457 I40E_AQ_MEDIA_AVAILABLE) &&
5458 (!(pf->hw.phy.link_info.an_info &
5459 I40E_AQ_QUALIFIED_MODULE)))
5460 netdev_err(vsi->netdev,
5461 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005462 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005463
5464 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005465 if (vsi->type == I40E_VSI_FDIR) {
5466 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005467 pf->fd_add_err = 0;
5468 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005469 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005470 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005471
5472 /* On the next run of the service_task, notify any clients of the new
5473 * opened netdev
5474 */
5475 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005476 i40e_service_event_schedule(pf);
5477
5478 return 0;
5479}
5480
5481/**
5482 * i40e_vsi_reinit_locked - Reset the VSI
5483 * @vsi: the VSI being configured
5484 *
5485 * Rebuild the ring structs after some configuration
5486 * has changed, e.g. MTU size.
5487 **/
5488static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5489{
5490 struct i40e_pf *pf = vsi->back;
5491
5492 WARN_ON(in_interrupt());
5493 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5494 usleep_range(1000, 2000);
5495 i40e_down(vsi);
5496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005497 i40e_up(vsi);
5498 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5499}
5500
5501/**
5502 * i40e_up - Bring the connection back up after being down
5503 * @vsi: the VSI being configured
5504 **/
5505int i40e_up(struct i40e_vsi *vsi)
5506{
5507 int err;
5508
5509 err = i40e_vsi_configure(vsi);
5510 if (!err)
5511 err = i40e_up_complete(vsi);
5512
5513 return err;
5514}
5515
5516/**
5517 * i40e_down - Shutdown the connection processing
5518 * @vsi: the VSI being stopped
5519 **/
5520void i40e_down(struct i40e_vsi *vsi)
5521{
5522 int i;
5523
5524 /* It is assumed that the caller of this function
5525 * sets the vsi->state __I40E_DOWN bit.
5526 */
5527 if (vsi->netdev) {
5528 netif_carrier_off(vsi->netdev);
5529 netif_tx_disable(vsi->netdev);
5530 }
5531 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005532 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005533 i40e_napi_disable_all(vsi);
5534
5535 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005536 i40e_clean_tx_ring(vsi->tx_rings[i]);
5537 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005538 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005539
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005540}
5541
5542/**
5543 * i40e_setup_tc - configure multiple traffic classes
5544 * @netdev: net device to configure
5545 * @tc: number of traffic classes to enable
5546 **/
5547static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5548{
5549 struct i40e_netdev_priv *np = netdev_priv(netdev);
5550 struct i40e_vsi *vsi = np->vsi;
5551 struct i40e_pf *pf = vsi->back;
5552 u8 enabled_tc = 0;
5553 int ret = -EINVAL;
5554 int i;
5555
5556 /* Check if DCB enabled to continue */
5557 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5558 netdev_info(netdev, "DCB is not enabled for adapter\n");
5559 goto exit;
5560 }
5561
5562 /* Check if MFP enabled */
5563 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5564 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5565 goto exit;
5566 }
5567
5568 /* Check whether tc count is within enabled limit */
5569 if (tc > i40e_pf_get_num_tc(pf)) {
5570 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5571 goto exit;
5572 }
5573
5574 /* Generate TC map for number of tc requested */
5575 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005576 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005577
5578 /* Requesting same TC configuration as already enabled */
5579 if (enabled_tc == vsi->tc_config.enabled_tc)
5580 return 0;
5581
5582 /* Quiesce VSI queues */
5583 i40e_quiesce_vsi(vsi);
5584
5585 /* Configure VSI for enabled TCs */
5586 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5587 if (ret) {
5588 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5589 vsi->seid);
5590 goto exit;
5591 }
5592
5593 /* Unquiesce VSI */
5594 i40e_unquiesce_vsi(vsi);
5595
5596exit:
5597 return ret;
5598}
5599
John Fastabende4c67342016-02-16 21:16:15 -08005600#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005601int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5602 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005603#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005604static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5605 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005606#endif
5607{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005608 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005609 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005610
5611 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5612
5613 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005614}
5615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005616/**
5617 * i40e_open - Called when a network interface is made active
5618 * @netdev: network interface device structure
5619 *
5620 * The open entry point is called when a network interface is made
5621 * active by the system (IFF_UP). At this point all resources needed
5622 * for transmit and receive operations are allocated, the interrupt
5623 * handler is registered with the OS, the netdev watchdog subtask is
5624 * enabled, and the stack is notified that the interface is ready.
5625 *
5626 * Returns 0 on success, negative value on failure
5627 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005628int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005629{
5630 struct i40e_netdev_priv *np = netdev_priv(netdev);
5631 struct i40e_vsi *vsi = np->vsi;
5632 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005633 int err;
5634
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005635 /* disallow open during test or if eeprom is broken */
5636 if (test_bit(__I40E_TESTING, &pf->state) ||
5637 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005638 return -EBUSY;
5639
5640 netif_carrier_off(netdev);
5641
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005642 err = i40e_vsi_open(vsi);
5643 if (err)
5644 return err;
5645
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005646 /* configure global TSO hardware offload settings */
5647 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5648 TCP_FLAG_FIN) >> 16);
5649 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5650 TCP_FLAG_FIN |
5651 TCP_FLAG_CWR) >> 16);
5652 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5653
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005654 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005655
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005656 return 0;
5657}
5658
5659/**
5660 * i40e_vsi_open -
5661 * @vsi: the VSI to open
5662 *
5663 * Finish initialization of the VSI.
5664 *
5665 * Returns 0 on success, negative value on failure
5666 **/
5667int i40e_vsi_open(struct i40e_vsi *vsi)
5668{
5669 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005670 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005671 int err;
5672
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005673 /* allocate descriptors */
5674 err = i40e_vsi_setup_tx_resources(vsi);
5675 if (err)
5676 goto err_setup_tx;
5677 err = i40e_vsi_setup_rx_resources(vsi);
5678 if (err)
5679 goto err_setup_rx;
5680
5681 err = i40e_vsi_configure(vsi);
5682 if (err)
5683 goto err_setup_rx;
5684
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005685 if (vsi->netdev) {
5686 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5687 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5688 err = i40e_vsi_request_irq(vsi, int_name);
5689 if (err)
5690 goto err_setup_rx;
5691
5692 /* Notify the stack of the actual queue counts. */
5693 err = netif_set_real_num_tx_queues(vsi->netdev,
5694 vsi->num_queue_pairs);
5695 if (err)
5696 goto err_set_queues;
5697
5698 err = netif_set_real_num_rx_queues(vsi->netdev,
5699 vsi->num_queue_pairs);
5700 if (err)
5701 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005702
5703 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005704 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005705 dev_driver_string(&pf->pdev->dev),
5706 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005707 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005708
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005709 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005710 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005711 goto err_setup_rx;
5712 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005714 err = i40e_up_complete(vsi);
5715 if (err)
5716 goto err_up_complete;
5717
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005718 return 0;
5719
5720err_up_complete:
5721 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005722err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005723 i40e_vsi_free_irq(vsi);
5724err_setup_rx:
5725 i40e_vsi_free_rx_resources(vsi);
5726err_setup_tx:
5727 i40e_vsi_free_tx_resources(vsi);
5728 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005729 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005730
5731 return err;
5732}
5733
5734/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005735 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005736 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005737 *
5738 * This function destroys the hlist where all the Flow Director
5739 * filters were saved.
5740 **/
5741static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5742{
5743 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005744 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005745 struct hlist_node *node2;
5746
5747 hlist_for_each_entry_safe(filter, node2,
5748 &pf->fdir_filter_list, fdir_node) {
5749 hlist_del(&filter->fdir_node);
5750 kfree(filter);
5751 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005752
Jacob Keller0e588de2017-02-06 14:38:50 -08005753 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5754 list_del(&pit_entry->list);
5755 kfree(pit_entry);
5756 }
5757 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5758
5759 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5760 list_del(&pit_entry->list);
5761 kfree(pit_entry);
5762 }
5763 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5764
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005765 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005766 pf->fd_tcp4_filter_cnt = 0;
5767 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005768 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005769 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005770
5771 /* Reprogram the default input set for TCP/IPv4 */
5772 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5773 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5774 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5775
5776 /* Reprogram the default input set for UDP/IPv4 */
5777 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5778 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5779 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5780
5781 /* Reprogram the default input set for SCTP/IPv4 */
5782 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5783 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5784 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5785
5786 /* Reprogram the default input set for Other/IPv4 */
5787 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5788 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005789}
5790
5791/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005792 * i40e_close - Disables a network interface
5793 * @netdev: network interface device structure
5794 *
5795 * The close entry point is called when an interface is de-activated
5796 * by the OS. The hardware is still under the driver's control, but
5797 * this netdev interface is disabled.
5798 *
5799 * Returns 0, this is not allowed to fail
5800 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005801int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005802{
5803 struct i40e_netdev_priv *np = netdev_priv(netdev);
5804 struct i40e_vsi *vsi = np->vsi;
5805
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005806 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005807
5808 return 0;
5809}
5810
5811/**
5812 * i40e_do_reset - Start a PF or Core Reset sequence
5813 * @pf: board private structure
5814 * @reset_flags: which reset is requested
5815 *
5816 * The essential difference in resets is that the PF Reset
5817 * doesn't clear the packet buffers, doesn't reset the PE
5818 * firmware, and doesn't bother the other PFs on the chip.
5819 **/
5820void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5821{
5822 u32 val;
5823
5824 WARN_ON(in_interrupt());
5825
Mitch Williams263fc482014-04-23 04:50:11 +00005826
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005827 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005828 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005829
5830 /* Request a Global Reset
5831 *
5832 * This will start the chip's countdown to the actual full
5833 * chip reset event, and a warning interrupt to be sent
5834 * to all PFs, including the requestor. Our handler
5835 * for the warning interrupt will deal with the shutdown
5836 * and recovery of the switch setup.
5837 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005838 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005839 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5840 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5841 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5842
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005843 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005844
5845 /* Request a Core Reset
5846 *
5847 * Same as Global Reset, except does *not* include the MAC/PHY
5848 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005849 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005850 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5851 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5852 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5853 i40e_flush(&pf->hw);
5854
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005855 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005856
5857 /* Request a PF Reset
5858 *
5859 * Resets only the PF-specific registers
5860 *
5861 * This goes directly to the tear-down and rebuild of
5862 * the switch, since we need to do all the recovery as
5863 * for the Core Reset.
5864 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005865 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005866 i40e_handle_reset_warning(pf);
5867
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005868 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005869 int v;
5870
5871 /* Find the VSI(s) that requested a re-init */
5872 dev_info(&pf->pdev->dev,
5873 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005874 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005875 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005877 if (vsi != NULL &&
5878 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5879 i40e_vsi_reinit_locked(pf->vsi[v]);
5880 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5881 }
5882 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005883 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005884 int v;
5885
5886 /* Find the VSI(s) that needs to be brought down */
5887 dev_info(&pf->pdev->dev, "VSI down requested\n");
5888 for (v = 0; v < pf->num_alloc_vsi; v++) {
5889 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005890
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005891 if (vsi != NULL &&
5892 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5893 set_bit(__I40E_DOWN, &vsi->state);
5894 i40e_down(vsi);
5895 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5896 }
5897 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005898 } else {
5899 dev_info(&pf->pdev->dev,
5900 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005901 }
5902}
5903
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005904#ifdef CONFIG_I40E_DCB
5905/**
5906 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5907 * @pf: board private structure
5908 * @old_cfg: current DCB config
5909 * @new_cfg: new DCB config
5910 **/
5911bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5912 struct i40e_dcbx_config *old_cfg,
5913 struct i40e_dcbx_config *new_cfg)
5914{
5915 bool need_reconfig = false;
5916
5917 /* Check if ETS configuration has changed */
5918 if (memcmp(&new_cfg->etscfg,
5919 &old_cfg->etscfg,
5920 sizeof(new_cfg->etscfg))) {
5921 /* If Priority Table has changed reconfig is needed */
5922 if (memcmp(&new_cfg->etscfg.prioritytable,
5923 &old_cfg->etscfg.prioritytable,
5924 sizeof(new_cfg->etscfg.prioritytable))) {
5925 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005926 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005927 }
5928
5929 if (memcmp(&new_cfg->etscfg.tcbwtable,
5930 &old_cfg->etscfg.tcbwtable,
5931 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005932 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005933
5934 if (memcmp(&new_cfg->etscfg.tsatable,
5935 &old_cfg->etscfg.tsatable,
5936 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005937 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005938 }
5939
5940 /* Check if PFC configuration has changed */
5941 if (memcmp(&new_cfg->pfc,
5942 &old_cfg->pfc,
5943 sizeof(new_cfg->pfc))) {
5944 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005945 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005946 }
5947
5948 /* Check if APP Table has changed */
5949 if (memcmp(&new_cfg->app,
5950 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005951 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005952 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005953 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005954 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005955
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005956 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005957 return need_reconfig;
5958}
5959
5960/**
5961 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5962 * @pf: board private structure
5963 * @e: event info posted on ARQ
5964 **/
5965static int i40e_handle_lldp_event(struct i40e_pf *pf,
5966 struct i40e_arq_event_info *e)
5967{
5968 struct i40e_aqc_lldp_get_mib *mib =
5969 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5970 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005971 struct i40e_dcbx_config tmp_dcbx_cfg;
5972 bool need_reconfig = false;
5973 int ret = 0;
5974 u8 type;
5975
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005976 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005977 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005978 return ret;
5979
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005980 /* Ignore if event is not for Nearest Bridge */
5981 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5982 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005983 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005984 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5985 return ret;
5986
5987 /* Check MIB Type and return if event for Remote MIB update */
5988 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005989 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005990 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005991 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5992 /* Update the remote cached instance and return */
5993 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5994 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5995 &hw->remote_dcbx_config);
5996 goto exit;
5997 }
5998
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005999 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07006000 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006001
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006002 /* Reset the old DCBx configuration data */
6003 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00006004 /* Get updated DCBX data from firmware */
6005 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006006 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006007 dev_info(&pf->pdev->dev,
6008 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6009 i40e_stat_str(&pf->hw, ret),
6010 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006011 goto exit;
6012 }
6013
6014 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006015 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6016 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006017 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006018 goto exit;
6019 }
6020
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006021 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6022 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006023
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006024 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006025
6026 if (!need_reconfig)
6027 goto exit;
6028
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006029 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006030 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006031 pf->flags |= I40E_FLAG_DCB_ENABLED;
6032 else
6033 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6034
Neerav Parikh69129dc2014-11-12 00:18:46 +00006035 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006036 /* Reconfiguration needed quiesce all VSIs */
6037 i40e_pf_quiesce_all_vsi(pf);
6038
6039 /* Changes in configuration update VEB/VSI */
6040 i40e_dcb_reconfigure(pf);
6041
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006042 ret = i40e_resume_port_tx(pf);
6043
Neerav Parikh69129dc2014-11-12 00:18:46 +00006044 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006045 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006046 if (ret)
6047 goto exit;
6048
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006049 /* Wait for the PF's queues to be disabled */
6050 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006051 if (ret) {
6052 /* Schedule PF reset to recover */
6053 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6054 i40e_service_event_schedule(pf);
6055 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006056 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006057 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6058 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006059 }
6060
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006061exit:
6062 return ret;
6063}
6064#endif /* CONFIG_I40E_DCB */
6065
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006066/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006067 * i40e_do_reset_safe - Protected reset path for userland calls.
6068 * @pf: board private structure
6069 * @reset_flags: which reset is requested
6070 *
6071 **/
6072void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6073{
6074 rtnl_lock();
6075 i40e_do_reset(pf, reset_flags);
6076 rtnl_unlock();
6077}
6078
6079/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006080 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6081 * @pf: board private structure
6082 * @e: event info posted on ARQ
6083 *
6084 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6085 * and VF queues
6086 **/
6087static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6088 struct i40e_arq_event_info *e)
6089{
6090 struct i40e_aqc_lan_overflow *data =
6091 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6092 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6093 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6094 struct i40e_hw *hw = &pf->hw;
6095 struct i40e_vf *vf;
6096 u16 vf_id;
6097
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006098 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6099 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006100
6101 /* Queue belongs to VF, find the VF and issue VF reset */
6102 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6103 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6104 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6105 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6106 vf_id -= hw->func_caps.vf_base_id;
6107 vf = &pf->vf[vf_id];
6108 i40e_vc_notify_vf_reset(vf);
6109 /* Allow VF to process pending reset notification */
6110 msleep(20);
6111 i40e_reset_vf(vf, false);
6112 }
6113}
6114
6115/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006116 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6117 * @pf: board private structure
6118 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006119u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006120{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006121 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006122
6123 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6124 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6125 return fcnt_prog;
6126}
6127
6128/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006129 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006130 * @pf: board private structure
6131 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006132u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006133{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006134 u32 val, fcnt_prog;
6135
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006136 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6137 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6138 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6139 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6140 return fcnt_prog;
6141}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006142
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006143/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006144 * i40e_get_global_fd_count - Get total FD filters programmed on device
6145 * @pf: board private structure
6146 **/
6147u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6148{
6149 u32 val, fcnt_prog;
6150
6151 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6152 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6153 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6154 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6155 return fcnt_prog;
6156}
6157
6158/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006159 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6160 * @pf: board private structure
6161 **/
6162void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6163{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006164 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006165 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006166 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006167
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006168 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6169 return;
6170
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006171 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6172 * to re-enable
6173 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006174 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006175 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006176 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6177 (pf->fd_add_err == 0) ||
6178 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006179 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006180 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6181 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006182 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6183 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 +00006184 }
6185 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006186
6187 /* Wait for some more space to be available to turn on ATR. We also
6188 * must check that no existing ntuple rules for TCP are in effect
6189 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006190 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6191 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006192 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006193 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006194 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006195 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006196 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 +00006197 }
6198 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006199
6200 /* if hw had a problem adding a filter, delete it */
6201 if (pf->fd_inv > 0) {
6202 hlist_for_each_entry_safe(filter, node,
6203 &pf->fdir_filter_list, fdir_node) {
6204 if (filter->fd_id == pf->fd_inv) {
6205 hlist_del(&filter->fdir_node);
6206 kfree(filter);
6207 pf->fdir_pf_active_filters--;
6208 }
6209 }
6210 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006211}
6212
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006213#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006214#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006215/**
6216 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6217 * @pf: board private structure
6218 **/
6219static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6220{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006221 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006222 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006223 bool disable_atr = false;
6224 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006225 int reg;
6226
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006227 if (!time_after(jiffies, pf->fd_flush_timestamp +
6228 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6229 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006230
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006231 /* If the flush is happening too quick and we have mostly SB rules we
6232 * should not re-enable ATR for some time.
6233 */
6234 min_flush_time = pf->fd_flush_timestamp +
6235 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6236 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006237
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006238 if (!(time_after(jiffies, min_flush_time)) &&
6239 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6240 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6241 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6242 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006243 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006244
6245 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006246 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006247 /* flush all filters */
6248 wr32(&pf->hw, I40E_PFQF_CTL_1,
6249 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6250 i40e_flush(&pf->hw);
6251 pf->fd_flush_cnt++;
6252 pf->fd_add_err = 0;
6253 do {
6254 /* Check FD flush status every 5-6msec */
6255 usleep_range(5000, 6000);
6256 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6257 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6258 break;
6259 } while (flush_wait_retry--);
6260 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6261 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6262 } else {
6263 /* replay sideband filters */
6264 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006265 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006266 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006267 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6268 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6269 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6270 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006271}
6272
6273/**
6274 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6275 * @pf: board private structure
6276 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006277u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006278{
6279 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6280}
6281
6282/* We can see up to 256 filter programming desc in transit if the filters are
6283 * being applied really fast; before we see the first
6284 * filter miss error on Rx queue 0. Accumulating enough error messages before
6285 * reacting will make sure we don't cause flush too often.
6286 */
6287#define I40E_MAX_FD_PROGRAM_ERROR 256
6288
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006289/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006290 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6291 * @pf: board private structure
6292 **/
6293static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6294{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006296 /* if interface is down do nothing */
6297 if (test_bit(__I40E_DOWN, &pf->state))
6298 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006299
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006300 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006301 i40e_fdir_flush_and_replay(pf);
6302
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006303 i40e_fdir_check_and_reenable(pf);
6304
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006305}
6306
6307/**
6308 * i40e_vsi_link_event - notify VSI of a link event
6309 * @vsi: vsi to be notified
6310 * @link_up: link up or down
6311 **/
6312static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6313{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006314 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006315 return;
6316
6317 switch (vsi->type) {
6318 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006319#ifdef I40E_FCOE
6320 case I40E_VSI_FCOE:
6321#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 if (!vsi->netdev || !vsi->netdev_registered)
6323 break;
6324
6325 if (link_up) {
6326 netif_carrier_on(vsi->netdev);
6327 netif_tx_wake_all_queues(vsi->netdev);
6328 } else {
6329 netif_carrier_off(vsi->netdev);
6330 netif_tx_stop_all_queues(vsi->netdev);
6331 }
6332 break;
6333
6334 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006335 case I40E_VSI_VMDQ2:
6336 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006337 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006338 case I40E_VSI_MIRROR:
6339 default:
6340 /* there is no notification for other VSIs */
6341 break;
6342 }
6343}
6344
6345/**
6346 * i40e_veb_link_event - notify elements on the veb of a link event
6347 * @veb: veb to be notified
6348 * @link_up: link up or down
6349 **/
6350static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6351{
6352 struct i40e_pf *pf;
6353 int i;
6354
6355 if (!veb || !veb->pf)
6356 return;
6357 pf = veb->pf;
6358
6359 /* depth first... */
6360 for (i = 0; i < I40E_MAX_VEB; i++)
6361 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6362 i40e_veb_link_event(pf->veb[i], link_up);
6363
6364 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006365 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006366 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6367 i40e_vsi_link_event(pf->vsi[i], link_up);
6368}
6369
6370/**
6371 * i40e_link_event - Update netif_carrier status
6372 * @pf: board private structure
6373 **/
6374static void i40e_link_event(struct i40e_pf *pf)
6375{
Mitch Williams320684c2014-10-17 03:14:43 +00006376 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006377 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006378 i40e_status status;
6379 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006380
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006381 /* save off old link status information */
6382 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6383
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006384 /* set this to force the get_link_status call to refresh state */
6385 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006386
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006387 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006388
6389 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006390
6391 /* On success, disable temp link polling */
6392 if (status == I40E_SUCCESS) {
6393 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6394 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6395 } else {
6396 /* Enable link polling temporarily until i40e_get_link_status
6397 * returns I40E_SUCCESS
6398 */
6399 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006400 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6401 status);
6402 return;
6403 }
6404
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006405 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6406 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006407
6408 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006409 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006410 (test_bit(__I40E_DOWN, &vsi->state) ||
6411 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006412 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006413
6414 if (!test_bit(__I40E_DOWN, &vsi->state))
6415 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006416
6417 /* Notify the base of the switch tree connected to
6418 * the link. Floating VEBs are not notified.
6419 */
6420 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6421 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6422 else
Mitch Williams320684c2014-10-17 03:14:43 +00006423 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006424
6425 if (pf->vf)
6426 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006427
6428 if (pf->flags & I40E_FLAG_PTP)
6429 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006430}
6431
6432/**
Shannon Nelson21536712014-10-25 10:35:25 +00006433 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006434 * @pf: board private structure
6435 **/
6436static void i40e_watchdog_subtask(struct i40e_pf *pf)
6437{
6438 int i;
6439
6440 /* if interface is down do nothing */
6441 if (test_bit(__I40E_DOWN, &pf->state) ||
6442 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6443 return;
6444
Shannon Nelson21536712014-10-25 10:35:25 +00006445 /* make sure we don't do these things too often */
6446 if (time_before(jiffies, (pf->service_timer_previous +
6447 pf->service_timer_period)))
6448 return;
6449 pf->service_timer_previous = jiffies;
6450
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006451 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6452 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006453 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006455 /* Update the stats for active netdevs so the network stack
6456 * can look at updated numbers whenever it cares to
6457 */
Mitch Williams505682c2014-05-20 08:01:37 +00006458 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006459 if (pf->vsi[i] && pf->vsi[i]->netdev)
6460 i40e_update_stats(pf->vsi[i]);
6461
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006462 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6463 /* Update the stats for the active switching components */
6464 for (i = 0; i < I40E_MAX_VEB; i++)
6465 if (pf->veb[i])
6466 i40e_update_veb_stats(pf->veb[i]);
6467 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006468
6469 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006470}
6471
6472/**
6473 * i40e_reset_subtask - Set up for resetting the device and driver
6474 * @pf: board private structure
6475 **/
6476static void i40e_reset_subtask(struct i40e_pf *pf)
6477{
6478 u32 reset_flags = 0;
6479
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006480 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006481 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006482 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006483 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6484 }
6485 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006486 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006487 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6488 }
6489 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006490 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006491 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6492 }
6493 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006494 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006495 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6496 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006497 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006498 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006499 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6500 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501
6502 /* If there's a recovery already waiting, it takes
6503 * precedence before starting a new reset sequence.
6504 */
6505 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6506 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006507 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006508 }
6509
6510 /* If we're already down or resetting, just bail */
6511 if (reset_flags &&
6512 !test_bit(__I40E_DOWN, &pf->state) &&
6513 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6514 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006515
6516unlock:
6517 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518}
6519
6520/**
6521 * i40e_handle_link_event - Handle link event
6522 * @pf: board private structure
6523 * @e: event info posted on ARQ
6524 **/
6525static void i40e_handle_link_event(struct i40e_pf *pf,
6526 struct i40e_arq_event_info *e)
6527{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006528 struct i40e_aqc_get_link_status *status =
6529 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006530
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006531 /* Do a new status request to re-enable LSE reporting
6532 * and load new status information into the hw struct
6533 * This completely ignores any state information
6534 * in the ARQ event info, instead choosing to always
6535 * issue the AQ update link status command.
6536 */
6537 i40e_link_event(pf);
6538
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006539 /* check for unqualified module, if link is down */
6540 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6541 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6542 (!(status->link_info & I40E_AQ_LINK_UP)))
6543 dev_err(&pf->pdev->dev,
6544 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006545}
6546
6547/**
6548 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6549 * @pf: board private structure
6550 **/
6551static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6552{
6553 struct i40e_arq_event_info event;
6554 struct i40e_hw *hw = &pf->hw;
6555 u16 pending, i = 0;
6556 i40e_status ret;
6557 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006558 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006559 u32 val;
6560
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006561 /* Do not run clean AQ when PF reset fails */
6562 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6563 return;
6564
Shannon Nelson86df2422014-05-20 08:01:35 +00006565 /* check for error indications */
6566 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6567 oldval = val;
6568 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006569 if (hw->debug_mask & I40E_DEBUG_AQ)
6570 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006571 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6572 }
6573 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006574 if (hw->debug_mask & I40E_DEBUG_AQ)
6575 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006576 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006577 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006578 }
6579 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006580 if (hw->debug_mask & I40E_DEBUG_AQ)
6581 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006582 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6583 }
6584 if (oldval != val)
6585 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6586
6587 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6588 oldval = val;
6589 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006590 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6591 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006592 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6593 }
6594 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006595 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6596 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006597 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6598 }
6599 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006600 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6601 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006602 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6603 }
6604 if (oldval != val)
6605 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6606
Mitch Williams1001dc32014-11-11 20:02:19 +00006607 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6608 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006609 if (!event.msg_buf)
6610 return;
6611
6612 do {
6613 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006614 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006615 break;
Mitch Williams56497972014-06-04 08:45:18 +00006616 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006617 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6618 break;
6619 }
6620
6621 opcode = le16_to_cpu(event.desc.opcode);
6622 switch (opcode) {
6623
6624 case i40e_aqc_opc_get_link_status:
6625 i40e_handle_link_event(pf, &event);
6626 break;
6627 case i40e_aqc_opc_send_msg_to_pf:
6628 ret = i40e_vc_process_vf_msg(pf,
6629 le16_to_cpu(event.desc.retval),
6630 le32_to_cpu(event.desc.cookie_high),
6631 le32_to_cpu(event.desc.cookie_low),
6632 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006633 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006634 break;
6635 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006636 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006637#ifdef CONFIG_I40E_DCB
6638 rtnl_lock();
6639 ret = i40e_handle_lldp_event(pf, &event);
6640 rtnl_unlock();
6641#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006642 break;
6643 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006644 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006645 i40e_handle_lan_overflow_event(pf, &event);
6646 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006647 case i40e_aqc_opc_send_msg_to_peer:
6648 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6649 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006650 case i40e_aqc_opc_nvm_erase:
6651 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006652 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006653 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6654 "ARQ NVM operation 0x%04x completed\n",
6655 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006656 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006657 default:
6658 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006659 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006660 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006661 break;
6662 }
6663 } while (pending && (i++ < pf->adminq_work_limit));
6664
6665 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6666 /* re-enable Admin queue interrupt cause */
6667 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6668 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6669 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6670 i40e_flush(hw);
6671
6672 kfree(event.msg_buf);
6673}
6674
6675/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006676 * i40e_verify_eeprom - make sure eeprom is good to use
6677 * @pf: board private structure
6678 **/
6679static void i40e_verify_eeprom(struct i40e_pf *pf)
6680{
6681 int err;
6682
6683 err = i40e_diag_eeprom_test(&pf->hw);
6684 if (err) {
6685 /* retry in case of garbage read */
6686 err = i40e_diag_eeprom_test(&pf->hw);
6687 if (err) {
6688 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6689 err);
6690 set_bit(__I40E_BAD_EEPROM, &pf->state);
6691 }
6692 }
6693
6694 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6695 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6696 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6697 }
6698}
6699
6700/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006701 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006702 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006703 *
6704 * enable switch loop back or die - no point in a return value
6705 **/
6706static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6707{
6708 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6709 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006710 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006711
6712 ctxt.seid = pf->main_vsi_seid;
6713 ctxt.pf_num = pf->hw.pf_id;
6714 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006715 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6716 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006717 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006718 "couldn't get PF vsi config, err %s aq_err %s\n",
6719 i40e_stat_str(&pf->hw, ret),
6720 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006721 return;
6722 }
6723 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6724 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6725 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6726
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006727 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6728 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006729 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006730 "update vsi switch failed, err %s aq_err %s\n",
6731 i40e_stat_str(&pf->hw, ret),
6732 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006733 }
6734}
6735
6736/**
6737 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006738 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006739 *
6740 * disable switch loop back or die - no point in a return value
6741 **/
6742static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6743{
6744 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6745 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006746 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006747
6748 ctxt.seid = pf->main_vsi_seid;
6749 ctxt.pf_num = pf->hw.pf_id;
6750 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006751 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6752 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006753 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006754 "couldn't get PF vsi config, err %s aq_err %s\n",
6755 i40e_stat_str(&pf->hw, ret),
6756 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006757 return;
6758 }
6759 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6760 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6761 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6762
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006763 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6764 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006765 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006766 "update vsi switch failed, err %s aq_err %s\n",
6767 i40e_stat_str(&pf->hw, ret),
6768 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006769 }
6770}
6771
6772/**
Neerav Parikh51616012015-02-06 08:52:14 +00006773 * i40e_config_bridge_mode - Configure the HW bridge mode
6774 * @veb: pointer to the bridge instance
6775 *
6776 * Configure the loop back mode for the LAN VSI that is downlink to the
6777 * specified HW bridge instance. It is expected this function is called
6778 * when a new HW bridge is instantiated.
6779 **/
6780static void i40e_config_bridge_mode(struct i40e_veb *veb)
6781{
6782 struct i40e_pf *pf = veb->pf;
6783
Shannon Nelson6dec1012015-09-28 14:12:30 -04006784 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6785 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6786 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006787 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6788 i40e_disable_pf_switch_lb(pf);
6789 else
6790 i40e_enable_pf_switch_lb(pf);
6791}
6792
6793/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006794 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6795 * @veb: pointer to the VEB instance
6796 *
6797 * This is a recursive function that first builds the attached VSIs then
6798 * recurses in to build the next layer of VEB. We track the connections
6799 * through our own index numbers because the seid's from the HW could
6800 * change across the reset.
6801 **/
6802static int i40e_reconstitute_veb(struct i40e_veb *veb)
6803{
6804 struct i40e_vsi *ctl_vsi = NULL;
6805 struct i40e_pf *pf = veb->pf;
6806 int v, veb_idx;
6807 int ret;
6808
6809 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006810 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006811 if (pf->vsi[v] &&
6812 pf->vsi[v]->veb_idx == veb->idx &&
6813 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6814 ctl_vsi = pf->vsi[v];
6815 break;
6816 }
6817 }
6818 if (!ctl_vsi) {
6819 dev_info(&pf->pdev->dev,
6820 "missing owner VSI for veb_idx %d\n", veb->idx);
6821 ret = -ENOENT;
6822 goto end_reconstitute;
6823 }
6824 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6825 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6826 ret = i40e_add_vsi(ctl_vsi);
6827 if (ret) {
6828 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006829 "rebuild of veb_idx %d owner VSI failed: %d\n",
6830 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006831 goto end_reconstitute;
6832 }
6833 i40e_vsi_reset_stats(ctl_vsi);
6834
6835 /* create the VEB in the switch and move the VSI onto the VEB */
6836 ret = i40e_add_veb(veb, ctl_vsi);
6837 if (ret)
6838 goto end_reconstitute;
6839
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006840 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6841 veb->bridge_mode = BRIDGE_MODE_VEB;
6842 else
6843 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006844 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006845
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006847 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6849 continue;
6850
6851 if (pf->vsi[v]->veb_idx == veb->idx) {
6852 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006853
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854 vsi->uplink_seid = veb->seid;
6855 ret = i40e_add_vsi(vsi);
6856 if (ret) {
6857 dev_info(&pf->pdev->dev,
6858 "rebuild of vsi_idx %d failed: %d\n",
6859 v, ret);
6860 goto end_reconstitute;
6861 }
6862 i40e_vsi_reset_stats(vsi);
6863 }
6864 }
6865
6866 /* create any VEBs attached to this VEB - RECURSION */
6867 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6868 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6869 pf->veb[veb_idx]->uplink_seid = veb->seid;
6870 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6871 if (ret)
6872 break;
6873 }
6874 }
6875
6876end_reconstitute:
6877 return ret;
6878}
6879
6880/**
6881 * i40e_get_capabilities - get info about the HW
6882 * @pf: the PF struct
6883 **/
6884static int i40e_get_capabilities(struct i40e_pf *pf)
6885{
6886 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6887 u16 data_size;
6888 int buf_len;
6889 int err;
6890
6891 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6892 do {
6893 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6894 if (!cap_buf)
6895 return -ENOMEM;
6896
6897 /* this loads the data into the hw struct for us */
6898 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6899 &data_size,
6900 i40e_aqc_opc_list_func_capabilities,
6901 NULL);
6902 /* data loaded, buffer no longer needed */
6903 kfree(cap_buf);
6904
6905 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6906 /* retry with a larger buffer */
6907 buf_len = data_size;
6908 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6909 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006910 "capability discovery failed, err %s aq_err %s\n",
6911 i40e_stat_str(&pf->hw, err),
6912 i40e_aq_str(&pf->hw,
6913 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914 return -ENODEV;
6915 }
6916 } while (err);
6917
6918 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6919 dev_info(&pf->pdev->dev,
6920 "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",
6921 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6922 pf->hw.func_caps.num_msix_vectors,
6923 pf->hw.func_caps.num_msix_vectors_vf,
6924 pf->hw.func_caps.fd_filters_guaranteed,
6925 pf->hw.func_caps.fd_filters_best_effort,
6926 pf->hw.func_caps.num_tx_qp,
6927 pf->hw.func_caps.num_vsis);
6928
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006929#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6930 + pf->hw.func_caps.num_vfs)
6931 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6932 dev_info(&pf->pdev->dev,
6933 "got num_vsis %d, setting num_vsis to %d\n",
6934 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6935 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6936 }
6937
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938 return 0;
6939}
6940
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006941static int i40e_vsi_clear(struct i40e_vsi *vsi);
6942
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006943/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006944 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006945 * @pf: board private structure
6946 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006947static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948{
6949 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006950
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006951 /* quick workaround for an NVM issue that leaves a critical register
6952 * uninitialized
6953 */
6954 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6955 static const u32 hkey[] = {
6956 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6957 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6958 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6959 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006960 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006961
6962 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6963 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6964 }
6965
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006966 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006967 return;
6968
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006969 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006970 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006971
6972 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006974 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6975 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006976 if (!vsi) {
6977 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006978 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6979 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006980 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006981 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006982
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006983 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006984}
6985
6986/**
6987 * i40e_fdir_teardown - release the Flow Director resources
6988 * @pf: board private structure
6989 **/
6990static void i40e_fdir_teardown(struct i40e_pf *pf)
6991{
Alexander Duyck4b816442016-10-11 15:26:53 -07006992 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006993
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006994 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006995 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6996 if (vsi)
6997 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006998}
6999
7000/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007001 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007002 * @pf: board private structure
7003 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007004 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007005 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007006static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007007{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007008 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007009 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007010 u32 v;
7011
7012 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
7013 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007014 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007015 if (i40e_check_asq_alive(&pf->hw))
7016 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007017
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007018 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007020 /* quiesce the VSIs and their queues that are not already DOWN */
7021 i40e_pf_quiesce_all_vsi(pf);
7022
Mitch Williams505682c2014-05-20 08:01:37 +00007023 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007024 if (pf->vsi[v])
7025 pf->vsi[v]->seid = 0;
7026 }
7027
7028 i40e_shutdown_adminq(&pf->hw);
7029
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007030 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007031 if (hw->hmc.hmc_obj) {
7032 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007033 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007034 dev_warn(&pf->pdev->dev,
7035 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007036 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007037}
7038
7039/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007040 * i40e_send_version - update firmware with driver version
7041 * @pf: PF struct
7042 */
7043static void i40e_send_version(struct i40e_pf *pf)
7044{
7045 struct i40e_driver_version dv;
7046
7047 dv.major_version = DRV_VERSION_MAJOR;
7048 dv.minor_version = DRV_VERSION_MINOR;
7049 dv.build_version = DRV_VERSION_BUILD;
7050 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007051 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007052 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7053}
7054
7055/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00007056 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007057 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007058 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007059 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007060static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007061{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007062 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007063 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007064 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007065 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007066 u32 v;
7067
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007068 /* Now we wait for GRST to settle out.
7069 * We don't have to delete the VEBs or VSIs from the hw switch
7070 * because the reset will make them disappear.
7071 */
7072 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00007073 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007075 set_bit(__I40E_RESET_FAILED, &pf->state);
7076 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00007077 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007078 pf->pfr_count++;
7079
7080 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007081 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007082 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007083
7084 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7085 ret = i40e_init_adminq(&pf->hw);
7086 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007087 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7088 i40e_stat_str(&pf->hw, ret),
7089 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007090 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007091 }
7092
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007093 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00007094 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007095 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007096
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007097 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007098 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007099 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007100 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007102 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7103 hw->func_caps.num_rx_qp,
7104 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7105 if (ret) {
7106 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7107 goto end_core_reset;
7108 }
7109 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7110 if (ret) {
7111 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7112 goto end_core_reset;
7113 }
7114
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007115#ifdef CONFIG_I40E_DCB
7116 ret = i40e_init_pf_dcb(pf);
7117 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007118 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7119 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7120 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007121 }
7122#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07007123#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04007124 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007125
Vasu Dev38e00432014-08-01 13:27:03 -07007126#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007127 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007128 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007129 if (ret)
7130 goto end_core_reset;
7131
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007132 /* The driver only wants link up/down and module qualification
7133 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007134 */
7135 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007136 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007137 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007138 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007139 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007140 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7141 i40e_stat_str(&pf->hw, ret),
7142 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007143
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007144 /* make sure our flow control settings are restored */
7145 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7146 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007147 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7148 i40e_stat_str(&pf->hw, ret),
7149 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007150
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007151 /* Rebuild the VSIs and VEBs that existed before reset.
7152 * They are still in our local switch element arrays, so only
7153 * need to rebuild the switch model in the HW.
7154 *
7155 * If there were VEBs but the reconstitution failed, we'll try
7156 * try to recover minimal use by getting the basic PF VSI working.
7157 */
7158 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007159 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007160 /* find the one VEB connected to the MAC, and find orphans */
7161 for (v = 0; v < I40E_MAX_VEB; v++) {
7162 if (!pf->veb[v])
7163 continue;
7164
7165 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7166 pf->veb[v]->uplink_seid == 0) {
7167 ret = i40e_reconstitute_veb(pf->veb[v]);
7168
7169 if (!ret)
7170 continue;
7171
7172 /* If Main VEB failed, we're in deep doodoo,
7173 * so give up rebuilding the switch and set up
7174 * for minimal rebuild of PF VSI.
7175 * If orphan failed, we'll report the error
7176 * but try to keep going.
7177 */
7178 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7179 dev_info(&pf->pdev->dev,
7180 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7181 ret);
7182 pf->vsi[pf->lan_vsi]->uplink_seid
7183 = pf->mac_seid;
7184 break;
7185 } else if (pf->veb[v]->uplink_seid == 0) {
7186 dev_info(&pf->pdev->dev,
7187 "rebuild of orphan VEB failed: %d\n",
7188 ret);
7189 }
7190 }
7191 }
7192 }
7193
7194 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007195 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007196 /* no VEB, so rebuild only the Main VSI */
7197 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7198 if (ret) {
7199 dev_info(&pf->pdev->dev,
7200 "rebuild of Main VSI failed: %d\n", ret);
7201 goto end_core_reset;
7202 }
7203 }
7204
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007205 /* Reconfigure hardware for allowing smaller MSS in the case
7206 * of TSO, so that we avoid the MDD being fired and causing
7207 * a reset in the case of small MSS+TSO.
7208 */
7209#define I40E_REG_MSS 0x000E64DC
7210#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7211#define I40E_64BYTE_MSS 0x400000
7212 val = rd32(hw, I40E_REG_MSS);
7213 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7214 val &= ~I40E_REG_MSS_MIN_MASK;
7215 val |= I40E_64BYTE_MSS;
7216 wr32(hw, I40E_REG_MSS, val);
7217 }
7218
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007219 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007220 msleep(75);
7221 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7222 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007223 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7224 i40e_stat_str(&pf->hw, ret),
7225 i40e_aq_str(&pf->hw,
7226 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007227 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007228 /* reinit the misc interrupt */
7229 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7230 ret = i40e_setup_misc_vector(pf);
7231
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007232 /* Add a filter to drop all Flow control frames from any VSI from being
7233 * transmitted. By doing so we stop a malicious VF from sending out
7234 * PAUSE or PFC frames and potentially controlling traffic for other
7235 * PF/VF VSIs.
7236 * The FW can still send Flow control frames if enabled.
7237 */
7238 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7239 pf->main_vsi_seid);
7240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007241 /* restart the VSIs that were rebuilt and running before the reset */
7242 i40e_pf_unquiesce_all_vsi(pf);
7243
Mitch Williams69f64b22014-02-13 03:48:46 -08007244 if (pf->num_alloc_vfs) {
7245 for (v = 0; v < pf->num_alloc_vfs; v++)
7246 i40e_reset_vf(&pf->vf[v], true);
7247 }
7248
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007249 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007250 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007251
7252end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007253 clear_bit(__I40E_RESET_FAILED, &pf->state);
7254clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007255 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7256}
7257
7258/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007259 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007260 * @pf: board private structure
7261 *
7262 * Close up the VFs and other things in prep for a Core Reset,
7263 * then get ready to rebuild the world.
7264 **/
7265static void i40e_handle_reset_warning(struct i40e_pf *pf)
7266{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007267 i40e_prep_for_reset(pf);
7268 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007269}
7270
7271/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007272 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007273 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007274 *
7275 * Called from the MDD irq handler to identify possibly malicious vfs
7276 **/
7277static void i40e_handle_mdd_event(struct i40e_pf *pf)
7278{
7279 struct i40e_hw *hw = &pf->hw;
7280 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007281 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007282 struct i40e_vf *vf;
7283 u32 reg;
7284 int i;
7285
7286 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7287 return;
7288
7289 /* find what triggered the MDD event */
7290 reg = rd32(hw, I40E_GL_MDET_TX);
7291 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007292 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7293 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007294 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007295 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007296 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007297 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007298 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7299 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7300 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007301 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007302 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 +00007303 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007304 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7305 mdd_detected = true;
7306 }
7307 reg = rd32(hw, I40E_GL_MDET_RX);
7308 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007309 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7310 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007311 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007312 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007313 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7314 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7315 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007316 if (netif_msg_rx_err(pf))
7317 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7318 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007319 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7320 mdd_detected = true;
7321 }
7322
Neerav Parikhdf430b12014-06-04 01:23:15 +00007323 if (mdd_detected) {
7324 reg = rd32(hw, I40E_PF_MDET_TX);
7325 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7326 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007327 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007328 pf_mdd_detected = true;
7329 }
7330 reg = rd32(hw, I40E_PF_MDET_RX);
7331 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7332 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007333 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007334 pf_mdd_detected = true;
7335 }
7336 /* Queue belongs to the PF, initiate a reset */
7337 if (pf_mdd_detected) {
7338 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7339 i40e_service_event_schedule(pf);
7340 }
7341 }
7342
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007343 /* see if one of the VFs needs its hand slapped */
7344 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7345 vf = &(pf->vf[i]);
7346 reg = rd32(hw, I40E_VP_MDET_TX(i));
7347 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7348 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7349 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007350 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7351 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007352 }
7353
7354 reg = rd32(hw, I40E_VP_MDET_RX(i));
7355 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7356 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7357 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007358 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7359 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007360 }
7361
7362 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7363 dev_info(&pf->pdev->dev,
7364 "Too many MDD events on VF %d, disabled\n", i);
7365 dev_info(&pf->pdev->dev,
7366 "Use PF Control I/F to re-enable the VF\n");
7367 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7368 }
7369 }
7370
7371 /* re-enable mdd interrupt cause */
7372 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7373 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7374 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7375 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7376 i40e_flush(hw);
7377}
7378
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007379/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007380 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007381 * @pf: board private structure
7382 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007383static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007384{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007385 struct i40e_hw *hw = &pf->hw;
7386 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007387 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007388 int i;
7389
Singhai, Anjali6a899022015-12-14 12:21:18 -08007390 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007391 return;
7392
Singhai, Anjali6a899022015-12-14 12:21:18 -08007393 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007394
7395 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007396 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7397 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7398 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007399 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007400 ret = i40e_aq_add_udp_tunnel(hw, port,
7401 pf->udp_ports[i].type,
7402 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007403 else
7404 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007405
7406 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007407 dev_dbg(&pf->pdev->dev,
7408 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7409 pf->udp_ports[i].type ? "vxlan" : "geneve",
7410 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007411 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007412 i40e_stat_str(&pf->hw, ret),
7413 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007414 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007415 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007416 }
7417 }
7418 }
7419}
7420
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007421/**
7422 * i40e_service_task - Run the driver's async subtasks
7423 * @work: pointer to work_struct containing our data
7424 **/
7425static void i40e_service_task(struct work_struct *work)
7426{
7427 struct i40e_pf *pf = container_of(work,
7428 struct i40e_pf,
7429 service_task);
7430 unsigned long start_time = jiffies;
7431
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007432 /* don't bother with service tasks if a reset is in progress */
7433 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007434 return;
7435 }
7436
Mitch Williams91089032016-11-21 13:03:51 -08007437 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7438 return;
7439
Kiran Patilb03a8c12015-09-24 18:13:15 -04007440 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007441 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007442 i40e_reset_subtask(pf);
7443 i40e_handle_mdd_event(pf);
7444 i40e_vc_process_vflr_event(pf);
7445 i40e_watchdog_subtask(pf);
7446 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007447 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7448 /* Client subtask will reopen next time through. */
7449 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7450 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7451 } else {
7452 i40e_client_subtask(pf);
7453 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7454 i40e_notify_client_of_l2_param_changes(
7455 pf->vsi[pf->lan_vsi]);
7456 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7457 }
7458 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007459 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007460 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007461 i40e_clean_adminq_subtask(pf);
7462
Mitch Williams91089032016-11-21 13:03:51 -08007463 /* flush memory to make sure state is correct before next watchdog */
7464 smp_mb__before_atomic();
7465 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007466
7467 /* If the tasks have taken longer than one timer cycle or there
7468 * is more work to be done, reschedule the service task now
7469 * rather than wait for the timer to tick again.
7470 */
7471 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7472 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7473 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7474 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7475 i40e_service_event_schedule(pf);
7476}
7477
7478/**
7479 * i40e_service_timer - timer callback
7480 * @data: pointer to PF struct
7481 **/
7482static void i40e_service_timer(unsigned long data)
7483{
7484 struct i40e_pf *pf = (struct i40e_pf *)data;
7485
7486 mod_timer(&pf->service_timer,
7487 round_jiffies(jiffies + pf->service_timer_period));
7488 i40e_service_event_schedule(pf);
7489}
7490
7491/**
7492 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7493 * @vsi: the VSI being configured
7494 **/
7495static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7496{
7497 struct i40e_pf *pf = vsi->back;
7498
7499 switch (vsi->type) {
7500 case I40E_VSI_MAIN:
7501 vsi->alloc_queue_pairs = pf->num_lan_qps;
7502 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7503 I40E_REQ_DESCRIPTOR_MULTIPLE);
7504 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7505 vsi->num_q_vectors = pf->num_lan_msix;
7506 else
7507 vsi->num_q_vectors = 1;
7508
7509 break;
7510
7511 case I40E_VSI_FDIR:
7512 vsi->alloc_queue_pairs = 1;
7513 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7514 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007515 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007516 break;
7517
7518 case I40E_VSI_VMDQ2:
7519 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7520 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7521 I40E_REQ_DESCRIPTOR_MULTIPLE);
7522 vsi->num_q_vectors = pf->num_vmdq_msix;
7523 break;
7524
7525 case I40E_VSI_SRIOV:
7526 vsi->alloc_queue_pairs = pf->num_vf_qps;
7527 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7528 I40E_REQ_DESCRIPTOR_MULTIPLE);
7529 break;
7530
Vasu Dev38e00432014-08-01 13:27:03 -07007531#ifdef I40E_FCOE
7532 case I40E_VSI_FCOE:
7533 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7534 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7535 I40E_REQ_DESCRIPTOR_MULTIPLE);
7536 vsi->num_q_vectors = pf->num_fcoe_msix;
7537 break;
7538
7539#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007540 default:
7541 WARN_ON(1);
7542 return -ENODATA;
7543 }
7544
7545 return 0;
7546}
7547
7548/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007549 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7550 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007551 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007552 *
7553 * On error: returns error code (negative)
7554 * On success: returns 0
7555 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007556static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007557{
7558 int size;
7559 int ret = 0;
7560
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007561 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007562 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7563 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7564 if (!vsi->tx_rings)
7565 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007566 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7567
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007568 if (alloc_qvectors) {
7569 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007570 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007571 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7572 if (!vsi->q_vectors) {
7573 ret = -ENOMEM;
7574 goto err_vectors;
7575 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007576 }
7577 return ret;
7578
7579err_vectors:
7580 kfree(vsi->tx_rings);
7581 return ret;
7582}
7583
7584/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007585 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7586 * @pf: board private structure
7587 * @type: type of VSI
7588 *
7589 * On error: returns error code (negative)
7590 * On success: returns vsi index in PF (positive)
7591 **/
7592static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7593{
7594 int ret = -ENODEV;
7595 struct i40e_vsi *vsi;
7596 int vsi_idx;
7597 int i;
7598
7599 /* Need to protect the allocation of the VSIs at the PF level */
7600 mutex_lock(&pf->switch_mutex);
7601
7602 /* VSI list may be fragmented if VSI creation/destruction has
7603 * been happening. We can afford to do a quick scan to look
7604 * for any free VSIs in the list.
7605 *
7606 * find next empty vsi slot, looping back around if necessary
7607 */
7608 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007609 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007610 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007611 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007612 i = 0;
7613 while (i < pf->next_vsi && pf->vsi[i])
7614 i++;
7615 }
7616
Mitch Williams505682c2014-05-20 08:01:37 +00007617 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007618 vsi_idx = i; /* Found one! */
7619 } else {
7620 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007621 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007622 }
7623 pf->next_vsi = ++i;
7624
7625 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7626 if (!vsi) {
7627 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007628 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007629 }
7630 vsi->type = type;
7631 vsi->back = pf;
7632 set_bit(__I40E_DOWN, &vsi->state);
7633 vsi->flags = 0;
7634 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007635 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007636 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7637 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007638 vsi->netdev_registered = false;
7639 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007640 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007641 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007642
Alexander Duyck9f65e152013-09-28 06:00:58 +00007643 ret = i40e_set_num_rings_in_vsi(vsi);
7644 if (ret)
7645 goto err_rings;
7646
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007647 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007648 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007649 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007650
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007651 /* Setup default MSIX irq handler for VSI */
7652 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7653
Kiran Patil21659032015-09-30 14:09:03 -04007654 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007655 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007656 pf->vsi[vsi_idx] = vsi;
7657 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007658 goto unlock_pf;
7659
Alexander Duyck9f65e152013-09-28 06:00:58 +00007660err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007661 pf->next_vsi = i - 1;
7662 kfree(vsi);
7663unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007664 mutex_unlock(&pf->switch_mutex);
7665 return ret;
7666}
7667
7668/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007669 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7670 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007671 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007672 *
7673 * On error: returns error code (negative)
7674 * On success: returns 0
7675 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007676static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007677{
7678 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007679 if (free_qvectors) {
7680 kfree(vsi->q_vectors);
7681 vsi->q_vectors = NULL;
7682 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007683 kfree(vsi->tx_rings);
7684 vsi->tx_rings = NULL;
7685 vsi->rx_rings = NULL;
7686}
7687
7688/**
Helin Zhang28c58692015-10-26 19:44:27 -04007689 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7690 * and lookup table
7691 * @vsi: Pointer to VSI structure
7692 */
7693static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7694{
7695 if (!vsi)
7696 return;
7697
7698 kfree(vsi->rss_hkey_user);
7699 vsi->rss_hkey_user = NULL;
7700
7701 kfree(vsi->rss_lut_user);
7702 vsi->rss_lut_user = NULL;
7703}
7704
7705/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007706 * i40e_vsi_clear - Deallocate the VSI provided
7707 * @vsi: the VSI being un-configured
7708 **/
7709static int i40e_vsi_clear(struct i40e_vsi *vsi)
7710{
7711 struct i40e_pf *pf;
7712
7713 if (!vsi)
7714 return 0;
7715
7716 if (!vsi->back)
7717 goto free_vsi;
7718 pf = vsi->back;
7719
7720 mutex_lock(&pf->switch_mutex);
7721 if (!pf->vsi[vsi->idx]) {
7722 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7723 vsi->idx, vsi->idx, vsi, vsi->type);
7724 goto unlock_vsi;
7725 }
7726
7727 if (pf->vsi[vsi->idx] != vsi) {
7728 dev_err(&pf->pdev->dev,
7729 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7730 pf->vsi[vsi->idx]->idx,
7731 pf->vsi[vsi->idx],
7732 pf->vsi[vsi->idx]->type,
7733 vsi->idx, vsi, vsi->type);
7734 goto unlock_vsi;
7735 }
7736
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007737 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007738 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7739 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7740
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007741 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007742 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007743
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007744 pf->vsi[vsi->idx] = NULL;
7745 if (vsi->idx < pf->next_vsi)
7746 pf->next_vsi = vsi->idx;
7747
7748unlock_vsi:
7749 mutex_unlock(&pf->switch_mutex);
7750free_vsi:
7751 kfree(vsi);
7752
7753 return 0;
7754}
7755
7756/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007757 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7758 * @vsi: the VSI being cleaned
7759 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007760static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007761{
7762 int i;
7763
Greg Rose8e9dca52013-12-18 13:45:53 +00007764 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007765 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007766 kfree_rcu(vsi->tx_rings[i], rcu);
7767 vsi->tx_rings[i] = NULL;
7768 vsi->rx_rings[i] = NULL;
7769 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007770 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007771}
7772
7773/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007774 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7775 * @vsi: the VSI being configured
7776 **/
7777static int i40e_alloc_rings(struct i40e_vsi *vsi)
7778{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007779 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007780 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007781 int i;
7782
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007783 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007784 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007785 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007786 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7787 if (!tx_ring)
7788 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007789
7790 tx_ring->queue_index = i;
7791 tx_ring->reg_idx = vsi->base_queue + i;
7792 tx_ring->ring_active = false;
7793 tx_ring->vsi = vsi;
7794 tx_ring->netdev = vsi->netdev;
7795 tx_ring->dev = &pf->pdev->dev;
7796 tx_ring->count = vsi->num_desc;
7797 tx_ring->size = 0;
7798 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007799 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7800 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007801 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007802 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803
Alexander Duyck9f65e152013-09-28 06:00:58 +00007804 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007805 rx_ring->queue_index = i;
7806 rx_ring->reg_idx = vsi->base_queue + i;
7807 rx_ring->ring_active = false;
7808 rx_ring->vsi = vsi;
7809 rx_ring->netdev = vsi->netdev;
7810 rx_ring->dev = &pf->pdev->dev;
7811 rx_ring->count = vsi->num_desc;
7812 rx_ring->size = 0;
7813 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007814 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007815 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007816 }
7817
7818 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007819
7820err_out:
7821 i40e_vsi_clear_rings(vsi);
7822 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007823}
7824
7825/**
7826 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7827 * @pf: board private structure
7828 * @vectors: the number of MSI-X vectors to request
7829 *
7830 * Returns the number of vectors reserved, or error
7831 **/
7832static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7833{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007834 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7835 I40E_MIN_MSIX, vectors);
7836 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007838 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007839 vectors = 0;
7840 }
7841
7842 return vectors;
7843}
7844
7845/**
7846 * i40e_init_msix - Setup the MSIX capability
7847 * @pf: board private structure
7848 *
7849 * Work with the OS to set up the MSIX vectors needed.
7850 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007851 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007852 **/
7853static int i40e_init_msix(struct i40e_pf *pf)
7854{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007855 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007856 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007857 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007858 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007859 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007860 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007861
7862 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7863 return -ENODEV;
7864
7865 /* The number of vectors we'll request will be comprised of:
7866 * - Add 1 for "other" cause for Admin Queue events, etc.
7867 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007868 * - Queues being used for RSS.
7869 * We don't need as many as max_rss_size vectors.
7870 * use rss_size instead in the calculation since that
7871 * is governed by number of cpus in the system.
7872 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007873 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007874 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007875#ifdef I40E_FCOE
7876 * - The number of FCOE qps.
7877#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007878 * Once we count this up, try the request.
7879 *
7880 * If we can't get what we want, we'll simplify to nearly nothing
7881 * and try again. If that still fails, we punt.
7882 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007883 vectors_left = hw->func_caps.num_msix_vectors;
7884 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007885
Shannon Nelson1e200e42015-02-27 09:15:24 +00007886 /* reserve one vector for miscellaneous handler */
7887 if (vectors_left) {
7888 v_budget++;
7889 vectors_left--;
7890 }
7891
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007892 /* reserve some vectors for the main PF traffic queues. Initially we
7893 * only reserve at most 50% of the available vectors, in the case that
7894 * the number of online CPUs is large. This ensures that we can enable
7895 * extra features as well. Once we've enabled the other features, we
7896 * will use any remaining vectors to reach as close as we can to the
7897 * number of online CPUs.
7898 */
7899 cpus = num_online_cpus();
7900 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007901 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007902
7903 /* reserve one vector for sideband flow director */
7904 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7905 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007906 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007907 v_budget++;
7908 vectors_left--;
7909 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007910 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007911 }
7912 }
John W Linville83840e42015-01-14 03:06:28 +00007913
Vasu Dev38e00432014-08-01 13:27:03 -07007914#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007915 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007916 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007917 if (!vectors_left)
7918 pf->num_fcoe_msix = 0;
7919 else if (vectors_left >= pf->num_fcoe_qps)
7920 pf->num_fcoe_msix = pf->num_fcoe_qps;
7921 else
7922 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007923 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007924 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007925 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007926
Vasu Dev38e00432014-08-01 13:27:03 -07007927#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007928 /* can we reserve enough for iWARP? */
7929 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007930 iwarp_requested = pf->num_iwarp_msix;
7931
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007932 if (!vectors_left)
7933 pf->num_iwarp_msix = 0;
7934 else if (vectors_left < pf->num_iwarp_msix)
7935 pf->num_iwarp_msix = 1;
7936 v_budget += pf->num_iwarp_msix;
7937 vectors_left -= pf->num_iwarp_msix;
7938 }
7939
Shannon Nelson1e200e42015-02-27 09:15:24 +00007940 /* any vectors left over go for VMDq support */
7941 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7942 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7943 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7944
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007945 if (!vectors_left) {
7946 pf->num_vmdq_msix = 0;
7947 pf->num_vmdq_qps = 0;
7948 } else {
7949 /* if we're short on vectors for what's desired, we limit
7950 * the queues per vmdq. If this is still more than are
7951 * available, the user will need to change the number of
7952 * queues/vectors used by the PF later with the ethtool
7953 * channels command
7954 */
7955 if (vmdq_vecs < vmdq_vecs_wanted)
7956 pf->num_vmdq_qps = 1;
7957 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007958
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007959 v_budget += vmdq_vecs;
7960 vectors_left -= vmdq_vecs;
7961 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007962 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007963
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007964 /* On systems with a large number of SMP cores, we previously limited
7965 * the number of vectors for num_lan_msix to be at most 50% of the
7966 * available vectors, to allow for other features. Now, we add back
7967 * the remaining vectors. However, we ensure that the total
7968 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7969 * calculate the number of vectors we can add without going over the
7970 * cap of CPUs. For systems with a small number of CPUs this will be
7971 * zero.
7972 */
7973 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7974 pf->num_lan_msix += extra_vectors;
7975 vectors_left -= extra_vectors;
7976
7977 WARN(vectors_left < 0,
7978 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7979
7980 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7982 GFP_KERNEL);
7983 if (!pf->msix_entries)
7984 return -ENOMEM;
7985
7986 for (i = 0; i < v_budget; i++)
7987 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007988 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007989
Shannon Nelson3b444392015-02-26 16:15:57 +00007990 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7992 kfree(pf->msix_entries);
7993 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007994 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007995 return -ENODEV;
7996
Shannon Nelson3b444392015-02-26 16:15:57 +00007997 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007998 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007999 pf->num_vmdq_vsis = 0;
8000 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008001 pf->num_lan_qps = 1;
8002 pf->num_lan_msix = 1;
8003
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008004 } else if (!vectors_left) {
8005 /* If we have limited resources, we will start with no vectors
8006 * for the special features and then allocate vectors to some
8007 * of these features based on the policy and at the end disable
8008 * the features that did not get any vectors.
8009 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008010 int vec;
8011
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008012 dev_info(&pf->pdev->dev,
8013 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008014 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008015 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008016
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008017 /* Scale vector usage down */
8018 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008019 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008020 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008021#ifdef I40E_FCOE
8022 pf->num_fcoe_qps = 0;
8023 pf->num_fcoe_msix = 0;
8024#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008025
8026 /* partition out the remaining vectors */
8027 switch (vec) {
8028 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008029 pf->num_lan_msix = 1;
8030 break;
8031 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008032 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8033 pf->num_lan_msix = 1;
8034 pf->num_iwarp_msix = 1;
8035 } else {
8036 pf->num_lan_msix = 2;
8037 }
Vasu Dev38e00432014-08-01 13:27:03 -07008038#ifdef I40E_FCOE
8039 /* give one vector to FCoE */
8040 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
8041 pf->num_lan_msix = 1;
8042 pf->num_fcoe_msix = 1;
8043 }
Vasu Dev38e00432014-08-01 13:27:03 -07008044#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008045 break;
8046 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008047 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8048 pf->num_iwarp_msix = min_t(int, (vec / 3),
8049 iwarp_requested);
8050 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8051 I40E_DEFAULT_NUM_VMDQ_VSI);
8052 } else {
8053 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8054 I40E_DEFAULT_NUM_VMDQ_VSI);
8055 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008056 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8057 pf->num_fdsb_msix = 1;
8058 vec--;
8059 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008060 pf->num_lan_msix = min_t(int,
8061 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8062 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008063 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07008064#ifdef I40E_FCOE
8065 /* give one vector to FCoE */
8066 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
8067 pf->num_fcoe_msix = 1;
8068 vec--;
8069 }
8070#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008071 break;
8072 }
8073 }
8074
Stefan Assmannabd97a92016-09-19 13:37:51 +02008075 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8076 (pf->num_fdsb_msix == 0)) {
8077 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8078 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8079 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008080 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8081 (pf->num_vmdq_msix == 0)) {
8082 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8083 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8084 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008085
8086 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8087 (pf->num_iwarp_msix == 0)) {
8088 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8089 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8090 }
Vasu Dev38e00432014-08-01 13:27:03 -07008091#ifdef I40E_FCOE
8092
8093 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
8094 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
8095 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
8096 }
8097#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008098 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8099 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8100 pf->num_lan_msix,
8101 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8102 pf->num_fdsb_msix,
8103 pf->num_iwarp_msix);
8104
Shannon Nelson3b444392015-02-26 16:15:57 +00008105 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008106}
8107
8108/**
Greg Rose90e04072014-03-06 08:59:57 +00008109 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008110 * @vsi: the VSI being configured
8111 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008112 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008113 *
8114 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8115 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008116static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008117{
8118 struct i40e_q_vector *q_vector;
8119
8120 /* allocate q_vector */
8121 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8122 if (!q_vector)
8123 return -ENOMEM;
8124
8125 q_vector->vsi = vsi;
8126 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008127 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
8128
Alexander Duyck493fb302013-09-28 07:01:44 +00008129 if (vsi->netdev)
8130 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008131 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008132
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008133 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8134 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8135
Alexander Duyck493fb302013-09-28 07:01:44 +00008136 /* tie q_vector and vsi together */
8137 vsi->q_vectors[v_idx] = q_vector;
8138
8139 return 0;
8140}
8141
8142/**
Greg Rose90e04072014-03-06 08:59:57 +00008143 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008144 * @vsi: the VSI being configured
8145 *
8146 * We allocate one q_vector per queue interrupt. If allocation fails we
8147 * return -ENOMEM.
8148 **/
Greg Rose90e04072014-03-06 08:59:57 +00008149static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008150{
8151 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008152 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008153
8154 /* if not MSIX, give the one vector only to the LAN VSI */
8155 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8156 num_q_vectors = vsi->num_q_vectors;
8157 else if (vsi == pf->vsi[pf->lan_vsi])
8158 num_q_vectors = 1;
8159 else
8160 return -EINVAL;
8161
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008162 current_cpu = cpumask_first(cpu_online_mask);
8163
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008164 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008165 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008166 if (err)
8167 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008168 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8169 if (unlikely(current_cpu >= nr_cpu_ids))
8170 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008171 }
8172
8173 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008174
8175err_out:
8176 while (v_idx--)
8177 i40e_free_q_vector(vsi, v_idx);
8178
8179 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008180}
8181
8182/**
8183 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8184 * @pf: board private structure to initialize
8185 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008186static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008187{
Shannon Nelson3b444392015-02-26 16:15:57 +00008188 int vectors = 0;
8189 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008190
8191 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008192 vectors = i40e_init_msix(pf);
8193 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008194 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008195 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07008196#ifdef I40E_FCOE
8197 I40E_FLAG_FCOE_ENABLED |
8198#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008199 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008200 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008201 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008202 I40E_FLAG_SRIOV_ENABLED |
8203 I40E_FLAG_FD_SB_ENABLED |
8204 I40E_FLAG_FD_ATR_ENABLED |
8205 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008206
8207 /* rework the queue expectations without MSIX */
8208 i40e_determine_queue_usage(pf);
8209 }
8210 }
8211
8212 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8213 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008214 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008215 vectors = pci_enable_msi(pf->pdev);
8216 if (vectors < 0) {
8217 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8218 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008219 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8220 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008221 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008222 }
8223
Shannon Nelson958a3e32013-09-28 07:13:28 +00008224 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008225 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008226
Shannon Nelson3b444392015-02-26 16:15:57 +00008227 /* set up vector assignment tracking */
8228 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8229 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008230 if (!pf->irq_pile) {
8231 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8232 return -ENOMEM;
8233 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008234 pf->irq_pile->num_entries = vectors;
8235 pf->irq_pile->search_hint = 0;
8236
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008237 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008238 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008239
8240 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008241}
8242
8243/**
8244 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8245 * @pf: board private structure
8246 *
8247 * This sets up the handler for MSIX 0, which is used to manage the
8248 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8249 * when in MSI or Legacy interrupt mode.
8250 **/
8251static int i40e_setup_misc_vector(struct i40e_pf *pf)
8252{
8253 struct i40e_hw *hw = &pf->hw;
8254 int err = 0;
8255
8256 /* Only request the irq if this is the first time through, and
8257 * not when we're rebuilding after a Reset
8258 */
8259 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8260 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008261 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008262 if (err) {
8263 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008264 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008265 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008266 return -EFAULT;
8267 }
8268 }
8269
Jacob Kellerab437b52014-12-14 01:55:08 +00008270 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008271
8272 /* associate no queues to the misc vector */
8273 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8274 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8275
8276 i40e_flush(hw);
8277
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008278 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008279
8280 return err;
8281}
8282
8283/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008284 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8285 * @vsi: vsi structure
8286 * @seed: RSS hash seed
8287 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008288static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8289 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008290{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008291 struct i40e_pf *pf = vsi->back;
8292 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008293 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008294
Jacob Keller776b2e12016-07-19 16:23:30 -07008295 if (seed) {
8296 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8297 (struct i40e_aqc_get_set_rss_key_data *)seed;
8298 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8299 if (ret) {
8300 dev_info(&pf->pdev->dev,
8301 "Cannot set RSS key, err %s aq_err %s\n",
8302 i40e_stat_str(hw, ret),
8303 i40e_aq_str(hw, hw->aq.asq_last_status));
8304 return ret;
8305 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008306 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008307 if (lut) {
8308 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008309
Jacob Keller776b2e12016-07-19 16:23:30 -07008310 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8311 if (ret) {
8312 dev_info(&pf->pdev->dev,
8313 "Cannot set RSS lut, err %s aq_err %s\n",
8314 i40e_stat_str(hw, ret),
8315 i40e_aq_str(hw, hw->aq.asq_last_status));
8316 return ret;
8317 }
8318 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008319 return ret;
8320}
8321
8322/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008323 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8324 * @vsi: Pointer to vsi structure
8325 * @seed: Buffter to store the hash keys
8326 * @lut: Buffer to store the lookup table entries
8327 * @lut_size: Size of buffer to store the lookup table entries
8328 *
8329 * Return 0 on success, negative on failure
8330 */
8331static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8332 u8 *lut, u16 lut_size)
8333{
8334 struct i40e_pf *pf = vsi->back;
8335 struct i40e_hw *hw = &pf->hw;
8336 int ret = 0;
8337
8338 if (seed) {
8339 ret = i40e_aq_get_rss_key(hw, vsi->id,
8340 (struct i40e_aqc_get_set_rss_key_data *)seed);
8341 if (ret) {
8342 dev_info(&pf->pdev->dev,
8343 "Cannot get RSS key, err %s aq_err %s\n",
8344 i40e_stat_str(&pf->hw, ret),
8345 i40e_aq_str(&pf->hw,
8346 pf->hw.aq.asq_last_status));
8347 return ret;
8348 }
8349 }
8350
8351 if (lut) {
8352 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8353
8354 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8355 if (ret) {
8356 dev_info(&pf->pdev->dev,
8357 "Cannot get RSS lut, err %s aq_err %s\n",
8358 i40e_stat_str(&pf->hw, ret),
8359 i40e_aq_str(&pf->hw,
8360 pf->hw.aq.asq_last_status));
8361 return ret;
8362 }
8363 }
8364
8365 return ret;
8366}
8367
8368/**
Jacob Keller0582b962016-07-19 16:23:29 -07008369 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8370 * @vsi: VSI structure
8371 **/
8372static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8373{
8374 u8 seed[I40E_HKEY_ARRAY_SIZE];
8375 struct i40e_pf *pf = vsi->back;
8376 u8 *lut;
8377 int ret;
8378
8379 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8380 return 0;
8381
Jacob Keller552b9962016-07-19 16:23:31 -07008382 if (!vsi->rss_size)
8383 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8384 vsi->num_queue_pairs);
8385 if (!vsi->rss_size)
8386 return -EINVAL;
8387
Jacob Keller0582b962016-07-19 16:23:29 -07008388 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8389 if (!lut)
8390 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008391 /* Use the user configured hash keys and lookup table if there is one,
8392 * otherwise use default
8393 */
8394 if (vsi->rss_lut_user)
8395 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8396 else
8397 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8398 if (vsi->rss_hkey_user)
8399 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8400 else
8401 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008402 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8403 kfree(lut);
8404
8405 return ret;
8406}
8407
8408/**
Helin Zhang043dd652015-10-21 19:56:23 -04008409 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008410 * @vsi: Pointer to vsi structure
8411 * @seed: RSS hash seed
8412 * @lut: Lookup table
8413 * @lut_size: Lookup table size
8414 *
8415 * Returns 0 on success, negative on failure
8416 **/
8417static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8418 const u8 *lut, u16 lut_size)
8419{
8420 struct i40e_pf *pf = vsi->back;
8421 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008422 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008423 u8 i;
8424
8425 /* Fill out hash function seed */
8426 if (seed) {
8427 u32 *seed_dw = (u32 *)seed;
8428
Mitch Williamsc4e18682016-04-12 08:30:40 -07008429 if (vsi->type == I40E_VSI_MAIN) {
8430 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008431 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008432 } else if (vsi->type == I40E_VSI_SRIOV) {
8433 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008434 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008435 } else {
8436 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8437 }
Helin Zhange69ff812015-10-21 19:56:22 -04008438 }
8439
8440 if (lut) {
8441 u32 *lut_dw = (u32 *)lut;
8442
Mitch Williamsc4e18682016-04-12 08:30:40 -07008443 if (vsi->type == I40E_VSI_MAIN) {
8444 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8445 return -EINVAL;
8446 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8447 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8448 } else if (vsi->type == I40E_VSI_SRIOV) {
8449 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8450 return -EINVAL;
8451 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008452 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008453 } else {
8454 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8455 }
Helin Zhange69ff812015-10-21 19:56:22 -04008456 }
8457 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008458
8459 return 0;
8460}
8461
8462/**
Helin Zhang043dd652015-10-21 19:56:23 -04008463 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8464 * @vsi: Pointer to VSI structure
8465 * @seed: Buffer to store the keys
8466 * @lut: Buffer to store the lookup table entries
8467 * @lut_size: Size of buffer to store the lookup table entries
8468 *
8469 * Returns 0 on success, negative on failure
8470 */
8471static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8472 u8 *lut, u16 lut_size)
8473{
8474 struct i40e_pf *pf = vsi->back;
8475 struct i40e_hw *hw = &pf->hw;
8476 u16 i;
8477
8478 if (seed) {
8479 u32 *seed_dw = (u32 *)seed;
8480
8481 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008482 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008483 }
8484 if (lut) {
8485 u32 *lut_dw = (u32 *)lut;
8486
8487 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8488 return -EINVAL;
8489 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8490 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8491 }
8492
8493 return 0;
8494}
8495
8496/**
8497 * i40e_config_rss - Configure RSS keys and lut
8498 * @vsi: Pointer to VSI structure
8499 * @seed: RSS hash seed
8500 * @lut: Lookup table
8501 * @lut_size: Lookup table size
8502 *
8503 * Returns 0 on success, negative on failure
8504 */
8505int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8506{
8507 struct i40e_pf *pf = vsi->back;
8508
8509 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8510 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8511 else
8512 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8513}
8514
8515/**
8516 * i40e_get_rss - Get RSS keys and lut
8517 * @vsi: Pointer to VSI structure
8518 * @seed: Buffer to store the keys
8519 * @lut: Buffer to store the lookup table entries
8520 * lut_size: Size of buffer to store the lookup table entries
8521 *
8522 * Returns 0 on success, negative on failure
8523 */
8524int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8525{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008526 struct i40e_pf *pf = vsi->back;
8527
8528 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8529 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8530 else
8531 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008532}
8533
8534/**
Helin Zhange69ff812015-10-21 19:56:22 -04008535 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8536 * @pf: Pointer to board private structure
8537 * @lut: Lookup table
8538 * @rss_table_size: Lookup table size
8539 * @rss_size: Range of queue number for hashing
8540 */
Alan Bradyf1582352016-08-24 11:33:46 -07008541void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8542 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008543{
Helin Zhange69ff812015-10-21 19:56:22 -04008544 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008545
Helin Zhange69ff812015-10-21 19:56:22 -04008546 for (i = 0; i < rss_table_size; i++)
8547 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008548}
8549
8550/**
Helin Zhang043dd652015-10-21 19:56:23 -04008551 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008552 * @pf: board private structure
8553 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008554static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008556 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008557 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008558 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008559 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008560 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008561 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008562 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008563
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008564 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008565 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8566 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008567 hena |= i40e_pf_get_default_rss_hena(pf);
8568
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008569 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8570 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008571
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008572 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008573 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008574 reg_val = (pf->rss_table_size == 512) ?
8575 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8576 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008577 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008578
Helin Zhang28c58692015-10-26 19:44:27 -04008579 /* Determine the RSS size of the VSI */
8580 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008581 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8582 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008583 if (!vsi->rss_size)
8584 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008585
Helin Zhange69ff812015-10-21 19:56:22 -04008586 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8587 if (!lut)
8588 return -ENOMEM;
8589
Helin Zhang28c58692015-10-26 19:44:27 -04008590 /* Use user configured lut if there is one, otherwise use default */
8591 if (vsi->rss_lut_user)
8592 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8593 else
8594 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008595
Helin Zhang28c58692015-10-26 19:44:27 -04008596 /* Use user configured hash key if there is one, otherwise
8597 * use default.
8598 */
8599 if (vsi->rss_hkey_user)
8600 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8601 else
8602 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008603 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008604 kfree(lut);
8605
8606 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008607}
8608
8609/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008610 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8611 * @pf: board private structure
8612 * @queue_count: the requested queue count for rss.
8613 *
8614 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8615 * count which may be different from the requested queue count.
8616 **/
8617int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8618{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008619 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8620 int new_rss_size;
8621
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008622 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8623 return 0;
8624
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008625 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008626
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008627 if (queue_count != vsi->num_queue_pairs) {
8628 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008629 i40e_prep_for_reset(pf);
8630
Helin Zhangacd65442015-10-26 19:44:28 -04008631 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008632
8633 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008634
8635 /* Discard the user configured hash keys and lut, if less
8636 * queues are enabled.
8637 */
8638 if (queue_count < vsi->rss_size) {
8639 i40e_clear_rss_config_user(vsi);
8640 dev_dbg(&pf->pdev->dev,
8641 "discard user configured hash keys and lut\n");
8642 }
8643
8644 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008645 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8646 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008647
Helin Zhang043dd652015-10-21 19:56:23 -04008648 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008649 }
Lihong Yang12815052016-09-27 11:28:48 -07008650 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8651 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008652 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008653}
8654
8655/**
Greg Rosef4492db2015-02-06 08:52:12 +00008656 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8657 * @pf: board private structure
8658 **/
8659i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8660{
8661 i40e_status status;
8662 bool min_valid, max_valid;
8663 u32 max_bw, min_bw;
8664
8665 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8666 &min_valid, &max_valid);
8667
8668 if (!status) {
8669 if (min_valid)
8670 pf->npar_min_bw = min_bw;
8671 if (max_valid)
8672 pf->npar_max_bw = max_bw;
8673 }
8674
8675 return status;
8676}
8677
8678/**
8679 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8680 * @pf: board private structure
8681 **/
8682i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8683{
8684 struct i40e_aqc_configure_partition_bw_data bw_data;
8685 i40e_status status;
8686
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008687 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008688 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008689 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8690 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8691
8692 /* Set the new bandwidths */
8693 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8694
8695 return status;
8696}
8697
8698/**
8699 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8700 * @pf: board private structure
8701 **/
8702i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8703{
8704 /* Commit temporary BW setting to permanent NVM image */
8705 enum i40e_admin_queue_err last_aq_status;
8706 i40e_status ret;
8707 u16 nvm_word;
8708
8709 if (pf->hw.partition_id != 1) {
8710 dev_info(&pf->pdev->dev,
8711 "Commit BW only works on partition 1! This is partition %d",
8712 pf->hw.partition_id);
8713 ret = I40E_NOT_SUPPORTED;
8714 goto bw_commit_out;
8715 }
8716
8717 /* Acquire NVM for read access */
8718 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8719 last_aq_status = pf->hw.aq.asq_last_status;
8720 if (ret) {
8721 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008722 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8723 i40e_stat_str(&pf->hw, ret),
8724 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008725 goto bw_commit_out;
8726 }
8727
8728 /* Read word 0x10 of NVM - SW compatibility word 1 */
8729 ret = i40e_aq_read_nvm(&pf->hw,
8730 I40E_SR_NVM_CONTROL_WORD,
8731 0x10, sizeof(nvm_word), &nvm_word,
8732 false, NULL);
8733 /* Save off last admin queue command status before releasing
8734 * the NVM
8735 */
8736 last_aq_status = pf->hw.aq.asq_last_status;
8737 i40e_release_nvm(&pf->hw);
8738 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008739 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8740 i40e_stat_str(&pf->hw, ret),
8741 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008742 goto bw_commit_out;
8743 }
8744
8745 /* Wait a bit for NVM release to complete */
8746 msleep(50);
8747
8748 /* Acquire NVM for write access */
8749 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8750 last_aq_status = pf->hw.aq.asq_last_status;
8751 if (ret) {
8752 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008753 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8754 i40e_stat_str(&pf->hw, ret),
8755 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008756 goto bw_commit_out;
8757 }
8758 /* Write it back out unchanged to initiate update NVM,
8759 * which will force a write of the shadow (alt) RAM to
8760 * the NVM - thus storing the bandwidth values permanently.
8761 */
8762 ret = i40e_aq_update_nvm(&pf->hw,
8763 I40E_SR_NVM_CONTROL_WORD,
8764 0x10, sizeof(nvm_word),
8765 &nvm_word, true, NULL);
8766 /* Save off last admin queue command status before releasing
8767 * the NVM
8768 */
8769 last_aq_status = pf->hw.aq.asq_last_status;
8770 i40e_release_nvm(&pf->hw);
8771 if (ret)
8772 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008773 "BW settings NOT SAVED, err %s aq_err %s\n",
8774 i40e_stat_str(&pf->hw, ret),
8775 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008776bw_commit_out:
8777
8778 return ret;
8779}
8780
8781/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008782 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8783 * @pf: board private structure to initialize
8784 *
8785 * i40e_sw_init initializes the Adapter private data structure.
8786 * Fields are initialized based on PCI device information and
8787 * OS network device settings (MTU size).
8788 **/
8789static int i40e_sw_init(struct i40e_pf *pf)
8790{
8791 int err = 0;
8792 int size;
8793
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008794 /* Set default capability flags */
8795 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8796 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008797 I40E_FLAG_MSIX_ENABLED;
8798
Mitch Williamsca99eb92014-04-04 04:43:07 +00008799 /* Set default ITR */
8800 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8801 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8802
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008803 /* Depending on PF configurations, it is possible that the RSS
8804 * maximum might end up larger than the available queues
8805 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008806 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008807 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008808 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008809 pf->rss_size_max = min_t(int, pf->rss_size_max,
8810 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008811 if (pf->hw.func_caps.rss) {
8812 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008813 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8814 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008815 }
8816
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008817 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008818 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008819 pf->flags |= I40E_FLAG_MFP_ENABLED;
8820 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008821 if (i40e_get_npar_bw_setting(pf))
8822 dev_warn(&pf->pdev->dev,
8823 "Could not get NPAR bw settings\n");
8824 else
8825 dev_info(&pf->pdev->dev,
8826 "Min BW = %8.8x, Max BW = %8.8x\n",
8827 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008828 }
8829
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008830 /* FW/NVM is not yet fixed in this regard */
8831 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8832 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8833 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8834 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008835 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8836 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008837 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008838 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008839 else
8840 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008841 pf->fdir_pf_filter_count =
8842 pf->hw.func_caps.fd_filters_guaranteed;
8843 pf->hw.fdir_shared_filter_count =
8844 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008845 }
8846
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008847 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008848 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008849 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008850 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008851 /* No DCB support for FW < v4.33 */
8852 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8853 }
8854
8855 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008856 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008857 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8858 (pf->hw.aq.fw_maj_ver < 4)))
8859 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8860
8861 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008862 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008863 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8864 (pf->hw.aq.fw_maj_ver >= 5)))
8865 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008867 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008868 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008869 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008870 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008871 }
8872
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008873 if (pf->hw.func_caps.iwarp) {
8874 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8875 /* IWARP needs one extra vector for CQP just like MISC.*/
8876 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8877 }
8878
Vasu Dev38e00432014-08-01 13:27:03 -07008879#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008880 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008881
8882#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008883#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008884 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008885 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8886 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8887 pf->num_req_vfs = min_t(int,
8888 pf->hw.func_caps.num_vfs,
8889 I40E_MAX_VF_COUNT);
8890 }
8891#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008892 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008893 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8894 | I40E_FLAG_128_QP_RSS_CAPABLE
8895 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8896 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8897 | I40E_FLAG_WB_ON_ITR_CAPABLE
8898 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8899 | I40E_FLAG_NO_PCI_LINK_CHECK
8900 | I40E_FLAG_USE_SET_LLDP_MIB
8901 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8902 | I40E_FLAG_PTP_L4_CAPABLE
8903 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008904 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8905 ((pf->hw.aq.api_maj_ver == 1) &&
8906 (pf->hw.aq.api_min_ver > 4))) {
8907 /* Supported in FW API version higher than 1.4 */
8908 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008909 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008910 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008911 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008912 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008913
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008914 pf->eeprom_version = 0xDEAD;
8915 pf->lan_veb = I40E_NO_VEB;
8916 pf->lan_vsi = I40E_NO_VSI;
8917
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008918 /* By default FW has this off for performance reasons */
8919 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8920
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008921 /* set up queue assignment tracking */
8922 size = sizeof(struct i40e_lump_tracking)
8923 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8924 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8925 if (!pf->qp_pile) {
8926 err = -ENOMEM;
8927 goto sw_init_done;
8928 }
8929 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8930 pf->qp_pile->search_hint = 0;
8931
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008932 pf->tx_timeout_recovery_level = 1;
8933
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008934 mutex_init(&pf->switch_mutex);
8935
Greg Rosec668a122015-02-26 16:10:39 +00008936 /* If NPAR is enabled nudge the Tx scheduler */
8937 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8938 i40e_set_npar_bw_setting(pf);
8939
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008940sw_init_done:
8941 return err;
8942}
8943
8944/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008945 * i40e_set_ntuple - set the ntuple feature flag and take action
8946 * @pf: board private structure to initialize
8947 * @features: the feature set that the stack is suggesting
8948 *
8949 * returns a bool to indicate if reset needs to happen
8950 **/
8951bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8952{
8953 bool need_reset = false;
8954
8955 /* Check if Flow Director n-tuple support was enabled or disabled. If
8956 * the state changed, we need to reset.
8957 */
8958 if (features & NETIF_F_NTUPLE) {
8959 /* Enable filters and mark for reset */
8960 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8961 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008962 /* enable FD_SB only if there is MSI-X vector */
8963 if (pf->num_fdsb_msix > 0)
8964 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008965 } else {
8966 /* turn off filters, mark for reset and clear SW filter list */
8967 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8968 need_reset = true;
8969 i40e_fdir_filter_exit(pf);
8970 }
8971 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008972 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008973 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008974 pf->fd_add_err = 0;
8975 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008976 /* if ATR was auto disabled it can be re-enabled. */
8977 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008978 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8979 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008980 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8981 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8982 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008983 }
8984 return need_reset;
8985}
8986
8987/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008988 * i40e_clear_rss_lut - clear the rx hash lookup table
8989 * @vsi: the VSI being configured
8990 **/
8991static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8992{
8993 struct i40e_pf *pf = vsi->back;
8994 struct i40e_hw *hw = &pf->hw;
8995 u16 vf_id = vsi->vf_id;
8996 u8 i;
8997
8998 if (vsi->type == I40E_VSI_MAIN) {
8999 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
9000 wr32(hw, I40E_PFQF_HLUT(i), 0);
9001 } else if (vsi->type == I40E_VSI_SRIOV) {
9002 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
9003 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
9004 } else {
9005 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
9006 }
9007}
9008
9009/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009010 * i40e_set_features - set the netdev feature flags
9011 * @netdev: ptr to the netdev being adjusted
9012 * @features: the feature set that the stack is suggesting
9013 **/
9014static int i40e_set_features(struct net_device *netdev,
9015 netdev_features_t features)
9016{
9017 struct i40e_netdev_priv *np = netdev_priv(netdev);
9018 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009019 struct i40e_pf *pf = vsi->back;
9020 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009021
Alan Bradyd8ec9862016-07-27 12:02:38 -07009022 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9023 i40e_pf_config_rss(pf);
9024 else if (!(features & NETIF_F_RXHASH) &&
9025 netdev->features & NETIF_F_RXHASH)
9026 i40e_clear_rss_lut(vsi);
9027
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009028 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9029 i40e_vlan_stripping_enable(vsi);
9030 else
9031 i40e_vlan_stripping_disable(vsi);
9032
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009033 need_reset = i40e_set_ntuple(pf, features);
9034
9035 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04009036 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009037
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009038 return 0;
9039}
9040
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009041/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009042 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009043 * @pf: board private structure
9044 * @port: The UDP port to look up
9045 *
9046 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9047 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009048static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009049{
9050 u8 i;
9051
9052 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08009053 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009054 return i;
9055 }
9056
9057 return i;
9058}
9059
9060/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009061 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009062 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009063 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009064 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009065static void i40e_udp_tunnel_add(struct net_device *netdev,
9066 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009067{
9068 struct i40e_netdev_priv *np = netdev_priv(netdev);
9069 struct i40e_vsi *vsi = np->vsi;
9070 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009071 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009072 u8 next_idx;
9073 u8 idx;
9074
Singhai, Anjali6a899022015-12-14 12:21:18 -08009075 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009076
9077 /* Check if port already exists */
9078 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009079 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009080 return;
9081 }
9082
9083 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009084 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009085
9086 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009087 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009088 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009089 return;
9090 }
9091
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009092 switch (ti->type) {
9093 case UDP_TUNNEL_TYPE_VXLAN:
9094 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9095 break;
9096 case UDP_TUNNEL_TYPE_GENEVE:
9097 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
9098 return;
9099 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9100 break;
9101 default:
9102 return;
9103 }
9104
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009105 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009106 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009107 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9108 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009109}
9110
9111/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009112 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009113 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009114 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009115 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009116static void i40e_udp_tunnel_del(struct net_device *netdev,
9117 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009118{
9119 struct i40e_netdev_priv *np = netdev_priv(netdev);
9120 struct i40e_vsi *vsi = np->vsi;
9121 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009122 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009123 u8 idx;
9124
Singhai, Anjali6a899022015-12-14 12:21:18 -08009125 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009126
9127 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009128 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9129 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009130
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009131 switch (ti->type) {
9132 case UDP_TUNNEL_TYPE_VXLAN:
9133 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9134 goto not_found;
9135 break;
9136 case UDP_TUNNEL_TYPE_GENEVE:
9137 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9138 goto not_found;
9139 break;
9140 default:
9141 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009142 }
9143
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009144 /* if port exists, set it to 0 (mark for deletion)
9145 * and make it pending
9146 */
9147 pf->udp_ports[idx].index = 0;
9148 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009149 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9150
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009151 return;
9152not_found:
9153 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009154 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009155}
9156
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009157static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009158 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009159{
9160 struct i40e_netdev_priv *np = netdev_priv(netdev);
9161 struct i40e_pf *pf = np->vsi->back;
9162 struct i40e_hw *hw = &pf->hw;
9163
9164 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9165 return -EOPNOTSUPP;
9166
9167 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9168 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9169
9170 return 0;
9171}
9172
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009173/**
9174 * i40e_ndo_fdb_add - add an entry to the hardware database
9175 * @ndm: the input from the stack
9176 * @tb: pointer to array of nladdr (unused)
9177 * @dev: the net device pointer
9178 * @addr: the MAC address entry being added
9179 * @flags: instructions from stack about fdb operation
9180 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009181static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9182 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009183 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009184 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009185{
9186 struct i40e_netdev_priv *np = netdev_priv(dev);
9187 struct i40e_pf *pf = np->vsi->back;
9188 int err = 0;
9189
9190 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9191 return -EOPNOTSUPP;
9192
Or Gerlitz65891fe2014-12-14 18:19:05 +02009193 if (vid) {
9194 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9195 return -EINVAL;
9196 }
9197
Greg Rose4ba0dea2014-03-06 08:59:55 +00009198 /* Hardware does not support aging addresses so if a
9199 * ndm_state is given only allow permanent addresses
9200 */
9201 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9202 netdev_info(dev, "FDB only supports static addresses\n");
9203 return -EINVAL;
9204 }
9205
9206 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9207 err = dev_uc_add_excl(dev, addr);
9208 else if (is_multicast_ether_addr(addr))
9209 err = dev_mc_add_excl(dev, addr);
9210 else
9211 err = -EINVAL;
9212
9213 /* Only return duplicate errors if NLM_F_EXCL is set */
9214 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9215 err = 0;
9216
9217 return err;
9218}
9219
Neerav Parikh51616012015-02-06 08:52:14 +00009220/**
9221 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9222 * @dev: the netdev being configured
9223 * @nlh: RTNL message
9224 *
9225 * Inserts a new hardware bridge if not already created and
9226 * enables the bridging mode requested (VEB or VEPA). If the
9227 * hardware bridge has already been inserted and the request
9228 * is to change the mode then that requires a PF reset to
9229 * allow rebuild of the components with required hardware
9230 * bridge mode enabled.
9231 **/
9232static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009233 struct nlmsghdr *nlh,
9234 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009235{
9236 struct i40e_netdev_priv *np = netdev_priv(dev);
9237 struct i40e_vsi *vsi = np->vsi;
9238 struct i40e_pf *pf = vsi->back;
9239 struct i40e_veb *veb = NULL;
9240 struct nlattr *attr, *br_spec;
9241 int i, rem;
9242
9243 /* Only for PF VSI for now */
9244 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9245 return -EOPNOTSUPP;
9246
9247 /* Find the HW bridge for PF VSI */
9248 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9249 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9250 veb = pf->veb[i];
9251 }
9252
9253 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9254
9255 nla_for_each_nested(attr, br_spec, rem) {
9256 __u16 mode;
9257
9258 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9259 continue;
9260
9261 mode = nla_get_u16(attr);
9262 if ((mode != BRIDGE_MODE_VEPA) &&
9263 (mode != BRIDGE_MODE_VEB))
9264 return -EINVAL;
9265
9266 /* Insert a new HW bridge */
9267 if (!veb) {
9268 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9269 vsi->tc_config.enabled_tc);
9270 if (veb) {
9271 veb->bridge_mode = mode;
9272 i40e_config_bridge_mode(veb);
9273 } else {
9274 /* No Bridge HW offload available */
9275 return -ENOENT;
9276 }
9277 break;
9278 } else if (mode != veb->bridge_mode) {
9279 /* Existing HW bridge but different mode needs reset */
9280 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009281 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9282 if (mode == BRIDGE_MODE_VEB)
9283 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9284 else
9285 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9286 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009287 break;
9288 }
9289 }
9290
9291 return 0;
9292}
9293
9294/**
9295 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9296 * @skb: skb buff
9297 * @pid: process id
9298 * @seq: RTNL message seq #
9299 * @dev: the netdev being configured
9300 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009301 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009302 *
9303 * Return the mode in which the hardware bridge is operating in
9304 * i.e VEB or VEPA.
9305 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009306static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9307 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009308 u32 __always_unused filter_mask,
9309 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009310{
9311 struct i40e_netdev_priv *np = netdev_priv(dev);
9312 struct i40e_vsi *vsi = np->vsi;
9313 struct i40e_pf *pf = vsi->back;
9314 struct i40e_veb *veb = NULL;
9315 int i;
9316
9317 /* Only for PF VSI for now */
9318 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9319 return -EOPNOTSUPP;
9320
9321 /* Find the HW bridge for the PF VSI */
9322 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9323 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9324 veb = pf->veb[i];
9325 }
9326
9327 if (!veb)
9328 return 0;
9329
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009330 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009331 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009332}
Neerav Parikh51616012015-02-06 08:52:14 +00009333
Joe Stringerf44a75e2015-04-14 17:09:14 -07009334/**
9335 * i40e_features_check - Validate encapsulated packet conforms to limits
9336 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009337 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009338 * @features: Offload features that the stack believes apply
9339 **/
9340static netdev_features_t i40e_features_check(struct sk_buff *skb,
9341 struct net_device *dev,
9342 netdev_features_t features)
9343{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009344 size_t len;
9345
9346 /* No point in doing any of this if neither checksum nor GSO are
9347 * being requested for this frame. We can rule out both by just
9348 * checking for CHECKSUM_PARTIAL
9349 */
9350 if (skb->ip_summed != CHECKSUM_PARTIAL)
9351 return features;
9352
9353 /* We cannot support GSO if the MSS is going to be less than
9354 * 64 bytes. If it is then we need to drop support for GSO.
9355 */
9356 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9357 features &= ~NETIF_F_GSO_MASK;
9358
9359 /* MACLEN can support at most 63 words */
9360 len = skb_network_header(skb) - skb->data;
9361 if (len & ~(63 * 2))
9362 goto out_err;
9363
9364 /* IPLEN and EIPLEN can support at most 127 dwords */
9365 len = skb_transport_header(skb) - skb_network_header(skb);
9366 if (len & ~(127 * 4))
9367 goto out_err;
9368
9369 if (skb->encapsulation) {
9370 /* L4TUNLEN can support 127 words */
9371 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9372 if (len & ~(127 * 2))
9373 goto out_err;
9374
9375 /* IPLEN can support at most 127 dwords */
9376 len = skb_inner_transport_header(skb) -
9377 skb_inner_network_header(skb);
9378 if (len & ~(127 * 4))
9379 goto out_err;
9380 }
9381
9382 /* No need to validate L4LEN as TCP is the only protocol with a
9383 * a flexible value and we support all possible values supported
9384 * by TCP, which is at most 15 dwords
9385 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009386
9387 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009388out_err:
9389 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009390}
9391
Shannon Nelson37a29732015-02-27 09:15:19 +00009392static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009393 .ndo_open = i40e_open,
9394 .ndo_stop = i40e_close,
9395 .ndo_start_xmit = i40e_lan_xmit_frame,
9396 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9397 .ndo_set_rx_mode = i40e_set_rx_mode,
9398 .ndo_validate_addr = eth_validate_addr,
9399 .ndo_set_mac_address = i40e_set_mac,
9400 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009401 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009402 .ndo_tx_timeout = i40e_tx_timeout,
9403 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9404 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9405#ifdef CONFIG_NET_POLL_CONTROLLER
9406 .ndo_poll_controller = i40e_netpoll,
9407#endif
John Fastabende4c67342016-02-16 21:16:15 -08009408 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009409#ifdef I40E_FCOE
9410 .ndo_fcoe_enable = i40e_fcoe_enable,
9411 .ndo_fcoe_disable = i40e_fcoe_disable,
9412#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009413 .ndo_set_features = i40e_set_features,
9414 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9415 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009416 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009417 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009418 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009419 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009420 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009421 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9422 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009423 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009424 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009425 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009426 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9427 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009428};
9429
9430/**
9431 * i40e_config_netdev - Setup the netdev flags
9432 * @vsi: the VSI being configured
9433 *
9434 * Returns 0 on success, negative value on failure
9435 **/
9436static int i40e_config_netdev(struct i40e_vsi *vsi)
9437{
9438 struct i40e_pf *pf = vsi->back;
9439 struct i40e_hw *hw = &pf->hw;
9440 struct i40e_netdev_priv *np;
9441 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009442 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009443 u8 mac_addr[ETH_ALEN];
9444 int etherdev_size;
9445
9446 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009447 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009448 if (!netdev)
9449 return -ENOMEM;
9450
9451 vsi->netdev = netdev;
9452 np = netdev_priv(netdev);
9453 np->vsi = vsi;
9454
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009455 netdev->hw_enc_features |= NETIF_F_SG |
9456 NETIF_F_IP_CSUM |
9457 NETIF_F_IPV6_CSUM |
9458 NETIF_F_HIGHDMA |
9459 NETIF_F_SOFT_FEATURES |
9460 NETIF_F_TSO |
9461 NETIF_F_TSO_ECN |
9462 NETIF_F_TSO6 |
9463 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009464 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009465 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009466 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009467 NETIF_F_GSO_UDP_TUNNEL |
9468 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009469 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009470 NETIF_F_SCTP_CRC |
9471 NETIF_F_RXHASH |
9472 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009473 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009474
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009475 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009476 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9477
9478 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009479
9480 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009481 netdev->vlan_features |= netdev->hw_enc_features |
9482 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009483
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009484 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009485 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009486
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009487 netdev->hw_features |= netdev->hw_enc_features |
9488 NETIF_F_HW_VLAN_CTAG_TX |
9489 NETIF_F_HW_VLAN_CTAG_RX;
9490
9491 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009492 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009493
9494 if (vsi->type == I40E_VSI_MAIN) {
9495 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009496 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Filip Sadowski3954b392017-01-30 12:29:34 -08009497 /* The following steps are necessary to properly keep track of
9498 * MAC-VLAN filters loaded into firmware - first we remove
9499 * filter that is automatically generated by firmware and then
9500 * add new filter both to the driver hash table and firmware.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009501 */
9502 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009503 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009504 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009505 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009506 } else {
9507 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9508 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9509 pf->vsi[pf->lan_vsi]->netdev->name);
9510 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009511
Jacob Keller278e7d02016-10-05 09:30:37 -07009512 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009513 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009514 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009515 }
Kiran Patil21659032015-09-30 14:09:03 -04009516
Jacob Keller435c0842016-11-08 13:05:10 -08009517 /* Add the broadcast filter so that we initially will receive
9518 * broadcast packets. Note that when a new VLAN is first added the
9519 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9520 * specific filters as part of transitioning into "vlan" operation.
9521 * When more VLANs are added, the driver will copy each existing MAC
9522 * filter and add it for the new VLAN.
9523 *
9524 * Broadcast filters are handled specially by
9525 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9526 * promiscuous bit instead of adding this directly as a MAC/VLAN
9527 * filter. The subtask will update the correct broadcast promiscuous
9528 * bits as VLANs become active or inactive.
9529 */
9530 eth_broadcast_addr(broadcast);
9531 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009532 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009533 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9534
Greg Rose9a173902014-05-22 06:32:02 +00009535 ether_addr_copy(netdev->dev_addr, mac_addr);
9536 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009537
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009538 netdev->priv_flags |= IFF_UNICAST_FLT;
9539 netdev->priv_flags |= IFF_SUPP_NOFCS;
9540 /* Setup netdev TC information */
9541 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9542
9543 netdev->netdev_ops = &i40e_netdev_ops;
9544 netdev->watchdog_timeo = 5 * HZ;
9545 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009546#ifdef I40E_FCOE
9547 i40e_fcoe_config_netdev(netdev, vsi);
9548#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009549
Jarod Wilson91c527a2016-10-17 15:54:05 -04009550 /* MTU range: 68 - 9706 */
9551 netdev->min_mtu = ETH_MIN_MTU;
9552 netdev->max_mtu = I40E_MAX_RXBUFFER -
9553 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9554
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009555 return 0;
9556}
9557
9558/**
9559 * i40e_vsi_delete - Delete a VSI from the switch
9560 * @vsi: the VSI being removed
9561 *
9562 * Returns 0 on success, negative value on failure
9563 **/
9564static void i40e_vsi_delete(struct i40e_vsi *vsi)
9565{
9566 /* remove default VSI is not allowed */
9567 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9568 return;
9569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009570 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009571}
9572
9573/**
Neerav Parikh51616012015-02-06 08:52:14 +00009574 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9575 * @vsi: the VSI being queried
9576 *
9577 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9578 **/
9579int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9580{
9581 struct i40e_veb *veb;
9582 struct i40e_pf *pf = vsi->back;
9583
9584 /* Uplink is not a bridge so default to VEB */
9585 if (vsi->veb_idx == I40E_NO_VEB)
9586 return 1;
9587
9588 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009589 if (!veb) {
9590 dev_info(&pf->pdev->dev,
9591 "There is no veb associated with the bridge\n");
9592 return -ENOENT;
9593 }
Neerav Parikh51616012015-02-06 08:52:14 +00009594
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009595 /* Uplink is a bridge in VEPA mode */
9596 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9597 return 0;
9598 } else {
9599 /* Uplink is a bridge in VEB mode */
9600 return 1;
9601 }
9602
9603 /* VEPA is now default bridge, so return 0 */
9604 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009605}
9606
9607/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009608 * i40e_add_vsi - Add a VSI to the switch
9609 * @vsi: the VSI being configured
9610 *
9611 * This initializes a VSI context depending on the VSI type to be added and
9612 * passes it down to the add_vsi aq command.
9613 **/
9614static int i40e_add_vsi(struct i40e_vsi *vsi)
9615{
9616 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 struct i40e_pf *pf = vsi->back;
9618 struct i40e_hw *hw = &pf->hw;
9619 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009620 struct i40e_mac_filter *f;
9621 struct hlist_node *h;
9622 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009624 u8 enabled_tc = 0x1; /* TC0 enabled */
9625 int f_count = 0;
9626
9627 memset(&ctxt, 0, sizeof(ctxt));
9628 switch (vsi->type) {
9629 case I40E_VSI_MAIN:
9630 /* The PF's main VSI is already setup as part of the
9631 * device initialization, so we'll not bother with
9632 * the add_vsi call, but we will retrieve the current
9633 * VSI context.
9634 */
9635 ctxt.seid = pf->main_vsi_seid;
9636 ctxt.pf_num = pf->hw.pf_id;
9637 ctxt.vf_num = 0;
9638 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9639 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9640 if (ret) {
9641 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009642 "couldn't get PF vsi config, err %s aq_err %s\n",
9643 i40e_stat_str(&pf->hw, ret),
9644 i40e_aq_str(&pf->hw,
9645 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009646 return -ENOENT;
9647 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009648 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009649 vsi->info.valid_sections = 0;
9650
9651 vsi->seid = ctxt.seid;
9652 vsi->id = ctxt.vsi_number;
9653
9654 enabled_tc = i40e_pf_get_tc_map(pf);
9655
9656 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009657 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9658 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009659 memset(&ctxt, 0, sizeof(ctxt));
9660 ctxt.seid = pf->main_vsi_seid;
9661 ctxt.pf_num = pf->hw.pf_id;
9662 ctxt.vf_num = 0;
9663 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9664 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9665 if (ret) {
9666 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009667 "update vsi failed, err %s aq_err %s\n",
9668 i40e_stat_str(&pf->hw, ret),
9669 i40e_aq_str(&pf->hw,
9670 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009671 ret = -ENOENT;
9672 goto err;
9673 }
9674 /* update the local VSI info queue map */
9675 i40e_vsi_update_queue_map(vsi, &ctxt);
9676 vsi->info.valid_sections = 0;
9677 } else {
9678 /* Default/Main VSI is only enabled for TC0
9679 * reconfigure it to enable all TCs that are
9680 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009681 * For MFP case the iSCSI PF would use this
9682 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009683 */
9684 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9685 if (ret) {
9686 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009687 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9688 enabled_tc,
9689 i40e_stat_str(&pf->hw, ret),
9690 i40e_aq_str(&pf->hw,
9691 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009692 ret = -ENOENT;
9693 }
9694 }
9695 break;
9696
9697 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009698 ctxt.pf_num = hw->pf_id;
9699 ctxt.vf_num = 0;
9700 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009701 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009702 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009703 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9704 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009705 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009706 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009707 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009708 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009709 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009710 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009711 break;
9712
9713 case I40E_VSI_VMDQ2:
9714 ctxt.pf_num = hw->pf_id;
9715 ctxt.vf_num = 0;
9716 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009717 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009718 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009720 /* This VSI is connected to VEB so the switch_id
9721 * should be set to zero by default.
9722 */
Neerav Parikh51616012015-02-06 08:52:14 +00009723 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9724 ctxt.info.valid_sections |=
9725 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9726 ctxt.info.switch_id =
9727 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9728 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009729
9730 /* Setup the VSI tx/rx queue map for TC0 only for now */
9731 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9732 break;
9733
9734 case I40E_VSI_SRIOV:
9735 ctxt.pf_num = hw->pf_id;
9736 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9737 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009738 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009739 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009741 /* This VSI is connected to VEB so the switch_id
9742 * should be set to zero by default.
9743 */
Neerav Parikh51616012015-02-06 08:52:14 +00009744 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9745 ctxt.info.valid_sections |=
9746 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9747 ctxt.info.switch_id =
9748 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9749 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009750
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009751 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9752 ctxt.info.valid_sections |=
9753 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9754 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009755 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9756 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009757 }
9758
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009759 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9760 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009761 if (pf->vf[vsi->vf_id].spoofchk) {
9762 ctxt.info.valid_sections |=
9763 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9764 ctxt.info.sec_flags |=
9765 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9766 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9767 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009768 /* Setup the VSI tx/rx queue map for TC0 only for now */
9769 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9770 break;
9771
Vasu Dev38e00432014-08-01 13:27:03 -07009772#ifdef I40E_FCOE
9773 case I40E_VSI_FCOE:
9774 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9775 if (ret) {
9776 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9777 return ret;
9778 }
9779 break;
9780
9781#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009782 case I40E_VSI_IWARP:
9783 /* send down message to iWARP */
9784 break;
9785
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009786 default:
9787 return -ENODEV;
9788 }
9789
9790 if (vsi->type != I40E_VSI_MAIN) {
9791 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9792 if (ret) {
9793 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009794 "add vsi failed, err %s aq_err %s\n",
9795 i40e_stat_str(&pf->hw, ret),
9796 i40e_aq_str(&pf->hw,
9797 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009798 ret = -ENOENT;
9799 goto err;
9800 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009801 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009802 vsi->info.valid_sections = 0;
9803 vsi->seid = ctxt.seid;
9804 vsi->id = ctxt.vsi_number;
9805 }
9806
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009807 vsi->active_filters = 0;
9808 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009809 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009810 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009811 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009812 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009813 f_count++;
9814 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009815 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009816
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009817 if (f_count) {
9818 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9819 pf->flags |= I40E_FLAG_FILTER_SYNC;
9820 }
9821
9822 /* Update VSI BW information */
9823 ret = i40e_vsi_get_bw_info(vsi);
9824 if (ret) {
9825 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009826 "couldn't get vsi bw info, err %s aq_err %s\n",
9827 i40e_stat_str(&pf->hw, ret),
9828 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009829 /* VSI is already added so not tearing that up */
9830 ret = 0;
9831 }
9832
9833err:
9834 return ret;
9835}
9836
9837/**
9838 * i40e_vsi_release - Delete a VSI and free its resources
9839 * @vsi: the VSI being removed
9840 *
9841 * Returns 0 on success or < 0 on error
9842 **/
9843int i40e_vsi_release(struct i40e_vsi *vsi)
9844{
Jacob Keller278e7d02016-10-05 09:30:37 -07009845 struct i40e_mac_filter *f;
9846 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009847 struct i40e_veb *veb = NULL;
9848 struct i40e_pf *pf;
9849 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009850 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009851
9852 pf = vsi->back;
9853
9854 /* release of a VEB-owner or last VSI is not allowed */
9855 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9856 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9857 vsi->seid, vsi->uplink_seid);
9858 return -ENODEV;
9859 }
9860 if (vsi == pf->vsi[pf->lan_vsi] &&
9861 !test_bit(__I40E_DOWN, &pf->state)) {
9862 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9863 return -ENODEV;
9864 }
9865
9866 uplink_seid = vsi->uplink_seid;
9867 if (vsi->type != I40E_VSI_SRIOV) {
9868 if (vsi->netdev_registered) {
9869 vsi->netdev_registered = false;
9870 if (vsi->netdev) {
9871 /* results in a call to i40e_close() */
9872 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009873 }
9874 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009875 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009876 }
9877 i40e_vsi_disable_irq(vsi);
9878 }
9879
Jacob Keller278e7d02016-10-05 09:30:37 -07009880 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009881
9882 /* clear the sync flag on all filters */
9883 if (vsi->netdev) {
9884 __dev_uc_unsync(vsi->netdev, NULL);
9885 __dev_mc_unsync(vsi->netdev, NULL);
9886 }
9887
9888 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009889 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009890 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009891
Jacob Keller278e7d02016-10-05 09:30:37 -07009892 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009893
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009894 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009895
9896 i40e_vsi_delete(vsi);
9897 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009898 if (vsi->netdev) {
9899 free_netdev(vsi->netdev);
9900 vsi->netdev = NULL;
9901 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009902 i40e_vsi_clear_rings(vsi);
9903 i40e_vsi_clear(vsi);
9904
9905 /* If this was the last thing on the VEB, except for the
9906 * controlling VSI, remove the VEB, which puts the controlling
9907 * VSI onto the next level down in the switch.
9908 *
9909 * Well, okay, there's one more exception here: don't remove
9910 * the orphan VEBs yet. We'll wait for an explicit remove request
9911 * from up the network stack.
9912 */
Mitch Williams505682c2014-05-20 08:01:37 +00009913 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009914 if (pf->vsi[i] &&
9915 pf->vsi[i]->uplink_seid == uplink_seid &&
9916 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9917 n++; /* count the VSIs */
9918 }
9919 }
9920 for (i = 0; i < I40E_MAX_VEB; i++) {
9921 if (!pf->veb[i])
9922 continue;
9923 if (pf->veb[i]->uplink_seid == uplink_seid)
9924 n++; /* count the VEBs */
9925 if (pf->veb[i]->seid == uplink_seid)
9926 veb = pf->veb[i];
9927 }
9928 if (n == 0 && veb && veb->uplink_seid != 0)
9929 i40e_veb_release(veb);
9930
9931 return 0;
9932}
9933
9934/**
9935 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9936 * @vsi: ptr to the VSI
9937 *
9938 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9939 * corresponding SW VSI structure and initializes num_queue_pairs for the
9940 * newly allocated VSI.
9941 *
9942 * Returns 0 on success or negative on failure
9943 **/
9944static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9945{
9946 int ret = -ENOENT;
9947 struct i40e_pf *pf = vsi->back;
9948
Alexander Duyck493fb302013-09-28 07:01:44 +00009949 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009950 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9951 vsi->seid);
9952 return -EEXIST;
9953 }
9954
9955 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009956 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009957 vsi->seid, vsi->base_vector);
9958 return -EEXIST;
9959 }
9960
Greg Rose90e04072014-03-06 08:59:57 +00009961 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009962 if (ret) {
9963 dev_info(&pf->pdev->dev,
9964 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9965 vsi->num_q_vectors, vsi->seid, ret);
9966 vsi->num_q_vectors = 0;
9967 goto vector_setup_out;
9968 }
9969
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009970 /* In Legacy mode, we do not have to get any other vector since we
9971 * piggyback on the misc/ICR0 for queue interrupts.
9972 */
9973 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9974 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009975 if (vsi->num_q_vectors)
9976 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9977 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009978 if (vsi->base_vector < 0) {
9979 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009980 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9981 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009982 i40e_vsi_free_q_vectors(vsi);
9983 ret = -ENOENT;
9984 goto vector_setup_out;
9985 }
9986
9987vector_setup_out:
9988 return ret;
9989}
9990
9991/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009992 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9993 * @vsi: pointer to the vsi.
9994 *
9995 * This re-allocates a vsi's queue resources.
9996 *
9997 * Returns pointer to the successfully allocated and configured VSI sw struct
9998 * on success, otherwise returns NULL on failure.
9999 **/
10000static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
10001{
John Underwoodf5340392016-02-18 09:19:24 -080010002 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010003 u8 enabled_tc;
10004 int ret;
10005
John Underwoodf5340392016-02-18 09:19:24 -080010006 if (!vsi)
10007 return NULL;
10008
10009 pf = vsi->back;
10010
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010011 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10012 i40e_vsi_clear_rings(vsi);
10013
10014 i40e_vsi_free_arrays(vsi, false);
10015 i40e_set_num_rings_in_vsi(vsi);
10016 ret = i40e_vsi_alloc_arrays(vsi, false);
10017 if (ret)
10018 goto err_vsi;
10019
10020 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
10021 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010022 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010023 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +000010024 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010025 goto err_vsi;
10026 }
10027 vsi->base_queue = ret;
10028
10029 /* Update the FW view of the VSI. Force a reset of TC and queue
10030 * layout configurations.
10031 */
10032 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10033 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10034 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10035 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010036 if (vsi->type == I40E_VSI_MAIN)
10037 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010038
10039 /* assign it some queues */
10040 ret = i40e_alloc_rings(vsi);
10041 if (ret)
10042 goto err_rings;
10043
10044 /* map all of the rings to the q_vectors */
10045 i40e_vsi_map_rings_to_vectors(vsi);
10046 return vsi;
10047
10048err_rings:
10049 i40e_vsi_free_q_vectors(vsi);
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 }
10056 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10057err_vsi:
10058 i40e_vsi_clear(vsi);
10059 return NULL;
10060}
10061
10062/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010063 * i40e_vsi_setup - Set up a VSI by a given type
10064 * @pf: board private structure
10065 * @type: VSI type
10066 * @uplink_seid: the switch element to link to
10067 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10068 *
10069 * This allocates the sw VSI structure and its queue resources, then add a VSI
10070 * to the identified VEB.
10071 *
10072 * Returns pointer to the successfully allocated and configure VSI sw struct on
10073 * success, otherwise returns NULL on failure.
10074 **/
10075struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10076 u16 uplink_seid, u32 param1)
10077{
10078 struct i40e_vsi *vsi = NULL;
10079 struct i40e_veb *veb = NULL;
10080 int ret, i;
10081 int v_idx;
10082
10083 /* The requested uplink_seid must be either
10084 * - the PF's port seid
10085 * no VEB is needed because this is the PF
10086 * or this is a Flow Director special case VSI
10087 * - seid of an existing VEB
10088 * - seid of a VSI that owns an existing VEB
10089 * - seid of a VSI that doesn't own a VEB
10090 * a new VEB is created and the VSI becomes the owner
10091 * - seid of the PF VSI, which is what creates the first VEB
10092 * this is a special case of the previous
10093 *
10094 * Find which uplink_seid we were given and create a new VEB if needed
10095 */
10096 for (i = 0; i < I40E_MAX_VEB; i++) {
10097 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10098 veb = pf->veb[i];
10099 break;
10100 }
10101 }
10102
10103 if (!veb && uplink_seid != pf->mac_seid) {
10104
Mitch Williams505682c2014-05-20 08:01:37 +000010105 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010106 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10107 vsi = pf->vsi[i];
10108 break;
10109 }
10110 }
10111 if (!vsi) {
10112 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10113 uplink_seid);
10114 return NULL;
10115 }
10116
10117 if (vsi->uplink_seid == pf->mac_seid)
10118 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10119 vsi->tc_config.enabled_tc);
10120 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10121 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10122 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010123 if (veb) {
10124 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10125 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010126 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010127 return NULL;
10128 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010129 /* We come up by default in VEPA mode if SRIOV is not
10130 * already enabled, in which case we can't force VEPA
10131 * mode.
10132 */
10133 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10134 veb->bridge_mode = BRIDGE_MODE_VEPA;
10135 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10136 }
Neerav Parikh51616012015-02-06 08:52:14 +000010137 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010138 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010139 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10140 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10141 veb = pf->veb[i];
10142 }
10143 if (!veb) {
10144 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10145 return NULL;
10146 }
10147
10148 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10149 uplink_seid = veb->seid;
10150 }
10151
10152 /* get vsi sw struct */
10153 v_idx = i40e_vsi_mem_alloc(pf, type);
10154 if (v_idx < 0)
10155 goto err_alloc;
10156 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010157 if (!vsi)
10158 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010159 vsi->type = type;
10160 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10161
10162 if (type == I40E_VSI_MAIN)
10163 pf->lan_vsi = v_idx;
10164 else if (type == I40E_VSI_SRIOV)
10165 vsi->vf_id = param1;
10166 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010167 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
10168 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010169 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010170 dev_info(&pf->pdev->dev,
10171 "failed to get tracking for %d queues for VSI %d err=%d\n",
10172 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010173 goto err_vsi;
10174 }
10175 vsi->base_queue = ret;
10176
10177 /* get a VSI from the hardware */
10178 vsi->uplink_seid = uplink_seid;
10179 ret = i40e_add_vsi(vsi);
10180 if (ret)
10181 goto err_vsi;
10182
10183 switch (vsi->type) {
10184 /* setup the netdev if needed */
10185 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010186 /* Apply relevant filters if a platform-specific mac
10187 * address was selected.
10188 */
10189 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10190 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10191 if (ret) {
10192 dev_warn(&pf->pdev->dev,
10193 "could not set up macaddr; err %d\n",
10194 ret);
10195 }
10196 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010197 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -070010198 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010199 ret = i40e_config_netdev(vsi);
10200 if (ret)
10201 goto err_netdev;
10202 ret = register_netdev(vsi->netdev);
10203 if (ret)
10204 goto err_netdev;
10205 vsi->netdev_registered = true;
10206 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010207#ifdef CONFIG_I40E_DCB
10208 /* Setup DCB netlink interface */
10209 i40e_dcbnl_setup(vsi);
10210#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010211 /* fall through */
10212
10213 case I40E_VSI_FDIR:
10214 /* set up vectors and rings if needed */
10215 ret = i40e_vsi_setup_vectors(vsi);
10216 if (ret)
10217 goto err_msix;
10218
10219 ret = i40e_alloc_rings(vsi);
10220 if (ret)
10221 goto err_rings;
10222
10223 /* map all of the rings to the q_vectors */
10224 i40e_vsi_map_rings_to_vectors(vsi);
10225
10226 i40e_vsi_reset_stats(vsi);
10227 break;
10228
10229 default:
10230 /* no netdev or rings for the other VSI types */
10231 break;
10232 }
10233
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010234 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10235 (vsi->type == I40E_VSI_VMDQ2)) {
10236 ret = i40e_vsi_config_rss(vsi);
10237 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010238 return vsi;
10239
10240err_rings:
10241 i40e_vsi_free_q_vectors(vsi);
10242err_msix:
10243 if (vsi->netdev_registered) {
10244 vsi->netdev_registered = false;
10245 unregister_netdev(vsi->netdev);
10246 free_netdev(vsi->netdev);
10247 vsi->netdev = NULL;
10248 }
10249err_netdev:
10250 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10251err_vsi:
10252 i40e_vsi_clear(vsi);
10253err_alloc:
10254 return NULL;
10255}
10256
10257/**
10258 * i40e_veb_get_bw_info - Query VEB BW information
10259 * @veb: the veb to query
10260 *
10261 * Query the Tx scheduler BW configuration data for given VEB
10262 **/
10263static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10264{
10265 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10266 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10267 struct i40e_pf *pf = veb->pf;
10268 struct i40e_hw *hw = &pf->hw;
10269 u32 tc_bw_max;
10270 int ret = 0;
10271 int i;
10272
10273 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10274 &bw_data, NULL);
10275 if (ret) {
10276 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010277 "query veb bw config failed, err %s aq_err %s\n",
10278 i40e_stat_str(&pf->hw, ret),
10279 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010280 goto out;
10281 }
10282
10283 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10284 &ets_data, NULL);
10285 if (ret) {
10286 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010287 "query veb bw ets config failed, err %s aq_err %s\n",
10288 i40e_stat_str(&pf->hw, ret),
10289 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010290 goto out;
10291 }
10292
10293 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10294 veb->bw_max_quanta = ets_data.tc_bw_max;
10295 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010296 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010297 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10298 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10299 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10300 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10301 veb->bw_tc_limit_credits[i] =
10302 le16_to_cpu(bw_data.tc_bw_limits[i]);
10303 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10304 }
10305
10306out:
10307 return ret;
10308}
10309
10310/**
10311 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10312 * @pf: board private structure
10313 *
10314 * On error: returns error code (negative)
10315 * On success: returns vsi index in PF (positive)
10316 **/
10317static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10318{
10319 int ret = -ENOENT;
10320 struct i40e_veb *veb;
10321 int i;
10322
10323 /* Need to protect the allocation of switch elements at the PF level */
10324 mutex_lock(&pf->switch_mutex);
10325
10326 /* VEB list may be fragmented if VEB creation/destruction has
10327 * been happening. We can afford to do a quick scan to look
10328 * for any free slots in the list.
10329 *
10330 * find next empty veb slot, looping back around if necessary
10331 */
10332 i = 0;
10333 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10334 i++;
10335 if (i >= I40E_MAX_VEB) {
10336 ret = -ENOMEM;
10337 goto err_alloc_veb; /* out of VEB slots! */
10338 }
10339
10340 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10341 if (!veb) {
10342 ret = -ENOMEM;
10343 goto err_alloc_veb;
10344 }
10345 veb->pf = pf;
10346 veb->idx = i;
10347 veb->enabled_tc = 1;
10348
10349 pf->veb[i] = veb;
10350 ret = i;
10351err_alloc_veb:
10352 mutex_unlock(&pf->switch_mutex);
10353 return ret;
10354}
10355
10356/**
10357 * i40e_switch_branch_release - Delete a branch of the switch tree
10358 * @branch: where to start deleting
10359 *
10360 * This uses recursion to find the tips of the branch to be
10361 * removed, deleting until we get back to and can delete this VEB.
10362 **/
10363static void i40e_switch_branch_release(struct i40e_veb *branch)
10364{
10365 struct i40e_pf *pf = branch->pf;
10366 u16 branch_seid = branch->seid;
10367 u16 veb_idx = branch->idx;
10368 int i;
10369
10370 /* release any VEBs on this VEB - RECURSION */
10371 for (i = 0; i < I40E_MAX_VEB; i++) {
10372 if (!pf->veb[i])
10373 continue;
10374 if (pf->veb[i]->uplink_seid == branch->seid)
10375 i40e_switch_branch_release(pf->veb[i]);
10376 }
10377
10378 /* Release the VSIs on this VEB, but not the owner VSI.
10379 *
10380 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10381 * the VEB itself, so don't use (*branch) after this loop.
10382 */
Mitch Williams505682c2014-05-20 08:01:37 +000010383 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010384 if (!pf->vsi[i])
10385 continue;
10386 if (pf->vsi[i]->uplink_seid == branch_seid &&
10387 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10388 i40e_vsi_release(pf->vsi[i]);
10389 }
10390 }
10391
10392 /* There's one corner case where the VEB might not have been
10393 * removed, so double check it here and remove it if needed.
10394 * This case happens if the veb was created from the debugfs
10395 * commands and no VSIs were added to it.
10396 */
10397 if (pf->veb[veb_idx])
10398 i40e_veb_release(pf->veb[veb_idx]);
10399}
10400
10401/**
10402 * i40e_veb_clear - remove veb struct
10403 * @veb: the veb to remove
10404 **/
10405static void i40e_veb_clear(struct i40e_veb *veb)
10406{
10407 if (!veb)
10408 return;
10409
10410 if (veb->pf) {
10411 struct i40e_pf *pf = veb->pf;
10412
10413 mutex_lock(&pf->switch_mutex);
10414 if (pf->veb[veb->idx] == veb)
10415 pf->veb[veb->idx] = NULL;
10416 mutex_unlock(&pf->switch_mutex);
10417 }
10418
10419 kfree(veb);
10420}
10421
10422/**
10423 * i40e_veb_release - Delete a VEB and free its resources
10424 * @veb: the VEB being removed
10425 **/
10426void i40e_veb_release(struct i40e_veb *veb)
10427{
10428 struct i40e_vsi *vsi = NULL;
10429 struct i40e_pf *pf;
10430 int i, n = 0;
10431
10432 pf = veb->pf;
10433
10434 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010435 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010436 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10437 n++;
10438 vsi = pf->vsi[i];
10439 }
10440 }
10441 if (n != 1) {
10442 dev_info(&pf->pdev->dev,
10443 "can't remove VEB %d with %d VSIs left\n",
10444 veb->seid, n);
10445 return;
10446 }
10447
10448 /* move the remaining VSI to uplink veb */
10449 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10450 if (veb->uplink_seid) {
10451 vsi->uplink_seid = veb->uplink_seid;
10452 if (veb->uplink_seid == pf->mac_seid)
10453 vsi->veb_idx = I40E_NO_VEB;
10454 else
10455 vsi->veb_idx = veb->veb_idx;
10456 } else {
10457 /* floating VEB */
10458 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10459 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10460 }
10461
10462 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10463 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010464}
10465
10466/**
10467 * i40e_add_veb - create the VEB in the switch
10468 * @veb: the VEB to be instantiated
10469 * @vsi: the controlling VSI
10470 **/
10471static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10472{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010473 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010474 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010475 int ret;
10476
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010477 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010478 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010479 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010480
10481 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010482 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010483 dev_info(&pf->pdev->dev,
10484 "couldn't add VEB, err %s aq_err %s\n",
10485 i40e_stat_str(&pf->hw, ret),
10486 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010487 return -EPERM;
10488 }
10489
10490 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010491 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010492 &veb->stats_idx, NULL, NULL, NULL);
10493 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010494 dev_info(&pf->pdev->dev,
10495 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10496 i40e_stat_str(&pf->hw, ret),
10497 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010498 return -EPERM;
10499 }
10500 ret = i40e_veb_get_bw_info(veb);
10501 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010502 dev_info(&pf->pdev->dev,
10503 "couldn't get VEB bw info, err %s aq_err %s\n",
10504 i40e_stat_str(&pf->hw, ret),
10505 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10506 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010507 return -ENOENT;
10508 }
10509
10510 vsi->uplink_seid = veb->seid;
10511 vsi->veb_idx = veb->idx;
10512 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10513
10514 return 0;
10515}
10516
10517/**
10518 * i40e_veb_setup - Set up a VEB
10519 * @pf: board private structure
10520 * @flags: VEB setup flags
10521 * @uplink_seid: the switch element to link to
10522 * @vsi_seid: the initial VSI seid
10523 * @enabled_tc: Enabled TC bit-map
10524 *
10525 * This allocates the sw VEB structure and links it into the switch
10526 * It is possible and legal for this to be a duplicate of an already
10527 * existing VEB. It is also possible for both uplink and vsi seids
10528 * to be zero, in order to create a floating VEB.
10529 *
10530 * Returns pointer to the successfully allocated VEB sw struct on
10531 * success, otherwise returns NULL on failure.
10532 **/
10533struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10534 u16 uplink_seid, u16 vsi_seid,
10535 u8 enabled_tc)
10536{
10537 struct i40e_veb *veb, *uplink_veb = NULL;
10538 int vsi_idx, veb_idx;
10539 int ret;
10540
10541 /* if one seid is 0, the other must be 0 to create a floating relay */
10542 if ((uplink_seid == 0 || vsi_seid == 0) &&
10543 (uplink_seid + vsi_seid != 0)) {
10544 dev_info(&pf->pdev->dev,
10545 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10546 uplink_seid, vsi_seid);
10547 return NULL;
10548 }
10549
10550 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010551 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010552 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10553 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010554 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010555 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10556 vsi_seid);
10557 return NULL;
10558 }
10559
10560 if (uplink_seid && uplink_seid != pf->mac_seid) {
10561 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10562 if (pf->veb[veb_idx] &&
10563 pf->veb[veb_idx]->seid == uplink_seid) {
10564 uplink_veb = pf->veb[veb_idx];
10565 break;
10566 }
10567 }
10568 if (!uplink_veb) {
10569 dev_info(&pf->pdev->dev,
10570 "uplink seid %d not found\n", uplink_seid);
10571 return NULL;
10572 }
10573 }
10574
10575 /* get veb sw struct */
10576 veb_idx = i40e_veb_mem_alloc(pf);
10577 if (veb_idx < 0)
10578 goto err_alloc;
10579 veb = pf->veb[veb_idx];
10580 veb->flags = flags;
10581 veb->uplink_seid = uplink_seid;
10582 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10583 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10584
10585 /* create the VEB in the switch */
10586 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10587 if (ret)
10588 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010589 if (vsi_idx == pf->lan_vsi)
10590 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010591
10592 return veb;
10593
10594err_veb:
10595 i40e_veb_clear(veb);
10596err_alloc:
10597 return NULL;
10598}
10599
10600/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010601 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010602 * @pf: board private structure
10603 * @ele: element we are building info from
10604 * @num_reported: total number of elements
10605 * @printconfig: should we print the contents
10606 *
10607 * helper function to assist in extracting a few useful SEID values.
10608 **/
10609static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10610 struct i40e_aqc_switch_config_element_resp *ele,
10611 u16 num_reported, bool printconfig)
10612{
10613 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10614 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10615 u8 element_type = ele->element_type;
10616 u16 seid = le16_to_cpu(ele->seid);
10617
10618 if (printconfig)
10619 dev_info(&pf->pdev->dev,
10620 "type=%d seid=%d uplink=%d downlink=%d\n",
10621 element_type, seid, uplink_seid, downlink_seid);
10622
10623 switch (element_type) {
10624 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10625 pf->mac_seid = seid;
10626 break;
10627 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10628 /* Main VEB? */
10629 if (uplink_seid != pf->mac_seid)
10630 break;
10631 if (pf->lan_veb == I40E_NO_VEB) {
10632 int v;
10633
10634 /* find existing or else empty VEB */
10635 for (v = 0; v < I40E_MAX_VEB; v++) {
10636 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10637 pf->lan_veb = v;
10638 break;
10639 }
10640 }
10641 if (pf->lan_veb == I40E_NO_VEB) {
10642 v = i40e_veb_mem_alloc(pf);
10643 if (v < 0)
10644 break;
10645 pf->lan_veb = v;
10646 }
10647 }
10648
10649 pf->veb[pf->lan_veb]->seid = seid;
10650 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10651 pf->veb[pf->lan_veb]->pf = pf;
10652 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10653 break;
10654 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10655 if (num_reported != 1)
10656 break;
10657 /* This is immediately after a reset so we can assume this is
10658 * the PF's VSI
10659 */
10660 pf->mac_seid = uplink_seid;
10661 pf->pf_seid = downlink_seid;
10662 pf->main_vsi_seid = seid;
10663 if (printconfig)
10664 dev_info(&pf->pdev->dev,
10665 "pf_seid=%d main_vsi_seid=%d\n",
10666 pf->pf_seid, pf->main_vsi_seid);
10667 break;
10668 case I40E_SWITCH_ELEMENT_TYPE_PF:
10669 case I40E_SWITCH_ELEMENT_TYPE_VF:
10670 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10671 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10672 case I40E_SWITCH_ELEMENT_TYPE_PE:
10673 case I40E_SWITCH_ELEMENT_TYPE_PA:
10674 /* ignore these for now */
10675 break;
10676 default:
10677 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10678 element_type, seid);
10679 break;
10680 }
10681}
10682
10683/**
10684 * i40e_fetch_switch_configuration - Get switch config from firmware
10685 * @pf: board private structure
10686 * @printconfig: should we print the contents
10687 *
10688 * Get the current switch configuration from the device and
10689 * extract a few useful SEID values.
10690 **/
10691int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10692{
10693 struct i40e_aqc_get_switch_config_resp *sw_config;
10694 u16 next_seid = 0;
10695 int ret = 0;
10696 u8 *aq_buf;
10697 int i;
10698
10699 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10700 if (!aq_buf)
10701 return -ENOMEM;
10702
10703 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10704 do {
10705 u16 num_reported, num_total;
10706
10707 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10708 I40E_AQ_LARGE_BUF,
10709 &next_seid, NULL);
10710 if (ret) {
10711 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010712 "get switch config failed err %s aq_err %s\n",
10713 i40e_stat_str(&pf->hw, ret),
10714 i40e_aq_str(&pf->hw,
10715 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010716 kfree(aq_buf);
10717 return -ENOENT;
10718 }
10719
10720 num_reported = le16_to_cpu(sw_config->header.num_reported);
10721 num_total = le16_to_cpu(sw_config->header.num_total);
10722
10723 if (printconfig)
10724 dev_info(&pf->pdev->dev,
10725 "header: %d reported %d total\n",
10726 num_reported, num_total);
10727
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010728 for (i = 0; i < num_reported; i++) {
10729 struct i40e_aqc_switch_config_element_resp *ele =
10730 &sw_config->element[i];
10731
10732 i40e_setup_pf_switch_element(pf, ele, num_reported,
10733 printconfig);
10734 }
10735 } while (next_seid != 0);
10736
10737 kfree(aq_buf);
10738 return ret;
10739}
10740
10741/**
10742 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10743 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010744 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010745 *
10746 * Returns 0 on success, negative value on failure
10747 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010748static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010749{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010750 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010751 int ret;
10752
10753 /* find out what's out there already */
10754 ret = i40e_fetch_switch_configuration(pf, false);
10755 if (ret) {
10756 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010757 "couldn't fetch switch config, err %s aq_err %s\n",
10758 i40e_stat_str(&pf->hw, ret),
10759 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010760 return ret;
10761 }
10762 i40e_pf_reset_stats(pf);
10763
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010764 /* set the switch config bit for the whole device to
10765 * support limited promisc or true promisc
10766 * when user requests promisc. The default is limited
10767 * promisc.
10768 */
10769
10770 if ((pf->hw.pf_id == 0) &&
10771 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10772 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10773
10774 if (pf->hw.pf_id == 0) {
10775 u16 valid_flags;
10776
10777 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10778 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10779 NULL);
10780 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10781 dev_info(&pf->pdev->dev,
10782 "couldn't set switch config bits, err %s aq_err %s\n",
10783 i40e_stat_str(&pf->hw, ret),
10784 i40e_aq_str(&pf->hw,
10785 pf->hw.aq.asq_last_status));
10786 /* not a fatal problem, just keep going */
10787 }
10788 }
10789
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010790 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010791 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010792 struct i40e_vsi *vsi = NULL;
10793 u16 uplink_seid;
10794
10795 /* Set up the PF VSI associated with the PF's main VSI
10796 * that is already in the HW switch
10797 */
10798 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10799 uplink_seid = pf->veb[pf->lan_veb]->seid;
10800 else
10801 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010802 if (pf->lan_vsi == I40E_NO_VSI)
10803 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10804 else if (reinit)
10805 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010806 if (!vsi) {
10807 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10808 i40e_fdir_teardown(pf);
10809 return -EAGAIN;
10810 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010811 } else {
10812 /* force a reset of TC and queue layout configurations */
10813 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010815 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10816 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10817 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10818 }
10819 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10820
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010821 i40e_fdir_sb_setup(pf);
10822
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010823 /* Setup static PF queue filter control settings */
10824 ret = i40e_setup_pf_filter_control(pf);
10825 if (ret) {
10826 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10827 ret);
10828 /* Failure here should not stop continuing other steps */
10829 }
10830
10831 /* enable RSS in the HW, even for only one queue, as the stack can use
10832 * the hash
10833 */
10834 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010835 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010836
10837 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010838 i40e_link_event(pf);
10839
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010840 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010841 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10842 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010843
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010844 i40e_ptp_init(pf);
10845
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010846 return ret;
10847}
10848
10849/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010850 * i40e_determine_queue_usage - Work out queue distribution
10851 * @pf: board private structure
10852 **/
10853static void i40e_determine_queue_usage(struct i40e_pf *pf)
10854{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010855 int queues_left;
10856
10857 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010858#ifdef I40E_FCOE
10859 pf->num_fcoe_qps = 0;
10860#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010861
10862 /* Find the max queues to be put into basic use. We'll always be
10863 * using TC0, whether or not DCB is running, and TC0 will get the
10864 * big RSS set.
10865 */
10866 queues_left = pf->hw.func_caps.num_tx_qp;
10867
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010868 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010869 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010870 /* one qp for PF, no queues for anything else */
10871 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010872 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010873
10874 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010875 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010876 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010877#ifdef I40E_FCOE
10878 I40E_FLAG_FCOE_ENABLED |
10879#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010880 I40E_FLAG_FD_SB_ENABLED |
10881 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010882 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010883 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010884 I40E_FLAG_SRIOV_ENABLED |
10885 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010886 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10887 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010888 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010889 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010890 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010891 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010892 queues_left -= pf->num_lan_qps;
10893
10894 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010895 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010896#ifdef I40E_FCOE
10897 I40E_FLAG_FCOE_ENABLED |
10898#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010899 I40E_FLAG_FD_SB_ENABLED |
10900 I40E_FLAG_FD_ATR_ENABLED |
10901 I40E_FLAG_DCB_ENABLED |
10902 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010903 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010904 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010905 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010906 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010907 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10908 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010909 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10910 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010911 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10912 num_online_cpus());
10913 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10914 pf->hw.func_caps.num_tx_qp);
10915
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010916 queues_left -= pf->num_lan_qps;
10917 }
10918
Vasu Dev38e00432014-08-01 13:27:03 -070010919#ifdef I40E_FCOE
10920 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10921 if (I40E_DEFAULT_FCOE <= queues_left) {
10922 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10923 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10924 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10925 } else {
10926 pf->num_fcoe_qps = 0;
10927 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10928 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10929 }
10930
10931 queues_left -= pf->num_fcoe_qps;
10932 }
10933
10934#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010935 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10936 if (queues_left > 1) {
10937 queues_left -= 1; /* save 1 queue for FD */
10938 } else {
10939 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10940 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10941 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010942 }
10943
10944 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10945 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010946 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10947 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010948 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10949 }
10950
10951 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10952 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10953 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10954 (queues_left / pf->num_vmdq_qps));
10955 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10956 }
10957
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010958 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010959 dev_dbg(&pf->pdev->dev,
10960 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10961 pf->hw.func_caps.num_tx_qp,
10962 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010963 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10964 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10965 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010966#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010967 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010968#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010969}
10970
10971/**
10972 * i40e_setup_pf_filter_control - Setup PF static filter control
10973 * @pf: PF to be setup
10974 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010975 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010976 * settings. If PE/FCoE are enabled then it will also set the per PF
10977 * based filter sizes required for them. It also enables Flow director,
10978 * ethertype and macvlan type filter settings for the pf.
10979 *
10980 * Returns 0 on success, negative on failure
10981 **/
10982static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10983{
10984 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10985
10986 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10987
10988 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010989 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010990 settings->enable_fdir = true;
10991
10992 /* Ethtype and MACVLAN filters enabled for PF */
10993 settings->enable_ethtype = true;
10994 settings->enable_macvlan = true;
10995
10996 if (i40e_set_filter_control(&pf->hw, settings))
10997 return -ENOENT;
10998
10999 return 0;
11000}
11001
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011002#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040011003#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011004static void i40e_print_features(struct i40e_pf *pf)
11005{
11006 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080011007 char *buf;
11008 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011009
Joe Perches3b195842015-12-03 04:20:57 -080011010 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11011 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011012 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011013
Joe Perches3b195842015-12-03 04:20:57 -080011014 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011015#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011016 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011017#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011018 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011019 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011020 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011021 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011022 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011023 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011024 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011025 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011026 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11027 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011028 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011029 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011030 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011031 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011032 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011033 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011034 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070011035#ifdef I40E_FCOE
11036 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011037 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070011038#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040011039 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011040 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011041 else
Joe Perches3b195842015-12-03 04:20:57 -080011042 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011043
Joe Perches3b195842015-12-03 04:20:57 -080011044 dev_info(&pf->pdev->dev, "%s\n", buf);
11045 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011046 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011047}
11048
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011049/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011050 * i40e_get_platform_mac_addr - get platform-specific MAC address
11051 *
11052 * @pdev: PCI device information struct
11053 * @pf: board private structure
11054 *
11055 * Look up the MAC address in Open Firmware on systems that support it,
11056 * and use IDPROM on SPARC if no OF address is found. On return, the
11057 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
11058 * has been selected.
11059 **/
11060static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11061{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011062 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080011063 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011064 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011065}
11066
11067/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011068 * i40e_probe - Device initialization routine
11069 * @pdev: PCI device information struct
11070 * @ent: entry in i40e_pci_tbl
11071 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011072 * i40e_probe initializes a PF identified by a pci_dev structure.
11073 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011074 * and a hardware reset occur.
11075 *
11076 * Returns 0 on success, negative on failure
11077 **/
11078static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11079{
Catherine Sullivane8278452015-02-06 08:52:08 +000011080 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011081 struct i40e_pf *pf;
11082 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011083 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011084 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011085 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011086 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011087 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011088 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011089 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011090
11091 err = pci_enable_device_mem(pdev);
11092 if (err)
11093 return err;
11094
11095 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011096 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011097 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011098 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11099 if (err) {
11100 dev_err(&pdev->dev,
11101 "DMA configuration failed: 0x%x\n", err);
11102 goto err_dma;
11103 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011104 }
11105
11106 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011107 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011108 if (err) {
11109 dev_info(&pdev->dev,
11110 "pci_request_selected_regions failed %d\n", err);
11111 goto err_pci_reg;
11112 }
11113
11114 pci_enable_pcie_error_reporting(pdev);
11115 pci_set_master(pdev);
11116
11117 /* Now that we have a PCI connection, we need to do the
11118 * low level device setup. This is primarily setting up
11119 * the Admin Queue structures and then querying for the
11120 * device's current profile information.
11121 */
11122 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11123 if (!pf) {
11124 err = -ENOMEM;
11125 goto err_pf_alloc;
11126 }
11127 pf->next_vsi = 0;
11128 pf->pdev = pdev;
11129 set_bit(__I40E_DOWN, &pf->state);
11130
11131 hw = &pf->hw;
11132 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011133
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011134 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11135 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011136
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011137 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011138 if (!hw->hw_addr) {
11139 err = -EIO;
11140 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11141 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011142 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011143 goto err_ioremap;
11144 }
11145 hw->vendor_id = pdev->vendor;
11146 hw->device_id = pdev->device;
11147 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11148 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11149 hw->subsystem_device_id = pdev->subsystem_device;
11150 hw->bus.device = PCI_SLOT(pdev->devfn);
11151 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011152 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011153 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011154
Jacob Keller0e588de2017-02-06 14:38:50 -080011155 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
11156 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
11157
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011158 /* set up the locks for the AQ, do this only once in probe
11159 * and destroy them only once in remove
11160 */
11161 mutex_init(&hw->aq.asq_mutex);
11162 mutex_init(&hw->aq.arq_mutex);
11163
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011164 pf->msg_enable = netif_msg_init(debug,
11165 NETIF_MSG_DRV |
11166 NETIF_MSG_PROBE |
11167 NETIF_MSG_LINK);
11168 if (debug < -1)
11169 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011170
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011171 /* do a special CORER for clearing PXE mode once at init */
11172 if (hw->revision_id == 0 &&
11173 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11174 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11175 i40e_flush(hw);
11176 msleep(200);
11177 pf->corer_count++;
11178
11179 i40e_clear_pxe_mode(hw);
11180 }
11181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011182 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011183 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011184 err = i40e_pf_reset(hw);
11185 if (err) {
11186 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11187 goto err_pf_reset;
11188 }
11189 pf->pfr_count++;
11190
11191 hw->aq.num_arq_entries = I40E_AQ_LEN;
11192 hw->aq.num_asq_entries = I40E_AQ_LEN;
11193 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11194 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11195 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011196
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011197 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011198 "%s-%s:misc",
11199 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011200
11201 err = i40e_init_shared_code(hw);
11202 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011203 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11204 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011205 goto err_pf_reset;
11206 }
11207
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011208 /* set up a default setting for link flow control */
11209 pf->hw.fc.requested_mode = I40E_FC_NONE;
11210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011211 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011212 if (err) {
11213 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11214 dev_info(&pdev->dev,
11215 "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");
11216 else
11217 dev_info(&pdev->dev,
11218 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11219
11220 goto err_pf_reset;
11221 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011222
Shannon Nelson6dec1012015-09-28 14:12:30 -040011223 /* provide nvm, fw, api versions */
11224 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11225 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11226 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11227 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011228
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011229 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11230 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011231 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011232 "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");
11233 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11234 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011235 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011236 "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 +000011237
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011238 i40e_verify_eeprom(pf);
11239
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011240 /* Rev 0 hardware was never productized */
11241 if (hw->revision_id < 1)
11242 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");
11243
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011244 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011245 err = i40e_get_capabilities(pf);
11246 if (err)
11247 goto err_adminq_setup;
11248
11249 err = i40e_sw_init(pf);
11250 if (err) {
11251 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11252 goto err_sw_init;
11253 }
11254
11255 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11256 hw->func_caps.num_rx_qp,
11257 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11258 if (err) {
11259 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11260 goto err_init_lan_hmc;
11261 }
11262
11263 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11264 if (err) {
11265 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11266 err = -ENOENT;
11267 goto err_configure_lan_hmc;
11268 }
11269
Neerav Parikhb686ece2014-12-14 01:55:11 +000011270 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11271 * Ignore error return codes because if it was already disabled via
11272 * hardware settings this will fail
11273 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011274 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011275 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11276 i40e_aq_stop_lldp(hw, true, NULL);
11277 }
11278
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011279 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011280 /* allow a platform config to override the HW addr */
11281 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011282 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011283 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11284 err = -EIO;
11285 goto err_mac_addr;
11286 }
11287 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011288 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011289 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11290 if (is_valid_ether_addr(hw->mac.port_addr))
11291 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011292#ifdef I40E_FCOE
11293 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11294 if (err)
11295 dev_info(&pdev->dev,
11296 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11297 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11298 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11299 hw->mac.san_addr);
11300 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11301 }
11302 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11303#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011304
11305 pci_set_drvdata(pdev, pf);
11306 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011307#ifdef CONFIG_I40E_DCB
11308 err = i40e_init_pf_dcb(pf);
11309 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011310 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011311 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011312 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011313 }
11314#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011315
11316 /* set up periodic task facility */
11317 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11318 pf->service_timer_period = HZ;
11319
11320 INIT_WORK(&pf->service_task, i40e_service_task);
11321 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11322 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011323
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011324 /* NVM bit on means WoL disabled for the port */
11325 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011326 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011327 pf->wol_en = false;
11328 else
11329 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011330 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11331
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011332 /* set up the main switch operations */
11333 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011334 err = i40e_init_interrupt_scheme(pf);
11335 if (err)
11336 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011337
Mitch Williams505682c2014-05-20 08:01:37 +000011338 /* The number of VSIs reported by the FW is the minimum guaranteed
11339 * to us; HW supports far more and we share the remaining pool with
11340 * the other PFs. We allocate space for more than the guarantee with
11341 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011342 */
Mitch Williams505682c2014-05-20 08:01:37 +000011343 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11344 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11345 else
11346 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11347
11348 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011349 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11350 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011351 if (!pf->vsi) {
11352 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011353 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011354 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011355
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011356#ifdef CONFIG_PCI_IOV
11357 /* prep for VF support */
11358 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11359 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11360 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11361 if (pci_num_vf(pdev))
11362 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11363 }
11364#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011365 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011366 if (err) {
11367 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11368 goto err_vsis;
11369 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011370
11371 /* Make sure flow control is set according to current settings */
11372 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11373 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11374 dev_dbg(&pf->pdev->dev,
11375 "Set fc with err %s aq_err %s on get_phy_cap\n",
11376 i40e_stat_str(hw, err),
11377 i40e_aq_str(hw, hw->aq.asq_last_status));
11378 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11379 dev_dbg(&pf->pdev->dev,
11380 "Set fc with err %s aq_err %s on set_phy_config\n",
11381 i40e_stat_str(hw, err),
11382 i40e_aq_str(hw, hw->aq.asq_last_status));
11383 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11384 dev_dbg(&pf->pdev->dev,
11385 "Set fc with err %s aq_err %s on get_link_info\n",
11386 i40e_stat_str(hw, err),
11387 i40e_aq_str(hw, hw->aq.asq_last_status));
11388
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011389 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011390 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011391 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11392 i40e_vsi_open(pf->vsi[i]);
11393 break;
11394 }
11395 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011396
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011397 /* The driver only wants link up/down and module qualification
11398 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011399 */
11400 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011401 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011402 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011403 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011404 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011405 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11406 i40e_stat_str(&pf->hw, err),
11407 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011408
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011409 /* Reconfigure hardware for allowing smaller MSS in the case
11410 * of TSO, so that we avoid the MDD being fired and causing
11411 * a reset in the case of small MSS+TSO.
11412 */
11413 val = rd32(hw, I40E_REG_MSS);
11414 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11415 val &= ~I40E_REG_MSS_MIN_MASK;
11416 val |= I40E_64BYTE_MSS;
11417 wr32(hw, I40E_REG_MSS, val);
11418 }
11419
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011420 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011421 msleep(75);
11422 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11423 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011424 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11425 i40e_stat_str(&pf->hw, err),
11426 i40e_aq_str(&pf->hw,
11427 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011428 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011429 /* The main driver is (mostly) up and happy. We need to set this state
11430 * before setting up the misc vector or we get a race and the vector
11431 * ends up disabled forever.
11432 */
11433 clear_bit(__I40E_DOWN, &pf->state);
11434
11435 /* In case of MSIX we are going to setup the misc vector right here
11436 * to handle admin queue events etc. In case of legacy and MSI
11437 * the misc functionality and queue processing is combined in
11438 * the same vector and that gets setup at open.
11439 */
11440 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11441 err = i40e_setup_misc_vector(pf);
11442 if (err) {
11443 dev_info(&pdev->dev,
11444 "setup of misc vector failed: %d\n", err);
11445 goto err_vsis;
11446 }
11447 }
11448
Greg Rosedf805f62014-04-04 04:43:16 +000011449#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011450 /* prep for VF support */
11451 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011452 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11453 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011454 /* disable link interrupts for VFs */
11455 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11456 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11457 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11458 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011459
11460 if (pci_num_vf(pdev)) {
11461 dev_info(&pdev->dev,
11462 "Active VFs found, allocating resources.\n");
11463 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11464 if (err)
11465 dev_info(&pdev->dev,
11466 "Error %d allocating resources for existing VFs\n",
11467 err);
11468 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011469 }
Greg Rosedf805f62014-04-04 04:43:16 +000011470#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011471
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011472 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11473 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11474 pf->num_iwarp_msix,
11475 I40E_IWARP_IRQ_PILE_ID);
11476 if (pf->iwarp_base_vector < 0) {
11477 dev_info(&pdev->dev,
11478 "failed to get tracking for %d vectors for IWARP err=%d\n",
11479 pf->num_iwarp_msix, pf->iwarp_base_vector);
11480 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11481 }
11482 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011483
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011484 i40e_dbg_pf_init(pf);
11485
11486 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011487 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011488
11489 /* since everything's happy, start the service_task timer */
11490 mod_timer(&pf->service_timer,
11491 round_jiffies(jiffies + pf->service_timer_period));
11492
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011493 /* add this PF to client device list and launch a client service task */
11494 err = i40e_lan_add_device(pf);
11495 if (err)
11496 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11497 err);
11498
Vasu Dev38e00432014-08-01 13:27:03 -070011499#ifdef I40E_FCOE
11500 /* create FCoE interface */
11501 i40e_fcoe_vsi_setup(pf);
11502
11503#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011504#define PCI_SPEED_SIZE 8
11505#define PCI_WIDTH_SIZE 8
11506 /* Devices on the IOSF bus do not have this information
11507 * and will report PCI Gen 1 x 1 by default so don't bother
11508 * checking them.
11509 */
11510 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11511 char speed[PCI_SPEED_SIZE] = "Unknown";
11512 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011513
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011514 /* Get the negotiated link width and speed from PCI config
11515 * space
11516 */
11517 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11518 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011519
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011520 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011521
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011522 switch (hw->bus.speed) {
11523 case i40e_bus_speed_8000:
11524 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11525 case i40e_bus_speed_5000:
11526 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11527 case i40e_bus_speed_2500:
11528 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11529 default:
11530 break;
11531 }
11532 switch (hw->bus.width) {
11533 case i40e_bus_width_pcie_x8:
11534 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11535 case i40e_bus_width_pcie_x4:
11536 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11537 case i40e_bus_width_pcie_x2:
11538 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11539 case i40e_bus_width_pcie_x1:
11540 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11541 default:
11542 break;
11543 }
11544
11545 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11546 speed, width);
11547
11548 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11549 hw->bus.speed < i40e_bus_speed_8000) {
11550 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11551 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11552 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011553 }
11554
Catherine Sullivane8278452015-02-06 08:52:08 +000011555 /* get the requested speeds from the fw */
11556 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11557 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011558 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11559 i40e_stat_str(&pf->hw, err),
11560 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011561 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11562
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011563 /* get the supported phy types from the fw */
11564 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11565 if (err)
11566 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11567 i40e_stat_str(&pf->hw, err),
11568 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011569
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011570 /* Add a filter to drop all Flow control frames from any VSI from being
11571 * transmitted. By doing so we stop a malicious VF from sending out
11572 * PAUSE or PFC frames and potentially controlling traffic for other
11573 * PF/VF VSIs.
11574 * The FW can still send Flow control frames if enabled.
11575 */
11576 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11577 pf->main_vsi_seid);
11578
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011579 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011580 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11581 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011582 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11583 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011584 /* print a string summarizing features */
11585 i40e_print_features(pf);
11586
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011587 return 0;
11588
11589 /* Unwind what we've done if something failed in the setup */
11590err_vsis:
11591 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011592 i40e_clear_interrupt_scheme(pf);
11593 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011594err_switch_setup:
11595 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011596 del_timer_sync(&pf->service_timer);
11597err_mac_addr:
11598err_configure_lan_hmc:
11599 (void)i40e_shutdown_lan_hmc(hw);
11600err_init_lan_hmc:
11601 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011602err_sw_init:
11603err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011604err_pf_reset:
11605 iounmap(hw->hw_addr);
11606err_ioremap:
11607 kfree(pf);
11608err_pf_alloc:
11609 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011610 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011611err_pci_reg:
11612err_dma:
11613 pci_disable_device(pdev);
11614 return err;
11615}
11616
11617/**
11618 * i40e_remove - Device removal routine
11619 * @pdev: PCI device information struct
11620 *
11621 * i40e_remove is called by the PCI subsystem to alert the driver
11622 * that is should release a PCI device. This could be caused by a
11623 * Hot-Plug event, or because the driver is going to be removed from
11624 * memory.
11625 **/
11626static void i40e_remove(struct pci_dev *pdev)
11627{
11628 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011629 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011630 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011631 int i;
11632
11633 i40e_dbg_pf_exit(pf);
11634
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011635 i40e_ptp_stop(pf);
11636
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011637 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011638 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11639 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011641 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011642 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011643 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011644 if (pf->service_timer.data)
11645 del_timer_sync(&pf->service_timer);
11646 if (pf->service_task.func)
11647 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011648
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011649 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11650 i40e_free_vfs(pf);
11651 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11652 }
11653
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011654 i40e_fdir_teardown(pf);
11655
11656 /* If there is a switch structure or any orphans, remove them.
11657 * This will leave only the PF's VSI remaining.
11658 */
11659 for (i = 0; i < I40E_MAX_VEB; i++) {
11660 if (!pf->veb[i])
11661 continue;
11662
11663 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11664 pf->veb[i]->uplink_seid == 0)
11665 i40e_switch_branch_release(pf->veb[i]);
11666 }
11667
11668 /* Now we can shutdown the PF's VSI, just before we kill
11669 * adminq and hmc.
11670 */
11671 if (pf->vsi[pf->lan_vsi])
11672 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11673
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011674 /* remove attached clients */
11675 ret_code = i40e_lan_del_device(pf);
11676 if (ret_code) {
11677 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11678 ret_code);
11679 }
11680
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011681 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011682 if (hw->hmc.hmc_obj) {
11683 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011684 if (ret_code)
11685 dev_warn(&pdev->dev,
11686 "Failed to destroy the HMC resources: %d\n",
11687 ret_code);
11688 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011689
11690 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011691 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011692
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011693 /* destroy the locks only once, here */
11694 mutex_destroy(&hw->aq.arq_mutex);
11695 mutex_destroy(&hw->aq.asq_mutex);
11696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011697 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11698 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011699 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011700 if (pf->vsi[i]) {
11701 i40e_vsi_clear_rings(pf->vsi[i]);
11702 i40e_vsi_clear(pf->vsi[i]);
11703 pf->vsi[i] = NULL;
11704 }
11705 }
11706
11707 for (i = 0; i < I40E_MAX_VEB; i++) {
11708 kfree(pf->veb[i]);
11709 pf->veb[i] = NULL;
11710 }
11711
11712 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011713 kfree(pf->vsi);
11714
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011715 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011716 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011717 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011718
11719 pci_disable_pcie_error_reporting(pdev);
11720 pci_disable_device(pdev);
11721}
11722
11723/**
11724 * i40e_pci_error_detected - warning that something funky happened in PCI land
11725 * @pdev: PCI device information struct
11726 *
11727 * Called to warn that something happened and the error handling steps
11728 * are in progress. Allows the driver to quiesce things, be ready for
11729 * remediation.
11730 **/
11731static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11732 enum pci_channel_state error)
11733{
11734 struct i40e_pf *pf = pci_get_drvdata(pdev);
11735
11736 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11737
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011738 if (!pf) {
11739 dev_info(&pdev->dev,
11740 "Cannot recover - error happened during device probe\n");
11741 return PCI_ERS_RESULT_DISCONNECT;
11742 }
11743
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011744 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011745 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11746 rtnl_lock();
11747 i40e_prep_for_reset(pf);
11748 rtnl_unlock();
11749 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011750
11751 /* Request a slot reset */
11752 return PCI_ERS_RESULT_NEED_RESET;
11753}
11754
11755/**
11756 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11757 * @pdev: PCI device information struct
11758 *
11759 * Called to find if the driver can work with the device now that
11760 * the pci slot has been reset. If a basic connection seems good
11761 * (registers are readable and have sane content) then return a
11762 * happy little PCI_ERS_RESULT_xxx.
11763 **/
11764static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11765{
11766 struct i40e_pf *pf = pci_get_drvdata(pdev);
11767 pci_ers_result_t result;
11768 int err;
11769 u32 reg;
11770
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011771 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011772 if (pci_enable_device_mem(pdev)) {
11773 dev_info(&pdev->dev,
11774 "Cannot re-enable PCI device after reset.\n");
11775 result = PCI_ERS_RESULT_DISCONNECT;
11776 } else {
11777 pci_set_master(pdev);
11778 pci_restore_state(pdev);
11779 pci_save_state(pdev);
11780 pci_wake_from_d3(pdev, false);
11781
11782 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11783 if (reg == 0)
11784 result = PCI_ERS_RESULT_RECOVERED;
11785 else
11786 result = PCI_ERS_RESULT_DISCONNECT;
11787 }
11788
11789 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11790 if (err) {
11791 dev_info(&pdev->dev,
11792 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11793 err);
11794 /* non-fatal, continue */
11795 }
11796
11797 return result;
11798}
11799
11800/**
11801 * i40e_pci_error_resume - restart operations after PCI error recovery
11802 * @pdev: PCI device information struct
11803 *
11804 * Called to allow the driver to bring things back up after PCI error
11805 * and/or reset recovery has finished.
11806 **/
11807static void i40e_pci_error_resume(struct pci_dev *pdev)
11808{
11809 struct i40e_pf *pf = pci_get_drvdata(pdev);
11810
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011811 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011812 if (test_bit(__I40E_SUSPENDED, &pf->state))
11813 return;
11814
11815 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011816 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011817 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011818}
11819
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011820/**
Joshua Hay1d680052016-12-12 15:44:08 -080011821 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11822 * using the mac_address_write admin q function
11823 * @pf: pointer to i40e_pf struct
11824 **/
11825static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11826{
11827 struct i40e_hw *hw = &pf->hw;
11828 i40e_status ret;
11829 u8 mac_addr[6];
11830 u16 flags = 0;
11831
11832 /* Get current MAC address in case it's an LAA */
11833 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11834 ether_addr_copy(mac_addr,
11835 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11836 } else {
11837 dev_err(&pf->pdev->dev,
11838 "Failed to retrieve MAC address; using default\n");
11839 ether_addr_copy(mac_addr, hw->mac.addr);
11840 }
11841
11842 /* The FW expects the mac address write cmd to first be called with
11843 * one of these flags before calling it again with the multicast
11844 * enable flags.
11845 */
11846 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11847
11848 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11849 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11850
11851 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11852 if (ret) {
11853 dev_err(&pf->pdev->dev,
11854 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11855 return;
11856 }
11857
11858 flags = I40E_AQC_MC_MAG_EN
11859 | I40E_AQC_WOL_PRESERVE_ON_PFR
11860 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11861 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11862 if (ret)
11863 dev_err(&pf->pdev->dev,
11864 "Failed to enable Multicast Magic Packet wake up\n");
11865}
11866
11867/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011868 * i40e_shutdown - PCI callback for shutting down
11869 * @pdev: PCI device information struct
11870 **/
11871static void i40e_shutdown(struct pci_dev *pdev)
11872{
11873 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011874 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011875
11876 set_bit(__I40E_SUSPENDED, &pf->state);
11877 set_bit(__I40E_DOWN, &pf->state);
11878 rtnl_lock();
11879 i40e_prep_for_reset(pf);
11880 rtnl_unlock();
11881
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011882 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11883 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11884
Catherine Sullivan02b42492015-07-10 19:35:59 -040011885 del_timer_sync(&pf->service_timer);
11886 cancel_work_sync(&pf->service_task);
11887 i40e_fdir_teardown(pf);
11888
Joshua Hay1d680052016-12-12 15:44:08 -080011889 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11890 i40e_enable_mc_magic_wake(pf);
11891
Catherine Sullivan02b42492015-07-10 19:35:59 -040011892 rtnl_lock();
11893 i40e_prep_for_reset(pf);
11894 rtnl_unlock();
11895
11896 wr32(hw, I40E_PFPM_APM,
11897 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11898 wr32(hw, I40E_PFPM_WUFC,
11899 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11900
Shannon Nelsone1477582015-02-21 06:44:33 +000011901 i40e_clear_interrupt_scheme(pf);
11902
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011903 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011904 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011905 pci_set_power_state(pdev, PCI_D3hot);
11906 }
11907}
11908
11909#ifdef CONFIG_PM
11910/**
11911 * i40e_suspend - PCI callback for moving to D3
11912 * @pdev: PCI device information struct
11913 **/
11914static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11915{
11916 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011917 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011918 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011919
11920 set_bit(__I40E_SUSPENDED, &pf->state);
11921 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011922
Joshua Hay1d680052016-12-12 15:44:08 -080011923 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11924 i40e_enable_mc_magic_wake(pf);
11925
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011926 rtnl_lock();
11927 i40e_prep_for_reset(pf);
11928 rtnl_unlock();
11929
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011930 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11931 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11932
Greg Roseb33d3b72016-05-16 10:26:37 -070011933 i40e_stop_misc_vector(pf);
11934
Greg Rose059ff692016-05-16 10:26:38 -070011935 retval = pci_save_state(pdev);
11936 if (retval)
11937 return retval;
11938
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011939 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011940 pci_set_power_state(pdev, PCI_D3hot);
11941
Greg Rose059ff692016-05-16 10:26:38 -070011942 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011943}
11944
11945/**
11946 * i40e_resume - PCI callback for waking up from D3
11947 * @pdev: PCI device information struct
11948 **/
11949static int i40e_resume(struct pci_dev *pdev)
11950{
11951 struct i40e_pf *pf = pci_get_drvdata(pdev);
11952 u32 err;
11953
11954 pci_set_power_state(pdev, PCI_D0);
11955 pci_restore_state(pdev);
11956 /* pci_restore_state() clears dev->state_saves, so
11957 * call pci_save_state() again to restore it.
11958 */
11959 pci_save_state(pdev);
11960
11961 err = pci_enable_device_mem(pdev);
11962 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011963 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011964 return err;
11965 }
11966 pci_set_master(pdev);
11967
11968 /* no wakeup events while running */
11969 pci_wake_from_d3(pdev, false);
11970
11971 /* handling the reset will rebuild the device state */
11972 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11973 clear_bit(__I40E_DOWN, &pf->state);
11974 rtnl_lock();
11975 i40e_reset_and_rebuild(pf, false);
11976 rtnl_unlock();
11977 }
11978
11979 return 0;
11980}
11981
11982#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011983static const struct pci_error_handlers i40e_err_handler = {
11984 .error_detected = i40e_pci_error_detected,
11985 .slot_reset = i40e_pci_error_slot_reset,
11986 .resume = i40e_pci_error_resume,
11987};
11988
11989static struct pci_driver i40e_driver = {
11990 .name = i40e_driver_name,
11991 .id_table = i40e_pci_tbl,
11992 .probe = i40e_probe,
11993 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011994#ifdef CONFIG_PM
11995 .suspend = i40e_suspend,
11996 .resume = i40e_resume,
11997#endif
11998 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011999 .err_handler = &i40e_err_handler,
12000 .sriov_configure = i40e_pci_sriov_configure,
12001};
12002
12003/**
12004 * i40e_init_module - Driver registration routine
12005 *
12006 * i40e_init_module is the first routine called when the driver is
12007 * loaded. All it does is register with the PCI subsystem.
12008 **/
12009static int __init i40e_init_module(void)
12010{
12011 pr_info("%s: %s - version %s\n", i40e_driver_name,
12012 i40e_driver_string, i40e_driver_version_str);
12013 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012014
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012015 /* we will see if single thread per module is enough for now,
12016 * it can't be any worse than using the system workqueue which
12017 * was already single threaded
12018 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070012019 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
12020 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012021 if (!i40e_wq) {
12022 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12023 return -ENOMEM;
12024 }
12025
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012026 i40e_dbg_init();
12027 return pci_register_driver(&i40e_driver);
12028}
12029module_init(i40e_init_module);
12030
12031/**
12032 * i40e_exit_module - Driver exit cleanup routine
12033 *
12034 * i40e_exit_module is called just before the driver is removed
12035 * from memory.
12036 **/
12037static void __exit i40e_exit_module(void)
12038{
12039 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012040 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012041 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012042}
12043module_exit(i40e_exit_module);