blob: 3fbecaa10286ad1c5df30e5774c1120356987948 [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 Keller671889e2016-12-02 12:33:00 -08001886 while (next) {
1887 next = hlist_entry(next->hlist.next,
1888 typeof(struct i40e_new_mac_filter),
1889 hlist);
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001890
1891 /* keep going if we found a broadcast filter */
Jacob Keller671889e2016-12-02 12:33:00 -08001892 if (next && is_broadcast_ether_addr(next->f->macaddr))
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001893 continue;
1894
1895 break;
1896 }
1897
Jacob Keller671889e2016-12-02 12:33:00 -08001898 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001899}
1900
1901/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001902 * i40e_update_filter_state - Update filter state based on return data
1903 * from firmware
1904 * @count: Number of filters added
1905 * @add_list: return data from fw
1906 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001907 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001908 * MAC filter entries from list were slated to be added to device. Returns
1909 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001910 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001911static int
1912i40e_update_filter_state(int count,
1913 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001914 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001915{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001916 int retval = 0;
1917 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001918
Jacob Kellerac9e2392016-11-11 12:39:27 -08001919 for (i = 0; i < count; i++) {
1920 /* Always check status of each filter. We don't need to check
1921 * the firmware return status because we pre-set the filter
1922 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1923 * request to the adminq. Thus, if it no longer matches then
1924 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001925 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001926 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001927 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001928 } else {
1929 add_head->state = I40E_FILTER_ACTIVE;
1930 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001931 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001932
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001933 add_head = i40e_next_filter(add_head);
1934 if (!add_head)
1935 break;
Kiran Patil21659032015-09-30 14:09:03 -04001936 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001937
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001938 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001939}
1940
1941/**
Jacob Keller00936312016-10-05 09:30:41 -07001942 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1943 * @vsi: ptr to the VSI
1944 * @vsi_name: name to display in messages
1945 * @list: the list of filters to send to firmware
1946 * @num_del: the number of filters to delete
1947 * @retval: Set to -EIO on failure to delete
1948 *
1949 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1950 * *retval instead of a return value so that success does not force ret_val to
1951 * be set to 0. This ensures that a sequence of calls to this function
1952 * preserve the previous value of *retval on successful delete.
1953 */
1954static
1955void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1956 struct i40e_aqc_remove_macvlan_element_data *list,
1957 int num_del, int *retval)
1958{
1959 struct i40e_hw *hw = &vsi->back->hw;
1960 i40e_status aq_ret;
1961 int aq_err;
1962
1963 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1964 aq_err = hw->aq.asq_last_status;
1965
1966 /* Explicitly ignore and do not report when firmware returns ENOENT */
1967 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1968 *retval = -EIO;
1969 dev_info(&vsi->back->pdev->dev,
1970 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1971 vsi_name, i40e_stat_str(hw, aq_ret),
1972 i40e_aq_str(hw, aq_err));
1973 }
1974}
1975
1976/**
1977 * i40e_aqc_add_filters - Request firmware to add a set of filters
1978 * @vsi: ptr to the VSI
1979 * @vsi_name: name to display in messages
1980 * @list: the list of filters to send to firmware
1981 * @add_head: Position in the add hlist
1982 * @num_add: the number of filters to add
1983 * @promisc_change: set to true on exit if promiscuous mode was forced on
1984 *
1985 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1986 * promisc_changed to true if the firmware has run out of space for more
1987 * filters.
1988 */
1989static
1990void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1991 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001992 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001993 int num_add, bool *promisc_changed)
1994{
1995 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001996 int aq_err, fcnt;
1997
Jacob Kellerac9e2392016-11-11 12:39:27 -08001998 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001999 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002000 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07002001
2002 if (fcnt != num_add) {
2003 *promisc_changed = true;
2004 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07002005 dev_warn(&vsi->back->pdev->dev,
2006 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2007 i40e_aq_str(hw, aq_err),
2008 vsi_name);
2009 }
2010}
2011
2012/**
Jacob Keller435c0842016-11-08 13:05:10 -08002013 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2014 * @vsi: pointer to the VSI
2015 * @f: filter data
2016 *
2017 * This function sets or clears the promiscuous broadcast flags for VLAN
2018 * filters in order to properly receive broadcast frames. Assumes that only
2019 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08002020 *
2021 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08002022 **/
Jacob Keller671889e2016-12-02 12:33:00 -08002023static i40e_status
2024i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2025 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08002026{
2027 bool enable = f->state == I40E_FILTER_NEW;
2028 struct i40e_hw *hw = &vsi->back->hw;
2029 i40e_status aq_ret;
2030
2031 if (f->vlan == I40E_VLAN_ANY) {
2032 aq_ret = i40e_aq_set_vsi_broadcast(hw,
2033 vsi->seid,
2034 enable,
2035 NULL);
2036 } else {
2037 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2038 vsi->seid,
2039 enable,
2040 f->vlan,
2041 NULL);
2042 }
2043
Jacob Keller671889e2016-12-02 12:33:00 -08002044 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08002045 dev_warn(&vsi->back->pdev->dev,
2046 "Error %s setting broadcast promiscuous mode on %s\n",
2047 i40e_aq_str(hw, hw->aq.asq_last_status),
2048 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08002049
2050 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08002051}
2052
2053/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002054 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2055 * @vsi: ptr to the VSI
2056 *
2057 * Push any outstanding VSI filter changes through the AdminQ.
2058 *
2059 * Returns 0 or error value
2060 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002061int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062{
Jacob Keller278e7d02016-10-05 09:30:37 -07002063 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08002064 struct i40e_mac_filter *f;
2065 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002066 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08002067 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002068 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002069 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07002070 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002071 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08002072 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002073 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002074 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075 struct i40e_pf *pf;
2076 int num_add = 0;
2077 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07002078 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002079 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002080 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07002081 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002082
2083 /* empty array typed pointers, kcalloc later */
2084 struct i40e_aqc_add_macvlan_element_data *add_list;
2085 struct i40e_aqc_remove_macvlan_element_data *del_list;
2086
2087 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2088 usleep_range(1000, 2000);
2089 pf = vsi->back;
2090
2091 if (vsi->netdev) {
2092 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2093 vsi->current_netdev_flags = vsi->netdev->flags;
2094 }
2095
Jacob Keller278e7d02016-10-05 09:30:37 -07002096 INIT_HLIST_HEAD(&tmp_add_list);
2097 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002098
Shannon Nelson2d1de822016-05-16 10:26:44 -07002099 if (vsi->type == I40E_VSI_SRIOV)
2100 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2101 else if (vsi->type != I40E_VSI_MAIN)
2102 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2103
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002104 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2105 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2106
Jacob Keller278e7d02016-10-05 09:30:37 -07002107 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002108 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002109 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002110 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002111 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002112 hash_del(&f->hlist);
2113 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002114
2115 /* Avoid counting removed filters */
2116 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002117 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002118 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002119 /* Create a temporary i40e_new_mac_filter */
2120 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2121 if (!new)
2122 goto err_no_memory_locked;
2123
2124 /* Store pointer to the real filter */
2125 new->f = f;
2126 new->state = f->state;
2127
2128 /* Add it to the hash list */
2129 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002130 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002131
Jacob Keller489a3262016-11-11 12:39:31 -08002132 /* Count the number of active (current and new) VLAN
2133 * filters we have now. Does not count filters which
2134 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002135 */
2136 if (f->vlan > 0)
2137 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002138 }
2139
Jacob Keller489a3262016-11-11 12:39:31 -08002140 retval = i40e_correct_mac_vlan_filters(vsi,
2141 &tmp_add_list,
2142 &tmp_del_list,
2143 vlan_filters);
2144 if (retval)
2145 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002146
Jacob Keller278e7d02016-10-05 09:30:37 -07002147 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002148 }
2149
2150 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002151 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002152 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002153 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002154 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002155 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002156 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002157 if (!del_list)
2158 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002159
Jacob Keller278e7d02016-10-05 09:30:37 -07002160 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 cmd_flags = 0;
2162
Jacob Keller435c0842016-11-08 13:05:10 -08002163 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002164 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002165 */
2166 if (is_broadcast_ether_addr(f->macaddr)) {
2167 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2168
2169 hlist_del(&f->hlist);
2170 kfree(f);
2171 continue;
2172 }
2173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002174 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002175 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002176 if (f->vlan == I40E_VLAN_ANY) {
2177 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002178 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002179 } else {
2180 del_list[num_del].vlan_tag =
2181 cpu_to_le16((u16)(f->vlan));
2182 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002183
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002184 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2185 del_list[num_del].flags = cmd_flags;
2186 num_del++;
2187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002188 /* flush a full buffer */
2189 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002190 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2191 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002192 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002193 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002194 }
Kiran Patil21659032015-09-30 14:09:03 -04002195 /* Release memory for MAC filter entries which were
2196 * synced up with HW.
2197 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002198 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002199 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002200 }
Kiran Patil21659032015-09-30 14:09:03 -04002201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002203 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2204 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 }
2206
2207 kfree(del_list);
2208 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002209 }
2210
Jacob Keller278e7d02016-10-05 09:30:37 -07002211 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002212 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002213 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002214 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002215 list_size = filter_list_len *
2216 sizeof(struct i40e_aqc_add_macvlan_element_data);
2217 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002218 if (!add_list)
2219 goto err_no_memory;
2220
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002221 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002222 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002223 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2224 &vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002225 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002226 continue;
2227 }
Jacob Keller435c0842016-11-08 13:05:10 -08002228
2229 /* handle broadcast filters by updating the broadcast
2230 * promiscuous flag instead of adding a MAC filter.
2231 */
Jacob Keller671889e2016-12-02 12:33:00 -08002232 if (is_broadcast_ether_addr(new->f->macaddr)) {
2233 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2234 new->f))
2235 new->state = I40E_FILTER_FAILED;
2236 else
2237 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002238 continue;
2239 }
2240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002242 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002243 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002244 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002245 ether_addr_copy(add_list[num_add].mac_addr,
2246 new->f->macaddr);
2247 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002248 add_list[num_add].vlan_tag = 0;
2249 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2250 } else {
2251 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002252 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002253 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002254 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002255 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002256 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002258 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2259 num_add++;
2260
2261 /* flush a full buffer */
2262 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002263 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2264 add_head, num_add,
2265 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002266 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002267 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 }
2269 }
2270 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002271 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2272 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002273 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002274 /* Now move all of the filters from the temp add list back to
2275 * the VSI's list.
2276 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002277 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002278 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2279 /* Only update the state if we're still NEW */
2280 if (new->f->state == I40E_FILTER_NEW)
2281 new->f->state = new->state;
2282 hlist_del(&new->hlist);
2283 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002284 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002285 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002286 kfree(add_list);
2287 add_list = NULL;
2288 }
2289
Jacob Keller38326212016-11-11 12:39:26 -08002290 /* Determine the number of active and failed filters. */
2291 spin_lock_bh(&vsi->mac_filter_hash_lock);
2292 vsi->active_filters = 0;
2293 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2294 if (f->state == I40E_FILTER_ACTIVE)
2295 vsi->active_filters++;
2296 else if (f->state == I40E_FILTER_FAILED)
2297 failed_filters++;
2298 }
2299 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2300
2301 /* If promiscuous mode has changed, we need to calculate a new
2302 * threshold for when we are safe to exit
2303 */
2304 if (promisc_changed)
2305 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2306
2307 /* Check if we are able to exit overflow promiscuous mode. We can
2308 * safely exit if we didn't just enter, we no longer have any failed
2309 * filters, and we have reduced filters below the threshold value.
2310 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002311 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002312 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002313 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002314 dev_info(&pf->pdev->dev,
2315 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2316 vsi_name);
2317 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2318 promisc_changed = true;
2319 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002320 }
2321
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002322 /* if the VF is not trusted do not do promisc */
2323 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2324 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2325 goto out;
2326 }
2327
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002328 /* check for changes in promiscuous modes */
2329 if (changed_flags & IFF_ALLMULTI) {
2330 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002331
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002332 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002333 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2334 vsi->seid,
2335 cur_multipromisc,
2336 NULL);
2337 if (aq_ret) {
2338 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002339 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002340 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002341 "set multi promisc failed on %s, err %s aq_err %s\n",
2342 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002343 i40e_stat_str(hw, aq_ret),
2344 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002345 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002346 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002347 if ((changed_flags & IFF_PROMISC) ||
2348 (promisc_changed &&
2349 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002350 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002352 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2353 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2354 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002355 if ((vsi->type == I40E_VSI_MAIN) &&
2356 (pf->lan_veb != I40E_NO_VEB) &&
2357 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002358 /* set defport ON for Main VSI instead of true promisc
2359 * this way we will get all unicast/multicast and VLAN
2360 * promisc behavior but will not get VF or VMDq traffic
2361 * replicated on the Main VSI.
2362 */
2363 if (pf->cur_promisc != cur_promisc) {
2364 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002365 if (cur_promisc)
2366 aq_ret =
2367 i40e_aq_set_default_vsi(hw,
2368 vsi->seid,
2369 NULL);
2370 else
2371 aq_ret =
2372 i40e_aq_clear_default_vsi(hw,
2373 vsi->seid,
2374 NULL);
2375 if (aq_ret) {
2376 retval = i40e_aq_rc_to_posix(aq_ret,
2377 hw->aq.asq_last_status);
2378 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002379 "Set default VSI failed on %s, err %s, aq_err %s\n",
2380 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002381 i40e_stat_str(hw, aq_ret),
2382 i40e_aq_str(hw,
2383 hw->aq.asq_last_status));
2384 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002385 }
2386 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002387 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002388 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002389 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002390 cur_promisc, NULL,
2391 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002392 if (aq_ret) {
2393 retval =
2394 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002395 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002396 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002397 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2398 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002399 i40e_stat_str(hw, aq_ret),
2400 i40e_aq_str(hw,
2401 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002402 }
2403 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002404 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002405 vsi->seid,
2406 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002407 if (aq_ret) {
2408 retval =
2409 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002410 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002411 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002412 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2413 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002414 i40e_stat_str(hw, aq_ret),
2415 i40e_aq_str(hw,
2416 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002417 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002418 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002419 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2420 vsi->seid,
2421 cur_promisc, NULL);
2422 if (aq_ret) {
2423 retval = i40e_aq_rc_to_posix(aq_ret,
2424 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002425 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002426 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002427 i40e_stat_str(hw, aq_ret),
2428 i40e_aq_str(hw,
2429 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002430 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002431 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002432out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002433 /* if something went wrong then set the changed flag so we try again */
2434 if (retval)
2435 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002438 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002439
2440err_no_memory:
2441 /* Restore elements on the temporary add and delete lists */
2442 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002443err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002444 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2445 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002446 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2447
2448 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2449 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2450 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451}
2452
2453/**
2454 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2455 * @pf: board private structure
2456 **/
2457static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2458{
2459 int v;
2460
2461 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2462 return;
2463 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2464
Mitch Williams505682c2014-05-20 08:01:37 +00002465 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002466 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002467 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2468 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2469
2470 if (ret) {
2471 /* come back and try again later */
2472 pf->flags |= I40E_FLAG_FILTER_SYNC;
2473 break;
2474 }
2475 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002476 }
2477}
2478
2479/**
2480 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2481 * @netdev: network interface device structure
2482 * @new_mtu: new value for maximum frame size
2483 *
2484 * Returns 0 on success, negative on failure
2485 **/
2486static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2487{
2488 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002489 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002490 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002491
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492 netdev_info(netdev, "changing MTU from %d to %d\n",
2493 netdev->mtu, new_mtu);
2494 netdev->mtu = new_mtu;
2495 if (netif_running(netdev))
2496 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002497 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2498 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002499 return 0;
2500}
2501
2502/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002503 * i40e_ioctl - Access the hwtstamp interface
2504 * @netdev: network interface device structure
2505 * @ifr: interface request data
2506 * @cmd: ioctl command
2507 **/
2508int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2509{
2510 struct i40e_netdev_priv *np = netdev_priv(netdev);
2511 struct i40e_pf *pf = np->vsi->back;
2512
2513 switch (cmd) {
2514 case SIOCGHWTSTAMP:
2515 return i40e_ptp_get_ts_config(pf, ifr);
2516 case SIOCSHWTSTAMP:
2517 return i40e_ptp_set_ts_config(pf, ifr);
2518 default:
2519 return -EOPNOTSUPP;
2520 }
2521}
2522
2523/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2525 * @vsi: the vsi being adjusted
2526 **/
2527void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2528{
2529 struct i40e_vsi_context ctxt;
2530 i40e_status ret;
2531
2532 if ((vsi->info.valid_sections &
2533 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2534 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2535 return; /* already enabled */
2536
2537 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2538 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2539 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2540
2541 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002542 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002543 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2544 if (ret) {
2545 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002546 "update vlan stripping failed, err %s aq_err %s\n",
2547 i40e_stat_str(&vsi->back->hw, ret),
2548 i40e_aq_str(&vsi->back->hw,
2549 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002550 }
2551}
2552
2553/**
2554 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2555 * @vsi: the vsi being adjusted
2556 **/
2557void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2558{
2559 struct i40e_vsi_context ctxt;
2560 i40e_status ret;
2561
2562 if ((vsi->info.valid_sections &
2563 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2564 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2565 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2566 return; /* already disabled */
2567
2568 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2569 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2570 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2571
2572 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002573 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002574 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2575 if (ret) {
2576 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002577 "update vlan stripping failed, err %s aq_err %s\n",
2578 i40e_stat_str(&vsi->back->hw, ret),
2579 i40e_aq_str(&vsi->back->hw,
2580 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002581 }
2582}
2583
2584/**
2585 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2586 * @netdev: network interface to be adjusted
2587 * @features: netdev features to test if VLAN offload is enabled or not
2588 **/
2589static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2590{
2591 struct i40e_netdev_priv *np = netdev_priv(netdev);
2592 struct i40e_vsi *vsi = np->vsi;
2593
2594 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2595 i40e_vlan_stripping_enable(vsi);
2596 else
2597 i40e_vlan_stripping_disable(vsi);
2598}
2599
2600/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002601 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002602 * @vsi: the vsi being configured
2603 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002604 *
2605 * This is a helper function for adding a new MAC/VLAN filter with the
2606 * specified VLAN for each existing MAC address already in the hash table.
2607 * This function does *not* perform any accounting to update filters based on
2608 * VLAN mode.
2609 *
2610 * NOTE: this function expects to be called while under the
2611 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002612 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002613int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002614{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002615 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002616 struct hlist_node *h;
2617 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002618
Jacob Keller278e7d02016-10-05 09:30:37 -07002619 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002620 if (f->state == I40E_FILTER_REMOVE)
2621 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002622 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002623 if (!add_f) {
2624 dev_info(&vsi->back->pdev->dev,
2625 "Could not add vlan filter %d for %pM\n",
2626 vid, f->macaddr);
2627 return -ENOMEM;
2628 }
2629 }
2630
Jacob Keller490a4ad2016-11-11 12:39:29 -08002631 return 0;
2632}
2633
2634/**
2635 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2636 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002637 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002638 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002639int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002640{
Jacob Keller489a3262016-11-11 12:39:31 -08002641 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002642
Jacob Kellerf94484b2016-12-07 14:05:34 -08002643 if (!vid || vsi->info.pvid)
2644 return -EINVAL;
2645
Jacob Keller490a4ad2016-11-11 12:39:29 -08002646 /* Locked once because all functions invoked below iterates list*/
2647 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002648 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002649 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002650 if (err)
2651 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002652
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002653 /* schedule our worker thread which will take care of
2654 * applying the new filter changes
2655 */
2656 i40e_service_event_schedule(vsi->back);
2657 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002658}
2659
2660/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002661 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002662 * @vsi: the vsi being configured
2663 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002664 *
2665 * This function should be used to remove all VLAN filters which match the
2666 * given VID. It does not schedule the service event and does not take the
2667 * mac_filter_hash_lock so it may be combined with other operations under
2668 * a single invocation of the mac_filter_hash_lock.
2669 *
2670 * NOTE: this function expects to be called while under the
2671 * mac_filter_hash_lock
2672 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002673void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002674{
Alan Brady84f5ca62016-10-05 09:30:39 -07002675 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002676 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002677 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002678
Jacob Keller278e7d02016-10-05 09:30:37 -07002679 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002680 if (f->vlan == vid)
2681 __i40e_del_filter(vsi, f);
2682 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002683}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684
Jacob Keller490a4ad2016-11-11 12:39:29 -08002685/**
2686 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2687 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002688 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002689 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002690void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002691{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002692 if (!vid || vsi->info.pvid)
2693 return;
2694
Jacob Keller490a4ad2016-11-11 12:39:29 -08002695 spin_lock_bh(&vsi->mac_filter_hash_lock);
2696 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002697 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002698
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002699 /* schedule our worker thread which will take care of
2700 * applying the new filter changes
2701 */
2702 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002703}
2704
2705/**
2706 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2707 * @netdev: network interface to be adjusted
2708 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002709 *
2710 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002711 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002712#ifdef I40E_FCOE
2713int i40e_vlan_rx_add_vid(struct net_device *netdev,
2714 __always_unused __be16 proto, u16 vid)
2715#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002716static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2717 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002718#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719{
2720 struct i40e_netdev_priv *np = netdev_priv(netdev);
2721 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002722 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002723
Jacob Keller6a1127852016-10-25 16:08:49 -07002724 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002725 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002726
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002727 /* If the network stack called us with vid = 0 then
2728 * it is asking to receive priority tagged packets with
2729 * vlan id 0. Our HW receives them by default when configured
2730 * to receive untagged packets so there is no need to add an
2731 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002732 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002733 if (vid)
2734 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002735
Jacob Keller6a1127852016-10-25 16:08:49 -07002736 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002737 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002738
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002739 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002740}
2741
2742/**
2743 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2744 * @netdev: network interface to be adjusted
2745 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002746 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002747 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002748 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002749#ifdef I40E_FCOE
2750int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2751 __always_unused __be16 proto, u16 vid)
2752#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002753static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2754 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002755#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756{
2757 struct i40e_netdev_priv *np = netdev_priv(netdev);
2758 struct i40e_vsi *vsi = np->vsi;
2759
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002760 /* return code is ignored as there is nothing a user
2761 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002762 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763 */
2764 i40e_vsi_kill_vlan(vsi, vid);
2765
2766 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002767
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002768 return 0;
2769}
2770
2771/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002772 * i40e_macaddr_init - explicitly write the mac address filters
2773 *
2774 * @vsi: pointer to the vsi
2775 * @macaddr: the MAC address
2776 *
2777 * This is needed when the macaddr has been obtained by other
2778 * means than the default, e.g., from Open Firmware or IDPROM.
2779 * Returns 0 on success, negative on failure
2780 **/
2781static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2782{
2783 int ret;
2784 struct i40e_aqc_add_macvlan_element_data element;
2785
2786 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2787 I40E_AQC_WRITE_TYPE_LAA_WOL,
2788 macaddr, NULL);
2789 if (ret) {
2790 dev_info(&vsi->back->pdev->dev,
2791 "Addr change for VSI failed: %d\n", ret);
2792 return -EADDRNOTAVAIL;
2793 }
2794
2795 memset(&element, 0, sizeof(element));
2796 ether_addr_copy(element.mac_addr, macaddr);
2797 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2798 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2799 if (ret) {
2800 dev_info(&vsi->back->pdev->dev,
2801 "add filter failed err %s aq_err %s\n",
2802 i40e_stat_str(&vsi->back->hw, ret),
2803 i40e_aq_str(&vsi->back->hw,
2804 vsi->back->hw.aq.asq_last_status));
2805 }
2806 return ret;
2807}
2808
2809/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002810 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2811 * @vsi: the vsi being brought back up
2812 **/
2813static void i40e_restore_vlan(struct i40e_vsi *vsi)
2814{
2815 u16 vid;
2816
2817 if (!vsi->netdev)
2818 return;
2819
2820 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2821
2822 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2823 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2824 vid);
2825}
2826
2827/**
2828 * i40e_vsi_add_pvid - Add pvid for the VSI
2829 * @vsi: the vsi being adjusted
2830 * @vid: the vlan id to set as a PVID
2831 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002832int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002833{
2834 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002835 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836
2837 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2838 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002839 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2840 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002841 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002842
2843 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002844 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002845 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2846 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002847 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002848 "add pvid failed, err %s aq_err %s\n",
2849 i40e_stat_str(&vsi->back->hw, ret),
2850 i40e_aq_str(&vsi->back->hw,
2851 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002852 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002853 }
2854
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002855 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002856}
2857
2858/**
2859 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2860 * @vsi: the vsi being adjusted
2861 *
2862 * Just use the vlan_rx_register() service to put it back to normal
2863 **/
2864void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2865{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002866 i40e_vlan_stripping_disable(vsi);
2867
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002869}
2870
2871/**
2872 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2873 * @vsi: ptr to the VSI
2874 *
2875 * If this function returns with an error, then it's possible one or
2876 * more of the rings is populated (while the rest are not). It is the
2877 * callers duty to clean those orphaned rings.
2878 *
2879 * Return 0 on success, negative on failure
2880 **/
2881static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2882{
2883 int i, err = 0;
2884
2885 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002886 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887
2888 return err;
2889}
2890
2891/**
2892 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2893 * @vsi: ptr to the VSI
2894 *
2895 * Free VSI's transmit software resources
2896 **/
2897static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2898{
2899 int i;
2900
Greg Rose8e9dca52013-12-18 13:45:53 +00002901 if (!vsi->tx_rings)
2902 return;
2903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002904 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002905 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002906 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002907}
2908
2909/**
2910 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2911 * @vsi: ptr to the VSI
2912 *
2913 * If this function returns with an error, then it's possible one or
2914 * more of the rings is populated (while the rest are not). It is the
2915 * callers duty to clean those orphaned rings.
2916 *
2917 * Return 0 on success, negative on failure
2918 **/
2919static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2920{
2921 int i, err = 0;
2922
2923 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002924 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002925#ifdef I40E_FCOE
2926 i40e_fcoe_setup_ddp_resources(vsi);
2927#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002928 return err;
2929}
2930
2931/**
2932 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2933 * @vsi: ptr to the VSI
2934 *
2935 * Free all receive software resources
2936 **/
2937static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2938{
2939 int i;
2940
Greg Rose8e9dca52013-12-18 13:45:53 +00002941 if (!vsi->rx_rings)
2942 return;
2943
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002944 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002945 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002946 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002947#ifdef I40E_FCOE
2948 i40e_fcoe_free_ddp_resources(vsi);
2949#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002950}
2951
2952/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002953 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2954 * @ring: The Tx ring to configure
2955 *
2956 * This enables/disables XPS for a given Tx descriptor ring
2957 * based on the TCs enabled for the VSI that ring belongs to.
2958 **/
2959static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2960{
2961 struct i40e_vsi *vsi = ring->vsi;
2962 cpumask_var_t mask;
2963
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002964 if (!ring->q_vector || !ring->netdev)
2965 return;
2966
2967 /* Single TC mode enable XPS */
2968 if (vsi->tc_config.numtc <= 1) {
2969 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002970 netif_set_xps_queue(ring->netdev,
2971 &ring->q_vector->affinity_mask,
2972 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002973 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2974 /* Disable XPS to allow selection based on TC */
2975 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2976 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2977 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002978 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002979
2980 /* schedule our worker thread which will take care of
2981 * applying the new filter changes
2982 */
2983 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002984}
2985
2986/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002987 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2988 * @ring: The Tx ring to configure
2989 *
2990 * Configure the Tx descriptor ring in the HMC context.
2991 **/
2992static int i40e_configure_tx_ring(struct i40e_ring *ring)
2993{
2994 struct i40e_vsi *vsi = ring->vsi;
2995 u16 pf_q = vsi->base_queue + ring->queue_index;
2996 struct i40e_hw *hw = &vsi->back->hw;
2997 struct i40e_hmc_obj_txq tx_ctx;
2998 i40e_status err = 0;
2999 u32 qtx_ctl = 0;
3000
3001 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003002 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003003 ring->atr_sample_rate = vsi->back->atr_sample_rate;
3004 ring->atr_count = 0;
3005 } else {
3006 ring->atr_sample_rate = 0;
3007 }
3008
Neerav Parikh3ffa0372014-11-12 00:19:02 +00003009 /* configure XPS */
3010 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011
3012 /* clear the context structure first */
3013 memset(&tx_ctx, 0, sizeof(tx_ctx));
3014
3015 tx_ctx.new_context = 1;
3016 tx_ctx.base = (ring->dma / 128);
3017 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08003018 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3019 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07003020#ifdef I40E_FCOE
3021 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3022#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003023 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00003024 /* FDIR VSI tx ring can still use RS bit and writebacks */
3025 if (vsi->type != I40E_VSI_FDIR)
3026 tx_ctx.head_wb_ena = 1;
3027 tx_ctx.head_wb_addr = ring->dma +
3028 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003029
3030 /* As part of VSI creation/update, FW allocates certain
3031 * Tx arbitration queue sets for each TC enabled for
3032 * the VSI. The FW returns the handles to these queue
3033 * sets as part of the response buffer to Add VSI,
3034 * Update VSI, etc. AQ commands. It is expected that
3035 * these queue set handles be associated with the Tx
3036 * queues by the driver as part of the TX queue context
3037 * initialization. This has to be done regardless of
3038 * DCB as by default everything is mapped to TC0.
3039 */
3040 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3041 tx_ctx.rdylist_act = 0;
3042
3043 /* clear the context in the HMC */
3044 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3045 if (err) {
3046 dev_info(&vsi->back->pdev->dev,
3047 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3048 ring->queue_index, pf_q, err);
3049 return -ENOMEM;
3050 }
3051
3052 /* set the context in the HMC */
3053 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3054 if (err) {
3055 dev_info(&vsi->back->pdev->dev,
3056 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3057 ring->queue_index, pf_q, err);
3058 return -ENOMEM;
3059 }
3060
3061 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00003062 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003063 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003064 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3065 I40E_QTX_CTL_VFVM_INDX_MASK;
3066 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08003067 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00003068 }
3069
Shannon Nelson13fd9772013-09-28 07:14:19 +00003070 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3071 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003072 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3073 i40e_flush(hw);
3074
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003075 /* cache tail off for easier writes later */
3076 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3077
3078 return 0;
3079}
3080
3081/**
3082 * i40e_configure_rx_ring - Configure a receive ring context
3083 * @ring: The Rx ring to configure
3084 *
3085 * Configure the Rx descriptor ring in the HMC context.
3086 **/
3087static int i40e_configure_rx_ring(struct i40e_ring *ring)
3088{
3089 struct i40e_vsi *vsi = ring->vsi;
3090 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3091 u16 pf_q = vsi->base_queue + ring->queue_index;
3092 struct i40e_hw *hw = &vsi->back->hw;
3093 struct i40e_hmc_obj_rxq rx_ctx;
3094 i40e_status err = 0;
3095
3096 ring->state = 0;
3097
3098 /* clear the context structure first */
3099 memset(&rx_ctx, 0, sizeof(rx_ctx));
3100
3101 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003102
3103 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003104
3105 rx_ctx.base = (ring->dma / 128);
3106 rx_ctx.qlen = ring->count;
3107
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003108 /* use 32 byte descriptors */
3109 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003110
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003111 /* descriptor type is always zero
3112 * rx_ctx.dtype = 0;
3113 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003114 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003115
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003116 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003117 if (hw->revision_id == 0)
3118 rx_ctx.lrxqthresh = 0;
3119 else
3120 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121 rx_ctx.crcstrip = 1;
3122 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003123 /* this controls whether VLAN is stripped from inner headers */
3124 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07003125#ifdef I40E_FCOE
3126 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
3127#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00003128 /* set the prefena field to 1 because the manual says to */
3129 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003130
3131 /* clear the context in the HMC */
3132 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3133 if (err) {
3134 dev_info(&vsi->back->pdev->dev,
3135 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3136 ring->queue_index, pf_q, err);
3137 return -ENOMEM;
3138 }
3139
3140 /* set the context in the HMC */
3141 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3142 if (err) {
3143 dev_info(&vsi->back->pdev->dev,
3144 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3145 ring->queue_index, pf_q, err);
3146 return -ENOMEM;
3147 }
3148
3149 /* cache tail for quicker writes, and clear the reg before use */
3150 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3151 writel(0, ring->tail);
3152
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003153 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003154
3155 return 0;
3156}
3157
3158/**
3159 * i40e_vsi_configure_tx - Configure the VSI for Tx
3160 * @vsi: VSI structure describing this set of rings and resources
3161 *
3162 * Configure the Tx VSI for operation.
3163 **/
3164static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3165{
3166 int err = 0;
3167 u16 i;
3168
Alexander Duyck9f65e152013-09-28 06:00:58 +00003169 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3170 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003171
3172 return err;
3173}
3174
3175/**
3176 * i40e_vsi_configure_rx - Configure the VSI for Rx
3177 * @vsi: the VSI being configured
3178 *
3179 * Configure the Rx VSI for operation.
3180 **/
3181static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3182{
3183 int err = 0;
3184 u16 i;
3185
3186 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3187 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3188 + ETH_FCS_LEN + VLAN_HLEN;
3189 else
3190 vsi->max_frame = I40E_RXBUFFER_2048;
3191
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003192 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193
Vasu Dev38e00432014-08-01 13:27:03 -07003194#ifdef I40E_FCOE
3195 /* setup rx buffer for FCoE */
3196 if ((vsi->type == I40E_VSI_FCOE) &&
3197 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003198 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3199 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003200 }
3201
3202#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003203 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003204 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003205 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206
3207 /* set up individual rings */
3208 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003209 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210
3211 return err;
3212}
3213
3214/**
3215 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3216 * @vsi: ptr to the VSI
3217 **/
3218static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3219{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003220 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221 u16 qoffset, qcount;
3222 int i, n;
3223
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003224 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3225 /* Reset the TC information */
3226 for (i = 0; i < vsi->num_queue_pairs; i++) {
3227 rx_ring = vsi->rx_rings[i];
3228 tx_ring = vsi->tx_rings[i];
3229 rx_ring->dcb_tc = 0;
3230 tx_ring->dcb_tc = 0;
3231 }
3232 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233
3234 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003235 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 continue;
3237
3238 qoffset = vsi->tc_config.tc_info[n].qoffset;
3239 qcount = vsi->tc_config.tc_info[n].qcount;
3240 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003241 rx_ring = vsi->rx_rings[i];
3242 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243 rx_ring->dcb_tc = n;
3244 tx_ring->dcb_tc = n;
3245 }
3246 }
3247}
3248
3249/**
3250 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3251 * @vsi: ptr to the VSI
3252 **/
3253static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3254{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003255 struct i40e_pf *pf = vsi->back;
3256 int err;
3257
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258 if (vsi->netdev)
3259 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003260
3261 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3262 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3263 if (err) {
3264 dev_warn(&pf->pdev->dev,
3265 "could not set up macaddr; err %d\n", err);
3266 }
3267 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268}
3269
3270/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003271 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3272 * @vsi: Pointer to the targeted VSI
3273 *
3274 * This function replays the hlist on the hw where all the SB Flow Director
3275 * filters were saved.
3276 **/
3277static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3278{
3279 struct i40e_fdir_filter *filter;
3280 struct i40e_pf *pf = vsi->back;
3281 struct hlist_node *node;
3282
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003283 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3284 return;
3285
Jacob Keller6d069422017-02-06 14:38:44 -08003286 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003287 pf->fd_tcp4_filter_cnt = 0;
3288 pf->fd_udp4_filter_cnt = 0;
3289 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003290
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003291 hlist_for_each_entry_safe(filter, node,
3292 &pf->fdir_filter_list, fdir_node) {
3293 i40e_add_del_fdir(vsi, filter, true);
3294 }
3295}
3296
3297/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003298 * i40e_vsi_configure - Set up the VSI for action
3299 * @vsi: the VSI being configured
3300 **/
3301static int i40e_vsi_configure(struct i40e_vsi *vsi)
3302{
3303 int err;
3304
3305 i40e_set_vsi_rx_mode(vsi);
3306 i40e_restore_vlan(vsi);
3307 i40e_vsi_config_dcb_rings(vsi);
3308 err = i40e_vsi_configure_tx(vsi);
3309 if (!err)
3310 err = i40e_vsi_configure_rx(vsi);
3311
3312 return err;
3313}
3314
3315/**
3316 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3317 * @vsi: the VSI being configured
3318 **/
3319static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3320{
3321 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003322 struct i40e_hw *hw = &pf->hw;
3323 u16 vector;
3324 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325 u32 qp;
3326
3327 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3328 * and PFINT_LNKLSTn registers, e.g.:
3329 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3330 */
3331 qp = vsi->base_queue;
3332 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003333 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003334 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3335
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003336 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003337 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3339 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3340 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003341 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3343 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3344 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003345 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003346 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347
3348 /* Linked list for the queuepairs assigned to this vector */
3349 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3350 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003351 u32 val;
3352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003353 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3354 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3355 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3356 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3357 (I40E_QUEUE_TYPE_TX
3358 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3359
3360 wr32(hw, I40E_QINT_RQCTL(qp), val);
3361
3362 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3363 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3364 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3365 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3366 (I40E_QUEUE_TYPE_RX
3367 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3368
3369 /* Terminate the linked list */
3370 if (q == (q_vector->num_ringpairs - 1))
3371 val |= (I40E_QUEUE_END_OF_LIST
3372 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3373
3374 wr32(hw, I40E_QINT_TQCTL(qp), val);
3375 qp++;
3376 }
3377 }
3378
3379 i40e_flush(hw);
3380}
3381
3382/**
3383 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3384 * @hw: ptr to the hardware info
3385 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003386static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003387{
Jacob Kellerab437b52014-12-14 01:55:08 +00003388 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003389 u32 val;
3390
3391 /* clear things first */
3392 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3393 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3394
3395 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3396 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3397 I40E_PFINT_ICR0_ENA_GRST_MASK |
3398 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3399 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003400 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3401 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3402 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3403
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003404 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3405 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3406
Jacob Kellerab437b52014-12-14 01:55:08 +00003407 if (pf->flags & I40E_FLAG_PTP)
3408 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003410 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3411
3412 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003413 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3414 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003415
3416 /* OTHER_ITR_IDX = 0 */
3417 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3418}
3419
3420/**
3421 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3422 * @vsi: the VSI being configured
3423 **/
3424static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3425{
Alexander Duyck493fb302013-09-28 07:01:44 +00003426 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003427 struct i40e_pf *pf = vsi->back;
3428 struct i40e_hw *hw = &pf->hw;
3429 u32 val;
3430
3431 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003432 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003433 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003434 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3435 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003436 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003437 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3438 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3439
Jacob Kellerab437b52014-12-14 01:55:08 +00003440 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003441
3442 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3443 wr32(hw, I40E_PFINT_LNKLST0, 0);
3444
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003445 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3447 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3448 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3449
3450 wr32(hw, I40E_QINT_RQCTL(0), val);
3451
3452 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3453 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3454 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3455
3456 wr32(hw, I40E_QINT_TQCTL(0), val);
3457 i40e_flush(hw);
3458}
3459
3460/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003461 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3462 * @pf: board private structure
3463 **/
3464void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3465{
3466 struct i40e_hw *hw = &pf->hw;
3467
3468 wr32(hw, I40E_PFINT_DYN_CTL0,
3469 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3470 i40e_flush(hw);
3471}
3472
3473/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003474 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3475 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003476 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003478void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003479{
3480 struct i40e_hw *hw = &pf->hw;
3481 u32 val;
3482
3483 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003484 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003485 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3486
3487 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3488 i40e_flush(hw);
3489}
3490
3491/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003492 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3493 * @irq: interrupt number
3494 * @data: pointer to a q_vector
3495 **/
3496static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3497{
3498 struct i40e_q_vector *q_vector = data;
3499
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003500 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003501 return IRQ_HANDLED;
3502
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003503 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003504
3505 return IRQ_HANDLED;
3506}
3507
3508/**
Alan Brady96db7762016-09-14 16:24:38 -07003509 * i40e_irq_affinity_notify - Callback for affinity changes
3510 * @notify: context as to what irq was changed
3511 * @mask: the new affinity mask
3512 *
3513 * This is a callback function used by the irq_set_affinity_notifier function
3514 * so that we may register to receive changes to the irq affinity masks.
3515 **/
3516static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3517 const cpumask_t *mask)
3518{
3519 struct i40e_q_vector *q_vector =
3520 container_of(notify, struct i40e_q_vector, affinity_notify);
3521
3522 q_vector->affinity_mask = *mask;
3523}
3524
3525/**
3526 * i40e_irq_affinity_release - Callback for affinity notifier release
3527 * @ref: internal core kernel usage
3528 *
3529 * This is a callback function used by the irq_set_affinity_notifier function
3530 * to inform the current notification subscriber that they will no longer
3531 * receive notifications.
3532 **/
3533static void i40e_irq_affinity_release(struct kref *ref) {}
3534
3535/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003536 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3537 * @vsi: the VSI being configured
3538 * @basename: name for the vector
3539 *
3540 * Allocates MSI-X vectors and requests interrupts from the kernel.
3541 **/
3542static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3543{
3544 int q_vectors = vsi->num_q_vectors;
3545 struct i40e_pf *pf = vsi->back;
3546 int base = vsi->base_vector;
3547 int rx_int_idx = 0;
3548 int tx_int_idx = 0;
3549 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003550 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003551
3552 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003553 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003554
Alan Brady96db7762016-09-14 16:24:38 -07003555 irq_num = pf->msix_entries[base + vector].vector;
3556
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003557 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003558 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3559 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3560 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003561 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003562 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3563 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003564 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003565 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3566 "%s-%s-%d", basename, "tx", tx_int_idx++);
3567 } else {
3568 /* skip this unused q_vector */
3569 continue;
3570 }
Alan Brady96db7762016-09-14 16:24:38 -07003571 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572 vsi->irq_handler,
3573 0,
3574 q_vector->name,
3575 q_vector);
3576 if (err) {
3577 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003578 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003579 goto free_queue_irqs;
3580 }
Alan Brady96db7762016-09-14 16:24:38 -07003581
3582 /* register for affinity change notifications */
3583 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3584 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3585 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003586 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003587 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003588 }
3589
Shannon Nelson63741842014-04-23 04:50:16 +00003590 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003591 return 0;
3592
3593free_queue_irqs:
3594 while (vector) {
3595 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003596 irq_num = pf->msix_entries[base + vector].vector;
3597 irq_set_affinity_notifier(irq_num, NULL);
3598 irq_set_affinity_hint(irq_num, NULL);
3599 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003600 }
3601 return err;
3602}
3603
3604/**
3605 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3606 * @vsi: the VSI being un-configured
3607 **/
3608static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3609{
3610 struct i40e_pf *pf = vsi->back;
3611 struct i40e_hw *hw = &pf->hw;
3612 int base = vsi->base_vector;
3613 int i;
3614
3615 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003616 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3617 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003618 }
3619
3620 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3621 for (i = vsi->base_vector;
3622 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3623 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3624
3625 i40e_flush(hw);
3626 for (i = 0; i < vsi->num_q_vectors; i++)
3627 synchronize_irq(pf->msix_entries[i + base].vector);
3628 } else {
3629 /* Legacy and MSI mode - this stops all interrupt handling */
3630 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3631 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3632 i40e_flush(hw);
3633 synchronize_irq(pf->pdev->irq);
3634 }
3635}
3636
3637/**
3638 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3639 * @vsi: the VSI being configured
3640 **/
3641static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3642{
3643 struct i40e_pf *pf = vsi->back;
3644 int i;
3645
3646 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003647 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003648 i40e_irq_dynamic_enable(vsi, i);
3649 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003650 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003651 }
3652
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003653 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003654 return 0;
3655}
3656
3657/**
3658 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3659 * @pf: board private structure
3660 **/
3661static void i40e_stop_misc_vector(struct i40e_pf *pf)
3662{
3663 /* Disable ICR 0 */
3664 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3665 i40e_flush(&pf->hw);
3666}
3667
3668/**
3669 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3670 * @irq: interrupt number
3671 * @data: pointer to a q_vector
3672 *
3673 * This is the handler used for all MSI/Legacy interrupts, and deals
3674 * with both queue and non-queue interrupts. This is also used in
3675 * MSIX mode to handle the non-queue interrupts.
3676 **/
3677static irqreturn_t i40e_intr(int irq, void *data)
3678{
3679 struct i40e_pf *pf = (struct i40e_pf *)data;
3680 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003681 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682 u32 icr0, icr0_remaining;
3683 u32 val, ena_mask;
3684
3685 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003686 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687
Shannon Nelson116a57d2013-09-28 07:13:59 +00003688 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3689 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003690 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003691
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003692 /* if interrupt but no bits showing, must be SWINT */
3693 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3694 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3695 pf->sw_int_count++;
3696
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003697 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3698 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3699 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3700 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003701 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003702 }
3703
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003704 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3705 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003706 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3707 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003708
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003709 /* We do not have a way to disarm Queue causes while leaving
3710 * interrupt enabled for all other causes, ideally
3711 * interrupt should be disabled while we are in NAPI but
3712 * this is not a performance path and napi_schedule()
3713 * can deal with rescheduling.
3714 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003715 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003716 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003717 }
3718
3719 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3720 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3721 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003722 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003723 }
3724
3725 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3726 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3727 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3728 }
3729
3730 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3731 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3732 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3733 }
3734
3735 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3736 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3737 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3738 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3739 val = rd32(hw, I40E_GLGEN_RSTAT);
3740 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3741 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003742 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003743 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003744 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003745 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003746 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003748 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003749 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003750 }
3751
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003752 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3753 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3754 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003755 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3756 rd32(hw, I40E_PFHMC_ERRORINFO),
3757 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003758 }
3759
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003760 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3761 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3762
3763 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003764 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003765 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003766 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003767 }
3768
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003769 /* If a critical error is pending we have no choice but to reset the
3770 * device.
3771 * Report and mask out any remaining unexpected interrupts.
3772 */
3773 icr0_remaining = icr0 & ena_mask;
3774 if (icr0_remaining) {
3775 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3776 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003777 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003778 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003779 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003780 dev_info(&pf->pdev->dev, "device will be reset\n");
3781 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3782 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783 }
3784 ena_mask &= ~icr0_remaining;
3785 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003786 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003787
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003788enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 /* re-enable interrupt causes */
3790 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003791 if (!test_bit(__I40E_DOWN, &pf->state)) {
3792 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003793 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003794 }
3795
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003796 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003797}
3798
3799/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003800 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3801 * @tx_ring: tx ring to clean
3802 * @budget: how many cleans we're allowed
3803 *
3804 * Returns true if there's any budget left (e.g. the clean is finished)
3805 **/
3806static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3807{
3808 struct i40e_vsi *vsi = tx_ring->vsi;
3809 u16 i = tx_ring->next_to_clean;
3810 struct i40e_tx_buffer *tx_buf;
3811 struct i40e_tx_desc *tx_desc;
3812
3813 tx_buf = &tx_ring->tx_bi[i];
3814 tx_desc = I40E_TX_DESC(tx_ring, i);
3815 i -= tx_ring->count;
3816
3817 do {
3818 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3819
3820 /* if next_to_watch is not set then there is no work pending */
3821 if (!eop_desc)
3822 break;
3823
3824 /* prevent any other reads prior to eop_desc */
3825 read_barrier_depends();
3826
3827 /* if the descriptor isn't done, no work yet to do */
3828 if (!(eop_desc->cmd_type_offset_bsz &
3829 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3830 break;
3831
3832 /* clear next_to_watch to prevent false hangs */
3833 tx_buf->next_to_watch = NULL;
3834
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003835 tx_desc->buffer_addr = 0;
3836 tx_desc->cmd_type_offset_bsz = 0;
3837 /* move past filter desc */
3838 tx_buf++;
3839 tx_desc++;
3840 i++;
3841 if (unlikely(!i)) {
3842 i -= tx_ring->count;
3843 tx_buf = tx_ring->tx_bi;
3844 tx_desc = I40E_TX_DESC(tx_ring, 0);
3845 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003846 /* unmap skb header data */
3847 dma_unmap_single(tx_ring->dev,
3848 dma_unmap_addr(tx_buf, dma),
3849 dma_unmap_len(tx_buf, len),
3850 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003851 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3852 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003853
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003854 tx_buf->raw_buf = NULL;
3855 tx_buf->tx_flags = 0;
3856 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003857 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003858 tx_desc->buffer_addr = 0;
3859 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003860
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003861 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003862 tx_buf++;
3863 tx_desc++;
3864 i++;
3865 if (unlikely(!i)) {
3866 i -= tx_ring->count;
3867 tx_buf = tx_ring->tx_bi;
3868 tx_desc = I40E_TX_DESC(tx_ring, 0);
3869 }
3870
3871 /* update budget accounting */
3872 budget--;
3873 } while (likely(budget));
3874
3875 i += tx_ring->count;
3876 tx_ring->next_to_clean = i;
3877
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003878 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003879 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003880
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003881 return budget > 0;
3882}
3883
3884/**
3885 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3886 * @irq: interrupt number
3887 * @data: pointer to a q_vector
3888 **/
3889static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3890{
3891 struct i40e_q_vector *q_vector = data;
3892 struct i40e_vsi *vsi;
3893
3894 if (!q_vector->tx.ring)
3895 return IRQ_HANDLED;
3896
3897 vsi = q_vector->tx.ring->vsi;
3898 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3899
3900 return IRQ_HANDLED;
3901}
3902
3903/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003904 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003905 * @vsi: the VSI being configured
3906 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003907 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003908 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003909static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003910{
Alexander Duyck493fb302013-09-28 07:01:44 +00003911 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003912 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3913 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003914
3915 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003916 tx_ring->next = q_vector->tx.ring;
3917 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003918 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003919
3920 rx_ring->q_vector = q_vector;
3921 rx_ring->next = q_vector->rx.ring;
3922 q_vector->rx.ring = rx_ring;
3923 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003924}
3925
3926/**
3927 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3928 * @vsi: the VSI being configured
3929 *
3930 * This function maps descriptor rings to the queue-specific vectors
3931 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3932 * one vector per queue pair, but on a constrained vector budget, we
3933 * group the queue pairs as "efficiently" as possible.
3934 **/
3935static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3936{
3937 int qp_remaining = vsi->num_queue_pairs;
3938 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003939 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003940 int v_start = 0;
3941 int qp_idx = 0;
3942
3943 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3944 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003945 * It is also important to go through all the vectors available to be
3946 * sure that if we don't use all the vectors, that the remaining vectors
3947 * are cleared. This is especially important when decreasing the
3948 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003949 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003950 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003951 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3952
3953 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3954
3955 q_vector->num_ringpairs = num_ringpairs;
3956
3957 q_vector->rx.count = 0;
3958 q_vector->tx.count = 0;
3959 q_vector->rx.ring = NULL;
3960 q_vector->tx.ring = NULL;
3961
3962 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003963 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003964 qp_idx++;
3965 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003966 }
3967 }
3968}
3969
3970/**
3971 * i40e_vsi_request_irq - Request IRQ from the OS
3972 * @vsi: the VSI being configured
3973 * @basename: name for the vector
3974 **/
3975static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3976{
3977 struct i40e_pf *pf = vsi->back;
3978 int err;
3979
3980 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3981 err = i40e_vsi_request_irq_msix(vsi, basename);
3982 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3983 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003984 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003985 else
3986 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003987 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003988
3989 if (err)
3990 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3991
3992 return err;
3993}
3994
3995#ifdef CONFIG_NET_POLL_CONTROLLER
3996/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003997 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003998 * @netdev: network interface device structure
3999 *
4000 * This is used by netconsole to send skbs without having to re-enable
4001 * interrupts. It's not called while the normal interrupt routine is executing.
4002 **/
Vasu Dev38e00432014-08-01 13:27:03 -07004003#ifdef I40E_FCOE
4004void i40e_netpoll(struct net_device *netdev)
4005#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004006static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07004007#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004008{
4009 struct i40e_netdev_priv *np = netdev_priv(netdev);
4010 struct i40e_vsi *vsi = np->vsi;
4011 struct i40e_pf *pf = vsi->back;
4012 int i;
4013
4014 /* if interface is down do nothing */
4015 if (test_bit(__I40E_DOWN, &vsi->state))
4016 return;
4017
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004018 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4019 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004020 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004021 } else {
4022 i40e_intr(pf->pdev->irq, netdev);
4023 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004024}
4025#endif
4026
4027/**
Neerav Parikh23527302014-06-03 23:50:15 +00004028 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4029 * @pf: the PF being configured
4030 * @pf_q: the PF queue
4031 * @enable: enable or disable state of the queue
4032 *
4033 * This routine will wait for the given Tx queue of the PF to reach the
4034 * enabled or disabled state.
4035 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4036 * multiple retries; else will return 0 in case of success.
4037 **/
4038static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4039{
4040 int i;
4041 u32 tx_reg;
4042
4043 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4044 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4045 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4046 break;
4047
Neerav Parikhf98a2002014-09-13 07:40:44 +00004048 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004049 }
4050 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4051 return -ETIMEDOUT;
4052
4053 return 0;
4054}
4055
4056/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004057 * i40e_vsi_control_tx - Start or stop a VSI's rings
4058 * @vsi: the VSI being configured
4059 * @enable: start or stop the rings
4060 **/
4061static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
4062{
4063 struct i40e_pf *pf = vsi->back;
4064 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004065 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004066 u32 tx_reg;
4067
4068 pf_q = vsi->base_queue;
4069 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00004070
4071 /* warn the TX unit of coming changes */
4072 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4073 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00004074 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00004075
Mitch Williams6c5ef622014-02-20 19:29:16 -08004076 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004077 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004078 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4079 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4080 break;
4081 usleep_range(1000, 2000);
4082 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00004083 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00004084 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00004085 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004086
4087 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004088 if (enable) {
4089 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08004090 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004091 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004092 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00004093 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004094
4095 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004096 /* No waiting for the Tx queue to disable */
4097 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4098 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004099
4100 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004101 ret = i40e_pf_txq_wait(pf, pf_q, enable);
4102 if (ret) {
4103 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004104 "VSI seid %d Tx ring %d %sable timeout\n",
4105 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004106 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004107 }
4108 }
4109
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004110 if (hw->revision_id == 0)
4111 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00004112 return ret;
4113}
4114
4115/**
4116 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4117 * @pf: the PF being configured
4118 * @pf_q: the PF queue
4119 * @enable: enable or disable state of the queue
4120 *
4121 * This routine will wait for the given Rx queue of the PF to reach the
4122 * enabled or disabled state.
4123 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4124 * multiple retries; else will return 0 in case of success.
4125 **/
4126static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4127{
4128 int i;
4129 u32 rx_reg;
4130
4131 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4132 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4133 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4134 break;
4135
Neerav Parikhf98a2002014-09-13 07:40:44 +00004136 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004137 }
4138 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4139 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004140
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004141 return 0;
4142}
4143
4144/**
4145 * i40e_vsi_control_rx - Start or stop a VSI's rings
4146 * @vsi: the VSI being configured
4147 * @enable: start or stop the rings
4148 **/
4149static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4150{
4151 struct i40e_pf *pf = vsi->back;
4152 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004153 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004154 u32 rx_reg;
4155
4156 pf_q = vsi->base_queue;
4157 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004158 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004159 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004160 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4161 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4162 break;
4163 usleep_range(1000, 2000);
4164 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004165
Catherine Sullivan7c122002014-03-14 07:32:29 +00004166 /* Skip if the queue is already in the requested state */
4167 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4168 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004169
4170 /* turn on/off the queue */
4171 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004172 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004173 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004174 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004175 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004176 /* No waiting for the Tx queue to disable */
4177 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4178 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004179
4180 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004181 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4182 if (ret) {
4183 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004184 "VSI seid %d Rx ring %d %sable timeout\n",
4185 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004186 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004187 }
4188 }
4189
Neerav Parikh23527302014-06-03 23:50:15 +00004190 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004191}
4192
4193/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004194 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004195 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004196 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004197int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004198{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004199 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004200
4201 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004202 ret = i40e_vsi_control_rx(vsi, true);
4203 if (ret)
4204 return ret;
4205 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004206
4207 return ret;
4208}
4209
4210/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004211 * i40e_vsi_stop_rings - Stop a VSI's rings
4212 * @vsi: the VSI being configured
4213 **/
4214void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4215{
4216 /* do rx first for enable and last for disable
4217 * Ignore return value, we need to shutdown whatever we can
4218 */
4219 i40e_vsi_control_tx(vsi, false);
4220 i40e_vsi_control_rx(vsi, false);
4221}
4222
4223/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004224 * i40e_vsi_free_irq - Free the irq association with the OS
4225 * @vsi: the VSI being configured
4226 **/
4227static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4228{
4229 struct i40e_pf *pf = vsi->back;
4230 struct i40e_hw *hw = &pf->hw;
4231 int base = vsi->base_vector;
4232 u32 val, qp;
4233 int i;
4234
4235 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4236 if (!vsi->q_vectors)
4237 return;
4238
Shannon Nelson63741842014-04-23 04:50:16 +00004239 if (!vsi->irqs_ready)
4240 return;
4241
4242 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004243 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004244 int irq_num;
4245 u16 vector;
4246
4247 vector = i + base;
4248 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004249
4250 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004251 if (!vsi->q_vectors[i] ||
4252 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004253 continue;
4254
Alan Brady96db7762016-09-14 16:24:38 -07004255 /* clear the affinity notifier in the IRQ descriptor */
4256 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004257 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004258 irq_set_affinity_hint(irq_num, NULL);
4259 synchronize_irq(irq_num);
4260 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004261
4262 /* Tear down the interrupt queue link list
4263 *
4264 * We know that they come in pairs and always
4265 * the Rx first, then the Tx. To clear the
4266 * link list, stick the EOL value into the
4267 * next_q field of the registers.
4268 */
4269 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4270 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4271 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4272 val |= I40E_QUEUE_END_OF_LIST
4273 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4274 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4275
4276 while (qp != I40E_QUEUE_END_OF_LIST) {
4277 u32 next;
4278
4279 val = rd32(hw, I40E_QINT_RQCTL(qp));
4280
4281 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4282 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4283 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4284 I40E_QINT_RQCTL_INTEVENT_MASK);
4285
4286 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4287 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4288
4289 wr32(hw, I40E_QINT_RQCTL(qp), val);
4290
4291 val = rd32(hw, I40E_QINT_TQCTL(qp));
4292
4293 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4294 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4295
4296 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4297 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4298 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4299 I40E_QINT_TQCTL_INTEVENT_MASK);
4300
4301 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4302 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4303
4304 wr32(hw, I40E_QINT_TQCTL(qp), val);
4305 qp = next;
4306 }
4307 }
4308 } else {
4309 free_irq(pf->pdev->irq, pf);
4310
4311 val = rd32(hw, I40E_PFINT_LNKLST0);
4312 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4313 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4314 val |= I40E_QUEUE_END_OF_LIST
4315 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4316 wr32(hw, I40E_PFINT_LNKLST0, val);
4317
4318 val = rd32(hw, I40E_QINT_RQCTL(qp));
4319 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4320 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4321 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4322 I40E_QINT_RQCTL_INTEVENT_MASK);
4323
4324 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4325 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4326
4327 wr32(hw, I40E_QINT_RQCTL(qp), val);
4328
4329 val = rd32(hw, I40E_QINT_TQCTL(qp));
4330
4331 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4332 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4333 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4334 I40E_QINT_TQCTL_INTEVENT_MASK);
4335
4336 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4337 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4338
4339 wr32(hw, I40E_QINT_TQCTL(qp), val);
4340 }
4341}
4342
4343/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004344 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4345 * @vsi: the VSI being configured
4346 * @v_idx: Index of vector to be freed
4347 *
4348 * This function frees the memory allocated to the q_vector. In addition if
4349 * NAPI is enabled it will delete any references to the NAPI struct prior
4350 * to freeing the q_vector.
4351 **/
4352static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4353{
4354 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004355 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004356
4357 if (!q_vector)
4358 return;
4359
4360 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004361 i40e_for_each_ring(ring, q_vector->tx)
4362 ring->q_vector = NULL;
4363
4364 i40e_for_each_ring(ring, q_vector->rx)
4365 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004366
4367 /* only VSI w/ an associated netdev is set up w/ NAPI */
4368 if (vsi->netdev)
4369 netif_napi_del(&q_vector->napi);
4370
4371 vsi->q_vectors[v_idx] = NULL;
4372
4373 kfree_rcu(q_vector, rcu);
4374}
4375
4376/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004377 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4378 * @vsi: the VSI being un-configured
4379 *
4380 * This frees the memory allocated to the q_vectors and
4381 * deletes references to the NAPI struct.
4382 **/
4383static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4384{
4385 int v_idx;
4386
Alexander Duyck493fb302013-09-28 07:01:44 +00004387 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4388 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004389}
4390
4391/**
4392 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4393 * @pf: board private structure
4394 **/
4395static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4396{
4397 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4398 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4399 pci_disable_msix(pf->pdev);
4400 kfree(pf->msix_entries);
4401 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004402 kfree(pf->irq_pile);
4403 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004404 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4405 pci_disable_msi(pf->pdev);
4406 }
4407 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4408}
4409
4410/**
4411 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4412 * @pf: board private structure
4413 *
4414 * We go through and clear interrupt specific resources and reset the structure
4415 * to pre-load conditions
4416 **/
4417static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4418{
4419 int i;
4420
Shannon Nelsone1477582015-02-21 06:44:33 +00004421 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004422 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004423 synchronize_irq(pf->msix_entries[0].vector);
4424 free_irq(pf->msix_entries[0].vector, pf);
4425 }
4426
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004427 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4428 I40E_IWARP_IRQ_PILE_ID);
4429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004430 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004431 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004432 if (pf->vsi[i])
4433 i40e_vsi_free_q_vectors(pf->vsi[i]);
4434 i40e_reset_interrupt_capability(pf);
4435}
4436
4437/**
4438 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4439 * @vsi: the VSI being configured
4440 **/
4441static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4442{
4443 int q_idx;
4444
4445 if (!vsi->netdev)
4446 return;
4447
4448 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004449 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004450}
4451
4452/**
4453 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4454 * @vsi: the VSI being configured
4455 **/
4456static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4457{
4458 int q_idx;
4459
4460 if (!vsi->netdev)
4461 return;
4462
4463 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004464 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004465}
4466
4467/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004468 * i40e_vsi_close - Shut down a VSI
4469 * @vsi: the vsi to be quelled
4470 **/
4471static void i40e_vsi_close(struct i40e_vsi *vsi)
4472{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004473 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004474 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4475 i40e_down(vsi);
4476 i40e_vsi_free_irq(vsi);
4477 i40e_vsi_free_tx_resources(vsi);
4478 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004479 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004480 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4481 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4482 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004483}
4484
4485/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004486 * i40e_quiesce_vsi - Pause a given VSI
4487 * @vsi: the VSI being paused
4488 **/
4489static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4490{
4491 if (test_bit(__I40E_DOWN, &vsi->state))
4492 return;
4493
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004494 /* No need to disable FCoE VSI when Tx suspended */
4495 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4496 vsi->type == I40E_VSI_FCOE) {
4497 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004498 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004499 return;
4500 }
4501
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004502 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004503 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004504 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004505 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004506 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004507}
4508
4509/**
4510 * i40e_unquiesce_vsi - Resume a given VSI
4511 * @vsi: the VSI being resumed
4512 **/
4513static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4514{
4515 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4516 return;
4517
4518 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4519 if (vsi->netdev && netif_running(vsi->netdev))
4520 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4521 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004522 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004523}
4524
4525/**
4526 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4527 * @pf: the PF
4528 **/
4529static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4530{
4531 int v;
4532
Mitch Williams505682c2014-05-20 08:01:37 +00004533 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004534 if (pf->vsi[v])
4535 i40e_quiesce_vsi(pf->vsi[v]);
4536 }
4537}
4538
4539/**
4540 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4541 * @pf: the PF
4542 **/
4543static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4544{
4545 int v;
4546
Mitch Williams505682c2014-05-20 08:01:37 +00004547 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004548 if (pf->vsi[v])
4549 i40e_unquiesce_vsi(pf->vsi[v]);
4550 }
4551}
4552
Neerav Parikh69129dc2014-11-12 00:18:46 +00004553#ifdef CONFIG_I40E_DCB
4554/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004555 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004556 * @vsi: the VSI being configured
4557 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004558 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004559 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004560static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004561{
4562 struct i40e_pf *pf = vsi->back;
4563 int i, pf_q, ret;
4564
4565 pf_q = vsi->base_queue;
4566 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4567 /* Check and wait for the disable status of the queue */
4568 ret = i40e_pf_txq_wait(pf, pf_q, false);
4569 if (ret) {
4570 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004571 "VSI seid %d Tx ring %d disable timeout\n",
4572 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004573 return ret;
4574 }
4575 }
4576
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004577 pf_q = vsi->base_queue;
4578 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4579 /* Check and wait for the disable status of the queue */
4580 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4581 if (ret) {
4582 dev_info(&pf->pdev->dev,
4583 "VSI seid %d Rx ring %d disable timeout\n",
4584 vsi->seid, pf_q);
4585 return ret;
4586 }
4587 }
4588
Neerav Parikh69129dc2014-11-12 00:18:46 +00004589 return 0;
4590}
4591
4592/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004593 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004594 * @pf: the PF
4595 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004596 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004597 * VSIs that are managed by this PF.
4598 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004599static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004600{
4601 int v, ret = 0;
4602
4603 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004604 /* No need to wait for FCoE VSI queues */
4605 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004606 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004607 if (ret)
4608 break;
4609 }
4610 }
4611
4612 return ret;
4613}
4614
4615#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004616
4617/**
4618 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4619 * @q_idx: TX queue number
4620 * @vsi: Pointer to VSI struct
4621 *
4622 * This function checks specified queue for given VSI. Detects hung condition.
4623 * Sets hung bit since it is two step process. Before next run of service task
4624 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4625 * hung condition remain unchanged and during subsequent run, this function
4626 * issues SW interrupt to recover from hung condition.
4627 **/
4628static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4629{
4630 struct i40e_ring *tx_ring = NULL;
4631 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004632 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004633 int i;
4634
4635 pf = vsi->back;
4636
4637 /* now that we have an index, find the tx_ring struct */
4638 for (i = 0; i < vsi->num_queue_pairs; i++) {
4639 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4640 if (q_idx == vsi->tx_rings[i]->queue_index) {
4641 tx_ring = vsi->tx_rings[i];
4642 break;
4643 }
4644 }
4645 }
4646
4647 if (!tx_ring)
4648 return;
4649
4650 /* Read interrupt register */
4651 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4652 val = rd32(&pf->hw,
4653 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4654 tx_ring->vsi->base_vector - 1));
4655 else
4656 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4657
4658 head = i40e_get_head(tx_ring);
4659
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004660 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004661
Kiran Patil9c6c1252015-11-06 15:26:02 -08004662 /* HW is done executing descriptors, updated HEAD write back,
4663 * but SW hasn't processed those descriptors. If interrupt is
4664 * not generated from this point ON, it could result into
4665 * dev_watchdog detecting timeout on those netdev_queue,
4666 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004667 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004668 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004669 /* NAPI Poll didn't run and clear since it was set */
4670 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4671 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004672 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",
4673 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004674 tx_ring->next_to_clean, head,
4675 tx_ring->next_to_use,
4676 readl(tx_ring->tail));
4677 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4678 vsi->seid, q_idx, val);
4679 i40e_force_wb(vsi, tx_ring->q_vector);
4680 } else {
4681 /* First Chance - detected possible hung */
4682 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4683 &tx_ring->q_vector->hung_detected);
4684 }
4685 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004686
4687 /* This is the case where we have interrupts missing,
4688 * so the tx_pending in HW will most likely be 0, but we
4689 * will have tx_pending in SW since the WB happened but the
4690 * interrupt got lost.
4691 */
4692 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4693 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004694 local_bh_disable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004695 if (napi_reschedule(&tx_ring->q_vector->napi))
4696 tx_ring->tx_stats.tx_lost_interrupt++;
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004697 local_bh_enable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004698 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004699}
4700
4701/**
4702 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4703 * @pf: pointer to PF struct
4704 *
4705 * LAN VSI has netdev and netdev has TX queues. This function is to check
4706 * each of those TX queues if they are hung, trigger recovery by issuing
4707 * SW interrupt.
4708 **/
4709static void i40e_detect_recover_hung(struct i40e_pf *pf)
4710{
4711 struct net_device *netdev;
4712 struct i40e_vsi *vsi;
4713 int i;
4714
4715 /* Only for LAN VSI */
4716 vsi = pf->vsi[pf->lan_vsi];
4717
4718 if (!vsi)
4719 return;
4720
4721 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4722 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4723 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4724 return;
4725
4726 /* Make sure type is MAIN VSI */
4727 if (vsi->type != I40E_VSI_MAIN)
4728 return;
4729
4730 netdev = vsi->netdev;
4731 if (!netdev)
4732 return;
4733
4734 /* Bail out if netif_carrier is not OK */
4735 if (!netif_carrier_ok(netdev))
4736 return;
4737
4738 /* Go thru' TX queues for netdev */
4739 for (i = 0; i < netdev->num_tx_queues; i++) {
4740 struct netdev_queue *q;
4741
4742 q = netdev_get_tx_queue(netdev, i);
4743 if (q)
4744 i40e_detect_recover_hung_queue(i, vsi);
4745 }
4746}
4747
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004748/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004749 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004750 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004751 *
4752 * Get TC map for ISCSI PF type that will include iSCSI TC
4753 * and LAN TC.
4754 **/
4755static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4756{
4757 struct i40e_dcb_app_priority_table app;
4758 struct i40e_hw *hw = &pf->hw;
4759 u8 enabled_tc = 1; /* TC0 is always enabled */
4760 u8 tc, i;
4761 /* Get the iSCSI APP TLV */
4762 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4763
4764 for (i = 0; i < dcbcfg->numapps; i++) {
4765 app = dcbcfg->app[i];
4766 if (app.selector == I40E_APP_SEL_TCPIP &&
4767 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4768 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004769 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004770 break;
4771 }
4772 }
4773
4774 return enabled_tc;
4775}
4776
4777/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4779 * @dcbcfg: the corresponding DCBx configuration structure
4780 *
4781 * Return the number of TCs from given DCBx configuration
4782 **/
4783static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4784{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004785 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004786 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004787 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004788
4789 /* Scan the ETS Config Priority Table to find
4790 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004791 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004792 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004793 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4794 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4795
4796 /* Now scan the bitmask to check for
4797 * contiguous TCs starting with TC0
4798 */
4799 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4800 if (num_tc & BIT(i)) {
4801 if (!tc_unused) {
4802 ret++;
4803 } else {
4804 pr_err("Non-contiguous TC - Disabling DCB\n");
4805 return 1;
4806 }
4807 } else {
4808 tc_unused = 1;
4809 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004810 }
4811
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004812 /* There is always at least TC0 */
4813 if (!ret)
4814 ret = 1;
4815
4816 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004817}
4818
4819/**
4820 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4821 * @dcbcfg: the corresponding DCBx configuration structure
4822 *
4823 * Query the current DCB configuration and return the number of
4824 * traffic classes enabled from the given DCBX config
4825 **/
4826static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4827{
4828 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4829 u8 enabled_tc = 1;
4830 u8 i;
4831
4832 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004833 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004834
4835 return enabled_tc;
4836}
4837
4838/**
4839 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4840 * @pf: PF being queried
4841 *
4842 * Return number of traffic classes enabled for the given PF
4843 **/
4844static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4845{
4846 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004847 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004848 u8 num_tc = 0;
4849 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4850
4851 /* If DCB is not enabled then always in single TC */
4852 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4853 return 1;
4854
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004855 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004856 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4857 return i40e_dcb_get_num_tc(dcbcfg);
4858
4859 /* MFP mode return count of enabled TCs for this PF */
4860 if (pf->hw.func_caps.iscsi)
4861 enabled_tc = i40e_get_iscsi_tc_map(pf);
4862 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004863 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004864
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004865 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004866 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004867 num_tc++;
4868 }
4869 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004870}
4871
4872/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4874 * @pf: PF being queried
4875 *
4876 * Return a bitmap for enabled traffic classes for this PF.
4877 **/
4878static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4879{
4880 /* If DCB is not enabled for this PF then just return default TC */
4881 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004882 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004884 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004885 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4886 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4887
Neerav Parikhfc51de92015-02-24 06:58:53 +00004888 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004889 if (pf->hw.func_caps.iscsi)
4890 return i40e_get_iscsi_tc_map(pf);
4891 else
David Ertmanea6acb72016-09-20 07:10:50 -07004892 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004893}
4894
4895/**
4896 * i40e_vsi_get_bw_info - Query VSI BW Information
4897 * @vsi: the VSI being queried
4898 *
4899 * Returns 0 on success, negative value on failure
4900 **/
4901static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4902{
4903 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4904 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4905 struct i40e_pf *pf = vsi->back;
4906 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004907 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004908 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004909 int i;
4910
4911 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004912 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4913 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004914 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004915 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4916 i40e_stat_str(&pf->hw, ret),
4917 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004918 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004919 }
4920
4921 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004922 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4923 NULL);
4924 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004925 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004926 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4927 i40e_stat_str(&pf->hw, ret),
4928 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004929 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004930 }
4931
4932 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4933 dev_info(&pf->pdev->dev,
4934 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4935 bw_config.tc_valid_bits,
4936 bw_ets_config.tc_valid_bits);
4937 /* Still continuing */
4938 }
4939
4940 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4941 vsi->bw_max_quanta = bw_config.max_bw;
4942 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4943 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4944 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4945 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4946 vsi->bw_ets_limit_credits[i] =
4947 le16_to_cpu(bw_ets_config.credits[i]);
4948 /* 3 bits out of 4 for each TC */
4949 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4950 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004951
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004952 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004953}
4954
4955/**
4956 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4957 * @vsi: the VSI being configured
4958 * @enabled_tc: TC bitmap
4959 * @bw_credits: BW shared credits per TC
4960 *
4961 * Returns 0 on success, negative value on failure
4962 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004963static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004964 u8 *bw_share)
4965{
4966 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004967 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004968 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004969
4970 bw_data.tc_valid_bits = enabled_tc;
4971 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4972 bw_data.tc_bw_credits[i] = bw_share[i];
4973
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004974 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4975 NULL);
4976 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004977 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004978 "AQ command Config VSI BW allocation per TC failed = %d\n",
4979 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004980 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004981 }
4982
4983 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4984 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4985
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004986 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004987}
4988
4989/**
4990 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4991 * @vsi: the VSI being configured
4992 * @enabled_tc: TC map to be enabled
4993 *
4994 **/
4995static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4996{
4997 struct net_device *netdev = vsi->netdev;
4998 struct i40e_pf *pf = vsi->back;
4999 struct i40e_hw *hw = &pf->hw;
5000 u8 netdev_tc = 0;
5001 int i;
5002 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5003
5004 if (!netdev)
5005 return;
5006
5007 if (!enabled_tc) {
5008 netdev_reset_tc(netdev);
5009 return;
5010 }
5011
5012 /* Set up actual enabled TCs on the VSI */
5013 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5014 return;
5015
5016 /* set per TC queues for the VSI */
5017 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5018 /* Only set TC queues for enabled tcs
5019 *
5020 * e.g. For a VSI that has TC0 and TC3 enabled the
5021 * enabled_tc bitmap would be 0x00001001; the driver
5022 * will set the numtc for netdev as 2 that will be
5023 * referenced by the netdev layer as TC 0 and 1.
5024 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005025 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005026 netdev_set_tc_queue(netdev,
5027 vsi->tc_config.tc_info[i].netdev_tc,
5028 vsi->tc_config.tc_info[i].qcount,
5029 vsi->tc_config.tc_info[i].qoffset);
5030 }
5031
5032 /* Assign UP2TC map for the VSI */
5033 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5034 /* Get the actual TC# for the UP */
5035 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5036 /* Get the mapped netdev TC# for the UP */
5037 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
5038 netdev_set_prio_tc_map(netdev, i, netdev_tc);
5039 }
5040}
5041
5042/**
5043 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5044 * @vsi: the VSI being configured
5045 * @ctxt: the ctxt buffer returned from AQ VSI update param command
5046 **/
5047static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5048 struct i40e_vsi_context *ctxt)
5049{
5050 /* copy just the sections touched not the entire info
5051 * since not all sections are valid as returned by
5052 * update vsi params
5053 */
5054 vsi->info.mapping_flags = ctxt->info.mapping_flags;
5055 memcpy(&vsi->info.queue_mapping,
5056 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5057 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5058 sizeof(vsi->info.tc_mapping));
5059}
5060
5061/**
5062 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5063 * @vsi: VSI to be configured
5064 * @enabled_tc: TC bitmap
5065 *
5066 * This configures a particular VSI for TCs that are mapped to the
5067 * given TC bitmap. It uses default bandwidth share for TCs across
5068 * VSIs to configure TC for a particular VSI.
5069 *
5070 * NOTE:
5071 * It is expected that the VSI queues have been quisced before calling
5072 * this function.
5073 **/
5074static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5075{
5076 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5077 struct i40e_vsi_context ctxt;
5078 int ret = 0;
5079 int i;
5080
5081 /* Check if enabled_tc is same as existing or new TCs */
5082 if (vsi->tc_config.enabled_tc == enabled_tc)
5083 return ret;
5084
5085 /* Enable ETS TCs with equal BW Share for now across all VSIs */
5086 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005087 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005088 bw_share[i] = 1;
5089 }
5090
5091 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5092 if (ret) {
5093 dev_info(&vsi->back->pdev->dev,
5094 "Failed configuring TC map %d for VSI %d\n",
5095 enabled_tc, vsi->seid);
5096 goto out;
5097 }
5098
5099 /* Update Queue Pairs Mapping for currently enabled UPs */
5100 ctxt.seid = vsi->seid;
5101 ctxt.pf_num = vsi->back->hw.pf_id;
5102 ctxt.vf_num = 0;
5103 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005104 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005105 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5106
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005107 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5108 ctxt.info.valid_sections |=
5109 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5110 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5111 }
5112
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005113 /* Update the VSI after updating the VSI queue-mapping information */
5114 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5115 if (ret) {
5116 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005117 "Update vsi tc config failed, err %s aq_err %s\n",
5118 i40e_stat_str(&vsi->back->hw, ret),
5119 i40e_aq_str(&vsi->back->hw,
5120 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005121 goto out;
5122 }
5123 /* update the local VSI info with updated queue map */
5124 i40e_vsi_update_queue_map(vsi, &ctxt);
5125 vsi->info.valid_sections = 0;
5126
5127 /* Update current VSI BW information */
5128 ret = i40e_vsi_get_bw_info(vsi);
5129 if (ret) {
5130 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005131 "Failed updating vsi bw info, err %s aq_err %s\n",
5132 i40e_stat_str(&vsi->back->hw, ret),
5133 i40e_aq_str(&vsi->back->hw,
5134 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005135 goto out;
5136 }
5137
5138 /* Update the netdev TC setup */
5139 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5140out:
5141 return ret;
5142}
5143
5144/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005145 * i40e_veb_config_tc - Configure TCs for given VEB
5146 * @veb: given VEB
5147 * @enabled_tc: TC bitmap
5148 *
5149 * Configures given TC bitmap for VEB (switching) element
5150 **/
5151int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5152{
5153 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5154 struct i40e_pf *pf = veb->pf;
5155 int ret = 0;
5156 int i;
5157
5158 /* No TCs or already enabled TCs just return */
5159 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5160 return ret;
5161
5162 bw_data.tc_valid_bits = enabled_tc;
5163 /* bw_data.absolute_credits is not set (relative) */
5164
5165 /* Enable ETS TCs with equal BW Share for now */
5166 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005167 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005168 bw_data.tc_bw_share_credits[i] = 1;
5169 }
5170
5171 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5172 &bw_data, NULL);
5173 if (ret) {
5174 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005175 "VEB bw config failed, err %s aq_err %s\n",
5176 i40e_stat_str(&pf->hw, ret),
5177 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005178 goto out;
5179 }
5180
5181 /* Update the BW information */
5182 ret = i40e_veb_get_bw_info(veb);
5183 if (ret) {
5184 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005185 "Failed getting veb bw config, err %s aq_err %s\n",
5186 i40e_stat_str(&pf->hw, ret),
5187 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005188 }
5189
5190out:
5191 return ret;
5192}
5193
5194#ifdef CONFIG_I40E_DCB
5195/**
5196 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5197 * @pf: PF struct
5198 *
5199 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5200 * the caller would've quiesce all the VSIs before calling
5201 * this function
5202 **/
5203static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5204{
5205 u8 tc_map = 0;
5206 int ret;
5207 u8 v;
5208
5209 /* Enable the TCs available on PF to all VEBs */
5210 tc_map = i40e_pf_get_tc_map(pf);
5211 for (v = 0; v < I40E_MAX_VEB; v++) {
5212 if (!pf->veb[v])
5213 continue;
5214 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5215 if (ret) {
5216 dev_info(&pf->pdev->dev,
5217 "Failed configuring TC for VEB seid=%d\n",
5218 pf->veb[v]->seid);
5219 /* Will try to configure as many components */
5220 }
5221 }
5222
5223 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005224 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005225 if (!pf->vsi[v])
5226 continue;
5227
5228 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005229#ifdef I40E_FCOE
5230 * - For FCoE VSI only enable the TC configured
5231 * as per the APP TLV
5232#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005233 * - For all others keep them at TC0 for now
5234 */
5235 if (v == pf->lan_vsi)
5236 tc_map = i40e_pf_get_tc_map(pf);
5237 else
David Ertmanea6acb72016-09-20 07:10:50 -07005238 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005239#ifdef I40E_FCOE
5240 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5241 tc_map = i40e_get_fcoe_tc_map(pf);
5242#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005243
5244 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5245 if (ret) {
5246 dev_info(&pf->pdev->dev,
5247 "Failed configuring TC for VSI seid=%d\n",
5248 pf->vsi[v]->seid);
5249 /* Will try to configure as many components */
5250 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005251 /* Re-configure VSI vectors based on updated TC map */
5252 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005253 if (pf->vsi[v]->netdev)
5254 i40e_dcbnl_set_all(pf->vsi[v]);
5255 }
5256 }
5257}
5258
5259/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005260 * i40e_resume_port_tx - Resume port Tx
5261 * @pf: PF struct
5262 *
5263 * Resume a port's Tx and issue a PF reset in case of failure to
5264 * resume.
5265 **/
5266static int i40e_resume_port_tx(struct i40e_pf *pf)
5267{
5268 struct i40e_hw *hw = &pf->hw;
5269 int ret;
5270
5271 ret = i40e_aq_resume_port_tx(hw, NULL);
5272 if (ret) {
5273 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005274 "Resume Port Tx failed, err %s aq_err %s\n",
5275 i40e_stat_str(&pf->hw, ret),
5276 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005277 /* Schedule PF reset to recover */
5278 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5279 i40e_service_event_schedule(pf);
5280 }
5281
5282 return ret;
5283}
5284
5285/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005286 * i40e_init_pf_dcb - Initialize DCB configuration
5287 * @pf: PF being configured
5288 *
5289 * Query the current DCB configuration and cache it
5290 * in the hardware structure
5291 **/
5292static int i40e_init_pf_dcb(struct i40e_pf *pf)
5293{
5294 struct i40e_hw *hw = &pf->hw;
5295 int err = 0;
5296
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005297 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005298 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005299 goto out;
5300
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005301 /* Get the initial DCB configuration */
5302 err = i40e_init_dcb(hw);
5303 if (!err) {
5304 /* Device/Function is not DCBX capable */
5305 if ((!hw->func_caps.dcb) ||
5306 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5307 dev_info(&pf->pdev->dev,
5308 "DCBX offload is not supported or is disabled for this PF.\n");
5309
5310 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5311 goto out;
5312
5313 } else {
5314 /* When status is not DISABLED then DCBX in FW */
5315 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5316 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005317
5318 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005319 /* Enable DCB tagging only when more than one TC
5320 * or explicitly disable if only one TC
5321 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005322 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5323 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005324 else
5325 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005326 dev_dbg(&pf->pdev->dev,
5327 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005328 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005329 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005330 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005331 "Query for DCB configuration failed, err %s aq_err %s\n",
5332 i40e_stat_str(&pf->hw, err),
5333 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005334 }
5335
5336out:
5337 return err;
5338}
5339#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005340#define SPEED_SIZE 14
5341#define FC_SIZE 8
5342/**
5343 * i40e_print_link_message - print link up or down
5344 * @vsi: the VSI for which link needs a message
5345 */
Matt Jaredc156f852015-08-27 11:42:39 -04005346void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005347{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005348 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005349 char *speed = "Unknown";
5350 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005351 char *fec = "";
5352 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005353
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005354 new_speed = vsi->back->hw.phy.link_info.link_speed;
5355
5356 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005357 return;
5358 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005359 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005360 if (!isup) {
5361 netdev_info(vsi->netdev, "NIC Link is Down\n");
5362 return;
5363 }
5364
Greg Rose148c2d82014-12-11 07:06:27 +00005365 /* Warn user if link speed on NPAR enabled partition is not at
5366 * least 10GB
5367 */
5368 if (vsi->back->hw.func_caps.npar_enable &&
5369 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5370 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5371 netdev_warn(vsi->netdev,
5372 "The partition detected link speed that is less than 10Gbps\n");
5373
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005374 switch (vsi->back->hw.phy.link_info.link_speed) {
5375 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005376 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005377 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005378 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005379 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005380 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005381 case I40E_LINK_SPEED_25GB:
5382 speed = "25 G";
5383 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005384 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005385 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005386 break;
5387 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005388 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005389 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005390 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005391 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005392 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005393 default:
5394 break;
5395 }
5396
5397 switch (vsi->back->hw.fc.current_mode) {
5398 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005399 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005400 break;
5401 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005402 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005403 break;
5404 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005405 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005406 break;
5407 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005408 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005409 break;
5410 }
5411
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005412 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5413 fec = ", FEC: None";
5414 an = ", Autoneg: False";
5415
5416 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5417 an = ", Autoneg: True";
5418
5419 if (vsi->back->hw.phy.link_info.fec_info &
5420 I40E_AQ_CONFIG_FEC_KR_ENA)
5421 fec = ", FEC: CL74 FC-FEC/BASE-R";
5422 else if (vsi->back->hw.phy.link_info.fec_info &
5423 I40E_AQ_CONFIG_FEC_RS_ENA)
5424 fec = ", FEC: CL108 RS-FEC";
5425 }
5426
5427 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5428 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005429}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005430
5431/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005432 * i40e_up_complete - Finish the last steps of bringing up a connection
5433 * @vsi: the VSI being configured
5434 **/
5435static int i40e_up_complete(struct i40e_vsi *vsi)
5436{
5437 struct i40e_pf *pf = vsi->back;
5438 int err;
5439
5440 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5441 i40e_vsi_configure_msix(vsi);
5442 else
5443 i40e_configure_msi_and_legacy(vsi);
5444
5445 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005446 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005447 if (err)
5448 return err;
5449
5450 clear_bit(__I40E_DOWN, &vsi->state);
5451 i40e_napi_enable_all(vsi);
5452 i40e_vsi_enable_irq(vsi);
5453
5454 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5455 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005456 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005457 netif_tx_start_all_queues(vsi->netdev);
5458 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005459 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005460 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005461 /* need to check for qualified module here*/
5462 if ((pf->hw.phy.link_info.link_info &
5463 I40E_AQ_MEDIA_AVAILABLE) &&
5464 (!(pf->hw.phy.link_info.an_info &
5465 I40E_AQ_QUALIFIED_MODULE)))
5466 netdev_err(vsi->netdev,
5467 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005468 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005469
5470 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005471 if (vsi->type == I40E_VSI_FDIR) {
5472 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005473 pf->fd_add_err = 0;
5474 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005475 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005476 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005477
5478 /* On the next run of the service_task, notify any clients of the new
5479 * opened netdev
5480 */
5481 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005482 i40e_service_event_schedule(pf);
5483
5484 return 0;
5485}
5486
5487/**
5488 * i40e_vsi_reinit_locked - Reset the VSI
5489 * @vsi: the VSI being configured
5490 *
5491 * Rebuild the ring structs after some configuration
5492 * has changed, e.g. MTU size.
5493 **/
5494static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5495{
5496 struct i40e_pf *pf = vsi->back;
5497
5498 WARN_ON(in_interrupt());
5499 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5500 usleep_range(1000, 2000);
5501 i40e_down(vsi);
5502
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503 i40e_up(vsi);
5504 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5505}
5506
5507/**
5508 * i40e_up - Bring the connection back up after being down
5509 * @vsi: the VSI being configured
5510 **/
5511int i40e_up(struct i40e_vsi *vsi)
5512{
5513 int err;
5514
5515 err = i40e_vsi_configure(vsi);
5516 if (!err)
5517 err = i40e_up_complete(vsi);
5518
5519 return err;
5520}
5521
5522/**
5523 * i40e_down - Shutdown the connection processing
5524 * @vsi: the VSI being stopped
5525 **/
5526void i40e_down(struct i40e_vsi *vsi)
5527{
5528 int i;
5529
5530 /* It is assumed that the caller of this function
5531 * sets the vsi->state __I40E_DOWN bit.
5532 */
5533 if (vsi->netdev) {
5534 netif_carrier_off(vsi->netdev);
5535 netif_tx_disable(vsi->netdev);
5536 }
5537 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005538 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005539 i40e_napi_disable_all(vsi);
5540
5541 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005542 i40e_clean_tx_ring(vsi->tx_rings[i]);
5543 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005544 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546}
5547
5548/**
5549 * i40e_setup_tc - configure multiple traffic classes
5550 * @netdev: net device to configure
5551 * @tc: number of traffic classes to enable
5552 **/
5553static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5554{
5555 struct i40e_netdev_priv *np = netdev_priv(netdev);
5556 struct i40e_vsi *vsi = np->vsi;
5557 struct i40e_pf *pf = vsi->back;
5558 u8 enabled_tc = 0;
5559 int ret = -EINVAL;
5560 int i;
5561
5562 /* Check if DCB enabled to continue */
5563 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5564 netdev_info(netdev, "DCB is not enabled for adapter\n");
5565 goto exit;
5566 }
5567
5568 /* Check if MFP enabled */
5569 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5570 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5571 goto exit;
5572 }
5573
5574 /* Check whether tc count is within enabled limit */
5575 if (tc > i40e_pf_get_num_tc(pf)) {
5576 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5577 goto exit;
5578 }
5579
5580 /* Generate TC map for number of tc requested */
5581 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005582 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583
5584 /* Requesting same TC configuration as already enabled */
5585 if (enabled_tc == vsi->tc_config.enabled_tc)
5586 return 0;
5587
5588 /* Quiesce VSI queues */
5589 i40e_quiesce_vsi(vsi);
5590
5591 /* Configure VSI for enabled TCs */
5592 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5593 if (ret) {
5594 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5595 vsi->seid);
5596 goto exit;
5597 }
5598
5599 /* Unquiesce VSI */
5600 i40e_unquiesce_vsi(vsi);
5601
5602exit:
5603 return ret;
5604}
5605
John Fastabende4c67342016-02-16 21:16:15 -08005606#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005607int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5608 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005609#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005610static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5611 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005612#endif
5613{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005614 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005615 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005616
5617 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5618
5619 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005620}
5621
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005622/**
5623 * i40e_open - Called when a network interface is made active
5624 * @netdev: network interface device structure
5625 *
5626 * The open entry point is called when a network interface is made
5627 * active by the system (IFF_UP). At this point all resources needed
5628 * for transmit and receive operations are allocated, the interrupt
5629 * handler is registered with the OS, the netdev watchdog subtask is
5630 * enabled, and the stack is notified that the interface is ready.
5631 *
5632 * Returns 0 on success, negative value on failure
5633 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005634int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005635{
5636 struct i40e_netdev_priv *np = netdev_priv(netdev);
5637 struct i40e_vsi *vsi = np->vsi;
5638 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005639 int err;
5640
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005641 /* disallow open during test or if eeprom is broken */
5642 if (test_bit(__I40E_TESTING, &pf->state) ||
5643 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005644 return -EBUSY;
5645
5646 netif_carrier_off(netdev);
5647
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005648 err = i40e_vsi_open(vsi);
5649 if (err)
5650 return err;
5651
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005652 /* configure global TSO hardware offload settings */
5653 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5654 TCP_FLAG_FIN) >> 16);
5655 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5656 TCP_FLAG_FIN |
5657 TCP_FLAG_CWR) >> 16);
5658 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5659
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005660 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005661
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005662 return 0;
5663}
5664
5665/**
5666 * i40e_vsi_open -
5667 * @vsi: the VSI to open
5668 *
5669 * Finish initialization of the VSI.
5670 *
5671 * Returns 0 on success, negative value on failure
5672 **/
5673int i40e_vsi_open(struct i40e_vsi *vsi)
5674{
5675 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005676 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005677 int err;
5678
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005679 /* allocate descriptors */
5680 err = i40e_vsi_setup_tx_resources(vsi);
5681 if (err)
5682 goto err_setup_tx;
5683 err = i40e_vsi_setup_rx_resources(vsi);
5684 if (err)
5685 goto err_setup_rx;
5686
5687 err = i40e_vsi_configure(vsi);
5688 if (err)
5689 goto err_setup_rx;
5690
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005691 if (vsi->netdev) {
5692 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5693 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5694 err = i40e_vsi_request_irq(vsi, int_name);
5695 if (err)
5696 goto err_setup_rx;
5697
5698 /* Notify the stack of the actual queue counts. */
5699 err = netif_set_real_num_tx_queues(vsi->netdev,
5700 vsi->num_queue_pairs);
5701 if (err)
5702 goto err_set_queues;
5703
5704 err = netif_set_real_num_rx_queues(vsi->netdev,
5705 vsi->num_queue_pairs);
5706 if (err)
5707 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005708
5709 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005710 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005711 dev_driver_string(&pf->pdev->dev),
5712 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005713 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005714
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005715 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005716 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005717 goto err_setup_rx;
5718 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005720 err = i40e_up_complete(vsi);
5721 if (err)
5722 goto err_up_complete;
5723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005724 return 0;
5725
5726err_up_complete:
5727 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005728err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005729 i40e_vsi_free_irq(vsi);
5730err_setup_rx:
5731 i40e_vsi_free_rx_resources(vsi);
5732err_setup_tx:
5733 i40e_vsi_free_tx_resources(vsi);
5734 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005735 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005736
5737 return err;
5738}
5739
5740/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005741 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005742 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005743 *
5744 * This function destroys the hlist where all the Flow Director
5745 * filters were saved.
5746 **/
5747static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5748{
5749 struct i40e_fdir_filter *filter;
5750 struct hlist_node *node2;
5751
5752 hlist_for_each_entry_safe(filter, node2,
5753 &pf->fdir_filter_list, fdir_node) {
5754 hlist_del(&filter->fdir_node);
5755 kfree(filter);
5756 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005757
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005758 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005759 pf->fd_tcp4_filter_cnt = 0;
5760 pf->fd_udp4_filter_cnt = 0;
5761 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005762
5763 /* Reprogram the default input set for TCP/IPv4 */
5764 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5765 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5766 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5767
5768 /* Reprogram the default input set for UDP/IPv4 */
5769 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5770 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5771 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5772
5773 /* Reprogram the default input set for SCTP/IPv4 */
5774 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5775 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5776 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5777
5778 /* Reprogram the default input set for Other/IPv4 */
5779 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5780 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005781}
5782
5783/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005784 * i40e_close - Disables a network interface
5785 * @netdev: network interface device structure
5786 *
5787 * The close entry point is called when an interface is de-activated
5788 * by the OS. The hardware is still under the driver's control, but
5789 * this netdev interface is disabled.
5790 *
5791 * Returns 0, this is not allowed to fail
5792 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005793int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005794{
5795 struct i40e_netdev_priv *np = netdev_priv(netdev);
5796 struct i40e_vsi *vsi = np->vsi;
5797
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005798 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005799
5800 return 0;
5801}
5802
5803/**
5804 * i40e_do_reset - Start a PF or Core Reset sequence
5805 * @pf: board private structure
5806 * @reset_flags: which reset is requested
5807 *
5808 * The essential difference in resets is that the PF Reset
5809 * doesn't clear the packet buffers, doesn't reset the PE
5810 * firmware, and doesn't bother the other PFs on the chip.
5811 **/
5812void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5813{
5814 u32 val;
5815
5816 WARN_ON(in_interrupt());
5817
Mitch Williams263fc482014-04-23 04:50:11 +00005818
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005819 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005820 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005821
5822 /* Request a Global Reset
5823 *
5824 * This will start the chip's countdown to the actual full
5825 * chip reset event, and a warning interrupt to be sent
5826 * to all PFs, including the requestor. Our handler
5827 * for the warning interrupt will deal with the shutdown
5828 * and recovery of the switch setup.
5829 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005830 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005831 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5832 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5833 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5834
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005835 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005836
5837 /* Request a Core Reset
5838 *
5839 * Same as Global Reset, except does *not* include the MAC/PHY
5840 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005841 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005842 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5843 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5844 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5845 i40e_flush(&pf->hw);
5846
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005847 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005848
5849 /* Request a PF Reset
5850 *
5851 * Resets only the PF-specific registers
5852 *
5853 * This goes directly to the tear-down and rebuild of
5854 * the switch, since we need to do all the recovery as
5855 * for the Core Reset.
5856 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005857 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005858 i40e_handle_reset_warning(pf);
5859
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005860 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005861 int v;
5862
5863 /* Find the VSI(s) that requested a re-init */
5864 dev_info(&pf->pdev->dev,
5865 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005866 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005867 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005868
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005869 if (vsi != NULL &&
5870 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5871 i40e_vsi_reinit_locked(pf->vsi[v]);
5872 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5873 }
5874 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005875 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005876 int v;
5877
5878 /* Find the VSI(s) that needs to be brought down */
5879 dev_info(&pf->pdev->dev, "VSI down requested\n");
5880 for (v = 0; v < pf->num_alloc_vsi; v++) {
5881 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005882
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005883 if (vsi != NULL &&
5884 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5885 set_bit(__I40E_DOWN, &vsi->state);
5886 i40e_down(vsi);
5887 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5888 }
5889 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005890 } else {
5891 dev_info(&pf->pdev->dev,
5892 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005893 }
5894}
5895
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005896#ifdef CONFIG_I40E_DCB
5897/**
5898 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5899 * @pf: board private structure
5900 * @old_cfg: current DCB config
5901 * @new_cfg: new DCB config
5902 **/
5903bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5904 struct i40e_dcbx_config *old_cfg,
5905 struct i40e_dcbx_config *new_cfg)
5906{
5907 bool need_reconfig = false;
5908
5909 /* Check if ETS configuration has changed */
5910 if (memcmp(&new_cfg->etscfg,
5911 &old_cfg->etscfg,
5912 sizeof(new_cfg->etscfg))) {
5913 /* If Priority Table has changed reconfig is needed */
5914 if (memcmp(&new_cfg->etscfg.prioritytable,
5915 &old_cfg->etscfg.prioritytable,
5916 sizeof(new_cfg->etscfg.prioritytable))) {
5917 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005918 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005919 }
5920
5921 if (memcmp(&new_cfg->etscfg.tcbwtable,
5922 &old_cfg->etscfg.tcbwtable,
5923 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005924 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005925
5926 if (memcmp(&new_cfg->etscfg.tsatable,
5927 &old_cfg->etscfg.tsatable,
5928 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005929 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005930 }
5931
5932 /* Check if PFC configuration has changed */
5933 if (memcmp(&new_cfg->pfc,
5934 &old_cfg->pfc,
5935 sizeof(new_cfg->pfc))) {
5936 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005937 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005938 }
5939
5940 /* Check if APP Table has changed */
5941 if (memcmp(&new_cfg->app,
5942 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005943 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005944 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005945 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005946 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005947
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005948 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005949 return need_reconfig;
5950}
5951
5952/**
5953 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5954 * @pf: board private structure
5955 * @e: event info posted on ARQ
5956 **/
5957static int i40e_handle_lldp_event(struct i40e_pf *pf,
5958 struct i40e_arq_event_info *e)
5959{
5960 struct i40e_aqc_lldp_get_mib *mib =
5961 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5962 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005963 struct i40e_dcbx_config tmp_dcbx_cfg;
5964 bool need_reconfig = false;
5965 int ret = 0;
5966 u8 type;
5967
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005968 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005969 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005970 return ret;
5971
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005972 /* Ignore if event is not for Nearest Bridge */
5973 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5974 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005975 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005976 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5977 return ret;
5978
5979 /* Check MIB Type and return if event for Remote MIB update */
5980 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005981 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005982 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005983 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5984 /* Update the remote cached instance and return */
5985 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5986 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5987 &hw->remote_dcbx_config);
5988 goto exit;
5989 }
5990
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005991 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005992 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005993
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005994 /* Reset the old DCBx configuration data */
5995 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005996 /* Get updated DCBX data from firmware */
5997 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005998 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005999 dev_info(&pf->pdev->dev,
6000 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
6001 i40e_stat_str(&pf->hw, ret),
6002 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006003 goto exit;
6004 }
6005
6006 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006007 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
6008 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006009 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006010 goto exit;
6011 }
6012
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006013 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
6014 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006015
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006016 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006017
6018 if (!need_reconfig)
6019 goto exit;
6020
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006021 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00006022 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00006023 pf->flags |= I40E_FLAG_DCB_ENABLED;
6024 else
6025 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6026
Neerav Parikh69129dc2014-11-12 00:18:46 +00006027 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006028 /* Reconfiguration needed quiesce all VSIs */
6029 i40e_pf_quiesce_all_vsi(pf);
6030
6031 /* Changes in configuration update VEB/VSI */
6032 i40e_dcb_reconfigure(pf);
6033
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006034 ret = i40e_resume_port_tx(pf);
6035
Neerav Parikh69129dc2014-11-12 00:18:46 +00006036 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006037 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00006038 if (ret)
6039 goto exit;
6040
Neerav Parikh3fe06f42016-02-17 16:12:15 -08006041 /* Wait for the PF's queues to be disabled */
6042 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006043 if (ret) {
6044 /* Schedule PF reset to recover */
6045 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6046 i40e_service_event_schedule(pf);
6047 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00006048 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08006049 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
6050 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00006051 }
6052
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006053exit:
6054 return ret;
6055}
6056#endif /* CONFIG_I40E_DCB */
6057
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006058/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006059 * i40e_do_reset_safe - Protected reset path for userland calls.
6060 * @pf: board private structure
6061 * @reset_flags: which reset is requested
6062 *
6063 **/
6064void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
6065{
6066 rtnl_lock();
6067 i40e_do_reset(pf, reset_flags);
6068 rtnl_unlock();
6069}
6070
6071/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006072 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
6073 * @pf: board private structure
6074 * @e: event info posted on ARQ
6075 *
6076 * Handler for LAN Queue Overflow Event generated by the firmware for PF
6077 * and VF queues
6078 **/
6079static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
6080 struct i40e_arq_event_info *e)
6081{
6082 struct i40e_aqc_lan_overflow *data =
6083 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
6084 u32 queue = le32_to_cpu(data->prtdcb_rupto);
6085 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
6086 struct i40e_hw *hw = &pf->hw;
6087 struct i40e_vf *vf;
6088 u16 vf_id;
6089
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006090 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
6091 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006092
6093 /* Queue belongs to VF, find the VF and issue VF reset */
6094 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
6095 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
6096 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
6097 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
6098 vf_id -= hw->func_caps.vf_base_id;
6099 vf = &pf->vf[vf_id];
6100 i40e_vc_notify_vf_reset(vf);
6101 /* Allow VF to process pending reset notification */
6102 msleep(20);
6103 i40e_reset_vf(vf, false);
6104 }
6105}
6106
6107/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006108 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
6109 * @pf: board private structure
6110 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006111u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006112{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006113 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006114
6115 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6116 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
6117 return fcnt_prog;
6118}
6119
6120/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006121 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006122 * @pf: board private structure
6123 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006124u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006125{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006126 u32 val, fcnt_prog;
6127
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006128 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6129 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6130 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6131 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6132 return fcnt_prog;
6133}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006134
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006135/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006136 * i40e_get_global_fd_count - Get total FD filters programmed on device
6137 * @pf: board private structure
6138 **/
6139u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6140{
6141 u32 val, fcnt_prog;
6142
6143 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6144 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6145 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6146 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6147 return fcnt_prog;
6148}
6149
6150/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006151 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6152 * @pf: board private structure
6153 **/
6154void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6155{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006156 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006157 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006158 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006159
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006160 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6161 return;
6162
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006163 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6164 * to re-enable
6165 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006166 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006167 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006168 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6169 (pf->fd_add_err == 0) ||
6170 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006171 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006172 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6173 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006174 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6175 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 +00006176 }
6177 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006178
6179 /* Wait for some more space to be available to turn on ATR. We also
6180 * must check that no existing ntuple rules for TCP are in effect
6181 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006182 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6183 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006184 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006185 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006186 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006187 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006188 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 +00006189 }
6190 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006191
6192 /* if hw had a problem adding a filter, delete it */
6193 if (pf->fd_inv > 0) {
6194 hlist_for_each_entry_safe(filter, node,
6195 &pf->fdir_filter_list, fdir_node) {
6196 if (filter->fd_id == pf->fd_inv) {
6197 hlist_del(&filter->fdir_node);
6198 kfree(filter);
6199 pf->fdir_pf_active_filters--;
6200 }
6201 }
6202 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006203}
6204
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006205#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006206#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006207/**
6208 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6209 * @pf: board private structure
6210 **/
6211static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6212{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006213 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006214 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006215 bool disable_atr = false;
6216 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006217 int reg;
6218
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006219 if (!time_after(jiffies, pf->fd_flush_timestamp +
6220 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6221 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006222
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006223 /* If the flush is happening too quick and we have mostly SB rules we
6224 * should not re-enable ATR for some time.
6225 */
6226 min_flush_time = pf->fd_flush_timestamp +
6227 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6228 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006229
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006230 if (!(time_after(jiffies, min_flush_time)) &&
6231 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6232 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6233 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6234 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006235 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006236
6237 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006238 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006239 /* flush all filters */
6240 wr32(&pf->hw, I40E_PFQF_CTL_1,
6241 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6242 i40e_flush(&pf->hw);
6243 pf->fd_flush_cnt++;
6244 pf->fd_add_err = 0;
6245 do {
6246 /* Check FD flush status every 5-6msec */
6247 usleep_range(5000, 6000);
6248 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6249 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6250 break;
6251 } while (flush_wait_retry--);
6252 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6253 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6254 } else {
6255 /* replay sideband filters */
6256 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006257 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006258 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006259 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6260 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6261 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6262 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006263}
6264
6265/**
6266 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6267 * @pf: board private structure
6268 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006269u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006270{
6271 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6272}
6273
6274/* We can see up to 256 filter programming desc in transit if the filters are
6275 * being applied really fast; before we see the first
6276 * filter miss error on Rx queue 0. Accumulating enough error messages before
6277 * reacting will make sure we don't cause flush too often.
6278 */
6279#define I40E_MAX_FD_PROGRAM_ERROR 256
6280
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006281/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006282 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6283 * @pf: board private structure
6284 **/
6285static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6286{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006288 /* if interface is down do nothing */
6289 if (test_bit(__I40E_DOWN, &pf->state))
6290 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006291
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006292 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006293 i40e_fdir_flush_and_replay(pf);
6294
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006295 i40e_fdir_check_and_reenable(pf);
6296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006297}
6298
6299/**
6300 * i40e_vsi_link_event - notify VSI of a link event
6301 * @vsi: vsi to be notified
6302 * @link_up: link up or down
6303 **/
6304static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6305{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006306 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006307 return;
6308
6309 switch (vsi->type) {
6310 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006311#ifdef I40E_FCOE
6312 case I40E_VSI_FCOE:
6313#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 if (!vsi->netdev || !vsi->netdev_registered)
6315 break;
6316
6317 if (link_up) {
6318 netif_carrier_on(vsi->netdev);
6319 netif_tx_wake_all_queues(vsi->netdev);
6320 } else {
6321 netif_carrier_off(vsi->netdev);
6322 netif_tx_stop_all_queues(vsi->netdev);
6323 }
6324 break;
6325
6326 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006327 case I40E_VSI_VMDQ2:
6328 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006329 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006330 case I40E_VSI_MIRROR:
6331 default:
6332 /* there is no notification for other VSIs */
6333 break;
6334 }
6335}
6336
6337/**
6338 * i40e_veb_link_event - notify elements on the veb of a link event
6339 * @veb: veb to be notified
6340 * @link_up: link up or down
6341 **/
6342static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6343{
6344 struct i40e_pf *pf;
6345 int i;
6346
6347 if (!veb || !veb->pf)
6348 return;
6349 pf = veb->pf;
6350
6351 /* depth first... */
6352 for (i = 0; i < I40E_MAX_VEB; i++)
6353 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6354 i40e_veb_link_event(pf->veb[i], link_up);
6355
6356 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006357 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006358 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6359 i40e_vsi_link_event(pf->vsi[i], link_up);
6360}
6361
6362/**
6363 * i40e_link_event - Update netif_carrier status
6364 * @pf: board private structure
6365 **/
6366static void i40e_link_event(struct i40e_pf *pf)
6367{
Mitch Williams320684c2014-10-17 03:14:43 +00006368 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006369 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006370 i40e_status status;
6371 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006372
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006373 /* save off old link status information */
6374 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6375
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006376 /* set this to force the get_link_status call to refresh state */
6377 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006378
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006379 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006380
6381 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006382
6383 /* On success, disable temp link polling */
6384 if (status == I40E_SUCCESS) {
6385 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6386 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6387 } else {
6388 /* Enable link polling temporarily until i40e_get_link_status
6389 * returns I40E_SUCCESS
6390 */
6391 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006392 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6393 status);
6394 return;
6395 }
6396
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006397 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6398 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006399
6400 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006401 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006402 (test_bit(__I40E_DOWN, &vsi->state) ||
6403 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006404 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006405
6406 if (!test_bit(__I40E_DOWN, &vsi->state))
6407 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006408
6409 /* Notify the base of the switch tree connected to
6410 * the link. Floating VEBs are not notified.
6411 */
6412 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6413 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6414 else
Mitch Williams320684c2014-10-17 03:14:43 +00006415 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006416
6417 if (pf->vf)
6418 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006419
6420 if (pf->flags & I40E_FLAG_PTP)
6421 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006422}
6423
6424/**
Shannon Nelson21536712014-10-25 10:35:25 +00006425 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006426 * @pf: board private structure
6427 **/
6428static void i40e_watchdog_subtask(struct i40e_pf *pf)
6429{
6430 int i;
6431
6432 /* if interface is down do nothing */
6433 if (test_bit(__I40E_DOWN, &pf->state) ||
6434 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6435 return;
6436
Shannon Nelson21536712014-10-25 10:35:25 +00006437 /* make sure we don't do these things too often */
6438 if (time_before(jiffies, (pf->service_timer_previous +
6439 pf->service_timer_period)))
6440 return;
6441 pf->service_timer_previous = jiffies;
6442
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006443 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6444 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006445 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006447 /* Update the stats for active netdevs so the network stack
6448 * can look at updated numbers whenever it cares to
6449 */
Mitch Williams505682c2014-05-20 08:01:37 +00006450 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006451 if (pf->vsi[i] && pf->vsi[i]->netdev)
6452 i40e_update_stats(pf->vsi[i]);
6453
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006454 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6455 /* Update the stats for the active switching components */
6456 for (i = 0; i < I40E_MAX_VEB; i++)
6457 if (pf->veb[i])
6458 i40e_update_veb_stats(pf->veb[i]);
6459 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006460
6461 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006462}
6463
6464/**
6465 * i40e_reset_subtask - Set up for resetting the device and driver
6466 * @pf: board private structure
6467 **/
6468static void i40e_reset_subtask(struct i40e_pf *pf)
6469{
6470 u32 reset_flags = 0;
6471
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006472 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006473 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006474 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006475 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6476 }
6477 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006478 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006479 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6480 }
6481 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006482 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006483 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6484 }
6485 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006486 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006487 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6488 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006489 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006490 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006491 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6492 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006493
6494 /* If there's a recovery already waiting, it takes
6495 * precedence before starting a new reset sequence.
6496 */
6497 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6498 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006499 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006500 }
6501
6502 /* If we're already down or resetting, just bail */
6503 if (reset_flags &&
6504 !test_bit(__I40E_DOWN, &pf->state) &&
6505 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6506 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006507
6508unlock:
6509 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006510}
6511
6512/**
6513 * i40e_handle_link_event - Handle link event
6514 * @pf: board private structure
6515 * @e: event info posted on ARQ
6516 **/
6517static void i40e_handle_link_event(struct i40e_pf *pf,
6518 struct i40e_arq_event_info *e)
6519{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006520 struct i40e_aqc_get_link_status *status =
6521 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006522
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006523 /* Do a new status request to re-enable LSE reporting
6524 * and load new status information into the hw struct
6525 * This completely ignores any state information
6526 * in the ARQ event info, instead choosing to always
6527 * issue the AQ update link status command.
6528 */
6529 i40e_link_event(pf);
6530
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006531 /* check for unqualified module, if link is down */
6532 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6533 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6534 (!(status->link_info & I40E_AQ_LINK_UP)))
6535 dev_err(&pf->pdev->dev,
6536 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006537}
6538
6539/**
6540 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6541 * @pf: board private structure
6542 **/
6543static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6544{
6545 struct i40e_arq_event_info event;
6546 struct i40e_hw *hw = &pf->hw;
6547 u16 pending, i = 0;
6548 i40e_status ret;
6549 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006550 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551 u32 val;
6552
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006553 /* Do not run clean AQ when PF reset fails */
6554 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6555 return;
6556
Shannon Nelson86df2422014-05-20 08:01:35 +00006557 /* check for error indications */
6558 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6559 oldval = val;
6560 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006561 if (hw->debug_mask & I40E_DEBUG_AQ)
6562 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006563 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6564 }
6565 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006566 if (hw->debug_mask & I40E_DEBUG_AQ)
6567 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006568 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006569 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006570 }
6571 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006572 if (hw->debug_mask & I40E_DEBUG_AQ)
6573 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006574 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6575 }
6576 if (oldval != val)
6577 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6578
6579 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6580 oldval = val;
6581 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006582 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6583 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006584 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6585 }
6586 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006587 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6588 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006589 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6590 }
6591 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006592 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6593 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006594 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6595 }
6596 if (oldval != val)
6597 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6598
Mitch Williams1001dc32014-11-11 20:02:19 +00006599 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6600 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006601 if (!event.msg_buf)
6602 return;
6603
6604 do {
6605 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006606 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006607 break;
Mitch Williams56497972014-06-04 08:45:18 +00006608 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006609 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6610 break;
6611 }
6612
6613 opcode = le16_to_cpu(event.desc.opcode);
6614 switch (opcode) {
6615
6616 case i40e_aqc_opc_get_link_status:
6617 i40e_handle_link_event(pf, &event);
6618 break;
6619 case i40e_aqc_opc_send_msg_to_pf:
6620 ret = i40e_vc_process_vf_msg(pf,
6621 le16_to_cpu(event.desc.retval),
6622 le32_to_cpu(event.desc.cookie_high),
6623 le32_to_cpu(event.desc.cookie_low),
6624 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006625 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006626 break;
6627 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006628 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006629#ifdef CONFIG_I40E_DCB
6630 rtnl_lock();
6631 ret = i40e_handle_lldp_event(pf, &event);
6632 rtnl_unlock();
6633#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006634 break;
6635 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006636 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006637 i40e_handle_lan_overflow_event(pf, &event);
6638 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006639 case i40e_aqc_opc_send_msg_to_peer:
6640 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6641 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006642 case i40e_aqc_opc_nvm_erase:
6643 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006644 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006645 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6646 "ARQ NVM operation 0x%04x completed\n",
6647 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006648 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006649 default:
6650 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006651 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006652 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653 break;
6654 }
6655 } while (pending && (i++ < pf->adminq_work_limit));
6656
6657 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6658 /* re-enable Admin queue interrupt cause */
6659 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6660 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6661 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6662 i40e_flush(hw);
6663
6664 kfree(event.msg_buf);
6665}
6666
6667/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006668 * i40e_verify_eeprom - make sure eeprom is good to use
6669 * @pf: board private structure
6670 **/
6671static void i40e_verify_eeprom(struct i40e_pf *pf)
6672{
6673 int err;
6674
6675 err = i40e_diag_eeprom_test(&pf->hw);
6676 if (err) {
6677 /* retry in case of garbage read */
6678 err = i40e_diag_eeprom_test(&pf->hw);
6679 if (err) {
6680 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6681 err);
6682 set_bit(__I40E_BAD_EEPROM, &pf->state);
6683 }
6684 }
6685
6686 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6687 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6688 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6689 }
6690}
6691
6692/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006693 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006694 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006695 *
6696 * enable switch loop back or die - no point in a return value
6697 **/
6698static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6699{
6700 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6701 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006702 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006703
6704 ctxt.seid = pf->main_vsi_seid;
6705 ctxt.pf_num = pf->hw.pf_id;
6706 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006707 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6708 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006709 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006710 "couldn't get PF vsi config, err %s aq_err %s\n",
6711 i40e_stat_str(&pf->hw, ret),
6712 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006713 return;
6714 }
6715 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6716 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6717 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6718
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006719 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6720 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006721 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006722 "update vsi switch failed, err %s aq_err %s\n",
6723 i40e_stat_str(&pf->hw, ret),
6724 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006725 }
6726}
6727
6728/**
6729 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006730 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006731 *
6732 * disable switch loop back or die - no point in a return value
6733 **/
6734static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6735{
6736 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6737 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006738 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006739
6740 ctxt.seid = pf->main_vsi_seid;
6741 ctxt.pf_num = pf->hw.pf_id;
6742 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006743 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6744 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006745 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006746 "couldn't get PF vsi config, err %s aq_err %s\n",
6747 i40e_stat_str(&pf->hw, ret),
6748 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006749 return;
6750 }
6751 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6752 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6753 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6754
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006755 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6756 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006757 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006758 "update vsi switch failed, err %s aq_err %s\n",
6759 i40e_stat_str(&pf->hw, ret),
6760 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006761 }
6762}
6763
6764/**
Neerav Parikh51616012015-02-06 08:52:14 +00006765 * i40e_config_bridge_mode - Configure the HW bridge mode
6766 * @veb: pointer to the bridge instance
6767 *
6768 * Configure the loop back mode for the LAN VSI that is downlink to the
6769 * specified HW bridge instance. It is expected this function is called
6770 * when a new HW bridge is instantiated.
6771 **/
6772static void i40e_config_bridge_mode(struct i40e_veb *veb)
6773{
6774 struct i40e_pf *pf = veb->pf;
6775
Shannon Nelson6dec1012015-09-28 14:12:30 -04006776 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6777 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6778 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006779 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6780 i40e_disable_pf_switch_lb(pf);
6781 else
6782 i40e_enable_pf_switch_lb(pf);
6783}
6784
6785/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006786 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6787 * @veb: pointer to the VEB instance
6788 *
6789 * This is a recursive function that first builds the attached VSIs then
6790 * recurses in to build the next layer of VEB. We track the connections
6791 * through our own index numbers because the seid's from the HW could
6792 * change across the reset.
6793 **/
6794static int i40e_reconstitute_veb(struct i40e_veb *veb)
6795{
6796 struct i40e_vsi *ctl_vsi = NULL;
6797 struct i40e_pf *pf = veb->pf;
6798 int v, veb_idx;
6799 int ret;
6800
6801 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006802 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006803 if (pf->vsi[v] &&
6804 pf->vsi[v]->veb_idx == veb->idx &&
6805 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6806 ctl_vsi = pf->vsi[v];
6807 break;
6808 }
6809 }
6810 if (!ctl_vsi) {
6811 dev_info(&pf->pdev->dev,
6812 "missing owner VSI for veb_idx %d\n", veb->idx);
6813 ret = -ENOENT;
6814 goto end_reconstitute;
6815 }
6816 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6817 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6818 ret = i40e_add_vsi(ctl_vsi);
6819 if (ret) {
6820 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006821 "rebuild of veb_idx %d owner VSI failed: %d\n",
6822 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006823 goto end_reconstitute;
6824 }
6825 i40e_vsi_reset_stats(ctl_vsi);
6826
6827 /* create the VEB in the switch and move the VSI onto the VEB */
6828 ret = i40e_add_veb(veb, ctl_vsi);
6829 if (ret)
6830 goto end_reconstitute;
6831
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006832 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6833 veb->bridge_mode = BRIDGE_MODE_VEB;
6834 else
6835 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006836 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006839 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006840 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6841 continue;
6842
6843 if (pf->vsi[v]->veb_idx == veb->idx) {
6844 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006845
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 vsi->uplink_seid = veb->seid;
6847 ret = i40e_add_vsi(vsi);
6848 if (ret) {
6849 dev_info(&pf->pdev->dev,
6850 "rebuild of vsi_idx %d failed: %d\n",
6851 v, ret);
6852 goto end_reconstitute;
6853 }
6854 i40e_vsi_reset_stats(vsi);
6855 }
6856 }
6857
6858 /* create any VEBs attached to this VEB - RECURSION */
6859 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6860 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6861 pf->veb[veb_idx]->uplink_seid = veb->seid;
6862 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6863 if (ret)
6864 break;
6865 }
6866 }
6867
6868end_reconstitute:
6869 return ret;
6870}
6871
6872/**
6873 * i40e_get_capabilities - get info about the HW
6874 * @pf: the PF struct
6875 **/
6876static int i40e_get_capabilities(struct i40e_pf *pf)
6877{
6878 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6879 u16 data_size;
6880 int buf_len;
6881 int err;
6882
6883 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6884 do {
6885 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6886 if (!cap_buf)
6887 return -ENOMEM;
6888
6889 /* this loads the data into the hw struct for us */
6890 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6891 &data_size,
6892 i40e_aqc_opc_list_func_capabilities,
6893 NULL);
6894 /* data loaded, buffer no longer needed */
6895 kfree(cap_buf);
6896
6897 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6898 /* retry with a larger buffer */
6899 buf_len = data_size;
6900 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6901 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006902 "capability discovery failed, err %s aq_err %s\n",
6903 i40e_stat_str(&pf->hw, err),
6904 i40e_aq_str(&pf->hw,
6905 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006906 return -ENODEV;
6907 }
6908 } while (err);
6909
6910 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6911 dev_info(&pf->pdev->dev,
6912 "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",
6913 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6914 pf->hw.func_caps.num_msix_vectors,
6915 pf->hw.func_caps.num_msix_vectors_vf,
6916 pf->hw.func_caps.fd_filters_guaranteed,
6917 pf->hw.func_caps.fd_filters_best_effort,
6918 pf->hw.func_caps.num_tx_qp,
6919 pf->hw.func_caps.num_vsis);
6920
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006921#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6922 + pf->hw.func_caps.num_vfs)
6923 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6924 dev_info(&pf->pdev->dev,
6925 "got num_vsis %d, setting num_vsis to %d\n",
6926 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6927 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6928 }
6929
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006930 return 0;
6931}
6932
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006933static int i40e_vsi_clear(struct i40e_vsi *vsi);
6934
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006936 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937 * @pf: board private structure
6938 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006939static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006940{
6941 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006942
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006943 /* quick workaround for an NVM issue that leaves a critical register
6944 * uninitialized
6945 */
6946 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6947 static const u32 hkey[] = {
6948 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6949 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6950 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6951 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006952 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006953
6954 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6955 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6956 }
6957
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006958 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 return;
6960
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006961 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006962 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006963
6964 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006965 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006966 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6967 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006968 if (!vsi) {
6969 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006970 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6971 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006972 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006974
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006975 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006976}
6977
6978/**
6979 * i40e_fdir_teardown - release the Flow Director resources
6980 * @pf: board private structure
6981 **/
6982static void i40e_fdir_teardown(struct i40e_pf *pf)
6983{
Alexander Duyck4b816442016-10-11 15:26:53 -07006984 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006985
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006986 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006987 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6988 if (vsi)
6989 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990}
6991
6992/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006993 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006994 * @pf: board private structure
6995 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006996 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006997 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006998static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00007001 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007002 u32 v;
7003
7004 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
7005 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007006 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08007007 if (i40e_check_asq_alive(&pf->hw))
7008 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007009
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007010 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007011
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007012 /* quiesce the VSIs and their queues that are not already DOWN */
7013 i40e_pf_quiesce_all_vsi(pf);
7014
Mitch Williams505682c2014-05-20 08:01:37 +00007015 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007016 if (pf->vsi[v])
7017 pf->vsi[v]->seid = 0;
7018 }
7019
7020 i40e_shutdown_adminq(&pf->hw);
7021
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007022 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00007023 if (hw->hmc.hmc_obj) {
7024 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007025 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00007026 dev_warn(&pf->pdev->dev,
7027 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007028 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007029}
7030
7031/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007032 * i40e_send_version - update firmware with driver version
7033 * @pf: PF struct
7034 */
7035static void i40e_send_version(struct i40e_pf *pf)
7036{
7037 struct i40e_driver_version dv;
7038
7039 dv.major_version = DRV_VERSION_MAJOR;
7040 dv.minor_version = DRV_VERSION_MINOR;
7041 dv.build_version = DRV_VERSION_BUILD;
7042 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00007043 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007044 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7045}
7046
7047/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00007048 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007049 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007050 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007051 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007052static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007053{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007054 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007055 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007056 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007057 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007058 u32 v;
7059
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007060 /* Now we wait for GRST to settle out.
7061 * We don't have to delete the VEBs or VSIs from the hw switch
7062 * because the reset will make them disappear.
7063 */
7064 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00007065 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007066 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007067 set_bit(__I40E_RESET_FAILED, &pf->state);
7068 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00007069 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007070 pf->pfr_count++;
7071
7072 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007073 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007074 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007075
7076 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
7077 ret = i40e_init_adminq(&pf->hw);
7078 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007079 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
7080 i40e_stat_str(&pf->hw, ret),
7081 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007082 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007083 }
7084
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007085 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00007086 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007087 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00007088
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00007089 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007090 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007091 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007092 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007094 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7095 hw->func_caps.num_rx_qp,
7096 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7097 if (ret) {
7098 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
7099 goto end_core_reset;
7100 }
7101 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7102 if (ret) {
7103 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
7104 goto end_core_reset;
7105 }
7106
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007107#ifdef CONFIG_I40E_DCB
7108 ret = i40e_init_pf_dcb(pf);
7109 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00007110 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
7111 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
7112 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007113 }
7114#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07007115#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04007116 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08007117
Vasu Dev38e00432014-08-01 13:27:03 -07007118#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007120 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007121 if (ret)
7122 goto end_core_reset;
7123
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007124 /* The driver only wants link up/down and module qualification
7125 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007126 */
7127 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007128 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007129 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007130 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007131 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007132 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7133 i40e_stat_str(&pf->hw, ret),
7134 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007135
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007136 /* make sure our flow control settings are restored */
7137 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7138 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007139 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7140 i40e_stat_str(&pf->hw, ret),
7141 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007142
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007143 /* Rebuild the VSIs and VEBs that existed before reset.
7144 * They are still in our local switch element arrays, so only
7145 * need to rebuild the switch model in the HW.
7146 *
7147 * If there were VEBs but the reconstitution failed, we'll try
7148 * try to recover minimal use by getting the basic PF VSI working.
7149 */
7150 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007151 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007152 /* find the one VEB connected to the MAC, and find orphans */
7153 for (v = 0; v < I40E_MAX_VEB; v++) {
7154 if (!pf->veb[v])
7155 continue;
7156
7157 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7158 pf->veb[v]->uplink_seid == 0) {
7159 ret = i40e_reconstitute_veb(pf->veb[v]);
7160
7161 if (!ret)
7162 continue;
7163
7164 /* If Main VEB failed, we're in deep doodoo,
7165 * so give up rebuilding the switch and set up
7166 * for minimal rebuild of PF VSI.
7167 * If orphan failed, we'll report the error
7168 * but try to keep going.
7169 */
7170 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7171 dev_info(&pf->pdev->dev,
7172 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7173 ret);
7174 pf->vsi[pf->lan_vsi]->uplink_seid
7175 = pf->mac_seid;
7176 break;
7177 } else if (pf->veb[v]->uplink_seid == 0) {
7178 dev_info(&pf->pdev->dev,
7179 "rebuild of orphan VEB failed: %d\n",
7180 ret);
7181 }
7182 }
7183 }
7184 }
7185
7186 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007187 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007188 /* no VEB, so rebuild only the Main VSI */
7189 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7190 if (ret) {
7191 dev_info(&pf->pdev->dev,
7192 "rebuild of Main VSI failed: %d\n", ret);
7193 goto end_core_reset;
7194 }
7195 }
7196
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007197 /* Reconfigure hardware for allowing smaller MSS in the case
7198 * of TSO, so that we avoid the MDD being fired and causing
7199 * a reset in the case of small MSS+TSO.
7200 */
7201#define I40E_REG_MSS 0x000E64DC
7202#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7203#define I40E_64BYTE_MSS 0x400000
7204 val = rd32(hw, I40E_REG_MSS);
7205 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7206 val &= ~I40E_REG_MSS_MIN_MASK;
7207 val |= I40E_64BYTE_MSS;
7208 wr32(hw, I40E_REG_MSS, val);
7209 }
7210
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007211 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007212 msleep(75);
7213 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7214 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007215 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7216 i40e_stat_str(&pf->hw, ret),
7217 i40e_aq_str(&pf->hw,
7218 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007219 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007220 /* reinit the misc interrupt */
7221 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7222 ret = i40e_setup_misc_vector(pf);
7223
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007224 /* Add a filter to drop all Flow control frames from any VSI from being
7225 * transmitted. By doing so we stop a malicious VF from sending out
7226 * PAUSE or PFC frames and potentially controlling traffic for other
7227 * PF/VF VSIs.
7228 * The FW can still send Flow control frames if enabled.
7229 */
7230 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7231 pf->main_vsi_seid);
7232
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007233 /* restart the VSIs that were rebuilt and running before the reset */
7234 i40e_pf_unquiesce_all_vsi(pf);
7235
Mitch Williams69f64b22014-02-13 03:48:46 -08007236 if (pf->num_alloc_vfs) {
7237 for (v = 0; v < pf->num_alloc_vfs; v++)
7238 i40e_reset_vf(&pf->vf[v], true);
7239 }
7240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007241 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007242 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007243
7244end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007245 clear_bit(__I40E_RESET_FAILED, &pf->state);
7246clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007247 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7248}
7249
7250/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007251 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007252 * @pf: board private structure
7253 *
7254 * Close up the VFs and other things in prep for a Core Reset,
7255 * then get ready to rebuild the world.
7256 **/
7257static void i40e_handle_reset_warning(struct i40e_pf *pf)
7258{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007259 i40e_prep_for_reset(pf);
7260 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007261}
7262
7263/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007264 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007265 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007266 *
7267 * Called from the MDD irq handler to identify possibly malicious vfs
7268 **/
7269static void i40e_handle_mdd_event(struct i40e_pf *pf)
7270{
7271 struct i40e_hw *hw = &pf->hw;
7272 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007273 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007274 struct i40e_vf *vf;
7275 u32 reg;
7276 int i;
7277
7278 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7279 return;
7280
7281 /* find what triggered the MDD event */
7282 reg = rd32(hw, I40E_GL_MDET_TX);
7283 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007284 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7285 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007286 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007287 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007288 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007289 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007290 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7291 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7292 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007293 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007294 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 +00007295 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007296 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7297 mdd_detected = true;
7298 }
7299 reg = rd32(hw, I40E_GL_MDET_RX);
7300 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007301 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7302 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007303 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007304 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007305 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7306 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7307 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007308 if (netif_msg_rx_err(pf))
7309 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7310 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007311 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7312 mdd_detected = true;
7313 }
7314
Neerav Parikhdf430b12014-06-04 01:23:15 +00007315 if (mdd_detected) {
7316 reg = rd32(hw, I40E_PF_MDET_TX);
7317 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7318 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007319 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007320 pf_mdd_detected = true;
7321 }
7322 reg = rd32(hw, I40E_PF_MDET_RX);
7323 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7324 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007325 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007326 pf_mdd_detected = true;
7327 }
7328 /* Queue belongs to the PF, initiate a reset */
7329 if (pf_mdd_detected) {
7330 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7331 i40e_service_event_schedule(pf);
7332 }
7333 }
7334
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007335 /* see if one of the VFs needs its hand slapped */
7336 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7337 vf = &(pf->vf[i]);
7338 reg = rd32(hw, I40E_VP_MDET_TX(i));
7339 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7340 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7341 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007342 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7343 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007344 }
7345
7346 reg = rd32(hw, I40E_VP_MDET_RX(i));
7347 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7348 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7349 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007350 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7351 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007352 }
7353
7354 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7355 dev_info(&pf->pdev->dev,
7356 "Too many MDD events on VF %d, disabled\n", i);
7357 dev_info(&pf->pdev->dev,
7358 "Use PF Control I/F to re-enable the VF\n");
7359 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7360 }
7361 }
7362
7363 /* re-enable mdd interrupt cause */
7364 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7365 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7366 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7367 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7368 i40e_flush(hw);
7369}
7370
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007371/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007372 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007373 * @pf: board private structure
7374 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007375static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007376{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007377 struct i40e_hw *hw = &pf->hw;
7378 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007379 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007380 int i;
7381
Singhai, Anjali6a899022015-12-14 12:21:18 -08007382 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007383 return;
7384
Singhai, Anjali6a899022015-12-14 12:21:18 -08007385 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007386
7387 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007388 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7389 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7390 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007391 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007392 ret = i40e_aq_add_udp_tunnel(hw, port,
7393 pf->udp_ports[i].type,
7394 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007395 else
7396 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007397
7398 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007399 dev_dbg(&pf->pdev->dev,
7400 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7401 pf->udp_ports[i].type ? "vxlan" : "geneve",
7402 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007403 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007404 i40e_stat_str(&pf->hw, ret),
7405 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007406 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007407 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007408 }
7409 }
7410 }
7411}
7412
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007413/**
7414 * i40e_service_task - Run the driver's async subtasks
7415 * @work: pointer to work_struct containing our data
7416 **/
7417static void i40e_service_task(struct work_struct *work)
7418{
7419 struct i40e_pf *pf = container_of(work,
7420 struct i40e_pf,
7421 service_task);
7422 unsigned long start_time = jiffies;
7423
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007424 /* don't bother with service tasks if a reset is in progress */
7425 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007426 return;
7427 }
7428
Mitch Williams91089032016-11-21 13:03:51 -08007429 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7430 return;
7431
Kiran Patilb03a8c12015-09-24 18:13:15 -04007432 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007433 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007434 i40e_reset_subtask(pf);
7435 i40e_handle_mdd_event(pf);
7436 i40e_vc_process_vflr_event(pf);
7437 i40e_watchdog_subtask(pf);
7438 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007439 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7440 /* Client subtask will reopen next time through. */
7441 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7442 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7443 } else {
7444 i40e_client_subtask(pf);
7445 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7446 i40e_notify_client_of_l2_param_changes(
7447 pf->vsi[pf->lan_vsi]);
7448 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7449 }
7450 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007451 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007452 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007453 i40e_clean_adminq_subtask(pf);
7454
Mitch Williams91089032016-11-21 13:03:51 -08007455 /* flush memory to make sure state is correct before next watchdog */
7456 smp_mb__before_atomic();
7457 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007458
7459 /* If the tasks have taken longer than one timer cycle or there
7460 * is more work to be done, reschedule the service task now
7461 * rather than wait for the timer to tick again.
7462 */
7463 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7464 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7465 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7466 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7467 i40e_service_event_schedule(pf);
7468}
7469
7470/**
7471 * i40e_service_timer - timer callback
7472 * @data: pointer to PF struct
7473 **/
7474static void i40e_service_timer(unsigned long data)
7475{
7476 struct i40e_pf *pf = (struct i40e_pf *)data;
7477
7478 mod_timer(&pf->service_timer,
7479 round_jiffies(jiffies + pf->service_timer_period));
7480 i40e_service_event_schedule(pf);
7481}
7482
7483/**
7484 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7485 * @vsi: the VSI being configured
7486 **/
7487static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7488{
7489 struct i40e_pf *pf = vsi->back;
7490
7491 switch (vsi->type) {
7492 case I40E_VSI_MAIN:
7493 vsi->alloc_queue_pairs = pf->num_lan_qps;
7494 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7495 I40E_REQ_DESCRIPTOR_MULTIPLE);
7496 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7497 vsi->num_q_vectors = pf->num_lan_msix;
7498 else
7499 vsi->num_q_vectors = 1;
7500
7501 break;
7502
7503 case I40E_VSI_FDIR:
7504 vsi->alloc_queue_pairs = 1;
7505 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7506 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007507 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007508 break;
7509
7510 case I40E_VSI_VMDQ2:
7511 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7512 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7513 I40E_REQ_DESCRIPTOR_MULTIPLE);
7514 vsi->num_q_vectors = pf->num_vmdq_msix;
7515 break;
7516
7517 case I40E_VSI_SRIOV:
7518 vsi->alloc_queue_pairs = pf->num_vf_qps;
7519 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7520 I40E_REQ_DESCRIPTOR_MULTIPLE);
7521 break;
7522
Vasu Dev38e00432014-08-01 13:27:03 -07007523#ifdef I40E_FCOE
7524 case I40E_VSI_FCOE:
7525 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7526 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7527 I40E_REQ_DESCRIPTOR_MULTIPLE);
7528 vsi->num_q_vectors = pf->num_fcoe_msix;
7529 break;
7530
7531#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007532 default:
7533 WARN_ON(1);
7534 return -ENODATA;
7535 }
7536
7537 return 0;
7538}
7539
7540/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007541 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7542 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007543 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007544 *
7545 * On error: returns error code (negative)
7546 * On success: returns 0
7547 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007548static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007549{
7550 int size;
7551 int ret = 0;
7552
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007553 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007554 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7555 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7556 if (!vsi->tx_rings)
7557 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007558 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7559
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007560 if (alloc_qvectors) {
7561 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007562 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007563 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7564 if (!vsi->q_vectors) {
7565 ret = -ENOMEM;
7566 goto err_vectors;
7567 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007568 }
7569 return ret;
7570
7571err_vectors:
7572 kfree(vsi->tx_rings);
7573 return ret;
7574}
7575
7576/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7578 * @pf: board private structure
7579 * @type: type of VSI
7580 *
7581 * On error: returns error code (negative)
7582 * On success: returns vsi index in PF (positive)
7583 **/
7584static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7585{
7586 int ret = -ENODEV;
7587 struct i40e_vsi *vsi;
7588 int vsi_idx;
7589 int i;
7590
7591 /* Need to protect the allocation of the VSIs at the PF level */
7592 mutex_lock(&pf->switch_mutex);
7593
7594 /* VSI list may be fragmented if VSI creation/destruction has
7595 * been happening. We can afford to do a quick scan to look
7596 * for any free VSIs in the list.
7597 *
7598 * find next empty vsi slot, looping back around if necessary
7599 */
7600 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007601 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007602 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007603 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007604 i = 0;
7605 while (i < pf->next_vsi && pf->vsi[i])
7606 i++;
7607 }
7608
Mitch Williams505682c2014-05-20 08:01:37 +00007609 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007610 vsi_idx = i; /* Found one! */
7611 } else {
7612 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007613 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007614 }
7615 pf->next_vsi = ++i;
7616
7617 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7618 if (!vsi) {
7619 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007620 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007621 }
7622 vsi->type = type;
7623 vsi->back = pf;
7624 set_bit(__I40E_DOWN, &vsi->state);
7625 vsi->flags = 0;
7626 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007627 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007628 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7629 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007630 vsi->netdev_registered = false;
7631 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007632 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007633 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007634
Alexander Duyck9f65e152013-09-28 06:00:58 +00007635 ret = i40e_set_num_rings_in_vsi(vsi);
7636 if (ret)
7637 goto err_rings;
7638
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007639 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007640 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007641 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007642
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007643 /* Setup default MSIX irq handler for VSI */
7644 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7645
Kiran Patil21659032015-09-30 14:09:03 -04007646 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007647 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007648 pf->vsi[vsi_idx] = vsi;
7649 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007650 goto unlock_pf;
7651
Alexander Duyck9f65e152013-09-28 06:00:58 +00007652err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007653 pf->next_vsi = i - 1;
7654 kfree(vsi);
7655unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007656 mutex_unlock(&pf->switch_mutex);
7657 return ret;
7658}
7659
7660/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007661 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7662 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007663 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007664 *
7665 * On error: returns error code (negative)
7666 * On success: returns 0
7667 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007668static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007669{
7670 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007671 if (free_qvectors) {
7672 kfree(vsi->q_vectors);
7673 vsi->q_vectors = NULL;
7674 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007675 kfree(vsi->tx_rings);
7676 vsi->tx_rings = NULL;
7677 vsi->rx_rings = NULL;
7678}
7679
7680/**
Helin Zhang28c58692015-10-26 19:44:27 -04007681 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7682 * and lookup table
7683 * @vsi: Pointer to VSI structure
7684 */
7685static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7686{
7687 if (!vsi)
7688 return;
7689
7690 kfree(vsi->rss_hkey_user);
7691 vsi->rss_hkey_user = NULL;
7692
7693 kfree(vsi->rss_lut_user);
7694 vsi->rss_lut_user = NULL;
7695}
7696
7697/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 * i40e_vsi_clear - Deallocate the VSI provided
7699 * @vsi: the VSI being un-configured
7700 **/
7701static int i40e_vsi_clear(struct i40e_vsi *vsi)
7702{
7703 struct i40e_pf *pf;
7704
7705 if (!vsi)
7706 return 0;
7707
7708 if (!vsi->back)
7709 goto free_vsi;
7710 pf = vsi->back;
7711
7712 mutex_lock(&pf->switch_mutex);
7713 if (!pf->vsi[vsi->idx]) {
7714 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7715 vsi->idx, vsi->idx, vsi, vsi->type);
7716 goto unlock_vsi;
7717 }
7718
7719 if (pf->vsi[vsi->idx] != vsi) {
7720 dev_err(&pf->pdev->dev,
7721 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7722 pf->vsi[vsi->idx]->idx,
7723 pf->vsi[vsi->idx],
7724 pf->vsi[vsi->idx]->type,
7725 vsi->idx, vsi, vsi->type);
7726 goto unlock_vsi;
7727 }
7728
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007729 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007730 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7731 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7732
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007733 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007734 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007735
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007736 pf->vsi[vsi->idx] = NULL;
7737 if (vsi->idx < pf->next_vsi)
7738 pf->next_vsi = vsi->idx;
7739
7740unlock_vsi:
7741 mutex_unlock(&pf->switch_mutex);
7742free_vsi:
7743 kfree(vsi);
7744
7745 return 0;
7746}
7747
7748/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007749 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7750 * @vsi: the VSI being cleaned
7751 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007752static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007753{
7754 int i;
7755
Greg Rose8e9dca52013-12-18 13:45:53 +00007756 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007757 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007758 kfree_rcu(vsi->tx_rings[i], rcu);
7759 vsi->tx_rings[i] = NULL;
7760 vsi->rx_rings[i] = NULL;
7761 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007762 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007763}
7764
7765/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007766 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7767 * @vsi: the VSI being configured
7768 **/
7769static int i40e_alloc_rings(struct i40e_vsi *vsi)
7770{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007771 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007772 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007773 int i;
7774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007775 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007776 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007777 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007778 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7779 if (!tx_ring)
7780 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007781
7782 tx_ring->queue_index = i;
7783 tx_ring->reg_idx = vsi->base_queue + i;
7784 tx_ring->ring_active = false;
7785 tx_ring->vsi = vsi;
7786 tx_ring->netdev = vsi->netdev;
7787 tx_ring->dev = &pf->pdev->dev;
7788 tx_ring->count = vsi->num_desc;
7789 tx_ring->size = 0;
7790 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007791 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7792 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007793 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007794 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007795
Alexander Duyck9f65e152013-09-28 06:00:58 +00007796 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007797 rx_ring->queue_index = i;
7798 rx_ring->reg_idx = vsi->base_queue + i;
7799 rx_ring->ring_active = false;
7800 rx_ring->vsi = vsi;
7801 rx_ring->netdev = vsi->netdev;
7802 rx_ring->dev = &pf->pdev->dev;
7803 rx_ring->count = vsi->num_desc;
7804 rx_ring->size = 0;
7805 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007806 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007807 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007808 }
7809
7810 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007811
7812err_out:
7813 i40e_vsi_clear_rings(vsi);
7814 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007815}
7816
7817/**
7818 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7819 * @pf: board private structure
7820 * @vectors: the number of MSI-X vectors to request
7821 *
7822 * Returns the number of vectors reserved, or error
7823 **/
7824static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7825{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007826 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7827 I40E_MIN_MSIX, vectors);
7828 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007829 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007830 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007831 vectors = 0;
7832 }
7833
7834 return vectors;
7835}
7836
7837/**
7838 * i40e_init_msix - Setup the MSIX capability
7839 * @pf: board private structure
7840 *
7841 * Work with the OS to set up the MSIX vectors needed.
7842 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007843 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007844 **/
7845static int i40e_init_msix(struct i40e_pf *pf)
7846{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007847 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007848 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007849 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007850 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007851 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007852 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007853
7854 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7855 return -ENODEV;
7856
7857 /* The number of vectors we'll request will be comprised of:
7858 * - Add 1 for "other" cause for Admin Queue events, etc.
7859 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007860 * - Queues being used for RSS.
7861 * We don't need as many as max_rss_size vectors.
7862 * use rss_size instead in the calculation since that
7863 * is governed by number of cpus in the system.
7864 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007865 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007866 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007867#ifdef I40E_FCOE
7868 * - The number of FCOE qps.
7869#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007870 * Once we count this up, try the request.
7871 *
7872 * If we can't get what we want, we'll simplify to nearly nothing
7873 * and try again. If that still fails, we punt.
7874 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007875 vectors_left = hw->func_caps.num_msix_vectors;
7876 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007877
Shannon Nelson1e200e42015-02-27 09:15:24 +00007878 /* reserve one vector for miscellaneous handler */
7879 if (vectors_left) {
7880 v_budget++;
7881 vectors_left--;
7882 }
7883
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007884 /* reserve some vectors for the main PF traffic queues. Initially we
7885 * only reserve at most 50% of the available vectors, in the case that
7886 * the number of online CPUs is large. This ensures that we can enable
7887 * extra features as well. Once we've enabled the other features, we
7888 * will use any remaining vectors to reach as close as we can to the
7889 * number of online CPUs.
7890 */
7891 cpus = num_online_cpus();
7892 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007893 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007894
7895 /* reserve one vector for sideband flow director */
7896 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7897 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007898 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007899 v_budget++;
7900 vectors_left--;
7901 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007902 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007903 }
7904 }
John W Linville83840e42015-01-14 03:06:28 +00007905
Vasu Dev38e00432014-08-01 13:27:03 -07007906#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007907 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007908 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007909 if (!vectors_left)
7910 pf->num_fcoe_msix = 0;
7911 else if (vectors_left >= pf->num_fcoe_qps)
7912 pf->num_fcoe_msix = pf->num_fcoe_qps;
7913 else
7914 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007915 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007916 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007917 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007918
Vasu Dev38e00432014-08-01 13:27:03 -07007919#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007920 /* can we reserve enough for iWARP? */
7921 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007922 iwarp_requested = pf->num_iwarp_msix;
7923
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007924 if (!vectors_left)
7925 pf->num_iwarp_msix = 0;
7926 else if (vectors_left < pf->num_iwarp_msix)
7927 pf->num_iwarp_msix = 1;
7928 v_budget += pf->num_iwarp_msix;
7929 vectors_left -= pf->num_iwarp_msix;
7930 }
7931
Shannon Nelson1e200e42015-02-27 09:15:24 +00007932 /* any vectors left over go for VMDq support */
7933 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7934 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7935 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7936
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007937 if (!vectors_left) {
7938 pf->num_vmdq_msix = 0;
7939 pf->num_vmdq_qps = 0;
7940 } else {
7941 /* if we're short on vectors for what's desired, we limit
7942 * the queues per vmdq. If this is still more than are
7943 * available, the user will need to change the number of
7944 * queues/vectors used by the PF later with the ethtool
7945 * channels command
7946 */
7947 if (vmdq_vecs < vmdq_vecs_wanted)
7948 pf->num_vmdq_qps = 1;
7949 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007950
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007951 v_budget += vmdq_vecs;
7952 vectors_left -= vmdq_vecs;
7953 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007954 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007955
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007956 /* On systems with a large number of SMP cores, we previously limited
7957 * the number of vectors for num_lan_msix to be at most 50% of the
7958 * available vectors, to allow for other features. Now, we add back
7959 * the remaining vectors. However, we ensure that the total
7960 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7961 * calculate the number of vectors we can add without going over the
7962 * cap of CPUs. For systems with a small number of CPUs this will be
7963 * zero.
7964 */
7965 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7966 pf->num_lan_msix += extra_vectors;
7967 vectors_left -= extra_vectors;
7968
7969 WARN(vectors_left < 0,
7970 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7971
7972 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007973 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7974 GFP_KERNEL);
7975 if (!pf->msix_entries)
7976 return -ENOMEM;
7977
7978 for (i = 0; i < v_budget; i++)
7979 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007980 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007981
Shannon Nelson3b444392015-02-26 16:15:57 +00007982 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7984 kfree(pf->msix_entries);
7985 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007986 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007987 return -ENODEV;
7988
Shannon Nelson3b444392015-02-26 16:15:57 +00007989 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007990 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007991 pf->num_vmdq_vsis = 0;
7992 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007993 pf->num_lan_qps = 1;
7994 pf->num_lan_msix = 1;
7995
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007996 } else if (!vectors_left) {
7997 /* If we have limited resources, we will start with no vectors
7998 * for the special features and then allocate vectors to some
7999 * of these features based on the policy and at the end disable
8000 * the features that did not get any vectors.
8001 */
Shannon Nelson3b444392015-02-26 16:15:57 +00008002 int vec;
8003
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008004 dev_info(&pf->pdev->dev,
8005 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008006 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00008007 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008008
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008009 /* Scale vector usage down */
8010 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008011 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00008012 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008013#ifdef I40E_FCOE
8014 pf->num_fcoe_qps = 0;
8015 pf->num_fcoe_msix = 0;
8016#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008017
8018 /* partition out the remaining vectors */
8019 switch (vec) {
8020 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008021 pf->num_lan_msix = 1;
8022 break;
8023 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008024 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8025 pf->num_lan_msix = 1;
8026 pf->num_iwarp_msix = 1;
8027 } else {
8028 pf->num_lan_msix = 2;
8029 }
Vasu Dev38e00432014-08-01 13:27:03 -07008030#ifdef I40E_FCOE
8031 /* give one vector to FCoE */
8032 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
8033 pf->num_lan_msix = 1;
8034 pf->num_fcoe_msix = 1;
8035 }
Vasu Dev38e00432014-08-01 13:27:03 -07008036#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008037 break;
8038 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008039 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
8040 pf->num_iwarp_msix = min_t(int, (vec / 3),
8041 iwarp_requested);
8042 pf->num_vmdq_vsis = min_t(int, (vec / 3),
8043 I40E_DEFAULT_NUM_VMDQ_VSI);
8044 } else {
8045 pf->num_vmdq_vsis = min_t(int, (vec / 2),
8046 I40E_DEFAULT_NUM_VMDQ_VSI);
8047 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02008048 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8049 pf->num_fdsb_msix = 1;
8050 vec--;
8051 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008052 pf->num_lan_msix = min_t(int,
8053 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
8054 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008055 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07008056#ifdef I40E_FCOE
8057 /* give one vector to FCoE */
8058 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
8059 pf->num_fcoe_msix = 1;
8060 vec--;
8061 }
8062#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008063 break;
8064 }
8065 }
8066
Stefan Assmannabd97a92016-09-19 13:37:51 +02008067 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8068 (pf->num_fdsb_msix == 0)) {
8069 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
8070 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8071 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00008072 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
8073 (pf->num_vmdq_msix == 0)) {
8074 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
8075 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
8076 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008077
8078 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
8079 (pf->num_iwarp_msix == 0)) {
8080 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
8081 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
8082 }
Vasu Dev38e00432014-08-01 13:27:03 -07008083#ifdef I40E_FCOE
8084
8085 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
8086 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
8087 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
8088 }
8089#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02008090 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
8091 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
8092 pf->num_lan_msix,
8093 pf->num_vmdq_msix * pf->num_vmdq_vsis,
8094 pf->num_fdsb_msix,
8095 pf->num_iwarp_msix);
8096
Shannon Nelson3b444392015-02-26 16:15:57 +00008097 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008098}
8099
8100/**
Greg Rose90e04072014-03-06 08:59:57 +00008101 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00008102 * @vsi: the VSI being configured
8103 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008104 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00008105 *
8106 * We allocate one q_vector. If allocation fails we return -ENOMEM.
8107 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008108static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00008109{
8110 struct i40e_q_vector *q_vector;
8111
8112 /* allocate q_vector */
8113 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
8114 if (!q_vector)
8115 return -ENOMEM;
8116
8117 q_vector->vsi = vsi;
8118 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008119 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
8120
Alexander Duyck493fb302013-09-28 07:01:44 +00008121 if (vsi->netdev)
8122 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00008123 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00008124
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00008125 q_vector->rx.latency_range = I40E_LOW_LATENCY;
8126 q_vector->tx.latency_range = I40E_LOW_LATENCY;
8127
Alexander Duyck493fb302013-09-28 07:01:44 +00008128 /* tie q_vector and vsi together */
8129 vsi->q_vectors[v_idx] = q_vector;
8130
8131 return 0;
8132}
8133
8134/**
Greg Rose90e04072014-03-06 08:59:57 +00008135 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008136 * @vsi: the VSI being configured
8137 *
8138 * We allocate one q_vector per queue interrupt. If allocation fails we
8139 * return -ENOMEM.
8140 **/
Greg Rose90e04072014-03-06 08:59:57 +00008141static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008142{
8143 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008144 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008145
8146 /* if not MSIX, give the one vector only to the LAN VSI */
8147 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
8148 num_q_vectors = vsi->num_q_vectors;
8149 else if (vsi == pf->vsi[pf->lan_vsi])
8150 num_q_vectors = 1;
8151 else
8152 return -EINVAL;
8153
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008154 current_cpu = cpumask_first(cpu_online_mask);
8155
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008156 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008157 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00008158 if (err)
8159 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03008160 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
8161 if (unlikely(current_cpu >= nr_cpu_ids))
8162 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008163 }
8164
8165 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008166
8167err_out:
8168 while (v_idx--)
8169 i40e_free_q_vector(vsi, v_idx);
8170
8171 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008172}
8173
8174/**
8175 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8176 * @pf: board private structure to initialize
8177 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008178static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008179{
Shannon Nelson3b444392015-02-26 16:15:57 +00008180 int vectors = 0;
8181 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008182
8183 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008184 vectors = i40e_init_msix(pf);
8185 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008186 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008187 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07008188#ifdef I40E_FCOE
8189 I40E_FLAG_FCOE_ENABLED |
8190#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008191 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008192 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008193 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008194 I40E_FLAG_SRIOV_ENABLED |
8195 I40E_FLAG_FD_SB_ENABLED |
8196 I40E_FLAG_FD_ATR_ENABLED |
8197 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008198
8199 /* rework the queue expectations without MSIX */
8200 i40e_determine_queue_usage(pf);
8201 }
8202 }
8203
8204 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8205 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008206 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008207 vectors = pci_enable_msi(pf->pdev);
8208 if (vectors < 0) {
8209 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8210 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008211 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8212 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008213 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008214 }
8215
Shannon Nelson958a3e32013-09-28 07:13:28 +00008216 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008217 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008218
Shannon Nelson3b444392015-02-26 16:15:57 +00008219 /* set up vector assignment tracking */
8220 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8221 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008222 if (!pf->irq_pile) {
8223 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8224 return -ENOMEM;
8225 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008226 pf->irq_pile->num_entries = vectors;
8227 pf->irq_pile->search_hint = 0;
8228
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008229 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008230 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008231
8232 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008233}
8234
8235/**
8236 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8237 * @pf: board private structure
8238 *
8239 * This sets up the handler for MSIX 0, which is used to manage the
8240 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8241 * when in MSI or Legacy interrupt mode.
8242 **/
8243static int i40e_setup_misc_vector(struct i40e_pf *pf)
8244{
8245 struct i40e_hw *hw = &pf->hw;
8246 int err = 0;
8247
8248 /* Only request the irq if this is the first time through, and
8249 * not when we're rebuilding after a Reset
8250 */
8251 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8252 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008253 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008254 if (err) {
8255 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008256 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008257 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008258 return -EFAULT;
8259 }
8260 }
8261
Jacob Kellerab437b52014-12-14 01:55:08 +00008262 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008263
8264 /* associate no queues to the misc vector */
8265 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8266 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8267
8268 i40e_flush(hw);
8269
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008270 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008271
8272 return err;
8273}
8274
8275/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008276 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8277 * @vsi: vsi structure
8278 * @seed: RSS hash seed
8279 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008280static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8281 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008282{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008283 struct i40e_pf *pf = vsi->back;
8284 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008285 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008286
Jacob Keller776b2e12016-07-19 16:23:30 -07008287 if (seed) {
8288 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8289 (struct i40e_aqc_get_set_rss_key_data *)seed;
8290 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8291 if (ret) {
8292 dev_info(&pf->pdev->dev,
8293 "Cannot set RSS key, err %s aq_err %s\n",
8294 i40e_stat_str(hw, ret),
8295 i40e_aq_str(hw, hw->aq.asq_last_status));
8296 return ret;
8297 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008298 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008299 if (lut) {
8300 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008301
Jacob Keller776b2e12016-07-19 16:23:30 -07008302 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8303 if (ret) {
8304 dev_info(&pf->pdev->dev,
8305 "Cannot set RSS lut, err %s aq_err %s\n",
8306 i40e_stat_str(hw, ret),
8307 i40e_aq_str(hw, hw->aq.asq_last_status));
8308 return ret;
8309 }
8310 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008311 return ret;
8312}
8313
8314/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008315 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8316 * @vsi: Pointer to vsi structure
8317 * @seed: Buffter to store the hash keys
8318 * @lut: Buffer to store the lookup table entries
8319 * @lut_size: Size of buffer to store the lookup table entries
8320 *
8321 * Return 0 on success, negative on failure
8322 */
8323static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8324 u8 *lut, u16 lut_size)
8325{
8326 struct i40e_pf *pf = vsi->back;
8327 struct i40e_hw *hw = &pf->hw;
8328 int ret = 0;
8329
8330 if (seed) {
8331 ret = i40e_aq_get_rss_key(hw, vsi->id,
8332 (struct i40e_aqc_get_set_rss_key_data *)seed);
8333 if (ret) {
8334 dev_info(&pf->pdev->dev,
8335 "Cannot get RSS key, err %s aq_err %s\n",
8336 i40e_stat_str(&pf->hw, ret),
8337 i40e_aq_str(&pf->hw,
8338 pf->hw.aq.asq_last_status));
8339 return ret;
8340 }
8341 }
8342
8343 if (lut) {
8344 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8345
8346 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8347 if (ret) {
8348 dev_info(&pf->pdev->dev,
8349 "Cannot get RSS lut, err %s aq_err %s\n",
8350 i40e_stat_str(&pf->hw, ret),
8351 i40e_aq_str(&pf->hw,
8352 pf->hw.aq.asq_last_status));
8353 return ret;
8354 }
8355 }
8356
8357 return ret;
8358}
8359
8360/**
Jacob Keller0582b962016-07-19 16:23:29 -07008361 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8362 * @vsi: VSI structure
8363 **/
8364static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8365{
8366 u8 seed[I40E_HKEY_ARRAY_SIZE];
8367 struct i40e_pf *pf = vsi->back;
8368 u8 *lut;
8369 int ret;
8370
8371 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8372 return 0;
8373
Jacob Keller552b9962016-07-19 16:23:31 -07008374 if (!vsi->rss_size)
8375 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8376 vsi->num_queue_pairs);
8377 if (!vsi->rss_size)
8378 return -EINVAL;
8379
Jacob Keller0582b962016-07-19 16:23:29 -07008380 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8381 if (!lut)
8382 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008383 /* Use the user configured hash keys and lookup table if there is one,
8384 * otherwise use default
8385 */
8386 if (vsi->rss_lut_user)
8387 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8388 else
8389 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8390 if (vsi->rss_hkey_user)
8391 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8392 else
8393 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008394 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8395 kfree(lut);
8396
8397 return ret;
8398}
8399
8400/**
Helin Zhang043dd652015-10-21 19:56:23 -04008401 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008402 * @vsi: Pointer to vsi structure
8403 * @seed: RSS hash seed
8404 * @lut: Lookup table
8405 * @lut_size: Lookup table size
8406 *
8407 * Returns 0 on success, negative on failure
8408 **/
8409static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8410 const u8 *lut, u16 lut_size)
8411{
8412 struct i40e_pf *pf = vsi->back;
8413 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008414 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008415 u8 i;
8416
8417 /* Fill out hash function seed */
8418 if (seed) {
8419 u32 *seed_dw = (u32 *)seed;
8420
Mitch Williamsc4e18682016-04-12 08:30:40 -07008421 if (vsi->type == I40E_VSI_MAIN) {
8422 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008423 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008424 } else if (vsi->type == I40E_VSI_SRIOV) {
8425 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008426 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008427 } else {
8428 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8429 }
Helin Zhange69ff812015-10-21 19:56:22 -04008430 }
8431
8432 if (lut) {
8433 u32 *lut_dw = (u32 *)lut;
8434
Mitch Williamsc4e18682016-04-12 08:30:40 -07008435 if (vsi->type == I40E_VSI_MAIN) {
8436 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8437 return -EINVAL;
8438 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8439 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8440 } else if (vsi->type == I40E_VSI_SRIOV) {
8441 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8442 return -EINVAL;
8443 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008444 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008445 } else {
8446 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8447 }
Helin Zhange69ff812015-10-21 19:56:22 -04008448 }
8449 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008450
8451 return 0;
8452}
8453
8454/**
Helin Zhang043dd652015-10-21 19:56:23 -04008455 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8456 * @vsi: Pointer to VSI structure
8457 * @seed: Buffer to store the keys
8458 * @lut: Buffer to store the lookup table entries
8459 * @lut_size: Size of buffer to store the lookup table entries
8460 *
8461 * Returns 0 on success, negative on failure
8462 */
8463static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8464 u8 *lut, u16 lut_size)
8465{
8466 struct i40e_pf *pf = vsi->back;
8467 struct i40e_hw *hw = &pf->hw;
8468 u16 i;
8469
8470 if (seed) {
8471 u32 *seed_dw = (u32 *)seed;
8472
8473 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008474 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008475 }
8476 if (lut) {
8477 u32 *lut_dw = (u32 *)lut;
8478
8479 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8480 return -EINVAL;
8481 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8482 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8483 }
8484
8485 return 0;
8486}
8487
8488/**
8489 * i40e_config_rss - Configure RSS keys and lut
8490 * @vsi: Pointer to VSI structure
8491 * @seed: RSS hash seed
8492 * @lut: Lookup table
8493 * @lut_size: Lookup table size
8494 *
8495 * Returns 0 on success, negative on failure
8496 */
8497int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8498{
8499 struct i40e_pf *pf = vsi->back;
8500
8501 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8502 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8503 else
8504 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8505}
8506
8507/**
8508 * i40e_get_rss - Get RSS keys and lut
8509 * @vsi: Pointer to VSI structure
8510 * @seed: Buffer to store the keys
8511 * @lut: Buffer to store the lookup table entries
8512 * lut_size: Size of buffer to store the lookup table entries
8513 *
8514 * Returns 0 on success, negative on failure
8515 */
8516int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8517{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008518 struct i40e_pf *pf = vsi->back;
8519
8520 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8521 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8522 else
8523 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008524}
8525
8526/**
Helin Zhange69ff812015-10-21 19:56:22 -04008527 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8528 * @pf: Pointer to board private structure
8529 * @lut: Lookup table
8530 * @rss_table_size: Lookup table size
8531 * @rss_size: Range of queue number for hashing
8532 */
Alan Bradyf1582352016-08-24 11:33:46 -07008533void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8534 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008535{
Helin Zhange69ff812015-10-21 19:56:22 -04008536 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008537
Helin Zhange69ff812015-10-21 19:56:22 -04008538 for (i = 0; i < rss_table_size; i++)
8539 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008540}
8541
8542/**
Helin Zhang043dd652015-10-21 19:56:23 -04008543 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008544 * @pf: board private structure
8545 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008546static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008547{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008548 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008549 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008550 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008551 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008552 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008553 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008554 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008556 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008557 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8558 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008559 hena |= i40e_pf_get_default_rss_hena(pf);
8560
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008561 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8562 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008563
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008564 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008565 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008566 reg_val = (pf->rss_table_size == 512) ?
8567 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8568 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008569 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008570
Helin Zhang28c58692015-10-26 19:44:27 -04008571 /* Determine the RSS size of the VSI */
8572 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008573 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8574 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008575 if (!vsi->rss_size)
8576 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008577
Helin Zhange69ff812015-10-21 19:56:22 -04008578 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8579 if (!lut)
8580 return -ENOMEM;
8581
Helin Zhang28c58692015-10-26 19:44:27 -04008582 /* Use user configured lut if there is one, otherwise use default */
8583 if (vsi->rss_lut_user)
8584 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8585 else
8586 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008587
Helin Zhang28c58692015-10-26 19:44:27 -04008588 /* Use user configured hash key if there is one, otherwise
8589 * use default.
8590 */
8591 if (vsi->rss_hkey_user)
8592 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8593 else
8594 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008595 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008596 kfree(lut);
8597
8598 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008599}
8600
8601/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008602 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8603 * @pf: board private structure
8604 * @queue_count: the requested queue count for rss.
8605 *
8606 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8607 * count which may be different from the requested queue count.
8608 **/
8609int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8610{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008611 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8612 int new_rss_size;
8613
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008614 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8615 return 0;
8616
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008617 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008618
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008619 if (queue_count != vsi->num_queue_pairs) {
8620 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008621 i40e_prep_for_reset(pf);
8622
Helin Zhangacd65442015-10-26 19:44:28 -04008623 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008624
8625 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008626
8627 /* Discard the user configured hash keys and lut, if less
8628 * queues are enabled.
8629 */
8630 if (queue_count < vsi->rss_size) {
8631 i40e_clear_rss_config_user(vsi);
8632 dev_dbg(&pf->pdev->dev,
8633 "discard user configured hash keys and lut\n");
8634 }
8635
8636 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008637 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8638 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008639
Helin Zhang043dd652015-10-21 19:56:23 -04008640 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008641 }
Lihong Yang12815052016-09-27 11:28:48 -07008642 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8643 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008644 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008645}
8646
8647/**
Greg Rosef4492db2015-02-06 08:52:12 +00008648 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8649 * @pf: board private structure
8650 **/
8651i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8652{
8653 i40e_status status;
8654 bool min_valid, max_valid;
8655 u32 max_bw, min_bw;
8656
8657 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8658 &min_valid, &max_valid);
8659
8660 if (!status) {
8661 if (min_valid)
8662 pf->npar_min_bw = min_bw;
8663 if (max_valid)
8664 pf->npar_max_bw = max_bw;
8665 }
8666
8667 return status;
8668}
8669
8670/**
8671 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8672 * @pf: board private structure
8673 **/
8674i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8675{
8676 struct i40e_aqc_configure_partition_bw_data bw_data;
8677 i40e_status status;
8678
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008679 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008680 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008681 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8682 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8683
8684 /* Set the new bandwidths */
8685 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8686
8687 return status;
8688}
8689
8690/**
8691 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8692 * @pf: board private structure
8693 **/
8694i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8695{
8696 /* Commit temporary BW setting to permanent NVM image */
8697 enum i40e_admin_queue_err last_aq_status;
8698 i40e_status ret;
8699 u16 nvm_word;
8700
8701 if (pf->hw.partition_id != 1) {
8702 dev_info(&pf->pdev->dev,
8703 "Commit BW only works on partition 1! This is partition %d",
8704 pf->hw.partition_id);
8705 ret = I40E_NOT_SUPPORTED;
8706 goto bw_commit_out;
8707 }
8708
8709 /* Acquire NVM for read access */
8710 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8711 last_aq_status = pf->hw.aq.asq_last_status;
8712 if (ret) {
8713 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008714 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8715 i40e_stat_str(&pf->hw, ret),
8716 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008717 goto bw_commit_out;
8718 }
8719
8720 /* Read word 0x10 of NVM - SW compatibility word 1 */
8721 ret = i40e_aq_read_nvm(&pf->hw,
8722 I40E_SR_NVM_CONTROL_WORD,
8723 0x10, sizeof(nvm_word), &nvm_word,
8724 false, NULL);
8725 /* Save off last admin queue command status before releasing
8726 * the NVM
8727 */
8728 last_aq_status = pf->hw.aq.asq_last_status;
8729 i40e_release_nvm(&pf->hw);
8730 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008731 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8732 i40e_stat_str(&pf->hw, ret),
8733 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008734 goto bw_commit_out;
8735 }
8736
8737 /* Wait a bit for NVM release to complete */
8738 msleep(50);
8739
8740 /* Acquire NVM for write access */
8741 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8742 last_aq_status = pf->hw.aq.asq_last_status;
8743 if (ret) {
8744 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008745 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8746 i40e_stat_str(&pf->hw, ret),
8747 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008748 goto bw_commit_out;
8749 }
8750 /* Write it back out unchanged to initiate update NVM,
8751 * which will force a write of the shadow (alt) RAM to
8752 * the NVM - thus storing the bandwidth values permanently.
8753 */
8754 ret = i40e_aq_update_nvm(&pf->hw,
8755 I40E_SR_NVM_CONTROL_WORD,
8756 0x10, sizeof(nvm_word),
8757 &nvm_word, true, NULL);
8758 /* Save off last admin queue command status before releasing
8759 * the NVM
8760 */
8761 last_aq_status = pf->hw.aq.asq_last_status;
8762 i40e_release_nvm(&pf->hw);
8763 if (ret)
8764 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008765 "BW settings NOT SAVED, err %s aq_err %s\n",
8766 i40e_stat_str(&pf->hw, ret),
8767 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008768bw_commit_out:
8769
8770 return ret;
8771}
8772
8773/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008774 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8775 * @pf: board private structure to initialize
8776 *
8777 * i40e_sw_init initializes the Adapter private data structure.
8778 * Fields are initialized based on PCI device information and
8779 * OS network device settings (MTU size).
8780 **/
8781static int i40e_sw_init(struct i40e_pf *pf)
8782{
8783 int err = 0;
8784 int size;
8785
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008786 /* Set default capability flags */
8787 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8788 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008789 I40E_FLAG_MSIX_ENABLED;
8790
Mitch Williamsca99eb92014-04-04 04:43:07 +00008791 /* Set default ITR */
8792 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8793 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8794
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008795 /* Depending on PF configurations, it is possible that the RSS
8796 * maximum might end up larger than the available queues
8797 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008798 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008799 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008800 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008801 pf->rss_size_max = min_t(int, pf->rss_size_max,
8802 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008803 if (pf->hw.func_caps.rss) {
8804 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008805 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8806 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008807 }
8808
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008809 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008810 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008811 pf->flags |= I40E_FLAG_MFP_ENABLED;
8812 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008813 if (i40e_get_npar_bw_setting(pf))
8814 dev_warn(&pf->pdev->dev,
8815 "Could not get NPAR bw settings\n");
8816 else
8817 dev_info(&pf->pdev->dev,
8818 "Min BW = %8.8x, Max BW = %8.8x\n",
8819 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008820 }
8821
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008822 /* FW/NVM is not yet fixed in this regard */
8823 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8824 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8825 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8826 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008827 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8828 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008829 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008830 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008831 else
8832 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008833 pf->fdir_pf_filter_count =
8834 pf->hw.func_caps.fd_filters_guaranteed;
8835 pf->hw.fdir_shared_filter_count =
8836 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008837 }
8838
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008839 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008840 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008841 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008842 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008843 /* No DCB support for FW < v4.33 */
8844 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8845 }
8846
8847 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008848 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008849 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8850 (pf->hw.aq.fw_maj_ver < 4)))
8851 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8852
8853 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008854 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008855 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8856 (pf->hw.aq.fw_maj_ver >= 5)))
8857 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008858
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008859 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008860 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008861 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008862 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008863 }
8864
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008865 if (pf->hw.func_caps.iwarp) {
8866 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8867 /* IWARP needs one extra vector for CQP just like MISC.*/
8868 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8869 }
8870
Vasu Dev38e00432014-08-01 13:27:03 -07008871#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008872 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008873
8874#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008875#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008876 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008877 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8878 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8879 pf->num_req_vfs = min_t(int,
8880 pf->hw.func_caps.num_vfs,
8881 I40E_MAX_VF_COUNT);
8882 }
8883#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008884 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008885 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8886 | I40E_FLAG_128_QP_RSS_CAPABLE
8887 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8888 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8889 | I40E_FLAG_WB_ON_ITR_CAPABLE
8890 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8891 | I40E_FLAG_NO_PCI_LINK_CHECK
8892 | I40E_FLAG_USE_SET_LLDP_MIB
8893 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8894 | I40E_FLAG_PTP_L4_CAPABLE
8895 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008896 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8897 ((pf->hw.aq.api_maj_ver == 1) &&
8898 (pf->hw.aq.api_min_ver > 4))) {
8899 /* Supported in FW API version higher than 1.4 */
8900 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008901 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008902 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008903 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008904 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008905
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008906 pf->eeprom_version = 0xDEAD;
8907 pf->lan_veb = I40E_NO_VEB;
8908 pf->lan_vsi = I40E_NO_VSI;
8909
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008910 /* By default FW has this off for performance reasons */
8911 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8912
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008913 /* set up queue assignment tracking */
8914 size = sizeof(struct i40e_lump_tracking)
8915 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8916 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8917 if (!pf->qp_pile) {
8918 err = -ENOMEM;
8919 goto sw_init_done;
8920 }
8921 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8922 pf->qp_pile->search_hint = 0;
8923
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008924 pf->tx_timeout_recovery_level = 1;
8925
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008926 mutex_init(&pf->switch_mutex);
8927
Greg Rosec668a122015-02-26 16:10:39 +00008928 /* If NPAR is enabled nudge the Tx scheduler */
8929 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8930 i40e_set_npar_bw_setting(pf);
8931
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008932sw_init_done:
8933 return err;
8934}
8935
8936/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008937 * i40e_set_ntuple - set the ntuple feature flag and take action
8938 * @pf: board private structure to initialize
8939 * @features: the feature set that the stack is suggesting
8940 *
8941 * returns a bool to indicate if reset needs to happen
8942 **/
8943bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8944{
8945 bool need_reset = false;
8946
8947 /* Check if Flow Director n-tuple support was enabled or disabled. If
8948 * the state changed, we need to reset.
8949 */
8950 if (features & NETIF_F_NTUPLE) {
8951 /* Enable filters and mark for reset */
8952 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8953 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008954 /* enable FD_SB only if there is MSI-X vector */
8955 if (pf->num_fdsb_msix > 0)
8956 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008957 } else {
8958 /* turn off filters, mark for reset and clear SW filter list */
8959 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8960 need_reset = true;
8961 i40e_fdir_filter_exit(pf);
8962 }
8963 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008964 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008965 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008966 pf->fd_add_err = 0;
8967 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008968 /* if ATR was auto disabled it can be re-enabled. */
8969 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008970 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8971 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008972 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8973 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8974 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008975 }
8976 return need_reset;
8977}
8978
8979/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008980 * i40e_clear_rss_lut - clear the rx hash lookup table
8981 * @vsi: the VSI being configured
8982 **/
8983static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8984{
8985 struct i40e_pf *pf = vsi->back;
8986 struct i40e_hw *hw = &pf->hw;
8987 u16 vf_id = vsi->vf_id;
8988 u8 i;
8989
8990 if (vsi->type == I40E_VSI_MAIN) {
8991 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8992 wr32(hw, I40E_PFQF_HLUT(i), 0);
8993 } else if (vsi->type == I40E_VSI_SRIOV) {
8994 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8995 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8996 } else {
8997 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8998 }
8999}
9000
9001/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009002 * i40e_set_features - set the netdev feature flags
9003 * @netdev: ptr to the netdev being adjusted
9004 * @features: the feature set that the stack is suggesting
9005 **/
9006static int i40e_set_features(struct net_device *netdev,
9007 netdev_features_t features)
9008{
9009 struct i40e_netdev_priv *np = netdev_priv(netdev);
9010 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009011 struct i40e_pf *pf = vsi->back;
9012 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009013
Alan Bradyd8ec9862016-07-27 12:02:38 -07009014 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
9015 i40e_pf_config_rss(pf);
9016 else if (!(features & NETIF_F_RXHASH) &&
9017 netdev->features & NETIF_F_RXHASH)
9018 i40e_clear_rss_lut(vsi);
9019
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009020 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9021 i40e_vlan_stripping_enable(vsi);
9022 else
9023 i40e_vlan_stripping_disable(vsi);
9024
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009025 need_reset = i40e_set_ntuple(pf, features);
9026
9027 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04009028 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00009029
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009030 return 0;
9031}
9032
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009033/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08009034 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009035 * @pf: board private structure
9036 * @port: The UDP port to look up
9037 *
9038 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
9039 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009040static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009041{
9042 u8 i;
9043
9044 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08009045 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009046 return i;
9047 }
9048
9049 return i;
9050}
9051
9052/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009053 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009054 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009055 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009056 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009057static void i40e_udp_tunnel_add(struct net_device *netdev,
9058 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009059{
9060 struct i40e_netdev_priv *np = netdev_priv(netdev);
9061 struct i40e_vsi *vsi = np->vsi;
9062 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009063 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009064 u8 next_idx;
9065 u8 idx;
9066
Singhai, Anjali6a899022015-12-14 12:21:18 -08009067 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009068
9069 /* Check if port already exists */
9070 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009071 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009072 return;
9073 }
9074
9075 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009076 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009077
9078 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009079 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009080 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009081 return;
9082 }
9083
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009084 switch (ti->type) {
9085 case UDP_TUNNEL_TYPE_VXLAN:
9086 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
9087 break;
9088 case UDP_TUNNEL_TYPE_GENEVE:
9089 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
9090 return;
9091 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
9092 break;
9093 default:
9094 return;
9095 }
9096
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009097 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08009098 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009099 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
9100 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009101}
9102
9103/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009104 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009105 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009106 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009107 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009108static void i40e_udp_tunnel_del(struct net_device *netdev,
9109 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009110{
9111 struct i40e_netdev_priv *np = netdev_priv(netdev);
9112 struct i40e_vsi *vsi = np->vsi;
9113 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009114 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009115 u8 idx;
9116
Singhai, Anjali6a899022015-12-14 12:21:18 -08009117 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009118
9119 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009120 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
9121 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009122
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009123 switch (ti->type) {
9124 case UDP_TUNNEL_TYPE_VXLAN:
9125 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
9126 goto not_found;
9127 break;
9128 case UDP_TUNNEL_TYPE_GENEVE:
9129 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
9130 goto not_found;
9131 break;
9132 default:
9133 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08009134 }
9135
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009136 /* if port exists, set it to 0 (mark for deletion)
9137 * and make it pending
9138 */
9139 pf->udp_ports[idx].index = 0;
9140 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009141 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
9142
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009143 return;
9144not_found:
9145 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08009146 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08009147}
9148
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009149static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01009150 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009151{
9152 struct i40e_netdev_priv *np = netdev_priv(netdev);
9153 struct i40e_pf *pf = np->vsi->back;
9154 struct i40e_hw *hw = &pf->hw;
9155
9156 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
9157 return -EOPNOTSUPP;
9158
9159 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
9160 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
9161
9162 return 0;
9163}
9164
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009165/**
9166 * i40e_ndo_fdb_add - add an entry to the hardware database
9167 * @ndm: the input from the stack
9168 * @tb: pointer to array of nladdr (unused)
9169 * @dev: the net device pointer
9170 * @addr: the MAC address entry being added
9171 * @flags: instructions from stack about fdb operation
9172 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009173static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9174 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009175 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009176 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009177{
9178 struct i40e_netdev_priv *np = netdev_priv(dev);
9179 struct i40e_pf *pf = np->vsi->back;
9180 int err = 0;
9181
9182 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9183 return -EOPNOTSUPP;
9184
Or Gerlitz65891fe2014-12-14 18:19:05 +02009185 if (vid) {
9186 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9187 return -EINVAL;
9188 }
9189
Greg Rose4ba0dea2014-03-06 08:59:55 +00009190 /* Hardware does not support aging addresses so if a
9191 * ndm_state is given only allow permanent addresses
9192 */
9193 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9194 netdev_info(dev, "FDB only supports static addresses\n");
9195 return -EINVAL;
9196 }
9197
9198 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9199 err = dev_uc_add_excl(dev, addr);
9200 else if (is_multicast_ether_addr(addr))
9201 err = dev_mc_add_excl(dev, addr);
9202 else
9203 err = -EINVAL;
9204
9205 /* Only return duplicate errors if NLM_F_EXCL is set */
9206 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9207 err = 0;
9208
9209 return err;
9210}
9211
Neerav Parikh51616012015-02-06 08:52:14 +00009212/**
9213 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9214 * @dev: the netdev being configured
9215 * @nlh: RTNL message
9216 *
9217 * Inserts a new hardware bridge if not already created and
9218 * enables the bridging mode requested (VEB or VEPA). If the
9219 * hardware bridge has already been inserted and the request
9220 * is to change the mode then that requires a PF reset to
9221 * allow rebuild of the components with required hardware
9222 * bridge mode enabled.
9223 **/
9224static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009225 struct nlmsghdr *nlh,
9226 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009227{
9228 struct i40e_netdev_priv *np = netdev_priv(dev);
9229 struct i40e_vsi *vsi = np->vsi;
9230 struct i40e_pf *pf = vsi->back;
9231 struct i40e_veb *veb = NULL;
9232 struct nlattr *attr, *br_spec;
9233 int i, rem;
9234
9235 /* Only for PF VSI for now */
9236 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9237 return -EOPNOTSUPP;
9238
9239 /* Find the HW bridge for PF VSI */
9240 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9241 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9242 veb = pf->veb[i];
9243 }
9244
9245 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9246
9247 nla_for_each_nested(attr, br_spec, rem) {
9248 __u16 mode;
9249
9250 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9251 continue;
9252
9253 mode = nla_get_u16(attr);
9254 if ((mode != BRIDGE_MODE_VEPA) &&
9255 (mode != BRIDGE_MODE_VEB))
9256 return -EINVAL;
9257
9258 /* Insert a new HW bridge */
9259 if (!veb) {
9260 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9261 vsi->tc_config.enabled_tc);
9262 if (veb) {
9263 veb->bridge_mode = mode;
9264 i40e_config_bridge_mode(veb);
9265 } else {
9266 /* No Bridge HW offload available */
9267 return -ENOENT;
9268 }
9269 break;
9270 } else if (mode != veb->bridge_mode) {
9271 /* Existing HW bridge but different mode needs reset */
9272 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009273 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9274 if (mode == BRIDGE_MODE_VEB)
9275 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9276 else
9277 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9278 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009279 break;
9280 }
9281 }
9282
9283 return 0;
9284}
9285
9286/**
9287 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9288 * @skb: skb buff
9289 * @pid: process id
9290 * @seq: RTNL message seq #
9291 * @dev: the netdev being configured
9292 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009293 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009294 *
9295 * Return the mode in which the hardware bridge is operating in
9296 * i.e VEB or VEPA.
9297 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009298static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9299 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009300 u32 __always_unused filter_mask,
9301 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009302{
9303 struct i40e_netdev_priv *np = netdev_priv(dev);
9304 struct i40e_vsi *vsi = np->vsi;
9305 struct i40e_pf *pf = vsi->back;
9306 struct i40e_veb *veb = NULL;
9307 int i;
9308
9309 /* Only for PF VSI for now */
9310 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9311 return -EOPNOTSUPP;
9312
9313 /* Find the HW bridge for the PF VSI */
9314 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9315 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9316 veb = pf->veb[i];
9317 }
9318
9319 if (!veb)
9320 return 0;
9321
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009322 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009323 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009324}
Neerav Parikh51616012015-02-06 08:52:14 +00009325
Joe Stringerf44a75e2015-04-14 17:09:14 -07009326/**
9327 * i40e_features_check - Validate encapsulated packet conforms to limits
9328 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009329 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009330 * @features: Offload features that the stack believes apply
9331 **/
9332static netdev_features_t i40e_features_check(struct sk_buff *skb,
9333 struct net_device *dev,
9334 netdev_features_t features)
9335{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009336 size_t len;
9337
9338 /* No point in doing any of this if neither checksum nor GSO are
9339 * being requested for this frame. We can rule out both by just
9340 * checking for CHECKSUM_PARTIAL
9341 */
9342 if (skb->ip_summed != CHECKSUM_PARTIAL)
9343 return features;
9344
9345 /* We cannot support GSO if the MSS is going to be less than
9346 * 64 bytes. If it is then we need to drop support for GSO.
9347 */
9348 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9349 features &= ~NETIF_F_GSO_MASK;
9350
9351 /* MACLEN can support at most 63 words */
9352 len = skb_network_header(skb) - skb->data;
9353 if (len & ~(63 * 2))
9354 goto out_err;
9355
9356 /* IPLEN and EIPLEN can support at most 127 dwords */
9357 len = skb_transport_header(skb) - skb_network_header(skb);
9358 if (len & ~(127 * 4))
9359 goto out_err;
9360
9361 if (skb->encapsulation) {
9362 /* L4TUNLEN can support 127 words */
9363 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9364 if (len & ~(127 * 2))
9365 goto out_err;
9366
9367 /* IPLEN can support at most 127 dwords */
9368 len = skb_inner_transport_header(skb) -
9369 skb_inner_network_header(skb);
9370 if (len & ~(127 * 4))
9371 goto out_err;
9372 }
9373
9374 /* No need to validate L4LEN as TCP is the only protocol with a
9375 * a flexible value and we support all possible values supported
9376 * by TCP, which is at most 15 dwords
9377 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009378
9379 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009380out_err:
9381 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009382}
9383
Shannon Nelson37a29732015-02-27 09:15:19 +00009384static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385 .ndo_open = i40e_open,
9386 .ndo_stop = i40e_close,
9387 .ndo_start_xmit = i40e_lan_xmit_frame,
9388 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9389 .ndo_set_rx_mode = i40e_set_rx_mode,
9390 .ndo_validate_addr = eth_validate_addr,
9391 .ndo_set_mac_address = i40e_set_mac,
9392 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009393 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009394 .ndo_tx_timeout = i40e_tx_timeout,
9395 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9396 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9397#ifdef CONFIG_NET_POLL_CONTROLLER
9398 .ndo_poll_controller = i40e_netpoll,
9399#endif
John Fastabende4c67342016-02-16 21:16:15 -08009400 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009401#ifdef I40E_FCOE
9402 .ndo_fcoe_enable = i40e_fcoe_enable,
9403 .ndo_fcoe_disable = i40e_fcoe_disable,
9404#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009405 .ndo_set_features = i40e_set_features,
9406 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9407 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009408 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009409 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009410 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009411 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009412 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009413 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9414 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009415 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009416 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009417 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009418 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9419 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009420};
9421
9422/**
9423 * i40e_config_netdev - Setup the netdev flags
9424 * @vsi: the VSI being configured
9425 *
9426 * Returns 0 on success, negative value on failure
9427 **/
9428static int i40e_config_netdev(struct i40e_vsi *vsi)
9429{
9430 struct i40e_pf *pf = vsi->back;
9431 struct i40e_hw *hw = &pf->hw;
9432 struct i40e_netdev_priv *np;
9433 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009434 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009435 u8 mac_addr[ETH_ALEN];
9436 int etherdev_size;
9437
9438 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009439 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009440 if (!netdev)
9441 return -ENOMEM;
9442
9443 vsi->netdev = netdev;
9444 np = netdev_priv(netdev);
9445 np->vsi = vsi;
9446
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009447 netdev->hw_enc_features |= NETIF_F_SG |
9448 NETIF_F_IP_CSUM |
9449 NETIF_F_IPV6_CSUM |
9450 NETIF_F_HIGHDMA |
9451 NETIF_F_SOFT_FEATURES |
9452 NETIF_F_TSO |
9453 NETIF_F_TSO_ECN |
9454 NETIF_F_TSO6 |
9455 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009456 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009457 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009458 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009459 NETIF_F_GSO_UDP_TUNNEL |
9460 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009461 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009462 NETIF_F_SCTP_CRC |
9463 NETIF_F_RXHASH |
9464 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009465 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009466
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009467 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009468 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9469
9470 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009471
9472 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009473 netdev->vlan_features |= netdev->hw_enc_features |
9474 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009475
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009476 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009477 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009478
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009479 netdev->hw_features |= netdev->hw_enc_features |
9480 NETIF_F_HW_VLAN_CTAG_TX |
9481 NETIF_F_HW_VLAN_CTAG_RX;
9482
9483 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009484 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009485
9486 if (vsi->type == I40E_VSI_MAIN) {
9487 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009488 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Filip Sadowski3954b392017-01-30 12:29:34 -08009489 /* The following steps are necessary to properly keep track of
9490 * MAC-VLAN filters loaded into firmware - first we remove
9491 * filter that is automatically generated by firmware and then
9492 * add new filter both to the driver hash table and firmware.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009493 */
9494 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009495 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009496 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009497 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009498 } else {
9499 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9500 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9501 pf->vsi[pf->lan_vsi]->netdev->name);
9502 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009503
Jacob Keller278e7d02016-10-05 09:30:37 -07009504 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009505 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009506 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009507 }
Kiran Patil21659032015-09-30 14:09:03 -04009508
Jacob Keller435c0842016-11-08 13:05:10 -08009509 /* Add the broadcast filter so that we initially will receive
9510 * broadcast packets. Note that when a new VLAN is first added the
9511 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9512 * specific filters as part of transitioning into "vlan" operation.
9513 * When more VLANs are added, the driver will copy each existing MAC
9514 * filter and add it for the new VLAN.
9515 *
9516 * Broadcast filters are handled specially by
9517 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9518 * promiscuous bit instead of adding this directly as a MAC/VLAN
9519 * filter. The subtask will update the correct broadcast promiscuous
9520 * bits as VLANs become active or inactive.
9521 */
9522 eth_broadcast_addr(broadcast);
9523 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009524 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009525 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9526
Greg Rose9a173902014-05-22 06:32:02 +00009527 ether_addr_copy(netdev->dev_addr, mac_addr);
9528 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530 netdev->priv_flags |= IFF_UNICAST_FLT;
9531 netdev->priv_flags |= IFF_SUPP_NOFCS;
9532 /* Setup netdev TC information */
9533 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9534
9535 netdev->netdev_ops = &i40e_netdev_ops;
9536 netdev->watchdog_timeo = 5 * HZ;
9537 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009538#ifdef I40E_FCOE
9539 i40e_fcoe_config_netdev(netdev, vsi);
9540#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009541
Jarod Wilson91c527a2016-10-17 15:54:05 -04009542 /* MTU range: 68 - 9706 */
9543 netdev->min_mtu = ETH_MIN_MTU;
9544 netdev->max_mtu = I40E_MAX_RXBUFFER -
9545 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009547 return 0;
9548}
9549
9550/**
9551 * i40e_vsi_delete - Delete a VSI from the switch
9552 * @vsi: the VSI being removed
9553 *
9554 * Returns 0 on success, negative value on failure
9555 **/
9556static void i40e_vsi_delete(struct i40e_vsi *vsi)
9557{
9558 /* remove default VSI is not allowed */
9559 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9560 return;
9561
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009562 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009563}
9564
9565/**
Neerav Parikh51616012015-02-06 08:52:14 +00009566 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9567 * @vsi: the VSI being queried
9568 *
9569 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9570 **/
9571int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9572{
9573 struct i40e_veb *veb;
9574 struct i40e_pf *pf = vsi->back;
9575
9576 /* Uplink is not a bridge so default to VEB */
9577 if (vsi->veb_idx == I40E_NO_VEB)
9578 return 1;
9579
9580 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009581 if (!veb) {
9582 dev_info(&pf->pdev->dev,
9583 "There is no veb associated with the bridge\n");
9584 return -ENOENT;
9585 }
Neerav Parikh51616012015-02-06 08:52:14 +00009586
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009587 /* Uplink is a bridge in VEPA mode */
9588 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9589 return 0;
9590 } else {
9591 /* Uplink is a bridge in VEB mode */
9592 return 1;
9593 }
9594
9595 /* VEPA is now default bridge, so return 0 */
9596 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009597}
9598
9599/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009600 * i40e_add_vsi - Add a VSI to the switch
9601 * @vsi: the VSI being configured
9602 *
9603 * This initializes a VSI context depending on the VSI type to be added and
9604 * passes it down to the add_vsi aq command.
9605 **/
9606static int i40e_add_vsi(struct i40e_vsi *vsi)
9607{
9608 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009609 struct i40e_pf *pf = vsi->back;
9610 struct i40e_hw *hw = &pf->hw;
9611 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009612 struct i40e_mac_filter *f;
9613 struct hlist_node *h;
9614 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009616 u8 enabled_tc = 0x1; /* TC0 enabled */
9617 int f_count = 0;
9618
9619 memset(&ctxt, 0, sizeof(ctxt));
9620 switch (vsi->type) {
9621 case I40E_VSI_MAIN:
9622 /* The PF's main VSI is already setup as part of the
9623 * device initialization, so we'll not bother with
9624 * the add_vsi call, but we will retrieve the current
9625 * VSI context.
9626 */
9627 ctxt.seid = pf->main_vsi_seid;
9628 ctxt.pf_num = pf->hw.pf_id;
9629 ctxt.vf_num = 0;
9630 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9631 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9632 if (ret) {
9633 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009634 "couldn't get PF vsi config, err %s aq_err %s\n",
9635 i40e_stat_str(&pf->hw, ret),
9636 i40e_aq_str(&pf->hw,
9637 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009638 return -ENOENT;
9639 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009640 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009641 vsi->info.valid_sections = 0;
9642
9643 vsi->seid = ctxt.seid;
9644 vsi->id = ctxt.vsi_number;
9645
9646 enabled_tc = i40e_pf_get_tc_map(pf);
9647
9648 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009649 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9650 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009651 memset(&ctxt, 0, sizeof(ctxt));
9652 ctxt.seid = pf->main_vsi_seid;
9653 ctxt.pf_num = pf->hw.pf_id;
9654 ctxt.vf_num = 0;
9655 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9656 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9657 if (ret) {
9658 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009659 "update vsi failed, err %s aq_err %s\n",
9660 i40e_stat_str(&pf->hw, ret),
9661 i40e_aq_str(&pf->hw,
9662 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009663 ret = -ENOENT;
9664 goto err;
9665 }
9666 /* update the local VSI info queue map */
9667 i40e_vsi_update_queue_map(vsi, &ctxt);
9668 vsi->info.valid_sections = 0;
9669 } else {
9670 /* Default/Main VSI is only enabled for TC0
9671 * reconfigure it to enable all TCs that are
9672 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009673 * For MFP case the iSCSI PF would use this
9674 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009675 */
9676 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9677 if (ret) {
9678 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009679 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9680 enabled_tc,
9681 i40e_stat_str(&pf->hw, ret),
9682 i40e_aq_str(&pf->hw,
9683 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009684 ret = -ENOENT;
9685 }
9686 }
9687 break;
9688
9689 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009690 ctxt.pf_num = hw->pf_id;
9691 ctxt.vf_num = 0;
9692 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009693 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009694 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009695 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9696 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009697 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009698 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009699 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009700 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009701 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009702 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009703 break;
9704
9705 case I40E_VSI_VMDQ2:
9706 ctxt.pf_num = hw->pf_id;
9707 ctxt.vf_num = 0;
9708 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009709 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009710 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9711
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009712 /* This VSI is connected to VEB so the switch_id
9713 * should be set to zero by default.
9714 */
Neerav Parikh51616012015-02-06 08:52:14 +00009715 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9716 ctxt.info.valid_sections |=
9717 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9718 ctxt.info.switch_id =
9719 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9720 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009721
9722 /* Setup the VSI tx/rx queue map for TC0 only for now */
9723 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9724 break;
9725
9726 case I40E_VSI_SRIOV:
9727 ctxt.pf_num = hw->pf_id;
9728 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9729 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009730 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009731 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9732
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009733 /* This VSI is connected to VEB so the switch_id
9734 * should be set to zero by default.
9735 */
Neerav Parikh51616012015-02-06 08:52:14 +00009736 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9737 ctxt.info.valid_sections |=
9738 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9739 ctxt.info.switch_id =
9740 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9741 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009742
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009743 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9744 ctxt.info.valid_sections |=
9745 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9746 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009747 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9748 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009749 }
9750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009751 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9752 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009753 if (pf->vf[vsi->vf_id].spoofchk) {
9754 ctxt.info.valid_sections |=
9755 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9756 ctxt.info.sec_flags |=
9757 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9758 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9759 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009760 /* Setup the VSI tx/rx queue map for TC0 only for now */
9761 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9762 break;
9763
Vasu Dev38e00432014-08-01 13:27:03 -07009764#ifdef I40E_FCOE
9765 case I40E_VSI_FCOE:
9766 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9767 if (ret) {
9768 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9769 return ret;
9770 }
9771 break;
9772
9773#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009774 case I40E_VSI_IWARP:
9775 /* send down message to iWARP */
9776 break;
9777
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009778 default:
9779 return -ENODEV;
9780 }
9781
9782 if (vsi->type != I40E_VSI_MAIN) {
9783 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9784 if (ret) {
9785 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009786 "add vsi failed, err %s aq_err %s\n",
9787 i40e_stat_str(&pf->hw, ret),
9788 i40e_aq_str(&pf->hw,
9789 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009790 ret = -ENOENT;
9791 goto err;
9792 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009793 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009794 vsi->info.valid_sections = 0;
9795 vsi->seid = ctxt.seid;
9796 vsi->id = ctxt.vsi_number;
9797 }
9798
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009799 vsi->active_filters = 0;
9800 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009801 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009802 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009803 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009804 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009805 f_count++;
9806 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009807 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009808
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009809 if (f_count) {
9810 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9811 pf->flags |= I40E_FLAG_FILTER_SYNC;
9812 }
9813
9814 /* Update VSI BW information */
9815 ret = i40e_vsi_get_bw_info(vsi);
9816 if (ret) {
9817 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009818 "couldn't get vsi bw info, err %s aq_err %s\n",
9819 i40e_stat_str(&pf->hw, ret),
9820 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009821 /* VSI is already added so not tearing that up */
9822 ret = 0;
9823 }
9824
9825err:
9826 return ret;
9827}
9828
9829/**
9830 * i40e_vsi_release - Delete a VSI and free its resources
9831 * @vsi: the VSI being removed
9832 *
9833 * Returns 0 on success or < 0 on error
9834 **/
9835int i40e_vsi_release(struct i40e_vsi *vsi)
9836{
Jacob Keller278e7d02016-10-05 09:30:37 -07009837 struct i40e_mac_filter *f;
9838 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009839 struct i40e_veb *veb = NULL;
9840 struct i40e_pf *pf;
9841 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009842 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009843
9844 pf = vsi->back;
9845
9846 /* release of a VEB-owner or last VSI is not allowed */
9847 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9848 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9849 vsi->seid, vsi->uplink_seid);
9850 return -ENODEV;
9851 }
9852 if (vsi == pf->vsi[pf->lan_vsi] &&
9853 !test_bit(__I40E_DOWN, &pf->state)) {
9854 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9855 return -ENODEV;
9856 }
9857
9858 uplink_seid = vsi->uplink_seid;
9859 if (vsi->type != I40E_VSI_SRIOV) {
9860 if (vsi->netdev_registered) {
9861 vsi->netdev_registered = false;
9862 if (vsi->netdev) {
9863 /* results in a call to i40e_close() */
9864 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009865 }
9866 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009867 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009868 }
9869 i40e_vsi_disable_irq(vsi);
9870 }
9871
Jacob Keller278e7d02016-10-05 09:30:37 -07009872 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009873
9874 /* clear the sync flag on all filters */
9875 if (vsi->netdev) {
9876 __dev_uc_unsync(vsi->netdev, NULL);
9877 __dev_mc_unsync(vsi->netdev, NULL);
9878 }
9879
9880 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009881 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009882 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009883
Jacob Keller278e7d02016-10-05 09:30:37 -07009884 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009885
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009886 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009887
9888 i40e_vsi_delete(vsi);
9889 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009890 if (vsi->netdev) {
9891 free_netdev(vsi->netdev);
9892 vsi->netdev = NULL;
9893 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009894 i40e_vsi_clear_rings(vsi);
9895 i40e_vsi_clear(vsi);
9896
9897 /* If this was the last thing on the VEB, except for the
9898 * controlling VSI, remove the VEB, which puts the controlling
9899 * VSI onto the next level down in the switch.
9900 *
9901 * Well, okay, there's one more exception here: don't remove
9902 * the orphan VEBs yet. We'll wait for an explicit remove request
9903 * from up the network stack.
9904 */
Mitch Williams505682c2014-05-20 08:01:37 +00009905 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009906 if (pf->vsi[i] &&
9907 pf->vsi[i]->uplink_seid == uplink_seid &&
9908 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9909 n++; /* count the VSIs */
9910 }
9911 }
9912 for (i = 0; i < I40E_MAX_VEB; i++) {
9913 if (!pf->veb[i])
9914 continue;
9915 if (pf->veb[i]->uplink_seid == uplink_seid)
9916 n++; /* count the VEBs */
9917 if (pf->veb[i]->seid == uplink_seid)
9918 veb = pf->veb[i];
9919 }
9920 if (n == 0 && veb && veb->uplink_seid != 0)
9921 i40e_veb_release(veb);
9922
9923 return 0;
9924}
9925
9926/**
9927 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9928 * @vsi: ptr to the VSI
9929 *
9930 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9931 * corresponding SW VSI structure and initializes num_queue_pairs for the
9932 * newly allocated VSI.
9933 *
9934 * Returns 0 on success or negative on failure
9935 **/
9936static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9937{
9938 int ret = -ENOENT;
9939 struct i40e_pf *pf = vsi->back;
9940
Alexander Duyck493fb302013-09-28 07:01:44 +00009941 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009942 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9943 vsi->seid);
9944 return -EEXIST;
9945 }
9946
9947 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009948 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009949 vsi->seid, vsi->base_vector);
9950 return -EEXIST;
9951 }
9952
Greg Rose90e04072014-03-06 08:59:57 +00009953 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009954 if (ret) {
9955 dev_info(&pf->pdev->dev,
9956 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9957 vsi->num_q_vectors, vsi->seid, ret);
9958 vsi->num_q_vectors = 0;
9959 goto vector_setup_out;
9960 }
9961
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009962 /* In Legacy mode, we do not have to get any other vector since we
9963 * piggyback on the misc/ICR0 for queue interrupts.
9964 */
9965 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9966 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009967 if (vsi->num_q_vectors)
9968 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9969 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009970 if (vsi->base_vector < 0) {
9971 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009972 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9973 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009974 i40e_vsi_free_q_vectors(vsi);
9975 ret = -ENOENT;
9976 goto vector_setup_out;
9977 }
9978
9979vector_setup_out:
9980 return ret;
9981}
9982
9983/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009984 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9985 * @vsi: pointer to the vsi.
9986 *
9987 * This re-allocates a vsi's queue resources.
9988 *
9989 * Returns pointer to the successfully allocated and configured VSI sw struct
9990 * on success, otherwise returns NULL on failure.
9991 **/
9992static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9993{
John Underwoodf5340392016-02-18 09:19:24 -08009994 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009995 u8 enabled_tc;
9996 int ret;
9997
John Underwoodf5340392016-02-18 09:19:24 -08009998 if (!vsi)
9999 return NULL;
10000
10001 pf = vsi->back;
10002
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010003 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10004 i40e_vsi_clear_rings(vsi);
10005
10006 i40e_vsi_free_arrays(vsi, false);
10007 i40e_set_num_rings_in_vsi(vsi);
10008 ret = i40e_vsi_alloc_arrays(vsi, false);
10009 if (ret)
10010 goto err_vsi;
10011
10012 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
10013 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010014 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010015 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +000010016 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010017 goto err_vsi;
10018 }
10019 vsi->base_queue = ret;
10020
10021 /* Update the FW view of the VSI. Force a reset of TC and queue
10022 * layout configurations.
10023 */
10024 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
10025 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10026 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10027 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -080010028 if (vsi->type == I40E_VSI_MAIN)
10029 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010030
10031 /* assign it some queues */
10032 ret = i40e_alloc_rings(vsi);
10033 if (ret)
10034 goto err_rings;
10035
10036 /* map all of the rings to the q_vectors */
10037 i40e_vsi_map_rings_to_vectors(vsi);
10038 return vsi;
10039
10040err_rings:
10041 i40e_vsi_free_q_vectors(vsi);
10042 if (vsi->netdev_registered) {
10043 vsi->netdev_registered = false;
10044 unregister_netdev(vsi->netdev);
10045 free_netdev(vsi->netdev);
10046 vsi->netdev = NULL;
10047 }
10048 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10049err_vsi:
10050 i40e_vsi_clear(vsi);
10051 return NULL;
10052}
10053
10054/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010055 * i40e_vsi_setup - Set up a VSI by a given type
10056 * @pf: board private structure
10057 * @type: VSI type
10058 * @uplink_seid: the switch element to link to
10059 * @param1: usage depends upon VSI type. For VF types, indicates VF id
10060 *
10061 * This allocates the sw VSI structure and its queue resources, then add a VSI
10062 * to the identified VEB.
10063 *
10064 * Returns pointer to the successfully allocated and configure VSI sw struct on
10065 * success, otherwise returns NULL on failure.
10066 **/
10067struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
10068 u16 uplink_seid, u32 param1)
10069{
10070 struct i40e_vsi *vsi = NULL;
10071 struct i40e_veb *veb = NULL;
10072 int ret, i;
10073 int v_idx;
10074
10075 /* The requested uplink_seid must be either
10076 * - the PF's port seid
10077 * no VEB is needed because this is the PF
10078 * or this is a Flow Director special case VSI
10079 * - seid of an existing VEB
10080 * - seid of a VSI that owns an existing VEB
10081 * - seid of a VSI that doesn't own a VEB
10082 * a new VEB is created and the VSI becomes the owner
10083 * - seid of the PF VSI, which is what creates the first VEB
10084 * this is a special case of the previous
10085 *
10086 * Find which uplink_seid we were given and create a new VEB if needed
10087 */
10088 for (i = 0; i < I40E_MAX_VEB; i++) {
10089 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
10090 veb = pf->veb[i];
10091 break;
10092 }
10093 }
10094
10095 if (!veb && uplink_seid != pf->mac_seid) {
10096
Mitch Williams505682c2014-05-20 08:01:37 +000010097 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010098 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
10099 vsi = pf->vsi[i];
10100 break;
10101 }
10102 }
10103 if (!vsi) {
10104 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
10105 uplink_seid);
10106 return NULL;
10107 }
10108
10109 if (vsi->uplink_seid == pf->mac_seid)
10110 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
10111 vsi->tc_config.enabled_tc);
10112 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
10113 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
10114 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010115 if (veb) {
10116 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
10117 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040010118 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010119 return NULL;
10120 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010121 /* We come up by default in VEPA mode if SRIOV is not
10122 * already enabled, in which case we can't force VEPA
10123 * mode.
10124 */
10125 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
10126 veb->bridge_mode = BRIDGE_MODE_VEPA;
10127 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
10128 }
Neerav Parikh51616012015-02-06 08:52:14 +000010129 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +000010130 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010131 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
10132 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
10133 veb = pf->veb[i];
10134 }
10135 if (!veb) {
10136 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
10137 return NULL;
10138 }
10139
10140 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10141 uplink_seid = veb->seid;
10142 }
10143
10144 /* get vsi sw struct */
10145 v_idx = i40e_vsi_mem_alloc(pf, type);
10146 if (v_idx < 0)
10147 goto err_alloc;
10148 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010149 if (!vsi)
10150 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010151 vsi->type = type;
10152 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
10153
10154 if (type == I40E_VSI_MAIN)
10155 pf->lan_vsi = v_idx;
10156 else if (type == I40E_VSI_SRIOV)
10157 vsi->vf_id = param1;
10158 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010159 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
10160 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010161 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +000010162 dev_info(&pf->pdev->dev,
10163 "failed to get tracking for %d queues for VSI %d err=%d\n",
10164 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010165 goto err_vsi;
10166 }
10167 vsi->base_queue = ret;
10168
10169 /* get a VSI from the hardware */
10170 vsi->uplink_seid = uplink_seid;
10171 ret = i40e_add_vsi(vsi);
10172 if (ret)
10173 goto err_vsi;
10174
10175 switch (vsi->type) {
10176 /* setup the netdev if needed */
10177 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010178 /* Apply relevant filters if a platform-specific mac
10179 * address was selected.
10180 */
10181 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10182 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10183 if (ret) {
10184 dev_warn(&pf->pdev->dev,
10185 "could not set up macaddr; err %d\n",
10186 ret);
10187 }
10188 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010189 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -070010190 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010191 ret = i40e_config_netdev(vsi);
10192 if (ret)
10193 goto err_netdev;
10194 ret = register_netdev(vsi->netdev);
10195 if (ret)
10196 goto err_netdev;
10197 vsi->netdev_registered = true;
10198 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010199#ifdef CONFIG_I40E_DCB
10200 /* Setup DCB netlink interface */
10201 i40e_dcbnl_setup(vsi);
10202#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010203 /* fall through */
10204
10205 case I40E_VSI_FDIR:
10206 /* set up vectors and rings if needed */
10207 ret = i40e_vsi_setup_vectors(vsi);
10208 if (ret)
10209 goto err_msix;
10210
10211 ret = i40e_alloc_rings(vsi);
10212 if (ret)
10213 goto err_rings;
10214
10215 /* map all of the rings to the q_vectors */
10216 i40e_vsi_map_rings_to_vectors(vsi);
10217
10218 i40e_vsi_reset_stats(vsi);
10219 break;
10220
10221 default:
10222 /* no netdev or rings for the other VSI types */
10223 break;
10224 }
10225
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010226 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10227 (vsi->type == I40E_VSI_VMDQ2)) {
10228 ret = i40e_vsi_config_rss(vsi);
10229 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010230 return vsi;
10231
10232err_rings:
10233 i40e_vsi_free_q_vectors(vsi);
10234err_msix:
10235 if (vsi->netdev_registered) {
10236 vsi->netdev_registered = false;
10237 unregister_netdev(vsi->netdev);
10238 free_netdev(vsi->netdev);
10239 vsi->netdev = NULL;
10240 }
10241err_netdev:
10242 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10243err_vsi:
10244 i40e_vsi_clear(vsi);
10245err_alloc:
10246 return NULL;
10247}
10248
10249/**
10250 * i40e_veb_get_bw_info - Query VEB BW information
10251 * @veb: the veb to query
10252 *
10253 * Query the Tx scheduler BW configuration data for given VEB
10254 **/
10255static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10256{
10257 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10258 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10259 struct i40e_pf *pf = veb->pf;
10260 struct i40e_hw *hw = &pf->hw;
10261 u32 tc_bw_max;
10262 int ret = 0;
10263 int i;
10264
10265 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10266 &bw_data, NULL);
10267 if (ret) {
10268 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010269 "query veb bw config failed, err %s aq_err %s\n",
10270 i40e_stat_str(&pf->hw, ret),
10271 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010272 goto out;
10273 }
10274
10275 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10276 &ets_data, NULL);
10277 if (ret) {
10278 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010279 "query veb bw ets config failed, err %s aq_err %s\n",
10280 i40e_stat_str(&pf->hw, ret),
10281 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010282 goto out;
10283 }
10284
10285 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10286 veb->bw_max_quanta = ets_data.tc_bw_max;
10287 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010288 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010289 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10290 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10291 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10292 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10293 veb->bw_tc_limit_credits[i] =
10294 le16_to_cpu(bw_data.tc_bw_limits[i]);
10295 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10296 }
10297
10298out:
10299 return ret;
10300}
10301
10302/**
10303 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10304 * @pf: board private structure
10305 *
10306 * On error: returns error code (negative)
10307 * On success: returns vsi index in PF (positive)
10308 **/
10309static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10310{
10311 int ret = -ENOENT;
10312 struct i40e_veb *veb;
10313 int i;
10314
10315 /* Need to protect the allocation of switch elements at the PF level */
10316 mutex_lock(&pf->switch_mutex);
10317
10318 /* VEB list may be fragmented if VEB creation/destruction has
10319 * been happening. We can afford to do a quick scan to look
10320 * for any free slots in the list.
10321 *
10322 * find next empty veb slot, looping back around if necessary
10323 */
10324 i = 0;
10325 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10326 i++;
10327 if (i >= I40E_MAX_VEB) {
10328 ret = -ENOMEM;
10329 goto err_alloc_veb; /* out of VEB slots! */
10330 }
10331
10332 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10333 if (!veb) {
10334 ret = -ENOMEM;
10335 goto err_alloc_veb;
10336 }
10337 veb->pf = pf;
10338 veb->idx = i;
10339 veb->enabled_tc = 1;
10340
10341 pf->veb[i] = veb;
10342 ret = i;
10343err_alloc_veb:
10344 mutex_unlock(&pf->switch_mutex);
10345 return ret;
10346}
10347
10348/**
10349 * i40e_switch_branch_release - Delete a branch of the switch tree
10350 * @branch: where to start deleting
10351 *
10352 * This uses recursion to find the tips of the branch to be
10353 * removed, deleting until we get back to and can delete this VEB.
10354 **/
10355static void i40e_switch_branch_release(struct i40e_veb *branch)
10356{
10357 struct i40e_pf *pf = branch->pf;
10358 u16 branch_seid = branch->seid;
10359 u16 veb_idx = branch->idx;
10360 int i;
10361
10362 /* release any VEBs on this VEB - RECURSION */
10363 for (i = 0; i < I40E_MAX_VEB; i++) {
10364 if (!pf->veb[i])
10365 continue;
10366 if (pf->veb[i]->uplink_seid == branch->seid)
10367 i40e_switch_branch_release(pf->veb[i]);
10368 }
10369
10370 /* Release the VSIs on this VEB, but not the owner VSI.
10371 *
10372 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10373 * the VEB itself, so don't use (*branch) after this loop.
10374 */
Mitch Williams505682c2014-05-20 08:01:37 +000010375 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010376 if (!pf->vsi[i])
10377 continue;
10378 if (pf->vsi[i]->uplink_seid == branch_seid &&
10379 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10380 i40e_vsi_release(pf->vsi[i]);
10381 }
10382 }
10383
10384 /* There's one corner case where the VEB might not have been
10385 * removed, so double check it here and remove it if needed.
10386 * This case happens if the veb was created from the debugfs
10387 * commands and no VSIs were added to it.
10388 */
10389 if (pf->veb[veb_idx])
10390 i40e_veb_release(pf->veb[veb_idx]);
10391}
10392
10393/**
10394 * i40e_veb_clear - remove veb struct
10395 * @veb: the veb to remove
10396 **/
10397static void i40e_veb_clear(struct i40e_veb *veb)
10398{
10399 if (!veb)
10400 return;
10401
10402 if (veb->pf) {
10403 struct i40e_pf *pf = veb->pf;
10404
10405 mutex_lock(&pf->switch_mutex);
10406 if (pf->veb[veb->idx] == veb)
10407 pf->veb[veb->idx] = NULL;
10408 mutex_unlock(&pf->switch_mutex);
10409 }
10410
10411 kfree(veb);
10412}
10413
10414/**
10415 * i40e_veb_release - Delete a VEB and free its resources
10416 * @veb: the VEB being removed
10417 **/
10418void i40e_veb_release(struct i40e_veb *veb)
10419{
10420 struct i40e_vsi *vsi = NULL;
10421 struct i40e_pf *pf;
10422 int i, n = 0;
10423
10424 pf = veb->pf;
10425
10426 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010427 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010428 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10429 n++;
10430 vsi = pf->vsi[i];
10431 }
10432 }
10433 if (n != 1) {
10434 dev_info(&pf->pdev->dev,
10435 "can't remove VEB %d with %d VSIs left\n",
10436 veb->seid, n);
10437 return;
10438 }
10439
10440 /* move the remaining VSI to uplink veb */
10441 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10442 if (veb->uplink_seid) {
10443 vsi->uplink_seid = veb->uplink_seid;
10444 if (veb->uplink_seid == pf->mac_seid)
10445 vsi->veb_idx = I40E_NO_VEB;
10446 else
10447 vsi->veb_idx = veb->veb_idx;
10448 } else {
10449 /* floating VEB */
10450 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10451 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10452 }
10453
10454 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10455 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010456}
10457
10458/**
10459 * i40e_add_veb - create the VEB in the switch
10460 * @veb: the VEB to be instantiated
10461 * @vsi: the controlling VSI
10462 **/
10463static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10464{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010465 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010466 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010467 int ret;
10468
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010469 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010470 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010471 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010472
10473 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010474 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010475 dev_info(&pf->pdev->dev,
10476 "couldn't add VEB, err %s aq_err %s\n",
10477 i40e_stat_str(&pf->hw, ret),
10478 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010479 return -EPERM;
10480 }
10481
10482 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010483 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010484 &veb->stats_idx, NULL, NULL, NULL);
10485 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010486 dev_info(&pf->pdev->dev,
10487 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10488 i40e_stat_str(&pf->hw, ret),
10489 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010490 return -EPERM;
10491 }
10492 ret = i40e_veb_get_bw_info(veb);
10493 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010494 dev_info(&pf->pdev->dev,
10495 "couldn't get VEB bw info, 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));
10498 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499 return -ENOENT;
10500 }
10501
10502 vsi->uplink_seid = veb->seid;
10503 vsi->veb_idx = veb->idx;
10504 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10505
10506 return 0;
10507}
10508
10509/**
10510 * i40e_veb_setup - Set up a VEB
10511 * @pf: board private structure
10512 * @flags: VEB setup flags
10513 * @uplink_seid: the switch element to link to
10514 * @vsi_seid: the initial VSI seid
10515 * @enabled_tc: Enabled TC bit-map
10516 *
10517 * This allocates the sw VEB structure and links it into the switch
10518 * It is possible and legal for this to be a duplicate of an already
10519 * existing VEB. It is also possible for both uplink and vsi seids
10520 * to be zero, in order to create a floating VEB.
10521 *
10522 * Returns pointer to the successfully allocated VEB sw struct on
10523 * success, otherwise returns NULL on failure.
10524 **/
10525struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10526 u16 uplink_seid, u16 vsi_seid,
10527 u8 enabled_tc)
10528{
10529 struct i40e_veb *veb, *uplink_veb = NULL;
10530 int vsi_idx, veb_idx;
10531 int ret;
10532
10533 /* if one seid is 0, the other must be 0 to create a floating relay */
10534 if ((uplink_seid == 0 || vsi_seid == 0) &&
10535 (uplink_seid + vsi_seid != 0)) {
10536 dev_info(&pf->pdev->dev,
10537 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10538 uplink_seid, vsi_seid);
10539 return NULL;
10540 }
10541
10542 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010543 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010544 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10545 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010546 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010547 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10548 vsi_seid);
10549 return NULL;
10550 }
10551
10552 if (uplink_seid && uplink_seid != pf->mac_seid) {
10553 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10554 if (pf->veb[veb_idx] &&
10555 pf->veb[veb_idx]->seid == uplink_seid) {
10556 uplink_veb = pf->veb[veb_idx];
10557 break;
10558 }
10559 }
10560 if (!uplink_veb) {
10561 dev_info(&pf->pdev->dev,
10562 "uplink seid %d not found\n", uplink_seid);
10563 return NULL;
10564 }
10565 }
10566
10567 /* get veb sw struct */
10568 veb_idx = i40e_veb_mem_alloc(pf);
10569 if (veb_idx < 0)
10570 goto err_alloc;
10571 veb = pf->veb[veb_idx];
10572 veb->flags = flags;
10573 veb->uplink_seid = uplink_seid;
10574 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10575 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10576
10577 /* create the VEB in the switch */
10578 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10579 if (ret)
10580 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010581 if (vsi_idx == pf->lan_vsi)
10582 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010583
10584 return veb;
10585
10586err_veb:
10587 i40e_veb_clear(veb);
10588err_alloc:
10589 return NULL;
10590}
10591
10592/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010593 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010594 * @pf: board private structure
10595 * @ele: element we are building info from
10596 * @num_reported: total number of elements
10597 * @printconfig: should we print the contents
10598 *
10599 * helper function to assist in extracting a few useful SEID values.
10600 **/
10601static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10602 struct i40e_aqc_switch_config_element_resp *ele,
10603 u16 num_reported, bool printconfig)
10604{
10605 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10606 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10607 u8 element_type = ele->element_type;
10608 u16 seid = le16_to_cpu(ele->seid);
10609
10610 if (printconfig)
10611 dev_info(&pf->pdev->dev,
10612 "type=%d seid=%d uplink=%d downlink=%d\n",
10613 element_type, seid, uplink_seid, downlink_seid);
10614
10615 switch (element_type) {
10616 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10617 pf->mac_seid = seid;
10618 break;
10619 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10620 /* Main VEB? */
10621 if (uplink_seid != pf->mac_seid)
10622 break;
10623 if (pf->lan_veb == I40E_NO_VEB) {
10624 int v;
10625
10626 /* find existing or else empty VEB */
10627 for (v = 0; v < I40E_MAX_VEB; v++) {
10628 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10629 pf->lan_veb = v;
10630 break;
10631 }
10632 }
10633 if (pf->lan_veb == I40E_NO_VEB) {
10634 v = i40e_veb_mem_alloc(pf);
10635 if (v < 0)
10636 break;
10637 pf->lan_veb = v;
10638 }
10639 }
10640
10641 pf->veb[pf->lan_veb]->seid = seid;
10642 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10643 pf->veb[pf->lan_veb]->pf = pf;
10644 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10645 break;
10646 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10647 if (num_reported != 1)
10648 break;
10649 /* This is immediately after a reset so we can assume this is
10650 * the PF's VSI
10651 */
10652 pf->mac_seid = uplink_seid;
10653 pf->pf_seid = downlink_seid;
10654 pf->main_vsi_seid = seid;
10655 if (printconfig)
10656 dev_info(&pf->pdev->dev,
10657 "pf_seid=%d main_vsi_seid=%d\n",
10658 pf->pf_seid, pf->main_vsi_seid);
10659 break;
10660 case I40E_SWITCH_ELEMENT_TYPE_PF:
10661 case I40E_SWITCH_ELEMENT_TYPE_VF:
10662 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10663 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10664 case I40E_SWITCH_ELEMENT_TYPE_PE:
10665 case I40E_SWITCH_ELEMENT_TYPE_PA:
10666 /* ignore these for now */
10667 break;
10668 default:
10669 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10670 element_type, seid);
10671 break;
10672 }
10673}
10674
10675/**
10676 * i40e_fetch_switch_configuration - Get switch config from firmware
10677 * @pf: board private structure
10678 * @printconfig: should we print the contents
10679 *
10680 * Get the current switch configuration from the device and
10681 * extract a few useful SEID values.
10682 **/
10683int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10684{
10685 struct i40e_aqc_get_switch_config_resp *sw_config;
10686 u16 next_seid = 0;
10687 int ret = 0;
10688 u8 *aq_buf;
10689 int i;
10690
10691 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10692 if (!aq_buf)
10693 return -ENOMEM;
10694
10695 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10696 do {
10697 u16 num_reported, num_total;
10698
10699 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10700 I40E_AQ_LARGE_BUF,
10701 &next_seid, NULL);
10702 if (ret) {
10703 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010704 "get switch config failed err %s aq_err %s\n",
10705 i40e_stat_str(&pf->hw, ret),
10706 i40e_aq_str(&pf->hw,
10707 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010708 kfree(aq_buf);
10709 return -ENOENT;
10710 }
10711
10712 num_reported = le16_to_cpu(sw_config->header.num_reported);
10713 num_total = le16_to_cpu(sw_config->header.num_total);
10714
10715 if (printconfig)
10716 dev_info(&pf->pdev->dev,
10717 "header: %d reported %d total\n",
10718 num_reported, num_total);
10719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010720 for (i = 0; i < num_reported; i++) {
10721 struct i40e_aqc_switch_config_element_resp *ele =
10722 &sw_config->element[i];
10723
10724 i40e_setup_pf_switch_element(pf, ele, num_reported,
10725 printconfig);
10726 }
10727 } while (next_seid != 0);
10728
10729 kfree(aq_buf);
10730 return ret;
10731}
10732
10733/**
10734 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10735 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010736 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010737 *
10738 * Returns 0 on success, negative value on failure
10739 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010740static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010741{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010742 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010743 int ret;
10744
10745 /* find out what's out there already */
10746 ret = i40e_fetch_switch_configuration(pf, false);
10747 if (ret) {
10748 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010749 "couldn't fetch switch config, err %s aq_err %s\n",
10750 i40e_stat_str(&pf->hw, ret),
10751 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010752 return ret;
10753 }
10754 i40e_pf_reset_stats(pf);
10755
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010756 /* set the switch config bit for the whole device to
10757 * support limited promisc or true promisc
10758 * when user requests promisc. The default is limited
10759 * promisc.
10760 */
10761
10762 if ((pf->hw.pf_id == 0) &&
10763 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10764 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10765
10766 if (pf->hw.pf_id == 0) {
10767 u16 valid_flags;
10768
10769 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10770 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10771 NULL);
10772 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10773 dev_info(&pf->pdev->dev,
10774 "couldn't set switch config bits, err %s aq_err %s\n",
10775 i40e_stat_str(&pf->hw, ret),
10776 i40e_aq_str(&pf->hw,
10777 pf->hw.aq.asq_last_status));
10778 /* not a fatal problem, just keep going */
10779 }
10780 }
10781
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010782 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010783 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010784 struct i40e_vsi *vsi = NULL;
10785 u16 uplink_seid;
10786
10787 /* Set up the PF VSI associated with the PF's main VSI
10788 * that is already in the HW switch
10789 */
10790 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10791 uplink_seid = pf->veb[pf->lan_veb]->seid;
10792 else
10793 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010794 if (pf->lan_vsi == I40E_NO_VSI)
10795 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10796 else if (reinit)
10797 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010798 if (!vsi) {
10799 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10800 i40e_fdir_teardown(pf);
10801 return -EAGAIN;
10802 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010803 } else {
10804 /* force a reset of TC and queue layout configurations */
10805 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010807 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10808 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10809 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10810 }
10811 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10812
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010813 i40e_fdir_sb_setup(pf);
10814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010815 /* Setup static PF queue filter control settings */
10816 ret = i40e_setup_pf_filter_control(pf);
10817 if (ret) {
10818 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10819 ret);
10820 /* Failure here should not stop continuing other steps */
10821 }
10822
10823 /* enable RSS in the HW, even for only one queue, as the stack can use
10824 * the hash
10825 */
10826 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010827 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010828
10829 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010830 i40e_link_event(pf);
10831
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010832 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010833 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10834 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010835
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010836 i40e_ptp_init(pf);
10837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010838 return ret;
10839}
10840
10841/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010842 * i40e_determine_queue_usage - Work out queue distribution
10843 * @pf: board private structure
10844 **/
10845static void i40e_determine_queue_usage(struct i40e_pf *pf)
10846{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010847 int queues_left;
10848
10849 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010850#ifdef I40E_FCOE
10851 pf->num_fcoe_qps = 0;
10852#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010853
10854 /* Find the max queues to be put into basic use. We'll always be
10855 * using TC0, whether or not DCB is running, and TC0 will get the
10856 * big RSS set.
10857 */
10858 queues_left = pf->hw.func_caps.num_tx_qp;
10859
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010860 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010861 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010862 /* one qp for PF, no queues for anything else */
10863 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010864 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010865
10866 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010867 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010868 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010869#ifdef I40E_FCOE
10870 I40E_FLAG_FCOE_ENABLED |
10871#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010872 I40E_FLAG_FD_SB_ENABLED |
10873 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010874 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010875 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010876 I40E_FLAG_SRIOV_ENABLED |
10877 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010878 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10879 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010880 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010881 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010882 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010883 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010884 queues_left -= pf->num_lan_qps;
10885
10886 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010887 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010888#ifdef I40E_FCOE
10889 I40E_FLAG_FCOE_ENABLED |
10890#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010891 I40E_FLAG_FD_SB_ENABLED |
10892 I40E_FLAG_FD_ATR_ENABLED |
10893 I40E_FLAG_DCB_ENABLED |
10894 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010895 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010896 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010897 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010898 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010899 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10900 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010901 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10902 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010903 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10904 num_online_cpus());
10905 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10906 pf->hw.func_caps.num_tx_qp);
10907
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010908 queues_left -= pf->num_lan_qps;
10909 }
10910
Vasu Dev38e00432014-08-01 13:27:03 -070010911#ifdef I40E_FCOE
10912 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10913 if (I40E_DEFAULT_FCOE <= queues_left) {
10914 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10915 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10916 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10917 } else {
10918 pf->num_fcoe_qps = 0;
10919 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10920 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10921 }
10922
10923 queues_left -= pf->num_fcoe_qps;
10924 }
10925
10926#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010927 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10928 if (queues_left > 1) {
10929 queues_left -= 1; /* save 1 queue for FD */
10930 } else {
10931 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10932 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10933 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010934 }
10935
10936 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10937 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010938 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10939 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010940 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10941 }
10942
10943 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10944 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10945 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10946 (queues_left / pf->num_vmdq_qps));
10947 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10948 }
10949
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010950 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010951 dev_dbg(&pf->pdev->dev,
10952 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10953 pf->hw.func_caps.num_tx_qp,
10954 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010955 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10956 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10957 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010958#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010959 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010960#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010961}
10962
10963/**
10964 * i40e_setup_pf_filter_control - Setup PF static filter control
10965 * @pf: PF to be setup
10966 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010967 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010968 * settings. If PE/FCoE are enabled then it will also set the per PF
10969 * based filter sizes required for them. It also enables Flow director,
10970 * ethertype and macvlan type filter settings for the pf.
10971 *
10972 * Returns 0 on success, negative on failure
10973 **/
10974static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10975{
10976 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10977
10978 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10979
10980 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010981 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010982 settings->enable_fdir = true;
10983
10984 /* Ethtype and MACVLAN filters enabled for PF */
10985 settings->enable_ethtype = true;
10986 settings->enable_macvlan = true;
10987
10988 if (i40e_set_filter_control(&pf->hw, settings))
10989 return -ENOENT;
10990
10991 return 0;
10992}
10993
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010994#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010995#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010996static void i40e_print_features(struct i40e_pf *pf)
10997{
10998 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010999 char *buf;
11000 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011001
Joe Perches3b195842015-12-03 04:20:57 -080011002 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
11003 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011004 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011005
Joe Perches3b195842015-12-03 04:20:57 -080011006 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011007#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080011008 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011009#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011010 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040011011 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070011012 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011013 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011014 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011015 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011016 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011017 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080011018 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
11019 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000011020 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011021 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080011022 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080011023 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080011024 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011025 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080011026 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070011027#ifdef I40E_FCOE
11028 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011029 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070011030#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040011031 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080011032 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040011033 else
Joe Perches3b195842015-12-03 04:20:57 -080011034 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011035
Joe Perches3b195842015-12-03 04:20:57 -080011036 dev_info(&pf->pdev->dev, "%s\n", buf);
11037 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040011038 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011039}
11040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011041/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011042 * i40e_get_platform_mac_addr - get platform-specific MAC address
11043 *
11044 * @pdev: PCI device information struct
11045 * @pf: board private structure
11046 *
11047 * Look up the MAC address in Open Firmware on systems that support it,
11048 * and use IDPROM on SPARC if no OF address is found. On return, the
11049 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
11050 * has been selected.
11051 **/
11052static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
11053{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011054 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080011055 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011056 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011057}
11058
11059/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011060 * i40e_probe - Device initialization routine
11061 * @pdev: PCI device information struct
11062 * @ent: entry in i40e_pci_tbl
11063 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000011064 * i40e_probe initializes a PF identified by a pci_dev structure.
11065 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011066 * and a hardware reset occur.
11067 *
11068 * Returns 0 on success, negative on failure
11069 **/
11070static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
11071{
Catherine Sullivane8278452015-02-06 08:52:08 +000011072 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011073 struct i40e_pf *pf;
11074 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011075 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011076 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011077 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060011078 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011079 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011080 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040011081 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011082
11083 err = pci_enable_device_mem(pdev);
11084 if (err)
11085 return err;
11086
11087 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000011088 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000011089 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000011090 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
11091 if (err) {
11092 dev_err(&pdev->dev,
11093 "DMA configuration failed: 0x%x\n", err);
11094 goto err_dma;
11095 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011096 }
11097
11098 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011099 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011100 if (err) {
11101 dev_info(&pdev->dev,
11102 "pci_request_selected_regions failed %d\n", err);
11103 goto err_pci_reg;
11104 }
11105
11106 pci_enable_pcie_error_reporting(pdev);
11107 pci_set_master(pdev);
11108
11109 /* Now that we have a PCI connection, we need to do the
11110 * low level device setup. This is primarily setting up
11111 * the Admin Queue structures and then querying for the
11112 * device's current profile information.
11113 */
11114 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
11115 if (!pf) {
11116 err = -ENOMEM;
11117 goto err_pf_alloc;
11118 }
11119 pf->next_vsi = 0;
11120 pf->pdev = pdev;
11121 set_bit(__I40E_DOWN, &pf->state);
11122
11123 hw = &pf->hw;
11124 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000011125
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011126 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
11127 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000011128
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011129 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011130 if (!hw->hw_addr) {
11131 err = -EIO;
11132 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
11133 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040011134 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011135 goto err_ioremap;
11136 }
11137 hw->vendor_id = pdev->vendor;
11138 hw->device_id = pdev->device;
11139 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
11140 hw->subsystem_vendor_id = pdev->subsystem_vendor;
11141 hw->subsystem_device_id = pdev->subsystem_device;
11142 hw->bus.device = PCI_SLOT(pdev->devfn);
11143 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080011144 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011145 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011146
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080011147 /* set up the locks for the AQ, do this only once in probe
11148 * and destroy them only once in remove
11149 */
11150 mutex_init(&hw->aq.asq_mutex);
11151 mutex_init(&hw->aq.arq_mutex);
11152
Alexander Duyck5d4ca232016-09-30 08:21:46 -040011153 pf->msg_enable = netif_msg_init(debug,
11154 NETIF_MSG_DRV |
11155 NETIF_MSG_PROBE |
11156 NETIF_MSG_LINK);
11157 if (debug < -1)
11158 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000011159
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000011160 /* do a special CORER for clearing PXE mode once at init */
11161 if (hw->revision_id == 0 &&
11162 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
11163 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
11164 i40e_flush(hw);
11165 msleep(200);
11166 pf->corer_count++;
11167
11168 i40e_clear_pxe_mode(hw);
11169 }
11170
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011171 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000011172 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011173 err = i40e_pf_reset(hw);
11174 if (err) {
11175 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
11176 goto err_pf_reset;
11177 }
11178 pf->pfr_count++;
11179
11180 hw->aq.num_arq_entries = I40E_AQ_LEN;
11181 hw->aq.num_asq_entries = I40E_AQ_LEN;
11182 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11183 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
11184 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011185
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000011186 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000011187 "%s-%s:misc",
11188 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011189
11190 err = i40e_init_shared_code(hw);
11191 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040011192 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
11193 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011194 goto err_pf_reset;
11195 }
11196
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000011197 /* set up a default setting for link flow control */
11198 pf->hw.fc.requested_mode = I40E_FC_NONE;
11199
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011200 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040011201 if (err) {
11202 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11203 dev_info(&pdev->dev,
11204 "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");
11205 else
11206 dev_info(&pdev->dev,
11207 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11208
11209 goto err_pf_reset;
11210 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011211
Shannon Nelson6dec1012015-09-28 14:12:30 -040011212 /* provide nvm, fw, api versions */
11213 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11214 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11215 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11216 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011217
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011218 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11219 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011220 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011221 "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");
11222 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11223 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011224 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011225 "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 +000011226
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011227 i40e_verify_eeprom(pf);
11228
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011229 /* Rev 0 hardware was never productized */
11230 if (hw->revision_id < 1)
11231 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");
11232
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011233 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011234 err = i40e_get_capabilities(pf);
11235 if (err)
11236 goto err_adminq_setup;
11237
11238 err = i40e_sw_init(pf);
11239 if (err) {
11240 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11241 goto err_sw_init;
11242 }
11243
11244 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
11245 hw->func_caps.num_rx_qp,
11246 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
11247 if (err) {
11248 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11249 goto err_init_lan_hmc;
11250 }
11251
11252 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11253 if (err) {
11254 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11255 err = -ENOENT;
11256 goto err_configure_lan_hmc;
11257 }
11258
Neerav Parikhb686ece2014-12-14 01:55:11 +000011259 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11260 * Ignore error return codes because if it was already disabled via
11261 * hardware settings this will fail
11262 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011263 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011264 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11265 i40e_aq_stop_lldp(hw, true, NULL);
11266 }
11267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011268 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011269 /* allow a platform config to override the HW addr */
11270 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011271 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011272 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11273 err = -EIO;
11274 goto err_mac_addr;
11275 }
11276 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011277 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011278 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11279 if (is_valid_ether_addr(hw->mac.port_addr))
11280 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011281#ifdef I40E_FCOE
11282 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11283 if (err)
11284 dev_info(&pdev->dev,
11285 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11286 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11287 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11288 hw->mac.san_addr);
11289 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11290 }
11291 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11292#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011293
11294 pci_set_drvdata(pdev, pf);
11295 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011296#ifdef CONFIG_I40E_DCB
11297 err = i40e_init_pf_dcb(pf);
11298 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011299 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011300 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011301 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011302 }
11303#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011304
11305 /* set up periodic task facility */
11306 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11307 pf->service_timer_period = HZ;
11308
11309 INIT_WORK(&pf->service_task, i40e_service_task);
11310 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11311 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011312
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011313 /* NVM bit on means WoL disabled for the port */
11314 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011315 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011316 pf->wol_en = false;
11317 else
11318 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011319 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11320
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011321 /* set up the main switch operations */
11322 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011323 err = i40e_init_interrupt_scheme(pf);
11324 if (err)
11325 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011326
Mitch Williams505682c2014-05-20 08:01:37 +000011327 /* The number of VSIs reported by the FW is the minimum guaranteed
11328 * to us; HW supports far more and we share the remaining pool with
11329 * the other PFs. We allocate space for more than the guarantee with
11330 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011331 */
Mitch Williams505682c2014-05-20 08:01:37 +000011332 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11333 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11334 else
11335 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11336
11337 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011338 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11339 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011340 if (!pf->vsi) {
11341 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011342 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011343 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011344
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011345#ifdef CONFIG_PCI_IOV
11346 /* prep for VF support */
11347 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11348 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11349 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11350 if (pci_num_vf(pdev))
11351 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11352 }
11353#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011354 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011355 if (err) {
11356 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11357 goto err_vsis;
11358 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011359
11360 /* Make sure flow control is set according to current settings */
11361 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11362 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11363 dev_dbg(&pf->pdev->dev,
11364 "Set fc with err %s aq_err %s on get_phy_cap\n",
11365 i40e_stat_str(hw, err),
11366 i40e_aq_str(hw, hw->aq.asq_last_status));
11367 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11368 dev_dbg(&pf->pdev->dev,
11369 "Set fc with err %s aq_err %s on set_phy_config\n",
11370 i40e_stat_str(hw, err),
11371 i40e_aq_str(hw, hw->aq.asq_last_status));
11372 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11373 dev_dbg(&pf->pdev->dev,
11374 "Set fc with err %s aq_err %s on get_link_info\n",
11375 i40e_stat_str(hw, err),
11376 i40e_aq_str(hw, hw->aq.asq_last_status));
11377
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011378 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011379 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011380 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11381 i40e_vsi_open(pf->vsi[i]);
11382 break;
11383 }
11384 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011385
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011386 /* The driver only wants link up/down and module qualification
11387 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011388 */
11389 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011390 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011391 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011392 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011393 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011394 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11395 i40e_stat_str(&pf->hw, err),
11396 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011397
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011398 /* Reconfigure hardware for allowing smaller MSS in the case
11399 * of TSO, so that we avoid the MDD being fired and causing
11400 * a reset in the case of small MSS+TSO.
11401 */
11402 val = rd32(hw, I40E_REG_MSS);
11403 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11404 val &= ~I40E_REG_MSS_MIN_MASK;
11405 val |= I40E_64BYTE_MSS;
11406 wr32(hw, I40E_REG_MSS, val);
11407 }
11408
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011409 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011410 msleep(75);
11411 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11412 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011413 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11414 i40e_stat_str(&pf->hw, err),
11415 i40e_aq_str(&pf->hw,
11416 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011417 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011418 /* The main driver is (mostly) up and happy. We need to set this state
11419 * before setting up the misc vector or we get a race and the vector
11420 * ends up disabled forever.
11421 */
11422 clear_bit(__I40E_DOWN, &pf->state);
11423
11424 /* In case of MSIX we are going to setup the misc vector right here
11425 * to handle admin queue events etc. In case of legacy and MSI
11426 * the misc functionality and queue processing is combined in
11427 * the same vector and that gets setup at open.
11428 */
11429 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11430 err = i40e_setup_misc_vector(pf);
11431 if (err) {
11432 dev_info(&pdev->dev,
11433 "setup of misc vector failed: %d\n", err);
11434 goto err_vsis;
11435 }
11436 }
11437
Greg Rosedf805f62014-04-04 04:43:16 +000011438#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011439 /* prep for VF support */
11440 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011441 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11442 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011443 /* disable link interrupts for VFs */
11444 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11445 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11446 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11447 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011448
11449 if (pci_num_vf(pdev)) {
11450 dev_info(&pdev->dev,
11451 "Active VFs found, allocating resources.\n");
11452 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11453 if (err)
11454 dev_info(&pdev->dev,
11455 "Error %d allocating resources for existing VFs\n",
11456 err);
11457 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011458 }
Greg Rosedf805f62014-04-04 04:43:16 +000011459#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011460
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011461 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11462 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11463 pf->num_iwarp_msix,
11464 I40E_IWARP_IRQ_PILE_ID);
11465 if (pf->iwarp_base_vector < 0) {
11466 dev_info(&pdev->dev,
11467 "failed to get tracking for %d vectors for IWARP err=%d\n",
11468 pf->num_iwarp_msix, pf->iwarp_base_vector);
11469 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11470 }
11471 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011473 i40e_dbg_pf_init(pf);
11474
11475 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011476 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011477
11478 /* since everything's happy, start the service_task timer */
11479 mod_timer(&pf->service_timer,
11480 round_jiffies(jiffies + pf->service_timer_period));
11481
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011482 /* add this PF to client device list and launch a client service task */
11483 err = i40e_lan_add_device(pf);
11484 if (err)
11485 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11486 err);
11487
Vasu Dev38e00432014-08-01 13:27:03 -070011488#ifdef I40E_FCOE
11489 /* create FCoE interface */
11490 i40e_fcoe_vsi_setup(pf);
11491
11492#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011493#define PCI_SPEED_SIZE 8
11494#define PCI_WIDTH_SIZE 8
11495 /* Devices on the IOSF bus do not have this information
11496 * and will report PCI Gen 1 x 1 by default so don't bother
11497 * checking them.
11498 */
11499 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11500 char speed[PCI_SPEED_SIZE] = "Unknown";
11501 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011502
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011503 /* Get the negotiated link width and speed from PCI config
11504 * space
11505 */
11506 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11507 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011508
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011509 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011510
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011511 switch (hw->bus.speed) {
11512 case i40e_bus_speed_8000:
11513 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11514 case i40e_bus_speed_5000:
11515 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11516 case i40e_bus_speed_2500:
11517 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11518 default:
11519 break;
11520 }
11521 switch (hw->bus.width) {
11522 case i40e_bus_width_pcie_x8:
11523 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11524 case i40e_bus_width_pcie_x4:
11525 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11526 case i40e_bus_width_pcie_x2:
11527 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11528 case i40e_bus_width_pcie_x1:
11529 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11530 default:
11531 break;
11532 }
11533
11534 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11535 speed, width);
11536
11537 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11538 hw->bus.speed < i40e_bus_speed_8000) {
11539 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11540 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11541 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011542 }
11543
Catherine Sullivane8278452015-02-06 08:52:08 +000011544 /* get the requested speeds from the fw */
11545 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11546 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011547 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11548 i40e_stat_str(&pf->hw, err),
11549 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011550 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11551
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011552 /* get the supported phy types from the fw */
11553 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11554 if (err)
11555 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11556 i40e_stat_str(&pf->hw, err),
11557 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011558
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011559 /* Add a filter to drop all Flow control frames from any VSI from being
11560 * transmitted. By doing so we stop a malicious VF from sending out
11561 * PAUSE or PFC frames and potentially controlling traffic for other
11562 * PF/VF VSIs.
11563 * The FW can still send Flow control frames if enabled.
11564 */
11565 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11566 pf->main_vsi_seid);
11567
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011568 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011569 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11570 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011571 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11572 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011573 /* print a string summarizing features */
11574 i40e_print_features(pf);
11575
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011576 return 0;
11577
11578 /* Unwind what we've done if something failed in the setup */
11579err_vsis:
11580 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011581 i40e_clear_interrupt_scheme(pf);
11582 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011583err_switch_setup:
11584 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011585 del_timer_sync(&pf->service_timer);
11586err_mac_addr:
11587err_configure_lan_hmc:
11588 (void)i40e_shutdown_lan_hmc(hw);
11589err_init_lan_hmc:
11590 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011591err_sw_init:
11592err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011593err_pf_reset:
11594 iounmap(hw->hw_addr);
11595err_ioremap:
11596 kfree(pf);
11597err_pf_alloc:
11598 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011599 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011600err_pci_reg:
11601err_dma:
11602 pci_disable_device(pdev);
11603 return err;
11604}
11605
11606/**
11607 * i40e_remove - Device removal routine
11608 * @pdev: PCI device information struct
11609 *
11610 * i40e_remove is called by the PCI subsystem to alert the driver
11611 * that is should release a PCI device. This could be caused by a
11612 * Hot-Plug event, or because the driver is going to be removed from
11613 * memory.
11614 **/
11615static void i40e_remove(struct pci_dev *pdev)
11616{
11617 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011618 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011619 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011620 int i;
11621
11622 i40e_dbg_pf_exit(pf);
11623
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011624 i40e_ptp_stop(pf);
11625
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011626 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011627 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11628 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011629
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011630 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011631 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011632 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011633 if (pf->service_timer.data)
11634 del_timer_sync(&pf->service_timer);
11635 if (pf->service_task.func)
11636 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011637
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011638 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11639 i40e_free_vfs(pf);
11640 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11641 }
11642
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011643 i40e_fdir_teardown(pf);
11644
11645 /* If there is a switch structure or any orphans, remove them.
11646 * This will leave only the PF's VSI remaining.
11647 */
11648 for (i = 0; i < I40E_MAX_VEB; i++) {
11649 if (!pf->veb[i])
11650 continue;
11651
11652 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11653 pf->veb[i]->uplink_seid == 0)
11654 i40e_switch_branch_release(pf->veb[i]);
11655 }
11656
11657 /* Now we can shutdown the PF's VSI, just before we kill
11658 * adminq and hmc.
11659 */
11660 if (pf->vsi[pf->lan_vsi])
11661 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11662
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011663 /* remove attached clients */
11664 ret_code = i40e_lan_del_device(pf);
11665 if (ret_code) {
11666 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11667 ret_code);
11668 }
11669
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011670 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011671 if (hw->hmc.hmc_obj) {
11672 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011673 if (ret_code)
11674 dev_warn(&pdev->dev,
11675 "Failed to destroy the HMC resources: %d\n",
11676 ret_code);
11677 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011678
11679 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011680 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011681
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011682 /* destroy the locks only once, here */
11683 mutex_destroy(&hw->aq.arq_mutex);
11684 mutex_destroy(&hw->aq.asq_mutex);
11685
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011686 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11687 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011688 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011689 if (pf->vsi[i]) {
11690 i40e_vsi_clear_rings(pf->vsi[i]);
11691 i40e_vsi_clear(pf->vsi[i]);
11692 pf->vsi[i] = NULL;
11693 }
11694 }
11695
11696 for (i = 0; i < I40E_MAX_VEB; i++) {
11697 kfree(pf->veb[i]);
11698 pf->veb[i] = NULL;
11699 }
11700
11701 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011702 kfree(pf->vsi);
11703
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011704 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011705 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011706 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011707
11708 pci_disable_pcie_error_reporting(pdev);
11709 pci_disable_device(pdev);
11710}
11711
11712/**
11713 * i40e_pci_error_detected - warning that something funky happened in PCI land
11714 * @pdev: PCI device information struct
11715 *
11716 * Called to warn that something happened and the error handling steps
11717 * are in progress. Allows the driver to quiesce things, be ready for
11718 * remediation.
11719 **/
11720static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11721 enum pci_channel_state error)
11722{
11723 struct i40e_pf *pf = pci_get_drvdata(pdev);
11724
11725 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11726
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011727 if (!pf) {
11728 dev_info(&pdev->dev,
11729 "Cannot recover - error happened during device probe\n");
11730 return PCI_ERS_RESULT_DISCONNECT;
11731 }
11732
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011733 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011734 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11735 rtnl_lock();
11736 i40e_prep_for_reset(pf);
11737 rtnl_unlock();
11738 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011739
11740 /* Request a slot reset */
11741 return PCI_ERS_RESULT_NEED_RESET;
11742}
11743
11744/**
11745 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11746 * @pdev: PCI device information struct
11747 *
11748 * Called to find if the driver can work with the device now that
11749 * the pci slot has been reset. If a basic connection seems good
11750 * (registers are readable and have sane content) then return a
11751 * happy little PCI_ERS_RESULT_xxx.
11752 **/
11753static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11754{
11755 struct i40e_pf *pf = pci_get_drvdata(pdev);
11756 pci_ers_result_t result;
11757 int err;
11758 u32 reg;
11759
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011760 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011761 if (pci_enable_device_mem(pdev)) {
11762 dev_info(&pdev->dev,
11763 "Cannot re-enable PCI device after reset.\n");
11764 result = PCI_ERS_RESULT_DISCONNECT;
11765 } else {
11766 pci_set_master(pdev);
11767 pci_restore_state(pdev);
11768 pci_save_state(pdev);
11769 pci_wake_from_d3(pdev, false);
11770
11771 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11772 if (reg == 0)
11773 result = PCI_ERS_RESULT_RECOVERED;
11774 else
11775 result = PCI_ERS_RESULT_DISCONNECT;
11776 }
11777
11778 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11779 if (err) {
11780 dev_info(&pdev->dev,
11781 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11782 err);
11783 /* non-fatal, continue */
11784 }
11785
11786 return result;
11787}
11788
11789/**
11790 * i40e_pci_error_resume - restart operations after PCI error recovery
11791 * @pdev: PCI device information struct
11792 *
11793 * Called to allow the driver to bring things back up after PCI error
11794 * and/or reset recovery has finished.
11795 **/
11796static void i40e_pci_error_resume(struct pci_dev *pdev)
11797{
11798 struct i40e_pf *pf = pci_get_drvdata(pdev);
11799
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011800 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011801 if (test_bit(__I40E_SUSPENDED, &pf->state))
11802 return;
11803
11804 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011805 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011806 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011807}
11808
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011809/**
Joshua Hay1d680052016-12-12 15:44:08 -080011810 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11811 * using the mac_address_write admin q function
11812 * @pf: pointer to i40e_pf struct
11813 **/
11814static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11815{
11816 struct i40e_hw *hw = &pf->hw;
11817 i40e_status ret;
11818 u8 mac_addr[6];
11819 u16 flags = 0;
11820
11821 /* Get current MAC address in case it's an LAA */
11822 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11823 ether_addr_copy(mac_addr,
11824 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11825 } else {
11826 dev_err(&pf->pdev->dev,
11827 "Failed to retrieve MAC address; using default\n");
11828 ether_addr_copy(mac_addr, hw->mac.addr);
11829 }
11830
11831 /* The FW expects the mac address write cmd to first be called with
11832 * one of these flags before calling it again with the multicast
11833 * enable flags.
11834 */
11835 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11836
11837 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11838 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11839
11840 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11841 if (ret) {
11842 dev_err(&pf->pdev->dev,
11843 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11844 return;
11845 }
11846
11847 flags = I40E_AQC_MC_MAG_EN
11848 | I40E_AQC_WOL_PRESERVE_ON_PFR
11849 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11850 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11851 if (ret)
11852 dev_err(&pf->pdev->dev,
11853 "Failed to enable Multicast Magic Packet wake up\n");
11854}
11855
11856/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011857 * i40e_shutdown - PCI callback for shutting down
11858 * @pdev: PCI device information struct
11859 **/
11860static void i40e_shutdown(struct pci_dev *pdev)
11861{
11862 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011863 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011864
11865 set_bit(__I40E_SUSPENDED, &pf->state);
11866 set_bit(__I40E_DOWN, &pf->state);
11867 rtnl_lock();
11868 i40e_prep_for_reset(pf);
11869 rtnl_unlock();
11870
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011871 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11872 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11873
Catherine Sullivan02b42492015-07-10 19:35:59 -040011874 del_timer_sync(&pf->service_timer);
11875 cancel_work_sync(&pf->service_task);
11876 i40e_fdir_teardown(pf);
11877
Joshua Hay1d680052016-12-12 15:44:08 -080011878 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11879 i40e_enable_mc_magic_wake(pf);
11880
Catherine Sullivan02b42492015-07-10 19:35:59 -040011881 rtnl_lock();
11882 i40e_prep_for_reset(pf);
11883 rtnl_unlock();
11884
11885 wr32(hw, I40E_PFPM_APM,
11886 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11887 wr32(hw, I40E_PFPM_WUFC,
11888 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11889
Shannon Nelsone1477582015-02-21 06:44:33 +000011890 i40e_clear_interrupt_scheme(pf);
11891
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011892 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011893 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011894 pci_set_power_state(pdev, PCI_D3hot);
11895 }
11896}
11897
11898#ifdef CONFIG_PM
11899/**
11900 * i40e_suspend - PCI callback for moving to D3
11901 * @pdev: PCI device information struct
11902 **/
11903static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11904{
11905 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011906 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011907 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011908
11909 set_bit(__I40E_SUSPENDED, &pf->state);
11910 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011911
Joshua Hay1d680052016-12-12 15:44:08 -080011912 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11913 i40e_enable_mc_magic_wake(pf);
11914
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011915 rtnl_lock();
11916 i40e_prep_for_reset(pf);
11917 rtnl_unlock();
11918
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011919 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11920 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11921
Greg Roseb33d3b72016-05-16 10:26:37 -070011922 i40e_stop_misc_vector(pf);
11923
Greg Rose059ff692016-05-16 10:26:38 -070011924 retval = pci_save_state(pdev);
11925 if (retval)
11926 return retval;
11927
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011928 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011929 pci_set_power_state(pdev, PCI_D3hot);
11930
Greg Rose059ff692016-05-16 10:26:38 -070011931 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011932}
11933
11934/**
11935 * i40e_resume - PCI callback for waking up from D3
11936 * @pdev: PCI device information struct
11937 **/
11938static int i40e_resume(struct pci_dev *pdev)
11939{
11940 struct i40e_pf *pf = pci_get_drvdata(pdev);
11941 u32 err;
11942
11943 pci_set_power_state(pdev, PCI_D0);
11944 pci_restore_state(pdev);
11945 /* pci_restore_state() clears dev->state_saves, so
11946 * call pci_save_state() again to restore it.
11947 */
11948 pci_save_state(pdev);
11949
11950 err = pci_enable_device_mem(pdev);
11951 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011952 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011953 return err;
11954 }
11955 pci_set_master(pdev);
11956
11957 /* no wakeup events while running */
11958 pci_wake_from_d3(pdev, false);
11959
11960 /* handling the reset will rebuild the device state */
11961 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11962 clear_bit(__I40E_DOWN, &pf->state);
11963 rtnl_lock();
11964 i40e_reset_and_rebuild(pf, false);
11965 rtnl_unlock();
11966 }
11967
11968 return 0;
11969}
11970
11971#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011972static const struct pci_error_handlers i40e_err_handler = {
11973 .error_detected = i40e_pci_error_detected,
11974 .slot_reset = i40e_pci_error_slot_reset,
11975 .resume = i40e_pci_error_resume,
11976};
11977
11978static struct pci_driver i40e_driver = {
11979 .name = i40e_driver_name,
11980 .id_table = i40e_pci_tbl,
11981 .probe = i40e_probe,
11982 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011983#ifdef CONFIG_PM
11984 .suspend = i40e_suspend,
11985 .resume = i40e_resume,
11986#endif
11987 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011988 .err_handler = &i40e_err_handler,
11989 .sriov_configure = i40e_pci_sriov_configure,
11990};
11991
11992/**
11993 * i40e_init_module - Driver registration routine
11994 *
11995 * i40e_init_module is the first routine called when the driver is
11996 * loaded. All it does is register with the PCI subsystem.
11997 **/
11998static int __init i40e_init_module(void)
11999{
12000 pr_info("%s: %s - version %s\n", i40e_driver_name,
12001 i40e_driver_string, i40e_driver_version_str);
12002 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000012003
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012004 /* we will see if single thread per module is enough for now,
12005 * it can't be any worse than using the system workqueue which
12006 * was already single threaded
12007 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070012008 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
12009 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012010 if (!i40e_wq) {
12011 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
12012 return -ENOMEM;
12013 }
12014
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012015 i40e_dbg_init();
12016 return pci_register_driver(&i40e_driver);
12017}
12018module_init(i40e_init_module);
12019
12020/**
12021 * i40e_exit_module - Driver exit cleanup routine
12022 *
12023 * i40e_exit_module is called just before the driver is removed
12024 * from memory.
12025 **/
12026static void __exit i40e_exit_module(void)
12027{
12028 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080012029 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012030 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000012031}
12032module_exit(i40e_exit_module);