blob: e974db32975b6f1ed920297a59c9fca6f1817d40 [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Singhai, Anjali6a899022015-12-14 12:21:18 -080034#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000035#include <net/vxlan.h>
36#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080037#if IS_ENABLED(CONFIG_GENEVE)
38#include <net/geneve.h>
39#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000040
41const char i40e_driver_name[] = "i40e";
42static const char i40e_driver_string[] =
43 "Intel(R) Ethernet Connection XL710 Network Driver";
44
45#define DRV_KERN "-k"
46
Catherine Sullivane8e724d2014-07-10 07:58:26 +000047#define DRV_VERSION_MAJOR 1
Catherine 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 Duyck9f65e15b2013-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 Duyck9f65e15b2013-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 Duyck9f65e15b2013-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Kirsherb40c82e2015-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 Jaine25d00b2015-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 Brandeburg2818ccd2016-01-13 16:51:38 -08002171 /* if something went wrong then set the changed flag so we try again */
2172 if (retval)
2173 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2174
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002175 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002176 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002177}
2178
2179/**
2180 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2181 * @pf: board private structure
2182 **/
2183static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2184{
2185 int v;
2186
2187 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2188 return;
2189 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2190
Mitch Williams505682c2014-05-20 08:01:37 +00002191 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002192 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002193 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2194 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2195
2196 if (ret) {
2197 /* come back and try again later */
2198 pf->flags |= I40E_FLAG_FILTER_SYNC;
2199 break;
2200 }
2201 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 }
2203}
2204
2205/**
2206 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2207 * @netdev: network interface device structure
2208 * @new_mtu: new value for maximum frame size
2209 *
2210 * Returns 0 on success, negative on failure
2211 **/
2212static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2213{
2214 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002215 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002216 struct i40e_vsi *vsi = np->vsi;
2217
2218 /* MTU < 68 is an error and causes problems on some kernels */
2219 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2220 return -EINVAL;
2221
2222 netdev_info(netdev, "changing MTU from %d to %d\n",
2223 netdev->mtu, new_mtu);
2224 netdev->mtu = new_mtu;
2225 if (netif_running(netdev))
2226 i40e_vsi_reinit_locked(vsi);
2227
2228 return 0;
2229}
2230
2231/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002232 * i40e_ioctl - Access the hwtstamp interface
2233 * @netdev: network interface device structure
2234 * @ifr: interface request data
2235 * @cmd: ioctl command
2236 **/
2237int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2238{
2239 struct i40e_netdev_priv *np = netdev_priv(netdev);
2240 struct i40e_pf *pf = np->vsi->back;
2241
2242 switch (cmd) {
2243 case SIOCGHWTSTAMP:
2244 return i40e_ptp_get_ts_config(pf, ifr);
2245 case SIOCSHWTSTAMP:
2246 return i40e_ptp_set_ts_config(pf, ifr);
2247 default:
2248 return -EOPNOTSUPP;
2249 }
2250}
2251
2252/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2254 * @vsi: the vsi being adjusted
2255 **/
2256void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2257{
2258 struct i40e_vsi_context ctxt;
2259 i40e_status ret;
2260
2261 if ((vsi->info.valid_sections &
2262 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2263 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2264 return; /* already enabled */
2265
2266 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2267 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2268 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2269
2270 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002271 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002272 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2273 if (ret) {
2274 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002275 "update vlan stripping failed, err %s aq_err %s\n",
2276 i40e_stat_str(&vsi->back->hw, ret),
2277 i40e_aq_str(&vsi->back->hw,
2278 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002279 }
2280}
2281
2282/**
2283 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2284 * @vsi: the vsi being adjusted
2285 **/
2286void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2287{
2288 struct i40e_vsi_context ctxt;
2289 i40e_status ret;
2290
2291 if ((vsi->info.valid_sections &
2292 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2293 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2294 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2295 return; /* already disabled */
2296
2297 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2298 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2299 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2300
2301 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002302 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002303 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2304 if (ret) {
2305 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002306 "update vlan stripping failed, err %s aq_err %s\n",
2307 i40e_stat_str(&vsi->back->hw, ret),
2308 i40e_aq_str(&vsi->back->hw,
2309 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002310 }
2311}
2312
2313/**
2314 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2315 * @netdev: network interface to be adjusted
2316 * @features: netdev features to test if VLAN offload is enabled or not
2317 **/
2318static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2319{
2320 struct i40e_netdev_priv *np = netdev_priv(netdev);
2321 struct i40e_vsi *vsi = np->vsi;
2322
2323 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2324 i40e_vlan_stripping_enable(vsi);
2325 else
2326 i40e_vlan_stripping_disable(vsi);
2327}
2328
2329/**
2330 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2331 * @vsi: the vsi being configured
2332 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2333 **/
2334int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2335{
2336 struct i40e_mac_filter *f, *add_f;
2337 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002338
2339 is_vf = (vsi->type == I40E_VSI_SRIOV);
2340 is_netdev = !!(vsi->netdev);
2341
Kiran Patil21659032015-09-30 14:09:03 -04002342 /* Locked once because all functions invoked below iterates list*/
2343 spin_lock_bh(&vsi->mac_filter_list_lock);
2344
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002345 if (is_netdev) {
2346 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2347 is_vf, is_netdev);
2348 if (!add_f) {
2349 dev_info(&vsi->back->pdev->dev,
2350 "Could not add vlan filter %d for %pM\n",
2351 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002352 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002353 return -ENOMEM;
2354 }
2355 }
2356
2357 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2358 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2359 if (!add_f) {
2360 dev_info(&vsi->back->pdev->dev,
2361 "Could not add vlan filter %d for %pM\n",
2362 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002363 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 return -ENOMEM;
2365 }
2366 }
2367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002368 /* Now if we add a vlan tag, make sure to check if it is the first
2369 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2370 * with 0, so we now accept untagged and specified tagged traffic
2371 * (and not any taged and untagged)
2372 */
2373 if (vid > 0) {
2374 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2375 I40E_VLAN_ANY,
2376 is_vf, is_netdev)) {
2377 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2378 I40E_VLAN_ANY, is_vf, is_netdev);
2379 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2380 is_vf, is_netdev);
2381 if (!add_f) {
2382 dev_info(&vsi->back->pdev->dev,
2383 "Could not add filter 0 for %pM\n",
2384 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002385 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002386 return -ENOMEM;
2387 }
2388 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002389 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002390
Greg Rose8d82a7c2014-01-13 16:13:04 -08002391 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2392 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002393 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002394 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2395 is_vf, is_netdev))
2396 continue;
2397 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2398 is_vf, is_netdev);
2399 add_f = i40e_add_filter(vsi, f->macaddr,
2400 0, is_vf, is_netdev);
2401 if (!add_f) {
2402 dev_info(&vsi->back->pdev->dev,
2403 "Could not add filter 0 for %pM\n",
2404 f->macaddr);
2405 spin_unlock_bh(&vsi->mac_filter_list_lock);
2406 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002407 }
2408 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002409 }
2410
Kiran Patil21659032015-09-30 14:09:03 -04002411 spin_unlock_bh(&vsi->mac_filter_list_lock);
2412
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002413 /* schedule our worker thread which will take care of
2414 * applying the new filter changes
2415 */
2416 i40e_service_event_schedule(vsi->back);
2417 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002418}
2419
2420/**
2421 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2422 * @vsi: the vsi being configured
2423 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002424 *
2425 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002426 **/
2427int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2428{
2429 struct net_device *netdev = vsi->netdev;
2430 struct i40e_mac_filter *f, *add_f;
2431 bool is_vf, is_netdev;
2432 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433
2434 is_vf = (vsi->type == I40E_VSI_SRIOV);
2435 is_netdev = !!(netdev);
2436
Kiran Patil21659032015-09-30 14:09:03 -04002437 /* Locked once because all functions invoked below iterates list */
2438 spin_lock_bh(&vsi->mac_filter_list_lock);
2439
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002440 if (is_netdev)
2441 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2442
2443 list_for_each_entry(f, &vsi->mac_filter_list, list)
2444 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2445
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002446 /* go through all the filters for this VSI and if there is only
2447 * vid == 0 it means there are no other filters, so vid 0 must
2448 * be replaced with -1. This signifies that we should from now
2449 * on accept any traffic (with any tag present, or untagged)
2450 */
2451 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2452 if (is_netdev) {
2453 if (f->vlan &&
2454 ether_addr_equal(netdev->dev_addr, f->macaddr))
2455 filter_count++;
2456 }
2457
2458 if (f->vlan)
2459 filter_count++;
2460 }
2461
2462 if (!filter_count && is_netdev) {
2463 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2464 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2465 is_vf, is_netdev);
2466 if (!f) {
2467 dev_info(&vsi->back->pdev->dev,
2468 "Could not add filter %d for %pM\n",
2469 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002470 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002471 return -ENOMEM;
2472 }
2473 }
2474
2475 if (!filter_count) {
2476 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2477 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2478 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002479 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002480 if (!add_f) {
2481 dev_info(&vsi->back->pdev->dev,
2482 "Could not add filter %d for %pM\n",
2483 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002484 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485 return -ENOMEM;
2486 }
2487 }
2488 }
2489
Kiran Patil21659032015-09-30 14:09:03 -04002490 spin_unlock_bh(&vsi->mac_filter_list_lock);
2491
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002492 /* schedule our worker thread which will take care of
2493 * applying the new filter changes
2494 */
2495 i40e_service_event_schedule(vsi->back);
2496 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002497}
2498
2499/**
2500 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2501 * @netdev: network interface to be adjusted
2502 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002503 *
2504 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002505 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002506#ifdef I40E_FCOE
2507int i40e_vlan_rx_add_vid(struct net_device *netdev,
2508 __always_unused __be16 proto, u16 vid)
2509#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002510static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2511 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002512#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513{
2514 struct i40e_netdev_priv *np = netdev_priv(netdev);
2515 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002516 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002517
2518 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002519 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002520
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002521 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2522
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002523 /* If the network stack called us with vid = 0 then
2524 * it is asking to receive priority tagged packets with
2525 * vlan id 0. Our HW receives them by default when configured
2526 * to receive untagged packets so there is no need to add an
2527 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002528 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002529 if (vid)
2530 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002531
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002532 if (!ret && (vid < VLAN_N_VID))
2533 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002534
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002535 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536}
2537
2538/**
2539 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2540 * @netdev: network interface to be adjusted
2541 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002542 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002543 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002544 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002545#ifdef I40E_FCOE
2546int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2547 __always_unused __be16 proto, u16 vid)
2548#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002549static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2550 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002551#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002552{
2553 struct i40e_netdev_priv *np = netdev_priv(netdev);
2554 struct i40e_vsi *vsi = np->vsi;
2555
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002556 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2557
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002558 /* return code is ignored as there is nothing a user
2559 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002560 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002561 */
2562 i40e_vsi_kill_vlan(vsi, vid);
2563
2564 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002565
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002566 return 0;
2567}
2568
2569/**
2570 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2571 * @vsi: the vsi being brought back up
2572 **/
2573static void i40e_restore_vlan(struct i40e_vsi *vsi)
2574{
2575 u16 vid;
2576
2577 if (!vsi->netdev)
2578 return;
2579
2580 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2581
2582 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2583 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2584 vid);
2585}
2586
2587/**
2588 * i40e_vsi_add_pvid - Add pvid for the VSI
2589 * @vsi: the vsi being adjusted
2590 * @vid: the vlan id to set as a PVID
2591 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002592int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593{
2594 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002595 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002596
2597 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2598 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002599 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2600 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002601 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002602
2603 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002604 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002605 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2606 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002607 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002608 "add pvid failed, err %s aq_err %s\n",
2609 i40e_stat_str(&vsi->back->hw, ret),
2610 i40e_aq_str(&vsi->back->hw,
2611 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002612 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002613 }
2614
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002615 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002616}
2617
2618/**
2619 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2620 * @vsi: the vsi being adjusted
2621 *
2622 * Just use the vlan_rx_register() service to put it back to normal
2623 **/
2624void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2625{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002626 i40e_vlan_stripping_disable(vsi);
2627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002628 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002629}
2630
2631/**
2632 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2633 * @vsi: ptr to the VSI
2634 *
2635 * If this function returns with an error, then it's possible one or
2636 * more of the rings is populated (while the rest are not). It is the
2637 * callers duty to clean those orphaned rings.
2638 *
2639 * Return 0 on success, negative on failure
2640 **/
2641static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2642{
2643 int i, err = 0;
2644
2645 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002646 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002647
2648 return err;
2649}
2650
2651/**
2652 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2653 * @vsi: ptr to the VSI
2654 *
2655 * Free VSI's transmit software resources
2656 **/
2657static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2658{
2659 int i;
2660
Greg Rose8e9dca52013-12-18 13:45:53 +00002661 if (!vsi->tx_rings)
2662 return;
2663
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002664 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002665 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002666 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002667}
2668
2669/**
2670 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2671 * @vsi: ptr to the VSI
2672 *
2673 * If this function returns with an error, then it's possible one or
2674 * more of the rings is populated (while the rest are not). It is the
2675 * callers duty to clean those orphaned rings.
2676 *
2677 * Return 0 on success, negative on failure
2678 **/
2679static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2680{
2681 int i, err = 0;
2682
2683 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002684 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002685#ifdef I40E_FCOE
2686 i40e_fcoe_setup_ddp_resources(vsi);
2687#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002688 return err;
2689}
2690
2691/**
2692 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2693 * @vsi: ptr to the VSI
2694 *
2695 * Free all receive software resources
2696 **/
2697static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2698{
2699 int i;
2700
Greg Rose8e9dca52013-12-18 13:45:53 +00002701 if (!vsi->rx_rings)
2702 return;
2703
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002704 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002705 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002706 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002707#ifdef I40E_FCOE
2708 i40e_fcoe_free_ddp_resources(vsi);
2709#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002710}
2711
2712/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002713 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2714 * @ring: The Tx ring to configure
2715 *
2716 * This enables/disables XPS for a given Tx descriptor ring
2717 * based on the TCs enabled for the VSI that ring belongs to.
2718 **/
2719static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2720{
2721 struct i40e_vsi *vsi = ring->vsi;
2722 cpumask_var_t mask;
2723
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002724 if (!ring->q_vector || !ring->netdev)
2725 return;
2726
2727 /* Single TC mode enable XPS */
2728 if (vsi->tc_config.numtc <= 1) {
2729 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002730 netif_set_xps_queue(ring->netdev,
2731 &ring->q_vector->affinity_mask,
2732 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002733 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2734 /* Disable XPS to allow selection based on TC */
2735 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2736 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2737 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002738 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002739
2740 /* schedule our worker thread which will take care of
2741 * applying the new filter changes
2742 */
2743 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002744}
2745
2746/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002747 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2748 * @ring: The Tx ring to configure
2749 *
2750 * Configure the Tx descriptor ring in the HMC context.
2751 **/
2752static int i40e_configure_tx_ring(struct i40e_ring *ring)
2753{
2754 struct i40e_vsi *vsi = ring->vsi;
2755 u16 pf_q = vsi->base_queue + ring->queue_index;
2756 struct i40e_hw *hw = &vsi->back->hw;
2757 struct i40e_hmc_obj_txq tx_ctx;
2758 i40e_status err = 0;
2759 u32 qtx_ctl = 0;
2760
2761 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002762 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002763 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2764 ring->atr_count = 0;
2765 } else {
2766 ring->atr_sample_rate = 0;
2767 }
2768
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002769 /* configure XPS */
2770 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002771
2772 /* clear the context structure first */
2773 memset(&tx_ctx, 0, sizeof(tx_ctx));
2774
2775 tx_ctx.new_context = 1;
2776 tx_ctx.base = (ring->dma / 128);
2777 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002778 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2779 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002780#ifdef I40E_FCOE
2781 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2782#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002783 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002784 /* FDIR VSI tx ring can still use RS bit and writebacks */
2785 if (vsi->type != I40E_VSI_FDIR)
2786 tx_ctx.head_wb_ena = 1;
2787 tx_ctx.head_wb_addr = ring->dma +
2788 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002789
2790 /* As part of VSI creation/update, FW allocates certain
2791 * Tx arbitration queue sets for each TC enabled for
2792 * the VSI. The FW returns the handles to these queue
2793 * sets as part of the response buffer to Add VSI,
2794 * Update VSI, etc. AQ commands. It is expected that
2795 * these queue set handles be associated with the Tx
2796 * queues by the driver as part of the TX queue context
2797 * initialization. This has to be done regardless of
2798 * DCB as by default everything is mapped to TC0.
2799 */
2800 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2801 tx_ctx.rdylist_act = 0;
2802
2803 /* clear the context in the HMC */
2804 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2805 if (err) {
2806 dev_info(&vsi->back->pdev->dev,
2807 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2808 ring->queue_index, pf_q, err);
2809 return -ENOMEM;
2810 }
2811
2812 /* set the context in the HMC */
2813 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2814 if (err) {
2815 dev_info(&vsi->back->pdev->dev,
2816 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2817 ring->queue_index, pf_q, err);
2818 return -ENOMEM;
2819 }
2820
2821 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002822 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002823 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002824 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2825 I40E_QTX_CTL_VFVM_INDX_MASK;
2826 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002827 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002828 }
2829
Shannon Nelson13fd9772013-09-28 07:14:19 +00002830 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2831 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002832 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2833 i40e_flush(hw);
2834
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002835 /* cache tail off for easier writes later */
2836 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2837
2838 return 0;
2839}
2840
2841/**
2842 * i40e_configure_rx_ring - Configure a receive ring context
2843 * @ring: The Rx ring to configure
2844 *
2845 * Configure the Rx descriptor ring in the HMC context.
2846 **/
2847static int i40e_configure_rx_ring(struct i40e_ring *ring)
2848{
2849 struct i40e_vsi *vsi = ring->vsi;
2850 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2851 u16 pf_q = vsi->base_queue + ring->queue_index;
2852 struct i40e_hw *hw = &vsi->back->hw;
2853 struct i40e_hmc_obj_rxq rx_ctx;
2854 i40e_status err = 0;
2855
2856 ring->state = 0;
2857
2858 /* clear the context structure first */
2859 memset(&rx_ctx, 0, sizeof(rx_ctx));
2860
2861 ring->rx_buf_len = vsi->rx_buf_len;
2862 ring->rx_hdr_len = vsi->rx_hdr_len;
2863
2864 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2865 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2866
2867 rx_ctx.base = (ring->dma / 128);
2868 rx_ctx.qlen = ring->count;
2869
2870 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2871 set_ring_16byte_desc_enabled(ring);
2872 rx_ctx.dsize = 0;
2873 } else {
2874 rx_ctx.dsize = 1;
2875 }
2876
2877 rx_ctx.dtype = vsi->dtype;
2878 if (vsi->dtype) {
2879 set_ring_ps_enabled(ring);
2880 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2881 I40E_RX_SPLIT_IP |
2882 I40E_RX_SPLIT_TCP_UDP |
2883 I40E_RX_SPLIT_SCTP;
2884 } else {
2885 rx_ctx.hsplit_0 = 0;
2886 }
2887
2888 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2889 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002890 if (hw->revision_id == 0)
2891 rx_ctx.lrxqthresh = 0;
2892 else
2893 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002894 rx_ctx.crcstrip = 1;
2895 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002896 /* this controls whether VLAN is stripped from inner headers */
2897 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002898#ifdef I40E_FCOE
2899 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2900#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002901 /* set the prefena field to 1 because the manual says to */
2902 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002903
2904 /* clear the context in the HMC */
2905 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2906 if (err) {
2907 dev_info(&vsi->back->pdev->dev,
2908 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2909 ring->queue_index, pf_q, err);
2910 return -ENOMEM;
2911 }
2912
2913 /* set the context in the HMC */
2914 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2915 if (err) {
2916 dev_info(&vsi->back->pdev->dev,
2917 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2918 ring->queue_index, pf_q, err);
2919 return -ENOMEM;
2920 }
2921
2922 /* cache tail for quicker writes, and clear the reg before use */
2923 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2924 writel(0, ring->tail);
2925
Mitch Williamsa132af22015-01-24 09:58:35 +00002926 if (ring_is_ps_enabled(ring)) {
2927 i40e_alloc_rx_headers(ring);
2928 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2929 } else {
2930 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2931 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002932
2933 return 0;
2934}
2935
2936/**
2937 * i40e_vsi_configure_tx - Configure the VSI for Tx
2938 * @vsi: VSI structure describing this set of rings and resources
2939 *
2940 * Configure the Tx VSI for operation.
2941 **/
2942static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2943{
2944 int err = 0;
2945 u16 i;
2946
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00002947 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2948 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002949
2950 return err;
2951}
2952
2953/**
2954 * i40e_vsi_configure_rx - Configure the VSI for Rx
2955 * @vsi: the VSI being configured
2956 *
2957 * Configure the Rx VSI for operation.
2958 **/
2959static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2960{
2961 int err = 0;
2962 u16 i;
2963
2964 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2965 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2966 + ETH_FCS_LEN + VLAN_HLEN;
2967 else
2968 vsi->max_frame = I40E_RXBUFFER_2048;
2969
2970 /* figure out correct receive buffer length */
2971 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2972 I40E_FLAG_RX_PS_ENABLED)) {
2973 case I40E_FLAG_RX_1BUF_ENABLED:
2974 vsi->rx_hdr_len = 0;
2975 vsi->rx_buf_len = vsi->max_frame;
2976 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2977 break;
2978 case I40E_FLAG_RX_PS_ENABLED:
2979 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2980 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2981 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2982 break;
2983 default:
2984 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2985 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2986 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2987 break;
2988 }
2989
Vasu Dev38e00432014-08-01 13:27:03 -07002990#ifdef I40E_FCOE
2991 /* setup rx buffer for FCoE */
2992 if ((vsi->type == I40E_VSI_FCOE) &&
2993 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2994 vsi->rx_hdr_len = 0;
2995 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2996 vsi->max_frame = I40E_RXBUFFER_3072;
2997 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2998 }
2999
3000#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003001 /* round up for the chip's needs */
3002 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003003 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003004 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003005 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006
3007 /* set up individual rings */
3008 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003009 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003010
3011 return err;
3012}
3013
3014/**
3015 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3016 * @vsi: ptr to the VSI
3017 **/
3018static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3019{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003020 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003021 u16 qoffset, qcount;
3022 int i, n;
3023
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003024 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3025 /* Reset the TC information */
3026 for (i = 0; i < vsi->num_queue_pairs; i++) {
3027 rx_ring = vsi->rx_rings[i];
3028 tx_ring = vsi->tx_rings[i];
3029 rx_ring->dcb_tc = 0;
3030 tx_ring->dcb_tc = 0;
3031 }
3032 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003033
3034 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003035 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003036 continue;
3037
3038 qoffset = vsi->tc_config.tc_info[n].qoffset;
3039 qcount = vsi->tc_config.tc_info[n].qcount;
3040 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003041 rx_ring = vsi->rx_rings[i];
3042 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003043 rx_ring->dcb_tc = n;
3044 tx_ring->dcb_tc = n;
3045 }
3046 }
3047}
3048
3049/**
3050 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3051 * @vsi: ptr to the VSI
3052 **/
3053static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3054{
3055 if (vsi->netdev)
3056 i40e_set_rx_mode(vsi->netdev);
3057}
3058
3059/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003060 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3061 * @vsi: Pointer to the targeted VSI
3062 *
3063 * This function replays the hlist on the hw where all the SB Flow Director
3064 * filters were saved.
3065 **/
3066static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3067{
3068 struct i40e_fdir_filter *filter;
3069 struct i40e_pf *pf = vsi->back;
3070 struct hlist_node *node;
3071
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003072 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3073 return;
3074
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003075 hlist_for_each_entry_safe(filter, node,
3076 &pf->fdir_filter_list, fdir_node) {
3077 i40e_add_del_fdir(vsi, filter, true);
3078 }
3079}
3080
3081/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003082 * i40e_vsi_configure - Set up the VSI for action
3083 * @vsi: the VSI being configured
3084 **/
3085static int i40e_vsi_configure(struct i40e_vsi *vsi)
3086{
3087 int err;
3088
3089 i40e_set_vsi_rx_mode(vsi);
3090 i40e_restore_vlan(vsi);
3091 i40e_vsi_config_dcb_rings(vsi);
3092 err = i40e_vsi_configure_tx(vsi);
3093 if (!err)
3094 err = i40e_vsi_configure_rx(vsi);
3095
3096 return err;
3097}
3098
3099/**
3100 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3101 * @vsi: the VSI being configured
3102 **/
3103static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3104{
3105 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003106 struct i40e_hw *hw = &pf->hw;
3107 u16 vector;
3108 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003109 u32 qp;
3110
3111 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3112 * and PFINT_LNKLSTn registers, e.g.:
3113 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3114 */
3115 qp = vsi->base_queue;
3116 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003117 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003118 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3119
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003120 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3122 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3123 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3124 q_vector->rx.itr);
3125 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3126 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3127 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3128 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003129 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3130 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003131
3132 /* Linked list for the queuepairs assigned to this vector */
3133 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3134 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003135 u32 val;
3136
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003137 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3138 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3139 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3140 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3141 (I40E_QUEUE_TYPE_TX
3142 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3143
3144 wr32(hw, I40E_QINT_RQCTL(qp), val);
3145
3146 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3147 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3148 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3149 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3150 (I40E_QUEUE_TYPE_RX
3151 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3152
3153 /* Terminate the linked list */
3154 if (q == (q_vector->num_ringpairs - 1))
3155 val |= (I40E_QUEUE_END_OF_LIST
3156 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3157
3158 wr32(hw, I40E_QINT_TQCTL(qp), val);
3159 qp++;
3160 }
3161 }
3162
3163 i40e_flush(hw);
3164}
3165
3166/**
3167 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3168 * @hw: ptr to the hardware info
3169 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003170static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003171{
Jacob Kellerab437b52014-12-14 01:55:08 +00003172 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003173 u32 val;
3174
3175 /* clear things first */
3176 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3177 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3178
3179 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3180 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3181 I40E_PFINT_ICR0_ENA_GRST_MASK |
3182 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3183 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3185 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3186 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3187
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003188 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3189 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3190
Jacob Kellerab437b52014-12-14 01:55:08 +00003191 if (pf->flags & I40E_FLAG_PTP)
3192 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3193
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003194 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3195
3196 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003197 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3198 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003199
3200 /* OTHER_ITR_IDX = 0 */
3201 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3202}
3203
3204/**
3205 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3206 * @vsi: the VSI being configured
3207 **/
3208static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3209{
Alexander Duyck493fb302013-09-28 07:01:44 +00003210 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211 struct i40e_pf *pf = vsi->back;
3212 struct i40e_hw *hw = &pf->hw;
3213 u32 val;
3214
3215 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003216 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
3218 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3219 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
3220 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
3221 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3222 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3223
Jacob Kellerab437b52014-12-14 01:55:08 +00003224 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003225
3226 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3227 wr32(hw, I40E_PFINT_LNKLST0, 0);
3228
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003229 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003230 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3231 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3232 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3233
3234 wr32(hw, I40E_QINT_RQCTL(0), val);
3235
3236 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3237 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3238 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3239
3240 wr32(hw, I40E_QINT_TQCTL(0), val);
3241 i40e_flush(hw);
3242}
3243
3244/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003245 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3246 * @pf: board private structure
3247 **/
3248void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3249{
3250 struct i40e_hw *hw = &pf->hw;
3251
3252 wr32(hw, I40E_PFINT_DYN_CTL0,
3253 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3254 i40e_flush(hw);
3255}
3256
3257/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003258 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3259 * @pf: board private structure
3260 **/
Shannon Nelson116a57d2013-09-28 07:13:59 +00003261void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262{
3263 struct i40e_hw *hw = &pf->hw;
3264 u32 val;
3265
3266 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
3267 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3268 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3269
3270 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3271 i40e_flush(hw);
3272}
3273
3274/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003275 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3276 * @irq: interrupt number
3277 * @data: pointer to a q_vector
3278 **/
3279static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3280{
3281 struct i40e_q_vector *q_vector = data;
3282
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003283 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003284 return IRQ_HANDLED;
3285
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003286 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003287
3288 return IRQ_HANDLED;
3289}
3290
3291/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003292 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3293 * @vsi: the VSI being configured
3294 * @basename: name for the vector
3295 *
3296 * Allocates MSI-X vectors and requests interrupts from the kernel.
3297 **/
3298static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3299{
3300 int q_vectors = vsi->num_q_vectors;
3301 struct i40e_pf *pf = vsi->back;
3302 int base = vsi->base_vector;
3303 int rx_int_idx = 0;
3304 int tx_int_idx = 0;
3305 int vector, err;
3306
3307 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003308 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003309
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003310 if (q_vector->tx.ring && 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, "TxRx", rx_int_idx++);
3313 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003314 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3316 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003317 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003318 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3319 "%s-%s-%d", basename, "tx", tx_int_idx++);
3320 } else {
3321 /* skip this unused q_vector */
3322 continue;
3323 }
3324 err = request_irq(pf->msix_entries[base + vector].vector,
3325 vsi->irq_handler,
3326 0,
3327 q_vector->name,
3328 q_vector);
3329 if (err) {
3330 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003331 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 goto free_queue_irqs;
3333 }
3334 /* assign the mask for this irq */
3335 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3336 &q_vector->affinity_mask);
3337 }
3338
Shannon Nelson63741842014-04-23 04:50:16 +00003339 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003340 return 0;
3341
3342free_queue_irqs:
3343 while (vector) {
3344 vector--;
3345 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3346 NULL);
3347 free_irq(pf->msix_entries[base + vector].vector,
3348 &(vsi->q_vectors[vector]));
3349 }
3350 return err;
3351}
3352
3353/**
3354 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3355 * @vsi: the VSI being un-configured
3356 **/
3357static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3358{
3359 struct i40e_pf *pf = vsi->back;
3360 struct i40e_hw *hw = &pf->hw;
3361 int base = vsi->base_vector;
3362 int i;
3363
3364 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003365 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3366 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003367 }
3368
3369 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3370 for (i = vsi->base_vector;
3371 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3372 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3373
3374 i40e_flush(hw);
3375 for (i = 0; i < vsi->num_q_vectors; i++)
3376 synchronize_irq(pf->msix_entries[i + base].vector);
3377 } else {
3378 /* Legacy and MSI mode - this stops all interrupt handling */
3379 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3380 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3381 i40e_flush(hw);
3382 synchronize_irq(pf->pdev->irq);
3383 }
3384}
3385
3386/**
3387 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3388 * @vsi: the VSI being configured
3389 **/
3390static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3391{
3392 struct i40e_pf *pf = vsi->back;
3393 int i;
3394
3395 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003396 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003397 i40e_irq_dynamic_enable(vsi, i);
3398 } else {
3399 i40e_irq_dynamic_enable_icr0(pf);
3400 }
3401
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003402 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003403 return 0;
3404}
3405
3406/**
3407 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3408 * @pf: board private structure
3409 **/
3410static void i40e_stop_misc_vector(struct i40e_pf *pf)
3411{
3412 /* Disable ICR 0 */
3413 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3414 i40e_flush(&pf->hw);
3415}
3416
3417/**
3418 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3419 * @irq: interrupt number
3420 * @data: pointer to a q_vector
3421 *
3422 * This is the handler used for all MSI/Legacy interrupts, and deals
3423 * with both queue and non-queue interrupts. This is also used in
3424 * MSIX mode to handle the non-queue interrupts.
3425 **/
3426static irqreturn_t i40e_intr(int irq, void *data)
3427{
3428 struct i40e_pf *pf = (struct i40e_pf *)data;
3429 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003430 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003431 u32 icr0, icr0_remaining;
3432 u32 val, ena_mask;
3433
3434 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003435 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436
Shannon Nelson116a57d2013-09-28 07:13:59 +00003437 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3438 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003439 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003440
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003441 /* if interrupt but no bits showing, must be SWINT */
3442 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3443 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3444 pf->sw_int_count++;
3445
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003446 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3447 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3448 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3449 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3450 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3451 }
3452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003453 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3454 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003455 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3456 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003457
3458 /* temporarily disable queue cause for NAPI processing */
3459 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003460
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3462 wr32(hw, I40E_QINT_RQCTL(0), qval);
3463
3464 qval = rd32(hw, I40E_QINT_TQCTL(0));
3465 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3466 wr32(hw, I40E_QINT_TQCTL(0), qval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467
3468 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003469 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470 }
3471
3472 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3473 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3474 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
3475 }
3476
3477 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3478 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3479 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3480 }
3481
3482 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3483 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3484 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3485 }
3486
3487 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3488 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3489 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3490 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3491 val = rd32(hw, I40E_GLGEN_RSTAT);
3492 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3493 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003494 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003495 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003496 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003497 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003498 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003500 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003501 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003502 }
3503
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003504 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3505 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3506 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003507 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3508 rd32(hw, I40E_PFHMC_ERRORINFO),
3509 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003510 }
3511
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003512 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3513 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3514
3515 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003516 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003517 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003518 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003519 }
3520
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003521 /* If a critical error is pending we have no choice but to reset the
3522 * device.
3523 * Report and mask out any remaining unexpected interrupts.
3524 */
3525 icr0_remaining = icr0 & ena_mask;
3526 if (icr0_remaining) {
3527 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3528 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003529 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003530 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003531 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003532 dev_info(&pf->pdev->dev, "device will be reset\n");
3533 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3534 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 }
3536 ena_mask &= ~icr0_remaining;
3537 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003538 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003539
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003540enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003541 /* re-enable interrupt causes */
3542 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003543 if (!test_bit(__I40E_DOWN, &pf->state)) {
3544 i40e_service_event_schedule(pf);
3545 i40e_irq_dynamic_enable_icr0(pf);
3546 }
3547
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003548 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003549}
3550
3551/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003552 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3553 * @tx_ring: tx ring to clean
3554 * @budget: how many cleans we're allowed
3555 *
3556 * Returns true if there's any budget left (e.g. the clean is finished)
3557 **/
3558static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3559{
3560 struct i40e_vsi *vsi = tx_ring->vsi;
3561 u16 i = tx_ring->next_to_clean;
3562 struct i40e_tx_buffer *tx_buf;
3563 struct i40e_tx_desc *tx_desc;
3564
3565 tx_buf = &tx_ring->tx_bi[i];
3566 tx_desc = I40E_TX_DESC(tx_ring, i);
3567 i -= tx_ring->count;
3568
3569 do {
3570 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3571
3572 /* if next_to_watch is not set then there is no work pending */
3573 if (!eop_desc)
3574 break;
3575
3576 /* prevent any other reads prior to eop_desc */
3577 read_barrier_depends();
3578
3579 /* if the descriptor isn't done, no work yet to do */
3580 if (!(eop_desc->cmd_type_offset_bsz &
3581 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3582 break;
3583
3584 /* clear next_to_watch to prevent false hangs */
3585 tx_buf->next_to_watch = NULL;
3586
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003587 tx_desc->buffer_addr = 0;
3588 tx_desc->cmd_type_offset_bsz = 0;
3589 /* move past filter desc */
3590 tx_buf++;
3591 tx_desc++;
3592 i++;
3593 if (unlikely(!i)) {
3594 i -= tx_ring->count;
3595 tx_buf = tx_ring->tx_bi;
3596 tx_desc = I40E_TX_DESC(tx_ring, 0);
3597 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003598 /* unmap skb header data */
3599 dma_unmap_single(tx_ring->dev,
3600 dma_unmap_addr(tx_buf, dma),
3601 dma_unmap_len(tx_buf, len),
3602 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003603 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3604 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003605
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003606 tx_buf->raw_buf = NULL;
3607 tx_buf->tx_flags = 0;
3608 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003609 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003610 tx_desc->buffer_addr = 0;
3611 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003612
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003613 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003614 tx_buf++;
3615 tx_desc++;
3616 i++;
3617 if (unlikely(!i)) {
3618 i -= tx_ring->count;
3619 tx_buf = tx_ring->tx_bi;
3620 tx_desc = I40E_TX_DESC(tx_ring, 0);
3621 }
3622
3623 /* update budget accounting */
3624 budget--;
3625 } while (likely(budget));
3626
3627 i += tx_ring->count;
3628 tx_ring->next_to_clean = i;
3629
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003630 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003631 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003632
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003633 return budget > 0;
3634}
3635
3636/**
3637 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3638 * @irq: interrupt number
3639 * @data: pointer to a q_vector
3640 **/
3641static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3642{
3643 struct i40e_q_vector *q_vector = data;
3644 struct i40e_vsi *vsi;
3645
3646 if (!q_vector->tx.ring)
3647 return IRQ_HANDLED;
3648
3649 vsi = q_vector->tx.ring->vsi;
3650 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3651
3652 return IRQ_HANDLED;
3653}
3654
3655/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003656 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003657 * @vsi: the VSI being configured
3658 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003659 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003660 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003661static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003662{
Alexander Duyck493fb302013-09-28 07:01:44 +00003663 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00003664 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3665 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003666
3667 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003668 tx_ring->next = q_vector->tx.ring;
3669 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003671
3672 rx_ring->q_vector = q_vector;
3673 rx_ring->next = q_vector->rx.ring;
3674 q_vector->rx.ring = rx_ring;
3675 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003676}
3677
3678/**
3679 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3680 * @vsi: the VSI being configured
3681 *
3682 * This function maps descriptor rings to the queue-specific vectors
3683 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3684 * one vector per queue pair, but on a constrained vector budget, we
3685 * group the queue pairs as "efficiently" as possible.
3686 **/
3687static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3688{
3689 int qp_remaining = vsi->num_queue_pairs;
3690 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003691 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692 int v_start = 0;
3693 int qp_idx = 0;
3694
3695 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3696 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003697 * It is also important to go through all the vectors available to be
3698 * sure that if we don't use all the vectors, that the remaining vectors
3699 * are cleared. This is especially important when decreasing the
3700 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003701 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003702 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003703 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3704
3705 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3706
3707 q_vector->num_ringpairs = num_ringpairs;
3708
3709 q_vector->rx.count = 0;
3710 q_vector->tx.count = 0;
3711 q_vector->rx.ring = NULL;
3712 q_vector->tx.ring = NULL;
3713
3714 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003715 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003716 qp_idx++;
3717 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718 }
3719 }
3720}
3721
3722/**
3723 * i40e_vsi_request_irq - Request IRQ from the OS
3724 * @vsi: the VSI being configured
3725 * @basename: name for the vector
3726 **/
3727static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3728{
3729 struct i40e_pf *pf = vsi->back;
3730 int err;
3731
3732 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3733 err = i40e_vsi_request_irq_msix(vsi, basename);
3734 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3735 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003736 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737 else
3738 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003739 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003740
3741 if (err)
3742 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3743
3744 return err;
3745}
3746
3747#ifdef CONFIG_NET_POLL_CONTROLLER
3748/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003749 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003750 * @netdev: network interface device structure
3751 *
3752 * This is used by netconsole to send skbs without having to re-enable
3753 * interrupts. It's not called while the normal interrupt routine is executing.
3754 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003755#ifdef I40E_FCOE
3756void i40e_netpoll(struct net_device *netdev)
3757#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003758static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003759#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003760{
3761 struct i40e_netdev_priv *np = netdev_priv(netdev);
3762 struct i40e_vsi *vsi = np->vsi;
3763 struct i40e_pf *pf = vsi->back;
3764 int i;
3765
3766 /* if interface is down do nothing */
3767 if (test_bit(__I40E_DOWN, &vsi->state))
3768 return;
3769
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3771 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003772 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003773 } else {
3774 i40e_intr(pf->pdev->irq, netdev);
3775 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003776}
3777#endif
3778
3779/**
Neerav Parikh23527302014-06-03 23:50:15 +00003780 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3781 * @pf: the PF being configured
3782 * @pf_q: the PF queue
3783 * @enable: enable or disable state of the queue
3784 *
3785 * This routine will wait for the given Tx queue of the PF to reach the
3786 * enabled or disabled state.
3787 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3788 * multiple retries; else will return 0 in case of success.
3789 **/
3790static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3791{
3792 int i;
3793 u32 tx_reg;
3794
3795 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3796 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3797 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3798 break;
3799
Neerav Parikhf98a2002014-09-13 07:40:44 +00003800 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003801 }
3802 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3803 return -ETIMEDOUT;
3804
3805 return 0;
3806}
3807
3808/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003809 * i40e_vsi_control_tx - Start or stop a VSI's rings
3810 * @vsi: the VSI being configured
3811 * @enable: start or stop the rings
3812 **/
3813static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3814{
3815 struct i40e_pf *pf = vsi->back;
3816 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003817 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003818 u32 tx_reg;
3819
3820 pf_q = vsi->base_queue;
3821 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499a2014-04-23 04:50:03 +00003822
3823 /* warn the TX unit of coming changes */
3824 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3825 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003826 usleep_range(10, 20);
Matt Jared351499a2014-04-23 04:50:03 +00003827
Mitch Williams6c5ef622014-02-20 19:29:16 -08003828 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003829 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003830 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3831 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3832 break;
3833 usleep_range(1000, 2000);
3834 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003835 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003836 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003837 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003838
3839 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003840 if (enable) {
3841 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003842 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003843 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003844 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003845 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003846
3847 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003848 /* No waiting for the Tx queue to disable */
3849 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3850 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003851
3852 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003853 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3854 if (ret) {
3855 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003856 "VSI seid %d Tx ring %d %sable timeout\n",
3857 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003858 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003859 }
3860 }
3861
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003862 if (hw->revision_id == 0)
3863 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003864 return ret;
3865}
3866
3867/**
3868 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3869 * @pf: the PF being configured
3870 * @pf_q: the PF queue
3871 * @enable: enable or disable state of the queue
3872 *
3873 * This routine will wait for the given Rx queue of the PF to reach the
3874 * enabled or disabled state.
3875 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3876 * multiple retries; else will return 0 in case of success.
3877 **/
3878static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3879{
3880 int i;
3881 u32 rx_reg;
3882
3883 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3884 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3885 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3886 break;
3887
Neerav Parikhf98a2002014-09-13 07:40:44 +00003888 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003889 }
3890 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3891 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003893 return 0;
3894}
3895
3896/**
3897 * i40e_vsi_control_rx - Start or stop a VSI's rings
3898 * @vsi: the VSI being configured
3899 * @enable: start or stop the rings
3900 **/
3901static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3902{
3903 struct i40e_pf *pf = vsi->back;
3904 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003905 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003906 u32 rx_reg;
3907
3908 pf_q = vsi->base_queue;
3909 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003910 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003911 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003912 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3913 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3914 break;
3915 usleep_range(1000, 2000);
3916 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003917
Catherine Sullivan7c122002014-03-14 07:32:29 +00003918 /* Skip if the queue is already in the requested state */
3919 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3920 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003921
3922 /* turn on/off the queue */
3923 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003924 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003925 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003926 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003927 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3928
3929 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003930 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3931 if (ret) {
3932 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003933 "VSI seid %d Rx ring %d %sable timeout\n",
3934 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003935 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003936 }
3937 }
3938
Neerav Parikh23527302014-06-03 23:50:15 +00003939 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003940}
3941
3942/**
3943 * i40e_vsi_control_rings - Start or stop a VSI's rings
3944 * @vsi: the VSI being configured
3945 * @enable: start or stop the rings
3946 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003947int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003948{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003949 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003950
3951 /* do rx first for enable and last for disable */
3952 if (request) {
3953 ret = i40e_vsi_control_rx(vsi, request);
3954 if (ret)
3955 return ret;
3956 ret = i40e_vsi_control_tx(vsi, request);
3957 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003958 /* Ignore return value, we need to shutdown whatever we can */
3959 i40e_vsi_control_tx(vsi, request);
3960 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003961 }
3962
3963 return ret;
3964}
3965
3966/**
3967 * i40e_vsi_free_irq - Free the irq association with the OS
3968 * @vsi: the VSI being configured
3969 **/
3970static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3971{
3972 struct i40e_pf *pf = vsi->back;
3973 struct i40e_hw *hw = &pf->hw;
3974 int base = vsi->base_vector;
3975 u32 val, qp;
3976 int i;
3977
3978 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3979 if (!vsi->q_vectors)
3980 return;
3981
Shannon Nelson63741842014-04-23 04:50:16 +00003982 if (!vsi->irqs_ready)
3983 return;
3984
3985 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003986 for (i = 0; i < vsi->num_q_vectors; i++) {
3987 u16 vector = i + base;
3988
3989 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003990 if (!vsi->q_vectors[i] ||
3991 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003992 continue;
3993
3994 /* clear the affinity_mask in the IRQ descriptor */
3995 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3996 NULL);
3997 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003998 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003999
4000 /* Tear down the interrupt queue link list
4001 *
4002 * We know that they come in pairs and always
4003 * the Rx first, then the Tx. To clear the
4004 * link list, stick the EOL value into the
4005 * next_q field of the registers.
4006 */
4007 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4008 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4009 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4010 val |= I40E_QUEUE_END_OF_LIST
4011 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4012 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4013
4014 while (qp != I40E_QUEUE_END_OF_LIST) {
4015 u32 next;
4016
4017 val = rd32(hw, I40E_QINT_RQCTL(qp));
4018
4019 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4020 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4021 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4022 I40E_QINT_RQCTL_INTEVENT_MASK);
4023
4024 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4025 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4026
4027 wr32(hw, I40E_QINT_RQCTL(qp), val);
4028
4029 val = rd32(hw, I40E_QINT_TQCTL(qp));
4030
4031 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4032 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4033
4034 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4035 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4036 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4037 I40E_QINT_TQCTL_INTEVENT_MASK);
4038
4039 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4040 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4041
4042 wr32(hw, I40E_QINT_TQCTL(qp), val);
4043 qp = next;
4044 }
4045 }
4046 } else {
4047 free_irq(pf->pdev->irq, pf);
4048
4049 val = rd32(hw, I40E_PFINT_LNKLST0);
4050 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4051 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4052 val |= I40E_QUEUE_END_OF_LIST
4053 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4054 wr32(hw, I40E_PFINT_LNKLST0, val);
4055
4056 val = rd32(hw, I40E_QINT_RQCTL(qp));
4057 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4058 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4059 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4060 I40E_QINT_RQCTL_INTEVENT_MASK);
4061
4062 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4063 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4064
4065 wr32(hw, I40E_QINT_RQCTL(qp), val);
4066
4067 val = rd32(hw, I40E_QINT_TQCTL(qp));
4068
4069 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4070 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4071 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4072 I40E_QINT_TQCTL_INTEVENT_MASK);
4073
4074 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4075 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4076
4077 wr32(hw, I40E_QINT_TQCTL(qp), val);
4078 }
4079}
4080
4081/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004082 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4083 * @vsi: the VSI being configured
4084 * @v_idx: Index of vector to be freed
4085 *
4086 * This function frees the memory allocated to the q_vector. In addition if
4087 * NAPI is enabled it will delete any references to the NAPI struct prior
4088 * to freeing the q_vector.
4089 **/
4090static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4091{
4092 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004093 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004094
4095 if (!q_vector)
4096 return;
4097
4098 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004099 i40e_for_each_ring(ring, q_vector->tx)
4100 ring->q_vector = NULL;
4101
4102 i40e_for_each_ring(ring, q_vector->rx)
4103 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004104
4105 /* only VSI w/ an associated netdev is set up w/ NAPI */
4106 if (vsi->netdev)
4107 netif_napi_del(&q_vector->napi);
4108
4109 vsi->q_vectors[v_idx] = NULL;
4110
4111 kfree_rcu(q_vector, rcu);
4112}
4113
4114/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004115 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4116 * @vsi: the VSI being un-configured
4117 *
4118 * This frees the memory allocated to the q_vectors and
4119 * deletes references to the NAPI struct.
4120 **/
4121static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4122{
4123 int v_idx;
4124
Alexander Duyck493fb302013-09-28 07:01:44 +00004125 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4126 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004127}
4128
4129/**
4130 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4131 * @pf: board private structure
4132 **/
4133static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4134{
4135 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4136 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4137 pci_disable_msix(pf->pdev);
4138 kfree(pf->msix_entries);
4139 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004140 kfree(pf->irq_pile);
4141 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4143 pci_disable_msi(pf->pdev);
4144 }
4145 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4146}
4147
4148/**
4149 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4150 * @pf: board private structure
4151 *
4152 * We go through and clear interrupt specific resources and reset the structure
4153 * to pre-load conditions
4154 **/
4155static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4156{
4157 int i;
4158
Shannon Nelsone1477582015-02-21 06:44:33 +00004159 i40e_stop_misc_vector(pf);
4160 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4161 synchronize_irq(pf->msix_entries[0].vector);
4162 free_irq(pf->msix_entries[0].vector, pf);
4163 }
4164
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004165 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004166 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004167 if (pf->vsi[i])
4168 i40e_vsi_free_q_vectors(pf->vsi[i]);
4169 i40e_reset_interrupt_capability(pf);
4170}
4171
4172/**
4173 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4174 * @vsi: the VSI being configured
4175 **/
4176static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4177{
4178 int q_idx;
4179
4180 if (!vsi->netdev)
4181 return;
4182
4183 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004184 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004185}
4186
4187/**
4188 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4189 * @vsi: the VSI being configured
4190 **/
4191static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4192{
4193 int q_idx;
4194
4195 if (!vsi->netdev)
4196 return;
4197
4198 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004199 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004200}
4201
4202/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004203 * i40e_vsi_close - Shut down a VSI
4204 * @vsi: the vsi to be quelled
4205 **/
4206static void i40e_vsi_close(struct i40e_vsi *vsi)
4207{
4208 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4209 i40e_down(vsi);
4210 i40e_vsi_free_irq(vsi);
4211 i40e_vsi_free_tx_resources(vsi);
4212 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004213 vsi->current_netdev_flags = 0;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004214}
4215
4216/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004217 * i40e_quiesce_vsi - Pause a given VSI
4218 * @vsi: the VSI being paused
4219 **/
4220static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4221{
4222 if (test_bit(__I40E_DOWN, &vsi->state))
4223 return;
4224
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004225 /* No need to disable FCoE VSI when Tx suspended */
4226 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4227 vsi->type == I40E_VSI_FCOE) {
4228 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004229 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004230 return;
4231 }
4232
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004233 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004234 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004235 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004236 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004237 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004238}
4239
4240/**
4241 * i40e_unquiesce_vsi - Resume a given VSI
4242 * @vsi: the VSI being resumed
4243 **/
4244static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4245{
4246 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4247 return;
4248
4249 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4250 if (vsi->netdev && netif_running(vsi->netdev))
4251 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4252 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004253 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004254}
4255
4256/**
4257 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4258 * @pf: the PF
4259 **/
4260static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4261{
4262 int v;
4263
Mitch Williams505682c2014-05-20 08:01:37 +00004264 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004265 if (pf->vsi[v])
4266 i40e_quiesce_vsi(pf->vsi[v]);
4267 }
4268}
4269
4270/**
4271 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4272 * @pf: the PF
4273 **/
4274static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4275{
4276 int v;
4277
Mitch Williams505682c2014-05-20 08:01:37 +00004278 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004279 if (pf->vsi[v])
4280 i40e_unquiesce_vsi(pf->vsi[v]);
4281 }
4282}
4283
Neerav Parikh69129dc2014-11-12 00:18:46 +00004284#ifdef CONFIG_I40E_DCB
4285/**
4286 * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
4287 * @vsi: the VSI being configured
4288 *
4289 * This function waits for the given VSI's Tx queues to be disabled.
4290 **/
4291static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
4292{
4293 struct i40e_pf *pf = vsi->back;
4294 int i, pf_q, ret;
4295
4296 pf_q = vsi->base_queue;
4297 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4298 /* Check and wait for the disable status of the queue */
4299 ret = i40e_pf_txq_wait(pf, pf_q, false);
4300 if (ret) {
4301 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004302 "VSI seid %d Tx ring %d disable timeout\n",
4303 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004304 return ret;
4305 }
4306 }
4307
4308 return 0;
4309}
4310
4311/**
4312 * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
4313 * @pf: the PF
4314 *
4315 * This function waits for the Tx queues to be in disabled state for all the
4316 * VSIs that are managed by this PF.
4317 **/
4318static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
4319{
4320 int v, ret = 0;
4321
4322 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004323 /* No need to wait for FCoE VSI queues */
4324 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh69129dc2014-11-12 00:18:46 +00004325 ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
4326 if (ret)
4327 break;
4328 }
4329 }
4330
4331 return ret;
4332}
4333
4334#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004335
4336/**
4337 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4338 * @q_idx: TX queue number
4339 * @vsi: Pointer to VSI struct
4340 *
4341 * This function checks specified queue for given VSI. Detects hung condition.
4342 * Sets hung bit since it is two step process. Before next run of service task
4343 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4344 * hung condition remain unchanged and during subsequent run, this function
4345 * issues SW interrupt to recover from hung condition.
4346 **/
4347static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4348{
4349 struct i40e_ring *tx_ring = NULL;
4350 struct i40e_pf *pf;
4351 u32 head, val, tx_pending;
4352 int i;
4353
4354 pf = vsi->back;
4355
4356 /* now that we have an index, find the tx_ring struct */
4357 for (i = 0; i < vsi->num_queue_pairs; i++) {
4358 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4359 if (q_idx == vsi->tx_rings[i]->queue_index) {
4360 tx_ring = vsi->tx_rings[i];
4361 break;
4362 }
4363 }
4364 }
4365
4366 if (!tx_ring)
4367 return;
4368
4369 /* Read interrupt register */
4370 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4371 val = rd32(&pf->hw,
4372 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4373 tx_ring->vsi->base_vector - 1));
4374 else
4375 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4376
Kiran Patil9c6c1252015-11-06 15:26:02 -08004377 /* Bail out if interrupts are disabled because napi_poll
4378 * execution in-progress or will get scheduled soon.
4379 * napi_poll cleans TX and RX queues and updates 'next_to_clean'.
4380 */
4381 if (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))
4382 return;
4383
Kiran Patilb03a8c12015-09-24 18:13:15 -04004384 head = i40e_get_head(tx_ring);
4385
4386 tx_pending = i40e_get_tx_pending(tx_ring);
4387
Kiran Patil9c6c1252015-11-06 15:26:02 -08004388 /* HW is done executing descriptors, updated HEAD write back,
4389 * but SW hasn't processed those descriptors. If interrupt is
4390 * not generated from this point ON, it could result into
4391 * dev_watchdog detecting timeout on those netdev_queue,
4392 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004393 */
Kiran Patil9c6c1252015-11-06 15:26:02 -08004394 if (tx_pending) {
4395 /* NAPI Poll didn't run and clear since it was set */
4396 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4397 &tx_ring->q_vector->hung_detected)) {
4398 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",
4399 vsi->seid, q_idx, tx_pending,
4400 tx_ring->next_to_clean, head,
4401 tx_ring->next_to_use,
4402 readl(tx_ring->tail));
4403 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4404 vsi->seid, q_idx, val);
4405 i40e_force_wb(vsi, tx_ring->q_vector);
4406 } else {
4407 /* First Chance - detected possible hung */
4408 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4409 &tx_ring->q_vector->hung_detected);
4410 }
4411 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004412}
4413
4414/**
4415 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4416 * @pf: pointer to PF struct
4417 *
4418 * LAN VSI has netdev and netdev has TX queues. This function is to check
4419 * each of those TX queues if they are hung, trigger recovery by issuing
4420 * SW interrupt.
4421 **/
4422static void i40e_detect_recover_hung(struct i40e_pf *pf)
4423{
4424 struct net_device *netdev;
4425 struct i40e_vsi *vsi;
4426 int i;
4427
4428 /* Only for LAN VSI */
4429 vsi = pf->vsi[pf->lan_vsi];
4430
4431 if (!vsi)
4432 return;
4433
4434 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4435 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4436 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4437 return;
4438
4439 /* Make sure type is MAIN VSI */
4440 if (vsi->type != I40E_VSI_MAIN)
4441 return;
4442
4443 netdev = vsi->netdev;
4444 if (!netdev)
4445 return;
4446
4447 /* Bail out if netif_carrier is not OK */
4448 if (!netif_carrier_ok(netdev))
4449 return;
4450
4451 /* Go thru' TX queues for netdev */
4452 for (i = 0; i < netdev->num_tx_queues; i++) {
4453 struct netdev_queue *q;
4454
4455 q = netdev_get_tx_queue(netdev, i);
4456 if (q)
4457 i40e_detect_recover_hung_queue(i, vsi);
4458 }
4459}
4460
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004461/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004462 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004463 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004464 *
4465 * Get TC map for ISCSI PF type that will include iSCSI TC
4466 * and LAN TC.
4467 **/
4468static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4469{
4470 struct i40e_dcb_app_priority_table app;
4471 struct i40e_hw *hw = &pf->hw;
4472 u8 enabled_tc = 1; /* TC0 is always enabled */
4473 u8 tc, i;
4474 /* Get the iSCSI APP TLV */
4475 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4476
4477 for (i = 0; i < dcbcfg->numapps; i++) {
4478 app = dcbcfg->app[i];
4479 if (app.selector == I40E_APP_SEL_TCPIP &&
4480 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4481 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004482 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004483 break;
4484 }
4485 }
4486
4487 return enabled_tc;
4488}
4489
4490/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004491 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4492 * @dcbcfg: the corresponding DCBx configuration structure
4493 *
4494 * Return the number of TCs from given DCBx configuration
4495 **/
4496static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4497{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004498 u8 num_tc = 0;
4499 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004500
4501 /* Scan the ETS Config Priority Table to find
4502 * traffic class enabled for a given priority
4503 * and use the traffic class index to get the
4504 * number of traffic classes enabled
4505 */
4506 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4507 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4508 num_tc = dcbcfg->etscfg.prioritytable[i];
4509 }
4510
4511 /* Traffic class index starts from zero so
4512 * increment to return the actual count
4513 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004514 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004515}
4516
4517/**
4518 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4519 * @dcbcfg: the corresponding DCBx configuration structure
4520 *
4521 * Query the current DCB configuration and return the number of
4522 * traffic classes enabled from the given DCBX config
4523 **/
4524static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4525{
4526 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4527 u8 enabled_tc = 1;
4528 u8 i;
4529
4530 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004531 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004532
4533 return enabled_tc;
4534}
4535
4536/**
4537 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4538 * @pf: PF being queried
4539 *
4540 * Return number of traffic classes enabled for the given PF
4541 **/
4542static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4543{
4544 struct i40e_hw *hw = &pf->hw;
4545 u8 i, enabled_tc;
4546 u8 num_tc = 0;
4547 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4548
4549 /* If DCB is not enabled then always in single TC */
4550 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4551 return 1;
4552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004553 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004554 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4555 return i40e_dcb_get_num_tc(dcbcfg);
4556
4557 /* MFP mode return count of enabled TCs for this PF */
4558 if (pf->hw.func_caps.iscsi)
4559 enabled_tc = i40e_get_iscsi_tc_map(pf);
4560 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004561 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004562
4563 /* At least have TC0 */
4564 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4565 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004566 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004567 num_tc++;
4568 }
4569 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004570}
4571
4572/**
4573 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4574 * @pf: PF being queried
4575 *
4576 * Return a bitmap for first enabled traffic class for this PF.
4577 **/
4578static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4579{
4580 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4581 u8 i = 0;
4582
4583 if (!enabled_tc)
4584 return 0x1; /* TC0 */
4585
4586 /* Find the first enabled TC */
4587 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004588 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004589 break;
4590 }
4591
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004592 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004593}
4594
4595/**
4596 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4597 * @pf: PF being queried
4598 *
4599 * Return a bitmap for enabled traffic classes for this PF.
4600 **/
4601static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4602{
4603 /* If DCB is not enabled for this PF then just return default TC */
4604 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4605 return i40e_pf_get_default_tc(pf);
4606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004607 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004608 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4609 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4610
Neerav Parikhfc51de92015-02-24 06:58:53 +00004611 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004612 if (pf->hw.func_caps.iscsi)
4613 return i40e_get_iscsi_tc_map(pf);
4614 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004615 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004616}
4617
4618/**
4619 * i40e_vsi_get_bw_info - Query VSI BW Information
4620 * @vsi: the VSI being queried
4621 *
4622 * Returns 0 on success, negative value on failure
4623 **/
4624static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4625{
4626 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4627 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4628 struct i40e_pf *pf = vsi->back;
4629 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004630 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004631 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004632 int i;
4633
4634 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004635 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4636 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004637 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004638 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4639 i40e_stat_str(&pf->hw, ret),
4640 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004641 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004642 }
4643
4644 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004645 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4646 NULL);
4647 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004648 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004649 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4650 i40e_stat_str(&pf->hw, ret),
4651 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004652 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004653 }
4654
4655 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4656 dev_info(&pf->pdev->dev,
4657 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4658 bw_config.tc_valid_bits,
4659 bw_ets_config.tc_valid_bits);
4660 /* Still continuing */
4661 }
4662
4663 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4664 vsi->bw_max_quanta = bw_config.max_bw;
4665 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4666 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4667 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4668 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4669 vsi->bw_ets_limit_credits[i] =
4670 le16_to_cpu(bw_ets_config.credits[i]);
4671 /* 3 bits out of 4 for each TC */
4672 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4673 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004674
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004675 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004676}
4677
4678/**
4679 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4680 * @vsi: the VSI being configured
4681 * @enabled_tc: TC bitmap
4682 * @bw_credits: BW shared credits per TC
4683 *
4684 * Returns 0 on success, negative value on failure
4685 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004686static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004687 u8 *bw_share)
4688{
4689 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004690 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004691 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004692
4693 bw_data.tc_valid_bits = enabled_tc;
4694 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4695 bw_data.tc_bw_credits[i] = bw_share[i];
4696
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004697 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4698 NULL);
4699 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004700 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004701 "AQ command Config VSI BW allocation per TC failed = %d\n",
4702 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004703 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004704 }
4705
4706 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4707 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4708
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004709 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004710}
4711
4712/**
4713 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4714 * @vsi: the VSI being configured
4715 * @enabled_tc: TC map to be enabled
4716 *
4717 **/
4718static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4719{
4720 struct net_device *netdev = vsi->netdev;
4721 struct i40e_pf *pf = vsi->back;
4722 struct i40e_hw *hw = &pf->hw;
4723 u8 netdev_tc = 0;
4724 int i;
4725 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4726
4727 if (!netdev)
4728 return;
4729
4730 if (!enabled_tc) {
4731 netdev_reset_tc(netdev);
4732 return;
4733 }
4734
4735 /* Set up actual enabled TCs on the VSI */
4736 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4737 return;
4738
4739 /* set per TC queues for the VSI */
4740 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4741 /* Only set TC queues for enabled tcs
4742 *
4743 * e.g. For a VSI that has TC0 and TC3 enabled the
4744 * enabled_tc bitmap would be 0x00001001; the driver
4745 * will set the numtc for netdev as 2 that will be
4746 * referenced by the netdev layer as TC 0 and 1.
4747 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004748 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004749 netdev_set_tc_queue(netdev,
4750 vsi->tc_config.tc_info[i].netdev_tc,
4751 vsi->tc_config.tc_info[i].qcount,
4752 vsi->tc_config.tc_info[i].qoffset);
4753 }
4754
4755 /* Assign UP2TC map for the VSI */
4756 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4757 /* Get the actual TC# for the UP */
4758 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4759 /* Get the mapped netdev TC# for the UP */
4760 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4761 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4762 }
4763}
4764
4765/**
4766 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4767 * @vsi: the VSI being configured
4768 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4769 **/
4770static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4771 struct i40e_vsi_context *ctxt)
4772{
4773 /* copy just the sections touched not the entire info
4774 * since not all sections are valid as returned by
4775 * update vsi params
4776 */
4777 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4778 memcpy(&vsi->info.queue_mapping,
4779 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4780 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4781 sizeof(vsi->info.tc_mapping));
4782}
4783
4784/**
4785 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4786 * @vsi: VSI to be configured
4787 * @enabled_tc: TC bitmap
4788 *
4789 * This configures a particular VSI for TCs that are mapped to the
4790 * given TC bitmap. It uses default bandwidth share for TCs across
4791 * VSIs to configure TC for a particular VSI.
4792 *
4793 * NOTE:
4794 * It is expected that the VSI queues have been quisced before calling
4795 * this function.
4796 **/
4797static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4798{
4799 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4800 struct i40e_vsi_context ctxt;
4801 int ret = 0;
4802 int i;
4803
4804 /* Check if enabled_tc is same as existing or new TCs */
4805 if (vsi->tc_config.enabled_tc == enabled_tc)
4806 return ret;
4807
4808 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4809 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004810 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004811 bw_share[i] = 1;
4812 }
4813
4814 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4815 if (ret) {
4816 dev_info(&vsi->back->pdev->dev,
4817 "Failed configuring TC map %d for VSI %d\n",
4818 enabled_tc, vsi->seid);
4819 goto out;
4820 }
4821
4822 /* Update Queue Pairs Mapping for currently enabled UPs */
4823 ctxt.seid = vsi->seid;
4824 ctxt.pf_num = vsi->back->hw.pf_id;
4825 ctxt.vf_num = 0;
4826 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004827 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004828 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4829
4830 /* Update the VSI after updating the VSI queue-mapping information */
4831 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4832 if (ret) {
4833 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004834 "Update vsi tc config failed, err %s aq_err %s\n",
4835 i40e_stat_str(&vsi->back->hw, ret),
4836 i40e_aq_str(&vsi->back->hw,
4837 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838 goto out;
4839 }
4840 /* update the local VSI info with updated queue map */
4841 i40e_vsi_update_queue_map(vsi, &ctxt);
4842 vsi->info.valid_sections = 0;
4843
4844 /* Update current VSI BW information */
4845 ret = i40e_vsi_get_bw_info(vsi);
4846 if (ret) {
4847 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004848 "Failed updating vsi bw info, err %s aq_err %s\n",
4849 i40e_stat_str(&vsi->back->hw, ret),
4850 i40e_aq_str(&vsi->back->hw,
4851 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004852 goto out;
4853 }
4854
4855 /* Update the netdev TC setup */
4856 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4857out:
4858 return ret;
4859}
4860
4861/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004862 * i40e_veb_config_tc - Configure TCs for given VEB
4863 * @veb: given VEB
4864 * @enabled_tc: TC bitmap
4865 *
4866 * Configures given TC bitmap for VEB (switching) element
4867 **/
4868int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4869{
4870 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4871 struct i40e_pf *pf = veb->pf;
4872 int ret = 0;
4873 int i;
4874
4875 /* No TCs or already enabled TCs just return */
4876 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4877 return ret;
4878
4879 bw_data.tc_valid_bits = enabled_tc;
4880 /* bw_data.absolute_credits is not set (relative) */
4881
4882 /* Enable ETS TCs with equal BW Share for now */
4883 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004884 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004885 bw_data.tc_bw_share_credits[i] = 1;
4886 }
4887
4888 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4889 &bw_data, NULL);
4890 if (ret) {
4891 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004892 "VEB bw config failed, err %s aq_err %s\n",
4893 i40e_stat_str(&pf->hw, ret),
4894 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004895 goto out;
4896 }
4897
4898 /* Update the BW information */
4899 ret = i40e_veb_get_bw_info(veb);
4900 if (ret) {
4901 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004902 "Failed getting veb bw config, err %s aq_err %s\n",
4903 i40e_stat_str(&pf->hw, ret),
4904 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004905 }
4906
4907out:
4908 return ret;
4909}
4910
4911#ifdef CONFIG_I40E_DCB
4912/**
4913 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4914 * @pf: PF struct
4915 *
4916 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4917 * the caller would've quiesce all the VSIs before calling
4918 * this function
4919 **/
4920static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4921{
4922 u8 tc_map = 0;
4923 int ret;
4924 u8 v;
4925
4926 /* Enable the TCs available on PF to all VEBs */
4927 tc_map = i40e_pf_get_tc_map(pf);
4928 for (v = 0; v < I40E_MAX_VEB; v++) {
4929 if (!pf->veb[v])
4930 continue;
4931 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4932 if (ret) {
4933 dev_info(&pf->pdev->dev,
4934 "Failed configuring TC for VEB seid=%d\n",
4935 pf->veb[v]->seid);
4936 /* Will try to configure as many components */
4937 }
4938 }
4939
4940 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004941 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004942 if (!pf->vsi[v])
4943 continue;
4944
4945 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004946#ifdef I40E_FCOE
4947 * - For FCoE VSI only enable the TC configured
4948 * as per the APP TLV
4949#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004950 * - For all others keep them at TC0 for now
4951 */
4952 if (v == pf->lan_vsi)
4953 tc_map = i40e_pf_get_tc_map(pf);
4954 else
4955 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004956#ifdef I40E_FCOE
4957 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4958 tc_map = i40e_get_fcoe_tc_map(pf);
4959#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004960
4961 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4962 if (ret) {
4963 dev_info(&pf->pdev->dev,
4964 "Failed configuring TC for VSI seid=%d\n",
4965 pf->vsi[v]->seid);
4966 /* Will try to configure as many components */
4967 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004968 /* Re-configure VSI vectors based on updated TC map */
4969 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004970 if (pf->vsi[v]->netdev)
4971 i40e_dcbnl_set_all(pf->vsi[v]);
4972 }
4973 }
4974}
4975
4976/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004977 * i40e_resume_port_tx - Resume port Tx
4978 * @pf: PF struct
4979 *
4980 * Resume a port's Tx and issue a PF reset in case of failure to
4981 * resume.
4982 **/
4983static int i40e_resume_port_tx(struct i40e_pf *pf)
4984{
4985 struct i40e_hw *hw = &pf->hw;
4986 int ret;
4987
4988 ret = i40e_aq_resume_port_tx(hw, NULL);
4989 if (ret) {
4990 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004991 "Resume Port Tx failed, err %s aq_err %s\n",
4992 i40e_stat_str(&pf->hw, ret),
4993 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00004994 /* Schedule PF reset to recover */
4995 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4996 i40e_service_event_schedule(pf);
4997 }
4998
4999 return ret;
5000}
5001
5002/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005003 * i40e_init_pf_dcb - Initialize DCB configuration
5004 * @pf: PF being configured
5005 *
5006 * Query the current DCB configuration and cache it
5007 * in the hardware structure
5008 **/
5009static int i40e_init_pf_dcb(struct i40e_pf *pf)
5010{
5011 struct i40e_hw *hw = &pf->hw;
5012 int err = 0;
5013
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005014 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
5015 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
5016 (pf->hw.aq.fw_maj_ver < 4))
5017 goto out;
5018
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005019 /* Get the initial DCB configuration */
5020 err = i40e_init_dcb(hw);
5021 if (!err) {
5022 /* Device/Function is not DCBX capable */
5023 if ((!hw->func_caps.dcb) ||
5024 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5025 dev_info(&pf->pdev->dev,
5026 "DCBX offload is not supported or is disabled for this PF.\n");
5027
5028 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5029 goto out;
5030
5031 } else {
5032 /* When status is not DISABLED then DCBX in FW */
5033 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5034 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005035
5036 pf->flags |= I40E_FLAG_DCB_CAPABLE;
5037 /* Enable DCB tagging only when more than one TC */
5038 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5039 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005040 dev_dbg(&pf->pdev->dev,
5041 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005042 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005043 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005044 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005045 "Query for DCB configuration failed, err %s aq_err %s\n",
5046 i40e_stat_str(&pf->hw, err),
5047 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005048 }
5049
5050out:
5051 return err;
5052}
5053#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005054#define SPEED_SIZE 14
5055#define FC_SIZE 8
5056/**
5057 * i40e_print_link_message - print link up or down
5058 * @vsi: the VSI for which link needs a message
5059 */
Matt Jaredc156f852015-08-27 11:42:39 -04005060void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005061{
Shannon Nelsona9165492015-09-03 17:19:00 -04005062 char *speed = "Unknown";
5063 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005064
Matt Jaredc156f852015-08-27 11:42:39 -04005065 if (vsi->current_isup == isup)
5066 return;
5067 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005068 if (!isup) {
5069 netdev_info(vsi->netdev, "NIC Link is Down\n");
5070 return;
5071 }
5072
Greg Rose148c2d82014-12-11 07:06:27 +00005073 /* Warn user if link speed on NPAR enabled partition is not at
5074 * least 10GB
5075 */
5076 if (vsi->back->hw.func_caps.npar_enable &&
5077 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5078 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5079 netdev_warn(vsi->netdev,
5080 "The partition detected link speed that is less than 10Gbps\n");
5081
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005082 switch (vsi->back->hw.phy.link_info.link_speed) {
5083 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005084 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005085 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005086 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005087 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005088 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005089 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005090 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005091 break;
5092 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005093 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005094 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005095 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005096 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005097 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005098 default:
5099 break;
5100 }
5101
5102 switch (vsi->back->hw.fc.current_mode) {
5103 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005104 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005105 break;
5106 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005107 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005108 break;
5109 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005110 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005111 break;
5112 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005113 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005114 break;
5115 }
5116
Shannon Nelsona9165492015-09-03 17:19:00 -04005117 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005118 speed, fc);
5119}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005120
5121/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005122 * i40e_up_complete - Finish the last steps of bringing up a connection
5123 * @vsi: the VSI being configured
5124 **/
5125static int i40e_up_complete(struct i40e_vsi *vsi)
5126{
5127 struct i40e_pf *pf = vsi->back;
5128 int err;
5129
5130 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5131 i40e_vsi_configure_msix(vsi);
5132 else
5133 i40e_configure_msi_and_legacy(vsi);
5134
5135 /* start rings */
5136 err = i40e_vsi_control_rings(vsi, true);
5137 if (err)
5138 return err;
5139
5140 clear_bit(__I40E_DOWN, &vsi->state);
5141 i40e_napi_enable_all(vsi);
5142 i40e_vsi_enable_irq(vsi);
5143
5144 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5145 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005146 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005147 netif_tx_start_all_queues(vsi->netdev);
5148 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005149 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005150 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005151 /* need to check for qualified module here*/
5152 if ((pf->hw.phy.link_info.link_info &
5153 I40E_AQ_MEDIA_AVAILABLE) &&
5154 (!(pf->hw.phy.link_info.an_info &
5155 I40E_AQ_QUALIFIED_MODULE)))
5156 netdev_err(vsi->netdev,
5157 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005158 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005159
5160 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005161 if (vsi->type == I40E_VSI_FDIR) {
5162 /* reset fd counters */
5163 pf->fd_add_err = pf->fd_atr_cnt = 0;
5164 if (pf->fd_tcp_rule > 0) {
5165 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005166 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5167 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005168 pf->fd_tcp_rule = 0;
5169 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005170 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005171 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005172 i40e_service_event_schedule(pf);
5173
5174 return 0;
5175}
5176
5177/**
5178 * i40e_vsi_reinit_locked - Reset the VSI
5179 * @vsi: the VSI being configured
5180 *
5181 * Rebuild the ring structs after some configuration
5182 * has changed, e.g. MTU size.
5183 **/
5184static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5185{
5186 struct i40e_pf *pf = vsi->back;
5187
5188 WARN_ON(in_interrupt());
5189 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5190 usleep_range(1000, 2000);
5191 i40e_down(vsi);
5192
5193 /* Give a VF some time to respond to the reset. The
5194 * two second wait is based upon the watchdog cycle in
5195 * the VF driver.
5196 */
5197 if (vsi->type == I40E_VSI_SRIOV)
5198 msleep(2000);
5199 i40e_up(vsi);
5200 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5201}
5202
5203/**
5204 * i40e_up - Bring the connection back up after being down
5205 * @vsi: the VSI being configured
5206 **/
5207int i40e_up(struct i40e_vsi *vsi)
5208{
5209 int err;
5210
5211 err = i40e_vsi_configure(vsi);
5212 if (!err)
5213 err = i40e_up_complete(vsi);
5214
5215 return err;
5216}
5217
5218/**
5219 * i40e_down - Shutdown the connection processing
5220 * @vsi: the VSI being stopped
5221 **/
5222void i40e_down(struct i40e_vsi *vsi)
5223{
5224 int i;
5225
5226 /* It is assumed that the caller of this function
5227 * sets the vsi->state __I40E_DOWN bit.
5228 */
5229 if (vsi->netdev) {
5230 netif_carrier_off(vsi->netdev);
5231 netif_tx_disable(vsi->netdev);
5232 }
5233 i40e_vsi_disable_irq(vsi);
5234 i40e_vsi_control_rings(vsi, false);
5235 i40e_napi_disable_all(vsi);
5236
5237 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00005238 i40e_clean_tx_ring(vsi->tx_rings[i]);
5239 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005240 }
5241}
5242
5243/**
5244 * i40e_setup_tc - configure multiple traffic classes
5245 * @netdev: net device to configure
5246 * @tc: number of traffic classes to enable
5247 **/
5248static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5249{
5250 struct i40e_netdev_priv *np = netdev_priv(netdev);
5251 struct i40e_vsi *vsi = np->vsi;
5252 struct i40e_pf *pf = vsi->back;
5253 u8 enabled_tc = 0;
5254 int ret = -EINVAL;
5255 int i;
5256
5257 /* Check if DCB enabled to continue */
5258 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5259 netdev_info(netdev, "DCB is not enabled for adapter\n");
5260 goto exit;
5261 }
5262
5263 /* Check if MFP enabled */
5264 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5265 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5266 goto exit;
5267 }
5268
5269 /* Check whether tc count is within enabled limit */
5270 if (tc > i40e_pf_get_num_tc(pf)) {
5271 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5272 goto exit;
5273 }
5274
5275 /* Generate TC map for number of tc requested */
5276 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005277 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005278
5279 /* Requesting same TC configuration as already enabled */
5280 if (enabled_tc == vsi->tc_config.enabled_tc)
5281 return 0;
5282
5283 /* Quiesce VSI queues */
5284 i40e_quiesce_vsi(vsi);
5285
5286 /* Configure VSI for enabled TCs */
5287 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5288 if (ret) {
5289 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5290 vsi->seid);
5291 goto exit;
5292 }
5293
5294 /* Unquiesce VSI */
5295 i40e_unquiesce_vsi(vsi);
5296
5297exit:
5298 return ret;
5299}
5300
John Fastabende4c67342016-02-16 21:16:15 -08005301#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005302int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5303 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005304#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005305static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5306 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005307#endif
5308{
John Fastabend16e5cc62016-02-16 21:16:43 -08005309 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005310 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005311 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005312}
5313
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005314/**
5315 * i40e_open - Called when a network interface is made active
5316 * @netdev: network interface device structure
5317 *
5318 * The open entry point is called when a network interface is made
5319 * active by the system (IFF_UP). At this point all resources needed
5320 * for transmit and receive operations are allocated, the interrupt
5321 * handler is registered with the OS, the netdev watchdog subtask is
5322 * enabled, and the stack is notified that the interface is ready.
5323 *
5324 * Returns 0 on success, negative value on failure
5325 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005326int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005327{
5328 struct i40e_netdev_priv *np = netdev_priv(netdev);
5329 struct i40e_vsi *vsi = np->vsi;
5330 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005331 int err;
5332
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005333 /* disallow open during test or if eeprom is broken */
5334 if (test_bit(__I40E_TESTING, &pf->state) ||
5335 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005336 return -EBUSY;
5337
5338 netif_carrier_off(netdev);
5339
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005340 err = i40e_vsi_open(vsi);
5341 if (err)
5342 return err;
5343
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005344 /* configure global TSO hardware offload settings */
5345 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5346 TCP_FLAG_FIN) >> 16);
5347 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5348 TCP_FLAG_FIN |
5349 TCP_FLAG_CWR) >> 16);
5350 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5351
Singhai, Anjali6633d382015-12-03 23:49:31 -08005352#ifdef CONFIG_I40E_VXLAN
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005353 vxlan_get_rx_port(netdev);
5354#endif
Singhai, Anjalicd866602015-12-14 12:21:21 -08005355#ifdef CONFIG_I40E_GENEVE
Anjali Singhaia340c782016-01-06 11:49:28 -08005356 if (pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE)
5357 geneve_get_rx_port(netdev);
Singhai, Anjalicd866602015-12-14 12:21:21 -08005358#endif
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005359
5360 return 0;
5361}
5362
5363/**
5364 * i40e_vsi_open -
5365 * @vsi: the VSI to open
5366 *
5367 * Finish initialization of the VSI.
5368 *
5369 * Returns 0 on success, negative value on failure
5370 **/
5371int i40e_vsi_open(struct i40e_vsi *vsi)
5372{
5373 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005374 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005375 int err;
5376
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005377 /* allocate descriptors */
5378 err = i40e_vsi_setup_tx_resources(vsi);
5379 if (err)
5380 goto err_setup_tx;
5381 err = i40e_vsi_setup_rx_resources(vsi);
5382 if (err)
5383 goto err_setup_rx;
5384
5385 err = i40e_vsi_configure(vsi);
5386 if (err)
5387 goto err_setup_rx;
5388
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005389 if (vsi->netdev) {
5390 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5391 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5392 err = i40e_vsi_request_irq(vsi, int_name);
5393 if (err)
5394 goto err_setup_rx;
5395
5396 /* Notify the stack of the actual queue counts. */
5397 err = netif_set_real_num_tx_queues(vsi->netdev,
5398 vsi->num_queue_pairs);
5399 if (err)
5400 goto err_set_queues;
5401
5402 err = netif_set_real_num_rx_queues(vsi->netdev,
5403 vsi->num_queue_pairs);
5404 if (err)
5405 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005406
5407 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005408 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005409 dev_driver_string(&pf->pdev->dev),
5410 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005411 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005412
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005413 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005414 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005415 goto err_setup_rx;
5416 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418 err = i40e_up_complete(vsi);
5419 if (err)
5420 goto err_up_complete;
5421
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005422 return 0;
5423
5424err_up_complete:
5425 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005426err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005427 i40e_vsi_free_irq(vsi);
5428err_setup_rx:
5429 i40e_vsi_free_rx_resources(vsi);
5430err_setup_tx:
5431 i40e_vsi_free_tx_resources(vsi);
5432 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005433 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005434
5435 return err;
5436}
5437
5438/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005439 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005440 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005441 *
5442 * This function destroys the hlist where all the Flow Director
5443 * filters were saved.
5444 **/
5445static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5446{
5447 struct i40e_fdir_filter *filter;
5448 struct hlist_node *node2;
5449
5450 hlist_for_each_entry_safe(filter, node2,
5451 &pf->fdir_filter_list, fdir_node) {
5452 hlist_del(&filter->fdir_node);
5453 kfree(filter);
5454 }
5455 pf->fdir_pf_active_filters = 0;
5456}
5457
5458/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005459 * i40e_close - Disables a network interface
5460 * @netdev: network interface device structure
5461 *
5462 * The close entry point is called when an interface is de-activated
5463 * by the OS. The hardware is still under the driver's control, but
5464 * this netdev interface is disabled.
5465 *
5466 * Returns 0, this is not allowed to fail
5467 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005468#ifdef I40E_FCOE
5469int i40e_close(struct net_device *netdev)
5470#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005471static int i40e_close(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07005472#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473{
5474 struct i40e_netdev_priv *np = netdev_priv(netdev);
5475 struct i40e_vsi *vsi = np->vsi;
5476
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005477 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005478
5479 return 0;
5480}
5481
5482/**
5483 * i40e_do_reset - Start a PF or Core Reset sequence
5484 * @pf: board private structure
5485 * @reset_flags: which reset is requested
5486 *
5487 * The essential difference in resets is that the PF Reset
5488 * doesn't clear the packet buffers, doesn't reset the PE
5489 * firmware, and doesn't bother the other PFs on the chip.
5490 **/
5491void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5492{
5493 u32 val;
5494
5495 WARN_ON(in_interrupt());
5496
Mitch Williams263fc482014-04-23 04:50:11 +00005497 if (i40e_check_asq_alive(&pf->hw))
5498 i40e_vc_notify_reset(pf);
5499
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005500 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005501 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005502
5503 /* Request a Global Reset
5504 *
5505 * This will start the chip's countdown to the actual full
5506 * chip reset event, and a warning interrupt to be sent
5507 * to all PFs, including the requestor. Our handler
5508 * for the warning interrupt will deal with the shutdown
5509 * and recovery of the switch setup.
5510 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005511 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005512 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5513 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5514 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5515
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005516 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005517
5518 /* Request a Core Reset
5519 *
5520 * Same as Global Reset, except does *not* include the MAC/PHY
5521 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005522 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005523 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5524 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5525 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5526 i40e_flush(&pf->hw);
5527
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005528 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005529
5530 /* Request a PF Reset
5531 *
5532 * Resets only the PF-specific registers
5533 *
5534 * This goes directly to the tear-down and rebuild of
5535 * the switch, since we need to do all the recovery as
5536 * for the Core Reset.
5537 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005538 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005539 i40e_handle_reset_warning(pf);
5540
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005541 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005542 int v;
5543
5544 /* Find the VSI(s) that requested a re-init */
5545 dev_info(&pf->pdev->dev,
5546 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005547 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005548 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005549
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005550 if (vsi != NULL &&
5551 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5552 i40e_vsi_reinit_locked(pf->vsi[v]);
5553 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5554 }
5555 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005556 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005557 int v;
5558
5559 /* Find the VSI(s) that needs to be brought down */
5560 dev_info(&pf->pdev->dev, "VSI down requested\n");
5561 for (v = 0; v < pf->num_alloc_vsi; v++) {
5562 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005563
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005564 if (vsi != NULL &&
5565 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5566 set_bit(__I40E_DOWN, &vsi->state);
5567 i40e_down(vsi);
5568 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5569 }
5570 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005571 } else {
5572 dev_info(&pf->pdev->dev,
5573 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005574 }
5575}
5576
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005577#ifdef CONFIG_I40E_DCB
5578/**
5579 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5580 * @pf: board private structure
5581 * @old_cfg: current DCB config
5582 * @new_cfg: new DCB config
5583 **/
5584bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5585 struct i40e_dcbx_config *old_cfg,
5586 struct i40e_dcbx_config *new_cfg)
5587{
5588 bool need_reconfig = false;
5589
5590 /* Check if ETS configuration has changed */
5591 if (memcmp(&new_cfg->etscfg,
5592 &old_cfg->etscfg,
5593 sizeof(new_cfg->etscfg))) {
5594 /* If Priority Table has changed reconfig is needed */
5595 if (memcmp(&new_cfg->etscfg.prioritytable,
5596 &old_cfg->etscfg.prioritytable,
5597 sizeof(new_cfg->etscfg.prioritytable))) {
5598 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005599 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005600 }
5601
5602 if (memcmp(&new_cfg->etscfg.tcbwtable,
5603 &old_cfg->etscfg.tcbwtable,
5604 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005605 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005606
5607 if (memcmp(&new_cfg->etscfg.tsatable,
5608 &old_cfg->etscfg.tsatable,
5609 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005610 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005611 }
5612
5613 /* Check if PFC configuration has changed */
5614 if (memcmp(&new_cfg->pfc,
5615 &old_cfg->pfc,
5616 sizeof(new_cfg->pfc))) {
5617 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005618 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005619 }
5620
5621 /* Check if APP Table has changed */
5622 if (memcmp(&new_cfg->app,
5623 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005624 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005625 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005626 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005627 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005628
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005629 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005630 return need_reconfig;
5631}
5632
5633/**
5634 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5635 * @pf: board private structure
5636 * @e: event info posted on ARQ
5637 **/
5638static int i40e_handle_lldp_event(struct i40e_pf *pf,
5639 struct i40e_arq_event_info *e)
5640{
5641 struct i40e_aqc_lldp_get_mib *mib =
5642 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5643 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005644 struct i40e_dcbx_config tmp_dcbx_cfg;
5645 bool need_reconfig = false;
5646 int ret = 0;
5647 u8 type;
5648
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005649 /* Not DCB capable or capability disabled */
5650 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5651 return ret;
5652
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005653 /* Ignore if event is not for Nearest Bridge */
5654 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5655 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005656 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005657 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5658 return ret;
5659
5660 /* Check MIB Type and return if event for Remote MIB update */
5661 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005662 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005663 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005664 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5665 /* Update the remote cached instance and return */
5666 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5667 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5668 &hw->remote_dcbx_config);
5669 goto exit;
5670 }
5671
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005672 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005673 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005674
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005675 /* Reset the old DCBx configuration data */
5676 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005677 /* Get updated DCBX data from firmware */
5678 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005679 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005680 dev_info(&pf->pdev->dev,
5681 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5682 i40e_stat_str(&pf->hw, ret),
5683 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005684 goto exit;
5685 }
5686
5687 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005688 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5689 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005690 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005691 goto exit;
5692 }
5693
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005694 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5695 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005696
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005697 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005698
5699 if (!need_reconfig)
5700 goto exit;
5701
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005702 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005703 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005704 pf->flags |= I40E_FLAG_DCB_ENABLED;
5705 else
5706 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5707
Neerav Parikh69129dc2014-11-12 00:18:46 +00005708 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005709 /* Reconfiguration needed quiesce all VSIs */
5710 i40e_pf_quiesce_all_vsi(pf);
5711
5712 /* Changes in configuration update VEB/VSI */
5713 i40e_dcb_reconfigure(pf);
5714
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005715 ret = i40e_resume_port_tx(pf);
5716
Neerav Parikh69129dc2014-11-12 00:18:46 +00005717 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005718 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005719 if (ret)
5720 goto exit;
5721
5722 /* Wait for the PF's Tx queues to be disabled */
5723 ret = i40e_pf_wait_txq_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005724 if (ret) {
5725 /* Schedule PF reset to recover */
5726 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5727 i40e_service_event_schedule(pf);
5728 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005729 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005730 }
5731
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005732exit:
5733 return ret;
5734}
5735#endif /* CONFIG_I40E_DCB */
5736
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005737/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005738 * i40e_do_reset_safe - Protected reset path for userland calls.
5739 * @pf: board private structure
5740 * @reset_flags: which reset is requested
5741 *
5742 **/
5743void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5744{
5745 rtnl_lock();
5746 i40e_do_reset(pf, reset_flags);
5747 rtnl_unlock();
5748}
5749
5750/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005751 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5752 * @pf: board private structure
5753 * @e: event info posted on ARQ
5754 *
5755 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5756 * and VF queues
5757 **/
5758static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5759 struct i40e_arq_event_info *e)
5760{
5761 struct i40e_aqc_lan_overflow *data =
5762 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5763 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5764 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5765 struct i40e_hw *hw = &pf->hw;
5766 struct i40e_vf *vf;
5767 u16 vf_id;
5768
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005769 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5770 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005771
5772 /* Queue belongs to VF, find the VF and issue VF reset */
5773 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5774 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5775 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5776 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5777 vf_id -= hw->func_caps.vf_base_id;
5778 vf = &pf->vf[vf_id];
5779 i40e_vc_notify_vf_reset(vf);
5780 /* Allow VF to process pending reset notification */
5781 msleep(20);
5782 i40e_reset_vf(vf, false);
5783 }
5784}
5785
5786/**
5787 * i40e_service_event_complete - Finish up the service event
5788 * @pf: board private structure
5789 **/
5790static void i40e_service_event_complete(struct i40e_pf *pf)
5791{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005792 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005793
5794 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005795 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005796 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5797}
5798
5799/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005800 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5801 * @pf: board private structure
5802 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005803u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005804{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005805 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005806
5807 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5808 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5809 return fcnt_prog;
5810}
5811
5812/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005813 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005814 * @pf: board private structure
5815 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005816u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005817{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005818 u32 val, fcnt_prog;
5819
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005820 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5821 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5822 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5823 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5824 return fcnt_prog;
5825}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005826
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005827/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005828 * i40e_get_global_fd_count - Get total FD filters programmed on device
5829 * @pf: board private structure
5830 **/
5831u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5832{
5833 u32 val, fcnt_prog;
5834
5835 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5836 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5837 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5838 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5839 return fcnt_prog;
5840}
5841
5842/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005843 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5844 * @pf: board private structure
5845 **/
5846void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5847{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005848 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005849 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005850 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005851
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005852 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5853 return;
5854
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005855 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5856 * to re-enable
5857 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005858 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005859 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005860 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5861 (pf->fd_add_err == 0) ||
5862 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005863 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5864 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5865 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005866 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5867 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 +00005868 }
5869 }
5870 /* Wait for some more space to be available to turn on ATR */
5871 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5872 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5873 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5874 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005875 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5876 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 +00005877 }
5878 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005879
5880 /* if hw had a problem adding a filter, delete it */
5881 if (pf->fd_inv > 0) {
5882 hlist_for_each_entry_safe(filter, node,
5883 &pf->fdir_filter_list, fdir_node) {
5884 if (filter->fd_id == pf->fd_inv) {
5885 hlist_del(&filter->fdir_node);
5886 kfree(filter);
5887 pf->fdir_pf_active_filters--;
5888 }
5889 }
5890 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005891}
5892
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005893#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005894#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005895/**
5896 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5897 * @pf: board private structure
5898 **/
5899static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5900{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005901 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005902 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005903 bool disable_atr = false;
5904 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005905 int reg;
5906
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005907 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5908 return;
5909
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005910 if (!time_after(jiffies, pf->fd_flush_timestamp +
5911 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5912 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005913
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005914 /* If the flush is happening too quick and we have mostly SB rules we
5915 * should not re-enable ATR for some time.
5916 */
5917 min_flush_time = pf->fd_flush_timestamp +
5918 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5919 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005920
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005921 if (!(time_after(jiffies, min_flush_time)) &&
5922 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5923 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5924 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5925 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005926 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005927
5928 pf->fd_flush_timestamp = jiffies;
5929 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5930 /* flush all filters */
5931 wr32(&pf->hw, I40E_PFQF_CTL_1,
5932 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5933 i40e_flush(&pf->hw);
5934 pf->fd_flush_cnt++;
5935 pf->fd_add_err = 0;
5936 do {
5937 /* Check FD flush status every 5-6msec */
5938 usleep_range(5000, 6000);
5939 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5940 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5941 break;
5942 } while (flush_wait_retry--);
5943 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5944 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5945 } else {
5946 /* replay sideband filters */
5947 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5948 if (!disable_atr)
5949 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5950 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5951 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5952 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5953 }
5954
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005955}
5956
5957/**
5958 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5959 * @pf: board private structure
5960 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005961u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005962{
5963 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5964}
5965
5966/* We can see up to 256 filter programming desc in transit if the filters are
5967 * being applied really fast; before we see the first
5968 * filter miss error on Rx queue 0. Accumulating enough error messages before
5969 * reacting will make sure we don't cause flush too often.
5970 */
5971#define I40E_MAX_FD_PROGRAM_ERROR 256
5972
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005973/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005974 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
5975 * @pf: board private structure
5976 **/
5977static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
5978{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005979
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005980 /* if interface is down do nothing */
5981 if (test_bit(__I40E_DOWN, &pf->state))
5982 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005983
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005984 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5985 return;
5986
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005987 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005988 i40e_fdir_flush_and_replay(pf);
5989
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005990 i40e_fdir_check_and_reenable(pf);
5991
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005992}
5993
5994/**
5995 * i40e_vsi_link_event - notify VSI of a link event
5996 * @vsi: vsi to be notified
5997 * @link_up: link up or down
5998 **/
5999static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6000{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006001 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006002 return;
6003
6004 switch (vsi->type) {
6005 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006006#ifdef I40E_FCOE
6007 case I40E_VSI_FCOE:
6008#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006009 if (!vsi->netdev || !vsi->netdev_registered)
6010 break;
6011
6012 if (link_up) {
6013 netif_carrier_on(vsi->netdev);
6014 netif_tx_wake_all_queues(vsi->netdev);
6015 } else {
6016 netif_carrier_off(vsi->netdev);
6017 netif_tx_stop_all_queues(vsi->netdev);
6018 }
6019 break;
6020
6021 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006022 case I40E_VSI_VMDQ2:
6023 case I40E_VSI_CTRL:
6024 case I40E_VSI_MIRROR:
6025 default:
6026 /* there is no notification for other VSIs */
6027 break;
6028 }
6029}
6030
6031/**
6032 * i40e_veb_link_event - notify elements on the veb of a link event
6033 * @veb: veb to be notified
6034 * @link_up: link up or down
6035 **/
6036static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6037{
6038 struct i40e_pf *pf;
6039 int i;
6040
6041 if (!veb || !veb->pf)
6042 return;
6043 pf = veb->pf;
6044
6045 /* depth first... */
6046 for (i = 0; i < I40E_MAX_VEB; i++)
6047 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6048 i40e_veb_link_event(pf->veb[i], link_up);
6049
6050 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006051 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006052 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6053 i40e_vsi_link_event(pf->vsi[i], link_up);
6054}
6055
6056/**
6057 * i40e_link_event - Update netif_carrier status
6058 * @pf: board private structure
6059 **/
6060static void i40e_link_event(struct i40e_pf *pf)
6061{
Mitch Williams320684c2014-10-17 03:14:43 +00006062 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006063 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006064 i40e_status status;
6065 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006066
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006067 /* save off old link status information */
6068 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6069
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006070 /* set this to force the get_link_status call to refresh state */
6071 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006072
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006073 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006074
6075 status = i40e_get_link_status(&pf->hw, &new_link);
6076 if (status) {
6077 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6078 status);
6079 return;
6080 }
6081
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006082 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6083 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006084
6085 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006086 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006087 (test_bit(__I40E_DOWN, &vsi->state) ||
6088 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006089 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006090
6091 if (!test_bit(__I40E_DOWN, &vsi->state))
6092 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006093
6094 /* Notify the base of the switch tree connected to
6095 * the link. Floating VEBs are not notified.
6096 */
6097 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6098 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6099 else
Mitch Williams320684c2014-10-17 03:14:43 +00006100 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006101
6102 if (pf->vf)
6103 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006104
6105 if (pf->flags & I40E_FLAG_PTP)
6106 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006107}
6108
6109/**
Shannon Nelson21536712014-10-25 10:35:25 +00006110 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006111 * @pf: board private structure
6112 **/
6113static void i40e_watchdog_subtask(struct i40e_pf *pf)
6114{
6115 int i;
6116
6117 /* if interface is down do nothing */
6118 if (test_bit(__I40E_DOWN, &pf->state) ||
6119 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6120 return;
6121
Shannon Nelson21536712014-10-25 10:35:25 +00006122 /* make sure we don't do these things too often */
6123 if (time_before(jiffies, (pf->service_timer_previous +
6124 pf->service_timer_period)))
6125 return;
6126 pf->service_timer_previous = jiffies;
6127
Shannon Nelson9ac77262015-08-27 11:42:40 -04006128 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6129 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006130
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006131 /* Update the stats for active netdevs so the network stack
6132 * can look at updated numbers whenever it cares to
6133 */
Mitch Williams505682c2014-05-20 08:01:37 +00006134 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006135 if (pf->vsi[i] && pf->vsi[i]->netdev)
6136 i40e_update_stats(pf->vsi[i]);
6137
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006138 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6139 /* Update the stats for the active switching components */
6140 for (i = 0; i < I40E_MAX_VEB; i++)
6141 if (pf->veb[i])
6142 i40e_update_veb_stats(pf->veb[i]);
6143 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006144
6145 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006146}
6147
6148/**
6149 * i40e_reset_subtask - Set up for resetting the device and driver
6150 * @pf: board private structure
6151 **/
6152static void i40e_reset_subtask(struct i40e_pf *pf)
6153{
6154 u32 reset_flags = 0;
6155
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006156 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006157 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006158 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6160 }
6161 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006162 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6164 }
6165 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006166 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006167 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6168 }
6169 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006170 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006171 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6172 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006173 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006174 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006175 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6176 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006177
6178 /* If there's a recovery already waiting, it takes
6179 * precedence before starting a new reset sequence.
6180 */
6181 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6182 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006183 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006184 }
6185
6186 /* If we're already down or resetting, just bail */
6187 if (reset_flags &&
6188 !test_bit(__I40E_DOWN, &pf->state) &&
6189 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6190 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006191
6192unlock:
6193 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006194}
6195
6196/**
6197 * i40e_handle_link_event - Handle link event
6198 * @pf: board private structure
6199 * @e: event info posted on ARQ
6200 **/
6201static void i40e_handle_link_event(struct i40e_pf *pf,
6202 struct i40e_arq_event_info *e)
6203{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006204 struct i40e_aqc_get_link_status *status =
6205 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006207 /* Do a new status request to re-enable LSE reporting
6208 * and load new status information into the hw struct
6209 * This completely ignores any state information
6210 * in the ARQ event info, instead choosing to always
6211 * issue the AQ update link status command.
6212 */
6213 i40e_link_event(pf);
6214
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006215 /* check for unqualified module, if link is down */
6216 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6217 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6218 (!(status->link_info & I40E_AQ_LINK_UP)))
6219 dev_err(&pf->pdev->dev,
6220 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006221}
6222
6223/**
6224 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6225 * @pf: board private structure
6226 **/
6227static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6228{
6229 struct i40e_arq_event_info event;
6230 struct i40e_hw *hw = &pf->hw;
6231 u16 pending, i = 0;
6232 i40e_status ret;
6233 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006234 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006235 u32 val;
6236
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006237 /* Do not run clean AQ when PF reset fails */
6238 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6239 return;
6240
Shannon Nelson86df2422014-05-20 08:01:35 +00006241 /* check for error indications */
6242 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6243 oldval = val;
6244 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006245 if (hw->debug_mask & I40E_DEBUG_AQ)
6246 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006247 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6248 }
6249 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006250 if (hw->debug_mask & I40E_DEBUG_AQ)
6251 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006252 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006253 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006254 }
6255 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006256 if (hw->debug_mask & I40E_DEBUG_AQ)
6257 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006258 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6259 }
6260 if (oldval != val)
6261 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6262
6263 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6264 oldval = val;
6265 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006266 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6267 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006268 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6269 }
6270 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006271 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6272 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006273 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6274 }
6275 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006276 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6277 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006278 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6279 }
6280 if (oldval != val)
6281 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6282
Mitch Williams1001dc32014-11-11 20:02:19 +00006283 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6284 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006285 if (!event.msg_buf)
6286 return;
6287
6288 do {
6289 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006290 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006291 break;
Mitch Williams56497972014-06-04 08:45:18 +00006292 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006293 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6294 break;
6295 }
6296
6297 opcode = le16_to_cpu(event.desc.opcode);
6298 switch (opcode) {
6299
6300 case i40e_aqc_opc_get_link_status:
6301 i40e_handle_link_event(pf, &event);
6302 break;
6303 case i40e_aqc_opc_send_msg_to_pf:
6304 ret = i40e_vc_process_vf_msg(pf,
6305 le16_to_cpu(event.desc.retval),
6306 le32_to_cpu(event.desc.cookie_high),
6307 le32_to_cpu(event.desc.cookie_low),
6308 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006309 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006310 break;
6311 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006312 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006313#ifdef CONFIG_I40E_DCB
6314 rtnl_lock();
6315 ret = i40e_handle_lldp_event(pf, &event);
6316 rtnl_unlock();
6317#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318 break;
6319 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006320 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006321 i40e_handle_lan_overflow_event(pf, &event);
6322 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006323 case i40e_aqc_opc_send_msg_to_peer:
6324 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6325 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006326 case i40e_aqc_opc_nvm_erase:
6327 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006328 case i40e_aqc_opc_oem_post_update:
Shannon Nelson91a0f932015-03-19 14:32:01 -07006329 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
6330 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006331 default:
6332 dev_info(&pf->pdev->dev,
Shannon Nelson0467bc92013-12-18 13:45:58 +00006333 "ARQ Error: Unknown event 0x%04x received\n",
6334 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006335 break;
6336 }
6337 } while (pending && (i++ < pf->adminq_work_limit));
6338
6339 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6340 /* re-enable Admin queue interrupt cause */
6341 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6342 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6343 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6344 i40e_flush(hw);
6345
6346 kfree(event.msg_buf);
6347}
6348
6349/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006350 * i40e_verify_eeprom - make sure eeprom is good to use
6351 * @pf: board private structure
6352 **/
6353static void i40e_verify_eeprom(struct i40e_pf *pf)
6354{
6355 int err;
6356
6357 err = i40e_diag_eeprom_test(&pf->hw);
6358 if (err) {
6359 /* retry in case of garbage read */
6360 err = i40e_diag_eeprom_test(&pf->hw);
6361 if (err) {
6362 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6363 err);
6364 set_bit(__I40E_BAD_EEPROM, &pf->state);
6365 }
6366 }
6367
6368 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6369 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6370 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6371 }
6372}
6373
6374/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006375 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006376 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006377 *
6378 * enable switch loop back or die - no point in a return value
6379 **/
6380static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6381{
6382 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6383 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006384 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006385
6386 ctxt.seid = pf->main_vsi_seid;
6387 ctxt.pf_num = pf->hw.pf_id;
6388 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006389 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6390 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006391 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006392 "couldn't get PF vsi config, err %s aq_err %s\n",
6393 i40e_stat_str(&pf->hw, ret),
6394 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006395 return;
6396 }
6397 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6398 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6399 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6400
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006401 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6402 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006403 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006404 "update vsi switch failed, err %s aq_err %s\n",
6405 i40e_stat_str(&pf->hw, ret),
6406 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006407 }
6408}
6409
6410/**
6411 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006412 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006413 *
6414 * disable switch loop back or die - no point in a return value
6415 **/
6416static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6417{
6418 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6419 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006420 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006421
6422 ctxt.seid = pf->main_vsi_seid;
6423 ctxt.pf_num = pf->hw.pf_id;
6424 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006425 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6426 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006427 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006428 "couldn't get PF vsi config, err %s aq_err %s\n",
6429 i40e_stat_str(&pf->hw, ret),
6430 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006431 return;
6432 }
6433 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6434 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6435 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6436
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006437 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6438 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006439 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006440 "update vsi switch failed, err %s aq_err %s\n",
6441 i40e_stat_str(&pf->hw, ret),
6442 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006443 }
6444}
6445
6446/**
Neerav Parikh51616012015-02-06 08:52:14 +00006447 * i40e_config_bridge_mode - Configure the HW bridge mode
6448 * @veb: pointer to the bridge instance
6449 *
6450 * Configure the loop back mode for the LAN VSI that is downlink to the
6451 * specified HW bridge instance. It is expected this function is called
6452 * when a new HW bridge is instantiated.
6453 **/
6454static void i40e_config_bridge_mode(struct i40e_veb *veb)
6455{
6456 struct i40e_pf *pf = veb->pf;
6457
Shannon Nelson6dec1012015-09-28 14:12:30 -04006458 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6459 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6460 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006461 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6462 i40e_disable_pf_switch_lb(pf);
6463 else
6464 i40e_enable_pf_switch_lb(pf);
6465}
6466
6467/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006468 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6469 * @veb: pointer to the VEB instance
6470 *
6471 * This is a recursive function that first builds the attached VSIs then
6472 * recurses in to build the next layer of VEB. We track the connections
6473 * through our own index numbers because the seid's from the HW could
6474 * change across the reset.
6475 **/
6476static int i40e_reconstitute_veb(struct i40e_veb *veb)
6477{
6478 struct i40e_vsi *ctl_vsi = NULL;
6479 struct i40e_pf *pf = veb->pf;
6480 int v, veb_idx;
6481 int ret;
6482
6483 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006484 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006485 if (pf->vsi[v] &&
6486 pf->vsi[v]->veb_idx == veb->idx &&
6487 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6488 ctl_vsi = pf->vsi[v];
6489 break;
6490 }
6491 }
6492 if (!ctl_vsi) {
6493 dev_info(&pf->pdev->dev,
6494 "missing owner VSI for veb_idx %d\n", veb->idx);
6495 ret = -ENOENT;
6496 goto end_reconstitute;
6497 }
6498 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6499 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6500 ret = i40e_add_vsi(ctl_vsi);
6501 if (ret) {
6502 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006503 "rebuild of veb_idx %d owner VSI failed: %d\n",
6504 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006505 goto end_reconstitute;
6506 }
6507 i40e_vsi_reset_stats(ctl_vsi);
6508
6509 /* create the VEB in the switch and move the VSI onto the VEB */
6510 ret = i40e_add_veb(veb, ctl_vsi);
6511 if (ret)
6512 goto end_reconstitute;
6513
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006514 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6515 veb->bridge_mode = BRIDGE_MODE_VEB;
6516 else
6517 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006518 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006519
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006520 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006521 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006522 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6523 continue;
6524
6525 if (pf->vsi[v]->veb_idx == veb->idx) {
6526 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006527
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006528 vsi->uplink_seid = veb->seid;
6529 ret = i40e_add_vsi(vsi);
6530 if (ret) {
6531 dev_info(&pf->pdev->dev,
6532 "rebuild of vsi_idx %d failed: %d\n",
6533 v, ret);
6534 goto end_reconstitute;
6535 }
6536 i40e_vsi_reset_stats(vsi);
6537 }
6538 }
6539
6540 /* create any VEBs attached to this VEB - RECURSION */
6541 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6542 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6543 pf->veb[veb_idx]->uplink_seid = veb->seid;
6544 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6545 if (ret)
6546 break;
6547 }
6548 }
6549
6550end_reconstitute:
6551 return ret;
6552}
6553
6554/**
6555 * i40e_get_capabilities - get info about the HW
6556 * @pf: the PF struct
6557 **/
6558static int i40e_get_capabilities(struct i40e_pf *pf)
6559{
6560 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6561 u16 data_size;
6562 int buf_len;
6563 int err;
6564
6565 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6566 do {
6567 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6568 if (!cap_buf)
6569 return -ENOMEM;
6570
6571 /* this loads the data into the hw struct for us */
6572 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6573 &data_size,
6574 i40e_aqc_opc_list_func_capabilities,
6575 NULL);
6576 /* data loaded, buffer no longer needed */
6577 kfree(cap_buf);
6578
6579 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6580 /* retry with a larger buffer */
6581 buf_len = data_size;
6582 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6583 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006584 "capability discovery failed, err %s aq_err %s\n",
6585 i40e_stat_str(&pf->hw, err),
6586 i40e_aq_str(&pf->hw,
6587 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006588 return -ENODEV;
6589 }
6590 } while (err);
6591
6592 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6593 dev_info(&pf->pdev->dev,
6594 "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",
6595 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6596 pf->hw.func_caps.num_msix_vectors,
6597 pf->hw.func_caps.num_msix_vectors_vf,
6598 pf->hw.func_caps.fd_filters_guaranteed,
6599 pf->hw.func_caps.fd_filters_best_effort,
6600 pf->hw.func_caps.num_tx_qp,
6601 pf->hw.func_caps.num_vsis);
6602
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006603#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6604 + pf->hw.func_caps.num_vfs)
6605 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6606 dev_info(&pf->pdev->dev,
6607 "got num_vsis %d, setting num_vsis to %d\n",
6608 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6609 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6610 }
6611
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006612 return 0;
6613}
6614
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006615static int i40e_vsi_clear(struct i40e_vsi *vsi);
6616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006617/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006618 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006619 * @pf: board private structure
6620 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006621static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006622{
6623 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006624 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006625
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006626 /* quick workaround for an NVM issue that leaves a critical register
6627 * uninitialized
6628 */
6629 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6630 static const u32 hkey[] = {
6631 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6632 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6633 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6634 0x95b3a76d};
6635
6636 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6637 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6638 }
6639
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006640 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006641 return;
6642
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006643 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006644 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006645 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006646 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006648 break;
6649 }
6650 }
6651
6652 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006654 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6655 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006656 if (!vsi) {
6657 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006658 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6659 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006660 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006661 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006662
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006663 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006664}
6665
6666/**
6667 * i40e_fdir_teardown - release the Flow Director resources
6668 * @pf: board private structure
6669 **/
6670static void i40e_fdir_teardown(struct i40e_pf *pf)
6671{
6672 int i;
6673
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006674 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006675 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6677 i40e_vsi_release(pf->vsi[i]);
6678 break;
6679 }
6680 }
6681}
6682
6683/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006684 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006685 * @pf: board private structure
6686 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006687 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006688 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006689static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006692 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006693 u32 v;
6694
6695 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6696 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006697 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006698
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006699 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006700
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006701 /* quiesce the VSIs and their queues that are not already DOWN */
6702 i40e_pf_quiesce_all_vsi(pf);
6703
Mitch Williams505682c2014-05-20 08:01:37 +00006704 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 if (pf->vsi[v])
6706 pf->vsi[v]->seid = 0;
6707 }
6708
6709 i40e_shutdown_adminq(&pf->hw);
6710
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006711 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006712 if (hw->hmc.hmc_obj) {
6713 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006714 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006715 dev_warn(&pf->pdev->dev,
6716 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006717 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006718}
6719
6720/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006721 * i40e_send_version - update firmware with driver version
6722 * @pf: PF struct
6723 */
6724static void i40e_send_version(struct i40e_pf *pf)
6725{
6726 struct i40e_driver_version dv;
6727
6728 dv.major_version = DRV_VERSION_MAJOR;
6729 dv.minor_version = DRV_VERSION_MINOR;
6730 dv.build_version = DRV_VERSION_BUILD;
6731 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006732 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006733 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6734}
6735
6736/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006737 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006738 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006739 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006740 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006741static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006742{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006743 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006744 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006745 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006746 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006747 u32 v;
6748
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006749 /* Now we wait for GRST to settle out.
6750 * We don't have to delete the VEBs or VSIs from the hw switch
6751 * because the reset will make them disappear.
6752 */
6753 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006754 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006755 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006756 set_bit(__I40E_RESET_FAILED, &pf->state);
6757 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006758 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006759 pf->pfr_count++;
6760
6761 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006762 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006763 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006764
6765 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6766 ret = i40e_init_adminq(&pf->hw);
6767 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006768 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6769 i40e_stat_str(&pf->hw, ret),
6770 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006771 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006772 }
6773
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006774 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006775 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006776 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006777
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006778 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006780 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006782
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006783 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6784 hw->func_caps.num_rx_qp,
6785 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6786 if (ret) {
6787 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6788 goto end_core_reset;
6789 }
6790 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6791 if (ret) {
6792 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6793 goto end_core_reset;
6794 }
6795
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006796#ifdef CONFIG_I40E_DCB
6797 ret = i40e_init_pf_dcb(pf);
6798 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006799 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6800 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6801 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006802 }
6803#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006804#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006805 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006806
Vasu Dev38e00432014-08-01 13:27:03 -07006807#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006808 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006809 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006810 if (ret)
6811 goto end_core_reset;
6812
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006813 /* The driver only wants link up/down and module qualification
6814 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006815 */
6816 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006817 ~(I40E_AQ_EVENT_LINK_UPDOWN |
6818 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006819 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006820 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6821 i40e_stat_str(&pf->hw, ret),
6822 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006823
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006824 /* make sure our flow control settings are restored */
6825 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6826 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006827 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6828 i40e_stat_str(&pf->hw, ret),
6829 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006830
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006831 /* Rebuild the VSIs and VEBs that existed before reset.
6832 * They are still in our local switch element arrays, so only
6833 * need to rebuild the switch model in the HW.
6834 *
6835 * If there were VEBs but the reconstitution failed, we'll try
6836 * try to recover minimal use by getting the basic PF VSI working.
6837 */
6838 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006839 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006840 /* find the one VEB connected to the MAC, and find orphans */
6841 for (v = 0; v < I40E_MAX_VEB; v++) {
6842 if (!pf->veb[v])
6843 continue;
6844
6845 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6846 pf->veb[v]->uplink_seid == 0) {
6847 ret = i40e_reconstitute_veb(pf->veb[v]);
6848
6849 if (!ret)
6850 continue;
6851
6852 /* If Main VEB failed, we're in deep doodoo,
6853 * so give up rebuilding the switch and set up
6854 * for minimal rebuild of PF VSI.
6855 * If orphan failed, we'll report the error
6856 * but try to keep going.
6857 */
6858 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6859 dev_info(&pf->pdev->dev,
6860 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6861 ret);
6862 pf->vsi[pf->lan_vsi]->uplink_seid
6863 = pf->mac_seid;
6864 break;
6865 } else if (pf->veb[v]->uplink_seid == 0) {
6866 dev_info(&pf->pdev->dev,
6867 "rebuild of orphan VEB failed: %d\n",
6868 ret);
6869 }
6870 }
6871 }
6872 }
6873
6874 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006875 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006876 /* no VEB, so rebuild only the Main VSI */
6877 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6878 if (ret) {
6879 dev_info(&pf->pdev->dev,
6880 "rebuild of Main VSI failed: %d\n", ret);
6881 goto end_core_reset;
6882 }
6883 }
6884
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006885 /* Reconfigure hardware for allowing smaller MSS in the case
6886 * of TSO, so that we avoid the MDD being fired and causing
6887 * a reset in the case of small MSS+TSO.
6888 */
6889#define I40E_REG_MSS 0x000E64DC
6890#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6891#define I40E_64BYTE_MSS 0x400000
6892 val = rd32(hw, I40E_REG_MSS);
6893 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6894 val &= ~I40E_REG_MSS_MIN_MASK;
6895 val |= I40E_64BYTE_MSS;
6896 wr32(hw, I40E_REG_MSS, val);
6897 }
6898
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006899 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006900 msleep(75);
6901 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6902 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006903 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6904 i40e_stat_str(&pf->hw, ret),
6905 i40e_aq_str(&pf->hw,
6906 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006907 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006908 /* reinit the misc interrupt */
6909 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6910 ret = i40e_setup_misc_vector(pf);
6911
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006912 /* Add a filter to drop all Flow control frames from any VSI from being
6913 * transmitted. By doing so we stop a malicious VF from sending out
6914 * PAUSE or PFC frames and potentially controlling traffic for other
6915 * PF/VF VSIs.
6916 * The FW can still send Flow control frames if enabled.
6917 */
6918 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6919 pf->main_vsi_seid);
6920
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006921 /* restart the VSIs that were rebuilt and running before the reset */
6922 i40e_pf_unquiesce_all_vsi(pf);
6923
Mitch Williams69f64b22014-02-13 03:48:46 -08006924 if (pf->num_alloc_vfs) {
6925 for (v = 0; v < pf->num_alloc_vfs; v++)
6926 i40e_reset_vf(&pf->vf[v], true);
6927 }
6928
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006929 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006930 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006931
6932end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006933 clear_bit(__I40E_RESET_FAILED, &pf->state);
6934clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006935 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6936}
6937
6938/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006939 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006940 * @pf: board private structure
6941 *
6942 * Close up the VFs and other things in prep for a Core Reset,
6943 * then get ready to rebuild the world.
6944 **/
6945static void i40e_handle_reset_warning(struct i40e_pf *pf)
6946{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006947 i40e_prep_for_reset(pf);
6948 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006949}
6950
6951/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006952 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006953 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006954 *
6955 * Called from the MDD irq handler to identify possibly malicious vfs
6956 **/
6957static void i40e_handle_mdd_event(struct i40e_pf *pf)
6958{
6959 struct i40e_hw *hw = &pf->hw;
6960 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006961 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006962 struct i40e_vf *vf;
6963 u32 reg;
6964 int i;
6965
6966 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
6967 return;
6968
6969 /* find what triggered the MDD event */
6970 reg = rd32(hw, I40E_GL_MDET_TX);
6971 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006972 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
6973 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006974 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006975 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006976 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006977 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006978 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
6979 I40E_GL_MDET_TX_QUEUE_SHIFT) -
6980 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006981 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006982 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 +00006983 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006984 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
6985 mdd_detected = true;
6986 }
6987 reg = rd32(hw, I40E_GL_MDET_RX);
6988 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006989 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
6990 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07006991 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00006992 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00006993 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
6994 I40E_GL_MDET_RX_QUEUE_SHIFT) -
6995 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00006996 if (netif_msg_rx_err(pf))
6997 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
6998 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006999 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7000 mdd_detected = true;
7001 }
7002
Neerav Parikhdf430b12014-06-04 01:23:15 +00007003 if (mdd_detected) {
7004 reg = rd32(hw, I40E_PF_MDET_TX);
7005 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7006 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007007 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007008 pf_mdd_detected = true;
7009 }
7010 reg = rd32(hw, I40E_PF_MDET_RX);
7011 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7012 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007013 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007014 pf_mdd_detected = true;
7015 }
7016 /* Queue belongs to the PF, initiate a reset */
7017 if (pf_mdd_detected) {
7018 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7019 i40e_service_event_schedule(pf);
7020 }
7021 }
7022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023 /* see if one of the VFs needs its hand slapped */
7024 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7025 vf = &(pf->vf[i]);
7026 reg = rd32(hw, I40E_VP_MDET_TX(i));
7027 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7028 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7029 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007030 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7031 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032 }
7033
7034 reg = rd32(hw, I40E_VP_MDET_RX(i));
7035 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7036 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7037 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007038 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7039 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007040 }
7041
7042 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7043 dev_info(&pf->pdev->dev,
7044 "Too many MDD events on VF %d, disabled\n", i);
7045 dev_info(&pf->pdev->dev,
7046 "Use PF Control I/F to re-enable the VF\n");
7047 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7048 }
7049 }
7050
7051 /* re-enable mdd interrupt cause */
7052 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7053 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7054 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7055 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7056 i40e_flush(hw);
7057}
7058
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007059/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007060 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007061 * @pf: board private structure
7062 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007063static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007064{
Singhai, Anjali6a899022015-12-14 12:21:18 -08007065#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007066 struct i40e_hw *hw = &pf->hw;
7067 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007068 __be16 port;
7069 int i;
7070
Singhai, Anjali6a899022015-12-14 12:21:18 -08007071 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007072 return;
7073
Singhai, Anjali6a899022015-12-14 12:21:18 -08007074 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007075
7076 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007077 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7078 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7079 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007080 if (port)
7081 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Singhai, Anjali6a899022015-12-14 12:21:18 -08007082 pf->udp_ports[i].type,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007083 NULL, NULL);
7084 else
7085 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007086
7087 if (ret) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007088 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007089 "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007090 port ? "add" : "delete",
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007091 ntohs(port), i,
7092 i40e_stat_str(&pf->hw, ret),
7093 i40e_aq_str(&pf->hw,
7094 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007095 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007096 }
7097 }
7098 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08007099#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007100}
7101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007102/**
7103 * i40e_service_task - Run the driver's async subtasks
7104 * @work: pointer to work_struct containing our data
7105 **/
7106static void i40e_service_task(struct work_struct *work)
7107{
7108 struct i40e_pf *pf = container_of(work,
7109 struct i40e_pf,
7110 service_task);
7111 unsigned long start_time = jiffies;
7112
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007113 /* don't bother with service tasks if a reset is in progress */
7114 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7115 i40e_service_event_complete(pf);
7116 return;
7117 }
7118
Kiran Patilb03a8c12015-09-24 18:13:15 -04007119 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007120 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007121 i40e_reset_subtask(pf);
7122 i40e_handle_mdd_event(pf);
7123 i40e_vc_process_vflr_event(pf);
7124 i40e_watchdog_subtask(pf);
7125 i40e_fdir_reinit_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007126 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007127 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007128 i40e_clean_adminq_subtask(pf);
7129
7130 i40e_service_event_complete(pf);
7131
7132 /* If the tasks have taken longer than one timer cycle or there
7133 * is more work to be done, reschedule the service task now
7134 * rather than wait for the timer to tick again.
7135 */
7136 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7137 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7138 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7139 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7140 i40e_service_event_schedule(pf);
7141}
7142
7143/**
7144 * i40e_service_timer - timer callback
7145 * @data: pointer to PF struct
7146 **/
7147static void i40e_service_timer(unsigned long data)
7148{
7149 struct i40e_pf *pf = (struct i40e_pf *)data;
7150
7151 mod_timer(&pf->service_timer,
7152 round_jiffies(jiffies + pf->service_timer_period));
7153 i40e_service_event_schedule(pf);
7154}
7155
7156/**
7157 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7158 * @vsi: the VSI being configured
7159 **/
7160static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7161{
7162 struct i40e_pf *pf = vsi->back;
7163
7164 switch (vsi->type) {
7165 case I40E_VSI_MAIN:
7166 vsi->alloc_queue_pairs = pf->num_lan_qps;
7167 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7168 I40E_REQ_DESCRIPTOR_MULTIPLE);
7169 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7170 vsi->num_q_vectors = pf->num_lan_msix;
7171 else
7172 vsi->num_q_vectors = 1;
7173
7174 break;
7175
7176 case I40E_VSI_FDIR:
7177 vsi->alloc_queue_pairs = 1;
7178 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7179 I40E_REQ_DESCRIPTOR_MULTIPLE);
7180 vsi->num_q_vectors = 1;
7181 break;
7182
7183 case I40E_VSI_VMDQ2:
7184 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7185 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7186 I40E_REQ_DESCRIPTOR_MULTIPLE);
7187 vsi->num_q_vectors = pf->num_vmdq_msix;
7188 break;
7189
7190 case I40E_VSI_SRIOV:
7191 vsi->alloc_queue_pairs = pf->num_vf_qps;
7192 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7193 I40E_REQ_DESCRIPTOR_MULTIPLE);
7194 break;
7195
Vasu Dev38e00432014-08-01 13:27:03 -07007196#ifdef I40E_FCOE
7197 case I40E_VSI_FCOE:
7198 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7199 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7200 I40E_REQ_DESCRIPTOR_MULTIPLE);
7201 vsi->num_q_vectors = pf->num_fcoe_msix;
7202 break;
7203
7204#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007205 default:
7206 WARN_ON(1);
7207 return -ENODATA;
7208 }
7209
7210 return 0;
7211}
7212
7213/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007214 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7215 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007216 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007217 *
7218 * On error: returns error code (negative)
7219 * On success: returns 0
7220 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007221static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007222{
7223 int size;
7224 int ret = 0;
7225
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007226 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007227 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7228 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7229 if (!vsi->tx_rings)
7230 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007231 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7232
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007233 if (alloc_qvectors) {
7234 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007235 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007236 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7237 if (!vsi->q_vectors) {
7238 ret = -ENOMEM;
7239 goto err_vectors;
7240 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007241 }
7242 return ret;
7243
7244err_vectors:
7245 kfree(vsi->tx_rings);
7246 return ret;
7247}
7248
7249/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007250 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7251 * @pf: board private structure
7252 * @type: type of VSI
7253 *
7254 * On error: returns error code (negative)
7255 * On success: returns vsi index in PF (positive)
7256 **/
7257static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7258{
7259 int ret = -ENODEV;
7260 struct i40e_vsi *vsi;
7261 int vsi_idx;
7262 int i;
7263
7264 /* Need to protect the allocation of the VSIs at the PF level */
7265 mutex_lock(&pf->switch_mutex);
7266
7267 /* VSI list may be fragmented if VSI creation/destruction has
7268 * been happening. We can afford to do a quick scan to look
7269 * for any free VSIs in the list.
7270 *
7271 * find next empty vsi slot, looping back around if necessary
7272 */
7273 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007274 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007275 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007276 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007277 i = 0;
7278 while (i < pf->next_vsi && pf->vsi[i])
7279 i++;
7280 }
7281
Mitch Williams505682c2014-05-20 08:01:37 +00007282 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007283 vsi_idx = i; /* Found one! */
7284 } else {
7285 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007286 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007287 }
7288 pf->next_vsi = ++i;
7289
7290 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7291 if (!vsi) {
7292 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007293 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007294 }
7295 vsi->type = type;
7296 vsi->back = pf;
7297 set_bit(__I40E_DOWN, &vsi->state);
7298 vsi->flags = 0;
7299 vsi->idx = vsi_idx;
7300 vsi->rx_itr_setting = pf->rx_itr_default;
7301 vsi->tx_itr_setting = pf->tx_itr_default;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007302 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007303 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7304 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007305 vsi->netdev_registered = false;
7306 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7307 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007308 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007309
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007310 ret = i40e_set_num_rings_in_vsi(vsi);
7311 if (ret)
7312 goto err_rings;
7313
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007314 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007315 if (ret)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007316 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007317
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007318 /* Setup default MSIX irq handler for VSI */
7319 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7320
Kiran Patil21659032015-09-30 14:09:03 -04007321 /* Initialize VSI lock */
7322 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007323 pf->vsi[vsi_idx] = vsi;
7324 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007325 goto unlock_pf;
7326
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007327err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007328 pf->next_vsi = i - 1;
7329 kfree(vsi);
7330unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007331 mutex_unlock(&pf->switch_mutex);
7332 return ret;
7333}
7334
7335/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007336 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7337 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007338 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007339 *
7340 * On error: returns error code (negative)
7341 * On success: returns 0
7342 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007343static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007344{
7345 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007346 if (free_qvectors) {
7347 kfree(vsi->q_vectors);
7348 vsi->q_vectors = NULL;
7349 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007350 kfree(vsi->tx_rings);
7351 vsi->tx_rings = NULL;
7352 vsi->rx_rings = NULL;
7353}
7354
7355/**
Helin Zhang28c58692015-10-26 19:44:27 -04007356 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7357 * and lookup table
7358 * @vsi: Pointer to VSI structure
7359 */
7360static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7361{
7362 if (!vsi)
7363 return;
7364
7365 kfree(vsi->rss_hkey_user);
7366 vsi->rss_hkey_user = NULL;
7367
7368 kfree(vsi->rss_lut_user);
7369 vsi->rss_lut_user = NULL;
7370}
7371
7372/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007373 * i40e_vsi_clear - Deallocate the VSI provided
7374 * @vsi: the VSI being un-configured
7375 **/
7376static int i40e_vsi_clear(struct i40e_vsi *vsi)
7377{
7378 struct i40e_pf *pf;
7379
7380 if (!vsi)
7381 return 0;
7382
7383 if (!vsi->back)
7384 goto free_vsi;
7385 pf = vsi->back;
7386
7387 mutex_lock(&pf->switch_mutex);
7388 if (!pf->vsi[vsi->idx]) {
7389 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7390 vsi->idx, vsi->idx, vsi, vsi->type);
7391 goto unlock_vsi;
7392 }
7393
7394 if (pf->vsi[vsi->idx] != vsi) {
7395 dev_err(&pf->pdev->dev,
7396 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7397 pf->vsi[vsi->idx]->idx,
7398 pf->vsi[vsi->idx],
7399 pf->vsi[vsi->idx]->type,
7400 vsi->idx, vsi, vsi->type);
7401 goto unlock_vsi;
7402 }
7403
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007404 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007405 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7406 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7407
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007408 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007409 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007411 pf->vsi[vsi->idx] = NULL;
7412 if (vsi->idx < pf->next_vsi)
7413 pf->next_vsi = vsi->idx;
7414
7415unlock_vsi:
7416 mutex_unlock(&pf->switch_mutex);
7417free_vsi:
7418 kfree(vsi);
7419
7420 return 0;
7421}
7422
7423/**
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007424 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7425 * @vsi: the VSI being cleaned
7426 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007427static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007428{
7429 int i;
7430
Greg Rose8e9dca52013-12-18 13:45:53 +00007431 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007432 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007433 kfree_rcu(vsi->tx_rings[i], rcu);
7434 vsi->tx_rings[i] = NULL;
7435 vsi->rx_rings[i] = NULL;
7436 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007437 }
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007438}
7439
7440/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007441 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7442 * @vsi: the VSI being configured
7443 **/
7444static int i40e_alloc_rings(struct i40e_vsi *vsi)
7445{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007446 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007447 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007448 int i;
7449
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007450 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007451 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007452 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007453 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7454 if (!tx_ring)
7455 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007456
7457 tx_ring->queue_index = i;
7458 tx_ring->reg_idx = vsi->base_queue + i;
7459 tx_ring->ring_active = false;
7460 tx_ring->vsi = vsi;
7461 tx_ring->netdev = vsi->netdev;
7462 tx_ring->dev = &pf->pdev->dev;
7463 tx_ring->count = vsi->num_desc;
7464 tx_ring->size = 0;
7465 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007466 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7467 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Anjali Singhai Jain527274c2015-06-05 12:20:31 -04007468 if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
7469 tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007470 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007472 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007473 rx_ring->queue_index = i;
7474 rx_ring->reg_idx = vsi->base_queue + i;
7475 rx_ring->ring_active = false;
7476 rx_ring->vsi = vsi;
7477 rx_ring->netdev = vsi->netdev;
7478 rx_ring->dev = &pf->pdev->dev;
7479 rx_ring->count = vsi->num_desc;
7480 rx_ring->size = 0;
7481 rx_ring->dcb_tc = 0;
7482 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7483 set_ring_16byte_desc_enabled(rx_ring);
7484 else
7485 clear_ring_16byte_desc_enabled(rx_ring);
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007486 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007487 }
7488
7489 return 0;
Alexander Duyck9f65e15b2013-09-28 06:00:58 +00007490
7491err_out:
7492 i40e_vsi_clear_rings(vsi);
7493 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007494}
7495
7496/**
7497 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7498 * @pf: board private structure
7499 * @vectors: the number of MSI-X vectors to request
7500 *
7501 * Returns the number of vectors reserved, or error
7502 **/
7503static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7504{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007505 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7506 I40E_MIN_MSIX, vectors);
7507 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007508 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007509 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007510 vectors = 0;
7511 }
7512
7513 return vectors;
7514}
7515
7516/**
7517 * i40e_init_msix - Setup the MSIX capability
7518 * @pf: board private structure
7519 *
7520 * Work with the OS to set up the MSIX vectors needed.
7521 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007522 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007523 **/
7524static int i40e_init_msix(struct i40e_pf *pf)
7525{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007526 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007527 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007528 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007529 int v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007530
7531 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7532 return -ENODEV;
7533
7534 /* The number of vectors we'll request will be comprised of:
7535 * - Add 1 for "other" cause for Admin Queue events, etc.
7536 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007537 * - Queues being used for RSS.
7538 * We don't need as many as max_rss_size vectors.
7539 * use rss_size instead in the calculation since that
7540 * is governed by number of cpus in the system.
7541 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007542 * - The number of VMDq pairs
Vasu Dev38e00432014-08-01 13:27:03 -07007543#ifdef I40E_FCOE
7544 * - The number of FCOE qps.
7545#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007546 * Once we count this up, try the request.
7547 *
7548 * If we can't get what we want, we'll simplify to nearly nothing
7549 * and try again. If that still fails, we punt.
7550 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007551 vectors_left = hw->func_caps.num_msix_vectors;
7552 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007553
Shannon Nelson1e200e42015-02-27 09:15:24 +00007554 /* reserve one vector for miscellaneous handler */
7555 if (vectors_left) {
7556 v_budget++;
7557 vectors_left--;
7558 }
7559
7560 /* reserve vectors for the main PF traffic queues */
7561 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7562 vectors_left -= pf->num_lan_msix;
7563 v_budget += pf->num_lan_msix;
7564
7565 /* reserve one vector for sideband flow director */
7566 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7567 if (vectors_left) {
7568 v_budget++;
7569 vectors_left--;
7570 } else {
7571 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7572 }
7573 }
John W Linville83840e42015-01-14 03:06:28 +00007574
Vasu Dev38e00432014-08-01 13:27:03 -07007575#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007576 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007577 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007578 if (!vectors_left)
7579 pf->num_fcoe_msix = 0;
7580 else if (vectors_left >= pf->num_fcoe_qps)
7581 pf->num_fcoe_msix = pf->num_fcoe_qps;
7582 else
7583 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007584 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007585 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007586 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007587
Vasu Dev38e00432014-08-01 13:27:03 -07007588#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007589 /* any vectors left over go for VMDq support */
7590 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7591 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7592 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7593
7594 /* if we're short on vectors for what's desired, we limit
7595 * the queues per vmdq. If this is still more than are
7596 * available, the user will need to change the number of
7597 * queues/vectors used by the PF later with the ethtool
7598 * channels command
7599 */
7600 if (vmdq_vecs < vmdq_vecs_wanted)
7601 pf->num_vmdq_qps = 1;
7602 pf->num_vmdq_msix = pf->num_vmdq_qps;
7603
7604 v_budget += vmdq_vecs;
7605 vectors_left -= vmdq_vecs;
7606 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007607
7608 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7609 GFP_KERNEL);
7610 if (!pf->msix_entries)
7611 return -ENOMEM;
7612
7613 for (i = 0; i < v_budget; i++)
7614 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007615 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007616
Shannon Nelson3b444392015-02-26 16:15:57 +00007617 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007618 /* If we have limited resources, we will start with no vectors
7619 * for the special features and then allocate vectors to some
7620 * of these features based on the policy and at the end disable
7621 * the features that did not get any vectors.
7622 */
Vasu Dev38e00432014-08-01 13:27:03 -07007623#ifdef I40E_FCOE
7624 pf->num_fcoe_qps = 0;
7625 pf->num_fcoe_msix = 0;
7626#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007627 pf->num_vmdq_msix = 0;
7628 }
7629
Shannon Nelson3b444392015-02-26 16:15:57 +00007630 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007631 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7632 kfree(pf->msix_entries);
7633 pf->msix_entries = NULL;
7634 return -ENODEV;
7635
Shannon Nelson3b444392015-02-26 16:15:57 +00007636 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007637 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007638 pf->num_vmdq_vsis = 0;
7639 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007640 pf->num_lan_qps = 1;
7641 pf->num_lan_msix = 1;
7642
Shannon Nelson3b444392015-02-26 16:15:57 +00007643 } else if (v_actual != v_budget) {
7644 int vec;
7645
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007646 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007647 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007648
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007649 /* Scale vector usage down */
7650 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007651 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007652 pf->num_vmdq_qps = 1;
7653 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007654
7655 /* partition out the remaining vectors */
7656 switch (vec) {
7657 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007658 pf->num_lan_msix = 1;
7659 break;
7660 case 3:
Vasu Dev38e00432014-08-01 13:27:03 -07007661#ifdef I40E_FCOE
7662 /* give one vector to FCoE */
7663 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7664 pf->num_lan_msix = 1;
7665 pf->num_fcoe_msix = 1;
7666 }
7667#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007668 pf->num_lan_msix = 2;
Vasu Dev38e00432014-08-01 13:27:03 -07007669#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007670 break;
7671 default:
Vasu Dev38e00432014-08-01 13:27:03 -07007672#ifdef I40E_FCOE
7673 /* give one vector to FCoE */
7674 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7675 pf->num_fcoe_msix = 1;
7676 vec--;
7677 }
7678#endif
Shannon Nelson1e200e42015-02-27 09:15:24 +00007679 /* give the rest to the PF */
7680 pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007681 break;
7682 }
7683 }
7684
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007685 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7686 (pf->num_vmdq_msix == 0)) {
7687 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7688 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7689 }
Vasu Dev38e00432014-08-01 13:27:03 -07007690#ifdef I40E_FCOE
7691
7692 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7693 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7694 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7695 }
7696#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007697 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698}
7699
7700/**
Greg Rose90e04072014-03-06 08:59:57 +00007701 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007702 * @vsi: the VSI being configured
7703 * @v_idx: index of the vector in the vsi struct
7704 *
7705 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7706 **/
Greg Rose90e04072014-03-06 08:59:57 +00007707static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007708{
7709 struct i40e_q_vector *q_vector;
7710
7711 /* allocate q_vector */
7712 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7713 if (!q_vector)
7714 return -ENOMEM;
7715
7716 q_vector->vsi = vsi;
7717 q_vector->v_idx = v_idx;
7718 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7719 if (vsi->netdev)
7720 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007721 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007722
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007723 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7724 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7725
Alexander Duyck493fb302013-09-28 07:01:44 +00007726 /* tie q_vector and vsi together */
7727 vsi->q_vectors[v_idx] = q_vector;
7728
7729 return 0;
7730}
7731
7732/**
Greg Rose90e04072014-03-06 08:59:57 +00007733 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007734 * @vsi: the VSI being configured
7735 *
7736 * We allocate one q_vector per queue interrupt. If allocation fails we
7737 * return -ENOMEM.
7738 **/
Greg Rose90e04072014-03-06 08:59:57 +00007739static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007740{
7741 struct i40e_pf *pf = vsi->back;
7742 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007743 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007744
7745 /* if not MSIX, give the one vector only to the LAN VSI */
7746 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7747 num_q_vectors = vsi->num_q_vectors;
7748 else if (vsi == pf->vsi[pf->lan_vsi])
7749 num_q_vectors = 1;
7750 else
7751 return -EINVAL;
7752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007753 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007754 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007755 if (err)
7756 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007757 }
7758
7759 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007760
7761err_out:
7762 while (v_idx--)
7763 i40e_free_q_vector(vsi, v_idx);
7764
7765 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007766}
7767
7768/**
7769 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7770 * @pf: board private structure to initialize
7771 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007772static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007773{
Shannon Nelson3b444392015-02-26 16:15:57 +00007774 int vectors = 0;
7775 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007776
7777 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007778 vectors = i40e_init_msix(pf);
7779 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007780 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007781#ifdef I40E_FCOE
7782 I40E_FLAG_FCOE_ENABLED |
7783#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007784 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007785 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007786 I40E_FLAG_SRIOV_ENABLED |
7787 I40E_FLAG_FD_SB_ENABLED |
7788 I40E_FLAG_FD_ATR_ENABLED |
7789 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007790
7791 /* rework the queue expectations without MSIX */
7792 i40e_determine_queue_usage(pf);
7793 }
7794 }
7795
7796 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7797 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007798 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007799 vectors = pci_enable_msi(pf->pdev);
7800 if (vectors < 0) {
7801 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7802 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007803 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7804 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007805 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007806 }
7807
Shannon Nelson958a3e32013-09-28 07:13:28 +00007808 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007809 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007810
Shannon Nelson3b444392015-02-26 16:15:57 +00007811 /* set up vector assignment tracking */
7812 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7813 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007814 if (!pf->irq_pile) {
7815 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7816 return -ENOMEM;
7817 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007818 pf->irq_pile->num_entries = vectors;
7819 pf->irq_pile->search_hint = 0;
7820
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007821 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007822 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007823
7824 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007825}
7826
7827/**
7828 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7829 * @pf: board private structure
7830 *
7831 * This sets up the handler for MSIX 0, which is used to manage the
7832 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7833 * when in MSI or Legacy interrupt mode.
7834 **/
7835static int i40e_setup_misc_vector(struct i40e_pf *pf)
7836{
7837 struct i40e_hw *hw = &pf->hw;
7838 int err = 0;
7839
7840 /* Only request the irq if this is the first time through, and
7841 * not when we're rebuilding after a Reset
7842 */
7843 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7844 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007845 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007846 if (err) {
7847 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007848 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007849 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007850 return -EFAULT;
7851 }
7852 }
7853
Jacob Kellerab437b52014-12-14 01:55:08 +00007854 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007855
7856 /* associate no queues to the misc vector */
7857 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7858 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7859
7860 i40e_flush(hw);
7861
7862 i40e_irq_dynamic_enable_icr0(pf);
7863
7864 return err;
7865}
7866
7867/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007868 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7869 * @vsi: vsi structure
7870 * @seed: RSS hash seed
7871 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007872static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7873 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007874{
7875 struct i40e_aqc_get_set_rss_key_data rss_key;
7876 struct i40e_pf *pf = vsi->back;
7877 struct i40e_hw *hw = &pf->hw;
7878 bool pf_lut = false;
7879 u8 *rss_lut;
7880 int ret, i;
7881
7882 memset(&rss_key, 0, sizeof(rss_key));
7883 memcpy(&rss_key, seed, sizeof(rss_key));
7884
7885 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7886 if (!rss_lut)
7887 return -ENOMEM;
7888
7889 /* Populate the LUT with max no. of queues in round robin fashion */
7890 for (i = 0; i < vsi->rss_table_size; i++)
7891 rss_lut[i] = i % vsi->rss_size;
7892
7893 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7894 if (ret) {
7895 dev_info(&pf->pdev->dev,
7896 "Cannot set RSS key, err %s aq_err %s\n",
7897 i40e_stat_str(&pf->hw, ret),
7898 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007899 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007900 }
7901
7902 if (vsi->type == I40E_VSI_MAIN)
7903 pf_lut = true;
7904
7905 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7906 vsi->rss_table_size);
7907 if (ret)
7908 dev_info(&pf->pdev->dev,
7909 "Cannot set RSS lut, err %s aq_err %s\n",
7910 i40e_stat_str(&pf->hw, ret),
7911 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7912
Anjali Singhai126b63d2015-08-24 13:26:53 -07007913config_rss_aq_out:
7914 kfree(rss_lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007915 return ret;
7916}
7917
7918/**
7919 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7920 * @vsi: VSI structure
7921 **/
7922static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7923{
7924 u8 seed[I40E_HKEY_ARRAY_SIZE];
7925 struct i40e_pf *pf = vsi->back;
Helin Zhange69ff812015-10-21 19:56:22 -04007926 u8 *lut;
7927 int ret;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007928
Helin Zhange69ff812015-10-21 19:56:22 -04007929 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
7930 return 0;
7931
7932 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
7933 if (!lut)
7934 return -ENOMEM;
7935
7936 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007937 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhangacd65442015-10-26 19:44:28 -04007938 vsi->rss_size = min_t(int, pf->alloc_rss_size, vsi->num_queue_pairs);
Helin Zhange69ff812015-10-21 19:56:22 -04007939 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
7940 kfree(lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007941
Helin Zhange69ff812015-10-21 19:56:22 -04007942 return ret;
7943}
7944
7945/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08007946 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
7947 * @vsi: Pointer to vsi structure
7948 * @seed: Buffter to store the hash keys
7949 * @lut: Buffer to store the lookup table entries
7950 * @lut_size: Size of buffer to store the lookup table entries
7951 *
7952 * Return 0 on success, negative on failure
7953 */
7954static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7955 u8 *lut, u16 lut_size)
7956{
7957 struct i40e_pf *pf = vsi->back;
7958 struct i40e_hw *hw = &pf->hw;
7959 int ret = 0;
7960
7961 if (seed) {
7962 ret = i40e_aq_get_rss_key(hw, vsi->id,
7963 (struct i40e_aqc_get_set_rss_key_data *)seed);
7964 if (ret) {
7965 dev_info(&pf->pdev->dev,
7966 "Cannot get RSS key, err %s aq_err %s\n",
7967 i40e_stat_str(&pf->hw, ret),
7968 i40e_aq_str(&pf->hw,
7969 pf->hw.aq.asq_last_status));
7970 return ret;
7971 }
7972 }
7973
7974 if (lut) {
7975 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
7976
7977 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
7978 if (ret) {
7979 dev_info(&pf->pdev->dev,
7980 "Cannot get RSS lut, err %s aq_err %s\n",
7981 i40e_stat_str(&pf->hw, ret),
7982 i40e_aq_str(&pf->hw,
7983 pf->hw.aq.asq_last_status));
7984 return ret;
7985 }
7986 }
7987
7988 return ret;
7989}
7990
7991/**
Helin Zhang043dd652015-10-21 19:56:23 -04007992 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04007993 * @vsi: Pointer to vsi structure
7994 * @seed: RSS hash seed
7995 * @lut: Lookup table
7996 * @lut_size: Lookup table size
7997 *
7998 * Returns 0 on success, negative on failure
7999 **/
8000static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8001 const u8 *lut, u16 lut_size)
8002{
8003 struct i40e_pf *pf = vsi->back;
8004 struct i40e_hw *hw = &pf->hw;
8005 u8 i;
8006
8007 /* Fill out hash function seed */
8008 if (seed) {
8009 u32 *seed_dw = (u32 *)seed;
8010
8011 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8012 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
8013 }
8014
8015 if (lut) {
8016 u32 *lut_dw = (u32 *)lut;
8017
8018 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8019 return -EINVAL;
8020
8021 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8022 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8023 }
8024 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008025
8026 return 0;
8027}
8028
8029/**
Helin Zhang043dd652015-10-21 19:56:23 -04008030 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8031 * @vsi: Pointer to VSI structure
8032 * @seed: Buffer to store the keys
8033 * @lut: Buffer to store the lookup table entries
8034 * @lut_size: Size of buffer to store the lookup table entries
8035 *
8036 * Returns 0 on success, negative on failure
8037 */
8038static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8039 u8 *lut, u16 lut_size)
8040{
8041 struct i40e_pf *pf = vsi->back;
8042 struct i40e_hw *hw = &pf->hw;
8043 u16 i;
8044
8045 if (seed) {
8046 u32 *seed_dw = (u32 *)seed;
8047
8048 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8049 seed_dw[i] = rd32(hw, I40E_PFQF_HKEY(i));
8050 }
8051 if (lut) {
8052 u32 *lut_dw = (u32 *)lut;
8053
8054 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8055 return -EINVAL;
8056 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8057 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8058 }
8059
8060 return 0;
8061}
8062
8063/**
8064 * i40e_config_rss - Configure RSS keys and lut
8065 * @vsi: Pointer to VSI structure
8066 * @seed: RSS hash seed
8067 * @lut: Lookup table
8068 * @lut_size: Lookup table size
8069 *
8070 * Returns 0 on success, negative on failure
8071 */
8072int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8073{
8074 struct i40e_pf *pf = vsi->back;
8075
8076 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8077 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8078 else
8079 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8080}
8081
8082/**
8083 * i40e_get_rss - Get RSS keys and lut
8084 * @vsi: Pointer to VSI structure
8085 * @seed: Buffer to store the keys
8086 * @lut: Buffer to store the lookup table entries
8087 * lut_size: Size of buffer to store the lookup table entries
8088 *
8089 * Returns 0 on success, negative on failure
8090 */
8091int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8092{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008093 struct i40e_pf *pf = vsi->back;
8094
8095 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8096 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8097 else
8098 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008099}
8100
8101/**
Helin Zhange69ff812015-10-21 19:56:22 -04008102 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8103 * @pf: Pointer to board private structure
8104 * @lut: Lookup table
8105 * @rss_table_size: Lookup table size
8106 * @rss_size: Range of queue number for hashing
8107 */
8108static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8109 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008110{
Helin Zhange69ff812015-10-21 19:56:22 -04008111 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008112
Helin Zhange69ff812015-10-21 19:56:22 -04008113 for (i = 0; i < rss_table_size; i++)
8114 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008115}
8116
8117/**
Helin Zhang043dd652015-10-21 19:56:23 -04008118 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008119 * @pf: board private structure
8120 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008121static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008122{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008123 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008124 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008125 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008126 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008127 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008128 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008129 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008130
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008131 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
8132 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
8133 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008134 hena |= i40e_pf_get_default_rss_hena(pf);
8135
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008136 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
8137 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
8138
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008139 /* Determine the RSS table size based on the hardware capabilities */
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008140 reg_val = rd32(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008141 reg_val = (pf->rss_table_size == 512) ?
8142 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8143 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008144 wr32(hw, I40E_PFQF_CTL_0, reg_val);
8145
Helin Zhang28c58692015-10-26 19:44:27 -04008146 /* Determine the RSS size of the VSI */
8147 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008148 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8149 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008150
Helin Zhange69ff812015-10-21 19:56:22 -04008151 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8152 if (!lut)
8153 return -ENOMEM;
8154
Helin Zhang28c58692015-10-26 19:44:27 -04008155 /* Use user configured lut if there is one, otherwise use default */
8156 if (vsi->rss_lut_user)
8157 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8158 else
8159 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008160
Helin Zhang28c58692015-10-26 19:44:27 -04008161 /* Use user configured hash key if there is one, otherwise
8162 * use default.
8163 */
8164 if (vsi->rss_hkey_user)
8165 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8166 else
8167 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008168 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008169 kfree(lut);
8170
8171 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008172}
8173
8174/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008175 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8176 * @pf: board private structure
8177 * @queue_count: the requested queue count for rss.
8178 *
8179 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8180 * count which may be different from the requested queue count.
8181 **/
8182int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8183{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008184 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8185 int new_rss_size;
8186
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008187 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8188 return 0;
8189
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008190 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008191
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008192 if (queue_count != vsi->num_queue_pairs) {
8193 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008194 i40e_prep_for_reset(pf);
8195
Helin Zhangacd65442015-10-26 19:44:28 -04008196 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008197
8198 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008199
8200 /* Discard the user configured hash keys and lut, if less
8201 * queues are enabled.
8202 */
8203 if (queue_count < vsi->rss_size) {
8204 i40e_clear_rss_config_user(vsi);
8205 dev_dbg(&pf->pdev->dev,
8206 "discard user configured hash keys and lut\n");
8207 }
8208
8209 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008210 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8211 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008212
Helin Zhang043dd652015-10-21 19:56:23 -04008213 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008214 }
Helin Zhange36b0b12015-10-26 19:44:36 -04008215 dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
8216 pf->alloc_rss_size, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008217 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008218}
8219
8220/**
Greg Rosef4492db2015-02-06 08:52:12 +00008221 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8222 * @pf: board private structure
8223 **/
8224i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8225{
8226 i40e_status status;
8227 bool min_valid, max_valid;
8228 u32 max_bw, min_bw;
8229
8230 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8231 &min_valid, &max_valid);
8232
8233 if (!status) {
8234 if (min_valid)
8235 pf->npar_min_bw = min_bw;
8236 if (max_valid)
8237 pf->npar_max_bw = max_bw;
8238 }
8239
8240 return status;
8241}
8242
8243/**
8244 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8245 * @pf: board private structure
8246 **/
8247i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8248{
8249 struct i40e_aqc_configure_partition_bw_data bw_data;
8250 i40e_status status;
8251
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008252 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008253 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008254 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8255 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8256
8257 /* Set the new bandwidths */
8258 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8259
8260 return status;
8261}
8262
8263/**
8264 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8265 * @pf: board private structure
8266 **/
8267i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8268{
8269 /* Commit temporary BW setting to permanent NVM image */
8270 enum i40e_admin_queue_err last_aq_status;
8271 i40e_status ret;
8272 u16 nvm_word;
8273
8274 if (pf->hw.partition_id != 1) {
8275 dev_info(&pf->pdev->dev,
8276 "Commit BW only works on partition 1! This is partition %d",
8277 pf->hw.partition_id);
8278 ret = I40E_NOT_SUPPORTED;
8279 goto bw_commit_out;
8280 }
8281
8282 /* Acquire NVM for read access */
8283 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8284 last_aq_status = pf->hw.aq.asq_last_status;
8285 if (ret) {
8286 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008287 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8288 i40e_stat_str(&pf->hw, ret),
8289 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008290 goto bw_commit_out;
8291 }
8292
8293 /* Read word 0x10 of NVM - SW compatibility word 1 */
8294 ret = i40e_aq_read_nvm(&pf->hw,
8295 I40E_SR_NVM_CONTROL_WORD,
8296 0x10, sizeof(nvm_word), &nvm_word,
8297 false, NULL);
8298 /* Save off last admin queue command status before releasing
8299 * the NVM
8300 */
8301 last_aq_status = pf->hw.aq.asq_last_status;
8302 i40e_release_nvm(&pf->hw);
8303 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008304 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8305 i40e_stat_str(&pf->hw, ret),
8306 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008307 goto bw_commit_out;
8308 }
8309
8310 /* Wait a bit for NVM release to complete */
8311 msleep(50);
8312
8313 /* Acquire NVM for write access */
8314 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8315 last_aq_status = pf->hw.aq.asq_last_status;
8316 if (ret) {
8317 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008318 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8319 i40e_stat_str(&pf->hw, ret),
8320 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008321 goto bw_commit_out;
8322 }
8323 /* Write it back out unchanged to initiate update NVM,
8324 * which will force a write of the shadow (alt) RAM to
8325 * the NVM - thus storing the bandwidth values permanently.
8326 */
8327 ret = i40e_aq_update_nvm(&pf->hw,
8328 I40E_SR_NVM_CONTROL_WORD,
8329 0x10, sizeof(nvm_word),
8330 &nvm_word, true, NULL);
8331 /* Save off last admin queue command status before releasing
8332 * the NVM
8333 */
8334 last_aq_status = pf->hw.aq.asq_last_status;
8335 i40e_release_nvm(&pf->hw);
8336 if (ret)
8337 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008338 "BW settings NOT SAVED, err %s aq_err %s\n",
8339 i40e_stat_str(&pf->hw, ret),
8340 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008341bw_commit_out:
8342
8343 return ret;
8344}
8345
8346/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008347 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8348 * @pf: board private structure to initialize
8349 *
8350 * i40e_sw_init initializes the Adapter private data structure.
8351 * Fields are initialized based on PCI device information and
8352 * OS network device settings (MTU size).
8353 **/
8354static int i40e_sw_init(struct i40e_pf *pf)
8355{
8356 int err = 0;
8357 int size;
8358
8359 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8360 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
Shannon Nelson27599972013-11-16 10:00:43 +00008361 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008362 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8363 if (I40E_DEBUG_USER & debug)
8364 pf->hw.debug_mask = debug;
8365 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8366 I40E_DEFAULT_MSG_ENABLE);
8367 }
8368
8369 /* Set default capability flags */
8370 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8371 I40E_FLAG_MSI_ENABLED |
Shannon Nelson9ac77262015-08-27 11:42:40 -04008372 I40E_FLAG_LINK_POLLING_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008373 I40E_FLAG_MSIX_ENABLED;
8374
8375 if (iommu_present(&pci_bus_type))
8376 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
8377 else
8378 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008379
Mitch Williamsca99eb92014-04-04 04:43:07 +00008380 /* Set default ITR */
8381 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8382 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8383
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008384 /* Depending on PF configurations, it is possible that the RSS
8385 * maximum might end up larger than the available queues
8386 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008387 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008388 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008389 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008390 pf->rss_size_max = min_t(int, pf->rss_size_max,
8391 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008392 if (pf->hw.func_caps.rss) {
8393 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008394 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8395 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008396 }
8397
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008398 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008399 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008400 pf->flags |= I40E_FLAG_MFP_ENABLED;
8401 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008402 if (i40e_get_npar_bw_setting(pf))
8403 dev_warn(&pf->pdev->dev,
8404 "Could not get NPAR bw settings\n");
8405 else
8406 dev_info(&pf->pdev->dev,
8407 "Min BW = %8.8x, Max BW = %8.8x\n",
8408 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008409 }
8410
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008411 /* FW/NVM is not yet fixed in this regard */
8412 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8413 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8414 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8415 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008416 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8417 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008418 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008419 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008420 else
8421 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008422 pf->fdir_pf_filter_count =
8423 pf->hw.func_caps.fd_filters_guaranteed;
8424 pf->hw.fdir_shared_filter_count =
8425 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008426 }
8427
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008428 if (((pf->hw.mac.type == I40E_MAC_X710) ||
8429 (pf->hw.mac.type == I40E_MAC_XL710)) &&
8430 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
8431 (pf->hw.aq.fw_maj_ver < 4)))
8432 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
8433
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008434 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008435 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008436 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008437 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008438 }
8439
Vasu Dev38e00432014-08-01 13:27:03 -07008440#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008441 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008442
8443#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008444#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f12014-12-11 07:06:34 +00008445 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008446 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8447 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8448 pf->num_req_vfs = min_t(int,
8449 pf->hw.func_caps.num_vfs,
8450 I40E_MAX_VF_COUNT);
8451 }
8452#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008453 if (pf->hw.mac.type == I40E_MAC_X722) {
8454 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8455 I40E_FLAG_128_QP_RSS_CAPABLE |
8456 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8457 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8458 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008459 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Catherine Sullivanf8db54cc2015-12-22 14:25:14 -08008460 I40E_FLAG_100M_SGMII_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008461 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008462 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8463 ((pf->hw.aq.api_maj_ver == 1) &&
8464 (pf->hw.aq.api_min_ver > 4))) {
8465 /* Supported in FW API version higher than 1.4 */
8466 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008467 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8468 } else {
8469 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008470 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008472 pf->eeprom_version = 0xDEAD;
8473 pf->lan_veb = I40E_NO_VEB;
8474 pf->lan_vsi = I40E_NO_VSI;
8475
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008476 /* By default FW has this off for performance reasons */
8477 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8478
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008479 /* set up queue assignment tracking */
8480 size = sizeof(struct i40e_lump_tracking)
8481 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8482 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8483 if (!pf->qp_pile) {
8484 err = -ENOMEM;
8485 goto sw_init_done;
8486 }
8487 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8488 pf->qp_pile->search_hint = 0;
8489
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008490 pf->tx_timeout_recovery_level = 1;
8491
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008492 mutex_init(&pf->switch_mutex);
8493
Greg Rosec668a122015-02-26 16:10:39 +00008494 /* If NPAR is enabled nudge the Tx scheduler */
8495 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8496 i40e_set_npar_bw_setting(pf);
8497
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008498sw_init_done:
8499 return err;
8500}
8501
8502/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008503 * i40e_set_ntuple - set the ntuple feature flag and take action
8504 * @pf: board private structure to initialize
8505 * @features: the feature set that the stack is suggesting
8506 *
8507 * returns a bool to indicate if reset needs to happen
8508 **/
8509bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8510{
8511 bool need_reset = false;
8512
8513 /* Check if Flow Director n-tuple support was enabled or disabled. If
8514 * the state changed, we need to reset.
8515 */
8516 if (features & NETIF_F_NTUPLE) {
8517 /* Enable filters and mark for reset */
8518 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8519 need_reset = true;
8520 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8521 } else {
8522 /* turn off filters, mark for reset and clear SW filter list */
8523 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8524 need_reset = true;
8525 i40e_fdir_filter_exit(pf);
8526 }
8527 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008528 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008529 /* reset fd counters */
8530 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8531 pf->fdir_pf_active_filters = 0;
8532 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008533 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8534 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008535 /* if ATR was auto disabled it can be re-enabled. */
8536 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8537 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8538 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008539 }
8540 return need_reset;
8541}
8542
8543/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008544 * i40e_set_features - set the netdev feature flags
8545 * @netdev: ptr to the netdev being adjusted
8546 * @features: the feature set that the stack is suggesting
8547 **/
8548static int i40e_set_features(struct net_device *netdev,
8549 netdev_features_t features)
8550{
8551 struct i40e_netdev_priv *np = netdev_priv(netdev);
8552 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008553 struct i40e_pf *pf = vsi->back;
8554 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555
8556 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8557 i40e_vlan_stripping_enable(vsi);
8558 else
8559 i40e_vlan_stripping_disable(vsi);
8560
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008561 need_reset = i40e_set_ntuple(pf, features);
8562
8563 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008564 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008565
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008566 return 0;
8567}
8568
Singhai, Anjali6a899022015-12-14 12:21:18 -08008569#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008570/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008571 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008572 * @pf: board private structure
8573 * @port: The UDP port to look up
8574 *
8575 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8576 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008577static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008578{
8579 u8 i;
8580
8581 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008582 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008583 return i;
8584 }
8585
8586 return i;
8587}
8588
Singhai, Anjali6a899022015-12-14 12:21:18 -08008589#endif
Eric Dumazet5cae7612016-01-20 19:53:02 -08008590
8591#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008592/**
8593 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8594 * @netdev: This physical port's netdev
8595 * @sa_family: Socket Family that VXLAN is notifying us about
8596 * @port: New UDP port number that VXLAN started listening to
8597 **/
8598static void i40e_add_vxlan_port(struct net_device *netdev,
8599 sa_family_t sa_family, __be16 port)
8600{
8601 struct i40e_netdev_priv *np = netdev_priv(netdev);
8602 struct i40e_vsi *vsi = np->vsi;
8603 struct i40e_pf *pf = vsi->back;
8604 u8 next_idx;
8605 u8 idx;
8606
8607 if (sa_family == AF_INET6)
8608 return;
8609
Singhai, Anjali6a899022015-12-14 12:21:18 -08008610 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008611
8612 /* Check if port already exists */
8613 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008614 netdev_info(netdev, "vxlan port %d already offloaded\n",
8615 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008616 return;
8617 }
8618
8619 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008620 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008621
8622 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008623 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008624 ntohs(port));
8625 return;
8626 }
8627
8628 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008629 pf->udp_ports[next_idx].index = port;
8630 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8631 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8632 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008633}
8634
8635/**
8636 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8637 * @netdev: This physical port's netdev
8638 * @sa_family: Socket Family that VXLAN is notifying us about
8639 * @port: UDP port number that VXLAN stopped listening to
8640 **/
8641static void i40e_del_vxlan_port(struct net_device *netdev,
8642 sa_family_t sa_family, __be16 port)
8643{
8644 struct i40e_netdev_priv *np = netdev_priv(netdev);
8645 struct i40e_vsi *vsi = np->vsi;
8646 struct i40e_pf *pf = vsi->back;
8647 u8 idx;
8648
8649 if (sa_family == AF_INET6)
8650 return;
8651
Singhai, Anjali6a899022015-12-14 12:21:18 -08008652 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008653
8654 /* Check if port already exists */
8655 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8656 /* if port exists, set it to 0 (mark for deletion)
8657 * and make it pending
8658 */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008659 pf->udp_ports[idx].index = 0;
8660 pf->pending_udp_bitmap |= BIT_ULL(idx);
8661 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008662 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008663 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008664 ntohs(port));
8665 }
8666}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008667#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008668
Eric Dumazet5cae7612016-01-20 19:53:02 -08008669#if IS_ENABLED(CONFIG_GENEVE)
Singhai, Anjali6a899022015-12-14 12:21:18 -08008670/**
8671 * i40e_add_geneve_port - Get notifications about GENEVE ports that come up
8672 * @netdev: This physical port's netdev
8673 * @sa_family: Socket Family that GENEVE is notifying us about
8674 * @port: New UDP port number that GENEVE started listening to
8675 **/
8676static void i40e_add_geneve_port(struct net_device *netdev,
8677 sa_family_t sa_family, __be16 port)
8678{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008679 struct i40e_netdev_priv *np = netdev_priv(netdev);
8680 struct i40e_vsi *vsi = np->vsi;
8681 struct i40e_pf *pf = vsi->back;
8682 u8 next_idx;
8683 u8 idx;
8684
Anjali Singhaia340c782016-01-06 11:49:28 -08008685 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8686 return;
8687
Singhai, Anjali6a899022015-12-14 12:21:18 -08008688 if (sa_family == AF_INET6)
8689 return;
8690
8691 idx = i40e_get_udp_port_idx(pf, port);
8692
8693 /* Check if port already exists */
8694 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8695 netdev_info(netdev, "udp port %d already offloaded\n",
8696 ntohs(port));
8697 return;
8698 }
8699
8700 /* Now check if there is space to add the new port */
8701 next_idx = i40e_get_udp_port_idx(pf, 0);
8702
8703 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8704 netdev_info(netdev, "maximum number of UDP ports reached, not adding port %d\n",
8705 ntohs(port));
8706 return;
8707 }
8708
8709 /* New port: add it and mark its index in the bitmap */
8710 pf->udp_ports[next_idx].index = port;
8711 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8712 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8713 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8714
8715 dev_info(&pf->pdev->dev, "adding geneve port %d\n", ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008716}
8717
8718/**
8719 * i40e_del_geneve_port - Get notifications about GENEVE ports that go away
8720 * @netdev: This physical port's netdev
8721 * @sa_family: Socket Family that GENEVE is notifying us about
8722 * @port: UDP port number that GENEVE stopped listening to
8723 **/
8724static void i40e_del_geneve_port(struct net_device *netdev,
8725 sa_family_t sa_family, __be16 port)
8726{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008727 struct i40e_netdev_priv *np = netdev_priv(netdev);
8728 struct i40e_vsi *vsi = np->vsi;
8729 struct i40e_pf *pf = vsi->back;
8730 u8 idx;
8731
8732 if (sa_family == AF_INET6)
8733 return;
8734
Anjali Singhaia340c782016-01-06 11:49:28 -08008735 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8736 return;
8737
Singhai, Anjali6a899022015-12-14 12:21:18 -08008738 idx = i40e_get_udp_port_idx(pf, port);
8739
8740 /* Check if port already exists */
8741 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8742 /* if port exists, set it to 0 (mark for deletion)
8743 * and make it pending
8744 */
8745 pf->udp_ports[idx].index = 0;
8746 pf->pending_udp_bitmap |= BIT_ULL(idx);
8747 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8748
8749 dev_info(&pf->pdev->dev, "deleting geneve port %d\n",
8750 ntohs(port));
8751 } else {
8752 netdev_warn(netdev, "geneve port %d was not found, not deleting\n",
8753 ntohs(port));
8754 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08008755}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008756#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008757
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008758static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008759 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008760{
8761 struct i40e_netdev_priv *np = netdev_priv(netdev);
8762 struct i40e_pf *pf = np->vsi->back;
8763 struct i40e_hw *hw = &pf->hw;
8764
8765 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8766 return -EOPNOTSUPP;
8767
8768 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8769 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8770
8771 return 0;
8772}
8773
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008774/**
8775 * i40e_ndo_fdb_add - add an entry to the hardware database
8776 * @ndm: the input from the stack
8777 * @tb: pointer to array of nladdr (unused)
8778 * @dev: the net device pointer
8779 * @addr: the MAC address entry being added
8780 * @flags: instructions from stack about fdb operation
8781 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008782static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8783 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008784 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008785 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008786{
8787 struct i40e_netdev_priv *np = netdev_priv(dev);
8788 struct i40e_pf *pf = np->vsi->back;
8789 int err = 0;
8790
8791 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8792 return -EOPNOTSUPP;
8793
Or Gerlitz65891fe2014-12-14 18:19:05 +02008794 if (vid) {
8795 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8796 return -EINVAL;
8797 }
8798
Greg Rose4ba0dea2014-03-06 08:59:55 +00008799 /* Hardware does not support aging addresses so if a
8800 * ndm_state is given only allow permanent addresses
8801 */
8802 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8803 netdev_info(dev, "FDB only supports static addresses\n");
8804 return -EINVAL;
8805 }
8806
8807 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8808 err = dev_uc_add_excl(dev, addr);
8809 else if (is_multicast_ether_addr(addr))
8810 err = dev_mc_add_excl(dev, addr);
8811 else
8812 err = -EINVAL;
8813
8814 /* Only return duplicate errors if NLM_F_EXCL is set */
8815 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8816 err = 0;
8817
8818 return err;
8819}
8820
Neerav Parikh51616012015-02-06 08:52:14 +00008821/**
8822 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8823 * @dev: the netdev being configured
8824 * @nlh: RTNL message
8825 *
8826 * Inserts a new hardware bridge if not already created and
8827 * enables the bridging mode requested (VEB or VEPA). If the
8828 * hardware bridge has already been inserted and the request
8829 * is to change the mode then that requires a PF reset to
8830 * allow rebuild of the components with required hardware
8831 * bridge mode enabled.
8832 **/
8833static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008834 struct nlmsghdr *nlh,
8835 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008836{
8837 struct i40e_netdev_priv *np = netdev_priv(dev);
8838 struct i40e_vsi *vsi = np->vsi;
8839 struct i40e_pf *pf = vsi->back;
8840 struct i40e_veb *veb = NULL;
8841 struct nlattr *attr, *br_spec;
8842 int i, rem;
8843
8844 /* Only for PF VSI for now */
8845 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8846 return -EOPNOTSUPP;
8847
8848 /* Find the HW bridge for PF VSI */
8849 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8850 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8851 veb = pf->veb[i];
8852 }
8853
8854 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8855
8856 nla_for_each_nested(attr, br_spec, rem) {
8857 __u16 mode;
8858
8859 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8860 continue;
8861
8862 mode = nla_get_u16(attr);
8863 if ((mode != BRIDGE_MODE_VEPA) &&
8864 (mode != BRIDGE_MODE_VEB))
8865 return -EINVAL;
8866
8867 /* Insert a new HW bridge */
8868 if (!veb) {
8869 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8870 vsi->tc_config.enabled_tc);
8871 if (veb) {
8872 veb->bridge_mode = mode;
8873 i40e_config_bridge_mode(veb);
8874 } else {
8875 /* No Bridge HW offload available */
8876 return -ENOENT;
8877 }
8878 break;
8879 } else if (mode != veb->bridge_mode) {
8880 /* Existing HW bridge but different mode needs reset */
8881 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008882 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8883 if (mode == BRIDGE_MODE_VEB)
8884 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8885 else
8886 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8887 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008888 break;
8889 }
8890 }
8891
8892 return 0;
8893}
8894
8895/**
8896 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8897 * @skb: skb buff
8898 * @pid: process id
8899 * @seq: RTNL message seq #
8900 * @dev: the netdev being configured
8901 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008902 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008903 *
8904 * Return the mode in which the hardware bridge is operating in
8905 * i.e VEB or VEPA.
8906 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008907static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8908 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008909 u32 __always_unused filter_mask,
8910 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008911{
8912 struct i40e_netdev_priv *np = netdev_priv(dev);
8913 struct i40e_vsi *vsi = np->vsi;
8914 struct i40e_pf *pf = vsi->back;
8915 struct i40e_veb *veb = NULL;
8916 int i;
8917
8918 /* Only for PF VSI for now */
8919 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8920 return -EOPNOTSUPP;
8921
8922 /* Find the HW bridge for the PF VSI */
8923 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8924 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8925 veb = pf->veb[i];
8926 }
8927
8928 if (!veb)
8929 return 0;
8930
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02008931 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07008932 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00008933}
Neerav Parikh51616012015-02-06 08:52:14 +00008934
Singhai, Anjali6a899022015-12-14 12:21:18 -08008935/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
8936 * inner mac plus all inner ethertypes.
8937 */
8938#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07008939/**
8940 * i40e_features_check - Validate encapsulated packet conforms to limits
8941 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06008942 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07008943 * @features: Offload features that the stack believes apply
8944 **/
8945static netdev_features_t i40e_features_check(struct sk_buff *skb,
8946 struct net_device *dev,
8947 netdev_features_t features)
8948{
8949 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08008950 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07008951 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08008952 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07008953
8954 return features;
8955}
8956
Shannon Nelson37a29732015-02-27 09:15:19 +00008957static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008958 .ndo_open = i40e_open,
8959 .ndo_stop = i40e_close,
8960 .ndo_start_xmit = i40e_lan_xmit_frame,
8961 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
8962 .ndo_set_rx_mode = i40e_set_rx_mode,
8963 .ndo_validate_addr = eth_validate_addr,
8964 .ndo_set_mac_address = i40e_set_mac,
8965 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00008966 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008967 .ndo_tx_timeout = i40e_tx_timeout,
8968 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
8969 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
8970#ifdef CONFIG_NET_POLL_CONTROLLER
8971 .ndo_poll_controller = i40e_netpoll,
8972#endif
John Fastabende4c67342016-02-16 21:16:15 -08008973 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07008974#ifdef I40E_FCOE
8975 .ndo_fcoe_enable = i40e_fcoe_enable,
8976 .ndo_fcoe_disable = i40e_fcoe_disable,
8977#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008978 .ndo_set_features = i40e_set_features,
8979 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
8980 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008981 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008982 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00008983 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00008984 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Singhai, Anjali6a899022015-12-14 12:21:18 -08008985#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008986 .ndo_add_vxlan_port = i40e_add_vxlan_port,
8987 .ndo_del_vxlan_port = i40e_del_vxlan_port,
8988#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008989#if IS_ENABLED(CONFIG_GENEVE)
8990 .ndo_add_geneve_port = i40e_add_geneve_port,
8991 .ndo_del_geneve_port = i40e_del_geneve_port,
8992#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008993 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008994 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07008995 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00008996 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
8997 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008998};
8999
9000/**
9001 * i40e_config_netdev - Setup the netdev flags
9002 * @vsi: the VSI being configured
9003 *
9004 * Returns 0 on success, negative value on failure
9005 **/
9006static int i40e_config_netdev(struct i40e_vsi *vsi)
9007{
Greg Rose1a103702013-11-28 06:42:39 +00009008 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009009 struct i40e_pf *pf = vsi->back;
9010 struct i40e_hw *hw = &pf->hw;
9011 struct i40e_netdev_priv *np;
9012 struct net_device *netdev;
9013 u8 mac_addr[ETH_ALEN];
9014 int etherdev_size;
9015
9016 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009017 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009018 if (!netdev)
9019 return -ENOMEM;
9020
9021 vsi->netdev = netdev;
9022 np = netdev_priv(netdev);
9023 np->vsi = vsi;
9024
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009025 netdev->hw_enc_features |= NETIF_F_IP_CSUM |
9026 NETIF_F_GSO_UDP_TUNNEL |
9027 NETIF_F_GSO_GRE |
9028 NETIF_F_TSO |
9029 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009030
9031 netdev->features = NETIF_F_SG |
9032 NETIF_F_IP_CSUM |
Tom Herbert53692b12015-12-14 11:19:41 -08009033 NETIF_F_SCTP_CRC |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009034 NETIF_F_HIGHDMA |
9035 NETIF_F_GSO_UDP_TUNNEL |
Jesse Brandeburgfec31ff2015-10-01 18:25:42 -07009036 NETIF_F_GSO_GRE |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009037 NETIF_F_HW_VLAN_CTAG_TX |
9038 NETIF_F_HW_VLAN_CTAG_RX |
9039 NETIF_F_HW_VLAN_CTAG_FILTER |
9040 NETIF_F_IPV6_CSUM |
9041 NETIF_F_TSO |
Jesse Brandeburg059dab62014-04-01 09:07:20 +00009042 NETIF_F_TSO_ECN |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009043 NETIF_F_TSO6 |
9044 NETIF_F_RXCSUM |
9045 NETIF_F_RXHASH |
9046 0;
9047
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009048 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
9049 netdev->features |= NETIF_F_NTUPLE;
9050
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009051 /* copy netdev features into list of user selectable features */
9052 netdev->hw_features |= netdev->features;
9053
9054 if (vsi->type == I40E_VSI_MAIN) {
9055 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009056 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009057 /* The following steps are necessary to prevent reception
9058 * of tagged packets - some older NVM configurations load a
9059 * default a MAC-VLAN filter that accepts any tagged packet
9060 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00009061 */
Kiran Patil21659032015-09-30 14:09:03 -04009062 if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
9063 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009064 i40e_add_filter(vsi, mac_addr,
9065 I40E_VLAN_ANY, false, true);
Kiran Patil21659032015-09-30 14:09:03 -04009066 spin_unlock_bh(&vsi->mac_filter_list_lock);
9067 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009068 } else {
9069 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9070 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9071 pf->vsi[pf->lan_vsi]->netdev->name);
9072 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009073
9074 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009075 i40e_add_filter(vsi, mac_addr, 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 }
Kiran Patil21659032015-09-30 14:09:03 -04009078
9079 spin_lock_bh(&vsi->mac_filter_list_lock);
Greg Rose1a103702013-11-28 06:42:39 +00009080 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009081 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009082
Greg Rose9a173902014-05-22 06:32:02 +00009083 ether_addr_copy(netdev->dev_addr, mac_addr);
9084 ether_addr_copy(netdev->perm_addr, mac_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009085 /* vlan gets same features (except vlan offload)
9086 * after any tweaks for specific VSI types
9087 */
9088 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
9089 NETIF_F_HW_VLAN_CTAG_RX |
9090 NETIF_F_HW_VLAN_CTAG_FILTER);
9091 netdev->priv_flags |= IFF_UNICAST_FLT;
9092 netdev->priv_flags |= IFF_SUPP_NOFCS;
9093 /* Setup netdev TC information */
9094 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9095
9096 netdev->netdev_ops = &i40e_netdev_ops;
9097 netdev->watchdog_timeo = 5 * HZ;
9098 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009099#ifdef I40E_FCOE
9100 i40e_fcoe_config_netdev(netdev, vsi);
9101#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009102
9103 return 0;
9104}
9105
9106/**
9107 * i40e_vsi_delete - Delete a VSI from the switch
9108 * @vsi: the VSI being removed
9109 *
9110 * Returns 0 on success, negative value on failure
9111 **/
9112static void i40e_vsi_delete(struct i40e_vsi *vsi)
9113{
9114 /* remove default VSI is not allowed */
9115 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9116 return;
9117
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009118 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009119}
9120
9121/**
Neerav Parikh51616012015-02-06 08:52:14 +00009122 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9123 * @vsi: the VSI being queried
9124 *
9125 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9126 **/
9127int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9128{
9129 struct i40e_veb *veb;
9130 struct i40e_pf *pf = vsi->back;
9131
9132 /* Uplink is not a bridge so default to VEB */
9133 if (vsi->veb_idx == I40E_NO_VEB)
9134 return 1;
9135
9136 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009137 if (!veb) {
9138 dev_info(&pf->pdev->dev,
9139 "There is no veb associated with the bridge\n");
9140 return -ENOENT;
9141 }
Neerav Parikh51616012015-02-06 08:52:14 +00009142
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009143 /* Uplink is a bridge in VEPA mode */
9144 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9145 return 0;
9146 } else {
9147 /* Uplink is a bridge in VEB mode */
9148 return 1;
9149 }
9150
9151 /* VEPA is now default bridge, so return 0 */
9152 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009153}
9154
9155/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009156 * i40e_add_vsi - Add a VSI to the switch
9157 * @vsi: the VSI being configured
9158 *
9159 * This initializes a VSI context depending on the VSI type to be added and
9160 * passes it down to the add_vsi aq command.
9161 **/
9162static int i40e_add_vsi(struct i40e_vsi *vsi)
9163{
9164 int ret = -ENODEV;
Kiran Patil21659032015-09-30 14:09:03 -04009165 u8 laa_macaddr[ETH_ALEN];
9166 bool found_laa_mac_filter = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009167 struct i40e_pf *pf = vsi->back;
9168 struct i40e_hw *hw = &pf->hw;
9169 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009170 struct i40e_mac_filter *f, *ftmp;
9171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009172 u8 enabled_tc = 0x1; /* TC0 enabled */
9173 int f_count = 0;
9174
9175 memset(&ctxt, 0, sizeof(ctxt));
9176 switch (vsi->type) {
9177 case I40E_VSI_MAIN:
9178 /* The PF's main VSI is already setup as part of the
9179 * device initialization, so we'll not bother with
9180 * the add_vsi call, but we will retrieve the current
9181 * VSI context.
9182 */
9183 ctxt.seid = pf->main_vsi_seid;
9184 ctxt.pf_num = pf->hw.pf_id;
9185 ctxt.vf_num = 0;
9186 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9187 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9188 if (ret) {
9189 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009190 "couldn't get PF vsi config, err %s aq_err %s\n",
9191 i40e_stat_str(&pf->hw, ret),
9192 i40e_aq_str(&pf->hw,
9193 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009194 return -ENOENT;
9195 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009196 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009197 vsi->info.valid_sections = 0;
9198
9199 vsi->seid = ctxt.seid;
9200 vsi->id = ctxt.vsi_number;
9201
9202 enabled_tc = i40e_pf_get_tc_map(pf);
9203
9204 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009205 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9206 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009207 memset(&ctxt, 0, sizeof(ctxt));
9208 ctxt.seid = pf->main_vsi_seid;
9209 ctxt.pf_num = pf->hw.pf_id;
9210 ctxt.vf_num = 0;
9211 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9212 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9213 if (ret) {
9214 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009215 "update vsi failed, err %s aq_err %s\n",
9216 i40e_stat_str(&pf->hw, ret),
9217 i40e_aq_str(&pf->hw,
9218 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009219 ret = -ENOENT;
9220 goto err;
9221 }
9222 /* update the local VSI info queue map */
9223 i40e_vsi_update_queue_map(vsi, &ctxt);
9224 vsi->info.valid_sections = 0;
9225 } else {
9226 /* Default/Main VSI is only enabled for TC0
9227 * reconfigure it to enable all TCs that are
9228 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009229 * For MFP case the iSCSI PF would use this
9230 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009231 */
9232 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9233 if (ret) {
9234 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009235 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9236 enabled_tc,
9237 i40e_stat_str(&pf->hw, ret),
9238 i40e_aq_str(&pf->hw,
9239 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009240 ret = -ENOENT;
9241 }
9242 }
9243 break;
9244
9245 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009246 ctxt.pf_num = hw->pf_id;
9247 ctxt.vf_num = 0;
9248 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009249 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009250 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009251 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9252 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009253 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009254 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009255 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009256 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009257 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009258 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009259 break;
9260
9261 case I40E_VSI_VMDQ2:
9262 ctxt.pf_num = hw->pf_id;
9263 ctxt.vf_num = 0;
9264 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009265 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009266 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9267
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009268 /* This VSI is connected to VEB so the switch_id
9269 * should be set to zero by default.
9270 */
Neerav Parikh51616012015-02-06 08:52:14 +00009271 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9272 ctxt.info.valid_sections |=
9273 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9274 ctxt.info.switch_id =
9275 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9276 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277
9278 /* Setup the VSI tx/rx queue map for TC0 only for now */
9279 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9280 break;
9281
9282 case I40E_VSI_SRIOV:
9283 ctxt.pf_num = hw->pf_id;
9284 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9285 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009286 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009287 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9288
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009289 /* This VSI is connected to VEB so the switch_id
9290 * should be set to zero by default.
9291 */
Neerav Parikh51616012015-02-06 08:52:14 +00009292 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9293 ctxt.info.valid_sections |=
9294 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9295 ctxt.info.switch_id =
9296 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9297 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009298
9299 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9300 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009301 if (pf->vf[vsi->vf_id].spoofchk) {
9302 ctxt.info.valid_sections |=
9303 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9304 ctxt.info.sec_flags |=
9305 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9306 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9307 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009308 /* Setup the VSI tx/rx queue map for TC0 only for now */
9309 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9310 break;
9311
Vasu Dev38e00432014-08-01 13:27:03 -07009312#ifdef I40E_FCOE
9313 case I40E_VSI_FCOE:
9314 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9315 if (ret) {
9316 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9317 return ret;
9318 }
9319 break;
9320
9321#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009322 default:
9323 return -ENODEV;
9324 }
9325
9326 if (vsi->type != I40E_VSI_MAIN) {
9327 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9328 if (ret) {
9329 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009330 "add vsi failed, err %s aq_err %s\n",
9331 i40e_stat_str(&pf->hw, ret),
9332 i40e_aq_str(&pf->hw,
9333 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009334 ret = -ENOENT;
9335 goto err;
9336 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009337 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009338 vsi->info.valid_sections = 0;
9339 vsi->seid = ctxt.seid;
9340 vsi->id = ctxt.vsi_number;
9341 }
9342
Kiran Patil21659032015-09-30 14:09:03 -04009343 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009344 /* If macvlan filters already exist, force them to get loaded */
9345 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
9346 f->changed = true;
9347 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009348
Kiran Patil21659032015-09-30 14:09:03 -04009349 /* Expected to have only one MAC filter entry for LAA in list */
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009350 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Kiran Patil21659032015-09-30 14:09:03 -04009351 ether_addr_copy(laa_macaddr, f->macaddr);
9352 found_laa_mac_filter = true;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009353 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009354 }
Kiran Patil21659032015-09-30 14:09:03 -04009355 spin_unlock_bh(&vsi->mac_filter_list_lock);
9356
9357 if (found_laa_mac_filter) {
9358 struct i40e_aqc_remove_macvlan_element_data element;
9359
9360 memset(&element, 0, sizeof(element));
9361 ether_addr_copy(element.mac_addr, laa_macaddr);
9362 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
9363 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
9364 &element, 1, NULL);
9365 if (ret) {
9366 /* some older FW has a different default */
9367 element.flags |=
9368 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
9369 i40e_aq_remove_macvlan(hw, vsi->seid,
9370 &element, 1, NULL);
9371 }
9372
9373 i40e_aq_mac_address_write(hw,
9374 I40E_AQC_WRITE_TYPE_LAA_WOL,
9375 laa_macaddr, NULL);
9376 }
9377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009378 if (f_count) {
9379 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9380 pf->flags |= I40E_FLAG_FILTER_SYNC;
9381 }
9382
9383 /* Update VSI BW information */
9384 ret = i40e_vsi_get_bw_info(vsi);
9385 if (ret) {
9386 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009387 "couldn't get vsi bw info, err %s aq_err %s\n",
9388 i40e_stat_str(&pf->hw, ret),
9389 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009390 /* VSI is already added so not tearing that up */
9391 ret = 0;
9392 }
9393
9394err:
9395 return ret;
9396}
9397
9398/**
9399 * i40e_vsi_release - Delete a VSI and free its resources
9400 * @vsi: the VSI being removed
9401 *
9402 * Returns 0 on success or < 0 on error
9403 **/
9404int i40e_vsi_release(struct i40e_vsi *vsi)
9405{
9406 struct i40e_mac_filter *f, *ftmp;
9407 struct i40e_veb *veb = NULL;
9408 struct i40e_pf *pf;
9409 u16 uplink_seid;
9410 int i, n;
9411
9412 pf = vsi->back;
9413
9414 /* release of a VEB-owner or last VSI is not allowed */
9415 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9416 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9417 vsi->seid, vsi->uplink_seid);
9418 return -ENODEV;
9419 }
9420 if (vsi == pf->vsi[pf->lan_vsi] &&
9421 !test_bit(__I40E_DOWN, &pf->state)) {
9422 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9423 return -ENODEV;
9424 }
9425
9426 uplink_seid = vsi->uplink_seid;
9427 if (vsi->type != I40E_VSI_SRIOV) {
9428 if (vsi->netdev_registered) {
9429 vsi->netdev_registered = false;
9430 if (vsi->netdev) {
9431 /* results in a call to i40e_close() */
9432 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009433 }
9434 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009435 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436 }
9437 i40e_vsi_disable_irq(vsi);
9438 }
9439
Kiran Patil21659032015-09-30 14:09:03 -04009440 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009441 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9442 i40e_del_filter(vsi, f->macaddr, f->vlan,
9443 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009444 spin_unlock_bh(&vsi->mac_filter_list_lock);
9445
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009446 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447
9448 i40e_vsi_delete(vsi);
9449 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009450 if (vsi->netdev) {
9451 free_netdev(vsi->netdev);
9452 vsi->netdev = NULL;
9453 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009454 i40e_vsi_clear_rings(vsi);
9455 i40e_vsi_clear(vsi);
9456
9457 /* If this was the last thing on the VEB, except for the
9458 * controlling VSI, remove the VEB, which puts the controlling
9459 * VSI onto the next level down in the switch.
9460 *
9461 * Well, okay, there's one more exception here: don't remove
9462 * the orphan VEBs yet. We'll wait for an explicit remove request
9463 * from up the network stack.
9464 */
Mitch Williams505682c2014-05-20 08:01:37 +00009465 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009466 if (pf->vsi[i] &&
9467 pf->vsi[i]->uplink_seid == uplink_seid &&
9468 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9469 n++; /* count the VSIs */
9470 }
9471 }
9472 for (i = 0; i < I40E_MAX_VEB; i++) {
9473 if (!pf->veb[i])
9474 continue;
9475 if (pf->veb[i]->uplink_seid == uplink_seid)
9476 n++; /* count the VEBs */
9477 if (pf->veb[i]->seid == uplink_seid)
9478 veb = pf->veb[i];
9479 }
9480 if (n == 0 && veb && veb->uplink_seid != 0)
9481 i40e_veb_release(veb);
9482
9483 return 0;
9484}
9485
9486/**
9487 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9488 * @vsi: ptr to the VSI
9489 *
9490 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9491 * corresponding SW VSI structure and initializes num_queue_pairs for the
9492 * newly allocated VSI.
9493 *
9494 * Returns 0 on success or negative on failure
9495 **/
9496static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9497{
9498 int ret = -ENOENT;
9499 struct i40e_pf *pf = vsi->back;
9500
Alexander Duyck493fb302013-09-28 07:01:44 +00009501 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009502 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9503 vsi->seid);
9504 return -EEXIST;
9505 }
9506
9507 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009508 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009509 vsi->seid, vsi->base_vector);
9510 return -EEXIST;
9511 }
9512
Greg Rose90e04072014-03-06 08:59:57 +00009513 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009514 if (ret) {
9515 dev_info(&pf->pdev->dev,
9516 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9517 vsi->num_q_vectors, vsi->seid, ret);
9518 vsi->num_q_vectors = 0;
9519 goto vector_setup_out;
9520 }
9521
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009522 /* In Legacy mode, we do not have to get any other vector since we
9523 * piggyback on the misc/ICR0 for queue interrupts.
9524 */
9525 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9526 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009527 if (vsi->num_q_vectors)
9528 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9529 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530 if (vsi->base_vector < 0) {
9531 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009532 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9533 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009534 i40e_vsi_free_q_vectors(vsi);
9535 ret = -ENOENT;
9536 goto vector_setup_out;
9537 }
9538
9539vector_setup_out:
9540 return ret;
9541}
9542
9543/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009544 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9545 * @vsi: pointer to the vsi.
9546 *
9547 * This re-allocates a vsi's queue resources.
9548 *
9549 * Returns pointer to the successfully allocated and configured VSI sw struct
9550 * on success, otherwise returns NULL on failure.
9551 **/
9552static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9553{
9554 struct i40e_pf *pf = vsi->back;
9555 u8 enabled_tc;
9556 int ret;
9557
9558 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9559 i40e_vsi_clear_rings(vsi);
9560
9561 i40e_vsi_free_arrays(vsi, false);
9562 i40e_set_num_rings_in_vsi(vsi);
9563 ret = i40e_vsi_alloc_arrays(vsi, false);
9564 if (ret)
9565 goto err_vsi;
9566
9567 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9568 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009569 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009570 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009571 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009572 goto err_vsi;
9573 }
9574 vsi->base_queue = ret;
9575
9576 /* Update the FW view of the VSI. Force a reset of TC and queue
9577 * layout configurations.
9578 */
9579 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9580 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9581 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9582 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9583
9584 /* assign it some queues */
9585 ret = i40e_alloc_rings(vsi);
9586 if (ret)
9587 goto err_rings;
9588
9589 /* map all of the rings to the q_vectors */
9590 i40e_vsi_map_rings_to_vectors(vsi);
9591 return vsi;
9592
9593err_rings:
9594 i40e_vsi_free_q_vectors(vsi);
9595 if (vsi->netdev_registered) {
9596 vsi->netdev_registered = false;
9597 unregister_netdev(vsi->netdev);
9598 free_netdev(vsi->netdev);
9599 vsi->netdev = NULL;
9600 }
9601 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9602err_vsi:
9603 i40e_vsi_clear(vsi);
9604 return NULL;
9605}
9606
9607/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009608 * i40e_macaddr_init - explicitly write the mac address filters.
9609 *
9610 * @vsi: pointer to the vsi.
9611 * @macaddr: the MAC address
9612 *
9613 * This is needed when the macaddr has been obtained by other
9614 * means than the default, e.g., from Open Firmware or IDPROM.
9615 * Returns 0 on success, negative on failure
9616 **/
9617static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
9618{
9619 int ret;
9620 struct i40e_aqc_add_macvlan_element_data element;
9621
9622 ret = i40e_aq_mac_address_write(&vsi->back->hw,
9623 I40E_AQC_WRITE_TYPE_LAA_WOL,
9624 macaddr, NULL);
9625 if (ret) {
9626 dev_info(&vsi->back->pdev->dev,
9627 "Addr change for VSI failed: %d\n", ret);
9628 return -EADDRNOTAVAIL;
9629 }
9630
9631 memset(&element, 0, sizeof(element));
9632 ether_addr_copy(element.mac_addr, macaddr);
9633 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
9634 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
9635 if (ret) {
9636 dev_info(&vsi->back->pdev->dev,
9637 "add filter failed err %s aq_err %s\n",
9638 i40e_stat_str(&vsi->back->hw, ret),
9639 i40e_aq_str(&vsi->back->hw,
9640 vsi->back->hw.aq.asq_last_status));
9641 }
9642 return ret;
9643}
9644
9645/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009646 * i40e_vsi_setup - Set up a VSI by a given type
9647 * @pf: board private structure
9648 * @type: VSI type
9649 * @uplink_seid: the switch element to link to
9650 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9651 *
9652 * This allocates the sw VSI structure and its queue resources, then add a VSI
9653 * to the identified VEB.
9654 *
9655 * Returns pointer to the successfully allocated and configure VSI sw struct on
9656 * success, otherwise returns NULL on failure.
9657 **/
9658struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9659 u16 uplink_seid, u32 param1)
9660{
9661 struct i40e_vsi *vsi = NULL;
9662 struct i40e_veb *veb = NULL;
9663 int ret, i;
9664 int v_idx;
9665
9666 /* The requested uplink_seid must be either
9667 * - the PF's port seid
9668 * no VEB is needed because this is the PF
9669 * or this is a Flow Director special case VSI
9670 * - seid of an existing VEB
9671 * - seid of a VSI that owns an existing VEB
9672 * - seid of a VSI that doesn't own a VEB
9673 * a new VEB is created and the VSI becomes the owner
9674 * - seid of the PF VSI, which is what creates the first VEB
9675 * this is a special case of the previous
9676 *
9677 * Find which uplink_seid we were given and create a new VEB if needed
9678 */
9679 for (i = 0; i < I40E_MAX_VEB; i++) {
9680 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9681 veb = pf->veb[i];
9682 break;
9683 }
9684 }
9685
9686 if (!veb && uplink_seid != pf->mac_seid) {
9687
Mitch Williams505682c2014-05-20 08:01:37 +00009688 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009689 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9690 vsi = pf->vsi[i];
9691 break;
9692 }
9693 }
9694 if (!vsi) {
9695 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9696 uplink_seid);
9697 return NULL;
9698 }
9699
9700 if (vsi->uplink_seid == pf->mac_seid)
9701 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9702 vsi->tc_config.enabled_tc);
9703 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9704 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9705 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009706 if (veb) {
9707 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9708 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009709 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009710 return NULL;
9711 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009712 /* We come up by default in VEPA mode if SRIOV is not
9713 * already enabled, in which case we can't force VEPA
9714 * mode.
9715 */
9716 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9717 veb->bridge_mode = BRIDGE_MODE_VEPA;
9718 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9719 }
Neerav Parikh51616012015-02-06 08:52:14 +00009720 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009721 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009722 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9723 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9724 veb = pf->veb[i];
9725 }
9726 if (!veb) {
9727 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9728 return NULL;
9729 }
9730
9731 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9732 uplink_seid = veb->seid;
9733 }
9734
9735 /* get vsi sw struct */
9736 v_idx = i40e_vsi_mem_alloc(pf, type);
9737 if (v_idx < 0)
9738 goto err_alloc;
9739 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009740 if (!vsi)
9741 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009742 vsi->type = type;
9743 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9744
9745 if (type == I40E_VSI_MAIN)
9746 pf->lan_vsi = v_idx;
9747 else if (type == I40E_VSI_SRIOV)
9748 vsi->vf_id = param1;
9749 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009750 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9751 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009752 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009753 dev_info(&pf->pdev->dev,
9754 "failed to get tracking for %d queues for VSI %d err=%d\n",
9755 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009756 goto err_vsi;
9757 }
9758 vsi->base_queue = ret;
9759
9760 /* get a VSI from the hardware */
9761 vsi->uplink_seid = uplink_seid;
9762 ret = i40e_add_vsi(vsi);
9763 if (ret)
9764 goto err_vsi;
9765
9766 switch (vsi->type) {
9767 /* setup the netdev if needed */
9768 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009769 /* Apply relevant filters if a platform-specific mac
9770 * address was selected.
9771 */
9772 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9773 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9774 if (ret) {
9775 dev_warn(&pf->pdev->dev,
9776 "could not set up macaddr; err %d\n",
9777 ret);
9778 }
9779 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009780 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009781 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009782 ret = i40e_config_netdev(vsi);
9783 if (ret)
9784 goto err_netdev;
9785 ret = register_netdev(vsi->netdev);
9786 if (ret)
9787 goto err_netdev;
9788 vsi->netdev_registered = true;
9789 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009790#ifdef CONFIG_I40E_DCB
9791 /* Setup DCB netlink interface */
9792 i40e_dcbnl_setup(vsi);
9793#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009794 /* fall through */
9795
9796 case I40E_VSI_FDIR:
9797 /* set up vectors and rings if needed */
9798 ret = i40e_vsi_setup_vectors(vsi);
9799 if (ret)
9800 goto err_msix;
9801
9802 ret = i40e_alloc_rings(vsi);
9803 if (ret)
9804 goto err_rings;
9805
9806 /* map all of the rings to the q_vectors */
9807 i40e_vsi_map_rings_to_vectors(vsi);
9808
9809 i40e_vsi_reset_stats(vsi);
9810 break;
9811
9812 default:
9813 /* no netdev or rings for the other VSI types */
9814 break;
9815 }
9816
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04009817 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9818 (vsi->type == I40E_VSI_VMDQ2)) {
9819 ret = i40e_vsi_config_rss(vsi);
9820 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009821 return vsi;
9822
9823err_rings:
9824 i40e_vsi_free_q_vectors(vsi);
9825err_msix:
9826 if (vsi->netdev_registered) {
9827 vsi->netdev_registered = false;
9828 unregister_netdev(vsi->netdev);
9829 free_netdev(vsi->netdev);
9830 vsi->netdev = NULL;
9831 }
9832err_netdev:
9833 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9834err_vsi:
9835 i40e_vsi_clear(vsi);
9836err_alloc:
9837 return NULL;
9838}
9839
9840/**
9841 * i40e_veb_get_bw_info - Query VEB BW information
9842 * @veb: the veb to query
9843 *
9844 * Query the Tx scheduler BW configuration data for given VEB
9845 **/
9846static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9847{
9848 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9849 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9850 struct i40e_pf *pf = veb->pf;
9851 struct i40e_hw *hw = &pf->hw;
9852 u32 tc_bw_max;
9853 int ret = 0;
9854 int i;
9855
9856 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9857 &bw_data, NULL);
9858 if (ret) {
9859 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009860 "query veb bw config failed, err %s aq_err %s\n",
9861 i40e_stat_str(&pf->hw, ret),
9862 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009863 goto out;
9864 }
9865
9866 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9867 &ets_data, NULL);
9868 if (ret) {
9869 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009870 "query veb bw ets config failed, err %s aq_err %s\n",
9871 i40e_stat_str(&pf->hw, ret),
9872 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009873 goto out;
9874 }
9875
9876 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9877 veb->bw_max_quanta = ets_data.tc_bw_max;
9878 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009879 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009880 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9881 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9882 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9883 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9884 veb->bw_tc_limit_credits[i] =
9885 le16_to_cpu(bw_data.tc_bw_limits[i]);
9886 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9887 }
9888
9889out:
9890 return ret;
9891}
9892
9893/**
9894 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9895 * @pf: board private structure
9896 *
9897 * On error: returns error code (negative)
9898 * On success: returns vsi index in PF (positive)
9899 **/
9900static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9901{
9902 int ret = -ENOENT;
9903 struct i40e_veb *veb;
9904 int i;
9905
9906 /* Need to protect the allocation of switch elements at the PF level */
9907 mutex_lock(&pf->switch_mutex);
9908
9909 /* VEB list may be fragmented if VEB creation/destruction has
9910 * been happening. We can afford to do a quick scan to look
9911 * for any free slots in the list.
9912 *
9913 * find next empty veb slot, looping back around if necessary
9914 */
9915 i = 0;
9916 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9917 i++;
9918 if (i >= I40E_MAX_VEB) {
9919 ret = -ENOMEM;
9920 goto err_alloc_veb; /* out of VEB slots! */
9921 }
9922
9923 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9924 if (!veb) {
9925 ret = -ENOMEM;
9926 goto err_alloc_veb;
9927 }
9928 veb->pf = pf;
9929 veb->idx = i;
9930 veb->enabled_tc = 1;
9931
9932 pf->veb[i] = veb;
9933 ret = i;
9934err_alloc_veb:
9935 mutex_unlock(&pf->switch_mutex);
9936 return ret;
9937}
9938
9939/**
9940 * i40e_switch_branch_release - Delete a branch of the switch tree
9941 * @branch: where to start deleting
9942 *
9943 * This uses recursion to find the tips of the branch to be
9944 * removed, deleting until we get back to and can delete this VEB.
9945 **/
9946static void i40e_switch_branch_release(struct i40e_veb *branch)
9947{
9948 struct i40e_pf *pf = branch->pf;
9949 u16 branch_seid = branch->seid;
9950 u16 veb_idx = branch->idx;
9951 int i;
9952
9953 /* release any VEBs on this VEB - RECURSION */
9954 for (i = 0; i < I40E_MAX_VEB; i++) {
9955 if (!pf->veb[i])
9956 continue;
9957 if (pf->veb[i]->uplink_seid == branch->seid)
9958 i40e_switch_branch_release(pf->veb[i]);
9959 }
9960
9961 /* Release the VSIs on this VEB, but not the owner VSI.
9962 *
9963 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
9964 * the VEB itself, so don't use (*branch) after this loop.
9965 */
Mitch Williams505682c2014-05-20 08:01:37 +00009966 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009967 if (!pf->vsi[i])
9968 continue;
9969 if (pf->vsi[i]->uplink_seid == branch_seid &&
9970 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9971 i40e_vsi_release(pf->vsi[i]);
9972 }
9973 }
9974
9975 /* There's one corner case where the VEB might not have been
9976 * removed, so double check it here and remove it if needed.
9977 * This case happens if the veb was created from the debugfs
9978 * commands and no VSIs were added to it.
9979 */
9980 if (pf->veb[veb_idx])
9981 i40e_veb_release(pf->veb[veb_idx]);
9982}
9983
9984/**
9985 * i40e_veb_clear - remove veb struct
9986 * @veb: the veb to remove
9987 **/
9988static void i40e_veb_clear(struct i40e_veb *veb)
9989{
9990 if (!veb)
9991 return;
9992
9993 if (veb->pf) {
9994 struct i40e_pf *pf = veb->pf;
9995
9996 mutex_lock(&pf->switch_mutex);
9997 if (pf->veb[veb->idx] == veb)
9998 pf->veb[veb->idx] = NULL;
9999 mutex_unlock(&pf->switch_mutex);
10000 }
10001
10002 kfree(veb);
10003}
10004
10005/**
10006 * i40e_veb_release - Delete a VEB and free its resources
10007 * @veb: the VEB being removed
10008 **/
10009void i40e_veb_release(struct i40e_veb *veb)
10010{
10011 struct i40e_vsi *vsi = NULL;
10012 struct i40e_pf *pf;
10013 int i, n = 0;
10014
10015 pf = veb->pf;
10016
10017 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010018 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010019 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10020 n++;
10021 vsi = pf->vsi[i];
10022 }
10023 }
10024 if (n != 1) {
10025 dev_info(&pf->pdev->dev,
10026 "can't remove VEB %d with %d VSIs left\n",
10027 veb->seid, n);
10028 return;
10029 }
10030
10031 /* move the remaining VSI to uplink veb */
10032 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10033 if (veb->uplink_seid) {
10034 vsi->uplink_seid = veb->uplink_seid;
10035 if (veb->uplink_seid == pf->mac_seid)
10036 vsi->veb_idx = I40E_NO_VEB;
10037 else
10038 vsi->veb_idx = veb->veb_idx;
10039 } else {
10040 /* floating VEB */
10041 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10042 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10043 }
10044
10045 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10046 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010047}
10048
10049/**
10050 * i40e_add_veb - create the VEB in the switch
10051 * @veb: the VEB to be instantiated
10052 * @vsi: the controlling VSI
10053 **/
10054static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10055{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010056 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -040010057 bool is_default = veb->pf->cur_promisc;
Kevin Scotte1c51b952013-11-20 10:02:51 +000010058 bool is_cloud = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010059 int ret;
10060
10061 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010062 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +000010063 veb->enabled_tc, is_default,
10064 is_cloud, &veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010065 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010066 dev_info(&pf->pdev->dev,
10067 "couldn't add VEB, err %s aq_err %s\n",
10068 i40e_stat_str(&pf->hw, ret),
10069 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010070 return -EPERM;
10071 }
10072
10073 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010074 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010075 &veb->stats_idx, NULL, NULL, NULL);
10076 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010077 dev_info(&pf->pdev->dev,
10078 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10079 i40e_stat_str(&pf->hw, ret),
10080 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010081 return -EPERM;
10082 }
10083 ret = i40e_veb_get_bw_info(veb);
10084 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010085 dev_info(&pf->pdev->dev,
10086 "couldn't get VEB bw info, err %s aq_err %s\n",
10087 i40e_stat_str(&pf->hw, ret),
10088 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10089 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010090 return -ENOENT;
10091 }
10092
10093 vsi->uplink_seid = veb->seid;
10094 vsi->veb_idx = veb->idx;
10095 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10096
10097 return 0;
10098}
10099
10100/**
10101 * i40e_veb_setup - Set up a VEB
10102 * @pf: board private structure
10103 * @flags: VEB setup flags
10104 * @uplink_seid: the switch element to link to
10105 * @vsi_seid: the initial VSI seid
10106 * @enabled_tc: Enabled TC bit-map
10107 *
10108 * This allocates the sw VEB structure and links it into the switch
10109 * It is possible and legal for this to be a duplicate of an already
10110 * existing VEB. It is also possible for both uplink and vsi seids
10111 * to be zero, in order to create a floating VEB.
10112 *
10113 * Returns pointer to the successfully allocated VEB sw struct on
10114 * success, otherwise returns NULL on failure.
10115 **/
10116struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10117 u16 uplink_seid, u16 vsi_seid,
10118 u8 enabled_tc)
10119{
10120 struct i40e_veb *veb, *uplink_veb = NULL;
10121 int vsi_idx, veb_idx;
10122 int ret;
10123
10124 /* if one seid is 0, the other must be 0 to create a floating relay */
10125 if ((uplink_seid == 0 || vsi_seid == 0) &&
10126 (uplink_seid + vsi_seid != 0)) {
10127 dev_info(&pf->pdev->dev,
10128 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10129 uplink_seid, vsi_seid);
10130 return NULL;
10131 }
10132
10133 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010134 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010135 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10136 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010137 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010138 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10139 vsi_seid);
10140 return NULL;
10141 }
10142
10143 if (uplink_seid && uplink_seid != pf->mac_seid) {
10144 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10145 if (pf->veb[veb_idx] &&
10146 pf->veb[veb_idx]->seid == uplink_seid) {
10147 uplink_veb = pf->veb[veb_idx];
10148 break;
10149 }
10150 }
10151 if (!uplink_veb) {
10152 dev_info(&pf->pdev->dev,
10153 "uplink seid %d not found\n", uplink_seid);
10154 return NULL;
10155 }
10156 }
10157
10158 /* get veb sw struct */
10159 veb_idx = i40e_veb_mem_alloc(pf);
10160 if (veb_idx < 0)
10161 goto err_alloc;
10162 veb = pf->veb[veb_idx];
10163 veb->flags = flags;
10164 veb->uplink_seid = uplink_seid;
10165 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10166 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10167
10168 /* create the VEB in the switch */
10169 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10170 if (ret)
10171 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010172 if (vsi_idx == pf->lan_vsi)
10173 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010174
10175 return veb;
10176
10177err_veb:
10178 i40e_veb_clear(veb);
10179err_alloc:
10180 return NULL;
10181}
10182
10183/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010184 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010185 * @pf: board private structure
10186 * @ele: element we are building info from
10187 * @num_reported: total number of elements
10188 * @printconfig: should we print the contents
10189 *
10190 * helper function to assist in extracting a few useful SEID values.
10191 **/
10192static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10193 struct i40e_aqc_switch_config_element_resp *ele,
10194 u16 num_reported, bool printconfig)
10195{
10196 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10197 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10198 u8 element_type = ele->element_type;
10199 u16 seid = le16_to_cpu(ele->seid);
10200
10201 if (printconfig)
10202 dev_info(&pf->pdev->dev,
10203 "type=%d seid=%d uplink=%d downlink=%d\n",
10204 element_type, seid, uplink_seid, downlink_seid);
10205
10206 switch (element_type) {
10207 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10208 pf->mac_seid = seid;
10209 break;
10210 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10211 /* Main VEB? */
10212 if (uplink_seid != pf->mac_seid)
10213 break;
10214 if (pf->lan_veb == I40E_NO_VEB) {
10215 int v;
10216
10217 /* find existing or else empty VEB */
10218 for (v = 0; v < I40E_MAX_VEB; v++) {
10219 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10220 pf->lan_veb = v;
10221 break;
10222 }
10223 }
10224 if (pf->lan_veb == I40E_NO_VEB) {
10225 v = i40e_veb_mem_alloc(pf);
10226 if (v < 0)
10227 break;
10228 pf->lan_veb = v;
10229 }
10230 }
10231
10232 pf->veb[pf->lan_veb]->seid = seid;
10233 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10234 pf->veb[pf->lan_veb]->pf = pf;
10235 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10236 break;
10237 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10238 if (num_reported != 1)
10239 break;
10240 /* This is immediately after a reset so we can assume this is
10241 * the PF's VSI
10242 */
10243 pf->mac_seid = uplink_seid;
10244 pf->pf_seid = downlink_seid;
10245 pf->main_vsi_seid = seid;
10246 if (printconfig)
10247 dev_info(&pf->pdev->dev,
10248 "pf_seid=%d main_vsi_seid=%d\n",
10249 pf->pf_seid, pf->main_vsi_seid);
10250 break;
10251 case I40E_SWITCH_ELEMENT_TYPE_PF:
10252 case I40E_SWITCH_ELEMENT_TYPE_VF:
10253 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10254 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10255 case I40E_SWITCH_ELEMENT_TYPE_PE:
10256 case I40E_SWITCH_ELEMENT_TYPE_PA:
10257 /* ignore these for now */
10258 break;
10259 default:
10260 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10261 element_type, seid);
10262 break;
10263 }
10264}
10265
10266/**
10267 * i40e_fetch_switch_configuration - Get switch config from firmware
10268 * @pf: board private structure
10269 * @printconfig: should we print the contents
10270 *
10271 * Get the current switch configuration from the device and
10272 * extract a few useful SEID values.
10273 **/
10274int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10275{
10276 struct i40e_aqc_get_switch_config_resp *sw_config;
10277 u16 next_seid = 0;
10278 int ret = 0;
10279 u8 *aq_buf;
10280 int i;
10281
10282 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10283 if (!aq_buf)
10284 return -ENOMEM;
10285
10286 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10287 do {
10288 u16 num_reported, num_total;
10289
10290 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10291 I40E_AQ_LARGE_BUF,
10292 &next_seid, NULL);
10293 if (ret) {
10294 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010295 "get switch config failed err %s aq_err %s\n",
10296 i40e_stat_str(&pf->hw, ret),
10297 i40e_aq_str(&pf->hw,
10298 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010299 kfree(aq_buf);
10300 return -ENOENT;
10301 }
10302
10303 num_reported = le16_to_cpu(sw_config->header.num_reported);
10304 num_total = le16_to_cpu(sw_config->header.num_total);
10305
10306 if (printconfig)
10307 dev_info(&pf->pdev->dev,
10308 "header: %d reported %d total\n",
10309 num_reported, num_total);
10310
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010311 for (i = 0; i < num_reported; i++) {
10312 struct i40e_aqc_switch_config_element_resp *ele =
10313 &sw_config->element[i];
10314
10315 i40e_setup_pf_switch_element(pf, ele, num_reported,
10316 printconfig);
10317 }
10318 } while (next_seid != 0);
10319
10320 kfree(aq_buf);
10321 return ret;
10322}
10323
10324/**
10325 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10326 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010327 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010328 *
10329 * Returns 0 on success, negative value on failure
10330 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010331static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010332{
10333 int ret;
10334
10335 /* find out what's out there already */
10336 ret = i40e_fetch_switch_configuration(pf, false);
10337 if (ret) {
10338 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010339 "couldn't fetch switch config, err %s aq_err %s\n",
10340 i40e_stat_str(&pf->hw, ret),
10341 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010342 return ret;
10343 }
10344 i40e_pf_reset_stats(pf);
10345
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010346 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010347 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010348 struct i40e_vsi *vsi = NULL;
10349 u16 uplink_seid;
10350
10351 /* Set up the PF VSI associated with the PF's main VSI
10352 * that is already in the HW switch
10353 */
10354 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10355 uplink_seid = pf->veb[pf->lan_veb]->seid;
10356 else
10357 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010358 if (pf->lan_vsi == I40E_NO_VSI)
10359 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10360 else if (reinit)
10361 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010362 if (!vsi) {
10363 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10364 i40e_fdir_teardown(pf);
10365 return -EAGAIN;
10366 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010367 } else {
10368 /* force a reset of TC and queue layout configurations */
10369 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010370
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010371 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10372 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10373 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10374 }
10375 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10376
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010377 i40e_fdir_sb_setup(pf);
10378
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010379 /* Setup static PF queue filter control settings */
10380 ret = i40e_setup_pf_filter_control(pf);
10381 if (ret) {
10382 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10383 ret);
10384 /* Failure here should not stop continuing other steps */
10385 }
10386
10387 /* enable RSS in the HW, even for only one queue, as the stack can use
10388 * the hash
10389 */
10390 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010391 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010392
10393 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010394 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010395 i40e_link_event(pf);
10396
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010397 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010398 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10399 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010400
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010401 i40e_ptp_init(pf);
10402
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010403 return ret;
10404}
10405
10406/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010407 * i40e_determine_queue_usage - Work out queue distribution
10408 * @pf: board private structure
10409 **/
10410static void i40e_determine_queue_usage(struct i40e_pf *pf)
10411{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010412 int queues_left;
10413
10414 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010415#ifdef I40E_FCOE
10416 pf->num_fcoe_qps = 0;
10417#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010418
10419 /* Find the max queues to be put into basic use. We'll always be
10420 * using TC0, whether or not DCB is running, and TC0 will get the
10421 * big RSS set.
10422 */
10423 queues_left = pf->hw.func_caps.num_tx_qp;
10424
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010425 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010426 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010427 /* one qp for PF, no queues for anything else */
10428 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010429 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010430
10431 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010432 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010433#ifdef I40E_FCOE
10434 I40E_FLAG_FCOE_ENABLED |
10435#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010436 I40E_FLAG_FD_SB_ENABLED |
10437 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010438 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010439 I40E_FLAG_SRIOV_ENABLED |
10440 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010441 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10442 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010443 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010444 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010445 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010446 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010447 queues_left -= pf->num_lan_qps;
10448
10449 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010450#ifdef I40E_FCOE
10451 I40E_FLAG_FCOE_ENABLED |
10452#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010453 I40E_FLAG_FD_SB_ENABLED |
10454 I40E_FLAG_FD_ATR_ENABLED |
10455 I40E_FLAG_DCB_ENABLED |
10456 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010457 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010458 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010459 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010460 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010461 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010462 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10463 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010464 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10465 num_online_cpus());
10466 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10467 pf->hw.func_caps.num_tx_qp);
10468
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010469 queues_left -= pf->num_lan_qps;
10470 }
10471
Vasu Dev38e00432014-08-01 13:27:03 -070010472#ifdef I40E_FCOE
10473 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10474 if (I40E_DEFAULT_FCOE <= queues_left) {
10475 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10476 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10477 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10478 } else {
10479 pf->num_fcoe_qps = 0;
10480 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10481 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10482 }
10483
10484 queues_left -= pf->num_fcoe_qps;
10485 }
10486
10487#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010488 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10489 if (queues_left > 1) {
10490 queues_left -= 1; /* save 1 queue for FD */
10491 } else {
10492 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10493 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10494 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010495 }
10496
10497 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10498 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010499 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10500 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010501 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10502 }
10503
10504 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10505 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10506 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10507 (queues_left / pf->num_vmdq_qps));
10508 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10509 }
10510
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010511 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010512 dev_dbg(&pf->pdev->dev,
10513 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10514 pf->hw.func_caps.num_tx_qp,
10515 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010516 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10517 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10518 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010519#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010520 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010521#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010522}
10523
10524/**
10525 * i40e_setup_pf_filter_control - Setup PF static filter control
10526 * @pf: PF to be setup
10527 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010528 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010529 * settings. If PE/FCoE are enabled then it will also set the per PF
10530 * based filter sizes required for them. It also enables Flow director,
10531 * ethertype and macvlan type filter settings for the pf.
10532 *
10533 * Returns 0 on success, negative on failure
10534 **/
10535static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10536{
10537 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10538
10539 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10540
10541 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010542 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010543 settings->enable_fdir = true;
10544
10545 /* Ethtype and MACVLAN filters enabled for PF */
10546 settings->enable_ethtype = true;
10547 settings->enable_macvlan = true;
10548
10549 if (i40e_set_filter_control(&pf->hw, settings))
10550 return -ENOENT;
10551
10552 return 0;
10553}
10554
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010555#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010556#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010557static void i40e_print_features(struct i40e_pf *pf)
10558{
10559 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010560 char *buf;
10561 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010562
Joe Perches3b195842015-12-03 04:20:57 -080010563 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10564 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010565 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010566
Joe Perches3b195842015-12-03 04:20:57 -080010567 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010568#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010569 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010570#endif
Joe Perches3b195842015-12-03 04:20:57 -080010571 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d RX: %s",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010572 pf->hw.func_caps.num_vsis,
10573 pf->vsi[pf->lan_vsi]->num_queue_pairs,
10574 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010575
10576 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010577 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010578 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010579 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010580 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010581 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10582 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010583 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010584 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010585 i += snprintf(&buf[i], REMAIN(i), " DCB");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010586#if IS_ENABLED(CONFIG_VXLAN)
Joe Perches3b195842015-12-03 04:20:57 -080010587 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010588#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080010589#if IS_ENABLED(CONFIG_GENEVE)
10590 i += snprintf(&buf[i], REMAIN(i), " Geneve");
10591#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010592 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010593 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010594#ifdef I40E_FCOE
10595 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010596 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010597#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010598 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010599 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010600 else
Joe Perches3b195842015-12-03 04:20:57 -080010601 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010602
Joe Perches3b195842015-12-03 04:20:57 -080010603 dev_info(&pf->pdev->dev, "%s\n", buf);
10604 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010605 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010606}
10607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010608/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010609 * i40e_get_platform_mac_addr - get platform-specific MAC address
10610 *
10611 * @pdev: PCI device information struct
10612 * @pf: board private structure
10613 *
10614 * Look up the MAC address in Open Firmware on systems that support it,
10615 * and use IDPROM on SPARC if no OF address is found. On return, the
10616 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10617 * has been selected.
10618 **/
10619static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10620{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010621 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010622 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010623 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010624}
10625
10626/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010627 * i40e_probe - Device initialization routine
10628 * @pdev: PCI device information struct
10629 * @ent: entry in i40e_pci_tbl
10630 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010631 * i40e_probe initializes a PF identified by a pci_dev structure.
10632 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010633 * and a hardware reset occur.
10634 *
10635 * Returns 0 on success, negative on failure
10636 **/
10637static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10638{
Catherine Sullivane8278452015-02-06 08:52:08 +000010639 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010640 struct i40e_pf *pf;
10641 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010642 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010643 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010644 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010645 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010646 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010647 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010648 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010649
10650 err = pci_enable_device_mem(pdev);
10651 if (err)
10652 return err;
10653
10654 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010655 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010656 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010657 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10658 if (err) {
10659 dev_err(&pdev->dev,
10660 "DMA configuration failed: 0x%x\n", err);
10661 goto err_dma;
10662 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010663 }
10664
10665 /* set up pci connections */
10666 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
10667 IORESOURCE_MEM), i40e_driver_name);
10668 if (err) {
10669 dev_info(&pdev->dev,
10670 "pci_request_selected_regions failed %d\n", err);
10671 goto err_pci_reg;
10672 }
10673
10674 pci_enable_pcie_error_reporting(pdev);
10675 pci_set_master(pdev);
10676
10677 /* Now that we have a PCI connection, we need to do the
10678 * low level device setup. This is primarily setting up
10679 * the Admin Queue structures and then querying for the
10680 * device's current profile information.
10681 */
10682 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10683 if (!pf) {
10684 err = -ENOMEM;
10685 goto err_pf_alloc;
10686 }
10687 pf->next_vsi = 0;
10688 pf->pdev = pdev;
10689 set_bit(__I40E_DOWN, &pf->state);
10690
10691 hw = &pf->hw;
10692 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010693
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010694 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10695 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010696
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010697 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010698 if (!hw->hw_addr) {
10699 err = -EIO;
10700 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10701 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010702 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010703 goto err_ioremap;
10704 }
10705 hw->vendor_id = pdev->vendor;
10706 hw->device_id = pdev->device;
10707 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10708 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10709 hw->subsystem_device_id = pdev->subsystem_device;
10710 hw->bus.device = PCI_SLOT(pdev->devfn);
10711 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010712 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010713
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010714 if (debug != -1) {
10715 pf->msg_enable = pf->hw.debug_mask;
10716 pf->msg_enable = debug;
10717 }
10718
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010719 /* do a special CORER for clearing PXE mode once at init */
10720 if (hw->revision_id == 0 &&
10721 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10722 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10723 i40e_flush(hw);
10724 msleep(200);
10725 pf->corer_count++;
10726
10727 i40e_clear_pxe_mode(hw);
10728 }
10729
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010730 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010731 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010732 err = i40e_pf_reset(hw);
10733 if (err) {
10734 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10735 goto err_pf_reset;
10736 }
10737 pf->pfr_count++;
10738
10739 hw->aq.num_arq_entries = I40E_AQ_LEN;
10740 hw->aq.num_asq_entries = I40E_AQ_LEN;
10741 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10742 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10743 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010744
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010745 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010746 "%s-%s:misc",
10747 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010748
10749 err = i40e_init_shared_code(hw);
10750 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010751 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10752 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010753 goto err_pf_reset;
10754 }
10755
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010756 /* set up a default setting for link flow control */
10757 pf->hw.fc.requested_mode = I40E_FC_NONE;
10758
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080010759 /* set up the locks for the AQ, do this only once in probe
10760 * and destroy them only once in remove
10761 */
10762 mutex_init(&hw->aq.asq_mutex);
10763 mutex_init(&hw->aq.arq_mutex);
10764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010765 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a2015-10-26 19:44:35 -040010766 if (err) {
10767 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10768 dev_info(&pdev->dev,
10769 "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");
10770 else
10771 dev_info(&pdev->dev,
10772 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10773
10774 goto err_pf_reset;
10775 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010776
Shannon Nelson6dec1012015-09-28 14:12:30 -040010777 /* provide nvm, fw, api versions */
10778 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10779 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10780 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10781 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010782
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010783 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10784 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010785 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010786 "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");
10787 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10788 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010789 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010790 "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 +000010791
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010792 i40e_verify_eeprom(pf);
10793
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010794 /* Rev 0 hardware was never productized */
10795 if (hw->revision_id < 1)
10796 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");
10797
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010798 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010799 err = i40e_get_capabilities(pf);
10800 if (err)
10801 goto err_adminq_setup;
10802
10803 err = i40e_sw_init(pf);
10804 if (err) {
10805 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10806 goto err_sw_init;
10807 }
10808
10809 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10810 hw->func_caps.num_rx_qp,
10811 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10812 if (err) {
10813 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10814 goto err_init_lan_hmc;
10815 }
10816
10817 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10818 if (err) {
10819 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10820 err = -ENOENT;
10821 goto err_configure_lan_hmc;
10822 }
10823
Neerav Parikhb686ece2014-12-14 01:55:11 +000010824 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10825 * Ignore error return codes because if it was already disabled via
10826 * hardware settings this will fail
10827 */
10828 if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
10829 (pf->hw.aq.fw_maj_ver < 4)) {
10830 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10831 i40e_aq_stop_lldp(hw, true, NULL);
10832 }
10833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010834 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010835 /* allow a platform config to override the HW addr */
10836 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010837 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010838 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10839 err = -EIO;
10840 goto err_mac_addr;
10841 }
10842 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010843 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010844 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10845 if (is_valid_ether_addr(hw->mac.port_addr))
10846 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010847#ifdef I40E_FCOE
10848 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10849 if (err)
10850 dev_info(&pdev->dev,
10851 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10852 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10853 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10854 hw->mac.san_addr);
10855 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10856 }
10857 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10858#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010859
10860 pci_set_drvdata(pdev, pf);
10861 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010862#ifdef CONFIG_I40E_DCB
10863 err = i40e_init_pf_dcb(pf);
10864 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010865 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010866 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010867 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010868 }
10869#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010870
10871 /* set up periodic task facility */
10872 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10873 pf->service_timer_period = HZ;
10874
10875 INIT_WORK(&pf->service_task, i40e_service_task);
10876 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10877 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010878
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010879 /* NVM bit on means WoL disabled for the port */
10880 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010881 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010882 pf->wol_en = false;
10883 else
10884 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010885 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10886
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010887 /* set up the main switch operations */
10888 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010889 err = i40e_init_interrupt_scheme(pf);
10890 if (err)
10891 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010892
Mitch Williams505682c2014-05-20 08:01:37 +000010893 /* The number of VSIs reported by the FW is the minimum guaranteed
10894 * to us; HW supports far more and we share the remaining pool with
10895 * the other PFs. We allocate space for more than the guarantee with
10896 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010897 */
Mitch Williams505682c2014-05-20 08:01:37 +000010898 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10899 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10900 else
10901 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10902
10903 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080010904 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
10905 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010906 if (!pf->vsi) {
10907 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010908 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010909 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010910
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010911#ifdef CONFIG_PCI_IOV
10912 /* prep for VF support */
10913 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10914 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10915 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10916 if (pci_num_vf(pdev))
10917 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10918 }
10919#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010920 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010921 if (err) {
10922 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10923 goto err_vsis;
10924 }
Helin Zhang58fc3262015-10-01 14:37:38 -040010925
10926 /* Make sure flow control is set according to current settings */
10927 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
10928 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
10929 dev_dbg(&pf->pdev->dev,
10930 "Set fc with err %s aq_err %s on get_phy_cap\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_SET)
10934 dev_dbg(&pf->pdev->dev,
10935 "Set fc with err %s aq_err %s on set_phy_config\n",
10936 i40e_stat_str(hw, err),
10937 i40e_aq_str(hw, hw->aq.asq_last_status));
10938 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
10939 dev_dbg(&pf->pdev->dev,
10940 "Set fc with err %s aq_err %s on get_link_info\n",
10941 i40e_stat_str(hw, err),
10942 i40e_aq_str(hw, hw->aq.asq_last_status));
10943
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010944 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000010945 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010946 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
10947 i40e_vsi_open(pf->vsi[i]);
10948 break;
10949 }
10950 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010951
Shannon Nelson2f0aff42016-01-04 10:33:08 -080010952 /* The driver only wants link up/down and module qualification
10953 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010954 */
10955 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080010956 ~(I40E_AQ_EVENT_LINK_UPDOWN |
10957 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010958 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010959 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10960 i40e_stat_str(&pf->hw, err),
10961 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000010962
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010963 /* Reconfigure hardware for allowing smaller MSS in the case
10964 * of TSO, so that we avoid the MDD being fired and causing
10965 * a reset in the case of small MSS+TSO.
10966 */
10967 val = rd32(hw, I40E_REG_MSS);
10968 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10969 val &= ~I40E_REG_MSS_MIN_MASK;
10970 val |= I40E_64BYTE_MSS;
10971 wr32(hw, I40E_REG_MSS, val);
10972 }
10973
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080010974 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000010975 msleep(75);
10976 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10977 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010978 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10979 i40e_stat_str(&pf->hw, err),
10980 i40e_aq_str(&pf->hw,
10981 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000010982 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010983 /* The main driver is (mostly) up and happy. We need to set this state
10984 * before setting up the misc vector or we get a race and the vector
10985 * ends up disabled forever.
10986 */
10987 clear_bit(__I40E_DOWN, &pf->state);
10988
10989 /* In case of MSIX we are going to setup the misc vector right here
10990 * to handle admin queue events etc. In case of legacy and MSI
10991 * the misc functionality and queue processing is combined in
10992 * the same vector and that gets setup at open.
10993 */
10994 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
10995 err = i40e_setup_misc_vector(pf);
10996 if (err) {
10997 dev_info(&pdev->dev,
10998 "setup of misc vector failed: %d\n", err);
10999 goto err_vsis;
11000 }
11001 }
11002
Greg Rosedf805f62014-04-04 04:43:16 +000011003#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011004 /* prep for VF support */
11005 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011006 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11007 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011008 /* disable link interrupts for VFs */
11009 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11010 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11011 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11012 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011013
11014 if (pci_num_vf(pdev)) {
11015 dev_info(&pdev->dev,
11016 "Active VFs found, allocating resources.\n");
11017 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11018 if (err)
11019 dev_info(&pdev->dev,
11020 "Error %d allocating resources for existing VFs\n",
11021 err);
11022 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011023 }
Greg Rosedf805f62014-04-04 04:43:16 +000011024#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011025
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011026 pfs_found++;
11027
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011028 i40e_dbg_pf_init(pf);
11029
11030 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011031 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011032
11033 /* since everything's happy, start the service_task timer */
11034 mod_timer(&pf->service_timer,
11035 round_jiffies(jiffies + pf->service_timer_period));
11036
Vasu Dev38e00432014-08-01 13:27:03 -070011037#ifdef I40E_FCOE
11038 /* create FCoE interface */
11039 i40e_fcoe_vsi_setup(pf);
11040
11041#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011042#define PCI_SPEED_SIZE 8
11043#define PCI_WIDTH_SIZE 8
11044 /* Devices on the IOSF bus do not have this information
11045 * and will report PCI Gen 1 x 1 by default so don't bother
11046 * checking them.
11047 */
11048 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11049 char speed[PCI_SPEED_SIZE] = "Unknown";
11050 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011051
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011052 /* Get the negotiated link width and speed from PCI config
11053 * space
11054 */
11055 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11056 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011057
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011058 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011059
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011060 switch (hw->bus.speed) {
11061 case i40e_bus_speed_8000:
11062 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11063 case i40e_bus_speed_5000:
11064 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11065 case i40e_bus_speed_2500:
11066 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11067 default:
11068 break;
11069 }
11070 switch (hw->bus.width) {
11071 case i40e_bus_width_pcie_x8:
11072 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11073 case i40e_bus_width_pcie_x4:
11074 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11075 case i40e_bus_width_pcie_x2:
11076 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11077 case i40e_bus_width_pcie_x1:
11078 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11079 default:
11080 break;
11081 }
11082
11083 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11084 speed, width);
11085
11086 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11087 hw->bus.speed < i40e_bus_speed_8000) {
11088 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11089 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11090 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011091 }
11092
Catherine Sullivane8278452015-02-06 08:52:08 +000011093 /* get the requested speeds from the fw */
11094 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11095 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011096 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11097 i40e_stat_str(&pf->hw, err),
11098 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011099 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11100
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011101 /* get the supported phy types from the fw */
11102 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11103 if (err)
11104 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11105 i40e_stat_str(&pf->hw, err),
11106 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11107 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11108
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011109 /* Add a filter to drop all Flow control frames from any VSI from being
11110 * transmitted. By doing so we stop a malicious VF from sending out
11111 * PAUSE or PFC frames and potentially controlling traffic for other
11112 * PF/VF VSIs.
11113 * The FW can still send Flow control frames if enabled.
11114 */
11115 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11116 pf->main_vsi_seid);
11117
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011118 /* print a string summarizing features */
11119 i40e_print_features(pf);
11120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011121 return 0;
11122
11123 /* Unwind what we've done if something failed in the setup */
11124err_vsis:
11125 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011126 i40e_clear_interrupt_scheme(pf);
11127 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011128err_switch_setup:
11129 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011130 del_timer_sync(&pf->service_timer);
11131err_mac_addr:
11132err_configure_lan_hmc:
11133 (void)i40e_shutdown_lan_hmc(hw);
11134err_init_lan_hmc:
11135 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136err_sw_init:
11137err_adminq_setup:
11138 (void)i40e_shutdown_adminq(hw);
11139err_pf_reset:
11140 iounmap(hw->hw_addr);
11141err_ioremap:
11142 kfree(pf);
11143err_pf_alloc:
11144 pci_disable_pcie_error_reporting(pdev);
11145 pci_release_selected_regions(pdev,
11146 pci_select_bars(pdev, IORESOURCE_MEM));
11147err_pci_reg:
11148err_dma:
11149 pci_disable_device(pdev);
11150 return err;
11151}
11152
11153/**
11154 * i40e_remove - Device removal routine
11155 * @pdev: PCI device information struct
11156 *
11157 * i40e_remove is called by the PCI subsystem to alert the driver
11158 * that is should release a PCI device. This could be caused by a
11159 * Hot-Plug event, or because the driver is going to be removed from
11160 * memory.
11161 **/
11162static void i40e_remove(struct pci_dev *pdev)
11163{
11164 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011165 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011166 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011167 int i;
11168
11169 i40e_dbg_pf_exit(pf);
11170
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011171 i40e_ptp_stop(pf);
11172
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011173 /* Disable RSS in hw */
11174 wr32(hw, I40E_PFQF_HENA(0), 0);
11175 wr32(hw, I40E_PFQF_HENA(1), 0);
11176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011177 /* no more scheduling of any task */
11178 set_bit(__I40E_DOWN, &pf->state);
11179 del_timer_sync(&pf->service_timer);
11180 cancel_work_sync(&pf->service_task);
11181
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011182 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11183 i40e_free_vfs(pf);
11184 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11185 }
11186
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011187 i40e_fdir_teardown(pf);
11188
11189 /* If there is a switch structure or any orphans, remove them.
11190 * This will leave only the PF's VSI remaining.
11191 */
11192 for (i = 0; i < I40E_MAX_VEB; i++) {
11193 if (!pf->veb[i])
11194 continue;
11195
11196 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11197 pf->veb[i]->uplink_seid == 0)
11198 i40e_switch_branch_release(pf->veb[i]);
11199 }
11200
11201 /* Now we can shutdown the PF's VSI, just before we kill
11202 * adminq and hmc.
11203 */
11204 if (pf->vsi[pf->lan_vsi])
11205 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11206
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011207 /* shutdown and destroy the HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +000011208 if (pf->hw.hmc.hmc_obj) {
11209 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
11210 if (ret_code)
11211 dev_warn(&pdev->dev,
11212 "Failed to destroy the HMC resources: %d\n",
11213 ret_code);
11214 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011215
11216 /* shutdown the adminq */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011217 ret_code = i40e_shutdown_adminq(&pf->hw);
11218 if (ret_code)
11219 dev_warn(&pdev->dev,
11220 "Failed to destroy the Admin Queue resources: %d\n",
11221 ret_code);
11222
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011223 /* destroy the locks only once, here */
11224 mutex_destroy(&hw->aq.arq_mutex);
11225 mutex_destroy(&hw->aq.asq_mutex);
11226
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011227 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11228 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011229 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011230 if (pf->vsi[i]) {
11231 i40e_vsi_clear_rings(pf->vsi[i]);
11232 i40e_vsi_clear(pf->vsi[i]);
11233 pf->vsi[i] = NULL;
11234 }
11235 }
11236
11237 for (i = 0; i < I40E_MAX_VEB; i++) {
11238 kfree(pf->veb[i]);
11239 pf->veb[i] = NULL;
11240 }
11241
11242 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011243 kfree(pf->vsi);
11244
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011245 iounmap(pf->hw.hw_addr);
11246 kfree(pf);
11247 pci_release_selected_regions(pdev,
11248 pci_select_bars(pdev, IORESOURCE_MEM));
11249
11250 pci_disable_pcie_error_reporting(pdev);
11251 pci_disable_device(pdev);
11252}
11253
11254/**
11255 * i40e_pci_error_detected - warning that something funky happened in PCI land
11256 * @pdev: PCI device information struct
11257 *
11258 * Called to warn that something happened and the error handling steps
11259 * are in progress. Allows the driver to quiesce things, be ready for
11260 * remediation.
11261 **/
11262static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11263 enum pci_channel_state error)
11264{
11265 struct i40e_pf *pf = pci_get_drvdata(pdev);
11266
11267 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11268
11269 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011270 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11271 rtnl_lock();
11272 i40e_prep_for_reset(pf);
11273 rtnl_unlock();
11274 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011275
11276 /* Request a slot reset */
11277 return PCI_ERS_RESULT_NEED_RESET;
11278}
11279
11280/**
11281 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11282 * @pdev: PCI device information struct
11283 *
11284 * Called to find if the driver can work with the device now that
11285 * the pci slot has been reset. If a basic connection seems good
11286 * (registers are readable and have sane content) then return a
11287 * happy little PCI_ERS_RESULT_xxx.
11288 **/
11289static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11290{
11291 struct i40e_pf *pf = pci_get_drvdata(pdev);
11292 pci_ers_result_t result;
11293 int err;
11294 u32 reg;
11295
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011296 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011297 if (pci_enable_device_mem(pdev)) {
11298 dev_info(&pdev->dev,
11299 "Cannot re-enable PCI device after reset.\n");
11300 result = PCI_ERS_RESULT_DISCONNECT;
11301 } else {
11302 pci_set_master(pdev);
11303 pci_restore_state(pdev);
11304 pci_save_state(pdev);
11305 pci_wake_from_d3(pdev, false);
11306
11307 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11308 if (reg == 0)
11309 result = PCI_ERS_RESULT_RECOVERED;
11310 else
11311 result = PCI_ERS_RESULT_DISCONNECT;
11312 }
11313
11314 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11315 if (err) {
11316 dev_info(&pdev->dev,
11317 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11318 err);
11319 /* non-fatal, continue */
11320 }
11321
11322 return result;
11323}
11324
11325/**
11326 * i40e_pci_error_resume - restart operations after PCI error recovery
11327 * @pdev: PCI device information struct
11328 *
11329 * Called to allow the driver to bring things back up after PCI error
11330 * and/or reset recovery has finished.
11331 **/
11332static void i40e_pci_error_resume(struct pci_dev *pdev)
11333{
11334 struct i40e_pf *pf = pci_get_drvdata(pdev);
11335
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011336 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011337 if (test_bit(__I40E_SUSPENDED, &pf->state))
11338 return;
11339
11340 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011341 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011342 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011343}
11344
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011345/**
11346 * i40e_shutdown - PCI callback for shutting down
11347 * @pdev: PCI device information struct
11348 **/
11349static void i40e_shutdown(struct pci_dev *pdev)
11350{
11351 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011352 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011353
11354 set_bit(__I40E_SUSPENDED, &pf->state);
11355 set_bit(__I40E_DOWN, &pf->state);
11356 rtnl_lock();
11357 i40e_prep_for_reset(pf);
11358 rtnl_unlock();
11359
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011360 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11361 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11362
Catherine Sullivan02b42492015-07-10 19:35:59 -040011363 del_timer_sync(&pf->service_timer);
11364 cancel_work_sync(&pf->service_task);
11365 i40e_fdir_teardown(pf);
11366
11367 rtnl_lock();
11368 i40e_prep_for_reset(pf);
11369 rtnl_unlock();
11370
11371 wr32(hw, I40E_PFPM_APM,
11372 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11373 wr32(hw, I40E_PFPM_WUFC,
11374 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11375
Shannon Nelsone1477582015-02-21 06:44:33 +000011376 i40e_clear_interrupt_scheme(pf);
11377
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011378 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011379 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011380 pci_set_power_state(pdev, PCI_D3hot);
11381 }
11382}
11383
11384#ifdef CONFIG_PM
11385/**
11386 * i40e_suspend - PCI callback for moving to D3
11387 * @pdev: PCI device information struct
11388 **/
11389static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11390{
11391 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011392 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011393
11394 set_bit(__I40E_SUSPENDED, &pf->state);
11395 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011396
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011397 rtnl_lock();
11398 i40e_prep_for_reset(pf);
11399 rtnl_unlock();
11400
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011401 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11402 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11403
11404 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011405 pci_set_power_state(pdev, PCI_D3hot);
11406
11407 return 0;
11408}
11409
11410/**
11411 * i40e_resume - PCI callback for waking up from D3
11412 * @pdev: PCI device information struct
11413 **/
11414static int i40e_resume(struct pci_dev *pdev)
11415{
11416 struct i40e_pf *pf = pci_get_drvdata(pdev);
11417 u32 err;
11418
11419 pci_set_power_state(pdev, PCI_D0);
11420 pci_restore_state(pdev);
11421 /* pci_restore_state() clears dev->state_saves, so
11422 * call pci_save_state() again to restore it.
11423 */
11424 pci_save_state(pdev);
11425
11426 err = pci_enable_device_mem(pdev);
11427 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011428 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011429 return err;
11430 }
11431 pci_set_master(pdev);
11432
11433 /* no wakeup events while running */
11434 pci_wake_from_d3(pdev, false);
11435
11436 /* handling the reset will rebuild the device state */
11437 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11438 clear_bit(__I40E_DOWN, &pf->state);
11439 rtnl_lock();
11440 i40e_reset_and_rebuild(pf, false);
11441 rtnl_unlock();
11442 }
11443
11444 return 0;
11445}
11446
11447#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011448static const struct pci_error_handlers i40e_err_handler = {
11449 .error_detected = i40e_pci_error_detected,
11450 .slot_reset = i40e_pci_error_slot_reset,
11451 .resume = i40e_pci_error_resume,
11452};
11453
11454static struct pci_driver i40e_driver = {
11455 .name = i40e_driver_name,
11456 .id_table = i40e_pci_tbl,
11457 .probe = i40e_probe,
11458 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011459#ifdef CONFIG_PM
11460 .suspend = i40e_suspend,
11461 .resume = i40e_resume,
11462#endif
11463 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011464 .err_handler = &i40e_err_handler,
11465 .sriov_configure = i40e_pci_sriov_configure,
11466};
11467
11468/**
11469 * i40e_init_module - Driver registration routine
11470 *
11471 * i40e_init_module is the first routine called when the driver is
11472 * loaded. All it does is register with the PCI subsystem.
11473 **/
11474static int __init i40e_init_module(void)
11475{
11476 pr_info("%s: %s - version %s\n", i40e_driver_name,
11477 i40e_driver_string, i40e_driver_version_str);
11478 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011479
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011480 /* we will see if single thread per module is enough for now,
11481 * it can't be any worse than using the system workqueue which
11482 * was already single threaded
11483 */
11484 i40e_wq = create_singlethread_workqueue(i40e_driver_name);
11485 if (!i40e_wq) {
11486 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11487 return -ENOMEM;
11488 }
11489
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011490 i40e_dbg_init();
11491 return pci_register_driver(&i40e_driver);
11492}
11493module_init(i40e_init_module);
11494
11495/**
11496 * i40e_exit_module - Driver exit cleanup routine
11497 *
11498 * i40e_exit_module is called just before the driver is removed
11499 * from memory.
11500 **/
11501static void __exit i40e_exit_module(void)
11502{
11503 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011504 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011505 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011506}
11507module_exit(i40e_exit_module);