blob: 1598fb31477a194345f86d3895c599083a0d14db [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Catherine Sullivane8278452015-02-06 08:52:08 +00004 * Copyright(c) 2013 - 2015 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
27/* Local includes */
28#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000029#include "i40e_diag.h"
Singhai, Anjali6a899022015-12-14 12:21:18 -080030#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000031#include <net/vxlan.h>
32#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080033#if IS_ENABLED(CONFIG_GENEVE)
34#include <net/geneve.h>
35#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000036
37const char i40e_driver_name[] = "i40e";
38static const char i40e_driver_string[] =
39 "Intel(R) Ethernet Connection XL710 Network Driver";
40
41#define DRV_KERN "-k"
42
Catherine Sullivane8e724d2014-07-10 07:58:26 +000043#define DRV_VERSION_MAJOR 1
Catherine Sullivan1c2df9e2015-10-21 19:56:24 -040044#define DRV_VERSION_MINOR 4
Catherine Sullivanc9c9f1b2015-11-19 11:34:25 -080045#define DRV_VERSION_BUILD 8
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000046#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
47 __stringify(DRV_VERSION_MINOR) "." \
48 __stringify(DRV_VERSION_BUILD) DRV_KERN
49const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080050static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000051
52/* a bit of forward declarations */
53static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
54static void i40e_handle_reset_warning(struct i40e_pf *pf);
55static int i40e_add_vsi(struct i40e_vsi *vsi);
56static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000057static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000058static int i40e_setup_misc_vector(struct i40e_pf *pf);
59static void i40e_determine_queue_usage(struct i40e_pf *pf);
60static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Helin Zhange69ff812015-10-21 19:56:22 -040061static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
62 u16 rss_table_size, u16 rss_size);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080063static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080064static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000065
66/* i40e_pci_tbl - PCI Device ID Table
67 *
68 * Last entry must be all 0s
69 *
70 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
71 * Class, Class Mask, private data (not used) }
72 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020073static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080074 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
78 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
80 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070084 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000090 /* required last entry */
91 {0, }
92};
93MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94
95#define I40E_MAX_VF_COUNT 128
96static int debug = -1;
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
99
100MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102MODULE_LICENSE("GPL");
103MODULE_VERSION(DRV_VERSION);
104
105/**
106 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
107 * @hw: pointer to the HW structure
108 * @mem: ptr to mem struct to fill out
109 * @size: size of memory requested
110 * @alignment: what to align the allocation to
111 **/
112int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
113 u64 size, u32 alignment)
114{
115 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
116
117 mem->size = ALIGN(size, alignment);
118 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
119 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000120 if (!mem->va)
121 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000122
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000123 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000124}
125
126/**
127 * i40e_free_dma_mem_d - OS specific memory free for shared code
128 * @hw: pointer to the HW structure
129 * @mem: ptr to mem struct to free
130 **/
131int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
132{
133 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
134
135 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
136 mem->va = NULL;
137 mem->pa = 0;
138 mem->size = 0;
139
140 return 0;
141}
142
143/**
144 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
145 * @hw: pointer to the HW structure
146 * @mem: ptr to mem struct to fill out
147 * @size: size of memory requested
148 **/
149int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
150 u32 size)
151{
152 mem->size = size;
153 mem->va = kzalloc(size, GFP_KERNEL);
154
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000155 if (!mem->va)
156 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000157
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000158 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000159}
160
161/**
162 * i40e_free_virt_mem_d - OS specific memory free for shared code
163 * @hw: pointer to the HW structure
164 * @mem: ptr to mem struct to free
165 **/
166int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
167{
168 /* it's ok to kfree a NULL pointer */
169 kfree(mem->va);
170 mem->va = NULL;
171 mem->size = 0;
172
173 return 0;
174}
175
176/**
177 * i40e_get_lump - find a lump of free generic resource
178 * @pf: board private structure
179 * @pile: the pile of resource to search
180 * @needed: the number of items needed
181 * @id: an owner id to stick on the items assigned
182 *
183 * Returns the base item index of the lump, or negative for error
184 *
185 * The search_hint trick and lack of advanced fit-finding only work
186 * because we're highly likely to have all the same size lump requests.
187 * Linear search time and any fragmentation should be minimal.
188 **/
189static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
190 u16 needed, u16 id)
191{
192 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000193 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000194
195 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
196 dev_info(&pf->pdev->dev,
197 "param err: pile=%p needed=%d id=0x%04x\n",
198 pile, needed, id);
199 return -EINVAL;
200 }
201
202 /* start the linear search with an imperfect hint */
203 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000204 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000205 /* skip already allocated entries */
206 if (pile->list[i] & I40E_PILE_VALID_BIT) {
207 i++;
208 continue;
209 }
210
211 /* do we have enough in this lump? */
212 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
213 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
214 break;
215 }
216
217 if (j == needed) {
218 /* there was enough, so assign it to the requestor */
219 for (j = 0; j < needed; j++)
220 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
221 ret = i;
222 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000223 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000224 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400225
226 /* not enough, so skip over it and continue looking */
227 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000228 }
229
230 return ret;
231}
232
233/**
234 * i40e_put_lump - return a lump of generic resource
235 * @pile: the pile of resource to search
236 * @index: the base item index
237 * @id: the owner id of the items assigned
238 *
239 * Returns the count of items in the lump
240 **/
241static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
242{
243 int valid_id = (id | I40E_PILE_VALID_BIT);
244 int count = 0;
245 int i;
246
247 if (!pile || index >= pile->num_entries)
248 return -EINVAL;
249
250 for (i = index;
251 i < pile->num_entries && pile->list[i] == valid_id;
252 i++) {
253 pile->list[i] = 0;
254 count++;
255 }
256
257 if (count && index < pile->search_hint)
258 pile->search_hint = index;
259
260 return count;
261}
262
263/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700264 * i40e_find_vsi_from_id - searches for the vsi with the given id
265 * @pf - the pf structure to search for the vsi
266 * @id - id of the vsi it is searching for
267 **/
268struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
269{
270 int i;
271
272 for (i = 0; i < pf->num_alloc_vsi; i++)
273 if (pf->vsi[i] && (pf->vsi[i]->id == id))
274 return pf->vsi[i];
275
276 return NULL;
277}
278
279/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000280 * i40e_service_event_schedule - Schedule the service task to wake up
281 * @pf: board private structure
282 *
283 * If not already scheduled, this puts the task into the work queue
284 **/
285static void i40e_service_event_schedule(struct i40e_pf *pf)
286{
287 if (!test_bit(__I40E_DOWN, &pf->state) &&
288 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
289 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
290 schedule_work(&pf->service_task);
291}
292
293/**
294 * i40e_tx_timeout - Respond to a Tx Hang
295 * @netdev: network interface device structure
296 *
297 * If any port has noticed a Tx timeout, it is likely that the whole
298 * device is munged, not just the one netdev port, so go for the full
299 * reset.
300 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700301#ifdef I40E_FCOE
302void i40e_tx_timeout(struct net_device *netdev)
303#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000304static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700305#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000306{
307 struct i40e_netdev_priv *np = netdev_priv(netdev);
308 struct i40e_vsi *vsi = np->vsi;
309 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400310 struct i40e_ring *tx_ring = NULL;
311 unsigned int i, hung_queue = 0;
312 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000313
314 pf->tx_timeout_count++;
315
Kiran Patilb03a8c12015-09-24 18:13:15 -0400316 /* find the stopped queue the same way the stack does */
317 for (i = 0; i < netdev->num_tx_queues; i++) {
318 struct netdev_queue *q;
319 unsigned long trans_start;
320
321 q = netdev_get_tx_queue(netdev, i);
322 trans_start = q->trans_start ? : netdev->trans_start;
323 if (netif_xmit_stopped(q) &&
324 time_after(jiffies,
325 (trans_start + netdev->watchdog_timeo))) {
326 hung_queue = i;
327 break;
328 }
329 }
330
331 if (i == netdev->num_tx_queues) {
332 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
333 } else {
334 /* now that we have an index, find the tx_ring struct */
335 for (i = 0; i < vsi->num_queue_pairs; i++) {
336 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
337 if (hung_queue ==
338 vsi->tx_rings[i]->queue_index) {
339 tx_ring = vsi->tx_rings[i];
340 break;
341 }
342 }
343 }
344 }
345
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000346 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400347 pf->tx_timeout_recovery_level = 1; /* reset after some time */
348 else if (time_before(jiffies,
349 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 return; /* don't do any new action before the next timeout */
351
352 if (tx_ring) {
353 head = i40e_get_head(tx_ring);
354 /* Read interrupt register */
355 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
356 val = rd32(&pf->hw,
357 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
358 tx_ring->vsi->base_vector - 1));
359 else
360 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
361
362 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",
363 vsi->seid, hung_queue, tx_ring->next_to_clean,
364 head, tx_ring->next_to_use,
365 readl(tx_ring->tail), val);
366 }
367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000368 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400369 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
370 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000371
372 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000373 case 1:
374 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
375 break;
376 case 2:
377 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
378 break;
379 case 3:
380 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
381 break;
382 default:
383 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000384 break;
385 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000387 i40e_service_event_schedule(pf);
388 pf->tx_timeout_recovery_level++;
389}
390
391/**
392 * i40e_release_rx_desc - Store the new tail and head values
393 * @rx_ring: ring to bump
394 * @val: new head index
395 **/
396static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
397{
398 rx_ring->next_to_use = val;
399
400 /* Force memory writes to complete before letting h/w
401 * know there are new descriptors to fetch. (Only
402 * applicable for weak-ordered memory model archs,
403 * such as IA-64).
404 */
405 wmb();
406 writel(val, rx_ring->tail);
407}
408
409/**
410 * i40e_get_vsi_stats_struct - Get System Network Statistics
411 * @vsi: the VSI we care about
412 *
413 * Returns the address of the device statistics structure.
414 * The statistics are actually updated from the service task.
415 **/
416struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
417{
418 return &vsi->net_stats;
419}
420
421/**
422 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
423 * @netdev: network interface device structure
424 *
425 * Returns the address of the device statistics structure.
426 * The statistics are actually updated from the service task.
427 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700428#ifdef I40E_FCOE
429struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
430 struct net_device *netdev,
431 struct rtnl_link_stats64 *stats)
432#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000433static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
434 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000435 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700436#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000437{
438 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000439 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000441 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
442 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000443
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000444 if (test_bit(__I40E_DOWN, &vsi->state))
445 return stats;
446
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800447 if (!vsi->tx_rings)
448 return stats;
449
Alexander Duyck980e9b12013-09-28 06:01:03 +0000450 rcu_read_lock();
451 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000452 u64 bytes, packets;
453 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000454
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
456 if (!tx_ring)
457 continue;
458
459 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700460 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000461 packets = tx_ring->stats.packets;
462 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700463 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000464
465 stats->tx_packets += packets;
466 stats->tx_bytes += bytes;
467 rx_ring = &tx_ring[1];
468
469 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700470 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000471 packets = rx_ring->stats.packets;
472 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700473 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000474
475 stats->rx_packets += packets;
476 stats->rx_bytes += bytes;
477 }
478 rcu_read_unlock();
479
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000480 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000481 stats->multicast = vsi_stats->multicast;
482 stats->tx_errors = vsi_stats->tx_errors;
483 stats->tx_dropped = vsi_stats->tx_dropped;
484 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400485 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000486 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
487 stats->rx_length_errors = vsi_stats->rx_length_errors;
488
489 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000490}
491
492/**
493 * i40e_vsi_reset_stats - Resets all stats of the given vsi
494 * @vsi: the VSI to have its stats reset
495 **/
496void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
497{
498 struct rtnl_link_stats64 *ns;
499 int i;
500
501 if (!vsi)
502 return;
503
504 ns = i40e_get_vsi_stats_struct(vsi);
505 memset(ns, 0, sizeof(*ns));
506 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
507 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
508 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000509 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000510 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400511 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000512 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400513 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000514 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400515 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000516 sizeof(vsi->tx_rings[i]->stats));
517 memset(&vsi->tx_rings[i]->tx_stats, 0,
518 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000519 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000520 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000521 vsi->stat_offsets_loaded = false;
522}
523
524/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000525 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000526 * @pf: the PF to be reset
527 **/
528void i40e_pf_reset_stats(struct i40e_pf *pf)
529{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000530 int i;
531
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000532 memset(&pf->stats, 0, sizeof(pf->stats));
533 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
534 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000535
536 for (i = 0; i < I40E_MAX_VEB; i++) {
537 if (pf->veb[i]) {
538 memset(&pf->veb[i]->stats, 0,
539 sizeof(pf->veb[i]->stats));
540 memset(&pf->veb[i]->stats_offsets, 0,
541 sizeof(pf->veb[i]->stats_offsets));
542 pf->veb[i]->stat_offsets_loaded = false;
543 }
544 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000545}
546
547/**
548 * i40e_stat_update48 - read and update a 48 bit stat from the chip
549 * @hw: ptr to the hardware info
550 * @hireg: the high 32 bit reg to read
551 * @loreg: the low 32 bit reg to read
552 * @offset_loaded: has the initial offset been loaded yet
553 * @offset: ptr to current offset value
554 * @stat: ptr to the stat
555 *
556 * Since the device stats are not reset at PFReset, they likely will not
557 * be zeroed when the driver starts. We'll save the first values read
558 * and use them as offsets to be subtracted from the raw values in order
559 * to report stats that count from zero. In the process, we also manage
560 * the potential roll-over.
561 **/
562static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
563 bool offset_loaded, u64 *offset, u64 *stat)
564{
565 u64 new_data;
566
Shannon Nelsonab600852014-01-17 15:36:39 -0800567 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000568 new_data = rd32(hw, loreg);
569 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
570 } else {
571 new_data = rd64(hw, loreg);
572 }
573 if (!offset_loaded)
574 *offset = new_data;
575 if (likely(new_data >= *offset))
576 *stat = new_data - *offset;
577 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400578 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000579 *stat &= 0xFFFFFFFFFFFFULL;
580}
581
582/**
583 * i40e_stat_update32 - read and update a 32 bit stat from the chip
584 * @hw: ptr to the hardware info
585 * @reg: the hw reg to read
586 * @offset_loaded: has the initial offset been loaded yet
587 * @offset: ptr to current offset value
588 * @stat: ptr to the stat
589 **/
590static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
591 bool offset_loaded, u64 *offset, u64 *stat)
592{
593 u32 new_data;
594
595 new_data = rd32(hw, reg);
596 if (!offset_loaded)
597 *offset = new_data;
598 if (likely(new_data >= *offset))
599 *stat = (u32)(new_data - *offset);
600 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400601 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000602}
603
604/**
605 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
606 * @vsi: the VSI to be updated
607 **/
608void i40e_update_eth_stats(struct i40e_vsi *vsi)
609{
610 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
611 struct i40e_pf *pf = vsi->back;
612 struct i40e_hw *hw = &pf->hw;
613 struct i40e_eth_stats *oes;
614 struct i40e_eth_stats *es; /* device's eth stats */
615
616 es = &vsi->eth_stats;
617 oes = &vsi->eth_stats_offsets;
618
619 /* Gather up the stats that the hw collects */
620 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
621 vsi->stat_offsets_loaded,
622 &oes->tx_errors, &es->tx_errors);
623 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
624 vsi->stat_offsets_loaded,
625 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000626 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
627 vsi->stat_offsets_loaded,
628 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
629 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
630 vsi->stat_offsets_loaded,
631 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000632
633 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
634 I40E_GLV_GORCL(stat_idx),
635 vsi->stat_offsets_loaded,
636 &oes->rx_bytes, &es->rx_bytes);
637 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
638 I40E_GLV_UPRCL(stat_idx),
639 vsi->stat_offsets_loaded,
640 &oes->rx_unicast, &es->rx_unicast);
641 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
642 I40E_GLV_MPRCL(stat_idx),
643 vsi->stat_offsets_loaded,
644 &oes->rx_multicast, &es->rx_multicast);
645 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
646 I40E_GLV_BPRCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->rx_broadcast, &es->rx_broadcast);
649
650 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
651 I40E_GLV_GOTCL(stat_idx),
652 vsi->stat_offsets_loaded,
653 &oes->tx_bytes, &es->tx_bytes);
654 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
655 I40E_GLV_UPTCL(stat_idx),
656 vsi->stat_offsets_loaded,
657 &oes->tx_unicast, &es->tx_unicast);
658 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
659 I40E_GLV_MPTCL(stat_idx),
660 vsi->stat_offsets_loaded,
661 &oes->tx_multicast, &es->tx_multicast);
662 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
663 I40E_GLV_BPTCL(stat_idx),
664 vsi->stat_offsets_loaded,
665 &oes->tx_broadcast, &es->tx_broadcast);
666 vsi->stat_offsets_loaded = true;
667}
668
669/**
670 * i40e_update_veb_stats - Update Switch component statistics
671 * @veb: the VEB being updated
672 **/
673static void i40e_update_veb_stats(struct i40e_veb *veb)
674{
675 struct i40e_pf *pf = veb->pf;
676 struct i40e_hw *hw = &pf->hw;
677 struct i40e_eth_stats *oes;
678 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400679 struct i40e_veb_tc_stats *veb_oes;
680 struct i40e_veb_tc_stats *veb_es;
681 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000682
683 idx = veb->stats_idx;
684 es = &veb->stats;
685 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400686 veb_es = &veb->tc_stats;
687 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000688
689 /* Gather up the stats that the hw collects */
690 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
691 veb->stat_offsets_loaded,
692 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000693 if (hw->revision_id > 0)
694 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
695 veb->stat_offsets_loaded,
696 &oes->rx_unknown_protocol,
697 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000698 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->rx_bytes, &es->rx_bytes);
701 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
702 veb->stat_offsets_loaded,
703 &oes->rx_unicast, &es->rx_unicast);
704 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
705 veb->stat_offsets_loaded,
706 &oes->rx_multicast, &es->rx_multicast);
707 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
708 veb->stat_offsets_loaded,
709 &oes->rx_broadcast, &es->rx_broadcast);
710
711 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
712 veb->stat_offsets_loaded,
713 &oes->tx_bytes, &es->tx_bytes);
714 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
715 veb->stat_offsets_loaded,
716 &oes->tx_unicast, &es->tx_unicast);
717 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
718 veb->stat_offsets_loaded,
719 &oes->tx_multicast, &es->tx_multicast);
720 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
721 veb->stat_offsets_loaded,
722 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400723 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
724 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
725 I40E_GLVEBTC_RPCL(i, idx),
726 veb->stat_offsets_loaded,
727 &veb_oes->tc_rx_packets[i],
728 &veb_es->tc_rx_packets[i]);
729 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
730 I40E_GLVEBTC_RBCL(i, idx),
731 veb->stat_offsets_loaded,
732 &veb_oes->tc_rx_bytes[i],
733 &veb_es->tc_rx_bytes[i]);
734 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
735 I40E_GLVEBTC_TPCL(i, idx),
736 veb->stat_offsets_loaded,
737 &veb_oes->tc_tx_packets[i],
738 &veb_es->tc_tx_packets[i]);
739 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
740 I40E_GLVEBTC_TBCL(i, idx),
741 veb->stat_offsets_loaded,
742 &veb_oes->tc_tx_bytes[i],
743 &veb_es->tc_tx_bytes[i]);
744 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000745 veb->stat_offsets_loaded = true;
746}
747
Vasu Dev38e00432014-08-01 13:27:03 -0700748#ifdef I40E_FCOE
749/**
750 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
751 * @vsi: the VSI that is capable of doing FCoE
752 **/
753static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
754{
755 struct i40e_pf *pf = vsi->back;
756 struct i40e_hw *hw = &pf->hw;
757 struct i40e_fcoe_stats *ofs;
758 struct i40e_fcoe_stats *fs; /* device's eth stats */
759 int idx;
760
761 if (vsi->type != I40E_VSI_FCOE)
762 return;
763
764 idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
765 fs = &vsi->fcoe_stats;
766 ofs = &vsi->fcoe_stats_offsets;
767
768 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
771 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
772 vsi->fcoe_stat_offsets_loaded,
773 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
774 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
775 vsi->fcoe_stat_offsets_loaded,
776 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
777 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
778 vsi->fcoe_stat_offsets_loaded,
779 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
780 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
781 vsi->fcoe_stat_offsets_loaded,
782 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
783 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
784 vsi->fcoe_stat_offsets_loaded,
785 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
786 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
787 vsi->fcoe_stat_offsets_loaded,
788 &ofs->fcoe_last_error, &fs->fcoe_last_error);
789 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
790 vsi->fcoe_stat_offsets_loaded,
791 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
792
793 vsi->fcoe_stat_offsets_loaded = true;
794}
795
796#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000797/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000798 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000799 * @vsi: the VSI to be updated
800 *
801 * There are a few instances where we store the same stat in a
802 * couple of different structs. This is partly because we have
803 * the netdev stats that need to be filled out, which is slightly
804 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000805 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000807static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808{
809 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000810 struct rtnl_link_stats64 *ons;
811 struct rtnl_link_stats64 *ns; /* netdev stats */
812 struct i40e_eth_stats *oes;
813 struct i40e_eth_stats *es; /* device's eth stats */
814 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000815 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000816 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000817 u64 bytes, packets;
818 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400819 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400820 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000821 u64 rx_p, rx_b;
822 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000823 u16 q;
824
825 if (test_bit(__I40E_DOWN, &vsi->state) ||
826 test_bit(__I40E_CONFIG_BUSY, &pf->state))
827 return;
828
829 ns = i40e_get_vsi_stats_struct(vsi);
830 ons = &vsi->net_stats_offsets;
831 es = &vsi->eth_stats;
832 oes = &vsi->eth_stats_offsets;
833
834 /* Gather up the netdev and vsi stats that the driver collects
835 * on the fly during packet processing
836 */
837 rx_b = rx_p = 0;
838 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400839 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000840 rx_page = 0;
841 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000842 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000843 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000844 /* locate Tx ring */
845 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000846
Alexander Duyck980e9b12013-09-28 06:01:03 +0000847 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700848 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000849 packets = p->stats.packets;
850 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700851 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000852 tx_b += bytes;
853 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000854 tx_restart += p->tx_stats.restart_queue;
855 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400856 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400857 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000858
859 /* Rx queue is part of the same block as Tx queue */
860 p = &p[1];
861 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700862 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000863 packets = p->stats.packets;
864 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700865 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000866 rx_b += bytes;
867 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000868 rx_buf += p->rx_stats.alloc_buff_failed;
869 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000870 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000871 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000872 vsi->tx_restart = tx_restart;
873 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400874 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400875 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000876 vsi->rx_page_failed = rx_page;
877 vsi->rx_buf_failed = rx_buf;
878
879 ns->rx_packets = rx_p;
880 ns->rx_bytes = rx_b;
881 ns->tx_packets = tx_p;
882 ns->tx_bytes = tx_b;
883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000885 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000886 ons->tx_errors = oes->tx_errors;
887 ns->tx_errors = es->tx_errors;
888 ons->multicast = oes->rx_multicast;
889 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000890 ons->rx_dropped = oes->rx_discards;
891 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000892 ons->tx_dropped = oes->tx_discards;
893 ns->tx_dropped = es->tx_discards;
894
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000895 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000896 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000897 ns->rx_crc_errors = pf->stats.crc_errors;
898 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
899 ns->rx_length_errors = pf->stats.rx_length_errors;
900 }
901}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000902
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000903/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000904 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000905 * @pf: the PF to be updated
906 **/
907static void i40e_update_pf_stats(struct i40e_pf *pf)
908{
909 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
910 struct i40e_hw_port_stats *nsd = &pf->stats;
911 struct i40e_hw *hw = &pf->hw;
912 u32 val;
913 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000914
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000915 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
916 I40E_GLPRT_GORCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
919 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
920 I40E_GLPRT_GOTCL(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
923 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
924 pf->stat_offsets_loaded,
925 &osd->eth.rx_discards,
926 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000927 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
928 I40E_GLPRT_UPRCL(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->eth.rx_unicast,
931 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000932 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
933 I40E_GLPRT_MPRCL(hw->port),
934 pf->stat_offsets_loaded,
935 &osd->eth.rx_multicast,
936 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000937 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
938 I40E_GLPRT_BPRCL(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->eth.rx_broadcast,
941 &nsd->eth.rx_broadcast);
942 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
943 I40E_GLPRT_UPTCL(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->eth.tx_unicast,
946 &nsd->eth.tx_unicast);
947 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
948 I40E_GLPRT_MPTCL(hw->port),
949 pf->stat_offsets_loaded,
950 &osd->eth.tx_multicast,
951 &nsd->eth.tx_multicast);
952 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
953 I40E_GLPRT_BPTCL(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->eth.tx_broadcast,
956 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000957
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000958 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->tx_dropped_link_down,
961 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000962
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000963 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000966
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000967 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000970
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000971 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
972 pf->stat_offsets_loaded,
973 &osd->mac_local_faults,
974 &nsd->mac_local_faults);
975 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->mac_remote_faults,
978 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000979
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000980 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->rx_length_errors,
983 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000984
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000985 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
986 pf->stat_offsets_loaded,
987 &osd->link_xon_rx, &nsd->link_xon_rx);
988 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
989 pf->stat_offsets_loaded,
990 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800991 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
992 pf->stat_offsets_loaded,
993 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000994 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000997
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000998 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800999 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1000 pf->stat_offsets_loaded,
1001 &osd->priority_xoff_rx[i],
1002 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001003 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001004 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001005 &osd->priority_xon_rx[i],
1006 &nsd->priority_xon_rx[i]);
1007 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001008 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001009 &osd->priority_xon_tx[i],
1010 &nsd->priority_xon_tx[i]);
1011 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001012 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001013 &osd->priority_xoff_tx[i],
1014 &nsd->priority_xoff_tx[i]);
1015 i40e_stat_update32(hw,
1016 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001017 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001018 &osd->priority_xon_2_xoff[i],
1019 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001020 }
1021
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001022 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1023 I40E_GLPRT_PRC64L(hw->port),
1024 pf->stat_offsets_loaded,
1025 &osd->rx_size_64, &nsd->rx_size_64);
1026 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1027 I40E_GLPRT_PRC127L(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->rx_size_127, &nsd->rx_size_127);
1030 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1031 I40E_GLPRT_PRC255L(hw->port),
1032 pf->stat_offsets_loaded,
1033 &osd->rx_size_255, &nsd->rx_size_255);
1034 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1035 I40E_GLPRT_PRC511L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->rx_size_511, &nsd->rx_size_511);
1038 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1039 I40E_GLPRT_PRC1023L(hw->port),
1040 pf->stat_offsets_loaded,
1041 &osd->rx_size_1023, &nsd->rx_size_1023);
1042 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1043 I40E_GLPRT_PRC1522L(hw->port),
1044 pf->stat_offsets_loaded,
1045 &osd->rx_size_1522, &nsd->rx_size_1522);
1046 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1047 I40E_GLPRT_PRC9522L(hw->port),
1048 pf->stat_offsets_loaded,
1049 &osd->rx_size_big, &nsd->rx_size_big);
1050
1051 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1052 I40E_GLPRT_PTC64L(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->tx_size_64, &nsd->tx_size_64);
1055 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1056 I40E_GLPRT_PTC127L(hw->port),
1057 pf->stat_offsets_loaded,
1058 &osd->tx_size_127, &nsd->tx_size_127);
1059 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1060 I40E_GLPRT_PTC255L(hw->port),
1061 pf->stat_offsets_loaded,
1062 &osd->tx_size_255, &nsd->tx_size_255);
1063 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1064 I40E_GLPRT_PTC511L(hw->port),
1065 pf->stat_offsets_loaded,
1066 &osd->tx_size_511, &nsd->tx_size_511);
1067 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1068 I40E_GLPRT_PTC1023L(hw->port),
1069 pf->stat_offsets_loaded,
1070 &osd->tx_size_1023, &nsd->tx_size_1023);
1071 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1072 I40E_GLPRT_PTC1522L(hw->port),
1073 pf->stat_offsets_loaded,
1074 &osd->tx_size_1522, &nsd->tx_size_1522);
1075 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1076 I40E_GLPRT_PTC9522L(hw->port),
1077 pf->stat_offsets_loaded,
1078 &osd->tx_size_big, &nsd->tx_size_big);
1079
1080 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1081 pf->stat_offsets_loaded,
1082 &osd->rx_undersize, &nsd->rx_undersize);
1083 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1084 pf->stat_offsets_loaded,
1085 &osd->rx_fragments, &nsd->rx_fragments);
1086 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1087 pf->stat_offsets_loaded,
1088 &osd->rx_oversize, &nsd->rx_oversize);
1089 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1090 pf->stat_offsets_loaded,
1091 &osd->rx_jabber, &nsd->rx_jabber);
1092
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001093 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001094 i40e_stat_update32(hw,
1095 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001096 pf->stat_offsets_loaded,
1097 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001098 i40e_stat_update32(hw,
1099 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001100 pf->stat_offsets_loaded,
1101 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001102 i40e_stat_update32(hw,
1103 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1104 pf->stat_offsets_loaded,
1105 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001106
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001107 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1108 nsd->tx_lpi_status =
1109 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1110 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1111 nsd->rx_lpi_status =
1112 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1113 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1114 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1115 pf->stat_offsets_loaded,
1116 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1117 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1118 pf->stat_offsets_loaded,
1119 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1120
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001121 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1122 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1123 nsd->fd_sb_status = true;
1124 else
1125 nsd->fd_sb_status = false;
1126
1127 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1128 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1129 nsd->fd_atr_status = true;
1130 else
1131 nsd->fd_atr_status = false;
1132
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001133 pf->stat_offsets_loaded = true;
1134}
1135
1136/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001137 * i40e_update_stats - Update the various statistics counters.
1138 * @vsi: the VSI to be updated
1139 *
1140 * Update the various stats for this VSI and its related entities.
1141 **/
1142void i40e_update_stats(struct i40e_vsi *vsi)
1143{
1144 struct i40e_pf *pf = vsi->back;
1145
1146 if (vsi == pf->vsi[pf->lan_vsi])
1147 i40e_update_pf_stats(pf);
1148
1149 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001150#ifdef I40E_FCOE
1151 i40e_update_fcoe_stats(vsi);
1152#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001153}
1154
1155/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1157 * @vsi: the VSI to be searched
1158 * @macaddr: the MAC address
1159 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001160 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001161 * @is_netdev: make sure its a netdev filter, else doesn't matter
1162 *
1163 * Returns ptr to the filter object or NULL
1164 **/
1165static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1166 u8 *macaddr, s16 vlan,
1167 bool is_vf, bool is_netdev)
1168{
1169 struct i40e_mac_filter *f;
1170
1171 if (!vsi || !macaddr)
1172 return NULL;
1173
1174 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1175 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1176 (vlan == f->vlan) &&
1177 (!is_vf || f->is_vf) &&
1178 (!is_netdev || f->is_netdev))
1179 return f;
1180 }
1181 return NULL;
1182}
1183
1184/**
1185 * i40e_find_mac - Find a mac addr in the macvlan filters list
1186 * @vsi: the VSI to be searched
1187 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001188 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001189 * @is_netdev: make sure its a netdev filter, else doesn't matter
1190 *
1191 * Returns the first filter with the provided MAC address or NULL if
1192 * MAC address was not found
1193 **/
1194struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1195 bool is_vf, bool is_netdev)
1196{
1197 struct i40e_mac_filter *f;
1198
1199 if (!vsi || !macaddr)
1200 return NULL;
1201
1202 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1203 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1204 (!is_vf || f->is_vf) &&
1205 (!is_netdev || f->is_netdev))
1206 return f;
1207 }
1208 return NULL;
1209}
1210
1211/**
1212 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1213 * @vsi: the VSI to be searched
1214 *
1215 * Returns true if VSI is in vlan mode or false otherwise
1216 **/
1217bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1218{
1219 struct i40e_mac_filter *f;
1220
1221 /* Only -1 for all the filters denotes not in vlan mode
1222 * so we have to go through all the list in order to make sure
1223 */
1224 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001225 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001226 return true;
1227 }
1228
1229 return false;
1230}
1231
1232/**
1233 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1234 * @vsi: the VSI to be searched
1235 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001236 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001237 * @is_netdev: true if it is a netdev
1238 *
1239 * Goes through all the macvlan filters and adds a
1240 * macvlan filter for each unique vlan that already exists
1241 *
1242 * Returns first filter found on success, else NULL
1243 **/
1244struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1245 bool is_vf, bool is_netdev)
1246{
1247 struct i40e_mac_filter *f;
1248
1249 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001250 if (vsi->info.pvid)
1251 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001252 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1253 is_vf, is_netdev)) {
1254 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001255 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001256 return NULL;
1257 }
1258 }
1259
1260 return list_first_entry_or_null(&vsi->mac_filter_list,
1261 struct i40e_mac_filter, list);
1262}
1263
1264/**
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001265 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1266 * @vsi: the VSI to be searched
1267 * @macaddr: the mac address to be removed
1268 * @is_vf: true if it is a VF
1269 * @is_netdev: true if it is a netdev
1270 *
1271 * Removes a given MAC address from a VSI, regardless of VLAN
1272 *
1273 * Returns 0 for success, or error
1274 **/
1275int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1276 bool is_vf, bool is_netdev)
1277{
1278 struct i40e_mac_filter *f = NULL;
1279 int changed = 0;
1280
1281 WARN(!spin_is_locked(&vsi->mac_filter_list_lock),
1282 "Missing mac_filter_list_lock\n");
1283 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1284 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1285 (is_vf == f->is_vf) &&
1286 (is_netdev == f->is_netdev)) {
1287 f->counter--;
1288 f->changed = true;
1289 changed = 1;
1290 }
1291 }
1292 if (changed) {
1293 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1294 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1295 return 0;
1296 }
1297 return -ENOENT;
1298}
1299
1300/**
Greg Rose8c27d422014-05-22 06:31:56 +00001301 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1302 * @vsi: the PF Main VSI - inappropriate for any other VSI
1303 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001304 *
1305 * Some older firmware configurations set up a default promiscuous VLAN
1306 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001307 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001308static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001309{
1310 struct i40e_aqc_remove_macvlan_element_data element;
1311 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001312 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001313
1314 /* Only appropriate for the PF main VSI */
1315 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001316 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001317
Shannon Nelson30650cc2014-07-29 04:01:50 +00001318 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001319 ether_addr_copy(element.mac_addr, macaddr);
1320 element.vlan_tag = 0;
1321 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1322 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001323 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1324 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001325 return -ENOENT;
1326
1327 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001328}
1329
1330/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001331 * i40e_add_filter - Add a mac/vlan filter to the VSI
1332 * @vsi: the VSI to be searched
1333 * @macaddr: the MAC address
1334 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001335 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001336 * @is_netdev: make sure its a netdev filter, else doesn't matter
1337 *
1338 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001339 *
1340 * NOTE: This function is expected to be called with mac_filter_list_lock
1341 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001342 **/
1343struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1344 u8 *macaddr, s16 vlan,
1345 bool is_vf, bool is_netdev)
1346{
1347 struct i40e_mac_filter *f;
1348
1349 if (!vsi || !macaddr)
1350 return NULL;
1351
1352 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1353 if (!f) {
1354 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1355 if (!f)
1356 goto add_filter_out;
1357
Greg Rose9a173902014-05-22 06:32:02 +00001358 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001359 f->vlan = vlan;
1360 f->changed = true;
1361
1362 INIT_LIST_HEAD(&f->list);
1363 list_add(&f->list, &vsi->mac_filter_list);
1364 }
1365
1366 /* increment counter and add a new flag if needed */
1367 if (is_vf) {
1368 if (!f->is_vf) {
1369 f->is_vf = true;
1370 f->counter++;
1371 }
1372 } else if (is_netdev) {
1373 if (!f->is_netdev) {
1374 f->is_netdev = true;
1375 f->counter++;
1376 }
1377 } else {
1378 f->counter++;
1379 }
1380
1381 /* changed tells sync_filters_subtask to
1382 * push the filter down to the firmware
1383 */
1384 if (f->changed) {
1385 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1386 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1387 }
1388
1389add_filter_out:
1390 return f;
1391}
1392
1393/**
1394 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1395 * @vsi: the VSI to be searched
1396 * @macaddr: the MAC address
1397 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001398 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001399 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001400 *
1401 * NOTE: This function is expected to be called with mac_filter_list_lock
1402 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403 **/
1404void i40e_del_filter(struct i40e_vsi *vsi,
1405 u8 *macaddr, s16 vlan,
1406 bool is_vf, bool is_netdev)
1407{
1408 struct i40e_mac_filter *f;
1409
1410 if (!vsi || !macaddr)
1411 return;
1412
1413 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1414 if (!f || f->counter == 0)
1415 return;
1416
1417 if (is_vf) {
1418 if (f->is_vf) {
1419 f->is_vf = false;
1420 f->counter--;
1421 }
1422 } else if (is_netdev) {
1423 if (f->is_netdev) {
1424 f->is_netdev = false;
1425 f->counter--;
1426 }
1427 } else {
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001428 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001429 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001430
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001431 min_f += (f->is_vf ? 1 : 0);
1432 min_f += (f->is_netdev ? 1 : 0);
1433
1434 if (f->counter > min_f)
1435 f->counter--;
1436 }
1437
1438 /* counter == 0 tells sync_filters_subtask to
1439 * remove the filter from the firmware's list
1440 */
1441 if (f->counter == 0) {
1442 f->changed = true;
1443 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1444 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1445 }
1446}
1447
1448/**
1449 * i40e_set_mac - NDO callback to set mac address
1450 * @netdev: network interface device structure
1451 * @p: pointer to an address structure
1452 *
1453 * Returns 0 on success, negative on failure
1454 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001455#ifdef I40E_FCOE
1456int i40e_set_mac(struct net_device *netdev, void *p)
1457#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001458static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001459#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460{
1461 struct i40e_netdev_priv *np = netdev_priv(netdev);
1462 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001463 struct i40e_pf *pf = vsi->back;
1464 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001465 struct sockaddr *addr = p;
1466 struct i40e_mac_filter *f;
1467
1468 if (!is_valid_ether_addr(addr->sa_data))
1469 return -EADDRNOTAVAIL;
1470
Shannon Nelson30650cc2014-07-29 04:01:50 +00001471 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1472 netdev_info(netdev, "already using mac address %pM\n",
1473 addr->sa_data);
1474 return 0;
1475 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001476
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001477 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1478 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1479 return -EADDRNOTAVAIL;
1480
Shannon Nelson30650cc2014-07-29 04:01:50 +00001481 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1482 netdev_info(netdev, "returning to hw mac address %pM\n",
1483 hw->mac.addr);
1484 else
1485 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1486
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001487 if (vsi->type == I40E_VSI_MAIN) {
1488 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001489
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001490 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001491 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001492 addr->sa_data, NULL);
1493 if (ret) {
1494 netdev_info(netdev,
1495 "Addr change for Main VSI failed: %d\n",
1496 ret);
1497 return -EADDRNOTAVAIL;
1498 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001499 }
1500
Shannon Nelson30650cc2014-07-29 04:01:50 +00001501 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1502 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001503
Shannon Nelson30650cc2014-07-29 04:01:50 +00001504 memset(&element, 0, sizeof(element));
1505 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1506 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1507 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1508 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001509 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001510 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1511 false, false);
Kiran Patil21659032015-09-30 14:09:03 -04001512 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001513 }
1514
Shannon Nelson30650cc2014-07-29 04:01:50 +00001515 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1516 struct i40e_aqc_add_macvlan_element_data element;
1517
1518 memset(&element, 0, sizeof(element));
1519 ether_addr_copy(element.mac_addr, hw->mac.addr);
1520 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1521 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1522 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001523 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001524 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1525 false, false);
1526 if (f)
1527 f->is_laa = true;
Kiran Patil21659032015-09-30 14:09:03 -04001528 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001529 }
1530
Shannon Nelson30650cc2014-07-29 04:01:50 +00001531 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Mitch Williamsea02e902015-11-09 15:35:50 -08001532
1533 return i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001534}
1535
1536/**
1537 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1538 * @vsi: the VSI being setup
1539 * @ctxt: VSI context structure
1540 * @enabled_tc: Enabled TCs bitmap
1541 * @is_add: True if called before Add VSI
1542 *
1543 * Setup VSI queue mapping for enabled traffic classes.
1544 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001545#ifdef I40E_FCOE
1546void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1547 struct i40e_vsi_context *ctxt,
1548 u8 enabled_tc,
1549 bool is_add)
1550#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001551static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1552 struct i40e_vsi_context *ctxt,
1553 u8 enabled_tc,
1554 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001555#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001556{
1557 struct i40e_pf *pf = vsi->back;
1558 u16 sections = 0;
1559 u8 netdev_tc = 0;
1560 u16 numtc = 0;
1561 u16 qcount;
1562 u8 offset;
1563 u16 qmap;
1564 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001565 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001566
1567 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1568 offset = 0;
1569
1570 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1571 /* Find numtc from enabled TC bitmap */
1572 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001573 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001574 numtc++;
1575 }
1576 if (!numtc) {
1577 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1578 numtc = 1;
1579 }
1580 } else {
1581 /* At least TC0 is enabled in case of non-DCB case */
1582 numtc = 1;
1583 }
1584
1585 vsi->tc_config.numtc = numtc;
1586 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001587 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001588 /* In MFP case we can have a much lower count of MSIx
1589 * vectors available and so we need to lower the used
1590 * q count.
1591 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001592 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1593 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1594 else
1595 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001596 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001597 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001598
1599 /* Setup queue offset/count for all TCs for given VSI */
1600 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1601 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001602 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001603 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604 int pow, num_qps;
1605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606 switch (vsi->type) {
1607 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001608 qcount = min_t(int, pf->alloc_rss_size,
1609 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001610 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001611#ifdef I40E_FCOE
1612 case I40E_VSI_FCOE:
1613 qcount = num_tc_qps;
1614 break;
1615#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001616 case I40E_VSI_FDIR:
1617 case I40E_VSI_SRIOV:
1618 case I40E_VSI_VMDQ2:
1619 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001620 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001621 WARN_ON(i != 0);
1622 break;
1623 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001624 vsi->tc_config.tc_info[i].qoffset = offset;
1625 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001626
Shannon Nelson1e200e42015-02-27 09:15:24 +00001627 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001628 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001630 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001631 pow++;
1632 num_qps >>= 1;
1633 }
1634
1635 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1636 qmap =
1637 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1638 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1639
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001640 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001641 } else {
1642 /* TC is not enabled so set the offset to
1643 * default queue and allocate one queue
1644 * for the given TC.
1645 */
1646 vsi->tc_config.tc_info[i].qoffset = 0;
1647 vsi->tc_config.tc_info[i].qcount = 1;
1648 vsi->tc_config.tc_info[i].netdev_tc = 0;
1649
1650 qmap = 0;
1651 }
1652 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1653 }
1654
1655 /* Set actual Tx/Rx queue pairs */
1656 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001657 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1658 if (vsi->req_queue_pairs > 0)
1659 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001660 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001661 vsi->num_queue_pairs = pf->num_lan_msix;
1662 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001663
1664 /* Scheduler section valid can only be set for ADD VSI */
1665 if (is_add) {
1666 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1667
1668 ctxt->info.up_enable_bits = enabled_tc;
1669 }
1670 if (vsi->type == I40E_VSI_SRIOV) {
1671 ctxt->info.mapping_flags |=
1672 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1673 for (i = 0; i < vsi->num_queue_pairs; i++)
1674 ctxt->info.queue_mapping[i] =
1675 cpu_to_le16(vsi->base_queue + i);
1676 } else {
1677 ctxt->info.mapping_flags |=
1678 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1679 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1680 }
1681 ctxt->info.valid_sections |= cpu_to_le16(sections);
1682}
1683
1684/**
1685 * i40e_set_rx_mode - NDO callback to set the netdev filters
1686 * @netdev: network interface device structure
1687 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001688#ifdef I40E_FCOE
1689void i40e_set_rx_mode(struct net_device *netdev)
1690#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001691static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001692#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001693{
1694 struct i40e_netdev_priv *np = netdev_priv(netdev);
1695 struct i40e_mac_filter *f, *ftmp;
1696 struct i40e_vsi *vsi = np->vsi;
1697 struct netdev_hw_addr *uca;
1698 struct netdev_hw_addr *mca;
1699 struct netdev_hw_addr *ha;
1700
Kiran Patil21659032015-09-30 14:09:03 -04001701 spin_lock_bh(&vsi->mac_filter_list_lock);
1702
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001703 /* add addr if not already in the filter list */
1704 netdev_for_each_uc_addr(uca, netdev) {
1705 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1706 if (i40e_is_vsi_in_vlan(vsi))
1707 i40e_put_mac_in_vlan(vsi, uca->addr,
1708 false, true);
1709 else
1710 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1711 false, true);
1712 }
1713 }
1714
1715 netdev_for_each_mc_addr(mca, netdev) {
1716 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1717 if (i40e_is_vsi_in_vlan(vsi))
1718 i40e_put_mac_in_vlan(vsi, mca->addr,
1719 false, true);
1720 else
1721 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1722 false, true);
1723 }
1724 }
1725
1726 /* remove filter if not in netdev list */
1727 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001728
1729 if (!f->is_netdev)
1730 continue;
1731
Shannon Nelson2f41f332015-08-26 15:14:20 -04001732 netdev_for_each_mc_addr(mca, netdev)
1733 if (ether_addr_equal(mca->addr, f->macaddr))
1734 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001735
Shannon Nelson2f41f332015-08-26 15:14:20 -04001736 netdev_for_each_uc_addr(uca, netdev)
1737 if (ether_addr_equal(uca->addr, f->macaddr))
1738 goto bottom_of_search_loop;
1739
1740 for_each_dev_addr(netdev, ha)
1741 if (ether_addr_equal(ha->addr, f->macaddr))
1742 goto bottom_of_search_loop;
1743
1744 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1745 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1746
1747bottom_of_search_loop:
1748 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001749 }
Kiran Patil21659032015-09-30 14:09:03 -04001750 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001751
1752 /* check for other flag changes */
1753 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1754 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1755 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1756 }
1757}
1758
1759/**
Kiran Patil21659032015-09-30 14:09:03 -04001760 * i40e_mac_filter_entry_clone - Clones a MAC filter entry
1761 * @src: source MAC filter entry to be clones
1762 *
1763 * Returns the pointer to newly cloned MAC filter entry or NULL
1764 * in case of error
1765 **/
1766static struct i40e_mac_filter *i40e_mac_filter_entry_clone(
1767 struct i40e_mac_filter *src)
1768{
1769 struct i40e_mac_filter *f;
1770
1771 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1772 if (!f)
1773 return NULL;
1774 *f = *src;
1775
1776 INIT_LIST_HEAD(&f->list);
1777
1778 return f;
1779}
1780
1781/**
1782 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1783 * @vsi: pointer to vsi struct
1784 * @from: Pointer to list which contains MAC filter entries - changes to
1785 * those entries needs to be undone.
1786 *
1787 * MAC filter entries from list were slated to be removed from device.
1788 **/
1789static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1790 struct list_head *from)
1791{
1792 struct i40e_mac_filter *f, *ftmp;
1793
1794 list_for_each_entry_safe(f, ftmp, from, list) {
1795 f->changed = true;
1796 /* Move the element back into MAC filter list*/
1797 list_move_tail(&f->list, &vsi->mac_filter_list);
1798 }
1799}
1800
1801/**
1802 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1803 * @vsi: pointer to vsi struct
1804 *
1805 * MAC filter entries from list were slated to be added from device.
1806 **/
1807static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi)
1808{
1809 struct i40e_mac_filter *f, *ftmp;
1810
1811 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1812 if (!f->changed && f->counter)
1813 f->changed = true;
1814 }
1815}
1816
1817/**
1818 * i40e_cleanup_add_list - Deletes the element from add list and release
1819 * memory
1820 * @add_list: Pointer to list which contains MAC filter entries
1821 **/
1822static void i40e_cleanup_add_list(struct list_head *add_list)
1823{
1824 struct i40e_mac_filter *f, *ftmp;
1825
1826 list_for_each_entry_safe(f, ftmp, add_list, list) {
1827 list_del(&f->list);
1828 kfree(f);
1829 }
1830}
1831
1832/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001833 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1834 * @vsi: ptr to the VSI
1835 *
1836 * Push any outstanding VSI filter changes through the AdminQ.
1837 *
1838 * Returns 0 or error value
1839 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001840int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001841{
Kiran Patil21659032015-09-30 14:09:03 -04001842 struct list_head tmp_del_list, tmp_add_list;
1843 struct i40e_mac_filter *f, *ftmp, *fclone;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001844 bool promisc_forced_on = false;
1845 bool add_happened = false;
1846 int filter_list_len = 0;
1847 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001848 i40e_status aq_ret = 0;
Kiran Patil21659032015-09-30 14:09:03 -04001849 bool err_cond = false;
Mitch Williamsea02e902015-11-09 15:35:50 -08001850 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001851 struct i40e_pf *pf;
1852 int num_add = 0;
1853 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001854 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001855 u16 cmd_flags;
1856
1857 /* empty array typed pointers, kcalloc later */
1858 struct i40e_aqc_add_macvlan_element_data *add_list;
1859 struct i40e_aqc_remove_macvlan_element_data *del_list;
1860
1861 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1862 usleep_range(1000, 2000);
1863 pf = vsi->back;
1864
1865 if (vsi->netdev) {
1866 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1867 vsi->current_netdev_flags = vsi->netdev->flags;
1868 }
1869
Kiran Patil21659032015-09-30 14:09:03 -04001870 INIT_LIST_HEAD(&tmp_del_list);
1871 INIT_LIST_HEAD(&tmp_add_list);
1872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001873 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1874 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1875
Kiran Patil21659032015-09-30 14:09:03 -04001876 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001877 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1878 if (!f->changed)
1879 continue;
1880
1881 if (f->counter != 0)
1882 continue;
1883 f->changed = false;
Kiran Patil21659032015-09-30 14:09:03 -04001884
1885 /* Move the element into temporary del_list */
1886 list_move_tail(&f->list, &tmp_del_list);
1887 }
1888
1889 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1890 if (!f->changed)
1891 continue;
1892
1893 if (f->counter == 0)
1894 continue;
1895 f->changed = false;
1896
1897 /* Clone MAC filter entry and add into temporary list */
1898 fclone = i40e_mac_filter_entry_clone(f);
1899 if (!fclone) {
1900 err_cond = true;
1901 break;
1902 }
1903 list_add_tail(&fclone->list, &tmp_add_list);
1904 }
1905
1906 /* if failed to clone MAC filter entry - undo */
1907 if (err_cond) {
1908 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1909 i40e_undo_add_filter_entries(vsi);
1910 }
1911 spin_unlock_bh(&vsi->mac_filter_list_lock);
1912
Mitch Williamsea02e902015-11-09 15:35:50 -08001913 if (err_cond) {
Kiran Patil21659032015-09-30 14:09:03 -04001914 i40e_cleanup_add_list(&tmp_add_list);
Mitch Williamsea02e902015-11-09 15:35:50 -08001915 retval = -ENOMEM;
1916 goto out;
1917 }
Kiran Patil21659032015-09-30 14:09:03 -04001918 }
1919
1920 /* Now process 'del_list' outside the lock */
1921 if (!list_empty(&tmp_del_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08001922 int del_list_size;
1923
Kiran Patil21659032015-09-30 14:09:03 -04001924 filter_list_len = pf->hw.aq.asq_buf_size /
1925 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Shannon Nelsonf1199992015-11-19 11:34:23 -08001926 del_list_size = filter_list_len *
1927 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1928 del_list = kzalloc(del_list_size, GFP_KERNEL);
Kiran Patil21659032015-09-30 14:09:03 -04001929 if (!del_list) {
1930 i40e_cleanup_add_list(&tmp_add_list);
1931
1932 /* Undo VSI's MAC filter entry element updates */
1933 spin_lock_bh(&vsi->mac_filter_list_lock);
1934 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1935 i40e_undo_add_filter_entries(vsi);
1936 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001937 retval = -ENOMEM;
1938 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001939 }
1940
1941 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001942 cmd_flags = 0;
1943
1944 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001945 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001946 del_list[num_del].vlan_tag =
1947 cpu_to_le16((u16)(f->vlan ==
1948 I40E_VLAN_ANY ? 0 : f->vlan));
1949
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001950 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1951 del_list[num_del].flags = cmd_flags;
1952 num_del++;
1953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001954 /* flush a full buffer */
1955 if (num_del == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001956 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
1957 vsi->seid,
1958 del_list,
1959 num_del,
1960 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001961 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962 num_del = 0;
Shannon Nelsonf1199992015-11-19 11:34:23 -08001963 memset(del_list, 0, del_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001964
Mitch Williamsea02e902015-11-09 15:35:50 -08001965 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT) {
1966 retval = -EIO;
Kiran Patil21659032015-09-30 14:09:03 -04001967 dev_err(&pf->pdev->dev,
1968 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08001969 i40e_stat_str(&pf->hw, aq_ret),
Kiran Patil21659032015-09-30 14:09:03 -04001970 i40e_aq_str(&pf->hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001971 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001972 }
Kiran Patil21659032015-09-30 14:09:03 -04001973 /* Release memory for MAC filter entries which were
1974 * synced up with HW.
1975 */
1976 list_del(&f->list);
1977 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001978 }
Kiran Patil21659032015-09-30 14:09:03 -04001979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001980 if (num_del) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001981 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
1982 del_list, num_del,
1983 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001984 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985 num_del = 0;
1986
Mitch Williamsea02e902015-11-09 15:35:50 -08001987 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001988 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001989 "ignoring delete macvlan error, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08001990 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001991 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992 }
1993
1994 kfree(del_list);
1995 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001996 }
1997
1998 if (!list_empty(&tmp_add_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08001999 int add_list_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002000
2001 /* do all the adds now */
2002 filter_list_len = pf->hw.aq.asq_buf_size /
2003 sizeof(struct i40e_aqc_add_macvlan_element_data),
Shannon Nelsonf1199992015-11-19 11:34:23 -08002004 add_list_size = filter_list_len *
2005 sizeof(struct i40e_aqc_add_macvlan_element_data);
2006 add_list = kzalloc(add_list_size, GFP_KERNEL);
Kiran Patil21659032015-09-30 14:09:03 -04002007 if (!add_list) {
2008 /* Purge element from temporary lists */
2009 i40e_cleanup_add_list(&tmp_add_list);
2010
2011 /* Undo add filter entries from VSI MAC filter list */
2012 spin_lock_bh(&vsi->mac_filter_list_lock);
2013 i40e_undo_add_filter_entries(vsi);
2014 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08002015 retval = -ENOMEM;
2016 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04002017 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018
Kiran Patil21659032015-09-30 14:09:03 -04002019 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002020
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002021 add_happened = true;
2022 cmd_flags = 0;
2023
2024 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00002025 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002026 add_list[num_add].vlan_tag =
2027 cpu_to_le16(
2028 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
2029 add_list[num_add].queue_number = 0;
2030
2031 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002032 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2033 num_add++;
2034
2035 /* flush a full buffer */
2036 if (num_add == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002037 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2038 add_list, num_add,
2039 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002040 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002041 num_add = 0;
2042
Mitch Williamsea02e902015-11-09 15:35:50 -08002043 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002044 break;
Shannon Nelsonf1199992015-11-19 11:34:23 -08002045 memset(add_list, 0, add_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002046 }
Kiran Patil21659032015-09-30 14:09:03 -04002047 /* Entries from tmp_add_list were cloned from MAC
2048 * filter list, hence clean those cloned entries
2049 */
2050 list_del(&f->list);
2051 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 }
Kiran Patil21659032015-09-30 14:09:03 -04002053
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002054 if (num_add) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002055 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2056 add_list, num_add, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002057 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002058 num_add = 0;
2059 }
2060 kfree(add_list);
2061 add_list = NULL;
2062
Mitch Williamsea02e902015-11-09 15:35:50 -08002063 if (add_happened && aq_ret && aq_err != I40E_AQ_RC_EINVAL) {
2064 retval = i40e_aq_rc_to_posix(aq_ret, aq_err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002065 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002066 "add filter failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002067 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002068 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002069 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
2070 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2071 &vsi->state)) {
2072 promisc_forced_on = true;
2073 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2074 &vsi->state);
2075 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
2076 }
2077 }
2078 }
2079
2080 /* check for changes in promiscuous modes */
2081 if (changed_flags & IFF_ALLMULTI) {
2082 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002083
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002084 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002085 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2086 vsi->seid,
2087 cur_multipromisc,
2088 NULL);
2089 if (aq_ret) {
2090 retval = i40e_aq_rc_to_posix(aq_ret,
2091 pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002092 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002093 "set multi promisc failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002094 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002095 i40e_aq_str(&pf->hw,
2096 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002097 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 }
2099 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
2100 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2103 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2104 &vsi->state));
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002105 if (vsi->type == I40E_VSI_MAIN && pf->lan_veb != I40E_NO_VEB) {
2106 /* set defport ON for Main VSI instead of true promisc
2107 * this way we will get all unicast/multicast and VLAN
2108 * promisc behavior but will not get VF or VMDq traffic
2109 * replicated on the Main VSI.
2110 */
2111 if (pf->cur_promisc != cur_promisc) {
2112 pf->cur_promisc = cur_promisc;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002113 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002114 }
2115 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002116 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002117 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002118 vsi->seid,
2119 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002120 if (aq_ret) {
2121 retval =
2122 i40e_aq_rc_to_posix(aq_ret,
2123 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002124 dev_info(&pf->pdev->dev,
2125 "set unicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002126 aq_ret, pf->hw.aq.asq_last_status);
2127 }
2128 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002129 &vsi->back->hw,
2130 vsi->seid,
2131 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002132 if (aq_ret) {
2133 retval =
2134 i40e_aq_rc_to_posix(aq_ret,
2135 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002136 dev_info(&pf->pdev->dev,
2137 "set multicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002138 aq_ret, pf->hw.aq.asq_last_status);
2139 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002140 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002141 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2142 vsi->seid,
2143 cur_promisc, NULL);
2144 if (aq_ret) {
2145 retval = i40e_aq_rc_to_posix(aq_ret,
2146 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002147 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002148 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002149 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002150 i40e_aq_str(&pf->hw,
2151 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002153 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002154out:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002155 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002156 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002157}
2158
2159/**
2160 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2161 * @pf: board private structure
2162 **/
2163static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2164{
2165 int v;
2166
2167 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2168 return;
2169 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2170
Mitch Williams505682c2014-05-20 08:01:37 +00002171 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002172 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002173 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2174 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2175
2176 if (ret) {
2177 /* come back and try again later */
2178 pf->flags |= I40E_FLAG_FILTER_SYNC;
2179 break;
2180 }
2181 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002182 }
2183}
2184
2185/**
2186 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2187 * @netdev: network interface device structure
2188 * @new_mtu: new value for maximum frame size
2189 *
2190 * Returns 0 on success, negative on failure
2191 **/
2192static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2193{
2194 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002195 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002196 struct i40e_vsi *vsi = np->vsi;
2197
2198 /* MTU < 68 is an error and causes problems on some kernels */
2199 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2200 return -EINVAL;
2201
2202 netdev_info(netdev, "changing MTU from %d to %d\n",
2203 netdev->mtu, new_mtu);
2204 netdev->mtu = new_mtu;
2205 if (netif_running(netdev))
2206 i40e_vsi_reinit_locked(vsi);
2207
2208 return 0;
2209}
2210
2211/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002212 * i40e_ioctl - Access the hwtstamp interface
2213 * @netdev: network interface device structure
2214 * @ifr: interface request data
2215 * @cmd: ioctl command
2216 **/
2217int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2218{
2219 struct i40e_netdev_priv *np = netdev_priv(netdev);
2220 struct i40e_pf *pf = np->vsi->back;
2221
2222 switch (cmd) {
2223 case SIOCGHWTSTAMP:
2224 return i40e_ptp_get_ts_config(pf, ifr);
2225 case SIOCSHWTSTAMP:
2226 return i40e_ptp_set_ts_config(pf, ifr);
2227 default:
2228 return -EOPNOTSUPP;
2229 }
2230}
2231
2232/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2234 * @vsi: the vsi being adjusted
2235 **/
2236void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2237{
2238 struct i40e_vsi_context ctxt;
2239 i40e_status ret;
2240
2241 if ((vsi->info.valid_sections &
2242 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2243 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2244 return; /* already enabled */
2245
2246 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2247 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2248 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2249
2250 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002251 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002252 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2253 if (ret) {
2254 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002255 "update vlan stripping failed, err %s aq_err %s\n",
2256 i40e_stat_str(&vsi->back->hw, ret),
2257 i40e_aq_str(&vsi->back->hw,
2258 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002259 }
2260}
2261
2262/**
2263 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2264 * @vsi: the vsi being adjusted
2265 **/
2266void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2267{
2268 struct i40e_vsi_context ctxt;
2269 i40e_status ret;
2270
2271 if ((vsi->info.valid_sections &
2272 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2273 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2274 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2275 return; /* already disabled */
2276
2277 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2278 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2279 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2280
2281 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002282 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002283 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2284 if (ret) {
2285 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002286 "update vlan stripping failed, err %s aq_err %s\n",
2287 i40e_stat_str(&vsi->back->hw, ret),
2288 i40e_aq_str(&vsi->back->hw,
2289 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002290 }
2291}
2292
2293/**
2294 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2295 * @netdev: network interface to be adjusted
2296 * @features: netdev features to test if VLAN offload is enabled or not
2297 **/
2298static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2299{
2300 struct i40e_netdev_priv *np = netdev_priv(netdev);
2301 struct i40e_vsi *vsi = np->vsi;
2302
2303 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2304 i40e_vlan_stripping_enable(vsi);
2305 else
2306 i40e_vlan_stripping_disable(vsi);
2307}
2308
2309/**
2310 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2311 * @vsi: the vsi being configured
2312 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2313 **/
2314int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2315{
2316 struct i40e_mac_filter *f, *add_f;
2317 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002318
2319 is_vf = (vsi->type == I40E_VSI_SRIOV);
2320 is_netdev = !!(vsi->netdev);
2321
Kiran Patil21659032015-09-30 14:09:03 -04002322 /* Locked once because all functions invoked below iterates list*/
2323 spin_lock_bh(&vsi->mac_filter_list_lock);
2324
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002325 if (is_netdev) {
2326 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2327 is_vf, is_netdev);
2328 if (!add_f) {
2329 dev_info(&vsi->back->pdev->dev,
2330 "Could not add vlan filter %d for %pM\n",
2331 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002332 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002333 return -ENOMEM;
2334 }
2335 }
2336
2337 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2338 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2339 if (!add_f) {
2340 dev_info(&vsi->back->pdev->dev,
2341 "Could not add vlan filter %d for %pM\n",
2342 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002343 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002344 return -ENOMEM;
2345 }
2346 }
2347
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002348 /* Now if we add a vlan tag, make sure to check if it is the first
2349 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2350 * with 0, so we now accept untagged and specified tagged traffic
2351 * (and not any taged and untagged)
2352 */
2353 if (vid > 0) {
2354 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2355 I40E_VLAN_ANY,
2356 is_vf, is_netdev)) {
2357 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2358 I40E_VLAN_ANY, is_vf, is_netdev);
2359 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2360 is_vf, is_netdev);
2361 if (!add_f) {
2362 dev_info(&vsi->back->pdev->dev,
2363 "Could not add filter 0 for %pM\n",
2364 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 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002369 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002370
Greg Rose8d82a7c2014-01-13 16:13:04 -08002371 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2372 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002373 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002374 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2375 is_vf, is_netdev))
2376 continue;
2377 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2378 is_vf, is_netdev);
2379 add_f = i40e_add_filter(vsi, f->macaddr,
2380 0, is_vf, is_netdev);
2381 if (!add_f) {
2382 dev_info(&vsi->back->pdev->dev,
2383 "Could not add filter 0 for %pM\n",
2384 f->macaddr);
2385 spin_unlock_bh(&vsi->mac_filter_list_lock);
2386 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002387 }
2388 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002389 }
2390
Kiran Patil21659032015-09-30 14:09:03 -04002391 spin_unlock_bh(&vsi->mac_filter_list_lock);
2392
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002393 /* schedule our worker thread which will take care of
2394 * applying the new filter changes
2395 */
2396 i40e_service_event_schedule(vsi->back);
2397 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398}
2399
2400/**
2401 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2402 * @vsi: the vsi being configured
2403 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002404 *
2405 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002406 **/
2407int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2408{
2409 struct net_device *netdev = vsi->netdev;
2410 struct i40e_mac_filter *f, *add_f;
2411 bool is_vf, is_netdev;
2412 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002413
2414 is_vf = (vsi->type == I40E_VSI_SRIOV);
2415 is_netdev = !!(netdev);
2416
Kiran Patil21659032015-09-30 14:09:03 -04002417 /* Locked once because all functions invoked below iterates list */
2418 spin_lock_bh(&vsi->mac_filter_list_lock);
2419
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002420 if (is_netdev)
2421 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2422
2423 list_for_each_entry(f, &vsi->mac_filter_list, list)
2424 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2425
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002426 /* go through all the filters for this VSI and if there is only
2427 * vid == 0 it means there are no other filters, so vid 0 must
2428 * be replaced with -1. This signifies that we should from now
2429 * on accept any traffic (with any tag present, or untagged)
2430 */
2431 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2432 if (is_netdev) {
2433 if (f->vlan &&
2434 ether_addr_equal(netdev->dev_addr, f->macaddr))
2435 filter_count++;
2436 }
2437
2438 if (f->vlan)
2439 filter_count++;
2440 }
2441
2442 if (!filter_count && is_netdev) {
2443 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2444 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2445 is_vf, is_netdev);
2446 if (!f) {
2447 dev_info(&vsi->back->pdev->dev,
2448 "Could not add filter %d for %pM\n",
2449 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002450 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451 return -ENOMEM;
2452 }
2453 }
2454
2455 if (!filter_count) {
2456 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2457 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2458 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002459 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002460 if (!add_f) {
2461 dev_info(&vsi->back->pdev->dev,
2462 "Could not add filter %d for %pM\n",
2463 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002464 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002465 return -ENOMEM;
2466 }
2467 }
2468 }
2469
Kiran Patil21659032015-09-30 14:09:03 -04002470 spin_unlock_bh(&vsi->mac_filter_list_lock);
2471
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002472 /* schedule our worker thread which will take care of
2473 * applying the new filter changes
2474 */
2475 i40e_service_event_schedule(vsi->back);
2476 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002477}
2478
2479/**
2480 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2481 * @netdev: network interface to be adjusted
2482 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002483 *
2484 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002486#ifdef I40E_FCOE
2487int i40e_vlan_rx_add_vid(struct net_device *netdev,
2488 __always_unused __be16 proto, u16 vid)
2489#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2491 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002492#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002493{
2494 struct i40e_netdev_priv *np = netdev_priv(netdev);
2495 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002496 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002497
2498 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002499 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002500
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002501 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2502
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002503 /* If the network stack called us with vid = 0 then
2504 * it is asking to receive priority tagged packets with
2505 * vlan id 0. Our HW receives them by default when configured
2506 * to receive untagged packets so there is no need to add an
2507 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002508 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002509 if (vid)
2510 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002511
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002512 if (!ret && (vid < VLAN_N_VID))
2513 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002514
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002515 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516}
2517
2518/**
2519 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2520 * @netdev: network interface to be adjusted
2521 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002522 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002523 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002525#ifdef I40E_FCOE
2526int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2527 __always_unused __be16 proto, u16 vid)
2528#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2530 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002531#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532{
2533 struct i40e_netdev_priv *np = netdev_priv(netdev);
2534 struct i40e_vsi *vsi = np->vsi;
2535
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002536 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2537
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002538 /* return code is ignored as there is nothing a user
2539 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002540 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002541 */
2542 i40e_vsi_kill_vlan(vsi, vid);
2543
2544 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002546 return 0;
2547}
2548
2549/**
2550 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2551 * @vsi: the vsi being brought back up
2552 **/
2553static void i40e_restore_vlan(struct i40e_vsi *vsi)
2554{
2555 u16 vid;
2556
2557 if (!vsi->netdev)
2558 return;
2559
2560 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2561
2562 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2563 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2564 vid);
2565}
2566
2567/**
2568 * i40e_vsi_add_pvid - Add pvid for the VSI
2569 * @vsi: the vsi being adjusted
2570 * @vid: the vlan id to set as a PVID
2571 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002572int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002573{
2574 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002575 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002576
2577 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2578 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002579 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2580 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002581 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002582
2583 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002584 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002585 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2586 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002587 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002588 "add pvid failed, err %s aq_err %s\n",
2589 i40e_stat_str(&vsi->back->hw, ret),
2590 i40e_aq_str(&vsi->back->hw,
2591 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002592 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593 }
2594
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002595 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002596}
2597
2598/**
2599 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2600 * @vsi: the vsi being adjusted
2601 *
2602 * Just use the vlan_rx_register() service to put it back to normal
2603 **/
2604void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2605{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002606 i40e_vlan_stripping_disable(vsi);
2607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002608 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609}
2610
2611/**
2612 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2613 * @vsi: ptr to the VSI
2614 *
2615 * If this function returns with an error, then it's possible one or
2616 * more of the rings is populated (while the rest are not). It is the
2617 * callers duty to clean those orphaned rings.
2618 *
2619 * Return 0 on success, negative on failure
2620 **/
2621static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2622{
2623 int i, err = 0;
2624
2625 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002626 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002627
2628 return err;
2629}
2630
2631/**
2632 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2633 * @vsi: ptr to the VSI
2634 *
2635 * Free VSI's transmit software resources
2636 **/
2637static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2638{
2639 int i;
2640
Greg Rose8e9dca52013-12-18 13:45:53 +00002641 if (!vsi->tx_rings)
2642 return;
2643
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002644 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002645 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002646 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002647}
2648
2649/**
2650 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2651 * @vsi: ptr to the VSI
2652 *
2653 * If this function returns with an error, then it's possible one or
2654 * more of the rings is populated (while the rest are not). It is the
2655 * callers duty to clean those orphaned rings.
2656 *
2657 * Return 0 on success, negative on failure
2658 **/
2659static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2660{
2661 int i, err = 0;
2662
2663 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002664 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002665#ifdef I40E_FCOE
2666 i40e_fcoe_setup_ddp_resources(vsi);
2667#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002668 return err;
2669}
2670
2671/**
2672 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2673 * @vsi: ptr to the VSI
2674 *
2675 * Free all receive software resources
2676 **/
2677static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2678{
2679 int i;
2680
Greg Rose8e9dca52013-12-18 13:45:53 +00002681 if (!vsi->rx_rings)
2682 return;
2683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002685 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002686 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002687#ifdef I40E_FCOE
2688 i40e_fcoe_free_ddp_resources(vsi);
2689#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690}
2691
2692/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002693 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2694 * @ring: The Tx ring to configure
2695 *
2696 * This enables/disables XPS for a given Tx descriptor ring
2697 * based on the TCs enabled for the VSI that ring belongs to.
2698 **/
2699static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2700{
2701 struct i40e_vsi *vsi = ring->vsi;
2702 cpumask_var_t mask;
2703
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002704 if (!ring->q_vector || !ring->netdev)
2705 return;
2706
2707 /* Single TC mode enable XPS */
2708 if (vsi->tc_config.numtc <= 1) {
2709 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002710 netif_set_xps_queue(ring->netdev,
2711 &ring->q_vector->affinity_mask,
2712 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002713 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2714 /* Disable XPS to allow selection based on TC */
2715 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2716 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2717 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002718 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002719
2720 /* schedule our worker thread which will take care of
2721 * applying the new filter changes
2722 */
2723 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002724}
2725
2726/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2728 * @ring: The Tx ring to configure
2729 *
2730 * Configure the Tx descriptor ring in the HMC context.
2731 **/
2732static int i40e_configure_tx_ring(struct i40e_ring *ring)
2733{
2734 struct i40e_vsi *vsi = ring->vsi;
2735 u16 pf_q = vsi->base_queue + ring->queue_index;
2736 struct i40e_hw *hw = &vsi->back->hw;
2737 struct i40e_hmc_obj_txq tx_ctx;
2738 i40e_status err = 0;
2739 u32 qtx_ctl = 0;
2740
2741 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002742 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002743 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2744 ring->atr_count = 0;
2745 } else {
2746 ring->atr_sample_rate = 0;
2747 }
2748
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002749 /* configure XPS */
2750 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002751
2752 /* clear the context structure first */
2753 memset(&tx_ctx, 0, sizeof(tx_ctx));
2754
2755 tx_ctx.new_context = 1;
2756 tx_ctx.base = (ring->dma / 128);
2757 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002758 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2759 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002760#ifdef I40E_FCOE
2761 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2762#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002763 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002764 /* FDIR VSI tx ring can still use RS bit and writebacks */
2765 if (vsi->type != I40E_VSI_FDIR)
2766 tx_ctx.head_wb_ena = 1;
2767 tx_ctx.head_wb_addr = ring->dma +
2768 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002769
2770 /* As part of VSI creation/update, FW allocates certain
2771 * Tx arbitration queue sets for each TC enabled for
2772 * the VSI. The FW returns the handles to these queue
2773 * sets as part of the response buffer to Add VSI,
2774 * Update VSI, etc. AQ commands. It is expected that
2775 * these queue set handles be associated with the Tx
2776 * queues by the driver as part of the TX queue context
2777 * initialization. This has to be done regardless of
2778 * DCB as by default everything is mapped to TC0.
2779 */
2780 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2781 tx_ctx.rdylist_act = 0;
2782
2783 /* clear the context in the HMC */
2784 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2785 if (err) {
2786 dev_info(&vsi->back->pdev->dev,
2787 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2788 ring->queue_index, pf_q, err);
2789 return -ENOMEM;
2790 }
2791
2792 /* set the context in the HMC */
2793 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2794 if (err) {
2795 dev_info(&vsi->back->pdev->dev,
2796 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2797 ring->queue_index, pf_q, err);
2798 return -ENOMEM;
2799 }
2800
2801 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002802 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002803 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002804 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2805 I40E_QTX_CTL_VFVM_INDX_MASK;
2806 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002807 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002808 }
2809
Shannon Nelson13fd9772013-09-28 07:14:19 +00002810 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2811 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002812 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2813 i40e_flush(hw);
2814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002815 /* cache tail off for easier writes later */
2816 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2817
2818 return 0;
2819}
2820
2821/**
2822 * i40e_configure_rx_ring - Configure a receive ring context
2823 * @ring: The Rx ring to configure
2824 *
2825 * Configure the Rx descriptor ring in the HMC context.
2826 **/
2827static int i40e_configure_rx_ring(struct i40e_ring *ring)
2828{
2829 struct i40e_vsi *vsi = ring->vsi;
2830 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2831 u16 pf_q = vsi->base_queue + ring->queue_index;
2832 struct i40e_hw *hw = &vsi->back->hw;
2833 struct i40e_hmc_obj_rxq rx_ctx;
2834 i40e_status err = 0;
2835
2836 ring->state = 0;
2837
2838 /* clear the context structure first */
2839 memset(&rx_ctx, 0, sizeof(rx_ctx));
2840
2841 ring->rx_buf_len = vsi->rx_buf_len;
2842 ring->rx_hdr_len = vsi->rx_hdr_len;
2843
2844 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2845 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2846
2847 rx_ctx.base = (ring->dma / 128);
2848 rx_ctx.qlen = ring->count;
2849
2850 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2851 set_ring_16byte_desc_enabled(ring);
2852 rx_ctx.dsize = 0;
2853 } else {
2854 rx_ctx.dsize = 1;
2855 }
2856
2857 rx_ctx.dtype = vsi->dtype;
2858 if (vsi->dtype) {
2859 set_ring_ps_enabled(ring);
2860 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2861 I40E_RX_SPLIT_IP |
2862 I40E_RX_SPLIT_TCP_UDP |
2863 I40E_RX_SPLIT_SCTP;
2864 } else {
2865 rx_ctx.hsplit_0 = 0;
2866 }
2867
2868 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2869 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002870 if (hw->revision_id == 0)
2871 rx_ctx.lrxqthresh = 0;
2872 else
2873 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002874 rx_ctx.crcstrip = 1;
2875 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002876 /* this controls whether VLAN is stripped from inner headers */
2877 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002878#ifdef I40E_FCOE
2879 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2880#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002881 /* set the prefena field to 1 because the manual says to */
2882 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002883
2884 /* clear the context in the HMC */
2885 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2886 if (err) {
2887 dev_info(&vsi->back->pdev->dev,
2888 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2889 ring->queue_index, pf_q, err);
2890 return -ENOMEM;
2891 }
2892
2893 /* set the context in the HMC */
2894 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2895 if (err) {
2896 dev_info(&vsi->back->pdev->dev,
2897 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2898 ring->queue_index, pf_q, err);
2899 return -ENOMEM;
2900 }
2901
2902 /* cache tail for quicker writes, and clear the reg before use */
2903 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2904 writel(0, ring->tail);
2905
Mitch Williamsa132af22015-01-24 09:58:35 +00002906 if (ring_is_ps_enabled(ring)) {
2907 i40e_alloc_rx_headers(ring);
2908 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2909 } else {
2910 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2911 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002912
2913 return 0;
2914}
2915
2916/**
2917 * i40e_vsi_configure_tx - Configure the VSI for Tx
2918 * @vsi: VSI structure describing this set of rings and resources
2919 *
2920 * Configure the Tx VSI for operation.
2921 **/
2922static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2923{
2924 int err = 0;
2925 u16 i;
2926
Alexander Duyck9f65e152013-09-28 06:00:58 +00002927 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2928 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002929
2930 return err;
2931}
2932
2933/**
2934 * i40e_vsi_configure_rx - Configure the VSI for Rx
2935 * @vsi: the VSI being configured
2936 *
2937 * Configure the Rx VSI for operation.
2938 **/
2939static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2940{
2941 int err = 0;
2942 u16 i;
2943
2944 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2945 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2946 + ETH_FCS_LEN + VLAN_HLEN;
2947 else
2948 vsi->max_frame = I40E_RXBUFFER_2048;
2949
2950 /* figure out correct receive buffer length */
2951 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2952 I40E_FLAG_RX_PS_ENABLED)) {
2953 case I40E_FLAG_RX_1BUF_ENABLED:
2954 vsi->rx_hdr_len = 0;
2955 vsi->rx_buf_len = vsi->max_frame;
2956 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2957 break;
2958 case I40E_FLAG_RX_PS_ENABLED:
2959 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2960 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2961 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2962 break;
2963 default:
2964 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2965 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2966 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2967 break;
2968 }
2969
Vasu Dev38e00432014-08-01 13:27:03 -07002970#ifdef I40E_FCOE
2971 /* setup rx buffer for FCoE */
2972 if ((vsi->type == I40E_VSI_FCOE) &&
2973 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2974 vsi->rx_hdr_len = 0;
2975 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2976 vsi->max_frame = I40E_RXBUFFER_3072;
2977 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2978 }
2979
2980#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002981 /* round up for the chip's needs */
2982 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002983 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002984 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002985 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002986
2987 /* set up individual rings */
2988 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002989 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002990
2991 return err;
2992}
2993
2994/**
2995 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2996 * @vsi: ptr to the VSI
2997 **/
2998static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2999{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003000 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003001 u16 qoffset, qcount;
3002 int i, n;
3003
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003004 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3005 /* Reset the TC information */
3006 for (i = 0; i < vsi->num_queue_pairs; i++) {
3007 rx_ring = vsi->rx_rings[i];
3008 tx_ring = vsi->tx_rings[i];
3009 rx_ring->dcb_tc = 0;
3010 tx_ring->dcb_tc = 0;
3011 }
3012 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003013
3014 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003015 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016 continue;
3017
3018 qoffset = vsi->tc_config.tc_info[n].qoffset;
3019 qcount = vsi->tc_config.tc_info[n].qcount;
3020 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003021 rx_ring = vsi->rx_rings[i];
3022 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003023 rx_ring->dcb_tc = n;
3024 tx_ring->dcb_tc = n;
3025 }
3026 }
3027}
3028
3029/**
3030 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3031 * @vsi: ptr to the VSI
3032 **/
3033static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3034{
3035 if (vsi->netdev)
3036 i40e_set_rx_mode(vsi->netdev);
3037}
3038
3039/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003040 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3041 * @vsi: Pointer to the targeted VSI
3042 *
3043 * This function replays the hlist on the hw where all the SB Flow Director
3044 * filters were saved.
3045 **/
3046static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3047{
3048 struct i40e_fdir_filter *filter;
3049 struct i40e_pf *pf = vsi->back;
3050 struct hlist_node *node;
3051
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003052 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3053 return;
3054
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003055 hlist_for_each_entry_safe(filter, node,
3056 &pf->fdir_filter_list, fdir_node) {
3057 i40e_add_del_fdir(vsi, filter, true);
3058 }
3059}
3060
3061/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003062 * i40e_vsi_configure - Set up the VSI for action
3063 * @vsi: the VSI being configured
3064 **/
3065static int i40e_vsi_configure(struct i40e_vsi *vsi)
3066{
3067 int err;
3068
3069 i40e_set_vsi_rx_mode(vsi);
3070 i40e_restore_vlan(vsi);
3071 i40e_vsi_config_dcb_rings(vsi);
3072 err = i40e_vsi_configure_tx(vsi);
3073 if (!err)
3074 err = i40e_vsi_configure_rx(vsi);
3075
3076 return err;
3077}
3078
3079/**
3080 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3081 * @vsi: the VSI being configured
3082 **/
3083static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3084{
3085 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003086 struct i40e_hw *hw = &pf->hw;
3087 u16 vector;
3088 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089 u32 qp;
3090
3091 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3092 * and PFINT_LNKLSTn registers, e.g.:
3093 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3094 */
3095 qp = vsi->base_queue;
3096 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003097 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003098 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3099
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003100 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003101 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3102 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3103 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3104 q_vector->rx.itr);
3105 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3106 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3107 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3108 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003109 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3110 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003111
3112 /* Linked list for the queuepairs assigned to this vector */
3113 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3114 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003115 u32 val;
3116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3118 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3119 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3120 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3121 (I40E_QUEUE_TYPE_TX
3122 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3123
3124 wr32(hw, I40E_QINT_RQCTL(qp), val);
3125
3126 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3127 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3128 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3129 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3130 (I40E_QUEUE_TYPE_RX
3131 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3132
3133 /* Terminate the linked list */
3134 if (q == (q_vector->num_ringpairs - 1))
3135 val |= (I40E_QUEUE_END_OF_LIST
3136 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3137
3138 wr32(hw, I40E_QINT_TQCTL(qp), val);
3139 qp++;
3140 }
3141 }
3142
3143 i40e_flush(hw);
3144}
3145
3146/**
3147 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3148 * @hw: ptr to the hardware info
3149 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003150static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003151{
Jacob Kellerab437b52014-12-14 01:55:08 +00003152 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003153 u32 val;
3154
3155 /* clear things first */
3156 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3157 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3158
3159 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3160 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3161 I40E_PFINT_ICR0_ENA_GRST_MASK |
3162 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3163 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003164 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3165 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3166 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3167
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003168 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3169 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3170
Jacob Kellerab437b52014-12-14 01:55:08 +00003171 if (pf->flags & I40E_FLAG_PTP)
3172 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003174 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3175
3176 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003177 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3178 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003179
3180 /* OTHER_ITR_IDX = 0 */
3181 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3182}
3183
3184/**
3185 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3186 * @vsi: the VSI being configured
3187 **/
3188static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3189{
Alexander Duyck493fb302013-09-28 07:01:44 +00003190 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003191 struct i40e_pf *pf = vsi->back;
3192 struct i40e_hw *hw = &pf->hw;
3193 u32 val;
3194
3195 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003196 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003197 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3198 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3199 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3200 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3201 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3202 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3203
Jacob Kellerab437b52014-12-14 01:55:08 +00003204 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205
3206 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3207 wr32(hw, I40E_PFINT_LNKLST0, 0);
3208
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003209 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3211 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3212 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3213
3214 wr32(hw, I40E_QINT_RQCTL(0), val);
3215
3216 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3217 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3218 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3219
3220 wr32(hw, I40E_QINT_TQCTL(0), val);
3221 i40e_flush(hw);
3222}
3223
3224/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003225 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3226 * @pf: board private structure
3227 **/
3228void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3229{
3230 struct i40e_hw *hw = &pf->hw;
3231
3232 wr32(hw, I40E_PFINT_DYN_CTL0,
3233 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3234 i40e_flush(hw);
3235}
3236
3237/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3239 * @pf: board private structure
3240 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00003241void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003242{
3243 struct i40e_hw *hw = &pf->hw;
3244 u32 val;
3245
3246 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3247 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3248 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3249
3250 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3251 i40e_flush(hw);
3252}
3253
3254/**
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003255 * i40e_irq_dynamic_disable - Disable default interrupt generation settings
3256 * @vsi: pointer to a vsi
Greg Rose03147772015-01-24 09:58:29 +00003257 * @vector: disable a particular Hw Interrupt vector
Carolyn Wyborny5c2cebd2014-06-04 01:23:18 +00003258 **/
3259void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
3260{
3261 struct i40e_pf *pf = vsi->back;
3262 struct i40e_hw *hw = &pf->hw;
3263 u32 val;
3264
3265 val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
3266 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
3267 i40e_flush(hw);
3268}
3269
3270/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3272 * @irq: interrupt number
3273 * @data: pointer to a q_vector
3274 **/
3275static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3276{
3277 struct i40e_q_vector *q_vector = data;
3278
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003279 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003280 return IRQ_HANDLED;
3281
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003282 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003283
3284 return IRQ_HANDLED;
3285}
3286
3287/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003288 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3289 * @vsi: the VSI being configured
3290 * @basename: name for the vector
3291 *
3292 * Allocates MSI-X vectors and requests interrupts from the kernel.
3293 **/
3294static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3295{
3296 int q_vectors = vsi->num_q_vectors;
3297 struct i40e_pf *pf = vsi->back;
3298 int base = vsi->base_vector;
3299 int rx_int_idx = 0;
3300 int tx_int_idx = 0;
3301 int vector, err;
3302
3303 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003304 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003305
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003306 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003307 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3308 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3309 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003310 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003311 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3312 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003313 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003314 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3315 "%s-%s-%d", basename, "tx", tx_int_idx++);
3316 } else {
3317 /* skip this unused q_vector */
3318 continue;
3319 }
3320 err = request_irq(pf->msix_entries[base + vector].vector,
3321 vsi->irq_handler,
3322 0,
3323 q_vector->name,
3324 q_vector);
3325 if (err) {
3326 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003327 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003328 goto free_queue_irqs;
3329 }
3330 /* assign the mask for this irq */
3331 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3332 &q_vector->affinity_mask);
3333 }
3334
Shannon Nelson63741842014-04-23 04:50:16 +00003335 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003336 return 0;
3337
3338free_queue_irqs:
3339 while (vector) {
3340 vector--;
3341 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3342 NULL);
3343 free_irq(pf->msix_entries[base + vector].vector,
3344 &(vsi->q_vectors[vector]));
3345 }
3346 return err;
3347}
3348
3349/**
3350 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3351 * @vsi: the VSI being un-configured
3352 **/
3353static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3354{
3355 struct i40e_pf *pf = vsi->back;
3356 struct i40e_hw *hw = &pf->hw;
3357 int base = vsi->base_vector;
3358 int i;
3359
3360 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003361 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3362 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003363 }
3364
3365 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3366 for (i = vsi->base_vector;
3367 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3368 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3369
3370 i40e_flush(hw);
3371 for (i = 0; i < vsi->num_q_vectors; i++)
3372 synchronize_irq(pf->msix_entries[i + base].vector);
3373 } else {
3374 /* Legacy and MSI mode - this stops all interrupt handling */
3375 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3376 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3377 i40e_flush(hw);
3378 synchronize_irq(pf->pdev->irq);
3379 }
3380}
3381
3382/**
3383 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3384 * @vsi: the VSI being configured
3385 **/
3386static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3387{
3388 struct i40e_pf *pf = vsi->back;
3389 int i;
3390
3391 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003392 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003393 i40e_irq_dynamic_enable(vsi, i);
3394 } else {
3395 i40e_irq_dynamic_enable_icr0(pf);
3396 }
3397
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003398 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003399 return 0;
3400}
3401
3402/**
3403 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3404 * @pf: board private structure
3405 **/
3406static void i40e_stop_misc_vector(struct i40e_pf *pf)
3407{
3408 /* Disable ICR 0 */
3409 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3410 i40e_flush(&pf->hw);
3411}
3412
3413/**
3414 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3415 * @irq: interrupt number
3416 * @data: pointer to a q_vector
3417 *
3418 * This is the handler used for all MSI/Legacy interrupts, and deals
3419 * with both queue and non-queue interrupts. This is also used in
3420 * MSIX mode to handle the non-queue interrupts.
3421 **/
3422static irqreturn_t i40e_intr(int irq, void *data)
3423{
3424 struct i40e_pf *pf = (struct i40e_pf *)data;
3425 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003426 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003427 u32 icr0, icr0_remaining;
3428 u32 val, ena_mask;
3429
3430 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003431 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003432
Shannon Nelson116a57d2013-09-28 07:13:59 +00003433 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3434 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003435 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003437 /* if interrupt but no bits showing, must be SWINT */
3438 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3439 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3440 pf->sw_int_count++;
3441
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003442 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3443 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3444 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3445 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3446 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3447 }
3448
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003449 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3450 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003451 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3452 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453
3454 /* temporarily disable queue cause for NAPI processing */
3455 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003456
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003457 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3458 wr32(hw, I40E_QINT_RQCTL(0), qval);
3459
3460 qval = rd32(hw, I40E_QINT_TQCTL(0));
3461 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3462 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003463
3464 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003465 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003466 }
3467
3468 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3469 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3470 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3471 }
3472
3473 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3474 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3475 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3476 }
3477
3478 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3479 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3480 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3481 }
3482
3483 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3484 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3485 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3486 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3487 val = rd32(hw, I40E_GLGEN_RSTAT);
3488 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3489 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003490 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003491 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003492 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003493 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003494 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003495 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003496 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003497 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003498 }
3499
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003500 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3501 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3502 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003503 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3504 rd32(hw, I40E_PFHMC_ERRORINFO),
3505 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003506 }
3507
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003508 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3509 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3510
3511 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003512 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003513 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003514 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003515 }
3516
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003517 /* If a critical error is pending we have no choice but to reset the
3518 * device.
3519 * Report and mask out any remaining unexpected interrupts.
3520 */
3521 icr0_remaining = icr0 & ena_mask;
3522 if (icr0_remaining) {
3523 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3524 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003525 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003526 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003527 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003528 dev_info(&pf->pdev->dev, "device will be reset\n");
3529 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3530 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003531 }
3532 ena_mask &= ~icr0_remaining;
3533 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003534 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003536enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 /* re-enable interrupt causes */
3538 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003539 if (!test_bit(__I40E_DOWN, &pf->state)) {
3540 i40e_service_event_schedule(pf);
3541 i40e_irq_dynamic_enable_icr0(pf);
3542 }
3543
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003544 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003545}
3546
3547/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003548 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3549 * @tx_ring: tx ring to clean
3550 * @budget: how many cleans we're allowed
3551 *
3552 * Returns true if there's any budget left (e.g. the clean is finished)
3553 **/
3554static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3555{
3556 struct i40e_vsi *vsi = tx_ring->vsi;
3557 u16 i = tx_ring->next_to_clean;
3558 struct i40e_tx_buffer *tx_buf;
3559 struct i40e_tx_desc *tx_desc;
3560
3561 tx_buf = &tx_ring->tx_bi[i];
3562 tx_desc = I40E_TX_DESC(tx_ring, i);
3563 i -= tx_ring->count;
3564
3565 do {
3566 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3567
3568 /* if next_to_watch is not set then there is no work pending */
3569 if (!eop_desc)
3570 break;
3571
3572 /* prevent any other reads prior to eop_desc */
3573 read_barrier_depends();
3574
3575 /* if the descriptor isn't done, no work yet to do */
3576 if (!(eop_desc->cmd_type_offset_bsz &
3577 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3578 break;
3579
3580 /* clear next_to_watch to prevent false hangs */
3581 tx_buf->next_to_watch = NULL;
3582
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003583 tx_desc->buffer_addr = 0;
3584 tx_desc->cmd_type_offset_bsz = 0;
3585 /* move past filter desc */
3586 tx_buf++;
3587 tx_desc++;
3588 i++;
3589 if (unlikely(!i)) {
3590 i -= tx_ring->count;
3591 tx_buf = tx_ring->tx_bi;
3592 tx_desc = I40E_TX_DESC(tx_ring, 0);
3593 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003594 /* unmap skb header data */
3595 dma_unmap_single(tx_ring->dev,
3596 dma_unmap_addr(tx_buf, dma),
3597 dma_unmap_len(tx_buf, len),
3598 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003599 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3600 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003601
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003602 tx_buf->raw_buf = NULL;
3603 tx_buf->tx_flags = 0;
3604 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003605 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003606 tx_desc->buffer_addr = 0;
3607 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003608
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003609 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003610 tx_buf++;
3611 tx_desc++;
3612 i++;
3613 if (unlikely(!i)) {
3614 i -= tx_ring->count;
3615 tx_buf = tx_ring->tx_bi;
3616 tx_desc = I40E_TX_DESC(tx_ring, 0);
3617 }
3618
3619 /* update budget accounting */
3620 budget--;
3621 } while (likely(budget));
3622
3623 i += tx_ring->count;
3624 tx_ring->next_to_clean = i;
3625
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003626 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003627 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003628
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003629 return budget > 0;
3630}
3631
3632/**
3633 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3634 * @irq: interrupt number
3635 * @data: pointer to a q_vector
3636 **/
3637static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3638{
3639 struct i40e_q_vector *q_vector = data;
3640 struct i40e_vsi *vsi;
3641
3642 if (!q_vector->tx.ring)
3643 return IRQ_HANDLED;
3644
3645 vsi = q_vector->tx.ring->vsi;
3646 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3647
3648 return IRQ_HANDLED;
3649}
3650
3651/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003652 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003653 * @vsi: the VSI being configured
3654 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003655 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003656 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003657static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003658{
Alexander Duyck493fb302013-09-28 07:01:44 +00003659 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003660 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3661 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003662
3663 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003664 tx_ring->next = q_vector->tx.ring;
3665 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003667
3668 rx_ring->q_vector = q_vector;
3669 rx_ring->next = q_vector->rx.ring;
3670 q_vector->rx.ring = rx_ring;
3671 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672}
3673
3674/**
3675 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3676 * @vsi: the VSI being configured
3677 *
3678 * This function maps descriptor rings to the queue-specific vectors
3679 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3680 * one vector per queue pair, but on a constrained vector budget, we
3681 * group the queue pairs as "efficiently" as possible.
3682 **/
3683static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3684{
3685 int qp_remaining = vsi->num_queue_pairs;
3686 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003687 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688 int v_start = 0;
3689 int qp_idx = 0;
3690
3691 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3692 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003693 * It is also important to go through all the vectors available to be
3694 * sure that if we don't use all the vectors, that the remaining vectors
3695 * are cleared. This is especially important when decreasing the
3696 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003697 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003698 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003699 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3700
3701 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3702
3703 q_vector->num_ringpairs = num_ringpairs;
3704
3705 q_vector->rx.count = 0;
3706 q_vector->tx.count = 0;
3707 q_vector->rx.ring = NULL;
3708 q_vector->tx.ring = NULL;
3709
3710 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003711 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003712 qp_idx++;
3713 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003714 }
3715 }
3716}
3717
3718/**
3719 * i40e_vsi_request_irq - Request IRQ from the OS
3720 * @vsi: the VSI being configured
3721 * @basename: name for the vector
3722 **/
3723static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3724{
3725 struct i40e_pf *pf = vsi->back;
3726 int err;
3727
3728 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3729 err = i40e_vsi_request_irq_msix(vsi, basename);
3730 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3731 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003732 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003733 else
3734 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003735 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003736
3737 if (err)
3738 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3739
3740 return err;
3741}
3742
3743#ifdef CONFIG_NET_POLL_CONTROLLER
3744/**
3745 * i40e_netpoll - A Polling 'interrupt'handler
3746 * @netdev: network interface device structure
3747 *
3748 * This is used by netconsole to send skbs without having to re-enable
3749 * interrupts. It's not called while the normal interrupt routine is executing.
3750 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003751#ifdef I40E_FCOE
3752void i40e_netpoll(struct net_device *netdev)
3753#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003754static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003755#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003756{
3757 struct i40e_netdev_priv *np = netdev_priv(netdev);
3758 struct i40e_vsi *vsi = np->vsi;
3759 struct i40e_pf *pf = vsi->back;
3760 int i;
3761
3762 /* if interface is down do nothing */
3763 if (test_bit(__I40E_DOWN, &vsi->state))
3764 return;
3765
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003766 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3767 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003768 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003769 } else {
3770 i40e_intr(pf->pdev->irq, netdev);
3771 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003772}
3773#endif
3774
3775/**
Neerav Parikh23527302014-06-03 23:50:15 +00003776 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3777 * @pf: the PF being configured
3778 * @pf_q: the PF queue
3779 * @enable: enable or disable state of the queue
3780 *
3781 * This routine will wait for the given Tx queue of the PF to reach the
3782 * enabled or disabled state.
3783 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3784 * multiple retries; else will return 0 in case of success.
3785 **/
3786static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3787{
3788 int i;
3789 u32 tx_reg;
3790
3791 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3792 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3793 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3794 break;
3795
Neerav Parikhf98a2002014-09-13 07:40:44 +00003796 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003797 }
3798 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3799 return -ETIMEDOUT;
3800
3801 return 0;
3802}
3803
3804/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003805 * i40e_vsi_control_tx - Start or stop a VSI's rings
3806 * @vsi: the VSI being configured
3807 * @enable: start or stop the rings
3808 **/
3809static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3810{
3811 struct i40e_pf *pf = vsi->back;
3812 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003813 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003814 u32 tx_reg;
3815
3816 pf_q = vsi->base_queue;
3817 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003818
3819 /* warn the TX unit of coming changes */
3820 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3821 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003822 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003823
Mitch Williams6c5ef622014-02-20 19:29:16 -08003824 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003825 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003826 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3827 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3828 break;
3829 usleep_range(1000, 2000);
3830 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003831 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003832 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003833 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003834
3835 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003836 if (enable) {
3837 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003838 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003839 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003840 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003841 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003842
3843 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003844 /* No waiting for the Tx queue to disable */
3845 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3846 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003847
3848 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003849 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3850 if (ret) {
3851 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003852 "VSI seid %d Tx ring %d %sable timeout\n",
3853 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003854 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003855 }
3856 }
3857
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003858 if (hw->revision_id == 0)
3859 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003860 return ret;
3861}
3862
3863/**
3864 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3865 * @pf: the PF being configured
3866 * @pf_q: the PF queue
3867 * @enable: enable or disable state of the queue
3868 *
3869 * This routine will wait for the given Rx queue of the PF to reach the
3870 * enabled or disabled state.
3871 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3872 * multiple retries; else will return 0 in case of success.
3873 **/
3874static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3875{
3876 int i;
3877 u32 rx_reg;
3878
3879 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3880 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3881 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3882 break;
3883
Neerav Parikhf98a2002014-09-13 07:40:44 +00003884 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003885 }
3886 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3887 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003888
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003889 return 0;
3890}
3891
3892/**
3893 * i40e_vsi_control_rx - Start or stop a VSI's rings
3894 * @vsi: the VSI being configured
3895 * @enable: start or stop the rings
3896 **/
3897static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3898{
3899 struct i40e_pf *pf = vsi->back;
3900 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003901 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003902 u32 rx_reg;
3903
3904 pf_q = vsi->base_queue;
3905 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003906 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003907 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003908 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3909 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3910 break;
3911 usleep_range(1000, 2000);
3912 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913
Catherine Sullivan7c122002014-03-14 07:32:29 +00003914 /* Skip if the queue is already in the requested state */
3915 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3916 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003917
3918 /* turn on/off the queue */
3919 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003920 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003921 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003922 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003923 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3924
3925 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003926 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3927 if (ret) {
3928 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003929 "VSI seid %d Rx ring %d %sable timeout\n",
3930 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003931 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003932 }
3933 }
3934
Neerav Parikh23527302014-06-03 23:50:15 +00003935 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003936}
3937
3938/**
3939 * i40e_vsi_control_rings - Start or stop a VSI's rings
3940 * @vsi: the VSI being configured
3941 * @enable: start or stop the rings
3942 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003943int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003944{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003945 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003946
3947 /* do rx first for enable and last for disable */
3948 if (request) {
3949 ret = i40e_vsi_control_rx(vsi, request);
3950 if (ret)
3951 return ret;
3952 ret = i40e_vsi_control_tx(vsi, request);
3953 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003954 /* Ignore return value, we need to shutdown whatever we can */
3955 i40e_vsi_control_tx(vsi, request);
3956 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957 }
3958
3959 return ret;
3960}
3961
3962/**
3963 * i40e_vsi_free_irq - Free the irq association with the OS
3964 * @vsi: the VSI being configured
3965 **/
3966static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3967{
3968 struct i40e_pf *pf = vsi->back;
3969 struct i40e_hw *hw = &pf->hw;
3970 int base = vsi->base_vector;
3971 u32 val, qp;
3972 int i;
3973
3974 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3975 if (!vsi->q_vectors)
3976 return;
3977
Shannon Nelson63741842014-04-23 04:50:16 +00003978 if (!vsi->irqs_ready)
3979 return;
3980
3981 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003982 for (i = 0; i < vsi->num_q_vectors; i++) {
3983 u16 vector = i + base;
3984
3985 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003986 if (!vsi->q_vectors[i] ||
3987 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003988 continue;
3989
3990 /* clear the affinity_mask in the IRQ descriptor */
3991 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3992 NULL);
3993 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003994 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003995
3996 /* Tear down the interrupt queue link list
3997 *
3998 * We know that they come in pairs and always
3999 * the Rx first, then the Tx. To clear the
4000 * link list, stick the EOL value into the
4001 * next_q field of the registers.
4002 */
4003 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4004 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4005 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4006 val |= I40E_QUEUE_END_OF_LIST
4007 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4008 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4009
4010 while (qp != I40E_QUEUE_END_OF_LIST) {
4011 u32 next;
4012
4013 val = rd32(hw, I40E_QINT_RQCTL(qp));
4014
4015 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4016 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4017 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4018 I40E_QINT_RQCTL_INTEVENT_MASK);
4019
4020 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4021 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4022
4023 wr32(hw, I40E_QINT_RQCTL(qp), val);
4024
4025 val = rd32(hw, I40E_QINT_TQCTL(qp));
4026
4027 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4028 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4029
4030 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4031 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4032 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4033 I40E_QINT_TQCTL_INTEVENT_MASK);
4034
4035 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4036 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4037
4038 wr32(hw, I40E_QINT_TQCTL(qp), val);
4039 qp = next;
4040 }
4041 }
4042 } else {
4043 free_irq(pf->pdev->irq, pf);
4044
4045 val = rd32(hw, I40E_PFINT_LNKLST0);
4046 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4047 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4048 val |= I40E_QUEUE_END_OF_LIST
4049 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4050 wr32(hw, I40E_PFINT_LNKLST0, val);
4051
4052 val = rd32(hw, I40E_QINT_RQCTL(qp));
4053 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4054 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4055 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4056 I40E_QINT_RQCTL_INTEVENT_MASK);
4057
4058 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4059 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4060
4061 wr32(hw, I40E_QINT_RQCTL(qp), val);
4062
4063 val = rd32(hw, I40E_QINT_TQCTL(qp));
4064
4065 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4066 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4067 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4068 I40E_QINT_TQCTL_INTEVENT_MASK);
4069
4070 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4071 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4072
4073 wr32(hw, I40E_QINT_TQCTL(qp), val);
4074 }
4075}
4076
4077/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004078 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4079 * @vsi: the VSI being configured
4080 * @v_idx: Index of vector to be freed
4081 *
4082 * This function frees the memory allocated to the q_vector. In addition if
4083 * NAPI is enabled it will delete any references to the NAPI struct prior
4084 * to freeing the q_vector.
4085 **/
4086static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4087{
4088 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004089 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004090
4091 if (!q_vector)
4092 return;
4093
4094 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004095 i40e_for_each_ring(ring, q_vector->tx)
4096 ring->q_vector = NULL;
4097
4098 i40e_for_each_ring(ring, q_vector->rx)
4099 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004100
4101 /* only VSI w/ an associated netdev is set up w/ NAPI */
4102 if (vsi->netdev)
4103 netif_napi_del(&q_vector->napi);
4104
4105 vsi->q_vectors[v_idx] = NULL;
4106
4107 kfree_rcu(q_vector, rcu);
4108}
4109
4110/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004111 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4112 * @vsi: the VSI being un-configured
4113 *
4114 * This frees the memory allocated to the q_vectors and
4115 * deletes references to the NAPI struct.
4116 **/
4117static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4118{
4119 int v_idx;
4120
Alexander Duyck493fb302013-09-28 07:01:44 +00004121 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4122 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004123}
4124
4125/**
4126 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4127 * @pf: board private structure
4128 **/
4129static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4130{
4131 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4132 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4133 pci_disable_msix(pf->pdev);
4134 kfree(pf->msix_entries);
4135 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004136 kfree(pf->irq_pile);
4137 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004138 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4139 pci_disable_msi(pf->pdev);
4140 }
4141 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4142}
4143
4144/**
4145 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4146 * @pf: board private structure
4147 *
4148 * We go through and clear interrupt specific resources and reset the structure
4149 * to pre-load conditions
4150 **/
4151static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4152{
4153 int i;
4154
Shannon Nelsone1477582015-02-21 06:44:33 +00004155 i40e_stop_misc_vector(pf);
4156 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4157 synchronize_irq(pf->msix_entries[0].vector);
4158 free_irq(pf->msix_entries[0].vector, pf);
4159 }
4160
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004161 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004162 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004163 if (pf->vsi[i])
4164 i40e_vsi_free_q_vectors(pf->vsi[i]);
4165 i40e_reset_interrupt_capability(pf);
4166}
4167
4168/**
4169 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4170 * @vsi: the VSI being configured
4171 **/
4172static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4173{
4174 int q_idx;
4175
4176 if (!vsi->netdev)
4177 return;
4178
4179 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004180 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004181}
4182
4183/**
4184 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4185 * @vsi: the VSI being configured
4186 **/
4187static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4188{
4189 int q_idx;
4190
4191 if (!vsi->netdev)
4192 return;
4193
4194 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004195 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004196}
4197
4198/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004199 * i40e_vsi_close - Shut down a VSI
4200 * @vsi: the vsi to be quelled
4201 **/
4202static void i40e_vsi_close(struct i40e_vsi *vsi)
4203{
4204 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4205 i40e_down(vsi);
4206 i40e_vsi_free_irq(vsi);
4207 i40e_vsi_free_tx_resources(vsi);
4208 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004209 vsi->current_netdev_flags = 0;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004210}
4211
4212/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004213 * i40e_quiesce_vsi - Pause a given VSI
4214 * @vsi: the VSI being paused
4215 **/
4216static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4217{
4218 if (test_bit(__I40E_DOWN, &vsi->state))
4219 return;
4220
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004221 /* No need to disable FCoE VSI when Tx suspended */
4222 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4223 vsi->type == I40E_VSI_FCOE) {
4224 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004225 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004226 return;
4227 }
4228
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004229 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004230 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004231 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004232 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004233 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004234}
4235
4236/**
4237 * i40e_unquiesce_vsi - Resume a given VSI
4238 * @vsi: the VSI being resumed
4239 **/
4240static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4241{
4242 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4243 return;
4244
4245 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4246 if (vsi->netdev && netif_running(vsi->netdev))
4247 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4248 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004249 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004250}
4251
4252/**
4253 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4254 * @pf: the PF
4255 **/
4256static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4257{
4258 int v;
4259
Mitch Williams505682c2014-05-20 08:01:37 +00004260 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004261 if (pf->vsi[v])
4262 i40e_quiesce_vsi(pf->vsi[v]);
4263 }
4264}
4265
4266/**
4267 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4268 * @pf: the PF
4269 **/
4270static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4271{
4272 int v;
4273
Mitch Williams505682c2014-05-20 08:01:37 +00004274 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004275 if (pf->vsi[v])
4276 i40e_unquiesce_vsi(pf->vsi[v]);
4277 }
4278}
4279
Neerav Parikh69129dc2014-11-12 00:18:46 +00004280#ifdef CONFIG_I40E_DCB
4281/**
4282 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4283 * @vsi: the VSI being configured
4284 *
4285 * This function waits for the given VSI's Tx queues to be disabled.
4286 **/
4287static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4288{
4289 struct i40e_pf *pf = vsi->back;
4290 int i, pf_q, ret;
4291
4292 pf_q = vsi->base_queue;
4293 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4294 /* Check and wait for the disable status of the queue */
4295 ret = i40e_pf_txq_wait(pf, pf_q, false);
4296 if (ret) {
4297 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004298 "VSI seid %d Tx ring %d disable timeout\n",
4299 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004300 return ret;
4301 }
4302 }
4303
4304 return 0;
4305}
4306
4307/**
4308 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4309 * @pf: the PF
4310 *
4311 * This function waits for the Tx queues to be in disabled state for all the
4312 * VSIs that are managed by this PF.
4313 **/
4314static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4315{
4316 int v, ret = 0;
4317
4318 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004319 /* No need to wait for FCoE VSI queues */
4320 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004321 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4322 if (ret)
4323 break;
4324 }
4325 }
4326
4327 return ret;
4328}
4329
4330#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004331
4332/**
4333 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4334 * @q_idx: TX queue number
4335 * @vsi: Pointer to VSI struct
4336 *
4337 * This function checks specified queue for given VSI. Detects hung condition.
4338 * Sets hung bit since it is two step process. Before next run of service task
4339 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4340 * hung condition remain unchanged and during subsequent run, this function
4341 * issues SW interrupt to recover from hung condition.
4342 **/
4343static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4344{
4345 struct i40e_ring *tx_ring = NULL;
4346 struct i40e_pf *pf;
4347 u32 head, val, tx_pending;
4348 int i;
4349
4350 pf = vsi->back;
4351
4352 /* now that we have an index, find the tx_ring struct */
4353 for (i = 0; i < vsi->num_queue_pairs; i++) {
4354 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4355 if (q_idx == vsi->tx_rings[i]->queue_index) {
4356 tx_ring = vsi->tx_rings[i];
4357 break;
4358 }
4359 }
4360 }
4361
4362 if (!tx_ring)
4363 return;
4364
4365 /* Read interrupt register */
4366 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4367 val = rd32(&pf->hw,
4368 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4369 tx_ring->vsi->base_vector - 1));
4370 else
4371 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4372
Kiran Patil9c6c1252015-11-06 15:26:02 -08004373 /* Bail out if interrupts are disabled because napi_poll
4374 * execution in-progress or will get scheduled soon.
4375 * napi_poll cleans TX and RX queues and updates 'next_to_clean'.
4376 */
4377 if (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))
4378 return;
4379
Kiran Patilb03a8c12015-09-24 18:13:15 -04004380 head = i40e_get_head(tx_ring);
4381
4382 tx_pending = i40e_get_tx_pending(tx_ring);
4383
Kiran Patil9c6c1252015-11-06 15:26:02 -08004384 /* HW is done executing descriptors, updated HEAD write back,
4385 * but SW hasn't processed those descriptors. If interrupt is
4386 * not generated from this point ON, it could result into
4387 * dev_watchdog detecting timeout on those netdev_queue,
4388 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004389 */
Kiran Patil9c6c1252015-11-06 15:26:02 -08004390 if (tx_pending) {
4391 /* NAPI Poll didn't run and clear since it was set */
4392 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4393 &tx_ring->q_vector->hung_detected)) {
4394 netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
4395 vsi->seid, q_idx, tx_pending,
4396 tx_ring->next_to_clean, head,
4397 tx_ring->next_to_use,
4398 readl(tx_ring->tail));
4399 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4400 vsi->seid, q_idx, val);
4401 i40e_force_wb(vsi, tx_ring->q_vector);
4402 } else {
4403 /* First Chance - detected possible hung */
4404 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4405 &tx_ring->q_vector->hung_detected);
4406 }
4407 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004408}
4409
4410/**
4411 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4412 * @pf: pointer to PF struct
4413 *
4414 * LAN VSI has netdev and netdev has TX queues. This function is to check
4415 * each of those TX queues if they are hung, trigger recovery by issuing
4416 * SW interrupt.
4417 **/
4418static void i40e_detect_recover_hung(struct i40e_pf *pf)
4419{
4420 struct net_device *netdev;
4421 struct i40e_vsi *vsi;
4422 int i;
4423
4424 /* Only for LAN VSI */
4425 vsi = pf->vsi[pf->lan_vsi];
4426
4427 if (!vsi)
4428 return;
4429
4430 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4431 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4432 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4433 return;
4434
4435 /* Make sure type is MAIN VSI */
4436 if (vsi->type != I40E_VSI_MAIN)
4437 return;
4438
4439 netdev = vsi->netdev;
4440 if (!netdev)
4441 return;
4442
4443 /* Bail out if netif_carrier is not OK */
4444 if (!netif_carrier_ok(netdev))
4445 return;
4446
4447 /* Go thru' TX queues for netdev */
4448 for (i = 0; i < netdev->num_tx_queues; i++) {
4449 struct netdev_queue *q;
4450
4451 q = netdev_get_tx_queue(netdev, i);
4452 if (q)
4453 i40e_detect_recover_hung_queue(i, vsi);
4454 }
4455}
4456
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004457/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004458 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004459 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004460 *
4461 * Get TC map for ISCSI PF type that will include iSCSI TC
4462 * and LAN TC.
4463 **/
4464static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4465{
4466 struct i40e_dcb_app_priority_table app;
4467 struct i40e_hw *hw = &pf->hw;
4468 u8 enabled_tc = 1; /* TC0 is always enabled */
4469 u8 tc, i;
4470 /* Get the iSCSI APP TLV */
4471 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4472
4473 for (i = 0; i < dcbcfg->numapps; i++) {
4474 app = dcbcfg->app[i];
4475 if (app.selector == I40E_APP_SEL_TCPIP &&
4476 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4477 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004478 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004479 break;
4480 }
4481 }
4482
4483 return enabled_tc;
4484}
4485
4486/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004487 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4488 * @dcbcfg: the corresponding DCBx configuration structure
4489 *
4490 * Return the number of TCs from given DCBx configuration
4491 **/
4492static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4493{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004494 u8 num_tc = 0;
4495 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004496
4497 /* Scan the ETS Config Priority Table to find
4498 * traffic class enabled for a given priority
4499 * and use the traffic class index to get the
4500 * number of traffic classes enabled
4501 */
4502 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4503 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4504 num_tc = dcbcfg->etscfg.prioritytable[i];
4505 }
4506
4507 /* Traffic class index starts from zero so
4508 * increment to return the actual count
4509 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004510 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004511}
4512
4513/**
4514 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4515 * @dcbcfg: the corresponding DCBx configuration structure
4516 *
4517 * Query the current DCB configuration and return the number of
4518 * traffic classes enabled from the given DCBX config
4519 **/
4520static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4521{
4522 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4523 u8 enabled_tc = 1;
4524 u8 i;
4525
4526 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004527 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004528
4529 return enabled_tc;
4530}
4531
4532/**
4533 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4534 * @pf: PF being queried
4535 *
4536 * Return number of traffic classes enabled for the given PF
4537 **/
4538static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4539{
4540 struct i40e_hw *hw = &pf->hw;
4541 u8 i, enabled_tc;
4542 u8 num_tc = 0;
4543 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4544
4545 /* If DCB is not enabled then always in single TC */
4546 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4547 return 1;
4548
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004549 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004550 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4551 return i40e_dcb_get_num_tc(dcbcfg);
4552
4553 /* MFP mode return count of enabled TCs for this PF */
4554 if (pf->hw.func_caps.iscsi)
4555 enabled_tc = i40e_get_iscsi_tc_map(pf);
4556 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004557 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004558
4559 /* At least have TC0 */
4560 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4561 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004562 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004563 num_tc++;
4564 }
4565 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004566}
4567
4568/**
4569 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4570 * @pf: PF being queried
4571 *
4572 * Return a bitmap for first enabled traffic class for this PF.
4573 **/
4574static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4575{
4576 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4577 u8 i = 0;
4578
4579 if (!enabled_tc)
4580 return 0x1; /* TC0 */
4581
4582 /* Find the first enabled TC */
4583 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004584 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004585 break;
4586 }
4587
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004588 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004589}
4590
4591/**
4592 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4593 * @pf: PF being queried
4594 *
4595 * Return a bitmap for enabled traffic classes for this PF.
4596 **/
4597static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4598{
4599 /* If DCB is not enabled for this PF then just return default TC */
4600 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4601 return i40e_pf_get_default_tc(pf);
4602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004603 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004604 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4605 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4606
Neerav Parikhfc51de92015-02-24 06:58:53 +00004607 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004608 if (pf->hw.func_caps.iscsi)
4609 return i40e_get_iscsi_tc_map(pf);
4610 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004611 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004612}
4613
4614/**
4615 * i40e_vsi_get_bw_info - Query VSI BW Information
4616 * @vsi: the VSI being queried
4617 *
4618 * Returns 0 on success, negative value on failure
4619 **/
4620static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4621{
4622 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4623 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4624 struct i40e_pf *pf = vsi->back;
4625 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004626 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004627 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004628 int i;
4629
4630 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004631 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4632 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004633 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004634 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4635 i40e_stat_str(&pf->hw, ret),
4636 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004637 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004638 }
4639
4640 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004641 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4642 NULL);
4643 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004644 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004645 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4646 i40e_stat_str(&pf->hw, ret),
4647 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004648 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004649 }
4650
4651 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4652 dev_info(&pf->pdev->dev,
4653 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4654 bw_config.tc_valid_bits,
4655 bw_ets_config.tc_valid_bits);
4656 /* Still continuing */
4657 }
4658
4659 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4660 vsi->bw_max_quanta = bw_config.max_bw;
4661 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4662 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4663 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4664 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4665 vsi->bw_ets_limit_credits[i] =
4666 le16_to_cpu(bw_ets_config.credits[i]);
4667 /* 3 bits out of 4 for each TC */
4668 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4669 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004670
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004671 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004672}
4673
4674/**
4675 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4676 * @vsi: the VSI being configured
4677 * @enabled_tc: TC bitmap
4678 * @bw_credits: BW shared credits per TC
4679 *
4680 * Returns 0 on success, negative value on failure
4681 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004682static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004683 u8 *bw_share)
4684{
4685 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004686 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004687 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004688
4689 bw_data.tc_valid_bits = enabled_tc;
4690 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4691 bw_data.tc_bw_credits[i] = bw_share[i];
4692
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004693 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4694 NULL);
4695 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004696 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004697 "AQ command Config VSI BW allocation per TC failed = %d\n",
4698 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004699 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004700 }
4701
4702 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4703 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4704
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004705 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004706}
4707
4708/**
4709 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4710 * @vsi: the VSI being configured
4711 * @enabled_tc: TC map to be enabled
4712 *
4713 **/
4714static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4715{
4716 struct net_device *netdev = vsi->netdev;
4717 struct i40e_pf *pf = vsi->back;
4718 struct i40e_hw *hw = &pf->hw;
4719 u8 netdev_tc = 0;
4720 int i;
4721 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4722
4723 if (!netdev)
4724 return;
4725
4726 if (!enabled_tc) {
4727 netdev_reset_tc(netdev);
4728 return;
4729 }
4730
4731 /* Set up actual enabled TCs on the VSI */
4732 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4733 return;
4734
4735 /* set per TC queues for the VSI */
4736 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4737 /* Only set TC queues for enabled tcs
4738 *
4739 * e.g. For a VSI that has TC0 and TC3 enabled the
4740 * enabled_tc bitmap would be 0x00001001; the driver
4741 * will set the numtc for netdev as 2 that will be
4742 * referenced by the netdev layer as TC 0 and 1.
4743 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004744 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004745 netdev_set_tc_queue(netdev,
4746 vsi->tc_config.tc_info[i].netdev_tc,
4747 vsi->tc_config.tc_info[i].qcount,
4748 vsi->tc_config.tc_info[i].qoffset);
4749 }
4750
4751 /* Assign UP2TC map for the VSI */
4752 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4753 /* Get the actual TC# for the UP */
4754 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4755 /* Get the mapped netdev TC# for the UP */
4756 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4757 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4758 }
4759}
4760
4761/**
4762 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4763 * @vsi: the VSI being configured
4764 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4765 **/
4766static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4767 struct i40e_vsi_context *ctxt)
4768{
4769 /* copy just the sections touched not the entire info
4770 * since not all sections are valid as returned by
4771 * update vsi params
4772 */
4773 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4774 memcpy(&vsi->info.queue_mapping,
4775 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4776 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4777 sizeof(vsi->info.tc_mapping));
4778}
4779
4780/**
4781 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4782 * @vsi: VSI to be configured
4783 * @enabled_tc: TC bitmap
4784 *
4785 * This configures a particular VSI for TCs that are mapped to the
4786 * given TC bitmap. It uses default bandwidth share for TCs across
4787 * VSIs to configure TC for a particular VSI.
4788 *
4789 * NOTE:
4790 * It is expected that the VSI queues have been quisced before calling
4791 * this function.
4792 **/
4793static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4794{
4795 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4796 struct i40e_vsi_context ctxt;
4797 int ret = 0;
4798 int i;
4799
4800 /* Check if enabled_tc is same as existing or new TCs */
4801 if (vsi->tc_config.enabled_tc == enabled_tc)
4802 return ret;
4803
4804 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4805 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004806 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004807 bw_share[i] = 1;
4808 }
4809
4810 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4811 if (ret) {
4812 dev_info(&vsi->back->pdev->dev,
4813 "Failed configuring TC map %d for VSI %d\n",
4814 enabled_tc, vsi->seid);
4815 goto out;
4816 }
4817
4818 /* Update Queue Pairs Mapping for currently enabled UPs */
4819 ctxt.seid = vsi->seid;
4820 ctxt.pf_num = vsi->back->hw.pf_id;
4821 ctxt.vf_num = 0;
4822 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004823 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004824 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4825
4826 /* Update the VSI after updating the VSI queue-mapping information */
4827 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4828 if (ret) {
4829 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004830 "Update vsi tc config failed, err %s aq_err %s\n",
4831 i40e_stat_str(&vsi->back->hw, ret),
4832 i40e_aq_str(&vsi->back->hw,
4833 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004834 goto out;
4835 }
4836 /* update the local VSI info with updated queue map */
4837 i40e_vsi_update_queue_map(vsi, &ctxt);
4838 vsi->info.valid_sections = 0;
4839
4840 /* Update current VSI BW information */
4841 ret = i40e_vsi_get_bw_info(vsi);
4842 if (ret) {
4843 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004844 "Failed updating vsi bw info, err %s aq_err %s\n",
4845 i40e_stat_str(&vsi->back->hw, ret),
4846 i40e_aq_str(&vsi->back->hw,
4847 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004848 goto out;
4849 }
4850
4851 /* Update the netdev TC setup */
4852 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4853out:
4854 return ret;
4855}
4856
4857/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004858 * i40e_veb_config_tc - Configure TCs for given VEB
4859 * @veb: given VEB
4860 * @enabled_tc: TC bitmap
4861 *
4862 * Configures given TC bitmap for VEB (switching) element
4863 **/
4864int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4865{
4866 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4867 struct i40e_pf *pf = veb->pf;
4868 int ret = 0;
4869 int i;
4870
4871 /* No TCs or already enabled TCs just return */
4872 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4873 return ret;
4874
4875 bw_data.tc_valid_bits = enabled_tc;
4876 /* bw_data.absolute_credits is not set (relative) */
4877
4878 /* Enable ETS TCs with equal BW Share for now */
4879 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004880 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004881 bw_data.tc_bw_share_credits[i] = 1;
4882 }
4883
4884 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4885 &bw_data, NULL);
4886 if (ret) {
4887 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004888 "VEB bw config failed, err %s aq_err %s\n",
4889 i40e_stat_str(&pf->hw, ret),
4890 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004891 goto out;
4892 }
4893
4894 /* Update the BW information */
4895 ret = i40e_veb_get_bw_info(veb);
4896 if (ret) {
4897 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004898 "Failed getting veb bw config, err %s aq_err %s\n",
4899 i40e_stat_str(&pf->hw, ret),
4900 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004901 }
4902
4903out:
4904 return ret;
4905}
4906
4907#ifdef CONFIG_I40E_DCB
4908/**
4909 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4910 * @pf: PF struct
4911 *
4912 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4913 * the caller would've quiesce all the VSIs before calling
4914 * this function
4915 **/
4916static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4917{
4918 u8 tc_map = 0;
4919 int ret;
4920 u8 v;
4921
4922 /* Enable the TCs available on PF to all VEBs */
4923 tc_map = i40e_pf_get_tc_map(pf);
4924 for (v = 0; v < I40E_MAX_VEB; v++) {
4925 if (!pf->veb[v])
4926 continue;
4927 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4928 if (ret) {
4929 dev_info(&pf->pdev->dev,
4930 "Failed configuring TC for VEB seid=%d\n",
4931 pf->veb[v]->seid);
4932 /* Will try to configure as many components */
4933 }
4934 }
4935
4936 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004937 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004938 if (!pf->vsi[v])
4939 continue;
4940
4941 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004942#ifdef I40E_FCOE
4943 * - For FCoE VSI only enable the TC configured
4944 * as per the APP TLV
4945#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004946 * - For all others keep them at TC0 for now
4947 */
4948 if (v == pf->lan_vsi)
4949 tc_map = i40e_pf_get_tc_map(pf);
4950 else
4951 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004952#ifdef I40E_FCOE
4953 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4954 tc_map = i40e_get_fcoe_tc_map(pf);
4955#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004956
4957 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4958 if (ret) {
4959 dev_info(&pf->pdev->dev,
4960 "Failed configuring TC for VSI seid=%d\n",
4961 pf->vsi[v]->seid);
4962 /* Will try to configure as many components */
4963 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004964 /* Re-configure VSI vectors based on updated TC map */
4965 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004966 if (pf->vsi[v]->netdev)
4967 i40e_dcbnl_set_all(pf->vsi[v]);
4968 }
4969 }
4970}
4971
4972/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004973 * i40e_resume_port_tx - Resume port Tx
4974 * @pf: PF struct
4975 *
4976 * Resume a port's Tx and issue a PF reset in case of failure to
4977 * resume.
4978 **/
4979static int i40e_resume_port_tx(struct i40e_pf *pf)
4980{
4981 struct i40e_hw *hw = &pf->hw;
4982 int ret;
4983
4984 ret = i40e_aq_resume_port_tx(hw, NULL);
4985 if (ret) {
4986 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004987 "Resume Port Tx failed, err %s aq_err %s\n",
4988 i40e_stat_str(&pf->hw, ret),
4989 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004990 /* Schedule PF reset to recover */
4991 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4992 i40e_service_event_schedule(pf);
4993 }
4994
4995 return ret;
4996}
4997
4998/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004999 * i40e_init_pf_dcb - Initialize DCB configuration
5000 * @pf: PF being configured
5001 *
5002 * Query the current DCB configuration and cache it
5003 * in the hardware structure
5004 **/
5005static int i40e_init_pf_dcb(struct i40e_pf *pf)
5006{
5007 struct i40e_hw *hw = &pf->hw;
5008 int err = 0;
5009
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005010 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
5011 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
5012 (pf->hw.aq.fw_maj_ver < 4))
5013 goto out;
5014
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005015 /* Get the initial DCB configuration */
5016 err = i40e_init_dcb(hw);
5017 if (!err) {
5018 /* Device/Function is not DCBX capable */
5019 if ((!hw->func_caps.dcb) ||
5020 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5021 dev_info(&pf->pdev->dev,
5022 "DCBX offload is not supported or is disabled for this PF.\n");
5023
5024 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5025 goto out;
5026
5027 } else {
5028 /* When status is not DISABLED then DCBX in FW */
5029 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5030 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005031
5032 pf->flags |= I40E_FLAG_DCB_CAPABLE;
5033 /* Enable DCB tagging only when more than one TC */
5034 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5035 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005036 dev_dbg(&pf->pdev->dev,
5037 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005038 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005039 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005040 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005041 "Query for DCB configuration failed, err %s aq_err %s\n",
5042 i40e_stat_str(&pf->hw, err),
5043 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005044 }
5045
5046out:
5047 return err;
5048}
5049#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005050#define SPEED_SIZE 14
5051#define FC_SIZE 8
5052/**
5053 * i40e_print_link_message - print link up or down
5054 * @vsi: the VSI for which link needs a message
5055 */
Matt Jaredc156f852015-08-27 11:42:39 -04005056void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005057{
Shannon Nelsona9165492015-09-03 17:19:00 -04005058 char *speed = "Unknown";
5059 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005060
Matt Jaredc156f852015-08-27 11:42:39 -04005061 if (vsi->current_isup == isup)
5062 return;
5063 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005064 if (!isup) {
5065 netdev_info(vsi->netdev, "NIC Link is Down\n");
5066 return;
5067 }
5068
Greg Rose148c2d82014-12-11 07:06:27 +00005069 /* Warn user if link speed on NPAR enabled partition is not at
5070 * least 10GB
5071 */
5072 if (vsi->back->hw.func_caps.npar_enable &&
5073 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5074 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5075 netdev_warn(vsi->netdev,
5076 "The partition detected link speed that is less than 10Gbps\n");
5077
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005078 switch (vsi->back->hw.phy.link_info.link_speed) {
5079 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005080 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005081 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005082 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005083 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005084 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005085 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005086 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005087 break;
5088 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005089 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005090 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005091 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005092 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005093 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005094 default:
5095 break;
5096 }
5097
5098 switch (vsi->back->hw.fc.current_mode) {
5099 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005100 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005101 break;
5102 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005103 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005104 break;
5105 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005106 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005107 break;
5108 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005109 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005110 break;
5111 }
5112
Shannon Nelsona9165492015-09-03 17:19:00 -04005113 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005114 speed, fc);
5115}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005116
5117/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005118 * i40e_up_complete - Finish the last steps of bringing up a connection
5119 * @vsi: the VSI being configured
5120 **/
5121static int i40e_up_complete(struct i40e_vsi *vsi)
5122{
5123 struct i40e_pf *pf = vsi->back;
5124 int err;
5125
5126 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5127 i40e_vsi_configure_msix(vsi);
5128 else
5129 i40e_configure_msi_and_legacy(vsi);
5130
5131 /* start rings */
5132 err = i40e_vsi_control_rings(vsi, true);
5133 if (err)
5134 return err;
5135
5136 clear_bit(__I40E_DOWN, &vsi->state);
5137 i40e_napi_enable_all(vsi);
5138 i40e_vsi_enable_irq(vsi);
5139
5140 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5141 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005142 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005143 netif_tx_start_all_queues(vsi->netdev);
5144 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005145 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005146 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005147 /* need to check for qualified module here*/
5148 if ((pf->hw.phy.link_info.link_info &
5149 I40E_AQ_MEDIA_AVAILABLE) &&
5150 (!(pf->hw.phy.link_info.an_info &
5151 I40E_AQ_QUALIFIED_MODULE)))
5152 netdev_err(vsi->netdev,
5153 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005154 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005155
5156 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005157 if (vsi->type == I40E_VSI_FDIR) {
5158 /* reset fd counters */
5159 pf->fd_add_err = pf->fd_atr_cnt = 0;
5160 if (pf->fd_tcp_rule > 0) {
5161 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005162 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5163 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005164 pf->fd_tcp_rule = 0;
5165 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005166 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005167 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005168 i40e_service_event_schedule(pf);
5169
5170 return 0;
5171}
5172
5173/**
5174 * i40e_vsi_reinit_locked - Reset the VSI
5175 * @vsi: the VSI being configured
5176 *
5177 * Rebuild the ring structs after some configuration
5178 * has changed, e.g. MTU size.
5179 **/
5180static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5181{
5182 struct i40e_pf *pf = vsi->back;
5183
5184 WARN_ON(in_interrupt());
5185 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5186 usleep_range(1000, 2000);
5187 i40e_down(vsi);
5188
5189 /* Give a VF some time to respond to the reset. The
5190 * two second wait is based upon the watchdog cycle in
5191 * the VF driver.
5192 */
5193 if (vsi->type == I40E_VSI_SRIOV)
5194 msleep(2000);
5195 i40e_up(vsi);
5196 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5197}
5198
5199/**
5200 * i40e_up - Bring the connection back up after being down
5201 * @vsi: the VSI being configured
5202 **/
5203int i40e_up(struct i40e_vsi *vsi)
5204{
5205 int err;
5206
5207 err = i40e_vsi_configure(vsi);
5208 if (!err)
5209 err = i40e_up_complete(vsi);
5210
5211 return err;
5212}
5213
5214/**
5215 * i40e_down - Shutdown the connection processing
5216 * @vsi: the VSI being stopped
5217 **/
5218void i40e_down(struct i40e_vsi *vsi)
5219{
5220 int i;
5221
5222 /* It is assumed that the caller of this function
5223 * sets the vsi->state __I40E_DOWN bit.
5224 */
5225 if (vsi->netdev) {
5226 netif_carrier_off(vsi->netdev);
5227 netif_tx_disable(vsi->netdev);
5228 }
5229 i40e_vsi_disable_irq(vsi);
5230 i40e_vsi_control_rings(vsi, false);
5231 i40e_napi_disable_all(vsi);
5232
5233 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005234 i40e_clean_tx_ring(vsi->tx_rings[i]);
5235 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005236 }
5237}
5238
5239/**
5240 * i40e_setup_tc - configure multiple traffic classes
5241 * @netdev: net device to configure
5242 * @tc: number of traffic classes to enable
5243 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005244#ifdef I40E_FCOE
5245int i40e_setup_tc(struct net_device *netdev, u8 tc)
5246#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005247static int i40e_setup_tc(struct net_device *netdev, u8 tc)
Vasu Dev38e00432014-08-01 13:27:03 -07005248#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005249{
5250 struct i40e_netdev_priv *np = netdev_priv(netdev);
5251 struct i40e_vsi *vsi = np->vsi;
5252 struct i40e_pf *pf = vsi->back;
5253 u8 enabled_tc = 0;
5254 int ret = -EINVAL;
5255 int i;
5256
5257 /* Check if DCB enabled to continue */
5258 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5259 netdev_info(netdev, "DCB is not enabled for adapter\n");
5260 goto exit;
5261 }
5262
5263 /* Check if MFP enabled */
5264 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5265 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5266 goto exit;
5267 }
5268
5269 /* Check whether tc count is within enabled limit */
5270 if (tc > i40e_pf_get_num_tc(pf)) {
5271 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5272 goto exit;
5273 }
5274
5275 /* Generate TC map for number of tc requested */
5276 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005277 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005278
5279 /* Requesting same TC configuration as already enabled */
5280 if (enabled_tc == vsi->tc_config.enabled_tc)
5281 return 0;
5282
5283 /* Quiesce VSI queues */
5284 i40e_quiesce_vsi(vsi);
5285
5286 /* Configure VSI for enabled TCs */
5287 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5288 if (ret) {
5289 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5290 vsi->seid);
5291 goto exit;
5292 }
5293
5294 /* Unquiesce VSI */
5295 i40e_unquiesce_vsi(vsi);
5296
5297exit:
5298 return ret;
5299}
5300
5301/**
5302 * i40e_open - Called when a network interface is made active
5303 * @netdev: network interface device structure
5304 *
5305 * The open entry point is called when a network interface is made
5306 * active by the system (IFF_UP). At this point all resources needed
5307 * for transmit and receive operations are allocated, the interrupt
5308 * handler is registered with the OS, the netdev watchdog subtask is
5309 * enabled, and the stack is notified that the interface is ready.
5310 *
5311 * Returns 0 on success, negative value on failure
5312 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005313int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005314{
5315 struct i40e_netdev_priv *np = netdev_priv(netdev);
5316 struct i40e_vsi *vsi = np->vsi;
5317 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005318 int err;
5319
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005320 /* disallow open during test or if eeprom is broken */
5321 if (test_bit(__I40E_TESTING, &pf->state) ||
5322 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005323 return -EBUSY;
5324
5325 netif_carrier_off(netdev);
5326
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005327 err = i40e_vsi_open(vsi);
5328 if (err)
5329 return err;
5330
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005331 /* configure global TSO hardware offload settings */
5332 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5333 TCP_FLAG_FIN) >> 16);
5334 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5335 TCP_FLAG_FIN |
5336 TCP_FLAG_CWR) >> 16);
5337 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5338
Singhai, Anjali6633d382015-12-03 23:49:31 -08005339#ifdef CONFIG_I40E_VXLAN
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005340 vxlan_get_rx_port(netdev);
5341#endif
Singhai, Anjalicd866602015-12-14 12:21:21 -08005342#ifdef CONFIG_I40E_GENEVE
5343 geneve_get_rx_port(netdev);
5344#endif
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005345
5346 return 0;
5347}
5348
5349/**
5350 * i40e_vsi_open -
5351 * @vsi: the VSI to open
5352 *
5353 * Finish initialization of the VSI.
5354 *
5355 * Returns 0 on success, negative value on failure
5356 **/
5357int i40e_vsi_open(struct i40e_vsi *vsi)
5358{
5359 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005360 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005361 int err;
5362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005363 /* allocate descriptors */
5364 err = i40e_vsi_setup_tx_resources(vsi);
5365 if (err)
5366 goto err_setup_tx;
5367 err = i40e_vsi_setup_rx_resources(vsi);
5368 if (err)
5369 goto err_setup_rx;
5370
5371 err = i40e_vsi_configure(vsi);
5372 if (err)
5373 goto err_setup_rx;
5374
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005375 if (vsi->netdev) {
5376 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5377 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5378 err = i40e_vsi_request_irq(vsi, int_name);
5379 if (err)
5380 goto err_setup_rx;
5381
5382 /* Notify the stack of the actual queue counts. */
5383 err = netif_set_real_num_tx_queues(vsi->netdev,
5384 vsi->num_queue_pairs);
5385 if (err)
5386 goto err_set_queues;
5387
5388 err = netif_set_real_num_rx_queues(vsi->netdev,
5389 vsi->num_queue_pairs);
5390 if (err)
5391 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005392
5393 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005394 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005395 dev_driver_string(&pf->pdev->dev),
5396 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005397 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005398
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005399 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005400 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005401 goto err_setup_rx;
5402 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005404 err = i40e_up_complete(vsi);
5405 if (err)
5406 goto err_up_complete;
5407
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005408 return 0;
5409
5410err_up_complete:
5411 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005412err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005413 i40e_vsi_free_irq(vsi);
5414err_setup_rx:
5415 i40e_vsi_free_rx_resources(vsi);
5416err_setup_tx:
5417 i40e_vsi_free_tx_resources(vsi);
5418 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005419 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005420
5421 return err;
5422}
5423
5424/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005425 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005426 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005427 *
5428 * This function destroys the hlist where all the Flow Director
5429 * filters were saved.
5430 **/
5431static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5432{
5433 struct i40e_fdir_filter *filter;
5434 struct hlist_node *node2;
5435
5436 hlist_for_each_entry_safe(filter, node2,
5437 &pf->fdir_filter_list, fdir_node) {
5438 hlist_del(&filter->fdir_node);
5439 kfree(filter);
5440 }
5441 pf->fdir_pf_active_filters = 0;
5442}
5443
5444/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005445 * i40e_close - Disables a network interface
5446 * @netdev: network interface device structure
5447 *
5448 * The close entry point is called when an interface is de-activated
5449 * by the OS. The hardware is still under the driver's control, but
5450 * this netdev interface is disabled.
5451 *
5452 * Returns 0, this is not allowed to fail
5453 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005454#ifdef I40E_FCOE
5455int i40e_close(struct net_device *netdev)
5456#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005457static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005458#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005459{
5460 struct i40e_netdev_priv *np = netdev_priv(netdev);
5461 struct i40e_vsi *vsi = np->vsi;
5462
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005463 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005464
5465 return 0;
5466}
5467
5468/**
5469 * i40e_do_reset - Start a PF or Core Reset sequence
5470 * @pf: board private structure
5471 * @reset_flags: which reset is requested
5472 *
5473 * The essential difference in resets is that the PF Reset
5474 * doesn't clear the packet buffers, doesn't reset the PE
5475 * firmware, and doesn't bother the other PFs on the chip.
5476 **/
5477void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5478{
5479 u32 val;
5480
5481 WARN_ON(in_interrupt());
5482
Mitch Williams263fc482014-04-23 04:50:11 +00005483 if (i40e_check_asq_alive(&pf->hw))
5484 i40e_vc_notify_reset(pf);
5485
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005486 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005487 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005488
5489 /* Request a Global Reset
5490 *
5491 * This will start the chip's countdown to the actual full
5492 * chip reset event, and a warning interrupt to be sent
5493 * to all PFs, including the requestor. Our handler
5494 * for the warning interrupt will deal with the shutdown
5495 * and recovery of the switch setup.
5496 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005497 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005498 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5499 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5500 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5501
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005502 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503
5504 /* Request a Core Reset
5505 *
5506 * Same as Global Reset, except does *not* include the MAC/PHY
5507 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005508 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005509 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5510 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5511 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5512 i40e_flush(&pf->hw);
5513
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005514 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005515
5516 /* Request a PF Reset
5517 *
5518 * Resets only the PF-specific registers
5519 *
5520 * This goes directly to the tear-down and rebuild of
5521 * the switch, since we need to do all the recovery as
5522 * for the Core Reset.
5523 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005524 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005525 i40e_handle_reset_warning(pf);
5526
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005527 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005528 int v;
5529
5530 /* Find the VSI(s) that requested a re-init */
5531 dev_info(&pf->pdev->dev,
5532 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005533 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005534 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005535
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005536 if (vsi != NULL &&
5537 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5538 i40e_vsi_reinit_locked(pf->vsi[v]);
5539 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5540 }
5541 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005542 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005543 int v;
5544
5545 /* Find the VSI(s) that needs to be brought down */
5546 dev_info(&pf->pdev->dev, "VSI down requested\n");
5547 for (v = 0; v < pf->num_alloc_vsi; v++) {
5548 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005549
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005550 if (vsi != NULL &&
5551 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5552 set_bit(__I40E_DOWN, &vsi->state);
5553 i40e_down(vsi);
5554 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5555 }
5556 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557 } else {
5558 dev_info(&pf->pdev->dev,
5559 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005560 }
5561}
5562
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005563#ifdef CONFIG_I40E_DCB
5564/**
5565 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5566 * @pf: board private structure
5567 * @old_cfg: current DCB config
5568 * @new_cfg: new DCB config
5569 **/
5570bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5571 struct i40e_dcbx_config *old_cfg,
5572 struct i40e_dcbx_config *new_cfg)
5573{
5574 bool need_reconfig = false;
5575
5576 /* Check if ETS configuration has changed */
5577 if (memcmp(&new_cfg->etscfg,
5578 &old_cfg->etscfg,
5579 sizeof(new_cfg->etscfg))) {
5580 /* If Priority Table has changed reconfig is needed */
5581 if (memcmp(&new_cfg->etscfg.prioritytable,
5582 &old_cfg->etscfg.prioritytable,
5583 sizeof(new_cfg->etscfg.prioritytable))) {
5584 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005585 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005586 }
5587
5588 if (memcmp(&new_cfg->etscfg.tcbwtable,
5589 &old_cfg->etscfg.tcbwtable,
5590 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005591 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005592
5593 if (memcmp(&new_cfg->etscfg.tsatable,
5594 &old_cfg->etscfg.tsatable,
5595 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005596 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005597 }
5598
5599 /* Check if PFC configuration has changed */
5600 if (memcmp(&new_cfg->pfc,
5601 &old_cfg->pfc,
5602 sizeof(new_cfg->pfc))) {
5603 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005604 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005605 }
5606
5607 /* Check if APP Table has changed */
5608 if (memcmp(&new_cfg->app,
5609 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005610 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005611 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005612 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005613 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005614
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005615 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005616 return need_reconfig;
5617}
5618
5619/**
5620 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5621 * @pf: board private structure
5622 * @e: event info posted on ARQ
5623 **/
5624static int i40e_handle_lldp_event(struct i40e_pf *pf,
5625 struct i40e_arq_event_info *e)
5626{
5627 struct i40e_aqc_lldp_get_mib *mib =
5628 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5629 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005630 struct i40e_dcbx_config tmp_dcbx_cfg;
5631 bool need_reconfig = false;
5632 int ret = 0;
5633 u8 type;
5634
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005635 /* Not DCB capable or capability disabled */
5636 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5637 return ret;
5638
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005639 /* Ignore if event is not for Nearest Bridge */
5640 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5641 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005642 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005643 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5644 return ret;
5645
5646 /* Check MIB Type and return if event for Remote MIB update */
5647 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005648 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005649 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005650 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5651 /* Update the remote cached instance and return */
5652 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5653 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5654 &hw->remote_dcbx_config);
5655 goto exit;
5656 }
5657
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005658 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005659 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005660
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005661 /* Reset the old DCBx configuration data */
5662 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005663 /* Get updated DCBX data from firmware */
5664 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005665 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005666 dev_info(&pf->pdev->dev,
5667 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5668 i40e_stat_str(&pf->hw, ret),
5669 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005670 goto exit;
5671 }
5672
5673 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005674 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5675 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005676 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005677 goto exit;
5678 }
5679
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005680 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5681 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005682
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005683 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005684
5685 if (!need_reconfig)
5686 goto exit;
5687
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005688 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005689 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005690 pf->flags |= I40E_FLAG_DCB_ENABLED;
5691 else
5692 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5693
Neerav Parikh69129dc2014-11-12 00:18:46 +00005694 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005695 /* Reconfiguration needed quiesce all VSIs */
5696 i40e_pf_quiesce_all_vsi(pf);
5697
5698 /* Changes in configuration update VEB/VSI */
5699 i40e_dcb_reconfigure(pf);
5700
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005701 ret = i40e_resume_port_tx(pf);
5702
Neerav Parikh69129dc2014-11-12 00:18:46 +00005703 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005704 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005705 if (ret)
5706 goto exit;
5707
5708 /* Wait for the PF's Tx queues to be disabled */
5709 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005710 if (ret) {
5711 /* Schedule PF reset to recover */
5712 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5713 i40e_service_event_schedule(pf);
5714 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005715 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005716 }
5717
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005718exit:
5719 return ret;
5720}
5721#endif /* CONFIG_I40E_DCB */
5722
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005723/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005724 * i40e_do_reset_safe - Protected reset path for userland calls.
5725 * @pf: board private structure
5726 * @reset_flags: which reset is requested
5727 *
5728 **/
5729void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5730{
5731 rtnl_lock();
5732 i40e_do_reset(pf, reset_flags);
5733 rtnl_unlock();
5734}
5735
5736/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005737 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5738 * @pf: board private structure
5739 * @e: event info posted on ARQ
5740 *
5741 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5742 * and VF queues
5743 **/
5744static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5745 struct i40e_arq_event_info *e)
5746{
5747 struct i40e_aqc_lan_overflow *data =
5748 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5749 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5750 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5751 struct i40e_hw *hw = &pf->hw;
5752 struct i40e_vf *vf;
5753 u16 vf_id;
5754
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005755 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5756 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005757
5758 /* Queue belongs to VF, find the VF and issue VF reset */
5759 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5760 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5761 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5762 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5763 vf_id -= hw->func_caps.vf_base_id;
5764 vf = &pf->vf[vf_id];
5765 i40e_vc_notify_vf_reset(vf);
5766 /* Allow VF to process pending reset notification */
5767 msleep(20);
5768 i40e_reset_vf(vf, false);
5769 }
5770}
5771
5772/**
5773 * i40e_service_event_complete - Finish up the service event
5774 * @pf: board private structure
5775 **/
5776static void i40e_service_event_complete(struct i40e_pf *pf)
5777{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005778 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005779
5780 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005781 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005782 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5783}
5784
5785/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005786 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5787 * @pf: board private structure
5788 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005789u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005790{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005791 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005792
5793 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5794 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5795 return fcnt_prog;
5796}
5797
5798/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005799 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005800 * @pf: board private structure
5801 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005802u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005803{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005804 u32 val, fcnt_prog;
5805
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005806 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5807 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5808 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5809 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5810 return fcnt_prog;
5811}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005812
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005813/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005814 * i40e_get_global_fd_count - Get total FD filters programmed on device
5815 * @pf: board private structure
5816 **/
5817u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5818{
5819 u32 val, fcnt_prog;
5820
5821 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5822 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5823 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5824 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5825 return fcnt_prog;
5826}
5827
5828/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005829 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5830 * @pf: board private structure
5831 **/
5832void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5833{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005834 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005835 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005836 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005837
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005838 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5839 return;
5840
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005841 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5842 * to re-enable
5843 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005844 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005845 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005846 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5847 (pf->fd_add_err == 0) ||
5848 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005849 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5850 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5851 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005852 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5853 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 +00005854 }
5855 }
5856 /* Wait for some more space to be available to turn on ATR */
5857 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5858 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5859 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5860 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005861 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5862 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 +00005863 }
5864 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005865
5866 /* if hw had a problem adding a filter, delete it */
5867 if (pf->fd_inv > 0) {
5868 hlist_for_each_entry_safe(filter, node,
5869 &pf->fdir_filter_list, fdir_node) {
5870 if (filter->fd_id == pf->fd_inv) {
5871 hlist_del(&filter->fdir_node);
5872 kfree(filter);
5873 pf->fdir_pf_active_filters--;
5874 }
5875 }
5876 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005877}
5878
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005879#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005880#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005881/**
5882 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5883 * @pf: board private structure
5884 **/
5885static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5886{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005887 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005888 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005889 bool disable_atr = false;
5890 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005891 int reg;
5892
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005893 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5894 return;
5895
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005896 if (!time_after(jiffies, pf->fd_flush_timestamp +
5897 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5898 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005899
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005900 /* If the flush is happening too quick and we have mostly SB rules we
5901 * should not re-enable ATR for some time.
5902 */
5903 min_flush_time = pf->fd_flush_timestamp +
5904 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5905 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005906
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005907 if (!(time_after(jiffies, min_flush_time)) &&
5908 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5909 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5910 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5911 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005912 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005913
5914 pf->fd_flush_timestamp = jiffies;
5915 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5916 /* flush all filters */
5917 wr32(&pf->hw, I40E_PFQF_CTL_1,
5918 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5919 i40e_flush(&pf->hw);
5920 pf->fd_flush_cnt++;
5921 pf->fd_add_err = 0;
5922 do {
5923 /* Check FD flush status every 5-6msec */
5924 usleep_range(5000, 6000);
5925 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5926 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5927 break;
5928 } while (flush_wait_retry--);
5929 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5930 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5931 } else {
5932 /* replay sideband filters */
5933 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5934 if (!disable_atr)
5935 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5936 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5937 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5938 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5939 }
5940
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005941}
5942
5943/**
5944 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5945 * @pf: board private structure
5946 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005947u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005948{
5949 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5950}
5951
5952/* We can see up to 256 filter programming desc in transit if the filters are
5953 * being applied really fast; before we see the first
5954 * filter miss error on Rx queue 0. Accumulating enough error messages before
5955 * reacting will make sure we don't cause flush too often.
5956 */
5957#define I40E_MAX_FD_PROGRAM_ERROR 256
5958
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005959/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005960 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5961 * @pf: board private structure
5962 **/
5963static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5964{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005965
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005966 /* if interface is down do nothing */
5967 if (test_bit(__I40E_DOWN, &pf->state))
5968 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005969
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005970 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5971 return;
5972
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005973 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005974 i40e_fdir_flush_and_replay(pf);
5975
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005976 i40e_fdir_check_and_reenable(pf);
5977
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005978}
5979
5980/**
5981 * i40e_vsi_link_event - notify VSI of a link event
5982 * @vsi: vsi to be notified
5983 * @link_up: link up or down
5984 **/
5985static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5986{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005987 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005988 return;
5989
5990 switch (vsi->type) {
5991 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07005992#ifdef I40E_FCOE
5993 case I40E_VSI_FCOE:
5994#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005995 if (!vsi->netdev || !vsi->netdev_registered)
5996 break;
5997
5998 if (link_up) {
5999 netif_carrier_on(vsi->netdev);
6000 netif_tx_wake_all_queues(vsi->netdev);
6001 } else {
6002 netif_carrier_off(vsi->netdev);
6003 netif_tx_stop_all_queues(vsi->netdev);
6004 }
6005 break;
6006
6007 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006008 case I40E_VSI_VMDQ2:
6009 case I40E_VSI_CTRL:
6010 case I40E_VSI_MIRROR:
6011 default:
6012 /* there is no notification for other VSIs */
6013 break;
6014 }
6015}
6016
6017/**
6018 * i40e_veb_link_event - notify elements on the veb of a link event
6019 * @veb: veb to be notified
6020 * @link_up: link up or down
6021 **/
6022static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6023{
6024 struct i40e_pf *pf;
6025 int i;
6026
6027 if (!veb || !veb->pf)
6028 return;
6029 pf = veb->pf;
6030
6031 /* depth first... */
6032 for (i = 0; i < I40E_MAX_VEB; i++)
6033 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6034 i40e_veb_link_event(pf->veb[i], link_up);
6035
6036 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006037 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006038 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6039 i40e_vsi_link_event(pf->vsi[i], link_up);
6040}
6041
6042/**
6043 * i40e_link_event - Update netif_carrier status
6044 * @pf: board private structure
6045 **/
6046static void i40e_link_event(struct i40e_pf *pf)
6047{
Mitch Williams320684c2014-10-17 03:14:43 +00006048 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006049 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006050 i40e_status status;
6051 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006052
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006053 /* save off old link status information */
6054 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6055
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006056 /* set this to force the get_link_status call to refresh state */
6057 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006058
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006059 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006060
6061 status = i40e_get_link_status(&pf->hw, &new_link);
6062 if (status) {
6063 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6064 status);
6065 return;
6066 }
6067
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006068 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6069 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006070
6071 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006072 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006073 (test_bit(__I40E_DOWN, &vsi->state) ||
6074 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006075 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006076
6077 if (!test_bit(__I40E_DOWN, &vsi->state))
6078 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006079
6080 /* Notify the base of the switch tree connected to
6081 * the link. Floating VEBs are not notified.
6082 */
6083 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6084 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6085 else
Mitch Williams320684c2014-10-17 03:14:43 +00006086 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006087
6088 if (pf->vf)
6089 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006090
6091 if (pf->flags & I40E_FLAG_PTP)
6092 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006093}
6094
6095/**
Shannon Nelson21536712014-10-25 10:35:25 +00006096 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006097 * @pf: board private structure
6098 **/
6099static void i40e_watchdog_subtask(struct i40e_pf *pf)
6100{
6101 int i;
6102
6103 /* if interface is down do nothing */
6104 if (test_bit(__I40E_DOWN, &pf->state) ||
6105 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6106 return;
6107
Shannon Nelson21536712014-10-25 10:35:25 +00006108 /* make sure we don't do these things too often */
6109 if (time_before(jiffies, (pf->service_timer_previous +
6110 pf->service_timer_period)))
6111 return;
6112 pf->service_timer_previous = jiffies;
6113
Shannon Nelson9ac77262015-08-27 11:42:40 -04006114 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6115 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006117 /* Update the stats for active netdevs so the network stack
6118 * can look at updated numbers whenever it cares to
6119 */
Mitch Williams505682c2014-05-20 08:01:37 +00006120 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006121 if (pf->vsi[i] && pf->vsi[i]->netdev)
6122 i40e_update_stats(pf->vsi[i]);
6123
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006124 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6125 /* Update the stats for the active switching components */
6126 for (i = 0; i < I40E_MAX_VEB; i++)
6127 if (pf->veb[i])
6128 i40e_update_veb_stats(pf->veb[i]);
6129 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006130
6131 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006132}
6133
6134/**
6135 * i40e_reset_subtask - Set up for resetting the device and driver
6136 * @pf: board private structure
6137 **/
6138static void i40e_reset_subtask(struct i40e_pf *pf)
6139{
6140 u32 reset_flags = 0;
6141
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006142 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006143 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006144 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006145 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6146 }
6147 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006148 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006149 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6150 }
6151 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006152 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006153 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6154 }
6155 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006156 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006157 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6158 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006159 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006160 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006161 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6162 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163
6164 /* If there's a recovery already waiting, it takes
6165 * precedence before starting a new reset sequence.
6166 */
6167 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6168 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006169 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006170 }
6171
6172 /* If we're already down or resetting, just bail */
6173 if (reset_flags &&
6174 !test_bit(__I40E_DOWN, &pf->state) &&
6175 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6176 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006177
6178unlock:
6179 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006180}
6181
6182/**
6183 * i40e_handle_link_event - Handle link event
6184 * @pf: board private structure
6185 * @e: event info posted on ARQ
6186 **/
6187static void i40e_handle_link_event(struct i40e_pf *pf,
6188 struct i40e_arq_event_info *e)
6189{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006190 struct i40e_aqc_get_link_status *status =
6191 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006192
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006193 /* Do a new status request to re-enable LSE reporting
6194 * and load new status information into the hw struct
6195 * This completely ignores any state information
6196 * in the ARQ event info, instead choosing to always
6197 * issue the AQ update link status command.
6198 */
6199 i40e_link_event(pf);
6200
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006201 /* check for unqualified module, if link is down */
6202 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6203 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6204 (!(status->link_info & I40E_AQ_LINK_UP)))
6205 dev_err(&pf->pdev->dev,
6206 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006207}
6208
6209/**
6210 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6211 * @pf: board private structure
6212 **/
6213static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6214{
6215 struct i40e_arq_event_info event;
6216 struct i40e_hw *hw = &pf->hw;
6217 u16 pending, i = 0;
6218 i40e_status ret;
6219 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006220 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006221 u32 val;
6222
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006223 /* Do not run clean AQ when PF reset fails */
6224 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6225 return;
6226
Shannon Nelson86df2422014-05-20 08:01:35 +00006227 /* check for error indications */
6228 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6229 oldval = val;
6230 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006231 if (hw->debug_mask & I40E_DEBUG_AQ)
6232 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006233 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6234 }
6235 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006236 if (hw->debug_mask & I40E_DEBUG_AQ)
6237 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006238 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
6239 }
6240 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006241 if (hw->debug_mask & I40E_DEBUG_AQ)
6242 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006243 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6244 }
6245 if (oldval != val)
6246 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6247
6248 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6249 oldval = val;
6250 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006251 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6252 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006253 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6254 }
6255 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006256 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6257 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006258 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6259 }
6260 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006261 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6262 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006263 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6264 }
6265 if (oldval != val)
6266 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6267
Mitch Williams1001dc32014-11-11 20:02:19 +00006268 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6269 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006270 if (!event.msg_buf)
6271 return;
6272
6273 do {
6274 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006275 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006276 break;
Mitch Williams56497972014-06-04 08:45:18 +00006277 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006278 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6279 break;
6280 }
6281
6282 opcode = le16_to_cpu(event.desc.opcode);
6283 switch (opcode) {
6284
6285 case i40e_aqc_opc_get_link_status:
6286 i40e_handle_link_event(pf, &event);
6287 break;
6288 case i40e_aqc_opc_send_msg_to_pf:
6289 ret = i40e_vc_process_vf_msg(pf,
6290 le16_to_cpu(event.desc.retval),
6291 le32_to_cpu(event.desc.cookie_high),
6292 le32_to_cpu(event.desc.cookie_low),
6293 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006294 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295 break;
6296 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006297 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006298#ifdef CONFIG_I40E_DCB
6299 rtnl_lock();
6300 ret = i40e_handle_lldp_event(pf, &event);
6301 rtnl_unlock();
6302#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006303 break;
6304 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006305 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006306 i40e_handle_lan_overflow_event(pf, &event);
6307 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006308 case i40e_aqc_opc_send_msg_to_peer:
6309 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6310 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006311 case i40e_aqc_opc_nvm_erase:
6312 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006313 case i40e_aqc_opc_oem_post_update:
Shannon Nelson91a0f932015-03-19 14:32:01 -07006314 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
6315 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006316 default:
6317 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00006318 "ARQ Error: Unknown event 0x%04x received\n",
6319 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 break;
6321 }
6322 } while (pending && (i++ < pf->adminq_work_limit));
6323
6324 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6325 /* re-enable Admin queue interrupt cause */
6326 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6327 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6328 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6329 i40e_flush(hw);
6330
6331 kfree(event.msg_buf);
6332}
6333
6334/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006335 * i40e_verify_eeprom - make sure eeprom is good to use
6336 * @pf: board private structure
6337 **/
6338static void i40e_verify_eeprom(struct i40e_pf *pf)
6339{
6340 int err;
6341
6342 err = i40e_diag_eeprom_test(&pf->hw);
6343 if (err) {
6344 /* retry in case of garbage read */
6345 err = i40e_diag_eeprom_test(&pf->hw);
6346 if (err) {
6347 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6348 err);
6349 set_bit(__I40E_BAD_EEPROM, &pf->state);
6350 }
6351 }
6352
6353 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6354 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6355 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6356 }
6357}
6358
6359/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006360 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006361 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006362 *
6363 * enable switch loop back or die - no point in a return value
6364 **/
6365static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6366{
6367 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6368 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006369 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006370
6371 ctxt.seid = pf->main_vsi_seid;
6372 ctxt.pf_num = pf->hw.pf_id;
6373 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006374 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6375 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006376 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006377 "couldn't get PF vsi config, err %s aq_err %s\n",
6378 i40e_stat_str(&pf->hw, ret),
6379 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006380 return;
6381 }
6382 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6383 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6384 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6385
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006386 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6387 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006388 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006389 "update vsi switch failed, err %s aq_err %s\n",
6390 i40e_stat_str(&pf->hw, ret),
6391 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006392 }
6393}
6394
6395/**
6396 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006397 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006398 *
6399 * disable switch loop back or die - no point in a return value
6400 **/
6401static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6402{
6403 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6404 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006405 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006406
6407 ctxt.seid = pf->main_vsi_seid;
6408 ctxt.pf_num = pf->hw.pf_id;
6409 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006410 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6411 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006412 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006413 "couldn't get PF vsi config, err %s aq_err %s\n",
6414 i40e_stat_str(&pf->hw, ret),
6415 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006416 return;
6417 }
6418 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6419 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6420 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6421
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006422 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6423 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006424 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006425 "update vsi switch failed, err %s aq_err %s\n",
6426 i40e_stat_str(&pf->hw, ret),
6427 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006428 }
6429}
6430
6431/**
Neerav Parikh51616012015-02-06 08:52:14 +00006432 * i40e_config_bridge_mode - Configure the HW bridge mode
6433 * @veb: pointer to the bridge instance
6434 *
6435 * Configure the loop back mode for the LAN VSI that is downlink to the
6436 * specified HW bridge instance. It is expected this function is called
6437 * when a new HW bridge is instantiated.
6438 **/
6439static void i40e_config_bridge_mode(struct i40e_veb *veb)
6440{
6441 struct i40e_pf *pf = veb->pf;
6442
Shannon Nelson6dec1012015-09-28 14:12:30 -04006443 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6444 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6445 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006446 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6447 i40e_disable_pf_switch_lb(pf);
6448 else
6449 i40e_enable_pf_switch_lb(pf);
6450}
6451
6452/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006453 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6454 * @veb: pointer to the VEB instance
6455 *
6456 * This is a recursive function that first builds the attached VSIs then
6457 * recurses in to build the next layer of VEB. We track the connections
6458 * through our own index numbers because the seid's from the HW could
6459 * change across the reset.
6460 **/
6461static int i40e_reconstitute_veb(struct i40e_veb *veb)
6462{
6463 struct i40e_vsi *ctl_vsi = NULL;
6464 struct i40e_pf *pf = veb->pf;
6465 int v, veb_idx;
6466 int ret;
6467
6468 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006469 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006470 if (pf->vsi[v] &&
6471 pf->vsi[v]->veb_idx == veb->idx &&
6472 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6473 ctl_vsi = pf->vsi[v];
6474 break;
6475 }
6476 }
6477 if (!ctl_vsi) {
6478 dev_info(&pf->pdev->dev,
6479 "missing owner VSI for veb_idx %d\n", veb->idx);
6480 ret = -ENOENT;
6481 goto end_reconstitute;
6482 }
6483 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6484 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6485 ret = i40e_add_vsi(ctl_vsi);
6486 if (ret) {
6487 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006488 "rebuild of veb_idx %d owner VSI failed: %d\n",
6489 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006490 goto end_reconstitute;
6491 }
6492 i40e_vsi_reset_stats(ctl_vsi);
6493
6494 /* create the VEB in the switch and move the VSI onto the VEB */
6495 ret = i40e_add_veb(veb, ctl_vsi);
6496 if (ret)
6497 goto end_reconstitute;
6498
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006499 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6500 veb->bridge_mode = BRIDGE_MODE_VEB;
6501 else
6502 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006503 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006504
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006505 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006506 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006507 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6508 continue;
6509
6510 if (pf->vsi[v]->veb_idx == veb->idx) {
6511 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006512
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006513 vsi->uplink_seid = veb->seid;
6514 ret = i40e_add_vsi(vsi);
6515 if (ret) {
6516 dev_info(&pf->pdev->dev,
6517 "rebuild of vsi_idx %d failed: %d\n",
6518 v, ret);
6519 goto end_reconstitute;
6520 }
6521 i40e_vsi_reset_stats(vsi);
6522 }
6523 }
6524
6525 /* create any VEBs attached to this VEB - RECURSION */
6526 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6527 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6528 pf->veb[veb_idx]->uplink_seid = veb->seid;
6529 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6530 if (ret)
6531 break;
6532 }
6533 }
6534
6535end_reconstitute:
6536 return ret;
6537}
6538
6539/**
6540 * i40e_get_capabilities - get info about the HW
6541 * @pf: the PF struct
6542 **/
6543static int i40e_get_capabilities(struct i40e_pf *pf)
6544{
6545 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6546 u16 data_size;
6547 int buf_len;
6548 int err;
6549
6550 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6551 do {
6552 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6553 if (!cap_buf)
6554 return -ENOMEM;
6555
6556 /* this loads the data into the hw struct for us */
6557 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6558 &data_size,
6559 i40e_aqc_opc_list_func_capabilities,
6560 NULL);
6561 /* data loaded, buffer no longer needed */
6562 kfree(cap_buf);
6563
6564 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6565 /* retry with a larger buffer */
6566 buf_len = data_size;
6567 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6568 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006569 "capability discovery failed, err %s aq_err %s\n",
6570 i40e_stat_str(&pf->hw, err),
6571 i40e_aq_str(&pf->hw,
6572 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006573 return -ENODEV;
6574 }
6575 } while (err);
6576
6577 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6578 dev_info(&pf->pdev->dev,
6579 "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",
6580 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6581 pf->hw.func_caps.num_msix_vectors,
6582 pf->hw.func_caps.num_msix_vectors_vf,
6583 pf->hw.func_caps.fd_filters_guaranteed,
6584 pf->hw.func_caps.fd_filters_best_effort,
6585 pf->hw.func_caps.num_tx_qp,
6586 pf->hw.func_caps.num_vsis);
6587
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006588#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6589 + pf->hw.func_caps.num_vfs)
6590 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6591 dev_info(&pf->pdev->dev,
6592 "got num_vsis %d, setting num_vsis to %d\n",
6593 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6594 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6595 }
6596
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006597 return 0;
6598}
6599
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006600static int i40e_vsi_clear(struct i40e_vsi *vsi);
6601
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006602/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006603 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006604 * @pf: board private structure
6605 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006606static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006607{
6608 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006609 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006610
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006611 /* quick workaround for an NVM issue that leaves a critical register
6612 * uninitialized
6613 */
6614 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6615 static const u32 hkey[] = {
6616 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6617 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6618 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6619 0x95b3a76d};
6620
6621 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6622 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6623 }
6624
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006625 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006626 return;
6627
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006628 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006629 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006630 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006631 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006632 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006633 break;
6634 }
6635 }
6636
6637 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006638 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006639 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6640 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006641 if (!vsi) {
6642 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006643 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6644 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006645 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006646 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006647
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006648 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006649}
6650
6651/**
6652 * i40e_fdir_teardown - release the Flow Director resources
6653 * @pf: board private structure
6654 **/
6655static void i40e_fdir_teardown(struct i40e_pf *pf)
6656{
6657 int i;
6658
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006659 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006660 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006661 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6662 i40e_vsi_release(pf->vsi[i]);
6663 break;
6664 }
6665 }
6666}
6667
6668/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006669 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006670 * @pf: board private structure
6671 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006672 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006673 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006674static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006675{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006677 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006678 u32 v;
6679
6680 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6681 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006682 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006683
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006684 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006686 /* quiesce the VSIs and their queues that are not already DOWN */
6687 i40e_pf_quiesce_all_vsi(pf);
6688
Mitch Williams505682c2014-05-20 08:01:37 +00006689 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690 if (pf->vsi[v])
6691 pf->vsi[v]->seid = 0;
6692 }
6693
6694 i40e_shutdown_adminq(&pf->hw);
6695
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006696 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006697 if (hw->hmc.hmc_obj) {
6698 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006699 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006700 dev_warn(&pf->pdev->dev,
6701 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006702 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006703}
6704
6705/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006706 * i40e_send_version - update firmware with driver version
6707 * @pf: PF struct
6708 */
6709static void i40e_send_version(struct i40e_pf *pf)
6710{
6711 struct i40e_driver_version dv;
6712
6713 dv.major_version = DRV_VERSION_MAJOR;
6714 dv.minor_version = DRV_VERSION_MINOR;
6715 dv.build_version = DRV_VERSION_BUILD;
6716 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006717 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006718 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6719}
6720
6721/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006722 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006723 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006724 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006725 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006726static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006727{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006728 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006729 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006730 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006731 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006732 u32 v;
6733
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006734 /* Now we wait for GRST to settle out.
6735 * We don't have to delete the VEBs or VSIs from the hw switch
6736 * because the reset will make them disappear.
6737 */
6738 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006739 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006740 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006741 set_bit(__I40E_RESET_FAILED, &pf->state);
6742 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006743 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006744 pf->pfr_count++;
6745
6746 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006747 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006748 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006749
6750 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6751 ret = i40e_init_adminq(&pf->hw);
6752 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006753 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6754 i40e_stat_str(&pf->hw, ret),
6755 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006756 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006757 }
6758
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006759 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006760 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006761 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006762
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006763 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006764 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006765 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006766 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006767
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006768 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6769 hw->func_caps.num_rx_qp,
6770 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6771 if (ret) {
6772 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6773 goto end_core_reset;
6774 }
6775 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6776 if (ret) {
6777 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6778 goto end_core_reset;
6779 }
6780
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006781#ifdef CONFIG_I40E_DCB
6782 ret = i40e_init_pf_dcb(pf);
6783 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006784 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6785 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6786 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006787 }
6788#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006789#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006790 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006791
Vasu Dev38e00432014-08-01 13:27:03 -07006792#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006793 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006794 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006795 if (ret)
6796 goto end_core_reset;
6797
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006798 /* driver is only interested in link up/down and module qualification
6799 * reports from firmware
6800 */
6801 ret = i40e_aq_set_phy_int_mask(&pf->hw,
6802 I40E_AQ_EVENT_LINK_UPDOWN |
6803 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
6804 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006805 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6806 i40e_stat_str(&pf->hw, ret),
6807 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006808
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006809 /* make sure our flow control settings are restored */
6810 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6811 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006812 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6813 i40e_stat_str(&pf->hw, ret),
6814 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006815
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 /* Rebuild the VSIs and VEBs that existed before reset.
6817 * They are still in our local switch element arrays, so only
6818 * need to rebuild the switch model in the HW.
6819 *
6820 * If there were VEBs but the reconstitution failed, we'll try
6821 * try to recover minimal use by getting the basic PF VSI working.
6822 */
6823 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006824 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006825 /* find the one VEB connected to the MAC, and find orphans */
6826 for (v = 0; v < I40E_MAX_VEB; v++) {
6827 if (!pf->veb[v])
6828 continue;
6829
6830 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6831 pf->veb[v]->uplink_seid == 0) {
6832 ret = i40e_reconstitute_veb(pf->veb[v]);
6833
6834 if (!ret)
6835 continue;
6836
6837 /* If Main VEB failed, we're in deep doodoo,
6838 * so give up rebuilding the switch and set up
6839 * for minimal rebuild of PF VSI.
6840 * If orphan failed, we'll report the error
6841 * but try to keep going.
6842 */
6843 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6844 dev_info(&pf->pdev->dev,
6845 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6846 ret);
6847 pf->vsi[pf->lan_vsi]->uplink_seid
6848 = pf->mac_seid;
6849 break;
6850 } else if (pf->veb[v]->uplink_seid == 0) {
6851 dev_info(&pf->pdev->dev,
6852 "rebuild of orphan VEB failed: %d\n",
6853 ret);
6854 }
6855 }
6856 }
6857 }
6858
6859 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006860 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861 /* no VEB, so rebuild only the Main VSI */
6862 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6863 if (ret) {
6864 dev_info(&pf->pdev->dev,
6865 "rebuild of Main VSI failed: %d\n", ret);
6866 goto end_core_reset;
6867 }
6868 }
6869
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006870 /* Reconfigure hardware for allowing smaller MSS in the case
6871 * of TSO, so that we avoid the MDD being fired and causing
6872 * a reset in the case of small MSS+TSO.
6873 */
6874#define I40E_REG_MSS 0x000E64DC
6875#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6876#define I40E_64BYTE_MSS 0x400000
6877 val = rd32(hw, I40E_REG_MSS);
6878 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6879 val &= ~I40E_REG_MSS_MIN_MASK;
6880 val |= I40E_64BYTE_MSS;
6881 wr32(hw, I40E_REG_MSS, val);
6882 }
6883
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006884 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
6885 (pf->hw.aq.fw_maj_ver < 4)) {
6886 msleep(75);
6887 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6888 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006889 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6890 i40e_stat_str(&pf->hw, ret),
6891 i40e_aq_str(&pf->hw,
6892 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006893 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006894 /* reinit the misc interrupt */
6895 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6896 ret = i40e_setup_misc_vector(pf);
6897
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006898 /* Add a filter to drop all Flow control frames from any VSI from being
6899 * transmitted. By doing so we stop a malicious VF from sending out
6900 * PAUSE or PFC frames and potentially controlling traffic for other
6901 * PF/VF VSIs.
6902 * The FW can still send Flow control frames if enabled.
6903 */
6904 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6905 pf->main_vsi_seid);
6906
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006907 /* restart the VSIs that were rebuilt and running before the reset */
6908 i40e_pf_unquiesce_all_vsi(pf);
6909
Mitch Williams69f64b22014-02-13 03:48:46 -08006910 if (pf->num_alloc_vfs) {
6911 for (v = 0; v < pf->num_alloc_vfs; v++)
6912 i40e_reset_vf(&pf->vf[v], true);
6913 }
6914
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006915 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006916 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006917
6918end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006919 clear_bit(__I40E_RESET_FAILED, &pf->state);
6920clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006921 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6922}
6923
6924/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006925 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006926 * @pf: board private structure
6927 *
6928 * Close up the VFs and other things in prep for a Core Reset,
6929 * then get ready to rebuild the world.
6930 **/
6931static void i40e_handle_reset_warning(struct i40e_pf *pf)
6932{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006933 i40e_prep_for_reset(pf);
6934 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006935}
6936
6937/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006938 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006939 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006940 *
6941 * Called from the MDD irq handler to identify possibly malicious vfs
6942 **/
6943static void i40e_handle_mdd_event(struct i40e_pf *pf)
6944{
6945 struct i40e_hw *hw = &pf->hw;
6946 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006947 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948 struct i40e_vf *vf;
6949 u32 reg;
6950 int i;
6951
6952 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6953 return;
6954
6955 /* find what triggered the MDD event */
6956 reg = rd32(hw, I40E_GL_MDET_TX);
6957 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006958 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6959 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006960 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006961 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006962 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006963 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006964 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6965 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6966 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006967 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006968 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 +00006969 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6971 mdd_detected = true;
6972 }
6973 reg = rd32(hw, I40E_GL_MDET_RX);
6974 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006975 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6976 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006977 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006978 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006979 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6980 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6981 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006982 if (netif_msg_rx_err(pf))
6983 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6984 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006985 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6986 mdd_detected = true;
6987 }
6988
Neerav Parikhdf430b12014-06-04 01:23:15 +00006989 if (mdd_detected) {
6990 reg = rd32(hw, I40E_PF_MDET_TX);
6991 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
6992 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006993 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00006994 pf_mdd_detected = true;
6995 }
6996 reg = rd32(hw, I40E_PF_MDET_RX);
6997 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
6998 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006999 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007000 pf_mdd_detected = true;
7001 }
7002 /* Queue belongs to the PF, initiate a reset */
7003 if (pf_mdd_detected) {
7004 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7005 i40e_service_event_schedule(pf);
7006 }
7007 }
7008
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007009 /* see if one of the VFs needs its hand slapped */
7010 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7011 vf = &(pf->vf[i]);
7012 reg = rd32(hw, I40E_VP_MDET_TX(i));
7013 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7014 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7015 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007016 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7017 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007018 }
7019
7020 reg = rd32(hw, I40E_VP_MDET_RX(i));
7021 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7022 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7023 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007024 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7025 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007026 }
7027
7028 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7029 dev_info(&pf->pdev->dev,
7030 "Too many MDD events on VF %d, disabled\n", i);
7031 dev_info(&pf->pdev->dev,
7032 "Use PF Control I/F to re-enable the VF\n");
7033 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7034 }
7035 }
7036
7037 /* re-enable mdd interrupt cause */
7038 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7039 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7040 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7041 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7042 i40e_flush(hw);
7043}
7044
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007045/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007046 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007047 * @pf: board private structure
7048 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007049static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007050{
Singhai, Anjali6a899022015-12-14 12:21:18 -08007051#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007052 struct i40e_hw *hw = &pf->hw;
7053 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007054 __be16 port;
7055 int i;
7056
Singhai, Anjali6a899022015-12-14 12:21:18 -08007057 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007058 return;
7059
Singhai, Anjali6a899022015-12-14 12:21:18 -08007060 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007061
7062 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007063 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7064 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7065 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007066 if (port)
7067 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Singhai, Anjali6a899022015-12-14 12:21:18 -08007068 pf->udp_ports[i].type,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007069 NULL, NULL);
7070 else
7071 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007072
7073 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007074 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007075 "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007076 port ? "add" : "delete",
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007077 ntohs(port), i,
7078 i40e_stat_str(&pf->hw, ret),
7079 i40e_aq_str(&pf->hw,
7080 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007081 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007082 }
7083 }
7084 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08007085#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007086}
7087
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007088/**
7089 * i40e_service_task - Run the driver's async subtasks
7090 * @work: pointer to work_struct containing our data
7091 **/
7092static void i40e_service_task(struct work_struct *work)
7093{
7094 struct i40e_pf *pf = container_of(work,
7095 struct i40e_pf,
7096 service_task);
7097 unsigned long start_time = jiffies;
7098
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007099 /* don't bother with service tasks if a reset is in progress */
7100 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7101 i40e_service_event_complete(pf);
7102 return;
7103 }
7104
Kiran Patilb03a8c12015-09-24 18:13:15 -04007105 i40e_detect_recover_hung(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007106 i40e_reset_subtask(pf);
7107 i40e_handle_mdd_event(pf);
7108 i40e_vc_process_vflr_event(pf);
7109 i40e_watchdog_subtask(pf);
7110 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007111 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007112#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
7113 i40e_sync_udp_filters_subtask(pf);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007114#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007115 i40e_clean_adminq_subtask(pf);
7116
7117 i40e_service_event_complete(pf);
7118
7119 /* If the tasks have taken longer than one timer cycle or there
7120 * is more work to be done, reschedule the service task now
7121 * rather than wait for the timer to tick again.
7122 */
7123 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7124 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7125 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7126 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7127 i40e_service_event_schedule(pf);
7128}
7129
7130/**
7131 * i40e_service_timer - timer callback
7132 * @data: pointer to PF struct
7133 **/
7134static void i40e_service_timer(unsigned long data)
7135{
7136 struct i40e_pf *pf = (struct i40e_pf *)data;
7137
7138 mod_timer(&pf->service_timer,
7139 round_jiffies(jiffies + pf->service_timer_period));
7140 i40e_service_event_schedule(pf);
7141}
7142
7143/**
7144 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7145 * @vsi: the VSI being configured
7146 **/
7147static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7148{
7149 struct i40e_pf *pf = vsi->back;
7150
7151 switch (vsi->type) {
7152 case I40E_VSI_MAIN:
7153 vsi->alloc_queue_pairs = pf->num_lan_qps;
7154 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7155 I40E_REQ_DESCRIPTOR_MULTIPLE);
7156 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7157 vsi->num_q_vectors = pf->num_lan_msix;
7158 else
7159 vsi->num_q_vectors = 1;
7160
7161 break;
7162
7163 case I40E_VSI_FDIR:
7164 vsi->alloc_queue_pairs = 1;
7165 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7166 I40E_REQ_DESCRIPTOR_MULTIPLE);
7167 vsi->num_q_vectors = 1;
7168 break;
7169
7170 case I40E_VSI_VMDQ2:
7171 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7172 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7173 I40E_REQ_DESCRIPTOR_MULTIPLE);
7174 vsi->num_q_vectors = pf->num_vmdq_msix;
7175 break;
7176
7177 case I40E_VSI_SRIOV:
7178 vsi->alloc_queue_pairs = pf->num_vf_qps;
7179 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7180 I40E_REQ_DESCRIPTOR_MULTIPLE);
7181 break;
7182
Vasu Dev38e00432014-08-01 13:27:03 -07007183#ifdef I40E_FCOE
7184 case I40E_VSI_FCOE:
7185 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7186 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7187 I40E_REQ_DESCRIPTOR_MULTIPLE);
7188 vsi->num_q_vectors = pf->num_fcoe_msix;
7189 break;
7190
7191#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007192 default:
7193 WARN_ON(1);
7194 return -ENODATA;
7195 }
7196
7197 return 0;
7198}
7199
7200/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007201 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7202 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007203 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007204 *
7205 * On error: returns error code (negative)
7206 * On success: returns 0
7207 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007208static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007209{
7210 int size;
7211 int ret = 0;
7212
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007213 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007214 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7215 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7216 if (!vsi->tx_rings)
7217 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007218 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7219
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007220 if (alloc_qvectors) {
7221 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007222 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007223 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7224 if (!vsi->q_vectors) {
7225 ret = -ENOMEM;
7226 goto err_vectors;
7227 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007228 }
7229 return ret;
7230
7231err_vectors:
7232 kfree(vsi->tx_rings);
7233 return ret;
7234}
7235
7236/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007237 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7238 * @pf: board private structure
7239 * @type: type of VSI
7240 *
7241 * On error: returns error code (negative)
7242 * On success: returns vsi index in PF (positive)
7243 **/
7244static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7245{
7246 int ret = -ENODEV;
7247 struct i40e_vsi *vsi;
7248 int vsi_idx;
7249 int i;
7250
7251 /* Need to protect the allocation of the VSIs at the PF level */
7252 mutex_lock(&pf->switch_mutex);
7253
7254 /* VSI list may be fragmented if VSI creation/destruction has
7255 * been happening. We can afford to do a quick scan to look
7256 * for any free VSIs in the list.
7257 *
7258 * find next empty vsi slot, looping back around if necessary
7259 */
7260 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007261 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007262 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007263 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007264 i = 0;
7265 while (i < pf->next_vsi && pf->vsi[i])
7266 i++;
7267 }
7268
Mitch Williams505682c2014-05-20 08:01:37 +00007269 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007270 vsi_idx = i; /* Found one! */
7271 } else {
7272 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007273 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007274 }
7275 pf->next_vsi = ++i;
7276
7277 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7278 if (!vsi) {
7279 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007280 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007281 }
7282 vsi->type = type;
7283 vsi->back = pf;
7284 set_bit(__I40E_DOWN, &vsi->state);
7285 vsi->flags = 0;
7286 vsi->idx = vsi_idx;
7287 vsi->rx_itr_setting = pf->rx_itr_default;
7288 vsi->tx_itr_setting = pf->tx_itr_default;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007289 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007290 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7291 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007292 vsi->netdev_registered = false;
7293 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7294 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007295 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007296
Alexander Duyck9f65e152013-09-28 06:00:58 +00007297 ret = i40e_set_num_rings_in_vsi(vsi);
7298 if (ret)
7299 goto err_rings;
7300
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007301 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007302 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007303 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007304
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007305 /* Setup default MSIX irq handler for VSI */
7306 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7307
Kiran Patil21659032015-09-30 14:09:03 -04007308 /* Initialize VSI lock */
7309 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007310 pf->vsi[vsi_idx] = vsi;
7311 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007312 goto unlock_pf;
7313
Alexander Duyck9f65e152013-09-28 06:00:58 +00007314err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007315 pf->next_vsi = i - 1;
7316 kfree(vsi);
7317unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007318 mutex_unlock(&pf->switch_mutex);
7319 return ret;
7320}
7321
7322/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007323 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7324 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007325 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007326 *
7327 * On error: returns error code (negative)
7328 * On success: returns 0
7329 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007330static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007331{
7332 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007333 if (free_qvectors) {
7334 kfree(vsi->q_vectors);
7335 vsi->q_vectors = NULL;
7336 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007337 kfree(vsi->tx_rings);
7338 vsi->tx_rings = NULL;
7339 vsi->rx_rings = NULL;
7340}
7341
7342/**
Helin Zhang28c58692015-10-26 19:44:27 -04007343 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7344 * and lookup table
7345 * @vsi: Pointer to VSI structure
7346 */
7347static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7348{
7349 if (!vsi)
7350 return;
7351
7352 kfree(vsi->rss_hkey_user);
7353 vsi->rss_hkey_user = NULL;
7354
7355 kfree(vsi->rss_lut_user);
7356 vsi->rss_lut_user = NULL;
7357}
7358
7359/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007360 * i40e_vsi_clear - Deallocate the VSI provided
7361 * @vsi: the VSI being un-configured
7362 **/
7363static int i40e_vsi_clear(struct i40e_vsi *vsi)
7364{
7365 struct i40e_pf *pf;
7366
7367 if (!vsi)
7368 return 0;
7369
7370 if (!vsi->back)
7371 goto free_vsi;
7372 pf = vsi->back;
7373
7374 mutex_lock(&pf->switch_mutex);
7375 if (!pf->vsi[vsi->idx]) {
7376 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7377 vsi->idx, vsi->idx, vsi, vsi->type);
7378 goto unlock_vsi;
7379 }
7380
7381 if (pf->vsi[vsi->idx] != vsi) {
7382 dev_err(&pf->pdev->dev,
7383 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7384 pf->vsi[vsi->idx]->idx,
7385 pf->vsi[vsi->idx],
7386 pf->vsi[vsi->idx]->type,
7387 vsi->idx, vsi, vsi->type);
7388 goto unlock_vsi;
7389 }
7390
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007391 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007392 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7393 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7394
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007395 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007396 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007397
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007398 pf->vsi[vsi->idx] = NULL;
7399 if (vsi->idx < pf->next_vsi)
7400 pf->next_vsi = vsi->idx;
7401
7402unlock_vsi:
7403 mutex_unlock(&pf->switch_mutex);
7404free_vsi:
7405 kfree(vsi);
7406
7407 return 0;
7408}
7409
7410/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007411 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7412 * @vsi: the VSI being cleaned
7413 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007414static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007415{
7416 int i;
7417
Greg Rose8e9dca52013-12-18 13:45:53 +00007418 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007419 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007420 kfree_rcu(vsi->tx_rings[i], rcu);
7421 vsi->tx_rings[i] = NULL;
7422 vsi->rx_rings[i] = NULL;
7423 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007424 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007425}
7426
7427/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007428 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7429 * @vsi: the VSI being configured
7430 **/
7431static int i40e_alloc_rings(struct i40e_vsi *vsi)
7432{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007433 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007434 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007435 int i;
7436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007437 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007438 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007439 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007440 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7441 if (!tx_ring)
7442 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007443
7444 tx_ring->queue_index = i;
7445 tx_ring->reg_idx = vsi->base_queue + i;
7446 tx_ring->ring_active = false;
7447 tx_ring->vsi = vsi;
7448 tx_ring->netdev = vsi->netdev;
7449 tx_ring->dev = &pf->pdev->dev;
7450 tx_ring->count = vsi->num_desc;
7451 tx_ring->size = 0;
7452 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007453 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7454 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04007455 if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
7456 tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007457 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007458
Alexander Duyck9f65e152013-09-28 06:00:58 +00007459 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007460 rx_ring->queue_index = i;
7461 rx_ring->reg_idx = vsi->base_queue + i;
7462 rx_ring->ring_active = false;
7463 rx_ring->vsi = vsi;
7464 rx_ring->netdev = vsi->netdev;
7465 rx_ring->dev = &pf->pdev->dev;
7466 rx_ring->count = vsi->num_desc;
7467 rx_ring->size = 0;
7468 rx_ring->dcb_tc = 0;
7469 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7470 set_ring_16byte_desc_enabled(rx_ring);
7471 else
7472 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e152013-09-28 06:00:58 +00007473 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007474 }
7475
7476 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007477
7478err_out:
7479 i40e_vsi_clear_rings(vsi);
7480 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481}
7482
7483/**
7484 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7485 * @pf: board private structure
7486 * @vectors: the number of MSI-X vectors to request
7487 *
7488 * Returns the number of vectors reserved, or error
7489 **/
7490static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7491{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007492 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7493 I40E_MIN_MSIX, vectors);
7494 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007495 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007496 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007497 vectors = 0;
7498 }
7499
7500 return vectors;
7501}
7502
7503/**
7504 * i40e_init_msix - Setup the MSIX capability
7505 * @pf: board private structure
7506 *
7507 * Work with the OS to set up the MSIX vectors needed.
7508 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007509 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007510 **/
7511static int i40e_init_msix(struct i40e_pf *pf)
7512{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007513 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007514 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007515 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007516 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007517
7518 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7519 return -ENODEV;
7520
7521 /* The number of vectors we'll request will be comprised of:
7522 * - Add 1 for "other" cause for Admin Queue events, etc.
7523 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007524 * - Queues being used for RSS.
7525 * We don't need as many as max_rss_size vectors.
7526 * use rss_size instead in the calculation since that
7527 * is governed by number of cpus in the system.
7528 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007529 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007530#ifdef I40E_FCOE
7531 * - The number of FCOE qps.
7532#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007533 * Once we count this up, try the request.
7534 *
7535 * If we can't get what we want, we'll simplify to nearly nothing
7536 * and try again. If that still fails, we punt.
7537 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007538 vectors_left = hw->func_caps.num_msix_vectors;
7539 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007540
Shannon Nelson1e200e42015-02-27 09:15:24 +00007541 /* reserve one vector for miscellaneous handler */
7542 if (vectors_left) {
7543 v_budget++;
7544 vectors_left--;
7545 }
7546
7547 /* reserve vectors for the main PF traffic queues */
7548 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7549 vectors_left -= pf->num_lan_msix;
7550 v_budget += pf->num_lan_msix;
7551
7552 /* reserve one vector for sideband flow director */
7553 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7554 if (vectors_left) {
7555 v_budget++;
7556 vectors_left--;
7557 } else {
7558 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7559 }
7560 }
John W Linville83840e42015-01-14 03:06:28 +00007561
Vasu Dev38e00432014-08-01 13:27:03 -07007562#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007563 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007564 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007565 if (!vectors_left)
7566 pf->num_fcoe_msix = 0;
7567 else if (vectors_left >= pf->num_fcoe_qps)
7568 pf->num_fcoe_msix = pf->num_fcoe_qps;
7569 else
7570 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007571 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007572 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007573 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007574
Vasu Dev38e00432014-08-01 13:27:03 -07007575#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007576 /* any vectors left over go for VMDq support */
7577 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7578 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7579 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7580
7581 /* if we're short on vectors for what's desired, we limit
7582 * the queues per vmdq. If this is still more than are
7583 * available, the user will need to change the number of
7584 * queues/vectors used by the PF later with the ethtool
7585 * channels command
7586 */
7587 if (vmdq_vecs < vmdq_vecs_wanted)
7588 pf->num_vmdq_qps = 1;
7589 pf->num_vmdq_msix = pf->num_vmdq_qps;
7590
7591 v_budget += vmdq_vecs;
7592 vectors_left -= vmdq_vecs;
7593 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007594
7595 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7596 GFP_KERNEL);
7597 if (!pf->msix_entries)
7598 return -ENOMEM;
7599
7600 for (i = 0; i < v_budget; i++)
7601 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007602 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007603
Shannon Nelson3b444392015-02-26 16:15:57 +00007604 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007605 /* If we have limited resources, we will start with no vectors
7606 * for the special features and then allocate vectors to some
7607 * of these features based on the policy and at the end disable
7608 * the features that did not get any vectors.
7609 */
Vasu Dev38e00432014-08-01 13:27:03 -07007610#ifdef I40E_FCOE
7611 pf->num_fcoe_qps = 0;
7612 pf->num_fcoe_msix = 0;
7613#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007614 pf->num_vmdq_msix = 0;
7615 }
7616
Shannon Nelson3b444392015-02-26 16:15:57 +00007617 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007618 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7619 kfree(pf->msix_entries);
7620 pf->msix_entries = NULL;
7621 return -ENODEV;
7622
Shannon Nelson3b444392015-02-26 16:15:57 +00007623 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007624 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007625 pf->num_vmdq_vsis = 0;
7626 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007627 pf->num_lan_qps = 1;
7628 pf->num_lan_msix = 1;
7629
Shannon Nelson3b444392015-02-26 16:15:57 +00007630 } else if (v_actual != v_budget) {
7631 int vec;
7632
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007633 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007634 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007635
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007636 /* Scale vector usage down */
7637 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007638 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007639 pf->num_vmdq_qps = 1;
7640 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007641
7642 /* partition out the remaining vectors */
7643 switch (vec) {
7644 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007645 pf->num_lan_msix = 1;
7646 break;
7647 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007648#ifdef I40E_FCOE
7649 /* give one vector to FCoE */
7650 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7651 pf->num_lan_msix = 1;
7652 pf->num_fcoe_msix = 1;
7653 }
7654#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007655 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007656#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007657 break;
7658 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007659#ifdef I40E_FCOE
7660 /* give one vector to FCoE */
7661 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7662 pf->num_fcoe_msix = 1;
7663 vec--;
7664 }
7665#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007666 /* give the rest to the PF */
7667 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007668 break;
7669 }
7670 }
7671
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007672 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7673 (pf->num_vmdq_msix == 0)) {
7674 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7675 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7676 }
Vasu Dev38e00432014-08-01 13:27:03 -07007677#ifdef I40E_FCOE
7678
7679 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7680 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7681 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7682 }
7683#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007684 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007685}
7686
7687/**
Greg Rose90e04072014-03-06 08:59:57 +00007688 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007689 * @vsi: the VSI being configured
7690 * @v_idx: index of the vector in the vsi struct
7691 *
7692 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7693 **/
Greg Rose90e04072014-03-06 08:59:57 +00007694static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007695{
7696 struct i40e_q_vector *q_vector;
7697
7698 /* allocate q_vector */
7699 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7700 if (!q_vector)
7701 return -ENOMEM;
7702
7703 q_vector->vsi = vsi;
7704 q_vector->v_idx = v_idx;
7705 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7706 if (vsi->netdev)
7707 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007708 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007709
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007710 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7711 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7712
Alexander Duyck493fb302013-09-28 07:01:44 +00007713 /* tie q_vector and vsi together */
7714 vsi->q_vectors[v_idx] = q_vector;
7715
7716 return 0;
7717}
7718
7719/**
Greg Rose90e04072014-03-06 08:59:57 +00007720 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007721 * @vsi: the VSI being configured
7722 *
7723 * We allocate one q_vector per queue interrupt. If allocation fails we
7724 * return -ENOMEM.
7725 **/
Greg Rose90e04072014-03-06 08:59:57 +00007726static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007727{
7728 struct i40e_pf *pf = vsi->back;
7729 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007730 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007731
7732 /* if not MSIX, give the one vector only to the LAN VSI */
7733 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7734 num_q_vectors = vsi->num_q_vectors;
7735 else if (vsi == pf->vsi[pf->lan_vsi])
7736 num_q_vectors = 1;
7737 else
7738 return -EINVAL;
7739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007740 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007741 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007742 if (err)
7743 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007744 }
7745
7746 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007747
7748err_out:
7749 while (v_idx--)
7750 i40e_free_q_vector(vsi, v_idx);
7751
7752 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007753}
7754
7755/**
7756 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7757 * @pf: board private structure to initialize
7758 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007759static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760{
Shannon Nelson3b444392015-02-26 16:15:57 +00007761 int vectors = 0;
7762 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007763
7764 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007765 vectors = i40e_init_msix(pf);
7766 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007767 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007768#ifdef I40E_FCOE
7769 I40E_FLAG_FCOE_ENABLED |
7770#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007771 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007772 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007773 I40E_FLAG_SRIOV_ENABLED |
7774 I40E_FLAG_FD_SB_ENABLED |
7775 I40E_FLAG_FD_ATR_ENABLED |
7776 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007777
7778 /* rework the queue expectations without MSIX */
7779 i40e_determine_queue_usage(pf);
7780 }
7781 }
7782
7783 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7784 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007785 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007786 vectors = pci_enable_msi(pf->pdev);
7787 if (vectors < 0) {
7788 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7789 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007790 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7791 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007792 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007793 }
7794
Shannon Nelson958a3e32013-09-28 07:13:28 +00007795 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007796 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007797
Shannon Nelson3b444392015-02-26 16:15:57 +00007798 /* set up vector assignment tracking */
7799 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7800 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007801 if (!pf->irq_pile) {
7802 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7803 return -ENOMEM;
7804 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007805 pf->irq_pile->num_entries = vectors;
7806 pf->irq_pile->search_hint = 0;
7807
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007808 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007809 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007810
7811 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007812}
7813
7814/**
7815 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7816 * @pf: board private structure
7817 *
7818 * This sets up the handler for MSIX 0, which is used to manage the
7819 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7820 * when in MSI or Legacy interrupt mode.
7821 **/
7822static int i40e_setup_misc_vector(struct i40e_pf *pf)
7823{
7824 struct i40e_hw *hw = &pf->hw;
7825 int err = 0;
7826
7827 /* Only request the irq if this is the first time through, and
7828 * not when we're rebuilding after a Reset
7829 */
7830 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7831 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007832 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007833 if (err) {
7834 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007835 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007836 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837 return -EFAULT;
7838 }
7839 }
7840
Jacob Kellerab437b52014-12-14 01:55:08 +00007841 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007842
7843 /* associate no queues to the misc vector */
7844 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7845 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7846
7847 i40e_flush(hw);
7848
7849 i40e_irq_dynamic_enable_icr0(pf);
7850
7851 return err;
7852}
7853
7854/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007855 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7856 * @vsi: vsi structure
7857 * @seed: RSS hash seed
7858 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007859static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7860 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007861{
7862 struct i40e_aqc_get_set_rss_key_data rss_key;
7863 struct i40e_pf *pf = vsi->back;
7864 struct i40e_hw *hw = &pf->hw;
7865 bool pf_lut = false;
7866 u8 *rss_lut;
7867 int ret, i;
7868
7869 memset(&rss_key, 0, sizeof(rss_key));
7870 memcpy(&rss_key, seed, sizeof(rss_key));
7871
7872 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7873 if (!rss_lut)
7874 return -ENOMEM;
7875
7876 /* Populate the LUT with max no. of queues in round robin fashion */
7877 for (i = 0; i < vsi->rss_table_size; i++)
7878 rss_lut[i] = i % vsi->rss_size;
7879
7880 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7881 if (ret) {
7882 dev_info(&pf->pdev->dev,
7883 "Cannot set RSS key, err %s aq_err %s\n",
7884 i40e_stat_str(&pf->hw, ret),
7885 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007886 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007887 }
7888
7889 if (vsi->type == I40E_VSI_MAIN)
7890 pf_lut = true;
7891
7892 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7893 vsi->rss_table_size);
7894 if (ret)
7895 dev_info(&pf->pdev->dev,
7896 "Cannot set RSS lut, err %s aq_err %s\n",
7897 i40e_stat_str(&pf->hw, ret),
7898 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7899
Anjali Singhai126b63d2015-08-24 13:26:53 -07007900config_rss_aq_out:
7901 kfree(rss_lut);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007902 return ret;
7903}
7904
7905/**
7906 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7907 * @vsi: VSI structure
7908 **/
7909static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7910{
7911 u8 seed[I40E_HKEY_ARRAY_SIZE];
7912 struct i40e_pf *pf = vsi->back;
Helin Zhange69ff812015-10-21 19:56:22 -04007913 u8 *lut;
7914 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007915
Helin Zhange69ff812015-10-21 19:56:22 -04007916 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
7917 return 0;
7918
7919 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
7920 if (!lut)
7921 return -ENOMEM;
7922
7923 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007924 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhangacd65442015-10-26 19:44:28 -04007925 vsi->rss_size = min_t(int, pf->alloc_rss_size, vsi->num_queue_pairs);
Helin Zhange69ff812015-10-21 19:56:22 -04007926 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
7927 kfree(lut);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007928
Helin Zhange69ff812015-10-21 19:56:22 -04007929 return ret;
7930}
7931
7932/**
Helin Zhang043dd652015-10-21 19:56:23 -04007933 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04007934 * @vsi: Pointer to vsi structure
7935 * @seed: RSS hash seed
7936 * @lut: Lookup table
7937 * @lut_size: Lookup table size
7938 *
7939 * Returns 0 on success, negative on failure
7940 **/
7941static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
7942 const u8 *lut, u16 lut_size)
7943{
7944 struct i40e_pf *pf = vsi->back;
7945 struct i40e_hw *hw = &pf->hw;
7946 u8 i;
7947
7948 /* Fill out hash function seed */
7949 if (seed) {
7950 u32 *seed_dw = (u32 *)seed;
7951
7952 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
7953 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
7954 }
7955
7956 if (lut) {
7957 u32 *lut_dw = (u32 *)lut;
7958
7959 if (lut_size != I40E_HLUT_ARRAY_SIZE)
7960 return -EINVAL;
7961
7962 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
7963 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
7964 }
7965 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007966
7967 return 0;
7968}
7969
7970/**
Helin Zhang043dd652015-10-21 19:56:23 -04007971 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
7972 * @vsi: Pointer to VSI structure
7973 * @seed: Buffer to store the keys
7974 * @lut: Buffer to store the lookup table entries
7975 * @lut_size: Size of buffer to store the lookup table entries
7976 *
7977 * Returns 0 on success, negative on failure
7978 */
7979static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
7980 u8 *lut, u16 lut_size)
7981{
7982 struct i40e_pf *pf = vsi->back;
7983 struct i40e_hw *hw = &pf->hw;
7984 u16 i;
7985
7986 if (seed) {
7987 u32 *seed_dw = (u32 *)seed;
7988
7989 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
7990 seed_dw[i] = rd32(hw, I40E_PFQF_HKEY(i));
7991 }
7992 if (lut) {
7993 u32 *lut_dw = (u32 *)lut;
7994
7995 if (lut_size != I40E_HLUT_ARRAY_SIZE)
7996 return -EINVAL;
7997 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
7998 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
7999 }
8000
8001 return 0;
8002}
8003
8004/**
8005 * i40e_config_rss - Configure RSS keys and lut
8006 * @vsi: Pointer to VSI structure
8007 * @seed: RSS hash seed
8008 * @lut: Lookup table
8009 * @lut_size: Lookup table size
8010 *
8011 * Returns 0 on success, negative on failure
8012 */
8013int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8014{
8015 struct i40e_pf *pf = vsi->back;
8016
8017 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8018 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8019 else
8020 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8021}
8022
8023/**
8024 * i40e_get_rss - Get RSS keys and lut
8025 * @vsi: Pointer to VSI structure
8026 * @seed: Buffer to store the keys
8027 * @lut: Buffer to store the lookup table entries
8028 * lut_size: Size of buffer to store the lookup table entries
8029 *
8030 * Returns 0 on success, negative on failure
8031 */
8032int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8033{
8034 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
8035}
8036
8037/**
Helin Zhange69ff812015-10-21 19:56:22 -04008038 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8039 * @pf: Pointer to board private structure
8040 * @lut: Lookup table
8041 * @rss_table_size: Lookup table size
8042 * @rss_size: Range of queue number for hashing
8043 */
8044static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8045 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008046{
Helin Zhange69ff812015-10-21 19:56:22 -04008047 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008048
Helin Zhange69ff812015-10-21 19:56:22 -04008049 for (i = 0; i < rss_table_size; i++)
8050 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008051}
8052
8053/**
Helin Zhang043dd652015-10-21 19:56:23 -04008054 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008055 * @pf: board private structure
8056 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008057static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008058{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008059 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008060 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008061 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008062 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008063 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008064 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008065 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008066
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008067 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
8068 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
8069 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008070 hena |= i40e_pf_get_default_rss_hena(pf);
8071
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008072 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
8073 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
8074
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008075 /* Determine the RSS table size based on the hardware capabilities */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008076 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008077 reg_val = (pf->rss_table_size == 512) ?
8078 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8079 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008080 wr32(hw, I40E_PFQF_CTL_0, reg_val);
8081
Helin Zhang28c58692015-10-26 19:44:27 -04008082 /* Determine the RSS size of the VSI */
8083 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008084 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8085 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008086
Helin Zhange69ff812015-10-21 19:56:22 -04008087 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8088 if (!lut)
8089 return -ENOMEM;
8090
Helin Zhang28c58692015-10-26 19:44:27 -04008091 /* Use user configured lut if there is one, otherwise use default */
8092 if (vsi->rss_lut_user)
8093 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8094 else
8095 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008096
Helin Zhang28c58692015-10-26 19:44:27 -04008097 /* Use user configured hash key if there is one, otherwise
8098 * use default.
8099 */
8100 if (vsi->rss_hkey_user)
8101 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8102 else
8103 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008104 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008105 kfree(lut);
8106
8107 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008108}
8109
8110/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008111 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8112 * @pf: board private structure
8113 * @queue_count: the requested queue count for rss.
8114 *
8115 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8116 * count which may be different from the requested queue count.
8117 **/
8118int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8119{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008120 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8121 int new_rss_size;
8122
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008123 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8124 return 0;
8125
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008126 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008127
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008128 if (queue_count != vsi->num_queue_pairs) {
8129 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008130 i40e_prep_for_reset(pf);
8131
Helin Zhangacd65442015-10-26 19:44:28 -04008132 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008133
8134 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008135
8136 /* Discard the user configured hash keys and lut, if less
8137 * queues are enabled.
8138 */
8139 if (queue_count < vsi->rss_size) {
8140 i40e_clear_rss_config_user(vsi);
8141 dev_dbg(&pf->pdev->dev,
8142 "discard user configured hash keys and lut\n");
8143 }
8144
8145 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008146 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8147 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008148
Helin Zhang043dd652015-10-21 19:56:23 -04008149 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008150 }
Helin Zhange36b0b12015-10-26 19:44:36 -04008151 dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
8152 pf->alloc_rss_size, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008153 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008154}
8155
8156/**
Greg Rosef4492db2015-02-06 08:52:12 +00008157 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8158 * @pf: board private structure
8159 **/
8160i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8161{
8162 i40e_status status;
8163 bool min_valid, max_valid;
8164 u32 max_bw, min_bw;
8165
8166 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8167 &min_valid, &max_valid);
8168
8169 if (!status) {
8170 if (min_valid)
8171 pf->npar_min_bw = min_bw;
8172 if (max_valid)
8173 pf->npar_max_bw = max_bw;
8174 }
8175
8176 return status;
8177}
8178
8179/**
8180 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8181 * @pf: board private structure
8182 **/
8183i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8184{
8185 struct i40e_aqc_configure_partition_bw_data bw_data;
8186 i40e_status status;
8187
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008188 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008189 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008190 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8191 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8192
8193 /* Set the new bandwidths */
8194 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8195
8196 return status;
8197}
8198
8199/**
8200 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8201 * @pf: board private structure
8202 **/
8203i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8204{
8205 /* Commit temporary BW setting to permanent NVM image */
8206 enum i40e_admin_queue_err last_aq_status;
8207 i40e_status ret;
8208 u16 nvm_word;
8209
8210 if (pf->hw.partition_id != 1) {
8211 dev_info(&pf->pdev->dev,
8212 "Commit BW only works on partition 1! This is partition %d",
8213 pf->hw.partition_id);
8214 ret = I40E_NOT_SUPPORTED;
8215 goto bw_commit_out;
8216 }
8217
8218 /* Acquire NVM for read access */
8219 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8220 last_aq_status = pf->hw.aq.asq_last_status;
8221 if (ret) {
8222 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008223 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8224 i40e_stat_str(&pf->hw, ret),
8225 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008226 goto bw_commit_out;
8227 }
8228
8229 /* Read word 0x10 of NVM - SW compatibility word 1 */
8230 ret = i40e_aq_read_nvm(&pf->hw,
8231 I40E_SR_NVM_CONTROL_WORD,
8232 0x10, sizeof(nvm_word), &nvm_word,
8233 false, NULL);
8234 /* Save off last admin queue command status before releasing
8235 * the NVM
8236 */
8237 last_aq_status = pf->hw.aq.asq_last_status;
8238 i40e_release_nvm(&pf->hw);
8239 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008240 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8241 i40e_stat_str(&pf->hw, ret),
8242 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008243 goto bw_commit_out;
8244 }
8245
8246 /* Wait a bit for NVM release to complete */
8247 msleep(50);
8248
8249 /* Acquire NVM for write access */
8250 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8251 last_aq_status = pf->hw.aq.asq_last_status;
8252 if (ret) {
8253 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008254 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8255 i40e_stat_str(&pf->hw, ret),
8256 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008257 goto bw_commit_out;
8258 }
8259 /* Write it back out unchanged to initiate update NVM,
8260 * which will force a write of the shadow (alt) RAM to
8261 * the NVM - thus storing the bandwidth values permanently.
8262 */
8263 ret = i40e_aq_update_nvm(&pf->hw,
8264 I40E_SR_NVM_CONTROL_WORD,
8265 0x10, sizeof(nvm_word),
8266 &nvm_word, true, NULL);
8267 /* Save off last admin queue command status before releasing
8268 * the NVM
8269 */
8270 last_aq_status = pf->hw.aq.asq_last_status;
8271 i40e_release_nvm(&pf->hw);
8272 if (ret)
8273 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008274 "BW settings NOT SAVED, err %s aq_err %s\n",
8275 i40e_stat_str(&pf->hw, ret),
8276 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008277bw_commit_out:
8278
8279 return ret;
8280}
8281
8282/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008283 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8284 * @pf: board private structure to initialize
8285 *
8286 * i40e_sw_init initializes the Adapter private data structure.
8287 * Fields are initialized based on PCI device information and
8288 * OS network device settings (MTU size).
8289 **/
8290static int i40e_sw_init(struct i40e_pf *pf)
8291{
8292 int err = 0;
8293 int size;
8294
8295 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8296 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00008297 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008298 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8299 if (I40E_DEBUG_USER & debug)
8300 pf->hw.debug_mask = debug;
8301 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8302 I40E_DEFAULT_MSG_ENABLE);
8303 }
8304
8305 /* Set default capability flags */
8306 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8307 I40E_FLAG_MSI_ENABLED |
Shannon Nelson9ac77262015-08-27 11:42:40 -04008308 I40E_FLAG_LINK_POLLING_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008309 I40E_FLAG_MSIX_ENABLED;
8310
8311 if (iommu_present(&pci_bus_type))
8312 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
8313 else
8314 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008315
Mitch Williamsca99eb92014-04-04 04:43:07 +00008316 /* Set default ITR */
8317 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8318 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8319
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008320 /* Depending on PF configurations, it is possible that the RSS
8321 * maximum might end up larger than the available queues
8322 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008323 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008324 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008325 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008326 pf->rss_size_max = min_t(int, pf->rss_size_max,
8327 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008328 if (pf->hw.func_caps.rss) {
8329 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008330 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8331 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008332 }
8333
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008334 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008335 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008336 pf->flags |= I40E_FLAG_MFP_ENABLED;
8337 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008338 if (i40e_get_npar_bw_setting(pf))
8339 dev_warn(&pf->pdev->dev,
8340 "Could not get NPAR bw settings\n");
8341 else
8342 dev_info(&pf->pdev->dev,
8343 "Min BW = %8.8x, Max BW = %8.8x\n",
8344 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008345 }
8346
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008347 /* FW/NVM is not yet fixed in this regard */
8348 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8349 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8350 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8351 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008352 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8353 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008354 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008355 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008356 else
8357 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008358 pf->fdir_pf_filter_count =
8359 pf->hw.func_caps.fd_filters_guaranteed;
8360 pf->hw.fdir_shared_filter_count =
8361 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008362 }
8363
8364 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008365 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008366 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008367 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008368 }
8369
Vasu Dev38e00432014-08-01 13:27:03 -07008370#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008371 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008372
8373#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008375 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008376 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8377 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8378 pf->num_req_vfs = min_t(int,
8379 pf->hw.func_caps.num_vfs,
8380 I40E_MAX_VF_COUNT);
8381 }
8382#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008383 if (pf->hw.mac.type == I40E_MAC_X722) {
8384 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8385 I40E_FLAG_128_QP_RSS_CAPABLE |
8386 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8387 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8388 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008389 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
8390 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008391 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008392 pf->eeprom_version = 0xDEAD;
8393 pf->lan_veb = I40E_NO_VEB;
8394 pf->lan_vsi = I40E_NO_VSI;
8395
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008396 /* By default FW has this off for performance reasons */
8397 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8398
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008399 /* set up queue assignment tracking */
8400 size = sizeof(struct i40e_lump_tracking)
8401 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8402 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8403 if (!pf->qp_pile) {
8404 err = -ENOMEM;
8405 goto sw_init_done;
8406 }
8407 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8408 pf->qp_pile->search_hint = 0;
8409
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008410 pf->tx_timeout_recovery_level = 1;
8411
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008412 mutex_init(&pf->switch_mutex);
8413
Greg Rosec668a122015-02-26 16:10:39 +00008414 /* If NPAR is enabled nudge the Tx scheduler */
8415 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8416 i40e_set_npar_bw_setting(pf);
8417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008418sw_init_done:
8419 return err;
8420}
8421
8422/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008423 * i40e_set_ntuple - set the ntuple feature flag and take action
8424 * @pf: board private structure to initialize
8425 * @features: the feature set that the stack is suggesting
8426 *
8427 * returns a bool to indicate if reset needs to happen
8428 **/
8429bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8430{
8431 bool need_reset = false;
8432
8433 /* Check if Flow Director n-tuple support was enabled or disabled. If
8434 * the state changed, we need to reset.
8435 */
8436 if (features & NETIF_F_NTUPLE) {
8437 /* Enable filters and mark for reset */
8438 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8439 need_reset = true;
8440 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8441 } else {
8442 /* turn off filters, mark for reset and clear SW filter list */
8443 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8444 need_reset = true;
8445 i40e_fdir_filter_exit(pf);
8446 }
8447 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008448 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008449 /* reset fd counters */
8450 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8451 pf->fdir_pf_active_filters = 0;
8452 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008453 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8454 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008455 /* if ATR was auto disabled it can be re-enabled. */
8456 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8457 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8458 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008459 }
8460 return need_reset;
8461}
8462
8463/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008464 * i40e_set_features - set the netdev feature flags
8465 * @netdev: ptr to the netdev being adjusted
8466 * @features: the feature set that the stack is suggesting
8467 **/
8468static int i40e_set_features(struct net_device *netdev,
8469 netdev_features_t features)
8470{
8471 struct i40e_netdev_priv *np = netdev_priv(netdev);
8472 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008473 struct i40e_pf *pf = vsi->back;
8474 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008475
8476 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8477 i40e_vlan_stripping_enable(vsi);
8478 else
8479 i40e_vlan_stripping_disable(vsi);
8480
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008481 need_reset = i40e_set_ntuple(pf, features);
8482
8483 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008484 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008485
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008486 return 0;
8487}
8488
Singhai, Anjali6a899022015-12-14 12:21:18 -08008489#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008490/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008491 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008492 * @pf: board private structure
8493 * @port: The UDP port to look up
8494 *
8495 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8496 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008497static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008498{
8499 u8 i;
8500
8501 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008502 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008503 return i;
8504 }
8505
8506 return i;
8507}
8508
Singhai, Anjali6a899022015-12-14 12:21:18 -08008509#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008510/**
8511 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8512 * @netdev: This physical port's netdev
8513 * @sa_family: Socket Family that VXLAN is notifying us about
8514 * @port: New UDP port number that VXLAN started listening to
8515 **/
8516static void i40e_add_vxlan_port(struct net_device *netdev,
8517 sa_family_t sa_family, __be16 port)
8518{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008519#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008520 struct i40e_netdev_priv *np = netdev_priv(netdev);
8521 struct i40e_vsi *vsi = np->vsi;
8522 struct i40e_pf *pf = vsi->back;
8523 u8 next_idx;
8524 u8 idx;
8525
8526 if (sa_family == AF_INET6)
8527 return;
8528
Singhai, Anjali6a899022015-12-14 12:21:18 -08008529 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008530
8531 /* Check if port already exists */
8532 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008533 netdev_info(netdev, "vxlan port %d already offloaded\n",
8534 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008535 return;
8536 }
8537
8538 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008539 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008540
8541 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008542 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008543 ntohs(port));
8544 return;
8545 }
8546
8547 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008548 pf->udp_ports[next_idx].index = port;
8549 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8550 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8551 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8552#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008553}
8554
8555/**
8556 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8557 * @netdev: This physical port's netdev
8558 * @sa_family: Socket Family that VXLAN is notifying us about
8559 * @port: UDP port number that VXLAN stopped listening to
8560 **/
8561static void i40e_del_vxlan_port(struct net_device *netdev,
8562 sa_family_t sa_family, __be16 port)
8563{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008564#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008565 struct i40e_netdev_priv *np = netdev_priv(netdev);
8566 struct i40e_vsi *vsi = np->vsi;
8567 struct i40e_pf *pf = vsi->back;
8568 u8 idx;
8569
8570 if (sa_family == AF_INET6)
8571 return;
8572
Singhai, Anjali6a899022015-12-14 12:21:18 -08008573 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008574
8575 /* Check if port already exists */
8576 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8577 /* if port exists, set it to 0 (mark for deletion)
8578 * and make it pending
8579 */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008580 pf->udp_ports[idx].index = 0;
8581 pf->pending_udp_bitmap |= BIT_ULL(idx);
8582 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008583 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008584 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008585 ntohs(port));
8586 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08008587#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008588}
8589
Singhai, Anjali6a899022015-12-14 12:21:18 -08008590/**
8591 * i40e_add_geneve_port - Get notifications about GENEVE ports that come up
8592 * @netdev: This physical port's netdev
8593 * @sa_family: Socket Family that GENEVE is notifying us about
8594 * @port: New UDP port number that GENEVE started listening to
8595 **/
8596static void i40e_add_geneve_port(struct net_device *netdev,
8597 sa_family_t sa_family, __be16 port)
8598{
8599#if IS_ENABLED(CONFIG_GENEVE)
8600 struct i40e_netdev_priv *np = netdev_priv(netdev);
8601 struct i40e_vsi *vsi = np->vsi;
8602 struct i40e_pf *pf = vsi->back;
8603 u8 next_idx;
8604 u8 idx;
8605
8606 if (sa_family == AF_INET6)
8607 return;
8608
8609 idx = i40e_get_udp_port_idx(pf, port);
8610
8611 /* Check if port already exists */
8612 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8613 netdev_info(netdev, "udp port %d already offloaded\n",
8614 ntohs(port));
8615 return;
8616 }
8617
8618 /* Now check if there is space to add the new port */
8619 next_idx = i40e_get_udp_port_idx(pf, 0);
8620
8621 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8622 netdev_info(netdev, "maximum number of UDP ports reached, not adding port %d\n",
8623 ntohs(port));
8624 return;
8625 }
8626
8627 /* New port: add it and mark its index in the bitmap */
8628 pf->udp_ports[next_idx].index = port;
8629 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8630 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8631 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8632
8633 dev_info(&pf->pdev->dev, "adding geneve port %d\n", ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008634#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008635}
8636
8637/**
8638 * i40e_del_geneve_port - Get notifications about GENEVE ports that go away
8639 * @netdev: This physical port's netdev
8640 * @sa_family: Socket Family that GENEVE is notifying us about
8641 * @port: UDP port number that GENEVE stopped listening to
8642 **/
8643static void i40e_del_geneve_port(struct net_device *netdev,
8644 sa_family_t sa_family, __be16 port)
8645{
8646#if IS_ENABLED(CONFIG_GENEVE)
8647 struct i40e_netdev_priv *np = netdev_priv(netdev);
8648 struct i40e_vsi *vsi = np->vsi;
8649 struct i40e_pf *pf = vsi->back;
8650 u8 idx;
8651
8652 if (sa_family == AF_INET6)
8653 return;
8654
8655 idx = i40e_get_udp_port_idx(pf, port);
8656
8657 /* Check if port already exists */
8658 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8659 /* if port exists, set it to 0 (mark for deletion)
8660 * and make it pending
8661 */
8662 pf->udp_ports[idx].index = 0;
8663 pf->pending_udp_bitmap |= BIT_ULL(idx);
8664 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8665
8666 dev_info(&pf->pdev->dev, "deleting geneve port %d\n",
8667 ntohs(port));
8668 } else {
8669 netdev_warn(netdev, "geneve port %d was not found, not deleting\n",
8670 ntohs(port));
8671 }
8672#endif
8673}
8674
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008675static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008676 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008677{
8678 struct i40e_netdev_priv *np = netdev_priv(netdev);
8679 struct i40e_pf *pf = np->vsi->back;
8680 struct i40e_hw *hw = &pf->hw;
8681
8682 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8683 return -EOPNOTSUPP;
8684
8685 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8686 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8687
8688 return 0;
8689}
8690
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008691/**
8692 * i40e_ndo_fdb_add - add an entry to the hardware database
8693 * @ndm: the input from the stack
8694 * @tb: pointer to array of nladdr (unused)
8695 * @dev: the net device pointer
8696 * @addr: the MAC address entry being added
8697 * @flags: instructions from stack about fdb operation
8698 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008699static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8700 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008701 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008702 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008703{
8704 struct i40e_netdev_priv *np = netdev_priv(dev);
8705 struct i40e_pf *pf = np->vsi->back;
8706 int err = 0;
8707
8708 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8709 return -EOPNOTSUPP;
8710
Or Gerlitz65891fe2014-12-14 18:19:05 +02008711 if (vid) {
8712 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8713 return -EINVAL;
8714 }
8715
Greg Rose4ba0dea2014-03-06 08:59:55 +00008716 /* Hardware does not support aging addresses so if a
8717 * ndm_state is given only allow permanent addresses
8718 */
8719 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8720 netdev_info(dev, "FDB only supports static addresses\n");
8721 return -EINVAL;
8722 }
8723
8724 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8725 err = dev_uc_add_excl(dev, addr);
8726 else if (is_multicast_ether_addr(addr))
8727 err = dev_mc_add_excl(dev, addr);
8728 else
8729 err = -EINVAL;
8730
8731 /* Only return duplicate errors if NLM_F_EXCL is set */
8732 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8733 err = 0;
8734
8735 return err;
8736}
8737
Neerav Parikh51616012015-02-06 08:52:14 +00008738/**
8739 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8740 * @dev: the netdev being configured
8741 * @nlh: RTNL message
8742 *
8743 * Inserts a new hardware bridge if not already created and
8744 * enables the bridging mode requested (VEB or VEPA). If the
8745 * hardware bridge has already been inserted and the request
8746 * is to change the mode then that requires a PF reset to
8747 * allow rebuild of the components with required hardware
8748 * bridge mode enabled.
8749 **/
8750static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008751 struct nlmsghdr *nlh,
8752 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008753{
8754 struct i40e_netdev_priv *np = netdev_priv(dev);
8755 struct i40e_vsi *vsi = np->vsi;
8756 struct i40e_pf *pf = vsi->back;
8757 struct i40e_veb *veb = NULL;
8758 struct nlattr *attr, *br_spec;
8759 int i, rem;
8760
8761 /* Only for PF VSI for now */
8762 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8763 return -EOPNOTSUPP;
8764
8765 /* Find the HW bridge for PF VSI */
8766 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8767 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8768 veb = pf->veb[i];
8769 }
8770
8771 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8772
8773 nla_for_each_nested(attr, br_spec, rem) {
8774 __u16 mode;
8775
8776 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8777 continue;
8778
8779 mode = nla_get_u16(attr);
8780 if ((mode != BRIDGE_MODE_VEPA) &&
8781 (mode != BRIDGE_MODE_VEB))
8782 return -EINVAL;
8783
8784 /* Insert a new HW bridge */
8785 if (!veb) {
8786 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8787 vsi->tc_config.enabled_tc);
8788 if (veb) {
8789 veb->bridge_mode = mode;
8790 i40e_config_bridge_mode(veb);
8791 } else {
8792 /* No Bridge HW offload available */
8793 return -ENOENT;
8794 }
8795 break;
8796 } else if (mode != veb->bridge_mode) {
8797 /* Existing HW bridge but different mode needs reset */
8798 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008799 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8800 if (mode == BRIDGE_MODE_VEB)
8801 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8802 else
8803 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8804 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008805 break;
8806 }
8807 }
8808
8809 return 0;
8810}
8811
8812/**
8813 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8814 * @skb: skb buff
8815 * @pid: process id
8816 * @seq: RTNL message seq #
8817 * @dev: the netdev being configured
8818 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008819 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008820 *
8821 * Return the mode in which the hardware bridge is operating in
8822 * i.e VEB or VEPA.
8823 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008824static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8825 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008826 u32 __always_unused filter_mask,
8827 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008828{
8829 struct i40e_netdev_priv *np = netdev_priv(dev);
8830 struct i40e_vsi *vsi = np->vsi;
8831 struct i40e_pf *pf = vsi->back;
8832 struct i40e_veb *veb = NULL;
8833 int i;
8834
8835 /* Only for PF VSI for now */
8836 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8837 return -EOPNOTSUPP;
8838
8839 /* Find the HW bridge for the PF VSI */
8840 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8841 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8842 veb = pf->veb[i];
8843 }
8844
8845 if (!veb)
8846 return 0;
8847
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008848 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008849 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00008850}
Neerav Parikh51616012015-02-06 08:52:14 +00008851
Singhai, Anjali6a899022015-12-14 12:21:18 -08008852/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
8853 * inner mac plus all inner ethertypes.
8854 */
8855#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07008856/**
8857 * i40e_features_check - Validate encapsulated packet conforms to limits
8858 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06008859 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07008860 * @features: Offload features that the stack believes apply
8861 **/
8862static netdev_features_t i40e_features_check(struct sk_buff *skb,
8863 struct net_device *dev,
8864 netdev_features_t features)
8865{
8866 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08008867 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07008868 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08008869 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07008870
8871 return features;
8872}
8873
Shannon Nelson37a29732015-02-27 09:15:19 +00008874static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008875 .ndo_open = i40e_open,
8876 .ndo_stop = i40e_close,
8877 .ndo_start_xmit = i40e_lan_xmit_frame,
8878 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8879 .ndo_set_rx_mode = i40e_set_rx_mode,
8880 .ndo_validate_addr = eth_validate_addr,
8881 .ndo_set_mac_address = i40e_set_mac,
8882 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008883 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008884 .ndo_tx_timeout = i40e_tx_timeout,
8885 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8886 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8887#ifdef CONFIG_NET_POLL_CONTROLLER
8888 .ndo_poll_controller = i40e_netpoll,
8889#endif
8890 .ndo_setup_tc = i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008891#ifdef I40E_FCOE
8892 .ndo_fcoe_enable = i40e_fcoe_enable,
8893 .ndo_fcoe_disable = i40e_fcoe_disable,
8894#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008895 .ndo_set_features = i40e_set_features,
8896 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8897 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008898 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008899 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008900 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008901 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Singhai, Anjali6a899022015-12-14 12:21:18 -08008902#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008903 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8904 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8905#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008906#if IS_ENABLED(CONFIG_GENEVE)
8907 .ndo_add_geneve_port = i40e_add_geneve_port,
8908 .ndo_del_geneve_port = i40e_del_geneve_port,
8909#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008910 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008911 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07008912 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00008913 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8914 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008915};
8916
8917/**
8918 * i40e_config_netdev - Setup the netdev flags
8919 * @vsi: the VSI being configured
8920 *
8921 * Returns 0 on success, negative value on failure
8922 **/
8923static int i40e_config_netdev(struct i40e_vsi *vsi)
8924{
Greg Rose1a103702013-11-28 06:42:39 +00008925 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008926 struct i40e_pf *pf = vsi->back;
8927 struct i40e_hw *hw = &pf->hw;
8928 struct i40e_netdev_priv *np;
8929 struct net_device *netdev;
8930 u8 mac_addr[ETH_ALEN];
8931 int etherdev_size;
8932
8933 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008934 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008935 if (!netdev)
8936 return -ENOMEM;
8937
8938 vsi->netdev = netdev;
8939 np = netdev_priv(netdev);
8940 np->vsi = vsi;
8941
Or Gerlitzd70e9412014-03-18 10:36:45 +02008942 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008943 NETIF_F_RXCSUM |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008944 NETIF_F_GSO_UDP_TUNNEL |
Jesse Brandeburgfec31ff2015-10-01 18:25:42 -07008945 NETIF_F_GSO_GRE |
Or Gerlitzd70e9412014-03-18 10:36:45 +02008946 NETIF_F_TSO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008947
8948 netdev->features = NETIF_F_SG |
8949 NETIF_F_IP_CSUM |
Tom Herbert53692b12015-12-14 11:19:41 -08008950 NETIF_F_SCTP_CRC |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008951 NETIF_F_HIGHDMA |
8952 NETIF_F_GSO_UDP_TUNNEL |
Jesse Brandeburgfec31ff2015-10-01 18:25:42 -07008953 NETIF_F_GSO_GRE |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008954 NETIF_F_HW_VLAN_CTAG_TX |
8955 NETIF_F_HW_VLAN_CTAG_RX |
8956 NETIF_F_HW_VLAN_CTAG_FILTER |
8957 NETIF_F_IPV6_CSUM |
8958 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00008959 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008960 NETIF_F_TSO6 |
8961 NETIF_F_RXCSUM |
8962 NETIF_F_RXHASH |
8963 0;
8964
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00008965 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
8966 netdev->features |= NETIF_F_NTUPLE;
8967
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008968 /* copy netdev features into list of user selectable features */
8969 netdev->hw_features |= netdev->features;
8970
8971 if (vsi->type == I40E_VSI_MAIN) {
8972 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00008973 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008974 /* The following steps are necessary to prevent reception
8975 * of tagged packets - some older NVM configurations load a
8976 * default a MAC-VLAN filter that accepts any tagged packet
8977 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00008978 */
Kiran Patil21659032015-09-30 14:09:03 -04008979 if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
8980 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00008981 i40e_add_filter(vsi, mac_addr,
8982 I40E_VLAN_ANY, false, true);
Kiran Patil21659032015-09-30 14:09:03 -04008983 spin_unlock_bh(&vsi->mac_filter_list_lock);
8984 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008985 } else {
8986 /* relate the VSI_VMDQ name to the VSI_MAIN name */
8987 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
8988 pf->vsi[pf->lan_vsi]->netdev->name);
8989 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04008990
8991 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008992 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04008993 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008994 }
Kiran Patil21659032015-09-30 14:09:03 -04008995
8996 spin_lock_bh(&vsi->mac_filter_list_lock);
Greg Rose1a103702013-11-28 06:42:39 +00008997 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04008998 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008999
Greg Rose9a173902014-05-22 06:32:02 +00009000 ether_addr_copy(netdev->dev_addr, mac_addr);
9001 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009002 /* vlan gets same features (except vlan offload)
9003 * after any tweaks for specific VSI types
9004 */
9005 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
9006 NETIF_F_HW_VLAN_CTAG_RX |
9007 NETIF_F_HW_VLAN_CTAG_FILTER);
9008 netdev->priv_flags |= IFF_UNICAST_FLT;
9009 netdev->priv_flags |= IFF_SUPP_NOFCS;
9010 /* Setup netdev TC information */
9011 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9012
9013 netdev->netdev_ops = &i40e_netdev_ops;
9014 netdev->watchdog_timeo = 5 * HZ;
9015 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009016#ifdef I40E_FCOE
9017 i40e_fcoe_config_netdev(netdev, vsi);
9018#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009019
9020 return 0;
9021}
9022
9023/**
9024 * i40e_vsi_delete - Delete a VSI from the switch
9025 * @vsi: the VSI being removed
9026 *
9027 * Returns 0 on success, negative value on failure
9028 **/
9029static void i40e_vsi_delete(struct i40e_vsi *vsi)
9030{
9031 /* remove default VSI is not allowed */
9032 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9033 return;
9034
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009035 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009036}
9037
9038/**
Neerav Parikh51616012015-02-06 08:52:14 +00009039 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9040 * @vsi: the VSI being queried
9041 *
9042 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9043 **/
9044int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9045{
9046 struct i40e_veb *veb;
9047 struct i40e_pf *pf = vsi->back;
9048
9049 /* Uplink is not a bridge so default to VEB */
9050 if (vsi->veb_idx == I40E_NO_VEB)
9051 return 1;
9052
9053 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009054 if (!veb) {
9055 dev_info(&pf->pdev->dev,
9056 "There is no veb associated with the bridge\n");
9057 return -ENOENT;
9058 }
Neerav Parikh51616012015-02-06 08:52:14 +00009059
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009060 /* Uplink is a bridge in VEPA mode */
9061 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9062 return 0;
9063 } else {
9064 /* Uplink is a bridge in VEB mode */
9065 return 1;
9066 }
9067
9068 /* VEPA is now default bridge, so return 0 */
9069 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009070}
9071
9072/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009073 * i40e_add_vsi - Add a VSI to the switch
9074 * @vsi: the VSI being configured
9075 *
9076 * This initializes a VSI context depending on the VSI type to be added and
9077 * passes it down to the add_vsi aq command.
9078 **/
9079static int i40e_add_vsi(struct i40e_vsi *vsi)
9080{
9081 int ret = -ENODEV;
Kiran Patil21659032015-09-30 14:09:03 -04009082 u8 laa_macaddr[ETH_ALEN];
9083 bool found_laa_mac_filter = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009084 struct i40e_pf *pf = vsi->back;
9085 struct i40e_hw *hw = &pf->hw;
9086 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009087 struct i40e_mac_filter *f, *ftmp;
9088
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009089 u8 enabled_tc = 0x1; /* TC0 enabled */
9090 int f_count = 0;
9091
9092 memset(&ctxt, 0, sizeof(ctxt));
9093 switch (vsi->type) {
9094 case I40E_VSI_MAIN:
9095 /* The PF's main VSI is already setup as part of the
9096 * device initialization, so we'll not bother with
9097 * the add_vsi call, but we will retrieve the current
9098 * VSI context.
9099 */
9100 ctxt.seid = pf->main_vsi_seid;
9101 ctxt.pf_num = pf->hw.pf_id;
9102 ctxt.vf_num = 0;
9103 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9104 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9105 if (ret) {
9106 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009107 "couldn't get PF vsi config, err %s aq_err %s\n",
9108 i40e_stat_str(&pf->hw, ret),
9109 i40e_aq_str(&pf->hw,
9110 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009111 return -ENOENT;
9112 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009113 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009114 vsi->info.valid_sections = 0;
9115
9116 vsi->seid = ctxt.seid;
9117 vsi->id = ctxt.vsi_number;
9118
9119 enabled_tc = i40e_pf_get_tc_map(pf);
9120
9121 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009122 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9123 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009124 memset(&ctxt, 0, sizeof(ctxt));
9125 ctxt.seid = pf->main_vsi_seid;
9126 ctxt.pf_num = pf->hw.pf_id;
9127 ctxt.vf_num = 0;
9128 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9129 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9130 if (ret) {
9131 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009132 "update vsi failed, err %s aq_err %s\n",
9133 i40e_stat_str(&pf->hw, ret),
9134 i40e_aq_str(&pf->hw,
9135 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009136 ret = -ENOENT;
9137 goto err;
9138 }
9139 /* update the local VSI info queue map */
9140 i40e_vsi_update_queue_map(vsi, &ctxt);
9141 vsi->info.valid_sections = 0;
9142 } else {
9143 /* Default/Main VSI is only enabled for TC0
9144 * reconfigure it to enable all TCs that are
9145 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009146 * For MFP case the iSCSI PF would use this
9147 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009148 */
9149 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9150 if (ret) {
9151 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009152 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9153 enabled_tc,
9154 i40e_stat_str(&pf->hw, ret),
9155 i40e_aq_str(&pf->hw,
9156 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009157 ret = -ENOENT;
9158 }
9159 }
9160 break;
9161
9162 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009163 ctxt.pf_num = hw->pf_id;
9164 ctxt.vf_num = 0;
9165 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009166 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009167 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009168 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9169 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009170 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009171 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009172 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009173 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009174 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009175 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009176 break;
9177
9178 case I40E_VSI_VMDQ2:
9179 ctxt.pf_num = hw->pf_id;
9180 ctxt.vf_num = 0;
9181 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009182 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009183 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9184
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009185 /* This VSI is connected to VEB so the switch_id
9186 * should be set to zero by default.
9187 */
Neerav Parikh51616012015-02-06 08:52:14 +00009188 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9189 ctxt.info.valid_sections |=
9190 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9191 ctxt.info.switch_id =
9192 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9193 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009194
9195 /* Setup the VSI tx/rx queue map for TC0 only for now */
9196 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9197 break;
9198
9199 case I40E_VSI_SRIOV:
9200 ctxt.pf_num = hw->pf_id;
9201 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9202 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009203 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009204 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9205
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009206 /* This VSI is connected to VEB so the switch_id
9207 * should be set to zero by default.
9208 */
Neerav Parikh51616012015-02-06 08:52:14 +00009209 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9210 ctxt.info.valid_sections |=
9211 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9212 ctxt.info.switch_id =
9213 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9214 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009215
9216 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9217 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009218 if (pf->vf[vsi->vf_id].spoofchk) {
9219 ctxt.info.valid_sections |=
9220 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9221 ctxt.info.sec_flags |=
9222 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9223 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9224 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009225 /* Setup the VSI tx/rx queue map for TC0 only for now */
9226 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9227 break;
9228
Vasu Dev38e00432014-08-01 13:27:03 -07009229#ifdef I40E_FCOE
9230 case I40E_VSI_FCOE:
9231 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9232 if (ret) {
9233 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9234 return ret;
9235 }
9236 break;
9237
9238#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009239 default:
9240 return -ENODEV;
9241 }
9242
9243 if (vsi->type != I40E_VSI_MAIN) {
9244 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9245 if (ret) {
9246 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009247 "add vsi failed, err %s aq_err %s\n",
9248 i40e_stat_str(&pf->hw, ret),
9249 i40e_aq_str(&pf->hw,
9250 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009251 ret = -ENOENT;
9252 goto err;
9253 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009254 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009255 vsi->info.valid_sections = 0;
9256 vsi->seid = ctxt.seid;
9257 vsi->id = ctxt.vsi_number;
9258 }
9259
Kiran Patil21659032015-09-30 14:09:03 -04009260 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009261 /* If macvlan filters already exist, force them to get loaded */
9262 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
9263 f->changed = true;
9264 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009265
Kiran Patil21659032015-09-30 14:09:03 -04009266 /* Expected to have only one MAC filter entry for LAA in list */
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009267 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Kiran Patil21659032015-09-30 14:09:03 -04009268 ether_addr_copy(laa_macaddr, f->macaddr);
9269 found_laa_mac_filter = true;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009270 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009271 }
Kiran Patil21659032015-09-30 14:09:03 -04009272 spin_unlock_bh(&vsi->mac_filter_list_lock);
9273
9274 if (found_laa_mac_filter) {
9275 struct i40e_aqc_remove_macvlan_element_data element;
9276
9277 memset(&element, 0, sizeof(element));
9278 ether_addr_copy(element.mac_addr, laa_macaddr);
9279 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
9280 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
9281 &element, 1, NULL);
9282 if (ret) {
9283 /* some older FW has a different default */
9284 element.flags |=
9285 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
9286 i40e_aq_remove_macvlan(hw, vsi->seid,
9287 &element, 1, NULL);
9288 }
9289
9290 i40e_aq_mac_address_write(hw,
9291 I40E_AQC_WRITE_TYPE_LAA_WOL,
9292 laa_macaddr, NULL);
9293 }
9294
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009295 if (f_count) {
9296 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9297 pf->flags |= I40E_FLAG_FILTER_SYNC;
9298 }
9299
9300 /* Update VSI BW information */
9301 ret = i40e_vsi_get_bw_info(vsi);
9302 if (ret) {
9303 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009304 "couldn't get vsi bw info, err %s aq_err %s\n",
9305 i40e_stat_str(&pf->hw, ret),
9306 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009307 /* VSI is already added so not tearing that up */
9308 ret = 0;
9309 }
9310
9311err:
9312 return ret;
9313}
9314
9315/**
9316 * i40e_vsi_release - Delete a VSI and free its resources
9317 * @vsi: the VSI being removed
9318 *
9319 * Returns 0 on success or < 0 on error
9320 **/
9321int i40e_vsi_release(struct i40e_vsi *vsi)
9322{
9323 struct i40e_mac_filter *f, *ftmp;
9324 struct i40e_veb *veb = NULL;
9325 struct i40e_pf *pf;
9326 u16 uplink_seid;
9327 int i, n;
9328
9329 pf = vsi->back;
9330
9331 /* release of a VEB-owner or last VSI is not allowed */
9332 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9333 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9334 vsi->seid, vsi->uplink_seid);
9335 return -ENODEV;
9336 }
9337 if (vsi == pf->vsi[pf->lan_vsi] &&
9338 !test_bit(__I40E_DOWN, &pf->state)) {
9339 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9340 return -ENODEV;
9341 }
9342
9343 uplink_seid = vsi->uplink_seid;
9344 if (vsi->type != I40E_VSI_SRIOV) {
9345 if (vsi->netdev_registered) {
9346 vsi->netdev_registered = false;
9347 if (vsi->netdev) {
9348 /* results in a call to i40e_close() */
9349 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009350 }
9351 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009352 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009353 }
9354 i40e_vsi_disable_irq(vsi);
9355 }
9356
Kiran Patil21659032015-09-30 14:09:03 -04009357 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009358 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9359 i40e_del_filter(vsi, f->macaddr, f->vlan,
9360 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009361 spin_unlock_bh(&vsi->mac_filter_list_lock);
9362
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009363 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009364
9365 i40e_vsi_delete(vsi);
9366 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009367 if (vsi->netdev) {
9368 free_netdev(vsi->netdev);
9369 vsi->netdev = NULL;
9370 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009371 i40e_vsi_clear_rings(vsi);
9372 i40e_vsi_clear(vsi);
9373
9374 /* If this was the last thing on the VEB, except for the
9375 * controlling VSI, remove the VEB, which puts the controlling
9376 * VSI onto the next level down in the switch.
9377 *
9378 * Well, okay, there's one more exception here: don't remove
9379 * the orphan VEBs yet. We'll wait for an explicit remove request
9380 * from up the network stack.
9381 */
Mitch Williams505682c2014-05-20 08:01:37 +00009382 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009383 if (pf->vsi[i] &&
9384 pf->vsi[i]->uplink_seid == uplink_seid &&
9385 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9386 n++; /* count the VSIs */
9387 }
9388 }
9389 for (i = 0; i < I40E_MAX_VEB; i++) {
9390 if (!pf->veb[i])
9391 continue;
9392 if (pf->veb[i]->uplink_seid == uplink_seid)
9393 n++; /* count the VEBs */
9394 if (pf->veb[i]->seid == uplink_seid)
9395 veb = pf->veb[i];
9396 }
9397 if (n == 0 && veb && veb->uplink_seid != 0)
9398 i40e_veb_release(veb);
9399
9400 return 0;
9401}
9402
9403/**
9404 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9405 * @vsi: ptr to the VSI
9406 *
9407 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9408 * corresponding SW VSI structure and initializes num_queue_pairs for the
9409 * newly allocated VSI.
9410 *
9411 * Returns 0 on success or negative on failure
9412 **/
9413static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9414{
9415 int ret = -ENOENT;
9416 struct i40e_pf *pf = vsi->back;
9417
Alexander Duyck493fb302013-09-28 07:01:44 +00009418 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009419 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9420 vsi->seid);
9421 return -EEXIST;
9422 }
9423
9424 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009425 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009426 vsi->seid, vsi->base_vector);
9427 return -EEXIST;
9428 }
9429
Greg Rose90e04072014-03-06 08:59:57 +00009430 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009431 if (ret) {
9432 dev_info(&pf->pdev->dev,
9433 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9434 vsi->num_q_vectors, vsi->seid, ret);
9435 vsi->num_q_vectors = 0;
9436 goto vector_setup_out;
9437 }
9438
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009439 /* In Legacy mode, we do not have to get any other vector since we
9440 * piggyback on the misc/ICR0 for queue interrupts.
9441 */
9442 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9443 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009444 if (vsi->num_q_vectors)
9445 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9446 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447 if (vsi->base_vector < 0) {
9448 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009449 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9450 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009451 i40e_vsi_free_q_vectors(vsi);
9452 ret = -ENOENT;
9453 goto vector_setup_out;
9454 }
9455
9456vector_setup_out:
9457 return ret;
9458}
9459
9460/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009461 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9462 * @vsi: pointer to the vsi.
9463 *
9464 * This re-allocates a vsi's queue resources.
9465 *
9466 * Returns pointer to the successfully allocated and configured VSI sw struct
9467 * on success, otherwise returns NULL on failure.
9468 **/
9469static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9470{
9471 struct i40e_pf *pf = vsi->back;
9472 u8 enabled_tc;
9473 int ret;
9474
9475 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9476 i40e_vsi_clear_rings(vsi);
9477
9478 i40e_vsi_free_arrays(vsi, false);
9479 i40e_set_num_rings_in_vsi(vsi);
9480 ret = i40e_vsi_alloc_arrays(vsi, false);
9481 if (ret)
9482 goto err_vsi;
9483
9484 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9485 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009486 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009487 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009488 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009489 goto err_vsi;
9490 }
9491 vsi->base_queue = ret;
9492
9493 /* Update the FW view of the VSI. Force a reset of TC and queue
9494 * layout configurations.
9495 */
9496 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9497 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9498 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9499 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9500
9501 /* assign it some queues */
9502 ret = i40e_alloc_rings(vsi);
9503 if (ret)
9504 goto err_rings;
9505
9506 /* map all of the rings to the q_vectors */
9507 i40e_vsi_map_rings_to_vectors(vsi);
9508 return vsi;
9509
9510err_rings:
9511 i40e_vsi_free_q_vectors(vsi);
9512 if (vsi->netdev_registered) {
9513 vsi->netdev_registered = false;
9514 unregister_netdev(vsi->netdev);
9515 free_netdev(vsi->netdev);
9516 vsi->netdev = NULL;
9517 }
9518 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9519err_vsi:
9520 i40e_vsi_clear(vsi);
9521 return NULL;
9522}
9523
9524/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009525 * i40e_vsi_setup - Set up a VSI by a given type
9526 * @pf: board private structure
9527 * @type: VSI type
9528 * @uplink_seid: the switch element to link to
9529 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9530 *
9531 * This allocates the sw VSI structure and its queue resources, then add a VSI
9532 * to the identified VEB.
9533 *
9534 * Returns pointer to the successfully allocated and configure VSI sw struct on
9535 * success, otherwise returns NULL on failure.
9536 **/
9537struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9538 u16 uplink_seid, u32 param1)
9539{
9540 struct i40e_vsi *vsi = NULL;
9541 struct i40e_veb *veb = NULL;
9542 int ret, i;
9543 int v_idx;
9544
9545 /* The requested uplink_seid must be either
9546 * - the PF's port seid
9547 * no VEB is needed because this is the PF
9548 * or this is a Flow Director special case VSI
9549 * - seid of an existing VEB
9550 * - seid of a VSI that owns an existing VEB
9551 * - seid of a VSI that doesn't own a VEB
9552 * a new VEB is created and the VSI becomes the owner
9553 * - seid of the PF VSI, which is what creates the first VEB
9554 * this is a special case of the previous
9555 *
9556 * Find which uplink_seid we were given and create a new VEB if needed
9557 */
9558 for (i = 0; i < I40E_MAX_VEB; i++) {
9559 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9560 veb = pf->veb[i];
9561 break;
9562 }
9563 }
9564
9565 if (!veb && uplink_seid != pf->mac_seid) {
9566
Mitch Williams505682c2014-05-20 08:01:37 +00009567 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009568 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9569 vsi = pf->vsi[i];
9570 break;
9571 }
9572 }
9573 if (!vsi) {
9574 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9575 uplink_seid);
9576 return NULL;
9577 }
9578
9579 if (vsi->uplink_seid == pf->mac_seid)
9580 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9581 vsi->tc_config.enabled_tc);
9582 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9583 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9584 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009585 if (veb) {
9586 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9587 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009588 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009589 return NULL;
9590 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009591 /* We come up by default in VEPA mode if SRIOV is not
9592 * already enabled, in which case we can't force VEPA
9593 * mode.
9594 */
9595 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9596 veb->bridge_mode = BRIDGE_MODE_VEPA;
9597 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9598 }
Neerav Parikh51616012015-02-06 08:52:14 +00009599 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009600 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009601 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9602 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9603 veb = pf->veb[i];
9604 }
9605 if (!veb) {
9606 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9607 return NULL;
9608 }
9609
9610 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9611 uplink_seid = veb->seid;
9612 }
9613
9614 /* get vsi sw struct */
9615 v_idx = i40e_vsi_mem_alloc(pf, type);
9616 if (v_idx < 0)
9617 goto err_alloc;
9618 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009619 if (!vsi)
9620 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009621 vsi->type = type;
9622 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9623
9624 if (type == I40E_VSI_MAIN)
9625 pf->lan_vsi = v_idx;
9626 else if (type == I40E_VSI_SRIOV)
9627 vsi->vf_id = param1;
9628 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009629 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9630 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009631 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009632 dev_info(&pf->pdev->dev,
9633 "failed to get tracking for %d queues for VSI %d err=%d\n",
9634 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009635 goto err_vsi;
9636 }
9637 vsi->base_queue = ret;
9638
9639 /* get a VSI from the hardware */
9640 vsi->uplink_seid = uplink_seid;
9641 ret = i40e_add_vsi(vsi);
9642 if (ret)
9643 goto err_vsi;
9644
9645 switch (vsi->type) {
9646 /* setup the netdev if needed */
9647 case I40E_VSI_MAIN:
9648 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009649 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009650 ret = i40e_config_netdev(vsi);
9651 if (ret)
9652 goto err_netdev;
9653 ret = register_netdev(vsi->netdev);
9654 if (ret)
9655 goto err_netdev;
9656 vsi->netdev_registered = true;
9657 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009658#ifdef CONFIG_I40E_DCB
9659 /* Setup DCB netlink interface */
9660 i40e_dcbnl_setup(vsi);
9661#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009662 /* fall through */
9663
9664 case I40E_VSI_FDIR:
9665 /* set up vectors and rings if needed */
9666 ret = i40e_vsi_setup_vectors(vsi);
9667 if (ret)
9668 goto err_msix;
9669
9670 ret = i40e_alloc_rings(vsi);
9671 if (ret)
9672 goto err_rings;
9673
9674 /* map all of the rings to the q_vectors */
9675 i40e_vsi_map_rings_to_vectors(vsi);
9676
9677 i40e_vsi_reset_stats(vsi);
9678 break;
9679
9680 default:
9681 /* no netdev or rings for the other VSI types */
9682 break;
9683 }
9684
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009685 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9686 (vsi->type == I40E_VSI_VMDQ2)) {
9687 ret = i40e_vsi_config_rss(vsi);
9688 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009689 return vsi;
9690
9691err_rings:
9692 i40e_vsi_free_q_vectors(vsi);
9693err_msix:
9694 if (vsi->netdev_registered) {
9695 vsi->netdev_registered = false;
9696 unregister_netdev(vsi->netdev);
9697 free_netdev(vsi->netdev);
9698 vsi->netdev = NULL;
9699 }
9700err_netdev:
9701 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9702err_vsi:
9703 i40e_vsi_clear(vsi);
9704err_alloc:
9705 return NULL;
9706}
9707
9708/**
9709 * i40e_veb_get_bw_info - Query VEB BW information
9710 * @veb: the veb to query
9711 *
9712 * Query the Tx scheduler BW configuration data for given VEB
9713 **/
9714static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9715{
9716 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9717 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9718 struct i40e_pf *pf = veb->pf;
9719 struct i40e_hw *hw = &pf->hw;
9720 u32 tc_bw_max;
9721 int ret = 0;
9722 int i;
9723
9724 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9725 &bw_data, NULL);
9726 if (ret) {
9727 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009728 "query veb bw config failed, err %s aq_err %s\n",
9729 i40e_stat_str(&pf->hw, ret),
9730 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009731 goto out;
9732 }
9733
9734 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9735 &ets_data, NULL);
9736 if (ret) {
9737 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009738 "query veb bw ets config failed, err %s aq_err %s\n",
9739 i40e_stat_str(&pf->hw, ret),
9740 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009741 goto out;
9742 }
9743
9744 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9745 veb->bw_max_quanta = ets_data.tc_bw_max;
9746 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009747 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009748 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9749 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9750 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9751 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9752 veb->bw_tc_limit_credits[i] =
9753 le16_to_cpu(bw_data.tc_bw_limits[i]);
9754 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9755 }
9756
9757out:
9758 return ret;
9759}
9760
9761/**
9762 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9763 * @pf: board private structure
9764 *
9765 * On error: returns error code (negative)
9766 * On success: returns vsi index in PF (positive)
9767 **/
9768static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9769{
9770 int ret = -ENOENT;
9771 struct i40e_veb *veb;
9772 int i;
9773
9774 /* Need to protect the allocation of switch elements at the PF level */
9775 mutex_lock(&pf->switch_mutex);
9776
9777 /* VEB list may be fragmented if VEB creation/destruction has
9778 * been happening. We can afford to do a quick scan to look
9779 * for any free slots in the list.
9780 *
9781 * find next empty veb slot, looping back around if necessary
9782 */
9783 i = 0;
9784 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9785 i++;
9786 if (i >= I40E_MAX_VEB) {
9787 ret = -ENOMEM;
9788 goto err_alloc_veb; /* out of VEB slots! */
9789 }
9790
9791 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9792 if (!veb) {
9793 ret = -ENOMEM;
9794 goto err_alloc_veb;
9795 }
9796 veb->pf = pf;
9797 veb->idx = i;
9798 veb->enabled_tc = 1;
9799
9800 pf->veb[i] = veb;
9801 ret = i;
9802err_alloc_veb:
9803 mutex_unlock(&pf->switch_mutex);
9804 return ret;
9805}
9806
9807/**
9808 * i40e_switch_branch_release - Delete a branch of the switch tree
9809 * @branch: where to start deleting
9810 *
9811 * This uses recursion to find the tips of the branch to be
9812 * removed, deleting until we get back to and can delete this VEB.
9813 **/
9814static void i40e_switch_branch_release(struct i40e_veb *branch)
9815{
9816 struct i40e_pf *pf = branch->pf;
9817 u16 branch_seid = branch->seid;
9818 u16 veb_idx = branch->idx;
9819 int i;
9820
9821 /* release any VEBs on this VEB - RECURSION */
9822 for (i = 0; i < I40E_MAX_VEB; i++) {
9823 if (!pf->veb[i])
9824 continue;
9825 if (pf->veb[i]->uplink_seid == branch->seid)
9826 i40e_switch_branch_release(pf->veb[i]);
9827 }
9828
9829 /* Release the VSIs on this VEB, but not the owner VSI.
9830 *
9831 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
9832 * the VEB itself, so don't use (*branch) after this loop.
9833 */
Mitch Williams505682c2014-05-20 08:01:37 +00009834 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009835 if (!pf->vsi[i])
9836 continue;
9837 if (pf->vsi[i]->uplink_seid == branch_seid &&
9838 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9839 i40e_vsi_release(pf->vsi[i]);
9840 }
9841 }
9842
9843 /* There's one corner case where the VEB might not have been
9844 * removed, so double check it here and remove it if needed.
9845 * This case happens if the veb was created from the debugfs
9846 * commands and no VSIs were added to it.
9847 */
9848 if (pf->veb[veb_idx])
9849 i40e_veb_release(pf->veb[veb_idx]);
9850}
9851
9852/**
9853 * i40e_veb_clear - remove veb struct
9854 * @veb: the veb to remove
9855 **/
9856static void i40e_veb_clear(struct i40e_veb *veb)
9857{
9858 if (!veb)
9859 return;
9860
9861 if (veb->pf) {
9862 struct i40e_pf *pf = veb->pf;
9863
9864 mutex_lock(&pf->switch_mutex);
9865 if (pf->veb[veb->idx] == veb)
9866 pf->veb[veb->idx] = NULL;
9867 mutex_unlock(&pf->switch_mutex);
9868 }
9869
9870 kfree(veb);
9871}
9872
9873/**
9874 * i40e_veb_release - Delete a VEB and free its resources
9875 * @veb: the VEB being removed
9876 **/
9877void i40e_veb_release(struct i40e_veb *veb)
9878{
9879 struct i40e_vsi *vsi = NULL;
9880 struct i40e_pf *pf;
9881 int i, n = 0;
9882
9883 pf = veb->pf;
9884
9885 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +00009886 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009887 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
9888 n++;
9889 vsi = pf->vsi[i];
9890 }
9891 }
9892 if (n != 1) {
9893 dev_info(&pf->pdev->dev,
9894 "can't remove VEB %d with %d VSIs left\n",
9895 veb->seid, n);
9896 return;
9897 }
9898
9899 /* move the remaining VSI to uplink veb */
9900 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
9901 if (veb->uplink_seid) {
9902 vsi->uplink_seid = veb->uplink_seid;
9903 if (veb->uplink_seid == pf->mac_seid)
9904 vsi->veb_idx = I40E_NO_VEB;
9905 else
9906 vsi->veb_idx = veb->veb_idx;
9907 } else {
9908 /* floating VEB */
9909 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9910 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
9911 }
9912
9913 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
9914 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009915}
9916
9917/**
9918 * i40e_add_veb - create the VEB in the switch
9919 * @veb: the VEB to be instantiated
9920 * @vsi: the controlling VSI
9921 **/
9922static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
9923{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009924 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04009925 bool is_default = veb->pf->cur_promisc;
Kevin Scotte1c51b952013-11-20 10:02:51 +00009926 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009927 int ret;
9928
9929 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009930 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +00009931 veb->enabled_tc, is_default,
9932 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009933 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009934 dev_info(&pf->pdev->dev,
9935 "couldn't add VEB, err %s aq_err %s\n",
9936 i40e_stat_str(&pf->hw, ret),
9937 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009938 return -EPERM;
9939 }
9940
9941 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009942 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009943 &veb->stats_idx, NULL, NULL, NULL);
9944 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009945 dev_info(&pf->pdev->dev,
9946 "couldn't get VEB statistics idx, err %s aq_err %s\n",
9947 i40e_stat_str(&pf->hw, ret),
9948 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009949 return -EPERM;
9950 }
9951 ret = i40e_veb_get_bw_info(veb);
9952 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009953 dev_info(&pf->pdev->dev,
9954 "couldn't get VEB bw info, err %s aq_err %s\n",
9955 i40e_stat_str(&pf->hw, ret),
9956 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9957 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009958 return -ENOENT;
9959 }
9960
9961 vsi->uplink_seid = veb->seid;
9962 vsi->veb_idx = veb->idx;
9963 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9964
9965 return 0;
9966}
9967
9968/**
9969 * i40e_veb_setup - Set up a VEB
9970 * @pf: board private structure
9971 * @flags: VEB setup flags
9972 * @uplink_seid: the switch element to link to
9973 * @vsi_seid: the initial VSI seid
9974 * @enabled_tc: Enabled TC bit-map
9975 *
9976 * This allocates the sw VEB structure and links it into the switch
9977 * It is possible and legal for this to be a duplicate of an already
9978 * existing VEB. It is also possible for both uplink and vsi seids
9979 * to be zero, in order to create a floating VEB.
9980 *
9981 * Returns pointer to the successfully allocated VEB sw struct on
9982 * success, otherwise returns NULL on failure.
9983 **/
9984struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
9985 u16 uplink_seid, u16 vsi_seid,
9986 u8 enabled_tc)
9987{
9988 struct i40e_veb *veb, *uplink_veb = NULL;
9989 int vsi_idx, veb_idx;
9990 int ret;
9991
9992 /* if one seid is 0, the other must be 0 to create a floating relay */
9993 if ((uplink_seid == 0 || vsi_seid == 0) &&
9994 (uplink_seid + vsi_seid != 0)) {
9995 dev_info(&pf->pdev->dev,
9996 "one, not both seid's are 0: uplink=%d vsi=%d\n",
9997 uplink_seid, vsi_seid);
9998 return NULL;
9999 }
10000
10001 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010002 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010003 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10004 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010005 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010006 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10007 vsi_seid);
10008 return NULL;
10009 }
10010
10011 if (uplink_seid && uplink_seid != pf->mac_seid) {
10012 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10013 if (pf->veb[veb_idx] &&
10014 pf->veb[veb_idx]->seid == uplink_seid) {
10015 uplink_veb = pf->veb[veb_idx];
10016 break;
10017 }
10018 }
10019 if (!uplink_veb) {
10020 dev_info(&pf->pdev->dev,
10021 "uplink seid %d not found\n", uplink_seid);
10022 return NULL;
10023 }
10024 }
10025
10026 /* get veb sw struct */
10027 veb_idx = i40e_veb_mem_alloc(pf);
10028 if (veb_idx < 0)
10029 goto err_alloc;
10030 veb = pf->veb[veb_idx];
10031 veb->flags = flags;
10032 veb->uplink_seid = uplink_seid;
10033 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10034 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10035
10036 /* create the VEB in the switch */
10037 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10038 if (ret)
10039 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010040 if (vsi_idx == pf->lan_vsi)
10041 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010042
10043 return veb;
10044
10045err_veb:
10046 i40e_veb_clear(veb);
10047err_alloc:
10048 return NULL;
10049}
10050
10051/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010052 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010053 * @pf: board private structure
10054 * @ele: element we are building info from
10055 * @num_reported: total number of elements
10056 * @printconfig: should we print the contents
10057 *
10058 * helper function to assist in extracting a few useful SEID values.
10059 **/
10060static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10061 struct i40e_aqc_switch_config_element_resp *ele,
10062 u16 num_reported, bool printconfig)
10063{
10064 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10065 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10066 u8 element_type = ele->element_type;
10067 u16 seid = le16_to_cpu(ele->seid);
10068
10069 if (printconfig)
10070 dev_info(&pf->pdev->dev,
10071 "type=%d seid=%d uplink=%d downlink=%d\n",
10072 element_type, seid, uplink_seid, downlink_seid);
10073
10074 switch (element_type) {
10075 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10076 pf->mac_seid = seid;
10077 break;
10078 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10079 /* Main VEB? */
10080 if (uplink_seid != pf->mac_seid)
10081 break;
10082 if (pf->lan_veb == I40E_NO_VEB) {
10083 int v;
10084
10085 /* find existing or else empty VEB */
10086 for (v = 0; v < I40E_MAX_VEB; v++) {
10087 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10088 pf->lan_veb = v;
10089 break;
10090 }
10091 }
10092 if (pf->lan_veb == I40E_NO_VEB) {
10093 v = i40e_veb_mem_alloc(pf);
10094 if (v < 0)
10095 break;
10096 pf->lan_veb = v;
10097 }
10098 }
10099
10100 pf->veb[pf->lan_veb]->seid = seid;
10101 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10102 pf->veb[pf->lan_veb]->pf = pf;
10103 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10104 break;
10105 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10106 if (num_reported != 1)
10107 break;
10108 /* This is immediately after a reset so we can assume this is
10109 * the PF's VSI
10110 */
10111 pf->mac_seid = uplink_seid;
10112 pf->pf_seid = downlink_seid;
10113 pf->main_vsi_seid = seid;
10114 if (printconfig)
10115 dev_info(&pf->pdev->dev,
10116 "pf_seid=%d main_vsi_seid=%d\n",
10117 pf->pf_seid, pf->main_vsi_seid);
10118 break;
10119 case I40E_SWITCH_ELEMENT_TYPE_PF:
10120 case I40E_SWITCH_ELEMENT_TYPE_VF:
10121 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10122 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10123 case I40E_SWITCH_ELEMENT_TYPE_PE:
10124 case I40E_SWITCH_ELEMENT_TYPE_PA:
10125 /* ignore these for now */
10126 break;
10127 default:
10128 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10129 element_type, seid);
10130 break;
10131 }
10132}
10133
10134/**
10135 * i40e_fetch_switch_configuration - Get switch config from firmware
10136 * @pf: board private structure
10137 * @printconfig: should we print the contents
10138 *
10139 * Get the current switch configuration from the device and
10140 * extract a few useful SEID values.
10141 **/
10142int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10143{
10144 struct i40e_aqc_get_switch_config_resp *sw_config;
10145 u16 next_seid = 0;
10146 int ret = 0;
10147 u8 *aq_buf;
10148 int i;
10149
10150 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10151 if (!aq_buf)
10152 return -ENOMEM;
10153
10154 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10155 do {
10156 u16 num_reported, num_total;
10157
10158 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10159 I40E_AQ_LARGE_BUF,
10160 &next_seid, NULL);
10161 if (ret) {
10162 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010163 "get switch config failed err %s aq_err %s\n",
10164 i40e_stat_str(&pf->hw, ret),
10165 i40e_aq_str(&pf->hw,
10166 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010167 kfree(aq_buf);
10168 return -ENOENT;
10169 }
10170
10171 num_reported = le16_to_cpu(sw_config->header.num_reported);
10172 num_total = le16_to_cpu(sw_config->header.num_total);
10173
10174 if (printconfig)
10175 dev_info(&pf->pdev->dev,
10176 "header: %d reported %d total\n",
10177 num_reported, num_total);
10178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010179 for (i = 0; i < num_reported; i++) {
10180 struct i40e_aqc_switch_config_element_resp *ele =
10181 &sw_config->element[i];
10182
10183 i40e_setup_pf_switch_element(pf, ele, num_reported,
10184 printconfig);
10185 }
10186 } while (next_seid != 0);
10187
10188 kfree(aq_buf);
10189 return ret;
10190}
10191
10192/**
10193 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10194 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010195 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010196 *
10197 * Returns 0 on success, negative value on failure
10198 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010199static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010200{
10201 int ret;
10202
10203 /* find out what's out there already */
10204 ret = i40e_fetch_switch_configuration(pf, false);
10205 if (ret) {
10206 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010207 "couldn't fetch switch config, err %s aq_err %s\n",
10208 i40e_stat_str(&pf->hw, ret),
10209 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010210 return ret;
10211 }
10212 i40e_pf_reset_stats(pf);
10213
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010214 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010215 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010216 struct i40e_vsi *vsi = NULL;
10217 u16 uplink_seid;
10218
10219 /* Set up the PF VSI associated with the PF's main VSI
10220 * that is already in the HW switch
10221 */
10222 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10223 uplink_seid = pf->veb[pf->lan_veb]->seid;
10224 else
10225 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010226 if (pf->lan_vsi == I40E_NO_VSI)
10227 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10228 else if (reinit)
10229 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010230 if (!vsi) {
10231 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10232 i40e_fdir_teardown(pf);
10233 return -EAGAIN;
10234 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010235 } else {
10236 /* force a reset of TC and queue layout configurations */
10237 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010239 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10240 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10241 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10242 }
10243 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10244
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010245 i40e_fdir_sb_setup(pf);
10246
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010247 /* Setup static PF queue filter control settings */
10248 ret = i40e_setup_pf_filter_control(pf);
10249 if (ret) {
10250 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10251 ret);
10252 /* Failure here should not stop continuing other steps */
10253 }
10254
10255 /* enable RSS in the HW, even for only one queue, as the stack can use
10256 * the hash
10257 */
10258 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010259 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010260
10261 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010262 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010263 i40e_link_event(pf);
10264
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010265 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010266 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10267 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010268
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010269 i40e_ptp_init(pf);
10270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010271 return ret;
10272}
10273
10274/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010275 * i40e_determine_queue_usage - Work out queue distribution
10276 * @pf: board private structure
10277 **/
10278static void i40e_determine_queue_usage(struct i40e_pf *pf)
10279{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010280 int queues_left;
10281
10282 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010283#ifdef I40E_FCOE
10284 pf->num_fcoe_qps = 0;
10285#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010286
10287 /* Find the max queues to be put into basic use. We'll always be
10288 * using TC0, whether or not DCB is running, and TC0 will get the
10289 * big RSS set.
10290 */
10291 queues_left = pf->hw.func_caps.num_tx_qp;
10292
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010293 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010294 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010295 /* one qp for PF, no queues for anything else */
10296 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010297 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010298
10299 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010300 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010301#ifdef I40E_FCOE
10302 I40E_FLAG_FCOE_ENABLED |
10303#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010304 I40E_FLAG_FD_SB_ENABLED |
10305 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010306 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010307 I40E_FLAG_SRIOV_ENABLED |
10308 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010309 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10310 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010311 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010312 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010313 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010314 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010315 queues_left -= pf->num_lan_qps;
10316
10317 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010318#ifdef I40E_FCOE
10319 I40E_FLAG_FCOE_ENABLED |
10320#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010321 I40E_FLAG_FD_SB_ENABLED |
10322 I40E_FLAG_FD_ATR_ENABLED |
10323 I40E_FLAG_DCB_ENABLED |
10324 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010325 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010326 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010327 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010328 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010329 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010330 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10331 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010332 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10333 num_online_cpus());
10334 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10335 pf->hw.func_caps.num_tx_qp);
10336
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010337 queues_left -= pf->num_lan_qps;
10338 }
10339
Vasu Dev38e00432014-08-01 13:27:03 -070010340#ifdef I40E_FCOE
10341 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10342 if (I40E_DEFAULT_FCOE <= queues_left) {
10343 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10344 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10345 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10346 } else {
10347 pf->num_fcoe_qps = 0;
10348 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10349 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10350 }
10351
10352 queues_left -= pf->num_fcoe_qps;
10353 }
10354
10355#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010356 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10357 if (queues_left > 1) {
10358 queues_left -= 1; /* save 1 queue for FD */
10359 } else {
10360 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10361 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10362 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010363 }
10364
10365 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10366 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010367 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10368 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010369 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10370 }
10371
10372 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10373 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10374 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10375 (queues_left / pf->num_vmdq_qps));
10376 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10377 }
10378
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010379 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010380 dev_dbg(&pf->pdev->dev,
10381 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10382 pf->hw.func_caps.num_tx_qp,
10383 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010384 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10385 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10386 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010387#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010388 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010389#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010390}
10391
10392/**
10393 * i40e_setup_pf_filter_control - Setup PF static filter control
10394 * @pf: PF to be setup
10395 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010396 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010397 * settings. If PE/FCoE are enabled then it will also set the per PF
10398 * based filter sizes required for them. It also enables Flow director,
10399 * ethertype and macvlan type filter settings for the pf.
10400 *
10401 * Returns 0 on success, negative on failure
10402 **/
10403static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10404{
10405 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10406
10407 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10408
10409 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010410 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010411 settings->enable_fdir = true;
10412
10413 /* Ethtype and MACVLAN filters enabled for PF */
10414 settings->enable_ethtype = true;
10415 settings->enable_macvlan = true;
10416
10417 if (i40e_set_filter_control(&pf->hw, settings))
10418 return -ENOENT;
10419
10420 return 0;
10421}
10422
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010423#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010424#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010425static void i40e_print_features(struct i40e_pf *pf)
10426{
10427 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010428 char *buf;
10429 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010430
Joe Perches3b195842015-12-03 04:20:57 -080010431 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10432 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010433 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010434
Joe Perches3b195842015-12-03 04:20:57 -080010435 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010436#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010437 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010438#endif
Joe Perches3b195842015-12-03 04:20:57 -080010439 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d RX: %s",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010440 pf->hw.func_caps.num_vsis,
10441 pf->vsi[pf->lan_vsi]->num_queue_pairs,
10442 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010443
10444 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010445 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010446 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010447 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010448 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010449 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10450 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010451 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010452 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010453 i += snprintf(&buf[i], REMAIN(i), " DCB");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010454#if IS_ENABLED(CONFIG_VXLAN)
Joe Perches3b195842015-12-03 04:20:57 -080010455 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010456#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080010457#if IS_ENABLED(CONFIG_GENEVE)
10458 i += snprintf(&buf[i], REMAIN(i), " Geneve");
10459#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010460 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010461 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010462#ifdef I40E_FCOE
10463 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010464 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010465#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010466 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010467 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010468 else
Joe Perches3b195842015-12-03 04:20:57 -080010469 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010470
Joe Perches3b195842015-12-03 04:20:57 -080010471 dev_info(&pf->pdev->dev, "%s\n", buf);
10472 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010473 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010474}
10475
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010476/**
10477 * i40e_probe - Device initialization routine
10478 * @pdev: PCI device information struct
10479 * @ent: entry in i40e_pci_tbl
10480 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010481 * i40e_probe initializes a PF identified by a pci_dev structure.
10482 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010483 * and a hardware reset occur.
10484 *
10485 * Returns 0 on success, negative on failure
10486 **/
10487static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10488{
Catherine Sullivane8278452015-02-06 08:52:08 +000010489 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010490 struct i40e_pf *pf;
10491 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010492 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010493 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010494 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010495 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010496 u32 len;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010497 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010498 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010499 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010500
10501 err = pci_enable_device_mem(pdev);
10502 if (err)
10503 return err;
10504
10505 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010506 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010507 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010508 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10509 if (err) {
10510 dev_err(&pdev->dev,
10511 "DMA configuration failed: 0x%x\n", err);
10512 goto err_dma;
10513 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010514 }
10515
10516 /* set up pci connections */
10517 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
10518 IORESOURCE_MEM), i40e_driver_name);
10519 if (err) {
10520 dev_info(&pdev->dev,
10521 "pci_request_selected_regions failed %d\n", err);
10522 goto err_pci_reg;
10523 }
10524
10525 pci_enable_pcie_error_reporting(pdev);
10526 pci_set_master(pdev);
10527
10528 /* Now that we have a PCI connection, we need to do the
10529 * low level device setup. This is primarily setting up
10530 * the Admin Queue structures and then querying for the
10531 * device's current profile information.
10532 */
10533 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10534 if (!pf) {
10535 err = -ENOMEM;
10536 goto err_pf_alloc;
10537 }
10538 pf->next_vsi = 0;
10539 pf->pdev = pdev;
10540 set_bit(__I40E_DOWN, &pf->state);
10541
10542 hw = &pf->hw;
10543 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010544
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010545 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10546 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010547
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010548 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010549 if (!hw->hw_addr) {
10550 err = -EIO;
10551 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10552 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010553 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010554 goto err_ioremap;
10555 }
10556 hw->vendor_id = pdev->vendor;
10557 hw->device_id = pdev->device;
10558 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10559 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10560 hw->subsystem_device_id = pdev->subsystem_device;
10561 hw->bus.device = PCI_SLOT(pdev->devfn);
10562 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010563 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010564
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010565 if (debug != -1) {
10566 pf->msg_enable = pf->hw.debug_mask;
10567 pf->msg_enable = debug;
10568 }
10569
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010570 /* do a special CORER for clearing PXE mode once at init */
10571 if (hw->revision_id == 0 &&
10572 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10573 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10574 i40e_flush(hw);
10575 msleep(200);
10576 pf->corer_count++;
10577
10578 i40e_clear_pxe_mode(hw);
10579 }
10580
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010581 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010582 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010583 err = i40e_pf_reset(hw);
10584 if (err) {
10585 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10586 goto err_pf_reset;
10587 }
10588 pf->pfr_count++;
10589
10590 hw->aq.num_arq_entries = I40E_AQ_LEN;
10591 hw->aq.num_asq_entries = I40E_AQ_LEN;
10592 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10593 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10594 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010595
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010596 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010597 "%s-%s:misc",
10598 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010599
10600 err = i40e_init_shared_code(hw);
10601 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010602 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10603 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010604 goto err_pf_reset;
10605 }
10606
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010607 /* set up a default setting for link flow control */
10608 pf->hw.fc.requested_mode = I40E_FC_NONE;
10609
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080010610 /* set up the locks for the AQ, do this only once in probe
10611 * and destroy them only once in remove
10612 */
10613 mutex_init(&hw->aq.asq_mutex);
10614 mutex_init(&hw->aq.arq_mutex);
10615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010616 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010617 if (err) {
10618 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10619 dev_info(&pdev->dev,
10620 "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");
10621 else
10622 dev_info(&pdev->dev,
10623 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10624
10625 goto err_pf_reset;
10626 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010627
Shannon Nelson6dec1012015-09-28 14:12:30 -040010628 /* provide nvm, fw, api versions */
10629 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10630 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10631 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10632 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010633
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010634 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10635 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010636 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010637 "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");
10638 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10639 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010640 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010641 "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 +000010642
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010643 i40e_verify_eeprom(pf);
10644
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010645 /* Rev 0 hardware was never productized */
10646 if (hw->revision_id < 1)
10647 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");
10648
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010649 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010650 err = i40e_get_capabilities(pf);
10651 if (err)
10652 goto err_adminq_setup;
10653
10654 err = i40e_sw_init(pf);
10655 if (err) {
10656 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10657 goto err_sw_init;
10658 }
10659
10660 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10661 hw->func_caps.num_rx_qp,
10662 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10663 if (err) {
10664 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10665 goto err_init_lan_hmc;
10666 }
10667
10668 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10669 if (err) {
10670 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10671 err = -ENOENT;
10672 goto err_configure_lan_hmc;
10673 }
10674
Neerav Parikhb686ece2014-12-14 01:55:11 +000010675 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10676 * Ignore error return codes because if it was already disabled via
10677 * hardware settings this will fail
10678 */
10679 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10680 (pf->hw.aq.fw_maj_ver < 4)) {
10681 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10682 i40e_aq_stop_lldp(hw, true, NULL);
10683 }
10684
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010685 i40e_get_mac_addr(hw, hw->mac.addr);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010686 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010687 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10688 err = -EIO;
10689 goto err_mac_addr;
10690 }
10691 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010692 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010693 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10694 if (is_valid_ether_addr(hw->mac.port_addr))
10695 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010696#ifdef I40E_FCOE
10697 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10698 if (err)
10699 dev_info(&pdev->dev,
10700 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10701 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10702 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10703 hw->mac.san_addr);
10704 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10705 }
10706 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10707#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010708
10709 pci_set_drvdata(pdev, pf);
10710 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010711#ifdef CONFIG_I40E_DCB
10712 err = i40e_init_pf_dcb(pf);
10713 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010714 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010715 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010716 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010717 }
10718#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719
10720 /* set up periodic task facility */
10721 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10722 pf->service_timer_period = HZ;
10723
10724 INIT_WORK(&pf->service_task, i40e_service_task);
10725 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10726 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010727
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010728 /* NVM bit on means WoL disabled for the port */
10729 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010730 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010731 pf->wol_en = false;
10732 else
10733 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010734 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10735
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010736 /* set up the main switch operations */
10737 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010738 err = i40e_init_interrupt_scheme(pf);
10739 if (err)
10740 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010741
Mitch Williams505682c2014-05-20 08:01:37 +000010742 /* The number of VSIs reported by the FW is the minimum guaranteed
10743 * to us; HW supports far more and we share the remaining pool with
10744 * the other PFs. We allocate space for more than the guarantee with
10745 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010746 */
Mitch Williams505682c2014-05-20 08:01:37 +000010747 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10748 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10749 else
10750 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10751
10752 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
10753 len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010754 pf->vsi = kzalloc(len, GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010755 if (!pf->vsi) {
10756 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010757 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010758 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010759
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010760#ifdef CONFIG_PCI_IOV
10761 /* prep for VF support */
10762 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10763 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10764 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10765 if (pci_num_vf(pdev))
10766 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10767 }
10768#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010769 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010770 if (err) {
10771 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10772 goto err_vsis;
10773 }
Helin Zhang58fc3262015-10-01 14:37:38 -040010774
10775 /* Make sure flow control is set according to current settings */
10776 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
10777 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
10778 dev_dbg(&pf->pdev->dev,
10779 "Set fc with err %s aq_err %s on get_phy_cap\n",
10780 i40e_stat_str(hw, err),
10781 i40e_aq_str(hw, hw->aq.asq_last_status));
10782 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
10783 dev_dbg(&pf->pdev->dev,
10784 "Set fc with err %s aq_err %s on set_phy_config\n",
10785 i40e_stat_str(hw, err),
10786 i40e_aq_str(hw, hw->aq.asq_last_status));
10787 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
10788 dev_dbg(&pf->pdev->dev,
10789 "Set fc with err %s aq_err %s on get_link_info\n",
10790 i40e_stat_str(hw, err),
10791 i40e_aq_str(hw, hw->aq.asq_last_status));
10792
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010793 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000010794 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010795 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
10796 i40e_vsi_open(pf->vsi[i]);
10797 break;
10798 }
10799 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010800
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010801 /* driver is only interested in link up/down and module qualification
10802 * reports from firmware
10803 */
10804 err = i40e_aq_set_phy_int_mask(&pf->hw,
10805 I40E_AQ_EVENT_LINK_UPDOWN |
10806 I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
10807 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010808 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10809 i40e_stat_str(&pf->hw, err),
10810 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010811
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010812 /* Reconfigure hardware for allowing smaller MSS in the case
10813 * of TSO, so that we avoid the MDD being fired and causing
10814 * a reset in the case of small MSS+TSO.
10815 */
10816 val = rd32(hw, I40E_REG_MSS);
10817 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10818 val &= ~I40E_REG_MSS_MIN_MASK;
10819 val |= I40E_64BYTE_MSS;
10820 wr32(hw, I40E_REG_MSS, val);
10821 }
10822
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000010823 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
10824 (pf->hw.aq.fw_maj_ver < 4)) {
10825 msleep(75);
10826 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10827 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010828 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10829 i40e_stat_str(&pf->hw, err),
10830 i40e_aq_str(&pf->hw,
10831 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000010832 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010833 /* The main driver is (mostly) up and happy. We need to set this state
10834 * before setting up the misc vector or we get a race and the vector
10835 * ends up disabled forever.
10836 */
10837 clear_bit(__I40E_DOWN, &pf->state);
10838
10839 /* In case of MSIX we are going to setup the misc vector right here
10840 * to handle admin queue events etc. In case of legacy and MSI
10841 * the misc functionality and queue processing is combined in
10842 * the same vector and that gets setup at open.
10843 */
10844 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10845 err = i40e_setup_misc_vector(pf);
10846 if (err) {
10847 dev_info(&pdev->dev,
10848 "setup of misc vector failed: %d\n", err);
10849 goto err_vsis;
10850 }
10851 }
10852
Greg Rosedf805f62014-04-04 04:43:16 +000010853#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010854 /* prep for VF support */
10855 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010856 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10857 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010858 u32 val;
10859
10860 /* disable link interrupts for VFs */
10861 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
10862 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
10863 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
10864 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080010865
10866 if (pci_num_vf(pdev)) {
10867 dev_info(&pdev->dev,
10868 "Active VFs found, allocating resources.\n");
10869 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
10870 if (err)
10871 dev_info(&pdev->dev,
10872 "Error %d allocating resources for existing VFs\n",
10873 err);
10874 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875 }
Greg Rosedf805f62014-04-04 04:43:16 +000010876#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010877
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010878 pfs_found++;
10879
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010880 i40e_dbg_pf_init(pf);
10881
10882 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000010883 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010884
10885 /* since everything's happy, start the service_task timer */
10886 mod_timer(&pf->service_timer,
10887 round_jiffies(jiffies + pf->service_timer_period));
10888
Vasu Dev38e00432014-08-01 13:27:03 -070010889#ifdef I40E_FCOE
10890 /* create FCoE interface */
10891 i40e_fcoe_vsi_setup(pf);
10892
10893#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010894#define PCI_SPEED_SIZE 8
10895#define PCI_WIDTH_SIZE 8
10896 /* Devices on the IOSF bus do not have this information
10897 * and will report PCI Gen 1 x 1 by default so don't bother
10898 * checking them.
10899 */
10900 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
10901 char speed[PCI_SPEED_SIZE] = "Unknown";
10902 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010903
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010904 /* Get the negotiated link width and speed from PCI config
10905 * space
10906 */
10907 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
10908 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010909
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010910 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010911
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040010912 switch (hw->bus.speed) {
10913 case i40e_bus_speed_8000:
10914 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
10915 case i40e_bus_speed_5000:
10916 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
10917 case i40e_bus_speed_2500:
10918 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
10919 default:
10920 break;
10921 }
10922 switch (hw->bus.width) {
10923 case i40e_bus_width_pcie_x8:
10924 strncpy(width, "8", PCI_WIDTH_SIZE); break;
10925 case i40e_bus_width_pcie_x4:
10926 strncpy(width, "4", PCI_WIDTH_SIZE); break;
10927 case i40e_bus_width_pcie_x2:
10928 strncpy(width, "2", PCI_WIDTH_SIZE); break;
10929 case i40e_bus_width_pcie_x1:
10930 strncpy(width, "1", PCI_WIDTH_SIZE); break;
10931 default:
10932 break;
10933 }
10934
10935 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
10936 speed, width);
10937
10938 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
10939 hw->bus.speed < i40e_bus_speed_8000) {
10940 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
10941 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
10942 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010943 }
10944
Catherine Sullivane8278452015-02-06 08:52:08 +000010945 /* get the requested speeds from the fw */
10946 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
10947 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040010948 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
10949 i40e_stat_str(&pf->hw, err),
10950 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000010951 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
10952
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040010953 /* get the supported phy types from the fw */
10954 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
10955 if (err)
10956 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
10957 i40e_stat_str(&pf->hw, err),
10958 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10959 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
10960
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040010961 /* Add a filter to drop all Flow control frames from any VSI from being
10962 * transmitted. By doing so we stop a malicious VF from sending out
10963 * PAUSE or PFC frames and potentially controlling traffic for other
10964 * PF/VF VSIs.
10965 * The FW can still send Flow control frames if enabled.
10966 */
10967 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10968 pf->main_vsi_seid);
10969
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010970 /* print a string summarizing features */
10971 i40e_print_features(pf);
10972
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010973 return 0;
10974
10975 /* Unwind what we've done if something failed in the setup */
10976err_vsis:
10977 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010978 i40e_clear_interrupt_scheme(pf);
10979 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000010980err_switch_setup:
10981 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010982 del_timer_sync(&pf->service_timer);
10983err_mac_addr:
10984err_configure_lan_hmc:
10985 (void)i40e_shutdown_lan_hmc(hw);
10986err_init_lan_hmc:
10987 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010988err_sw_init:
10989err_adminq_setup:
10990 (void)i40e_shutdown_adminq(hw);
10991err_pf_reset:
10992 iounmap(hw->hw_addr);
10993err_ioremap:
10994 kfree(pf);
10995err_pf_alloc:
10996 pci_disable_pcie_error_reporting(pdev);
10997 pci_release_selected_regions(pdev,
10998 pci_select_bars(pdev, IORESOURCE_MEM));
10999err_pci_reg:
11000err_dma:
11001 pci_disable_device(pdev);
11002 return err;
11003}
11004
11005/**
11006 * i40e_remove - Device removal routine
11007 * @pdev: PCI device information struct
11008 *
11009 * i40e_remove is called by the PCI subsystem to alert the driver
11010 * that is should release a PCI device. This could be caused by a
11011 * Hot-Plug event, or because the driver is going to be removed from
11012 * memory.
11013 **/
11014static void i40e_remove(struct pci_dev *pdev)
11015{
11016 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011017 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011018 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011019 int i;
11020
11021 i40e_dbg_pf_exit(pf);
11022
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011023 i40e_ptp_stop(pf);
11024
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011025 /* Disable RSS in hw */
11026 wr32(hw, I40E_PFQF_HENA(0), 0);
11027 wr32(hw, I40E_PFQF_HENA(1), 0);
11028
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011029 /* no more scheduling of any task */
11030 set_bit(__I40E_DOWN, &pf->state);
11031 del_timer_sync(&pf->service_timer);
11032 cancel_work_sync(&pf->service_task);
11033
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011034 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11035 i40e_free_vfs(pf);
11036 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11037 }
11038
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011039 i40e_fdir_teardown(pf);
11040
11041 /* If there is a switch structure or any orphans, remove them.
11042 * This will leave only the PF's VSI remaining.
11043 */
11044 for (i = 0; i < I40E_MAX_VEB; i++) {
11045 if (!pf->veb[i])
11046 continue;
11047
11048 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11049 pf->veb[i]->uplink_seid == 0)
11050 i40e_switch_branch_release(pf->veb[i]);
11051 }
11052
11053 /* Now we can shutdown the PF's VSI, just before we kill
11054 * adminq and hmc.
11055 */
11056 if (pf->vsi[pf->lan_vsi])
11057 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11058
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011059 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000011060 if (pf->hw.hmc.hmc_obj) {
11061 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
11062 if (ret_code)
11063 dev_warn(&pdev->dev,
11064 "Failed to destroy the HMC resources: %d\n",
11065 ret_code);
11066 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011067
11068 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011069 ret_code = i40e_shutdown_adminq(&pf->hw);
11070 if (ret_code)
11071 dev_warn(&pdev->dev,
11072 "Failed to destroy the Admin Queue resources: %d\n",
11073 ret_code);
11074
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011075 /* destroy the locks only once, here */
11076 mutex_destroy(&hw->aq.arq_mutex);
11077 mutex_destroy(&hw->aq.asq_mutex);
11078
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011079 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11080 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011081 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011082 if (pf->vsi[i]) {
11083 i40e_vsi_clear_rings(pf->vsi[i]);
11084 i40e_vsi_clear(pf->vsi[i]);
11085 pf->vsi[i] = NULL;
11086 }
11087 }
11088
11089 for (i = 0; i < I40E_MAX_VEB; i++) {
11090 kfree(pf->veb[i]);
11091 pf->veb[i] = NULL;
11092 }
11093
11094 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011095 kfree(pf->vsi);
11096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011097 iounmap(pf->hw.hw_addr);
11098 kfree(pf);
11099 pci_release_selected_regions(pdev,
11100 pci_select_bars(pdev, IORESOURCE_MEM));
11101
11102 pci_disable_pcie_error_reporting(pdev);
11103 pci_disable_device(pdev);
11104}
11105
11106/**
11107 * i40e_pci_error_detected - warning that something funky happened in PCI land
11108 * @pdev: PCI device information struct
11109 *
11110 * Called to warn that something happened and the error handling steps
11111 * are in progress. Allows the driver to quiesce things, be ready for
11112 * remediation.
11113 **/
11114static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11115 enum pci_channel_state error)
11116{
11117 struct i40e_pf *pf = pci_get_drvdata(pdev);
11118
11119 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11120
11121 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011122 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11123 rtnl_lock();
11124 i40e_prep_for_reset(pf);
11125 rtnl_unlock();
11126 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011127
11128 /* Request a slot reset */
11129 return PCI_ERS_RESULT_NEED_RESET;
11130}
11131
11132/**
11133 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11134 * @pdev: PCI device information struct
11135 *
11136 * Called to find if the driver can work with the device now that
11137 * the pci slot has been reset. If a basic connection seems good
11138 * (registers are readable and have sane content) then return a
11139 * happy little PCI_ERS_RESULT_xxx.
11140 **/
11141static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11142{
11143 struct i40e_pf *pf = pci_get_drvdata(pdev);
11144 pci_ers_result_t result;
11145 int err;
11146 u32 reg;
11147
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011148 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011149 if (pci_enable_device_mem(pdev)) {
11150 dev_info(&pdev->dev,
11151 "Cannot re-enable PCI device after reset.\n");
11152 result = PCI_ERS_RESULT_DISCONNECT;
11153 } else {
11154 pci_set_master(pdev);
11155 pci_restore_state(pdev);
11156 pci_save_state(pdev);
11157 pci_wake_from_d3(pdev, false);
11158
11159 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11160 if (reg == 0)
11161 result = PCI_ERS_RESULT_RECOVERED;
11162 else
11163 result = PCI_ERS_RESULT_DISCONNECT;
11164 }
11165
11166 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11167 if (err) {
11168 dev_info(&pdev->dev,
11169 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11170 err);
11171 /* non-fatal, continue */
11172 }
11173
11174 return result;
11175}
11176
11177/**
11178 * i40e_pci_error_resume - restart operations after PCI error recovery
11179 * @pdev: PCI device information struct
11180 *
11181 * Called to allow the driver to bring things back up after PCI error
11182 * and/or reset recovery has finished.
11183 **/
11184static void i40e_pci_error_resume(struct pci_dev *pdev)
11185{
11186 struct i40e_pf *pf = pci_get_drvdata(pdev);
11187
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011188 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011189 if (test_bit(__I40E_SUSPENDED, &pf->state))
11190 return;
11191
11192 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011193 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011194 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011195}
11196
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011197/**
11198 * i40e_shutdown - PCI callback for shutting down
11199 * @pdev: PCI device information struct
11200 **/
11201static void i40e_shutdown(struct pci_dev *pdev)
11202{
11203 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011204 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011205
11206 set_bit(__I40E_SUSPENDED, &pf->state);
11207 set_bit(__I40E_DOWN, &pf->state);
11208 rtnl_lock();
11209 i40e_prep_for_reset(pf);
11210 rtnl_unlock();
11211
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011212 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11213 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11214
Catherine Sullivan02b42492015-07-10 19:35:59 -040011215 del_timer_sync(&pf->service_timer);
11216 cancel_work_sync(&pf->service_task);
11217 i40e_fdir_teardown(pf);
11218
11219 rtnl_lock();
11220 i40e_prep_for_reset(pf);
11221 rtnl_unlock();
11222
11223 wr32(hw, I40E_PFPM_APM,
11224 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11225 wr32(hw, I40E_PFPM_WUFC,
11226 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11227
Shannon Nelsone1477582015-02-21 06:44:33 +000011228 i40e_clear_interrupt_scheme(pf);
11229
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011230 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011231 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011232 pci_set_power_state(pdev, PCI_D3hot);
11233 }
11234}
11235
11236#ifdef CONFIG_PM
11237/**
11238 * i40e_suspend - PCI callback for moving to D3
11239 * @pdev: PCI device information struct
11240 **/
11241static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11242{
11243 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011244 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011245
11246 set_bit(__I40E_SUSPENDED, &pf->state);
11247 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011248
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011249 rtnl_lock();
11250 i40e_prep_for_reset(pf);
11251 rtnl_unlock();
11252
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011253 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11254 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11255
11256 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011257 pci_set_power_state(pdev, PCI_D3hot);
11258
11259 return 0;
11260}
11261
11262/**
11263 * i40e_resume - PCI callback for waking up from D3
11264 * @pdev: PCI device information struct
11265 **/
11266static int i40e_resume(struct pci_dev *pdev)
11267{
11268 struct i40e_pf *pf = pci_get_drvdata(pdev);
11269 u32 err;
11270
11271 pci_set_power_state(pdev, PCI_D0);
11272 pci_restore_state(pdev);
11273 /* pci_restore_state() clears dev->state_saves, so
11274 * call pci_save_state() again to restore it.
11275 */
11276 pci_save_state(pdev);
11277
11278 err = pci_enable_device_mem(pdev);
11279 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011280 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011281 return err;
11282 }
11283 pci_set_master(pdev);
11284
11285 /* no wakeup events while running */
11286 pci_wake_from_d3(pdev, false);
11287
11288 /* handling the reset will rebuild the device state */
11289 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11290 clear_bit(__I40E_DOWN, &pf->state);
11291 rtnl_lock();
11292 i40e_reset_and_rebuild(pf, false);
11293 rtnl_unlock();
11294 }
11295
11296 return 0;
11297}
11298
11299#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011300static const struct pci_error_handlers i40e_err_handler = {
11301 .error_detected = i40e_pci_error_detected,
11302 .slot_reset = i40e_pci_error_slot_reset,
11303 .resume = i40e_pci_error_resume,
11304};
11305
11306static struct pci_driver i40e_driver = {
11307 .name = i40e_driver_name,
11308 .id_table = i40e_pci_tbl,
11309 .probe = i40e_probe,
11310 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011311#ifdef CONFIG_PM
11312 .suspend = i40e_suspend,
11313 .resume = i40e_resume,
11314#endif
11315 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011316 .err_handler = &i40e_err_handler,
11317 .sriov_configure = i40e_pci_sriov_configure,
11318};
11319
11320/**
11321 * i40e_init_module - Driver registration routine
11322 *
11323 * i40e_init_module is the first routine called when the driver is
11324 * loaded. All it does is register with the PCI subsystem.
11325 **/
11326static int __init i40e_init_module(void)
11327{
11328 pr_info("%s: %s - version %s\n", i40e_driver_name,
11329 i40e_driver_string, i40e_driver_version_str);
11330 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011331
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011332 i40e_dbg_init();
11333 return pci_register_driver(&i40e_driver);
11334}
11335module_init(i40e_init_module);
11336
11337/**
11338 * i40e_exit_module - Driver exit cleanup routine
11339 *
11340 * i40e_exit_module is called just before the driver is removed
11341 * from memory.
11342 **/
11343static void __exit i40e_exit_module(void)
11344{
11345 pci_unregister_driver(&i40e_driver);
11346 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011347}
11348module_exit(i40e_exit_module);