blob: a45748e52ef631aa70caea40b2b5ed1635d9e720 [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"
Singhai, Anjali6a899022015-12-14 12:21:18 -080034#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000035#include <net/vxlan.h>
36#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080037#if IS_ENABLED(CONFIG_GENEVE)
38#include <net/geneve.h>
39#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000040
41const char i40e_driver_name[] = "i40e";
42static const char i40e_driver_string[] =
43 "Intel(R) Ethernet Connection XL710 Network Driver";
44
45#define DRV_KERN "-k"
46
Catherine Sullivane8e724d2014-07-10 07:58:26 +000047#define DRV_VERSION_MAJOR 1
Catherine Sullivan50f26a52016-03-10 14:59:51 -080048#define DRV_VERSION_MINOR 5
Harshitha Ramamurthy47c46772016-04-01 03:56:13 -070049#define DRV_VERSION_BUILD 5
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) DRV_KERN
53const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080054static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055
56/* a bit of forward declarations */
57static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
58static void i40e_handle_reset_warning(struct i40e_pf *pf);
59static int i40e_add_vsi(struct i40e_vsi *vsi);
60static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000061static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062static int i40e_setup_misc_vector(struct i40e_pf *pf);
63static void i40e_determine_queue_usage(struct i40e_pf *pf);
64static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Helin Zhange69ff812015-10-21 19:56:22 -040065static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
66 u16 rss_table_size, u16 rss_size);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080067static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080068static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000069
70/* i40e_pci_tbl - PCI Device ID Table
71 *
72 * Last entry must be all 0s
73 *
74 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
75 * Class, Class Mask, private data (not used) }
76 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020077static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070093 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040094 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
95 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000096 /* required last entry */
97 {0, }
98};
99MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
100
101#define I40E_MAX_VF_COUNT 128
102static int debug = -1;
103module_param(debug, int, 0);
104MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
105
106MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
107MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
108MODULE_LICENSE("GPL");
109MODULE_VERSION(DRV_VERSION);
110
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800111static struct workqueue_struct *i40e_wq;
112
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000113/**
114 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
115 * @hw: pointer to the HW structure
116 * @mem: ptr to mem struct to fill out
117 * @size: size of memory requested
118 * @alignment: what to align the allocation to
119 **/
120int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
121 u64 size, u32 alignment)
122{
123 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
124
125 mem->size = ALIGN(size, alignment);
126 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
127 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000128 if (!mem->va)
129 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000130
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000131 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000132}
133
134/**
135 * i40e_free_dma_mem_d - OS specific memory free for shared code
136 * @hw: pointer to the HW structure
137 * @mem: ptr to mem struct to free
138 **/
139int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
140{
141 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
142
143 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
144 mem->va = NULL;
145 mem->pa = 0;
146 mem->size = 0;
147
148 return 0;
149}
150
151/**
152 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
153 * @hw: pointer to the HW structure
154 * @mem: ptr to mem struct to fill out
155 * @size: size of memory requested
156 **/
157int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
158 u32 size)
159{
160 mem->size = size;
161 mem->va = kzalloc(size, GFP_KERNEL);
162
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000163 if (!mem->va)
164 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000165
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000166 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000167}
168
169/**
170 * i40e_free_virt_mem_d - OS specific memory free for shared code
171 * @hw: pointer to the HW structure
172 * @mem: ptr to mem struct to free
173 **/
174int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
175{
176 /* it's ok to kfree a NULL pointer */
177 kfree(mem->va);
178 mem->va = NULL;
179 mem->size = 0;
180
181 return 0;
182}
183
184/**
185 * i40e_get_lump - find a lump of free generic resource
186 * @pf: board private structure
187 * @pile: the pile of resource to search
188 * @needed: the number of items needed
189 * @id: an owner id to stick on the items assigned
190 *
191 * Returns the base item index of the lump, or negative for error
192 *
193 * The search_hint trick and lack of advanced fit-finding only work
194 * because we're highly likely to have all the same size lump requests.
195 * Linear search time and any fragmentation should be minimal.
196 **/
197static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
198 u16 needed, u16 id)
199{
200 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000201 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000202
203 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
204 dev_info(&pf->pdev->dev,
205 "param err: pile=%p needed=%d id=0x%04x\n",
206 pile, needed, id);
207 return -EINVAL;
208 }
209
210 /* start the linear search with an imperfect hint */
211 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000212 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000213 /* skip already allocated entries */
214 if (pile->list[i] & I40E_PILE_VALID_BIT) {
215 i++;
216 continue;
217 }
218
219 /* do we have enough in this lump? */
220 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
221 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
222 break;
223 }
224
225 if (j == needed) {
226 /* there was enough, so assign it to the requestor */
227 for (j = 0; j < needed; j++)
228 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
229 ret = i;
230 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000231 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000232 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400233
234 /* not enough, so skip over it and continue looking */
235 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000236 }
237
238 return ret;
239}
240
241/**
242 * i40e_put_lump - return a lump of generic resource
243 * @pile: the pile of resource to search
244 * @index: the base item index
245 * @id: the owner id of the items assigned
246 *
247 * Returns the count of items in the lump
248 **/
249static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
250{
251 int valid_id = (id | I40E_PILE_VALID_BIT);
252 int count = 0;
253 int i;
254
255 if (!pile || index >= pile->num_entries)
256 return -EINVAL;
257
258 for (i = index;
259 i < pile->num_entries && pile->list[i] == valid_id;
260 i++) {
261 pile->list[i] = 0;
262 count++;
263 }
264
265 if (count && index < pile->search_hint)
266 pile->search_hint = index;
267
268 return count;
269}
270
271/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700272 * i40e_find_vsi_from_id - searches for the vsi with the given id
273 * @pf - the pf structure to search for the vsi
274 * @id - id of the vsi it is searching for
275 **/
276struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
277{
278 int i;
279
280 for (i = 0; i < pf->num_alloc_vsi; i++)
281 if (pf->vsi[i] && (pf->vsi[i]->id == id))
282 return pf->vsi[i];
283
284 return NULL;
285}
286
287/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000288 * i40e_service_event_schedule - Schedule the service task to wake up
289 * @pf: board private structure
290 *
291 * If not already scheduled, this puts the task into the work queue
292 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600293void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000294{
295 if (!test_bit(__I40E_DOWN, &pf->state) &&
296 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
297 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800298 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000299}
300
301/**
302 * i40e_tx_timeout - Respond to a Tx Hang
303 * @netdev: network interface device structure
304 *
305 * If any port has noticed a Tx timeout, it is likely that the whole
306 * device is munged, not just the one netdev port, so go for the full
307 * reset.
308 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700309#ifdef I40E_FCOE
310void i40e_tx_timeout(struct net_device *netdev)
311#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000312static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700313#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000314{
315 struct i40e_netdev_priv *np = netdev_priv(netdev);
316 struct i40e_vsi *vsi = np->vsi;
317 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400318 struct i40e_ring *tx_ring = NULL;
319 unsigned int i, hung_queue = 0;
320 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000321
322 pf->tx_timeout_count++;
323
Kiran Patilb03a8c12015-09-24 18:13:15 -0400324 /* find the stopped queue the same way the stack does */
325 for (i = 0; i < netdev->num_tx_queues; i++) {
326 struct netdev_queue *q;
327 unsigned long trans_start;
328
329 q = netdev_get_tx_queue(netdev, i);
330 trans_start = q->trans_start ? : netdev->trans_start;
331 if (netif_xmit_stopped(q) &&
332 time_after(jiffies,
333 (trans_start + netdev->watchdog_timeo))) {
334 hung_queue = i;
335 break;
336 }
337 }
338
339 if (i == netdev->num_tx_queues) {
340 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
341 } else {
342 /* now that we have an index, find the tx_ring struct */
343 for (i = 0; i < vsi->num_queue_pairs; i++) {
344 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
345 if (hung_queue ==
346 vsi->tx_rings[i]->queue_index) {
347 tx_ring = vsi->tx_rings[i];
348 break;
349 }
350 }
351 }
352 }
353
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000354 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400355 pf->tx_timeout_recovery_level = 1; /* reset after some time */
356 else if (time_before(jiffies,
357 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
358 return; /* don't do any new action before the next timeout */
359
360 if (tx_ring) {
361 head = i40e_get_head(tx_ring);
362 /* Read interrupt register */
363 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
364 val = rd32(&pf->hw,
365 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
366 tx_ring->vsi->base_vector - 1));
367 else
368 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
369
370 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",
371 vsi->seid, hung_queue, tx_ring->next_to_clean,
372 head, tx_ring->next_to_use,
373 readl(tx_ring->tail), val);
374 }
375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000376 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400377 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
378 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000379
380 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000381 case 1:
382 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
383 break;
384 case 2:
385 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
386 break;
387 case 3:
388 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
389 break;
390 default:
391 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000392 break;
393 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000395 i40e_service_event_schedule(pf);
396 pf->tx_timeout_recovery_level++;
397}
398
399/**
400 * i40e_release_rx_desc - Store the new tail and head values
401 * @rx_ring: ring to bump
402 * @val: new head index
403 **/
404static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
405{
406 rx_ring->next_to_use = val;
407
408 /* Force memory writes to complete before letting h/w
409 * know there are new descriptors to fetch. (Only
410 * applicable for weak-ordered memory model archs,
411 * such as IA-64).
412 */
413 wmb();
414 writel(val, rx_ring->tail);
415}
416
417/**
418 * i40e_get_vsi_stats_struct - Get System Network Statistics
419 * @vsi: the VSI we care about
420 *
421 * Returns the address of the device statistics structure.
422 * The statistics are actually updated from the service task.
423 **/
424struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
425{
426 return &vsi->net_stats;
427}
428
429/**
430 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
431 * @netdev: network interface device structure
432 *
433 * Returns the address of the device statistics structure.
434 * The statistics are actually updated from the service task.
435 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700436#ifdef I40E_FCOE
437struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
438 struct net_device *netdev,
439 struct rtnl_link_stats64 *stats)
440#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000441static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
442 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000443 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700444#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000445{
446 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000447 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000448 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000449 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
450 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000451
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000452 if (test_bit(__I40E_DOWN, &vsi->state))
453 return stats;
454
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800455 if (!vsi->tx_rings)
456 return stats;
457
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 rcu_read_lock();
459 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000460 u64 bytes, packets;
461 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000462
Alexander Duyck980e9b12013-09-28 06:01:03 +0000463 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
464 if (!tx_ring)
465 continue;
466
467 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700468 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000469 packets = tx_ring->stats.packets;
470 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700471 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000472
473 stats->tx_packets += packets;
474 stats->tx_bytes += bytes;
475 rx_ring = &tx_ring[1];
476
477 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700478 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000479 packets = rx_ring->stats.packets;
480 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700481 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000482
483 stats->rx_packets += packets;
484 stats->rx_bytes += bytes;
485 }
486 rcu_read_unlock();
487
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000488 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000489 stats->multicast = vsi_stats->multicast;
490 stats->tx_errors = vsi_stats->tx_errors;
491 stats->tx_dropped = vsi_stats->tx_dropped;
492 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400493 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000494 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
495 stats->rx_length_errors = vsi_stats->rx_length_errors;
496
497 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000498}
499
500/**
501 * i40e_vsi_reset_stats - Resets all stats of the given vsi
502 * @vsi: the VSI to have its stats reset
503 **/
504void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
505{
506 struct rtnl_link_stats64 *ns;
507 int i;
508
509 if (!vsi)
510 return;
511
512 ns = i40e_get_vsi_stats_struct(vsi);
513 memset(ns, 0, sizeof(*ns));
514 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
515 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
516 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000517 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000518 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400519 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000520 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400521 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000522 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400523 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e15b2013-09-28 06:00:58 +0000524 sizeof(vsi->tx_rings[i]->stats));
525 memset(&vsi->tx_rings[i]->tx_stats, 0,
526 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000527 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000528 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529 vsi->stat_offsets_loaded = false;
530}
531
532/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000533 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000534 * @pf: the PF to be reset
535 **/
536void i40e_pf_reset_stats(struct i40e_pf *pf)
537{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000538 int i;
539
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000540 memset(&pf->stats, 0, sizeof(pf->stats));
541 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
542 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000543
544 for (i = 0; i < I40E_MAX_VEB; i++) {
545 if (pf->veb[i]) {
546 memset(&pf->veb[i]->stats, 0,
547 sizeof(pf->veb[i]->stats));
548 memset(&pf->veb[i]->stats_offsets, 0,
549 sizeof(pf->veb[i]->stats_offsets));
550 pf->veb[i]->stat_offsets_loaded = false;
551 }
552 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000553}
554
555/**
556 * i40e_stat_update48 - read and update a 48 bit stat from the chip
557 * @hw: ptr to the hardware info
558 * @hireg: the high 32 bit reg to read
559 * @loreg: the low 32 bit reg to read
560 * @offset_loaded: has the initial offset been loaded yet
561 * @offset: ptr to current offset value
562 * @stat: ptr to the stat
563 *
564 * Since the device stats are not reset at PFReset, they likely will not
565 * be zeroed when the driver starts. We'll save the first values read
566 * and use them as offsets to be subtracted from the raw values in order
567 * to report stats that count from zero. In the process, we also manage
568 * the potential roll-over.
569 **/
570static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
571 bool offset_loaded, u64 *offset, u64 *stat)
572{
573 u64 new_data;
574
Shannon Nelsonab600852014-01-17 15:36:39 -0800575 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000576 new_data = rd32(hw, loreg);
577 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
578 } else {
579 new_data = rd64(hw, loreg);
580 }
581 if (!offset_loaded)
582 *offset = new_data;
583 if (likely(new_data >= *offset))
584 *stat = new_data - *offset;
585 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400586 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000587 *stat &= 0xFFFFFFFFFFFFULL;
588}
589
590/**
591 * i40e_stat_update32 - read and update a 32 bit stat from the chip
592 * @hw: ptr to the hardware info
593 * @reg: the hw reg to read
594 * @offset_loaded: has the initial offset been loaded yet
595 * @offset: ptr to current offset value
596 * @stat: ptr to the stat
597 **/
598static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
599 bool offset_loaded, u64 *offset, u64 *stat)
600{
601 u32 new_data;
602
603 new_data = rd32(hw, reg);
604 if (!offset_loaded)
605 *offset = new_data;
606 if (likely(new_data >= *offset))
607 *stat = (u32)(new_data - *offset);
608 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400609 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000610}
611
612/**
613 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
614 * @vsi: the VSI to be updated
615 **/
616void i40e_update_eth_stats(struct i40e_vsi *vsi)
617{
618 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
619 struct i40e_pf *pf = vsi->back;
620 struct i40e_hw *hw = &pf->hw;
621 struct i40e_eth_stats *oes;
622 struct i40e_eth_stats *es; /* device's eth stats */
623
624 es = &vsi->eth_stats;
625 oes = &vsi->eth_stats_offsets;
626
627 /* Gather up the stats that the hw collects */
628 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
629 vsi->stat_offsets_loaded,
630 &oes->tx_errors, &es->tx_errors);
631 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
632 vsi->stat_offsets_loaded,
633 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000634 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
635 vsi->stat_offsets_loaded,
636 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
637 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000640
641 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
642 I40E_GLV_GORCL(stat_idx),
643 vsi->stat_offsets_loaded,
644 &oes->rx_bytes, &es->rx_bytes);
645 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
646 I40E_GLV_UPRCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->rx_unicast, &es->rx_unicast);
649 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
650 I40E_GLV_MPRCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->rx_multicast, &es->rx_multicast);
653 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
654 I40E_GLV_BPRCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->rx_broadcast, &es->rx_broadcast);
657
658 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
659 I40E_GLV_GOTCL(stat_idx),
660 vsi->stat_offsets_loaded,
661 &oes->tx_bytes, &es->tx_bytes);
662 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
663 I40E_GLV_UPTCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->tx_unicast, &es->tx_unicast);
666 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
667 I40E_GLV_MPTCL(stat_idx),
668 vsi->stat_offsets_loaded,
669 &oes->tx_multicast, &es->tx_multicast);
670 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
671 I40E_GLV_BPTCL(stat_idx),
672 vsi->stat_offsets_loaded,
673 &oes->tx_broadcast, &es->tx_broadcast);
674 vsi->stat_offsets_loaded = true;
675}
676
677/**
678 * i40e_update_veb_stats - Update Switch component statistics
679 * @veb: the VEB being updated
680 **/
681static void i40e_update_veb_stats(struct i40e_veb *veb)
682{
683 struct i40e_pf *pf = veb->pf;
684 struct i40e_hw *hw = &pf->hw;
685 struct i40e_eth_stats *oes;
686 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400687 struct i40e_veb_tc_stats *veb_oes;
688 struct i40e_veb_tc_stats *veb_es;
689 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000690
691 idx = veb->stats_idx;
692 es = &veb->stats;
693 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400694 veb_es = &veb->tc_stats;
695 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000696
697 /* Gather up the stats that the hw collects */
698 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
699 veb->stat_offsets_loaded,
700 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000701 if (hw->revision_id > 0)
702 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
703 veb->stat_offsets_loaded,
704 &oes->rx_unknown_protocol,
705 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000706 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
707 veb->stat_offsets_loaded,
708 &oes->rx_bytes, &es->rx_bytes);
709 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
710 veb->stat_offsets_loaded,
711 &oes->rx_unicast, &es->rx_unicast);
712 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
713 veb->stat_offsets_loaded,
714 &oes->rx_multicast, &es->rx_multicast);
715 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
716 veb->stat_offsets_loaded,
717 &oes->rx_broadcast, &es->rx_broadcast);
718
719 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
720 veb->stat_offsets_loaded,
721 &oes->tx_bytes, &es->tx_bytes);
722 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
723 veb->stat_offsets_loaded,
724 &oes->tx_unicast, &es->tx_unicast);
725 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
726 veb->stat_offsets_loaded,
727 &oes->tx_multicast, &es->tx_multicast);
728 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
729 veb->stat_offsets_loaded,
730 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400731 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
732 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
733 I40E_GLVEBTC_RPCL(i, idx),
734 veb->stat_offsets_loaded,
735 &veb_oes->tc_rx_packets[i],
736 &veb_es->tc_rx_packets[i]);
737 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
738 I40E_GLVEBTC_RBCL(i, idx),
739 veb->stat_offsets_loaded,
740 &veb_oes->tc_rx_bytes[i],
741 &veb_es->tc_rx_bytes[i]);
742 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
743 I40E_GLVEBTC_TPCL(i, idx),
744 veb->stat_offsets_loaded,
745 &veb_oes->tc_tx_packets[i],
746 &veb_es->tc_tx_packets[i]);
747 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
748 I40E_GLVEBTC_TBCL(i, idx),
749 veb->stat_offsets_loaded,
750 &veb_oes->tc_tx_bytes[i],
751 &veb_es->tc_tx_bytes[i]);
752 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000753 veb->stat_offsets_loaded = true;
754}
755
Vasu Dev38e00432014-08-01 13:27:03 -0700756#ifdef I40E_FCOE
757/**
758 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
759 * @vsi: the VSI that is capable of doing FCoE
760 **/
761static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
762{
763 struct i40e_pf *pf = vsi->back;
764 struct i40e_hw *hw = &pf->hw;
765 struct i40e_fcoe_stats *ofs;
766 struct i40e_fcoe_stats *fs; /* device's eth stats */
767 int idx;
768
769 if (vsi->type != I40E_VSI_FCOE)
770 return;
771
Kiran Patil4147e2c2016-01-15 14:33:14 -0800772 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700773 fs = &vsi->fcoe_stats;
774 ofs = &vsi->fcoe_stats_offsets;
775
776 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
777 vsi->fcoe_stat_offsets_loaded,
778 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
779 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
780 vsi->fcoe_stat_offsets_loaded,
781 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
782 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
783 vsi->fcoe_stat_offsets_loaded,
784 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
785 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
786 vsi->fcoe_stat_offsets_loaded,
787 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
788 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
789 vsi->fcoe_stat_offsets_loaded,
790 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
791 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
792 vsi->fcoe_stat_offsets_loaded,
793 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
794 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
795 vsi->fcoe_stat_offsets_loaded,
796 &ofs->fcoe_last_error, &fs->fcoe_last_error);
797 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
798 vsi->fcoe_stat_offsets_loaded,
799 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
800
801 vsi->fcoe_stat_offsets_loaded = true;
802}
803
804#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000805/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000806 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000807 * @vsi: the VSI to be updated
808 *
809 * There are a few instances where we store the same stat in a
810 * couple of different structs. This is partly because we have
811 * the netdev stats that need to be filled out, which is slightly
812 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000813 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000814 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000815static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000816{
817 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000818 struct rtnl_link_stats64 *ons;
819 struct rtnl_link_stats64 *ns; /* netdev stats */
820 struct i40e_eth_stats *oes;
821 struct i40e_eth_stats *es; /* device's eth stats */
822 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800823 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000824 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000825 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000826 u64 bytes, packets;
827 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400828 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400829 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000830 u64 rx_p, rx_b;
831 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000832 u16 q;
833
834 if (test_bit(__I40E_DOWN, &vsi->state) ||
835 test_bit(__I40E_CONFIG_BUSY, &pf->state))
836 return;
837
838 ns = i40e_get_vsi_stats_struct(vsi);
839 ons = &vsi->net_stats_offsets;
840 es = &vsi->eth_stats;
841 oes = &vsi->eth_stats_offsets;
842
843 /* Gather up the netdev and vsi stats that the driver collects
844 * on the fly during packet processing
845 */
846 rx_b = rx_p = 0;
847 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400848 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800849 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000850 rx_page = 0;
851 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000852 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000853 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000854 /* locate Tx ring */
855 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000856
Alexander Duyck980e9b12013-09-28 06:01:03 +0000857 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700858 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000859 packets = p->stats.packets;
860 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700861 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000862 tx_b += bytes;
863 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000864 tx_restart += p->tx_stats.restart_queue;
865 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400866 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400867 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800868 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000869
870 /* Rx queue is part of the same block as Tx queue */
871 p = &p[1];
872 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700873 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000874 packets = p->stats.packets;
875 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700876 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000877 rx_b += bytes;
878 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000879 rx_buf += p->rx_stats.alloc_buff_failed;
880 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000881 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000882 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000883 vsi->tx_restart = tx_restart;
884 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400885 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400886 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800887 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000888 vsi->rx_page_failed = rx_page;
889 vsi->rx_buf_failed = rx_buf;
890
891 ns->rx_packets = rx_p;
892 ns->rx_bytes = rx_b;
893 ns->tx_packets = tx_p;
894 ns->tx_bytes = tx_b;
895
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000896 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000897 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000898 ons->tx_errors = oes->tx_errors;
899 ns->tx_errors = es->tx_errors;
900 ons->multicast = oes->rx_multicast;
901 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000902 ons->rx_dropped = oes->rx_discards;
903 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000904 ons->tx_dropped = oes->tx_discards;
905 ns->tx_dropped = es->tx_discards;
906
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000907 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000908 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000909 ns->rx_crc_errors = pf->stats.crc_errors;
910 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
911 ns->rx_length_errors = pf->stats.rx_length_errors;
912 }
913}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000914
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000915/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000916 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000917 * @pf: the PF to be updated
918 **/
919static void i40e_update_pf_stats(struct i40e_pf *pf)
920{
921 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
922 struct i40e_hw_port_stats *nsd = &pf->stats;
923 struct i40e_hw *hw = &pf->hw;
924 u32 val;
925 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000926
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000927 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
928 I40E_GLPRT_GORCL(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
931 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
932 I40E_GLPRT_GOTCL(hw->port),
933 pf->stat_offsets_loaded,
934 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
935 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->eth.rx_discards,
938 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000939 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
940 I40E_GLPRT_UPRCL(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->eth.rx_unicast,
943 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000944 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
945 I40E_GLPRT_MPRCL(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->eth.rx_multicast,
948 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000949 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
950 I40E_GLPRT_BPRCL(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->eth.rx_broadcast,
953 &nsd->eth.rx_broadcast);
954 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
955 I40E_GLPRT_UPTCL(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->eth.tx_unicast,
958 &nsd->eth.tx_unicast);
959 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
960 I40E_GLPRT_MPTCL(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->eth.tx_multicast,
963 &nsd->eth.tx_multicast);
964 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
965 I40E_GLPRT_BPTCL(hw->port),
966 pf->stat_offsets_loaded,
967 &osd->eth.tx_broadcast,
968 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000969
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000970 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->tx_dropped_link_down,
973 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000974
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000975 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000978
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000979 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000982
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000983 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->mac_local_faults,
986 &nsd->mac_local_faults);
987 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->mac_remote_faults,
990 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000991
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
993 pf->stat_offsets_loaded,
994 &osd->rx_length_errors,
995 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000996
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000997 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
998 pf->stat_offsets_loaded,
999 &osd->link_xon_rx, &nsd->link_xon_rx);
1000 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1001 pf->stat_offsets_loaded,
1002 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -08001003 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1004 pf->stat_offsets_loaded,
1005 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001006 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001009
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001010 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -08001011 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1012 pf->stat_offsets_loaded,
1013 &osd->priority_xoff_rx[i],
1014 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001015 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001016 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001017 &osd->priority_xon_rx[i],
1018 &nsd->priority_xon_rx[i]);
1019 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001020 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001021 &osd->priority_xon_tx[i],
1022 &nsd->priority_xon_tx[i]);
1023 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001024 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001025 &osd->priority_xoff_tx[i],
1026 &nsd->priority_xoff_tx[i]);
1027 i40e_stat_update32(hw,
1028 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001029 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001030 &osd->priority_xon_2_xoff[i],
1031 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001032 }
1033
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001034 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1035 I40E_GLPRT_PRC64L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->rx_size_64, &nsd->rx_size_64);
1038 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1039 I40E_GLPRT_PRC127L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->rx_size_127, &nsd->rx_size_127);
1042 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1043 I40E_GLPRT_PRC255L(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->rx_size_255, &nsd->rx_size_255);
1046 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1047 I40E_GLPRT_PRC511L(hw->port),
1048 pf->stat_offsets_loaded,
1049 &osd->rx_size_511, &nsd->rx_size_511);
1050 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1051 I40E_GLPRT_PRC1023L(hw->port),
1052 pf->stat_offsets_loaded,
1053 &osd->rx_size_1023, &nsd->rx_size_1023);
1054 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1055 I40E_GLPRT_PRC1522L(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->rx_size_1522, &nsd->rx_size_1522);
1058 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1059 I40E_GLPRT_PRC9522L(hw->port),
1060 pf->stat_offsets_loaded,
1061 &osd->rx_size_big, &nsd->rx_size_big);
1062
1063 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1064 I40E_GLPRT_PTC64L(hw->port),
1065 pf->stat_offsets_loaded,
1066 &osd->tx_size_64, &nsd->tx_size_64);
1067 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1068 I40E_GLPRT_PTC127L(hw->port),
1069 pf->stat_offsets_loaded,
1070 &osd->tx_size_127, &nsd->tx_size_127);
1071 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1072 I40E_GLPRT_PTC255L(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->tx_size_255, &nsd->tx_size_255);
1075 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1076 I40E_GLPRT_PTC511L(hw->port),
1077 pf->stat_offsets_loaded,
1078 &osd->tx_size_511, &nsd->tx_size_511);
1079 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1080 I40E_GLPRT_PTC1023L(hw->port),
1081 pf->stat_offsets_loaded,
1082 &osd->tx_size_1023, &nsd->tx_size_1023);
1083 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1084 I40E_GLPRT_PTC1522L(hw->port),
1085 pf->stat_offsets_loaded,
1086 &osd->tx_size_1522, &nsd->tx_size_1522);
1087 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1088 I40E_GLPRT_PTC9522L(hw->port),
1089 pf->stat_offsets_loaded,
1090 &osd->tx_size_big, &nsd->tx_size_big);
1091
1092 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1093 pf->stat_offsets_loaded,
1094 &osd->rx_undersize, &nsd->rx_undersize);
1095 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1096 pf->stat_offsets_loaded,
1097 &osd->rx_fragments, &nsd->rx_fragments);
1098 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1099 pf->stat_offsets_loaded,
1100 &osd->rx_oversize, &nsd->rx_oversize);
1101 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1102 pf->stat_offsets_loaded,
1103 &osd->rx_jabber, &nsd->rx_jabber);
1104
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001105 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001106 i40e_stat_update32(hw,
1107 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001108 pf->stat_offsets_loaded,
1109 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001110 i40e_stat_update32(hw,
1111 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001112 pf->stat_offsets_loaded,
1113 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001114 i40e_stat_update32(hw,
1115 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1116 pf->stat_offsets_loaded,
1117 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001118
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001119 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1120 nsd->tx_lpi_status =
1121 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1122 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1123 nsd->rx_lpi_status =
1124 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1125 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1126 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1127 pf->stat_offsets_loaded,
1128 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1129 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1130 pf->stat_offsets_loaded,
1131 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1132
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001133 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1134 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1135 nsd->fd_sb_status = true;
1136 else
1137 nsd->fd_sb_status = false;
1138
1139 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1140 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1141 nsd->fd_atr_status = true;
1142 else
1143 nsd->fd_atr_status = false;
1144
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001145 pf->stat_offsets_loaded = true;
1146}
1147
1148/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001149 * i40e_update_stats - Update the various statistics counters.
1150 * @vsi: the VSI to be updated
1151 *
1152 * Update the various stats for this VSI and its related entities.
1153 **/
1154void i40e_update_stats(struct i40e_vsi *vsi)
1155{
1156 struct i40e_pf *pf = vsi->back;
1157
1158 if (vsi == pf->vsi[pf->lan_vsi])
1159 i40e_update_pf_stats(pf);
1160
1161 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001162#ifdef I40E_FCOE
1163 i40e_update_fcoe_stats(vsi);
1164#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001165}
1166
1167/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001168 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1169 * @vsi: the VSI to be searched
1170 * @macaddr: the MAC address
1171 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001172 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173 * @is_netdev: make sure its a netdev filter, else doesn't matter
1174 *
1175 * Returns ptr to the filter object or NULL
1176 **/
1177static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1178 u8 *macaddr, s16 vlan,
1179 bool is_vf, bool is_netdev)
1180{
1181 struct i40e_mac_filter *f;
1182
1183 if (!vsi || !macaddr)
1184 return NULL;
1185
1186 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1187 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1188 (vlan == f->vlan) &&
1189 (!is_vf || f->is_vf) &&
1190 (!is_netdev || f->is_netdev))
1191 return f;
1192 }
1193 return NULL;
1194}
1195
1196/**
1197 * i40e_find_mac - Find a mac addr in the macvlan filters list
1198 * @vsi: the VSI to be searched
1199 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001200 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001201 * @is_netdev: make sure its a netdev filter, else doesn't matter
1202 *
1203 * Returns the first filter with the provided MAC address or NULL if
1204 * MAC address was not found
1205 **/
1206struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1207 bool is_vf, bool is_netdev)
1208{
1209 struct i40e_mac_filter *f;
1210
1211 if (!vsi || !macaddr)
1212 return NULL;
1213
1214 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1215 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1216 (!is_vf || f->is_vf) &&
1217 (!is_netdev || f->is_netdev))
1218 return f;
1219 }
1220 return NULL;
1221}
1222
1223/**
1224 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1225 * @vsi: the VSI to be searched
1226 *
1227 * Returns true if VSI is in vlan mode or false otherwise
1228 **/
1229bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1230{
1231 struct i40e_mac_filter *f;
1232
1233 /* Only -1 for all the filters denotes not in vlan mode
1234 * so we have to go through all the list in order to make sure
1235 */
1236 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001237 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001238 return true;
1239 }
1240
1241 return false;
1242}
1243
1244/**
1245 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1246 * @vsi: the VSI to be searched
1247 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001248 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001249 * @is_netdev: true if it is a netdev
1250 *
1251 * Goes through all the macvlan filters and adds a
1252 * macvlan filter for each unique vlan that already exists
1253 *
1254 * Returns first filter found on success, else NULL
1255 **/
1256struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1257 bool is_vf, bool is_netdev)
1258{
1259 struct i40e_mac_filter *f;
1260
1261 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001262 if (vsi->info.pvid)
1263 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001264 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1265 is_vf, is_netdev)) {
1266 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001267 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001268 return NULL;
1269 }
1270 }
1271
1272 return list_first_entry_or_null(&vsi->mac_filter_list,
1273 struct i40e_mac_filter, list);
1274}
1275
1276/**
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001277 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1278 * @vsi: the VSI to be searched
1279 * @macaddr: the mac address to be removed
1280 * @is_vf: true if it is a VF
1281 * @is_netdev: true if it is a netdev
1282 *
1283 * Removes a given MAC address from a VSI, regardless of VLAN
1284 *
1285 * Returns 0 for success, or error
1286 **/
1287int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1288 bool is_vf, bool is_netdev)
1289{
1290 struct i40e_mac_filter *f = NULL;
1291 int changed = 0;
1292
1293 WARN(!spin_is_locked(&vsi->mac_filter_list_lock),
1294 "Missing mac_filter_list_lock\n");
1295 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1296 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1297 (is_vf == f->is_vf) &&
1298 (is_netdev == f->is_netdev)) {
1299 f->counter--;
1300 f->changed = true;
1301 changed = 1;
1302 }
1303 }
1304 if (changed) {
1305 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1306 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1307 return 0;
1308 }
1309 return -ENOENT;
1310}
1311
1312/**
Greg Rose8c27d422014-05-22 06:31:56 +00001313 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1314 * @vsi: the PF Main VSI - inappropriate for any other VSI
1315 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001316 *
1317 * Some older firmware configurations set up a default promiscuous VLAN
1318 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001319 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001320static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001321{
1322 struct i40e_aqc_remove_macvlan_element_data element;
1323 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001324 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001325
1326 /* Only appropriate for the PF main VSI */
1327 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001328 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001329
Shannon Nelson30650cc2014-07-29 04:01:50 +00001330 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001331 ether_addr_copy(element.mac_addr, macaddr);
1332 element.vlan_tag = 0;
1333 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1334 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001335 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1336 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001337 return -ENOENT;
1338
1339 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001340}
1341
1342/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001343 * i40e_add_filter - Add a mac/vlan filter to the VSI
1344 * @vsi: the VSI to be searched
1345 * @macaddr: the MAC address
1346 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001347 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001348 * @is_netdev: make sure its a netdev filter, else doesn't matter
1349 *
1350 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001351 *
1352 * NOTE: This function is expected to be called with mac_filter_list_lock
1353 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 **/
1355struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1356 u8 *macaddr, s16 vlan,
1357 bool is_vf, bool is_netdev)
1358{
1359 struct i40e_mac_filter *f;
1360
1361 if (!vsi || !macaddr)
1362 return NULL;
1363
1364 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1365 if (!f) {
1366 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1367 if (!f)
1368 goto add_filter_out;
1369
Greg Rose9a173902014-05-22 06:32:02 +00001370 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371 f->vlan = vlan;
1372 f->changed = true;
1373
1374 INIT_LIST_HEAD(&f->list);
Kiran Patil04d5a212015-12-09 15:50:23 -08001375 list_add_tail(&f->list, &vsi->mac_filter_list);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001376 }
1377
1378 /* increment counter and add a new flag if needed */
1379 if (is_vf) {
1380 if (!f->is_vf) {
1381 f->is_vf = true;
1382 f->counter++;
1383 }
1384 } else if (is_netdev) {
1385 if (!f->is_netdev) {
1386 f->is_netdev = true;
1387 f->counter++;
1388 }
1389 } else {
1390 f->counter++;
1391 }
1392
1393 /* changed tells sync_filters_subtask to
1394 * push the filter down to the firmware
1395 */
1396 if (f->changed) {
1397 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1398 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1399 }
1400
1401add_filter_out:
1402 return f;
1403}
1404
1405/**
1406 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1407 * @vsi: the VSI to be searched
1408 * @macaddr: the MAC address
1409 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001410 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001411 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001412 *
1413 * NOTE: This function is expected to be called with mac_filter_list_lock
1414 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001415 **/
1416void i40e_del_filter(struct i40e_vsi *vsi,
1417 u8 *macaddr, s16 vlan,
1418 bool is_vf, bool is_netdev)
1419{
1420 struct i40e_mac_filter *f;
1421
1422 if (!vsi || !macaddr)
1423 return;
1424
1425 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1426 if (!f || f->counter == 0)
1427 return;
1428
1429 if (is_vf) {
1430 if (f->is_vf) {
1431 f->is_vf = false;
1432 f->counter--;
1433 }
1434 } else if (is_netdev) {
1435 if (f->is_netdev) {
1436 f->is_netdev = false;
1437 f->counter--;
1438 }
1439 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001440 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001441 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001442
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001443 min_f += (f->is_vf ? 1 : 0);
1444 min_f += (f->is_netdev ? 1 : 0);
1445
1446 if (f->counter > min_f)
1447 f->counter--;
1448 }
1449
1450 /* counter == 0 tells sync_filters_subtask to
1451 * remove the filter from the firmware's list
1452 */
1453 if (f->counter == 0) {
1454 f->changed = true;
1455 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1456 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1457 }
1458}
1459
1460/**
1461 * i40e_set_mac - NDO callback to set mac address
1462 * @netdev: network interface device structure
1463 * @p: pointer to an address structure
1464 *
1465 * Returns 0 on success, negative on failure
1466 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001467#ifdef I40E_FCOE
1468int i40e_set_mac(struct net_device *netdev, void *p)
1469#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001470static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001471#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001472{
1473 struct i40e_netdev_priv *np = netdev_priv(netdev);
1474 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001475 struct i40e_pf *pf = vsi->back;
1476 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001477 struct sockaddr *addr = p;
1478 struct i40e_mac_filter *f;
1479
1480 if (!is_valid_ether_addr(addr->sa_data))
1481 return -EADDRNOTAVAIL;
1482
Shannon Nelson30650cc2014-07-29 04:01:50 +00001483 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1484 netdev_info(netdev, "already using mac address %pM\n",
1485 addr->sa_data);
1486 return 0;
1487 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001488
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001489 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1490 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1491 return -EADDRNOTAVAIL;
1492
Shannon Nelson30650cc2014-07-29 04:01:50 +00001493 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1494 netdev_info(netdev, "returning to hw mac address %pM\n",
1495 hw->mac.addr);
1496 else
1497 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1498
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001499 if (vsi->type == I40E_VSI_MAIN) {
1500 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001501
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001502 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001503 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001504 addr->sa_data, NULL);
1505 if (ret) {
1506 netdev_info(netdev,
1507 "Addr change for Main VSI failed: %d\n",
1508 ret);
1509 return -EADDRNOTAVAIL;
1510 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001511 }
1512
Shannon Nelson30650cc2014-07-29 04:01:50 +00001513 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1514 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001515
Shannon Nelson30650cc2014-07-29 04:01:50 +00001516 memset(&element, 0, sizeof(element));
1517 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1518 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1519 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1520 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001521 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001522 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1523 false, false);
Kiran Patil21659032015-09-30 14:09:03 -04001524 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001525 }
1526
Shannon Nelson30650cc2014-07-29 04:01:50 +00001527 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1528 struct i40e_aqc_add_macvlan_element_data element;
1529
1530 memset(&element, 0, sizeof(element));
1531 ether_addr_copy(element.mac_addr, hw->mac.addr);
1532 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1533 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1534 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001535 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001536 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1537 false, false);
1538 if (f)
1539 f->is_laa = true;
Kiran Patil21659032015-09-30 14:09:03 -04001540 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001541 }
1542
Shannon Nelson30650cc2014-07-29 04:01:50 +00001543 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Mitch Williamsea02e902015-11-09 15:35:50 -08001544
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001545 /* schedule our worker thread which will take care of
1546 * applying the new filter changes
1547 */
1548 i40e_service_event_schedule(vsi->back);
1549 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001550}
1551
1552/**
1553 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1554 * @vsi: the VSI being setup
1555 * @ctxt: VSI context structure
1556 * @enabled_tc: Enabled TCs bitmap
1557 * @is_add: True if called before Add VSI
1558 *
1559 * Setup VSI queue mapping for enabled traffic classes.
1560 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001561#ifdef I40E_FCOE
1562void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1563 struct i40e_vsi_context *ctxt,
1564 u8 enabled_tc,
1565 bool is_add)
1566#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001567static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1568 struct i40e_vsi_context *ctxt,
1569 u8 enabled_tc,
1570 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001571#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572{
1573 struct i40e_pf *pf = vsi->back;
1574 u16 sections = 0;
1575 u8 netdev_tc = 0;
1576 u16 numtc = 0;
1577 u16 qcount;
1578 u8 offset;
1579 u16 qmap;
1580 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001581 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582
1583 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1584 offset = 0;
1585
1586 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1587 /* Find numtc from enabled TC bitmap */
1588 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001589 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001590 numtc++;
1591 }
1592 if (!numtc) {
1593 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1594 numtc = 1;
1595 }
1596 } else {
1597 /* At least TC0 is enabled in case of non-DCB case */
1598 numtc = 1;
1599 }
1600
1601 vsi->tc_config.numtc = numtc;
1602 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001603 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001604 /* In MFP case we can have a much lower count of MSIx
1605 * vectors available and so we need to lower the used
1606 * q count.
1607 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001608 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1609 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1610 else
1611 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001612 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001613 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614
1615 /* Setup queue offset/count for all TCs for given VSI */
1616 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1617 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001618 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001619 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001620 int pow, num_qps;
1621
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001622 switch (vsi->type) {
1623 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001624 qcount = min_t(int, pf->alloc_rss_size,
1625 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001626 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001627#ifdef I40E_FCOE
1628 case I40E_VSI_FCOE:
1629 qcount = num_tc_qps;
1630 break;
1631#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632 case I40E_VSI_FDIR:
1633 case I40E_VSI_SRIOV:
1634 case I40E_VSI_VMDQ2:
1635 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001636 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637 WARN_ON(i != 0);
1638 break;
1639 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001640 vsi->tc_config.tc_info[i].qoffset = offset;
1641 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001642
Shannon Nelson1e200e42015-02-27 09:15:24 +00001643 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001644 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001645 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001646 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001647 pow++;
1648 num_qps >>= 1;
1649 }
1650
1651 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1652 qmap =
1653 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1654 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1655
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001656 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001657 } else {
1658 /* TC is not enabled so set the offset to
1659 * default queue and allocate one queue
1660 * for the given TC.
1661 */
1662 vsi->tc_config.tc_info[i].qoffset = 0;
1663 vsi->tc_config.tc_info[i].qcount = 1;
1664 vsi->tc_config.tc_info[i].netdev_tc = 0;
1665
1666 qmap = 0;
1667 }
1668 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1669 }
1670
1671 /* Set actual Tx/Rx queue pairs */
1672 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001673 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1674 if (vsi->req_queue_pairs > 0)
1675 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001676 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001677 vsi->num_queue_pairs = pf->num_lan_msix;
1678 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001679
1680 /* Scheduler section valid can only be set for ADD VSI */
1681 if (is_add) {
1682 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1683
1684 ctxt->info.up_enable_bits = enabled_tc;
1685 }
1686 if (vsi->type == I40E_VSI_SRIOV) {
1687 ctxt->info.mapping_flags |=
1688 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1689 for (i = 0; i < vsi->num_queue_pairs; i++)
1690 ctxt->info.queue_mapping[i] =
1691 cpu_to_le16(vsi->base_queue + i);
1692 } else {
1693 ctxt->info.mapping_flags |=
1694 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1695 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1696 }
1697 ctxt->info.valid_sections |= cpu_to_le16(sections);
1698}
1699
1700/**
1701 * i40e_set_rx_mode - NDO callback to set the netdev filters
1702 * @netdev: network interface device structure
1703 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001704#ifdef I40E_FCOE
1705void i40e_set_rx_mode(struct net_device *netdev)
1706#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001707static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001708#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001709{
1710 struct i40e_netdev_priv *np = netdev_priv(netdev);
1711 struct i40e_mac_filter *f, *ftmp;
1712 struct i40e_vsi *vsi = np->vsi;
1713 struct netdev_hw_addr *uca;
1714 struct netdev_hw_addr *mca;
1715 struct netdev_hw_addr *ha;
1716
Kiran Patil21659032015-09-30 14:09:03 -04001717 spin_lock_bh(&vsi->mac_filter_list_lock);
1718
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001719 /* add addr if not already in the filter list */
1720 netdev_for_each_uc_addr(uca, netdev) {
1721 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1722 if (i40e_is_vsi_in_vlan(vsi))
1723 i40e_put_mac_in_vlan(vsi, uca->addr,
1724 false, true);
1725 else
1726 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1727 false, true);
1728 }
1729 }
1730
1731 netdev_for_each_mc_addr(mca, netdev) {
1732 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1733 if (i40e_is_vsi_in_vlan(vsi))
1734 i40e_put_mac_in_vlan(vsi, mca->addr,
1735 false, true);
1736 else
1737 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1738 false, true);
1739 }
1740 }
1741
1742 /* remove filter if not in netdev list */
1743 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001744
1745 if (!f->is_netdev)
1746 continue;
1747
Shannon Nelson2f41f332015-08-26 15:14:20 -04001748 netdev_for_each_mc_addr(mca, netdev)
1749 if (ether_addr_equal(mca->addr, f->macaddr))
1750 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001751
Shannon Nelson2f41f332015-08-26 15:14:20 -04001752 netdev_for_each_uc_addr(uca, netdev)
1753 if (ether_addr_equal(uca->addr, f->macaddr))
1754 goto bottom_of_search_loop;
1755
1756 for_each_dev_addr(netdev, ha)
1757 if (ether_addr_equal(ha->addr, f->macaddr))
1758 goto bottom_of_search_loop;
1759
1760 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1761 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1762
1763bottom_of_search_loop:
1764 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001765 }
Kiran Patil21659032015-09-30 14:09:03 -04001766 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001767
1768 /* check for other flag changes */
1769 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1770 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1771 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1772 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001773
1774 /* schedule our worker thread which will take care of
1775 * applying the new filter changes
1776 */
1777 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001778}
1779
1780/**
Kiran Patil21659032015-09-30 14:09:03 -04001781 * i40e_mac_filter_entry_clone - Clones a MAC filter entry
1782 * @src: source MAC filter entry to be clones
1783 *
1784 * Returns the pointer to newly cloned MAC filter entry or NULL
1785 * in case of error
1786 **/
1787static struct i40e_mac_filter *i40e_mac_filter_entry_clone(
1788 struct i40e_mac_filter *src)
1789{
1790 struct i40e_mac_filter *f;
1791
1792 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1793 if (!f)
1794 return NULL;
1795 *f = *src;
1796
1797 INIT_LIST_HEAD(&f->list);
1798
1799 return f;
1800}
1801
1802/**
1803 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1804 * @vsi: pointer to vsi struct
1805 * @from: Pointer to list which contains MAC filter entries - changes to
1806 * those entries needs to be undone.
1807 *
1808 * MAC filter entries from list were slated to be removed from device.
1809 **/
1810static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1811 struct list_head *from)
1812{
1813 struct i40e_mac_filter *f, *ftmp;
1814
1815 list_for_each_entry_safe(f, ftmp, from, list) {
1816 f->changed = true;
1817 /* Move the element back into MAC filter list*/
1818 list_move_tail(&f->list, &vsi->mac_filter_list);
1819 }
1820}
1821
1822/**
1823 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1824 * @vsi: pointer to vsi struct
1825 *
1826 * MAC filter entries from list were slated to be added from device.
1827 **/
1828static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi)
1829{
1830 struct i40e_mac_filter *f, *ftmp;
1831
1832 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1833 if (!f->changed && f->counter)
1834 f->changed = true;
1835 }
1836}
1837
1838/**
1839 * i40e_cleanup_add_list - Deletes the element from add list and release
1840 * memory
1841 * @add_list: Pointer to list which contains MAC filter entries
1842 **/
1843static void i40e_cleanup_add_list(struct list_head *add_list)
1844{
1845 struct i40e_mac_filter *f, *ftmp;
1846
1847 list_for_each_entry_safe(f, ftmp, add_list, list) {
1848 list_del(&f->list);
1849 kfree(f);
1850 }
1851}
1852
1853/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001854 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1855 * @vsi: ptr to the VSI
1856 *
1857 * Push any outstanding VSI filter changes through the AdminQ.
1858 *
1859 * Returns 0 or error value
1860 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001861int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001862{
Kiran Patil21659032015-09-30 14:09:03 -04001863 struct list_head tmp_del_list, tmp_add_list;
1864 struct i40e_mac_filter *f, *ftmp, *fclone;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001865 bool promisc_forced_on = false;
1866 bool add_happened = false;
1867 int filter_list_len = 0;
1868 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001869 i40e_status aq_ret = 0;
Kiran Patil21659032015-09-30 14:09:03 -04001870 bool err_cond = false;
Mitch Williamsea02e902015-11-09 15:35:50 -08001871 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001872 struct i40e_pf *pf;
1873 int num_add = 0;
1874 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001875 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001876 u16 cmd_flags;
1877
1878 /* empty array typed pointers, kcalloc later */
1879 struct i40e_aqc_add_macvlan_element_data *add_list;
1880 struct i40e_aqc_remove_macvlan_element_data *del_list;
1881
1882 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1883 usleep_range(1000, 2000);
1884 pf = vsi->back;
1885
1886 if (vsi->netdev) {
1887 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1888 vsi->current_netdev_flags = vsi->netdev->flags;
1889 }
1890
Kiran Patil21659032015-09-30 14:09:03 -04001891 INIT_LIST_HEAD(&tmp_del_list);
1892 INIT_LIST_HEAD(&tmp_add_list);
1893
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001894 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1895 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1896
Kiran Patil21659032015-09-30 14:09:03 -04001897 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001898 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1899 if (!f->changed)
1900 continue;
1901
1902 if (f->counter != 0)
1903 continue;
1904 f->changed = false;
Kiran Patil21659032015-09-30 14:09:03 -04001905
1906 /* Move the element into temporary del_list */
1907 list_move_tail(&f->list, &tmp_del_list);
1908 }
1909
1910 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1911 if (!f->changed)
1912 continue;
1913
1914 if (f->counter == 0)
1915 continue;
1916 f->changed = false;
1917
1918 /* Clone MAC filter entry and add into temporary list */
1919 fclone = i40e_mac_filter_entry_clone(f);
1920 if (!fclone) {
1921 err_cond = true;
1922 break;
1923 }
1924 list_add_tail(&fclone->list, &tmp_add_list);
1925 }
1926
1927 /* if failed to clone MAC filter entry - undo */
1928 if (err_cond) {
1929 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1930 i40e_undo_add_filter_entries(vsi);
1931 }
1932 spin_unlock_bh(&vsi->mac_filter_list_lock);
1933
Mitch Williamsea02e902015-11-09 15:35:50 -08001934 if (err_cond) {
Kiran Patil21659032015-09-30 14:09:03 -04001935 i40e_cleanup_add_list(&tmp_add_list);
Mitch Williamsea02e902015-11-09 15:35:50 -08001936 retval = -ENOMEM;
1937 goto out;
1938 }
Kiran Patil21659032015-09-30 14:09:03 -04001939 }
1940
1941 /* Now process 'del_list' outside the lock */
1942 if (!list_empty(&tmp_del_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08001943 int del_list_size;
1944
Kiran Patil21659032015-09-30 14:09:03 -04001945 filter_list_len = pf->hw.aq.asq_buf_size /
1946 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Shannon Nelsonf1199992015-11-19 11:34:23 -08001947 del_list_size = filter_list_len *
1948 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Jesse Brandeburg38c3cec2016-01-04 10:33:05 -08001949 del_list = kzalloc(del_list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001950 if (!del_list) {
1951 i40e_cleanup_add_list(&tmp_add_list);
1952
1953 /* Undo VSI's MAC filter entry element updates */
1954 spin_lock_bh(&vsi->mac_filter_list_lock);
1955 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1956 i40e_undo_add_filter_entries(vsi);
1957 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001958 retval = -ENOMEM;
1959 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001960 }
1961
1962 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001963 cmd_flags = 0;
1964
1965 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001966 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001967 del_list[num_del].vlan_tag =
1968 cpu_to_le16((u16)(f->vlan ==
1969 I40E_VLAN_ANY ? 0 : f->vlan));
1970
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001971 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1972 del_list[num_del].flags = cmd_flags;
1973 num_del++;
1974
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001975 /* flush a full buffer */
1976 if (num_del == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001977 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
1978 vsi->seid,
1979 del_list,
1980 num_del,
1981 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001982 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001983 num_del = 0;
Shannon Nelsonf1199992015-11-19 11:34:23 -08001984 memset(del_list, 0, del_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985
Mitch Williamsea02e902015-11-09 15:35:50 -08001986 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT) {
1987 retval = -EIO;
Kiran Patil21659032015-09-30 14:09:03 -04001988 dev_err(&pf->pdev->dev,
1989 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08001990 i40e_stat_str(&pf->hw, aq_ret),
Kiran Patil21659032015-09-30 14:09:03 -04001991 i40e_aq_str(&pf->hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001992 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001993 }
Kiran Patil21659032015-09-30 14:09:03 -04001994 /* Release memory for MAC filter entries which were
1995 * synced up with HW.
1996 */
1997 list_del(&f->list);
1998 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001999 }
Kiran Patil21659032015-09-30 14:09:03 -04002000
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002001 if (num_del) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002002 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
2003 del_list, num_del,
2004 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002005 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002006 num_del = 0;
2007
Mitch Williamsea02e902015-11-09 15:35:50 -08002008 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002009 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002010 "ignoring delete macvlan error, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002011 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002012 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002013 }
2014
2015 kfree(del_list);
2016 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002017 }
2018
2019 if (!list_empty(&tmp_add_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08002020 int add_list_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002021
2022 /* do all the adds now */
2023 filter_list_len = pf->hw.aq.asq_buf_size /
2024 sizeof(struct i40e_aqc_add_macvlan_element_data),
Shannon Nelsonf1199992015-11-19 11:34:23 -08002025 add_list_size = filter_list_len *
2026 sizeof(struct i40e_aqc_add_macvlan_element_data);
Jesse Brandeburg38c3cec2016-01-04 10:33:05 -08002027 add_list = kzalloc(add_list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04002028 if (!add_list) {
2029 /* Purge element from temporary lists */
2030 i40e_cleanup_add_list(&tmp_add_list);
2031
2032 /* Undo add filter entries from VSI MAC filter list */
2033 spin_lock_bh(&vsi->mac_filter_list_lock);
2034 i40e_undo_add_filter_entries(vsi);
2035 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08002036 retval = -ENOMEM;
2037 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04002038 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002039
Kiran Patil21659032015-09-30 14:09:03 -04002040 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002041
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002042 add_happened = true;
2043 cmd_flags = 0;
2044
2045 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00002046 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002047 add_list[num_add].vlan_tag =
2048 cpu_to_le16(
2049 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
2050 add_list[num_add].queue_number = 0;
2051
2052 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002053 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2054 num_add++;
2055
2056 /* flush a full buffer */
2057 if (num_add == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002058 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2059 add_list, num_add,
2060 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002061 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 num_add = 0;
2063
Mitch Williamsea02e902015-11-09 15:35:50 -08002064 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002065 break;
Shannon Nelsonf1199992015-11-19 11:34:23 -08002066 memset(add_list, 0, add_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002067 }
Kiran Patil21659032015-09-30 14:09:03 -04002068 /* Entries from tmp_add_list were cloned from MAC
2069 * filter list, hence clean those cloned entries
2070 */
2071 list_del(&f->list);
2072 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073 }
Kiran Patil21659032015-09-30 14:09:03 -04002074
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075 if (num_add) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002076 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2077 add_list, num_add, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002078 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002079 num_add = 0;
2080 }
2081 kfree(add_list);
2082 add_list = NULL;
2083
Mitch Williamsea02e902015-11-09 15:35:50 -08002084 if (add_happened && aq_ret && aq_err != I40E_AQ_RC_EINVAL) {
2085 retval = i40e_aq_rc_to_posix(aq_ret, aq_err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002086 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002087 "add filter failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002088 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002089 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002090 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
2091 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2092 &vsi->state)) {
2093 promisc_forced_on = true;
2094 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2095 &vsi->state);
2096 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
2097 }
2098 }
2099 }
2100
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002101 /* if the VF is not trusted do not do promisc */
2102 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2103 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2104 goto out;
2105 }
2106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002107 /* check for changes in promiscuous modes */
2108 if (changed_flags & IFF_ALLMULTI) {
2109 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002110
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002111 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002112 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2113 vsi->seid,
2114 cur_multipromisc,
2115 NULL);
2116 if (aq_ret) {
2117 retval = i40e_aq_rc_to_posix(aq_ret,
2118 pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002120 "set multi promisc failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002121 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002122 i40e_aq_str(&pf->hw,
2123 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002124 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002125 }
2126 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
2127 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002128
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002129 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2130 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2131 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002132 if ((vsi->type == I40E_VSI_MAIN) &&
2133 (pf->lan_veb != I40E_NO_VEB) &&
2134 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002135 /* set defport ON for Main VSI instead of true promisc
2136 * this way we will get all unicast/multicast and VLAN
2137 * promisc behavior but will not get VF or VMDq traffic
2138 * replicated on the Main VSI.
2139 */
2140 if (pf->cur_promisc != cur_promisc) {
2141 pf->cur_promisc = cur_promisc;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002142 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002143 }
2144 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002145 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002146 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002147 vsi->seid,
2148 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002149 if (aq_ret) {
2150 retval =
2151 i40e_aq_rc_to_posix(aq_ret,
2152 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002153 dev_info(&pf->pdev->dev,
2154 "set unicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002155 aq_ret, pf->hw.aq.asq_last_status);
2156 }
2157 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002158 &vsi->back->hw,
2159 vsi->seid,
2160 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002161 if (aq_ret) {
2162 retval =
2163 i40e_aq_rc_to_posix(aq_ret,
2164 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002165 dev_info(&pf->pdev->dev,
2166 "set multicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002167 aq_ret, pf->hw.aq.asq_last_status);
2168 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002169 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002170 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2171 vsi->seid,
2172 cur_promisc, NULL);
2173 if (aq_ret) {
2174 retval = i40e_aq_rc_to_posix(aq_ret,
2175 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002176 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002177 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002178 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002179 i40e_aq_str(&pf->hw,
2180 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002181 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002182 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002183out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002184 /* if something went wrong then set the changed flag so we try again */
2185 if (retval)
2186 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002188 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002189 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002190}
2191
2192/**
2193 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2194 * @pf: board private structure
2195 **/
2196static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2197{
2198 int v;
2199
2200 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2201 return;
2202 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2203
Mitch Williams505682c2014-05-20 08:01:37 +00002204 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002205 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002206 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2207 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2208
2209 if (ret) {
2210 /* come back and try again later */
2211 pf->flags |= I40E_FLAG_FILTER_SYNC;
2212 break;
2213 }
2214 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002215 }
2216}
2217
2218/**
2219 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2220 * @netdev: network interface device structure
2221 * @new_mtu: new value for maximum frame size
2222 *
2223 * Returns 0 on success, negative on failure
2224 **/
2225static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2226{
2227 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002228 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002229 struct i40e_vsi *vsi = np->vsi;
2230
2231 /* MTU < 68 is an error and causes problems on some kernels */
2232 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2233 return -EINVAL;
2234
2235 netdev_info(netdev, "changing MTU from %d to %d\n",
2236 netdev->mtu, new_mtu);
2237 netdev->mtu = new_mtu;
2238 if (netif_running(netdev))
2239 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002240 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 return 0;
2242}
2243
2244/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002245 * i40e_ioctl - Access the hwtstamp interface
2246 * @netdev: network interface device structure
2247 * @ifr: interface request data
2248 * @cmd: ioctl command
2249 **/
2250int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2251{
2252 struct i40e_netdev_priv *np = netdev_priv(netdev);
2253 struct i40e_pf *pf = np->vsi->back;
2254
2255 switch (cmd) {
2256 case SIOCGHWTSTAMP:
2257 return i40e_ptp_get_ts_config(pf, ifr);
2258 case SIOCSHWTSTAMP:
2259 return i40e_ptp_set_ts_config(pf, ifr);
2260 default:
2261 return -EOPNOTSUPP;
2262 }
2263}
2264
2265/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002266 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2267 * @vsi: the vsi being adjusted
2268 **/
2269void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2270{
2271 struct i40e_vsi_context ctxt;
2272 i40e_status ret;
2273
2274 if ((vsi->info.valid_sections &
2275 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2276 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2277 return; /* already enabled */
2278
2279 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2280 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2281 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2282
2283 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002284 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002285 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2286 if (ret) {
2287 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002288 "update vlan stripping failed, err %s aq_err %s\n",
2289 i40e_stat_str(&vsi->back->hw, ret),
2290 i40e_aq_str(&vsi->back->hw,
2291 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002292 }
2293}
2294
2295/**
2296 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2297 * @vsi: the vsi being adjusted
2298 **/
2299void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2300{
2301 struct i40e_vsi_context ctxt;
2302 i40e_status ret;
2303
2304 if ((vsi->info.valid_sections &
2305 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2306 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2307 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2308 return; /* already disabled */
2309
2310 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2311 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2312 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2313
2314 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002315 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002316 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2317 if (ret) {
2318 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002319 "update vlan stripping failed, err %s aq_err %s\n",
2320 i40e_stat_str(&vsi->back->hw, ret),
2321 i40e_aq_str(&vsi->back->hw,
2322 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002323 }
2324}
2325
2326/**
2327 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2328 * @netdev: network interface to be adjusted
2329 * @features: netdev features to test if VLAN offload is enabled or not
2330 **/
2331static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2332{
2333 struct i40e_netdev_priv *np = netdev_priv(netdev);
2334 struct i40e_vsi *vsi = np->vsi;
2335
2336 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2337 i40e_vlan_stripping_enable(vsi);
2338 else
2339 i40e_vlan_stripping_disable(vsi);
2340}
2341
2342/**
2343 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2344 * @vsi: the vsi being configured
2345 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2346 **/
2347int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2348{
2349 struct i40e_mac_filter *f, *add_f;
2350 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002351
2352 is_vf = (vsi->type == I40E_VSI_SRIOV);
2353 is_netdev = !!(vsi->netdev);
2354
Kiran Patil21659032015-09-30 14:09:03 -04002355 /* Locked once because all functions invoked below iterates list*/
2356 spin_lock_bh(&vsi->mac_filter_list_lock);
2357
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002358 if (is_netdev) {
2359 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2360 is_vf, is_netdev);
2361 if (!add_f) {
2362 dev_info(&vsi->back->pdev->dev,
2363 "Could not add vlan filter %d for %pM\n",
2364 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002365 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002366 return -ENOMEM;
2367 }
2368 }
2369
2370 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2371 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2372 if (!add_f) {
2373 dev_info(&vsi->back->pdev->dev,
2374 "Could not add vlan filter %d for %pM\n",
2375 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002376 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002377 return -ENOMEM;
2378 }
2379 }
2380
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002381 /* Now if we add a vlan tag, make sure to check if it is the first
2382 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2383 * with 0, so we now accept untagged and specified tagged traffic
2384 * (and not any taged and untagged)
2385 */
2386 if (vid > 0) {
2387 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2388 I40E_VLAN_ANY,
2389 is_vf, is_netdev)) {
2390 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2391 I40E_VLAN_ANY, is_vf, is_netdev);
2392 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2393 is_vf, is_netdev);
2394 if (!add_f) {
2395 dev_info(&vsi->back->pdev->dev,
2396 "Could not add filter 0 for %pM\n",
2397 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002398 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002399 return -ENOMEM;
2400 }
2401 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002402 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403
Greg Rose8d82a7c2014-01-13 16:13:04 -08002404 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2405 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002406 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002407 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2408 is_vf, is_netdev))
2409 continue;
2410 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2411 is_vf, is_netdev);
2412 add_f = i40e_add_filter(vsi, f->macaddr,
2413 0, is_vf, is_netdev);
2414 if (!add_f) {
2415 dev_info(&vsi->back->pdev->dev,
2416 "Could not add filter 0 for %pM\n",
2417 f->macaddr);
2418 spin_unlock_bh(&vsi->mac_filter_list_lock);
2419 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002420 }
2421 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002422 }
2423
Kiran Patil21659032015-09-30 14:09:03 -04002424 spin_unlock_bh(&vsi->mac_filter_list_lock);
2425
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002426 /* schedule our worker thread which will take care of
2427 * applying the new filter changes
2428 */
2429 i40e_service_event_schedule(vsi->back);
2430 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002431}
2432
2433/**
2434 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2435 * @vsi: the vsi being configured
2436 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002437 *
2438 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002439 **/
2440int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2441{
2442 struct net_device *netdev = vsi->netdev;
2443 struct i40e_mac_filter *f, *add_f;
2444 bool is_vf, is_netdev;
2445 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002446
2447 is_vf = (vsi->type == I40E_VSI_SRIOV);
2448 is_netdev = !!(netdev);
2449
Kiran Patil21659032015-09-30 14:09:03 -04002450 /* Locked once because all functions invoked below iterates list */
2451 spin_lock_bh(&vsi->mac_filter_list_lock);
2452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002453 if (is_netdev)
2454 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2455
2456 list_for_each_entry(f, &vsi->mac_filter_list, list)
2457 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2458
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002459 /* go through all the filters for this VSI and if there is only
2460 * vid == 0 it means there are no other filters, so vid 0 must
2461 * be replaced with -1. This signifies that we should from now
2462 * on accept any traffic (with any tag present, or untagged)
2463 */
2464 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2465 if (is_netdev) {
2466 if (f->vlan &&
2467 ether_addr_equal(netdev->dev_addr, f->macaddr))
2468 filter_count++;
2469 }
2470
2471 if (f->vlan)
2472 filter_count++;
2473 }
2474
2475 if (!filter_count && is_netdev) {
2476 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2477 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2478 is_vf, is_netdev);
2479 if (!f) {
2480 dev_info(&vsi->back->pdev->dev,
2481 "Could not add filter %d for %pM\n",
2482 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002483 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002484 return -ENOMEM;
2485 }
2486 }
2487
2488 if (!filter_count) {
2489 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2490 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2491 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002492 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002493 if (!add_f) {
2494 dev_info(&vsi->back->pdev->dev,
2495 "Could not add filter %d for %pM\n",
2496 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002497 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002498 return -ENOMEM;
2499 }
2500 }
2501 }
2502
Kiran Patil21659032015-09-30 14:09:03 -04002503 spin_unlock_bh(&vsi->mac_filter_list_lock);
2504
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002505 /* schedule our worker thread which will take care of
2506 * applying the new filter changes
2507 */
2508 i40e_service_event_schedule(vsi->back);
2509 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002510}
2511
2512/**
2513 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2514 * @netdev: network interface to be adjusted
2515 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002516 *
2517 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002518 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002519#ifdef I40E_FCOE
2520int i40e_vlan_rx_add_vid(struct net_device *netdev,
2521 __always_unused __be16 proto, u16 vid)
2522#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002523static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2524 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002525#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002526{
2527 struct i40e_netdev_priv *np = netdev_priv(netdev);
2528 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002529 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002530
2531 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002532 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002533
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002534 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2535
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002536 /* If the network stack called us with vid = 0 then
2537 * it is asking to receive priority tagged packets with
2538 * vlan id 0. Our HW receives them by default when configured
2539 * to receive untagged packets so there is no need to add an
2540 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002541 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002542 if (vid)
2543 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002544
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002545 if (!ret && (vid < VLAN_N_VID))
2546 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002548 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002549}
2550
2551/**
2552 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2553 * @netdev: network interface to be adjusted
2554 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002555 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002556 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002558#ifdef I40E_FCOE
2559int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2560 __always_unused __be16 proto, u16 vid)
2561#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002562static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2563 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002564#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002565{
2566 struct i40e_netdev_priv *np = netdev_priv(netdev);
2567 struct i40e_vsi *vsi = np->vsi;
2568
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002569 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2570
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002571 /* return code is ignored as there is nothing a user
2572 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002573 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002574 */
2575 i40e_vsi_kill_vlan(vsi, vid);
2576
2577 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002578
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002579 return 0;
2580}
2581
2582/**
2583 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2584 * @vsi: the vsi being brought back up
2585 **/
2586static void i40e_restore_vlan(struct i40e_vsi *vsi)
2587{
2588 u16 vid;
2589
2590 if (!vsi->netdev)
2591 return;
2592
2593 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2594
2595 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2596 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2597 vid);
2598}
2599
2600/**
2601 * i40e_vsi_add_pvid - Add pvid for the VSI
2602 * @vsi: the vsi being adjusted
2603 * @vid: the vlan id to set as a PVID
2604 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002605int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002606{
2607 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002608 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609
2610 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2611 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002612 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2613 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002614 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002615
2616 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002617 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002618 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2619 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002620 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002621 "add pvid failed, err %s aq_err %s\n",
2622 i40e_stat_str(&vsi->back->hw, ret),
2623 i40e_aq_str(&vsi->back->hw,
2624 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002625 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002626 }
2627
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002628 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002629}
2630
2631/**
2632 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2633 * @vsi: the vsi being adjusted
2634 *
2635 * Just use the vlan_rx_register() service to put it back to normal
2636 **/
2637void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2638{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002639 i40e_vlan_stripping_disable(vsi);
2640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002641 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002642}
2643
2644/**
2645 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2646 * @vsi: ptr to the VSI
2647 *
2648 * If this function returns with an error, then it's possible one or
2649 * more of the rings is populated (while the rest are not). It is the
2650 * callers duty to clean those orphaned rings.
2651 *
2652 * Return 0 on success, negative on failure
2653 **/
2654static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2655{
2656 int i, err = 0;
2657
2658 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002659 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660
2661 return err;
2662}
2663
2664/**
2665 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2666 * @vsi: ptr to the VSI
2667 *
2668 * Free VSI's transmit software resources
2669 **/
2670static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2671{
2672 int i;
2673
Greg Rose8e9dca52013-12-18 13:45:53 +00002674 if (!vsi->tx_rings)
2675 return;
2676
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002677 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002678 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002679 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002680}
2681
2682/**
2683 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2684 * @vsi: ptr to the VSI
2685 *
2686 * If this function returns with an error, then it's possible one or
2687 * more of the rings is populated (while the rest are not). It is the
2688 * callers duty to clean those orphaned rings.
2689 *
2690 * Return 0 on success, negative on failure
2691 **/
2692static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2693{
2694 int i, err = 0;
2695
2696 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002697 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002698#ifdef I40E_FCOE
2699 i40e_fcoe_setup_ddp_resources(vsi);
2700#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002701 return err;
2702}
2703
2704/**
2705 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2706 * @vsi: ptr to the VSI
2707 *
2708 * Free all receive software resources
2709 **/
2710static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2711{
2712 int i;
2713
Greg Rose8e9dca52013-12-18 13:45:53 +00002714 if (!vsi->rx_rings)
2715 return;
2716
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002717 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002718 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002719 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002720#ifdef I40E_FCOE
2721 i40e_fcoe_free_ddp_resources(vsi);
2722#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002723}
2724
2725/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002726 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2727 * @ring: The Tx ring to configure
2728 *
2729 * This enables/disables XPS for a given Tx descriptor ring
2730 * based on the TCs enabled for the VSI that ring belongs to.
2731 **/
2732static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2733{
2734 struct i40e_vsi *vsi = ring->vsi;
2735 cpumask_var_t mask;
2736
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002737 if (!ring->q_vector || !ring->netdev)
2738 return;
2739
2740 /* Single TC mode enable XPS */
2741 if (vsi->tc_config.numtc <= 1) {
2742 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002743 netif_set_xps_queue(ring->netdev,
2744 &ring->q_vector->affinity_mask,
2745 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002746 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2747 /* Disable XPS to allow selection based on TC */
2748 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2749 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2750 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002751 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002752
2753 /* schedule our worker thread which will take care of
2754 * applying the new filter changes
2755 */
2756 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002757}
2758
2759/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002760 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2761 * @ring: The Tx ring to configure
2762 *
2763 * Configure the Tx descriptor ring in the HMC context.
2764 **/
2765static int i40e_configure_tx_ring(struct i40e_ring *ring)
2766{
2767 struct i40e_vsi *vsi = ring->vsi;
2768 u16 pf_q = vsi->base_queue + ring->queue_index;
2769 struct i40e_hw *hw = &vsi->back->hw;
2770 struct i40e_hmc_obj_txq tx_ctx;
2771 i40e_status err = 0;
2772 u32 qtx_ctl = 0;
2773
2774 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002775 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002776 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2777 ring->atr_count = 0;
2778 } else {
2779 ring->atr_sample_rate = 0;
2780 }
2781
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002782 /* configure XPS */
2783 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002784
2785 /* clear the context structure first */
2786 memset(&tx_ctx, 0, sizeof(tx_ctx));
2787
2788 tx_ctx.new_context = 1;
2789 tx_ctx.base = (ring->dma / 128);
2790 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002791 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2792 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002793#ifdef I40E_FCOE
2794 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2795#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002796 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002797 /* FDIR VSI tx ring can still use RS bit and writebacks */
2798 if (vsi->type != I40E_VSI_FDIR)
2799 tx_ctx.head_wb_ena = 1;
2800 tx_ctx.head_wb_addr = ring->dma +
2801 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002802
2803 /* As part of VSI creation/update, FW allocates certain
2804 * Tx arbitration queue sets for each TC enabled for
2805 * the VSI. The FW returns the handles to these queue
2806 * sets as part of the response buffer to Add VSI,
2807 * Update VSI, etc. AQ commands. It is expected that
2808 * these queue set handles be associated with the Tx
2809 * queues by the driver as part of the TX queue context
2810 * initialization. This has to be done regardless of
2811 * DCB as by default everything is mapped to TC0.
2812 */
2813 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2814 tx_ctx.rdylist_act = 0;
2815
2816 /* clear the context in the HMC */
2817 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2818 if (err) {
2819 dev_info(&vsi->back->pdev->dev,
2820 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2821 ring->queue_index, pf_q, err);
2822 return -ENOMEM;
2823 }
2824
2825 /* set the context in the HMC */
2826 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2827 if (err) {
2828 dev_info(&vsi->back->pdev->dev,
2829 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2830 ring->queue_index, pf_q, err);
2831 return -ENOMEM;
2832 }
2833
2834 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002835 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002836 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002837 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2838 I40E_QTX_CTL_VFVM_INDX_MASK;
2839 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002840 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002841 }
2842
Shannon Nelson13fd9772013-09-28 07:14:19 +00002843 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2844 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002845 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2846 i40e_flush(hw);
2847
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002848 /* cache tail off for easier writes later */
2849 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2850
2851 return 0;
2852}
2853
2854/**
2855 * i40e_configure_rx_ring - Configure a receive ring context
2856 * @ring: The Rx ring to configure
2857 *
2858 * Configure the Rx descriptor ring in the HMC context.
2859 **/
2860static int i40e_configure_rx_ring(struct i40e_ring *ring)
2861{
2862 struct i40e_vsi *vsi = ring->vsi;
2863 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2864 u16 pf_q = vsi->base_queue + ring->queue_index;
2865 struct i40e_hw *hw = &vsi->back->hw;
2866 struct i40e_hmc_obj_rxq rx_ctx;
2867 i40e_status err = 0;
2868
2869 ring->state = 0;
2870
2871 /* clear the context structure first */
2872 memset(&rx_ctx, 0, sizeof(rx_ctx));
2873
2874 ring->rx_buf_len = vsi->rx_buf_len;
2875 ring->rx_hdr_len = vsi->rx_hdr_len;
2876
2877 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2878 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2879
2880 rx_ctx.base = (ring->dma / 128);
2881 rx_ctx.qlen = ring->count;
2882
2883 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2884 set_ring_16byte_desc_enabled(ring);
2885 rx_ctx.dsize = 0;
2886 } else {
2887 rx_ctx.dsize = 1;
2888 }
2889
2890 rx_ctx.dtype = vsi->dtype;
2891 if (vsi->dtype) {
2892 set_ring_ps_enabled(ring);
2893 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2894 I40E_RX_SPLIT_IP |
2895 I40E_RX_SPLIT_TCP_UDP |
2896 I40E_RX_SPLIT_SCTP;
2897 } else {
2898 rx_ctx.hsplit_0 = 0;
2899 }
2900
2901 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2902 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002903 if (hw->revision_id == 0)
2904 rx_ctx.lrxqthresh = 0;
2905 else
2906 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002907 rx_ctx.crcstrip = 1;
2908 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002909 /* this controls whether VLAN is stripped from inner headers */
2910 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002911#ifdef I40E_FCOE
2912 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2913#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002914 /* set the prefena field to 1 because the manual says to */
2915 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002916
2917 /* clear the context in the HMC */
2918 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2919 if (err) {
2920 dev_info(&vsi->back->pdev->dev,
2921 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2922 ring->queue_index, pf_q, err);
2923 return -ENOMEM;
2924 }
2925
2926 /* set the context in the HMC */
2927 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2928 if (err) {
2929 dev_info(&vsi->back->pdev->dev,
2930 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2931 ring->queue_index, pf_q, err);
2932 return -ENOMEM;
2933 }
2934
2935 /* cache tail for quicker writes, and clear the reg before use */
2936 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2937 writel(0, ring->tail);
2938
Mitch Williamsa132af22015-01-24 09:58:35 +00002939 if (ring_is_ps_enabled(ring)) {
2940 i40e_alloc_rx_headers(ring);
2941 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2942 } else {
2943 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2944 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945
2946 return 0;
2947}
2948
2949/**
2950 * i40e_vsi_configure_tx - Configure the VSI for Tx
2951 * @vsi: VSI structure describing this set of rings and resources
2952 *
2953 * Configure the Tx VSI for operation.
2954 **/
2955static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2956{
2957 int err = 0;
2958 u16 i;
2959
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002960 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2961 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002962
2963 return err;
2964}
2965
2966/**
2967 * i40e_vsi_configure_rx - Configure the VSI for Rx
2968 * @vsi: the VSI being configured
2969 *
2970 * Configure the Rx VSI for operation.
2971 **/
2972static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2973{
2974 int err = 0;
2975 u16 i;
2976
2977 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2978 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2979 + ETH_FCS_LEN + VLAN_HLEN;
2980 else
2981 vsi->max_frame = I40E_RXBUFFER_2048;
2982
2983 /* figure out correct receive buffer length */
2984 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2985 I40E_FLAG_RX_PS_ENABLED)) {
2986 case I40E_FLAG_RX_1BUF_ENABLED:
2987 vsi->rx_hdr_len = 0;
2988 vsi->rx_buf_len = vsi->max_frame;
2989 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2990 break;
2991 case I40E_FLAG_RX_PS_ENABLED:
2992 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2993 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2994 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2995 break;
2996 default:
2997 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2998 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2999 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
3000 break;
3001 }
3002
Vasu Dev38e00432014-08-01 13:27:03 -07003003#ifdef I40E_FCOE
3004 /* setup rx buffer for FCoE */
3005 if ((vsi->type == I40E_VSI_FCOE) &&
3006 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
3007 vsi->rx_hdr_len = 0;
3008 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3009 vsi->max_frame = I40E_RXBUFFER_3072;
3010 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
3011 }
3012
3013#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014 /* round up for the chip's needs */
3015 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003016 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003018 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003019
3020 /* set up individual rings */
3021 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003022 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003023
3024 return err;
3025}
3026
3027/**
3028 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3029 * @vsi: ptr to the VSI
3030 **/
3031static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3032{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003033 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003034 u16 qoffset, qcount;
3035 int i, n;
3036
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003037 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3038 /* Reset the TC information */
3039 for (i = 0; i < vsi->num_queue_pairs; i++) {
3040 rx_ring = vsi->rx_rings[i];
3041 tx_ring = vsi->tx_rings[i];
3042 rx_ring->dcb_tc = 0;
3043 tx_ring->dcb_tc = 0;
3044 }
3045 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003046
3047 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003048 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003049 continue;
3050
3051 qoffset = vsi->tc_config.tc_info[n].qoffset;
3052 qcount = vsi->tc_config.tc_info[n].qcount;
3053 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003054 rx_ring = vsi->rx_rings[i];
3055 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003056 rx_ring->dcb_tc = n;
3057 tx_ring->dcb_tc = n;
3058 }
3059 }
3060}
3061
3062/**
3063 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3064 * @vsi: ptr to the VSI
3065 **/
3066static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3067{
3068 if (vsi->netdev)
3069 i40e_set_rx_mode(vsi->netdev);
3070}
3071
3072/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003073 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3074 * @vsi: Pointer to the targeted VSI
3075 *
3076 * This function replays the hlist on the hw where all the SB Flow Director
3077 * filters were saved.
3078 **/
3079static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3080{
3081 struct i40e_fdir_filter *filter;
3082 struct i40e_pf *pf = vsi->back;
3083 struct hlist_node *node;
3084
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003085 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3086 return;
3087
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003088 hlist_for_each_entry_safe(filter, node,
3089 &pf->fdir_filter_list, fdir_node) {
3090 i40e_add_del_fdir(vsi, filter, true);
3091 }
3092}
3093
3094/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003095 * i40e_vsi_configure - Set up the VSI for action
3096 * @vsi: the VSI being configured
3097 **/
3098static int i40e_vsi_configure(struct i40e_vsi *vsi)
3099{
3100 int err;
3101
3102 i40e_set_vsi_rx_mode(vsi);
3103 i40e_restore_vlan(vsi);
3104 i40e_vsi_config_dcb_rings(vsi);
3105 err = i40e_vsi_configure_tx(vsi);
3106 if (!err)
3107 err = i40e_vsi_configure_rx(vsi);
3108
3109 return err;
3110}
3111
3112/**
3113 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3114 * @vsi: the VSI being configured
3115 **/
3116static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3117{
3118 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 struct i40e_hw *hw = &pf->hw;
3120 u16 vector;
3121 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122 u32 qp;
3123
3124 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3125 * and PFINT_LNKLSTn registers, e.g.:
3126 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3127 */
3128 qp = vsi->base_queue;
3129 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003130 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003131 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3132
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003133 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003134 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003135 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3136 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3137 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003138 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003139 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3140 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3141 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003142 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3143 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003144
3145 /* Linked list for the queuepairs assigned to this vector */
3146 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3147 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003148 u32 val;
3149
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003150 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3151 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3152 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3153 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3154 (I40E_QUEUE_TYPE_TX
3155 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3156
3157 wr32(hw, I40E_QINT_RQCTL(qp), val);
3158
3159 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3160 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3161 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3162 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3163 (I40E_QUEUE_TYPE_RX
3164 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3165
3166 /* Terminate the linked list */
3167 if (q == (q_vector->num_ringpairs - 1))
3168 val |= (I40E_QUEUE_END_OF_LIST
3169 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3170
3171 wr32(hw, I40E_QINT_TQCTL(qp), val);
3172 qp++;
3173 }
3174 }
3175
3176 i40e_flush(hw);
3177}
3178
3179/**
3180 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3181 * @hw: ptr to the hardware info
3182 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003183static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184{
Jacob Kellerab437b52014-12-14 01:55:08 +00003185 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003186 u32 val;
3187
3188 /* clear things first */
3189 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3190 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3191
3192 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3193 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3194 I40E_PFINT_ICR0_ENA_GRST_MASK |
3195 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3196 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003197 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3198 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3199 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3200
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003201 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3202 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3203
Jacob Kellerab437b52014-12-14 01:55:08 +00003204 if (pf->flags & I40E_FLAG_PTP)
3205 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3206
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003207 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3208
3209 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003210 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3211 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003212
3213 /* OTHER_ITR_IDX = 0 */
3214 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3215}
3216
3217/**
3218 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3219 * @vsi: the VSI being configured
3220 **/
3221static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3222{
Alexander Duyck493fb302013-09-28 07:01:44 +00003223 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003224 struct i40e_pf *pf = vsi->back;
3225 struct i40e_hw *hw = &pf->hw;
3226 u32 val;
3227
3228 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003229 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003230 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3232 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003233 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003234 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3235 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3236
Jacob Kellerab437b52014-12-14 01:55:08 +00003237 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238
3239 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3240 wr32(hw, I40E_PFINT_LNKLST0, 0);
3241
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003242 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3244 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3245 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3246
3247 wr32(hw, I40E_QINT_RQCTL(0), val);
3248
3249 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3250 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3251 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3252
3253 wr32(hw, I40E_QINT_TQCTL(0), val);
3254 i40e_flush(hw);
3255}
3256
3257/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003258 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3259 * @pf: board private structure
3260 **/
3261void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3262{
3263 struct i40e_hw *hw = &pf->hw;
3264
3265 wr32(hw, I40E_PFINT_DYN_CTL0,
3266 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3267 i40e_flush(hw);
3268}
3269
3270/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3272 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003273 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003274 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003275void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003276{
3277 struct i40e_hw *hw = &pf->hw;
3278 u32 val;
3279
3280 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003281 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3283
3284 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3285 i40e_flush(hw);
3286}
3287
3288/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003289 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3290 * @irq: interrupt number
3291 * @data: pointer to a q_vector
3292 **/
3293static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3294{
3295 struct i40e_q_vector *q_vector = data;
3296
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003297 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003298 return IRQ_HANDLED;
3299
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003300 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 return IRQ_HANDLED;
3303}
3304
3305/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003306 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3307 * @vsi: the VSI being configured
3308 * @basename: name for the vector
3309 *
3310 * Allocates MSI-X vectors and requests interrupts from the kernel.
3311 **/
3312static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3313{
3314 int q_vectors = vsi->num_q_vectors;
3315 struct i40e_pf *pf = vsi->back;
3316 int base = vsi->base_vector;
3317 int rx_int_idx = 0;
3318 int tx_int_idx = 0;
3319 int vector, err;
3320
3321 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003322 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003324 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3326 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3327 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003328 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3330 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003331 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3333 "%s-%s-%d", basename, "tx", tx_int_idx++);
3334 } else {
3335 /* skip this unused q_vector */
3336 continue;
3337 }
3338 err = request_irq(pf->msix_entries[base + vector].vector,
3339 vsi->irq_handler,
3340 0,
3341 q_vector->name,
3342 q_vector);
3343 if (err) {
3344 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003345 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003346 goto free_queue_irqs;
3347 }
3348 /* assign the mask for this irq */
3349 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3350 &q_vector->affinity_mask);
3351 }
3352
Shannon Nelson63741842014-04-23 04:50:16 +00003353 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003354 return 0;
3355
3356free_queue_irqs:
3357 while (vector) {
3358 vector--;
3359 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3360 NULL);
3361 free_irq(pf->msix_entries[base + vector].vector,
3362 &(vsi->q_vectors[vector]));
3363 }
3364 return err;
3365}
3366
3367/**
3368 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3369 * @vsi: the VSI being un-configured
3370 **/
3371static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3372{
3373 struct i40e_pf *pf = vsi->back;
3374 struct i40e_hw *hw = &pf->hw;
3375 int base = vsi->base_vector;
3376 int i;
3377
3378 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003379 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3380 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003381 }
3382
3383 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3384 for (i = vsi->base_vector;
3385 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3386 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3387
3388 i40e_flush(hw);
3389 for (i = 0; i < vsi->num_q_vectors; i++)
3390 synchronize_irq(pf->msix_entries[i + base].vector);
3391 } else {
3392 /* Legacy and MSI mode - this stops all interrupt handling */
3393 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3394 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3395 i40e_flush(hw);
3396 synchronize_irq(pf->pdev->irq);
3397 }
3398}
3399
3400/**
3401 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3402 * @vsi: the VSI being configured
3403 **/
3404static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3405{
3406 struct i40e_pf *pf = vsi->back;
3407 int i;
3408
3409 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003410 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003411 i40e_irq_dynamic_enable(vsi, i);
3412 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003413 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003414 }
3415
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003416 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003417 return 0;
3418}
3419
3420/**
3421 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3422 * @pf: board private structure
3423 **/
3424static void i40e_stop_misc_vector(struct i40e_pf *pf)
3425{
3426 /* Disable ICR 0 */
3427 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3428 i40e_flush(&pf->hw);
3429}
3430
3431/**
3432 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3433 * @irq: interrupt number
3434 * @data: pointer to a q_vector
3435 *
3436 * This is the handler used for all MSI/Legacy interrupts, and deals
3437 * with both queue and non-queue interrupts. This is also used in
3438 * MSIX mode to handle the non-queue interrupts.
3439 **/
3440static irqreturn_t i40e_intr(int irq, void *data)
3441{
3442 struct i40e_pf *pf = (struct i40e_pf *)data;
3443 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003444 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003445 u32 icr0, icr0_remaining;
3446 u32 val, ena_mask;
3447
3448 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003449 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450
Shannon Nelson116a57d2013-09-28 07:13:59 +00003451 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3452 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003453 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003454
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003455 /* if interrupt but no bits showing, must be SWINT */
3456 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3457 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3458 pf->sw_int_count++;
3459
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003460 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3461 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3462 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3463 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3464 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3465 }
3466
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3468 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003469 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3470 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003472 /* We do not have a way to disarm Queue causes while leaving
3473 * interrupt enabled for all other causes, ideally
3474 * interrupt should be disabled while we are in NAPI but
3475 * this is not a performance path and napi_schedule()
3476 * can deal with rescheduling.
3477 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003478 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003479 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003480 }
3481
3482 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3483 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3484 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003485 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 }
3487
3488 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3489 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3490 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3491 }
3492
3493 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3494 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3495 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3496 }
3497
3498 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3499 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3500 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3501 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3502 val = rd32(hw, I40E_GLGEN_RSTAT);
3503 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3504 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003505 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003506 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003507 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003508 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003509 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003510 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003511 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003512 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003513 }
3514
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003515 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3516 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3517 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003518 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3519 rd32(hw, I40E_PFHMC_ERRORINFO),
3520 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003521 }
3522
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003523 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3524 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3525
3526 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003527 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003528 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003529 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003530 }
3531
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 /* If a critical error is pending we have no choice but to reset the
3533 * device.
3534 * Report and mask out any remaining unexpected interrupts.
3535 */
3536 icr0_remaining = icr0 & ena_mask;
3537 if (icr0_remaining) {
3538 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3539 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003540 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003541 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003542 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003543 dev_info(&pf->pdev->dev, "device will be reset\n");
3544 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3545 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546 }
3547 ena_mask &= ~icr0_remaining;
3548 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003549 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003550
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003551enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003552 /* re-enable interrupt causes */
3553 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003554 if (!test_bit(__I40E_DOWN, &pf->state)) {
3555 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003556 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003557 }
3558
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003559 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003560}
3561
3562/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003563 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3564 * @tx_ring: tx ring to clean
3565 * @budget: how many cleans we're allowed
3566 *
3567 * Returns true if there's any budget left (e.g. the clean is finished)
3568 **/
3569static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3570{
3571 struct i40e_vsi *vsi = tx_ring->vsi;
3572 u16 i = tx_ring->next_to_clean;
3573 struct i40e_tx_buffer *tx_buf;
3574 struct i40e_tx_desc *tx_desc;
3575
3576 tx_buf = &tx_ring->tx_bi[i];
3577 tx_desc = I40E_TX_DESC(tx_ring, i);
3578 i -= tx_ring->count;
3579
3580 do {
3581 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3582
3583 /* if next_to_watch is not set then there is no work pending */
3584 if (!eop_desc)
3585 break;
3586
3587 /* prevent any other reads prior to eop_desc */
3588 read_barrier_depends();
3589
3590 /* if the descriptor isn't done, no work yet to do */
3591 if (!(eop_desc->cmd_type_offset_bsz &
3592 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3593 break;
3594
3595 /* clear next_to_watch to prevent false hangs */
3596 tx_buf->next_to_watch = NULL;
3597
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003598 tx_desc->buffer_addr = 0;
3599 tx_desc->cmd_type_offset_bsz = 0;
3600 /* move past filter desc */
3601 tx_buf++;
3602 tx_desc++;
3603 i++;
3604 if (unlikely(!i)) {
3605 i -= tx_ring->count;
3606 tx_buf = tx_ring->tx_bi;
3607 tx_desc = I40E_TX_DESC(tx_ring, 0);
3608 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003609 /* unmap skb header data */
3610 dma_unmap_single(tx_ring->dev,
3611 dma_unmap_addr(tx_buf, dma),
3612 dma_unmap_len(tx_buf, len),
3613 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003614 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3615 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003616
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003617 tx_buf->raw_buf = NULL;
3618 tx_buf->tx_flags = 0;
3619 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003620 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003621 tx_desc->buffer_addr = 0;
3622 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003623
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003624 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003625 tx_buf++;
3626 tx_desc++;
3627 i++;
3628 if (unlikely(!i)) {
3629 i -= tx_ring->count;
3630 tx_buf = tx_ring->tx_bi;
3631 tx_desc = I40E_TX_DESC(tx_ring, 0);
3632 }
3633
3634 /* update budget accounting */
3635 budget--;
3636 } while (likely(budget));
3637
3638 i += tx_ring->count;
3639 tx_ring->next_to_clean = i;
3640
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003641 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003642 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003643
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003644 return budget > 0;
3645}
3646
3647/**
3648 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3649 * @irq: interrupt number
3650 * @data: pointer to a q_vector
3651 **/
3652static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3653{
3654 struct i40e_q_vector *q_vector = data;
3655 struct i40e_vsi *vsi;
3656
3657 if (!q_vector->tx.ring)
3658 return IRQ_HANDLED;
3659
3660 vsi = q_vector->tx.ring->vsi;
3661 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3662
3663 return IRQ_HANDLED;
3664}
3665
3666/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003667 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003668 * @vsi: the VSI being configured
3669 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003670 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003671 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003672static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673{
Alexander Duyck493fb302013-09-28 07:01:44 +00003674 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003675 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3676 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677
3678 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003679 tx_ring->next = q_vector->tx.ring;
3680 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003681 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003682
3683 rx_ring->q_vector = q_vector;
3684 rx_ring->next = q_vector->rx.ring;
3685 q_vector->rx.ring = rx_ring;
3686 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687}
3688
3689/**
3690 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3691 * @vsi: the VSI being configured
3692 *
3693 * This function maps descriptor rings to the queue-specific vectors
3694 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3695 * one vector per queue pair, but on a constrained vector budget, we
3696 * group the queue pairs as "efficiently" as possible.
3697 **/
3698static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3699{
3700 int qp_remaining = vsi->num_queue_pairs;
3701 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003702 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003703 int v_start = 0;
3704 int qp_idx = 0;
3705
3706 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3707 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003708 * It is also important to go through all the vectors available to be
3709 * sure that if we don't use all the vectors, that the remaining vectors
3710 * are cleared. This is especially important when decreasing the
3711 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003713 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003714 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3715
3716 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3717
3718 q_vector->num_ringpairs = num_ringpairs;
3719
3720 q_vector->rx.count = 0;
3721 q_vector->tx.count = 0;
3722 q_vector->rx.ring = NULL;
3723 q_vector->tx.ring = NULL;
3724
3725 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003726 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003727 qp_idx++;
3728 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003729 }
3730 }
3731}
3732
3733/**
3734 * i40e_vsi_request_irq - Request IRQ from the OS
3735 * @vsi: the VSI being configured
3736 * @basename: name for the vector
3737 **/
3738static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3739{
3740 struct i40e_pf *pf = vsi->back;
3741 int err;
3742
3743 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3744 err = i40e_vsi_request_irq_msix(vsi, basename);
3745 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3746 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003747 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003748 else
3749 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003750 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003751
3752 if (err)
3753 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3754
3755 return err;
3756}
3757
3758#ifdef CONFIG_NET_POLL_CONTROLLER
3759/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003760 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003761 * @netdev: network interface device structure
3762 *
3763 * This is used by netconsole to send skbs without having to re-enable
3764 * interrupts. It's not called while the normal interrupt routine is executing.
3765 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003766#ifdef I40E_FCOE
3767void i40e_netpoll(struct net_device *netdev)
3768#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003769static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003770#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003771{
3772 struct i40e_netdev_priv *np = netdev_priv(netdev);
3773 struct i40e_vsi *vsi = np->vsi;
3774 struct i40e_pf *pf = vsi->back;
3775 int i;
3776
3777 /* if interface is down do nothing */
3778 if (test_bit(__I40E_DOWN, &vsi->state))
3779 return;
3780
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003781 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3782 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003783 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003784 } else {
3785 i40e_intr(pf->pdev->irq, netdev);
3786 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003787}
3788#endif
3789
3790/**
Neerav Parikh23527302014-06-03 23:50:15 +00003791 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3792 * @pf: the PF being configured
3793 * @pf_q: the PF queue
3794 * @enable: enable or disable state of the queue
3795 *
3796 * This routine will wait for the given Tx queue of the PF to reach the
3797 * enabled or disabled state.
3798 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3799 * multiple retries; else will return 0 in case of success.
3800 **/
3801static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3802{
3803 int i;
3804 u32 tx_reg;
3805
3806 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3807 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3808 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3809 break;
3810
Neerav Parikhf98a2002014-09-13 07:40:44 +00003811 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003812 }
3813 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3814 return -ETIMEDOUT;
3815
3816 return 0;
3817}
3818
3819/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003820 * i40e_vsi_control_tx - Start or stop a VSI's rings
3821 * @vsi: the VSI being configured
3822 * @enable: start or stop the rings
3823 **/
3824static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3825{
3826 struct i40e_pf *pf = vsi->back;
3827 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003828 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003829 u32 tx_reg;
3830
3831 pf_q = vsi->base_queue;
3832 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003833
3834 /* warn the TX unit of coming changes */
3835 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3836 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003837 usleep_range(10, 20);
Matt Jared351499a2014-04-23 04:50:03 +00003838
Mitch Williams6c5ef622014-02-20 19:29:16 -08003839 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003840 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003841 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3842 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3843 break;
3844 usleep_range(1000, 2000);
3845 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003846 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003847 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003848 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003849
3850 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003851 if (enable) {
3852 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003853 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003854 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003855 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003856 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003857
3858 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003859 /* No waiting for the Tx queue to disable */
3860 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3861 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003862
3863 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003864 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3865 if (ret) {
3866 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003867 "VSI seid %d Tx ring %d %sable timeout\n",
3868 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003869 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003870 }
3871 }
3872
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003873 if (hw->revision_id == 0)
3874 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003875 return ret;
3876}
3877
3878/**
3879 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3880 * @pf: the PF being configured
3881 * @pf_q: the PF queue
3882 * @enable: enable or disable state of the queue
3883 *
3884 * This routine will wait for the given Rx queue of the PF to reach the
3885 * enabled or disabled state.
3886 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3887 * multiple retries; else will return 0 in case of success.
3888 **/
3889static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3890{
3891 int i;
3892 u32 rx_reg;
3893
3894 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3895 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3896 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3897 break;
3898
Neerav Parikhf98a2002014-09-13 07:40:44 +00003899 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003900 }
3901 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3902 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003904 return 0;
3905}
3906
3907/**
3908 * i40e_vsi_control_rx - Start or stop a VSI's rings
3909 * @vsi: the VSI being configured
3910 * @enable: start or stop the rings
3911 **/
3912static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3913{
3914 struct i40e_pf *pf = vsi->back;
3915 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003916 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003917 u32 rx_reg;
3918
3919 pf_q = vsi->base_queue;
3920 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003921 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003922 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003923 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3924 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3925 break;
3926 usleep_range(1000, 2000);
3927 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003928
Catherine Sullivan7c122002014-03-14 07:32:29 +00003929 /* Skip if the queue is already in the requested state */
3930 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3931 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003932
3933 /* turn on/off the queue */
3934 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003935 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003936 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003937 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003938 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003939 /* No waiting for the Tx queue to disable */
3940 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3941 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003942
3943 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003944 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3945 if (ret) {
3946 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003947 "VSI seid %d Rx ring %d %sable timeout\n",
3948 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003949 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003950 }
3951 }
3952
Neerav Parikh23527302014-06-03 23:50:15 +00003953 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003954}
3955
3956/**
3957 * i40e_vsi_control_rings - Start or stop a VSI's rings
3958 * @vsi: the VSI being configured
3959 * @enable: start or stop the rings
3960 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003961int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003962{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003963 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003964
3965 /* do rx first for enable and last for disable */
3966 if (request) {
3967 ret = i40e_vsi_control_rx(vsi, request);
3968 if (ret)
3969 return ret;
3970 ret = i40e_vsi_control_tx(vsi, request);
3971 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003972 /* Ignore return value, we need to shutdown whatever we can */
3973 i40e_vsi_control_tx(vsi, request);
3974 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003975 }
3976
3977 return ret;
3978}
3979
3980/**
3981 * i40e_vsi_free_irq - Free the irq association with the OS
3982 * @vsi: the VSI being configured
3983 **/
3984static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3985{
3986 struct i40e_pf *pf = vsi->back;
3987 struct i40e_hw *hw = &pf->hw;
3988 int base = vsi->base_vector;
3989 u32 val, qp;
3990 int i;
3991
3992 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3993 if (!vsi->q_vectors)
3994 return;
3995
Shannon Nelson63741842014-04-23 04:50:16 +00003996 if (!vsi->irqs_ready)
3997 return;
3998
3999 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004000 for (i = 0; i < vsi->num_q_vectors; i++) {
4001 u16 vector = i + base;
4002
4003 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004004 if (!vsi->q_vectors[i] ||
4005 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004006 continue;
4007
4008 /* clear the affinity_mask in the IRQ descriptor */
4009 irq_set_affinity_hint(pf->msix_entries[vector].vector,
4010 NULL);
4011 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00004012 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004013
4014 /* Tear down the interrupt queue link list
4015 *
4016 * We know that they come in pairs and always
4017 * the Rx first, then the Tx. To clear the
4018 * link list, stick the EOL value into the
4019 * next_q field of the registers.
4020 */
4021 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4022 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4023 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4024 val |= I40E_QUEUE_END_OF_LIST
4025 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4026 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4027
4028 while (qp != I40E_QUEUE_END_OF_LIST) {
4029 u32 next;
4030
4031 val = rd32(hw, I40E_QINT_RQCTL(qp));
4032
4033 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4034 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4035 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4036 I40E_QINT_RQCTL_INTEVENT_MASK);
4037
4038 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4039 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4040
4041 wr32(hw, I40E_QINT_RQCTL(qp), val);
4042
4043 val = rd32(hw, I40E_QINT_TQCTL(qp));
4044
4045 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4046 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4047
4048 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4049 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4050 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4051 I40E_QINT_TQCTL_INTEVENT_MASK);
4052
4053 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4054 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4055
4056 wr32(hw, I40E_QINT_TQCTL(qp), val);
4057 qp = next;
4058 }
4059 }
4060 } else {
4061 free_irq(pf->pdev->irq, pf);
4062
4063 val = rd32(hw, I40E_PFINT_LNKLST0);
4064 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4065 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4066 val |= I40E_QUEUE_END_OF_LIST
4067 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4068 wr32(hw, I40E_PFINT_LNKLST0, val);
4069
4070 val = rd32(hw, I40E_QINT_RQCTL(qp));
4071 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4072 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4073 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4074 I40E_QINT_RQCTL_INTEVENT_MASK);
4075
4076 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4077 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4078
4079 wr32(hw, I40E_QINT_RQCTL(qp), val);
4080
4081 val = rd32(hw, I40E_QINT_TQCTL(qp));
4082
4083 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4084 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4085 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4086 I40E_QINT_TQCTL_INTEVENT_MASK);
4087
4088 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4089 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4090
4091 wr32(hw, I40E_QINT_TQCTL(qp), val);
4092 }
4093}
4094
4095/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004096 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4097 * @vsi: the VSI being configured
4098 * @v_idx: Index of vector to be freed
4099 *
4100 * This function frees the memory allocated to the q_vector. In addition if
4101 * NAPI is enabled it will delete any references to the NAPI struct prior
4102 * to freeing the q_vector.
4103 **/
4104static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4105{
4106 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004107 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004108
4109 if (!q_vector)
4110 return;
4111
4112 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004113 i40e_for_each_ring(ring, q_vector->tx)
4114 ring->q_vector = NULL;
4115
4116 i40e_for_each_ring(ring, q_vector->rx)
4117 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004118
4119 /* only VSI w/ an associated netdev is set up w/ NAPI */
4120 if (vsi->netdev)
4121 netif_napi_del(&q_vector->napi);
4122
4123 vsi->q_vectors[v_idx] = NULL;
4124
4125 kfree_rcu(q_vector, rcu);
4126}
4127
4128/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004129 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4130 * @vsi: the VSI being un-configured
4131 *
4132 * This frees the memory allocated to the q_vectors and
4133 * deletes references to the NAPI struct.
4134 **/
4135static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4136{
4137 int v_idx;
4138
Alexander Duyck493fb302013-09-28 07:01:44 +00004139 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4140 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004141}
4142
4143/**
4144 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4145 * @pf: board private structure
4146 **/
4147static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4148{
4149 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4150 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4151 pci_disable_msix(pf->pdev);
4152 kfree(pf->msix_entries);
4153 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004154 kfree(pf->irq_pile);
4155 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004156 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4157 pci_disable_msi(pf->pdev);
4158 }
4159 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4160}
4161
4162/**
4163 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4164 * @pf: board private structure
4165 *
4166 * We go through and clear interrupt specific resources and reset the structure
4167 * to pre-load conditions
4168 **/
4169static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4170{
4171 int i;
4172
Shannon Nelsone1477582015-02-21 06:44:33 +00004173 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004174 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004175 synchronize_irq(pf->msix_entries[0].vector);
4176 free_irq(pf->msix_entries[0].vector, pf);
4177 }
4178
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004179 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4180 I40E_IWARP_IRQ_PILE_ID);
4181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004182 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004183 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004184 if (pf->vsi[i])
4185 i40e_vsi_free_q_vectors(pf->vsi[i]);
4186 i40e_reset_interrupt_capability(pf);
4187}
4188
4189/**
4190 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4191 * @vsi: the VSI being configured
4192 **/
4193static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4194{
4195 int q_idx;
4196
4197 if (!vsi->netdev)
4198 return;
4199
4200 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004201 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004202}
4203
4204/**
4205 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4206 * @vsi: the VSI being configured
4207 **/
4208static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4209{
4210 int q_idx;
4211
4212 if (!vsi->netdev)
4213 return;
4214
4215 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004216 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004217}
4218
4219/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004220 * i40e_vsi_close - Shut down a VSI
4221 * @vsi: the vsi to be quelled
4222 **/
4223static void i40e_vsi_close(struct i40e_vsi *vsi)
4224{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004225 bool reset = false;
4226
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004227 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4228 i40e_down(vsi);
4229 i40e_vsi_free_irq(vsi);
4230 i40e_vsi_free_tx_resources(vsi);
4231 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004232 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004233 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4234 reset = true;
4235 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004236}
4237
4238/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004239 * i40e_quiesce_vsi - Pause a given VSI
4240 * @vsi: the VSI being paused
4241 **/
4242static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4243{
4244 if (test_bit(__I40E_DOWN, &vsi->state))
4245 return;
4246
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004247 /* No need to disable FCoE VSI when Tx suspended */
4248 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4249 vsi->type == I40E_VSI_FCOE) {
4250 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004251 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004252 return;
4253 }
4254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004255 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004256 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004257 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004258 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004259 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004260}
4261
4262/**
4263 * i40e_unquiesce_vsi - Resume a given VSI
4264 * @vsi: the VSI being resumed
4265 **/
4266static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4267{
4268 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4269 return;
4270
4271 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4272 if (vsi->netdev && netif_running(vsi->netdev))
4273 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4274 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004275 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004276}
4277
4278/**
4279 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4280 * @pf: the PF
4281 **/
4282static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4283{
4284 int v;
4285
Mitch Williams505682c2014-05-20 08:01:37 +00004286 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004287 if (pf->vsi[v])
4288 i40e_quiesce_vsi(pf->vsi[v]);
4289 }
4290}
4291
4292/**
4293 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4294 * @pf: the PF
4295 **/
4296static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4297{
4298 int v;
4299
Mitch Williams505682c2014-05-20 08:01:37 +00004300 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004301 if (pf->vsi[v])
4302 i40e_unquiesce_vsi(pf->vsi[v]);
4303 }
4304}
4305
Neerav Parikh69129dc2014-11-12 00:18:46 +00004306#ifdef CONFIG_I40E_DCB
4307/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004308 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004309 * @vsi: the VSI being configured
4310 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004311 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004312 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004313static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004314{
4315 struct i40e_pf *pf = vsi->back;
4316 int i, pf_q, ret;
4317
4318 pf_q = vsi->base_queue;
4319 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4320 /* Check and wait for the disable status of the queue */
4321 ret = i40e_pf_txq_wait(pf, pf_q, false);
4322 if (ret) {
4323 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004324 "VSI seid %d Tx ring %d disable timeout\n",
4325 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004326 return ret;
4327 }
4328 }
4329
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004330 pf_q = vsi->base_queue;
4331 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4332 /* Check and wait for the disable status of the queue */
4333 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4334 if (ret) {
4335 dev_info(&pf->pdev->dev,
4336 "VSI seid %d Rx ring %d disable timeout\n",
4337 vsi->seid, pf_q);
4338 return ret;
4339 }
4340 }
4341
Neerav Parikh69129dc2014-11-12 00:18:46 +00004342 return 0;
4343}
4344
4345/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004346 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004347 * @pf: the PF
4348 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004349 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004350 * VSIs that are managed by this PF.
4351 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004352static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004353{
4354 int v, ret = 0;
4355
4356 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004357 /* No need to wait for FCoE VSI queues */
4358 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004359 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004360 if (ret)
4361 break;
4362 }
4363 }
4364
4365 return ret;
4366}
4367
4368#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004369
4370/**
4371 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4372 * @q_idx: TX queue number
4373 * @vsi: Pointer to VSI struct
4374 *
4375 * This function checks specified queue for given VSI. Detects hung condition.
4376 * Sets hung bit since it is two step process. Before next run of service task
4377 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4378 * hung condition remain unchanged and during subsequent run, this function
4379 * issues SW interrupt to recover from hung condition.
4380 **/
4381static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4382{
4383 struct i40e_ring *tx_ring = NULL;
4384 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004385 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004386 int i;
4387
4388 pf = vsi->back;
4389
4390 /* now that we have an index, find the tx_ring struct */
4391 for (i = 0; i < vsi->num_queue_pairs; i++) {
4392 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4393 if (q_idx == vsi->tx_rings[i]->queue_index) {
4394 tx_ring = vsi->tx_rings[i];
4395 break;
4396 }
4397 }
4398 }
4399
4400 if (!tx_ring)
4401 return;
4402
4403 /* Read interrupt register */
4404 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4405 val = rd32(&pf->hw,
4406 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4407 tx_ring->vsi->base_vector - 1));
4408 else
4409 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4410
4411 head = i40e_get_head(tx_ring);
4412
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004413 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004414
Kiran Patil9c6c1252015-11-06 15:26:02 -08004415 /* HW is done executing descriptors, updated HEAD write back,
4416 * but SW hasn't processed those descriptors. If interrupt is
4417 * not generated from this point ON, it could result into
4418 * dev_watchdog detecting timeout on those netdev_queue,
4419 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004420 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004421 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004422 /* NAPI Poll didn't run and clear since it was set */
4423 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4424 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004425 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",
4426 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004427 tx_ring->next_to_clean, head,
4428 tx_ring->next_to_use,
4429 readl(tx_ring->tail));
4430 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4431 vsi->seid, q_idx, val);
4432 i40e_force_wb(vsi, tx_ring->q_vector);
4433 } else {
4434 /* First Chance - detected possible hung */
4435 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4436 &tx_ring->q_vector->hung_detected);
4437 }
4438 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004439
4440 /* This is the case where we have interrupts missing,
4441 * so the tx_pending in HW will most likely be 0, but we
4442 * will have tx_pending in SW since the WB happened but the
4443 * interrupt got lost.
4444 */
4445 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4446 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4447 if (napi_reschedule(&tx_ring->q_vector->napi))
4448 tx_ring->tx_stats.tx_lost_interrupt++;
4449 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004450}
4451
4452/**
4453 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4454 * @pf: pointer to PF struct
4455 *
4456 * LAN VSI has netdev and netdev has TX queues. This function is to check
4457 * each of those TX queues if they are hung, trigger recovery by issuing
4458 * SW interrupt.
4459 **/
4460static void i40e_detect_recover_hung(struct i40e_pf *pf)
4461{
4462 struct net_device *netdev;
4463 struct i40e_vsi *vsi;
4464 int i;
4465
4466 /* Only for LAN VSI */
4467 vsi = pf->vsi[pf->lan_vsi];
4468
4469 if (!vsi)
4470 return;
4471
4472 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4473 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4474 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4475 return;
4476
4477 /* Make sure type is MAIN VSI */
4478 if (vsi->type != I40E_VSI_MAIN)
4479 return;
4480
4481 netdev = vsi->netdev;
4482 if (!netdev)
4483 return;
4484
4485 /* Bail out if netif_carrier is not OK */
4486 if (!netif_carrier_ok(netdev))
4487 return;
4488
4489 /* Go thru' TX queues for netdev */
4490 for (i = 0; i < netdev->num_tx_queues; i++) {
4491 struct netdev_queue *q;
4492
4493 q = netdev_get_tx_queue(netdev, i);
4494 if (q)
4495 i40e_detect_recover_hung_queue(i, vsi);
4496 }
4497}
4498
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004499/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004500 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004501 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004502 *
4503 * Get TC map for ISCSI PF type that will include iSCSI TC
4504 * and LAN TC.
4505 **/
4506static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4507{
4508 struct i40e_dcb_app_priority_table app;
4509 struct i40e_hw *hw = &pf->hw;
4510 u8 enabled_tc = 1; /* TC0 is always enabled */
4511 u8 tc, i;
4512 /* Get the iSCSI APP TLV */
4513 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4514
4515 for (i = 0; i < dcbcfg->numapps; i++) {
4516 app = dcbcfg->app[i];
4517 if (app.selector == I40E_APP_SEL_TCPIP &&
4518 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4519 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004520 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004521 break;
4522 }
4523 }
4524
4525 return enabled_tc;
4526}
4527
4528/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004529 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4530 * @dcbcfg: the corresponding DCBx configuration structure
4531 *
4532 * Return the number of TCs from given DCBx configuration
4533 **/
4534static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4535{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004536 u8 num_tc = 0;
4537 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004538
4539 /* Scan the ETS Config Priority Table to find
4540 * traffic class enabled for a given priority
4541 * and use the traffic class index to get the
4542 * number of traffic classes enabled
4543 */
4544 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4545 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4546 num_tc = dcbcfg->etscfg.prioritytable[i];
4547 }
4548
4549 /* Traffic class index starts from zero so
4550 * increment to return the actual count
4551 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004552 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004553}
4554
4555/**
4556 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4557 * @dcbcfg: the corresponding DCBx configuration structure
4558 *
4559 * Query the current DCB configuration and return the number of
4560 * traffic classes enabled from the given DCBX config
4561 **/
4562static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4563{
4564 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4565 u8 enabled_tc = 1;
4566 u8 i;
4567
4568 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004569 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004570
4571 return enabled_tc;
4572}
4573
4574/**
4575 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4576 * @pf: PF being queried
4577 *
4578 * Return number of traffic classes enabled for the given PF
4579 **/
4580static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4581{
4582 struct i40e_hw *hw = &pf->hw;
4583 u8 i, enabled_tc;
4584 u8 num_tc = 0;
4585 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4586
4587 /* If DCB is not enabled then always in single TC */
4588 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4589 return 1;
4590
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004591 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004592 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4593 return i40e_dcb_get_num_tc(dcbcfg);
4594
4595 /* MFP mode return count of enabled TCs for this PF */
4596 if (pf->hw.func_caps.iscsi)
4597 enabled_tc = i40e_get_iscsi_tc_map(pf);
4598 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004599 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004600
4601 /* At least have TC0 */
4602 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4603 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004604 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004605 num_tc++;
4606 }
4607 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004608}
4609
4610/**
4611 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4612 * @pf: PF being queried
4613 *
4614 * Return a bitmap for first enabled traffic class for this PF.
4615 **/
4616static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4617{
4618 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4619 u8 i = 0;
4620
4621 if (!enabled_tc)
4622 return 0x1; /* TC0 */
4623
4624 /* Find the first enabled TC */
4625 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004626 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004627 break;
4628 }
4629
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004630 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004631}
4632
4633/**
4634 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4635 * @pf: PF being queried
4636 *
4637 * Return a bitmap for enabled traffic classes for this PF.
4638 **/
4639static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4640{
4641 /* If DCB is not enabled for this PF then just return default TC */
4642 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4643 return i40e_pf_get_default_tc(pf);
4644
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004645 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004646 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4647 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4648
Neerav Parikhfc51de92015-02-24 06:58:53 +00004649 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004650 if (pf->hw.func_caps.iscsi)
4651 return i40e_get_iscsi_tc_map(pf);
4652 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004653 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004654}
4655
4656/**
4657 * i40e_vsi_get_bw_info - Query VSI BW Information
4658 * @vsi: the VSI being queried
4659 *
4660 * Returns 0 on success, negative value on failure
4661 **/
4662static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4663{
4664 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4665 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4666 struct i40e_pf *pf = vsi->back;
4667 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004668 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004669 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004670 int i;
4671
4672 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004673 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4674 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004675 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004676 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4677 i40e_stat_str(&pf->hw, ret),
4678 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004679 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004680 }
4681
4682 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004683 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4684 NULL);
4685 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004686 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004687 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4688 i40e_stat_str(&pf->hw, ret),
4689 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004690 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004691 }
4692
4693 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4694 dev_info(&pf->pdev->dev,
4695 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4696 bw_config.tc_valid_bits,
4697 bw_ets_config.tc_valid_bits);
4698 /* Still continuing */
4699 }
4700
4701 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4702 vsi->bw_max_quanta = bw_config.max_bw;
4703 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4704 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4705 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4706 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4707 vsi->bw_ets_limit_credits[i] =
4708 le16_to_cpu(bw_ets_config.credits[i]);
4709 /* 3 bits out of 4 for each TC */
4710 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4711 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004712
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004713 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004714}
4715
4716/**
4717 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4718 * @vsi: the VSI being configured
4719 * @enabled_tc: TC bitmap
4720 * @bw_credits: BW shared credits per TC
4721 *
4722 * Returns 0 on success, negative value on failure
4723 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004724static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004725 u8 *bw_share)
4726{
4727 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004728 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004729 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004730
4731 bw_data.tc_valid_bits = enabled_tc;
4732 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4733 bw_data.tc_bw_credits[i] = bw_share[i];
4734
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004735 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4736 NULL);
4737 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004738 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004739 "AQ command Config VSI BW allocation per TC failed = %d\n",
4740 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004741 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004742 }
4743
4744 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4745 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4746
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004747 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004748}
4749
4750/**
4751 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4752 * @vsi: the VSI being configured
4753 * @enabled_tc: TC map to be enabled
4754 *
4755 **/
4756static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4757{
4758 struct net_device *netdev = vsi->netdev;
4759 struct i40e_pf *pf = vsi->back;
4760 struct i40e_hw *hw = &pf->hw;
4761 u8 netdev_tc = 0;
4762 int i;
4763 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4764
4765 if (!netdev)
4766 return;
4767
4768 if (!enabled_tc) {
4769 netdev_reset_tc(netdev);
4770 return;
4771 }
4772
4773 /* Set up actual enabled TCs on the VSI */
4774 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4775 return;
4776
4777 /* set per TC queues for the VSI */
4778 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4779 /* Only set TC queues for enabled tcs
4780 *
4781 * e.g. For a VSI that has TC0 and TC3 enabled the
4782 * enabled_tc bitmap would be 0x00001001; the driver
4783 * will set the numtc for netdev as 2 that will be
4784 * referenced by the netdev layer as TC 0 and 1.
4785 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004786 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004787 netdev_set_tc_queue(netdev,
4788 vsi->tc_config.tc_info[i].netdev_tc,
4789 vsi->tc_config.tc_info[i].qcount,
4790 vsi->tc_config.tc_info[i].qoffset);
4791 }
4792
4793 /* Assign UP2TC map for the VSI */
4794 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4795 /* Get the actual TC# for the UP */
4796 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4797 /* Get the mapped netdev TC# for the UP */
4798 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4799 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4800 }
4801}
4802
4803/**
4804 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4805 * @vsi: the VSI being configured
4806 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4807 **/
4808static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4809 struct i40e_vsi_context *ctxt)
4810{
4811 /* copy just the sections touched not the entire info
4812 * since not all sections are valid as returned by
4813 * update vsi params
4814 */
4815 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4816 memcpy(&vsi->info.queue_mapping,
4817 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4818 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4819 sizeof(vsi->info.tc_mapping));
4820}
4821
4822/**
4823 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4824 * @vsi: VSI to be configured
4825 * @enabled_tc: TC bitmap
4826 *
4827 * This configures a particular VSI for TCs that are mapped to the
4828 * given TC bitmap. It uses default bandwidth share for TCs across
4829 * VSIs to configure TC for a particular VSI.
4830 *
4831 * NOTE:
4832 * It is expected that the VSI queues have been quisced before calling
4833 * this function.
4834 **/
4835static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4836{
4837 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4838 struct i40e_vsi_context ctxt;
4839 int ret = 0;
4840 int i;
4841
4842 /* Check if enabled_tc is same as existing or new TCs */
4843 if (vsi->tc_config.enabled_tc == enabled_tc)
4844 return ret;
4845
4846 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4847 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004848 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 bw_share[i] = 1;
4850 }
4851
4852 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4853 if (ret) {
4854 dev_info(&vsi->back->pdev->dev,
4855 "Failed configuring TC map %d for VSI %d\n",
4856 enabled_tc, vsi->seid);
4857 goto out;
4858 }
4859
4860 /* Update Queue Pairs Mapping for currently enabled UPs */
4861 ctxt.seid = vsi->seid;
4862 ctxt.pf_num = vsi->back->hw.pf_id;
4863 ctxt.vf_num = 0;
4864 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004865 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004866 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4867
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004868 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4869 ctxt.info.valid_sections |=
4870 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4871 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4872 }
4873
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004874 /* Update the VSI after updating the VSI queue-mapping information */
4875 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4876 if (ret) {
4877 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004878 "Update vsi tc config failed, err %s aq_err %s\n",
4879 i40e_stat_str(&vsi->back->hw, ret),
4880 i40e_aq_str(&vsi->back->hw,
4881 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004882 goto out;
4883 }
4884 /* update the local VSI info with updated queue map */
4885 i40e_vsi_update_queue_map(vsi, &ctxt);
4886 vsi->info.valid_sections = 0;
4887
4888 /* Update current VSI BW information */
4889 ret = i40e_vsi_get_bw_info(vsi);
4890 if (ret) {
4891 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004892 "Failed updating vsi bw info, err %s aq_err %s\n",
4893 i40e_stat_str(&vsi->back->hw, ret),
4894 i40e_aq_str(&vsi->back->hw,
4895 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004896 goto out;
4897 }
4898
4899 /* Update the netdev TC setup */
4900 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4901out:
4902 return ret;
4903}
4904
4905/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004906 * i40e_veb_config_tc - Configure TCs for given VEB
4907 * @veb: given VEB
4908 * @enabled_tc: TC bitmap
4909 *
4910 * Configures given TC bitmap for VEB (switching) element
4911 **/
4912int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4913{
4914 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4915 struct i40e_pf *pf = veb->pf;
4916 int ret = 0;
4917 int i;
4918
4919 /* No TCs or already enabled TCs just return */
4920 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4921 return ret;
4922
4923 bw_data.tc_valid_bits = enabled_tc;
4924 /* bw_data.absolute_credits is not set (relative) */
4925
4926 /* Enable ETS TCs with equal BW Share for now */
4927 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004928 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004929 bw_data.tc_bw_share_credits[i] = 1;
4930 }
4931
4932 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4933 &bw_data, NULL);
4934 if (ret) {
4935 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004936 "VEB bw config failed, err %s aq_err %s\n",
4937 i40e_stat_str(&pf->hw, ret),
4938 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004939 goto out;
4940 }
4941
4942 /* Update the BW information */
4943 ret = i40e_veb_get_bw_info(veb);
4944 if (ret) {
4945 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004946 "Failed getting veb bw config, err %s aq_err %s\n",
4947 i40e_stat_str(&pf->hw, ret),
4948 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004949 }
4950
4951out:
4952 return ret;
4953}
4954
4955#ifdef CONFIG_I40E_DCB
4956/**
4957 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4958 * @pf: PF struct
4959 *
4960 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4961 * the caller would've quiesce all the VSIs before calling
4962 * this function
4963 **/
4964static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4965{
4966 u8 tc_map = 0;
4967 int ret;
4968 u8 v;
4969
4970 /* Enable the TCs available on PF to all VEBs */
4971 tc_map = i40e_pf_get_tc_map(pf);
4972 for (v = 0; v < I40E_MAX_VEB; v++) {
4973 if (!pf->veb[v])
4974 continue;
4975 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4976 if (ret) {
4977 dev_info(&pf->pdev->dev,
4978 "Failed configuring TC for VEB seid=%d\n",
4979 pf->veb[v]->seid);
4980 /* Will try to configure as many components */
4981 }
4982 }
4983
4984 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004985 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004986 if (!pf->vsi[v])
4987 continue;
4988
4989 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004990#ifdef I40E_FCOE
4991 * - For FCoE VSI only enable the TC configured
4992 * as per the APP TLV
4993#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004994 * - For all others keep them at TC0 for now
4995 */
4996 if (v == pf->lan_vsi)
4997 tc_map = i40e_pf_get_tc_map(pf);
4998 else
4999 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07005000#ifdef I40E_FCOE
5001 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5002 tc_map = i40e_get_fcoe_tc_map(pf);
5003#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005004
5005 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5006 if (ret) {
5007 dev_info(&pf->pdev->dev,
5008 "Failed configuring TC for VSI seid=%d\n",
5009 pf->vsi[v]->seid);
5010 /* Will try to configure as many components */
5011 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005012 /* Re-configure VSI vectors based on updated TC map */
5013 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005014 if (pf->vsi[v]->netdev)
5015 i40e_dcbnl_set_all(pf->vsi[v]);
5016 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005017 i40e_notify_client_of_l2_param_changes(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005018 }
5019}
5020
5021/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005022 * i40e_resume_port_tx - Resume port Tx
5023 * @pf: PF struct
5024 *
5025 * Resume a port's Tx and issue a PF reset in case of failure to
5026 * resume.
5027 **/
5028static int i40e_resume_port_tx(struct i40e_pf *pf)
5029{
5030 struct i40e_hw *hw = &pf->hw;
5031 int ret;
5032
5033 ret = i40e_aq_resume_port_tx(hw, NULL);
5034 if (ret) {
5035 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005036 "Resume Port Tx failed, err %s aq_err %s\n",
5037 i40e_stat_str(&pf->hw, ret),
5038 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005039 /* Schedule PF reset to recover */
5040 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5041 i40e_service_event_schedule(pf);
5042 }
5043
5044 return ret;
5045}
5046
5047/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005048 * i40e_init_pf_dcb - Initialize DCB configuration
5049 * @pf: PF being configured
5050 *
5051 * Query the current DCB configuration and cache it
5052 * in the hardware structure
5053 **/
5054static int i40e_init_pf_dcb(struct i40e_pf *pf)
5055{
5056 struct i40e_hw *hw = &pf->hw;
5057 int err = 0;
5058
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005059 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005060 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005061 goto out;
5062
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005063 /* Get the initial DCB configuration */
5064 err = i40e_init_dcb(hw);
5065 if (!err) {
5066 /* Device/Function is not DCBX capable */
5067 if ((!hw->func_caps.dcb) ||
5068 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5069 dev_info(&pf->pdev->dev,
5070 "DCBX offload is not supported or is disabled for this PF.\n");
5071
5072 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5073 goto out;
5074
5075 } else {
5076 /* When status is not DISABLED then DCBX in FW */
5077 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5078 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005079
5080 pf->flags |= I40E_FLAG_DCB_CAPABLE;
5081 /* Enable DCB tagging only when more than one TC */
5082 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5083 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005084 dev_dbg(&pf->pdev->dev,
5085 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005086 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005087 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005088 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005089 "Query for DCB configuration failed, err %s aq_err %s\n",
5090 i40e_stat_str(&pf->hw, err),
5091 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005092 }
5093
5094out:
5095 return err;
5096}
5097#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005098#define SPEED_SIZE 14
5099#define FC_SIZE 8
5100/**
5101 * i40e_print_link_message - print link up or down
5102 * @vsi: the VSI for which link needs a message
5103 */
Matt Jaredc156f852015-08-27 11:42:39 -04005104void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005105{
Shannon Nelsona9165492015-09-03 17:19:00 -04005106 char *speed = "Unknown";
5107 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005108
Matt Jaredc156f852015-08-27 11:42:39 -04005109 if (vsi->current_isup == isup)
5110 return;
5111 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005112 if (!isup) {
5113 netdev_info(vsi->netdev, "NIC Link is Down\n");
5114 return;
5115 }
5116
Greg Rose148c2d82014-12-11 07:06:27 +00005117 /* Warn user if link speed on NPAR enabled partition is not at
5118 * least 10GB
5119 */
5120 if (vsi->back->hw.func_caps.npar_enable &&
5121 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5122 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5123 netdev_warn(vsi->netdev,
5124 "The partition detected link speed that is less than 10Gbps\n");
5125
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005126 switch (vsi->back->hw.phy.link_info.link_speed) {
5127 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005128 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005129 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005130 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005131 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005132 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005133 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005134 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005135 break;
5136 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005137 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005138 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005139 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005140 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005141 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005142 default:
5143 break;
5144 }
5145
5146 switch (vsi->back->hw.fc.current_mode) {
5147 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005148 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005149 break;
5150 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005151 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005152 break;
5153 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005154 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005155 break;
5156 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005157 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005158 break;
5159 }
5160
Shannon Nelsona9165492015-09-03 17:19:00 -04005161 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005162 speed, fc);
5163}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005164
5165/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005166 * i40e_up_complete - Finish the last steps of bringing up a connection
5167 * @vsi: the VSI being configured
5168 **/
5169static int i40e_up_complete(struct i40e_vsi *vsi)
5170{
5171 struct i40e_pf *pf = vsi->back;
5172 int err;
5173
5174 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5175 i40e_vsi_configure_msix(vsi);
5176 else
5177 i40e_configure_msi_and_legacy(vsi);
5178
5179 /* start rings */
5180 err = i40e_vsi_control_rings(vsi, true);
5181 if (err)
5182 return err;
5183
5184 clear_bit(__I40E_DOWN, &vsi->state);
5185 i40e_napi_enable_all(vsi);
5186 i40e_vsi_enable_irq(vsi);
5187
5188 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5189 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005190 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005191 netif_tx_start_all_queues(vsi->netdev);
5192 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005193 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005194 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005195 /* need to check for qualified module here*/
5196 if ((pf->hw.phy.link_info.link_info &
5197 I40E_AQ_MEDIA_AVAILABLE) &&
5198 (!(pf->hw.phy.link_info.an_info &
5199 I40E_AQ_QUALIFIED_MODULE)))
5200 netdev_err(vsi->netdev,
5201 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005202 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005203
5204 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005205 if (vsi->type == I40E_VSI_FDIR) {
5206 /* reset fd counters */
5207 pf->fd_add_err = pf->fd_atr_cnt = 0;
5208 if (pf->fd_tcp_rule > 0) {
5209 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005210 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5211 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005212 pf->fd_tcp_rule = 0;
5213 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005214 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005215 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005216
5217 /* On the next run of the service_task, notify any clients of the new
5218 * opened netdev
5219 */
5220 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005221 i40e_service_event_schedule(pf);
5222
5223 return 0;
5224}
5225
5226/**
5227 * i40e_vsi_reinit_locked - Reset the VSI
5228 * @vsi: the VSI being configured
5229 *
5230 * Rebuild the ring structs after some configuration
5231 * has changed, e.g. MTU size.
5232 **/
5233static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5234{
5235 struct i40e_pf *pf = vsi->back;
5236
5237 WARN_ON(in_interrupt());
5238 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5239 usleep_range(1000, 2000);
5240 i40e_down(vsi);
5241
5242 /* Give a VF some time to respond to the reset. The
5243 * two second wait is based upon the watchdog cycle in
5244 * the VF driver.
5245 */
5246 if (vsi->type == I40E_VSI_SRIOV)
5247 msleep(2000);
5248 i40e_up(vsi);
5249 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5250}
5251
5252/**
5253 * i40e_up - Bring the connection back up after being down
5254 * @vsi: the VSI being configured
5255 **/
5256int i40e_up(struct i40e_vsi *vsi)
5257{
5258 int err;
5259
5260 err = i40e_vsi_configure(vsi);
5261 if (!err)
5262 err = i40e_up_complete(vsi);
5263
5264 return err;
5265}
5266
5267/**
5268 * i40e_down - Shutdown the connection processing
5269 * @vsi: the VSI being stopped
5270 **/
5271void i40e_down(struct i40e_vsi *vsi)
5272{
5273 int i;
5274
5275 /* It is assumed that the caller of this function
5276 * sets the vsi->state __I40E_DOWN bit.
5277 */
5278 if (vsi->netdev) {
5279 netif_carrier_off(vsi->netdev);
5280 netif_tx_disable(vsi->netdev);
5281 }
5282 i40e_vsi_disable_irq(vsi);
5283 i40e_vsi_control_rings(vsi, false);
5284 i40e_napi_disable_all(vsi);
5285
5286 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005287 i40e_clean_tx_ring(vsi->tx_rings[i]);
5288 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005289 }
5290}
5291
5292/**
5293 * i40e_setup_tc - configure multiple traffic classes
5294 * @netdev: net device to configure
5295 * @tc: number of traffic classes to enable
5296 **/
5297static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5298{
5299 struct i40e_netdev_priv *np = netdev_priv(netdev);
5300 struct i40e_vsi *vsi = np->vsi;
5301 struct i40e_pf *pf = vsi->back;
5302 u8 enabled_tc = 0;
5303 int ret = -EINVAL;
5304 int i;
5305
5306 /* Check if DCB enabled to continue */
5307 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5308 netdev_info(netdev, "DCB is not enabled for adapter\n");
5309 goto exit;
5310 }
5311
5312 /* Check if MFP enabled */
5313 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5314 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5315 goto exit;
5316 }
5317
5318 /* Check whether tc count is within enabled limit */
5319 if (tc > i40e_pf_get_num_tc(pf)) {
5320 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5321 goto exit;
5322 }
5323
5324 /* Generate TC map for number of tc requested */
5325 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005326 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005327
5328 /* Requesting same TC configuration as already enabled */
5329 if (enabled_tc == vsi->tc_config.enabled_tc)
5330 return 0;
5331
5332 /* Quiesce VSI queues */
5333 i40e_quiesce_vsi(vsi);
5334
5335 /* Configure VSI for enabled TCs */
5336 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5337 if (ret) {
5338 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5339 vsi->seid);
5340 goto exit;
5341 }
5342
5343 /* Unquiesce VSI */
5344 i40e_unquiesce_vsi(vsi);
5345
5346exit:
5347 return ret;
5348}
5349
John Fastabende4c67342016-02-16 21:16:15 -08005350#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005351int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5352 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005353#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005354static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5355 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005356#endif
5357{
John Fastabend16e5cc62016-02-16 21:16:43 -08005358 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005359 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005360 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005361}
5362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005363/**
5364 * i40e_open - Called when a network interface is made active
5365 * @netdev: network interface device structure
5366 *
5367 * The open entry point is called when a network interface is made
5368 * active by the system (IFF_UP). At this point all resources needed
5369 * for transmit and receive operations are allocated, the interrupt
5370 * handler is registered with the OS, the netdev watchdog subtask is
5371 * enabled, and the stack is notified that the interface is ready.
5372 *
5373 * Returns 0 on success, negative value on failure
5374 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005375int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376{
5377 struct i40e_netdev_priv *np = netdev_priv(netdev);
5378 struct i40e_vsi *vsi = np->vsi;
5379 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005380 int err;
5381
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005382 /* disallow open during test or if eeprom is broken */
5383 if (test_bit(__I40E_TESTING, &pf->state) ||
5384 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005385 return -EBUSY;
5386
5387 netif_carrier_off(netdev);
5388
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005389 err = i40e_vsi_open(vsi);
5390 if (err)
5391 return err;
5392
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005393 /* configure global TSO hardware offload settings */
5394 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5395 TCP_FLAG_FIN) >> 16);
5396 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5397 TCP_FLAG_FIN |
5398 TCP_FLAG_CWR) >> 16);
5399 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5400
Singhai, Anjali6633d382015-12-03 23:49:31 -08005401#ifdef CONFIG_I40E_VXLAN
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005402 vxlan_get_rx_port(netdev);
5403#endif
Singhai, Anjalicd866602015-12-14 12:21:21 -08005404#ifdef CONFIG_I40E_GENEVE
Anjali Singhaia340c782016-01-06 11:49:28 -08005405 if (pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE)
5406 geneve_get_rx_port(netdev);
Singhai, Anjalicd866602015-12-14 12:21:21 -08005407#endif
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005408
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005409 i40e_notify_client_of_netdev_open(vsi);
5410
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005411 return 0;
5412}
5413
5414/**
5415 * i40e_vsi_open -
5416 * @vsi: the VSI to open
5417 *
5418 * Finish initialization of the VSI.
5419 *
5420 * Returns 0 on success, negative value on failure
5421 **/
5422int i40e_vsi_open(struct i40e_vsi *vsi)
5423{
5424 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005425 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005426 int err;
5427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005428 /* allocate descriptors */
5429 err = i40e_vsi_setup_tx_resources(vsi);
5430 if (err)
5431 goto err_setup_tx;
5432 err = i40e_vsi_setup_rx_resources(vsi);
5433 if (err)
5434 goto err_setup_rx;
5435
5436 err = i40e_vsi_configure(vsi);
5437 if (err)
5438 goto err_setup_rx;
5439
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005440 if (vsi->netdev) {
5441 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5442 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5443 err = i40e_vsi_request_irq(vsi, int_name);
5444 if (err)
5445 goto err_setup_rx;
5446
5447 /* Notify the stack of the actual queue counts. */
5448 err = netif_set_real_num_tx_queues(vsi->netdev,
5449 vsi->num_queue_pairs);
5450 if (err)
5451 goto err_set_queues;
5452
5453 err = netif_set_real_num_rx_queues(vsi->netdev,
5454 vsi->num_queue_pairs);
5455 if (err)
5456 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005457
5458 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005459 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005460 dev_driver_string(&pf->pdev->dev),
5461 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005462 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005463
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005464 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005465 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005466 goto err_setup_rx;
5467 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005468
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005469 err = i40e_up_complete(vsi);
5470 if (err)
5471 goto err_up_complete;
5472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473 return 0;
5474
5475err_up_complete:
5476 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005477err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005478 i40e_vsi_free_irq(vsi);
5479err_setup_rx:
5480 i40e_vsi_free_rx_resources(vsi);
5481err_setup_tx:
5482 i40e_vsi_free_tx_resources(vsi);
5483 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005484 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005485
5486 return err;
5487}
5488
5489/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005490 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005491 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005492 *
5493 * This function destroys the hlist where all the Flow Director
5494 * filters were saved.
5495 **/
5496static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5497{
5498 struct i40e_fdir_filter *filter;
5499 struct hlist_node *node2;
5500
5501 hlist_for_each_entry_safe(filter, node2,
5502 &pf->fdir_filter_list, fdir_node) {
5503 hlist_del(&filter->fdir_node);
5504 kfree(filter);
5505 }
5506 pf->fdir_pf_active_filters = 0;
5507}
5508
5509/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005510 * i40e_close - Disables a network interface
5511 * @netdev: network interface device structure
5512 *
5513 * The close entry point is called when an interface is de-activated
5514 * by the OS. The hardware is still under the driver's control, but
5515 * this netdev interface is disabled.
5516 *
5517 * Returns 0, this is not allowed to fail
5518 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005519int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005520{
5521 struct i40e_netdev_priv *np = netdev_priv(netdev);
5522 struct i40e_vsi *vsi = np->vsi;
5523
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005524 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005525
5526 return 0;
5527}
5528
5529/**
5530 * i40e_do_reset - Start a PF or Core Reset sequence
5531 * @pf: board private structure
5532 * @reset_flags: which reset is requested
5533 *
5534 * The essential difference in resets is that the PF Reset
5535 * doesn't clear the packet buffers, doesn't reset the PE
5536 * firmware, and doesn't bother the other PFs on the chip.
5537 **/
5538void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5539{
5540 u32 val;
5541
5542 WARN_ON(in_interrupt());
5543
Mitch Williams263fc482014-04-23 04:50:11 +00005544
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005545 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005546 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005547
5548 /* Request a Global Reset
5549 *
5550 * This will start the chip's countdown to the actual full
5551 * chip reset event, and a warning interrupt to be sent
5552 * to all PFs, including the requestor. Our handler
5553 * for the warning interrupt will deal with the shutdown
5554 * and recovery of the switch setup.
5555 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005556 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5558 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5559 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5560
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005561 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005562
5563 /* Request a Core Reset
5564 *
5565 * Same as Global Reset, except does *not* include the MAC/PHY
5566 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005567 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005568 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5569 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5570 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5571 i40e_flush(&pf->hw);
5572
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005573 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005574
5575 /* Request a PF Reset
5576 *
5577 * Resets only the PF-specific registers
5578 *
5579 * This goes directly to the tear-down and rebuild of
5580 * the switch, since we need to do all the recovery as
5581 * for the Core Reset.
5582 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005583 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005584 i40e_handle_reset_warning(pf);
5585
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005586 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005587 int v;
5588
5589 /* Find the VSI(s) that requested a re-init */
5590 dev_info(&pf->pdev->dev,
5591 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005592 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005593 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005594
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005595 if (vsi != NULL &&
5596 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5597 i40e_vsi_reinit_locked(pf->vsi[v]);
5598 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5599 }
5600 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005601 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005602 int v;
5603
5604 /* Find the VSI(s) that needs to be brought down */
5605 dev_info(&pf->pdev->dev, "VSI down requested\n");
5606 for (v = 0; v < pf->num_alloc_vsi; v++) {
5607 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005608
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005609 if (vsi != NULL &&
5610 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5611 set_bit(__I40E_DOWN, &vsi->state);
5612 i40e_down(vsi);
5613 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5614 }
5615 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005616 } else {
5617 dev_info(&pf->pdev->dev,
5618 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005619 }
5620}
5621
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005622#ifdef CONFIG_I40E_DCB
5623/**
5624 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5625 * @pf: board private structure
5626 * @old_cfg: current DCB config
5627 * @new_cfg: new DCB config
5628 **/
5629bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5630 struct i40e_dcbx_config *old_cfg,
5631 struct i40e_dcbx_config *new_cfg)
5632{
5633 bool need_reconfig = false;
5634
5635 /* Check if ETS configuration has changed */
5636 if (memcmp(&new_cfg->etscfg,
5637 &old_cfg->etscfg,
5638 sizeof(new_cfg->etscfg))) {
5639 /* If Priority Table has changed reconfig is needed */
5640 if (memcmp(&new_cfg->etscfg.prioritytable,
5641 &old_cfg->etscfg.prioritytable,
5642 sizeof(new_cfg->etscfg.prioritytable))) {
5643 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005644 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005645 }
5646
5647 if (memcmp(&new_cfg->etscfg.tcbwtable,
5648 &old_cfg->etscfg.tcbwtable,
5649 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005650 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005651
5652 if (memcmp(&new_cfg->etscfg.tsatable,
5653 &old_cfg->etscfg.tsatable,
5654 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005655 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005656 }
5657
5658 /* Check if PFC configuration has changed */
5659 if (memcmp(&new_cfg->pfc,
5660 &old_cfg->pfc,
5661 sizeof(new_cfg->pfc))) {
5662 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005663 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005664 }
5665
5666 /* Check if APP Table has changed */
5667 if (memcmp(&new_cfg->app,
5668 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005669 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005670 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005671 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005672 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005673
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005674 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005675 return need_reconfig;
5676}
5677
5678/**
5679 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5680 * @pf: board private structure
5681 * @e: event info posted on ARQ
5682 **/
5683static int i40e_handle_lldp_event(struct i40e_pf *pf,
5684 struct i40e_arq_event_info *e)
5685{
5686 struct i40e_aqc_lldp_get_mib *mib =
5687 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5688 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005689 struct i40e_dcbx_config tmp_dcbx_cfg;
5690 bool need_reconfig = false;
5691 int ret = 0;
5692 u8 type;
5693
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005694 /* Not DCB capable or capability disabled */
5695 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5696 return ret;
5697
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005698 /* Ignore if event is not for Nearest Bridge */
5699 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5700 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005701 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005702 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5703 return ret;
5704
5705 /* Check MIB Type and return if event for Remote MIB update */
5706 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005707 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005708 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005709 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5710 /* Update the remote cached instance and return */
5711 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5712 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5713 &hw->remote_dcbx_config);
5714 goto exit;
5715 }
5716
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005717 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005718 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005719
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005720 /* Reset the old DCBx configuration data */
5721 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005722 /* Get updated DCBX data from firmware */
5723 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005724 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005725 dev_info(&pf->pdev->dev,
5726 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5727 i40e_stat_str(&pf->hw, ret),
5728 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005729 goto exit;
5730 }
5731
5732 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005733 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5734 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005735 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005736 goto exit;
5737 }
5738
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005739 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5740 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005741
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005742 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005743
5744 if (!need_reconfig)
5745 goto exit;
5746
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005747 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005748 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005749 pf->flags |= I40E_FLAG_DCB_ENABLED;
5750 else
5751 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5752
Neerav Parikh69129dc2014-11-12 00:18:46 +00005753 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005754 /* Reconfiguration needed quiesce all VSIs */
5755 i40e_pf_quiesce_all_vsi(pf);
5756
5757 /* Changes in configuration update VEB/VSI */
5758 i40e_dcb_reconfigure(pf);
5759
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005760 ret = i40e_resume_port_tx(pf);
5761
Neerav Parikh69129dc2014-11-12 00:18:46 +00005762 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005763 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005764 if (ret)
5765 goto exit;
5766
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005767 /* Wait for the PF's queues to be disabled */
5768 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005769 if (ret) {
5770 /* Schedule PF reset to recover */
5771 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5772 i40e_service_event_schedule(pf);
5773 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005774 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005775 }
5776
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005777exit:
5778 return ret;
5779}
5780#endif /* CONFIG_I40E_DCB */
5781
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005782/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005783 * i40e_do_reset_safe - Protected reset path for userland calls.
5784 * @pf: board private structure
5785 * @reset_flags: which reset is requested
5786 *
5787 **/
5788void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5789{
5790 rtnl_lock();
5791 i40e_do_reset(pf, reset_flags);
5792 rtnl_unlock();
5793}
5794
5795/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005796 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5797 * @pf: board private structure
5798 * @e: event info posted on ARQ
5799 *
5800 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5801 * and VF queues
5802 **/
5803static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5804 struct i40e_arq_event_info *e)
5805{
5806 struct i40e_aqc_lan_overflow *data =
5807 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5808 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5809 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5810 struct i40e_hw *hw = &pf->hw;
5811 struct i40e_vf *vf;
5812 u16 vf_id;
5813
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005814 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5815 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005816
5817 /* Queue belongs to VF, find the VF and issue VF reset */
5818 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5819 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5820 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5821 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5822 vf_id -= hw->func_caps.vf_base_id;
5823 vf = &pf->vf[vf_id];
5824 i40e_vc_notify_vf_reset(vf);
5825 /* Allow VF to process pending reset notification */
5826 msleep(20);
5827 i40e_reset_vf(vf, false);
5828 }
5829}
5830
5831/**
5832 * i40e_service_event_complete - Finish up the service event
5833 * @pf: board private structure
5834 **/
5835static void i40e_service_event_complete(struct i40e_pf *pf)
5836{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005837 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005838
5839 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005840 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005841 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5842}
5843
5844/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005845 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5846 * @pf: board private structure
5847 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005848u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005849{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005850 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005851
5852 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5853 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5854 return fcnt_prog;
5855}
5856
5857/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005858 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005859 * @pf: board private structure
5860 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005861u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005862{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005863 u32 val, fcnt_prog;
5864
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005865 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5866 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5867 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5868 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5869 return fcnt_prog;
5870}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005871
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005872/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005873 * i40e_get_global_fd_count - Get total FD filters programmed on device
5874 * @pf: board private structure
5875 **/
5876u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5877{
5878 u32 val, fcnt_prog;
5879
5880 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5881 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5882 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5883 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5884 return fcnt_prog;
5885}
5886
5887/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005888 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5889 * @pf: board private structure
5890 **/
5891void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5892{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005893 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005894 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005895 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005896
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005897 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5898 return;
5899
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005900 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5901 * to re-enable
5902 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005903 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005904 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005905 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5906 (pf->fd_add_err == 0) ||
5907 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005908 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5909 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5910 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005911 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5912 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 +00005913 }
5914 }
5915 /* Wait for some more space to be available to turn on ATR */
5916 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5917 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5918 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5919 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005920 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5921 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005922 }
5923 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005924
5925 /* if hw had a problem adding a filter, delete it */
5926 if (pf->fd_inv > 0) {
5927 hlist_for_each_entry_safe(filter, node,
5928 &pf->fdir_filter_list, fdir_node) {
5929 if (filter->fd_id == pf->fd_inv) {
5930 hlist_del(&filter->fdir_node);
5931 kfree(filter);
5932 pf->fdir_pf_active_filters--;
5933 }
5934 }
5935 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005936}
5937
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005938#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005939#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005940/**
5941 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5942 * @pf: board private structure
5943 **/
5944static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5945{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005946 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005947 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005948 bool disable_atr = false;
5949 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005950 int reg;
5951
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005952 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5953 return;
5954
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005955 if (!time_after(jiffies, pf->fd_flush_timestamp +
5956 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5957 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005958
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005959 /* If the flush is happening too quick and we have mostly SB rules we
5960 * should not re-enable ATR for some time.
5961 */
5962 min_flush_time = pf->fd_flush_timestamp +
5963 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5964 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005965
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005966 if (!(time_after(jiffies, min_flush_time)) &&
5967 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5968 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5969 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5970 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005971 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005972
5973 pf->fd_flush_timestamp = jiffies;
5974 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5975 /* flush all filters */
5976 wr32(&pf->hw, I40E_PFQF_CTL_1,
5977 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5978 i40e_flush(&pf->hw);
5979 pf->fd_flush_cnt++;
5980 pf->fd_add_err = 0;
5981 do {
5982 /* Check FD flush status every 5-6msec */
5983 usleep_range(5000, 6000);
5984 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5985 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5986 break;
5987 } while (flush_wait_retry--);
5988 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5989 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5990 } else {
5991 /* replay sideband filters */
5992 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5993 if (!disable_atr)
5994 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5995 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5996 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5997 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5998 }
5999
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006000}
6001
6002/**
6003 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6004 * @pf: board private structure
6005 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006006u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006007{
6008 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6009}
6010
6011/* We can see up to 256 filter programming desc in transit if the filters are
6012 * being applied really fast; before we see the first
6013 * filter miss error on Rx queue 0. Accumulating enough error messages before
6014 * reacting will make sure we don't cause flush too often.
6015 */
6016#define I40E_MAX_FD_PROGRAM_ERROR 256
6017
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006018/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006019 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6020 * @pf: board private structure
6021 **/
6022static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6023{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006024
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006025 /* if interface is down do nothing */
6026 if (test_bit(__I40E_DOWN, &pf->state))
6027 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006028
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00006029 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
6030 return;
6031
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006032 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006033 i40e_fdir_flush_and_replay(pf);
6034
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006035 i40e_fdir_check_and_reenable(pf);
6036
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006037}
6038
6039/**
6040 * i40e_vsi_link_event - notify VSI of a link event
6041 * @vsi: vsi to be notified
6042 * @link_up: link up or down
6043 **/
6044static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6045{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006046 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006047 return;
6048
6049 switch (vsi->type) {
6050 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006051#ifdef I40E_FCOE
6052 case I40E_VSI_FCOE:
6053#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006054 if (!vsi->netdev || !vsi->netdev_registered)
6055 break;
6056
6057 if (link_up) {
6058 netif_carrier_on(vsi->netdev);
6059 netif_tx_wake_all_queues(vsi->netdev);
6060 } else {
6061 netif_carrier_off(vsi->netdev);
6062 netif_tx_stop_all_queues(vsi->netdev);
6063 }
6064 break;
6065
6066 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006067 case I40E_VSI_VMDQ2:
6068 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006069 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006070 case I40E_VSI_MIRROR:
6071 default:
6072 /* there is no notification for other VSIs */
6073 break;
6074 }
6075}
6076
6077/**
6078 * i40e_veb_link_event - notify elements on the veb of a link event
6079 * @veb: veb to be notified
6080 * @link_up: link up or down
6081 **/
6082static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6083{
6084 struct i40e_pf *pf;
6085 int i;
6086
6087 if (!veb || !veb->pf)
6088 return;
6089 pf = veb->pf;
6090
6091 /* depth first... */
6092 for (i = 0; i < I40E_MAX_VEB; i++)
6093 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6094 i40e_veb_link_event(pf->veb[i], link_up);
6095
6096 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006097 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006098 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6099 i40e_vsi_link_event(pf->vsi[i], link_up);
6100}
6101
6102/**
6103 * i40e_link_event - Update netif_carrier status
6104 * @pf: board private structure
6105 **/
6106static void i40e_link_event(struct i40e_pf *pf)
6107{
Mitch Williams320684c2014-10-17 03:14:43 +00006108 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006109 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006110 i40e_status status;
6111 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006112
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006113 /* save off old link status information */
6114 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6115
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006116 /* set this to force the get_link_status call to refresh state */
6117 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006118
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006119 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006120
6121 status = i40e_get_link_status(&pf->hw, &new_link);
6122 if (status) {
6123 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6124 status);
6125 return;
6126 }
6127
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006128 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6129 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006130
6131 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006132 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006133 (test_bit(__I40E_DOWN, &vsi->state) ||
6134 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006135 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006136
6137 if (!test_bit(__I40E_DOWN, &vsi->state))
6138 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006139
6140 /* Notify the base of the switch tree connected to
6141 * the link. Floating VEBs are not notified.
6142 */
6143 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6144 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6145 else
Mitch Williams320684c2014-10-17 03:14:43 +00006146 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006147
6148 if (pf->vf)
6149 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006150
6151 if (pf->flags & I40E_FLAG_PTP)
6152 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006153}
6154
6155/**
Shannon Nelson21536712014-10-25 10:35:25 +00006156 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006157 * @pf: board private structure
6158 **/
6159static void i40e_watchdog_subtask(struct i40e_pf *pf)
6160{
6161 int i;
6162
6163 /* if interface is down do nothing */
6164 if (test_bit(__I40E_DOWN, &pf->state) ||
6165 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6166 return;
6167
Shannon Nelson21536712014-10-25 10:35:25 +00006168 /* make sure we don't do these things too often */
6169 if (time_before(jiffies, (pf->service_timer_previous +
6170 pf->service_timer_period)))
6171 return;
6172 pf->service_timer_previous = jiffies;
6173
Shannon Nelson9ac77262015-08-27 11:42:40 -04006174 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6175 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006177 /* Update the stats for active netdevs so the network stack
6178 * can look at updated numbers whenever it cares to
6179 */
Mitch Williams505682c2014-05-20 08:01:37 +00006180 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006181 if (pf->vsi[i] && pf->vsi[i]->netdev)
6182 i40e_update_stats(pf->vsi[i]);
6183
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006184 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6185 /* Update the stats for the active switching components */
6186 for (i = 0; i < I40E_MAX_VEB; i++)
6187 if (pf->veb[i])
6188 i40e_update_veb_stats(pf->veb[i]);
6189 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006190
6191 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006192}
6193
6194/**
6195 * i40e_reset_subtask - Set up for resetting the device and driver
6196 * @pf: board private structure
6197 **/
6198static void i40e_reset_subtask(struct i40e_pf *pf)
6199{
6200 u32 reset_flags = 0;
6201
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006202 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006203 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006204 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006205 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6206 }
6207 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006208 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006209 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6210 }
6211 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006212 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006213 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6214 }
6215 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006216 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6218 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006219 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006220 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006221 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6222 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006223
6224 /* If there's a recovery already waiting, it takes
6225 * precedence before starting a new reset sequence.
6226 */
6227 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6228 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006229 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006230 }
6231
6232 /* If we're already down or resetting, just bail */
6233 if (reset_flags &&
6234 !test_bit(__I40E_DOWN, &pf->state) &&
6235 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6236 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006237
6238unlock:
6239 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006240}
6241
6242/**
6243 * i40e_handle_link_event - Handle link event
6244 * @pf: board private structure
6245 * @e: event info posted on ARQ
6246 **/
6247static void i40e_handle_link_event(struct i40e_pf *pf,
6248 struct i40e_arq_event_info *e)
6249{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 struct i40e_aqc_get_link_status *status =
6251 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006252
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006253 /* Do a new status request to re-enable LSE reporting
6254 * and load new status information into the hw struct
6255 * This completely ignores any state information
6256 * in the ARQ event info, instead choosing to always
6257 * issue the AQ update link status command.
6258 */
6259 i40e_link_event(pf);
6260
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006261 /* check for unqualified module, if link is down */
6262 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6263 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6264 (!(status->link_info & I40E_AQ_LINK_UP)))
6265 dev_err(&pf->pdev->dev,
6266 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006267}
6268
6269/**
6270 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6271 * @pf: board private structure
6272 **/
6273static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6274{
6275 struct i40e_arq_event_info event;
6276 struct i40e_hw *hw = &pf->hw;
6277 u16 pending, i = 0;
6278 i40e_status ret;
6279 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006280 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006281 u32 val;
6282
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006283 /* Do not run clean AQ when PF reset fails */
6284 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6285 return;
6286
Shannon Nelson86df2422014-05-20 08:01:35 +00006287 /* check for error indications */
6288 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6289 oldval = val;
6290 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006291 if (hw->debug_mask & I40E_DEBUG_AQ)
6292 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006293 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6294 }
6295 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006296 if (hw->debug_mask & I40E_DEBUG_AQ)
6297 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006298 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006299 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006300 }
6301 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006302 if (hw->debug_mask & I40E_DEBUG_AQ)
6303 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006304 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6305 }
6306 if (oldval != val)
6307 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6308
6309 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6310 oldval = val;
6311 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006312 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6313 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006314 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6315 }
6316 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006317 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6318 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006319 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6320 }
6321 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006322 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6323 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006324 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6325 }
6326 if (oldval != val)
6327 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6328
Mitch Williams1001dc32014-11-11 20:02:19 +00006329 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6330 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 if (!event.msg_buf)
6332 return;
6333
6334 do {
6335 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006336 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006337 break;
Mitch Williams56497972014-06-04 08:45:18 +00006338 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006339 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6340 break;
6341 }
6342
6343 opcode = le16_to_cpu(event.desc.opcode);
6344 switch (opcode) {
6345
6346 case i40e_aqc_opc_get_link_status:
6347 i40e_handle_link_event(pf, &event);
6348 break;
6349 case i40e_aqc_opc_send_msg_to_pf:
6350 ret = i40e_vc_process_vf_msg(pf,
6351 le16_to_cpu(event.desc.retval),
6352 le32_to_cpu(event.desc.cookie_high),
6353 le32_to_cpu(event.desc.cookie_low),
6354 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006355 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006356 break;
6357 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006358 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006359#ifdef CONFIG_I40E_DCB
6360 rtnl_lock();
6361 ret = i40e_handle_lldp_event(pf, &event);
6362 rtnl_unlock();
6363#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006364 break;
6365 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006366 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367 i40e_handle_lan_overflow_event(pf, &event);
6368 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006369 case i40e_aqc_opc_send_msg_to_peer:
6370 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6371 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006372 case i40e_aqc_opc_nvm_erase:
6373 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006374 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006375 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6376 "ARQ NVM operation 0x%04x completed\n",
6377 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006378 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006379 default:
6380 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006381 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006382 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006383 break;
6384 }
6385 } while (pending && (i++ < pf->adminq_work_limit));
6386
6387 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6388 /* re-enable Admin queue interrupt cause */
6389 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6390 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6391 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6392 i40e_flush(hw);
6393
6394 kfree(event.msg_buf);
6395}
6396
6397/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006398 * i40e_verify_eeprom - make sure eeprom is good to use
6399 * @pf: board private structure
6400 **/
6401static void i40e_verify_eeprom(struct i40e_pf *pf)
6402{
6403 int err;
6404
6405 err = i40e_diag_eeprom_test(&pf->hw);
6406 if (err) {
6407 /* retry in case of garbage read */
6408 err = i40e_diag_eeprom_test(&pf->hw);
6409 if (err) {
6410 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6411 err);
6412 set_bit(__I40E_BAD_EEPROM, &pf->state);
6413 }
6414 }
6415
6416 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6417 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6418 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6419 }
6420}
6421
6422/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006423 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006424 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006425 *
6426 * enable switch loop back or die - no point in a return value
6427 **/
6428static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6429{
6430 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6431 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006432 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006433
6434 ctxt.seid = pf->main_vsi_seid;
6435 ctxt.pf_num = pf->hw.pf_id;
6436 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006437 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6438 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006439 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006440 "couldn't get PF vsi config, err %s aq_err %s\n",
6441 i40e_stat_str(&pf->hw, ret),
6442 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006443 return;
6444 }
6445 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6446 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6447 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6448
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006449 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6450 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006451 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006452 "update vsi switch failed, err %s aq_err %s\n",
6453 i40e_stat_str(&pf->hw, ret),
6454 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006455 }
6456}
6457
6458/**
6459 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006460 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006461 *
6462 * disable switch loop back or die - no point in a return value
6463 **/
6464static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6465{
6466 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6467 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006468 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006469
6470 ctxt.seid = pf->main_vsi_seid;
6471 ctxt.pf_num = pf->hw.pf_id;
6472 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006473 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6474 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006475 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006476 "couldn't get PF vsi config, err %s aq_err %s\n",
6477 i40e_stat_str(&pf->hw, ret),
6478 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006479 return;
6480 }
6481 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6482 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6483 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6484
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006485 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6486 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006487 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006488 "update vsi switch failed, err %s aq_err %s\n",
6489 i40e_stat_str(&pf->hw, ret),
6490 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006491 }
6492}
6493
6494/**
Neerav Parikh51616012015-02-06 08:52:14 +00006495 * i40e_config_bridge_mode - Configure the HW bridge mode
6496 * @veb: pointer to the bridge instance
6497 *
6498 * Configure the loop back mode for the LAN VSI that is downlink to the
6499 * specified HW bridge instance. It is expected this function is called
6500 * when a new HW bridge is instantiated.
6501 **/
6502static void i40e_config_bridge_mode(struct i40e_veb *veb)
6503{
6504 struct i40e_pf *pf = veb->pf;
6505
Shannon Nelson6dec1012015-09-28 14:12:30 -04006506 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6507 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6508 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006509 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6510 i40e_disable_pf_switch_lb(pf);
6511 else
6512 i40e_enable_pf_switch_lb(pf);
6513}
6514
6515/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6517 * @veb: pointer to the VEB instance
6518 *
6519 * This is a recursive function that first builds the attached VSIs then
6520 * recurses in to build the next layer of VEB. We track the connections
6521 * through our own index numbers because the seid's from the HW could
6522 * change across the reset.
6523 **/
6524static int i40e_reconstitute_veb(struct i40e_veb *veb)
6525{
6526 struct i40e_vsi *ctl_vsi = NULL;
6527 struct i40e_pf *pf = veb->pf;
6528 int v, veb_idx;
6529 int ret;
6530
6531 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006532 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006533 if (pf->vsi[v] &&
6534 pf->vsi[v]->veb_idx == veb->idx &&
6535 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6536 ctl_vsi = pf->vsi[v];
6537 break;
6538 }
6539 }
6540 if (!ctl_vsi) {
6541 dev_info(&pf->pdev->dev,
6542 "missing owner VSI for veb_idx %d\n", veb->idx);
6543 ret = -ENOENT;
6544 goto end_reconstitute;
6545 }
6546 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6547 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6548 ret = i40e_add_vsi(ctl_vsi);
6549 if (ret) {
6550 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006551 "rebuild of veb_idx %d owner VSI failed: %d\n",
6552 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553 goto end_reconstitute;
6554 }
6555 i40e_vsi_reset_stats(ctl_vsi);
6556
6557 /* create the VEB in the switch and move the VSI onto the VEB */
6558 ret = i40e_add_veb(veb, ctl_vsi);
6559 if (ret)
6560 goto end_reconstitute;
6561
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006562 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6563 veb->bridge_mode = BRIDGE_MODE_VEB;
6564 else
6565 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006566 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006567
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006568 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006569 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006570 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6571 continue;
6572
6573 if (pf->vsi[v]->veb_idx == veb->idx) {
6574 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006575
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006576 vsi->uplink_seid = veb->seid;
6577 ret = i40e_add_vsi(vsi);
6578 if (ret) {
6579 dev_info(&pf->pdev->dev,
6580 "rebuild of vsi_idx %d failed: %d\n",
6581 v, ret);
6582 goto end_reconstitute;
6583 }
6584 i40e_vsi_reset_stats(vsi);
6585 }
6586 }
6587
6588 /* create any VEBs attached to this VEB - RECURSION */
6589 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6590 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6591 pf->veb[veb_idx]->uplink_seid = veb->seid;
6592 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6593 if (ret)
6594 break;
6595 }
6596 }
6597
6598end_reconstitute:
6599 return ret;
6600}
6601
6602/**
6603 * i40e_get_capabilities - get info about the HW
6604 * @pf: the PF struct
6605 **/
6606static int i40e_get_capabilities(struct i40e_pf *pf)
6607{
6608 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6609 u16 data_size;
6610 int buf_len;
6611 int err;
6612
6613 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6614 do {
6615 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6616 if (!cap_buf)
6617 return -ENOMEM;
6618
6619 /* this loads the data into the hw struct for us */
6620 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6621 &data_size,
6622 i40e_aqc_opc_list_func_capabilities,
6623 NULL);
6624 /* data loaded, buffer no longer needed */
6625 kfree(cap_buf);
6626
6627 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6628 /* retry with a larger buffer */
6629 buf_len = data_size;
6630 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6631 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006632 "capability discovery failed, err %s aq_err %s\n",
6633 i40e_stat_str(&pf->hw, err),
6634 i40e_aq_str(&pf->hw,
6635 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006636 return -ENODEV;
6637 }
6638 } while (err);
6639
6640 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6641 dev_info(&pf->pdev->dev,
6642 "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",
6643 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6644 pf->hw.func_caps.num_msix_vectors,
6645 pf->hw.func_caps.num_msix_vectors_vf,
6646 pf->hw.func_caps.fd_filters_guaranteed,
6647 pf->hw.func_caps.fd_filters_best_effort,
6648 pf->hw.func_caps.num_tx_qp,
6649 pf->hw.func_caps.num_vsis);
6650
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006651#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6652 + pf->hw.func_caps.num_vfs)
6653 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6654 dev_info(&pf->pdev->dev,
6655 "got num_vsis %d, setting num_vsis to %d\n",
6656 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6657 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6658 }
6659
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006660 return 0;
6661}
6662
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006663static int i40e_vsi_clear(struct i40e_vsi *vsi);
6664
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006665/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006666 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006667 * @pf: board private structure
6668 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006669static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006670{
6671 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006672 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006673
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006674 /* quick workaround for an NVM issue that leaves a critical register
6675 * uninitialized
6676 */
6677 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6678 static const u32 hkey[] = {
6679 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6680 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6681 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6682 0x95b3a76d};
6683
6684 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6685 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6686 }
6687
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006688 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689 return;
6690
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006691 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006692 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006693 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006694 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006696 break;
6697 }
6698 }
6699
6700 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006702 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6703 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006704 if (!vsi) {
6705 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006706 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6707 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006708 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006709 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006710
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006711 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006712}
6713
6714/**
6715 * i40e_fdir_teardown - release the Flow Director resources
6716 * @pf: board private structure
6717 **/
6718static void i40e_fdir_teardown(struct i40e_pf *pf)
6719{
6720 int i;
6721
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006722 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006723 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006724 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6725 i40e_vsi_release(pf->vsi[i]);
6726 break;
6727 }
6728 }
6729}
6730
6731/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006732 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006733 * @pf: board private structure
6734 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006735 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006736 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006737static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006738{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006739 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006740 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006741 u32 v;
6742
6743 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6744 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006745 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006746 if (i40e_check_asq_alive(&pf->hw))
6747 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006748
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006749 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006751 /* quiesce the VSIs and their queues that are not already DOWN */
6752 i40e_pf_quiesce_all_vsi(pf);
6753
Mitch Williams505682c2014-05-20 08:01:37 +00006754 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006755 if (pf->vsi[v])
6756 pf->vsi[v]->seid = 0;
6757 }
6758
6759 i40e_shutdown_adminq(&pf->hw);
6760
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006761 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006762 if (hw->hmc.hmc_obj) {
6763 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006764 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006765 dev_warn(&pf->pdev->dev,
6766 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006767 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006768}
6769
6770/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006771 * i40e_send_version - update firmware with driver version
6772 * @pf: PF struct
6773 */
6774static void i40e_send_version(struct i40e_pf *pf)
6775{
6776 struct i40e_driver_version dv;
6777
6778 dv.major_version = DRV_VERSION_MAJOR;
6779 dv.minor_version = DRV_VERSION_MINOR;
6780 dv.build_version = DRV_VERSION_BUILD;
6781 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006782 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006783 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6784}
6785
6786/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006787 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006788 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006789 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006790 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006791static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006792{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006793 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006794 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006795 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006796 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006797 u32 v;
6798
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006799 /* Now we wait for GRST to settle out.
6800 * We don't have to delete the VEBs or VSIs from the hw switch
6801 * because the reset will make them disappear.
6802 */
6803 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006804 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006805 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006806 set_bit(__I40E_RESET_FAILED, &pf->state);
6807 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006808 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006809 pf->pfr_count++;
6810
6811 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006812 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006813 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006814
6815 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6816 ret = i40e_init_adminq(&pf->hw);
6817 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006818 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6819 i40e_stat_str(&pf->hw, ret),
6820 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006821 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006822 }
6823
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006824 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006825 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006826 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006827
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006828 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006829 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006830 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006831 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006832
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6834 hw->func_caps.num_rx_qp,
6835 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6836 if (ret) {
6837 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6838 goto end_core_reset;
6839 }
6840 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6841 if (ret) {
6842 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6843 goto end_core_reset;
6844 }
6845
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006846#ifdef CONFIG_I40E_DCB
6847 ret = i40e_init_pf_dcb(pf);
6848 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006849 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6850 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6851 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006852 }
6853#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006854#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006855 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006856
Vasu Dev38e00432014-08-01 13:27:03 -07006857#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006858 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006859 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006860 if (ret)
6861 goto end_core_reset;
6862
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006863 /* The driver only wants link up/down and module qualification
6864 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006865 */
6866 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006867 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006868 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006869 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006870 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006871 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6872 i40e_stat_str(&pf->hw, ret),
6873 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006874
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006875 /* make sure our flow control settings are restored */
6876 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6877 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006878 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6879 i40e_stat_str(&pf->hw, ret),
6880 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006881
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006882 /* Rebuild the VSIs and VEBs that existed before reset.
6883 * They are still in our local switch element arrays, so only
6884 * need to rebuild the switch model in the HW.
6885 *
6886 * If there were VEBs but the reconstitution failed, we'll try
6887 * try to recover minimal use by getting the basic PF VSI working.
6888 */
6889 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006890 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006891 /* find the one VEB connected to the MAC, and find orphans */
6892 for (v = 0; v < I40E_MAX_VEB; v++) {
6893 if (!pf->veb[v])
6894 continue;
6895
6896 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6897 pf->veb[v]->uplink_seid == 0) {
6898 ret = i40e_reconstitute_veb(pf->veb[v]);
6899
6900 if (!ret)
6901 continue;
6902
6903 /* If Main VEB failed, we're in deep doodoo,
6904 * so give up rebuilding the switch and set up
6905 * for minimal rebuild of PF VSI.
6906 * If orphan failed, we'll report the error
6907 * but try to keep going.
6908 */
6909 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6910 dev_info(&pf->pdev->dev,
6911 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6912 ret);
6913 pf->vsi[pf->lan_vsi]->uplink_seid
6914 = pf->mac_seid;
6915 break;
6916 } else if (pf->veb[v]->uplink_seid == 0) {
6917 dev_info(&pf->pdev->dev,
6918 "rebuild of orphan VEB failed: %d\n",
6919 ret);
6920 }
6921 }
6922 }
6923 }
6924
6925 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006926 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006927 /* no VEB, so rebuild only the Main VSI */
6928 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6929 if (ret) {
6930 dev_info(&pf->pdev->dev,
6931 "rebuild of Main VSI failed: %d\n", ret);
6932 goto end_core_reset;
6933 }
6934 }
6935
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006936 /* Reconfigure hardware for allowing smaller MSS in the case
6937 * of TSO, so that we avoid the MDD being fired and causing
6938 * a reset in the case of small MSS+TSO.
6939 */
6940#define I40E_REG_MSS 0x000E64DC
6941#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6942#define I40E_64BYTE_MSS 0x400000
6943 val = rd32(hw, I40E_REG_MSS);
6944 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6945 val &= ~I40E_REG_MSS_MIN_MASK;
6946 val |= I40E_64BYTE_MSS;
6947 wr32(hw, I40E_REG_MSS, val);
6948 }
6949
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006950 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006951 msleep(75);
6952 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6953 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006954 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6955 i40e_stat_str(&pf->hw, ret),
6956 i40e_aq_str(&pf->hw,
6957 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006958 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 /* reinit the misc interrupt */
6960 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6961 ret = i40e_setup_misc_vector(pf);
6962
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006963 /* Add a filter to drop all Flow control frames from any VSI from being
6964 * transmitted. By doing so we stop a malicious VF from sending out
6965 * PAUSE or PFC frames and potentially controlling traffic for other
6966 * PF/VF VSIs.
6967 * The FW can still send Flow control frames if enabled.
6968 */
6969 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6970 pf->main_vsi_seid);
6971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006972 /* restart the VSIs that were rebuilt and running before the reset */
6973 i40e_pf_unquiesce_all_vsi(pf);
6974
Mitch Williams69f64b22014-02-13 03:48:46 -08006975 if (pf->num_alloc_vfs) {
6976 for (v = 0; v < pf->num_alloc_vfs; v++)
6977 i40e_reset_vf(&pf->vf[v], true);
6978 }
6979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006980 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006981 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006982
6983end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006984 clear_bit(__I40E_RESET_FAILED, &pf->state);
6985clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006986 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6987}
6988
6989/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006990 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006991 * @pf: board private structure
6992 *
6993 * Close up the VFs and other things in prep for a Core Reset,
6994 * then get ready to rebuild the world.
6995 **/
6996static void i40e_handle_reset_warning(struct i40e_pf *pf)
6997{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006998 i40e_prep_for_reset(pf);
6999 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007000}
7001
7002/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007003 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007004 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007005 *
7006 * Called from the MDD irq handler to identify possibly malicious vfs
7007 **/
7008static void i40e_handle_mdd_event(struct i40e_pf *pf)
7009{
7010 struct i40e_hw *hw = &pf->hw;
7011 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007012 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007013 struct i40e_vf *vf;
7014 u32 reg;
7015 int i;
7016
7017 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7018 return;
7019
7020 /* find what triggered the MDD event */
7021 reg = rd32(hw, I40E_GL_MDET_TX);
7022 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007023 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7024 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007025 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007026 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007027 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007028 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007029 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7030 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7031 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007032 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007033 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 +00007034 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007035 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7036 mdd_detected = true;
7037 }
7038 reg = rd32(hw, I40E_GL_MDET_RX);
7039 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007040 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7041 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007042 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007043 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007044 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7045 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7046 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007047 if (netif_msg_rx_err(pf))
7048 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7049 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007050 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7051 mdd_detected = true;
7052 }
7053
Neerav Parikhdf430b12014-06-04 01:23:15 +00007054 if (mdd_detected) {
7055 reg = rd32(hw, I40E_PF_MDET_TX);
7056 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7057 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007058 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007059 pf_mdd_detected = true;
7060 }
7061 reg = rd32(hw, I40E_PF_MDET_RX);
7062 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7063 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007064 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007065 pf_mdd_detected = true;
7066 }
7067 /* Queue belongs to the PF, initiate a reset */
7068 if (pf_mdd_detected) {
7069 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7070 i40e_service_event_schedule(pf);
7071 }
7072 }
7073
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074 /* see if one of the VFs needs its hand slapped */
7075 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7076 vf = &(pf->vf[i]);
7077 reg = rd32(hw, I40E_VP_MDET_TX(i));
7078 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7079 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7080 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007081 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7082 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007083 }
7084
7085 reg = rd32(hw, I40E_VP_MDET_RX(i));
7086 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7087 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7088 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007089 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7090 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007091 }
7092
7093 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7094 dev_info(&pf->pdev->dev,
7095 "Too many MDD events on VF %d, disabled\n", i);
7096 dev_info(&pf->pdev->dev,
7097 "Use PF Control I/F to re-enable the VF\n");
7098 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7099 }
7100 }
7101
7102 /* re-enable mdd interrupt cause */
7103 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7104 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7105 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7106 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7107 i40e_flush(hw);
7108}
7109
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007110/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007111 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007112 * @pf: board private structure
7113 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007114static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007115{
Singhai, Anjali6a899022015-12-14 12:21:18 -08007116#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007117 struct i40e_hw *hw = &pf->hw;
7118 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007119 __be16 port;
7120 int i;
7121
Singhai, Anjali6a899022015-12-14 12:21:18 -08007122 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007123 return;
7124
Singhai, Anjali6a899022015-12-14 12:21:18 -08007125 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007126
7127 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007128 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7129 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7130 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007131 if (port)
7132 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Singhai, Anjali6a899022015-12-14 12:21:18 -08007133 pf->udp_ports[i].type,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007134 NULL, NULL);
7135 else
7136 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007137
7138 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007139 dev_dbg(&pf->pdev->dev,
7140 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7141 pf->udp_ports[i].type ? "vxlan" : "geneve",
7142 port ? "add" : "delete",
7143 ntohs(port), i,
7144 i40e_stat_str(&pf->hw, ret),
7145 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007146 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007147 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007148 }
7149 }
7150 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08007151#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007152}
7153
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007154/**
7155 * i40e_service_task - Run the driver's async subtasks
7156 * @work: pointer to work_struct containing our data
7157 **/
7158static void i40e_service_task(struct work_struct *work)
7159{
7160 struct i40e_pf *pf = container_of(work,
7161 struct i40e_pf,
7162 service_task);
7163 unsigned long start_time = jiffies;
7164
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007165 /* don't bother with service tasks if a reset is in progress */
7166 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7167 i40e_service_event_complete(pf);
7168 return;
7169 }
7170
Kiran Patilb03a8c12015-09-24 18:13:15 -04007171 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007172 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007173 i40e_reset_subtask(pf);
7174 i40e_handle_mdd_event(pf);
7175 i40e_vc_process_vflr_event(pf);
7176 i40e_watchdog_subtask(pf);
7177 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007178 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007179 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007180 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007181 i40e_clean_adminq_subtask(pf);
7182
7183 i40e_service_event_complete(pf);
7184
7185 /* If the tasks have taken longer than one timer cycle or there
7186 * is more work to be done, reschedule the service task now
7187 * rather than wait for the timer to tick again.
7188 */
7189 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7190 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7191 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7192 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7193 i40e_service_event_schedule(pf);
7194}
7195
7196/**
7197 * i40e_service_timer - timer callback
7198 * @data: pointer to PF struct
7199 **/
7200static void i40e_service_timer(unsigned long data)
7201{
7202 struct i40e_pf *pf = (struct i40e_pf *)data;
7203
7204 mod_timer(&pf->service_timer,
7205 round_jiffies(jiffies + pf->service_timer_period));
7206 i40e_service_event_schedule(pf);
7207}
7208
7209/**
7210 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7211 * @vsi: the VSI being configured
7212 **/
7213static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7214{
7215 struct i40e_pf *pf = vsi->back;
7216
7217 switch (vsi->type) {
7218 case I40E_VSI_MAIN:
7219 vsi->alloc_queue_pairs = pf->num_lan_qps;
7220 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7221 I40E_REQ_DESCRIPTOR_MULTIPLE);
7222 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7223 vsi->num_q_vectors = pf->num_lan_msix;
7224 else
7225 vsi->num_q_vectors = 1;
7226
7227 break;
7228
7229 case I40E_VSI_FDIR:
7230 vsi->alloc_queue_pairs = 1;
7231 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7232 I40E_REQ_DESCRIPTOR_MULTIPLE);
7233 vsi->num_q_vectors = 1;
7234 break;
7235
7236 case I40E_VSI_VMDQ2:
7237 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7238 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7239 I40E_REQ_DESCRIPTOR_MULTIPLE);
7240 vsi->num_q_vectors = pf->num_vmdq_msix;
7241 break;
7242
7243 case I40E_VSI_SRIOV:
7244 vsi->alloc_queue_pairs = pf->num_vf_qps;
7245 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7246 I40E_REQ_DESCRIPTOR_MULTIPLE);
7247 break;
7248
Vasu Dev38e00432014-08-01 13:27:03 -07007249#ifdef I40E_FCOE
7250 case I40E_VSI_FCOE:
7251 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7252 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7253 I40E_REQ_DESCRIPTOR_MULTIPLE);
7254 vsi->num_q_vectors = pf->num_fcoe_msix;
7255 break;
7256
7257#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007258 default:
7259 WARN_ON(1);
7260 return -ENODATA;
7261 }
7262
7263 return 0;
7264}
7265
7266/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007267 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7268 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007269 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007270 *
7271 * On error: returns error code (negative)
7272 * On success: returns 0
7273 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007274static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007275{
7276 int size;
7277 int ret = 0;
7278
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007279 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007280 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7281 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7282 if (!vsi->tx_rings)
7283 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007284 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7285
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007286 if (alloc_qvectors) {
7287 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007288 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007289 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7290 if (!vsi->q_vectors) {
7291 ret = -ENOMEM;
7292 goto err_vectors;
7293 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007294 }
7295 return ret;
7296
7297err_vectors:
7298 kfree(vsi->tx_rings);
7299 return ret;
7300}
7301
7302/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007303 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7304 * @pf: board private structure
7305 * @type: type of VSI
7306 *
7307 * On error: returns error code (negative)
7308 * On success: returns vsi index in PF (positive)
7309 **/
7310static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7311{
7312 int ret = -ENODEV;
7313 struct i40e_vsi *vsi;
7314 int vsi_idx;
7315 int i;
7316
7317 /* Need to protect the allocation of the VSIs at the PF level */
7318 mutex_lock(&pf->switch_mutex);
7319
7320 /* VSI list may be fragmented if VSI creation/destruction has
7321 * been happening. We can afford to do a quick scan to look
7322 * for any free VSIs in the list.
7323 *
7324 * find next empty vsi slot, looping back around if necessary
7325 */
7326 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007327 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007328 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007329 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007330 i = 0;
7331 while (i < pf->next_vsi && pf->vsi[i])
7332 i++;
7333 }
7334
Mitch Williams505682c2014-05-20 08:01:37 +00007335 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336 vsi_idx = i; /* Found one! */
7337 } else {
7338 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007339 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007340 }
7341 pf->next_vsi = ++i;
7342
7343 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7344 if (!vsi) {
7345 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007346 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007347 }
7348 vsi->type = type;
7349 vsi->back = pf;
7350 set_bit(__I40E_DOWN, &vsi->state);
7351 vsi->flags = 0;
7352 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007353 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007354 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7355 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007356 vsi->netdev_registered = false;
7357 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7358 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007359 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007360
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007361 ret = i40e_set_num_rings_in_vsi(vsi);
7362 if (ret)
7363 goto err_rings;
7364
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007365 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007366 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007367 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007369 /* Setup default MSIX irq handler for VSI */
7370 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7371
Kiran Patil21659032015-09-30 14:09:03 -04007372 /* Initialize VSI lock */
7373 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007374 pf->vsi[vsi_idx] = vsi;
7375 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007376 goto unlock_pf;
7377
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007378err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007379 pf->next_vsi = i - 1;
7380 kfree(vsi);
7381unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007382 mutex_unlock(&pf->switch_mutex);
7383 return ret;
7384}
7385
7386/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007387 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7388 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007389 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007390 *
7391 * On error: returns error code (negative)
7392 * On success: returns 0
7393 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007394static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007395{
7396 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007397 if (free_qvectors) {
7398 kfree(vsi->q_vectors);
7399 vsi->q_vectors = NULL;
7400 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007401 kfree(vsi->tx_rings);
7402 vsi->tx_rings = NULL;
7403 vsi->rx_rings = NULL;
7404}
7405
7406/**
Helin Zhang28c58692015-10-26 19:44:27 -04007407 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7408 * and lookup table
7409 * @vsi: Pointer to VSI structure
7410 */
7411static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7412{
7413 if (!vsi)
7414 return;
7415
7416 kfree(vsi->rss_hkey_user);
7417 vsi->rss_hkey_user = NULL;
7418
7419 kfree(vsi->rss_lut_user);
7420 vsi->rss_lut_user = NULL;
7421}
7422
7423/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007424 * i40e_vsi_clear - Deallocate the VSI provided
7425 * @vsi: the VSI being un-configured
7426 **/
7427static int i40e_vsi_clear(struct i40e_vsi *vsi)
7428{
7429 struct i40e_pf *pf;
7430
7431 if (!vsi)
7432 return 0;
7433
7434 if (!vsi->back)
7435 goto free_vsi;
7436 pf = vsi->back;
7437
7438 mutex_lock(&pf->switch_mutex);
7439 if (!pf->vsi[vsi->idx]) {
7440 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7441 vsi->idx, vsi->idx, vsi, vsi->type);
7442 goto unlock_vsi;
7443 }
7444
7445 if (pf->vsi[vsi->idx] != vsi) {
7446 dev_err(&pf->pdev->dev,
7447 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7448 pf->vsi[vsi->idx]->idx,
7449 pf->vsi[vsi->idx],
7450 pf->vsi[vsi->idx]->type,
7451 vsi->idx, vsi, vsi->type);
7452 goto unlock_vsi;
7453 }
7454
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007455 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007456 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7457 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7458
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007459 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007460 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007461
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007462 pf->vsi[vsi->idx] = NULL;
7463 if (vsi->idx < pf->next_vsi)
7464 pf->next_vsi = vsi->idx;
7465
7466unlock_vsi:
7467 mutex_unlock(&pf->switch_mutex);
7468free_vsi:
7469 kfree(vsi);
7470
7471 return 0;
7472}
7473
7474/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007475 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7476 * @vsi: the VSI being cleaned
7477 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007478static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007479{
7480 int i;
7481
Greg Rose8e9dca52013-12-18 13:45:53 +00007482 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007483 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007484 kfree_rcu(vsi->tx_rings[i], rcu);
7485 vsi->tx_rings[i] = NULL;
7486 vsi->rx_rings[i] = NULL;
7487 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007488 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007489}
7490
7491/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007492 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7493 * @vsi: the VSI being configured
7494 **/
7495static int i40e_alloc_rings(struct i40e_vsi *vsi)
7496{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007497 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007498 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007499 int i;
7500
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007502 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007503 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007504 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7505 if (!tx_ring)
7506 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007507
7508 tx_ring->queue_index = i;
7509 tx_ring->reg_idx = vsi->base_queue + i;
7510 tx_ring->ring_active = false;
7511 tx_ring->vsi = vsi;
7512 tx_ring->netdev = vsi->netdev;
7513 tx_ring->dev = &pf->pdev->dev;
7514 tx_ring->count = vsi->num_desc;
7515 tx_ring->size = 0;
7516 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007517 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7518 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007519 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007520 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007521
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007522 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007523 rx_ring->queue_index = i;
7524 rx_ring->reg_idx = vsi->base_queue + i;
7525 rx_ring->ring_active = false;
7526 rx_ring->vsi = vsi;
7527 rx_ring->netdev = vsi->netdev;
7528 rx_ring->dev = &pf->pdev->dev;
7529 rx_ring->count = vsi->num_desc;
7530 rx_ring->size = 0;
7531 rx_ring->dcb_tc = 0;
7532 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7533 set_ring_16byte_desc_enabled(rx_ring);
7534 else
7535 clear_ring_16byte_desc_enabled(rx_ring);
Kan Lianga75e8002016-02-19 09:24:04 -05007536 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007537 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007538 }
7539
7540 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007541
7542err_out:
7543 i40e_vsi_clear_rings(vsi);
7544 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007545}
7546
7547/**
7548 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7549 * @pf: board private structure
7550 * @vectors: the number of MSI-X vectors to request
7551 *
7552 * Returns the number of vectors reserved, or error
7553 **/
7554static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7555{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007556 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7557 I40E_MIN_MSIX, vectors);
7558 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007559 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007560 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007561 vectors = 0;
7562 }
7563
7564 return vectors;
7565}
7566
7567/**
7568 * i40e_init_msix - Setup the MSIX capability
7569 * @pf: board private structure
7570 *
7571 * Work with the OS to set up the MSIX vectors needed.
7572 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007573 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007574 **/
7575static int i40e_init_msix(struct i40e_pf *pf)
7576{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007578 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007579 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007580 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007581 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582
7583 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7584 return -ENODEV;
7585
7586 /* The number of vectors we'll request will be comprised of:
7587 * - Add 1 for "other" cause for Admin Queue events, etc.
7588 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007589 * - Queues being used for RSS.
7590 * We don't need as many as max_rss_size vectors.
7591 * use rss_size instead in the calculation since that
7592 * is governed by number of cpus in the system.
7593 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007594 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007595 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007596#ifdef I40E_FCOE
7597 * - The number of FCOE qps.
7598#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007599 * Once we count this up, try the request.
7600 *
7601 * If we can't get what we want, we'll simplify to nearly nothing
7602 * and try again. If that still fails, we punt.
7603 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007604 vectors_left = hw->func_caps.num_msix_vectors;
7605 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007606
Shannon Nelson1e200e42015-02-27 09:15:24 +00007607 /* reserve one vector for miscellaneous handler */
7608 if (vectors_left) {
7609 v_budget++;
7610 vectors_left--;
7611 }
7612
7613 /* reserve vectors for the main PF traffic queues */
7614 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7615 vectors_left -= pf->num_lan_msix;
7616 v_budget += pf->num_lan_msix;
7617
7618 /* reserve one vector for sideband flow director */
7619 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7620 if (vectors_left) {
7621 v_budget++;
7622 vectors_left--;
7623 } else {
7624 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7625 }
7626 }
John W Linville83840e42015-01-14 03:06:28 +00007627
Vasu Dev38e00432014-08-01 13:27:03 -07007628#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007629 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007630 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007631 if (!vectors_left)
7632 pf->num_fcoe_msix = 0;
7633 else if (vectors_left >= pf->num_fcoe_qps)
7634 pf->num_fcoe_msix = pf->num_fcoe_qps;
7635 else
7636 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007637 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007638 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007639 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007640
Vasu Dev38e00432014-08-01 13:27:03 -07007641#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007642 /* can we reserve enough for iWARP? */
7643 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7644 if (!vectors_left)
7645 pf->num_iwarp_msix = 0;
7646 else if (vectors_left < pf->num_iwarp_msix)
7647 pf->num_iwarp_msix = 1;
7648 v_budget += pf->num_iwarp_msix;
7649 vectors_left -= pf->num_iwarp_msix;
7650 }
7651
Shannon Nelson1e200e42015-02-27 09:15:24 +00007652 /* any vectors left over go for VMDq support */
7653 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7654 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7655 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7656
7657 /* if we're short on vectors for what's desired, we limit
7658 * the queues per vmdq. If this is still more than are
7659 * available, the user will need to change the number of
7660 * queues/vectors used by the PF later with the ethtool
7661 * channels command
7662 */
7663 if (vmdq_vecs < vmdq_vecs_wanted)
7664 pf->num_vmdq_qps = 1;
7665 pf->num_vmdq_msix = pf->num_vmdq_qps;
7666
7667 v_budget += vmdq_vecs;
7668 vectors_left -= vmdq_vecs;
7669 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007670
7671 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7672 GFP_KERNEL);
7673 if (!pf->msix_entries)
7674 return -ENOMEM;
7675
7676 for (i = 0; i < v_budget; i++)
7677 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007678 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007679
Shannon Nelson3b444392015-02-26 16:15:57 +00007680 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007681 /* If we have limited resources, we will start with no vectors
7682 * for the special features and then allocate vectors to some
7683 * of these features based on the policy and at the end disable
7684 * the features that did not get any vectors.
7685 */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007686 iwarp_requested = pf->num_iwarp_msix;
7687 pf->num_iwarp_msix = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07007688#ifdef I40E_FCOE
7689 pf->num_fcoe_qps = 0;
7690 pf->num_fcoe_msix = 0;
7691#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007692 pf->num_vmdq_msix = 0;
7693 }
7694
Shannon Nelson3b444392015-02-26 16:15:57 +00007695 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7697 kfree(pf->msix_entries);
7698 pf->msix_entries = NULL;
7699 return -ENODEV;
7700
Shannon Nelson3b444392015-02-26 16:15:57 +00007701 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007702 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007703 pf->num_vmdq_vsis = 0;
7704 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007705 pf->num_lan_qps = 1;
7706 pf->num_lan_msix = 1;
7707
Shannon Nelson3b444392015-02-26 16:15:57 +00007708 } else if (v_actual != v_budget) {
7709 int vec;
7710
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007711 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007712 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714 /* Scale vector usage down */
7715 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007716 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007717 pf->num_vmdq_qps = 1;
7718 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007719
7720 /* partition out the remaining vectors */
7721 switch (vec) {
7722 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007723 pf->num_lan_msix = 1;
7724 break;
7725 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007726 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7727 pf->num_lan_msix = 1;
7728 pf->num_iwarp_msix = 1;
7729 } else {
7730 pf->num_lan_msix = 2;
7731 }
Vasu Dev38e00432014-08-01 13:27:03 -07007732#ifdef I40E_FCOE
7733 /* give one vector to FCoE */
7734 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7735 pf->num_lan_msix = 1;
7736 pf->num_fcoe_msix = 1;
7737 }
Vasu Dev38e00432014-08-01 13:27:03 -07007738#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007739 break;
7740 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007741 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7742 pf->num_iwarp_msix = min_t(int, (vec / 3),
7743 iwarp_requested);
7744 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7745 I40E_DEFAULT_NUM_VMDQ_VSI);
7746 } else {
7747 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7748 I40E_DEFAULT_NUM_VMDQ_VSI);
7749 }
7750 pf->num_lan_msix = min_t(int,
7751 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7752 pf->num_lan_msix);
Vasu Dev38e00432014-08-01 13:27:03 -07007753#ifdef I40E_FCOE
7754 /* give one vector to FCoE */
7755 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7756 pf->num_fcoe_msix = 1;
7757 vec--;
7758 }
7759#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760 break;
7761 }
7762 }
7763
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007764 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7765 (pf->num_vmdq_msix == 0)) {
7766 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7767 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7768 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007769
7770 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7771 (pf->num_iwarp_msix == 0)) {
7772 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7773 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7774 }
Vasu Dev38e00432014-08-01 13:27:03 -07007775#ifdef I40E_FCOE
7776
7777 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7778 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7779 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7780 }
7781#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007782 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007783}
7784
7785/**
Greg Rose90e04072014-03-06 08:59:57 +00007786 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007787 * @vsi: the VSI being configured
7788 * @v_idx: index of the vector in the vsi struct
7789 *
7790 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7791 **/
Greg Rose90e04072014-03-06 08:59:57 +00007792static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007793{
7794 struct i40e_q_vector *q_vector;
7795
7796 /* allocate q_vector */
7797 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7798 if (!q_vector)
7799 return -ENOMEM;
7800
7801 q_vector->vsi = vsi;
7802 q_vector->v_idx = v_idx;
7803 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7804 if (vsi->netdev)
7805 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007806 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007807
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007808 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7809 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7810
Alexander Duyck493fb302013-09-28 07:01:44 +00007811 /* tie q_vector and vsi together */
7812 vsi->q_vectors[v_idx] = q_vector;
7813
7814 return 0;
7815}
7816
7817/**
Greg Rose90e04072014-03-06 08:59:57 +00007818 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007819 * @vsi: the VSI being configured
7820 *
7821 * We allocate one q_vector per queue interrupt. If allocation fails we
7822 * return -ENOMEM.
7823 **/
Greg Rose90e04072014-03-06 08:59:57 +00007824static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007825{
7826 struct i40e_pf *pf = vsi->back;
7827 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007828 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007829
7830 /* if not MSIX, give the one vector only to the LAN VSI */
7831 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7832 num_q_vectors = vsi->num_q_vectors;
7833 else if (vsi == pf->vsi[pf->lan_vsi])
7834 num_q_vectors = 1;
7835 else
7836 return -EINVAL;
7837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007838 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007839 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007840 if (err)
7841 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007842 }
7843
7844 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007845
7846err_out:
7847 while (v_idx--)
7848 i40e_free_q_vector(vsi, v_idx);
7849
7850 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007851}
7852
7853/**
7854 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7855 * @pf: board private structure to initialize
7856 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007857static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007858{
Shannon Nelson3b444392015-02-26 16:15:57 +00007859 int vectors = 0;
7860 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007861
7862 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007863 vectors = i40e_init_msix(pf);
7864 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007865 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007866 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007867#ifdef I40E_FCOE
7868 I40E_FLAG_FCOE_ENABLED |
7869#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007870 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007871 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007872 I40E_FLAG_SRIOV_ENABLED |
7873 I40E_FLAG_FD_SB_ENABLED |
7874 I40E_FLAG_FD_ATR_ENABLED |
7875 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007876
7877 /* rework the queue expectations without MSIX */
7878 i40e_determine_queue_usage(pf);
7879 }
7880 }
7881
7882 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7883 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007884 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007885 vectors = pci_enable_msi(pf->pdev);
7886 if (vectors < 0) {
7887 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7888 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007889 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7890 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007891 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007892 }
7893
Shannon Nelson958a3e32013-09-28 07:13:28 +00007894 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007895 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007896
Shannon Nelson3b444392015-02-26 16:15:57 +00007897 /* set up vector assignment tracking */
7898 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7899 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007900 if (!pf->irq_pile) {
7901 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7902 return -ENOMEM;
7903 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007904 pf->irq_pile->num_entries = vectors;
7905 pf->irq_pile->search_hint = 0;
7906
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007907 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007908 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007909
7910 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007911}
7912
7913/**
7914 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7915 * @pf: board private structure
7916 *
7917 * This sets up the handler for MSIX 0, which is used to manage the
7918 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7919 * when in MSI or Legacy interrupt mode.
7920 **/
7921static int i40e_setup_misc_vector(struct i40e_pf *pf)
7922{
7923 struct i40e_hw *hw = &pf->hw;
7924 int err = 0;
7925
7926 /* Only request the irq if this is the first time through, and
7927 * not when we're rebuilding after a Reset
7928 */
7929 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7930 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007931 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007932 if (err) {
7933 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007934 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007935 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007936 return -EFAULT;
7937 }
7938 }
7939
Jacob Kellerab437b52014-12-14 01:55:08 +00007940 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007941
7942 /* associate no queues to the misc vector */
7943 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7944 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7945
7946 i40e_flush(hw);
7947
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007948 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007949
7950 return err;
7951}
7952
7953/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007954 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7955 * @vsi: vsi structure
7956 * @seed: RSS hash seed
7957 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007958static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7959 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007960{
7961 struct i40e_aqc_get_set_rss_key_data rss_key;
7962 struct i40e_pf *pf = vsi->back;
7963 struct i40e_hw *hw = &pf->hw;
7964 bool pf_lut = false;
7965 u8 *rss_lut;
7966 int ret, i;
7967
7968 memset(&rss_key, 0, sizeof(rss_key));
7969 memcpy(&rss_key, seed, sizeof(rss_key));
7970
7971 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7972 if (!rss_lut)
7973 return -ENOMEM;
7974
7975 /* Populate the LUT with max no. of queues in round robin fashion */
7976 for (i = 0; i < vsi->rss_table_size; i++)
7977 rss_lut[i] = i % vsi->rss_size;
7978
7979 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7980 if (ret) {
7981 dev_info(&pf->pdev->dev,
7982 "Cannot set RSS key, err %s aq_err %s\n",
7983 i40e_stat_str(&pf->hw, ret),
7984 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007985 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007986 }
7987
7988 if (vsi->type == I40E_VSI_MAIN)
7989 pf_lut = true;
7990
7991 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7992 vsi->rss_table_size);
7993 if (ret)
7994 dev_info(&pf->pdev->dev,
7995 "Cannot set RSS lut, err %s aq_err %s\n",
7996 i40e_stat_str(&pf->hw, ret),
7997 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7998
Anjali Singhai126b63d2015-08-24 13:26:53 -07007999config_rss_aq_out:
8000 kfree(rss_lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008001 return ret;
8002}
8003
8004/**
8005 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8006 * @vsi: VSI structure
8007 **/
8008static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8009{
8010 u8 seed[I40E_HKEY_ARRAY_SIZE];
8011 struct i40e_pf *pf = vsi->back;
Helin Zhange69ff812015-10-21 19:56:22 -04008012 u8 *lut;
8013 int ret;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008014
Helin Zhange69ff812015-10-21 19:56:22 -04008015 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8016 return 0;
8017
8018 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8019 if (!lut)
8020 return -ENOMEM;
8021
8022 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008023 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhangacd65442015-10-26 19:44:28 -04008024 vsi->rss_size = min_t(int, pf->alloc_rss_size, vsi->num_queue_pairs);
Helin Zhange69ff812015-10-21 19:56:22 -04008025 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8026 kfree(lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008027
Helin Zhange69ff812015-10-21 19:56:22 -04008028 return ret;
8029}
8030
8031/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008032 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8033 * @vsi: Pointer to vsi structure
8034 * @seed: Buffter to store the hash keys
8035 * @lut: Buffer to store the lookup table entries
8036 * @lut_size: Size of buffer to store the lookup table entries
8037 *
8038 * Return 0 on success, negative on failure
8039 */
8040static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8041 u8 *lut, u16 lut_size)
8042{
8043 struct i40e_pf *pf = vsi->back;
8044 struct i40e_hw *hw = &pf->hw;
8045 int ret = 0;
8046
8047 if (seed) {
8048 ret = i40e_aq_get_rss_key(hw, vsi->id,
8049 (struct i40e_aqc_get_set_rss_key_data *)seed);
8050 if (ret) {
8051 dev_info(&pf->pdev->dev,
8052 "Cannot get RSS key, err %s aq_err %s\n",
8053 i40e_stat_str(&pf->hw, ret),
8054 i40e_aq_str(&pf->hw,
8055 pf->hw.aq.asq_last_status));
8056 return ret;
8057 }
8058 }
8059
8060 if (lut) {
8061 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8062
8063 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8064 if (ret) {
8065 dev_info(&pf->pdev->dev,
8066 "Cannot get RSS lut, err %s aq_err %s\n",
8067 i40e_stat_str(&pf->hw, ret),
8068 i40e_aq_str(&pf->hw,
8069 pf->hw.aq.asq_last_status));
8070 return ret;
8071 }
8072 }
8073
8074 return ret;
8075}
8076
8077/**
Helin Zhang043dd652015-10-21 19:56:23 -04008078 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008079 * @vsi: Pointer to vsi structure
8080 * @seed: RSS hash seed
8081 * @lut: Lookup table
8082 * @lut_size: Lookup table size
8083 *
8084 * Returns 0 on success, negative on failure
8085 **/
8086static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8087 const u8 *lut, u16 lut_size)
8088{
8089 struct i40e_pf *pf = vsi->back;
8090 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008091 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008092 u8 i;
8093
8094 /* Fill out hash function seed */
8095 if (seed) {
8096 u32 *seed_dw = (u32 *)seed;
8097
Mitch Williamsc4e18682016-04-12 08:30:40 -07008098 if (vsi->type == I40E_VSI_MAIN) {
8099 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8100 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8101 seed_dw[i]);
8102 } else if (vsi->type == I40E_VSI_SRIOV) {
8103 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8104 i40e_write_rx_ctl(hw,
8105 I40E_VFQF_HKEY1(i, vf_id),
8106 seed_dw[i]);
8107 } else {
8108 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8109 }
Helin Zhange69ff812015-10-21 19:56:22 -04008110 }
8111
8112 if (lut) {
8113 u32 *lut_dw = (u32 *)lut;
8114
Mitch Williamsc4e18682016-04-12 08:30:40 -07008115 if (vsi->type == I40E_VSI_MAIN) {
8116 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8117 return -EINVAL;
8118 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8119 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8120 } else if (vsi->type == I40E_VSI_SRIOV) {
8121 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8122 return -EINVAL;
8123 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8124 i40e_write_rx_ctl(hw,
8125 I40E_VFQF_HLUT1(i, vf_id),
8126 lut_dw[i]);
8127 } else {
8128 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8129 }
Helin Zhange69ff812015-10-21 19:56:22 -04008130 }
8131 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008132
8133 return 0;
8134}
8135
8136/**
Helin Zhang043dd652015-10-21 19:56:23 -04008137 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8138 * @vsi: Pointer to VSI structure
8139 * @seed: Buffer to store the keys
8140 * @lut: Buffer to store the lookup table entries
8141 * @lut_size: Size of buffer to store the lookup table entries
8142 *
8143 * Returns 0 on success, negative on failure
8144 */
8145static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8146 u8 *lut, u16 lut_size)
8147{
8148 struct i40e_pf *pf = vsi->back;
8149 struct i40e_hw *hw = &pf->hw;
8150 u16 i;
8151
8152 if (seed) {
8153 u32 *seed_dw = (u32 *)seed;
8154
8155 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008156 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008157 }
8158 if (lut) {
8159 u32 *lut_dw = (u32 *)lut;
8160
8161 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8162 return -EINVAL;
8163 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8164 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8165 }
8166
8167 return 0;
8168}
8169
8170/**
8171 * i40e_config_rss - Configure RSS keys and lut
8172 * @vsi: Pointer to VSI structure
8173 * @seed: RSS hash seed
8174 * @lut: Lookup table
8175 * @lut_size: Lookup table size
8176 *
8177 * Returns 0 on success, negative on failure
8178 */
8179int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8180{
8181 struct i40e_pf *pf = vsi->back;
8182
8183 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8184 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8185 else
8186 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8187}
8188
8189/**
8190 * i40e_get_rss - Get RSS keys and lut
8191 * @vsi: Pointer to VSI structure
8192 * @seed: Buffer to store the keys
8193 * @lut: Buffer to store the lookup table entries
8194 * lut_size: Size of buffer to store the lookup table entries
8195 *
8196 * Returns 0 on success, negative on failure
8197 */
8198int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8199{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008200 struct i40e_pf *pf = vsi->back;
8201
8202 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8203 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8204 else
8205 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008206}
8207
8208/**
Helin Zhange69ff812015-10-21 19:56:22 -04008209 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8210 * @pf: Pointer to board private structure
8211 * @lut: Lookup table
8212 * @rss_table_size: Lookup table size
8213 * @rss_size: Range of queue number for hashing
8214 */
8215static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8216 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008217{
Helin Zhange69ff812015-10-21 19:56:22 -04008218 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008219
Helin Zhange69ff812015-10-21 19:56:22 -04008220 for (i = 0; i < rss_table_size; i++)
8221 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008222}
8223
8224/**
Helin Zhang043dd652015-10-21 19:56:23 -04008225 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008226 * @pf: board private structure
8227 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008228static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008229{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008230 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008231 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008232 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008233 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008234 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008235 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008236 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008237
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008238 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008239 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8240 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008241 hena |= i40e_pf_get_default_rss_hena(pf);
8242
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008243 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8244 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008245
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008246 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008247 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008248 reg_val = (pf->rss_table_size == 512) ?
8249 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8250 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008251 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008252
Helin Zhang28c58692015-10-26 19:44:27 -04008253 /* Determine the RSS size of the VSI */
8254 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008255 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8256 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008257
Helin Zhange69ff812015-10-21 19:56:22 -04008258 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8259 if (!lut)
8260 return -ENOMEM;
8261
Helin Zhang28c58692015-10-26 19:44:27 -04008262 /* Use user configured lut if there is one, otherwise use default */
8263 if (vsi->rss_lut_user)
8264 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8265 else
8266 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008267
Helin Zhang28c58692015-10-26 19:44:27 -04008268 /* Use user configured hash key if there is one, otherwise
8269 * use default.
8270 */
8271 if (vsi->rss_hkey_user)
8272 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8273 else
8274 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008275 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008276 kfree(lut);
8277
8278 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008279}
8280
8281/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008282 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8283 * @pf: board private structure
8284 * @queue_count: the requested queue count for rss.
8285 *
8286 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8287 * count which may be different from the requested queue count.
8288 **/
8289int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8290{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008291 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8292 int new_rss_size;
8293
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008294 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8295 return 0;
8296
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008297 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008298
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008299 if (queue_count != vsi->num_queue_pairs) {
8300 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008301 i40e_prep_for_reset(pf);
8302
Helin Zhangacd65442015-10-26 19:44:28 -04008303 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008304
8305 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008306
8307 /* Discard the user configured hash keys and lut, if less
8308 * queues are enabled.
8309 */
8310 if (queue_count < vsi->rss_size) {
8311 i40e_clear_rss_config_user(vsi);
8312 dev_dbg(&pf->pdev->dev,
8313 "discard user configured hash keys and lut\n");
8314 }
8315
8316 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008317 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8318 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008319
Helin Zhang043dd652015-10-21 19:56:23 -04008320 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008321 }
Helin Zhange36b0b12015-10-26 19:44:36 -04008322 dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
8323 pf->alloc_rss_size, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008324 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008325}
8326
8327/**
Greg Rosef4492db2015-02-06 08:52:12 +00008328 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8329 * @pf: board private structure
8330 **/
8331i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8332{
8333 i40e_status status;
8334 bool min_valid, max_valid;
8335 u32 max_bw, min_bw;
8336
8337 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8338 &min_valid, &max_valid);
8339
8340 if (!status) {
8341 if (min_valid)
8342 pf->npar_min_bw = min_bw;
8343 if (max_valid)
8344 pf->npar_max_bw = max_bw;
8345 }
8346
8347 return status;
8348}
8349
8350/**
8351 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8352 * @pf: board private structure
8353 **/
8354i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8355{
8356 struct i40e_aqc_configure_partition_bw_data bw_data;
8357 i40e_status status;
8358
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008359 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008360 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008361 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8362 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8363
8364 /* Set the new bandwidths */
8365 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8366
8367 return status;
8368}
8369
8370/**
8371 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8372 * @pf: board private structure
8373 **/
8374i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8375{
8376 /* Commit temporary BW setting to permanent NVM image */
8377 enum i40e_admin_queue_err last_aq_status;
8378 i40e_status ret;
8379 u16 nvm_word;
8380
8381 if (pf->hw.partition_id != 1) {
8382 dev_info(&pf->pdev->dev,
8383 "Commit BW only works on partition 1! This is partition %d",
8384 pf->hw.partition_id);
8385 ret = I40E_NOT_SUPPORTED;
8386 goto bw_commit_out;
8387 }
8388
8389 /* Acquire NVM for read access */
8390 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8391 last_aq_status = pf->hw.aq.asq_last_status;
8392 if (ret) {
8393 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008394 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8395 i40e_stat_str(&pf->hw, ret),
8396 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008397 goto bw_commit_out;
8398 }
8399
8400 /* Read word 0x10 of NVM - SW compatibility word 1 */
8401 ret = i40e_aq_read_nvm(&pf->hw,
8402 I40E_SR_NVM_CONTROL_WORD,
8403 0x10, sizeof(nvm_word), &nvm_word,
8404 false, NULL);
8405 /* Save off last admin queue command status before releasing
8406 * the NVM
8407 */
8408 last_aq_status = pf->hw.aq.asq_last_status;
8409 i40e_release_nvm(&pf->hw);
8410 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008411 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8412 i40e_stat_str(&pf->hw, ret),
8413 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008414 goto bw_commit_out;
8415 }
8416
8417 /* Wait a bit for NVM release to complete */
8418 msleep(50);
8419
8420 /* Acquire NVM for write access */
8421 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8422 last_aq_status = pf->hw.aq.asq_last_status;
8423 if (ret) {
8424 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008425 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8426 i40e_stat_str(&pf->hw, ret),
8427 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008428 goto bw_commit_out;
8429 }
8430 /* Write it back out unchanged to initiate update NVM,
8431 * which will force a write of the shadow (alt) RAM to
8432 * the NVM - thus storing the bandwidth values permanently.
8433 */
8434 ret = i40e_aq_update_nvm(&pf->hw,
8435 I40E_SR_NVM_CONTROL_WORD,
8436 0x10, sizeof(nvm_word),
8437 &nvm_word, true, NULL);
8438 /* Save off last admin queue command status before releasing
8439 * the NVM
8440 */
8441 last_aq_status = pf->hw.aq.asq_last_status;
8442 i40e_release_nvm(&pf->hw);
8443 if (ret)
8444 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008445 "BW settings NOT SAVED, err %s aq_err %s\n",
8446 i40e_stat_str(&pf->hw, ret),
8447 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008448bw_commit_out:
8449
8450 return ret;
8451}
8452
8453/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008454 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8455 * @pf: board private structure to initialize
8456 *
8457 * i40e_sw_init initializes the Adapter private data structure.
8458 * Fields are initialized based on PCI device information and
8459 * OS network device settings (MTU size).
8460 **/
8461static int i40e_sw_init(struct i40e_pf *pf)
8462{
8463 int err = 0;
8464 int size;
8465
8466 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8467 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
8468 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8469 if (I40E_DEBUG_USER & debug)
8470 pf->hw.debug_mask = debug;
8471 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8472 I40E_DEFAULT_MSG_ENABLE);
8473 }
8474
8475 /* Set default capability flags */
8476 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8477 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008478 I40E_FLAG_MSIX_ENABLED;
8479
8480 if (iommu_present(&pci_bus_type))
8481 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
8482 else
8483 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008484
Mitch Williamsca99eb92014-04-04 04:43:07 +00008485 /* Set default ITR */
8486 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8487 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8488
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008489 /* Depending on PF configurations, it is possible that the RSS
8490 * maximum might end up larger than the available queues
8491 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008492 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008493 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008494 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008495 pf->rss_size_max = min_t(int, pf->rss_size_max,
8496 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008497 if (pf->hw.func_caps.rss) {
8498 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008499 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8500 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008501 }
8502
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008503 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008504 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008505 pf->flags |= I40E_FLAG_MFP_ENABLED;
8506 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008507 if (i40e_get_npar_bw_setting(pf))
8508 dev_warn(&pf->pdev->dev,
8509 "Could not get NPAR bw settings\n");
8510 else
8511 dev_info(&pf->pdev->dev,
8512 "Min BW = %8.8x, Max BW = %8.8x\n",
8513 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008514 }
8515
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008516 /* FW/NVM is not yet fixed in this regard */
8517 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8518 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8519 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8520 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008521 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8522 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008523 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008524 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008525 else
8526 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008527 pf->fdir_pf_filter_count =
8528 pf->hw.func_caps.fd_filters_guaranteed;
8529 pf->hw.fdir_shared_filter_count =
8530 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008531 }
8532
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008533 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008534 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008535 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008536 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008537 /* No DCB support for FW < v4.33 */
8538 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8539 }
8540
8541 /* Disable FW LLDP if FW < v4.3 */
8542 if (i40e_is_mac_710(&pf->hw) &&
8543 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8544 (pf->hw.aq.fw_maj_ver < 4)))
8545 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8546
8547 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8548 if (i40e_is_mac_710(&pf->hw) &&
8549 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8550 (pf->hw.aq.fw_maj_ver >= 5)))
8551 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008553 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008554 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008555 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008556 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008557 }
8558
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008559 if (pf->hw.func_caps.iwarp) {
8560 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8561 /* IWARP needs one extra vector for CQP just like MISC.*/
8562 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8563 }
8564
Vasu Dev38e00432014-08-01 13:27:03 -07008565#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008566 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008567
8568#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008569#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f12014-12-11 07:06:34 +00008570 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008571 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8572 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8573 pf->num_req_vfs = min_t(int,
8574 pf->hw.func_caps.num_vfs,
8575 I40E_MAX_VF_COUNT);
8576 }
8577#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008578 if (pf->hw.mac.type == I40E_MAC_X722) {
8579 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8580 I40E_FLAG_128_QP_RSS_CAPABLE |
8581 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8582 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8583 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008584 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008585 I40E_FLAG_NO_PCI_LINK_CHECK |
Catherine Sullivanf8db54cc2015-12-22 14:25:14 -08008586 I40E_FLAG_100M_SGMII_CAPABLE |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008587 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008588 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008589 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8590 ((pf->hw.aq.api_maj_ver == 1) &&
8591 (pf->hw.aq.api_min_ver > 4))) {
8592 /* Supported in FW API version higher than 1.4 */
8593 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008594 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8595 } else {
8596 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008597 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008598
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008599 pf->eeprom_version = 0xDEAD;
8600 pf->lan_veb = I40E_NO_VEB;
8601 pf->lan_vsi = I40E_NO_VSI;
8602
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008603 /* By default FW has this off for performance reasons */
8604 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008606 /* set up queue assignment tracking */
8607 size = sizeof(struct i40e_lump_tracking)
8608 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8609 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8610 if (!pf->qp_pile) {
8611 err = -ENOMEM;
8612 goto sw_init_done;
8613 }
8614 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8615 pf->qp_pile->search_hint = 0;
8616
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008617 pf->tx_timeout_recovery_level = 1;
8618
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008619 mutex_init(&pf->switch_mutex);
8620
Greg Rosec668a122015-02-26 16:10:39 +00008621 /* If NPAR is enabled nudge the Tx scheduler */
8622 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8623 i40e_set_npar_bw_setting(pf);
8624
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008625sw_init_done:
8626 return err;
8627}
8628
8629/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008630 * i40e_set_ntuple - set the ntuple feature flag and take action
8631 * @pf: board private structure to initialize
8632 * @features: the feature set that the stack is suggesting
8633 *
8634 * returns a bool to indicate if reset needs to happen
8635 **/
8636bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8637{
8638 bool need_reset = false;
8639
8640 /* Check if Flow Director n-tuple support was enabled or disabled. If
8641 * the state changed, we need to reset.
8642 */
8643 if (features & NETIF_F_NTUPLE) {
8644 /* Enable filters and mark for reset */
8645 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8646 need_reset = true;
8647 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8648 } else {
8649 /* turn off filters, mark for reset and clear SW filter list */
8650 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8651 need_reset = true;
8652 i40e_fdir_filter_exit(pf);
8653 }
8654 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008655 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008656 /* reset fd counters */
8657 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8658 pf->fdir_pf_active_filters = 0;
8659 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008660 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8661 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008662 /* if ATR was auto disabled it can be re-enabled. */
8663 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8664 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8665 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008666 }
8667 return need_reset;
8668}
8669
8670/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008671 * i40e_set_features - set the netdev feature flags
8672 * @netdev: ptr to the netdev being adjusted
8673 * @features: the feature set that the stack is suggesting
8674 **/
8675static int i40e_set_features(struct net_device *netdev,
8676 netdev_features_t features)
8677{
8678 struct i40e_netdev_priv *np = netdev_priv(netdev);
8679 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008680 struct i40e_pf *pf = vsi->back;
8681 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008682
8683 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8684 i40e_vlan_stripping_enable(vsi);
8685 else
8686 i40e_vlan_stripping_disable(vsi);
8687
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008688 need_reset = i40e_set_ntuple(pf, features);
8689
8690 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008691 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008692
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008693 return 0;
8694}
8695
Singhai, Anjali6a899022015-12-14 12:21:18 -08008696#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008697/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008698 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008699 * @pf: board private structure
8700 * @port: The UDP port to look up
8701 *
8702 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8703 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008704static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008705{
8706 u8 i;
8707
8708 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008709 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008710 return i;
8711 }
8712
8713 return i;
8714}
8715
Singhai, Anjali6a899022015-12-14 12:21:18 -08008716#endif
Eric Dumazet5cae7612016-01-20 19:53:02 -08008717
8718#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008719/**
8720 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8721 * @netdev: This physical port's netdev
8722 * @sa_family: Socket Family that VXLAN is notifying us about
8723 * @port: New UDP port number that VXLAN started listening to
8724 **/
8725static void i40e_add_vxlan_port(struct net_device *netdev,
8726 sa_family_t sa_family, __be16 port)
8727{
8728 struct i40e_netdev_priv *np = netdev_priv(netdev);
8729 struct i40e_vsi *vsi = np->vsi;
8730 struct i40e_pf *pf = vsi->back;
8731 u8 next_idx;
8732 u8 idx;
8733
Singhai, Anjali6a899022015-12-14 12:21:18 -08008734 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008735
8736 /* Check if port already exists */
8737 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008738 netdev_info(netdev, "vxlan port %d already offloaded\n",
8739 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008740 return;
8741 }
8742
8743 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008744 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008745
8746 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008747 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008748 ntohs(port));
8749 return;
8750 }
8751
8752 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008753 pf->udp_ports[next_idx].index = port;
8754 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8755 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8756 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008757}
8758
8759/**
8760 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8761 * @netdev: This physical port's netdev
8762 * @sa_family: Socket Family that VXLAN is notifying us about
8763 * @port: UDP port number that VXLAN stopped listening to
8764 **/
8765static void i40e_del_vxlan_port(struct net_device *netdev,
8766 sa_family_t sa_family, __be16 port)
8767{
8768 struct i40e_netdev_priv *np = netdev_priv(netdev);
8769 struct i40e_vsi *vsi = np->vsi;
8770 struct i40e_pf *pf = vsi->back;
8771 u8 idx;
8772
Singhai, Anjali6a899022015-12-14 12:21:18 -08008773 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008774
8775 /* Check if port already exists */
8776 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8777 /* if port exists, set it to 0 (mark for deletion)
8778 * and make it pending
8779 */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008780 pf->udp_ports[idx].index = 0;
8781 pf->pending_udp_bitmap |= BIT_ULL(idx);
8782 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008783 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008784 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008785 ntohs(port));
8786 }
8787}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008788#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008789
Eric Dumazet5cae7612016-01-20 19:53:02 -08008790#if IS_ENABLED(CONFIG_GENEVE)
Singhai, Anjali6a899022015-12-14 12:21:18 -08008791/**
8792 * i40e_add_geneve_port - Get notifications about GENEVE ports that come up
8793 * @netdev: This physical port's netdev
8794 * @sa_family: Socket Family that GENEVE is notifying us about
8795 * @port: New UDP port number that GENEVE started listening to
8796 **/
8797static void i40e_add_geneve_port(struct net_device *netdev,
8798 sa_family_t sa_family, __be16 port)
8799{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008800 struct i40e_netdev_priv *np = netdev_priv(netdev);
8801 struct i40e_vsi *vsi = np->vsi;
8802 struct i40e_pf *pf = vsi->back;
8803 u8 next_idx;
8804 u8 idx;
8805
Anjali Singhaia340c782016-01-06 11:49:28 -08008806 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8807 return;
8808
Singhai, Anjali6a899022015-12-14 12:21:18 -08008809 idx = i40e_get_udp_port_idx(pf, port);
8810
8811 /* Check if port already exists */
8812 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8813 netdev_info(netdev, "udp port %d already offloaded\n",
8814 ntohs(port));
8815 return;
8816 }
8817
8818 /* Now check if there is space to add the new port */
8819 next_idx = i40e_get_udp_port_idx(pf, 0);
8820
8821 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8822 netdev_info(netdev, "maximum number of UDP ports reached, not adding port %d\n",
8823 ntohs(port));
8824 return;
8825 }
8826
8827 /* New port: add it and mark its index in the bitmap */
8828 pf->udp_ports[next_idx].index = port;
8829 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8830 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8831 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8832
8833 dev_info(&pf->pdev->dev, "adding geneve port %d\n", ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008834}
8835
8836/**
8837 * i40e_del_geneve_port - Get notifications about GENEVE ports that go away
8838 * @netdev: This physical port's netdev
8839 * @sa_family: Socket Family that GENEVE is notifying us about
8840 * @port: UDP port number that GENEVE stopped listening to
8841 **/
8842static void i40e_del_geneve_port(struct net_device *netdev,
8843 sa_family_t sa_family, __be16 port)
8844{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008845 struct i40e_netdev_priv *np = netdev_priv(netdev);
8846 struct i40e_vsi *vsi = np->vsi;
8847 struct i40e_pf *pf = vsi->back;
8848 u8 idx;
8849
Anjali Singhaia340c782016-01-06 11:49:28 -08008850 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8851 return;
8852
Singhai, Anjali6a899022015-12-14 12:21:18 -08008853 idx = i40e_get_udp_port_idx(pf, port);
8854
8855 /* Check if port already exists */
8856 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8857 /* if port exists, set it to 0 (mark for deletion)
8858 * and make it pending
8859 */
8860 pf->udp_ports[idx].index = 0;
8861 pf->pending_udp_bitmap |= BIT_ULL(idx);
8862 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8863
8864 dev_info(&pf->pdev->dev, "deleting geneve port %d\n",
8865 ntohs(port));
8866 } else {
8867 netdev_warn(netdev, "geneve port %d was not found, not deleting\n",
8868 ntohs(port));
8869 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08008870}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008871#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008872
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008873static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008874 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008875{
8876 struct i40e_netdev_priv *np = netdev_priv(netdev);
8877 struct i40e_pf *pf = np->vsi->back;
8878 struct i40e_hw *hw = &pf->hw;
8879
8880 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8881 return -EOPNOTSUPP;
8882
8883 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8884 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8885
8886 return 0;
8887}
8888
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008889/**
8890 * i40e_ndo_fdb_add - add an entry to the hardware database
8891 * @ndm: the input from the stack
8892 * @tb: pointer to array of nladdr (unused)
8893 * @dev: the net device pointer
8894 * @addr: the MAC address entry being added
8895 * @flags: instructions from stack about fdb operation
8896 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008897static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8898 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008899 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008900 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008901{
8902 struct i40e_netdev_priv *np = netdev_priv(dev);
8903 struct i40e_pf *pf = np->vsi->back;
8904 int err = 0;
8905
8906 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8907 return -EOPNOTSUPP;
8908
Or Gerlitz65891fe2014-12-14 18:19:05 +02008909 if (vid) {
8910 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8911 return -EINVAL;
8912 }
8913
Greg Rose4ba0dea2014-03-06 08:59:55 +00008914 /* Hardware does not support aging addresses so if a
8915 * ndm_state is given only allow permanent addresses
8916 */
8917 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8918 netdev_info(dev, "FDB only supports static addresses\n");
8919 return -EINVAL;
8920 }
8921
8922 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8923 err = dev_uc_add_excl(dev, addr);
8924 else if (is_multicast_ether_addr(addr))
8925 err = dev_mc_add_excl(dev, addr);
8926 else
8927 err = -EINVAL;
8928
8929 /* Only return duplicate errors if NLM_F_EXCL is set */
8930 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8931 err = 0;
8932
8933 return err;
8934}
8935
Neerav Parikh51616012015-02-06 08:52:14 +00008936/**
8937 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8938 * @dev: the netdev being configured
8939 * @nlh: RTNL message
8940 *
8941 * Inserts a new hardware bridge if not already created and
8942 * enables the bridging mode requested (VEB or VEPA). If the
8943 * hardware bridge has already been inserted and the request
8944 * is to change the mode then that requires a PF reset to
8945 * allow rebuild of the components with required hardware
8946 * bridge mode enabled.
8947 **/
8948static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008949 struct nlmsghdr *nlh,
8950 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008951{
8952 struct i40e_netdev_priv *np = netdev_priv(dev);
8953 struct i40e_vsi *vsi = np->vsi;
8954 struct i40e_pf *pf = vsi->back;
8955 struct i40e_veb *veb = NULL;
8956 struct nlattr *attr, *br_spec;
8957 int i, rem;
8958
8959 /* Only for PF VSI for now */
8960 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8961 return -EOPNOTSUPP;
8962
8963 /* Find the HW bridge for PF VSI */
8964 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8965 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8966 veb = pf->veb[i];
8967 }
8968
8969 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8970
8971 nla_for_each_nested(attr, br_spec, rem) {
8972 __u16 mode;
8973
8974 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8975 continue;
8976
8977 mode = nla_get_u16(attr);
8978 if ((mode != BRIDGE_MODE_VEPA) &&
8979 (mode != BRIDGE_MODE_VEB))
8980 return -EINVAL;
8981
8982 /* Insert a new HW bridge */
8983 if (!veb) {
8984 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8985 vsi->tc_config.enabled_tc);
8986 if (veb) {
8987 veb->bridge_mode = mode;
8988 i40e_config_bridge_mode(veb);
8989 } else {
8990 /* No Bridge HW offload available */
8991 return -ENOENT;
8992 }
8993 break;
8994 } else if (mode != veb->bridge_mode) {
8995 /* Existing HW bridge but different mode needs reset */
8996 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008997 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8998 if (mode == BRIDGE_MODE_VEB)
8999 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9000 else
9001 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9002 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009003 break;
9004 }
9005 }
9006
9007 return 0;
9008}
9009
9010/**
9011 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9012 * @skb: skb buff
9013 * @pid: process id
9014 * @seq: RTNL message seq #
9015 * @dev: the netdev being configured
9016 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009017 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009018 *
9019 * Return the mode in which the hardware bridge is operating in
9020 * i.e VEB or VEPA.
9021 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009022static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9023 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009024 u32 __always_unused filter_mask,
9025 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009026{
9027 struct i40e_netdev_priv *np = netdev_priv(dev);
9028 struct i40e_vsi *vsi = np->vsi;
9029 struct i40e_pf *pf = vsi->back;
9030 struct i40e_veb *veb = NULL;
9031 int i;
9032
9033 /* Only for PF VSI for now */
9034 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9035 return -EOPNOTSUPP;
9036
9037 /* Find the HW bridge for the PF VSI */
9038 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9039 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9040 veb = pf->veb[i];
9041 }
9042
9043 if (!veb)
9044 return 0;
9045
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009046 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07009047 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009048}
Neerav Parikh51616012015-02-06 08:52:14 +00009049
Singhai, Anjali6a899022015-12-14 12:21:18 -08009050/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9051 * inner mac plus all inner ethertypes.
9052 */
9053#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009054/**
9055 * i40e_features_check - Validate encapsulated packet conforms to limits
9056 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009057 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009058 * @features: Offload features that the stack believes apply
9059 **/
9060static netdev_features_t i40e_features_check(struct sk_buff *skb,
9061 struct net_device *dev,
9062 netdev_features_t features)
9063{
9064 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009065 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009066 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009067 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009068
9069 return features;
9070}
9071
Shannon Nelson37a29732015-02-27 09:15:19 +00009072static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009073 .ndo_open = i40e_open,
9074 .ndo_stop = i40e_close,
9075 .ndo_start_xmit = i40e_lan_xmit_frame,
9076 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9077 .ndo_set_rx_mode = i40e_set_rx_mode,
9078 .ndo_validate_addr = eth_validate_addr,
9079 .ndo_set_mac_address = i40e_set_mac,
9080 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009081 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009082 .ndo_tx_timeout = i40e_tx_timeout,
9083 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9084 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9085#ifdef CONFIG_NET_POLL_CONTROLLER
9086 .ndo_poll_controller = i40e_netpoll,
9087#endif
John Fastabende4c67342016-02-16 21:16:15 -08009088 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009089#ifdef I40E_FCOE
9090 .ndo_fcoe_enable = i40e_fcoe_enable,
9091 .ndo_fcoe_disable = i40e_fcoe_disable,
9092#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009093 .ndo_set_features = i40e_set_features,
9094 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9095 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009096 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009097 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009098 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009099 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009100 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Singhai, Anjali6a899022015-12-14 12:21:18 -08009101#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009102 .ndo_add_vxlan_port = i40e_add_vxlan_port,
9103 .ndo_del_vxlan_port = i40e_del_vxlan_port,
9104#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08009105#if IS_ENABLED(CONFIG_GENEVE)
9106 .ndo_add_geneve_port = i40e_add_geneve_port,
9107 .ndo_del_geneve_port = i40e_del_geneve_port,
9108#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009109 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009110 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009111 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009112 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9113 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009114};
9115
9116/**
9117 * i40e_config_netdev - Setup the netdev flags
9118 * @vsi: the VSI being configured
9119 *
9120 * Returns 0 on success, negative value on failure
9121 **/
9122static int i40e_config_netdev(struct i40e_vsi *vsi)
9123{
Greg Rose1a103702013-11-28 06:42:39 +00009124 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009125 struct i40e_pf *pf = vsi->back;
9126 struct i40e_hw *hw = &pf->hw;
9127 struct i40e_netdev_priv *np;
9128 struct net_device *netdev;
9129 u8 mac_addr[ETH_ALEN];
9130 int etherdev_size;
9131
9132 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009133 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009134 if (!netdev)
9135 return -ENOMEM;
9136
9137 vsi->netdev = netdev;
9138 np = netdev_priv(netdev);
9139 np->vsi = vsi;
9140
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009141 netdev->hw_enc_features |= NETIF_F_SG |
9142 NETIF_F_IP_CSUM |
9143 NETIF_F_IPV6_CSUM |
9144 NETIF_F_HIGHDMA |
9145 NETIF_F_SOFT_FEATURES |
9146 NETIF_F_TSO |
9147 NETIF_F_TSO_ECN |
9148 NETIF_F_TSO6 |
9149 NETIF_F_GSO_GRE |
Alexander Duyck577389a2016-04-02 00:06:56 -07009150 NETIF_F_GSO_IPIP |
9151 NETIF_F_GSO_SIT |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009152 NETIF_F_GSO_UDP_TUNNEL |
9153 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9154 NETIF_F_SCTP_CRC |
9155 NETIF_F_RXHASH |
9156 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009157 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009158
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009159 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
9160 netdev->hw_enc_features ^= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9161
9162 /* record features VLANs can make use of */
9163 netdev->vlan_features |= netdev->hw_enc_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009164
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009165 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009166 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009167
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009168 netdev->hw_features |= netdev->hw_enc_features |
9169 NETIF_F_HW_VLAN_CTAG_TX |
9170 NETIF_F_HW_VLAN_CTAG_RX;
9171
9172 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009173
9174 if (vsi->type == I40E_VSI_MAIN) {
9175 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009176 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009177 /* The following steps are necessary to prevent reception
9178 * of tagged packets - some older NVM configurations load a
9179 * default a MAC-VLAN filter that accepts any tagged packet
9180 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00009181 */
Kiran Patil21659032015-09-30 14:09:03 -04009182 if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
9183 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009184 i40e_add_filter(vsi, mac_addr,
9185 I40E_VLAN_ANY, false, true);
Kiran Patil21659032015-09-30 14:09:03 -04009186 spin_unlock_bh(&vsi->mac_filter_list_lock);
9187 }
Anjali Singhai Jain3845cce2016-03-18 12:18:05 -07009188 } else if ((pf->hw.aq.api_maj_ver > 1) ||
9189 ((pf->hw.aq.api_maj_ver == 1) &&
9190 (pf->hw.aq.api_min_ver > 4))) {
9191 /* Supported in FW API version higher than 1.4 */
9192 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
9193 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009194 } else {
9195 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9196 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9197 pf->vsi[pf->lan_vsi]->netdev->name);
9198 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009199
9200 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009201 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009202 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009203 }
Kiran Patil21659032015-09-30 14:09:03 -04009204
9205 spin_lock_bh(&vsi->mac_filter_list_lock);
Greg Rose1a103702013-11-28 06:42:39 +00009206 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009207 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009208
Greg Rose9a173902014-05-22 06:32:02 +00009209 ether_addr_copy(netdev->dev_addr, mac_addr);
9210 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009211
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009212 netdev->priv_flags |= IFF_UNICAST_FLT;
9213 netdev->priv_flags |= IFF_SUPP_NOFCS;
9214 /* Setup netdev TC information */
9215 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9216
9217 netdev->netdev_ops = &i40e_netdev_ops;
9218 netdev->watchdog_timeo = 5 * HZ;
9219 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009220#ifdef I40E_FCOE
9221 i40e_fcoe_config_netdev(netdev, vsi);
9222#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009223
9224 return 0;
9225}
9226
9227/**
9228 * i40e_vsi_delete - Delete a VSI from the switch
9229 * @vsi: the VSI being removed
9230 *
9231 * Returns 0 on success, negative value on failure
9232 **/
9233static void i40e_vsi_delete(struct i40e_vsi *vsi)
9234{
9235 /* remove default VSI is not allowed */
9236 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9237 return;
9238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009239 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009240}
9241
9242/**
Neerav Parikh51616012015-02-06 08:52:14 +00009243 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9244 * @vsi: the VSI being queried
9245 *
9246 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9247 **/
9248int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9249{
9250 struct i40e_veb *veb;
9251 struct i40e_pf *pf = vsi->back;
9252
9253 /* Uplink is not a bridge so default to VEB */
9254 if (vsi->veb_idx == I40E_NO_VEB)
9255 return 1;
9256
9257 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009258 if (!veb) {
9259 dev_info(&pf->pdev->dev,
9260 "There is no veb associated with the bridge\n");
9261 return -ENOENT;
9262 }
Neerav Parikh51616012015-02-06 08:52:14 +00009263
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009264 /* Uplink is a bridge in VEPA mode */
9265 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9266 return 0;
9267 } else {
9268 /* Uplink is a bridge in VEB mode */
9269 return 1;
9270 }
9271
9272 /* VEPA is now default bridge, so return 0 */
9273 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009274}
9275
9276/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277 * i40e_add_vsi - Add a VSI to the switch
9278 * @vsi: the VSI being configured
9279 *
9280 * This initializes a VSI context depending on the VSI type to be added and
9281 * passes it down to the add_vsi aq command.
9282 **/
9283static int i40e_add_vsi(struct i40e_vsi *vsi)
9284{
9285 int ret = -ENODEV;
Kiran Patil21659032015-09-30 14:09:03 -04009286 u8 laa_macaddr[ETH_ALEN];
9287 bool found_laa_mac_filter = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009288 struct i40e_pf *pf = vsi->back;
9289 struct i40e_hw *hw = &pf->hw;
9290 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009291 struct i40e_mac_filter *f, *ftmp;
9292
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009293 u8 enabled_tc = 0x1; /* TC0 enabled */
9294 int f_count = 0;
9295
9296 memset(&ctxt, 0, sizeof(ctxt));
9297 switch (vsi->type) {
9298 case I40E_VSI_MAIN:
9299 /* The PF's main VSI is already setup as part of the
9300 * device initialization, so we'll not bother with
9301 * the add_vsi call, but we will retrieve the current
9302 * VSI context.
9303 */
9304 ctxt.seid = pf->main_vsi_seid;
9305 ctxt.pf_num = pf->hw.pf_id;
9306 ctxt.vf_num = 0;
9307 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9308 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9309 if (ret) {
9310 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009311 "couldn't get PF vsi config, err %s aq_err %s\n",
9312 i40e_stat_str(&pf->hw, ret),
9313 i40e_aq_str(&pf->hw,
9314 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009315 return -ENOENT;
9316 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009317 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009318 vsi->info.valid_sections = 0;
9319
9320 vsi->seid = ctxt.seid;
9321 vsi->id = ctxt.vsi_number;
9322
9323 enabled_tc = i40e_pf_get_tc_map(pf);
9324
9325 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009326 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9327 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009328 memset(&ctxt, 0, sizeof(ctxt));
9329 ctxt.seid = pf->main_vsi_seid;
9330 ctxt.pf_num = pf->hw.pf_id;
9331 ctxt.vf_num = 0;
9332 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9333 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9334 if (ret) {
9335 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009336 "update vsi failed, err %s aq_err %s\n",
9337 i40e_stat_str(&pf->hw, ret),
9338 i40e_aq_str(&pf->hw,
9339 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009340 ret = -ENOENT;
9341 goto err;
9342 }
9343 /* update the local VSI info queue map */
9344 i40e_vsi_update_queue_map(vsi, &ctxt);
9345 vsi->info.valid_sections = 0;
9346 } else {
9347 /* Default/Main VSI is only enabled for TC0
9348 * reconfigure it to enable all TCs that are
9349 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009350 * For MFP case the iSCSI PF would use this
9351 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009352 */
9353 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9354 if (ret) {
9355 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009356 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9357 enabled_tc,
9358 i40e_stat_str(&pf->hw, ret),
9359 i40e_aq_str(&pf->hw,
9360 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009361 ret = -ENOENT;
9362 }
9363 }
9364 break;
9365
9366 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009367 ctxt.pf_num = hw->pf_id;
9368 ctxt.vf_num = 0;
9369 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009370 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009371 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009372 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9373 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009374 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009375 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009376 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009377 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009378 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009379 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009380 break;
9381
9382 case I40E_VSI_VMDQ2:
9383 ctxt.pf_num = hw->pf_id;
9384 ctxt.vf_num = 0;
9385 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009386 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009387 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009389 /* This VSI is connected to VEB so the switch_id
9390 * should be set to zero by default.
9391 */
Neerav Parikh51616012015-02-06 08:52:14 +00009392 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9393 ctxt.info.valid_sections |=
9394 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9395 ctxt.info.switch_id =
9396 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9397 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009398
9399 /* Setup the VSI tx/rx queue map for TC0 only for now */
9400 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9401 break;
9402
9403 case I40E_VSI_SRIOV:
9404 ctxt.pf_num = hw->pf_id;
9405 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9406 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009407 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009408 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009410 /* This VSI is connected to VEB so the switch_id
9411 * should be set to zero by default.
9412 */
Neerav Parikh51616012015-02-06 08:52:14 +00009413 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9414 ctxt.info.valid_sections |=
9415 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9416 ctxt.info.switch_id =
9417 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9418 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009419
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009420 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9421 ctxt.info.valid_sections |=
9422 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9423 ctxt.info.queueing_opt_flags |=
9424 I40E_AQ_VSI_QUE_OPT_TCP_ENA;
9425 }
9426
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009427 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9428 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009429 if (pf->vf[vsi->vf_id].spoofchk) {
9430 ctxt.info.valid_sections |=
9431 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9432 ctxt.info.sec_flags |=
9433 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9434 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9435 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436 /* Setup the VSI tx/rx queue map for TC0 only for now */
9437 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9438 break;
9439
Vasu Dev38e00432014-08-01 13:27:03 -07009440#ifdef I40E_FCOE
9441 case I40E_VSI_FCOE:
9442 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9443 if (ret) {
9444 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9445 return ret;
9446 }
9447 break;
9448
9449#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009450 case I40E_VSI_IWARP:
9451 /* send down message to iWARP */
9452 break;
9453
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009454 default:
9455 return -ENODEV;
9456 }
9457
9458 if (vsi->type != I40E_VSI_MAIN) {
9459 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9460 if (ret) {
9461 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009462 "add vsi failed, err %s aq_err %s\n",
9463 i40e_stat_str(&pf->hw, ret),
9464 i40e_aq_str(&pf->hw,
9465 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009466 ret = -ENOENT;
9467 goto err;
9468 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009469 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009470 vsi->info.valid_sections = 0;
9471 vsi->seid = ctxt.seid;
9472 vsi->id = ctxt.vsi_number;
9473 }
9474
Kiran Patil21659032015-09-30 14:09:03 -04009475 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009476 /* If macvlan filters already exist, force them to get loaded */
9477 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
9478 f->changed = true;
9479 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009480
Kiran Patil21659032015-09-30 14:09:03 -04009481 /* Expected to have only one MAC filter entry for LAA in list */
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009482 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Kiran Patil21659032015-09-30 14:09:03 -04009483 ether_addr_copy(laa_macaddr, f->macaddr);
9484 found_laa_mac_filter = true;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009485 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009486 }
Kiran Patil21659032015-09-30 14:09:03 -04009487 spin_unlock_bh(&vsi->mac_filter_list_lock);
9488
9489 if (found_laa_mac_filter) {
9490 struct i40e_aqc_remove_macvlan_element_data element;
9491
9492 memset(&element, 0, sizeof(element));
9493 ether_addr_copy(element.mac_addr, laa_macaddr);
9494 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
9495 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
9496 &element, 1, NULL);
9497 if (ret) {
9498 /* some older FW has a different default */
9499 element.flags |=
9500 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
9501 i40e_aq_remove_macvlan(hw, vsi->seid,
9502 &element, 1, NULL);
9503 }
9504
9505 i40e_aq_mac_address_write(hw,
9506 I40E_AQC_WRITE_TYPE_LAA_WOL,
9507 laa_macaddr, NULL);
9508 }
9509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009510 if (f_count) {
9511 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9512 pf->flags |= I40E_FLAG_FILTER_SYNC;
9513 }
9514
9515 /* Update VSI BW information */
9516 ret = i40e_vsi_get_bw_info(vsi);
9517 if (ret) {
9518 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009519 "couldn't get vsi bw info, err %s aq_err %s\n",
9520 i40e_stat_str(&pf->hw, ret),
9521 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009522 /* VSI is already added so not tearing that up */
9523 ret = 0;
9524 }
9525
9526err:
9527 return ret;
9528}
9529
9530/**
9531 * i40e_vsi_release - Delete a VSI and free its resources
9532 * @vsi: the VSI being removed
9533 *
9534 * Returns 0 on success or < 0 on error
9535 **/
9536int i40e_vsi_release(struct i40e_vsi *vsi)
9537{
9538 struct i40e_mac_filter *f, *ftmp;
9539 struct i40e_veb *veb = NULL;
9540 struct i40e_pf *pf;
9541 u16 uplink_seid;
9542 int i, n;
9543
9544 pf = vsi->back;
9545
9546 /* release of a VEB-owner or last VSI is not allowed */
9547 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9548 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9549 vsi->seid, vsi->uplink_seid);
9550 return -ENODEV;
9551 }
9552 if (vsi == pf->vsi[pf->lan_vsi] &&
9553 !test_bit(__I40E_DOWN, &pf->state)) {
9554 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9555 return -ENODEV;
9556 }
9557
9558 uplink_seid = vsi->uplink_seid;
9559 if (vsi->type != I40E_VSI_SRIOV) {
9560 if (vsi->netdev_registered) {
9561 vsi->netdev_registered = false;
9562 if (vsi->netdev) {
9563 /* results in a call to i40e_close() */
9564 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009565 }
9566 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009567 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009568 }
9569 i40e_vsi_disable_irq(vsi);
9570 }
9571
Kiran Patil21659032015-09-30 14:09:03 -04009572 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009573 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9574 i40e_del_filter(vsi, f->macaddr, f->vlan,
9575 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009576 spin_unlock_bh(&vsi->mac_filter_list_lock);
9577
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009578 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009579
9580 i40e_vsi_delete(vsi);
9581 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009582 if (vsi->netdev) {
9583 free_netdev(vsi->netdev);
9584 vsi->netdev = NULL;
9585 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009586 i40e_vsi_clear_rings(vsi);
9587 i40e_vsi_clear(vsi);
9588
9589 /* If this was the last thing on the VEB, except for the
9590 * controlling VSI, remove the VEB, which puts the controlling
9591 * VSI onto the next level down in the switch.
9592 *
9593 * Well, okay, there's one more exception here: don't remove
9594 * the orphan VEBs yet. We'll wait for an explicit remove request
9595 * from up the network stack.
9596 */
Mitch Williams505682c2014-05-20 08:01:37 +00009597 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009598 if (pf->vsi[i] &&
9599 pf->vsi[i]->uplink_seid == uplink_seid &&
9600 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9601 n++; /* count the VSIs */
9602 }
9603 }
9604 for (i = 0; i < I40E_MAX_VEB; i++) {
9605 if (!pf->veb[i])
9606 continue;
9607 if (pf->veb[i]->uplink_seid == uplink_seid)
9608 n++; /* count the VEBs */
9609 if (pf->veb[i]->seid == uplink_seid)
9610 veb = pf->veb[i];
9611 }
9612 if (n == 0 && veb && veb->uplink_seid != 0)
9613 i40e_veb_release(veb);
9614
9615 return 0;
9616}
9617
9618/**
9619 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9620 * @vsi: ptr to the VSI
9621 *
9622 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9623 * corresponding SW VSI structure and initializes num_queue_pairs for the
9624 * newly allocated VSI.
9625 *
9626 * Returns 0 on success or negative on failure
9627 **/
9628static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9629{
9630 int ret = -ENOENT;
9631 struct i40e_pf *pf = vsi->back;
9632
Alexander Duyck493fb302013-09-28 07:01:44 +00009633 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009634 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9635 vsi->seid);
9636 return -EEXIST;
9637 }
9638
9639 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009640 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009641 vsi->seid, vsi->base_vector);
9642 return -EEXIST;
9643 }
9644
Greg Rose90e04072014-03-06 08:59:57 +00009645 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009646 if (ret) {
9647 dev_info(&pf->pdev->dev,
9648 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9649 vsi->num_q_vectors, vsi->seid, ret);
9650 vsi->num_q_vectors = 0;
9651 goto vector_setup_out;
9652 }
9653
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009654 /* In Legacy mode, we do not have to get any other vector since we
9655 * piggyback on the misc/ICR0 for queue interrupts.
9656 */
9657 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9658 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009659 if (vsi->num_q_vectors)
9660 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9661 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009662 if (vsi->base_vector < 0) {
9663 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009664 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9665 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009666 i40e_vsi_free_q_vectors(vsi);
9667 ret = -ENOENT;
9668 goto vector_setup_out;
9669 }
9670
9671vector_setup_out:
9672 return ret;
9673}
9674
9675/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009676 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9677 * @vsi: pointer to the vsi.
9678 *
9679 * This re-allocates a vsi's queue resources.
9680 *
9681 * Returns pointer to the successfully allocated and configured VSI sw struct
9682 * on success, otherwise returns NULL on failure.
9683 **/
9684static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9685{
John Underwoodf5340392016-02-18 09:19:24 -08009686 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009687 u8 enabled_tc;
9688 int ret;
9689
John Underwoodf5340392016-02-18 09:19:24 -08009690 if (!vsi)
9691 return NULL;
9692
9693 pf = vsi->back;
9694
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009695 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9696 i40e_vsi_clear_rings(vsi);
9697
9698 i40e_vsi_free_arrays(vsi, false);
9699 i40e_set_num_rings_in_vsi(vsi);
9700 ret = i40e_vsi_alloc_arrays(vsi, false);
9701 if (ret)
9702 goto err_vsi;
9703
9704 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9705 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009706 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009707 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009708 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009709 goto err_vsi;
9710 }
9711 vsi->base_queue = ret;
9712
9713 /* Update the FW view of the VSI. Force a reset of TC and queue
9714 * layout configurations.
9715 */
9716 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9717 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9718 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9719 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9720
9721 /* assign it some queues */
9722 ret = i40e_alloc_rings(vsi);
9723 if (ret)
9724 goto err_rings;
9725
9726 /* map all of the rings to the q_vectors */
9727 i40e_vsi_map_rings_to_vectors(vsi);
9728 return vsi;
9729
9730err_rings:
9731 i40e_vsi_free_q_vectors(vsi);
9732 if (vsi->netdev_registered) {
9733 vsi->netdev_registered = false;
9734 unregister_netdev(vsi->netdev);
9735 free_netdev(vsi->netdev);
9736 vsi->netdev = NULL;
9737 }
9738 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9739err_vsi:
9740 i40e_vsi_clear(vsi);
9741 return NULL;
9742}
9743
9744/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009745 * i40e_macaddr_init - explicitly write the mac address filters.
9746 *
9747 * @vsi: pointer to the vsi.
9748 * @macaddr: the MAC address
9749 *
9750 * This is needed when the macaddr has been obtained by other
9751 * means than the default, e.g., from Open Firmware or IDPROM.
9752 * Returns 0 on success, negative on failure
9753 **/
9754static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
9755{
9756 int ret;
9757 struct i40e_aqc_add_macvlan_element_data element;
9758
9759 ret = i40e_aq_mac_address_write(&vsi->back->hw,
9760 I40E_AQC_WRITE_TYPE_LAA_WOL,
9761 macaddr, NULL);
9762 if (ret) {
9763 dev_info(&vsi->back->pdev->dev,
9764 "Addr change for VSI failed: %d\n", ret);
9765 return -EADDRNOTAVAIL;
9766 }
9767
9768 memset(&element, 0, sizeof(element));
9769 ether_addr_copy(element.mac_addr, macaddr);
9770 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
9771 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
9772 if (ret) {
9773 dev_info(&vsi->back->pdev->dev,
9774 "add filter failed err %s aq_err %s\n",
9775 i40e_stat_str(&vsi->back->hw, ret),
9776 i40e_aq_str(&vsi->back->hw,
9777 vsi->back->hw.aq.asq_last_status));
9778 }
9779 return ret;
9780}
9781
9782/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009783 * i40e_vsi_setup - Set up a VSI by a given type
9784 * @pf: board private structure
9785 * @type: VSI type
9786 * @uplink_seid: the switch element to link to
9787 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9788 *
9789 * This allocates the sw VSI structure and its queue resources, then add a VSI
9790 * to the identified VEB.
9791 *
9792 * Returns pointer to the successfully allocated and configure VSI sw struct on
9793 * success, otherwise returns NULL on failure.
9794 **/
9795struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9796 u16 uplink_seid, u32 param1)
9797{
9798 struct i40e_vsi *vsi = NULL;
9799 struct i40e_veb *veb = NULL;
9800 int ret, i;
9801 int v_idx;
9802
9803 /* The requested uplink_seid must be either
9804 * - the PF's port seid
9805 * no VEB is needed because this is the PF
9806 * or this is a Flow Director special case VSI
9807 * - seid of an existing VEB
9808 * - seid of a VSI that owns an existing VEB
9809 * - seid of a VSI that doesn't own a VEB
9810 * a new VEB is created and the VSI becomes the owner
9811 * - seid of the PF VSI, which is what creates the first VEB
9812 * this is a special case of the previous
9813 *
9814 * Find which uplink_seid we were given and create a new VEB if needed
9815 */
9816 for (i = 0; i < I40E_MAX_VEB; i++) {
9817 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9818 veb = pf->veb[i];
9819 break;
9820 }
9821 }
9822
9823 if (!veb && uplink_seid != pf->mac_seid) {
9824
Mitch Williams505682c2014-05-20 08:01:37 +00009825 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9827 vsi = pf->vsi[i];
9828 break;
9829 }
9830 }
9831 if (!vsi) {
9832 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9833 uplink_seid);
9834 return NULL;
9835 }
9836
9837 if (vsi->uplink_seid == pf->mac_seid)
9838 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9839 vsi->tc_config.enabled_tc);
9840 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9841 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9842 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009843 if (veb) {
9844 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9845 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009846 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009847 return NULL;
9848 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009849 /* We come up by default in VEPA mode if SRIOV is not
9850 * already enabled, in which case we can't force VEPA
9851 * mode.
9852 */
9853 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9854 veb->bridge_mode = BRIDGE_MODE_VEPA;
9855 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9856 }
Neerav Parikh51616012015-02-06 08:52:14 +00009857 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009858 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009859 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9860 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9861 veb = pf->veb[i];
9862 }
9863 if (!veb) {
9864 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9865 return NULL;
9866 }
9867
9868 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9869 uplink_seid = veb->seid;
9870 }
9871
9872 /* get vsi sw struct */
9873 v_idx = i40e_vsi_mem_alloc(pf, type);
9874 if (v_idx < 0)
9875 goto err_alloc;
9876 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009877 if (!vsi)
9878 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009879 vsi->type = type;
9880 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9881
9882 if (type == I40E_VSI_MAIN)
9883 pf->lan_vsi = v_idx;
9884 else if (type == I40E_VSI_SRIOV)
9885 vsi->vf_id = param1;
9886 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009887 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9888 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009889 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009890 dev_info(&pf->pdev->dev,
9891 "failed to get tracking for %d queues for VSI %d err=%d\n",
9892 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009893 goto err_vsi;
9894 }
9895 vsi->base_queue = ret;
9896
9897 /* get a VSI from the hardware */
9898 vsi->uplink_seid = uplink_seid;
9899 ret = i40e_add_vsi(vsi);
9900 if (ret)
9901 goto err_vsi;
9902
9903 switch (vsi->type) {
9904 /* setup the netdev if needed */
9905 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009906 /* Apply relevant filters if a platform-specific mac
9907 * address was selected.
9908 */
9909 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9910 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9911 if (ret) {
9912 dev_warn(&pf->pdev->dev,
9913 "could not set up macaddr; err %d\n",
9914 ret);
9915 }
9916 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009917 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009918 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009919 ret = i40e_config_netdev(vsi);
9920 if (ret)
9921 goto err_netdev;
9922 ret = register_netdev(vsi->netdev);
9923 if (ret)
9924 goto err_netdev;
9925 vsi->netdev_registered = true;
9926 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009927#ifdef CONFIG_I40E_DCB
9928 /* Setup DCB netlink interface */
9929 i40e_dcbnl_setup(vsi);
9930#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009931 /* fall through */
9932
9933 case I40E_VSI_FDIR:
9934 /* set up vectors and rings if needed */
9935 ret = i40e_vsi_setup_vectors(vsi);
9936 if (ret)
9937 goto err_msix;
9938
9939 ret = i40e_alloc_rings(vsi);
9940 if (ret)
9941 goto err_rings;
9942
9943 /* map all of the rings to the q_vectors */
9944 i40e_vsi_map_rings_to_vectors(vsi);
9945
9946 i40e_vsi_reset_stats(vsi);
9947 break;
9948
9949 default:
9950 /* no netdev or rings for the other VSI types */
9951 break;
9952 }
9953
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04009954 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9955 (vsi->type == I40E_VSI_VMDQ2)) {
9956 ret = i40e_vsi_config_rss(vsi);
9957 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009958 return vsi;
9959
9960err_rings:
9961 i40e_vsi_free_q_vectors(vsi);
9962err_msix:
9963 if (vsi->netdev_registered) {
9964 vsi->netdev_registered = false;
9965 unregister_netdev(vsi->netdev);
9966 free_netdev(vsi->netdev);
9967 vsi->netdev = NULL;
9968 }
9969err_netdev:
9970 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9971err_vsi:
9972 i40e_vsi_clear(vsi);
9973err_alloc:
9974 return NULL;
9975}
9976
9977/**
9978 * i40e_veb_get_bw_info - Query VEB BW information
9979 * @veb: the veb to query
9980 *
9981 * Query the Tx scheduler BW configuration data for given VEB
9982 **/
9983static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9984{
9985 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9986 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9987 struct i40e_pf *pf = veb->pf;
9988 struct i40e_hw *hw = &pf->hw;
9989 u32 tc_bw_max;
9990 int ret = 0;
9991 int i;
9992
9993 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9994 &bw_data, NULL);
9995 if (ret) {
9996 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009997 "query veb bw config failed, err %s aq_err %s\n",
9998 i40e_stat_str(&pf->hw, ret),
9999 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010000 goto out;
10001 }
10002
10003 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10004 &ets_data, NULL);
10005 if (ret) {
10006 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010007 "query veb bw ets config failed, err %s aq_err %s\n",
10008 i40e_stat_str(&pf->hw, ret),
10009 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010010 goto out;
10011 }
10012
10013 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10014 veb->bw_max_quanta = ets_data.tc_bw_max;
10015 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010016 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010017 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10018 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10019 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10020 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10021 veb->bw_tc_limit_credits[i] =
10022 le16_to_cpu(bw_data.tc_bw_limits[i]);
10023 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10024 }
10025
10026out:
10027 return ret;
10028}
10029
10030/**
10031 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10032 * @pf: board private structure
10033 *
10034 * On error: returns error code (negative)
10035 * On success: returns vsi index in PF (positive)
10036 **/
10037static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10038{
10039 int ret = -ENOENT;
10040 struct i40e_veb *veb;
10041 int i;
10042
10043 /* Need to protect the allocation of switch elements at the PF level */
10044 mutex_lock(&pf->switch_mutex);
10045
10046 /* VEB list may be fragmented if VEB creation/destruction has
10047 * been happening. We can afford to do a quick scan to look
10048 * for any free slots in the list.
10049 *
10050 * find next empty veb slot, looping back around if necessary
10051 */
10052 i = 0;
10053 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10054 i++;
10055 if (i >= I40E_MAX_VEB) {
10056 ret = -ENOMEM;
10057 goto err_alloc_veb; /* out of VEB slots! */
10058 }
10059
10060 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10061 if (!veb) {
10062 ret = -ENOMEM;
10063 goto err_alloc_veb;
10064 }
10065 veb->pf = pf;
10066 veb->idx = i;
10067 veb->enabled_tc = 1;
10068
10069 pf->veb[i] = veb;
10070 ret = i;
10071err_alloc_veb:
10072 mutex_unlock(&pf->switch_mutex);
10073 return ret;
10074}
10075
10076/**
10077 * i40e_switch_branch_release - Delete a branch of the switch tree
10078 * @branch: where to start deleting
10079 *
10080 * This uses recursion to find the tips of the branch to be
10081 * removed, deleting until we get back to and can delete this VEB.
10082 **/
10083static void i40e_switch_branch_release(struct i40e_veb *branch)
10084{
10085 struct i40e_pf *pf = branch->pf;
10086 u16 branch_seid = branch->seid;
10087 u16 veb_idx = branch->idx;
10088 int i;
10089
10090 /* release any VEBs on this VEB - RECURSION */
10091 for (i = 0; i < I40E_MAX_VEB; i++) {
10092 if (!pf->veb[i])
10093 continue;
10094 if (pf->veb[i]->uplink_seid == branch->seid)
10095 i40e_switch_branch_release(pf->veb[i]);
10096 }
10097
10098 /* Release the VSIs on this VEB, but not the owner VSI.
10099 *
10100 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10101 * the VEB itself, so don't use (*branch) after this loop.
10102 */
Mitch Williams505682c2014-05-20 08:01:37 +000010103 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010104 if (!pf->vsi[i])
10105 continue;
10106 if (pf->vsi[i]->uplink_seid == branch_seid &&
10107 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10108 i40e_vsi_release(pf->vsi[i]);
10109 }
10110 }
10111
10112 /* There's one corner case where the VEB might not have been
10113 * removed, so double check it here and remove it if needed.
10114 * This case happens if the veb was created from the debugfs
10115 * commands and no VSIs were added to it.
10116 */
10117 if (pf->veb[veb_idx])
10118 i40e_veb_release(pf->veb[veb_idx]);
10119}
10120
10121/**
10122 * i40e_veb_clear - remove veb struct
10123 * @veb: the veb to remove
10124 **/
10125static void i40e_veb_clear(struct i40e_veb *veb)
10126{
10127 if (!veb)
10128 return;
10129
10130 if (veb->pf) {
10131 struct i40e_pf *pf = veb->pf;
10132
10133 mutex_lock(&pf->switch_mutex);
10134 if (pf->veb[veb->idx] == veb)
10135 pf->veb[veb->idx] = NULL;
10136 mutex_unlock(&pf->switch_mutex);
10137 }
10138
10139 kfree(veb);
10140}
10141
10142/**
10143 * i40e_veb_release - Delete a VEB and free its resources
10144 * @veb: the VEB being removed
10145 **/
10146void i40e_veb_release(struct i40e_veb *veb)
10147{
10148 struct i40e_vsi *vsi = NULL;
10149 struct i40e_pf *pf;
10150 int i, n = 0;
10151
10152 pf = veb->pf;
10153
10154 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010155 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010156 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10157 n++;
10158 vsi = pf->vsi[i];
10159 }
10160 }
10161 if (n != 1) {
10162 dev_info(&pf->pdev->dev,
10163 "can't remove VEB %d with %d VSIs left\n",
10164 veb->seid, n);
10165 return;
10166 }
10167
10168 /* move the remaining VSI to uplink veb */
10169 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10170 if (veb->uplink_seid) {
10171 vsi->uplink_seid = veb->uplink_seid;
10172 if (veb->uplink_seid == pf->mac_seid)
10173 vsi->veb_idx = I40E_NO_VEB;
10174 else
10175 vsi->veb_idx = veb->veb_idx;
10176 } else {
10177 /* floating VEB */
10178 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10179 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10180 }
10181
10182 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10183 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010184}
10185
10186/**
10187 * i40e_add_veb - create the VEB in the switch
10188 * @veb: the VEB to be instantiated
10189 * @vsi: the controlling VSI
10190 **/
10191static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10192{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010193 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -040010194 bool is_default = veb->pf->cur_promisc;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010195 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010196 int ret;
10197
10198 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010199 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +000010200 veb->enabled_tc, is_default,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010201 &veb->seid, enable_stats, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010202 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010203 dev_info(&pf->pdev->dev,
10204 "couldn't add VEB, err %s aq_err %s\n",
10205 i40e_stat_str(&pf->hw, ret),
10206 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010207 return -EPERM;
10208 }
10209
10210 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010211 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010212 &veb->stats_idx, NULL, NULL, NULL);
10213 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010214 dev_info(&pf->pdev->dev,
10215 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10216 i40e_stat_str(&pf->hw, ret),
10217 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010218 return -EPERM;
10219 }
10220 ret = i40e_veb_get_bw_info(veb);
10221 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010222 dev_info(&pf->pdev->dev,
10223 "couldn't get VEB bw info, err %s aq_err %s\n",
10224 i40e_stat_str(&pf->hw, ret),
10225 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10226 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010227 return -ENOENT;
10228 }
10229
10230 vsi->uplink_seid = veb->seid;
10231 vsi->veb_idx = veb->idx;
10232 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10233
10234 return 0;
10235}
10236
10237/**
10238 * i40e_veb_setup - Set up a VEB
10239 * @pf: board private structure
10240 * @flags: VEB setup flags
10241 * @uplink_seid: the switch element to link to
10242 * @vsi_seid: the initial VSI seid
10243 * @enabled_tc: Enabled TC bit-map
10244 *
10245 * This allocates the sw VEB structure and links it into the switch
10246 * It is possible and legal for this to be a duplicate of an already
10247 * existing VEB. It is also possible for both uplink and vsi seids
10248 * to be zero, in order to create a floating VEB.
10249 *
10250 * Returns pointer to the successfully allocated VEB sw struct on
10251 * success, otherwise returns NULL on failure.
10252 **/
10253struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10254 u16 uplink_seid, u16 vsi_seid,
10255 u8 enabled_tc)
10256{
10257 struct i40e_veb *veb, *uplink_veb = NULL;
10258 int vsi_idx, veb_idx;
10259 int ret;
10260
10261 /* if one seid is 0, the other must be 0 to create a floating relay */
10262 if ((uplink_seid == 0 || vsi_seid == 0) &&
10263 (uplink_seid + vsi_seid != 0)) {
10264 dev_info(&pf->pdev->dev,
10265 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10266 uplink_seid, vsi_seid);
10267 return NULL;
10268 }
10269
10270 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010271 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010272 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10273 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010274 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010275 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10276 vsi_seid);
10277 return NULL;
10278 }
10279
10280 if (uplink_seid && uplink_seid != pf->mac_seid) {
10281 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10282 if (pf->veb[veb_idx] &&
10283 pf->veb[veb_idx]->seid == uplink_seid) {
10284 uplink_veb = pf->veb[veb_idx];
10285 break;
10286 }
10287 }
10288 if (!uplink_veb) {
10289 dev_info(&pf->pdev->dev,
10290 "uplink seid %d not found\n", uplink_seid);
10291 return NULL;
10292 }
10293 }
10294
10295 /* get veb sw struct */
10296 veb_idx = i40e_veb_mem_alloc(pf);
10297 if (veb_idx < 0)
10298 goto err_alloc;
10299 veb = pf->veb[veb_idx];
10300 veb->flags = flags;
10301 veb->uplink_seid = uplink_seid;
10302 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10303 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10304
10305 /* create the VEB in the switch */
10306 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10307 if (ret)
10308 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010309 if (vsi_idx == pf->lan_vsi)
10310 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010311
10312 return veb;
10313
10314err_veb:
10315 i40e_veb_clear(veb);
10316err_alloc:
10317 return NULL;
10318}
10319
10320/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010321 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010322 * @pf: board private structure
10323 * @ele: element we are building info from
10324 * @num_reported: total number of elements
10325 * @printconfig: should we print the contents
10326 *
10327 * helper function to assist in extracting a few useful SEID values.
10328 **/
10329static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10330 struct i40e_aqc_switch_config_element_resp *ele,
10331 u16 num_reported, bool printconfig)
10332{
10333 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10334 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10335 u8 element_type = ele->element_type;
10336 u16 seid = le16_to_cpu(ele->seid);
10337
10338 if (printconfig)
10339 dev_info(&pf->pdev->dev,
10340 "type=%d seid=%d uplink=%d downlink=%d\n",
10341 element_type, seid, uplink_seid, downlink_seid);
10342
10343 switch (element_type) {
10344 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10345 pf->mac_seid = seid;
10346 break;
10347 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10348 /* Main VEB? */
10349 if (uplink_seid != pf->mac_seid)
10350 break;
10351 if (pf->lan_veb == I40E_NO_VEB) {
10352 int v;
10353
10354 /* find existing or else empty VEB */
10355 for (v = 0; v < I40E_MAX_VEB; v++) {
10356 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10357 pf->lan_veb = v;
10358 break;
10359 }
10360 }
10361 if (pf->lan_veb == I40E_NO_VEB) {
10362 v = i40e_veb_mem_alloc(pf);
10363 if (v < 0)
10364 break;
10365 pf->lan_veb = v;
10366 }
10367 }
10368
10369 pf->veb[pf->lan_veb]->seid = seid;
10370 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10371 pf->veb[pf->lan_veb]->pf = pf;
10372 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10373 break;
10374 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10375 if (num_reported != 1)
10376 break;
10377 /* This is immediately after a reset so we can assume this is
10378 * the PF's VSI
10379 */
10380 pf->mac_seid = uplink_seid;
10381 pf->pf_seid = downlink_seid;
10382 pf->main_vsi_seid = seid;
10383 if (printconfig)
10384 dev_info(&pf->pdev->dev,
10385 "pf_seid=%d main_vsi_seid=%d\n",
10386 pf->pf_seid, pf->main_vsi_seid);
10387 break;
10388 case I40E_SWITCH_ELEMENT_TYPE_PF:
10389 case I40E_SWITCH_ELEMENT_TYPE_VF:
10390 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10391 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10392 case I40E_SWITCH_ELEMENT_TYPE_PE:
10393 case I40E_SWITCH_ELEMENT_TYPE_PA:
10394 /* ignore these for now */
10395 break;
10396 default:
10397 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10398 element_type, seid);
10399 break;
10400 }
10401}
10402
10403/**
10404 * i40e_fetch_switch_configuration - Get switch config from firmware
10405 * @pf: board private structure
10406 * @printconfig: should we print the contents
10407 *
10408 * Get the current switch configuration from the device and
10409 * extract a few useful SEID values.
10410 **/
10411int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10412{
10413 struct i40e_aqc_get_switch_config_resp *sw_config;
10414 u16 next_seid = 0;
10415 int ret = 0;
10416 u8 *aq_buf;
10417 int i;
10418
10419 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10420 if (!aq_buf)
10421 return -ENOMEM;
10422
10423 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10424 do {
10425 u16 num_reported, num_total;
10426
10427 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10428 I40E_AQ_LARGE_BUF,
10429 &next_seid, NULL);
10430 if (ret) {
10431 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010432 "get switch config failed err %s aq_err %s\n",
10433 i40e_stat_str(&pf->hw, ret),
10434 i40e_aq_str(&pf->hw,
10435 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010436 kfree(aq_buf);
10437 return -ENOENT;
10438 }
10439
10440 num_reported = le16_to_cpu(sw_config->header.num_reported);
10441 num_total = le16_to_cpu(sw_config->header.num_total);
10442
10443 if (printconfig)
10444 dev_info(&pf->pdev->dev,
10445 "header: %d reported %d total\n",
10446 num_reported, num_total);
10447
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010448 for (i = 0; i < num_reported; i++) {
10449 struct i40e_aqc_switch_config_element_resp *ele =
10450 &sw_config->element[i];
10451
10452 i40e_setup_pf_switch_element(pf, ele, num_reported,
10453 printconfig);
10454 }
10455 } while (next_seid != 0);
10456
10457 kfree(aq_buf);
10458 return ret;
10459}
10460
10461/**
10462 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10463 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010464 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010465 *
10466 * Returns 0 on success, negative value on failure
10467 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010468static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010469{
10470 int ret;
10471
10472 /* find out what's out there already */
10473 ret = i40e_fetch_switch_configuration(pf, false);
10474 if (ret) {
10475 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010476 "couldn't fetch switch config, 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 ret;
10480 }
10481 i40e_pf_reset_stats(pf);
10482
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010483 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010484 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010485 struct i40e_vsi *vsi = NULL;
10486 u16 uplink_seid;
10487
10488 /* Set up the PF VSI associated with the PF's main VSI
10489 * that is already in the HW switch
10490 */
10491 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10492 uplink_seid = pf->veb[pf->lan_veb]->seid;
10493 else
10494 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010495 if (pf->lan_vsi == I40E_NO_VSI)
10496 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10497 else if (reinit)
10498 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499 if (!vsi) {
10500 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10501 i40e_fdir_teardown(pf);
10502 return -EAGAIN;
10503 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010504 } else {
10505 /* force a reset of TC and queue layout configurations */
10506 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010507
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010508 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10509 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10510 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10511 }
10512 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10513
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010514 i40e_fdir_sb_setup(pf);
10515
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010516 /* Setup static PF queue filter control settings */
10517 ret = i40e_setup_pf_filter_control(pf);
10518 if (ret) {
10519 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10520 ret);
10521 /* Failure here should not stop continuing other steps */
10522 }
10523
10524 /* enable RSS in the HW, even for only one queue, as the stack can use
10525 * the hash
10526 */
10527 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010528 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010529
10530 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010531 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010532 i40e_link_event(pf);
10533
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010534 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010535 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10536 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010537
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010538 i40e_ptp_init(pf);
10539
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010540 return ret;
10541}
10542
10543/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010544 * i40e_determine_queue_usage - Work out queue distribution
10545 * @pf: board private structure
10546 **/
10547static void i40e_determine_queue_usage(struct i40e_pf *pf)
10548{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010549 int queues_left;
10550
10551 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010552#ifdef I40E_FCOE
10553 pf->num_fcoe_qps = 0;
10554#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010555
10556 /* Find the max queues to be put into basic use. We'll always be
10557 * using TC0, whether or not DCB is running, and TC0 will get the
10558 * big RSS set.
10559 */
10560 queues_left = pf->hw.func_caps.num_tx_qp;
10561
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010562 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010563 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010564 /* one qp for PF, no queues for anything else */
10565 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010566 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010567
10568 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010569 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010570 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010571#ifdef I40E_FCOE
10572 I40E_FLAG_FCOE_ENABLED |
10573#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010574 I40E_FLAG_FD_SB_ENABLED |
10575 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010576 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010577 I40E_FLAG_SRIOV_ENABLED |
10578 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010579 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10580 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010581 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010582 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010583 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010584 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010585 queues_left -= pf->num_lan_qps;
10586
10587 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010588 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010589#ifdef I40E_FCOE
10590 I40E_FLAG_FCOE_ENABLED |
10591#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010592 I40E_FLAG_FD_SB_ENABLED |
10593 I40E_FLAG_FD_ATR_ENABLED |
10594 I40E_FLAG_DCB_ENABLED |
10595 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010596 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010597 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010598 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010599 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010600 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010601 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10602 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010603 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10604 num_online_cpus());
10605 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10606 pf->hw.func_caps.num_tx_qp);
10607
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010608 queues_left -= pf->num_lan_qps;
10609 }
10610
Vasu Dev38e00432014-08-01 13:27:03 -070010611#ifdef I40E_FCOE
10612 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10613 if (I40E_DEFAULT_FCOE <= queues_left) {
10614 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10615 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10616 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10617 } else {
10618 pf->num_fcoe_qps = 0;
10619 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10620 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10621 }
10622
10623 queues_left -= pf->num_fcoe_qps;
10624 }
10625
10626#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010627 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10628 if (queues_left > 1) {
10629 queues_left -= 1; /* save 1 queue for FD */
10630 } else {
10631 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10632 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10633 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010634 }
10635
10636 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10637 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010638 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10639 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010640 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10641 }
10642
10643 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10644 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10645 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10646 (queues_left / pf->num_vmdq_qps));
10647 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10648 }
10649
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010650 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010651 dev_dbg(&pf->pdev->dev,
10652 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10653 pf->hw.func_caps.num_tx_qp,
10654 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010655 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10656 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10657 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010658#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010659 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010660#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010661}
10662
10663/**
10664 * i40e_setup_pf_filter_control - Setup PF static filter control
10665 * @pf: PF to be setup
10666 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010667 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010668 * settings. If PE/FCoE are enabled then it will also set the per PF
10669 * based filter sizes required for them. It also enables Flow director,
10670 * ethertype and macvlan type filter settings for the pf.
10671 *
10672 * Returns 0 on success, negative on failure
10673 **/
10674static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10675{
10676 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10677
10678 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10679
10680 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010681 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010682 settings->enable_fdir = true;
10683
10684 /* Ethtype and MACVLAN filters enabled for PF */
10685 settings->enable_ethtype = true;
10686 settings->enable_macvlan = true;
10687
10688 if (i40e_set_filter_control(&pf->hw, settings))
10689 return -ENOENT;
10690
10691 return 0;
10692}
10693
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010694#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010695#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010696static void i40e_print_features(struct i40e_pf *pf)
10697{
10698 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010699 char *buf;
10700 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010701
Joe Perches3b195842015-12-03 04:20:57 -080010702 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10703 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010704 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010705
Joe Perches3b195842015-12-03 04:20:57 -080010706 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010707#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010708 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010709#endif
Joe Perches3b195842015-12-03 04:20:57 -080010710 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d RX: %s",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010711 pf->hw.func_caps.num_vsis,
10712 pf->vsi[pf->lan_vsi]->num_queue_pairs,
10713 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010714
10715 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010716 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010717 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010718 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010719 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010720 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10721 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010722 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010723 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010724 i += snprintf(&buf[i], REMAIN(i), " DCB");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010725#if IS_ENABLED(CONFIG_VXLAN)
Joe Perches3b195842015-12-03 04:20:57 -080010726 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010727#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080010728#if IS_ENABLED(CONFIG_GENEVE)
10729 i += snprintf(&buf[i], REMAIN(i), " Geneve");
10730#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010731 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010732 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010733#ifdef I40E_FCOE
10734 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010735 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010736#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010737 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010738 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010739 else
Joe Perches3b195842015-12-03 04:20:57 -080010740 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010741
Joe Perches3b195842015-12-03 04:20:57 -080010742 dev_info(&pf->pdev->dev, "%s\n", buf);
10743 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010744 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010745}
10746
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010747/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010748 * i40e_get_platform_mac_addr - get platform-specific MAC address
10749 *
10750 * @pdev: PCI device information struct
10751 * @pf: board private structure
10752 *
10753 * Look up the MAC address in Open Firmware on systems that support it,
10754 * and use IDPROM on SPARC if no OF address is found. On return, the
10755 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10756 * has been selected.
10757 **/
10758static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10759{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010760 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010761 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010762 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010763}
10764
10765/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010766 * i40e_probe - Device initialization routine
10767 * @pdev: PCI device information struct
10768 * @ent: entry in i40e_pci_tbl
10769 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010770 * i40e_probe initializes a PF identified by a pci_dev structure.
10771 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010772 * and a hardware reset occur.
10773 *
10774 * Returns 0 on success, negative on failure
10775 **/
10776static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10777{
Catherine Sullivane8278452015-02-06 08:52:08 +000010778 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010779 struct i40e_pf *pf;
10780 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010781 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010782 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010783 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010784 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010785 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010786 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010787 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010788
10789 err = pci_enable_device_mem(pdev);
10790 if (err)
10791 return err;
10792
10793 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010794 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010795 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010796 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10797 if (err) {
10798 dev_err(&pdev->dev,
10799 "DMA configuration failed: 0x%x\n", err);
10800 goto err_dma;
10801 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010802 }
10803
10804 /* set up pci connections */
10805 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
10806 IORESOURCE_MEM), i40e_driver_name);
10807 if (err) {
10808 dev_info(&pdev->dev,
10809 "pci_request_selected_regions failed %d\n", err);
10810 goto err_pci_reg;
10811 }
10812
10813 pci_enable_pcie_error_reporting(pdev);
10814 pci_set_master(pdev);
10815
10816 /* Now that we have a PCI connection, we need to do the
10817 * low level device setup. This is primarily setting up
10818 * the Admin Queue structures and then querying for the
10819 * device's current profile information.
10820 */
10821 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10822 if (!pf) {
10823 err = -ENOMEM;
10824 goto err_pf_alloc;
10825 }
10826 pf->next_vsi = 0;
10827 pf->pdev = pdev;
10828 set_bit(__I40E_DOWN, &pf->state);
10829
10830 hw = &pf->hw;
10831 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010832
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010833 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10834 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010835
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010836 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010837 if (!hw->hw_addr) {
10838 err = -EIO;
10839 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10840 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010841 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010842 goto err_ioremap;
10843 }
10844 hw->vendor_id = pdev->vendor;
10845 hw->device_id = pdev->device;
10846 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10847 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10848 hw->subsystem_device_id = pdev->subsystem_device;
10849 hw->bus.device = PCI_SLOT(pdev->devfn);
10850 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010851 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010852
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010853 /* set up the locks for the AQ, do this only once in probe
10854 * and destroy them only once in remove
10855 */
10856 mutex_init(&hw->aq.asq_mutex);
10857 mutex_init(&hw->aq.arq_mutex);
10858
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010859 if (debug != -1) {
10860 pf->msg_enable = pf->hw.debug_mask;
10861 pf->msg_enable = debug;
10862 }
10863
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010864 /* do a special CORER for clearing PXE mode once at init */
10865 if (hw->revision_id == 0 &&
10866 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10867 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10868 i40e_flush(hw);
10869 msleep(200);
10870 pf->corer_count++;
10871
10872 i40e_clear_pxe_mode(hw);
10873 }
10874
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010876 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010877 err = i40e_pf_reset(hw);
10878 if (err) {
10879 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10880 goto err_pf_reset;
10881 }
10882 pf->pfr_count++;
10883
10884 hw->aq.num_arq_entries = I40E_AQ_LEN;
10885 hw->aq.num_asq_entries = I40E_AQ_LEN;
10886 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10887 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10888 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010889
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010890 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010891 "%s-%s:misc",
10892 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010893
10894 err = i40e_init_shared_code(hw);
10895 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010896 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10897 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010898 goto err_pf_reset;
10899 }
10900
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010901 /* set up a default setting for link flow control */
10902 pf->hw.fc.requested_mode = I40E_FC_NONE;
10903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010904 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a2015-10-26 19:44:35 -040010905 if (err) {
10906 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10907 dev_info(&pdev->dev,
10908 "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");
10909 else
10910 dev_info(&pdev->dev,
10911 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10912
10913 goto err_pf_reset;
10914 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010915
Shannon Nelson6dec1012015-09-28 14:12:30 -040010916 /* provide nvm, fw, api versions */
10917 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10918 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10919 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10920 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010921
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010922 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10923 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010924 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010925 "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");
10926 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10927 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010928 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010929 "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 +000010930
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010931 i40e_verify_eeprom(pf);
10932
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010933 /* Rev 0 hardware was never productized */
10934 if (hw->revision_id < 1)
10935 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");
10936
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010937 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010938 err = i40e_get_capabilities(pf);
10939 if (err)
10940 goto err_adminq_setup;
10941
10942 err = i40e_sw_init(pf);
10943 if (err) {
10944 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10945 goto err_sw_init;
10946 }
10947
10948 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10949 hw->func_caps.num_rx_qp,
10950 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10951 if (err) {
10952 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10953 goto err_init_lan_hmc;
10954 }
10955
10956 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10957 if (err) {
10958 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10959 err = -ENOENT;
10960 goto err_configure_lan_hmc;
10961 }
10962
Neerav Parikhb686ece2014-12-14 01:55:11 +000010963 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10964 * Ignore error return codes because if it was already disabled via
10965 * hardware settings this will fail
10966 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010967 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010968 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10969 i40e_aq_stop_lldp(hw, true, NULL);
10970 }
10971
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010972 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010973 /* allow a platform config to override the HW addr */
10974 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010975 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010976 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10977 err = -EIO;
10978 goto err_mac_addr;
10979 }
10980 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010981 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010982 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10983 if (is_valid_ether_addr(hw->mac.port_addr))
10984 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010985#ifdef I40E_FCOE
10986 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10987 if (err)
10988 dev_info(&pdev->dev,
10989 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10990 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10991 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10992 hw->mac.san_addr);
10993 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10994 }
10995 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10996#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010997
10998 pci_set_drvdata(pdev, pf);
10999 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011000#ifdef CONFIG_I40E_DCB
11001 err = i40e_init_pf_dcb(pf);
11002 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011003 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000011004 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000011005 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011006 }
11007#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011008
11009 /* set up periodic task facility */
11010 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11011 pf->service_timer_period = HZ;
11012
11013 INIT_WORK(&pf->service_task, i40e_service_task);
11014 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11015 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011016
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011017 /* NVM bit on means WoL disabled for the port */
11018 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011019 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011020 pf->wol_en = false;
11021 else
11022 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011023 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11024
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011025 /* set up the main switch operations */
11026 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011027 err = i40e_init_interrupt_scheme(pf);
11028 if (err)
11029 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011030
Mitch Williams505682c2014-05-20 08:01:37 +000011031 /* The number of VSIs reported by the FW is the minimum guaranteed
11032 * to us; HW supports far more and we share the remaining pool with
11033 * the other PFs. We allocate space for more than the guarantee with
11034 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011035 */
Mitch Williams505682c2014-05-20 08:01:37 +000011036 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11037 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11038 else
11039 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11040
11041 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011042 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11043 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011044 if (!pf->vsi) {
11045 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011046 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011047 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011048
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011049#ifdef CONFIG_PCI_IOV
11050 /* prep for VF support */
11051 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11052 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11053 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11054 if (pci_num_vf(pdev))
11055 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11056 }
11057#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011058 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011059 if (err) {
11060 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11061 goto err_vsis;
11062 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011063
11064 /* Make sure flow control is set according to current settings */
11065 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11066 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11067 dev_dbg(&pf->pdev->dev,
11068 "Set fc with err %s aq_err %s on get_phy_cap\n",
11069 i40e_stat_str(hw, err),
11070 i40e_aq_str(hw, hw->aq.asq_last_status));
11071 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11072 dev_dbg(&pf->pdev->dev,
11073 "Set fc with err %s aq_err %s on set_phy_config\n",
11074 i40e_stat_str(hw, err),
11075 i40e_aq_str(hw, hw->aq.asq_last_status));
11076 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11077 dev_dbg(&pf->pdev->dev,
11078 "Set fc with err %s aq_err %s on get_link_info\n",
11079 i40e_stat_str(hw, err),
11080 i40e_aq_str(hw, hw->aq.asq_last_status));
11081
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011082 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011083 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011084 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11085 i40e_vsi_open(pf->vsi[i]);
11086 break;
11087 }
11088 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011089
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011090 /* The driver only wants link up/down and module qualification
11091 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011092 */
11093 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011094 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011095 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011096 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011097 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011098 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11099 i40e_stat_str(&pf->hw, err),
11100 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011101
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011102 /* Reconfigure hardware for allowing smaller MSS in the case
11103 * of TSO, so that we avoid the MDD being fired and causing
11104 * a reset in the case of small MSS+TSO.
11105 */
11106 val = rd32(hw, I40E_REG_MSS);
11107 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11108 val &= ~I40E_REG_MSS_MIN_MASK;
11109 val |= I40E_64BYTE_MSS;
11110 wr32(hw, I40E_REG_MSS, val);
11111 }
11112
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011113 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011114 msleep(75);
11115 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11116 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011117 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11118 i40e_stat_str(&pf->hw, err),
11119 i40e_aq_str(&pf->hw,
11120 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011121 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011122 /* The main driver is (mostly) up and happy. We need to set this state
11123 * before setting up the misc vector or we get a race and the vector
11124 * ends up disabled forever.
11125 */
11126 clear_bit(__I40E_DOWN, &pf->state);
11127
11128 /* In case of MSIX we are going to setup the misc vector right here
11129 * to handle admin queue events etc. In case of legacy and MSI
11130 * the misc functionality and queue processing is combined in
11131 * the same vector and that gets setup at open.
11132 */
11133 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11134 err = i40e_setup_misc_vector(pf);
11135 if (err) {
11136 dev_info(&pdev->dev,
11137 "setup of misc vector failed: %d\n", err);
11138 goto err_vsis;
11139 }
11140 }
11141
Greg Rosedf805f62014-04-04 04:43:16 +000011142#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011143 /* prep for VF support */
11144 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011145 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11146 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011147 /* disable link interrupts for VFs */
11148 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11149 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11150 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11151 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011152
11153 if (pci_num_vf(pdev)) {
11154 dev_info(&pdev->dev,
11155 "Active VFs found, allocating resources.\n");
11156 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11157 if (err)
11158 dev_info(&pdev->dev,
11159 "Error %d allocating resources for existing VFs\n",
11160 err);
11161 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011162 }
Greg Rosedf805f62014-04-04 04:43:16 +000011163#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011164
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011165 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11166 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11167 pf->num_iwarp_msix,
11168 I40E_IWARP_IRQ_PILE_ID);
11169 if (pf->iwarp_base_vector < 0) {
11170 dev_info(&pdev->dev,
11171 "failed to get tracking for %d vectors for IWARP err=%d\n",
11172 pf->num_iwarp_msix, pf->iwarp_base_vector);
11173 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11174 }
11175 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011177 i40e_dbg_pf_init(pf);
11178
11179 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011180 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011181
11182 /* since everything's happy, start the service_task timer */
11183 mod_timer(&pf->service_timer,
11184 round_jiffies(jiffies + pf->service_timer_period));
11185
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011186 /* add this PF to client device list and launch a client service task */
11187 err = i40e_lan_add_device(pf);
11188 if (err)
11189 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11190 err);
11191
Vasu Dev38e00432014-08-01 13:27:03 -070011192#ifdef I40E_FCOE
11193 /* create FCoE interface */
11194 i40e_fcoe_vsi_setup(pf);
11195
11196#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011197#define PCI_SPEED_SIZE 8
11198#define PCI_WIDTH_SIZE 8
11199 /* Devices on the IOSF bus do not have this information
11200 * and will report PCI Gen 1 x 1 by default so don't bother
11201 * checking them.
11202 */
11203 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11204 char speed[PCI_SPEED_SIZE] = "Unknown";
11205 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011206
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011207 /* Get the negotiated link width and speed from PCI config
11208 * space
11209 */
11210 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11211 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011212
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011213 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011214
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011215 switch (hw->bus.speed) {
11216 case i40e_bus_speed_8000:
11217 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11218 case i40e_bus_speed_5000:
11219 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11220 case i40e_bus_speed_2500:
11221 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11222 default:
11223 break;
11224 }
11225 switch (hw->bus.width) {
11226 case i40e_bus_width_pcie_x8:
11227 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11228 case i40e_bus_width_pcie_x4:
11229 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11230 case i40e_bus_width_pcie_x2:
11231 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11232 case i40e_bus_width_pcie_x1:
11233 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11234 default:
11235 break;
11236 }
11237
11238 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11239 speed, width);
11240
11241 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11242 hw->bus.speed < i40e_bus_speed_8000) {
11243 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11244 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11245 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011246 }
11247
Catherine Sullivane8278452015-02-06 08:52:08 +000011248 /* get the requested speeds from the fw */
11249 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11250 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011251 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11252 i40e_stat_str(&pf->hw, err),
11253 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011254 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11255
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011256 /* get the supported phy types from the fw */
11257 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11258 if (err)
11259 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11260 i40e_stat_str(&pf->hw, err),
11261 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11262 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11263
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011264 /* Add a filter to drop all Flow control frames from any VSI from being
11265 * transmitted. By doing so we stop a malicious VF from sending out
11266 * PAUSE or PFC frames and potentially controlling traffic for other
11267 * PF/VF VSIs.
11268 * The FW can still send Flow control frames if enabled.
11269 */
11270 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11271 pf->main_vsi_seid);
11272
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011273 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11274 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11275 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11276
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011277 /* print a string summarizing features */
11278 i40e_print_features(pf);
11279
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011280 return 0;
11281
11282 /* Unwind what we've done if something failed in the setup */
11283err_vsis:
11284 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011285 i40e_clear_interrupt_scheme(pf);
11286 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011287err_switch_setup:
11288 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011289 del_timer_sync(&pf->service_timer);
11290err_mac_addr:
11291err_configure_lan_hmc:
11292 (void)i40e_shutdown_lan_hmc(hw);
11293err_init_lan_hmc:
11294 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011295err_sw_init:
11296err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011297err_pf_reset:
11298 iounmap(hw->hw_addr);
11299err_ioremap:
11300 kfree(pf);
11301err_pf_alloc:
11302 pci_disable_pcie_error_reporting(pdev);
11303 pci_release_selected_regions(pdev,
11304 pci_select_bars(pdev, IORESOURCE_MEM));
11305err_pci_reg:
11306err_dma:
11307 pci_disable_device(pdev);
11308 return err;
11309}
11310
11311/**
11312 * i40e_remove - Device removal routine
11313 * @pdev: PCI device information struct
11314 *
11315 * i40e_remove is called by the PCI subsystem to alert the driver
11316 * that is should release a PCI device. This could be caused by a
11317 * Hot-Plug event, or because the driver is going to be removed from
11318 * memory.
11319 **/
11320static void i40e_remove(struct pci_dev *pdev)
11321{
11322 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011323 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011324 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011325 int i;
11326
11327 i40e_dbg_pf_exit(pf);
11328
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011329 i40e_ptp_stop(pf);
11330
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011331 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011332 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11333 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011334
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011335 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011336 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011337 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011338 if (pf->service_timer.data)
11339 del_timer_sync(&pf->service_timer);
11340 if (pf->service_task.func)
11341 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011342
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011343 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11344 i40e_free_vfs(pf);
11345 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11346 }
11347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011348 i40e_fdir_teardown(pf);
11349
11350 /* If there is a switch structure or any orphans, remove them.
11351 * This will leave only the PF's VSI remaining.
11352 */
11353 for (i = 0; i < I40E_MAX_VEB; i++) {
11354 if (!pf->veb[i])
11355 continue;
11356
11357 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11358 pf->veb[i]->uplink_seid == 0)
11359 i40e_switch_branch_release(pf->veb[i]);
11360 }
11361
11362 /* Now we can shutdown the PF's VSI, just before we kill
11363 * adminq and hmc.
11364 */
11365 if (pf->vsi[pf->lan_vsi])
11366 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11367
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011368 /* remove attached clients */
11369 ret_code = i40e_lan_del_device(pf);
11370 if (ret_code) {
11371 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11372 ret_code);
11373 }
11374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011375 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011376 if (hw->hmc.hmc_obj) {
11377 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011378 if (ret_code)
11379 dev_warn(&pdev->dev,
11380 "Failed to destroy the HMC resources: %d\n",
11381 ret_code);
11382 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011383
11384 /* shutdown the adminq */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011385 ret_code = i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011386 if (ret_code)
11387 dev_warn(&pdev->dev,
11388 "Failed to destroy the Admin Queue resources: %d\n",
11389 ret_code);
11390
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011391 /* destroy the locks only once, here */
11392 mutex_destroy(&hw->aq.arq_mutex);
11393 mutex_destroy(&hw->aq.asq_mutex);
11394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011395 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11396 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011397 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011398 if (pf->vsi[i]) {
11399 i40e_vsi_clear_rings(pf->vsi[i]);
11400 i40e_vsi_clear(pf->vsi[i]);
11401 pf->vsi[i] = NULL;
11402 }
11403 }
11404
11405 for (i = 0; i < I40E_MAX_VEB; i++) {
11406 kfree(pf->veb[i]);
11407 pf->veb[i] = NULL;
11408 }
11409
11410 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011411 kfree(pf->vsi);
11412
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011413 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011414 kfree(pf);
11415 pci_release_selected_regions(pdev,
11416 pci_select_bars(pdev, IORESOURCE_MEM));
11417
11418 pci_disable_pcie_error_reporting(pdev);
11419 pci_disable_device(pdev);
11420}
11421
11422/**
11423 * i40e_pci_error_detected - warning that something funky happened in PCI land
11424 * @pdev: PCI device information struct
11425 *
11426 * Called to warn that something happened and the error handling steps
11427 * are in progress. Allows the driver to quiesce things, be ready for
11428 * remediation.
11429 **/
11430static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11431 enum pci_channel_state error)
11432{
11433 struct i40e_pf *pf = pci_get_drvdata(pdev);
11434
11435 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11436
11437 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011438 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11439 rtnl_lock();
11440 i40e_prep_for_reset(pf);
11441 rtnl_unlock();
11442 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011443
11444 /* Request a slot reset */
11445 return PCI_ERS_RESULT_NEED_RESET;
11446}
11447
11448/**
11449 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11450 * @pdev: PCI device information struct
11451 *
11452 * Called to find if the driver can work with the device now that
11453 * the pci slot has been reset. If a basic connection seems good
11454 * (registers are readable and have sane content) then return a
11455 * happy little PCI_ERS_RESULT_xxx.
11456 **/
11457static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11458{
11459 struct i40e_pf *pf = pci_get_drvdata(pdev);
11460 pci_ers_result_t result;
11461 int err;
11462 u32 reg;
11463
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011464 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011465 if (pci_enable_device_mem(pdev)) {
11466 dev_info(&pdev->dev,
11467 "Cannot re-enable PCI device after reset.\n");
11468 result = PCI_ERS_RESULT_DISCONNECT;
11469 } else {
11470 pci_set_master(pdev);
11471 pci_restore_state(pdev);
11472 pci_save_state(pdev);
11473 pci_wake_from_d3(pdev, false);
11474
11475 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11476 if (reg == 0)
11477 result = PCI_ERS_RESULT_RECOVERED;
11478 else
11479 result = PCI_ERS_RESULT_DISCONNECT;
11480 }
11481
11482 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11483 if (err) {
11484 dev_info(&pdev->dev,
11485 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11486 err);
11487 /* non-fatal, continue */
11488 }
11489
11490 return result;
11491}
11492
11493/**
11494 * i40e_pci_error_resume - restart operations after PCI error recovery
11495 * @pdev: PCI device information struct
11496 *
11497 * Called to allow the driver to bring things back up after PCI error
11498 * and/or reset recovery has finished.
11499 **/
11500static void i40e_pci_error_resume(struct pci_dev *pdev)
11501{
11502 struct i40e_pf *pf = pci_get_drvdata(pdev);
11503
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011504 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011505 if (test_bit(__I40E_SUSPENDED, &pf->state))
11506 return;
11507
11508 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011509 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011510 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011511}
11512
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011513/**
11514 * i40e_shutdown - PCI callback for shutting down
11515 * @pdev: PCI device information struct
11516 **/
11517static void i40e_shutdown(struct pci_dev *pdev)
11518{
11519 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011520 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011521
11522 set_bit(__I40E_SUSPENDED, &pf->state);
11523 set_bit(__I40E_DOWN, &pf->state);
11524 rtnl_lock();
11525 i40e_prep_for_reset(pf);
11526 rtnl_unlock();
11527
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011528 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11529 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11530
Catherine Sullivan02b42492015-07-10 19:35:59 -040011531 del_timer_sync(&pf->service_timer);
11532 cancel_work_sync(&pf->service_task);
11533 i40e_fdir_teardown(pf);
11534
11535 rtnl_lock();
11536 i40e_prep_for_reset(pf);
11537 rtnl_unlock();
11538
11539 wr32(hw, I40E_PFPM_APM,
11540 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11541 wr32(hw, I40E_PFPM_WUFC,
11542 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11543
Shannon Nelsone1477582015-02-21 06:44:33 +000011544 i40e_clear_interrupt_scheme(pf);
11545
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011546 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011547 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011548 pci_set_power_state(pdev, PCI_D3hot);
11549 }
11550}
11551
11552#ifdef CONFIG_PM
11553/**
11554 * i40e_suspend - PCI callback for moving to D3
11555 * @pdev: PCI device information struct
11556 **/
11557static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11558{
11559 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011560 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011561
11562 set_bit(__I40E_SUSPENDED, &pf->state);
11563 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011564
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011565 rtnl_lock();
11566 i40e_prep_for_reset(pf);
11567 rtnl_unlock();
11568
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011569 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11570 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11571
11572 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011573 pci_set_power_state(pdev, PCI_D3hot);
11574
11575 return 0;
11576}
11577
11578/**
11579 * i40e_resume - PCI callback for waking up from D3
11580 * @pdev: PCI device information struct
11581 **/
11582static int i40e_resume(struct pci_dev *pdev)
11583{
11584 struct i40e_pf *pf = pci_get_drvdata(pdev);
11585 u32 err;
11586
11587 pci_set_power_state(pdev, PCI_D0);
11588 pci_restore_state(pdev);
11589 /* pci_restore_state() clears dev->state_saves, so
11590 * call pci_save_state() again to restore it.
11591 */
11592 pci_save_state(pdev);
11593
11594 err = pci_enable_device_mem(pdev);
11595 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011596 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011597 return err;
11598 }
11599 pci_set_master(pdev);
11600
11601 /* no wakeup events while running */
11602 pci_wake_from_d3(pdev, false);
11603
11604 /* handling the reset will rebuild the device state */
11605 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11606 clear_bit(__I40E_DOWN, &pf->state);
11607 rtnl_lock();
11608 i40e_reset_and_rebuild(pf, false);
11609 rtnl_unlock();
11610 }
11611
11612 return 0;
11613}
11614
11615#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011616static const struct pci_error_handlers i40e_err_handler = {
11617 .error_detected = i40e_pci_error_detected,
11618 .slot_reset = i40e_pci_error_slot_reset,
11619 .resume = i40e_pci_error_resume,
11620};
11621
11622static struct pci_driver i40e_driver = {
11623 .name = i40e_driver_name,
11624 .id_table = i40e_pci_tbl,
11625 .probe = i40e_probe,
11626 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011627#ifdef CONFIG_PM
11628 .suspend = i40e_suspend,
11629 .resume = i40e_resume,
11630#endif
11631 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011632 .err_handler = &i40e_err_handler,
11633 .sriov_configure = i40e_pci_sriov_configure,
11634};
11635
11636/**
11637 * i40e_init_module - Driver registration routine
11638 *
11639 * i40e_init_module is the first routine called when the driver is
11640 * loaded. All it does is register with the PCI subsystem.
11641 **/
11642static int __init i40e_init_module(void)
11643{
11644 pr_info("%s: %s - version %s\n", i40e_driver_name,
11645 i40e_driver_string, i40e_driver_version_str);
11646 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011647
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011648 /* we will see if single thread per module is enough for now,
11649 * it can't be any worse than using the system workqueue which
11650 * was already single threaded
11651 */
11652 i40e_wq = create_singlethread_workqueue(i40e_driver_name);
11653 if (!i40e_wq) {
11654 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11655 return -ENOMEM;
11656 }
11657
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011658 i40e_dbg_init();
11659 return pci_register_driver(&i40e_driver);
11660}
11661module_init(i40e_init_module);
11662
11663/**
11664 * i40e_exit_module - Driver exit cleanup routine
11665 *
11666 * i40e_exit_module is called just before the driver is removed
11667 * from memory.
11668 **/
11669static void __exit i40e_exit_module(void)
11670{
11671 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011672 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011673 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011674}
11675module_exit(i40e_exit_module);