blob: 04417e61b52378e103cf1d7c885017621b0aa229 [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
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Singhai, Anjali6a899022015-12-14 12:21:18 -080034#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000035#include <net/vxlan.h>
36#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080037#if IS_ENABLED(CONFIG_GENEVE)
38#include <net/geneve.h>
39#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000040
41const char i40e_driver_name[] = "i40e";
42static const char i40e_driver_string[] =
43 "Intel(R) Ethernet Connection XL710 Network Driver";
44
45#define DRV_KERN "-k"
46
Catherine Sullivane8e724d2014-07-10 07:58:26 +000047#define DRV_VERSION_MAJOR 1
Catherine Sullivan1c2df9e2015-10-21 19:56:24 -040048#define DRV_VERSION_MINOR 4
Jesse Brandeburg4580de02016-01-04 10:33:12 -080049#define DRV_VERSION_BUILD 12
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) DRV_KERN
53const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080054static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055
56/* a bit of forward declarations */
57static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
58static void i40e_handle_reset_warning(struct i40e_pf *pf);
59static int i40e_add_vsi(struct i40e_vsi *vsi);
60static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000061static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062static int i40e_setup_misc_vector(struct i40e_pf *pf);
63static void i40e_determine_queue_usage(struct i40e_pf *pf);
64static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Helin Zhange69ff812015-10-21 19:56:22 -040065static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
66 u16 rss_table_size, u16 rss_size);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080067static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080068static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000069
70/* i40e_pci_tbl - PCI Device ID Table
71 *
72 * Last entry must be all 0s
73 *
74 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
75 * Class, Class Mask, private data (not used) }
76 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020077static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040093 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000095 /* required last entry */
96 {0, }
97};
98MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
99
100#define I40E_MAX_VF_COUNT 128
101static int debug = -1;
102module_param(debug, int, 0);
103MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
104
105MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
106MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
107MODULE_LICENSE("GPL");
108MODULE_VERSION(DRV_VERSION);
109
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800110static struct workqueue_struct *i40e_wq;
111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000112/**
113 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
114 * @hw: pointer to the HW structure
115 * @mem: ptr to mem struct to fill out
116 * @size: size of memory requested
117 * @alignment: what to align the allocation to
118 **/
119int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
120 u64 size, u32 alignment)
121{
122 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
123
124 mem->size = ALIGN(size, alignment);
125 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
126 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000127 if (!mem->va)
128 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000129
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000130 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000131}
132
133/**
134 * i40e_free_dma_mem_d - OS specific memory free for shared code
135 * @hw: pointer to the HW structure
136 * @mem: ptr to mem struct to free
137 **/
138int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
139{
140 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
141
142 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
143 mem->va = NULL;
144 mem->pa = 0;
145 mem->size = 0;
146
147 return 0;
148}
149
150/**
151 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
152 * @hw: pointer to the HW structure
153 * @mem: ptr to mem struct to fill out
154 * @size: size of memory requested
155 **/
156int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
157 u32 size)
158{
159 mem->size = size;
160 mem->va = kzalloc(size, GFP_KERNEL);
161
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000162 if (!mem->va)
163 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000164
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000165 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000166}
167
168/**
169 * i40e_free_virt_mem_d - OS specific memory free for shared code
170 * @hw: pointer to the HW structure
171 * @mem: ptr to mem struct to free
172 **/
173int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
174{
175 /* it's ok to kfree a NULL pointer */
176 kfree(mem->va);
177 mem->va = NULL;
178 mem->size = 0;
179
180 return 0;
181}
182
183/**
184 * i40e_get_lump - find a lump of free generic resource
185 * @pf: board private structure
186 * @pile: the pile of resource to search
187 * @needed: the number of items needed
188 * @id: an owner id to stick on the items assigned
189 *
190 * Returns the base item index of the lump, or negative for error
191 *
192 * The search_hint trick and lack of advanced fit-finding only work
193 * because we're highly likely to have all the same size lump requests.
194 * Linear search time and any fragmentation should be minimal.
195 **/
196static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
197 u16 needed, u16 id)
198{
199 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000200 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000201
202 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
203 dev_info(&pf->pdev->dev,
204 "param err: pile=%p needed=%d id=0x%04x\n",
205 pile, needed, id);
206 return -EINVAL;
207 }
208
209 /* start the linear search with an imperfect hint */
210 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000211 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000212 /* skip already allocated entries */
213 if (pile->list[i] & I40E_PILE_VALID_BIT) {
214 i++;
215 continue;
216 }
217
218 /* do we have enough in this lump? */
219 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
220 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
221 break;
222 }
223
224 if (j == needed) {
225 /* there was enough, so assign it to the requestor */
226 for (j = 0; j < needed; j++)
227 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
228 ret = i;
229 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000230 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000231 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400232
233 /* not enough, so skip over it and continue looking */
234 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000235 }
236
237 return ret;
238}
239
240/**
241 * i40e_put_lump - return a lump of generic resource
242 * @pile: the pile of resource to search
243 * @index: the base item index
244 * @id: the owner id of the items assigned
245 *
246 * Returns the count of items in the lump
247 **/
248static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
249{
250 int valid_id = (id | I40E_PILE_VALID_BIT);
251 int count = 0;
252 int i;
253
254 if (!pile || index >= pile->num_entries)
255 return -EINVAL;
256
257 for (i = index;
258 i < pile->num_entries && pile->list[i] == valid_id;
259 i++) {
260 pile->list[i] = 0;
261 count++;
262 }
263
264 if (count && index < pile->search_hint)
265 pile->search_hint = index;
266
267 return count;
268}
269
270/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700271 * i40e_find_vsi_from_id - searches for the vsi with the given id
272 * @pf - the pf structure to search for the vsi
273 * @id - id of the vsi it is searching for
274 **/
275struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
276{
277 int i;
278
279 for (i = 0; i < pf->num_alloc_vsi; i++)
280 if (pf->vsi[i] && (pf->vsi[i]->id == id))
281 return pf->vsi[i];
282
283 return NULL;
284}
285
286/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000287 * i40e_service_event_schedule - Schedule the service task to wake up
288 * @pf: board private structure
289 *
290 * If not already scheduled, this puts the task into the work queue
291 **/
292static void i40e_service_event_schedule(struct i40e_pf *pf)
293{
294 if (!test_bit(__I40E_DOWN, &pf->state) &&
295 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
296 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800297 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000298}
299
300/**
301 * i40e_tx_timeout - Respond to a Tx Hang
302 * @netdev: network interface device structure
303 *
304 * If any port has noticed a Tx timeout, it is likely that the whole
305 * device is munged, not just the one netdev port, so go for the full
306 * reset.
307 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700308#ifdef I40E_FCOE
309void i40e_tx_timeout(struct net_device *netdev)
310#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000311static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700312#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000313{
314 struct i40e_netdev_priv *np = netdev_priv(netdev);
315 struct i40e_vsi *vsi = np->vsi;
316 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 struct i40e_ring *tx_ring = NULL;
318 unsigned int i, hung_queue = 0;
319 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000320
321 pf->tx_timeout_count++;
322
Kiran Patilb03a8c12015-09-24 18:13:15 -0400323 /* find the stopped queue the same way the stack does */
324 for (i = 0; i < netdev->num_tx_queues; i++) {
325 struct netdev_queue *q;
326 unsigned long trans_start;
327
328 q = netdev_get_tx_queue(netdev, i);
329 trans_start = q->trans_start ? : netdev->trans_start;
330 if (netif_xmit_stopped(q) &&
331 time_after(jiffies,
332 (trans_start + netdev->watchdog_timeo))) {
333 hung_queue = i;
334 break;
335 }
336 }
337
338 if (i == netdev->num_tx_queues) {
339 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
340 } else {
341 /* now that we have an index, find the tx_ring struct */
342 for (i = 0; i < vsi->num_queue_pairs; i++) {
343 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
344 if (hung_queue ==
345 vsi->tx_rings[i]->queue_index) {
346 tx_ring = vsi->tx_rings[i];
347 break;
348 }
349 }
350 }
351 }
352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000353 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400354 pf->tx_timeout_recovery_level = 1; /* reset after some time */
355 else if (time_before(jiffies,
356 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
357 return; /* don't do any new action before the next timeout */
358
359 if (tx_ring) {
360 head = i40e_get_head(tx_ring);
361 /* Read interrupt register */
362 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
363 val = rd32(&pf->hw,
364 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
365 tx_ring->vsi->base_vector - 1));
366 else
367 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
368
369 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",
370 vsi->seid, hung_queue, tx_ring->next_to_clean,
371 head, tx_ring->next_to_use,
372 readl(tx_ring->tail), val);
373 }
374
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000375 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400376 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
377 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000378
379 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000380 case 1:
381 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
382 break;
383 case 2:
384 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
385 break;
386 case 3:
387 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
388 break;
389 default:
390 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000391 break;
392 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400393
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000394 i40e_service_event_schedule(pf);
395 pf->tx_timeout_recovery_level++;
396}
397
398/**
399 * i40e_release_rx_desc - Store the new tail and head values
400 * @rx_ring: ring to bump
401 * @val: new head index
402 **/
403static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
404{
405 rx_ring->next_to_use = val;
406
407 /* Force memory writes to complete before letting h/w
408 * know there are new descriptors to fetch. (Only
409 * applicable for weak-ordered memory model archs,
410 * such as IA-64).
411 */
412 wmb();
413 writel(val, rx_ring->tail);
414}
415
416/**
417 * i40e_get_vsi_stats_struct - Get System Network Statistics
418 * @vsi: the VSI we care about
419 *
420 * Returns the address of the device statistics structure.
421 * The statistics are actually updated from the service task.
422 **/
423struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
424{
425 return &vsi->net_stats;
426}
427
428/**
429 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
430 * @netdev: network interface device structure
431 *
432 * Returns the address of the device statistics structure.
433 * The statistics are actually updated from the service task.
434 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700435#ifdef I40E_FCOE
436struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
437 struct net_device *netdev,
438 struct rtnl_link_stats64 *stats)
439#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000440static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
441 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000442 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700443#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000444{
445 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000446 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000447 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000448 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
449 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000450
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000451 if (test_bit(__I40E_DOWN, &vsi->state))
452 return stats;
453
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800454 if (!vsi->tx_rings)
455 return stats;
456
Alexander Duyck980e9b12013-09-28 06:01:03 +0000457 rcu_read_lock();
458 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000459 u64 bytes, packets;
460 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000461
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
463 if (!tx_ring)
464 continue;
465
466 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700467 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000468 packets = tx_ring->stats.packets;
469 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700470 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000471
472 stats->tx_packets += packets;
473 stats->tx_bytes += bytes;
474 rx_ring = &tx_ring[1];
475
476 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700477 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000478 packets = rx_ring->stats.packets;
479 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700480 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000481
482 stats->rx_packets += packets;
483 stats->rx_bytes += bytes;
484 }
485 rcu_read_unlock();
486
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000487 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000488 stats->multicast = vsi_stats->multicast;
489 stats->tx_errors = vsi_stats->tx_errors;
490 stats->tx_dropped = vsi_stats->tx_dropped;
491 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400492 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000493 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
494 stats->rx_length_errors = vsi_stats->rx_length_errors;
495
496 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000497}
498
499/**
500 * i40e_vsi_reset_stats - Resets all stats of the given vsi
501 * @vsi: the VSI to have its stats reset
502 **/
503void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
504{
505 struct rtnl_link_stats64 *ns;
506 int i;
507
508 if (!vsi)
509 return;
510
511 ns = i40e_get_vsi_stats_struct(vsi);
512 memset(ns, 0, sizeof(*ns));
513 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
514 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
515 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000516 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000517 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400518 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000519 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400520 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000521 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400522 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000523 sizeof(vsi->tx_rings[i]->stats));
524 memset(&vsi->tx_rings[i]->tx_stats, 0,
525 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000526 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000527 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000528 vsi->stat_offsets_loaded = false;
529}
530
531/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000532 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000533 * @pf: the PF to be reset
534 **/
535void i40e_pf_reset_stats(struct i40e_pf *pf)
536{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000537 int i;
538
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000539 memset(&pf->stats, 0, sizeof(pf->stats));
540 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
541 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000542
543 for (i = 0; i < I40E_MAX_VEB; i++) {
544 if (pf->veb[i]) {
545 memset(&pf->veb[i]->stats, 0,
546 sizeof(pf->veb[i]->stats));
547 memset(&pf->veb[i]->stats_offsets, 0,
548 sizeof(pf->veb[i]->stats_offsets));
549 pf->veb[i]->stat_offsets_loaded = false;
550 }
551 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000552}
553
554/**
555 * i40e_stat_update48 - read and update a 48 bit stat from the chip
556 * @hw: ptr to the hardware info
557 * @hireg: the high 32 bit reg to read
558 * @loreg: the low 32 bit reg to read
559 * @offset_loaded: has the initial offset been loaded yet
560 * @offset: ptr to current offset value
561 * @stat: ptr to the stat
562 *
563 * Since the device stats are not reset at PFReset, they likely will not
564 * be zeroed when the driver starts. We'll save the first values read
565 * and use them as offsets to be subtracted from the raw values in order
566 * to report stats that count from zero. In the process, we also manage
567 * the potential roll-over.
568 **/
569static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
570 bool offset_loaded, u64 *offset, u64 *stat)
571{
572 u64 new_data;
573
Shannon Nelsonab600852014-01-17 15:36:39 -0800574 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000575 new_data = rd32(hw, loreg);
576 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
577 } else {
578 new_data = rd64(hw, loreg);
579 }
580 if (!offset_loaded)
581 *offset = new_data;
582 if (likely(new_data >= *offset))
583 *stat = new_data - *offset;
584 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400585 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000586 *stat &= 0xFFFFFFFFFFFFULL;
587}
588
589/**
590 * i40e_stat_update32 - read and update a 32 bit stat from the chip
591 * @hw: ptr to the hardware info
592 * @reg: the hw reg to read
593 * @offset_loaded: has the initial offset been loaded yet
594 * @offset: ptr to current offset value
595 * @stat: ptr to the stat
596 **/
597static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
598 bool offset_loaded, u64 *offset, u64 *stat)
599{
600 u32 new_data;
601
602 new_data = rd32(hw, reg);
603 if (!offset_loaded)
604 *offset = new_data;
605 if (likely(new_data >= *offset))
606 *stat = (u32)(new_data - *offset);
607 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400608 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000609}
610
611/**
612 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
613 * @vsi: the VSI to be updated
614 **/
615void i40e_update_eth_stats(struct i40e_vsi *vsi)
616{
617 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
618 struct i40e_pf *pf = vsi->back;
619 struct i40e_hw *hw = &pf->hw;
620 struct i40e_eth_stats *oes;
621 struct i40e_eth_stats *es; /* device's eth stats */
622
623 es = &vsi->eth_stats;
624 oes = &vsi->eth_stats_offsets;
625
626 /* Gather up the stats that the hw collects */
627 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
628 vsi->stat_offsets_loaded,
629 &oes->tx_errors, &es->tx_errors);
630 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
631 vsi->stat_offsets_loaded,
632 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000633 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
636 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
637 vsi->stat_offsets_loaded,
638 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000639
640 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
641 I40E_GLV_GORCL(stat_idx),
642 vsi->stat_offsets_loaded,
643 &oes->rx_bytes, &es->rx_bytes);
644 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
645 I40E_GLV_UPRCL(stat_idx),
646 vsi->stat_offsets_loaded,
647 &oes->rx_unicast, &es->rx_unicast);
648 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
649 I40E_GLV_MPRCL(stat_idx),
650 vsi->stat_offsets_loaded,
651 &oes->rx_multicast, &es->rx_multicast);
652 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
653 I40E_GLV_BPRCL(stat_idx),
654 vsi->stat_offsets_loaded,
655 &oes->rx_broadcast, &es->rx_broadcast);
656
657 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
658 I40E_GLV_GOTCL(stat_idx),
659 vsi->stat_offsets_loaded,
660 &oes->tx_bytes, &es->tx_bytes);
661 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
662 I40E_GLV_UPTCL(stat_idx),
663 vsi->stat_offsets_loaded,
664 &oes->tx_unicast, &es->tx_unicast);
665 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
666 I40E_GLV_MPTCL(stat_idx),
667 vsi->stat_offsets_loaded,
668 &oes->tx_multicast, &es->tx_multicast);
669 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
670 I40E_GLV_BPTCL(stat_idx),
671 vsi->stat_offsets_loaded,
672 &oes->tx_broadcast, &es->tx_broadcast);
673 vsi->stat_offsets_loaded = true;
674}
675
676/**
677 * i40e_update_veb_stats - Update Switch component statistics
678 * @veb: the VEB being updated
679 **/
680static void i40e_update_veb_stats(struct i40e_veb *veb)
681{
682 struct i40e_pf *pf = veb->pf;
683 struct i40e_hw *hw = &pf->hw;
684 struct i40e_eth_stats *oes;
685 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400686 struct i40e_veb_tc_stats *veb_oes;
687 struct i40e_veb_tc_stats *veb_es;
688 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000689
690 idx = veb->stats_idx;
691 es = &veb->stats;
692 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400693 veb_es = &veb->tc_stats;
694 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000695
696 /* Gather up the stats that the hw collects */
697 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
698 veb->stat_offsets_loaded,
699 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000700 if (hw->revision_id > 0)
701 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
702 veb->stat_offsets_loaded,
703 &oes->rx_unknown_protocol,
704 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000705 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->rx_bytes, &es->rx_bytes);
708 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
709 veb->stat_offsets_loaded,
710 &oes->rx_unicast, &es->rx_unicast);
711 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
712 veb->stat_offsets_loaded,
713 &oes->rx_multicast, &es->rx_multicast);
714 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
715 veb->stat_offsets_loaded,
716 &oes->rx_broadcast, &es->rx_broadcast);
717
718 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
719 veb->stat_offsets_loaded,
720 &oes->tx_bytes, &es->tx_bytes);
721 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
722 veb->stat_offsets_loaded,
723 &oes->tx_unicast, &es->tx_unicast);
724 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
725 veb->stat_offsets_loaded,
726 &oes->tx_multicast, &es->tx_multicast);
727 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
728 veb->stat_offsets_loaded,
729 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400730 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
731 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
732 I40E_GLVEBTC_RPCL(i, idx),
733 veb->stat_offsets_loaded,
734 &veb_oes->tc_rx_packets[i],
735 &veb_es->tc_rx_packets[i]);
736 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
737 I40E_GLVEBTC_RBCL(i, idx),
738 veb->stat_offsets_loaded,
739 &veb_oes->tc_rx_bytes[i],
740 &veb_es->tc_rx_bytes[i]);
741 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
742 I40E_GLVEBTC_TPCL(i, idx),
743 veb->stat_offsets_loaded,
744 &veb_oes->tc_tx_packets[i],
745 &veb_es->tc_tx_packets[i]);
746 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
747 I40E_GLVEBTC_TBCL(i, idx),
748 veb->stat_offsets_loaded,
749 &veb_oes->tc_tx_bytes[i],
750 &veb_es->tc_tx_bytes[i]);
751 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000752 veb->stat_offsets_loaded = true;
753}
754
Vasu Dev38e00432014-08-01 13:27:03 -0700755#ifdef I40E_FCOE
756/**
757 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
758 * @vsi: the VSI that is capable of doing FCoE
759 **/
760static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
761{
762 struct i40e_pf *pf = vsi->back;
763 struct i40e_hw *hw = &pf->hw;
764 struct i40e_fcoe_stats *ofs;
765 struct i40e_fcoe_stats *fs; /* device's eth stats */
766 int idx;
767
768 if (vsi->type != I40E_VSI_FCOE)
769 return;
770
771 idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
772 fs = &vsi->fcoe_stats;
773 ofs = &vsi->fcoe_stats_offsets;
774
775 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
776 vsi->fcoe_stat_offsets_loaded,
777 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
778 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
779 vsi->fcoe_stat_offsets_loaded,
780 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
781 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
782 vsi->fcoe_stat_offsets_loaded,
783 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
784 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
785 vsi->fcoe_stat_offsets_loaded,
786 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
787 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
788 vsi->fcoe_stat_offsets_loaded,
789 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
790 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
791 vsi->fcoe_stat_offsets_loaded,
792 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
793 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
794 vsi->fcoe_stat_offsets_loaded,
795 &ofs->fcoe_last_error, &fs->fcoe_last_error);
796 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
797 vsi->fcoe_stat_offsets_loaded,
798 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
799
800 vsi->fcoe_stat_offsets_loaded = true;
801}
802
803#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000804/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000805 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 * @vsi: the VSI to be updated
807 *
808 * There are a few instances where we store the same stat in a
809 * couple of different structs. This is partly because we have
810 * the netdev stats that need to be filled out, which is slightly
811 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000812 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000813 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000814static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000815{
816 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 struct rtnl_link_stats64 *ons;
818 struct rtnl_link_stats64 *ns; /* netdev stats */
819 struct i40e_eth_stats *oes;
820 struct i40e_eth_stats *es; /* device's eth stats */
821 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000822 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000823 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000824 u64 bytes, packets;
825 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400826 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400827 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000828 u64 rx_p, rx_b;
829 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000830 u16 q;
831
832 if (test_bit(__I40E_DOWN, &vsi->state) ||
833 test_bit(__I40E_CONFIG_BUSY, &pf->state))
834 return;
835
836 ns = i40e_get_vsi_stats_struct(vsi);
837 ons = &vsi->net_stats_offsets;
838 es = &vsi->eth_stats;
839 oes = &vsi->eth_stats_offsets;
840
841 /* Gather up the netdev and vsi stats that the driver collects
842 * on the fly during packet processing
843 */
844 rx_b = rx_p = 0;
845 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400846 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000847 rx_page = 0;
848 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000849 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000850 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000851 /* locate Tx ring */
852 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000853
Alexander Duyck980e9b12013-09-28 06:01:03 +0000854 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700855 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000856 packets = p->stats.packets;
857 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700858 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000859 tx_b += bytes;
860 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000861 tx_restart += p->tx_stats.restart_queue;
862 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400863 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400864 tx_force_wb += p->tx_stats.tx_force_wb;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000865
866 /* Rx queue is part of the same block as Tx queue */
867 p = &p[1];
868 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700869 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000870 packets = p->stats.packets;
871 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700872 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000873 rx_b += bytes;
874 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000875 rx_buf += p->rx_stats.alloc_buff_failed;
876 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000877 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000878 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000879 vsi->tx_restart = tx_restart;
880 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400881 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400882 vsi->tx_force_wb = tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000883 vsi->rx_page_failed = rx_page;
884 vsi->rx_buf_failed = rx_buf;
885
886 ns->rx_packets = rx_p;
887 ns->rx_bytes = rx_b;
888 ns->tx_packets = tx_p;
889 ns->tx_bytes = tx_b;
890
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000893 ons->tx_errors = oes->tx_errors;
894 ns->tx_errors = es->tx_errors;
895 ons->multicast = oes->rx_multicast;
896 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000897 ons->rx_dropped = oes->rx_discards;
898 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000899 ons->tx_dropped = oes->tx_discards;
900 ns->tx_dropped = es->tx_discards;
901
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000902 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000903 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000904 ns->rx_crc_errors = pf->stats.crc_errors;
905 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
906 ns->rx_length_errors = pf->stats.rx_length_errors;
907 }
908}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000909
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000910/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +0000911 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000912 * @pf: the PF to be updated
913 **/
914static void i40e_update_pf_stats(struct i40e_pf *pf)
915{
916 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
917 struct i40e_hw_port_stats *nsd = &pf->stats;
918 struct i40e_hw *hw = &pf->hw;
919 u32 val;
920 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000921
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000922 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
923 I40E_GLPRT_GORCL(hw->port),
924 pf->stat_offsets_loaded,
925 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
926 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
927 I40E_GLPRT_GOTCL(hw->port),
928 pf->stat_offsets_loaded,
929 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
930 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
931 pf->stat_offsets_loaded,
932 &osd->eth.rx_discards,
933 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000934 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
935 I40E_GLPRT_UPRCL(hw->port),
936 pf->stat_offsets_loaded,
937 &osd->eth.rx_unicast,
938 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000939 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
940 I40E_GLPRT_MPRCL(hw->port),
941 pf->stat_offsets_loaded,
942 &osd->eth.rx_multicast,
943 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000944 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
945 I40E_GLPRT_BPRCL(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->eth.rx_broadcast,
948 &nsd->eth.rx_broadcast);
949 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
950 I40E_GLPRT_UPTCL(hw->port),
951 pf->stat_offsets_loaded,
952 &osd->eth.tx_unicast,
953 &nsd->eth.tx_unicast);
954 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
955 I40E_GLPRT_MPTCL(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->eth.tx_multicast,
958 &nsd->eth.tx_multicast);
959 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
960 I40E_GLPRT_BPTCL(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->eth.tx_broadcast,
963 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000964
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000965 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
966 pf->stat_offsets_loaded,
967 &osd->tx_dropped_link_down,
968 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000969
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000970 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000973
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000974 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
975 pf->stat_offsets_loaded,
976 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000977
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000978 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
979 pf->stat_offsets_loaded,
980 &osd->mac_local_faults,
981 &nsd->mac_local_faults);
982 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->mac_remote_faults,
985 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000986
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000987 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
988 pf->stat_offsets_loaded,
989 &osd->rx_length_errors,
990 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000991
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000992 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
993 pf->stat_offsets_loaded,
994 &osd->link_xon_rx, &nsd->link_xon_rx);
995 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
996 pf->stat_offsets_loaded,
997 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800998 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
999 pf->stat_offsets_loaded,
1000 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001001 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1002 pf->stat_offsets_loaded,
1003 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001004
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001005 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -08001006 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1007 pf->stat_offsets_loaded,
1008 &osd->priority_xoff_rx[i],
1009 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001010 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001011 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001012 &osd->priority_xon_rx[i],
1013 &nsd->priority_xon_rx[i]);
1014 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001015 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001016 &osd->priority_xon_tx[i],
1017 &nsd->priority_xon_tx[i]);
1018 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001019 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001020 &osd->priority_xoff_tx[i],
1021 &nsd->priority_xoff_tx[i]);
1022 i40e_stat_update32(hw,
1023 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001024 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001025 &osd->priority_xon_2_xoff[i],
1026 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001027 }
1028
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001029 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1030 I40E_GLPRT_PRC64L(hw->port),
1031 pf->stat_offsets_loaded,
1032 &osd->rx_size_64, &nsd->rx_size_64);
1033 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1034 I40E_GLPRT_PRC127L(hw->port),
1035 pf->stat_offsets_loaded,
1036 &osd->rx_size_127, &nsd->rx_size_127);
1037 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1038 I40E_GLPRT_PRC255L(hw->port),
1039 pf->stat_offsets_loaded,
1040 &osd->rx_size_255, &nsd->rx_size_255);
1041 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1042 I40E_GLPRT_PRC511L(hw->port),
1043 pf->stat_offsets_loaded,
1044 &osd->rx_size_511, &nsd->rx_size_511);
1045 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1046 I40E_GLPRT_PRC1023L(hw->port),
1047 pf->stat_offsets_loaded,
1048 &osd->rx_size_1023, &nsd->rx_size_1023);
1049 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1050 I40E_GLPRT_PRC1522L(hw->port),
1051 pf->stat_offsets_loaded,
1052 &osd->rx_size_1522, &nsd->rx_size_1522);
1053 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1054 I40E_GLPRT_PRC9522L(hw->port),
1055 pf->stat_offsets_loaded,
1056 &osd->rx_size_big, &nsd->rx_size_big);
1057
1058 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1059 I40E_GLPRT_PTC64L(hw->port),
1060 pf->stat_offsets_loaded,
1061 &osd->tx_size_64, &nsd->tx_size_64);
1062 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1063 I40E_GLPRT_PTC127L(hw->port),
1064 pf->stat_offsets_loaded,
1065 &osd->tx_size_127, &nsd->tx_size_127);
1066 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1067 I40E_GLPRT_PTC255L(hw->port),
1068 pf->stat_offsets_loaded,
1069 &osd->tx_size_255, &nsd->tx_size_255);
1070 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1071 I40E_GLPRT_PTC511L(hw->port),
1072 pf->stat_offsets_loaded,
1073 &osd->tx_size_511, &nsd->tx_size_511);
1074 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1075 I40E_GLPRT_PTC1023L(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->tx_size_1023, &nsd->tx_size_1023);
1078 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1079 I40E_GLPRT_PTC1522L(hw->port),
1080 pf->stat_offsets_loaded,
1081 &osd->tx_size_1522, &nsd->tx_size_1522);
1082 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1083 I40E_GLPRT_PTC9522L(hw->port),
1084 pf->stat_offsets_loaded,
1085 &osd->tx_size_big, &nsd->tx_size_big);
1086
1087 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1088 pf->stat_offsets_loaded,
1089 &osd->rx_undersize, &nsd->rx_undersize);
1090 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1091 pf->stat_offsets_loaded,
1092 &osd->rx_fragments, &nsd->rx_fragments);
1093 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1094 pf->stat_offsets_loaded,
1095 &osd->rx_oversize, &nsd->rx_oversize);
1096 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1097 pf->stat_offsets_loaded,
1098 &osd->rx_jabber, &nsd->rx_jabber);
1099
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001100 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001101 i40e_stat_update32(hw,
1102 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001103 pf->stat_offsets_loaded,
1104 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001105 i40e_stat_update32(hw,
1106 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001107 pf->stat_offsets_loaded,
1108 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001109 i40e_stat_update32(hw,
1110 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1111 pf->stat_offsets_loaded,
1112 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001113
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001114 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1115 nsd->tx_lpi_status =
1116 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1117 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1118 nsd->rx_lpi_status =
1119 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1120 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1121 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1122 pf->stat_offsets_loaded,
1123 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1124 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1125 pf->stat_offsets_loaded,
1126 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1127
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001128 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1129 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1130 nsd->fd_sb_status = true;
1131 else
1132 nsd->fd_sb_status = false;
1133
1134 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1135 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1136 nsd->fd_atr_status = true;
1137 else
1138 nsd->fd_atr_status = false;
1139
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001140 pf->stat_offsets_loaded = true;
1141}
1142
1143/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001144 * i40e_update_stats - Update the various statistics counters.
1145 * @vsi: the VSI to be updated
1146 *
1147 * Update the various stats for this VSI and its related entities.
1148 **/
1149void i40e_update_stats(struct i40e_vsi *vsi)
1150{
1151 struct i40e_pf *pf = vsi->back;
1152
1153 if (vsi == pf->vsi[pf->lan_vsi])
1154 i40e_update_pf_stats(pf);
1155
1156 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001157#ifdef I40E_FCOE
1158 i40e_update_fcoe_stats(vsi);
1159#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001160}
1161
1162/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001163 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1164 * @vsi: the VSI to be searched
1165 * @macaddr: the MAC address
1166 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001167 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001168 * @is_netdev: make sure its a netdev filter, else doesn't matter
1169 *
1170 * Returns ptr to the filter object or NULL
1171 **/
1172static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1173 u8 *macaddr, s16 vlan,
1174 bool is_vf, bool is_netdev)
1175{
1176 struct i40e_mac_filter *f;
1177
1178 if (!vsi || !macaddr)
1179 return NULL;
1180
1181 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1182 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1183 (vlan == f->vlan) &&
1184 (!is_vf || f->is_vf) &&
1185 (!is_netdev || f->is_netdev))
1186 return f;
1187 }
1188 return NULL;
1189}
1190
1191/**
1192 * i40e_find_mac - Find a mac addr in the macvlan filters list
1193 * @vsi: the VSI to be searched
1194 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001195 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001196 * @is_netdev: make sure its a netdev filter, else doesn't matter
1197 *
1198 * Returns the first filter with the provided MAC address or NULL if
1199 * MAC address was not found
1200 **/
1201struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1202 bool is_vf, bool is_netdev)
1203{
1204 struct i40e_mac_filter *f;
1205
1206 if (!vsi || !macaddr)
1207 return NULL;
1208
1209 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1210 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1211 (!is_vf || f->is_vf) &&
1212 (!is_netdev || f->is_netdev))
1213 return f;
1214 }
1215 return NULL;
1216}
1217
1218/**
1219 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1220 * @vsi: the VSI to be searched
1221 *
1222 * Returns true if VSI is in vlan mode or false otherwise
1223 **/
1224bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1225{
1226 struct i40e_mac_filter *f;
1227
1228 /* Only -1 for all the filters denotes not in vlan mode
1229 * so we have to go through all the list in order to make sure
1230 */
1231 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001232 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001233 return true;
1234 }
1235
1236 return false;
1237}
1238
1239/**
1240 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1241 * @vsi: the VSI to be searched
1242 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001243 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244 * @is_netdev: true if it is a netdev
1245 *
1246 * Goes through all the macvlan filters and adds a
1247 * macvlan filter for each unique vlan that already exists
1248 *
1249 * Returns first filter found on success, else NULL
1250 **/
1251struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1252 bool is_vf, bool is_netdev)
1253{
1254 struct i40e_mac_filter *f;
1255
1256 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001257 if (vsi->info.pvid)
1258 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001259 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1260 is_vf, is_netdev)) {
1261 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001262 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001263 return NULL;
1264 }
1265 }
1266
1267 return list_first_entry_or_null(&vsi->mac_filter_list,
1268 struct i40e_mac_filter, list);
1269}
1270
1271/**
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001272 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1273 * @vsi: the VSI to be searched
1274 * @macaddr: the mac address to be removed
1275 * @is_vf: true if it is a VF
1276 * @is_netdev: true if it is a netdev
1277 *
1278 * Removes a given MAC address from a VSI, regardless of VLAN
1279 *
1280 * Returns 0 for success, or error
1281 **/
1282int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1283 bool is_vf, bool is_netdev)
1284{
1285 struct i40e_mac_filter *f = NULL;
1286 int changed = 0;
1287
1288 WARN(!spin_is_locked(&vsi->mac_filter_list_lock),
1289 "Missing mac_filter_list_lock\n");
1290 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1291 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1292 (is_vf == f->is_vf) &&
1293 (is_netdev == f->is_netdev)) {
1294 f->counter--;
1295 f->changed = true;
1296 changed = 1;
1297 }
1298 }
1299 if (changed) {
1300 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1301 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1302 return 0;
1303 }
1304 return -ENOENT;
1305}
1306
1307/**
Greg Rose8c27d422014-05-22 06:31:56 +00001308 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1309 * @vsi: the PF Main VSI - inappropriate for any other VSI
1310 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001311 *
1312 * Some older firmware configurations set up a default promiscuous VLAN
1313 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001314 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001315static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001316{
1317 struct i40e_aqc_remove_macvlan_element_data element;
1318 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001319 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001320
1321 /* Only appropriate for the PF main VSI */
1322 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001323 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001324
Shannon Nelson30650cc2014-07-29 04:01:50 +00001325 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001326 ether_addr_copy(element.mac_addr, macaddr);
1327 element.vlan_tag = 0;
1328 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1329 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001330 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1331 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001332 return -ENOENT;
1333
1334 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001335}
1336
1337/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001338 * i40e_add_filter - Add a mac/vlan filter to the VSI
1339 * @vsi: the VSI to be searched
1340 * @macaddr: the MAC address
1341 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001342 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001343 * @is_netdev: make sure its a netdev filter, else doesn't matter
1344 *
1345 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001346 *
1347 * NOTE: This function is expected to be called with mac_filter_list_lock
1348 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001349 **/
1350struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1351 u8 *macaddr, s16 vlan,
1352 bool is_vf, bool is_netdev)
1353{
1354 struct i40e_mac_filter *f;
1355
1356 if (!vsi || !macaddr)
1357 return NULL;
1358
1359 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1360 if (!f) {
1361 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1362 if (!f)
1363 goto add_filter_out;
1364
Greg Rose9a173902014-05-22 06:32:02 +00001365 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001366 f->vlan = vlan;
1367 f->changed = true;
1368
1369 INIT_LIST_HEAD(&f->list);
Kiran Patil04d5a212015-12-09 15:50:23 -08001370 list_add_tail(&f->list, &vsi->mac_filter_list);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371 }
1372
1373 /* increment counter and add a new flag if needed */
1374 if (is_vf) {
1375 if (!f->is_vf) {
1376 f->is_vf = true;
1377 f->counter++;
1378 }
1379 } else if (is_netdev) {
1380 if (!f->is_netdev) {
1381 f->is_netdev = true;
1382 f->counter++;
1383 }
1384 } else {
1385 f->counter++;
1386 }
1387
1388 /* changed tells sync_filters_subtask to
1389 * push the filter down to the firmware
1390 */
1391 if (f->changed) {
1392 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1393 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1394 }
1395
1396add_filter_out:
1397 return f;
1398}
1399
1400/**
1401 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1402 * @vsi: the VSI to be searched
1403 * @macaddr: the MAC address
1404 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001405 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001406 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001407 *
1408 * NOTE: This function is expected to be called with mac_filter_list_lock
1409 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001410 **/
1411void i40e_del_filter(struct i40e_vsi *vsi,
1412 u8 *macaddr, s16 vlan,
1413 bool is_vf, bool is_netdev)
1414{
1415 struct i40e_mac_filter *f;
1416
1417 if (!vsi || !macaddr)
1418 return;
1419
1420 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1421 if (!f || f->counter == 0)
1422 return;
1423
1424 if (is_vf) {
1425 if (f->is_vf) {
1426 f->is_vf = false;
1427 f->counter--;
1428 }
1429 } else if (is_netdev) {
1430 if (f->is_netdev) {
1431 f->is_netdev = false;
1432 f->counter--;
1433 }
1434 } else {
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001435 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001436 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001437
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001438 min_f += (f->is_vf ? 1 : 0);
1439 min_f += (f->is_netdev ? 1 : 0);
1440
1441 if (f->counter > min_f)
1442 f->counter--;
1443 }
1444
1445 /* counter == 0 tells sync_filters_subtask to
1446 * remove the filter from the firmware's list
1447 */
1448 if (f->counter == 0) {
1449 f->changed = true;
1450 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1451 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1452 }
1453}
1454
1455/**
1456 * i40e_set_mac - NDO callback to set mac address
1457 * @netdev: network interface device structure
1458 * @p: pointer to an address structure
1459 *
1460 * Returns 0 on success, negative on failure
1461 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001462#ifdef I40E_FCOE
1463int i40e_set_mac(struct net_device *netdev, void *p)
1464#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001465static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001466#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001467{
1468 struct i40e_netdev_priv *np = netdev_priv(netdev);
1469 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001470 struct i40e_pf *pf = vsi->back;
1471 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001472 struct sockaddr *addr = p;
1473 struct i40e_mac_filter *f;
1474
1475 if (!is_valid_ether_addr(addr->sa_data))
1476 return -EADDRNOTAVAIL;
1477
Shannon Nelson30650cc2014-07-29 04:01:50 +00001478 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1479 netdev_info(netdev, "already using mac address %pM\n",
1480 addr->sa_data);
1481 return 0;
1482 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001483
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001484 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1485 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1486 return -EADDRNOTAVAIL;
1487
Shannon Nelson30650cc2014-07-29 04:01:50 +00001488 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1489 netdev_info(netdev, "returning to hw mac address %pM\n",
1490 hw->mac.addr);
1491 else
1492 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1493
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001494 if (vsi->type == I40E_VSI_MAIN) {
1495 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001497 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001498 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001499 addr->sa_data, NULL);
1500 if (ret) {
1501 netdev_info(netdev,
1502 "Addr change for Main VSI failed: %d\n",
1503 ret);
1504 return -EADDRNOTAVAIL;
1505 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001506 }
1507
Shannon Nelson30650cc2014-07-29 04:01:50 +00001508 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1509 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001510
Shannon Nelson30650cc2014-07-29 04:01:50 +00001511 memset(&element, 0, sizeof(element));
1512 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1513 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1514 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1515 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001516 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001517 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1518 false, false);
Kiran Patil21659032015-09-30 14:09:03 -04001519 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001520 }
1521
Shannon Nelson30650cc2014-07-29 04:01:50 +00001522 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1523 struct i40e_aqc_add_macvlan_element_data element;
1524
1525 memset(&element, 0, sizeof(element));
1526 ether_addr_copy(element.mac_addr, hw->mac.addr);
1527 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1528 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1529 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001530 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001531 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1532 false, false);
1533 if (f)
1534 f->is_laa = true;
Kiran Patil21659032015-09-30 14:09:03 -04001535 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001536 }
1537
Shannon Nelson30650cc2014-07-29 04:01:50 +00001538 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Mitch Williamsea02e902015-11-09 15:35:50 -08001539
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001540 /* schedule our worker thread which will take care of
1541 * applying the new filter changes
1542 */
1543 i40e_service_event_schedule(vsi->back);
1544 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001545}
1546
1547/**
1548 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1549 * @vsi: the VSI being setup
1550 * @ctxt: VSI context structure
1551 * @enabled_tc: Enabled TCs bitmap
1552 * @is_add: True if called before Add VSI
1553 *
1554 * Setup VSI queue mapping for enabled traffic classes.
1555 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001556#ifdef I40E_FCOE
1557void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1558 struct i40e_vsi_context *ctxt,
1559 u8 enabled_tc,
1560 bool is_add)
1561#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001562static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1563 struct i40e_vsi_context *ctxt,
1564 u8 enabled_tc,
1565 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001566#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001567{
1568 struct i40e_pf *pf = vsi->back;
1569 u16 sections = 0;
1570 u8 netdev_tc = 0;
1571 u16 numtc = 0;
1572 u16 qcount;
1573 u8 offset;
1574 u16 qmap;
1575 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001576 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577
1578 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1579 offset = 0;
1580
1581 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1582 /* Find numtc from enabled TC bitmap */
1583 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001584 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001585 numtc++;
1586 }
1587 if (!numtc) {
1588 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1589 numtc = 1;
1590 }
1591 } else {
1592 /* At least TC0 is enabled in case of non-DCB case */
1593 numtc = 1;
1594 }
1595
1596 vsi->tc_config.numtc = numtc;
1597 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001598 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001599 /* In MFP case we can have a much lower count of MSIx
1600 * vectors available and so we need to lower the used
1601 * q count.
1602 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001603 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1604 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1605 else
1606 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001607 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001608 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609
1610 /* Setup queue offset/count for all TCs for given VSI */
1611 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1612 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001613 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001614 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001615 int pow, num_qps;
1616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 switch (vsi->type) {
1618 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001619 qcount = min_t(int, pf->alloc_rss_size,
1620 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001621 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001622#ifdef I40E_FCOE
1623 case I40E_VSI_FCOE:
1624 qcount = num_tc_qps;
1625 break;
1626#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001627 case I40E_VSI_FDIR:
1628 case I40E_VSI_SRIOV:
1629 case I40E_VSI_VMDQ2:
1630 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001631 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632 WARN_ON(i != 0);
1633 break;
1634 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001635 vsi->tc_config.tc_info[i].qoffset = offset;
1636 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001637
Shannon Nelson1e200e42015-02-27 09:15:24 +00001638 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001639 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001641 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001642 pow++;
1643 num_qps >>= 1;
1644 }
1645
1646 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1647 qmap =
1648 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1649 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1650
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001651 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001652 } else {
1653 /* TC is not enabled so set the offset to
1654 * default queue and allocate one queue
1655 * for the given TC.
1656 */
1657 vsi->tc_config.tc_info[i].qoffset = 0;
1658 vsi->tc_config.tc_info[i].qcount = 1;
1659 vsi->tc_config.tc_info[i].netdev_tc = 0;
1660
1661 qmap = 0;
1662 }
1663 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1664 }
1665
1666 /* Set actual Tx/Rx queue pairs */
1667 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001668 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1669 if (vsi->req_queue_pairs > 0)
1670 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001671 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001672 vsi->num_queue_pairs = pf->num_lan_msix;
1673 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001674
1675 /* Scheduler section valid can only be set for ADD VSI */
1676 if (is_add) {
1677 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1678
1679 ctxt->info.up_enable_bits = enabled_tc;
1680 }
1681 if (vsi->type == I40E_VSI_SRIOV) {
1682 ctxt->info.mapping_flags |=
1683 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1684 for (i = 0; i < vsi->num_queue_pairs; i++)
1685 ctxt->info.queue_mapping[i] =
1686 cpu_to_le16(vsi->base_queue + i);
1687 } else {
1688 ctxt->info.mapping_flags |=
1689 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1690 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1691 }
1692 ctxt->info.valid_sections |= cpu_to_le16(sections);
1693}
1694
1695/**
1696 * i40e_set_rx_mode - NDO callback to set the netdev filters
1697 * @netdev: network interface device structure
1698 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001699#ifdef I40E_FCOE
1700void i40e_set_rx_mode(struct net_device *netdev)
1701#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001702static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001703#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001704{
1705 struct i40e_netdev_priv *np = netdev_priv(netdev);
1706 struct i40e_mac_filter *f, *ftmp;
1707 struct i40e_vsi *vsi = np->vsi;
1708 struct netdev_hw_addr *uca;
1709 struct netdev_hw_addr *mca;
1710 struct netdev_hw_addr *ha;
1711
Kiran Patil21659032015-09-30 14:09:03 -04001712 spin_lock_bh(&vsi->mac_filter_list_lock);
1713
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001714 /* add addr if not already in the filter list */
1715 netdev_for_each_uc_addr(uca, netdev) {
1716 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1717 if (i40e_is_vsi_in_vlan(vsi))
1718 i40e_put_mac_in_vlan(vsi, uca->addr,
1719 false, true);
1720 else
1721 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1722 false, true);
1723 }
1724 }
1725
1726 netdev_for_each_mc_addr(mca, netdev) {
1727 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1728 if (i40e_is_vsi_in_vlan(vsi))
1729 i40e_put_mac_in_vlan(vsi, mca->addr,
1730 false, true);
1731 else
1732 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1733 false, true);
1734 }
1735 }
1736
1737 /* remove filter if not in netdev list */
1738 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001739
1740 if (!f->is_netdev)
1741 continue;
1742
Shannon Nelson2f41f332015-08-26 15:14:20 -04001743 netdev_for_each_mc_addr(mca, netdev)
1744 if (ether_addr_equal(mca->addr, f->macaddr))
1745 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001746
Shannon Nelson2f41f332015-08-26 15:14:20 -04001747 netdev_for_each_uc_addr(uca, netdev)
1748 if (ether_addr_equal(uca->addr, f->macaddr))
1749 goto bottom_of_search_loop;
1750
1751 for_each_dev_addr(netdev, ha)
1752 if (ether_addr_equal(ha->addr, f->macaddr))
1753 goto bottom_of_search_loop;
1754
1755 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1756 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1757
1758bottom_of_search_loop:
1759 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001760 }
Kiran Patil21659032015-09-30 14:09:03 -04001761 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001762
1763 /* check for other flag changes */
1764 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1765 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1766 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1767 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001768
1769 /* schedule our worker thread which will take care of
1770 * applying the new filter changes
1771 */
1772 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001773}
1774
1775/**
Kiran Patil21659032015-09-30 14:09:03 -04001776 * i40e_mac_filter_entry_clone - Clones a MAC filter entry
1777 * @src: source MAC filter entry to be clones
1778 *
1779 * Returns the pointer to newly cloned MAC filter entry or NULL
1780 * in case of error
1781 **/
1782static struct i40e_mac_filter *i40e_mac_filter_entry_clone(
1783 struct i40e_mac_filter *src)
1784{
1785 struct i40e_mac_filter *f;
1786
1787 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1788 if (!f)
1789 return NULL;
1790 *f = *src;
1791
1792 INIT_LIST_HEAD(&f->list);
1793
1794 return f;
1795}
1796
1797/**
1798 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1799 * @vsi: pointer to vsi struct
1800 * @from: Pointer to list which contains MAC filter entries - changes to
1801 * those entries needs to be undone.
1802 *
1803 * MAC filter entries from list were slated to be removed from device.
1804 **/
1805static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1806 struct list_head *from)
1807{
1808 struct i40e_mac_filter *f, *ftmp;
1809
1810 list_for_each_entry_safe(f, ftmp, from, list) {
1811 f->changed = true;
1812 /* Move the element back into MAC filter list*/
1813 list_move_tail(&f->list, &vsi->mac_filter_list);
1814 }
1815}
1816
1817/**
1818 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1819 * @vsi: pointer to vsi struct
1820 *
1821 * MAC filter entries from list were slated to be added from device.
1822 **/
1823static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi)
1824{
1825 struct i40e_mac_filter *f, *ftmp;
1826
1827 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1828 if (!f->changed && f->counter)
1829 f->changed = true;
1830 }
1831}
1832
1833/**
1834 * i40e_cleanup_add_list - Deletes the element from add list and release
1835 * memory
1836 * @add_list: Pointer to list which contains MAC filter entries
1837 **/
1838static void i40e_cleanup_add_list(struct list_head *add_list)
1839{
1840 struct i40e_mac_filter *f, *ftmp;
1841
1842 list_for_each_entry_safe(f, ftmp, add_list, list) {
1843 list_del(&f->list);
1844 kfree(f);
1845 }
1846}
1847
1848/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001849 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1850 * @vsi: ptr to the VSI
1851 *
1852 * Push any outstanding VSI filter changes through the AdminQ.
1853 *
1854 * Returns 0 or error value
1855 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001856int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001857{
Kiran Patil21659032015-09-30 14:09:03 -04001858 struct list_head tmp_del_list, tmp_add_list;
1859 struct i40e_mac_filter *f, *ftmp, *fclone;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001860 bool promisc_forced_on = false;
1861 bool add_happened = false;
1862 int filter_list_len = 0;
1863 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001864 i40e_status aq_ret = 0;
Kiran Patil21659032015-09-30 14:09:03 -04001865 bool err_cond = false;
Mitch Williamsea02e902015-11-09 15:35:50 -08001866 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001867 struct i40e_pf *pf;
1868 int num_add = 0;
1869 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001870 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001871 u16 cmd_flags;
1872
1873 /* empty array typed pointers, kcalloc later */
1874 struct i40e_aqc_add_macvlan_element_data *add_list;
1875 struct i40e_aqc_remove_macvlan_element_data *del_list;
1876
1877 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1878 usleep_range(1000, 2000);
1879 pf = vsi->back;
1880
1881 if (vsi->netdev) {
1882 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1883 vsi->current_netdev_flags = vsi->netdev->flags;
1884 }
1885
Kiran Patil21659032015-09-30 14:09:03 -04001886 INIT_LIST_HEAD(&tmp_del_list);
1887 INIT_LIST_HEAD(&tmp_add_list);
1888
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001889 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1890 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1891
Kiran Patil21659032015-09-30 14:09:03 -04001892 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001893 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1894 if (!f->changed)
1895 continue;
1896
1897 if (f->counter != 0)
1898 continue;
1899 f->changed = false;
Kiran Patil21659032015-09-30 14:09:03 -04001900
1901 /* Move the element into temporary del_list */
1902 list_move_tail(&f->list, &tmp_del_list);
1903 }
1904
1905 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1906 if (!f->changed)
1907 continue;
1908
1909 if (f->counter == 0)
1910 continue;
1911 f->changed = false;
1912
1913 /* Clone MAC filter entry and add into temporary list */
1914 fclone = i40e_mac_filter_entry_clone(f);
1915 if (!fclone) {
1916 err_cond = true;
1917 break;
1918 }
1919 list_add_tail(&fclone->list, &tmp_add_list);
1920 }
1921
1922 /* if failed to clone MAC filter entry - undo */
1923 if (err_cond) {
1924 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1925 i40e_undo_add_filter_entries(vsi);
1926 }
1927 spin_unlock_bh(&vsi->mac_filter_list_lock);
1928
Mitch Williamsea02e902015-11-09 15:35:50 -08001929 if (err_cond) {
Kiran Patil21659032015-09-30 14:09:03 -04001930 i40e_cleanup_add_list(&tmp_add_list);
Mitch Williamsea02e902015-11-09 15:35:50 -08001931 retval = -ENOMEM;
1932 goto out;
1933 }
Kiran Patil21659032015-09-30 14:09:03 -04001934 }
1935
1936 /* Now process 'del_list' outside the lock */
1937 if (!list_empty(&tmp_del_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08001938 int del_list_size;
1939
Kiran Patil21659032015-09-30 14:09:03 -04001940 filter_list_len = pf->hw.aq.asq_buf_size /
1941 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Shannon Nelsonf1199992015-11-19 11:34:23 -08001942 del_list_size = filter_list_len *
1943 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Jesse Brandeburg38c3cec2016-01-04 10:33:05 -08001944 del_list = kzalloc(del_list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001945 if (!del_list) {
1946 i40e_cleanup_add_list(&tmp_add_list);
1947
1948 /* Undo VSI's MAC filter entry element updates */
1949 spin_lock_bh(&vsi->mac_filter_list_lock);
1950 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1951 i40e_undo_add_filter_entries(vsi);
1952 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001953 retval = -ENOMEM;
1954 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001955 }
1956
1957 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001958 cmd_flags = 0;
1959
1960 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001961 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001962 del_list[num_del].vlan_tag =
1963 cpu_to_le16((u16)(f->vlan ==
1964 I40E_VLAN_ANY ? 0 : f->vlan));
1965
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001966 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1967 del_list[num_del].flags = cmd_flags;
1968 num_del++;
1969
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001970 /* flush a full buffer */
1971 if (num_del == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001972 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
1973 vsi->seid,
1974 del_list,
1975 num_del,
1976 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001977 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001978 num_del = 0;
Shannon Nelsonf1199992015-11-19 11:34:23 -08001979 memset(del_list, 0, del_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001980
Mitch Williamsea02e902015-11-09 15:35:50 -08001981 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT) {
1982 retval = -EIO;
Kiran Patil21659032015-09-30 14:09:03 -04001983 dev_err(&pf->pdev->dev,
1984 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08001985 i40e_stat_str(&pf->hw, aq_ret),
Kiran Patil21659032015-09-30 14:09:03 -04001986 i40e_aq_str(&pf->hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001987 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001988 }
Kiran Patil21659032015-09-30 14:09:03 -04001989 /* Release memory for MAC filter entries which were
1990 * synced up with HW.
1991 */
1992 list_del(&f->list);
1993 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001994 }
Kiran Patil21659032015-09-30 14:09:03 -04001995
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001996 if (num_del) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001997 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
1998 del_list, num_del,
1999 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002000 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002001 num_del = 0;
2002
Mitch Williamsea02e902015-11-09 15:35:50 -08002003 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002004 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002005 "ignoring delete macvlan error, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002006 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002007 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002008 }
2009
2010 kfree(del_list);
2011 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002012 }
2013
2014 if (!list_empty(&tmp_add_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08002015 int add_list_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002016
2017 /* do all the adds now */
2018 filter_list_len = pf->hw.aq.asq_buf_size /
2019 sizeof(struct i40e_aqc_add_macvlan_element_data),
Shannon Nelsonf1199992015-11-19 11:34:23 -08002020 add_list_size = filter_list_len *
2021 sizeof(struct i40e_aqc_add_macvlan_element_data);
Jesse Brandeburg38c3cec2016-01-04 10:33:05 -08002022 add_list = kzalloc(add_list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04002023 if (!add_list) {
2024 /* Purge element from temporary lists */
2025 i40e_cleanup_add_list(&tmp_add_list);
2026
2027 /* Undo add filter entries from VSI MAC filter list */
2028 spin_lock_bh(&vsi->mac_filter_list_lock);
2029 i40e_undo_add_filter_entries(vsi);
2030 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08002031 retval = -ENOMEM;
2032 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04002033 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002034
Kiran Patil21659032015-09-30 14:09:03 -04002035 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002037 add_happened = true;
2038 cmd_flags = 0;
2039
2040 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00002041 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002042 add_list[num_add].vlan_tag =
2043 cpu_to_le16(
2044 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
2045 add_list[num_add].queue_number = 0;
2046
2047 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002048 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2049 num_add++;
2050
2051 /* flush a full buffer */
2052 if (num_add == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002053 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2054 add_list, num_add,
2055 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002056 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002057 num_add = 0;
2058
Mitch Williamsea02e902015-11-09 15:35:50 -08002059 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002060 break;
Shannon Nelsonf1199992015-11-19 11:34:23 -08002061 memset(add_list, 0, add_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 }
Kiran Patil21659032015-09-30 14:09:03 -04002063 /* Entries from tmp_add_list were cloned from MAC
2064 * filter list, hence clean those cloned entries
2065 */
2066 list_del(&f->list);
2067 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002068 }
Kiran Patil21659032015-09-30 14:09:03 -04002069
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002070 if (num_add) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002071 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2072 add_list, num_add, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002073 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002074 num_add = 0;
2075 }
2076 kfree(add_list);
2077 add_list = NULL;
2078
Mitch Williamsea02e902015-11-09 15:35:50 -08002079 if (add_happened && aq_ret && aq_err != I40E_AQ_RC_EINVAL) {
2080 retval = i40e_aq_rc_to_posix(aq_ret, aq_err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002082 "add filter failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002083 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002084 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002085 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
2086 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2087 &vsi->state)) {
2088 promisc_forced_on = true;
2089 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2090 &vsi->state);
2091 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
2092 }
2093 }
2094 }
2095
2096 /* check for changes in promiscuous modes */
2097 if (changed_flags & IFF_ALLMULTI) {
2098 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002099
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002100 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002101 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2102 vsi->seid,
2103 cur_multipromisc,
2104 NULL);
2105 if (aq_ret) {
2106 retval = i40e_aq_rc_to_posix(aq_ret,
2107 pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002109 "set multi promisc failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002110 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002111 i40e_aq_str(&pf->hw,
2112 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002113 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114 }
2115 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
2116 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002117
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002118 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2119 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2120 &vsi->state));
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002121 if (vsi->type == I40E_VSI_MAIN && pf->lan_veb != I40E_NO_VEB) {
2122 /* set defport ON for Main VSI instead of true promisc
2123 * this way we will get all unicast/multicast and VLAN
2124 * promisc behavior but will not get VF or VMDq traffic
2125 * replicated on the Main VSI.
2126 */
2127 if (pf->cur_promisc != cur_promisc) {
2128 pf->cur_promisc = cur_promisc;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002129 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002130 }
2131 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002132 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002133 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002134 vsi->seid,
2135 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002136 if (aq_ret) {
2137 retval =
2138 i40e_aq_rc_to_posix(aq_ret,
2139 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002140 dev_info(&pf->pdev->dev,
2141 "set unicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002142 aq_ret, pf->hw.aq.asq_last_status);
2143 }
2144 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002145 &vsi->back->hw,
2146 vsi->seid,
2147 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002148 if (aq_ret) {
2149 retval =
2150 i40e_aq_rc_to_posix(aq_ret,
2151 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002152 dev_info(&pf->pdev->dev,
2153 "set multicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002154 aq_ret, pf->hw.aq.asq_last_status);
2155 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002156 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002157 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2158 vsi->seid,
2159 cur_promisc, NULL);
2160 if (aq_ret) {
2161 retval = i40e_aq_rc_to_posix(aq_ret,
2162 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002163 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002164 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002165 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002166 i40e_aq_str(&pf->hw,
2167 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002168 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002169 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002170out:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002171 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002172 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002173}
2174
2175/**
2176 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2177 * @pf: board private structure
2178 **/
2179static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2180{
2181 int v;
2182
2183 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2184 return;
2185 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2186
Mitch Williams505682c2014-05-20 08:01:37 +00002187 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002188 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002189 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2190 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2191
2192 if (ret) {
2193 /* come back and try again later */
2194 pf->flags |= I40E_FLAG_FILTER_SYNC;
2195 break;
2196 }
2197 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002198 }
2199}
2200
2201/**
2202 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2203 * @netdev: network interface device structure
2204 * @new_mtu: new value for maximum frame size
2205 *
2206 * Returns 0 on success, negative on failure
2207 **/
2208static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2209{
2210 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002211 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002212 struct i40e_vsi *vsi = np->vsi;
2213
2214 /* MTU < 68 is an error and causes problems on some kernels */
2215 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2216 return -EINVAL;
2217
2218 netdev_info(netdev, "changing MTU from %d to %d\n",
2219 netdev->mtu, new_mtu);
2220 netdev->mtu = new_mtu;
2221 if (netif_running(netdev))
2222 i40e_vsi_reinit_locked(vsi);
2223
2224 return 0;
2225}
2226
2227/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002228 * i40e_ioctl - Access the hwtstamp interface
2229 * @netdev: network interface device structure
2230 * @ifr: interface request data
2231 * @cmd: ioctl command
2232 **/
2233int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2234{
2235 struct i40e_netdev_priv *np = netdev_priv(netdev);
2236 struct i40e_pf *pf = np->vsi->back;
2237
2238 switch (cmd) {
2239 case SIOCGHWTSTAMP:
2240 return i40e_ptp_get_ts_config(pf, ifr);
2241 case SIOCSHWTSTAMP:
2242 return i40e_ptp_set_ts_config(pf, ifr);
2243 default:
2244 return -EOPNOTSUPP;
2245 }
2246}
2247
2248/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002249 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2250 * @vsi: the vsi being adjusted
2251 **/
2252void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2253{
2254 struct i40e_vsi_context ctxt;
2255 i40e_status ret;
2256
2257 if ((vsi->info.valid_sections &
2258 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2259 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2260 return; /* already enabled */
2261
2262 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2263 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2264 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2265
2266 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002267 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2269 if (ret) {
2270 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002271 "update vlan stripping failed, err %s aq_err %s\n",
2272 i40e_stat_str(&vsi->back->hw, ret),
2273 i40e_aq_str(&vsi->back->hw,
2274 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002275 }
2276}
2277
2278/**
2279 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2280 * @vsi: the vsi being adjusted
2281 **/
2282void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2283{
2284 struct i40e_vsi_context ctxt;
2285 i40e_status ret;
2286
2287 if ((vsi->info.valid_sections &
2288 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2289 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2290 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2291 return; /* already disabled */
2292
2293 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2294 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2295 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2296
2297 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002298 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2300 if (ret) {
2301 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002302 "update vlan stripping failed, err %s aq_err %s\n",
2303 i40e_stat_str(&vsi->back->hw, ret),
2304 i40e_aq_str(&vsi->back->hw,
2305 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002306 }
2307}
2308
2309/**
2310 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2311 * @netdev: network interface to be adjusted
2312 * @features: netdev features to test if VLAN offload is enabled or not
2313 **/
2314static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2315{
2316 struct i40e_netdev_priv *np = netdev_priv(netdev);
2317 struct i40e_vsi *vsi = np->vsi;
2318
2319 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2320 i40e_vlan_stripping_enable(vsi);
2321 else
2322 i40e_vlan_stripping_disable(vsi);
2323}
2324
2325/**
2326 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2327 * @vsi: the vsi being configured
2328 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2329 **/
2330int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2331{
2332 struct i40e_mac_filter *f, *add_f;
2333 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002334
2335 is_vf = (vsi->type == I40E_VSI_SRIOV);
2336 is_netdev = !!(vsi->netdev);
2337
Kiran Patil21659032015-09-30 14:09:03 -04002338 /* Locked once because all functions invoked below iterates list*/
2339 spin_lock_bh(&vsi->mac_filter_list_lock);
2340
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002341 if (is_netdev) {
2342 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2343 is_vf, is_netdev);
2344 if (!add_f) {
2345 dev_info(&vsi->back->pdev->dev,
2346 "Could not add vlan filter %d for %pM\n",
2347 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002348 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002349 return -ENOMEM;
2350 }
2351 }
2352
2353 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2354 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2355 if (!add_f) {
2356 dev_info(&vsi->back->pdev->dev,
2357 "Could not add vlan filter %d for %pM\n",
2358 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002359 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002360 return -ENOMEM;
2361 }
2362 }
2363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 /* Now if we add a vlan tag, make sure to check if it is the first
2365 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2366 * with 0, so we now accept untagged and specified tagged traffic
2367 * (and not any taged and untagged)
2368 */
2369 if (vid > 0) {
2370 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2371 I40E_VLAN_ANY,
2372 is_vf, is_netdev)) {
2373 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2374 I40E_VLAN_ANY, is_vf, is_netdev);
2375 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2376 is_vf, is_netdev);
2377 if (!add_f) {
2378 dev_info(&vsi->back->pdev->dev,
2379 "Could not add filter 0 for %pM\n",
2380 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002381 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002382 return -ENOMEM;
2383 }
2384 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002385 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002386
Greg Rose8d82a7c2014-01-13 16:13:04 -08002387 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2388 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002389 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002390 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2391 is_vf, is_netdev))
2392 continue;
2393 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2394 is_vf, is_netdev);
2395 add_f = i40e_add_filter(vsi, f->macaddr,
2396 0, is_vf, is_netdev);
2397 if (!add_f) {
2398 dev_info(&vsi->back->pdev->dev,
2399 "Could not add filter 0 for %pM\n",
2400 f->macaddr);
2401 spin_unlock_bh(&vsi->mac_filter_list_lock);
2402 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403 }
2404 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405 }
2406
Kiran Patil21659032015-09-30 14:09:03 -04002407 spin_unlock_bh(&vsi->mac_filter_list_lock);
2408
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002409 /* schedule our worker thread which will take care of
2410 * applying the new filter changes
2411 */
2412 i40e_service_event_schedule(vsi->back);
2413 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002414}
2415
2416/**
2417 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2418 * @vsi: the vsi being configured
2419 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002420 *
2421 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002422 **/
2423int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2424{
2425 struct net_device *netdev = vsi->netdev;
2426 struct i40e_mac_filter *f, *add_f;
2427 bool is_vf, is_netdev;
2428 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429
2430 is_vf = (vsi->type == I40E_VSI_SRIOV);
2431 is_netdev = !!(netdev);
2432
Kiran Patil21659032015-09-30 14:09:03 -04002433 /* Locked once because all functions invoked below iterates list */
2434 spin_lock_bh(&vsi->mac_filter_list_lock);
2435
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002436 if (is_netdev)
2437 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2438
2439 list_for_each_entry(f, &vsi->mac_filter_list, list)
2440 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2441
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002442 /* go through all the filters for this VSI and if there is only
2443 * vid == 0 it means there are no other filters, so vid 0 must
2444 * be replaced with -1. This signifies that we should from now
2445 * on accept any traffic (with any tag present, or untagged)
2446 */
2447 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2448 if (is_netdev) {
2449 if (f->vlan &&
2450 ether_addr_equal(netdev->dev_addr, f->macaddr))
2451 filter_count++;
2452 }
2453
2454 if (f->vlan)
2455 filter_count++;
2456 }
2457
2458 if (!filter_count && is_netdev) {
2459 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2460 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2461 is_vf, is_netdev);
2462 if (!f) {
2463 dev_info(&vsi->back->pdev->dev,
2464 "Could not add filter %d for %pM\n",
2465 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002466 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002467 return -ENOMEM;
2468 }
2469 }
2470
2471 if (!filter_count) {
2472 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2473 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2474 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002475 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002476 if (!add_f) {
2477 dev_info(&vsi->back->pdev->dev,
2478 "Could not add filter %d for %pM\n",
2479 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002480 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002481 return -ENOMEM;
2482 }
2483 }
2484 }
2485
Kiran Patil21659032015-09-30 14:09:03 -04002486 spin_unlock_bh(&vsi->mac_filter_list_lock);
2487
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002488 /* schedule our worker thread which will take care of
2489 * applying the new filter changes
2490 */
2491 i40e_service_event_schedule(vsi->back);
2492 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002493}
2494
2495/**
2496 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2497 * @netdev: network interface to be adjusted
2498 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002499 *
2500 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002501 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002502#ifdef I40E_FCOE
2503int i40e_vlan_rx_add_vid(struct net_device *netdev,
2504 __always_unused __be16 proto, u16 vid)
2505#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002506static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2507 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002508#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509{
2510 struct i40e_netdev_priv *np = netdev_priv(netdev);
2511 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002512 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513
2514 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002515 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002517 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2518
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002519 /* If the network stack called us with vid = 0 then
2520 * it is asking to receive priority tagged packets with
2521 * vlan id 0. Our HW receives them by default when configured
2522 * to receive untagged packets so there is no need to add an
2523 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002525 if (vid)
2526 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002527
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002528 if (!ret && (vid < VLAN_N_VID))
2529 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002530
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002531 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532}
2533
2534/**
2535 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2536 * @netdev: network interface to be adjusted
2537 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002538 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002539 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002540 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002541#ifdef I40E_FCOE
2542int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2543 __always_unused __be16 proto, u16 vid)
2544#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002545static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2546 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002547#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002548{
2549 struct i40e_netdev_priv *np = netdev_priv(netdev);
2550 struct i40e_vsi *vsi = np->vsi;
2551
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002552 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2553
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002554 /* return code is ignored as there is nothing a user
2555 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002556 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557 */
2558 i40e_vsi_kill_vlan(vsi, vid);
2559
2560 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002561
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002562 return 0;
2563}
2564
2565/**
2566 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2567 * @vsi: the vsi being brought back up
2568 **/
2569static void i40e_restore_vlan(struct i40e_vsi *vsi)
2570{
2571 u16 vid;
2572
2573 if (!vsi->netdev)
2574 return;
2575
2576 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2577
2578 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2579 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2580 vid);
2581}
2582
2583/**
2584 * i40e_vsi_add_pvid - Add pvid for the VSI
2585 * @vsi: the vsi being adjusted
2586 * @vid: the vlan id to set as a PVID
2587 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002588int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589{
2590 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002591 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592
2593 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2594 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002595 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2596 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002597 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002598
2599 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002600 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002601 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2602 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002603 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002604 "add pvid failed, err %s aq_err %s\n",
2605 i40e_stat_str(&vsi->back->hw, ret),
2606 i40e_aq_str(&vsi->back->hw,
2607 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002608 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609 }
2610
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002611 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002612}
2613
2614/**
2615 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2616 * @vsi: the vsi being adjusted
2617 *
2618 * Just use the vlan_rx_register() service to put it back to normal
2619 **/
2620void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2621{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002622 i40e_vlan_stripping_disable(vsi);
2623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002625}
2626
2627/**
2628 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2629 * @vsi: ptr to the VSI
2630 *
2631 * If this function returns with an error, then it's possible one or
2632 * more of the rings is populated (while the rest are not). It is the
2633 * callers duty to clean those orphaned rings.
2634 *
2635 * Return 0 on success, negative on failure
2636 **/
2637static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2638{
2639 int i, err = 0;
2640
2641 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002642 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002643
2644 return err;
2645}
2646
2647/**
2648 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2649 * @vsi: ptr to the VSI
2650 *
2651 * Free VSI's transmit software resources
2652 **/
2653static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2654{
2655 int i;
2656
Greg Rose8e9dca52013-12-18 13:45:53 +00002657 if (!vsi->tx_rings)
2658 return;
2659
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002661 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002662 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002663}
2664
2665/**
2666 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2667 * @vsi: ptr to the VSI
2668 *
2669 * If this function returns with an error, then it's possible one or
2670 * more of the rings is populated (while the rest are not). It is the
2671 * callers duty to clean those orphaned rings.
2672 *
2673 * Return 0 on success, negative on failure
2674 **/
2675static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2676{
2677 int i, err = 0;
2678
2679 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002680 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002681#ifdef I40E_FCOE
2682 i40e_fcoe_setup_ddp_resources(vsi);
2683#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684 return err;
2685}
2686
2687/**
2688 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2689 * @vsi: ptr to the VSI
2690 *
2691 * Free all receive software resources
2692 **/
2693static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2694{
2695 int i;
2696
Greg Rose8e9dca52013-12-18 13:45:53 +00002697 if (!vsi->rx_rings)
2698 return;
2699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002700 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002701 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002702 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002703#ifdef I40E_FCOE
2704 i40e_fcoe_free_ddp_resources(vsi);
2705#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002706}
2707
2708/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002709 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2710 * @ring: The Tx ring to configure
2711 *
2712 * This enables/disables XPS for a given Tx descriptor ring
2713 * based on the TCs enabled for the VSI that ring belongs to.
2714 **/
2715static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2716{
2717 struct i40e_vsi *vsi = ring->vsi;
2718 cpumask_var_t mask;
2719
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002720 if (!ring->q_vector || !ring->netdev)
2721 return;
2722
2723 /* Single TC mode enable XPS */
2724 if (vsi->tc_config.numtc <= 1) {
2725 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002726 netif_set_xps_queue(ring->netdev,
2727 &ring->q_vector->affinity_mask,
2728 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002729 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2730 /* Disable XPS to allow selection based on TC */
2731 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2732 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2733 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002734 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002735
2736 /* schedule our worker thread which will take care of
2737 * applying the new filter changes
2738 */
2739 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002740}
2741
2742/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002743 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2744 * @ring: The Tx ring to configure
2745 *
2746 * Configure the Tx descriptor ring in the HMC context.
2747 **/
2748static int i40e_configure_tx_ring(struct i40e_ring *ring)
2749{
2750 struct i40e_vsi *vsi = ring->vsi;
2751 u16 pf_q = vsi->base_queue + ring->queue_index;
2752 struct i40e_hw *hw = &vsi->back->hw;
2753 struct i40e_hmc_obj_txq tx_ctx;
2754 i40e_status err = 0;
2755 u32 qtx_ctl = 0;
2756
2757 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002758 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002759 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2760 ring->atr_count = 0;
2761 } else {
2762 ring->atr_sample_rate = 0;
2763 }
2764
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002765 /* configure XPS */
2766 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002767
2768 /* clear the context structure first */
2769 memset(&tx_ctx, 0, sizeof(tx_ctx));
2770
2771 tx_ctx.new_context = 1;
2772 tx_ctx.base = (ring->dma / 128);
2773 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002774 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2775 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002776#ifdef I40E_FCOE
2777 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2778#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002779 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002780 /* FDIR VSI tx ring can still use RS bit and writebacks */
2781 if (vsi->type != I40E_VSI_FDIR)
2782 tx_ctx.head_wb_ena = 1;
2783 tx_ctx.head_wb_addr = ring->dma +
2784 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002785
2786 /* As part of VSI creation/update, FW allocates certain
2787 * Tx arbitration queue sets for each TC enabled for
2788 * the VSI. The FW returns the handles to these queue
2789 * sets as part of the response buffer to Add VSI,
2790 * Update VSI, etc. AQ commands. It is expected that
2791 * these queue set handles be associated with the Tx
2792 * queues by the driver as part of the TX queue context
2793 * initialization. This has to be done regardless of
2794 * DCB as by default everything is mapped to TC0.
2795 */
2796 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2797 tx_ctx.rdylist_act = 0;
2798
2799 /* clear the context in the HMC */
2800 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2801 if (err) {
2802 dev_info(&vsi->back->pdev->dev,
2803 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2804 ring->queue_index, pf_q, err);
2805 return -ENOMEM;
2806 }
2807
2808 /* set the context in the HMC */
2809 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2810 if (err) {
2811 dev_info(&vsi->back->pdev->dev,
2812 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2813 ring->queue_index, pf_q, err);
2814 return -ENOMEM;
2815 }
2816
2817 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002818 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002819 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002820 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2821 I40E_QTX_CTL_VFVM_INDX_MASK;
2822 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002823 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002824 }
2825
Shannon Nelson13fd9772013-09-28 07:14:19 +00002826 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2827 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002828 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2829 i40e_flush(hw);
2830
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831 /* cache tail off for easier writes later */
2832 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2833
2834 return 0;
2835}
2836
2837/**
2838 * i40e_configure_rx_ring - Configure a receive ring context
2839 * @ring: The Rx ring to configure
2840 *
2841 * Configure the Rx descriptor ring in the HMC context.
2842 **/
2843static int i40e_configure_rx_ring(struct i40e_ring *ring)
2844{
2845 struct i40e_vsi *vsi = ring->vsi;
2846 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2847 u16 pf_q = vsi->base_queue + ring->queue_index;
2848 struct i40e_hw *hw = &vsi->back->hw;
2849 struct i40e_hmc_obj_rxq rx_ctx;
2850 i40e_status err = 0;
2851
2852 ring->state = 0;
2853
2854 /* clear the context structure first */
2855 memset(&rx_ctx, 0, sizeof(rx_ctx));
2856
2857 ring->rx_buf_len = vsi->rx_buf_len;
2858 ring->rx_hdr_len = vsi->rx_hdr_len;
2859
2860 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2861 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2862
2863 rx_ctx.base = (ring->dma / 128);
2864 rx_ctx.qlen = ring->count;
2865
2866 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2867 set_ring_16byte_desc_enabled(ring);
2868 rx_ctx.dsize = 0;
2869 } else {
2870 rx_ctx.dsize = 1;
2871 }
2872
2873 rx_ctx.dtype = vsi->dtype;
2874 if (vsi->dtype) {
2875 set_ring_ps_enabled(ring);
2876 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2877 I40E_RX_SPLIT_IP |
2878 I40E_RX_SPLIT_TCP_UDP |
2879 I40E_RX_SPLIT_SCTP;
2880 } else {
2881 rx_ctx.hsplit_0 = 0;
2882 }
2883
2884 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2885 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002886 if (hw->revision_id == 0)
2887 rx_ctx.lrxqthresh = 0;
2888 else
2889 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002890 rx_ctx.crcstrip = 1;
2891 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002892 /* this controls whether VLAN is stripped from inner headers */
2893 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002894#ifdef I40E_FCOE
2895 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2896#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002897 /* set the prefena field to 1 because the manual says to */
2898 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002899
2900 /* clear the context in the HMC */
2901 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2902 if (err) {
2903 dev_info(&vsi->back->pdev->dev,
2904 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2905 ring->queue_index, pf_q, err);
2906 return -ENOMEM;
2907 }
2908
2909 /* set the context in the HMC */
2910 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2911 if (err) {
2912 dev_info(&vsi->back->pdev->dev,
2913 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2914 ring->queue_index, pf_q, err);
2915 return -ENOMEM;
2916 }
2917
2918 /* cache tail for quicker writes, and clear the reg before use */
2919 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2920 writel(0, ring->tail);
2921
Mitch Williamsa132af22015-01-24 09:58:35 +00002922 if (ring_is_ps_enabled(ring)) {
2923 i40e_alloc_rx_headers(ring);
2924 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2925 } else {
2926 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2927 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002928
2929 return 0;
2930}
2931
2932/**
2933 * i40e_vsi_configure_tx - Configure the VSI for Tx
2934 * @vsi: VSI structure describing this set of rings and resources
2935 *
2936 * Configure the Tx VSI for operation.
2937 **/
2938static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2939{
2940 int err = 0;
2941 u16 i;
2942
Alexander Duyck9f65e152013-09-28 06:00:58 +00002943 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2944 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945
2946 return err;
2947}
2948
2949/**
2950 * i40e_vsi_configure_rx - Configure the VSI for Rx
2951 * @vsi: the VSI being configured
2952 *
2953 * Configure the Rx VSI for operation.
2954 **/
2955static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2956{
2957 int err = 0;
2958 u16 i;
2959
2960 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2961 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2962 + ETH_FCS_LEN + VLAN_HLEN;
2963 else
2964 vsi->max_frame = I40E_RXBUFFER_2048;
2965
2966 /* figure out correct receive buffer length */
2967 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2968 I40E_FLAG_RX_PS_ENABLED)) {
2969 case I40E_FLAG_RX_1BUF_ENABLED:
2970 vsi->rx_hdr_len = 0;
2971 vsi->rx_buf_len = vsi->max_frame;
2972 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2973 break;
2974 case I40E_FLAG_RX_PS_ENABLED:
2975 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2976 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2977 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2978 break;
2979 default:
2980 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2981 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2982 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2983 break;
2984 }
2985
Vasu Dev38e00432014-08-01 13:27:03 -07002986#ifdef I40E_FCOE
2987 /* setup rx buffer for FCoE */
2988 if ((vsi->type == I40E_VSI_FCOE) &&
2989 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2990 vsi->rx_hdr_len = 0;
2991 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2992 vsi->max_frame = I40E_RXBUFFER_3072;
2993 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2994 }
2995
2996#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997 /* round up for the chip's needs */
2998 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002999 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003001 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003002
3003 /* set up individual rings */
3004 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003005 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006
3007 return err;
3008}
3009
3010/**
3011 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3012 * @vsi: ptr to the VSI
3013 **/
3014static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3015{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003016 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017 u16 qoffset, qcount;
3018 int i, n;
3019
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003020 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3021 /* Reset the TC information */
3022 for (i = 0; i < vsi->num_queue_pairs; i++) {
3023 rx_ring = vsi->rx_rings[i];
3024 tx_ring = vsi->tx_rings[i];
3025 rx_ring->dcb_tc = 0;
3026 tx_ring->dcb_tc = 0;
3027 }
3028 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003029
3030 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003031 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032 continue;
3033
3034 qoffset = vsi->tc_config.tc_info[n].qoffset;
3035 qcount = vsi->tc_config.tc_info[n].qcount;
3036 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003037 rx_ring = vsi->rx_rings[i];
3038 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003039 rx_ring->dcb_tc = n;
3040 tx_ring->dcb_tc = n;
3041 }
3042 }
3043}
3044
3045/**
3046 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3047 * @vsi: ptr to the VSI
3048 **/
3049static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3050{
3051 if (vsi->netdev)
3052 i40e_set_rx_mode(vsi->netdev);
3053}
3054
3055/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003056 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3057 * @vsi: Pointer to the targeted VSI
3058 *
3059 * This function replays the hlist on the hw where all the SB Flow Director
3060 * filters were saved.
3061 **/
3062static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3063{
3064 struct i40e_fdir_filter *filter;
3065 struct i40e_pf *pf = vsi->back;
3066 struct hlist_node *node;
3067
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003068 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3069 return;
3070
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003071 hlist_for_each_entry_safe(filter, node,
3072 &pf->fdir_filter_list, fdir_node) {
3073 i40e_add_del_fdir(vsi, filter, true);
3074 }
3075}
3076
3077/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003078 * i40e_vsi_configure - Set up the VSI for action
3079 * @vsi: the VSI being configured
3080 **/
3081static int i40e_vsi_configure(struct i40e_vsi *vsi)
3082{
3083 int err;
3084
3085 i40e_set_vsi_rx_mode(vsi);
3086 i40e_restore_vlan(vsi);
3087 i40e_vsi_config_dcb_rings(vsi);
3088 err = i40e_vsi_configure_tx(vsi);
3089 if (!err)
3090 err = i40e_vsi_configure_rx(vsi);
3091
3092 return err;
3093}
3094
3095/**
3096 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3097 * @vsi: the VSI being configured
3098 **/
3099static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3100{
3101 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003102 struct i40e_hw *hw = &pf->hw;
3103 u16 vector;
3104 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003105 u32 qp;
3106
3107 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3108 * and PFINT_LNKLSTn registers, e.g.:
3109 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3110 */
3111 qp = vsi->base_queue;
3112 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003113 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003114 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3115
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003116 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3118 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3119 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3120 q_vector->rx.itr);
3121 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3122 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3123 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3124 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003125 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3126 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127
3128 /* Linked list for the queuepairs assigned to this vector */
3129 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3130 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003131 u32 val;
3132
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003133 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3134 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3135 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3136 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3137 (I40E_QUEUE_TYPE_TX
3138 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3139
3140 wr32(hw, I40E_QINT_RQCTL(qp), val);
3141
3142 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3143 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3144 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3145 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3146 (I40E_QUEUE_TYPE_RX
3147 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3148
3149 /* Terminate the linked list */
3150 if (q == (q_vector->num_ringpairs - 1))
3151 val |= (I40E_QUEUE_END_OF_LIST
3152 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3153
3154 wr32(hw, I40E_QINT_TQCTL(qp), val);
3155 qp++;
3156 }
3157 }
3158
3159 i40e_flush(hw);
3160}
3161
3162/**
3163 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3164 * @hw: ptr to the hardware info
3165 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003166static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003167{
Jacob Kellerab437b52014-12-14 01:55:08 +00003168 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003169 u32 val;
3170
3171 /* clear things first */
3172 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3173 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3174
3175 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3176 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3177 I40E_PFINT_ICR0_ENA_GRST_MASK |
3178 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3179 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003180 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3181 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3182 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3183
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003184 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3185 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3186
Jacob Kellerab437b52014-12-14 01:55:08 +00003187 if (pf->flags & I40E_FLAG_PTP)
3188 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3189
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003190 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3191
3192 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003193 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3194 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003195
3196 /* OTHER_ITR_IDX = 0 */
3197 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3198}
3199
3200/**
3201 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3202 * @vsi: the VSI being configured
3203 **/
3204static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3205{
Alexander Duyck493fb302013-09-28 07:01:44 +00003206 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003207 struct i40e_pf *pf = vsi->back;
3208 struct i40e_hw *hw = &pf->hw;
3209 u32 val;
3210
3211 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003212 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003213 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3214 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3215 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3216 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3217 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3218 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3219
Jacob Kellerab437b52014-12-14 01:55:08 +00003220 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221
3222 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3223 wr32(hw, I40E_PFINT_LNKLST0, 0);
3224
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003225 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3227 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3228 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3229
3230 wr32(hw, I40E_QINT_RQCTL(0), val);
3231
3232 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3233 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3234 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3235
3236 wr32(hw, I40E_QINT_TQCTL(0), val);
3237 i40e_flush(hw);
3238}
3239
3240/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003241 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3242 * @pf: board private structure
3243 **/
3244void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3245{
3246 struct i40e_hw *hw = &pf->hw;
3247
3248 wr32(hw, I40E_PFINT_DYN_CTL0,
3249 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3250 i40e_flush(hw);
3251}
3252
3253/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3255 * @pf: board private structure
3256 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00003257void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258{
3259 struct i40e_hw *hw = &pf->hw;
3260 u32 val;
3261
3262 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3263 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3264 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3265
3266 wr32(hw, I40E_PFINT_DYN_CTL0, 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/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003745 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003746 * @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 **/
5244static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5245{
5246 struct i40e_netdev_priv *np = netdev_priv(netdev);
5247 struct i40e_vsi *vsi = np->vsi;
5248 struct i40e_pf *pf = vsi->back;
5249 u8 enabled_tc = 0;
5250 int ret = -EINVAL;
5251 int i;
5252
5253 /* Check if DCB enabled to continue */
5254 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5255 netdev_info(netdev, "DCB is not enabled for adapter\n");
5256 goto exit;
5257 }
5258
5259 /* Check if MFP enabled */
5260 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5261 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5262 goto exit;
5263 }
5264
5265 /* Check whether tc count is within enabled limit */
5266 if (tc > i40e_pf_get_num_tc(pf)) {
5267 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5268 goto exit;
5269 }
5270
5271 /* Generate TC map for number of tc requested */
5272 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005273 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005274
5275 /* Requesting same TC configuration as already enabled */
5276 if (enabled_tc == vsi->tc_config.enabled_tc)
5277 return 0;
5278
5279 /* Quiesce VSI queues */
5280 i40e_quiesce_vsi(vsi);
5281
5282 /* Configure VSI for enabled TCs */
5283 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5284 if (ret) {
5285 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5286 vsi->seid);
5287 goto exit;
5288 }
5289
5290 /* Unquiesce VSI */
5291 i40e_unquiesce_vsi(vsi);
5292
5293exit:
5294 return ret;
5295}
5296
John Fastabende4c67342016-02-16 21:16:15 -08005297#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005298int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5299 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005300#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005301static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5302 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005303#endif
5304{
John Fastabend16e5cc62016-02-16 21:16:43 -08005305 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005306 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005307 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005308}
5309
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005310/**
5311 * i40e_open - Called when a network interface is made active
5312 * @netdev: network interface device structure
5313 *
5314 * The open entry point is called when a network interface is made
5315 * active by the system (IFF_UP). At this point all resources needed
5316 * for transmit and receive operations are allocated, the interrupt
5317 * handler is registered with the OS, the netdev watchdog subtask is
5318 * enabled, and the stack is notified that the interface is ready.
5319 *
5320 * Returns 0 on success, negative value on failure
5321 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005322int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005323{
5324 struct i40e_netdev_priv *np = netdev_priv(netdev);
5325 struct i40e_vsi *vsi = np->vsi;
5326 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005327 int err;
5328
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005329 /* disallow open during test or if eeprom is broken */
5330 if (test_bit(__I40E_TESTING, &pf->state) ||
5331 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005332 return -EBUSY;
5333
5334 netif_carrier_off(netdev);
5335
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005336 err = i40e_vsi_open(vsi);
5337 if (err)
5338 return err;
5339
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005340 /* configure global TSO hardware offload settings */
5341 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5342 TCP_FLAG_FIN) >> 16);
5343 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5344 TCP_FLAG_FIN |
5345 TCP_FLAG_CWR) >> 16);
5346 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5347
Singhai, Anjali6633d382015-12-03 23:49:31 -08005348#ifdef CONFIG_I40E_VXLAN
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005349 vxlan_get_rx_port(netdev);
5350#endif
Singhai, Anjalicd866602015-12-14 12:21:21 -08005351#ifdef CONFIG_I40E_GENEVE
Anjali Singhaia340c782016-01-06 11:49:28 -08005352 if (pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE)
5353 geneve_get_rx_port(netdev);
Singhai, Anjalicd866602015-12-14 12:21:21 -08005354#endif
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005355
5356 return 0;
5357}
5358
5359/**
5360 * i40e_vsi_open -
5361 * @vsi: the VSI to open
5362 *
5363 * Finish initialization of the VSI.
5364 *
5365 * Returns 0 on success, negative value on failure
5366 **/
5367int i40e_vsi_open(struct i40e_vsi *vsi)
5368{
5369 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005370 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005371 int err;
5372
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005373 /* allocate descriptors */
5374 err = i40e_vsi_setup_tx_resources(vsi);
5375 if (err)
5376 goto err_setup_tx;
5377 err = i40e_vsi_setup_rx_resources(vsi);
5378 if (err)
5379 goto err_setup_rx;
5380
5381 err = i40e_vsi_configure(vsi);
5382 if (err)
5383 goto err_setup_rx;
5384
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005385 if (vsi->netdev) {
5386 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5387 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5388 err = i40e_vsi_request_irq(vsi, int_name);
5389 if (err)
5390 goto err_setup_rx;
5391
5392 /* Notify the stack of the actual queue counts. */
5393 err = netif_set_real_num_tx_queues(vsi->netdev,
5394 vsi->num_queue_pairs);
5395 if (err)
5396 goto err_set_queues;
5397
5398 err = netif_set_real_num_rx_queues(vsi->netdev,
5399 vsi->num_queue_pairs);
5400 if (err)
5401 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005402
5403 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005404 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005405 dev_driver_string(&pf->pdev->dev),
5406 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005407 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005408
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005409 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005410 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005411 goto err_setup_rx;
5412 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005413
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005414 err = i40e_up_complete(vsi);
5415 if (err)
5416 goto err_up_complete;
5417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418 return 0;
5419
5420err_up_complete:
5421 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005422err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005423 i40e_vsi_free_irq(vsi);
5424err_setup_rx:
5425 i40e_vsi_free_rx_resources(vsi);
5426err_setup_tx:
5427 i40e_vsi_free_tx_resources(vsi);
5428 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005429 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005430
5431 return err;
5432}
5433
5434/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005435 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005436 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005437 *
5438 * This function destroys the hlist where all the Flow Director
5439 * filters were saved.
5440 **/
5441static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5442{
5443 struct i40e_fdir_filter *filter;
5444 struct hlist_node *node2;
5445
5446 hlist_for_each_entry_safe(filter, node2,
5447 &pf->fdir_filter_list, fdir_node) {
5448 hlist_del(&filter->fdir_node);
5449 kfree(filter);
5450 }
5451 pf->fdir_pf_active_filters = 0;
5452}
5453
5454/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005455 * i40e_close - Disables a network interface
5456 * @netdev: network interface device structure
5457 *
5458 * The close entry point is called when an interface is de-activated
5459 * by the OS. The hardware is still under the driver's control, but
5460 * this netdev interface is disabled.
5461 *
5462 * Returns 0, this is not allowed to fail
5463 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005464#ifdef I40E_FCOE
5465int i40e_close(struct net_device *netdev)
5466#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005467static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005468#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005469{
5470 struct i40e_netdev_priv *np = netdev_priv(netdev);
5471 struct i40e_vsi *vsi = np->vsi;
5472
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005473 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005474
5475 return 0;
5476}
5477
5478/**
5479 * i40e_do_reset - Start a PF or Core Reset sequence
5480 * @pf: board private structure
5481 * @reset_flags: which reset is requested
5482 *
5483 * The essential difference in resets is that the PF Reset
5484 * doesn't clear the packet buffers, doesn't reset the PE
5485 * firmware, and doesn't bother the other PFs on the chip.
5486 **/
5487void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5488{
5489 u32 val;
5490
5491 WARN_ON(in_interrupt());
5492
Mitch Williams263fc482014-04-23 04:50:11 +00005493 if (i40e_check_asq_alive(&pf->hw))
5494 i40e_vc_notify_reset(pf);
5495
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005496 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005497 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005498
5499 /* Request a Global Reset
5500 *
5501 * This will start the chip's countdown to the actual full
5502 * chip reset event, and a warning interrupt to be sent
5503 * to all PFs, including the requestor. Our handler
5504 * for the warning interrupt will deal with the shutdown
5505 * and recovery of the switch setup.
5506 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005507 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5509 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5510 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5511
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005512 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005513
5514 /* Request a Core Reset
5515 *
5516 * Same as Global Reset, except does *not* include the MAC/PHY
5517 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005518 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005519 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5520 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5521 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5522 i40e_flush(&pf->hw);
5523
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005524 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005525
5526 /* Request a PF Reset
5527 *
5528 * Resets only the PF-specific registers
5529 *
5530 * This goes directly to the tear-down and rebuild of
5531 * the switch, since we need to do all the recovery as
5532 * for the Core Reset.
5533 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005534 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005535 i40e_handle_reset_warning(pf);
5536
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005537 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005538 int v;
5539
5540 /* Find the VSI(s) that requested a re-init */
5541 dev_info(&pf->pdev->dev,
5542 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005543 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005544 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005546 if (vsi != NULL &&
5547 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5548 i40e_vsi_reinit_locked(pf->vsi[v]);
5549 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5550 }
5551 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005552 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005553 int v;
5554
5555 /* Find the VSI(s) that needs to be brought down */
5556 dev_info(&pf->pdev->dev, "VSI down requested\n");
5557 for (v = 0; v < pf->num_alloc_vsi; v++) {
5558 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005559
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005560 if (vsi != NULL &&
5561 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5562 set_bit(__I40E_DOWN, &vsi->state);
5563 i40e_down(vsi);
5564 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5565 }
5566 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005567 } else {
5568 dev_info(&pf->pdev->dev,
5569 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005570 }
5571}
5572
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005573#ifdef CONFIG_I40E_DCB
5574/**
5575 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5576 * @pf: board private structure
5577 * @old_cfg: current DCB config
5578 * @new_cfg: new DCB config
5579 **/
5580bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5581 struct i40e_dcbx_config *old_cfg,
5582 struct i40e_dcbx_config *new_cfg)
5583{
5584 bool need_reconfig = false;
5585
5586 /* Check if ETS configuration has changed */
5587 if (memcmp(&new_cfg->etscfg,
5588 &old_cfg->etscfg,
5589 sizeof(new_cfg->etscfg))) {
5590 /* If Priority Table has changed reconfig is needed */
5591 if (memcmp(&new_cfg->etscfg.prioritytable,
5592 &old_cfg->etscfg.prioritytable,
5593 sizeof(new_cfg->etscfg.prioritytable))) {
5594 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005595 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005596 }
5597
5598 if (memcmp(&new_cfg->etscfg.tcbwtable,
5599 &old_cfg->etscfg.tcbwtable,
5600 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005601 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005602
5603 if (memcmp(&new_cfg->etscfg.tsatable,
5604 &old_cfg->etscfg.tsatable,
5605 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005606 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005607 }
5608
5609 /* Check if PFC configuration has changed */
5610 if (memcmp(&new_cfg->pfc,
5611 &old_cfg->pfc,
5612 sizeof(new_cfg->pfc))) {
5613 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005614 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005615 }
5616
5617 /* Check if APP Table has changed */
5618 if (memcmp(&new_cfg->app,
5619 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005620 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005621 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005622 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005623 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005624
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005625 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005626 return need_reconfig;
5627}
5628
5629/**
5630 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5631 * @pf: board private structure
5632 * @e: event info posted on ARQ
5633 **/
5634static int i40e_handle_lldp_event(struct i40e_pf *pf,
5635 struct i40e_arq_event_info *e)
5636{
5637 struct i40e_aqc_lldp_get_mib *mib =
5638 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5639 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005640 struct i40e_dcbx_config tmp_dcbx_cfg;
5641 bool need_reconfig = false;
5642 int ret = 0;
5643 u8 type;
5644
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005645 /* Not DCB capable or capability disabled */
5646 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5647 return ret;
5648
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005649 /* Ignore if event is not for Nearest Bridge */
5650 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5651 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005652 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005653 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5654 return ret;
5655
5656 /* Check MIB Type and return if event for Remote MIB update */
5657 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005658 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005659 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005660 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5661 /* Update the remote cached instance and return */
5662 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5663 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5664 &hw->remote_dcbx_config);
5665 goto exit;
5666 }
5667
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005668 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005669 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005670
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005671 /* Reset the old DCBx configuration data */
5672 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005673 /* Get updated DCBX data from firmware */
5674 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005675 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005676 dev_info(&pf->pdev->dev,
5677 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5678 i40e_stat_str(&pf->hw, ret),
5679 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005680 goto exit;
5681 }
5682
5683 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005684 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5685 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005686 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005687 goto exit;
5688 }
5689
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005690 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5691 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005692
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005693 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005694
5695 if (!need_reconfig)
5696 goto exit;
5697
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005698 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005699 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005700 pf->flags |= I40E_FLAG_DCB_ENABLED;
5701 else
5702 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5703
Neerav Parikh69129dc2014-11-12 00:18:46 +00005704 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005705 /* Reconfiguration needed quiesce all VSIs */
5706 i40e_pf_quiesce_all_vsi(pf);
5707
5708 /* Changes in configuration update VEB/VSI */
5709 i40e_dcb_reconfigure(pf);
5710
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005711 ret = i40e_resume_port_tx(pf);
5712
Neerav Parikh69129dc2014-11-12 00:18:46 +00005713 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005714 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005715 if (ret)
5716 goto exit;
5717
5718 /* Wait for the PF's Tx queues to be disabled */
5719 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005720 if (ret) {
5721 /* Schedule PF reset to recover */
5722 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5723 i40e_service_event_schedule(pf);
5724 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005725 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005726 }
5727
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005728exit:
5729 return ret;
5730}
5731#endif /* CONFIG_I40E_DCB */
5732
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005733/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005734 * i40e_do_reset_safe - Protected reset path for userland calls.
5735 * @pf: board private structure
5736 * @reset_flags: which reset is requested
5737 *
5738 **/
5739void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5740{
5741 rtnl_lock();
5742 i40e_do_reset(pf, reset_flags);
5743 rtnl_unlock();
5744}
5745
5746/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005747 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5748 * @pf: board private structure
5749 * @e: event info posted on ARQ
5750 *
5751 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5752 * and VF queues
5753 **/
5754static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5755 struct i40e_arq_event_info *e)
5756{
5757 struct i40e_aqc_lan_overflow *data =
5758 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5759 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5760 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5761 struct i40e_hw *hw = &pf->hw;
5762 struct i40e_vf *vf;
5763 u16 vf_id;
5764
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005765 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5766 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005767
5768 /* Queue belongs to VF, find the VF and issue VF reset */
5769 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5770 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5771 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5772 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5773 vf_id -= hw->func_caps.vf_base_id;
5774 vf = &pf->vf[vf_id];
5775 i40e_vc_notify_vf_reset(vf);
5776 /* Allow VF to process pending reset notification */
5777 msleep(20);
5778 i40e_reset_vf(vf, false);
5779 }
5780}
5781
5782/**
5783 * i40e_service_event_complete - Finish up the service event
5784 * @pf: board private structure
5785 **/
5786static void i40e_service_event_complete(struct i40e_pf *pf)
5787{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005788 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005789
5790 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005791 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005792 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5793}
5794
5795/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005796 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5797 * @pf: board private structure
5798 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005799u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005800{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005801 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005802
5803 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5804 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5805 return fcnt_prog;
5806}
5807
5808/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005809 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005810 * @pf: board private structure
5811 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005812u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005813{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005814 u32 val, fcnt_prog;
5815
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005816 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5817 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5818 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5819 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5820 return fcnt_prog;
5821}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005822
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005823/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005824 * i40e_get_global_fd_count - Get total FD filters programmed on device
5825 * @pf: board private structure
5826 **/
5827u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5828{
5829 u32 val, fcnt_prog;
5830
5831 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5832 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5833 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5834 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5835 return fcnt_prog;
5836}
5837
5838/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005839 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5840 * @pf: board private structure
5841 **/
5842void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5843{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005844 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005845 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005846 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005847
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005848 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5849 return;
5850
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005851 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5852 * to re-enable
5853 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005854 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005855 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005856 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5857 (pf->fd_add_err == 0) ||
5858 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005859 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5860 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5861 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005862 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5863 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 +00005864 }
5865 }
5866 /* Wait for some more space to be available to turn on ATR */
5867 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5868 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5869 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5870 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005871 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5872 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 +00005873 }
5874 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005875
5876 /* if hw had a problem adding a filter, delete it */
5877 if (pf->fd_inv > 0) {
5878 hlist_for_each_entry_safe(filter, node,
5879 &pf->fdir_filter_list, fdir_node) {
5880 if (filter->fd_id == pf->fd_inv) {
5881 hlist_del(&filter->fdir_node);
5882 kfree(filter);
5883 pf->fdir_pf_active_filters--;
5884 }
5885 }
5886 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005887}
5888
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005889#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005890#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005891/**
5892 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5893 * @pf: board private structure
5894 **/
5895static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5896{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005897 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005898 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005899 bool disable_atr = false;
5900 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005901 int reg;
5902
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005903 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5904 return;
5905
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005906 if (!time_after(jiffies, pf->fd_flush_timestamp +
5907 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5908 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005909
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005910 /* If the flush is happening too quick and we have mostly SB rules we
5911 * should not re-enable ATR for some time.
5912 */
5913 min_flush_time = pf->fd_flush_timestamp +
5914 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5915 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005916
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005917 if (!(time_after(jiffies, min_flush_time)) &&
5918 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5919 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5920 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5921 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005922 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005923
5924 pf->fd_flush_timestamp = jiffies;
5925 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5926 /* flush all filters */
5927 wr32(&pf->hw, I40E_PFQF_CTL_1,
5928 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5929 i40e_flush(&pf->hw);
5930 pf->fd_flush_cnt++;
5931 pf->fd_add_err = 0;
5932 do {
5933 /* Check FD flush status every 5-6msec */
5934 usleep_range(5000, 6000);
5935 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5936 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5937 break;
5938 } while (flush_wait_retry--);
5939 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5940 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5941 } else {
5942 /* replay sideband filters */
5943 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5944 if (!disable_atr)
5945 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5946 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5947 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5948 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5949 }
5950
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005951}
5952
5953/**
5954 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5955 * @pf: board private structure
5956 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005957u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005958{
5959 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5960}
5961
5962/* We can see up to 256 filter programming desc in transit if the filters are
5963 * being applied really fast; before we see the first
5964 * filter miss error on Rx queue 0. Accumulating enough error messages before
5965 * reacting will make sure we don't cause flush too often.
5966 */
5967#define I40E_MAX_FD_PROGRAM_ERROR 256
5968
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005969/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005970 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5971 * @pf: board private structure
5972 **/
5973static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5974{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005975
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005976 /* if interface is down do nothing */
5977 if (test_bit(__I40E_DOWN, &pf->state))
5978 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005979
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005980 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5981 return;
5982
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005983 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005984 i40e_fdir_flush_and_replay(pf);
5985
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005986 i40e_fdir_check_and_reenable(pf);
5987
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005988}
5989
5990/**
5991 * i40e_vsi_link_event - notify VSI of a link event
5992 * @vsi: vsi to be notified
5993 * @link_up: link up or down
5994 **/
5995static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
5996{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00005997 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005998 return;
5999
6000 switch (vsi->type) {
6001 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006002#ifdef I40E_FCOE
6003 case I40E_VSI_FCOE:
6004#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006005 if (!vsi->netdev || !vsi->netdev_registered)
6006 break;
6007
6008 if (link_up) {
6009 netif_carrier_on(vsi->netdev);
6010 netif_tx_wake_all_queues(vsi->netdev);
6011 } else {
6012 netif_carrier_off(vsi->netdev);
6013 netif_tx_stop_all_queues(vsi->netdev);
6014 }
6015 break;
6016
6017 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006018 case I40E_VSI_VMDQ2:
6019 case I40E_VSI_CTRL:
6020 case I40E_VSI_MIRROR:
6021 default:
6022 /* there is no notification for other VSIs */
6023 break;
6024 }
6025}
6026
6027/**
6028 * i40e_veb_link_event - notify elements on the veb of a link event
6029 * @veb: veb to be notified
6030 * @link_up: link up or down
6031 **/
6032static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6033{
6034 struct i40e_pf *pf;
6035 int i;
6036
6037 if (!veb || !veb->pf)
6038 return;
6039 pf = veb->pf;
6040
6041 /* depth first... */
6042 for (i = 0; i < I40E_MAX_VEB; i++)
6043 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6044 i40e_veb_link_event(pf->veb[i], link_up);
6045
6046 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006047 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006048 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6049 i40e_vsi_link_event(pf->vsi[i], link_up);
6050}
6051
6052/**
6053 * i40e_link_event - Update netif_carrier status
6054 * @pf: board private structure
6055 **/
6056static void i40e_link_event(struct i40e_pf *pf)
6057{
Mitch Williams320684c2014-10-17 03:14:43 +00006058 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006059 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006060 i40e_status status;
6061 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006062
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006063 /* save off old link status information */
6064 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6065
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006066 /* set this to force the get_link_status call to refresh state */
6067 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006068
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006069 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006070
6071 status = i40e_get_link_status(&pf->hw, &new_link);
6072 if (status) {
6073 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6074 status);
6075 return;
6076 }
6077
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006078 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6079 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006080
6081 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006082 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006083 (test_bit(__I40E_DOWN, &vsi->state) ||
6084 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006085 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006086
6087 if (!test_bit(__I40E_DOWN, &vsi->state))
6088 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006089
6090 /* Notify the base of the switch tree connected to
6091 * the link. Floating VEBs are not notified.
6092 */
6093 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6094 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6095 else
Mitch Williams320684c2014-10-17 03:14:43 +00006096 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006097
6098 if (pf->vf)
6099 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006100
6101 if (pf->flags & I40E_FLAG_PTP)
6102 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006103}
6104
6105/**
Shannon Nelson21536712014-10-25 10:35:25 +00006106 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006107 * @pf: board private structure
6108 **/
6109static void i40e_watchdog_subtask(struct i40e_pf *pf)
6110{
6111 int i;
6112
6113 /* if interface is down do nothing */
6114 if (test_bit(__I40E_DOWN, &pf->state) ||
6115 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6116 return;
6117
Shannon Nelson21536712014-10-25 10:35:25 +00006118 /* make sure we don't do these things too often */
6119 if (time_before(jiffies, (pf->service_timer_previous +
6120 pf->service_timer_period)))
6121 return;
6122 pf->service_timer_previous = jiffies;
6123
Shannon Nelson9ac77262015-08-27 11:42:40 -04006124 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6125 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006126
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006127 /* Update the stats for active netdevs so the network stack
6128 * can look at updated numbers whenever it cares to
6129 */
Mitch Williams505682c2014-05-20 08:01:37 +00006130 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006131 if (pf->vsi[i] && pf->vsi[i]->netdev)
6132 i40e_update_stats(pf->vsi[i]);
6133
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006134 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6135 /* Update the stats for the active switching components */
6136 for (i = 0; i < I40E_MAX_VEB; i++)
6137 if (pf->veb[i])
6138 i40e_update_veb_stats(pf->veb[i]);
6139 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006140
6141 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006142}
6143
6144/**
6145 * i40e_reset_subtask - Set up for resetting the device and driver
6146 * @pf: board private structure
6147 **/
6148static void i40e_reset_subtask(struct i40e_pf *pf)
6149{
6150 u32 reset_flags = 0;
6151
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006152 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006153 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006154 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006155 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6156 }
6157 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006158 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6160 }
6161 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006162 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6164 }
6165 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006166 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006167 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6168 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006169 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006170 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006171 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6172 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006173
6174 /* If there's a recovery already waiting, it takes
6175 * precedence before starting a new reset sequence.
6176 */
6177 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6178 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006179 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006180 }
6181
6182 /* If we're already down or resetting, just bail */
6183 if (reset_flags &&
6184 !test_bit(__I40E_DOWN, &pf->state) &&
6185 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6186 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006187
6188unlock:
6189 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006190}
6191
6192/**
6193 * i40e_handle_link_event - Handle link event
6194 * @pf: board private structure
6195 * @e: event info posted on ARQ
6196 **/
6197static void i40e_handle_link_event(struct i40e_pf *pf,
6198 struct i40e_arq_event_info *e)
6199{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006200 struct i40e_aqc_get_link_status *status =
6201 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006202
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006203 /* Do a new status request to re-enable LSE reporting
6204 * and load new status information into the hw struct
6205 * This completely ignores any state information
6206 * in the ARQ event info, instead choosing to always
6207 * issue the AQ update link status command.
6208 */
6209 i40e_link_event(pf);
6210
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006211 /* check for unqualified module, if link is down */
6212 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6213 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6214 (!(status->link_info & I40E_AQ_LINK_UP)))
6215 dev_err(&pf->pdev->dev,
6216 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217}
6218
6219/**
6220 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6221 * @pf: board private structure
6222 **/
6223static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6224{
6225 struct i40e_arq_event_info event;
6226 struct i40e_hw *hw = &pf->hw;
6227 u16 pending, i = 0;
6228 i40e_status ret;
6229 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006230 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006231 u32 val;
6232
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006233 /* Do not run clean AQ when PF reset fails */
6234 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6235 return;
6236
Shannon Nelson86df2422014-05-20 08:01:35 +00006237 /* check for error indications */
6238 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6239 oldval = val;
6240 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006241 if (hw->debug_mask & I40E_DEBUG_AQ)
6242 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006243 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6244 }
6245 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006246 if (hw->debug_mask & I40E_DEBUG_AQ)
6247 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006248 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006249 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006250 }
6251 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006252 if (hw->debug_mask & I40E_DEBUG_AQ)
6253 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006254 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6255 }
6256 if (oldval != val)
6257 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6258
6259 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6260 oldval = val;
6261 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006262 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6263 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006264 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6265 }
6266 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006267 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6268 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006269 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6270 }
6271 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006272 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6273 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006274 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6275 }
6276 if (oldval != val)
6277 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6278
Mitch Williams1001dc32014-11-11 20:02:19 +00006279 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6280 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006281 if (!event.msg_buf)
6282 return;
6283
6284 do {
6285 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006286 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287 break;
Mitch Williams56497972014-06-04 08:45:18 +00006288 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6290 break;
6291 }
6292
6293 opcode = le16_to_cpu(event.desc.opcode);
6294 switch (opcode) {
6295
6296 case i40e_aqc_opc_get_link_status:
6297 i40e_handle_link_event(pf, &event);
6298 break;
6299 case i40e_aqc_opc_send_msg_to_pf:
6300 ret = i40e_vc_process_vf_msg(pf,
6301 le16_to_cpu(event.desc.retval),
6302 le32_to_cpu(event.desc.cookie_high),
6303 le32_to_cpu(event.desc.cookie_low),
6304 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006305 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006306 break;
6307 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006308 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006309#ifdef CONFIG_I40E_DCB
6310 rtnl_lock();
6311 ret = i40e_handle_lldp_event(pf, &event);
6312 rtnl_unlock();
6313#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 break;
6315 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006316 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006317 i40e_handle_lan_overflow_event(pf, &event);
6318 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006319 case i40e_aqc_opc_send_msg_to_peer:
6320 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6321 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006322 case i40e_aqc_opc_nvm_erase:
6323 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006324 case i40e_aqc_opc_oem_post_update:
Shannon Nelson91a0f932015-03-19 14:32:01 -07006325 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
6326 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006327 default:
6328 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00006329 "ARQ Error: Unknown event 0x%04x received\n",
6330 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 break;
6332 }
6333 } while (pending && (i++ < pf->adminq_work_limit));
6334
6335 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6336 /* re-enable Admin queue interrupt cause */
6337 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6338 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6339 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6340 i40e_flush(hw);
6341
6342 kfree(event.msg_buf);
6343}
6344
6345/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006346 * i40e_verify_eeprom - make sure eeprom is good to use
6347 * @pf: board private structure
6348 **/
6349static void i40e_verify_eeprom(struct i40e_pf *pf)
6350{
6351 int err;
6352
6353 err = i40e_diag_eeprom_test(&pf->hw);
6354 if (err) {
6355 /* retry in case of garbage read */
6356 err = i40e_diag_eeprom_test(&pf->hw);
6357 if (err) {
6358 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6359 err);
6360 set_bit(__I40E_BAD_EEPROM, &pf->state);
6361 }
6362 }
6363
6364 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6365 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6366 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6367 }
6368}
6369
6370/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006371 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006372 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006373 *
6374 * enable switch loop back or die - no point in a return value
6375 **/
6376static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6377{
6378 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6379 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006380 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006381
6382 ctxt.seid = pf->main_vsi_seid;
6383 ctxt.pf_num = pf->hw.pf_id;
6384 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006385 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6386 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006387 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006388 "couldn't get PF vsi config, err %s aq_err %s\n",
6389 i40e_stat_str(&pf->hw, ret),
6390 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006391 return;
6392 }
6393 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6394 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6395 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6396
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006397 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6398 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006399 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006400 "update vsi switch failed, err %s aq_err %s\n",
6401 i40e_stat_str(&pf->hw, ret),
6402 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006403 }
6404}
6405
6406/**
6407 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006408 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006409 *
6410 * disable switch loop back or die - no point in a return value
6411 **/
6412static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6413{
6414 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6415 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006416 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006417
6418 ctxt.seid = pf->main_vsi_seid;
6419 ctxt.pf_num = pf->hw.pf_id;
6420 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006421 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6422 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006423 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006424 "couldn't get PF vsi config, err %s aq_err %s\n",
6425 i40e_stat_str(&pf->hw, ret),
6426 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006427 return;
6428 }
6429 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6430 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6431 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6432
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006433 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6434 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006435 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006436 "update vsi switch failed, err %s aq_err %s\n",
6437 i40e_stat_str(&pf->hw, ret),
6438 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006439 }
6440}
6441
6442/**
Neerav Parikh51616012015-02-06 08:52:14 +00006443 * i40e_config_bridge_mode - Configure the HW bridge mode
6444 * @veb: pointer to the bridge instance
6445 *
6446 * Configure the loop back mode for the LAN VSI that is downlink to the
6447 * specified HW bridge instance. It is expected this function is called
6448 * when a new HW bridge is instantiated.
6449 **/
6450static void i40e_config_bridge_mode(struct i40e_veb *veb)
6451{
6452 struct i40e_pf *pf = veb->pf;
6453
Shannon Nelson6dec1012015-09-28 14:12:30 -04006454 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6455 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6456 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006457 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6458 i40e_disable_pf_switch_lb(pf);
6459 else
6460 i40e_enable_pf_switch_lb(pf);
6461}
6462
6463/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006464 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6465 * @veb: pointer to the VEB instance
6466 *
6467 * This is a recursive function that first builds the attached VSIs then
6468 * recurses in to build the next layer of VEB. We track the connections
6469 * through our own index numbers because the seid's from the HW could
6470 * change across the reset.
6471 **/
6472static int i40e_reconstitute_veb(struct i40e_veb *veb)
6473{
6474 struct i40e_vsi *ctl_vsi = NULL;
6475 struct i40e_pf *pf = veb->pf;
6476 int v, veb_idx;
6477 int ret;
6478
6479 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006480 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006481 if (pf->vsi[v] &&
6482 pf->vsi[v]->veb_idx == veb->idx &&
6483 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6484 ctl_vsi = pf->vsi[v];
6485 break;
6486 }
6487 }
6488 if (!ctl_vsi) {
6489 dev_info(&pf->pdev->dev,
6490 "missing owner VSI for veb_idx %d\n", veb->idx);
6491 ret = -ENOENT;
6492 goto end_reconstitute;
6493 }
6494 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6495 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6496 ret = i40e_add_vsi(ctl_vsi);
6497 if (ret) {
6498 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006499 "rebuild of veb_idx %d owner VSI failed: %d\n",
6500 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501 goto end_reconstitute;
6502 }
6503 i40e_vsi_reset_stats(ctl_vsi);
6504
6505 /* create the VEB in the switch and move the VSI onto the VEB */
6506 ret = i40e_add_veb(veb, ctl_vsi);
6507 if (ret)
6508 goto end_reconstitute;
6509
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006510 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6511 veb->bridge_mode = BRIDGE_MODE_VEB;
6512 else
6513 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006514 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006515
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006517 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006518 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6519 continue;
6520
6521 if (pf->vsi[v]->veb_idx == veb->idx) {
6522 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006523
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006524 vsi->uplink_seid = veb->seid;
6525 ret = i40e_add_vsi(vsi);
6526 if (ret) {
6527 dev_info(&pf->pdev->dev,
6528 "rebuild of vsi_idx %d failed: %d\n",
6529 v, ret);
6530 goto end_reconstitute;
6531 }
6532 i40e_vsi_reset_stats(vsi);
6533 }
6534 }
6535
6536 /* create any VEBs attached to this VEB - RECURSION */
6537 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6538 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6539 pf->veb[veb_idx]->uplink_seid = veb->seid;
6540 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6541 if (ret)
6542 break;
6543 }
6544 }
6545
6546end_reconstitute:
6547 return ret;
6548}
6549
6550/**
6551 * i40e_get_capabilities - get info about the HW
6552 * @pf: the PF struct
6553 **/
6554static int i40e_get_capabilities(struct i40e_pf *pf)
6555{
6556 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6557 u16 data_size;
6558 int buf_len;
6559 int err;
6560
6561 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6562 do {
6563 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6564 if (!cap_buf)
6565 return -ENOMEM;
6566
6567 /* this loads the data into the hw struct for us */
6568 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6569 &data_size,
6570 i40e_aqc_opc_list_func_capabilities,
6571 NULL);
6572 /* data loaded, buffer no longer needed */
6573 kfree(cap_buf);
6574
6575 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6576 /* retry with a larger buffer */
6577 buf_len = data_size;
6578 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6579 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006580 "capability discovery failed, err %s aq_err %s\n",
6581 i40e_stat_str(&pf->hw, err),
6582 i40e_aq_str(&pf->hw,
6583 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006584 return -ENODEV;
6585 }
6586 } while (err);
6587
6588 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6589 dev_info(&pf->pdev->dev,
6590 "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",
6591 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6592 pf->hw.func_caps.num_msix_vectors,
6593 pf->hw.func_caps.num_msix_vectors_vf,
6594 pf->hw.func_caps.fd_filters_guaranteed,
6595 pf->hw.func_caps.fd_filters_best_effort,
6596 pf->hw.func_caps.num_tx_qp,
6597 pf->hw.func_caps.num_vsis);
6598
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006599#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6600 + pf->hw.func_caps.num_vfs)
6601 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6602 dev_info(&pf->pdev->dev,
6603 "got num_vsis %d, setting num_vsis to %d\n",
6604 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6605 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6606 }
6607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006608 return 0;
6609}
6610
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006611static int i40e_vsi_clear(struct i40e_vsi *vsi);
6612
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006613/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006614 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006615 * @pf: board private structure
6616 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006617static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006618{
6619 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006620 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006621
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006622 /* quick workaround for an NVM issue that leaves a critical register
6623 * uninitialized
6624 */
6625 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6626 static const u32 hkey[] = {
6627 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6628 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6629 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6630 0x95b3a76d};
6631
6632 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6633 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6634 }
6635
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006636 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006637 return;
6638
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006639 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006640 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006641 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006642 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006643 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006644 break;
6645 }
6646 }
6647
6648 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006649 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006650 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6651 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006652 if (!vsi) {
6653 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006654 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6655 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006656 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006657 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006658
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006659 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006660}
6661
6662/**
6663 * i40e_fdir_teardown - release the Flow Director resources
6664 * @pf: board private structure
6665 **/
6666static void i40e_fdir_teardown(struct i40e_pf *pf)
6667{
6668 int i;
6669
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006670 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006671 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006672 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6673 i40e_vsi_release(pf->vsi[i]);
6674 break;
6675 }
6676 }
6677}
6678
6679/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006680 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006681 * @pf: board private structure
6682 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006683 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006684 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006685static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006686{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006687 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006688 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689 u32 v;
6690
6691 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6692 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006693 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006694
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006695 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006697 /* quiesce the VSIs and their queues that are not already DOWN */
6698 i40e_pf_quiesce_all_vsi(pf);
6699
Mitch Williams505682c2014-05-20 08:01:37 +00006700 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 if (pf->vsi[v])
6702 pf->vsi[v]->seid = 0;
6703 }
6704
6705 i40e_shutdown_adminq(&pf->hw);
6706
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006707 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006708 if (hw->hmc.hmc_obj) {
6709 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006710 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006711 dev_warn(&pf->pdev->dev,
6712 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006713 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006714}
6715
6716/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006717 * i40e_send_version - update firmware with driver version
6718 * @pf: PF struct
6719 */
6720static void i40e_send_version(struct i40e_pf *pf)
6721{
6722 struct i40e_driver_version dv;
6723
6724 dv.major_version = DRV_VERSION_MAJOR;
6725 dv.minor_version = DRV_VERSION_MINOR;
6726 dv.build_version = DRV_VERSION_BUILD;
6727 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006728 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006729 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6730}
6731
6732/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006733 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006734 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006735 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006736 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006737static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006738{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006739 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006740 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006741 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006742 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006743 u32 v;
6744
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006745 /* Now we wait for GRST to settle out.
6746 * We don't have to delete the VEBs or VSIs from the hw switch
6747 * because the reset will make them disappear.
6748 */
6749 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006750 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006751 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006752 set_bit(__I40E_RESET_FAILED, &pf->state);
6753 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006754 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006755 pf->pfr_count++;
6756
6757 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006758 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006759 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006760
6761 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6762 ret = i40e_init_adminq(&pf->hw);
6763 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006764 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6765 i40e_stat_str(&pf->hw, ret),
6766 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006767 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006768 }
6769
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006770 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006771 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006772 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006773
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006774 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006775 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006776 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006777 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006778
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6780 hw->func_caps.num_rx_qp,
6781 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6782 if (ret) {
6783 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6784 goto end_core_reset;
6785 }
6786 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6787 if (ret) {
6788 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6789 goto end_core_reset;
6790 }
6791
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006792#ifdef CONFIG_I40E_DCB
6793 ret = i40e_init_pf_dcb(pf);
6794 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006795 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6796 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6797 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006798 }
6799#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006800#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006801 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006802
Vasu Dev38e00432014-08-01 13:27:03 -07006803#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006805 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006806 if (ret)
6807 goto end_core_reset;
6808
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006809 /* The driver only wants link up/down and module qualification
6810 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006811 */
6812 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006813 ~(I40E_AQ_EVENT_LINK_UPDOWN |
6814 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006815 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006816 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6817 i40e_stat_str(&pf->hw, ret),
6818 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006819
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006820 /* make sure our flow control settings are restored */
6821 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6822 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006823 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6824 i40e_stat_str(&pf->hw, ret),
6825 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006826
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006827 /* Rebuild the VSIs and VEBs that existed before reset.
6828 * They are still in our local switch element arrays, so only
6829 * need to rebuild the switch model in the HW.
6830 *
6831 * If there were VEBs but the reconstitution failed, we'll try
6832 * try to recover minimal use by getting the basic PF VSI working.
6833 */
6834 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006835 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006836 /* find the one VEB connected to the MAC, and find orphans */
6837 for (v = 0; v < I40E_MAX_VEB; v++) {
6838 if (!pf->veb[v])
6839 continue;
6840
6841 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6842 pf->veb[v]->uplink_seid == 0) {
6843 ret = i40e_reconstitute_veb(pf->veb[v]);
6844
6845 if (!ret)
6846 continue;
6847
6848 /* If Main VEB failed, we're in deep doodoo,
6849 * so give up rebuilding the switch and set up
6850 * for minimal rebuild of PF VSI.
6851 * If orphan failed, we'll report the error
6852 * but try to keep going.
6853 */
6854 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6855 dev_info(&pf->pdev->dev,
6856 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6857 ret);
6858 pf->vsi[pf->lan_vsi]->uplink_seid
6859 = pf->mac_seid;
6860 break;
6861 } else if (pf->veb[v]->uplink_seid == 0) {
6862 dev_info(&pf->pdev->dev,
6863 "rebuild of orphan VEB failed: %d\n",
6864 ret);
6865 }
6866 }
6867 }
6868 }
6869
6870 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006871 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872 /* no VEB, so rebuild only the Main VSI */
6873 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6874 if (ret) {
6875 dev_info(&pf->pdev->dev,
6876 "rebuild of Main VSI failed: %d\n", ret);
6877 goto end_core_reset;
6878 }
6879 }
6880
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006881 /* Reconfigure hardware for allowing smaller MSS in the case
6882 * of TSO, so that we avoid the MDD being fired and causing
6883 * a reset in the case of small MSS+TSO.
6884 */
6885#define I40E_REG_MSS 0x000E64DC
6886#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6887#define I40E_64BYTE_MSS 0x400000
6888 val = rd32(hw, I40E_REG_MSS);
6889 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6890 val &= ~I40E_REG_MSS_MIN_MASK;
6891 val |= I40E_64BYTE_MSS;
6892 wr32(hw, I40E_REG_MSS, val);
6893 }
6894
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006895 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006896 msleep(75);
6897 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6898 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006899 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6900 i40e_stat_str(&pf->hw, ret),
6901 i40e_aq_str(&pf->hw,
6902 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006903 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006904 /* reinit the misc interrupt */
6905 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6906 ret = i40e_setup_misc_vector(pf);
6907
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006908 /* Add a filter to drop all Flow control frames from any VSI from being
6909 * transmitted. By doing so we stop a malicious VF from sending out
6910 * PAUSE or PFC frames and potentially controlling traffic for other
6911 * PF/VF VSIs.
6912 * The FW can still send Flow control frames if enabled.
6913 */
6914 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6915 pf->main_vsi_seid);
6916
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006917 /* restart the VSIs that were rebuilt and running before the reset */
6918 i40e_pf_unquiesce_all_vsi(pf);
6919
Mitch Williams69f64b22014-02-13 03:48:46 -08006920 if (pf->num_alloc_vfs) {
6921 for (v = 0; v < pf->num_alloc_vfs; v++)
6922 i40e_reset_vf(&pf->vf[v], true);
6923 }
6924
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006925 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006926 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006927
6928end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006929 clear_bit(__I40E_RESET_FAILED, &pf->state);
6930clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006931 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6932}
6933
6934/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006935 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006936 * @pf: board private structure
6937 *
6938 * Close up the VFs and other things in prep for a Core Reset,
6939 * then get ready to rebuild the world.
6940 **/
6941static void i40e_handle_reset_warning(struct i40e_pf *pf)
6942{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006943 i40e_prep_for_reset(pf);
6944 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006945}
6946
6947/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006949 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006950 *
6951 * Called from the MDD irq handler to identify possibly malicious vfs
6952 **/
6953static void i40e_handle_mdd_event(struct i40e_pf *pf)
6954{
6955 struct i40e_hw *hw = &pf->hw;
6956 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006957 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958 struct i40e_vf *vf;
6959 u32 reg;
6960 int i;
6961
6962 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6963 return;
6964
6965 /* find what triggered the MDD event */
6966 reg = rd32(hw, I40E_GL_MDET_TX);
6967 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006968 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6969 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006970 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006971 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006972 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006973 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006974 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6975 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6976 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006977 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006978 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 +00006979 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006980 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6981 mdd_detected = true;
6982 }
6983 reg = rd32(hw, I40E_GL_MDET_RX);
6984 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006985 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6986 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006987 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006988 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006989 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6990 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6991 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006992 if (netif_msg_rx_err(pf))
6993 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6994 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006995 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
6996 mdd_detected = true;
6997 }
6998
Neerav Parikhdf430b12014-06-04 01:23:15 +00006999 if (mdd_detected) {
7000 reg = rd32(hw, I40E_PF_MDET_TX);
7001 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7002 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007003 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007004 pf_mdd_detected = true;
7005 }
7006 reg = rd32(hw, I40E_PF_MDET_RX);
7007 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7008 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007009 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007010 pf_mdd_detected = true;
7011 }
7012 /* Queue belongs to the PF, initiate a reset */
7013 if (pf_mdd_detected) {
7014 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7015 i40e_service_event_schedule(pf);
7016 }
7017 }
7018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019 /* see if one of the VFs needs its hand slapped */
7020 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7021 vf = &(pf->vf[i]);
7022 reg = rd32(hw, I40E_VP_MDET_TX(i));
7023 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7024 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7025 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007026 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7027 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007028 }
7029
7030 reg = rd32(hw, I40E_VP_MDET_RX(i));
7031 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7032 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7033 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007034 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7035 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007036 }
7037
7038 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7039 dev_info(&pf->pdev->dev,
7040 "Too many MDD events on VF %d, disabled\n", i);
7041 dev_info(&pf->pdev->dev,
7042 "Use PF Control I/F to re-enable the VF\n");
7043 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7044 }
7045 }
7046
7047 /* re-enable mdd interrupt cause */
7048 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7049 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7050 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7051 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7052 i40e_flush(hw);
7053}
7054
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007055/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007056 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007057 * @pf: board private structure
7058 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007059static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007060{
Singhai, Anjali6a899022015-12-14 12:21:18 -08007061#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007062 struct i40e_hw *hw = &pf->hw;
7063 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007064 __be16 port;
7065 int i;
7066
Singhai, Anjali6a899022015-12-14 12:21:18 -08007067 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007068 return;
7069
Singhai, Anjali6a899022015-12-14 12:21:18 -08007070 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007071
7072 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007073 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7074 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7075 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007076 if (port)
7077 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Singhai, Anjali6a899022015-12-14 12:21:18 -08007078 pf->udp_ports[i].type,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007079 NULL, NULL);
7080 else
7081 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007082
7083 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007084 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007085 "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007086 port ? "add" : "delete",
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007087 ntohs(port), i,
7088 i40e_stat_str(&pf->hw, ret),
7089 i40e_aq_str(&pf->hw,
7090 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007091 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007092 }
7093 }
7094 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08007095#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007096}
7097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007098/**
7099 * i40e_service_task - Run the driver's async subtasks
7100 * @work: pointer to work_struct containing our data
7101 **/
7102static void i40e_service_task(struct work_struct *work)
7103{
7104 struct i40e_pf *pf = container_of(work,
7105 struct i40e_pf,
7106 service_task);
7107 unsigned long start_time = jiffies;
7108
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007109 /* don't bother with service tasks if a reset is in progress */
7110 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7111 i40e_service_event_complete(pf);
7112 return;
7113 }
7114
Kiran Patilb03a8c12015-09-24 18:13:15 -04007115 i40e_detect_recover_hung(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007116 i40e_reset_subtask(pf);
7117 i40e_handle_mdd_event(pf);
7118 i40e_vc_process_vflr_event(pf);
7119 i40e_watchdog_subtask(pf);
7120 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007121 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007122 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007123 i40e_clean_adminq_subtask(pf);
7124
7125 i40e_service_event_complete(pf);
7126
7127 /* If the tasks have taken longer than one timer cycle or there
7128 * is more work to be done, reschedule the service task now
7129 * rather than wait for the timer to tick again.
7130 */
7131 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7132 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7133 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7134 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7135 i40e_service_event_schedule(pf);
7136}
7137
7138/**
7139 * i40e_service_timer - timer callback
7140 * @data: pointer to PF struct
7141 **/
7142static void i40e_service_timer(unsigned long data)
7143{
7144 struct i40e_pf *pf = (struct i40e_pf *)data;
7145
7146 mod_timer(&pf->service_timer,
7147 round_jiffies(jiffies + pf->service_timer_period));
7148 i40e_service_event_schedule(pf);
7149}
7150
7151/**
7152 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7153 * @vsi: the VSI being configured
7154 **/
7155static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7156{
7157 struct i40e_pf *pf = vsi->back;
7158
7159 switch (vsi->type) {
7160 case I40E_VSI_MAIN:
7161 vsi->alloc_queue_pairs = pf->num_lan_qps;
7162 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7163 I40E_REQ_DESCRIPTOR_MULTIPLE);
7164 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7165 vsi->num_q_vectors = pf->num_lan_msix;
7166 else
7167 vsi->num_q_vectors = 1;
7168
7169 break;
7170
7171 case I40E_VSI_FDIR:
7172 vsi->alloc_queue_pairs = 1;
7173 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7174 I40E_REQ_DESCRIPTOR_MULTIPLE);
7175 vsi->num_q_vectors = 1;
7176 break;
7177
7178 case I40E_VSI_VMDQ2:
7179 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7180 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7181 I40E_REQ_DESCRIPTOR_MULTIPLE);
7182 vsi->num_q_vectors = pf->num_vmdq_msix;
7183 break;
7184
7185 case I40E_VSI_SRIOV:
7186 vsi->alloc_queue_pairs = pf->num_vf_qps;
7187 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7188 I40E_REQ_DESCRIPTOR_MULTIPLE);
7189 break;
7190
Vasu Dev38e00432014-08-01 13:27:03 -07007191#ifdef I40E_FCOE
7192 case I40E_VSI_FCOE:
7193 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7194 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7195 I40E_REQ_DESCRIPTOR_MULTIPLE);
7196 vsi->num_q_vectors = pf->num_fcoe_msix;
7197 break;
7198
7199#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007200 default:
7201 WARN_ON(1);
7202 return -ENODATA;
7203 }
7204
7205 return 0;
7206}
7207
7208/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007209 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7210 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007211 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007212 *
7213 * On error: returns error code (negative)
7214 * On success: returns 0
7215 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007216static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007217{
7218 int size;
7219 int ret = 0;
7220
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007221 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007222 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7223 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7224 if (!vsi->tx_rings)
7225 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007226 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7227
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007228 if (alloc_qvectors) {
7229 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007230 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007231 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7232 if (!vsi->q_vectors) {
7233 ret = -ENOMEM;
7234 goto err_vectors;
7235 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007236 }
7237 return ret;
7238
7239err_vectors:
7240 kfree(vsi->tx_rings);
7241 return ret;
7242}
7243
7244/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007245 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7246 * @pf: board private structure
7247 * @type: type of VSI
7248 *
7249 * On error: returns error code (negative)
7250 * On success: returns vsi index in PF (positive)
7251 **/
7252static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7253{
7254 int ret = -ENODEV;
7255 struct i40e_vsi *vsi;
7256 int vsi_idx;
7257 int i;
7258
7259 /* Need to protect the allocation of the VSIs at the PF level */
7260 mutex_lock(&pf->switch_mutex);
7261
7262 /* VSI list may be fragmented if VSI creation/destruction has
7263 * been happening. We can afford to do a quick scan to look
7264 * for any free VSIs in the list.
7265 *
7266 * find next empty vsi slot, looping back around if necessary
7267 */
7268 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007269 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007270 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007271 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007272 i = 0;
7273 while (i < pf->next_vsi && pf->vsi[i])
7274 i++;
7275 }
7276
Mitch Williams505682c2014-05-20 08:01:37 +00007277 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007278 vsi_idx = i; /* Found one! */
7279 } else {
7280 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007281 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007282 }
7283 pf->next_vsi = ++i;
7284
7285 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7286 if (!vsi) {
7287 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007288 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007289 }
7290 vsi->type = type;
7291 vsi->back = pf;
7292 set_bit(__I40E_DOWN, &vsi->state);
7293 vsi->flags = 0;
7294 vsi->idx = vsi_idx;
7295 vsi->rx_itr_setting = pf->rx_itr_default;
7296 vsi->tx_itr_setting = pf->tx_itr_default;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007297 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007298 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7299 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007300 vsi->netdev_registered = false;
7301 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7302 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007303 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007304
Alexander Duyck9f65e152013-09-28 06:00:58 +00007305 ret = i40e_set_num_rings_in_vsi(vsi);
7306 if (ret)
7307 goto err_rings;
7308
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007309 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007310 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007311 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007312
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007313 /* Setup default MSIX irq handler for VSI */
7314 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7315
Kiran Patil21659032015-09-30 14:09:03 -04007316 /* Initialize VSI lock */
7317 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007318 pf->vsi[vsi_idx] = vsi;
7319 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007320 goto unlock_pf;
7321
Alexander Duyck9f65e152013-09-28 06:00:58 +00007322err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007323 pf->next_vsi = i - 1;
7324 kfree(vsi);
7325unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007326 mutex_unlock(&pf->switch_mutex);
7327 return ret;
7328}
7329
7330/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007331 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7332 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007333 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007334 *
7335 * On error: returns error code (negative)
7336 * On success: returns 0
7337 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007338static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007339{
7340 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007341 if (free_qvectors) {
7342 kfree(vsi->q_vectors);
7343 vsi->q_vectors = NULL;
7344 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007345 kfree(vsi->tx_rings);
7346 vsi->tx_rings = NULL;
7347 vsi->rx_rings = NULL;
7348}
7349
7350/**
Helin Zhang28c58692015-10-26 19:44:27 -04007351 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7352 * and lookup table
7353 * @vsi: Pointer to VSI structure
7354 */
7355static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7356{
7357 if (!vsi)
7358 return;
7359
7360 kfree(vsi->rss_hkey_user);
7361 vsi->rss_hkey_user = NULL;
7362
7363 kfree(vsi->rss_lut_user);
7364 vsi->rss_lut_user = NULL;
7365}
7366
7367/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007368 * i40e_vsi_clear - Deallocate the VSI provided
7369 * @vsi: the VSI being un-configured
7370 **/
7371static int i40e_vsi_clear(struct i40e_vsi *vsi)
7372{
7373 struct i40e_pf *pf;
7374
7375 if (!vsi)
7376 return 0;
7377
7378 if (!vsi->back)
7379 goto free_vsi;
7380 pf = vsi->back;
7381
7382 mutex_lock(&pf->switch_mutex);
7383 if (!pf->vsi[vsi->idx]) {
7384 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7385 vsi->idx, vsi->idx, vsi, vsi->type);
7386 goto unlock_vsi;
7387 }
7388
7389 if (pf->vsi[vsi->idx] != vsi) {
7390 dev_err(&pf->pdev->dev,
7391 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7392 pf->vsi[vsi->idx]->idx,
7393 pf->vsi[vsi->idx],
7394 pf->vsi[vsi->idx]->type,
7395 vsi->idx, vsi, vsi->type);
7396 goto unlock_vsi;
7397 }
7398
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007399 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007400 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7401 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7402
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007403 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007404 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007405
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007406 pf->vsi[vsi->idx] = NULL;
7407 if (vsi->idx < pf->next_vsi)
7408 pf->next_vsi = vsi->idx;
7409
7410unlock_vsi:
7411 mutex_unlock(&pf->switch_mutex);
7412free_vsi:
7413 kfree(vsi);
7414
7415 return 0;
7416}
7417
7418/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007419 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7420 * @vsi: the VSI being cleaned
7421 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007422static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007423{
7424 int i;
7425
Greg Rose8e9dca52013-12-18 13:45:53 +00007426 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007427 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007428 kfree_rcu(vsi->tx_rings[i], rcu);
7429 vsi->tx_rings[i] = NULL;
7430 vsi->rx_rings[i] = NULL;
7431 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007432 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007433}
7434
7435/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007436 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7437 * @vsi: the VSI being configured
7438 **/
7439static int i40e_alloc_rings(struct i40e_vsi *vsi)
7440{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007441 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007442 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007443 int i;
7444
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007445 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007446 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007447 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007448 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7449 if (!tx_ring)
7450 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007451
7452 tx_ring->queue_index = i;
7453 tx_ring->reg_idx = vsi->base_queue + i;
7454 tx_ring->ring_active = false;
7455 tx_ring->vsi = vsi;
7456 tx_ring->netdev = vsi->netdev;
7457 tx_ring->dev = &pf->pdev->dev;
7458 tx_ring->count = vsi->num_desc;
7459 tx_ring->size = 0;
7460 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007461 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7462 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04007463 if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
7464 tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007465 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007466
Alexander Duyck9f65e152013-09-28 06:00:58 +00007467 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007468 rx_ring->queue_index = i;
7469 rx_ring->reg_idx = vsi->base_queue + i;
7470 rx_ring->ring_active = false;
7471 rx_ring->vsi = vsi;
7472 rx_ring->netdev = vsi->netdev;
7473 rx_ring->dev = &pf->pdev->dev;
7474 rx_ring->count = vsi->num_desc;
7475 rx_ring->size = 0;
7476 rx_ring->dcb_tc = 0;
7477 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7478 set_ring_16byte_desc_enabled(rx_ring);
7479 else
7480 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e152013-09-28 06:00:58 +00007481 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007482 }
7483
7484 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007485
7486err_out:
7487 i40e_vsi_clear_rings(vsi);
7488 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007489}
7490
7491/**
7492 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7493 * @pf: board private structure
7494 * @vectors: the number of MSI-X vectors to request
7495 *
7496 * Returns the number of vectors reserved, or error
7497 **/
7498static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7499{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007500 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7501 I40E_MIN_MSIX, vectors);
7502 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007503 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007504 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007505 vectors = 0;
7506 }
7507
7508 return vectors;
7509}
7510
7511/**
7512 * i40e_init_msix - Setup the MSIX capability
7513 * @pf: board private structure
7514 *
7515 * Work with the OS to set up the MSIX vectors needed.
7516 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007517 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007518 **/
7519static int i40e_init_msix(struct i40e_pf *pf)
7520{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007521 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007522 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007523 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007524 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007525
7526 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7527 return -ENODEV;
7528
7529 /* The number of vectors we'll request will be comprised of:
7530 * - Add 1 for "other" cause for Admin Queue events, etc.
7531 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007532 * - Queues being used for RSS.
7533 * We don't need as many as max_rss_size vectors.
7534 * use rss_size instead in the calculation since that
7535 * is governed by number of cpus in the system.
7536 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007537 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007538#ifdef I40E_FCOE
7539 * - The number of FCOE qps.
7540#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007541 * Once we count this up, try the request.
7542 *
7543 * If we can't get what we want, we'll simplify to nearly nothing
7544 * and try again. If that still fails, we punt.
7545 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007546 vectors_left = hw->func_caps.num_msix_vectors;
7547 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007548
Shannon Nelson1e200e42015-02-27 09:15:24 +00007549 /* reserve one vector for miscellaneous handler */
7550 if (vectors_left) {
7551 v_budget++;
7552 vectors_left--;
7553 }
7554
7555 /* reserve vectors for the main PF traffic queues */
7556 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7557 vectors_left -= pf->num_lan_msix;
7558 v_budget += pf->num_lan_msix;
7559
7560 /* reserve one vector for sideband flow director */
7561 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7562 if (vectors_left) {
7563 v_budget++;
7564 vectors_left--;
7565 } else {
7566 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7567 }
7568 }
John W Linville83840e42015-01-14 03:06:28 +00007569
Vasu Dev38e00432014-08-01 13:27:03 -07007570#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007571 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007572 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007573 if (!vectors_left)
7574 pf->num_fcoe_msix = 0;
7575 else if (vectors_left >= pf->num_fcoe_qps)
7576 pf->num_fcoe_msix = pf->num_fcoe_qps;
7577 else
7578 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007579 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007580 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007581 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007582
Vasu Dev38e00432014-08-01 13:27:03 -07007583#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007584 /* any vectors left over go for VMDq support */
7585 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7586 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7587 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7588
7589 /* if we're short on vectors for what's desired, we limit
7590 * the queues per vmdq. If this is still more than are
7591 * available, the user will need to change the number of
7592 * queues/vectors used by the PF later with the ethtool
7593 * channels command
7594 */
7595 if (vmdq_vecs < vmdq_vecs_wanted)
7596 pf->num_vmdq_qps = 1;
7597 pf->num_vmdq_msix = pf->num_vmdq_qps;
7598
7599 v_budget += vmdq_vecs;
7600 vectors_left -= vmdq_vecs;
7601 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007602
7603 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7604 GFP_KERNEL);
7605 if (!pf->msix_entries)
7606 return -ENOMEM;
7607
7608 for (i = 0; i < v_budget; i++)
7609 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007610 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007611
Shannon Nelson3b444392015-02-26 16:15:57 +00007612 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007613 /* If we have limited resources, we will start with no vectors
7614 * for the special features and then allocate vectors to some
7615 * of these features based on the policy and at the end disable
7616 * the features that did not get any vectors.
7617 */
Vasu Dev38e00432014-08-01 13:27:03 -07007618#ifdef I40E_FCOE
7619 pf->num_fcoe_qps = 0;
7620 pf->num_fcoe_msix = 0;
7621#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007622 pf->num_vmdq_msix = 0;
7623 }
7624
Shannon Nelson3b444392015-02-26 16:15:57 +00007625 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007626 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7627 kfree(pf->msix_entries);
7628 pf->msix_entries = NULL;
7629 return -ENODEV;
7630
Shannon Nelson3b444392015-02-26 16:15:57 +00007631 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007632 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007633 pf->num_vmdq_vsis = 0;
7634 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007635 pf->num_lan_qps = 1;
7636 pf->num_lan_msix = 1;
7637
Shannon Nelson3b444392015-02-26 16:15:57 +00007638 } else if (v_actual != v_budget) {
7639 int vec;
7640
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007641 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007642 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007643
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007644 /* Scale vector usage down */
7645 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007646 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007647 pf->num_vmdq_qps = 1;
7648 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007649
7650 /* partition out the remaining vectors */
7651 switch (vec) {
7652 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007653 pf->num_lan_msix = 1;
7654 break;
7655 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007656#ifdef I40E_FCOE
7657 /* give one vector to FCoE */
7658 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7659 pf->num_lan_msix = 1;
7660 pf->num_fcoe_msix = 1;
7661 }
7662#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007663 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007664#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007665 break;
7666 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007667#ifdef I40E_FCOE
7668 /* give one vector to FCoE */
7669 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7670 pf->num_fcoe_msix = 1;
7671 vec--;
7672 }
7673#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007674 /* give the rest to the PF */
7675 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007676 break;
7677 }
7678 }
7679
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007680 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7681 (pf->num_vmdq_msix == 0)) {
7682 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7683 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7684 }
Vasu Dev38e00432014-08-01 13:27:03 -07007685#ifdef I40E_FCOE
7686
7687 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7688 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7689 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7690 }
7691#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007692 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007693}
7694
7695/**
Greg Rose90e04072014-03-06 08:59:57 +00007696 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007697 * @vsi: the VSI being configured
7698 * @v_idx: index of the vector in the vsi struct
7699 *
7700 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7701 **/
Greg Rose90e04072014-03-06 08:59:57 +00007702static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007703{
7704 struct i40e_q_vector *q_vector;
7705
7706 /* allocate q_vector */
7707 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7708 if (!q_vector)
7709 return -ENOMEM;
7710
7711 q_vector->vsi = vsi;
7712 q_vector->v_idx = v_idx;
7713 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7714 if (vsi->netdev)
7715 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007716 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007717
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007718 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7719 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7720
Alexander Duyck493fb302013-09-28 07:01:44 +00007721 /* tie q_vector and vsi together */
7722 vsi->q_vectors[v_idx] = q_vector;
7723
7724 return 0;
7725}
7726
7727/**
Greg Rose90e04072014-03-06 08:59:57 +00007728 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007729 * @vsi: the VSI being configured
7730 *
7731 * We allocate one q_vector per queue interrupt. If allocation fails we
7732 * return -ENOMEM.
7733 **/
Greg Rose90e04072014-03-06 08:59:57 +00007734static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007735{
7736 struct i40e_pf *pf = vsi->back;
7737 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007738 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007739
7740 /* if not MSIX, give the one vector only to the LAN VSI */
7741 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7742 num_q_vectors = vsi->num_q_vectors;
7743 else if (vsi == pf->vsi[pf->lan_vsi])
7744 num_q_vectors = 1;
7745 else
7746 return -EINVAL;
7747
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007748 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007749 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007750 if (err)
7751 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007752 }
7753
7754 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007755
7756err_out:
7757 while (v_idx--)
7758 i40e_free_q_vector(vsi, v_idx);
7759
7760 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007761}
7762
7763/**
7764 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7765 * @pf: board private structure to initialize
7766 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007767static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007768{
Shannon Nelson3b444392015-02-26 16:15:57 +00007769 int vectors = 0;
7770 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007771
7772 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007773 vectors = i40e_init_msix(pf);
7774 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007775 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007776#ifdef I40E_FCOE
7777 I40E_FLAG_FCOE_ENABLED |
7778#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007779 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007780 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007781 I40E_FLAG_SRIOV_ENABLED |
7782 I40E_FLAG_FD_SB_ENABLED |
7783 I40E_FLAG_FD_ATR_ENABLED |
7784 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007785
7786 /* rework the queue expectations without MSIX */
7787 i40e_determine_queue_usage(pf);
7788 }
7789 }
7790
7791 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7792 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007793 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007794 vectors = pci_enable_msi(pf->pdev);
7795 if (vectors < 0) {
7796 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7797 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007798 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7799 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007800 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007801 }
7802
Shannon Nelson958a3e32013-09-28 07:13:28 +00007803 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007804 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007805
Shannon Nelson3b444392015-02-26 16:15:57 +00007806 /* set up vector assignment tracking */
7807 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7808 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007809 if (!pf->irq_pile) {
7810 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7811 return -ENOMEM;
7812 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007813 pf->irq_pile->num_entries = vectors;
7814 pf->irq_pile->search_hint = 0;
7815
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007816 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007817 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007818
7819 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007820}
7821
7822/**
7823 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7824 * @pf: board private structure
7825 *
7826 * This sets up the handler for MSIX 0, which is used to manage the
7827 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7828 * when in MSI or Legacy interrupt mode.
7829 **/
7830static int i40e_setup_misc_vector(struct i40e_pf *pf)
7831{
7832 struct i40e_hw *hw = &pf->hw;
7833 int err = 0;
7834
7835 /* Only request the irq if this is the first time through, and
7836 * not when we're rebuilding after a Reset
7837 */
7838 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7839 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007840 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007841 if (err) {
7842 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007843 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007844 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007845 return -EFAULT;
7846 }
7847 }
7848
Jacob Kellerab437b52014-12-14 01:55:08 +00007849 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007850
7851 /* associate no queues to the misc vector */
7852 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7853 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7854
7855 i40e_flush(hw);
7856
7857 i40e_irq_dynamic_enable_icr0(pf);
7858
7859 return err;
7860}
7861
7862/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007863 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7864 * @vsi: vsi structure
7865 * @seed: RSS hash seed
7866 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007867static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7868 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007869{
7870 struct i40e_aqc_get_set_rss_key_data rss_key;
7871 struct i40e_pf *pf = vsi->back;
7872 struct i40e_hw *hw = &pf->hw;
7873 bool pf_lut = false;
7874 u8 *rss_lut;
7875 int ret, i;
7876
7877 memset(&rss_key, 0, sizeof(rss_key));
7878 memcpy(&rss_key, seed, sizeof(rss_key));
7879
7880 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7881 if (!rss_lut)
7882 return -ENOMEM;
7883
7884 /* Populate the LUT with max no. of queues in round robin fashion */
7885 for (i = 0; i < vsi->rss_table_size; i++)
7886 rss_lut[i] = i % vsi->rss_size;
7887
7888 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7889 if (ret) {
7890 dev_info(&pf->pdev->dev,
7891 "Cannot set RSS key, err %s aq_err %s\n",
7892 i40e_stat_str(&pf->hw, ret),
7893 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007894 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007895 }
7896
7897 if (vsi->type == I40E_VSI_MAIN)
7898 pf_lut = true;
7899
7900 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7901 vsi->rss_table_size);
7902 if (ret)
7903 dev_info(&pf->pdev->dev,
7904 "Cannot set RSS lut, err %s aq_err %s\n",
7905 i40e_stat_str(&pf->hw, ret),
7906 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7907
Anjali Singhai126b63d2015-08-24 13:26:53 -07007908config_rss_aq_out:
7909 kfree(rss_lut);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007910 return ret;
7911}
7912
7913/**
7914 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7915 * @vsi: VSI structure
7916 **/
7917static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7918{
7919 u8 seed[I40E_HKEY_ARRAY_SIZE];
7920 struct i40e_pf *pf = vsi->back;
Helin Zhange69ff812015-10-21 19:56:22 -04007921 u8 *lut;
7922 int ret;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007923
Helin Zhange69ff812015-10-21 19:56:22 -04007924 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
7925 return 0;
7926
7927 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
7928 if (!lut)
7929 return -ENOMEM;
7930
7931 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007932 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhangacd65442015-10-26 19:44:28 -04007933 vsi->rss_size = min_t(int, pf->alloc_rss_size, vsi->num_queue_pairs);
Helin Zhange69ff812015-10-21 19:56:22 -04007934 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
7935 kfree(lut);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007936
Helin Zhange69ff812015-10-21 19:56:22 -04007937 return ret;
7938}
7939
7940/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08007941 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
7942 * @vsi: Pointer to vsi structure
7943 * @seed: Buffter to store the hash keys
7944 * @lut: Buffer to store the lookup table entries
7945 * @lut_size: Size of buffer to store the lookup table entries
7946 *
7947 * Return 0 on success, negative on failure
7948 */
7949static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7950 u8 *lut, u16 lut_size)
7951{
7952 struct i40e_pf *pf = vsi->back;
7953 struct i40e_hw *hw = &pf->hw;
7954 int ret = 0;
7955
7956 if (seed) {
7957 ret = i40e_aq_get_rss_key(hw, vsi->id,
7958 (struct i40e_aqc_get_set_rss_key_data *)seed);
7959 if (ret) {
7960 dev_info(&pf->pdev->dev,
7961 "Cannot get RSS key, err %s aq_err %s\n",
7962 i40e_stat_str(&pf->hw, ret),
7963 i40e_aq_str(&pf->hw,
7964 pf->hw.aq.asq_last_status));
7965 return ret;
7966 }
7967 }
7968
7969 if (lut) {
7970 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
7971
7972 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
7973 if (ret) {
7974 dev_info(&pf->pdev->dev,
7975 "Cannot get RSS lut, err %s aq_err %s\n",
7976 i40e_stat_str(&pf->hw, ret),
7977 i40e_aq_str(&pf->hw,
7978 pf->hw.aq.asq_last_status));
7979 return ret;
7980 }
7981 }
7982
7983 return ret;
7984}
7985
7986/**
Helin Zhang043dd652015-10-21 19:56:23 -04007987 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04007988 * @vsi: Pointer to vsi structure
7989 * @seed: RSS hash seed
7990 * @lut: Lookup table
7991 * @lut_size: Lookup table size
7992 *
7993 * Returns 0 on success, negative on failure
7994 **/
7995static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
7996 const u8 *lut, u16 lut_size)
7997{
7998 struct i40e_pf *pf = vsi->back;
7999 struct i40e_hw *hw = &pf->hw;
8000 u8 i;
8001
8002 /* Fill out hash function seed */
8003 if (seed) {
8004 u32 *seed_dw = (u32 *)seed;
8005
8006 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8007 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
8008 }
8009
8010 if (lut) {
8011 u32 *lut_dw = (u32 *)lut;
8012
8013 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8014 return -EINVAL;
8015
8016 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8017 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8018 }
8019 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008020
8021 return 0;
8022}
8023
8024/**
Helin Zhang043dd652015-10-21 19:56:23 -04008025 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8026 * @vsi: Pointer to VSI structure
8027 * @seed: Buffer to store the keys
8028 * @lut: Buffer to store the lookup table entries
8029 * @lut_size: Size of buffer to store the lookup table entries
8030 *
8031 * Returns 0 on success, negative on failure
8032 */
8033static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8034 u8 *lut, u16 lut_size)
8035{
8036 struct i40e_pf *pf = vsi->back;
8037 struct i40e_hw *hw = &pf->hw;
8038 u16 i;
8039
8040 if (seed) {
8041 u32 *seed_dw = (u32 *)seed;
8042
8043 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8044 seed_dw[i] = rd32(hw, I40E_PFQF_HKEY(i));
8045 }
8046 if (lut) {
8047 u32 *lut_dw = (u32 *)lut;
8048
8049 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8050 return -EINVAL;
8051 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8052 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8053 }
8054
8055 return 0;
8056}
8057
8058/**
8059 * i40e_config_rss - Configure RSS keys and lut
8060 * @vsi: Pointer to VSI structure
8061 * @seed: RSS hash seed
8062 * @lut: Lookup table
8063 * @lut_size: Lookup table size
8064 *
8065 * Returns 0 on success, negative on failure
8066 */
8067int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8068{
8069 struct i40e_pf *pf = vsi->back;
8070
8071 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8072 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8073 else
8074 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8075}
8076
8077/**
8078 * i40e_get_rss - Get RSS keys and lut
8079 * @vsi: Pointer to VSI structure
8080 * @seed: Buffer to store the keys
8081 * @lut: Buffer to store the lookup table entries
8082 * lut_size: Size of buffer to store the lookup table entries
8083 *
8084 * Returns 0 on success, negative on failure
8085 */
8086int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8087{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008088 struct i40e_pf *pf = vsi->back;
8089
8090 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8091 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8092 else
8093 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008094}
8095
8096/**
Helin Zhange69ff812015-10-21 19:56:22 -04008097 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8098 * @pf: Pointer to board private structure
8099 * @lut: Lookup table
8100 * @rss_table_size: Lookup table size
8101 * @rss_size: Range of queue number for hashing
8102 */
8103static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8104 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008105{
Helin Zhange69ff812015-10-21 19:56:22 -04008106 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008107
Helin Zhange69ff812015-10-21 19:56:22 -04008108 for (i = 0; i < rss_table_size; i++)
8109 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008110}
8111
8112/**
Helin Zhang043dd652015-10-21 19:56:23 -04008113 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008114 * @pf: board private structure
8115 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008116static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008117{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008118 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008119 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008120 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008121 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008122 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008123 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008124 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008125
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008126 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
8127 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
8128 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008129 hena |= i40e_pf_get_default_rss_hena(pf);
8130
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008131 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
8132 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
8133
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008134 /* Determine the RSS table size based on the hardware capabilities */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008135 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008136 reg_val = (pf->rss_table_size == 512) ?
8137 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8138 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008139 wr32(hw, I40E_PFQF_CTL_0, reg_val);
8140
Helin Zhang28c58692015-10-26 19:44:27 -04008141 /* Determine the RSS size of the VSI */
8142 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008143 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8144 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008145
Helin Zhange69ff812015-10-21 19:56:22 -04008146 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8147 if (!lut)
8148 return -ENOMEM;
8149
Helin Zhang28c58692015-10-26 19:44:27 -04008150 /* Use user configured lut if there is one, otherwise use default */
8151 if (vsi->rss_lut_user)
8152 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8153 else
8154 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008155
Helin Zhang28c58692015-10-26 19:44:27 -04008156 /* Use user configured hash key if there is one, otherwise
8157 * use default.
8158 */
8159 if (vsi->rss_hkey_user)
8160 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8161 else
8162 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008163 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008164 kfree(lut);
8165
8166 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008167}
8168
8169/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008170 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8171 * @pf: board private structure
8172 * @queue_count: the requested queue count for rss.
8173 *
8174 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8175 * count which may be different from the requested queue count.
8176 **/
8177int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8178{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008179 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8180 int new_rss_size;
8181
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008182 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8183 return 0;
8184
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008185 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008186
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008187 if (queue_count != vsi->num_queue_pairs) {
8188 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008189 i40e_prep_for_reset(pf);
8190
Helin Zhangacd65442015-10-26 19:44:28 -04008191 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008192
8193 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008194
8195 /* Discard the user configured hash keys and lut, if less
8196 * queues are enabled.
8197 */
8198 if (queue_count < vsi->rss_size) {
8199 i40e_clear_rss_config_user(vsi);
8200 dev_dbg(&pf->pdev->dev,
8201 "discard user configured hash keys and lut\n");
8202 }
8203
8204 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008205 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8206 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008207
Helin Zhang043dd652015-10-21 19:56:23 -04008208 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008209 }
Helin Zhange36b0b12015-10-26 19:44:36 -04008210 dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
8211 pf->alloc_rss_size, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008212 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008213}
8214
8215/**
Greg Rosef4492db2015-02-06 08:52:12 +00008216 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8217 * @pf: board private structure
8218 **/
8219i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8220{
8221 i40e_status status;
8222 bool min_valid, max_valid;
8223 u32 max_bw, min_bw;
8224
8225 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8226 &min_valid, &max_valid);
8227
8228 if (!status) {
8229 if (min_valid)
8230 pf->npar_min_bw = min_bw;
8231 if (max_valid)
8232 pf->npar_max_bw = max_bw;
8233 }
8234
8235 return status;
8236}
8237
8238/**
8239 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8240 * @pf: board private structure
8241 **/
8242i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8243{
8244 struct i40e_aqc_configure_partition_bw_data bw_data;
8245 i40e_status status;
8246
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008247 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008248 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008249 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8250 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8251
8252 /* Set the new bandwidths */
8253 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8254
8255 return status;
8256}
8257
8258/**
8259 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8260 * @pf: board private structure
8261 **/
8262i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8263{
8264 /* Commit temporary BW setting to permanent NVM image */
8265 enum i40e_admin_queue_err last_aq_status;
8266 i40e_status ret;
8267 u16 nvm_word;
8268
8269 if (pf->hw.partition_id != 1) {
8270 dev_info(&pf->pdev->dev,
8271 "Commit BW only works on partition 1! This is partition %d",
8272 pf->hw.partition_id);
8273 ret = I40E_NOT_SUPPORTED;
8274 goto bw_commit_out;
8275 }
8276
8277 /* Acquire NVM for read access */
8278 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8279 last_aq_status = pf->hw.aq.asq_last_status;
8280 if (ret) {
8281 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008282 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8283 i40e_stat_str(&pf->hw, ret),
8284 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008285 goto bw_commit_out;
8286 }
8287
8288 /* Read word 0x10 of NVM - SW compatibility word 1 */
8289 ret = i40e_aq_read_nvm(&pf->hw,
8290 I40E_SR_NVM_CONTROL_WORD,
8291 0x10, sizeof(nvm_word), &nvm_word,
8292 false, NULL);
8293 /* Save off last admin queue command status before releasing
8294 * the NVM
8295 */
8296 last_aq_status = pf->hw.aq.asq_last_status;
8297 i40e_release_nvm(&pf->hw);
8298 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008299 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8300 i40e_stat_str(&pf->hw, ret),
8301 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008302 goto bw_commit_out;
8303 }
8304
8305 /* Wait a bit for NVM release to complete */
8306 msleep(50);
8307
8308 /* Acquire NVM for write access */
8309 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8310 last_aq_status = pf->hw.aq.asq_last_status;
8311 if (ret) {
8312 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008313 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8314 i40e_stat_str(&pf->hw, ret),
8315 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008316 goto bw_commit_out;
8317 }
8318 /* Write it back out unchanged to initiate update NVM,
8319 * which will force a write of the shadow (alt) RAM to
8320 * the NVM - thus storing the bandwidth values permanently.
8321 */
8322 ret = i40e_aq_update_nvm(&pf->hw,
8323 I40E_SR_NVM_CONTROL_WORD,
8324 0x10, sizeof(nvm_word),
8325 &nvm_word, true, NULL);
8326 /* Save off last admin queue command status before releasing
8327 * the NVM
8328 */
8329 last_aq_status = pf->hw.aq.asq_last_status;
8330 i40e_release_nvm(&pf->hw);
8331 if (ret)
8332 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008333 "BW settings NOT SAVED, err %s aq_err %s\n",
8334 i40e_stat_str(&pf->hw, ret),
8335 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008336bw_commit_out:
8337
8338 return ret;
8339}
8340
8341/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008342 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8343 * @pf: board private structure to initialize
8344 *
8345 * i40e_sw_init initializes the Adapter private data structure.
8346 * Fields are initialized based on PCI device information and
8347 * OS network device settings (MTU size).
8348 **/
8349static int i40e_sw_init(struct i40e_pf *pf)
8350{
8351 int err = 0;
8352 int size;
8353
8354 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8355 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00008356 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008357 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8358 if (I40E_DEBUG_USER & debug)
8359 pf->hw.debug_mask = debug;
8360 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8361 I40E_DEFAULT_MSG_ENABLE);
8362 }
8363
8364 /* Set default capability flags */
8365 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8366 I40E_FLAG_MSI_ENABLED |
Shannon Nelson9ac77262015-08-27 11:42:40 -04008367 I40E_FLAG_LINK_POLLING_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008368 I40E_FLAG_MSIX_ENABLED;
8369
8370 if (iommu_present(&pci_bus_type))
8371 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
8372 else
8373 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008374
Mitch Williamsca99eb92014-04-04 04:43:07 +00008375 /* Set default ITR */
8376 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8377 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8378
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008379 /* Depending on PF configurations, it is possible that the RSS
8380 * maximum might end up larger than the available queues
8381 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008382 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008383 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008384 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008385 pf->rss_size_max = min_t(int, pf->rss_size_max,
8386 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008387 if (pf->hw.func_caps.rss) {
8388 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008389 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8390 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008391 }
8392
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008393 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008394 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008395 pf->flags |= I40E_FLAG_MFP_ENABLED;
8396 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008397 if (i40e_get_npar_bw_setting(pf))
8398 dev_warn(&pf->pdev->dev,
8399 "Could not get NPAR bw settings\n");
8400 else
8401 dev_info(&pf->pdev->dev,
8402 "Min BW = %8.8x, Max BW = %8.8x\n",
8403 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008404 }
8405
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008406 /* FW/NVM is not yet fixed in this regard */
8407 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8408 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8409 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8410 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008411 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8412 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008413 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008414 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008415 else
8416 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008417 pf->fdir_pf_filter_count =
8418 pf->hw.func_caps.fd_filters_guaranteed;
8419 pf->hw.fdir_shared_filter_count =
8420 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008421 }
8422
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008423 if (((pf->hw.mac.type == I40E_MAC_X710) ||
8424 (pf->hw.mac.type == I40E_MAC_XL710)) &&
8425 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
8426 (pf->hw.aq.fw_maj_ver < 4)))
8427 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
8428
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008429 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008430 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008431 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008432 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008433 }
8434
Vasu Dev38e00432014-08-01 13:27:03 -07008435#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008436 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008437
8438#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008439#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008440 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008441 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8442 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8443 pf->num_req_vfs = min_t(int,
8444 pf->hw.func_caps.num_vfs,
8445 I40E_MAX_VF_COUNT);
8446 }
8447#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008448 if (pf->hw.mac.type == I40E_MAC_X722) {
8449 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8450 I40E_FLAG_128_QP_RSS_CAPABLE |
8451 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8452 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8453 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008454 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Catherine Sullivanf8db54cc2015-12-22 14:25:14 -08008455 I40E_FLAG_100M_SGMII_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008456 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008457 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8458 ((pf->hw.aq.api_maj_ver == 1) &&
8459 (pf->hw.aq.api_min_ver > 4))) {
8460 /* Supported in FW API version higher than 1.4 */
8461 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008462 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8463 } else {
8464 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008465 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008466
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008467 pf->eeprom_version = 0xDEAD;
8468 pf->lan_veb = I40E_NO_VEB;
8469 pf->lan_vsi = I40E_NO_VSI;
8470
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008471 /* By default FW has this off for performance reasons */
8472 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8473
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008474 /* set up queue assignment tracking */
8475 size = sizeof(struct i40e_lump_tracking)
8476 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8477 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8478 if (!pf->qp_pile) {
8479 err = -ENOMEM;
8480 goto sw_init_done;
8481 }
8482 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8483 pf->qp_pile->search_hint = 0;
8484
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008485 pf->tx_timeout_recovery_level = 1;
8486
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008487 mutex_init(&pf->switch_mutex);
8488
Greg Rosec668a122015-02-26 16:10:39 +00008489 /* If NPAR is enabled nudge the Tx scheduler */
8490 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8491 i40e_set_npar_bw_setting(pf);
8492
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008493sw_init_done:
8494 return err;
8495}
8496
8497/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008498 * i40e_set_ntuple - set the ntuple feature flag and take action
8499 * @pf: board private structure to initialize
8500 * @features: the feature set that the stack is suggesting
8501 *
8502 * returns a bool to indicate if reset needs to happen
8503 **/
8504bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8505{
8506 bool need_reset = false;
8507
8508 /* Check if Flow Director n-tuple support was enabled or disabled. If
8509 * the state changed, we need to reset.
8510 */
8511 if (features & NETIF_F_NTUPLE) {
8512 /* Enable filters and mark for reset */
8513 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8514 need_reset = true;
8515 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8516 } else {
8517 /* turn off filters, mark for reset and clear SW filter list */
8518 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8519 need_reset = true;
8520 i40e_fdir_filter_exit(pf);
8521 }
8522 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008523 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008524 /* reset fd counters */
8525 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8526 pf->fdir_pf_active_filters = 0;
8527 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008528 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8529 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008530 /* if ATR was auto disabled it can be re-enabled. */
8531 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8532 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8533 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008534 }
8535 return need_reset;
8536}
8537
8538/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008539 * i40e_set_features - set the netdev feature flags
8540 * @netdev: ptr to the netdev being adjusted
8541 * @features: the feature set that the stack is suggesting
8542 **/
8543static int i40e_set_features(struct net_device *netdev,
8544 netdev_features_t features)
8545{
8546 struct i40e_netdev_priv *np = netdev_priv(netdev);
8547 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008548 struct i40e_pf *pf = vsi->back;
8549 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008550
8551 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8552 i40e_vlan_stripping_enable(vsi);
8553 else
8554 i40e_vlan_stripping_disable(vsi);
8555
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008556 need_reset = i40e_set_ntuple(pf, features);
8557
8558 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008559 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008560
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008561 return 0;
8562}
8563
Singhai, Anjali6a899022015-12-14 12:21:18 -08008564#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008565/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008566 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008567 * @pf: board private structure
8568 * @port: The UDP port to look up
8569 *
8570 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8571 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008572static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008573{
8574 u8 i;
8575
8576 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008577 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008578 return i;
8579 }
8580
8581 return i;
8582}
8583
Singhai, Anjali6a899022015-12-14 12:21:18 -08008584#endif
Eric Dumazet5cae7612016-01-20 19:53:02 -08008585
8586#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008587/**
8588 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8589 * @netdev: This physical port's netdev
8590 * @sa_family: Socket Family that VXLAN is notifying us about
8591 * @port: New UDP port number that VXLAN started listening to
8592 **/
8593static void i40e_add_vxlan_port(struct net_device *netdev,
8594 sa_family_t sa_family, __be16 port)
8595{
8596 struct i40e_netdev_priv *np = netdev_priv(netdev);
8597 struct i40e_vsi *vsi = np->vsi;
8598 struct i40e_pf *pf = vsi->back;
8599 u8 next_idx;
8600 u8 idx;
8601
8602 if (sa_family == AF_INET6)
8603 return;
8604
Singhai, Anjali6a899022015-12-14 12:21:18 -08008605 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008606
8607 /* Check if port already exists */
8608 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008609 netdev_info(netdev, "vxlan port %d already offloaded\n",
8610 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008611 return;
8612 }
8613
8614 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008615 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008616
8617 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008618 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008619 ntohs(port));
8620 return;
8621 }
8622
8623 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008624 pf->udp_ports[next_idx].index = port;
8625 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8626 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8627 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008628}
8629
8630/**
8631 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8632 * @netdev: This physical port's netdev
8633 * @sa_family: Socket Family that VXLAN is notifying us about
8634 * @port: UDP port number that VXLAN stopped listening to
8635 **/
8636static void i40e_del_vxlan_port(struct net_device *netdev,
8637 sa_family_t sa_family, __be16 port)
8638{
8639 struct i40e_netdev_priv *np = netdev_priv(netdev);
8640 struct i40e_vsi *vsi = np->vsi;
8641 struct i40e_pf *pf = vsi->back;
8642 u8 idx;
8643
8644 if (sa_family == AF_INET6)
8645 return;
8646
Singhai, Anjali6a899022015-12-14 12:21:18 -08008647 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008648
8649 /* Check if port already exists */
8650 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8651 /* if port exists, set it to 0 (mark for deletion)
8652 * and make it pending
8653 */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008654 pf->udp_ports[idx].index = 0;
8655 pf->pending_udp_bitmap |= BIT_ULL(idx);
8656 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008657 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008658 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008659 ntohs(port));
8660 }
8661}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008662#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008663
Eric Dumazet5cae7612016-01-20 19:53:02 -08008664#if IS_ENABLED(CONFIG_GENEVE)
Singhai, Anjali6a899022015-12-14 12:21:18 -08008665/**
8666 * i40e_add_geneve_port - Get notifications about GENEVE ports that come up
8667 * @netdev: This physical port's netdev
8668 * @sa_family: Socket Family that GENEVE is notifying us about
8669 * @port: New UDP port number that GENEVE started listening to
8670 **/
8671static void i40e_add_geneve_port(struct net_device *netdev,
8672 sa_family_t sa_family, __be16 port)
8673{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008674 struct i40e_netdev_priv *np = netdev_priv(netdev);
8675 struct i40e_vsi *vsi = np->vsi;
8676 struct i40e_pf *pf = vsi->back;
8677 u8 next_idx;
8678 u8 idx;
8679
Anjali Singhaia340c782016-01-06 11:49:28 -08008680 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8681 return;
8682
Singhai, Anjali6a899022015-12-14 12:21:18 -08008683 if (sa_family == AF_INET6)
8684 return;
8685
8686 idx = i40e_get_udp_port_idx(pf, port);
8687
8688 /* Check if port already exists */
8689 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8690 netdev_info(netdev, "udp port %d already offloaded\n",
8691 ntohs(port));
8692 return;
8693 }
8694
8695 /* Now check if there is space to add the new port */
8696 next_idx = i40e_get_udp_port_idx(pf, 0);
8697
8698 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8699 netdev_info(netdev, "maximum number of UDP ports reached, not adding port %d\n",
8700 ntohs(port));
8701 return;
8702 }
8703
8704 /* New port: add it and mark its index in the bitmap */
8705 pf->udp_ports[next_idx].index = port;
8706 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8707 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8708 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8709
8710 dev_info(&pf->pdev->dev, "adding geneve port %d\n", ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008711}
8712
8713/**
8714 * i40e_del_geneve_port - Get notifications about GENEVE ports that go away
8715 * @netdev: This physical port's netdev
8716 * @sa_family: Socket Family that GENEVE is notifying us about
8717 * @port: UDP port number that GENEVE stopped listening to
8718 **/
8719static void i40e_del_geneve_port(struct net_device *netdev,
8720 sa_family_t sa_family, __be16 port)
8721{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008722 struct i40e_netdev_priv *np = netdev_priv(netdev);
8723 struct i40e_vsi *vsi = np->vsi;
8724 struct i40e_pf *pf = vsi->back;
8725 u8 idx;
8726
8727 if (sa_family == AF_INET6)
8728 return;
8729
Anjali Singhaia340c782016-01-06 11:49:28 -08008730 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8731 return;
8732
Singhai, Anjali6a899022015-12-14 12:21:18 -08008733 idx = i40e_get_udp_port_idx(pf, port);
8734
8735 /* Check if port already exists */
8736 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8737 /* if port exists, set it to 0 (mark for deletion)
8738 * and make it pending
8739 */
8740 pf->udp_ports[idx].index = 0;
8741 pf->pending_udp_bitmap |= BIT_ULL(idx);
8742 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8743
8744 dev_info(&pf->pdev->dev, "deleting geneve port %d\n",
8745 ntohs(port));
8746 } else {
8747 netdev_warn(netdev, "geneve port %d was not found, not deleting\n",
8748 ntohs(port));
8749 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08008750}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008751#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008752
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008753static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008754 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008755{
8756 struct i40e_netdev_priv *np = netdev_priv(netdev);
8757 struct i40e_pf *pf = np->vsi->back;
8758 struct i40e_hw *hw = &pf->hw;
8759
8760 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8761 return -EOPNOTSUPP;
8762
8763 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8764 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8765
8766 return 0;
8767}
8768
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008769/**
8770 * i40e_ndo_fdb_add - add an entry to the hardware database
8771 * @ndm: the input from the stack
8772 * @tb: pointer to array of nladdr (unused)
8773 * @dev: the net device pointer
8774 * @addr: the MAC address entry being added
8775 * @flags: instructions from stack about fdb operation
8776 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008777static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8778 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008779 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008780 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008781{
8782 struct i40e_netdev_priv *np = netdev_priv(dev);
8783 struct i40e_pf *pf = np->vsi->back;
8784 int err = 0;
8785
8786 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8787 return -EOPNOTSUPP;
8788
Or Gerlitz65891fe2014-12-14 18:19:05 +02008789 if (vid) {
8790 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8791 return -EINVAL;
8792 }
8793
Greg Rose4ba0dea2014-03-06 08:59:55 +00008794 /* Hardware does not support aging addresses so if a
8795 * ndm_state is given only allow permanent addresses
8796 */
8797 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8798 netdev_info(dev, "FDB only supports static addresses\n");
8799 return -EINVAL;
8800 }
8801
8802 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8803 err = dev_uc_add_excl(dev, addr);
8804 else if (is_multicast_ether_addr(addr))
8805 err = dev_mc_add_excl(dev, addr);
8806 else
8807 err = -EINVAL;
8808
8809 /* Only return duplicate errors if NLM_F_EXCL is set */
8810 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8811 err = 0;
8812
8813 return err;
8814}
8815
Neerav Parikh51616012015-02-06 08:52:14 +00008816/**
8817 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8818 * @dev: the netdev being configured
8819 * @nlh: RTNL message
8820 *
8821 * Inserts a new hardware bridge if not already created and
8822 * enables the bridging mode requested (VEB or VEPA). If the
8823 * hardware bridge has already been inserted and the request
8824 * is to change the mode then that requires a PF reset to
8825 * allow rebuild of the components with required hardware
8826 * bridge mode enabled.
8827 **/
8828static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008829 struct nlmsghdr *nlh,
8830 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008831{
8832 struct i40e_netdev_priv *np = netdev_priv(dev);
8833 struct i40e_vsi *vsi = np->vsi;
8834 struct i40e_pf *pf = vsi->back;
8835 struct i40e_veb *veb = NULL;
8836 struct nlattr *attr, *br_spec;
8837 int i, rem;
8838
8839 /* Only for PF VSI for now */
8840 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8841 return -EOPNOTSUPP;
8842
8843 /* Find the HW bridge for PF VSI */
8844 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8845 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8846 veb = pf->veb[i];
8847 }
8848
8849 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8850
8851 nla_for_each_nested(attr, br_spec, rem) {
8852 __u16 mode;
8853
8854 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8855 continue;
8856
8857 mode = nla_get_u16(attr);
8858 if ((mode != BRIDGE_MODE_VEPA) &&
8859 (mode != BRIDGE_MODE_VEB))
8860 return -EINVAL;
8861
8862 /* Insert a new HW bridge */
8863 if (!veb) {
8864 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8865 vsi->tc_config.enabled_tc);
8866 if (veb) {
8867 veb->bridge_mode = mode;
8868 i40e_config_bridge_mode(veb);
8869 } else {
8870 /* No Bridge HW offload available */
8871 return -ENOENT;
8872 }
8873 break;
8874 } else if (mode != veb->bridge_mode) {
8875 /* Existing HW bridge but different mode needs reset */
8876 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008877 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8878 if (mode == BRIDGE_MODE_VEB)
8879 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8880 else
8881 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8882 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008883 break;
8884 }
8885 }
8886
8887 return 0;
8888}
8889
8890/**
8891 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8892 * @skb: skb buff
8893 * @pid: process id
8894 * @seq: RTNL message seq #
8895 * @dev: the netdev being configured
8896 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008897 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008898 *
8899 * Return the mode in which the hardware bridge is operating in
8900 * i.e VEB or VEPA.
8901 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008902static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8903 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008904 u32 __always_unused filter_mask,
8905 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008906{
8907 struct i40e_netdev_priv *np = netdev_priv(dev);
8908 struct i40e_vsi *vsi = np->vsi;
8909 struct i40e_pf *pf = vsi->back;
8910 struct i40e_veb *veb = NULL;
8911 int i;
8912
8913 /* Only for PF VSI for now */
8914 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8915 return -EOPNOTSUPP;
8916
8917 /* Find the HW bridge for the PF VSI */
8918 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8919 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8920 veb = pf->veb[i];
8921 }
8922
8923 if (!veb)
8924 return 0;
8925
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008926 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008927 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00008928}
Neerav Parikh51616012015-02-06 08:52:14 +00008929
Singhai, Anjali6a899022015-12-14 12:21:18 -08008930/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
8931 * inner mac plus all inner ethertypes.
8932 */
8933#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07008934/**
8935 * i40e_features_check - Validate encapsulated packet conforms to limits
8936 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06008937 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07008938 * @features: Offload features that the stack believes apply
8939 **/
8940static netdev_features_t i40e_features_check(struct sk_buff *skb,
8941 struct net_device *dev,
8942 netdev_features_t features)
8943{
8944 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08008945 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07008946 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08008947 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07008948
8949 return features;
8950}
8951
Shannon Nelson37a29732015-02-27 09:15:19 +00008952static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008953 .ndo_open = i40e_open,
8954 .ndo_stop = i40e_close,
8955 .ndo_start_xmit = i40e_lan_xmit_frame,
8956 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8957 .ndo_set_rx_mode = i40e_set_rx_mode,
8958 .ndo_validate_addr = eth_validate_addr,
8959 .ndo_set_mac_address = i40e_set_mac,
8960 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008961 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008962 .ndo_tx_timeout = i40e_tx_timeout,
8963 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8964 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8965#ifdef CONFIG_NET_POLL_CONTROLLER
8966 .ndo_poll_controller = i40e_netpoll,
8967#endif
John Fastabende4c67342016-02-16 21:16:15 -08008968 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008969#ifdef I40E_FCOE
8970 .ndo_fcoe_enable = i40e_fcoe_enable,
8971 .ndo_fcoe_disable = i40e_fcoe_disable,
8972#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008973 .ndo_set_features = i40e_set_features,
8974 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8975 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008976 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008977 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008978 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008979 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Singhai, Anjali6a899022015-12-14 12:21:18 -08008980#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008981 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8982 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8983#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008984#if IS_ENABLED(CONFIG_GENEVE)
8985 .ndo_add_geneve_port = i40e_add_geneve_port,
8986 .ndo_del_geneve_port = i40e_del_geneve_port,
8987#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008988 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008989 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07008990 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00008991 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8992 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008993};
8994
8995/**
8996 * i40e_config_netdev - Setup the netdev flags
8997 * @vsi: the VSI being configured
8998 *
8999 * Returns 0 on success, negative value on failure
9000 **/
9001static int i40e_config_netdev(struct i40e_vsi *vsi)
9002{
Greg Rose1a103702013-11-28 06:42:39 +00009003 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009004 struct i40e_pf *pf = vsi->back;
9005 struct i40e_hw *hw = &pf->hw;
9006 struct i40e_netdev_priv *np;
9007 struct net_device *netdev;
9008 u8 mac_addr[ETH_ALEN];
9009 int etherdev_size;
9010
9011 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009012 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009013 if (!netdev)
9014 return -ENOMEM;
9015
9016 vsi->netdev = netdev;
9017 np = netdev_priv(netdev);
9018 np->vsi = vsi;
9019
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009020 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
9021 NETIF_F_GSO_UDP_TUNNEL |
9022 NETIF_F_GSO_GRE |
9023 NETIF_F_TSO |
9024 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009025
9026 netdev->features = NETIF_F_SG |
9027 NETIF_F_IP_CSUM |
Tom Herbert53692b12015-12-14 11:19:41 -08009028 NETIF_F_SCTP_CRC |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009029 NETIF_F_HIGHDMA |
9030 NETIF_F_GSO_UDP_TUNNEL |
Jesse Brandeburgfec31ff2015-10-01 18:25:42 -07009031 NETIF_F_GSO_GRE |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009032 NETIF_F_HW_VLAN_CTAG_TX |
9033 NETIF_F_HW_VLAN_CTAG_RX |
9034 NETIF_F_HW_VLAN_CTAG_FILTER |
9035 NETIF_F_IPV6_CSUM |
9036 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00009037 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009038 NETIF_F_TSO6 |
9039 NETIF_F_RXCSUM |
9040 NETIF_F_RXHASH |
9041 0;
9042
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009043 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
9044 netdev->features |= NETIF_F_NTUPLE;
9045
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009046 /* copy netdev features into list of user selectable features */
9047 netdev->hw_features |= netdev->features;
9048
9049 if (vsi->type == I40E_VSI_MAIN) {
9050 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009051 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009052 /* The following steps are necessary to prevent reception
9053 * of tagged packets - some older NVM configurations load a
9054 * default a MAC-VLAN filter that accepts any tagged packet
9055 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00009056 */
Kiran Patil21659032015-09-30 14:09:03 -04009057 if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
9058 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009059 i40e_add_filter(vsi, mac_addr,
9060 I40E_VLAN_ANY, false, true);
Kiran Patil21659032015-09-30 14:09:03 -04009061 spin_unlock_bh(&vsi->mac_filter_list_lock);
9062 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009063 } else {
9064 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9065 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9066 pf->vsi[pf->lan_vsi]->netdev->name);
9067 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009068
9069 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009070 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009071 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009072 }
Kiran Patil21659032015-09-30 14:09:03 -04009073
9074 spin_lock_bh(&vsi->mac_filter_list_lock);
Greg Rose1a103702013-11-28 06:42:39 +00009075 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009076 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009077
Greg Rose9a173902014-05-22 06:32:02 +00009078 ether_addr_copy(netdev->dev_addr, mac_addr);
9079 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009080 /* vlan gets same features (except vlan offload)
9081 * after any tweaks for specific VSI types
9082 */
9083 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
9084 NETIF_F_HW_VLAN_CTAG_RX |
9085 NETIF_F_HW_VLAN_CTAG_FILTER);
9086 netdev->priv_flags |= IFF_UNICAST_FLT;
9087 netdev->priv_flags |= IFF_SUPP_NOFCS;
9088 /* Setup netdev TC information */
9089 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9090
9091 netdev->netdev_ops = &i40e_netdev_ops;
9092 netdev->watchdog_timeo = 5 * HZ;
9093 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009094#ifdef I40E_FCOE
9095 i40e_fcoe_config_netdev(netdev, vsi);
9096#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009097
9098 return 0;
9099}
9100
9101/**
9102 * i40e_vsi_delete - Delete a VSI from the switch
9103 * @vsi: the VSI being removed
9104 *
9105 * Returns 0 on success, negative value on failure
9106 **/
9107static void i40e_vsi_delete(struct i40e_vsi *vsi)
9108{
9109 /* remove default VSI is not allowed */
9110 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9111 return;
9112
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009113 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009114}
9115
9116/**
Neerav Parikh51616012015-02-06 08:52:14 +00009117 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9118 * @vsi: the VSI being queried
9119 *
9120 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9121 **/
9122int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9123{
9124 struct i40e_veb *veb;
9125 struct i40e_pf *pf = vsi->back;
9126
9127 /* Uplink is not a bridge so default to VEB */
9128 if (vsi->veb_idx == I40E_NO_VEB)
9129 return 1;
9130
9131 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009132 if (!veb) {
9133 dev_info(&pf->pdev->dev,
9134 "There is no veb associated with the bridge\n");
9135 return -ENOENT;
9136 }
Neerav Parikh51616012015-02-06 08:52:14 +00009137
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009138 /* Uplink is a bridge in VEPA mode */
9139 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9140 return 0;
9141 } else {
9142 /* Uplink is a bridge in VEB mode */
9143 return 1;
9144 }
9145
9146 /* VEPA is now default bridge, so return 0 */
9147 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009148}
9149
9150/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009151 * i40e_add_vsi - Add a VSI to the switch
9152 * @vsi: the VSI being configured
9153 *
9154 * This initializes a VSI context depending on the VSI type to be added and
9155 * passes it down to the add_vsi aq command.
9156 **/
9157static int i40e_add_vsi(struct i40e_vsi *vsi)
9158{
9159 int ret = -ENODEV;
Kiran Patil21659032015-09-30 14:09:03 -04009160 u8 laa_macaddr[ETH_ALEN];
9161 bool found_laa_mac_filter = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009162 struct i40e_pf *pf = vsi->back;
9163 struct i40e_hw *hw = &pf->hw;
9164 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009165 struct i40e_mac_filter *f, *ftmp;
9166
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009167 u8 enabled_tc = 0x1; /* TC0 enabled */
9168 int f_count = 0;
9169
9170 memset(&ctxt, 0, sizeof(ctxt));
9171 switch (vsi->type) {
9172 case I40E_VSI_MAIN:
9173 /* The PF's main VSI is already setup as part of the
9174 * device initialization, so we'll not bother with
9175 * the add_vsi call, but we will retrieve the current
9176 * VSI context.
9177 */
9178 ctxt.seid = pf->main_vsi_seid;
9179 ctxt.pf_num = pf->hw.pf_id;
9180 ctxt.vf_num = 0;
9181 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9182 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9183 if (ret) {
9184 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009185 "couldn't get PF vsi config, err %s aq_err %s\n",
9186 i40e_stat_str(&pf->hw, ret),
9187 i40e_aq_str(&pf->hw,
9188 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009189 return -ENOENT;
9190 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009191 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009192 vsi->info.valid_sections = 0;
9193
9194 vsi->seid = ctxt.seid;
9195 vsi->id = ctxt.vsi_number;
9196
9197 enabled_tc = i40e_pf_get_tc_map(pf);
9198
9199 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009200 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9201 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009202 memset(&ctxt, 0, sizeof(ctxt));
9203 ctxt.seid = pf->main_vsi_seid;
9204 ctxt.pf_num = pf->hw.pf_id;
9205 ctxt.vf_num = 0;
9206 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9207 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9208 if (ret) {
9209 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009210 "update vsi failed, err %s aq_err %s\n",
9211 i40e_stat_str(&pf->hw, ret),
9212 i40e_aq_str(&pf->hw,
9213 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009214 ret = -ENOENT;
9215 goto err;
9216 }
9217 /* update the local VSI info queue map */
9218 i40e_vsi_update_queue_map(vsi, &ctxt);
9219 vsi->info.valid_sections = 0;
9220 } else {
9221 /* Default/Main VSI is only enabled for TC0
9222 * reconfigure it to enable all TCs that are
9223 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009224 * For MFP case the iSCSI PF would use this
9225 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009226 */
9227 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9228 if (ret) {
9229 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009230 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9231 enabled_tc,
9232 i40e_stat_str(&pf->hw, ret),
9233 i40e_aq_str(&pf->hw,
9234 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009235 ret = -ENOENT;
9236 }
9237 }
9238 break;
9239
9240 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009241 ctxt.pf_num = hw->pf_id;
9242 ctxt.vf_num = 0;
9243 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009244 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009245 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009246 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9247 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009248 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009249 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009250 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009251 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009252 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009253 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009254 break;
9255
9256 case I40E_VSI_VMDQ2:
9257 ctxt.pf_num = hw->pf_id;
9258 ctxt.vf_num = 0;
9259 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009260 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009261 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9262
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009263 /* This VSI is connected to VEB so the switch_id
9264 * should be set to zero by default.
9265 */
Neerav Parikh51616012015-02-06 08:52:14 +00009266 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9267 ctxt.info.valid_sections |=
9268 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9269 ctxt.info.switch_id =
9270 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9271 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009272
9273 /* Setup the VSI tx/rx queue map for TC0 only for now */
9274 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9275 break;
9276
9277 case I40E_VSI_SRIOV:
9278 ctxt.pf_num = hw->pf_id;
9279 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9280 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009281 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009282 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009284 /* This VSI is connected to VEB so the switch_id
9285 * should be set to zero by default.
9286 */
Neerav Parikh51616012015-02-06 08:52:14 +00009287 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9288 ctxt.info.valid_sections |=
9289 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9290 ctxt.info.switch_id =
9291 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9292 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009293
9294 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9295 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009296 if (pf->vf[vsi->vf_id].spoofchk) {
9297 ctxt.info.valid_sections |=
9298 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9299 ctxt.info.sec_flags |=
9300 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9301 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9302 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009303 /* Setup the VSI tx/rx queue map for TC0 only for now */
9304 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9305 break;
9306
Vasu Dev38e00432014-08-01 13:27:03 -07009307#ifdef I40E_FCOE
9308 case I40E_VSI_FCOE:
9309 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9310 if (ret) {
9311 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9312 return ret;
9313 }
9314 break;
9315
9316#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009317 default:
9318 return -ENODEV;
9319 }
9320
9321 if (vsi->type != I40E_VSI_MAIN) {
9322 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9323 if (ret) {
9324 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009325 "add vsi failed, err %s aq_err %s\n",
9326 i40e_stat_str(&pf->hw, ret),
9327 i40e_aq_str(&pf->hw,
9328 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009329 ret = -ENOENT;
9330 goto err;
9331 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009332 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009333 vsi->info.valid_sections = 0;
9334 vsi->seid = ctxt.seid;
9335 vsi->id = ctxt.vsi_number;
9336 }
9337
Kiran Patil21659032015-09-30 14:09:03 -04009338 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009339 /* If macvlan filters already exist, force them to get loaded */
9340 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
9341 f->changed = true;
9342 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009343
Kiran Patil21659032015-09-30 14:09:03 -04009344 /* Expected to have only one MAC filter entry for LAA in list */
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009345 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Kiran Patil21659032015-09-30 14:09:03 -04009346 ether_addr_copy(laa_macaddr, f->macaddr);
9347 found_laa_mac_filter = true;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009348 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009349 }
Kiran Patil21659032015-09-30 14:09:03 -04009350 spin_unlock_bh(&vsi->mac_filter_list_lock);
9351
9352 if (found_laa_mac_filter) {
9353 struct i40e_aqc_remove_macvlan_element_data element;
9354
9355 memset(&element, 0, sizeof(element));
9356 ether_addr_copy(element.mac_addr, laa_macaddr);
9357 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
9358 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
9359 &element, 1, NULL);
9360 if (ret) {
9361 /* some older FW has a different default */
9362 element.flags |=
9363 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
9364 i40e_aq_remove_macvlan(hw, vsi->seid,
9365 &element, 1, NULL);
9366 }
9367
9368 i40e_aq_mac_address_write(hw,
9369 I40E_AQC_WRITE_TYPE_LAA_WOL,
9370 laa_macaddr, NULL);
9371 }
9372
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009373 if (f_count) {
9374 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9375 pf->flags |= I40E_FLAG_FILTER_SYNC;
9376 }
9377
9378 /* Update VSI BW information */
9379 ret = i40e_vsi_get_bw_info(vsi);
9380 if (ret) {
9381 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009382 "couldn't get vsi bw info, err %s aq_err %s\n",
9383 i40e_stat_str(&pf->hw, ret),
9384 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009385 /* VSI is already added so not tearing that up */
9386 ret = 0;
9387 }
9388
9389err:
9390 return ret;
9391}
9392
9393/**
9394 * i40e_vsi_release - Delete a VSI and free its resources
9395 * @vsi: the VSI being removed
9396 *
9397 * Returns 0 on success or < 0 on error
9398 **/
9399int i40e_vsi_release(struct i40e_vsi *vsi)
9400{
9401 struct i40e_mac_filter *f, *ftmp;
9402 struct i40e_veb *veb = NULL;
9403 struct i40e_pf *pf;
9404 u16 uplink_seid;
9405 int i, n;
9406
9407 pf = vsi->back;
9408
9409 /* release of a VEB-owner or last VSI is not allowed */
9410 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9411 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9412 vsi->seid, vsi->uplink_seid);
9413 return -ENODEV;
9414 }
9415 if (vsi == pf->vsi[pf->lan_vsi] &&
9416 !test_bit(__I40E_DOWN, &pf->state)) {
9417 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9418 return -ENODEV;
9419 }
9420
9421 uplink_seid = vsi->uplink_seid;
9422 if (vsi->type != I40E_VSI_SRIOV) {
9423 if (vsi->netdev_registered) {
9424 vsi->netdev_registered = false;
9425 if (vsi->netdev) {
9426 /* results in a call to i40e_close() */
9427 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009428 }
9429 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009430 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009431 }
9432 i40e_vsi_disable_irq(vsi);
9433 }
9434
Kiran Patil21659032015-09-30 14:09:03 -04009435 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9437 i40e_del_filter(vsi, f->macaddr, f->vlan,
9438 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009439 spin_unlock_bh(&vsi->mac_filter_list_lock);
9440
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009441 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009442
9443 i40e_vsi_delete(vsi);
9444 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009445 if (vsi->netdev) {
9446 free_netdev(vsi->netdev);
9447 vsi->netdev = NULL;
9448 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009449 i40e_vsi_clear_rings(vsi);
9450 i40e_vsi_clear(vsi);
9451
9452 /* If this was the last thing on the VEB, except for the
9453 * controlling VSI, remove the VEB, which puts the controlling
9454 * VSI onto the next level down in the switch.
9455 *
9456 * Well, okay, there's one more exception here: don't remove
9457 * the orphan VEBs yet. We'll wait for an explicit remove request
9458 * from up the network stack.
9459 */
Mitch Williams505682c2014-05-20 08:01:37 +00009460 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009461 if (pf->vsi[i] &&
9462 pf->vsi[i]->uplink_seid == uplink_seid &&
9463 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9464 n++; /* count the VSIs */
9465 }
9466 }
9467 for (i = 0; i < I40E_MAX_VEB; i++) {
9468 if (!pf->veb[i])
9469 continue;
9470 if (pf->veb[i]->uplink_seid == uplink_seid)
9471 n++; /* count the VEBs */
9472 if (pf->veb[i]->seid == uplink_seid)
9473 veb = pf->veb[i];
9474 }
9475 if (n == 0 && veb && veb->uplink_seid != 0)
9476 i40e_veb_release(veb);
9477
9478 return 0;
9479}
9480
9481/**
9482 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9483 * @vsi: ptr to the VSI
9484 *
9485 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9486 * corresponding SW VSI structure and initializes num_queue_pairs for the
9487 * newly allocated VSI.
9488 *
9489 * Returns 0 on success or negative on failure
9490 **/
9491static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9492{
9493 int ret = -ENOENT;
9494 struct i40e_pf *pf = vsi->back;
9495
Alexander Duyck493fb302013-09-28 07:01:44 +00009496 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009497 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9498 vsi->seid);
9499 return -EEXIST;
9500 }
9501
9502 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009503 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009504 vsi->seid, vsi->base_vector);
9505 return -EEXIST;
9506 }
9507
Greg Rose90e04072014-03-06 08:59:57 +00009508 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009509 if (ret) {
9510 dev_info(&pf->pdev->dev,
9511 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9512 vsi->num_q_vectors, vsi->seid, ret);
9513 vsi->num_q_vectors = 0;
9514 goto vector_setup_out;
9515 }
9516
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009517 /* In Legacy mode, we do not have to get any other vector since we
9518 * piggyback on the misc/ICR0 for queue interrupts.
9519 */
9520 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9521 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009522 if (vsi->num_q_vectors)
9523 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9524 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009525 if (vsi->base_vector < 0) {
9526 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009527 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9528 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009529 i40e_vsi_free_q_vectors(vsi);
9530 ret = -ENOENT;
9531 goto vector_setup_out;
9532 }
9533
9534vector_setup_out:
9535 return ret;
9536}
9537
9538/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009539 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9540 * @vsi: pointer to the vsi.
9541 *
9542 * This re-allocates a vsi's queue resources.
9543 *
9544 * Returns pointer to the successfully allocated and configured VSI sw struct
9545 * on success, otherwise returns NULL on failure.
9546 **/
9547static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9548{
9549 struct i40e_pf *pf = vsi->back;
9550 u8 enabled_tc;
9551 int ret;
9552
9553 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9554 i40e_vsi_clear_rings(vsi);
9555
9556 i40e_vsi_free_arrays(vsi, false);
9557 i40e_set_num_rings_in_vsi(vsi);
9558 ret = i40e_vsi_alloc_arrays(vsi, false);
9559 if (ret)
9560 goto err_vsi;
9561
9562 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9563 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009564 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009565 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009566 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009567 goto err_vsi;
9568 }
9569 vsi->base_queue = ret;
9570
9571 /* Update the FW view of the VSI. Force a reset of TC and queue
9572 * layout configurations.
9573 */
9574 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9575 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9576 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9577 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9578
9579 /* assign it some queues */
9580 ret = i40e_alloc_rings(vsi);
9581 if (ret)
9582 goto err_rings;
9583
9584 /* map all of the rings to the q_vectors */
9585 i40e_vsi_map_rings_to_vectors(vsi);
9586 return vsi;
9587
9588err_rings:
9589 i40e_vsi_free_q_vectors(vsi);
9590 if (vsi->netdev_registered) {
9591 vsi->netdev_registered = false;
9592 unregister_netdev(vsi->netdev);
9593 free_netdev(vsi->netdev);
9594 vsi->netdev = NULL;
9595 }
9596 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9597err_vsi:
9598 i40e_vsi_clear(vsi);
9599 return NULL;
9600}
9601
9602/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009603 * i40e_macaddr_init - explicitly write the mac address filters.
9604 *
9605 * @vsi: pointer to the vsi.
9606 * @macaddr: the MAC address
9607 *
9608 * This is needed when the macaddr has been obtained by other
9609 * means than the default, e.g., from Open Firmware or IDPROM.
9610 * Returns 0 on success, negative on failure
9611 **/
9612static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
9613{
9614 int ret;
9615 struct i40e_aqc_add_macvlan_element_data element;
9616
9617 ret = i40e_aq_mac_address_write(&vsi->back->hw,
9618 I40E_AQC_WRITE_TYPE_LAA_WOL,
9619 macaddr, NULL);
9620 if (ret) {
9621 dev_info(&vsi->back->pdev->dev,
9622 "Addr change for VSI failed: %d\n", ret);
9623 return -EADDRNOTAVAIL;
9624 }
9625
9626 memset(&element, 0, sizeof(element));
9627 ether_addr_copy(element.mac_addr, macaddr);
9628 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
9629 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
9630 if (ret) {
9631 dev_info(&vsi->back->pdev->dev,
9632 "add filter failed err %s aq_err %s\n",
9633 i40e_stat_str(&vsi->back->hw, ret),
9634 i40e_aq_str(&vsi->back->hw,
9635 vsi->back->hw.aq.asq_last_status));
9636 }
9637 return ret;
9638}
9639
9640/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009641 * i40e_vsi_setup - Set up a VSI by a given type
9642 * @pf: board private structure
9643 * @type: VSI type
9644 * @uplink_seid: the switch element to link to
9645 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9646 *
9647 * This allocates the sw VSI structure and its queue resources, then add a VSI
9648 * to the identified VEB.
9649 *
9650 * Returns pointer to the successfully allocated and configure VSI sw struct on
9651 * success, otherwise returns NULL on failure.
9652 **/
9653struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9654 u16 uplink_seid, u32 param1)
9655{
9656 struct i40e_vsi *vsi = NULL;
9657 struct i40e_veb *veb = NULL;
9658 int ret, i;
9659 int v_idx;
9660
9661 /* The requested uplink_seid must be either
9662 * - the PF's port seid
9663 * no VEB is needed because this is the PF
9664 * or this is a Flow Director special case VSI
9665 * - seid of an existing VEB
9666 * - seid of a VSI that owns an existing VEB
9667 * - seid of a VSI that doesn't own a VEB
9668 * a new VEB is created and the VSI becomes the owner
9669 * - seid of the PF VSI, which is what creates the first VEB
9670 * this is a special case of the previous
9671 *
9672 * Find which uplink_seid we were given and create a new VEB if needed
9673 */
9674 for (i = 0; i < I40E_MAX_VEB; i++) {
9675 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9676 veb = pf->veb[i];
9677 break;
9678 }
9679 }
9680
9681 if (!veb && uplink_seid != pf->mac_seid) {
9682
Mitch Williams505682c2014-05-20 08:01:37 +00009683 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009684 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9685 vsi = pf->vsi[i];
9686 break;
9687 }
9688 }
9689 if (!vsi) {
9690 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9691 uplink_seid);
9692 return NULL;
9693 }
9694
9695 if (vsi->uplink_seid == pf->mac_seid)
9696 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9697 vsi->tc_config.enabled_tc);
9698 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9699 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9700 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009701 if (veb) {
9702 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9703 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009704 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009705 return NULL;
9706 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009707 /* We come up by default in VEPA mode if SRIOV is not
9708 * already enabled, in which case we can't force VEPA
9709 * mode.
9710 */
9711 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9712 veb->bridge_mode = BRIDGE_MODE_VEPA;
9713 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9714 }
Neerav Parikh51616012015-02-06 08:52:14 +00009715 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009716 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009717 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9718 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9719 veb = pf->veb[i];
9720 }
9721 if (!veb) {
9722 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9723 return NULL;
9724 }
9725
9726 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9727 uplink_seid = veb->seid;
9728 }
9729
9730 /* get vsi sw struct */
9731 v_idx = i40e_vsi_mem_alloc(pf, type);
9732 if (v_idx < 0)
9733 goto err_alloc;
9734 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009735 if (!vsi)
9736 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009737 vsi->type = type;
9738 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9739
9740 if (type == I40E_VSI_MAIN)
9741 pf->lan_vsi = v_idx;
9742 else if (type == I40E_VSI_SRIOV)
9743 vsi->vf_id = param1;
9744 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009745 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9746 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009747 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009748 dev_info(&pf->pdev->dev,
9749 "failed to get tracking for %d queues for VSI %d err=%d\n",
9750 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009751 goto err_vsi;
9752 }
9753 vsi->base_queue = ret;
9754
9755 /* get a VSI from the hardware */
9756 vsi->uplink_seid = uplink_seid;
9757 ret = i40e_add_vsi(vsi);
9758 if (ret)
9759 goto err_vsi;
9760
9761 switch (vsi->type) {
9762 /* setup the netdev if needed */
9763 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009764 /* Apply relevant filters if a platform-specific mac
9765 * address was selected.
9766 */
9767 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9768 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9769 if (ret) {
9770 dev_warn(&pf->pdev->dev,
9771 "could not set up macaddr; err %d\n",
9772 ret);
9773 }
9774 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009775 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009776 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009777 ret = i40e_config_netdev(vsi);
9778 if (ret)
9779 goto err_netdev;
9780 ret = register_netdev(vsi->netdev);
9781 if (ret)
9782 goto err_netdev;
9783 vsi->netdev_registered = true;
9784 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009785#ifdef CONFIG_I40E_DCB
9786 /* Setup DCB netlink interface */
9787 i40e_dcbnl_setup(vsi);
9788#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009789 /* fall through */
9790
9791 case I40E_VSI_FDIR:
9792 /* set up vectors and rings if needed */
9793 ret = i40e_vsi_setup_vectors(vsi);
9794 if (ret)
9795 goto err_msix;
9796
9797 ret = i40e_alloc_rings(vsi);
9798 if (ret)
9799 goto err_rings;
9800
9801 /* map all of the rings to the q_vectors */
9802 i40e_vsi_map_rings_to_vectors(vsi);
9803
9804 i40e_vsi_reset_stats(vsi);
9805 break;
9806
9807 default:
9808 /* no netdev or rings for the other VSI types */
9809 break;
9810 }
9811
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009812 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9813 (vsi->type == I40E_VSI_VMDQ2)) {
9814 ret = i40e_vsi_config_rss(vsi);
9815 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009816 return vsi;
9817
9818err_rings:
9819 i40e_vsi_free_q_vectors(vsi);
9820err_msix:
9821 if (vsi->netdev_registered) {
9822 vsi->netdev_registered = false;
9823 unregister_netdev(vsi->netdev);
9824 free_netdev(vsi->netdev);
9825 vsi->netdev = NULL;
9826 }
9827err_netdev:
9828 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9829err_vsi:
9830 i40e_vsi_clear(vsi);
9831err_alloc:
9832 return NULL;
9833}
9834
9835/**
9836 * i40e_veb_get_bw_info - Query VEB BW information
9837 * @veb: the veb to query
9838 *
9839 * Query the Tx scheduler BW configuration data for given VEB
9840 **/
9841static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9842{
9843 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9844 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9845 struct i40e_pf *pf = veb->pf;
9846 struct i40e_hw *hw = &pf->hw;
9847 u32 tc_bw_max;
9848 int ret = 0;
9849 int i;
9850
9851 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9852 &bw_data, NULL);
9853 if (ret) {
9854 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009855 "query veb bw config failed, err %s aq_err %s\n",
9856 i40e_stat_str(&pf->hw, ret),
9857 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009858 goto out;
9859 }
9860
9861 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9862 &ets_data, NULL);
9863 if (ret) {
9864 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009865 "query veb bw ets config failed, err %s aq_err %s\n",
9866 i40e_stat_str(&pf->hw, ret),
9867 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009868 goto out;
9869 }
9870
9871 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9872 veb->bw_max_quanta = ets_data.tc_bw_max;
9873 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009874 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009875 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9876 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9877 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9878 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9879 veb->bw_tc_limit_credits[i] =
9880 le16_to_cpu(bw_data.tc_bw_limits[i]);
9881 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9882 }
9883
9884out:
9885 return ret;
9886}
9887
9888/**
9889 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9890 * @pf: board private structure
9891 *
9892 * On error: returns error code (negative)
9893 * On success: returns vsi index in PF (positive)
9894 **/
9895static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9896{
9897 int ret = -ENOENT;
9898 struct i40e_veb *veb;
9899 int i;
9900
9901 /* Need to protect the allocation of switch elements at the PF level */
9902 mutex_lock(&pf->switch_mutex);
9903
9904 /* VEB list may be fragmented if VEB creation/destruction has
9905 * been happening. We can afford to do a quick scan to look
9906 * for any free slots in the list.
9907 *
9908 * find next empty veb slot, looping back around if necessary
9909 */
9910 i = 0;
9911 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9912 i++;
9913 if (i >= I40E_MAX_VEB) {
9914 ret = -ENOMEM;
9915 goto err_alloc_veb; /* out of VEB slots! */
9916 }
9917
9918 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9919 if (!veb) {
9920 ret = -ENOMEM;
9921 goto err_alloc_veb;
9922 }
9923 veb->pf = pf;
9924 veb->idx = i;
9925 veb->enabled_tc = 1;
9926
9927 pf->veb[i] = veb;
9928 ret = i;
9929err_alloc_veb:
9930 mutex_unlock(&pf->switch_mutex);
9931 return ret;
9932}
9933
9934/**
9935 * i40e_switch_branch_release - Delete a branch of the switch tree
9936 * @branch: where to start deleting
9937 *
9938 * This uses recursion to find the tips of the branch to be
9939 * removed, deleting until we get back to and can delete this VEB.
9940 **/
9941static void i40e_switch_branch_release(struct i40e_veb *branch)
9942{
9943 struct i40e_pf *pf = branch->pf;
9944 u16 branch_seid = branch->seid;
9945 u16 veb_idx = branch->idx;
9946 int i;
9947
9948 /* release any VEBs on this VEB - RECURSION */
9949 for (i = 0; i < I40E_MAX_VEB; i++) {
9950 if (!pf->veb[i])
9951 continue;
9952 if (pf->veb[i]->uplink_seid == branch->seid)
9953 i40e_switch_branch_release(pf->veb[i]);
9954 }
9955
9956 /* Release the VSIs on this VEB, but not the owner VSI.
9957 *
9958 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
9959 * the VEB itself, so don't use (*branch) after this loop.
9960 */
Mitch Williams505682c2014-05-20 08:01:37 +00009961 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009962 if (!pf->vsi[i])
9963 continue;
9964 if (pf->vsi[i]->uplink_seid == branch_seid &&
9965 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9966 i40e_vsi_release(pf->vsi[i]);
9967 }
9968 }
9969
9970 /* There's one corner case where the VEB might not have been
9971 * removed, so double check it here and remove it if needed.
9972 * This case happens if the veb was created from the debugfs
9973 * commands and no VSIs were added to it.
9974 */
9975 if (pf->veb[veb_idx])
9976 i40e_veb_release(pf->veb[veb_idx]);
9977}
9978
9979/**
9980 * i40e_veb_clear - remove veb struct
9981 * @veb: the veb to remove
9982 **/
9983static void i40e_veb_clear(struct i40e_veb *veb)
9984{
9985 if (!veb)
9986 return;
9987
9988 if (veb->pf) {
9989 struct i40e_pf *pf = veb->pf;
9990
9991 mutex_lock(&pf->switch_mutex);
9992 if (pf->veb[veb->idx] == veb)
9993 pf->veb[veb->idx] = NULL;
9994 mutex_unlock(&pf->switch_mutex);
9995 }
9996
9997 kfree(veb);
9998}
9999
10000/**
10001 * i40e_veb_release - Delete a VEB and free its resources
10002 * @veb: the VEB being removed
10003 **/
10004void i40e_veb_release(struct i40e_veb *veb)
10005{
10006 struct i40e_vsi *vsi = NULL;
10007 struct i40e_pf *pf;
10008 int i, n = 0;
10009
10010 pf = veb->pf;
10011
10012 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010013 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010014 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10015 n++;
10016 vsi = pf->vsi[i];
10017 }
10018 }
10019 if (n != 1) {
10020 dev_info(&pf->pdev->dev,
10021 "can't remove VEB %d with %d VSIs left\n",
10022 veb->seid, n);
10023 return;
10024 }
10025
10026 /* move the remaining VSI to uplink veb */
10027 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10028 if (veb->uplink_seid) {
10029 vsi->uplink_seid = veb->uplink_seid;
10030 if (veb->uplink_seid == pf->mac_seid)
10031 vsi->veb_idx = I40E_NO_VEB;
10032 else
10033 vsi->veb_idx = veb->veb_idx;
10034 } else {
10035 /* floating VEB */
10036 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10037 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10038 }
10039
10040 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10041 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010042}
10043
10044/**
10045 * i40e_add_veb - create the VEB in the switch
10046 * @veb: the VEB to be instantiated
10047 * @vsi: the controlling VSI
10048 **/
10049static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10050{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010051 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -040010052 bool is_default = veb->pf->cur_promisc;
Kevin Scotte1c51b952013-11-20 10:02:51 +000010053 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010054 int ret;
10055
10056 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010057 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +000010058 veb->enabled_tc, is_default,
10059 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010060 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010061 dev_info(&pf->pdev->dev,
10062 "couldn't add VEB, err %s aq_err %s\n",
10063 i40e_stat_str(&pf->hw, ret),
10064 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010065 return -EPERM;
10066 }
10067
10068 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010069 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010070 &veb->stats_idx, NULL, NULL, NULL);
10071 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010072 dev_info(&pf->pdev->dev,
10073 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10074 i40e_stat_str(&pf->hw, ret),
10075 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010076 return -EPERM;
10077 }
10078 ret = i40e_veb_get_bw_info(veb);
10079 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010080 dev_info(&pf->pdev->dev,
10081 "couldn't get VEB bw info, err %s aq_err %s\n",
10082 i40e_stat_str(&pf->hw, ret),
10083 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10084 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010085 return -ENOENT;
10086 }
10087
10088 vsi->uplink_seid = veb->seid;
10089 vsi->veb_idx = veb->idx;
10090 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10091
10092 return 0;
10093}
10094
10095/**
10096 * i40e_veb_setup - Set up a VEB
10097 * @pf: board private structure
10098 * @flags: VEB setup flags
10099 * @uplink_seid: the switch element to link to
10100 * @vsi_seid: the initial VSI seid
10101 * @enabled_tc: Enabled TC bit-map
10102 *
10103 * This allocates the sw VEB structure and links it into the switch
10104 * It is possible and legal for this to be a duplicate of an already
10105 * existing VEB. It is also possible for both uplink and vsi seids
10106 * to be zero, in order to create a floating VEB.
10107 *
10108 * Returns pointer to the successfully allocated VEB sw struct on
10109 * success, otherwise returns NULL on failure.
10110 **/
10111struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10112 u16 uplink_seid, u16 vsi_seid,
10113 u8 enabled_tc)
10114{
10115 struct i40e_veb *veb, *uplink_veb = NULL;
10116 int vsi_idx, veb_idx;
10117 int ret;
10118
10119 /* if one seid is 0, the other must be 0 to create a floating relay */
10120 if ((uplink_seid == 0 || vsi_seid == 0) &&
10121 (uplink_seid + vsi_seid != 0)) {
10122 dev_info(&pf->pdev->dev,
10123 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10124 uplink_seid, vsi_seid);
10125 return NULL;
10126 }
10127
10128 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010129 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010130 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10131 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010132 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010133 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10134 vsi_seid);
10135 return NULL;
10136 }
10137
10138 if (uplink_seid && uplink_seid != pf->mac_seid) {
10139 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10140 if (pf->veb[veb_idx] &&
10141 pf->veb[veb_idx]->seid == uplink_seid) {
10142 uplink_veb = pf->veb[veb_idx];
10143 break;
10144 }
10145 }
10146 if (!uplink_veb) {
10147 dev_info(&pf->pdev->dev,
10148 "uplink seid %d not found\n", uplink_seid);
10149 return NULL;
10150 }
10151 }
10152
10153 /* get veb sw struct */
10154 veb_idx = i40e_veb_mem_alloc(pf);
10155 if (veb_idx < 0)
10156 goto err_alloc;
10157 veb = pf->veb[veb_idx];
10158 veb->flags = flags;
10159 veb->uplink_seid = uplink_seid;
10160 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10161 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10162
10163 /* create the VEB in the switch */
10164 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10165 if (ret)
10166 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010167 if (vsi_idx == pf->lan_vsi)
10168 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010169
10170 return veb;
10171
10172err_veb:
10173 i40e_veb_clear(veb);
10174err_alloc:
10175 return NULL;
10176}
10177
10178/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010179 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010180 * @pf: board private structure
10181 * @ele: element we are building info from
10182 * @num_reported: total number of elements
10183 * @printconfig: should we print the contents
10184 *
10185 * helper function to assist in extracting a few useful SEID values.
10186 **/
10187static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10188 struct i40e_aqc_switch_config_element_resp *ele,
10189 u16 num_reported, bool printconfig)
10190{
10191 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10192 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10193 u8 element_type = ele->element_type;
10194 u16 seid = le16_to_cpu(ele->seid);
10195
10196 if (printconfig)
10197 dev_info(&pf->pdev->dev,
10198 "type=%d seid=%d uplink=%d downlink=%d\n",
10199 element_type, seid, uplink_seid, downlink_seid);
10200
10201 switch (element_type) {
10202 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10203 pf->mac_seid = seid;
10204 break;
10205 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10206 /* Main VEB? */
10207 if (uplink_seid != pf->mac_seid)
10208 break;
10209 if (pf->lan_veb == I40E_NO_VEB) {
10210 int v;
10211
10212 /* find existing or else empty VEB */
10213 for (v = 0; v < I40E_MAX_VEB; v++) {
10214 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10215 pf->lan_veb = v;
10216 break;
10217 }
10218 }
10219 if (pf->lan_veb == I40E_NO_VEB) {
10220 v = i40e_veb_mem_alloc(pf);
10221 if (v < 0)
10222 break;
10223 pf->lan_veb = v;
10224 }
10225 }
10226
10227 pf->veb[pf->lan_veb]->seid = seid;
10228 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10229 pf->veb[pf->lan_veb]->pf = pf;
10230 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10231 break;
10232 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10233 if (num_reported != 1)
10234 break;
10235 /* This is immediately after a reset so we can assume this is
10236 * the PF's VSI
10237 */
10238 pf->mac_seid = uplink_seid;
10239 pf->pf_seid = downlink_seid;
10240 pf->main_vsi_seid = seid;
10241 if (printconfig)
10242 dev_info(&pf->pdev->dev,
10243 "pf_seid=%d main_vsi_seid=%d\n",
10244 pf->pf_seid, pf->main_vsi_seid);
10245 break;
10246 case I40E_SWITCH_ELEMENT_TYPE_PF:
10247 case I40E_SWITCH_ELEMENT_TYPE_VF:
10248 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10249 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10250 case I40E_SWITCH_ELEMENT_TYPE_PE:
10251 case I40E_SWITCH_ELEMENT_TYPE_PA:
10252 /* ignore these for now */
10253 break;
10254 default:
10255 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10256 element_type, seid);
10257 break;
10258 }
10259}
10260
10261/**
10262 * i40e_fetch_switch_configuration - Get switch config from firmware
10263 * @pf: board private structure
10264 * @printconfig: should we print the contents
10265 *
10266 * Get the current switch configuration from the device and
10267 * extract a few useful SEID values.
10268 **/
10269int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10270{
10271 struct i40e_aqc_get_switch_config_resp *sw_config;
10272 u16 next_seid = 0;
10273 int ret = 0;
10274 u8 *aq_buf;
10275 int i;
10276
10277 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10278 if (!aq_buf)
10279 return -ENOMEM;
10280
10281 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10282 do {
10283 u16 num_reported, num_total;
10284
10285 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10286 I40E_AQ_LARGE_BUF,
10287 &next_seid, NULL);
10288 if (ret) {
10289 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010290 "get switch config failed err %s aq_err %s\n",
10291 i40e_stat_str(&pf->hw, ret),
10292 i40e_aq_str(&pf->hw,
10293 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010294 kfree(aq_buf);
10295 return -ENOENT;
10296 }
10297
10298 num_reported = le16_to_cpu(sw_config->header.num_reported);
10299 num_total = le16_to_cpu(sw_config->header.num_total);
10300
10301 if (printconfig)
10302 dev_info(&pf->pdev->dev,
10303 "header: %d reported %d total\n",
10304 num_reported, num_total);
10305
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010306 for (i = 0; i < num_reported; i++) {
10307 struct i40e_aqc_switch_config_element_resp *ele =
10308 &sw_config->element[i];
10309
10310 i40e_setup_pf_switch_element(pf, ele, num_reported,
10311 printconfig);
10312 }
10313 } while (next_seid != 0);
10314
10315 kfree(aq_buf);
10316 return ret;
10317}
10318
10319/**
10320 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10321 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010322 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010323 *
10324 * Returns 0 on success, negative value on failure
10325 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010326static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010327{
10328 int ret;
10329
10330 /* find out what's out there already */
10331 ret = i40e_fetch_switch_configuration(pf, false);
10332 if (ret) {
10333 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010334 "couldn't fetch switch config, err %s aq_err %s\n",
10335 i40e_stat_str(&pf->hw, ret),
10336 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010337 return ret;
10338 }
10339 i40e_pf_reset_stats(pf);
10340
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010341 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010342 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010343 struct i40e_vsi *vsi = NULL;
10344 u16 uplink_seid;
10345
10346 /* Set up the PF VSI associated with the PF's main VSI
10347 * that is already in the HW switch
10348 */
10349 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10350 uplink_seid = pf->veb[pf->lan_veb]->seid;
10351 else
10352 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010353 if (pf->lan_vsi == I40E_NO_VSI)
10354 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10355 else if (reinit)
10356 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010357 if (!vsi) {
10358 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10359 i40e_fdir_teardown(pf);
10360 return -EAGAIN;
10361 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010362 } else {
10363 /* force a reset of TC and queue layout configurations */
10364 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010365
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010366 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10367 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10368 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10369 }
10370 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10371
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010372 i40e_fdir_sb_setup(pf);
10373
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010374 /* Setup static PF queue filter control settings */
10375 ret = i40e_setup_pf_filter_control(pf);
10376 if (ret) {
10377 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10378 ret);
10379 /* Failure here should not stop continuing other steps */
10380 }
10381
10382 /* enable RSS in the HW, even for only one queue, as the stack can use
10383 * the hash
10384 */
10385 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010386 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010387
10388 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010389 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010390 i40e_link_event(pf);
10391
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010392 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010393 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10394 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010395
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010396 i40e_ptp_init(pf);
10397
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010398 return ret;
10399}
10400
10401/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010402 * i40e_determine_queue_usage - Work out queue distribution
10403 * @pf: board private structure
10404 **/
10405static void i40e_determine_queue_usage(struct i40e_pf *pf)
10406{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010407 int queues_left;
10408
10409 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010410#ifdef I40E_FCOE
10411 pf->num_fcoe_qps = 0;
10412#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010413
10414 /* Find the max queues to be put into basic use. We'll always be
10415 * using TC0, whether or not DCB is running, and TC0 will get the
10416 * big RSS set.
10417 */
10418 queues_left = pf->hw.func_caps.num_tx_qp;
10419
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010420 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010421 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010422 /* one qp for PF, no queues for anything else */
10423 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010424 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010425
10426 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010427 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010428#ifdef I40E_FCOE
10429 I40E_FLAG_FCOE_ENABLED |
10430#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010431 I40E_FLAG_FD_SB_ENABLED |
10432 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010433 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010434 I40E_FLAG_SRIOV_ENABLED |
10435 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010436 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10437 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010438 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010439 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010440 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010441 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010442 queues_left -= pf->num_lan_qps;
10443
10444 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010445#ifdef I40E_FCOE
10446 I40E_FLAG_FCOE_ENABLED |
10447#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010448 I40E_FLAG_FD_SB_ENABLED |
10449 I40E_FLAG_FD_ATR_ENABLED |
10450 I40E_FLAG_DCB_ENABLED |
10451 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010452 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010453 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010454 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010455 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010456 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010457 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10458 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010459 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10460 num_online_cpus());
10461 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10462 pf->hw.func_caps.num_tx_qp);
10463
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010464 queues_left -= pf->num_lan_qps;
10465 }
10466
Vasu Dev38e00432014-08-01 13:27:03 -070010467#ifdef I40E_FCOE
10468 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10469 if (I40E_DEFAULT_FCOE <= queues_left) {
10470 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10471 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10472 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10473 } else {
10474 pf->num_fcoe_qps = 0;
10475 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10476 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10477 }
10478
10479 queues_left -= pf->num_fcoe_qps;
10480 }
10481
10482#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010483 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10484 if (queues_left > 1) {
10485 queues_left -= 1; /* save 1 queue for FD */
10486 } else {
10487 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10488 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10489 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010490 }
10491
10492 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10493 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010494 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10495 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010496 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10497 }
10498
10499 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10500 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10501 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10502 (queues_left / pf->num_vmdq_qps));
10503 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10504 }
10505
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010506 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010507 dev_dbg(&pf->pdev->dev,
10508 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10509 pf->hw.func_caps.num_tx_qp,
10510 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010511 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10512 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10513 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010514#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010515 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010516#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010517}
10518
10519/**
10520 * i40e_setup_pf_filter_control - Setup PF static filter control
10521 * @pf: PF to be setup
10522 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010523 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010524 * settings. If PE/FCoE are enabled then it will also set the per PF
10525 * based filter sizes required for them. It also enables Flow director,
10526 * ethertype and macvlan type filter settings for the pf.
10527 *
10528 * Returns 0 on success, negative on failure
10529 **/
10530static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10531{
10532 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10533
10534 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10535
10536 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010537 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010538 settings->enable_fdir = true;
10539
10540 /* Ethtype and MACVLAN filters enabled for PF */
10541 settings->enable_ethtype = true;
10542 settings->enable_macvlan = true;
10543
10544 if (i40e_set_filter_control(&pf->hw, settings))
10545 return -ENOENT;
10546
10547 return 0;
10548}
10549
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010550#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010551#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010552static void i40e_print_features(struct i40e_pf *pf)
10553{
10554 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010555 char *buf;
10556 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010557
Joe Perches3b195842015-12-03 04:20:57 -080010558 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10559 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010560 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010561
Joe Perches3b195842015-12-03 04:20:57 -080010562 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010563#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010564 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010565#endif
Joe Perches3b195842015-12-03 04:20:57 -080010566 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d RX: %s",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010567 pf->hw.func_caps.num_vsis,
10568 pf->vsi[pf->lan_vsi]->num_queue_pairs,
10569 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010570
10571 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010572 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010573 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010574 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010575 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010576 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10577 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010578 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010579 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010580 i += snprintf(&buf[i], REMAIN(i), " DCB");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010581#if IS_ENABLED(CONFIG_VXLAN)
Joe Perches3b195842015-12-03 04:20:57 -080010582 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010583#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080010584#if IS_ENABLED(CONFIG_GENEVE)
10585 i += snprintf(&buf[i], REMAIN(i), " Geneve");
10586#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010587 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010588 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010589#ifdef I40E_FCOE
10590 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010591 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010592#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010593 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010594 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010595 else
Joe Perches3b195842015-12-03 04:20:57 -080010596 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010597
Joe Perches3b195842015-12-03 04:20:57 -080010598 dev_info(&pf->pdev->dev, "%s\n", buf);
10599 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010600 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010601}
10602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010603/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010604 * i40e_get_platform_mac_addr - get platform-specific MAC address
10605 *
10606 * @pdev: PCI device information struct
10607 * @pf: board private structure
10608 *
10609 * Look up the MAC address in Open Firmware on systems that support it,
10610 * and use IDPROM on SPARC if no OF address is found. On return, the
10611 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10612 * has been selected.
10613 **/
10614static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10615{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010616 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010617 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010618 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010619}
10620
10621/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010622 * i40e_probe - Device initialization routine
10623 * @pdev: PCI device information struct
10624 * @ent: entry in i40e_pci_tbl
10625 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010626 * i40e_probe initializes a PF identified by a pci_dev structure.
10627 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010628 * and a hardware reset occur.
10629 *
10630 * Returns 0 on success, negative on failure
10631 **/
10632static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10633{
Catherine Sullivane8278452015-02-06 08:52:08 +000010634 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010635 struct i40e_pf *pf;
10636 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010637 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010638 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010639 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010640 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010641 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010642 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010643 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010644
10645 err = pci_enable_device_mem(pdev);
10646 if (err)
10647 return err;
10648
10649 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010650 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010651 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010652 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10653 if (err) {
10654 dev_err(&pdev->dev,
10655 "DMA configuration failed: 0x%x\n", err);
10656 goto err_dma;
10657 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010658 }
10659
10660 /* set up pci connections */
10661 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
10662 IORESOURCE_MEM), i40e_driver_name);
10663 if (err) {
10664 dev_info(&pdev->dev,
10665 "pci_request_selected_regions failed %d\n", err);
10666 goto err_pci_reg;
10667 }
10668
10669 pci_enable_pcie_error_reporting(pdev);
10670 pci_set_master(pdev);
10671
10672 /* Now that we have a PCI connection, we need to do the
10673 * low level device setup. This is primarily setting up
10674 * the Admin Queue structures and then querying for the
10675 * device's current profile information.
10676 */
10677 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10678 if (!pf) {
10679 err = -ENOMEM;
10680 goto err_pf_alloc;
10681 }
10682 pf->next_vsi = 0;
10683 pf->pdev = pdev;
10684 set_bit(__I40E_DOWN, &pf->state);
10685
10686 hw = &pf->hw;
10687 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010688
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010689 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10690 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010691
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010692 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010693 if (!hw->hw_addr) {
10694 err = -EIO;
10695 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10696 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010697 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010698 goto err_ioremap;
10699 }
10700 hw->vendor_id = pdev->vendor;
10701 hw->device_id = pdev->device;
10702 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10703 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10704 hw->subsystem_device_id = pdev->subsystem_device;
10705 hw->bus.device = PCI_SLOT(pdev->devfn);
10706 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010707 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010708
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010709 if (debug != -1) {
10710 pf->msg_enable = pf->hw.debug_mask;
10711 pf->msg_enable = debug;
10712 }
10713
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010714 /* do a special CORER for clearing PXE mode once at init */
10715 if (hw->revision_id == 0 &&
10716 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10717 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10718 i40e_flush(hw);
10719 msleep(200);
10720 pf->corer_count++;
10721
10722 i40e_clear_pxe_mode(hw);
10723 }
10724
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010725 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010726 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010727 err = i40e_pf_reset(hw);
10728 if (err) {
10729 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10730 goto err_pf_reset;
10731 }
10732 pf->pfr_count++;
10733
10734 hw->aq.num_arq_entries = I40E_AQ_LEN;
10735 hw->aq.num_asq_entries = I40E_AQ_LEN;
10736 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10737 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10738 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010739
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010740 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010741 "%s-%s:misc",
10742 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010743
10744 err = i40e_init_shared_code(hw);
10745 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010746 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10747 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010748 goto err_pf_reset;
10749 }
10750
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010751 /* set up a default setting for link flow control */
10752 pf->hw.fc.requested_mode = I40E_FC_NONE;
10753
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080010754 /* set up the locks for the AQ, do this only once in probe
10755 * and destroy them only once in remove
10756 */
10757 mutex_init(&hw->aq.asq_mutex);
10758 mutex_init(&hw->aq.arq_mutex);
10759
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010760 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010761 if (err) {
10762 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10763 dev_info(&pdev->dev,
10764 "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");
10765 else
10766 dev_info(&pdev->dev,
10767 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10768
10769 goto err_pf_reset;
10770 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010771
Shannon Nelson6dec1012015-09-28 14:12:30 -040010772 /* provide nvm, fw, api versions */
10773 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10774 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10775 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10776 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010777
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010778 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10779 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010780 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010781 "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");
10782 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10783 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010784 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010785 "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 +000010786
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010787 i40e_verify_eeprom(pf);
10788
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010789 /* Rev 0 hardware was never productized */
10790 if (hw->revision_id < 1)
10791 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");
10792
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010793 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010794 err = i40e_get_capabilities(pf);
10795 if (err)
10796 goto err_adminq_setup;
10797
10798 err = i40e_sw_init(pf);
10799 if (err) {
10800 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10801 goto err_sw_init;
10802 }
10803
10804 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10805 hw->func_caps.num_rx_qp,
10806 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10807 if (err) {
10808 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10809 goto err_init_lan_hmc;
10810 }
10811
10812 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10813 if (err) {
10814 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10815 err = -ENOENT;
10816 goto err_configure_lan_hmc;
10817 }
10818
Neerav Parikhb686ece2014-12-14 01:55:11 +000010819 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10820 * Ignore error return codes because if it was already disabled via
10821 * hardware settings this will fail
10822 */
10823 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10824 (pf->hw.aq.fw_maj_ver < 4)) {
10825 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10826 i40e_aq_stop_lldp(hw, true, NULL);
10827 }
10828
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010829 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010830 /* allow a platform config to override the HW addr */
10831 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010832 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010833 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10834 err = -EIO;
10835 goto err_mac_addr;
10836 }
10837 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010838 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010839 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10840 if (is_valid_ether_addr(hw->mac.port_addr))
10841 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010842#ifdef I40E_FCOE
10843 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10844 if (err)
10845 dev_info(&pdev->dev,
10846 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10847 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10848 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10849 hw->mac.san_addr);
10850 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10851 }
10852 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10853#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010854
10855 pci_set_drvdata(pdev, pf);
10856 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010857#ifdef CONFIG_I40E_DCB
10858 err = i40e_init_pf_dcb(pf);
10859 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010860 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010861 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010862 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010863 }
10864#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010865
10866 /* set up periodic task facility */
10867 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10868 pf->service_timer_period = HZ;
10869
10870 INIT_WORK(&pf->service_task, i40e_service_task);
10871 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10872 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010873
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010874 /* NVM bit on means WoL disabled for the port */
10875 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010876 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010877 pf->wol_en = false;
10878 else
10879 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010880 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10881
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010882 /* set up the main switch operations */
10883 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010884 err = i40e_init_interrupt_scheme(pf);
10885 if (err)
10886 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010887
Mitch Williams505682c2014-05-20 08:01:37 +000010888 /* The number of VSIs reported by the FW is the minimum guaranteed
10889 * to us; HW supports far more and we share the remaining pool with
10890 * the other PFs. We allocate space for more than the guarantee with
10891 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010892 */
Mitch Williams505682c2014-05-20 08:01:37 +000010893 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10894 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10895 else
10896 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10897
10898 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080010899 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
10900 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010901 if (!pf->vsi) {
10902 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010903 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010904 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010905
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010906#ifdef CONFIG_PCI_IOV
10907 /* prep for VF support */
10908 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10909 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10910 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10911 if (pci_num_vf(pdev))
10912 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10913 }
10914#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010915 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010916 if (err) {
10917 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10918 goto err_vsis;
10919 }
Helin Zhang58fc3262015-10-01 14:37:38 -040010920
10921 /* Make sure flow control is set according to current settings */
10922 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
10923 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
10924 dev_dbg(&pf->pdev->dev,
10925 "Set fc with err %s aq_err %s on get_phy_cap\n",
10926 i40e_stat_str(hw, err),
10927 i40e_aq_str(hw, hw->aq.asq_last_status));
10928 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
10929 dev_dbg(&pf->pdev->dev,
10930 "Set fc with err %s aq_err %s on set_phy_config\n",
10931 i40e_stat_str(hw, err),
10932 i40e_aq_str(hw, hw->aq.asq_last_status));
10933 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
10934 dev_dbg(&pf->pdev->dev,
10935 "Set fc with err %s aq_err %s on get_link_info\n",
10936 i40e_stat_str(hw, err),
10937 i40e_aq_str(hw, hw->aq.asq_last_status));
10938
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010939 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000010940 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010941 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
10942 i40e_vsi_open(pf->vsi[i]);
10943 break;
10944 }
10945 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010946
Shannon Nelson2f0aff42016-01-04 10:33:08 -080010947 /* The driver only wants link up/down and module qualification
10948 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010949 */
10950 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080010951 ~(I40E_AQ_EVENT_LINK_UPDOWN |
10952 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010953 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010954 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10955 i40e_stat_str(&pf->hw, err),
10956 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010957
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010958 /* Reconfigure hardware for allowing smaller MSS in the case
10959 * of TSO, so that we avoid the MDD being fired and causing
10960 * a reset in the case of small MSS+TSO.
10961 */
10962 val = rd32(hw, I40E_REG_MSS);
10963 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10964 val &= ~I40E_REG_MSS_MIN_MASK;
10965 val |= I40E_64BYTE_MSS;
10966 wr32(hw, I40E_REG_MSS, val);
10967 }
10968
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080010969 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000010970 msleep(75);
10971 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10972 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010973 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10974 i40e_stat_str(&pf->hw, err),
10975 i40e_aq_str(&pf->hw,
10976 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000010977 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010978 /* The main driver is (mostly) up and happy. We need to set this state
10979 * before setting up the misc vector or we get a race and the vector
10980 * ends up disabled forever.
10981 */
10982 clear_bit(__I40E_DOWN, &pf->state);
10983
10984 /* In case of MSIX we are going to setup the misc vector right here
10985 * to handle admin queue events etc. In case of legacy and MSI
10986 * the misc functionality and queue processing is combined in
10987 * the same vector and that gets setup at open.
10988 */
10989 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10990 err = i40e_setup_misc_vector(pf);
10991 if (err) {
10992 dev_info(&pdev->dev,
10993 "setup of misc vector failed: %d\n", err);
10994 goto err_vsis;
10995 }
10996 }
10997
Greg Rosedf805f62014-04-04 04:43:16 +000010998#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010999 /* prep for VF support */
11000 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011001 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11002 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011003 /* disable link interrupts for VFs */
11004 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11005 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11006 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11007 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011008
11009 if (pci_num_vf(pdev)) {
11010 dev_info(&pdev->dev,
11011 "Active VFs found, allocating resources.\n");
11012 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11013 if (err)
11014 dev_info(&pdev->dev,
11015 "Error %d allocating resources for existing VFs\n",
11016 err);
11017 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011018 }
Greg Rosedf805f62014-04-04 04:43:16 +000011019#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011020
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011021 pfs_found++;
11022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011023 i40e_dbg_pf_init(pf);
11024
11025 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011026 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011027
11028 /* since everything's happy, start the service_task timer */
11029 mod_timer(&pf->service_timer,
11030 round_jiffies(jiffies + pf->service_timer_period));
11031
Vasu Dev38e00432014-08-01 13:27:03 -070011032#ifdef I40E_FCOE
11033 /* create FCoE interface */
11034 i40e_fcoe_vsi_setup(pf);
11035
11036#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011037#define PCI_SPEED_SIZE 8
11038#define PCI_WIDTH_SIZE 8
11039 /* Devices on the IOSF bus do not have this information
11040 * and will report PCI Gen 1 x 1 by default so don't bother
11041 * checking them.
11042 */
11043 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11044 char speed[PCI_SPEED_SIZE] = "Unknown";
11045 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011046
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011047 /* Get the negotiated link width and speed from PCI config
11048 * space
11049 */
11050 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11051 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011052
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011053 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011054
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011055 switch (hw->bus.speed) {
11056 case i40e_bus_speed_8000:
11057 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11058 case i40e_bus_speed_5000:
11059 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11060 case i40e_bus_speed_2500:
11061 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11062 default:
11063 break;
11064 }
11065 switch (hw->bus.width) {
11066 case i40e_bus_width_pcie_x8:
11067 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11068 case i40e_bus_width_pcie_x4:
11069 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11070 case i40e_bus_width_pcie_x2:
11071 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11072 case i40e_bus_width_pcie_x1:
11073 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11074 default:
11075 break;
11076 }
11077
11078 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11079 speed, width);
11080
11081 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11082 hw->bus.speed < i40e_bus_speed_8000) {
11083 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11084 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11085 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011086 }
11087
Catherine Sullivane8278452015-02-06 08:52:08 +000011088 /* get the requested speeds from the fw */
11089 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11090 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011091 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11092 i40e_stat_str(&pf->hw, err),
11093 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011094 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11095
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011096 /* get the supported phy types from the fw */
11097 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11098 if (err)
11099 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11100 i40e_stat_str(&pf->hw, err),
11101 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11102 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11103
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011104 /* Add a filter to drop all Flow control frames from any VSI from being
11105 * transmitted. By doing so we stop a malicious VF from sending out
11106 * PAUSE or PFC frames and potentially controlling traffic for other
11107 * PF/VF VSIs.
11108 * The FW can still send Flow control frames if enabled.
11109 */
11110 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11111 pf->main_vsi_seid);
11112
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011113 /* print a string summarizing features */
11114 i40e_print_features(pf);
11115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011116 return 0;
11117
11118 /* Unwind what we've done if something failed in the setup */
11119err_vsis:
11120 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011121 i40e_clear_interrupt_scheme(pf);
11122 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011123err_switch_setup:
11124 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011125 del_timer_sync(&pf->service_timer);
11126err_mac_addr:
11127err_configure_lan_hmc:
11128 (void)i40e_shutdown_lan_hmc(hw);
11129err_init_lan_hmc:
11130 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011131err_sw_init:
11132err_adminq_setup:
11133 (void)i40e_shutdown_adminq(hw);
11134err_pf_reset:
11135 iounmap(hw->hw_addr);
11136err_ioremap:
11137 kfree(pf);
11138err_pf_alloc:
11139 pci_disable_pcie_error_reporting(pdev);
11140 pci_release_selected_regions(pdev,
11141 pci_select_bars(pdev, IORESOURCE_MEM));
11142err_pci_reg:
11143err_dma:
11144 pci_disable_device(pdev);
11145 return err;
11146}
11147
11148/**
11149 * i40e_remove - Device removal routine
11150 * @pdev: PCI device information struct
11151 *
11152 * i40e_remove is called by the PCI subsystem to alert the driver
11153 * that is should release a PCI device. This could be caused by a
11154 * Hot-Plug event, or because the driver is going to be removed from
11155 * memory.
11156 **/
11157static void i40e_remove(struct pci_dev *pdev)
11158{
11159 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011160 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011161 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011162 int i;
11163
11164 i40e_dbg_pf_exit(pf);
11165
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011166 i40e_ptp_stop(pf);
11167
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011168 /* Disable RSS in hw */
11169 wr32(hw, I40E_PFQF_HENA(0), 0);
11170 wr32(hw, I40E_PFQF_HENA(1), 0);
11171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011172 /* no more scheduling of any task */
11173 set_bit(__I40E_DOWN, &pf->state);
11174 del_timer_sync(&pf->service_timer);
11175 cancel_work_sync(&pf->service_task);
11176
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011177 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11178 i40e_free_vfs(pf);
11179 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11180 }
11181
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011182 i40e_fdir_teardown(pf);
11183
11184 /* If there is a switch structure or any orphans, remove them.
11185 * This will leave only the PF's VSI remaining.
11186 */
11187 for (i = 0; i < I40E_MAX_VEB; i++) {
11188 if (!pf->veb[i])
11189 continue;
11190
11191 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11192 pf->veb[i]->uplink_seid == 0)
11193 i40e_switch_branch_release(pf->veb[i]);
11194 }
11195
11196 /* Now we can shutdown the PF's VSI, just before we kill
11197 * adminq and hmc.
11198 */
11199 if (pf->vsi[pf->lan_vsi])
11200 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011202 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000011203 if (pf->hw.hmc.hmc_obj) {
11204 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
11205 if (ret_code)
11206 dev_warn(&pdev->dev,
11207 "Failed to destroy the HMC resources: %d\n",
11208 ret_code);
11209 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011210
11211 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011212 ret_code = i40e_shutdown_adminq(&pf->hw);
11213 if (ret_code)
11214 dev_warn(&pdev->dev,
11215 "Failed to destroy the Admin Queue resources: %d\n",
11216 ret_code);
11217
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011218 /* destroy the locks only once, here */
11219 mutex_destroy(&hw->aq.arq_mutex);
11220 mutex_destroy(&hw->aq.asq_mutex);
11221
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011222 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11223 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011224 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011225 if (pf->vsi[i]) {
11226 i40e_vsi_clear_rings(pf->vsi[i]);
11227 i40e_vsi_clear(pf->vsi[i]);
11228 pf->vsi[i] = NULL;
11229 }
11230 }
11231
11232 for (i = 0; i < I40E_MAX_VEB; i++) {
11233 kfree(pf->veb[i]);
11234 pf->veb[i] = NULL;
11235 }
11236
11237 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011238 kfree(pf->vsi);
11239
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011240 iounmap(pf->hw.hw_addr);
11241 kfree(pf);
11242 pci_release_selected_regions(pdev,
11243 pci_select_bars(pdev, IORESOURCE_MEM));
11244
11245 pci_disable_pcie_error_reporting(pdev);
11246 pci_disable_device(pdev);
11247}
11248
11249/**
11250 * i40e_pci_error_detected - warning that something funky happened in PCI land
11251 * @pdev: PCI device information struct
11252 *
11253 * Called to warn that something happened and the error handling steps
11254 * are in progress. Allows the driver to quiesce things, be ready for
11255 * remediation.
11256 **/
11257static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11258 enum pci_channel_state error)
11259{
11260 struct i40e_pf *pf = pci_get_drvdata(pdev);
11261
11262 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11263
11264 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011265 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11266 rtnl_lock();
11267 i40e_prep_for_reset(pf);
11268 rtnl_unlock();
11269 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011270
11271 /* Request a slot reset */
11272 return PCI_ERS_RESULT_NEED_RESET;
11273}
11274
11275/**
11276 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11277 * @pdev: PCI device information struct
11278 *
11279 * Called to find if the driver can work with the device now that
11280 * the pci slot has been reset. If a basic connection seems good
11281 * (registers are readable and have sane content) then return a
11282 * happy little PCI_ERS_RESULT_xxx.
11283 **/
11284static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11285{
11286 struct i40e_pf *pf = pci_get_drvdata(pdev);
11287 pci_ers_result_t result;
11288 int err;
11289 u32 reg;
11290
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011291 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011292 if (pci_enable_device_mem(pdev)) {
11293 dev_info(&pdev->dev,
11294 "Cannot re-enable PCI device after reset.\n");
11295 result = PCI_ERS_RESULT_DISCONNECT;
11296 } else {
11297 pci_set_master(pdev);
11298 pci_restore_state(pdev);
11299 pci_save_state(pdev);
11300 pci_wake_from_d3(pdev, false);
11301
11302 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11303 if (reg == 0)
11304 result = PCI_ERS_RESULT_RECOVERED;
11305 else
11306 result = PCI_ERS_RESULT_DISCONNECT;
11307 }
11308
11309 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11310 if (err) {
11311 dev_info(&pdev->dev,
11312 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11313 err);
11314 /* non-fatal, continue */
11315 }
11316
11317 return result;
11318}
11319
11320/**
11321 * i40e_pci_error_resume - restart operations after PCI error recovery
11322 * @pdev: PCI device information struct
11323 *
11324 * Called to allow the driver to bring things back up after PCI error
11325 * and/or reset recovery has finished.
11326 **/
11327static void i40e_pci_error_resume(struct pci_dev *pdev)
11328{
11329 struct i40e_pf *pf = pci_get_drvdata(pdev);
11330
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011331 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011332 if (test_bit(__I40E_SUSPENDED, &pf->state))
11333 return;
11334
11335 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011336 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011337 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011338}
11339
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011340/**
11341 * i40e_shutdown - PCI callback for shutting down
11342 * @pdev: PCI device information struct
11343 **/
11344static void i40e_shutdown(struct pci_dev *pdev)
11345{
11346 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011347 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011348
11349 set_bit(__I40E_SUSPENDED, &pf->state);
11350 set_bit(__I40E_DOWN, &pf->state);
11351 rtnl_lock();
11352 i40e_prep_for_reset(pf);
11353 rtnl_unlock();
11354
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011355 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11356 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11357
Catherine Sullivan02b42492015-07-10 19:35:59 -040011358 del_timer_sync(&pf->service_timer);
11359 cancel_work_sync(&pf->service_task);
11360 i40e_fdir_teardown(pf);
11361
11362 rtnl_lock();
11363 i40e_prep_for_reset(pf);
11364 rtnl_unlock();
11365
11366 wr32(hw, I40E_PFPM_APM,
11367 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11368 wr32(hw, I40E_PFPM_WUFC,
11369 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11370
Shannon Nelsone1477582015-02-21 06:44:33 +000011371 i40e_clear_interrupt_scheme(pf);
11372
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011373 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011374 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011375 pci_set_power_state(pdev, PCI_D3hot);
11376 }
11377}
11378
11379#ifdef CONFIG_PM
11380/**
11381 * i40e_suspend - PCI callback for moving to D3
11382 * @pdev: PCI device information struct
11383 **/
11384static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11385{
11386 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011387 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011388
11389 set_bit(__I40E_SUSPENDED, &pf->state);
11390 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011391
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011392 rtnl_lock();
11393 i40e_prep_for_reset(pf);
11394 rtnl_unlock();
11395
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011396 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11397 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11398
11399 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011400 pci_set_power_state(pdev, PCI_D3hot);
11401
11402 return 0;
11403}
11404
11405/**
11406 * i40e_resume - PCI callback for waking up from D3
11407 * @pdev: PCI device information struct
11408 **/
11409static int i40e_resume(struct pci_dev *pdev)
11410{
11411 struct i40e_pf *pf = pci_get_drvdata(pdev);
11412 u32 err;
11413
11414 pci_set_power_state(pdev, PCI_D0);
11415 pci_restore_state(pdev);
11416 /* pci_restore_state() clears dev->state_saves, so
11417 * call pci_save_state() again to restore it.
11418 */
11419 pci_save_state(pdev);
11420
11421 err = pci_enable_device_mem(pdev);
11422 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011423 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011424 return err;
11425 }
11426 pci_set_master(pdev);
11427
11428 /* no wakeup events while running */
11429 pci_wake_from_d3(pdev, false);
11430
11431 /* handling the reset will rebuild the device state */
11432 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11433 clear_bit(__I40E_DOWN, &pf->state);
11434 rtnl_lock();
11435 i40e_reset_and_rebuild(pf, false);
11436 rtnl_unlock();
11437 }
11438
11439 return 0;
11440}
11441
11442#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011443static const struct pci_error_handlers i40e_err_handler = {
11444 .error_detected = i40e_pci_error_detected,
11445 .slot_reset = i40e_pci_error_slot_reset,
11446 .resume = i40e_pci_error_resume,
11447};
11448
11449static struct pci_driver i40e_driver = {
11450 .name = i40e_driver_name,
11451 .id_table = i40e_pci_tbl,
11452 .probe = i40e_probe,
11453 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011454#ifdef CONFIG_PM
11455 .suspend = i40e_suspend,
11456 .resume = i40e_resume,
11457#endif
11458 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011459 .err_handler = &i40e_err_handler,
11460 .sriov_configure = i40e_pci_sriov_configure,
11461};
11462
11463/**
11464 * i40e_init_module - Driver registration routine
11465 *
11466 * i40e_init_module is the first routine called when the driver is
11467 * loaded. All it does is register with the PCI subsystem.
11468 **/
11469static int __init i40e_init_module(void)
11470{
11471 pr_info("%s: %s - version %s\n", i40e_driver_name,
11472 i40e_driver_string, i40e_driver_version_str);
11473 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011474
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011475 /* we will see if single thread per module is enough for now,
11476 * it can't be any worse than using the system workqueue which
11477 * was already single threaded
11478 */
11479 i40e_wq = create_singlethread_workqueue(i40e_driver_name);
11480 if (!i40e_wq) {
11481 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11482 return -ENOMEM;
11483 }
11484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011485 i40e_dbg_init();
11486 return pci_register_driver(&i40e_driver);
11487}
11488module_init(i40e_init_module);
11489
11490/**
11491 * i40e_exit_module - Driver exit cleanup routine
11492 *
11493 * i40e_exit_module is called just before the driver is removed
11494 * from memory.
11495 **/
11496static void __exit i40e_exit_module(void)
11497{
11498 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011499 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011500 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011501}
11502module_exit(i40e_exit_module);