blob: 8e6c0f2487d7bb94d306f4708bc78ef59f0b0f0f [file] [log] [blame]
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08004 * Copyright(c) 2013 - 2016 Intel Corporation.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050027#include <linux/etherdevice.h>
28#include <linux/of_net.h>
29#include <linux/pci.h>
30
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000031/* Local includes */
32#include "i40e.h"
Shannon Nelson4eb3f762014-03-06 08:59:58 +000033#include "i40e_diag.h"
Singhai, Anjali6a899022015-12-14 12:21:18 -080034#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +000035#include <net/vxlan.h>
36#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080037#if IS_ENABLED(CONFIG_GENEVE)
38#include <net/geneve.h>
39#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000040
41const char i40e_driver_name[] = "i40e";
42static const char i40e_driver_string[] =
43 "Intel(R) Ethernet Connection XL710 Network Driver";
44
45#define DRV_KERN "-k"
46
Catherine Sullivane8e724d2014-07-10 07:58:26 +000047#define DRV_VERSION_MAJOR 1
Catherine Sullivan50f26a52016-03-10 14:59:51 -080048#define DRV_VERSION_MINOR 5
Harshitha Ramamurthy5a6fc252016-04-13 03:08:32 -070049#define DRV_VERSION_BUILD 10
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
51 __stringify(DRV_VERSION_MINOR) "." \
52 __stringify(DRV_VERSION_BUILD) DRV_KERN
53const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080054static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000055
56/* a bit of forward declarations */
57static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
58static void i40e_handle_reset_warning(struct i40e_pf *pf);
59static int i40e_add_vsi(struct i40e_vsi *vsi);
60static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000061static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062static int i40e_setup_misc_vector(struct i40e_pf *pf);
63static void i40e_determine_queue_usage(struct i40e_pf *pf);
64static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Helin Zhange69ff812015-10-21 19:56:22 -040065static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
66 u16 rss_table_size, u16 rss_size);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080067static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080068static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000069
70/* i40e_pci_tbl - PCI Device ID Table
71 *
72 * Last entry must be all 0s
73 *
74 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
75 * Class, Class Mask, private data (not used) }
76 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020077static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040090 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070093 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Catherine Sullivana3aa5032016-04-13 03:08:31 -070094 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040095 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
96 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000097 /* required last entry */
98 {0, }
99};
100MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
101
102#define I40E_MAX_VF_COUNT 128
103static int debug = -1;
104module_param(debug, int, 0);
105MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
106
107MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
108MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
109MODULE_LICENSE("GPL");
110MODULE_VERSION(DRV_VERSION);
111
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800112static struct workqueue_struct *i40e_wq;
113
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000114/**
115 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
116 * @hw: pointer to the HW structure
117 * @mem: ptr to mem struct to fill out
118 * @size: size of memory requested
119 * @alignment: what to align the allocation to
120 **/
121int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
122 u64 size, u32 alignment)
123{
124 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
125
126 mem->size = ALIGN(size, alignment);
127 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
128 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000129 if (!mem->va)
130 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000131
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000132 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000133}
134
135/**
136 * i40e_free_dma_mem_d - OS specific memory free for shared code
137 * @hw: pointer to the HW structure
138 * @mem: ptr to mem struct to free
139 **/
140int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
141{
142 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
143
144 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
145 mem->va = NULL;
146 mem->pa = 0;
147 mem->size = 0;
148
149 return 0;
150}
151
152/**
153 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
154 * @hw: pointer to the HW structure
155 * @mem: ptr to mem struct to fill out
156 * @size: size of memory requested
157 **/
158int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
159 u32 size)
160{
161 mem->size = size;
162 mem->va = kzalloc(size, GFP_KERNEL);
163
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000164 if (!mem->va)
165 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000166
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000167 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000168}
169
170/**
171 * i40e_free_virt_mem_d - OS specific memory free for shared code
172 * @hw: pointer to the HW structure
173 * @mem: ptr to mem struct to free
174 **/
175int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
176{
177 /* it's ok to kfree a NULL pointer */
178 kfree(mem->va);
179 mem->va = NULL;
180 mem->size = 0;
181
182 return 0;
183}
184
185/**
186 * i40e_get_lump - find a lump of free generic resource
187 * @pf: board private structure
188 * @pile: the pile of resource to search
189 * @needed: the number of items needed
190 * @id: an owner id to stick on the items assigned
191 *
192 * Returns the base item index of the lump, or negative for error
193 *
194 * The search_hint trick and lack of advanced fit-finding only work
195 * because we're highly likely to have all the same size lump requests.
196 * Linear search time and any fragmentation should be minimal.
197 **/
198static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
199 u16 needed, u16 id)
200{
201 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000202 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000203
204 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
205 dev_info(&pf->pdev->dev,
206 "param err: pile=%p needed=%d id=0x%04x\n",
207 pile, needed, id);
208 return -EINVAL;
209 }
210
211 /* start the linear search with an imperfect hint */
212 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000213 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000214 /* skip already allocated entries */
215 if (pile->list[i] & I40E_PILE_VALID_BIT) {
216 i++;
217 continue;
218 }
219
220 /* do we have enough in this lump? */
221 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
222 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
223 break;
224 }
225
226 if (j == needed) {
227 /* there was enough, so assign it to the requestor */
228 for (j = 0; j < needed; j++)
229 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
230 ret = i;
231 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000232 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000233 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400234
235 /* not enough, so skip over it and continue looking */
236 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000237 }
238
239 return ret;
240}
241
242/**
243 * i40e_put_lump - return a lump of generic resource
244 * @pile: the pile of resource to search
245 * @index: the base item index
246 * @id: the owner id of the items assigned
247 *
248 * Returns the count of items in the lump
249 **/
250static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
251{
252 int valid_id = (id | I40E_PILE_VALID_BIT);
253 int count = 0;
254 int i;
255
256 if (!pile || index >= pile->num_entries)
257 return -EINVAL;
258
259 for (i = index;
260 i < pile->num_entries && pile->list[i] == valid_id;
261 i++) {
262 pile->list[i] = 0;
263 count++;
264 }
265
266 if (count && index < pile->search_hint)
267 pile->search_hint = index;
268
269 return count;
270}
271
272/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700273 * i40e_find_vsi_from_id - searches for the vsi with the given id
274 * @pf - the pf structure to search for the vsi
275 * @id - id of the vsi it is searching for
276 **/
277struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
278{
279 int i;
280
281 for (i = 0; i < pf->num_alloc_vsi; i++)
282 if (pf->vsi[i] && (pf->vsi[i]->id == id))
283 return pf->vsi[i];
284
285 return NULL;
286}
287
288/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000289 * i40e_service_event_schedule - Schedule the service task to wake up
290 * @pf: board private structure
291 *
292 * If not already scheduled, this puts the task into the work queue
293 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600294void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000295{
296 if (!test_bit(__I40E_DOWN, &pf->state) &&
297 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
298 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800299 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000300}
301
302/**
303 * i40e_tx_timeout - Respond to a Tx Hang
304 * @netdev: network interface device structure
305 *
306 * If any port has noticed a Tx timeout, it is likely that the whole
307 * device is munged, not just the one netdev port, so go for the full
308 * reset.
309 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700310#ifdef I40E_FCOE
311void i40e_tx_timeout(struct net_device *netdev)
312#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000313static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700314#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000315{
316 struct i40e_netdev_priv *np = netdev_priv(netdev);
317 struct i40e_vsi *vsi = np->vsi;
318 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400319 struct i40e_ring *tx_ring = NULL;
320 unsigned int i, hung_queue = 0;
321 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000322
323 pf->tx_timeout_count++;
324
Kiran Patilb03a8c12015-09-24 18:13:15 -0400325 /* find the stopped queue the same way the stack does */
326 for (i = 0; i < netdev->num_tx_queues; i++) {
327 struct netdev_queue *q;
328 unsigned long trans_start;
329
330 q = netdev_get_tx_queue(netdev, i);
331 trans_start = q->trans_start ? : netdev->trans_start;
332 if (netif_xmit_stopped(q) &&
333 time_after(jiffies,
334 (trans_start + netdev->watchdog_timeo))) {
335 hung_queue = i;
336 break;
337 }
338 }
339
340 if (i == netdev->num_tx_queues) {
341 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
342 } else {
343 /* now that we have an index, find the tx_ring struct */
344 for (i = 0; i < vsi->num_queue_pairs; i++) {
345 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
346 if (hung_queue ==
347 vsi->tx_rings[i]->queue_index) {
348 tx_ring = vsi->tx_rings[i];
349 break;
350 }
351 }
352 }
353 }
354
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000355 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400356 pf->tx_timeout_recovery_level = 1; /* reset after some time */
357 else if (time_before(jiffies,
358 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
359 return; /* don't do any new action before the next timeout */
360
361 if (tx_ring) {
362 head = i40e_get_head(tx_ring);
363 /* Read interrupt register */
364 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
365 val = rd32(&pf->hw,
366 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
367 tx_ring->vsi->base_vector - 1));
368 else
369 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
370
371 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",
372 vsi->seid, hung_queue, tx_ring->next_to_clean,
373 head, tx_ring->next_to_use,
374 readl(tx_ring->tail), val);
375 }
376
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000377 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400378 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
379 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000380
381 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000382 case 1:
383 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
384 break;
385 case 2:
386 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
387 break;
388 case 3:
389 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
390 break;
391 default:
392 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000393 break;
394 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400395
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000396 i40e_service_event_schedule(pf);
397 pf->tx_timeout_recovery_level++;
398}
399
400/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000401 * i40e_get_vsi_stats_struct - Get System Network Statistics
402 * @vsi: the VSI we care about
403 *
404 * Returns the address of the device statistics structure.
405 * The statistics are actually updated from the service task.
406 **/
407struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
408{
409 return &vsi->net_stats;
410}
411
412/**
413 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
414 * @netdev: network interface device structure
415 *
416 * Returns the address of the device statistics structure.
417 * The statistics are actually updated from the service task.
418 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700419#ifdef I40E_FCOE
420struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
421 struct net_device *netdev,
422 struct rtnl_link_stats64 *stats)
423#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000424static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
425 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000426 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700427#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000428{
429 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000430 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000431 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000432 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
433 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000434
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000435 if (test_bit(__I40E_DOWN, &vsi->state))
436 return stats;
437
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800438 if (!vsi->tx_rings)
439 return stats;
440
Alexander Duyck980e9b12013-09-28 06:01:03 +0000441 rcu_read_lock();
442 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000443 u64 bytes, packets;
444 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000445
Alexander Duyck980e9b12013-09-28 06:01:03 +0000446 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
447 if (!tx_ring)
448 continue;
449
450 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700451 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000452 packets = tx_ring->stats.packets;
453 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700454 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000455
456 stats->tx_packets += packets;
457 stats->tx_bytes += bytes;
458 rx_ring = &tx_ring[1];
459
460 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700461 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000462 packets = rx_ring->stats.packets;
463 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700464 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000465
466 stats->rx_packets += packets;
467 stats->rx_bytes += bytes;
468 }
469 rcu_read_unlock();
470
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000471 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000472 stats->multicast = vsi_stats->multicast;
473 stats->tx_errors = vsi_stats->tx_errors;
474 stats->tx_dropped = vsi_stats->tx_dropped;
475 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400476 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000477 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
478 stats->rx_length_errors = vsi_stats->rx_length_errors;
479
480 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000481}
482
483/**
484 * i40e_vsi_reset_stats - Resets all stats of the given vsi
485 * @vsi: the VSI to have its stats reset
486 **/
487void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
488{
489 struct rtnl_link_stats64 *ns;
490 int i;
491
492 if (!vsi)
493 return;
494
495 ns = i40e_get_vsi_stats_struct(vsi);
496 memset(ns, 0, sizeof(*ns));
497 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
498 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
499 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000500 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000501 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400502 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000503 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400504 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000505 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400506 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000507 sizeof(vsi->tx_rings[i]->stats));
508 memset(&vsi->tx_rings[i]->tx_stats, 0,
509 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000510 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000511 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000512 vsi->stat_offsets_loaded = false;
513}
514
515/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000516 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000517 * @pf: the PF to be reset
518 **/
519void i40e_pf_reset_stats(struct i40e_pf *pf)
520{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000521 int i;
522
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000523 memset(&pf->stats, 0, sizeof(pf->stats));
524 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
525 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000526
527 for (i = 0; i < I40E_MAX_VEB; i++) {
528 if (pf->veb[i]) {
529 memset(&pf->veb[i]->stats, 0,
530 sizeof(pf->veb[i]->stats));
531 memset(&pf->veb[i]->stats_offsets, 0,
532 sizeof(pf->veb[i]->stats_offsets));
533 pf->veb[i]->stat_offsets_loaded = false;
534 }
535 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000536}
537
538/**
539 * i40e_stat_update48 - read and update a 48 bit stat from the chip
540 * @hw: ptr to the hardware info
541 * @hireg: the high 32 bit reg to read
542 * @loreg: the low 32 bit reg to read
543 * @offset_loaded: has the initial offset been loaded yet
544 * @offset: ptr to current offset value
545 * @stat: ptr to the stat
546 *
547 * Since the device stats are not reset at PFReset, they likely will not
548 * be zeroed when the driver starts. We'll save the first values read
549 * and use them as offsets to be subtracted from the raw values in order
550 * to report stats that count from zero. In the process, we also manage
551 * the potential roll-over.
552 **/
553static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
554 bool offset_loaded, u64 *offset, u64 *stat)
555{
556 u64 new_data;
557
Shannon Nelsonab600852014-01-17 15:36:39 -0800558 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000559 new_data = rd32(hw, loreg);
560 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
561 } else {
562 new_data = rd64(hw, loreg);
563 }
564 if (!offset_loaded)
565 *offset = new_data;
566 if (likely(new_data >= *offset))
567 *stat = new_data - *offset;
568 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400569 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000570 *stat &= 0xFFFFFFFFFFFFULL;
571}
572
573/**
574 * i40e_stat_update32 - read and update a 32 bit stat from the chip
575 * @hw: ptr to the hardware info
576 * @reg: the hw reg to read
577 * @offset_loaded: has the initial offset been loaded yet
578 * @offset: ptr to current offset value
579 * @stat: ptr to the stat
580 **/
581static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
582 bool offset_loaded, u64 *offset, u64 *stat)
583{
584 u32 new_data;
585
586 new_data = rd32(hw, reg);
587 if (!offset_loaded)
588 *offset = new_data;
589 if (likely(new_data >= *offset))
590 *stat = (u32)(new_data - *offset);
591 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400592 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000593}
594
595/**
596 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
597 * @vsi: the VSI to be updated
598 **/
599void i40e_update_eth_stats(struct i40e_vsi *vsi)
600{
601 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
602 struct i40e_pf *pf = vsi->back;
603 struct i40e_hw *hw = &pf->hw;
604 struct i40e_eth_stats *oes;
605 struct i40e_eth_stats *es; /* device's eth stats */
606
607 es = &vsi->eth_stats;
608 oes = &vsi->eth_stats_offsets;
609
610 /* Gather up the stats that the hw collects */
611 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
612 vsi->stat_offsets_loaded,
613 &oes->tx_errors, &es->tx_errors);
614 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
615 vsi->stat_offsets_loaded,
616 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000617 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
618 vsi->stat_offsets_loaded,
619 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
620 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
621 vsi->stat_offsets_loaded,
622 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000623
624 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
625 I40E_GLV_GORCL(stat_idx),
626 vsi->stat_offsets_loaded,
627 &oes->rx_bytes, &es->rx_bytes);
628 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
629 I40E_GLV_UPRCL(stat_idx),
630 vsi->stat_offsets_loaded,
631 &oes->rx_unicast, &es->rx_unicast);
632 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
633 I40E_GLV_MPRCL(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->rx_multicast, &es->rx_multicast);
636 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
637 I40E_GLV_BPRCL(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->rx_broadcast, &es->rx_broadcast);
640
641 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
642 I40E_GLV_GOTCL(stat_idx),
643 vsi->stat_offsets_loaded,
644 &oes->tx_bytes, &es->tx_bytes);
645 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
646 I40E_GLV_UPTCL(stat_idx),
647 vsi->stat_offsets_loaded,
648 &oes->tx_unicast, &es->tx_unicast);
649 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
650 I40E_GLV_MPTCL(stat_idx),
651 vsi->stat_offsets_loaded,
652 &oes->tx_multicast, &es->tx_multicast);
653 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
654 I40E_GLV_BPTCL(stat_idx),
655 vsi->stat_offsets_loaded,
656 &oes->tx_broadcast, &es->tx_broadcast);
657 vsi->stat_offsets_loaded = true;
658}
659
660/**
661 * i40e_update_veb_stats - Update Switch component statistics
662 * @veb: the VEB being updated
663 **/
664static void i40e_update_veb_stats(struct i40e_veb *veb)
665{
666 struct i40e_pf *pf = veb->pf;
667 struct i40e_hw *hw = &pf->hw;
668 struct i40e_eth_stats *oes;
669 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400670 struct i40e_veb_tc_stats *veb_oes;
671 struct i40e_veb_tc_stats *veb_es;
672 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000673
674 idx = veb->stats_idx;
675 es = &veb->stats;
676 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400677 veb_es = &veb->tc_stats;
678 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000679
680 /* Gather up the stats that the hw collects */
681 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
682 veb->stat_offsets_loaded,
683 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000684 if (hw->revision_id > 0)
685 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
686 veb->stat_offsets_loaded,
687 &oes->rx_unknown_protocol,
688 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000689 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
690 veb->stat_offsets_loaded,
691 &oes->rx_bytes, &es->rx_bytes);
692 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
693 veb->stat_offsets_loaded,
694 &oes->rx_unicast, &es->rx_unicast);
695 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
696 veb->stat_offsets_loaded,
697 &oes->rx_multicast, &es->rx_multicast);
698 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->rx_broadcast, &es->rx_broadcast);
701
702 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
703 veb->stat_offsets_loaded,
704 &oes->tx_bytes, &es->tx_bytes);
705 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
706 veb->stat_offsets_loaded,
707 &oes->tx_unicast, &es->tx_unicast);
708 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
709 veb->stat_offsets_loaded,
710 &oes->tx_multicast, &es->tx_multicast);
711 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
712 veb->stat_offsets_loaded,
713 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400714 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
715 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
716 I40E_GLVEBTC_RPCL(i, idx),
717 veb->stat_offsets_loaded,
718 &veb_oes->tc_rx_packets[i],
719 &veb_es->tc_rx_packets[i]);
720 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
721 I40E_GLVEBTC_RBCL(i, idx),
722 veb->stat_offsets_loaded,
723 &veb_oes->tc_rx_bytes[i],
724 &veb_es->tc_rx_bytes[i]);
725 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
726 I40E_GLVEBTC_TPCL(i, idx),
727 veb->stat_offsets_loaded,
728 &veb_oes->tc_tx_packets[i],
729 &veb_es->tc_tx_packets[i]);
730 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
731 I40E_GLVEBTC_TBCL(i, idx),
732 veb->stat_offsets_loaded,
733 &veb_oes->tc_tx_bytes[i],
734 &veb_es->tc_tx_bytes[i]);
735 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000736 veb->stat_offsets_loaded = true;
737}
738
Vasu Dev38e00432014-08-01 13:27:03 -0700739#ifdef I40E_FCOE
740/**
741 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
742 * @vsi: the VSI that is capable of doing FCoE
743 **/
744static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
745{
746 struct i40e_pf *pf = vsi->back;
747 struct i40e_hw *hw = &pf->hw;
748 struct i40e_fcoe_stats *ofs;
749 struct i40e_fcoe_stats *fs; /* device's eth stats */
750 int idx;
751
752 if (vsi->type != I40E_VSI_FCOE)
753 return;
754
Kiran Patil4147e2c2016-01-15 14:33:14 -0800755 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700756 fs = &vsi->fcoe_stats;
757 ofs = &vsi->fcoe_stats_offsets;
758
759 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
760 vsi->fcoe_stat_offsets_loaded,
761 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
762 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
763 vsi->fcoe_stat_offsets_loaded,
764 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
765 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
766 vsi->fcoe_stat_offsets_loaded,
767 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
768 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
769 vsi->fcoe_stat_offsets_loaded,
770 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
771 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
772 vsi->fcoe_stat_offsets_loaded,
773 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
774 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
775 vsi->fcoe_stat_offsets_loaded,
776 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
777 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
778 vsi->fcoe_stat_offsets_loaded,
779 &ofs->fcoe_last_error, &fs->fcoe_last_error);
780 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
781 vsi->fcoe_stat_offsets_loaded,
782 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
783
784 vsi->fcoe_stat_offsets_loaded = true;
785}
786
787#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000788/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000789 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000790 * @vsi: the VSI to be updated
791 *
792 * There are a few instances where we store the same stat in a
793 * couple of different structs. This is partly because we have
794 * the netdev stats that need to be filled out, which is slightly
795 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000796 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000797 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000798static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000799{
800 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 struct rtnl_link_stats64 *ons;
802 struct rtnl_link_stats64 *ns; /* netdev stats */
803 struct i40e_eth_stats *oes;
804 struct i40e_eth_stats *es; /* device's eth stats */
805 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800806 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000807 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000809 u64 bytes, packets;
810 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400811 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400812 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000813 u64 rx_p, rx_b;
814 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000815 u16 q;
816
817 if (test_bit(__I40E_DOWN, &vsi->state) ||
818 test_bit(__I40E_CONFIG_BUSY, &pf->state))
819 return;
820
821 ns = i40e_get_vsi_stats_struct(vsi);
822 ons = &vsi->net_stats_offsets;
823 es = &vsi->eth_stats;
824 oes = &vsi->eth_stats_offsets;
825
826 /* Gather up the netdev and vsi stats that the driver collects
827 * on the fly during packet processing
828 */
829 rx_b = rx_p = 0;
830 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400831 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800832 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000833 rx_page = 0;
834 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000835 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000836 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000837 /* locate Tx ring */
838 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000839
Alexander Duyck980e9b12013-09-28 06:01:03 +0000840 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700841 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000842 packets = p->stats.packets;
843 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700844 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000845 tx_b += bytes;
846 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000847 tx_restart += p->tx_stats.restart_queue;
848 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400849 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400850 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800851 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000852
853 /* Rx queue is part of the same block as Tx queue */
854 p = &p[1];
855 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700856 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000857 packets = p->stats.packets;
858 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700859 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000860 rx_b += bytes;
861 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000862 rx_buf += p->rx_stats.alloc_buff_failed;
863 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000864 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000865 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000866 vsi->tx_restart = tx_restart;
867 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400868 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400869 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800870 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000871 vsi->rx_page_failed = rx_page;
872 vsi->rx_buf_failed = rx_buf;
873
874 ns->rx_packets = rx_p;
875 ns->rx_bytes = rx_b;
876 ns->tx_packets = tx_p;
877 ns->tx_bytes = tx_b;
878
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000879 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000880 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000881 ons->tx_errors = oes->tx_errors;
882 ns->tx_errors = es->tx_errors;
883 ons->multicast = oes->rx_multicast;
884 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000885 ons->rx_dropped = oes->rx_discards;
886 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000887 ons->tx_dropped = oes->tx_discards;
888 ns->tx_dropped = es->tx_discards;
889
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000890 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892 ns->rx_crc_errors = pf->stats.crc_errors;
893 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
894 ns->rx_length_errors = pf->stats.rx_length_errors;
895 }
896}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000897
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000898/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000899 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000900 * @pf: the PF to be updated
901 **/
902static void i40e_update_pf_stats(struct i40e_pf *pf)
903{
904 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
905 struct i40e_hw_port_stats *nsd = &pf->stats;
906 struct i40e_hw *hw = &pf->hw;
907 u32 val;
908 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000909
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000910 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
911 I40E_GLPRT_GORCL(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
914 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
915 I40E_GLPRT_GOTCL(hw->port),
916 pf->stat_offsets_loaded,
917 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
918 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
919 pf->stat_offsets_loaded,
920 &osd->eth.rx_discards,
921 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000922 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
923 I40E_GLPRT_UPRCL(hw->port),
924 pf->stat_offsets_loaded,
925 &osd->eth.rx_unicast,
926 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000927 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
928 I40E_GLPRT_MPRCL(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->eth.rx_multicast,
931 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000932 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
933 I40E_GLPRT_BPRCL(hw->port),
934 pf->stat_offsets_loaded,
935 &osd->eth.rx_broadcast,
936 &nsd->eth.rx_broadcast);
937 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
938 I40E_GLPRT_UPTCL(hw->port),
939 pf->stat_offsets_loaded,
940 &osd->eth.tx_unicast,
941 &nsd->eth.tx_unicast);
942 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
943 I40E_GLPRT_MPTCL(hw->port),
944 pf->stat_offsets_loaded,
945 &osd->eth.tx_multicast,
946 &nsd->eth.tx_multicast);
947 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
948 I40E_GLPRT_BPTCL(hw->port),
949 pf->stat_offsets_loaded,
950 &osd->eth.tx_broadcast,
951 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000952
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000953 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->tx_dropped_link_down,
956 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000957
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000958 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
959 pf->stat_offsets_loaded,
960 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000961
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000962 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
963 pf->stat_offsets_loaded,
964 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000965
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000966 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
967 pf->stat_offsets_loaded,
968 &osd->mac_local_faults,
969 &nsd->mac_local_faults);
970 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->mac_remote_faults,
973 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000974
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000975 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
976 pf->stat_offsets_loaded,
977 &osd->rx_length_errors,
978 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000979
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000980 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
981 pf->stat_offsets_loaded,
982 &osd->link_xon_rx, &nsd->link_xon_rx);
983 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
984 pf->stat_offsets_loaded,
985 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800986 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000989 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
990 pf->stat_offsets_loaded,
991 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000992
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000993 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800994 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
995 pf->stat_offsets_loaded,
996 &osd->priority_xoff_rx[i],
997 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000998 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000999 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001000 &osd->priority_xon_rx[i],
1001 &nsd->priority_xon_rx[i]);
1002 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001003 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001004 &osd->priority_xon_tx[i],
1005 &nsd->priority_xon_tx[i]);
1006 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001007 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001008 &osd->priority_xoff_tx[i],
1009 &nsd->priority_xoff_tx[i]);
1010 i40e_stat_update32(hw,
1011 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001012 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001013 &osd->priority_xon_2_xoff[i],
1014 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001015 }
1016
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001017 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1018 I40E_GLPRT_PRC64L(hw->port),
1019 pf->stat_offsets_loaded,
1020 &osd->rx_size_64, &nsd->rx_size_64);
1021 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1022 I40E_GLPRT_PRC127L(hw->port),
1023 pf->stat_offsets_loaded,
1024 &osd->rx_size_127, &nsd->rx_size_127);
1025 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1026 I40E_GLPRT_PRC255L(hw->port),
1027 pf->stat_offsets_loaded,
1028 &osd->rx_size_255, &nsd->rx_size_255);
1029 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1030 I40E_GLPRT_PRC511L(hw->port),
1031 pf->stat_offsets_loaded,
1032 &osd->rx_size_511, &nsd->rx_size_511);
1033 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1034 I40E_GLPRT_PRC1023L(hw->port),
1035 pf->stat_offsets_loaded,
1036 &osd->rx_size_1023, &nsd->rx_size_1023);
1037 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1038 I40E_GLPRT_PRC1522L(hw->port),
1039 pf->stat_offsets_loaded,
1040 &osd->rx_size_1522, &nsd->rx_size_1522);
1041 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1042 I40E_GLPRT_PRC9522L(hw->port),
1043 pf->stat_offsets_loaded,
1044 &osd->rx_size_big, &nsd->rx_size_big);
1045
1046 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1047 I40E_GLPRT_PTC64L(hw->port),
1048 pf->stat_offsets_loaded,
1049 &osd->tx_size_64, &nsd->tx_size_64);
1050 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1051 I40E_GLPRT_PTC127L(hw->port),
1052 pf->stat_offsets_loaded,
1053 &osd->tx_size_127, &nsd->tx_size_127);
1054 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1055 I40E_GLPRT_PTC255L(hw->port),
1056 pf->stat_offsets_loaded,
1057 &osd->tx_size_255, &nsd->tx_size_255);
1058 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1059 I40E_GLPRT_PTC511L(hw->port),
1060 pf->stat_offsets_loaded,
1061 &osd->tx_size_511, &nsd->tx_size_511);
1062 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1063 I40E_GLPRT_PTC1023L(hw->port),
1064 pf->stat_offsets_loaded,
1065 &osd->tx_size_1023, &nsd->tx_size_1023);
1066 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1067 I40E_GLPRT_PTC1522L(hw->port),
1068 pf->stat_offsets_loaded,
1069 &osd->tx_size_1522, &nsd->tx_size_1522);
1070 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1071 I40E_GLPRT_PTC9522L(hw->port),
1072 pf->stat_offsets_loaded,
1073 &osd->tx_size_big, &nsd->tx_size_big);
1074
1075 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1076 pf->stat_offsets_loaded,
1077 &osd->rx_undersize, &nsd->rx_undersize);
1078 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1079 pf->stat_offsets_loaded,
1080 &osd->rx_fragments, &nsd->rx_fragments);
1081 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1082 pf->stat_offsets_loaded,
1083 &osd->rx_oversize, &nsd->rx_oversize);
1084 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1085 pf->stat_offsets_loaded,
1086 &osd->rx_jabber, &nsd->rx_jabber);
1087
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001088 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001089 i40e_stat_update32(hw,
1090 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001091 pf->stat_offsets_loaded,
1092 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001093 i40e_stat_update32(hw,
1094 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001095 pf->stat_offsets_loaded,
1096 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001097 i40e_stat_update32(hw,
1098 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1099 pf->stat_offsets_loaded,
1100 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001101
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001102 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1103 nsd->tx_lpi_status =
1104 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1105 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1106 nsd->rx_lpi_status =
1107 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1108 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1109 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1110 pf->stat_offsets_loaded,
1111 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1112 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1113 pf->stat_offsets_loaded,
1114 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1115
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001116 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1117 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1118 nsd->fd_sb_status = true;
1119 else
1120 nsd->fd_sb_status = false;
1121
1122 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1123 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1124 nsd->fd_atr_status = true;
1125 else
1126 nsd->fd_atr_status = false;
1127
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001128 pf->stat_offsets_loaded = true;
1129}
1130
1131/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001132 * i40e_update_stats - Update the various statistics counters.
1133 * @vsi: the VSI to be updated
1134 *
1135 * Update the various stats for this VSI and its related entities.
1136 **/
1137void i40e_update_stats(struct i40e_vsi *vsi)
1138{
1139 struct i40e_pf *pf = vsi->back;
1140
1141 if (vsi == pf->vsi[pf->lan_vsi])
1142 i40e_update_pf_stats(pf);
1143
1144 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001145#ifdef I40E_FCOE
1146 i40e_update_fcoe_stats(vsi);
1147#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001148}
1149
1150/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001151 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1152 * @vsi: the VSI to be searched
1153 * @macaddr: the MAC address
1154 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001155 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156 * @is_netdev: make sure its a netdev filter, else doesn't matter
1157 *
1158 * Returns ptr to the filter object or NULL
1159 **/
1160static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1161 u8 *macaddr, s16 vlan,
1162 bool is_vf, bool is_netdev)
1163{
1164 struct i40e_mac_filter *f;
1165
1166 if (!vsi || !macaddr)
1167 return NULL;
1168
1169 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1170 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1171 (vlan == f->vlan) &&
1172 (!is_vf || f->is_vf) &&
1173 (!is_netdev || f->is_netdev))
1174 return f;
1175 }
1176 return NULL;
1177}
1178
1179/**
1180 * i40e_find_mac - Find a mac addr in the macvlan filters list
1181 * @vsi: the VSI to be searched
1182 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001183 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001184 * @is_netdev: make sure its a netdev filter, else doesn't matter
1185 *
1186 * Returns the first filter with the provided MAC address or NULL if
1187 * MAC address was not found
1188 **/
1189struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1190 bool is_vf, bool is_netdev)
1191{
1192 struct i40e_mac_filter *f;
1193
1194 if (!vsi || !macaddr)
1195 return NULL;
1196
1197 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1198 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1199 (!is_vf || f->is_vf) &&
1200 (!is_netdev || f->is_netdev))
1201 return f;
1202 }
1203 return NULL;
1204}
1205
1206/**
1207 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1208 * @vsi: the VSI to be searched
1209 *
1210 * Returns true if VSI is in vlan mode or false otherwise
1211 **/
1212bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1213{
1214 struct i40e_mac_filter *f;
1215
1216 /* Only -1 for all the filters denotes not in vlan mode
1217 * so we have to go through all the list in order to make sure
1218 */
1219 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001220 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001221 return true;
1222 }
1223
1224 return false;
1225}
1226
1227/**
1228 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1229 * @vsi: the VSI to be searched
1230 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001231 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001232 * @is_netdev: true if it is a netdev
1233 *
1234 * Goes through all the macvlan filters and adds a
1235 * macvlan filter for each unique vlan that already exists
1236 *
1237 * Returns first filter found on success, else NULL
1238 **/
1239struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1240 bool is_vf, bool is_netdev)
1241{
1242 struct i40e_mac_filter *f;
1243
1244 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001245 if (vsi->info.pvid)
1246 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001247 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1248 is_vf, is_netdev)) {
1249 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001250 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001251 return NULL;
1252 }
1253 }
1254
1255 return list_first_entry_or_null(&vsi->mac_filter_list,
1256 struct i40e_mac_filter, list);
1257}
1258
1259/**
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001260 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1261 * @vsi: the VSI to be searched
1262 * @macaddr: the mac address to be removed
1263 * @is_vf: true if it is a VF
1264 * @is_netdev: true if it is a netdev
1265 *
1266 * Removes a given MAC address from a VSI, regardless of VLAN
1267 *
1268 * Returns 0 for success, or error
1269 **/
1270int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1271 bool is_vf, bool is_netdev)
1272{
1273 struct i40e_mac_filter *f = NULL;
1274 int changed = 0;
1275
1276 WARN(!spin_is_locked(&vsi->mac_filter_list_lock),
1277 "Missing mac_filter_list_lock\n");
1278 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1279 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1280 (is_vf == f->is_vf) &&
1281 (is_netdev == f->is_netdev)) {
1282 f->counter--;
1283 f->changed = true;
1284 changed = 1;
1285 }
1286 }
1287 if (changed) {
1288 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1289 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1290 return 0;
1291 }
1292 return -ENOENT;
1293}
1294
1295/**
Greg Rose8c27d422014-05-22 06:31:56 +00001296 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1297 * @vsi: the PF Main VSI - inappropriate for any other VSI
1298 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001299 *
1300 * Some older firmware configurations set up a default promiscuous VLAN
1301 * filter that needs to be removed.
Greg Rose8c27d422014-05-22 06:31:56 +00001302 **/
Shannon Nelson30650cc2014-07-29 04:01:50 +00001303static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001304{
1305 struct i40e_aqc_remove_macvlan_element_data element;
1306 struct i40e_pf *pf = vsi->back;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001307 i40e_status ret;
Greg Rose8c27d422014-05-22 06:31:56 +00001308
1309 /* Only appropriate for the PF main VSI */
1310 if (vsi->type != I40E_VSI_MAIN)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001311 return -EINVAL;
Greg Rose8c27d422014-05-22 06:31:56 +00001312
Shannon Nelson30650cc2014-07-29 04:01:50 +00001313 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001314 ether_addr_copy(element.mac_addr, macaddr);
1315 element.vlan_tag = 0;
1316 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1317 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001318 ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1319 if (ret)
Shannon Nelson30650cc2014-07-29 04:01:50 +00001320 return -ENOENT;
1321
1322 return 0;
Greg Rose8c27d422014-05-22 06:31:56 +00001323}
1324
1325/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001326 * i40e_add_filter - Add a mac/vlan filter to the VSI
1327 * @vsi: the VSI to be searched
1328 * @macaddr: the MAC address
1329 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001330 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001331 * @is_netdev: make sure its a netdev filter, else doesn't matter
1332 *
1333 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001334 *
1335 * NOTE: This function is expected to be called with mac_filter_list_lock
1336 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001337 **/
1338struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1339 u8 *macaddr, s16 vlan,
1340 bool is_vf, bool is_netdev)
1341{
1342 struct i40e_mac_filter *f;
1343
1344 if (!vsi || !macaddr)
1345 return NULL;
1346
1347 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1348 if (!f) {
1349 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1350 if (!f)
1351 goto add_filter_out;
1352
Greg Rose9a173902014-05-22 06:32:02 +00001353 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001354 f->vlan = vlan;
1355 f->changed = true;
1356
1357 INIT_LIST_HEAD(&f->list);
Kiran Patil04d5a212015-12-09 15:50:23 -08001358 list_add_tail(&f->list, &vsi->mac_filter_list);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001359 }
1360
1361 /* increment counter and add a new flag if needed */
1362 if (is_vf) {
1363 if (!f->is_vf) {
1364 f->is_vf = true;
1365 f->counter++;
1366 }
1367 } else if (is_netdev) {
1368 if (!f->is_netdev) {
1369 f->is_netdev = true;
1370 f->counter++;
1371 }
1372 } else {
1373 f->counter++;
1374 }
1375
1376 /* changed tells sync_filters_subtask to
1377 * push the filter down to the firmware
1378 */
1379 if (f->changed) {
1380 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1381 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1382 }
1383
1384add_filter_out:
1385 return f;
1386}
1387
1388/**
1389 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1390 * @vsi: the VSI to be searched
1391 * @macaddr: the MAC address
1392 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001393 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001394 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001395 *
1396 * NOTE: This function is expected to be called with mac_filter_list_lock
1397 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001398 **/
1399void i40e_del_filter(struct i40e_vsi *vsi,
1400 u8 *macaddr, s16 vlan,
1401 bool is_vf, bool is_netdev)
1402{
1403 struct i40e_mac_filter *f;
1404
1405 if (!vsi || !macaddr)
1406 return;
1407
1408 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1409 if (!f || f->counter == 0)
1410 return;
1411
1412 if (is_vf) {
1413 if (f->is_vf) {
1414 f->is_vf = false;
1415 f->counter--;
1416 }
1417 } else if (is_netdev) {
1418 if (f->is_netdev) {
1419 f->is_netdev = false;
1420 f->counter--;
1421 }
1422 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001423 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001424 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001425
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001426 min_f += (f->is_vf ? 1 : 0);
1427 min_f += (f->is_netdev ? 1 : 0);
1428
1429 if (f->counter > min_f)
1430 f->counter--;
1431 }
1432
1433 /* counter == 0 tells sync_filters_subtask to
1434 * remove the filter from the firmware's list
1435 */
1436 if (f->counter == 0) {
1437 f->changed = true;
1438 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1439 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1440 }
1441}
1442
1443/**
1444 * i40e_set_mac - NDO callback to set mac address
1445 * @netdev: network interface device structure
1446 * @p: pointer to an address structure
1447 *
1448 * Returns 0 on success, negative on failure
1449 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001450#ifdef I40E_FCOE
1451int i40e_set_mac(struct net_device *netdev, void *p)
1452#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001453static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001454#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001455{
1456 struct i40e_netdev_priv *np = netdev_priv(netdev);
1457 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001458 struct i40e_pf *pf = vsi->back;
1459 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001460 struct sockaddr *addr = p;
1461 struct i40e_mac_filter *f;
1462
1463 if (!is_valid_ether_addr(addr->sa_data))
1464 return -EADDRNOTAVAIL;
1465
Shannon Nelson30650cc2014-07-29 04:01:50 +00001466 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1467 netdev_info(netdev, "already using mac address %pM\n",
1468 addr->sa_data);
1469 return 0;
1470 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001471
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001472 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1473 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1474 return -EADDRNOTAVAIL;
1475
Shannon Nelson30650cc2014-07-29 04:01:50 +00001476 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1477 netdev_info(netdev, "returning to hw mac address %pM\n",
1478 hw->mac.addr);
1479 else
1480 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1481
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001482 if (vsi->type == I40E_VSI_MAIN) {
1483 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001484
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001485 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001486 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001487 addr->sa_data, NULL);
1488 if (ret) {
1489 netdev_info(netdev,
1490 "Addr change for Main VSI failed: %d\n",
1491 ret);
1492 return -EADDRNOTAVAIL;
1493 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001494 }
1495
Shannon Nelson30650cc2014-07-29 04:01:50 +00001496 if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
1497 struct i40e_aqc_remove_macvlan_element_data element;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001498
Shannon Nelson30650cc2014-07-29 04:01:50 +00001499 memset(&element, 0, sizeof(element));
1500 ether_addr_copy(element.mac_addr, netdev->dev_addr);
1501 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1502 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1503 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001504 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001505 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1506 false, false);
Kiran Patil21659032015-09-30 14:09:03 -04001507 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson6c8ad1b2014-06-04 01:23:22 +00001508 }
1509
Shannon Nelson30650cc2014-07-29 04:01:50 +00001510 if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
1511 struct i40e_aqc_add_macvlan_element_data element;
1512
1513 memset(&element, 0, sizeof(element));
1514 ether_addr_copy(element.mac_addr, hw->mac.addr);
1515 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
1516 i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1517 } else {
Kiran Patil21659032015-09-30 14:09:03 -04001518 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001519 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
1520 false, false);
1521 if (f)
1522 f->is_laa = true;
Kiran Patil21659032015-09-30 14:09:03 -04001523 spin_unlock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001524 }
1525
Shannon Nelson30650cc2014-07-29 04:01:50 +00001526 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Mitch Williamsea02e902015-11-09 15:35:50 -08001527
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001528 /* schedule our worker thread which will take care of
1529 * applying the new filter changes
1530 */
1531 i40e_service_event_schedule(vsi->back);
1532 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001533}
1534
1535/**
1536 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1537 * @vsi: the VSI being setup
1538 * @ctxt: VSI context structure
1539 * @enabled_tc: Enabled TCs bitmap
1540 * @is_add: True if called before Add VSI
1541 *
1542 * Setup VSI queue mapping for enabled traffic classes.
1543 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001544#ifdef I40E_FCOE
1545void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1546 struct i40e_vsi_context *ctxt,
1547 u8 enabled_tc,
1548 bool is_add)
1549#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001550static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1551 struct i40e_vsi_context *ctxt,
1552 u8 enabled_tc,
1553 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001554#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555{
1556 struct i40e_pf *pf = vsi->back;
1557 u16 sections = 0;
1558 u8 netdev_tc = 0;
1559 u16 numtc = 0;
1560 u16 qcount;
1561 u8 offset;
1562 u16 qmap;
1563 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001564 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001565
1566 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1567 offset = 0;
1568
1569 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1570 /* Find numtc from enabled TC bitmap */
1571 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001572 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001573 numtc++;
1574 }
1575 if (!numtc) {
1576 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1577 numtc = 1;
1578 }
1579 } else {
1580 /* At least TC0 is enabled in case of non-DCB case */
1581 numtc = 1;
1582 }
1583
1584 vsi->tc_config.numtc = numtc;
1585 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001586 /* Number of queues per enabled TC */
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001587 /* In MFP case we can have a much lower count of MSIx
1588 * vectors available and so we need to lower the used
1589 * q count.
1590 */
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001591 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1592 qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
1593 else
1594 qcount = vsi->alloc_queue_pairs;
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001595 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001596 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001597
1598 /* Setup queue offset/count for all TCs for given VSI */
1599 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1600 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001601 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001602 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001603 int pow, num_qps;
1604
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001605 switch (vsi->type) {
1606 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001607 qcount = min_t(int, pf->alloc_rss_size,
1608 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001609 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001610#ifdef I40E_FCOE
1611 case I40E_VSI_FCOE:
1612 qcount = num_tc_qps;
1613 break;
1614#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001615 case I40E_VSI_FDIR:
1616 case I40E_VSI_SRIOV:
1617 case I40E_VSI_VMDQ2:
1618 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001619 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001620 WARN_ON(i != 0);
1621 break;
1622 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001623 vsi->tc_config.tc_info[i].qoffset = offset;
1624 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625
Shannon Nelson1e200e42015-02-27 09:15:24 +00001626 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001627 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001628 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001629 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001630 pow++;
1631 num_qps >>= 1;
1632 }
1633
1634 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1635 qmap =
1636 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1637 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1638
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001639 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001640 } else {
1641 /* TC is not enabled so set the offset to
1642 * default queue and allocate one queue
1643 * for the given TC.
1644 */
1645 vsi->tc_config.tc_info[i].qoffset = 0;
1646 vsi->tc_config.tc_info[i].qcount = 1;
1647 vsi->tc_config.tc_info[i].netdev_tc = 0;
1648
1649 qmap = 0;
1650 }
1651 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1652 }
1653
1654 /* Set actual Tx/Rx queue pairs */
1655 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001656 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1657 if (vsi->req_queue_pairs > 0)
1658 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001659 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001660 vsi->num_queue_pairs = pf->num_lan_msix;
1661 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001662
1663 /* Scheduler section valid can only be set for ADD VSI */
1664 if (is_add) {
1665 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1666
1667 ctxt->info.up_enable_bits = enabled_tc;
1668 }
1669 if (vsi->type == I40E_VSI_SRIOV) {
1670 ctxt->info.mapping_flags |=
1671 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1672 for (i = 0; i < vsi->num_queue_pairs; i++)
1673 ctxt->info.queue_mapping[i] =
1674 cpu_to_le16(vsi->base_queue + i);
1675 } else {
1676 ctxt->info.mapping_flags |=
1677 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1678 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1679 }
1680 ctxt->info.valid_sections |= cpu_to_le16(sections);
1681}
1682
1683/**
1684 * i40e_set_rx_mode - NDO callback to set the netdev filters
1685 * @netdev: network interface device structure
1686 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001687#ifdef I40E_FCOE
1688void i40e_set_rx_mode(struct net_device *netdev)
1689#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001690static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001691#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001692{
1693 struct i40e_netdev_priv *np = netdev_priv(netdev);
1694 struct i40e_mac_filter *f, *ftmp;
1695 struct i40e_vsi *vsi = np->vsi;
1696 struct netdev_hw_addr *uca;
1697 struct netdev_hw_addr *mca;
1698 struct netdev_hw_addr *ha;
1699
Kiran Patil21659032015-09-30 14:09:03 -04001700 spin_lock_bh(&vsi->mac_filter_list_lock);
1701
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001702 /* add addr if not already in the filter list */
1703 netdev_for_each_uc_addr(uca, netdev) {
1704 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1705 if (i40e_is_vsi_in_vlan(vsi))
1706 i40e_put_mac_in_vlan(vsi, uca->addr,
1707 false, true);
1708 else
1709 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1710 false, true);
1711 }
1712 }
1713
1714 netdev_for_each_mc_addr(mca, netdev) {
1715 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1716 if (i40e_is_vsi_in_vlan(vsi))
1717 i40e_put_mac_in_vlan(vsi, mca->addr,
1718 false, true);
1719 else
1720 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1721 false, true);
1722 }
1723 }
1724
1725 /* remove filter if not in netdev list */
1726 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001727
1728 if (!f->is_netdev)
1729 continue;
1730
Shannon Nelson2f41f332015-08-26 15:14:20 -04001731 netdev_for_each_mc_addr(mca, netdev)
1732 if (ether_addr_equal(mca->addr, f->macaddr))
1733 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001734
Shannon Nelson2f41f332015-08-26 15:14:20 -04001735 netdev_for_each_uc_addr(uca, netdev)
1736 if (ether_addr_equal(uca->addr, f->macaddr))
1737 goto bottom_of_search_loop;
1738
1739 for_each_dev_addr(netdev, ha)
1740 if (ether_addr_equal(ha->addr, f->macaddr))
1741 goto bottom_of_search_loop;
1742
1743 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1744 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1745
1746bottom_of_search_loop:
1747 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001748 }
Kiran Patil21659032015-09-30 14:09:03 -04001749 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001750
1751 /* check for other flag changes */
1752 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1753 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1754 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1755 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001756
1757 /* schedule our worker thread which will take care of
1758 * applying the new filter changes
1759 */
1760 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001761}
1762
1763/**
Kiran Patil21659032015-09-30 14:09:03 -04001764 * i40e_mac_filter_entry_clone - Clones a MAC filter entry
1765 * @src: source MAC filter entry to be clones
1766 *
1767 * Returns the pointer to newly cloned MAC filter entry or NULL
1768 * in case of error
1769 **/
1770static struct i40e_mac_filter *i40e_mac_filter_entry_clone(
1771 struct i40e_mac_filter *src)
1772{
1773 struct i40e_mac_filter *f;
1774
1775 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1776 if (!f)
1777 return NULL;
1778 *f = *src;
1779
1780 INIT_LIST_HEAD(&f->list);
1781
1782 return f;
1783}
1784
1785/**
1786 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1787 * @vsi: pointer to vsi struct
1788 * @from: Pointer to list which contains MAC filter entries - changes to
1789 * those entries needs to be undone.
1790 *
1791 * MAC filter entries from list were slated to be removed from device.
1792 **/
1793static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1794 struct list_head *from)
1795{
1796 struct i40e_mac_filter *f, *ftmp;
1797
1798 list_for_each_entry_safe(f, ftmp, from, list) {
1799 f->changed = true;
1800 /* Move the element back into MAC filter list*/
1801 list_move_tail(&f->list, &vsi->mac_filter_list);
1802 }
1803}
1804
1805/**
1806 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1807 * @vsi: pointer to vsi struct
1808 *
1809 * MAC filter entries from list were slated to be added from device.
1810 **/
1811static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi)
1812{
1813 struct i40e_mac_filter *f, *ftmp;
1814
1815 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1816 if (!f->changed && f->counter)
1817 f->changed = true;
1818 }
1819}
1820
1821/**
1822 * i40e_cleanup_add_list - Deletes the element from add list and release
1823 * memory
1824 * @add_list: Pointer to list which contains MAC filter entries
1825 **/
1826static void i40e_cleanup_add_list(struct list_head *add_list)
1827{
1828 struct i40e_mac_filter *f, *ftmp;
1829
1830 list_for_each_entry_safe(f, ftmp, add_list, list) {
1831 list_del(&f->list);
1832 kfree(f);
1833 }
1834}
1835
1836/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001837 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1838 * @vsi: ptr to the VSI
1839 *
1840 * Push any outstanding VSI filter changes through the AdminQ.
1841 *
1842 * Returns 0 or error value
1843 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001844int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001845{
Kiran Patil21659032015-09-30 14:09:03 -04001846 struct list_head tmp_del_list, tmp_add_list;
1847 struct i40e_mac_filter *f, *ftmp, *fclone;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001848 bool promisc_forced_on = false;
1849 bool add_happened = false;
1850 int filter_list_len = 0;
1851 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001852 i40e_status aq_ret = 0;
Kiran Patil21659032015-09-30 14:09:03 -04001853 bool err_cond = false;
Mitch Williamsea02e902015-11-09 15:35:50 -08001854 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001855 struct i40e_pf *pf;
1856 int num_add = 0;
1857 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001858 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001859 u16 cmd_flags;
1860
1861 /* empty array typed pointers, kcalloc later */
1862 struct i40e_aqc_add_macvlan_element_data *add_list;
1863 struct i40e_aqc_remove_macvlan_element_data *del_list;
1864
1865 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1866 usleep_range(1000, 2000);
1867 pf = vsi->back;
1868
1869 if (vsi->netdev) {
1870 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1871 vsi->current_netdev_flags = vsi->netdev->flags;
1872 }
1873
Kiran Patil21659032015-09-30 14:09:03 -04001874 INIT_LIST_HEAD(&tmp_del_list);
1875 INIT_LIST_HEAD(&tmp_add_list);
1876
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001877 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1878 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1879
Kiran Patil21659032015-09-30 14:09:03 -04001880 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001881 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1882 if (!f->changed)
1883 continue;
1884
1885 if (f->counter != 0)
1886 continue;
1887 f->changed = false;
Kiran Patil21659032015-09-30 14:09:03 -04001888
1889 /* Move the element into temporary del_list */
1890 list_move_tail(&f->list, &tmp_del_list);
1891 }
1892
1893 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;
1900
1901 /* Clone MAC filter entry and add into temporary list */
1902 fclone = i40e_mac_filter_entry_clone(f);
1903 if (!fclone) {
1904 err_cond = true;
1905 break;
1906 }
1907 list_add_tail(&fclone->list, &tmp_add_list);
1908 }
1909
1910 /* if failed to clone MAC filter entry - undo */
1911 if (err_cond) {
1912 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1913 i40e_undo_add_filter_entries(vsi);
1914 }
1915 spin_unlock_bh(&vsi->mac_filter_list_lock);
1916
Mitch Williamsea02e902015-11-09 15:35:50 -08001917 if (err_cond) {
Kiran Patil21659032015-09-30 14:09:03 -04001918 i40e_cleanup_add_list(&tmp_add_list);
Mitch Williamsea02e902015-11-09 15:35:50 -08001919 retval = -ENOMEM;
1920 goto out;
1921 }
Kiran Patil21659032015-09-30 14:09:03 -04001922 }
1923
1924 /* Now process 'del_list' outside the lock */
1925 if (!list_empty(&tmp_del_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08001926 int del_list_size;
1927
Kiran Patil21659032015-09-30 14:09:03 -04001928 filter_list_len = pf->hw.aq.asq_buf_size /
1929 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Shannon Nelsonf1199992015-11-19 11:34:23 -08001930 del_list_size = filter_list_len *
1931 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Jesse Brandeburg38c3cec2016-01-04 10:33:05 -08001932 del_list = kzalloc(del_list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001933 if (!del_list) {
1934 i40e_cleanup_add_list(&tmp_add_list);
1935
1936 /* Undo VSI's MAC filter entry element updates */
1937 spin_lock_bh(&vsi->mac_filter_list_lock);
1938 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
1939 i40e_undo_add_filter_entries(vsi);
1940 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001941 retval = -ENOMEM;
1942 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001943 }
1944
1945 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001946 cmd_flags = 0;
1947
1948 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001949 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001950 del_list[num_del].vlan_tag =
1951 cpu_to_le16((u16)(f->vlan ==
1952 I40E_VLAN_ANY ? 0 : f->vlan));
1953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001954 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1955 del_list[num_del].flags = cmd_flags;
1956 num_del++;
1957
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001958 /* flush a full buffer */
1959 if (num_del == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001960 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
1961 vsi->seid,
1962 del_list,
1963 num_del,
1964 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001965 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001966 num_del = 0;
Shannon Nelsonf1199992015-11-19 11:34:23 -08001967 memset(del_list, 0, del_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001968
Mitch Williamsea02e902015-11-09 15:35:50 -08001969 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT) {
1970 retval = -EIO;
Kiran Patil21659032015-09-30 14:09:03 -04001971 dev_err(&pf->pdev->dev,
1972 "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08001973 i40e_stat_str(&pf->hw, aq_ret),
Kiran Patil21659032015-09-30 14:09:03 -04001974 i40e_aq_str(&pf->hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001975 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001976 }
Kiran Patil21659032015-09-30 14:09:03 -04001977 /* Release memory for MAC filter entries which were
1978 * synced up with HW.
1979 */
1980 list_del(&f->list);
1981 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001982 }
Kiran Patil21659032015-09-30 14:09:03 -04001983
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001984 if (num_del) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001985 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
1986 del_list, num_del,
1987 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001988 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989 num_del = 0;
1990
Mitch Williamsea02e902015-11-09 15:35:50 -08001991 if (aq_ret && aq_err != I40E_AQ_RC_ENOENT)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001993 "ignoring delete macvlan error, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08001994 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001995 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001996 }
1997
1998 kfree(del_list);
1999 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002000 }
2001
2002 if (!list_empty(&tmp_add_list)) {
Shannon Nelsonf1199992015-11-19 11:34:23 -08002003 int add_list_size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002004
2005 /* do all the adds now */
2006 filter_list_len = pf->hw.aq.asq_buf_size /
2007 sizeof(struct i40e_aqc_add_macvlan_element_data),
Shannon Nelsonf1199992015-11-19 11:34:23 -08002008 add_list_size = filter_list_len *
2009 sizeof(struct i40e_aqc_add_macvlan_element_data);
Jesse Brandeburg38c3cec2016-01-04 10:33:05 -08002010 add_list = kzalloc(add_list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04002011 if (!add_list) {
2012 /* Purge element from temporary lists */
2013 i40e_cleanup_add_list(&tmp_add_list);
2014
2015 /* Undo add filter entries from VSI MAC filter list */
2016 spin_lock_bh(&vsi->mac_filter_list_lock);
2017 i40e_undo_add_filter_entries(vsi);
2018 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08002019 retval = -ENOMEM;
2020 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04002021 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002022
Kiran Patil21659032015-09-30 14:09:03 -04002023 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002024
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002025 add_happened = true;
2026 cmd_flags = 0;
2027
2028 /* add to add array */
Greg Rose9a173902014-05-22 06:32:02 +00002029 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002030 add_list[num_add].vlan_tag =
2031 cpu_to_le16(
2032 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
2033 add_list[num_add].queue_number = 0;
2034
2035 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2037 num_add++;
2038
2039 /* flush a full buffer */
2040 if (num_add == filter_list_len) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002041 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2042 add_list, num_add,
2043 NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002044 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002045 num_add = 0;
2046
Mitch Williamsea02e902015-11-09 15:35:50 -08002047 if (aq_ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002048 break;
Shannon Nelsonf1199992015-11-19 11:34:23 -08002049 memset(add_list, 0, add_list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002050 }
Kiran Patil21659032015-09-30 14:09:03 -04002051 /* Entries from tmp_add_list were cloned from MAC
2052 * filter list, hence clean those cloned entries
2053 */
2054 list_del(&f->list);
2055 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002056 }
Kiran Patil21659032015-09-30 14:09:03 -04002057
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002058 if (num_add) {
Mitch Williamsea02e902015-11-09 15:35:50 -08002059 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
2060 add_list, num_add, NULL);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002061 aq_err = pf->hw.aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 num_add = 0;
2063 }
2064 kfree(add_list);
2065 add_list = NULL;
2066
Mitch Williamsea02e902015-11-09 15:35:50 -08002067 if (add_happened && aq_ret && aq_err != I40E_AQ_RC_EINVAL) {
2068 retval = i40e_aq_rc_to_posix(aq_ret, aq_err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002069 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002070 "add filter failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002071 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002072 i40e_aq_str(&pf->hw, aq_err));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002073 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
2074 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2075 &vsi->state)) {
2076 promisc_forced_on = true;
2077 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2078 &vsi->state);
2079 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
2080 }
2081 }
2082 }
2083
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002084 /* if the VF is not trusted do not do promisc */
2085 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2086 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2087 goto out;
2088 }
2089
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002090 /* check for changes in promiscuous modes */
2091 if (changed_flags & IFF_ALLMULTI) {
2092 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002093
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002094 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002095 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2096 vsi->seid,
2097 cur_multipromisc,
2098 NULL);
2099 if (aq_ret) {
2100 retval = i40e_aq_rc_to_posix(aq_ret,
2101 pf->hw.aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002103 "set multi promisc failed, err %s aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002104 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002105 i40e_aq_str(&pf->hw,
2106 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002107 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108 }
2109 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
2110 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002112 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2113 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2114 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002115 if ((vsi->type == I40E_VSI_MAIN) &&
2116 (pf->lan_veb != I40E_NO_VEB) &&
2117 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002118 /* set defport ON for Main VSI instead of true promisc
2119 * this way we will get all unicast/multicast and VLAN
2120 * promisc behavior but will not get VF or VMDq traffic
2121 * replicated on the Main VSI.
2122 */
2123 if (pf->cur_promisc != cur_promisc) {
2124 pf->cur_promisc = cur_promisc;
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002125 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002126 }
2127 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002128 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002129 &vsi->back->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002130 vsi->seid,
2131 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002132 if (aq_ret) {
2133 retval =
2134 i40e_aq_rc_to_posix(aq_ret,
2135 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002136 dev_info(&pf->pdev->dev,
2137 "set unicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002138 aq_ret, pf->hw.aq.asq_last_status);
2139 }
2140 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002141 &vsi->back->hw,
2142 vsi->seid,
2143 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002144 if (aq_ret) {
2145 retval =
2146 i40e_aq_rc_to_posix(aq_ret,
2147 pf->hw.aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002148 dev_info(&pf->pdev->dev,
2149 "set multicast promisc failed, err %d, aq_err %d\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002150 aq_ret, pf->hw.aq.asq_last_status);
2151 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002152 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002153 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2154 vsi->seid,
2155 cur_promisc, NULL);
2156 if (aq_ret) {
2157 retval = i40e_aq_rc_to_posix(aq_ret,
2158 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002159 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002160 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williamsea02e902015-11-09 15:35:50 -08002161 i40e_stat_str(&pf->hw, aq_ret),
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002162 i40e_aq_str(&pf->hw,
2163 pf->hw.aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002164 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002165 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002166out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002167 /* if something went wrong then set the changed flag so we try again */
2168 if (retval)
2169 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2170
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002171 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002172 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002173}
2174
2175/**
2176 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2177 * @pf: board private structure
2178 **/
2179static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2180{
2181 int v;
2182
2183 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2184 return;
2185 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2186
Mitch Williams505682c2014-05-20 08:01:37 +00002187 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002188 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002189 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2190 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2191
2192 if (ret) {
2193 /* come back and try again later */
2194 pf->flags |= I40E_FLAG_FILTER_SYNC;
2195 break;
2196 }
2197 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002198 }
2199}
2200
2201/**
2202 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2203 * @netdev: network interface device structure
2204 * @new_mtu: new value for maximum frame size
2205 *
2206 * Returns 0 on success, negative on failure
2207 **/
2208static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2209{
2210 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002211 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002212 struct i40e_vsi *vsi = np->vsi;
2213
2214 /* MTU < 68 is an error and causes problems on some kernels */
2215 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2216 return -EINVAL;
2217
2218 netdev_info(netdev, "changing MTU from %d to %d\n",
2219 netdev->mtu, new_mtu);
2220 netdev->mtu = new_mtu;
2221 if (netif_running(netdev))
2222 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002223 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002224 return 0;
2225}
2226
2227/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002228 * i40e_ioctl - Access the hwtstamp interface
2229 * @netdev: network interface device structure
2230 * @ifr: interface request data
2231 * @cmd: ioctl command
2232 **/
2233int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2234{
2235 struct i40e_netdev_priv *np = netdev_priv(netdev);
2236 struct i40e_pf *pf = np->vsi->back;
2237
2238 switch (cmd) {
2239 case SIOCGHWTSTAMP:
2240 return i40e_ptp_get_ts_config(pf, ifr);
2241 case SIOCSHWTSTAMP:
2242 return i40e_ptp_set_ts_config(pf, ifr);
2243 default:
2244 return -EOPNOTSUPP;
2245 }
2246}
2247
2248/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002249 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2250 * @vsi: the vsi being adjusted
2251 **/
2252void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2253{
2254 struct i40e_vsi_context ctxt;
2255 i40e_status ret;
2256
2257 if ((vsi->info.valid_sections &
2258 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2259 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2260 return; /* already enabled */
2261
2262 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2263 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2264 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2265
2266 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002267 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2269 if (ret) {
2270 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002271 "update vlan stripping failed, err %s aq_err %s\n",
2272 i40e_stat_str(&vsi->back->hw, ret),
2273 i40e_aq_str(&vsi->back->hw,
2274 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002275 }
2276}
2277
2278/**
2279 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2280 * @vsi: the vsi being adjusted
2281 **/
2282void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2283{
2284 struct i40e_vsi_context ctxt;
2285 i40e_status ret;
2286
2287 if ((vsi->info.valid_sections &
2288 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2289 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2290 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2291 return; /* already disabled */
2292
2293 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2294 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2295 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2296
2297 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002298 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2300 if (ret) {
2301 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002302 "update vlan stripping failed, err %s aq_err %s\n",
2303 i40e_stat_str(&vsi->back->hw, ret),
2304 i40e_aq_str(&vsi->back->hw,
2305 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002306 }
2307}
2308
2309/**
2310 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2311 * @netdev: network interface to be adjusted
2312 * @features: netdev features to test if VLAN offload is enabled or not
2313 **/
2314static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2315{
2316 struct i40e_netdev_priv *np = netdev_priv(netdev);
2317 struct i40e_vsi *vsi = np->vsi;
2318
2319 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2320 i40e_vlan_stripping_enable(vsi);
2321 else
2322 i40e_vlan_stripping_disable(vsi);
2323}
2324
2325/**
2326 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2327 * @vsi: the vsi being configured
2328 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2329 **/
2330int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2331{
2332 struct i40e_mac_filter *f, *add_f;
2333 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002334
2335 is_vf = (vsi->type == I40E_VSI_SRIOV);
2336 is_netdev = !!(vsi->netdev);
2337
Kiran Patil21659032015-09-30 14:09:03 -04002338 /* Locked once because all functions invoked below iterates list*/
2339 spin_lock_bh(&vsi->mac_filter_list_lock);
2340
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002341 if (is_netdev) {
2342 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2343 is_vf, is_netdev);
2344 if (!add_f) {
2345 dev_info(&vsi->back->pdev->dev,
2346 "Could not add vlan filter %d for %pM\n",
2347 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002348 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002349 return -ENOMEM;
2350 }
2351 }
2352
2353 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2354 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2355 if (!add_f) {
2356 dev_info(&vsi->back->pdev->dev,
2357 "Could not add vlan filter %d for %pM\n",
2358 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002359 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002360 return -ENOMEM;
2361 }
2362 }
2363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 /* Now if we add a vlan tag, make sure to check if it is the first
2365 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2366 * with 0, so we now accept untagged and specified tagged traffic
2367 * (and not any taged and untagged)
2368 */
2369 if (vid > 0) {
2370 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2371 I40E_VLAN_ANY,
2372 is_vf, is_netdev)) {
2373 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2374 I40E_VLAN_ANY, is_vf, is_netdev);
2375 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2376 is_vf, is_netdev);
2377 if (!add_f) {
2378 dev_info(&vsi->back->pdev->dev,
2379 "Could not add filter 0 for %pM\n",
2380 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002381 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002382 return -ENOMEM;
2383 }
2384 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002385 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002386
Greg Rose8d82a7c2014-01-13 16:13:04 -08002387 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2388 if (vid > 0 && !vsi->info.pvid) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002389 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002390 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2391 is_vf, is_netdev))
2392 continue;
2393 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2394 is_vf, is_netdev);
2395 add_f = i40e_add_filter(vsi, f->macaddr,
2396 0, is_vf, is_netdev);
2397 if (!add_f) {
2398 dev_info(&vsi->back->pdev->dev,
2399 "Could not add filter 0 for %pM\n",
2400 f->macaddr);
2401 spin_unlock_bh(&vsi->mac_filter_list_lock);
2402 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403 }
2404 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405 }
2406
Kiran Patil21659032015-09-30 14:09:03 -04002407 spin_unlock_bh(&vsi->mac_filter_list_lock);
2408
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002409 /* schedule our worker thread which will take care of
2410 * applying the new filter changes
2411 */
2412 i40e_service_event_schedule(vsi->back);
2413 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002414}
2415
2416/**
2417 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2418 * @vsi: the vsi being configured
2419 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002420 *
2421 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002422 **/
2423int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2424{
2425 struct net_device *netdev = vsi->netdev;
2426 struct i40e_mac_filter *f, *add_f;
2427 bool is_vf, is_netdev;
2428 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429
2430 is_vf = (vsi->type == I40E_VSI_SRIOV);
2431 is_netdev = !!(netdev);
2432
Kiran Patil21659032015-09-30 14:09:03 -04002433 /* Locked once because all functions invoked below iterates list */
2434 spin_lock_bh(&vsi->mac_filter_list_lock);
2435
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002436 if (is_netdev)
2437 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2438
2439 list_for_each_entry(f, &vsi->mac_filter_list, list)
2440 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2441
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002442 /* go through all the filters for this VSI and if there is only
2443 * vid == 0 it means there are no other filters, so vid 0 must
2444 * be replaced with -1. This signifies that we should from now
2445 * on accept any traffic (with any tag present, or untagged)
2446 */
2447 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2448 if (is_netdev) {
2449 if (f->vlan &&
2450 ether_addr_equal(netdev->dev_addr, f->macaddr))
2451 filter_count++;
2452 }
2453
2454 if (f->vlan)
2455 filter_count++;
2456 }
2457
2458 if (!filter_count && is_netdev) {
2459 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2460 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2461 is_vf, is_netdev);
2462 if (!f) {
2463 dev_info(&vsi->back->pdev->dev,
2464 "Could not add filter %d for %pM\n",
2465 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002466 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002467 return -ENOMEM;
2468 }
2469 }
2470
2471 if (!filter_count) {
2472 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2473 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2474 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002475 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002476 if (!add_f) {
2477 dev_info(&vsi->back->pdev->dev,
2478 "Could not add filter %d for %pM\n",
2479 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002480 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002481 return -ENOMEM;
2482 }
2483 }
2484 }
2485
Kiran Patil21659032015-09-30 14:09:03 -04002486 spin_unlock_bh(&vsi->mac_filter_list_lock);
2487
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002488 /* schedule our worker thread which will take care of
2489 * applying the new filter changes
2490 */
2491 i40e_service_event_schedule(vsi->back);
2492 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002493}
2494
2495/**
2496 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2497 * @netdev: network interface to be adjusted
2498 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002499 *
2500 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002501 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002502#ifdef I40E_FCOE
2503int i40e_vlan_rx_add_vid(struct net_device *netdev,
2504 __always_unused __be16 proto, u16 vid)
2505#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002506static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2507 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002508#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509{
2510 struct i40e_netdev_priv *np = netdev_priv(netdev);
2511 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002512 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513
2514 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002515 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002517 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
2518
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002519 /* If the network stack called us with vid = 0 then
2520 * it is asking to receive priority tagged packets with
2521 * vlan id 0. Our HW receives them by default when configured
2522 * to receive untagged packets so there is no need to add an
2523 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002525 if (vid)
2526 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002527
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002528 if (!ret && (vid < VLAN_N_VID))
2529 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002530
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002531 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532}
2533
2534/**
2535 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2536 * @netdev: network interface to be adjusted
2537 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002538 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002539 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002540 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002541#ifdef I40E_FCOE
2542int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2543 __always_unused __be16 proto, u16 vid)
2544#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002545static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2546 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002547#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002548{
2549 struct i40e_netdev_priv *np = netdev_priv(netdev);
2550 struct i40e_vsi *vsi = np->vsi;
2551
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002552 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
2553
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002554 /* return code is ignored as there is nothing a user
2555 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002556 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002557 */
2558 i40e_vsi_kill_vlan(vsi, vid);
2559
2560 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002561
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002562 return 0;
2563}
2564
2565/**
2566 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2567 * @vsi: the vsi being brought back up
2568 **/
2569static void i40e_restore_vlan(struct i40e_vsi *vsi)
2570{
2571 u16 vid;
2572
2573 if (!vsi->netdev)
2574 return;
2575
2576 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2577
2578 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2579 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2580 vid);
2581}
2582
2583/**
2584 * i40e_vsi_add_pvid - Add pvid for the VSI
2585 * @vsi: the vsi being adjusted
2586 * @vid: the vlan id to set as a PVID
2587 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002588int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589{
2590 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002591 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592
2593 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2594 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002595 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2596 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002597 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002598
2599 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002600 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002601 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2602 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002603 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002604 "add pvid failed, err %s aq_err %s\n",
2605 i40e_stat_str(&vsi->back->hw, ret),
2606 i40e_aq_str(&vsi->back->hw,
2607 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002608 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002609 }
2610
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002611 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002612}
2613
2614/**
2615 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2616 * @vsi: the vsi being adjusted
2617 *
2618 * Just use the vlan_rx_register() service to put it back to normal
2619 **/
2620void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2621{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002622 i40e_vlan_stripping_disable(vsi);
2623
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002625}
2626
2627/**
2628 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2629 * @vsi: ptr to the VSI
2630 *
2631 * If this function returns with an error, then it's possible one or
2632 * more of the rings is populated (while the rest are not). It is the
2633 * callers duty to clean those orphaned rings.
2634 *
2635 * Return 0 on success, negative on failure
2636 **/
2637static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2638{
2639 int i, err = 0;
2640
2641 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002642 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002643
2644 return err;
2645}
2646
2647/**
2648 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2649 * @vsi: ptr to the VSI
2650 *
2651 * Free VSI's transmit software resources
2652 **/
2653static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2654{
2655 int i;
2656
Greg Rose8e9dca52013-12-18 13:45:53 +00002657 if (!vsi->tx_rings)
2658 return;
2659
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002660 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002661 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002662 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002663}
2664
2665/**
2666 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2667 * @vsi: ptr to the VSI
2668 *
2669 * If this function returns with an error, then it's possible one or
2670 * more of the rings is populated (while the rest are not). It is the
2671 * callers duty to clean those orphaned rings.
2672 *
2673 * Return 0 on success, negative on failure
2674 **/
2675static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2676{
2677 int i, err = 0;
2678
2679 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002680 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002681#ifdef I40E_FCOE
2682 i40e_fcoe_setup_ddp_resources(vsi);
2683#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684 return err;
2685}
2686
2687/**
2688 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2689 * @vsi: ptr to the VSI
2690 *
2691 * Free all receive software resources
2692 **/
2693static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2694{
2695 int i;
2696
Greg Rose8e9dca52013-12-18 13:45:53 +00002697 if (!vsi->rx_rings)
2698 return;
2699
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002700 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002701 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002702 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002703#ifdef I40E_FCOE
2704 i40e_fcoe_free_ddp_resources(vsi);
2705#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002706}
2707
2708/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002709 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2710 * @ring: The Tx ring to configure
2711 *
2712 * This enables/disables XPS for a given Tx descriptor ring
2713 * based on the TCs enabled for the VSI that ring belongs to.
2714 **/
2715static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2716{
2717 struct i40e_vsi *vsi = ring->vsi;
2718 cpumask_var_t mask;
2719
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002720 if (!ring->q_vector || !ring->netdev)
2721 return;
2722
2723 /* Single TC mode enable XPS */
2724 if (vsi->tc_config.numtc <= 1) {
2725 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002726 netif_set_xps_queue(ring->netdev,
2727 &ring->q_vector->affinity_mask,
2728 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002729 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2730 /* Disable XPS to allow selection based on TC */
2731 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2732 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2733 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002734 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002735
2736 /* schedule our worker thread which will take care of
2737 * applying the new filter changes
2738 */
2739 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002740}
2741
2742/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002743 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2744 * @ring: The Tx ring to configure
2745 *
2746 * Configure the Tx descriptor ring in the HMC context.
2747 **/
2748static int i40e_configure_tx_ring(struct i40e_ring *ring)
2749{
2750 struct i40e_vsi *vsi = ring->vsi;
2751 u16 pf_q = vsi->base_queue + ring->queue_index;
2752 struct i40e_hw *hw = &vsi->back->hw;
2753 struct i40e_hmc_obj_txq tx_ctx;
2754 i40e_status err = 0;
2755 u32 qtx_ctl = 0;
2756
2757 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002758 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002759 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2760 ring->atr_count = 0;
2761 } else {
2762 ring->atr_sample_rate = 0;
2763 }
2764
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002765 /* configure XPS */
2766 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002767
2768 /* clear the context structure first */
2769 memset(&tx_ctx, 0, sizeof(tx_ctx));
2770
2771 tx_ctx.new_context = 1;
2772 tx_ctx.base = (ring->dma / 128);
2773 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002774 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2775 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002776#ifdef I40E_FCOE
2777 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2778#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002779 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002780 /* FDIR VSI tx ring can still use RS bit and writebacks */
2781 if (vsi->type != I40E_VSI_FDIR)
2782 tx_ctx.head_wb_ena = 1;
2783 tx_ctx.head_wb_addr = ring->dma +
2784 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002785
2786 /* As part of VSI creation/update, FW allocates certain
2787 * Tx arbitration queue sets for each TC enabled for
2788 * the VSI. The FW returns the handles to these queue
2789 * sets as part of the response buffer to Add VSI,
2790 * Update VSI, etc. AQ commands. It is expected that
2791 * these queue set handles be associated with the Tx
2792 * queues by the driver as part of the TX queue context
2793 * initialization. This has to be done regardless of
2794 * DCB as by default everything is mapped to TC0.
2795 */
2796 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2797 tx_ctx.rdylist_act = 0;
2798
2799 /* clear the context in the HMC */
2800 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2801 if (err) {
2802 dev_info(&vsi->back->pdev->dev,
2803 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2804 ring->queue_index, pf_q, err);
2805 return -ENOMEM;
2806 }
2807
2808 /* set the context in the HMC */
2809 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2810 if (err) {
2811 dev_info(&vsi->back->pdev->dev,
2812 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2813 ring->queue_index, pf_q, err);
2814 return -ENOMEM;
2815 }
2816
2817 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002818 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002819 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002820 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2821 I40E_QTX_CTL_VFVM_INDX_MASK;
2822 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002823 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002824 }
2825
Shannon Nelson13fd977492013-09-28 07:14:19 +00002826 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2827 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002828 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2829 i40e_flush(hw);
2830
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831 /* cache tail off for easier writes later */
2832 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2833
2834 return 0;
2835}
2836
2837/**
2838 * i40e_configure_rx_ring - Configure a receive ring context
2839 * @ring: The Rx ring to configure
2840 *
2841 * Configure the Rx descriptor ring in the HMC context.
2842 **/
2843static int i40e_configure_rx_ring(struct i40e_ring *ring)
2844{
2845 struct i40e_vsi *vsi = ring->vsi;
2846 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2847 u16 pf_q = vsi->base_queue + ring->queue_index;
2848 struct i40e_hw *hw = &vsi->back->hw;
2849 struct i40e_hmc_obj_rxq rx_ctx;
2850 i40e_status err = 0;
2851
2852 ring->state = 0;
2853
2854 /* clear the context structure first */
2855 memset(&rx_ctx, 0, sizeof(rx_ctx));
2856
2857 ring->rx_buf_len = vsi->rx_buf_len;
2858 ring->rx_hdr_len = vsi->rx_hdr_len;
2859
2860 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2861 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2862
2863 rx_ctx.base = (ring->dma / 128);
2864 rx_ctx.qlen = ring->count;
2865
2866 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2867 set_ring_16byte_desc_enabled(ring);
2868 rx_ctx.dsize = 0;
2869 } else {
2870 rx_ctx.dsize = 1;
2871 }
2872
2873 rx_ctx.dtype = vsi->dtype;
2874 if (vsi->dtype) {
2875 set_ring_ps_enabled(ring);
2876 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2877 I40E_RX_SPLIT_IP |
2878 I40E_RX_SPLIT_TCP_UDP |
2879 I40E_RX_SPLIT_SCTP;
2880 } else {
2881 rx_ctx.hsplit_0 = 0;
2882 }
2883
2884 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2885 (chain_len * ring->rx_buf_len));
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002886 if (hw->revision_id == 0)
2887 rx_ctx.lrxqthresh = 0;
2888 else
2889 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002890 rx_ctx.crcstrip = 1;
2891 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002892 /* this controls whether VLAN is stripped from inner headers */
2893 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002894#ifdef I40E_FCOE
2895 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2896#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002897 /* set the prefena field to 1 because the manual says to */
2898 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002899
2900 /* clear the context in the HMC */
2901 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2902 if (err) {
2903 dev_info(&vsi->back->pdev->dev,
2904 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2905 ring->queue_index, pf_q, err);
2906 return -ENOMEM;
2907 }
2908
2909 /* set the context in the HMC */
2910 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2911 if (err) {
2912 dev_info(&vsi->back->pdev->dev,
2913 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2914 ring->queue_index, pf_q, err);
2915 return -ENOMEM;
2916 }
2917
2918 /* cache tail for quicker writes, and clear the reg before use */
2919 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2920 writel(0, ring->tail);
2921
Mitch Williamsa132af22015-01-24 09:58:35 +00002922 if (ring_is_ps_enabled(ring)) {
2923 i40e_alloc_rx_headers(ring);
2924 i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
2925 } else {
2926 i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
2927 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002928
2929 return 0;
2930}
2931
2932/**
2933 * i40e_vsi_configure_tx - Configure the VSI for Tx
2934 * @vsi: VSI structure describing this set of rings and resources
2935 *
2936 * Configure the Tx VSI for operation.
2937 **/
2938static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2939{
2940 int err = 0;
2941 u16 i;
2942
Alexander Duyck9f65e152013-09-28 06:00:58 +00002943 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2944 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002945
2946 return err;
2947}
2948
2949/**
2950 * i40e_vsi_configure_rx - Configure the VSI for Rx
2951 * @vsi: the VSI being configured
2952 *
2953 * Configure the Rx VSI for operation.
2954 **/
2955static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2956{
2957 int err = 0;
2958 u16 i;
2959
2960 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2961 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2962 + ETH_FCS_LEN + VLAN_HLEN;
2963 else
2964 vsi->max_frame = I40E_RXBUFFER_2048;
2965
2966 /* figure out correct receive buffer length */
2967 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2968 I40E_FLAG_RX_PS_ENABLED)) {
2969 case I40E_FLAG_RX_1BUF_ENABLED:
2970 vsi->rx_hdr_len = 0;
2971 vsi->rx_buf_len = vsi->max_frame;
2972 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2973 break;
2974 case I40E_FLAG_RX_PS_ENABLED:
2975 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2976 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2977 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2978 break;
2979 default:
2980 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2981 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2982 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2983 break;
2984 }
2985
Vasu Dev38e00432014-08-01 13:27:03 -07002986#ifdef I40E_FCOE
2987 /* setup rx buffer for FCoE */
2988 if ((vsi->type == I40E_VSI_FCOE) &&
2989 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
2990 vsi->rx_hdr_len = 0;
2991 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2992 vsi->max_frame = I40E_RXBUFFER_3072;
2993 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2994 }
2995
2996#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997 /* round up for the chip's needs */
2998 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002999 BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003001 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003002
3003 /* set up individual rings */
3004 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003005 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006
3007 return err;
3008}
3009
3010/**
3011 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3012 * @vsi: ptr to the VSI
3013 **/
3014static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3015{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003016 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017 u16 qoffset, qcount;
3018 int i, n;
3019
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003020 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3021 /* Reset the TC information */
3022 for (i = 0; i < vsi->num_queue_pairs; i++) {
3023 rx_ring = vsi->rx_rings[i];
3024 tx_ring = vsi->tx_rings[i];
3025 rx_ring->dcb_tc = 0;
3026 tx_ring->dcb_tc = 0;
3027 }
3028 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003029
3030 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003031 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032 continue;
3033
3034 qoffset = vsi->tc_config.tc_info[n].qoffset;
3035 qcount = vsi->tc_config.tc_info[n].qcount;
3036 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003037 rx_ring = vsi->rx_rings[i];
3038 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003039 rx_ring->dcb_tc = n;
3040 tx_ring->dcb_tc = n;
3041 }
3042 }
3043}
3044
3045/**
3046 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3047 * @vsi: ptr to the VSI
3048 **/
3049static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3050{
3051 if (vsi->netdev)
3052 i40e_set_rx_mode(vsi->netdev);
3053}
3054
3055/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003056 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3057 * @vsi: Pointer to the targeted VSI
3058 *
3059 * This function replays the hlist on the hw where all the SB Flow Director
3060 * filters were saved.
3061 **/
3062static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3063{
3064 struct i40e_fdir_filter *filter;
3065 struct i40e_pf *pf = vsi->back;
3066 struct hlist_node *node;
3067
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003068 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3069 return;
3070
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003071 hlist_for_each_entry_safe(filter, node,
3072 &pf->fdir_filter_list, fdir_node) {
3073 i40e_add_del_fdir(vsi, filter, true);
3074 }
3075}
3076
3077/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003078 * i40e_vsi_configure - Set up the VSI for action
3079 * @vsi: the VSI being configured
3080 **/
3081static int i40e_vsi_configure(struct i40e_vsi *vsi)
3082{
3083 int err;
3084
3085 i40e_set_vsi_rx_mode(vsi);
3086 i40e_restore_vlan(vsi);
3087 i40e_vsi_config_dcb_rings(vsi);
3088 err = i40e_vsi_configure_tx(vsi);
3089 if (!err)
3090 err = i40e_vsi_configure_rx(vsi);
3091
3092 return err;
3093}
3094
3095/**
3096 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3097 * @vsi: the VSI being configured
3098 **/
3099static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3100{
3101 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003102 struct i40e_hw *hw = &pf->hw;
3103 u16 vector;
3104 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003105 u32 qp;
3106
3107 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3108 * and PFINT_LNKLSTn registers, e.g.:
3109 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3110 */
3111 qp = vsi->base_queue;
3112 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003113 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003114 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3115
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003116 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003117 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003118 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3119 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3120 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003121 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003122 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3123 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3124 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003125 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3126 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003127
3128 /* Linked list for the queuepairs assigned to this vector */
3129 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3130 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003131 u32 val;
3132
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003133 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3134 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3135 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3136 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3137 (I40E_QUEUE_TYPE_TX
3138 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3139
3140 wr32(hw, I40E_QINT_RQCTL(qp), val);
3141
3142 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3143 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3144 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3145 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3146 (I40E_QUEUE_TYPE_RX
3147 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3148
3149 /* Terminate the linked list */
3150 if (q == (q_vector->num_ringpairs - 1))
3151 val |= (I40E_QUEUE_END_OF_LIST
3152 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3153
3154 wr32(hw, I40E_QINT_TQCTL(qp), val);
3155 qp++;
3156 }
3157 }
3158
3159 i40e_flush(hw);
3160}
3161
3162/**
3163 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3164 * @hw: ptr to the hardware info
3165 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003166static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003167{
Jacob Kellerab437b52014-12-14 01:55:08 +00003168 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003169 u32 val;
3170
3171 /* clear things first */
3172 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3173 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3174
3175 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3176 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3177 I40E_PFINT_ICR0_ENA_GRST_MASK |
3178 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3179 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003180 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3181 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3182 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3183
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003184 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3185 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3186
Jacob Kellerab437b52014-12-14 01:55:08 +00003187 if (pf->flags & I40E_FLAG_PTP)
3188 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3189
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003190 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3191
3192 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003193 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3194 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003195
3196 /* OTHER_ITR_IDX = 0 */
3197 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3198}
3199
3200/**
3201 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3202 * @vsi: the VSI being configured
3203 **/
3204static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3205{
Alexander Duyck493fb302013-09-28 07:01:44 +00003206 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003207 struct i40e_pf *pf = vsi->back;
3208 struct i40e_hw *hw = &pf->hw;
3209 u32 val;
3210
3211 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003212 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003213 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003214 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3215 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003216 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003217 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3218 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3219
Jacob Kellerab437b52014-12-14 01:55:08 +00003220 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003221
3222 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3223 wr32(hw, I40E_PFINT_LNKLST0, 0);
3224
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003225 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3227 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3228 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3229
3230 wr32(hw, I40E_QINT_RQCTL(0), val);
3231
3232 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3233 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3234 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3235
3236 wr32(hw, I40E_QINT_TQCTL(0), val);
3237 i40e_flush(hw);
3238}
3239
3240/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003241 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3242 * @pf: board private structure
3243 **/
3244void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3245{
3246 struct i40e_hw *hw = &pf->hw;
3247
3248 wr32(hw, I40E_PFINT_DYN_CTL0,
3249 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3250 i40e_flush(hw);
3251}
3252
3253/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003254 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3255 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003256 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003257 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003258void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003259{
3260 struct i40e_hw *hw = &pf->hw;
3261 u32 val;
3262
3263 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003264 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3266
3267 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3268 i40e_flush(hw);
3269}
3270
3271/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003272 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3273 * @irq: interrupt number
3274 * @data: pointer to a q_vector
3275 **/
3276static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3277{
3278 struct i40e_q_vector *q_vector = data;
3279
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003280 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003281 return IRQ_HANDLED;
3282
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003283 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003284
3285 return IRQ_HANDLED;
3286}
3287
3288/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003289 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3290 * @vsi: the VSI being configured
3291 * @basename: name for the vector
3292 *
3293 * Allocates MSI-X vectors and requests interrupts from the kernel.
3294 **/
3295static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3296{
3297 int q_vectors = vsi->num_q_vectors;
3298 struct i40e_pf *pf = vsi->back;
3299 int base = vsi->base_vector;
3300 int rx_int_idx = 0;
3301 int tx_int_idx = 0;
3302 int vector, err;
3303
3304 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003305 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003306
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003307 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003308 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3309 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3310 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003311 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003312 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3313 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003314 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3316 "%s-%s-%d", basename, "tx", tx_int_idx++);
3317 } else {
3318 /* skip this unused q_vector */
3319 continue;
3320 }
3321 err = request_irq(pf->msix_entries[base + vector].vector,
3322 vsi->irq_handler,
3323 0,
3324 q_vector->name,
3325 q_vector);
3326 if (err) {
3327 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003328 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 goto free_queue_irqs;
3330 }
3331 /* assign the mask for this irq */
3332 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3333 &q_vector->affinity_mask);
3334 }
3335
Shannon Nelson63741842014-04-23 04:50:16 +00003336 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003337 return 0;
3338
3339free_queue_irqs:
3340 while (vector) {
3341 vector--;
3342 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3343 NULL);
3344 free_irq(pf->msix_entries[base + vector].vector,
3345 &(vsi->q_vectors[vector]));
3346 }
3347 return err;
3348}
3349
3350/**
3351 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3352 * @vsi: the VSI being un-configured
3353 **/
3354static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3355{
3356 struct i40e_pf *pf = vsi->back;
3357 struct i40e_hw *hw = &pf->hw;
3358 int base = vsi->base_vector;
3359 int i;
3360
3361 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003362 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3363 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364 }
3365
3366 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3367 for (i = vsi->base_vector;
3368 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3369 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3370
3371 i40e_flush(hw);
3372 for (i = 0; i < vsi->num_q_vectors; i++)
3373 synchronize_irq(pf->msix_entries[i + base].vector);
3374 } else {
3375 /* Legacy and MSI mode - this stops all interrupt handling */
3376 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3377 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3378 i40e_flush(hw);
3379 synchronize_irq(pf->pdev->irq);
3380 }
3381}
3382
3383/**
3384 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3385 * @vsi: the VSI being configured
3386 **/
3387static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3388{
3389 struct i40e_pf *pf = vsi->back;
3390 int i;
3391
3392 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003393 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003394 i40e_irq_dynamic_enable(vsi, i);
3395 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003396 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003397 }
3398
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003399 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003400 return 0;
3401}
3402
3403/**
3404 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3405 * @pf: board private structure
3406 **/
3407static void i40e_stop_misc_vector(struct i40e_pf *pf)
3408{
3409 /* Disable ICR 0 */
3410 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3411 i40e_flush(&pf->hw);
3412}
3413
3414/**
3415 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3416 * @irq: interrupt number
3417 * @data: pointer to a q_vector
3418 *
3419 * This is the handler used for all MSI/Legacy interrupts, and deals
3420 * with both queue and non-queue interrupts. This is also used in
3421 * MSIX mode to handle the non-queue interrupts.
3422 **/
3423static irqreturn_t i40e_intr(int irq, void *data)
3424{
3425 struct i40e_pf *pf = (struct i40e_pf *)data;
3426 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003427 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003428 u32 icr0, icr0_remaining;
3429 u32 val, ena_mask;
3430
3431 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003432 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003433
Shannon Nelson116a57d2013-09-28 07:13:59 +00003434 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3435 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003436 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003437
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003438 /* if interrupt but no bits showing, must be SWINT */
3439 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3440 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3441 pf->sw_int_count++;
3442
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003443 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3444 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3445 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3446 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3447 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3448 }
3449
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3451 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003452 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3453 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003454
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003455 /* We do not have a way to disarm Queue causes while leaving
3456 * interrupt enabled for all other causes, ideally
3457 * interrupt should be disabled while we are in NAPI but
3458 * this is not a performance path and napi_schedule()
3459 * can deal with rescheduling.
3460 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003461 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003462 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003463 }
3464
3465 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3466 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3467 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003468 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469 }
3470
3471 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3472 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3473 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3474 }
3475
3476 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3477 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3478 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3479 }
3480
3481 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3482 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3483 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3484 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3485 val = rd32(hw, I40E_GLGEN_RSTAT);
3486 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3487 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003488 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003489 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003490 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003491 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003492 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003493 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003494 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003495 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003496 }
3497
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003498 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3499 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3500 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003501 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3502 rd32(hw, I40E_PFHMC_ERRORINFO),
3503 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003504 }
3505
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003506 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3507 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3508
3509 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003510 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003511 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003512 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003513 }
3514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003515 /* If a critical error is pending we have no choice but to reset the
3516 * device.
3517 * Report and mask out any remaining unexpected interrupts.
3518 */
3519 icr0_remaining = icr0 & ena_mask;
3520 if (icr0_remaining) {
3521 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3522 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003523 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003524 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003525 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003526 dev_info(&pf->pdev->dev, "device will be reset\n");
3527 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3528 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003529 }
3530 ena_mask &= ~icr0_remaining;
3531 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003532 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003533
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003534enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 /* re-enable interrupt causes */
3536 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 if (!test_bit(__I40E_DOWN, &pf->state)) {
3538 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003539 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540 }
3541
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003542 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003543}
3544
3545/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003546 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3547 * @tx_ring: tx ring to clean
3548 * @budget: how many cleans we're allowed
3549 *
3550 * Returns true if there's any budget left (e.g. the clean is finished)
3551 **/
3552static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3553{
3554 struct i40e_vsi *vsi = tx_ring->vsi;
3555 u16 i = tx_ring->next_to_clean;
3556 struct i40e_tx_buffer *tx_buf;
3557 struct i40e_tx_desc *tx_desc;
3558
3559 tx_buf = &tx_ring->tx_bi[i];
3560 tx_desc = I40E_TX_DESC(tx_ring, i);
3561 i -= tx_ring->count;
3562
3563 do {
3564 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3565
3566 /* if next_to_watch is not set then there is no work pending */
3567 if (!eop_desc)
3568 break;
3569
3570 /* prevent any other reads prior to eop_desc */
3571 read_barrier_depends();
3572
3573 /* if the descriptor isn't done, no work yet to do */
3574 if (!(eop_desc->cmd_type_offset_bsz &
3575 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3576 break;
3577
3578 /* clear next_to_watch to prevent false hangs */
3579 tx_buf->next_to_watch = NULL;
3580
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003581 tx_desc->buffer_addr = 0;
3582 tx_desc->cmd_type_offset_bsz = 0;
3583 /* move past filter desc */
3584 tx_buf++;
3585 tx_desc++;
3586 i++;
3587 if (unlikely(!i)) {
3588 i -= tx_ring->count;
3589 tx_buf = tx_ring->tx_bi;
3590 tx_desc = I40E_TX_DESC(tx_ring, 0);
3591 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003592 /* unmap skb header data */
3593 dma_unmap_single(tx_ring->dev,
3594 dma_unmap_addr(tx_buf, dma),
3595 dma_unmap_len(tx_buf, len),
3596 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003597 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3598 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003599
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003600 tx_buf->raw_buf = NULL;
3601 tx_buf->tx_flags = 0;
3602 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003603 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003604 tx_desc->buffer_addr = 0;
3605 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003606
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003607 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003608 tx_buf++;
3609 tx_desc++;
3610 i++;
3611 if (unlikely(!i)) {
3612 i -= tx_ring->count;
3613 tx_buf = tx_ring->tx_bi;
3614 tx_desc = I40E_TX_DESC(tx_ring, 0);
3615 }
3616
3617 /* update budget accounting */
3618 budget--;
3619 } while (likely(budget));
3620
3621 i += tx_ring->count;
3622 tx_ring->next_to_clean = i;
3623
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003624 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003625 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003626
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003627 return budget > 0;
3628}
3629
3630/**
3631 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3632 * @irq: interrupt number
3633 * @data: pointer to a q_vector
3634 **/
3635static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3636{
3637 struct i40e_q_vector *q_vector = data;
3638 struct i40e_vsi *vsi;
3639
3640 if (!q_vector->tx.ring)
3641 return IRQ_HANDLED;
3642
3643 vsi = q_vector->tx.ring->vsi;
3644 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3645
3646 return IRQ_HANDLED;
3647}
3648
3649/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003650 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003651 * @vsi: the VSI being configured
3652 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003653 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003654 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003655static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003656{
Alexander Duyck493fb302013-09-28 07:01:44 +00003657 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003658 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3659 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003660
3661 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003662 tx_ring->next = q_vector->tx.ring;
3663 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003665
3666 rx_ring->q_vector = q_vector;
3667 rx_ring->next = q_vector->rx.ring;
3668 q_vector->rx.ring = rx_ring;
3669 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003670}
3671
3672/**
3673 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3674 * @vsi: the VSI being configured
3675 *
3676 * This function maps descriptor rings to the queue-specific vectors
3677 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3678 * one vector per queue pair, but on a constrained vector budget, we
3679 * group the queue pairs as "efficiently" as possible.
3680 **/
3681static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3682{
3683 int qp_remaining = vsi->num_queue_pairs;
3684 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003685 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686 int v_start = 0;
3687 int qp_idx = 0;
3688
3689 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3690 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003691 * It is also important to go through all the vectors available to be
3692 * sure that if we don't use all the vectors, that the remaining vectors
3693 * are cleared. This is especially important when decreasing the
3694 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003695 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003696 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003697 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3698
3699 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3700
3701 q_vector->num_ringpairs = num_ringpairs;
3702
3703 q_vector->rx.count = 0;
3704 q_vector->tx.count = 0;
3705 q_vector->rx.ring = NULL;
3706 q_vector->tx.ring = NULL;
3707
3708 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003709 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003710 qp_idx++;
3711 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003712 }
3713 }
3714}
3715
3716/**
3717 * i40e_vsi_request_irq - Request IRQ from the OS
3718 * @vsi: the VSI being configured
3719 * @basename: name for the vector
3720 **/
3721static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3722{
3723 struct i40e_pf *pf = vsi->back;
3724 int err;
3725
3726 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3727 err = i40e_vsi_request_irq_msix(vsi, basename);
3728 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3729 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003730 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003731 else
3732 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003733 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003734
3735 if (err)
3736 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3737
3738 return err;
3739}
3740
3741#ifdef CONFIG_NET_POLL_CONTROLLER
3742/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003743 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744 * @netdev: network interface device structure
3745 *
3746 * This is used by netconsole to send skbs without having to re-enable
3747 * interrupts. It's not called while the normal interrupt routine is executing.
3748 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003749#ifdef I40E_FCOE
3750void i40e_netpoll(struct net_device *netdev)
3751#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003752static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003753#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003754{
3755 struct i40e_netdev_priv *np = netdev_priv(netdev);
3756 struct i40e_vsi *vsi = np->vsi;
3757 struct i40e_pf *pf = vsi->back;
3758 int i;
3759
3760 /* if interface is down do nothing */
3761 if (test_bit(__I40E_DOWN, &vsi->state))
3762 return;
3763
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003764 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3765 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003766 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003767 } else {
3768 i40e_intr(pf->pdev->irq, netdev);
3769 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770}
3771#endif
3772
3773/**
Neerav Parikh23527302014-06-03 23:50:15 +00003774 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3775 * @pf: the PF being configured
3776 * @pf_q: the PF queue
3777 * @enable: enable or disable state of the queue
3778 *
3779 * This routine will wait for the given Tx queue of the PF to reach the
3780 * enabled or disabled state.
3781 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3782 * multiple retries; else will return 0 in case of success.
3783 **/
3784static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3785{
3786 int i;
3787 u32 tx_reg;
3788
3789 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3790 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3791 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3792 break;
3793
Neerav Parikhf98a2002014-09-13 07:40:44 +00003794 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003795 }
3796 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3797 return -ETIMEDOUT;
3798
3799 return 0;
3800}
3801
3802/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003803 * i40e_vsi_control_tx - Start or stop a VSI's rings
3804 * @vsi: the VSI being configured
3805 * @enable: start or stop the rings
3806 **/
3807static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3808{
3809 struct i40e_pf *pf = vsi->back;
3810 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003811 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003812 u32 tx_reg;
3813
3814 pf_q = vsi->base_queue;
3815 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003816
3817 /* warn the TX unit of coming changes */
3818 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3819 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003820 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003821
Mitch Williams6c5ef622014-02-20 19:29:16 -08003822 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003823 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003824 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3825 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3826 break;
3827 usleep_range(1000, 2000);
3828 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003829 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003830 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003831 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003832
3833 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003834 if (enable) {
3835 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003836 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003837 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003838 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003839 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003840
3841 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003842 /* No waiting for the Tx queue to disable */
3843 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3844 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003845
3846 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003847 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3848 if (ret) {
3849 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003850 "VSI seid %d Tx ring %d %sable timeout\n",
3851 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003852 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003853 }
3854 }
3855
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003856 if (hw->revision_id == 0)
3857 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003858 return ret;
3859}
3860
3861/**
3862 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3863 * @pf: the PF being configured
3864 * @pf_q: the PF queue
3865 * @enable: enable or disable state of the queue
3866 *
3867 * This routine will wait for the given Rx queue of the PF to reach the
3868 * enabled or disabled state.
3869 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3870 * multiple retries; else will return 0 in case of success.
3871 **/
3872static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3873{
3874 int i;
3875 u32 rx_reg;
3876
3877 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3878 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3879 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3880 break;
3881
Neerav Parikhf98a2002014-09-13 07:40:44 +00003882 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003883 }
3884 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3885 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003886
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003887 return 0;
3888}
3889
3890/**
3891 * i40e_vsi_control_rx - Start or stop a VSI's rings
3892 * @vsi: the VSI being configured
3893 * @enable: start or stop the rings
3894 **/
3895static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3896{
3897 struct i40e_pf *pf = vsi->back;
3898 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003899 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003900 u32 rx_reg;
3901
3902 pf_q = vsi->base_queue;
3903 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003904 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003905 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003906 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3907 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3908 break;
3909 usleep_range(1000, 2000);
3910 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003911
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003912 /* Skip if the queue is already in the requested state */
3913 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3914 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003915
3916 /* turn on/off the queue */
3917 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003918 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003919 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003920 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003921 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003922 /* No waiting for the Tx queue to disable */
3923 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3924 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003925
3926 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003927 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3928 if (ret) {
3929 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003930 "VSI seid %d Rx ring %d %sable timeout\n",
3931 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003932 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003933 }
3934 }
3935
Neerav Parikh23527302014-06-03 23:50:15 +00003936 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003937}
3938
3939/**
3940 * i40e_vsi_control_rings - Start or stop a VSI's rings
3941 * @vsi: the VSI being configured
3942 * @enable: start or stop the rings
3943 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003944int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003945{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003946 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947
3948 /* do rx first for enable and last for disable */
3949 if (request) {
3950 ret = i40e_vsi_control_rx(vsi, request);
3951 if (ret)
3952 return ret;
3953 ret = i40e_vsi_control_tx(vsi, request);
3954 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003955 /* Ignore return value, we need to shutdown whatever we can */
3956 i40e_vsi_control_tx(vsi, request);
3957 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003958 }
3959
3960 return ret;
3961}
3962
3963/**
3964 * i40e_vsi_free_irq - Free the irq association with the OS
3965 * @vsi: the VSI being configured
3966 **/
3967static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3968{
3969 struct i40e_pf *pf = vsi->back;
3970 struct i40e_hw *hw = &pf->hw;
3971 int base = vsi->base_vector;
3972 u32 val, qp;
3973 int i;
3974
3975 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3976 if (!vsi->q_vectors)
3977 return;
3978
Shannon Nelson63741842014-04-23 04:50:16 +00003979 if (!vsi->irqs_ready)
3980 return;
3981
3982 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003983 for (i = 0; i < vsi->num_q_vectors; i++) {
3984 u16 vector = i + base;
3985
3986 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00003987 if (!vsi->q_vectors[i] ||
3988 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003989 continue;
3990
3991 /* clear the affinity_mask in the IRQ descriptor */
3992 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3993 NULL);
3994 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00003995 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003996
3997 /* Tear down the interrupt queue link list
3998 *
3999 * We know that they come in pairs and always
4000 * the Rx first, then the Tx. To clear the
4001 * link list, stick the EOL value into the
4002 * next_q field of the registers.
4003 */
4004 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4005 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4006 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4007 val |= I40E_QUEUE_END_OF_LIST
4008 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4009 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4010
4011 while (qp != I40E_QUEUE_END_OF_LIST) {
4012 u32 next;
4013
4014 val = rd32(hw, I40E_QINT_RQCTL(qp));
4015
4016 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4017 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4018 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4019 I40E_QINT_RQCTL_INTEVENT_MASK);
4020
4021 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4022 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4023
4024 wr32(hw, I40E_QINT_RQCTL(qp), val);
4025
4026 val = rd32(hw, I40E_QINT_TQCTL(qp));
4027
4028 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4029 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4030
4031 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4032 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4033 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4034 I40E_QINT_TQCTL_INTEVENT_MASK);
4035
4036 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4037 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4038
4039 wr32(hw, I40E_QINT_TQCTL(qp), val);
4040 qp = next;
4041 }
4042 }
4043 } else {
4044 free_irq(pf->pdev->irq, pf);
4045
4046 val = rd32(hw, I40E_PFINT_LNKLST0);
4047 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4048 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4049 val |= I40E_QUEUE_END_OF_LIST
4050 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4051 wr32(hw, I40E_PFINT_LNKLST0, val);
4052
4053 val = rd32(hw, I40E_QINT_RQCTL(qp));
4054 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4055 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4056 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4057 I40E_QINT_RQCTL_INTEVENT_MASK);
4058
4059 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4060 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4061
4062 wr32(hw, I40E_QINT_RQCTL(qp), val);
4063
4064 val = rd32(hw, I40E_QINT_TQCTL(qp));
4065
4066 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4067 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4068 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4069 I40E_QINT_TQCTL_INTEVENT_MASK);
4070
4071 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4072 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4073
4074 wr32(hw, I40E_QINT_TQCTL(qp), val);
4075 }
4076}
4077
4078/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004079 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4080 * @vsi: the VSI being configured
4081 * @v_idx: Index of vector to be freed
4082 *
4083 * This function frees the memory allocated to the q_vector. In addition if
4084 * NAPI is enabled it will delete any references to the NAPI struct prior
4085 * to freeing the q_vector.
4086 **/
4087static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4088{
4089 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004090 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004091
4092 if (!q_vector)
4093 return;
4094
4095 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004096 i40e_for_each_ring(ring, q_vector->tx)
4097 ring->q_vector = NULL;
4098
4099 i40e_for_each_ring(ring, q_vector->rx)
4100 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004101
4102 /* only VSI w/ an associated netdev is set up w/ NAPI */
4103 if (vsi->netdev)
4104 netif_napi_del(&q_vector->napi);
4105
4106 vsi->q_vectors[v_idx] = NULL;
4107
4108 kfree_rcu(q_vector, rcu);
4109}
4110
4111/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004112 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4113 * @vsi: the VSI being un-configured
4114 *
4115 * This frees the memory allocated to the q_vectors and
4116 * deletes references to the NAPI struct.
4117 **/
4118static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4119{
4120 int v_idx;
4121
Alexander Duyck493fb302013-09-28 07:01:44 +00004122 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4123 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004124}
4125
4126/**
4127 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4128 * @pf: board private structure
4129 **/
4130static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4131{
4132 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4133 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4134 pci_disable_msix(pf->pdev);
4135 kfree(pf->msix_entries);
4136 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004137 kfree(pf->irq_pile);
4138 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004139 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4140 pci_disable_msi(pf->pdev);
4141 }
4142 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4143}
4144
4145/**
4146 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4147 * @pf: board private structure
4148 *
4149 * We go through and clear interrupt specific resources and reset the structure
4150 * to pre-load conditions
4151 **/
4152static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4153{
4154 int i;
4155
Shannon Nelsone1477582015-02-21 06:44:33 +00004156 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004157 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004158 synchronize_irq(pf->msix_entries[0].vector);
4159 free_irq(pf->msix_entries[0].vector, pf);
4160 }
4161
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004162 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4163 I40E_IWARP_IRQ_PILE_ID);
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{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004208 bool reset = false;
4209
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004210 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4211 i40e_down(vsi);
4212 i40e_vsi_free_irq(vsi);
4213 i40e_vsi_free_tx_resources(vsi);
4214 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004215 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004216 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4217 reset = true;
4218 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004219}
4220
4221/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004222 * i40e_quiesce_vsi - Pause a given VSI
4223 * @vsi: the VSI being paused
4224 **/
4225static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4226{
4227 if (test_bit(__I40E_DOWN, &vsi->state))
4228 return;
4229
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004230 /* No need to disable FCoE VSI when Tx suspended */
4231 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4232 vsi->type == I40E_VSI_FCOE) {
4233 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004234 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004235 return;
4236 }
4237
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004238 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004239 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004240 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004241 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004242 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004243}
4244
4245/**
4246 * i40e_unquiesce_vsi - Resume a given VSI
4247 * @vsi: the VSI being resumed
4248 **/
4249static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4250{
4251 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4252 return;
4253
4254 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4255 if (vsi->netdev && netif_running(vsi->netdev))
4256 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4257 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004258 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004259}
4260
4261/**
4262 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4263 * @pf: the PF
4264 **/
4265static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4266{
4267 int v;
4268
Mitch Williams505682c2014-05-20 08:01:37 +00004269 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004270 if (pf->vsi[v])
4271 i40e_quiesce_vsi(pf->vsi[v]);
4272 }
4273}
4274
4275/**
4276 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4277 * @pf: the PF
4278 **/
4279static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4280{
4281 int v;
4282
Mitch Williams505682c2014-05-20 08:01:37 +00004283 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004284 if (pf->vsi[v])
4285 i40e_unquiesce_vsi(pf->vsi[v]);
4286 }
4287}
4288
Neerav Parikh69129dc2014-11-12 00:18:46 +00004289#ifdef CONFIG_I40E_DCB
4290/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004291 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004292 * @vsi: the VSI being configured
4293 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004294 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004295 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004296static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004297{
4298 struct i40e_pf *pf = vsi->back;
4299 int i, pf_q, ret;
4300
4301 pf_q = vsi->base_queue;
4302 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4303 /* Check and wait for the disable status of the queue */
4304 ret = i40e_pf_txq_wait(pf, pf_q, false);
4305 if (ret) {
4306 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004307 "VSI seid %d Tx ring %d disable timeout\n",
4308 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004309 return ret;
4310 }
4311 }
4312
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004313 pf_q = vsi->base_queue;
4314 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4315 /* Check and wait for the disable status of the queue */
4316 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4317 if (ret) {
4318 dev_info(&pf->pdev->dev,
4319 "VSI seid %d Rx ring %d disable timeout\n",
4320 vsi->seid, pf_q);
4321 return ret;
4322 }
4323 }
4324
Neerav Parikh69129dc2014-11-12 00:18:46 +00004325 return 0;
4326}
4327
4328/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004329 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004330 * @pf: the PF
4331 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004332 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004333 * VSIs that are managed by this PF.
4334 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004335static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004336{
4337 int v, ret = 0;
4338
4339 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004340 /* No need to wait for FCoE VSI queues */
4341 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004342 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004343 if (ret)
4344 break;
4345 }
4346 }
4347
4348 return ret;
4349}
4350
4351#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004352
4353/**
4354 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4355 * @q_idx: TX queue number
4356 * @vsi: Pointer to VSI struct
4357 *
4358 * This function checks specified queue for given VSI. Detects hung condition.
4359 * Sets hung bit since it is two step process. Before next run of service task
4360 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4361 * hung condition remain unchanged and during subsequent run, this function
4362 * issues SW interrupt to recover from hung condition.
4363 **/
4364static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4365{
4366 struct i40e_ring *tx_ring = NULL;
4367 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004368 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004369 int i;
4370
4371 pf = vsi->back;
4372
4373 /* now that we have an index, find the tx_ring struct */
4374 for (i = 0; i < vsi->num_queue_pairs; i++) {
4375 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4376 if (q_idx == vsi->tx_rings[i]->queue_index) {
4377 tx_ring = vsi->tx_rings[i];
4378 break;
4379 }
4380 }
4381 }
4382
4383 if (!tx_ring)
4384 return;
4385
4386 /* Read interrupt register */
4387 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4388 val = rd32(&pf->hw,
4389 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4390 tx_ring->vsi->base_vector - 1));
4391 else
4392 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4393
4394 head = i40e_get_head(tx_ring);
4395
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004396 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004397
Kiran Patil9c6c1252015-11-06 15:26:02 -08004398 /* HW is done executing descriptors, updated HEAD write back,
4399 * but SW hasn't processed those descriptors. If interrupt is
4400 * not generated from this point ON, it could result into
4401 * dev_watchdog detecting timeout on those netdev_queue,
4402 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004403 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004404 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004405 /* NAPI Poll didn't run and clear since it was set */
4406 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4407 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004408 netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
4409 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004410 tx_ring->next_to_clean, head,
4411 tx_ring->next_to_use,
4412 readl(tx_ring->tail));
4413 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4414 vsi->seid, q_idx, val);
4415 i40e_force_wb(vsi, tx_ring->q_vector);
4416 } else {
4417 /* First Chance - detected possible hung */
4418 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4419 &tx_ring->q_vector->hung_detected);
4420 }
4421 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004422
4423 /* This is the case where we have interrupts missing,
4424 * so the tx_pending in HW will most likely be 0, but we
4425 * will have tx_pending in SW since the WB happened but the
4426 * interrupt got lost.
4427 */
4428 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4429 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4430 if (napi_reschedule(&tx_ring->q_vector->napi))
4431 tx_ring->tx_stats.tx_lost_interrupt++;
4432 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004433}
4434
4435/**
4436 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4437 * @pf: pointer to PF struct
4438 *
4439 * LAN VSI has netdev and netdev has TX queues. This function is to check
4440 * each of those TX queues if they are hung, trigger recovery by issuing
4441 * SW interrupt.
4442 **/
4443static void i40e_detect_recover_hung(struct i40e_pf *pf)
4444{
4445 struct net_device *netdev;
4446 struct i40e_vsi *vsi;
4447 int i;
4448
4449 /* Only for LAN VSI */
4450 vsi = pf->vsi[pf->lan_vsi];
4451
4452 if (!vsi)
4453 return;
4454
4455 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4456 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4457 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4458 return;
4459
4460 /* Make sure type is MAIN VSI */
4461 if (vsi->type != I40E_VSI_MAIN)
4462 return;
4463
4464 netdev = vsi->netdev;
4465 if (!netdev)
4466 return;
4467
4468 /* Bail out if netif_carrier is not OK */
4469 if (!netif_carrier_ok(netdev))
4470 return;
4471
4472 /* Go thru' TX queues for netdev */
4473 for (i = 0; i < netdev->num_tx_queues; i++) {
4474 struct netdev_queue *q;
4475
4476 q = netdev_get_tx_queue(netdev, i);
4477 if (q)
4478 i40e_detect_recover_hung_queue(i, vsi);
4479 }
4480}
4481
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004482/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004483 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004484 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004485 *
4486 * Get TC map for ISCSI PF type that will include iSCSI TC
4487 * and LAN TC.
4488 **/
4489static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4490{
4491 struct i40e_dcb_app_priority_table app;
4492 struct i40e_hw *hw = &pf->hw;
4493 u8 enabled_tc = 1; /* TC0 is always enabled */
4494 u8 tc, i;
4495 /* Get the iSCSI APP TLV */
4496 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4497
4498 for (i = 0; i < dcbcfg->numapps; i++) {
4499 app = dcbcfg->app[i];
4500 if (app.selector == I40E_APP_SEL_TCPIP &&
4501 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4502 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004503 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004504 break;
4505 }
4506 }
4507
4508 return enabled_tc;
4509}
4510
4511/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004512 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4513 * @dcbcfg: the corresponding DCBx configuration structure
4514 *
4515 * Return the number of TCs from given DCBx configuration
4516 **/
4517static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4518{
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004519 u8 num_tc = 0;
4520 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004521
4522 /* Scan the ETS Config Priority Table to find
4523 * traffic class enabled for a given priority
4524 * and use the traffic class index to get the
4525 * number of traffic classes enabled
4526 */
4527 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4528 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
4529 num_tc = dcbcfg->etscfg.prioritytable[i];
4530 }
4531
4532 /* Traffic class index starts from zero so
4533 * increment to return the actual count
4534 */
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004535 return num_tc + 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004536}
4537
4538/**
4539 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4540 * @dcbcfg: the corresponding DCBx configuration structure
4541 *
4542 * Query the current DCB configuration and return the number of
4543 * traffic classes enabled from the given DCBX config
4544 **/
4545static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4546{
4547 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4548 u8 enabled_tc = 1;
4549 u8 i;
4550
4551 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004552 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004553
4554 return enabled_tc;
4555}
4556
4557/**
4558 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4559 * @pf: PF being queried
4560 *
4561 * Return number of traffic classes enabled for the given PF
4562 **/
4563static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4564{
4565 struct i40e_hw *hw = &pf->hw;
4566 u8 i, enabled_tc;
4567 u8 num_tc = 0;
4568 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4569
4570 /* If DCB is not enabled then always in single TC */
4571 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4572 return 1;
4573
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004574 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004575 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4576 return i40e_dcb_get_num_tc(dcbcfg);
4577
4578 /* MFP mode return count of enabled TCs for this PF */
4579 if (pf->hw.func_caps.iscsi)
4580 enabled_tc = i40e_get_iscsi_tc_map(pf);
4581 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004582 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004583
4584 /* At least have TC0 */
4585 enabled_tc = (enabled_tc ? enabled_tc : 0x1);
4586 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004587 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004588 num_tc++;
4589 }
4590 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004591}
4592
4593/**
4594 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4595 * @pf: PF being queried
4596 *
4597 * Return a bitmap for first enabled traffic class for this PF.
4598 **/
4599static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4600{
4601 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4602 u8 i = 0;
4603
4604 if (!enabled_tc)
4605 return 0x1; /* TC0 */
4606
4607 /* Find the first enabled TC */
4608 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004609 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004610 break;
4611 }
4612
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004613 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004614}
4615
4616/**
4617 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4618 * @pf: PF being queried
4619 *
4620 * Return a bitmap for enabled traffic classes for this PF.
4621 **/
4622static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4623{
4624 /* If DCB is not enabled for this PF then just return default TC */
4625 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4626 return i40e_pf_get_default_tc(pf);
4627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004628 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004629 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4630 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4631
Neerav Parikhfc51de92015-02-24 06:58:53 +00004632 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004633 if (pf->hw.func_caps.iscsi)
4634 return i40e_get_iscsi_tc_map(pf);
4635 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004636 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004637}
4638
4639/**
4640 * i40e_vsi_get_bw_info - Query VSI BW Information
4641 * @vsi: the VSI being queried
4642 *
4643 * Returns 0 on success, negative value on failure
4644 **/
4645static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4646{
4647 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4648 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4649 struct i40e_pf *pf = vsi->back;
4650 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004651 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004652 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004653 int i;
4654
4655 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004656 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4657 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004658 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004659 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4660 i40e_stat_str(&pf->hw, ret),
4661 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004662 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004663 }
4664
4665 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004666 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4667 NULL);
4668 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004669 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004670 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4671 i40e_stat_str(&pf->hw, ret),
4672 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004673 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004674 }
4675
4676 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4677 dev_info(&pf->pdev->dev,
4678 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4679 bw_config.tc_valid_bits,
4680 bw_ets_config.tc_valid_bits);
4681 /* Still continuing */
4682 }
4683
4684 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4685 vsi->bw_max_quanta = bw_config.max_bw;
4686 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4687 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4688 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4689 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4690 vsi->bw_ets_limit_credits[i] =
4691 le16_to_cpu(bw_ets_config.credits[i]);
4692 /* 3 bits out of 4 for each TC */
4693 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4694 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004695
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004696 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004697}
4698
4699/**
4700 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4701 * @vsi: the VSI being configured
4702 * @enabled_tc: TC bitmap
4703 * @bw_credits: BW shared credits per TC
4704 *
4705 * Returns 0 on success, negative value on failure
4706 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004707static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004708 u8 *bw_share)
4709{
4710 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004711 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004712 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004713
4714 bw_data.tc_valid_bits = enabled_tc;
4715 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4716 bw_data.tc_bw_credits[i] = bw_share[i];
4717
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004718 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4719 NULL);
4720 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004721 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004722 "AQ command Config VSI BW allocation per TC failed = %d\n",
4723 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004724 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004725 }
4726
4727 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4728 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4729
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004730 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004731}
4732
4733/**
4734 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4735 * @vsi: the VSI being configured
4736 * @enabled_tc: TC map to be enabled
4737 *
4738 **/
4739static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4740{
4741 struct net_device *netdev = vsi->netdev;
4742 struct i40e_pf *pf = vsi->back;
4743 struct i40e_hw *hw = &pf->hw;
4744 u8 netdev_tc = 0;
4745 int i;
4746 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4747
4748 if (!netdev)
4749 return;
4750
4751 if (!enabled_tc) {
4752 netdev_reset_tc(netdev);
4753 return;
4754 }
4755
4756 /* Set up actual enabled TCs on the VSI */
4757 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4758 return;
4759
4760 /* set per TC queues for the VSI */
4761 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4762 /* Only set TC queues for enabled tcs
4763 *
4764 * e.g. For a VSI that has TC0 and TC3 enabled the
4765 * enabled_tc bitmap would be 0x00001001; the driver
4766 * will set the numtc for netdev as 2 that will be
4767 * referenced by the netdev layer as TC 0 and 1.
4768 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004769 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004770 netdev_set_tc_queue(netdev,
4771 vsi->tc_config.tc_info[i].netdev_tc,
4772 vsi->tc_config.tc_info[i].qcount,
4773 vsi->tc_config.tc_info[i].qoffset);
4774 }
4775
4776 /* Assign UP2TC map for the VSI */
4777 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4778 /* Get the actual TC# for the UP */
4779 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4780 /* Get the mapped netdev TC# for the UP */
4781 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4782 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4783 }
4784}
4785
4786/**
4787 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4788 * @vsi: the VSI being configured
4789 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4790 **/
4791static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4792 struct i40e_vsi_context *ctxt)
4793{
4794 /* copy just the sections touched not the entire info
4795 * since not all sections are valid as returned by
4796 * update vsi params
4797 */
4798 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4799 memcpy(&vsi->info.queue_mapping,
4800 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4801 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4802 sizeof(vsi->info.tc_mapping));
4803}
4804
4805/**
4806 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4807 * @vsi: VSI to be configured
4808 * @enabled_tc: TC bitmap
4809 *
4810 * This configures a particular VSI for TCs that are mapped to the
4811 * given TC bitmap. It uses default bandwidth share for TCs across
4812 * VSIs to configure TC for a particular VSI.
4813 *
4814 * NOTE:
4815 * It is expected that the VSI queues have been quisced before calling
4816 * this function.
4817 **/
4818static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4819{
4820 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4821 struct i40e_vsi_context ctxt;
4822 int ret = 0;
4823 int i;
4824
4825 /* Check if enabled_tc is same as existing or new TCs */
4826 if (vsi->tc_config.enabled_tc == enabled_tc)
4827 return ret;
4828
4829 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4830 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004831 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004832 bw_share[i] = 1;
4833 }
4834
4835 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4836 if (ret) {
4837 dev_info(&vsi->back->pdev->dev,
4838 "Failed configuring TC map %d for VSI %d\n",
4839 enabled_tc, vsi->seid);
4840 goto out;
4841 }
4842
4843 /* Update Queue Pairs Mapping for currently enabled UPs */
4844 ctxt.seid = vsi->seid;
4845 ctxt.pf_num = vsi->back->hw.pf_id;
4846 ctxt.vf_num = 0;
4847 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004848 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4850
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004851 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4852 ctxt.info.valid_sections |=
4853 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4854 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4855 }
4856
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004857 /* Update the VSI after updating the VSI queue-mapping information */
4858 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4859 if (ret) {
4860 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004861 "Update vsi tc config failed, err %s aq_err %s\n",
4862 i40e_stat_str(&vsi->back->hw, ret),
4863 i40e_aq_str(&vsi->back->hw,
4864 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004865 goto out;
4866 }
4867 /* update the local VSI info with updated queue map */
4868 i40e_vsi_update_queue_map(vsi, &ctxt);
4869 vsi->info.valid_sections = 0;
4870
4871 /* Update current VSI BW information */
4872 ret = i40e_vsi_get_bw_info(vsi);
4873 if (ret) {
4874 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004875 "Failed updating vsi bw info, err %s aq_err %s\n",
4876 i40e_stat_str(&vsi->back->hw, ret),
4877 i40e_aq_str(&vsi->back->hw,
4878 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004879 goto out;
4880 }
4881
4882 /* Update the netdev TC setup */
4883 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4884out:
4885 return ret;
4886}
4887
4888/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004889 * i40e_veb_config_tc - Configure TCs for given VEB
4890 * @veb: given VEB
4891 * @enabled_tc: TC bitmap
4892 *
4893 * Configures given TC bitmap for VEB (switching) element
4894 **/
4895int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4896{
4897 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4898 struct i40e_pf *pf = veb->pf;
4899 int ret = 0;
4900 int i;
4901
4902 /* No TCs or already enabled TCs just return */
4903 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4904 return ret;
4905
4906 bw_data.tc_valid_bits = enabled_tc;
4907 /* bw_data.absolute_credits is not set (relative) */
4908
4909 /* Enable ETS TCs with equal BW Share for now */
4910 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004911 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004912 bw_data.tc_bw_share_credits[i] = 1;
4913 }
4914
4915 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4916 &bw_data, NULL);
4917 if (ret) {
4918 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004919 "VEB bw config failed, err %s aq_err %s\n",
4920 i40e_stat_str(&pf->hw, ret),
4921 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004922 goto out;
4923 }
4924
4925 /* Update the BW information */
4926 ret = i40e_veb_get_bw_info(veb);
4927 if (ret) {
4928 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004929 "Failed getting veb bw config, err %s aq_err %s\n",
4930 i40e_stat_str(&pf->hw, ret),
4931 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004932 }
4933
4934out:
4935 return ret;
4936}
4937
4938#ifdef CONFIG_I40E_DCB
4939/**
4940 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4941 * @pf: PF struct
4942 *
4943 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4944 * the caller would've quiesce all the VSIs before calling
4945 * this function
4946 **/
4947static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4948{
4949 u8 tc_map = 0;
4950 int ret;
4951 u8 v;
4952
4953 /* Enable the TCs available on PF to all VEBs */
4954 tc_map = i40e_pf_get_tc_map(pf);
4955 for (v = 0; v < I40E_MAX_VEB; v++) {
4956 if (!pf->veb[v])
4957 continue;
4958 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4959 if (ret) {
4960 dev_info(&pf->pdev->dev,
4961 "Failed configuring TC for VEB seid=%d\n",
4962 pf->veb[v]->seid);
4963 /* Will try to configure as many components */
4964 }
4965 }
4966
4967 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004968 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004969 if (!pf->vsi[v])
4970 continue;
4971
4972 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004973#ifdef I40E_FCOE
4974 * - For FCoE VSI only enable the TC configured
4975 * as per the APP TLV
4976#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004977 * - For all others keep them at TC0 for now
4978 */
4979 if (v == pf->lan_vsi)
4980 tc_map = i40e_pf_get_tc_map(pf);
4981 else
4982 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07004983#ifdef I40E_FCOE
4984 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4985 tc_map = i40e_get_fcoe_tc_map(pf);
4986#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004987
4988 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4989 if (ret) {
4990 dev_info(&pf->pdev->dev,
4991 "Failed configuring TC for VSI seid=%d\n",
4992 pf->vsi[v]->seid);
4993 /* Will try to configure as many components */
4994 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00004995 /* Re-configure VSI vectors based on updated TC map */
4996 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004997 if (pf->vsi[v]->netdev)
4998 i40e_dcbnl_set_all(pf->vsi[v]);
4999 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005000 i40e_notify_client_of_l2_param_changes(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005001 }
5002}
5003
5004/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005005 * i40e_resume_port_tx - Resume port Tx
5006 * @pf: PF struct
5007 *
5008 * Resume a port's Tx and issue a PF reset in case of failure to
5009 * resume.
5010 **/
5011static int i40e_resume_port_tx(struct i40e_pf *pf)
5012{
5013 struct i40e_hw *hw = &pf->hw;
5014 int ret;
5015
5016 ret = i40e_aq_resume_port_tx(hw, NULL);
5017 if (ret) {
5018 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005019 "Resume Port Tx failed, err %s aq_err %s\n",
5020 i40e_stat_str(&pf->hw, ret),
5021 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005022 /* Schedule PF reset to recover */
5023 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5024 i40e_service_event_schedule(pf);
5025 }
5026
5027 return ret;
5028}
5029
5030/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005031 * i40e_init_pf_dcb - Initialize DCB configuration
5032 * @pf: PF being configured
5033 *
5034 * Query the current DCB configuration and cache it
5035 * in the hardware structure
5036 **/
5037static int i40e_init_pf_dcb(struct i40e_pf *pf)
5038{
5039 struct i40e_hw *hw = &pf->hw;
5040 int err = 0;
5041
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005042 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005043 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005044 goto out;
5045
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005046 /* Get the initial DCB configuration */
5047 err = i40e_init_dcb(hw);
5048 if (!err) {
5049 /* Device/Function is not DCBX capable */
5050 if ((!hw->func_caps.dcb) ||
5051 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5052 dev_info(&pf->pdev->dev,
5053 "DCBX offload is not supported or is disabled for this PF.\n");
5054
5055 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5056 goto out;
5057
5058 } else {
5059 /* When status is not DISABLED then DCBX in FW */
5060 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5061 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005062
5063 pf->flags |= I40E_FLAG_DCB_CAPABLE;
5064 /* Enable DCB tagging only when more than one TC */
5065 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5066 pf->flags |= I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005067 dev_dbg(&pf->pdev->dev,
5068 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005069 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005070 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005071 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005072 "Query for DCB configuration failed, err %s aq_err %s\n",
5073 i40e_stat_str(&pf->hw, err),
5074 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005075 }
5076
5077out:
5078 return err;
5079}
5080#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005081#define SPEED_SIZE 14
5082#define FC_SIZE 8
5083/**
5084 * i40e_print_link_message - print link up or down
5085 * @vsi: the VSI for which link needs a message
5086 */
Matt Jaredc156f852015-08-27 11:42:39 -04005087void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005088{
Shannon Nelsona9165492015-09-03 17:19:00 -04005089 char *speed = "Unknown";
5090 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005091
Matt Jaredc156f852015-08-27 11:42:39 -04005092 if (vsi->current_isup == isup)
5093 return;
5094 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005095 if (!isup) {
5096 netdev_info(vsi->netdev, "NIC Link is Down\n");
5097 return;
5098 }
5099
Greg Rose148c2d82014-12-11 07:06:27 +00005100 /* Warn user if link speed on NPAR enabled partition is not at
5101 * least 10GB
5102 */
5103 if (vsi->back->hw.func_caps.npar_enable &&
5104 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5105 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5106 netdev_warn(vsi->netdev,
5107 "The partition detected link speed that is less than 10Gbps\n");
5108
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005109 switch (vsi->back->hw.phy.link_info.link_speed) {
5110 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005111 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005112 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005113 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005114 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005115 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005116 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005117 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005118 break;
5119 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005120 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005121 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005122 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005123 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005124 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005125 default:
5126 break;
5127 }
5128
5129 switch (vsi->back->hw.fc.current_mode) {
5130 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005131 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005132 break;
5133 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005134 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005135 break;
5136 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005137 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005138 break;
5139 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005140 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005141 break;
5142 }
5143
Shannon Nelsona9165492015-09-03 17:19:00 -04005144 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005145 speed, fc);
5146}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005147
5148/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005149 * i40e_up_complete - Finish the last steps of bringing up a connection
5150 * @vsi: the VSI being configured
5151 **/
5152static int i40e_up_complete(struct i40e_vsi *vsi)
5153{
5154 struct i40e_pf *pf = vsi->back;
5155 int err;
5156
5157 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5158 i40e_vsi_configure_msix(vsi);
5159 else
5160 i40e_configure_msi_and_legacy(vsi);
5161
5162 /* start rings */
5163 err = i40e_vsi_control_rings(vsi, true);
5164 if (err)
5165 return err;
5166
5167 clear_bit(__I40E_DOWN, &vsi->state);
5168 i40e_napi_enable_all(vsi);
5169 i40e_vsi_enable_irq(vsi);
5170
5171 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5172 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005173 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005174 netif_tx_start_all_queues(vsi->netdev);
5175 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005176 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005177 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005178 /* need to check for qualified module here*/
5179 if ((pf->hw.phy.link_info.link_info &
5180 I40E_AQ_MEDIA_AVAILABLE) &&
5181 (!(pf->hw.phy.link_info.an_info &
5182 I40E_AQ_QUALIFIED_MODULE)))
5183 netdev_err(vsi->netdev,
5184 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005185 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005186
5187 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005188 if (vsi->type == I40E_VSI_FDIR) {
5189 /* reset fd counters */
5190 pf->fd_add_err = pf->fd_atr_cnt = 0;
5191 if (pf->fd_tcp_rule > 0) {
5192 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005193 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5194 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005195 pf->fd_tcp_rule = 0;
5196 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005197 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005198 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005199
5200 /* On the next run of the service_task, notify any clients of the new
5201 * opened netdev
5202 */
5203 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005204 i40e_service_event_schedule(pf);
5205
5206 return 0;
5207}
5208
5209/**
5210 * i40e_vsi_reinit_locked - Reset the VSI
5211 * @vsi: the VSI being configured
5212 *
5213 * Rebuild the ring structs after some configuration
5214 * has changed, e.g. MTU size.
5215 **/
5216static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5217{
5218 struct i40e_pf *pf = vsi->back;
5219
5220 WARN_ON(in_interrupt());
5221 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5222 usleep_range(1000, 2000);
5223 i40e_down(vsi);
5224
5225 /* Give a VF some time to respond to the reset. The
5226 * two second wait is based upon the watchdog cycle in
5227 * the VF driver.
5228 */
5229 if (vsi->type == I40E_VSI_SRIOV)
5230 msleep(2000);
5231 i40e_up(vsi);
5232 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5233}
5234
5235/**
5236 * i40e_up - Bring the connection back up after being down
5237 * @vsi: the VSI being configured
5238 **/
5239int i40e_up(struct i40e_vsi *vsi)
5240{
5241 int err;
5242
5243 err = i40e_vsi_configure(vsi);
5244 if (!err)
5245 err = i40e_up_complete(vsi);
5246
5247 return err;
5248}
5249
5250/**
5251 * i40e_down - Shutdown the connection processing
5252 * @vsi: the VSI being stopped
5253 **/
5254void i40e_down(struct i40e_vsi *vsi)
5255{
5256 int i;
5257
5258 /* It is assumed that the caller of this function
5259 * sets the vsi->state __I40E_DOWN bit.
5260 */
5261 if (vsi->netdev) {
5262 netif_carrier_off(vsi->netdev);
5263 netif_tx_disable(vsi->netdev);
5264 }
5265 i40e_vsi_disable_irq(vsi);
5266 i40e_vsi_control_rings(vsi, false);
5267 i40e_napi_disable_all(vsi);
5268
5269 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005270 i40e_clean_tx_ring(vsi->tx_rings[i]);
5271 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005272 }
5273}
5274
5275/**
5276 * i40e_setup_tc - configure multiple traffic classes
5277 * @netdev: net device to configure
5278 * @tc: number of traffic classes to enable
5279 **/
5280static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5281{
5282 struct i40e_netdev_priv *np = netdev_priv(netdev);
5283 struct i40e_vsi *vsi = np->vsi;
5284 struct i40e_pf *pf = vsi->back;
5285 u8 enabled_tc = 0;
5286 int ret = -EINVAL;
5287 int i;
5288
5289 /* Check if DCB enabled to continue */
5290 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5291 netdev_info(netdev, "DCB is not enabled for adapter\n");
5292 goto exit;
5293 }
5294
5295 /* Check if MFP enabled */
5296 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5297 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5298 goto exit;
5299 }
5300
5301 /* Check whether tc count is within enabled limit */
5302 if (tc > i40e_pf_get_num_tc(pf)) {
5303 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5304 goto exit;
5305 }
5306
5307 /* Generate TC map for number of tc requested */
5308 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005309 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005310
5311 /* Requesting same TC configuration as already enabled */
5312 if (enabled_tc == vsi->tc_config.enabled_tc)
5313 return 0;
5314
5315 /* Quiesce VSI queues */
5316 i40e_quiesce_vsi(vsi);
5317
5318 /* Configure VSI for enabled TCs */
5319 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5320 if (ret) {
5321 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5322 vsi->seid);
5323 goto exit;
5324 }
5325
5326 /* Unquiesce VSI */
5327 i40e_unquiesce_vsi(vsi);
5328
5329exit:
5330 return ret;
5331}
5332
John Fastabende4c67342016-02-16 21:16:15 -08005333#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005334int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5335 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005336#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005337static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5338 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005339#endif
5340{
John Fastabend16e5cc62016-02-16 21:16:43 -08005341 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005342 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005343 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005344}
5345
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005346/**
5347 * i40e_open - Called when a network interface is made active
5348 * @netdev: network interface device structure
5349 *
5350 * The open entry point is called when a network interface is made
5351 * active by the system (IFF_UP). At this point all resources needed
5352 * for transmit and receive operations are allocated, the interrupt
5353 * handler is registered with the OS, the netdev watchdog subtask is
5354 * enabled, and the stack is notified that the interface is ready.
5355 *
5356 * Returns 0 on success, negative value on failure
5357 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005358int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005359{
5360 struct i40e_netdev_priv *np = netdev_priv(netdev);
5361 struct i40e_vsi *vsi = np->vsi;
5362 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005363 int err;
5364
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005365 /* disallow open during test or if eeprom is broken */
5366 if (test_bit(__I40E_TESTING, &pf->state) ||
5367 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005368 return -EBUSY;
5369
5370 netif_carrier_off(netdev);
5371
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005372 err = i40e_vsi_open(vsi);
5373 if (err)
5374 return err;
5375
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005376 /* configure global TSO hardware offload settings */
5377 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5378 TCP_FLAG_FIN) >> 16);
5379 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5380 TCP_FLAG_FIN |
5381 TCP_FLAG_CWR) >> 16);
5382 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5383
Singhai, Anjali6633d382015-12-03 23:49:31 -08005384#ifdef CONFIG_I40E_VXLAN
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005385 vxlan_get_rx_port(netdev);
5386#endif
Singhai, Anjalicd866602015-12-14 12:21:21 -08005387#ifdef CONFIG_I40E_GENEVE
Anjali Singhaia340c782016-01-06 11:49:28 -08005388 if (pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE)
5389 geneve_get_rx_port(netdev);
Singhai, Anjalicd866602015-12-14 12:21:21 -08005390#endif
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005391
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005392 i40e_notify_client_of_netdev_open(vsi);
5393
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005394 return 0;
5395}
5396
5397/**
5398 * i40e_vsi_open -
5399 * @vsi: the VSI to open
5400 *
5401 * Finish initialization of the VSI.
5402 *
5403 * Returns 0 on success, negative value on failure
5404 **/
5405int i40e_vsi_open(struct i40e_vsi *vsi)
5406{
5407 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005408 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005409 int err;
5410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005411 /* allocate descriptors */
5412 err = i40e_vsi_setup_tx_resources(vsi);
5413 if (err)
5414 goto err_setup_tx;
5415 err = i40e_vsi_setup_rx_resources(vsi);
5416 if (err)
5417 goto err_setup_rx;
5418
5419 err = i40e_vsi_configure(vsi);
5420 if (err)
5421 goto err_setup_rx;
5422
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005423 if (vsi->netdev) {
5424 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5425 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5426 err = i40e_vsi_request_irq(vsi, int_name);
5427 if (err)
5428 goto err_setup_rx;
5429
5430 /* Notify the stack of the actual queue counts. */
5431 err = netif_set_real_num_tx_queues(vsi->netdev,
5432 vsi->num_queue_pairs);
5433 if (err)
5434 goto err_set_queues;
5435
5436 err = netif_set_real_num_rx_queues(vsi->netdev,
5437 vsi->num_queue_pairs);
5438 if (err)
5439 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005440
5441 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005442 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005443 dev_driver_string(&pf->pdev->dev),
5444 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005445 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005446
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005447 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005448 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005449 goto err_setup_rx;
5450 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005452 err = i40e_up_complete(vsi);
5453 if (err)
5454 goto err_up_complete;
5455
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456 return 0;
5457
5458err_up_complete:
5459 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005460err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005461 i40e_vsi_free_irq(vsi);
5462err_setup_rx:
5463 i40e_vsi_free_rx_resources(vsi);
5464err_setup_tx:
5465 i40e_vsi_free_tx_resources(vsi);
5466 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005467 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005468
5469 return err;
5470}
5471
5472/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005473 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005474 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005475 *
5476 * This function destroys the hlist where all the Flow Director
5477 * filters were saved.
5478 **/
5479static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5480{
5481 struct i40e_fdir_filter *filter;
5482 struct hlist_node *node2;
5483
5484 hlist_for_each_entry_safe(filter, node2,
5485 &pf->fdir_filter_list, fdir_node) {
5486 hlist_del(&filter->fdir_node);
5487 kfree(filter);
5488 }
5489 pf->fdir_pf_active_filters = 0;
5490}
5491
5492/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005493 * i40e_close - Disables a network interface
5494 * @netdev: network interface device structure
5495 *
5496 * The close entry point is called when an interface is de-activated
5497 * by the OS. The hardware is still under the driver's control, but
5498 * this netdev interface is disabled.
5499 *
5500 * Returns 0, this is not allowed to fail
5501 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005502int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503{
5504 struct i40e_netdev_priv *np = netdev_priv(netdev);
5505 struct i40e_vsi *vsi = np->vsi;
5506
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005507 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508
5509 return 0;
5510}
5511
5512/**
5513 * i40e_do_reset - Start a PF or Core Reset sequence
5514 * @pf: board private structure
5515 * @reset_flags: which reset is requested
5516 *
5517 * The essential difference in resets is that the PF Reset
5518 * doesn't clear the packet buffers, doesn't reset the PE
5519 * firmware, and doesn't bother the other PFs on the chip.
5520 **/
5521void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5522{
5523 u32 val;
5524
5525 WARN_ON(in_interrupt());
5526
Mitch Williams263fc482014-04-23 04:50:11 +00005527
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005528 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005529 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005530
5531 /* Request a Global Reset
5532 *
5533 * This will start the chip's countdown to the actual full
5534 * chip reset event, and a warning interrupt to be sent
5535 * to all PFs, including the requestor. Our handler
5536 * for the warning interrupt will deal with the shutdown
5537 * and recovery of the switch setup.
5538 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005539 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005540 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5541 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5542 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5543
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005544 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005545
5546 /* Request a Core Reset
5547 *
5548 * Same as Global Reset, except does *not* include the MAC/PHY
5549 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005550 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5552 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5553 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5554 i40e_flush(&pf->hw);
5555
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005556 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557
5558 /* Request a PF Reset
5559 *
5560 * Resets only the PF-specific registers
5561 *
5562 * This goes directly to the tear-down and rebuild of
5563 * the switch, since we need to do all the recovery as
5564 * for the Core Reset.
5565 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005566 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005567 i40e_handle_reset_warning(pf);
5568
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005569 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005570 int v;
5571
5572 /* Find the VSI(s) that requested a re-init */
5573 dev_info(&pf->pdev->dev,
5574 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005575 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005576 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005577
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005578 if (vsi != NULL &&
5579 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5580 i40e_vsi_reinit_locked(pf->vsi[v]);
5581 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5582 }
5583 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005584 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005585 int v;
5586
5587 /* Find the VSI(s) that needs to be brought down */
5588 dev_info(&pf->pdev->dev, "VSI down requested\n");
5589 for (v = 0; v < pf->num_alloc_vsi; v++) {
5590 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005591
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005592 if (vsi != NULL &&
5593 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5594 set_bit(__I40E_DOWN, &vsi->state);
5595 i40e_down(vsi);
5596 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5597 }
5598 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599 } else {
5600 dev_info(&pf->pdev->dev,
5601 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005602 }
5603}
5604
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005605#ifdef CONFIG_I40E_DCB
5606/**
5607 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5608 * @pf: board private structure
5609 * @old_cfg: current DCB config
5610 * @new_cfg: new DCB config
5611 **/
5612bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5613 struct i40e_dcbx_config *old_cfg,
5614 struct i40e_dcbx_config *new_cfg)
5615{
5616 bool need_reconfig = false;
5617
5618 /* Check if ETS configuration has changed */
5619 if (memcmp(&new_cfg->etscfg,
5620 &old_cfg->etscfg,
5621 sizeof(new_cfg->etscfg))) {
5622 /* If Priority Table has changed reconfig is needed */
5623 if (memcmp(&new_cfg->etscfg.prioritytable,
5624 &old_cfg->etscfg.prioritytable,
5625 sizeof(new_cfg->etscfg.prioritytable))) {
5626 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005627 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005628 }
5629
5630 if (memcmp(&new_cfg->etscfg.tcbwtable,
5631 &old_cfg->etscfg.tcbwtable,
5632 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005633 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005634
5635 if (memcmp(&new_cfg->etscfg.tsatable,
5636 &old_cfg->etscfg.tsatable,
5637 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005638 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005639 }
5640
5641 /* Check if PFC configuration has changed */
5642 if (memcmp(&new_cfg->pfc,
5643 &old_cfg->pfc,
5644 sizeof(new_cfg->pfc))) {
5645 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005646 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005647 }
5648
5649 /* Check if APP Table has changed */
5650 if (memcmp(&new_cfg->app,
5651 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005652 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005653 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005654 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005655 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005656
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005657 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005658 return need_reconfig;
5659}
5660
5661/**
5662 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5663 * @pf: board private structure
5664 * @e: event info posted on ARQ
5665 **/
5666static int i40e_handle_lldp_event(struct i40e_pf *pf,
5667 struct i40e_arq_event_info *e)
5668{
5669 struct i40e_aqc_lldp_get_mib *mib =
5670 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5671 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005672 struct i40e_dcbx_config tmp_dcbx_cfg;
5673 bool need_reconfig = false;
5674 int ret = 0;
5675 u8 type;
5676
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005677 /* Not DCB capable or capability disabled */
5678 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
5679 return ret;
5680
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005681 /* Ignore if event is not for Nearest Bridge */
5682 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5683 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005684 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005685 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5686 return ret;
5687
5688 /* Check MIB Type and return if event for Remote MIB update */
5689 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005690 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005691 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005692 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5693 /* Update the remote cached instance and return */
5694 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5695 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5696 &hw->remote_dcbx_config);
5697 goto exit;
5698 }
5699
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005700 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005701 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005702
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005703 /* Reset the old DCBx configuration data */
5704 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005705 /* Get updated DCBX data from firmware */
5706 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005707 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005708 dev_info(&pf->pdev->dev,
5709 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5710 i40e_stat_str(&pf->hw, ret),
5711 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005712 goto exit;
5713 }
5714
5715 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005716 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5717 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005718 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005719 goto exit;
5720 }
5721
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005722 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5723 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005724
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005725 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005726
5727 if (!need_reconfig)
5728 goto exit;
5729
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005730 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005731 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005732 pf->flags |= I40E_FLAG_DCB_ENABLED;
5733 else
5734 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5735
Neerav Parikh69129dc2014-11-12 00:18:46 +00005736 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005737 /* Reconfiguration needed quiesce all VSIs */
5738 i40e_pf_quiesce_all_vsi(pf);
5739
5740 /* Changes in configuration update VEB/VSI */
5741 i40e_dcb_reconfigure(pf);
5742
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005743 ret = i40e_resume_port_tx(pf);
5744
Neerav Parikh69129dc2014-11-12 00:18:46 +00005745 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005746 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005747 if (ret)
5748 goto exit;
5749
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005750 /* Wait for the PF's queues to be disabled */
5751 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005752 if (ret) {
5753 /* Schedule PF reset to recover */
5754 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5755 i40e_service_event_schedule(pf);
5756 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005757 i40e_pf_unquiesce_all_vsi(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005758 }
5759
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005760exit:
5761 return ret;
5762}
5763#endif /* CONFIG_I40E_DCB */
5764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005765/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005766 * i40e_do_reset_safe - Protected reset path for userland calls.
5767 * @pf: board private structure
5768 * @reset_flags: which reset is requested
5769 *
5770 **/
5771void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5772{
5773 rtnl_lock();
5774 i40e_do_reset(pf, reset_flags);
5775 rtnl_unlock();
5776}
5777
5778/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005779 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5780 * @pf: board private structure
5781 * @e: event info posted on ARQ
5782 *
5783 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5784 * and VF queues
5785 **/
5786static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5787 struct i40e_arq_event_info *e)
5788{
5789 struct i40e_aqc_lan_overflow *data =
5790 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5791 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5792 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5793 struct i40e_hw *hw = &pf->hw;
5794 struct i40e_vf *vf;
5795 u16 vf_id;
5796
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005797 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5798 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005799
5800 /* Queue belongs to VF, find the VF and issue VF reset */
5801 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5802 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5803 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5804 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5805 vf_id -= hw->func_caps.vf_base_id;
5806 vf = &pf->vf[vf_id];
5807 i40e_vc_notify_vf_reset(vf);
5808 /* Allow VF to process pending reset notification */
5809 msleep(20);
5810 i40e_reset_vf(vf, false);
5811 }
5812}
5813
5814/**
5815 * i40e_service_event_complete - Finish up the service event
5816 * @pf: board private structure
5817 **/
5818static void i40e_service_event_complete(struct i40e_pf *pf)
5819{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005820 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005821
5822 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005823 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005824 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5825}
5826
5827/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005828 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5829 * @pf: board private structure
5830 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005831u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005832{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005833 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005834
5835 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5836 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5837 return fcnt_prog;
5838}
5839
5840/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005841 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005842 * @pf: board private structure
5843 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005844u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005845{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005846 u32 val, fcnt_prog;
5847
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005848 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5849 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5850 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5851 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5852 return fcnt_prog;
5853}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005854
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005855/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005856 * i40e_get_global_fd_count - Get total FD filters programmed on device
5857 * @pf: board private structure
5858 **/
5859u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5860{
5861 u32 val, fcnt_prog;
5862
5863 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5864 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5865 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5866 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5867 return fcnt_prog;
5868}
5869
5870/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005871 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5872 * @pf: board private structure
5873 **/
5874void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5875{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005876 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005877 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005878 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005879
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005880 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5881 return;
5882
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005883 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5884 * to re-enable
5885 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005886 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005887 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005888 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5889 (pf->fd_add_err == 0) ||
5890 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005891 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5892 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5893 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005894 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5895 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 +00005896 }
5897 }
5898 /* Wait for some more space to be available to turn on ATR */
5899 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5900 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
5901 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
5902 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005903 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5904 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 +00005905 }
5906 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005907
5908 /* if hw had a problem adding a filter, delete it */
5909 if (pf->fd_inv > 0) {
5910 hlist_for_each_entry_safe(filter, node,
5911 &pf->fdir_filter_list, fdir_node) {
5912 if (filter->fd_id == pf->fd_inv) {
5913 hlist_del(&filter->fdir_node);
5914 kfree(filter);
5915 pf->fdir_pf_active_filters--;
5916 }
5917 }
5918 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005919}
5920
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005921#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005922#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005923/**
5924 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5925 * @pf: board private structure
5926 **/
5927static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5928{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005929 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005930 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005931 bool disable_atr = false;
5932 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005933 int reg;
5934
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00005935 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
5936 return;
5937
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005938 if (!time_after(jiffies, pf->fd_flush_timestamp +
5939 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5940 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005941
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005942 /* If the flush is happening too quick and we have mostly SB rules we
5943 * should not re-enable ATR for some time.
5944 */
5945 min_flush_time = pf->fd_flush_timestamp +
5946 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5947 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005948
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005949 if (!(time_after(jiffies, min_flush_time)) &&
5950 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5951 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5952 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5953 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005954 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005955
5956 pf->fd_flush_timestamp = jiffies;
5957 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
5958 /* flush all filters */
5959 wr32(&pf->hw, I40E_PFQF_CTL_1,
5960 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5961 i40e_flush(&pf->hw);
5962 pf->fd_flush_cnt++;
5963 pf->fd_add_err = 0;
5964 do {
5965 /* Check FD flush status every 5-6msec */
5966 usleep_range(5000, 6000);
5967 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5968 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5969 break;
5970 } while (flush_wait_retry--);
5971 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5972 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5973 } else {
5974 /* replay sideband filters */
5975 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5976 if (!disable_atr)
5977 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
5978 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5979 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5980 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5981 }
5982
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005983}
5984
5985/**
5986 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5987 * @pf: board private structure
5988 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005989u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005990{
5991 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5992}
5993
5994/* We can see up to 256 filter programming desc in transit if the filters are
5995 * being applied really fast; before we see the first
5996 * filter miss error on Rx queue 0. Accumulating enough error messages before
5997 * reacting will make sure we don't cause flush too often.
5998 */
5999#define I40E_MAX_FD_PROGRAM_ERROR 256
6000
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006001/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006002 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6003 * @pf: board private structure
6004 **/
6005static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6006{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006007
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006008 /* if interface is down do nothing */
6009 if (test_bit(__I40E_DOWN, &pf->state))
6010 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006011
Akeem G Abodunrin1790ed02014-10-17 03:14:41 +00006012 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
6013 return;
6014
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006015 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006016 i40e_fdir_flush_and_replay(pf);
6017
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006018 i40e_fdir_check_and_reenable(pf);
6019
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006020}
6021
6022/**
6023 * i40e_vsi_link_event - notify VSI of a link event
6024 * @vsi: vsi to be notified
6025 * @link_up: link up or down
6026 **/
6027static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6028{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006029 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006030 return;
6031
6032 switch (vsi->type) {
6033 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006034#ifdef I40E_FCOE
6035 case I40E_VSI_FCOE:
6036#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006037 if (!vsi->netdev || !vsi->netdev_registered)
6038 break;
6039
6040 if (link_up) {
6041 netif_carrier_on(vsi->netdev);
6042 netif_tx_wake_all_queues(vsi->netdev);
6043 } else {
6044 netif_carrier_off(vsi->netdev);
6045 netif_tx_stop_all_queues(vsi->netdev);
6046 }
6047 break;
6048
6049 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006050 case I40E_VSI_VMDQ2:
6051 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006052 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006053 case I40E_VSI_MIRROR:
6054 default:
6055 /* there is no notification for other VSIs */
6056 break;
6057 }
6058}
6059
6060/**
6061 * i40e_veb_link_event - notify elements on the veb of a link event
6062 * @veb: veb to be notified
6063 * @link_up: link up or down
6064 **/
6065static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6066{
6067 struct i40e_pf *pf;
6068 int i;
6069
6070 if (!veb || !veb->pf)
6071 return;
6072 pf = veb->pf;
6073
6074 /* depth first... */
6075 for (i = 0; i < I40E_MAX_VEB; i++)
6076 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6077 i40e_veb_link_event(pf->veb[i], link_up);
6078
6079 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006080 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006081 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6082 i40e_vsi_link_event(pf->vsi[i], link_up);
6083}
6084
6085/**
6086 * i40e_link_event - Update netif_carrier status
6087 * @pf: board private structure
6088 **/
6089static void i40e_link_event(struct i40e_pf *pf)
6090{
Mitch Williams320684c2014-10-17 03:14:43 +00006091 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006092 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006093 i40e_status status;
6094 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006095
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006096 /* save off old link status information */
6097 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6098
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006099 /* set this to force the get_link_status call to refresh state */
6100 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006101
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006102 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006103
6104 status = i40e_get_link_status(&pf->hw, &new_link);
6105 if (status) {
6106 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6107 status);
6108 return;
6109 }
6110
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006111 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6112 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006113
6114 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006115 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006116 (test_bit(__I40E_DOWN, &vsi->state) ||
6117 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006118 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006119
6120 if (!test_bit(__I40E_DOWN, &vsi->state))
6121 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006122
6123 /* Notify the base of the switch tree connected to
6124 * the link. Floating VEBs are not notified.
6125 */
6126 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6127 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6128 else
Mitch Williams320684c2014-10-17 03:14:43 +00006129 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006130
6131 if (pf->vf)
6132 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006133
6134 if (pf->flags & I40E_FLAG_PTP)
6135 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136}
6137
6138/**
Shannon Nelson21536712014-10-25 10:35:25 +00006139 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006140 * @pf: board private structure
6141 **/
6142static void i40e_watchdog_subtask(struct i40e_pf *pf)
6143{
6144 int i;
6145
6146 /* if interface is down do nothing */
6147 if (test_bit(__I40E_DOWN, &pf->state) ||
6148 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6149 return;
6150
Shannon Nelson21536712014-10-25 10:35:25 +00006151 /* make sure we don't do these things too often */
6152 if (time_before(jiffies, (pf->service_timer_previous +
6153 pf->service_timer_period)))
6154 return;
6155 pf->service_timer_previous = jiffies;
6156
Shannon Nelson9ac77262015-08-27 11:42:40 -04006157 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6158 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006159
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006160 /* Update the stats for active netdevs so the network stack
6161 * can look at updated numbers whenever it cares to
6162 */
Mitch Williams505682c2014-05-20 08:01:37 +00006163 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006164 if (pf->vsi[i] && pf->vsi[i]->netdev)
6165 i40e_update_stats(pf->vsi[i]);
6166
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006167 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6168 /* Update the stats for the active switching components */
6169 for (i = 0; i < I40E_MAX_VEB; i++)
6170 if (pf->veb[i])
6171 i40e_update_veb_stats(pf->veb[i]);
6172 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006173
6174 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006175}
6176
6177/**
6178 * i40e_reset_subtask - Set up for resetting the device and driver
6179 * @pf: board private structure
6180 **/
6181static void i40e_reset_subtask(struct i40e_pf *pf)
6182{
6183 u32 reset_flags = 0;
6184
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006185 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006186 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006187 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006188 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6189 }
6190 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006191 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006192 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6193 }
6194 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006195 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006196 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6197 }
6198 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006199 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006200 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6201 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006202 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006203 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006204 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6205 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206
6207 /* If there's a recovery already waiting, it takes
6208 * precedence before starting a new reset sequence.
6209 */
6210 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6211 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006212 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006213 }
6214
6215 /* If we're already down or resetting, just bail */
6216 if (reset_flags &&
6217 !test_bit(__I40E_DOWN, &pf->state) &&
6218 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6219 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006220
6221unlock:
6222 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006223}
6224
6225/**
6226 * i40e_handle_link_event - Handle link event
6227 * @pf: board private structure
6228 * @e: event info posted on ARQ
6229 **/
6230static void i40e_handle_link_event(struct i40e_pf *pf,
6231 struct i40e_arq_event_info *e)
6232{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006233 struct i40e_aqc_get_link_status *status =
6234 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006235
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006236 /* Do a new status request to re-enable LSE reporting
6237 * and load new status information into the hw struct
6238 * This completely ignores any state information
6239 * in the ARQ event info, instead choosing to always
6240 * issue the AQ update link status command.
6241 */
6242 i40e_link_event(pf);
6243
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006244 /* check for unqualified module, if link is down */
6245 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6246 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6247 (!(status->link_info & I40E_AQ_LINK_UP)))
6248 dev_err(&pf->pdev->dev,
6249 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250}
6251
6252/**
6253 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6254 * @pf: board private structure
6255 **/
6256static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6257{
6258 struct i40e_arq_event_info event;
6259 struct i40e_hw *hw = &pf->hw;
6260 u16 pending, i = 0;
6261 i40e_status ret;
6262 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006263 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006264 u32 val;
6265
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006266 /* Do not run clean AQ when PF reset fails */
6267 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6268 return;
6269
Shannon Nelson86df2422014-05-20 08:01:35 +00006270 /* check for error indications */
6271 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6272 oldval = val;
6273 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006274 if (hw->debug_mask & I40E_DEBUG_AQ)
6275 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006276 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6277 }
6278 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006279 if (hw->debug_mask & I40E_DEBUG_AQ)
6280 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006281 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006282 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006283 }
6284 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006285 if (hw->debug_mask & I40E_DEBUG_AQ)
6286 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006287 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6288 }
6289 if (oldval != val)
6290 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6291
6292 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6293 oldval = val;
6294 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006295 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6296 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006297 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6298 }
6299 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006300 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6301 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006302 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6303 }
6304 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006305 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6306 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006307 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6308 }
6309 if (oldval != val)
6310 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6311
Mitch Williams1001dc32014-11-11 20:02:19 +00006312 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6313 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 if (!event.msg_buf)
6315 return;
6316
6317 do {
6318 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006319 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 break;
Mitch Williams56497972014-06-04 08:45:18 +00006321 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006322 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6323 break;
6324 }
6325
6326 opcode = le16_to_cpu(event.desc.opcode);
6327 switch (opcode) {
6328
6329 case i40e_aqc_opc_get_link_status:
6330 i40e_handle_link_event(pf, &event);
6331 break;
6332 case i40e_aqc_opc_send_msg_to_pf:
6333 ret = i40e_vc_process_vf_msg(pf,
6334 le16_to_cpu(event.desc.retval),
6335 le32_to_cpu(event.desc.cookie_high),
6336 le32_to_cpu(event.desc.cookie_low),
6337 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006338 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006339 break;
6340 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006341 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006342#ifdef CONFIG_I40E_DCB
6343 rtnl_lock();
6344 ret = i40e_handle_lldp_event(pf, &event);
6345 rtnl_unlock();
6346#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006347 break;
6348 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006349 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006350 i40e_handle_lan_overflow_event(pf, &event);
6351 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006352 case i40e_aqc_opc_send_msg_to_peer:
6353 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6354 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006355 case i40e_aqc_opc_nvm_erase:
6356 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006357 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006358 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6359 "ARQ NVM operation 0x%04x completed\n",
6360 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006361 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006362 default:
6363 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006364 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006365 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006366 break;
6367 }
6368 } while (pending && (i++ < pf->adminq_work_limit));
6369
6370 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6371 /* re-enable Admin queue interrupt cause */
6372 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6373 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6374 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6375 i40e_flush(hw);
6376
6377 kfree(event.msg_buf);
6378}
6379
6380/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006381 * i40e_verify_eeprom - make sure eeprom is good to use
6382 * @pf: board private structure
6383 **/
6384static void i40e_verify_eeprom(struct i40e_pf *pf)
6385{
6386 int err;
6387
6388 err = i40e_diag_eeprom_test(&pf->hw);
6389 if (err) {
6390 /* retry in case of garbage read */
6391 err = i40e_diag_eeprom_test(&pf->hw);
6392 if (err) {
6393 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6394 err);
6395 set_bit(__I40E_BAD_EEPROM, &pf->state);
6396 }
6397 }
6398
6399 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6400 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6401 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6402 }
6403}
6404
6405/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006406 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006407 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006408 *
6409 * enable switch loop back or die - no point in a return value
6410 **/
6411static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6412{
6413 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6414 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006415 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006416
6417 ctxt.seid = pf->main_vsi_seid;
6418 ctxt.pf_num = pf->hw.pf_id;
6419 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006420 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6421 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006422 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006423 "couldn't get PF vsi config, err %s aq_err %s\n",
6424 i40e_stat_str(&pf->hw, ret),
6425 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006426 return;
6427 }
6428 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6429 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6430 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6431
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006432 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6433 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006434 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006435 "update vsi switch failed, err %s aq_err %s\n",
6436 i40e_stat_str(&pf->hw, ret),
6437 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006438 }
6439}
6440
6441/**
6442 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006443 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006444 *
6445 * disable switch loop back or die - no point in a return value
6446 **/
6447static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6448{
6449 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6450 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006451 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006452
6453 ctxt.seid = pf->main_vsi_seid;
6454 ctxt.pf_num = pf->hw.pf_id;
6455 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006456 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6457 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006458 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006459 "couldn't get PF vsi config, err %s aq_err %s\n",
6460 i40e_stat_str(&pf->hw, ret),
6461 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006462 return;
6463 }
6464 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6465 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6466 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6467
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006468 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6469 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006470 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006471 "update vsi switch failed, err %s aq_err %s\n",
6472 i40e_stat_str(&pf->hw, ret),
6473 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006474 }
6475}
6476
6477/**
Neerav Parikh51616012015-02-06 08:52:14 +00006478 * i40e_config_bridge_mode - Configure the HW bridge mode
6479 * @veb: pointer to the bridge instance
6480 *
6481 * Configure the loop back mode for the LAN VSI that is downlink to the
6482 * specified HW bridge instance. It is expected this function is called
6483 * when a new HW bridge is instantiated.
6484 **/
6485static void i40e_config_bridge_mode(struct i40e_veb *veb)
6486{
6487 struct i40e_pf *pf = veb->pf;
6488
Shannon Nelson6dec1012015-09-28 14:12:30 -04006489 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6490 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6491 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006492 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6493 i40e_disable_pf_switch_lb(pf);
6494 else
6495 i40e_enable_pf_switch_lb(pf);
6496}
6497
6498/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006499 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6500 * @veb: pointer to the VEB instance
6501 *
6502 * This is a recursive function that first builds the attached VSIs then
6503 * recurses in to build the next layer of VEB. We track the connections
6504 * through our own index numbers because the seid's from the HW could
6505 * change across the reset.
6506 **/
6507static int i40e_reconstitute_veb(struct i40e_veb *veb)
6508{
6509 struct i40e_vsi *ctl_vsi = NULL;
6510 struct i40e_pf *pf = veb->pf;
6511 int v, veb_idx;
6512 int ret;
6513
6514 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006515 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 if (pf->vsi[v] &&
6517 pf->vsi[v]->veb_idx == veb->idx &&
6518 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6519 ctl_vsi = pf->vsi[v];
6520 break;
6521 }
6522 }
6523 if (!ctl_vsi) {
6524 dev_info(&pf->pdev->dev,
6525 "missing owner VSI for veb_idx %d\n", veb->idx);
6526 ret = -ENOENT;
6527 goto end_reconstitute;
6528 }
6529 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6530 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6531 ret = i40e_add_vsi(ctl_vsi);
6532 if (ret) {
6533 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006534 "rebuild of veb_idx %d owner VSI failed: %d\n",
6535 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006536 goto end_reconstitute;
6537 }
6538 i40e_vsi_reset_stats(ctl_vsi);
6539
6540 /* create the VEB in the switch and move the VSI onto the VEB */
6541 ret = i40e_add_veb(veb, ctl_vsi);
6542 if (ret)
6543 goto end_reconstitute;
6544
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006545 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6546 veb->bridge_mode = BRIDGE_MODE_VEB;
6547 else
6548 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006549 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006551 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006552 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006553 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6554 continue;
6555
6556 if (pf->vsi[v]->veb_idx == veb->idx) {
6557 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006558
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006559 vsi->uplink_seid = veb->seid;
6560 ret = i40e_add_vsi(vsi);
6561 if (ret) {
6562 dev_info(&pf->pdev->dev,
6563 "rebuild of vsi_idx %d failed: %d\n",
6564 v, ret);
6565 goto end_reconstitute;
6566 }
6567 i40e_vsi_reset_stats(vsi);
6568 }
6569 }
6570
6571 /* create any VEBs attached to this VEB - RECURSION */
6572 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6573 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6574 pf->veb[veb_idx]->uplink_seid = veb->seid;
6575 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6576 if (ret)
6577 break;
6578 }
6579 }
6580
6581end_reconstitute:
6582 return ret;
6583}
6584
6585/**
6586 * i40e_get_capabilities - get info about the HW
6587 * @pf: the PF struct
6588 **/
6589static int i40e_get_capabilities(struct i40e_pf *pf)
6590{
6591 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6592 u16 data_size;
6593 int buf_len;
6594 int err;
6595
6596 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6597 do {
6598 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6599 if (!cap_buf)
6600 return -ENOMEM;
6601
6602 /* this loads the data into the hw struct for us */
6603 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6604 &data_size,
6605 i40e_aqc_opc_list_func_capabilities,
6606 NULL);
6607 /* data loaded, buffer no longer needed */
6608 kfree(cap_buf);
6609
6610 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6611 /* retry with a larger buffer */
6612 buf_len = data_size;
6613 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6614 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006615 "capability discovery failed, err %s aq_err %s\n",
6616 i40e_stat_str(&pf->hw, err),
6617 i40e_aq_str(&pf->hw,
6618 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006619 return -ENODEV;
6620 }
6621 } while (err);
6622
6623 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6624 dev_info(&pf->pdev->dev,
6625 "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",
6626 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6627 pf->hw.func_caps.num_msix_vectors,
6628 pf->hw.func_caps.num_msix_vectors_vf,
6629 pf->hw.func_caps.fd_filters_guaranteed,
6630 pf->hw.func_caps.fd_filters_best_effort,
6631 pf->hw.func_caps.num_tx_qp,
6632 pf->hw.func_caps.num_vsis);
6633
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006634#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6635 + pf->hw.func_caps.num_vfs)
6636 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6637 dev_info(&pf->pdev->dev,
6638 "got num_vsis %d, setting num_vsis to %d\n",
6639 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6640 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6641 }
6642
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006643 return 0;
6644}
6645
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006646static int i40e_vsi_clear(struct i40e_vsi *vsi);
6647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006648/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006649 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006650 * @pf: board private structure
6651 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006652static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653{
6654 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006655 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006656
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006657 /* quick workaround for an NVM issue that leaves a critical register
6658 * uninitialized
6659 */
6660 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6661 static const u32 hkey[] = {
6662 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6663 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6664 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6665 0x95b3a76d};
6666
6667 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6668 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6669 }
6670
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006671 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006672 return;
6673
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006674 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006675 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006676 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006677 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006678 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006679 break;
6680 }
6681 }
6682
6683 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006684 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006685 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6686 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006687 if (!vsi) {
6688 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006689 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6690 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006692 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006693
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006694 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695}
6696
6697/**
6698 * i40e_fdir_teardown - release the Flow Director resources
6699 * @pf: board private structure
6700 **/
6701static void i40e_fdir_teardown(struct i40e_pf *pf)
6702{
6703 int i;
6704
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006705 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006706 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006707 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6708 i40e_vsi_release(pf->vsi[i]);
6709 break;
6710 }
6711 }
6712}
6713
6714/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006715 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006716 * @pf: board private structure
6717 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006718 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006719 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006720static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006721{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006722 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006723 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006724 u32 v;
6725
6726 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6727 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006728 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006729 if (i40e_check_asq_alive(&pf->hw))
6730 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006731
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006732 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006733
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006734 /* quiesce the VSIs and their queues that are not already DOWN */
6735 i40e_pf_quiesce_all_vsi(pf);
6736
Mitch Williams505682c2014-05-20 08:01:37 +00006737 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006738 if (pf->vsi[v])
6739 pf->vsi[v]->seid = 0;
6740 }
6741
6742 i40e_shutdown_adminq(&pf->hw);
6743
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006744 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006745 if (hw->hmc.hmc_obj) {
6746 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006747 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006748 dev_warn(&pf->pdev->dev,
6749 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006750 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006751}
6752
6753/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006754 * i40e_send_version - update firmware with driver version
6755 * @pf: PF struct
6756 */
6757static void i40e_send_version(struct i40e_pf *pf)
6758{
6759 struct i40e_driver_version dv;
6760
6761 dv.major_version = DRV_VERSION_MAJOR;
6762 dv.minor_version = DRV_VERSION_MINOR;
6763 dv.build_version = DRV_VERSION_BUILD;
6764 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006765 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006766 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6767}
6768
6769/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006770 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006771 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006772 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006773 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006774static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006775{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006776 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006777 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006778 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006779 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006780 u32 v;
6781
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006782 /* Now we wait for GRST to settle out.
6783 * We don't have to delete the VEBs or VSIs from the hw switch
6784 * because the reset will make them disappear.
6785 */
6786 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006787 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006788 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006789 set_bit(__I40E_RESET_FAILED, &pf->state);
6790 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006791 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006792 pf->pfr_count++;
6793
6794 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006795 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006796 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006797
6798 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6799 ret = i40e_init_adminq(&pf->hw);
6800 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006801 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6802 i40e_stat_str(&pf->hw, ret),
6803 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006804 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006805 }
6806
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006807 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006808 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006809 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006810
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006811 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006812 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006813 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006814 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6817 hw->func_caps.num_rx_qp,
6818 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6819 if (ret) {
6820 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6821 goto end_core_reset;
6822 }
6823 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6824 if (ret) {
6825 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6826 goto end_core_reset;
6827 }
6828
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006829#ifdef CONFIG_I40E_DCB
6830 ret = i40e_init_pf_dcb(pf);
6831 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006832 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6833 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6834 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006835 }
6836#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006837#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006838 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006839
Vasu Dev38e00432014-08-01 13:27:03 -07006840#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006841 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006842 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006843 if (ret)
6844 goto end_core_reset;
6845
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006846 /* The driver only wants link up/down and module qualification
6847 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006848 */
6849 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006850 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006851 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006852 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006853 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006854 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6855 i40e_stat_str(&pf->hw, ret),
6856 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006857
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006858 /* make sure our flow control settings are restored */
6859 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6860 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006861 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6862 i40e_stat_str(&pf->hw, ret),
6863 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006864
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006865 /* Rebuild the VSIs and VEBs that existed before reset.
6866 * They are still in our local switch element arrays, so only
6867 * need to rebuild the switch model in the HW.
6868 *
6869 * If there were VEBs but the reconstitution failed, we'll try
6870 * try to recover minimal use by getting the basic PF VSI working.
6871 */
6872 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006873 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006874 /* find the one VEB connected to the MAC, and find orphans */
6875 for (v = 0; v < I40E_MAX_VEB; v++) {
6876 if (!pf->veb[v])
6877 continue;
6878
6879 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6880 pf->veb[v]->uplink_seid == 0) {
6881 ret = i40e_reconstitute_veb(pf->veb[v]);
6882
6883 if (!ret)
6884 continue;
6885
6886 /* If Main VEB failed, we're in deep doodoo,
6887 * so give up rebuilding the switch and set up
6888 * for minimal rebuild of PF VSI.
6889 * If orphan failed, we'll report the error
6890 * but try to keep going.
6891 */
6892 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6893 dev_info(&pf->pdev->dev,
6894 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6895 ret);
6896 pf->vsi[pf->lan_vsi]->uplink_seid
6897 = pf->mac_seid;
6898 break;
6899 } else if (pf->veb[v]->uplink_seid == 0) {
6900 dev_info(&pf->pdev->dev,
6901 "rebuild of orphan VEB failed: %d\n",
6902 ret);
6903 }
6904 }
6905 }
6906 }
6907
6908 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006909 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006910 /* no VEB, so rebuild only the Main VSI */
6911 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6912 if (ret) {
6913 dev_info(&pf->pdev->dev,
6914 "rebuild of Main VSI failed: %d\n", ret);
6915 goto end_core_reset;
6916 }
6917 }
6918
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006919 /* Reconfigure hardware for allowing smaller MSS in the case
6920 * of TSO, so that we avoid the MDD being fired and causing
6921 * a reset in the case of small MSS+TSO.
6922 */
6923#define I40E_REG_MSS 0x000E64DC
6924#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6925#define I40E_64BYTE_MSS 0x400000
6926 val = rd32(hw, I40E_REG_MSS);
6927 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6928 val &= ~I40E_REG_MSS_MIN_MASK;
6929 val |= I40E_64BYTE_MSS;
6930 wr32(hw, I40E_REG_MSS, val);
6931 }
6932
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006933 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006934 msleep(75);
6935 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6936 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006937 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6938 i40e_stat_str(&pf->hw, ret),
6939 i40e_aq_str(&pf->hw,
6940 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006941 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006942 /* reinit the misc interrupt */
6943 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6944 ret = i40e_setup_misc_vector(pf);
6945
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006946 /* Add a filter to drop all Flow control frames from any VSI from being
6947 * transmitted. By doing so we stop a malicious VF from sending out
6948 * PAUSE or PFC frames and potentially controlling traffic for other
6949 * PF/VF VSIs.
6950 * The FW can still send Flow control frames if enabled.
6951 */
6952 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6953 pf->main_vsi_seid);
6954
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006955 /* restart the VSIs that were rebuilt and running before the reset */
6956 i40e_pf_unquiesce_all_vsi(pf);
6957
Mitch Williams69f64b22014-02-13 03:48:46 -08006958 if (pf->num_alloc_vfs) {
6959 for (v = 0; v < pf->num_alloc_vfs; v++)
6960 i40e_reset_vf(&pf->vf[v], true);
6961 }
6962
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006963 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006964 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006965
6966end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006967 clear_bit(__I40E_RESET_FAILED, &pf->state);
6968clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006969 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6970}
6971
6972/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006973 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006974 * @pf: board private structure
6975 *
6976 * Close up the VFs and other things in prep for a Core Reset,
6977 * then get ready to rebuild the world.
6978 **/
6979static void i40e_handle_reset_warning(struct i40e_pf *pf)
6980{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006981 i40e_prep_for_reset(pf);
6982 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006983}
6984
6985/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006986 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006987 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006988 *
6989 * Called from the MDD irq handler to identify possibly malicious vfs
6990 **/
6991static void i40e_handle_mdd_event(struct i40e_pf *pf)
6992{
6993 struct i40e_hw *hw = &pf->hw;
6994 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006995 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006996 struct i40e_vf *vf;
6997 u32 reg;
6998 int i;
6999
7000 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7001 return;
7002
7003 /* find what triggered the MDD event */
7004 reg = rd32(hw, I40E_GL_MDET_TX);
7005 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007006 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7007 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007008 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007009 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007010 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007011 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007012 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7013 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7014 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007015 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007016 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 +00007017 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007018 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7019 mdd_detected = true;
7020 }
7021 reg = rd32(hw, I40E_GL_MDET_RX);
7022 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007023 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7024 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007025 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007026 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007027 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7028 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7029 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007030 if (netif_msg_rx_err(pf))
7031 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7032 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007033 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7034 mdd_detected = true;
7035 }
7036
Neerav Parikhdf430b12014-06-04 01:23:15 +00007037 if (mdd_detected) {
7038 reg = rd32(hw, I40E_PF_MDET_TX);
7039 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7040 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007041 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007042 pf_mdd_detected = true;
7043 }
7044 reg = rd32(hw, I40E_PF_MDET_RX);
7045 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7046 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007047 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007048 pf_mdd_detected = true;
7049 }
7050 /* Queue belongs to the PF, initiate a reset */
7051 if (pf_mdd_detected) {
7052 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7053 i40e_service_event_schedule(pf);
7054 }
7055 }
7056
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007057 /* see if one of the VFs needs its hand slapped */
7058 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7059 vf = &(pf->vf[i]);
7060 reg = rd32(hw, I40E_VP_MDET_TX(i));
7061 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7062 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7063 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007064 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7065 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007066 }
7067
7068 reg = rd32(hw, I40E_VP_MDET_RX(i));
7069 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7070 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7071 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007072 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7073 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074 }
7075
7076 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7077 dev_info(&pf->pdev->dev,
7078 "Too many MDD events on VF %d, disabled\n", i);
7079 dev_info(&pf->pdev->dev,
7080 "Use PF Control I/F to re-enable the VF\n");
7081 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7082 }
7083 }
7084
7085 /* re-enable mdd interrupt cause */
7086 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7087 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7088 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7089 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7090 i40e_flush(hw);
7091}
7092
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007093/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007094 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007095 * @pf: board private structure
7096 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007097static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007098{
Singhai, Anjali6a899022015-12-14 12:21:18 -08007099#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007100 struct i40e_hw *hw = &pf->hw;
7101 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007102 __be16 port;
7103 int i;
7104
Singhai, Anjali6a899022015-12-14 12:21:18 -08007105 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007106 return;
7107
Singhai, Anjali6a899022015-12-14 12:21:18 -08007108 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007109
7110 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007111 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7112 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7113 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007114 if (port)
7115 ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
Singhai, Anjali6a899022015-12-14 12:21:18 -08007116 pf->udp_ports[i].type,
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007117 NULL, NULL);
7118 else
7119 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007120
7121 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007122 dev_dbg(&pf->pdev->dev,
7123 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7124 pf->udp_ports[i].type ? "vxlan" : "geneve",
7125 port ? "add" : "delete",
7126 ntohs(port), i,
7127 i40e_stat_str(&pf->hw, ret),
7128 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007129 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007130 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007131 }
7132 }
7133 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08007134#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007135}
7136
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007137/**
7138 * i40e_service_task - Run the driver's async subtasks
7139 * @work: pointer to work_struct containing our data
7140 **/
7141static void i40e_service_task(struct work_struct *work)
7142{
7143 struct i40e_pf *pf = container_of(work,
7144 struct i40e_pf,
7145 service_task);
7146 unsigned long start_time = jiffies;
7147
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007148 /* don't bother with service tasks if a reset is in progress */
7149 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7150 i40e_service_event_complete(pf);
7151 return;
7152 }
7153
Kiran Patilb03a8c12015-09-24 18:13:15 -04007154 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007155 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007156 i40e_reset_subtask(pf);
7157 i40e_handle_mdd_event(pf);
7158 i40e_vc_process_vflr_event(pf);
7159 i40e_watchdog_subtask(pf);
7160 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007161 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007162 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007163 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007164 i40e_clean_adminq_subtask(pf);
7165
7166 i40e_service_event_complete(pf);
7167
7168 /* If the tasks have taken longer than one timer cycle or there
7169 * is more work to be done, reschedule the service task now
7170 * rather than wait for the timer to tick again.
7171 */
7172 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7173 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7174 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7175 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7176 i40e_service_event_schedule(pf);
7177}
7178
7179/**
7180 * i40e_service_timer - timer callback
7181 * @data: pointer to PF struct
7182 **/
7183static void i40e_service_timer(unsigned long data)
7184{
7185 struct i40e_pf *pf = (struct i40e_pf *)data;
7186
7187 mod_timer(&pf->service_timer,
7188 round_jiffies(jiffies + pf->service_timer_period));
7189 i40e_service_event_schedule(pf);
7190}
7191
7192/**
7193 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7194 * @vsi: the VSI being configured
7195 **/
7196static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7197{
7198 struct i40e_pf *pf = vsi->back;
7199
7200 switch (vsi->type) {
7201 case I40E_VSI_MAIN:
7202 vsi->alloc_queue_pairs = pf->num_lan_qps;
7203 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7204 I40E_REQ_DESCRIPTOR_MULTIPLE);
7205 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7206 vsi->num_q_vectors = pf->num_lan_msix;
7207 else
7208 vsi->num_q_vectors = 1;
7209
7210 break;
7211
7212 case I40E_VSI_FDIR:
7213 vsi->alloc_queue_pairs = 1;
7214 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7215 I40E_REQ_DESCRIPTOR_MULTIPLE);
7216 vsi->num_q_vectors = 1;
7217 break;
7218
7219 case I40E_VSI_VMDQ2:
7220 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7221 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7222 I40E_REQ_DESCRIPTOR_MULTIPLE);
7223 vsi->num_q_vectors = pf->num_vmdq_msix;
7224 break;
7225
7226 case I40E_VSI_SRIOV:
7227 vsi->alloc_queue_pairs = pf->num_vf_qps;
7228 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7229 I40E_REQ_DESCRIPTOR_MULTIPLE);
7230 break;
7231
Vasu Dev38e00432014-08-01 13:27:03 -07007232#ifdef I40E_FCOE
7233 case I40E_VSI_FCOE:
7234 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7235 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7236 I40E_REQ_DESCRIPTOR_MULTIPLE);
7237 vsi->num_q_vectors = pf->num_fcoe_msix;
7238 break;
7239
7240#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007241 default:
7242 WARN_ON(1);
7243 return -ENODATA;
7244 }
7245
7246 return 0;
7247}
7248
7249/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007250 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7251 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007252 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007253 *
7254 * On error: returns error code (negative)
7255 * On success: returns 0
7256 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007257static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007258{
7259 int size;
7260 int ret = 0;
7261
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007262 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007263 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7264 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7265 if (!vsi->tx_rings)
7266 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007267 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7268
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007269 if (alloc_qvectors) {
7270 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007271 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007272 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7273 if (!vsi->q_vectors) {
7274 ret = -ENOMEM;
7275 goto err_vectors;
7276 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007277 }
7278 return ret;
7279
7280err_vectors:
7281 kfree(vsi->tx_rings);
7282 return ret;
7283}
7284
7285/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007286 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7287 * @pf: board private structure
7288 * @type: type of VSI
7289 *
7290 * On error: returns error code (negative)
7291 * On success: returns vsi index in PF (positive)
7292 **/
7293static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7294{
7295 int ret = -ENODEV;
7296 struct i40e_vsi *vsi;
7297 int vsi_idx;
7298 int i;
7299
7300 /* Need to protect the allocation of the VSIs at the PF level */
7301 mutex_lock(&pf->switch_mutex);
7302
7303 /* VSI list may be fragmented if VSI creation/destruction has
7304 * been happening. We can afford to do a quick scan to look
7305 * for any free VSIs in the list.
7306 *
7307 * find next empty vsi slot, looping back around if necessary
7308 */
7309 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007310 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007311 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007312 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007313 i = 0;
7314 while (i < pf->next_vsi && pf->vsi[i])
7315 i++;
7316 }
7317
Mitch Williams505682c2014-05-20 08:01:37 +00007318 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007319 vsi_idx = i; /* Found one! */
7320 } else {
7321 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007322 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007323 }
7324 pf->next_vsi = ++i;
7325
7326 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7327 if (!vsi) {
7328 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007329 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007330 }
7331 vsi->type = type;
7332 vsi->back = pf;
7333 set_bit(__I40E_DOWN, &vsi->state);
7334 vsi->flags = 0;
7335 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007336 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007337 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7338 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007339 vsi->netdev_registered = false;
7340 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7341 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007342 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007343
Alexander Duyck9f65e152013-09-28 06:00:58 +00007344 ret = i40e_set_num_rings_in_vsi(vsi);
7345 if (ret)
7346 goto err_rings;
7347
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007348 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007349 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007350 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007352 /* Setup default MSIX irq handler for VSI */
7353 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7354
Kiran Patil21659032015-09-30 14:09:03 -04007355 /* Initialize VSI lock */
7356 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007357 pf->vsi[vsi_idx] = vsi;
7358 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007359 goto unlock_pf;
7360
Alexander Duyck9f65e152013-09-28 06:00:58 +00007361err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007362 pf->next_vsi = i - 1;
7363 kfree(vsi);
7364unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007365 mutex_unlock(&pf->switch_mutex);
7366 return ret;
7367}
7368
7369/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007370 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7371 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007372 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007373 *
7374 * On error: returns error code (negative)
7375 * On success: returns 0
7376 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007377static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007378{
7379 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007380 if (free_qvectors) {
7381 kfree(vsi->q_vectors);
7382 vsi->q_vectors = NULL;
7383 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007384 kfree(vsi->tx_rings);
7385 vsi->tx_rings = NULL;
7386 vsi->rx_rings = NULL;
7387}
7388
7389/**
Helin Zhang28c58692015-10-26 19:44:27 -04007390 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7391 * and lookup table
7392 * @vsi: Pointer to VSI structure
7393 */
7394static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7395{
7396 if (!vsi)
7397 return;
7398
7399 kfree(vsi->rss_hkey_user);
7400 vsi->rss_hkey_user = NULL;
7401
7402 kfree(vsi->rss_lut_user);
7403 vsi->rss_lut_user = NULL;
7404}
7405
7406/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007407 * i40e_vsi_clear - Deallocate the VSI provided
7408 * @vsi: the VSI being un-configured
7409 **/
7410static int i40e_vsi_clear(struct i40e_vsi *vsi)
7411{
7412 struct i40e_pf *pf;
7413
7414 if (!vsi)
7415 return 0;
7416
7417 if (!vsi->back)
7418 goto free_vsi;
7419 pf = vsi->back;
7420
7421 mutex_lock(&pf->switch_mutex);
7422 if (!pf->vsi[vsi->idx]) {
7423 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7424 vsi->idx, vsi->idx, vsi, vsi->type);
7425 goto unlock_vsi;
7426 }
7427
7428 if (pf->vsi[vsi->idx] != vsi) {
7429 dev_err(&pf->pdev->dev,
7430 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7431 pf->vsi[vsi->idx]->idx,
7432 pf->vsi[vsi->idx],
7433 pf->vsi[vsi->idx]->type,
7434 vsi->idx, vsi, vsi->type);
7435 goto unlock_vsi;
7436 }
7437
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007438 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007439 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7440 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7441
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007442 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007443 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007444
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007445 pf->vsi[vsi->idx] = NULL;
7446 if (vsi->idx < pf->next_vsi)
7447 pf->next_vsi = vsi->idx;
7448
7449unlock_vsi:
7450 mutex_unlock(&pf->switch_mutex);
7451free_vsi:
7452 kfree(vsi);
7453
7454 return 0;
7455}
7456
7457/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007458 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7459 * @vsi: the VSI being cleaned
7460 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007461static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007462{
7463 int i;
7464
Greg Rose8e9dca52013-12-18 13:45:53 +00007465 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007466 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007467 kfree_rcu(vsi->tx_rings[i], rcu);
7468 vsi->tx_rings[i] = NULL;
7469 vsi->rx_rings[i] = NULL;
7470 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007471 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007472}
7473
7474/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007475 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7476 * @vsi: the VSI being configured
7477 **/
7478static int i40e_alloc_rings(struct i40e_vsi *vsi)
7479{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007480 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007482 int i;
7483
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007484 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007485 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007486 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007487 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7488 if (!tx_ring)
7489 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007490
7491 tx_ring->queue_index = i;
7492 tx_ring->reg_idx = vsi->base_queue + i;
7493 tx_ring->ring_active = false;
7494 tx_ring->vsi = vsi;
7495 tx_ring->netdev = vsi->netdev;
7496 tx_ring->dev = &pf->pdev->dev;
7497 tx_ring->count = vsi->num_desc;
7498 tx_ring->size = 0;
7499 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007500 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7501 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007502 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007503 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007504
Alexander Duyck9f65e152013-09-28 06:00:58 +00007505 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007506 rx_ring->queue_index = i;
7507 rx_ring->reg_idx = vsi->base_queue + i;
7508 rx_ring->ring_active = false;
7509 rx_ring->vsi = vsi;
7510 rx_ring->netdev = vsi->netdev;
7511 rx_ring->dev = &pf->pdev->dev;
7512 rx_ring->count = vsi->num_desc;
7513 rx_ring->size = 0;
7514 rx_ring->dcb_tc = 0;
7515 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
7516 set_ring_16byte_desc_enabled(rx_ring);
7517 else
7518 clear_ring_16byte_desc_enabled(rx_ring);
Kan Lianga75e8002016-02-19 09:24:04 -05007519 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007520 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007521 }
7522
7523 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007524
7525err_out:
7526 i40e_vsi_clear_rings(vsi);
7527 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007528}
7529
7530/**
7531 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7532 * @pf: board private structure
7533 * @vectors: the number of MSI-X vectors to request
7534 *
7535 * Returns the number of vectors reserved, or error
7536 **/
7537static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7538{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007539 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7540 I40E_MIN_MSIX, vectors);
7541 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007542 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007543 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007544 vectors = 0;
7545 }
7546
7547 return vectors;
7548}
7549
7550/**
7551 * i40e_init_msix - Setup the MSIX capability
7552 * @pf: board private structure
7553 *
7554 * Work with the OS to set up the MSIX vectors needed.
7555 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007556 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007557 **/
7558static int i40e_init_msix(struct i40e_pf *pf)
7559{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007560 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007561 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007562 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007563 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007564 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007565
7566 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7567 return -ENODEV;
7568
7569 /* The number of vectors we'll request will be comprised of:
7570 * - Add 1 for "other" cause for Admin Queue events, etc.
7571 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007572 * - Queues being used for RSS.
7573 * We don't need as many as max_rss_size vectors.
7574 * use rss_size instead in the calculation since that
7575 * is governed by number of cpus in the system.
7576 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007578 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007579#ifdef I40E_FCOE
7580 * - The number of FCOE qps.
7581#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582 * Once we count this up, try the request.
7583 *
7584 * If we can't get what we want, we'll simplify to nearly nothing
7585 * and try again. If that still fails, we punt.
7586 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007587 vectors_left = hw->func_caps.num_msix_vectors;
7588 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007589
Shannon Nelson1e200e42015-02-27 09:15:24 +00007590 /* reserve one vector for miscellaneous handler */
7591 if (vectors_left) {
7592 v_budget++;
7593 vectors_left--;
7594 }
7595
7596 /* reserve vectors for the main PF traffic queues */
7597 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7598 vectors_left -= pf->num_lan_msix;
7599 v_budget += pf->num_lan_msix;
7600
7601 /* reserve one vector for sideband flow director */
7602 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7603 if (vectors_left) {
7604 v_budget++;
7605 vectors_left--;
7606 } else {
7607 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7608 }
7609 }
John W Linville83840e42015-01-14 03:06:28 +00007610
Vasu Dev38e00432014-08-01 13:27:03 -07007611#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007612 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007613 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007614 if (!vectors_left)
7615 pf->num_fcoe_msix = 0;
7616 else if (vectors_left >= pf->num_fcoe_qps)
7617 pf->num_fcoe_msix = pf->num_fcoe_qps;
7618 else
7619 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007620 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007621 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007622 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007623
Vasu Dev38e00432014-08-01 13:27:03 -07007624#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007625 /* can we reserve enough for iWARP? */
7626 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7627 if (!vectors_left)
7628 pf->num_iwarp_msix = 0;
7629 else if (vectors_left < pf->num_iwarp_msix)
7630 pf->num_iwarp_msix = 1;
7631 v_budget += pf->num_iwarp_msix;
7632 vectors_left -= pf->num_iwarp_msix;
7633 }
7634
Shannon Nelson1e200e42015-02-27 09:15:24 +00007635 /* any vectors left over go for VMDq support */
7636 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7637 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7638 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7639
7640 /* if we're short on vectors for what's desired, we limit
7641 * the queues per vmdq. If this is still more than are
7642 * available, the user will need to change the number of
7643 * queues/vectors used by the PF later with the ethtool
7644 * channels command
7645 */
7646 if (vmdq_vecs < vmdq_vecs_wanted)
7647 pf->num_vmdq_qps = 1;
7648 pf->num_vmdq_msix = pf->num_vmdq_qps;
7649
7650 v_budget += vmdq_vecs;
7651 vectors_left -= vmdq_vecs;
7652 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007653
7654 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7655 GFP_KERNEL);
7656 if (!pf->msix_entries)
7657 return -ENOMEM;
7658
7659 for (i = 0; i < v_budget; i++)
7660 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007661 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007662
Shannon Nelson3b444392015-02-26 16:15:57 +00007663 if (v_actual != v_budget) {
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007664 /* If we have limited resources, we will start with no vectors
7665 * for the special features and then allocate vectors to some
7666 * of these features based on the policy and at the end disable
7667 * the features that did not get any vectors.
7668 */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007669 iwarp_requested = pf->num_iwarp_msix;
7670 pf->num_iwarp_msix = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07007671#ifdef I40E_FCOE
7672 pf->num_fcoe_qps = 0;
7673 pf->num_fcoe_msix = 0;
7674#endif
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007675 pf->num_vmdq_msix = 0;
7676 }
7677
Shannon Nelson3b444392015-02-26 16:15:57 +00007678 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007679 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7680 kfree(pf->msix_entries);
7681 pf->msix_entries = NULL;
7682 return -ENODEV;
7683
Shannon Nelson3b444392015-02-26 16:15:57 +00007684 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007685 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007686 pf->num_vmdq_vsis = 0;
7687 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007688 pf->num_lan_qps = 1;
7689 pf->num_lan_msix = 1;
7690
Shannon Nelson3b444392015-02-26 16:15:57 +00007691 } else if (v_actual != v_budget) {
7692 int vec;
7693
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007694 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007695 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007696
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007697 /* Scale vector usage down */
7698 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007699 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007700 pf->num_vmdq_qps = 1;
7701 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007702
7703 /* partition out the remaining vectors */
7704 switch (vec) {
7705 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007706 pf->num_lan_msix = 1;
7707 break;
7708 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007709 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7710 pf->num_lan_msix = 1;
7711 pf->num_iwarp_msix = 1;
7712 } else {
7713 pf->num_lan_msix = 2;
7714 }
Vasu Dev38e00432014-08-01 13:27:03 -07007715#ifdef I40E_FCOE
7716 /* give one vector to FCoE */
7717 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7718 pf->num_lan_msix = 1;
7719 pf->num_fcoe_msix = 1;
7720 }
Vasu Dev38e00432014-08-01 13:27:03 -07007721#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007722 break;
7723 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007724 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7725 pf->num_iwarp_msix = min_t(int, (vec / 3),
7726 iwarp_requested);
7727 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7728 I40E_DEFAULT_NUM_VMDQ_VSI);
7729 } else {
7730 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7731 I40E_DEFAULT_NUM_VMDQ_VSI);
7732 }
7733 pf->num_lan_msix = min_t(int,
7734 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7735 pf->num_lan_msix);
Vasu Dev38e00432014-08-01 13:27:03 -07007736#ifdef I40E_FCOE
7737 /* give one vector to FCoE */
7738 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7739 pf->num_fcoe_msix = 1;
7740 vec--;
7741 }
7742#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743 break;
7744 }
7745 }
7746
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007747 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7748 (pf->num_vmdq_msix == 0)) {
7749 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7750 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7751 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007752
7753 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7754 (pf->num_iwarp_msix == 0)) {
7755 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7756 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7757 }
Vasu Dev38e00432014-08-01 13:27:03 -07007758#ifdef I40E_FCOE
7759
7760 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7761 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7762 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7763 }
7764#endif
Shannon Nelson3b444392015-02-26 16:15:57 +00007765 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007766}
7767
7768/**
Greg Rose90e04072014-03-06 08:59:57 +00007769 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007770 * @vsi: the VSI being configured
7771 * @v_idx: index of the vector in the vsi struct
7772 *
7773 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7774 **/
Greg Rose90e04072014-03-06 08:59:57 +00007775static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
Alexander Duyck493fb302013-09-28 07:01:44 +00007776{
7777 struct i40e_q_vector *q_vector;
7778
7779 /* allocate q_vector */
7780 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7781 if (!q_vector)
7782 return -ENOMEM;
7783
7784 q_vector->vsi = vsi;
7785 q_vector->v_idx = v_idx;
7786 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
7787 if (vsi->netdev)
7788 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007789 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007790
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007791 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7792 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7793
Alexander Duyck493fb302013-09-28 07:01:44 +00007794 /* tie q_vector and vsi together */
7795 vsi->q_vectors[v_idx] = q_vector;
7796
7797 return 0;
7798}
7799
7800/**
Greg Rose90e04072014-03-06 08:59:57 +00007801 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007802 * @vsi: the VSI being configured
7803 *
7804 * We allocate one q_vector per queue interrupt. If allocation fails we
7805 * return -ENOMEM.
7806 **/
Greg Rose90e04072014-03-06 08:59:57 +00007807static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007808{
7809 struct i40e_pf *pf = vsi->back;
7810 int v_idx, num_q_vectors;
Alexander Duyck493fb302013-09-28 07:01:44 +00007811 int err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007812
7813 /* if not MSIX, give the one vector only to the LAN VSI */
7814 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7815 num_q_vectors = vsi->num_q_vectors;
7816 else if (vsi == pf->vsi[pf->lan_vsi])
7817 num_q_vectors = 1;
7818 else
7819 return -EINVAL;
7820
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007821 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Greg Rose90e04072014-03-06 08:59:57 +00007822 err = i40e_vsi_alloc_q_vector(vsi, v_idx);
Alexander Duyck493fb302013-09-28 07:01:44 +00007823 if (err)
7824 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007825 }
7826
7827 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007828
7829err_out:
7830 while (v_idx--)
7831 i40e_free_q_vector(vsi, v_idx);
7832
7833 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007834}
7835
7836/**
7837 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7838 * @pf: board private structure to initialize
7839 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007840static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007841{
Shannon Nelson3b444392015-02-26 16:15:57 +00007842 int vectors = 0;
7843 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007844
7845 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007846 vectors = i40e_init_msix(pf);
7847 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007848 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007849 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007850#ifdef I40E_FCOE
7851 I40E_FLAG_FCOE_ENABLED |
7852#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007853 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007854 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007855 I40E_FLAG_SRIOV_ENABLED |
7856 I40E_FLAG_FD_SB_ENABLED |
7857 I40E_FLAG_FD_ATR_ENABLED |
7858 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007859
7860 /* rework the queue expectations without MSIX */
7861 i40e_determine_queue_usage(pf);
7862 }
7863 }
7864
7865 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7866 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007867 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007868 vectors = pci_enable_msi(pf->pdev);
7869 if (vectors < 0) {
7870 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7871 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007872 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7873 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007874 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007875 }
7876
Shannon Nelson958a3e32013-09-28 07:13:28 +00007877 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007878 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007879
Shannon Nelson3b444392015-02-26 16:15:57 +00007880 /* set up vector assignment tracking */
7881 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7882 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007883 if (!pf->irq_pile) {
7884 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7885 return -ENOMEM;
7886 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007887 pf->irq_pile->num_entries = vectors;
7888 pf->irq_pile->search_hint = 0;
7889
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007890 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007891 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007892
7893 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007894}
7895
7896/**
7897 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7898 * @pf: board private structure
7899 *
7900 * This sets up the handler for MSIX 0, which is used to manage the
7901 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7902 * when in MSI or Legacy interrupt mode.
7903 **/
7904static int i40e_setup_misc_vector(struct i40e_pf *pf)
7905{
7906 struct i40e_hw *hw = &pf->hw;
7907 int err = 0;
7908
7909 /* Only request the irq if this is the first time through, and
7910 * not when we're rebuilding after a Reset
7911 */
7912 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7913 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007914 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007915 if (err) {
7916 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007917 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007918 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007919 return -EFAULT;
7920 }
7921 }
7922
Jacob Kellerab437b52014-12-14 01:55:08 +00007923 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007924
7925 /* associate no queues to the misc vector */
7926 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7927 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7928
7929 i40e_flush(hw);
7930
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007931 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007932
7933 return err;
7934}
7935
7936/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007937 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7938 * @vsi: vsi structure
7939 * @seed: RSS hash seed
7940 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007941static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7942 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007943{
7944 struct i40e_aqc_get_set_rss_key_data rss_key;
7945 struct i40e_pf *pf = vsi->back;
7946 struct i40e_hw *hw = &pf->hw;
7947 bool pf_lut = false;
7948 u8 *rss_lut;
7949 int ret, i;
7950
7951 memset(&rss_key, 0, sizeof(rss_key));
7952 memcpy(&rss_key, seed, sizeof(rss_key));
7953
7954 rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
7955 if (!rss_lut)
7956 return -ENOMEM;
7957
7958 /* Populate the LUT with max no. of queues in round robin fashion */
7959 for (i = 0; i < vsi->rss_table_size; i++)
7960 rss_lut[i] = i % vsi->rss_size;
7961
7962 ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
7963 if (ret) {
7964 dev_info(&pf->pdev->dev,
7965 "Cannot set RSS key, err %s aq_err %s\n",
7966 i40e_stat_str(&pf->hw, ret),
7967 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai126b63d2015-08-24 13:26:53 -07007968 goto config_rss_aq_out;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007969 }
7970
7971 if (vsi->type == I40E_VSI_MAIN)
7972 pf_lut = true;
7973
7974 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
7975 vsi->rss_table_size);
7976 if (ret)
7977 dev_info(&pf->pdev->dev,
7978 "Cannot set RSS lut, err %s aq_err %s\n",
7979 i40e_stat_str(&pf->hw, ret),
7980 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7981
Anjali Singhai126b63d2015-08-24 13:26:53 -07007982config_rss_aq_out:
7983 kfree(rss_lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007984 return ret;
7985}
7986
7987/**
7988 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
7989 * @vsi: VSI structure
7990 **/
7991static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
7992{
7993 u8 seed[I40E_HKEY_ARRAY_SIZE];
7994 struct i40e_pf *pf = vsi->back;
Helin Zhange69ff812015-10-21 19:56:22 -04007995 u8 *lut;
7996 int ret;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007997
Helin Zhange69ff812015-10-21 19:56:22 -04007998 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
7999 return 0;
8000
8001 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8002 if (!lut)
8003 return -ENOMEM;
8004
8005 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008006 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhangacd65442015-10-26 19:44:28 -04008007 vsi->rss_size = min_t(int, pf->alloc_rss_size, vsi->num_queue_pairs);
Helin Zhange69ff812015-10-21 19:56:22 -04008008 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8009 kfree(lut);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008010
Helin Zhange69ff812015-10-21 19:56:22 -04008011 return ret;
8012}
8013
8014/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008015 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8016 * @vsi: Pointer to vsi structure
8017 * @seed: Buffter to store the hash keys
8018 * @lut: Buffer to store the lookup table entries
8019 * @lut_size: Size of buffer to store the lookup table entries
8020 *
8021 * Return 0 on success, negative on failure
8022 */
8023static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8024 u8 *lut, u16 lut_size)
8025{
8026 struct i40e_pf *pf = vsi->back;
8027 struct i40e_hw *hw = &pf->hw;
8028 int ret = 0;
8029
8030 if (seed) {
8031 ret = i40e_aq_get_rss_key(hw, vsi->id,
8032 (struct i40e_aqc_get_set_rss_key_data *)seed);
8033 if (ret) {
8034 dev_info(&pf->pdev->dev,
8035 "Cannot get RSS key, err %s aq_err %s\n",
8036 i40e_stat_str(&pf->hw, ret),
8037 i40e_aq_str(&pf->hw,
8038 pf->hw.aq.asq_last_status));
8039 return ret;
8040 }
8041 }
8042
8043 if (lut) {
8044 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8045
8046 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8047 if (ret) {
8048 dev_info(&pf->pdev->dev,
8049 "Cannot get RSS lut, err %s aq_err %s\n",
8050 i40e_stat_str(&pf->hw, ret),
8051 i40e_aq_str(&pf->hw,
8052 pf->hw.aq.asq_last_status));
8053 return ret;
8054 }
8055 }
8056
8057 return ret;
8058}
8059
8060/**
Helin Zhang043dd652015-10-21 19:56:23 -04008061 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008062 * @vsi: Pointer to vsi structure
8063 * @seed: RSS hash seed
8064 * @lut: Lookup table
8065 * @lut_size: Lookup table size
8066 *
8067 * Returns 0 on success, negative on failure
8068 **/
8069static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8070 const u8 *lut, u16 lut_size)
8071{
8072 struct i40e_pf *pf = vsi->back;
8073 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008074 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008075 u8 i;
8076
8077 /* Fill out hash function seed */
8078 if (seed) {
8079 u32 *seed_dw = (u32 *)seed;
8080
Mitch Williamsc4e18682016-04-12 08:30:40 -07008081 if (vsi->type == I40E_VSI_MAIN) {
8082 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8083 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8084 seed_dw[i]);
8085 } else if (vsi->type == I40E_VSI_SRIOV) {
8086 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8087 i40e_write_rx_ctl(hw,
8088 I40E_VFQF_HKEY1(i, vf_id),
8089 seed_dw[i]);
8090 } else {
8091 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8092 }
Helin Zhange69ff812015-10-21 19:56:22 -04008093 }
8094
8095 if (lut) {
8096 u32 *lut_dw = (u32 *)lut;
8097
Mitch Williamsc4e18682016-04-12 08:30:40 -07008098 if (vsi->type == I40E_VSI_MAIN) {
8099 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8100 return -EINVAL;
8101 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8102 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8103 } else if (vsi->type == I40E_VSI_SRIOV) {
8104 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8105 return -EINVAL;
8106 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8107 i40e_write_rx_ctl(hw,
8108 I40E_VFQF_HLUT1(i, vf_id),
8109 lut_dw[i]);
8110 } else {
8111 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8112 }
Helin Zhange69ff812015-10-21 19:56:22 -04008113 }
8114 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008115
8116 return 0;
8117}
8118
8119/**
Helin Zhang043dd652015-10-21 19:56:23 -04008120 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8121 * @vsi: Pointer to VSI structure
8122 * @seed: Buffer to store the keys
8123 * @lut: Buffer to store the lookup table entries
8124 * @lut_size: Size of buffer to store the lookup table entries
8125 *
8126 * Returns 0 on success, negative on failure
8127 */
8128static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8129 u8 *lut, u16 lut_size)
8130{
8131 struct i40e_pf *pf = vsi->back;
8132 struct i40e_hw *hw = &pf->hw;
8133 u16 i;
8134
8135 if (seed) {
8136 u32 *seed_dw = (u32 *)seed;
8137
8138 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008139 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008140 }
8141 if (lut) {
8142 u32 *lut_dw = (u32 *)lut;
8143
8144 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8145 return -EINVAL;
8146 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8147 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8148 }
8149
8150 return 0;
8151}
8152
8153/**
8154 * i40e_config_rss - Configure RSS keys and lut
8155 * @vsi: Pointer to VSI structure
8156 * @seed: RSS hash seed
8157 * @lut: Lookup table
8158 * @lut_size: Lookup table size
8159 *
8160 * Returns 0 on success, negative on failure
8161 */
8162int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8163{
8164 struct i40e_pf *pf = vsi->back;
8165
8166 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8167 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8168 else
8169 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8170}
8171
8172/**
8173 * i40e_get_rss - Get RSS keys and lut
8174 * @vsi: Pointer to VSI structure
8175 * @seed: Buffer to store the keys
8176 * @lut: Buffer to store the lookup table entries
8177 * lut_size: Size of buffer to store the lookup table entries
8178 *
8179 * Returns 0 on success, negative on failure
8180 */
8181int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8182{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008183 struct i40e_pf *pf = vsi->back;
8184
8185 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8186 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8187 else
8188 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008189}
8190
8191/**
Helin Zhange69ff812015-10-21 19:56:22 -04008192 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8193 * @pf: Pointer to board private structure
8194 * @lut: Lookup table
8195 * @rss_table_size: Lookup table size
8196 * @rss_size: Range of queue number for hashing
8197 */
8198static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8199 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008200{
Helin Zhange69ff812015-10-21 19:56:22 -04008201 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008202
Helin Zhange69ff812015-10-21 19:56:22 -04008203 for (i = 0; i < rss_table_size; i++)
8204 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008205}
8206
8207/**
Helin Zhang043dd652015-10-21 19:56:23 -04008208 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008209 * @pf: board private structure
8210 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008211static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008212{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008213 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008214 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008215 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008216 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008217 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008218 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008219 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008220
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008221 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008222 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8223 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008224 hena |= i40e_pf_get_default_rss_hena(pf);
8225
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008226 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8227 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008228
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008229 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008230 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008231 reg_val = (pf->rss_table_size == 512) ?
8232 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8233 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008234 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008235
Helin Zhang28c58692015-10-26 19:44:27 -04008236 /* Determine the RSS size of the VSI */
8237 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008238 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8239 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008240
Helin Zhange69ff812015-10-21 19:56:22 -04008241 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8242 if (!lut)
8243 return -ENOMEM;
8244
Helin Zhang28c58692015-10-26 19:44:27 -04008245 /* Use user configured lut if there is one, otherwise use default */
8246 if (vsi->rss_lut_user)
8247 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8248 else
8249 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008250
Helin Zhang28c58692015-10-26 19:44:27 -04008251 /* Use user configured hash key if there is one, otherwise
8252 * use default.
8253 */
8254 if (vsi->rss_hkey_user)
8255 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8256 else
8257 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008258 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008259 kfree(lut);
8260
8261 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008262}
8263
8264/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008265 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8266 * @pf: board private structure
8267 * @queue_count: the requested queue count for rss.
8268 *
8269 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8270 * count which may be different from the requested queue count.
8271 **/
8272int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8273{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008274 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8275 int new_rss_size;
8276
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008277 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8278 return 0;
8279
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008280 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008281
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008282 if (queue_count != vsi->num_queue_pairs) {
8283 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008284 i40e_prep_for_reset(pf);
8285
Helin Zhangacd65442015-10-26 19:44:28 -04008286 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008287
8288 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008289
8290 /* Discard the user configured hash keys and lut, if less
8291 * queues are enabled.
8292 */
8293 if (queue_count < vsi->rss_size) {
8294 i40e_clear_rss_config_user(vsi);
8295 dev_dbg(&pf->pdev->dev,
8296 "discard user configured hash keys and lut\n");
8297 }
8298
8299 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008300 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8301 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008302
Helin Zhang043dd652015-10-21 19:56:23 -04008303 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008304 }
Helin Zhange36b0b12015-10-26 19:44:36 -04008305 dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
8306 pf->alloc_rss_size, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008307 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008308}
8309
8310/**
Greg Rosef4492db2015-02-06 08:52:12 +00008311 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8312 * @pf: board private structure
8313 **/
8314i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8315{
8316 i40e_status status;
8317 bool min_valid, max_valid;
8318 u32 max_bw, min_bw;
8319
8320 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8321 &min_valid, &max_valid);
8322
8323 if (!status) {
8324 if (min_valid)
8325 pf->npar_min_bw = min_bw;
8326 if (max_valid)
8327 pf->npar_max_bw = max_bw;
8328 }
8329
8330 return status;
8331}
8332
8333/**
8334 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8335 * @pf: board private structure
8336 **/
8337i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8338{
8339 struct i40e_aqc_configure_partition_bw_data bw_data;
8340 i40e_status status;
8341
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008342 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008343 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008344 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8345 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8346
8347 /* Set the new bandwidths */
8348 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8349
8350 return status;
8351}
8352
8353/**
8354 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8355 * @pf: board private structure
8356 **/
8357i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8358{
8359 /* Commit temporary BW setting to permanent NVM image */
8360 enum i40e_admin_queue_err last_aq_status;
8361 i40e_status ret;
8362 u16 nvm_word;
8363
8364 if (pf->hw.partition_id != 1) {
8365 dev_info(&pf->pdev->dev,
8366 "Commit BW only works on partition 1! This is partition %d",
8367 pf->hw.partition_id);
8368 ret = I40E_NOT_SUPPORTED;
8369 goto bw_commit_out;
8370 }
8371
8372 /* Acquire NVM for read access */
8373 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8374 last_aq_status = pf->hw.aq.asq_last_status;
8375 if (ret) {
8376 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008377 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8378 i40e_stat_str(&pf->hw, ret),
8379 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008380 goto bw_commit_out;
8381 }
8382
8383 /* Read word 0x10 of NVM - SW compatibility word 1 */
8384 ret = i40e_aq_read_nvm(&pf->hw,
8385 I40E_SR_NVM_CONTROL_WORD,
8386 0x10, sizeof(nvm_word), &nvm_word,
8387 false, NULL);
8388 /* Save off last admin queue command status before releasing
8389 * the NVM
8390 */
8391 last_aq_status = pf->hw.aq.asq_last_status;
8392 i40e_release_nvm(&pf->hw);
8393 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008394 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8395 i40e_stat_str(&pf->hw, ret),
8396 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008397 goto bw_commit_out;
8398 }
8399
8400 /* Wait a bit for NVM release to complete */
8401 msleep(50);
8402
8403 /* Acquire NVM for write access */
8404 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8405 last_aq_status = pf->hw.aq.asq_last_status;
8406 if (ret) {
8407 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008408 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8409 i40e_stat_str(&pf->hw, ret),
8410 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008411 goto bw_commit_out;
8412 }
8413 /* Write it back out unchanged to initiate update NVM,
8414 * which will force a write of the shadow (alt) RAM to
8415 * the NVM - thus storing the bandwidth values permanently.
8416 */
8417 ret = i40e_aq_update_nvm(&pf->hw,
8418 I40E_SR_NVM_CONTROL_WORD,
8419 0x10, sizeof(nvm_word),
8420 &nvm_word, true, NULL);
8421 /* Save off last admin queue command status before releasing
8422 * the NVM
8423 */
8424 last_aq_status = pf->hw.aq.asq_last_status;
8425 i40e_release_nvm(&pf->hw);
8426 if (ret)
8427 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008428 "BW settings NOT SAVED, err %s aq_err %s\n",
8429 i40e_stat_str(&pf->hw, ret),
8430 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008431bw_commit_out:
8432
8433 return ret;
8434}
8435
8436/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008437 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8438 * @pf: board private structure to initialize
8439 *
8440 * i40e_sw_init initializes the Adapter private data structure.
8441 * Fields are initialized based on PCI device information and
8442 * OS network device settings (MTU size).
8443 **/
8444static int i40e_sw_init(struct i40e_pf *pf)
8445{
8446 int err = 0;
8447 int size;
8448
8449 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8450 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
8451 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8452 if (I40E_DEBUG_USER & debug)
8453 pf->hw.debug_mask = debug;
8454 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8455 I40E_DEFAULT_MSG_ENABLE);
8456 }
8457
8458 /* Set default capability flags */
8459 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8460 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008461 I40E_FLAG_MSIX_ENABLED;
8462
8463 if (iommu_present(&pci_bus_type))
8464 pf->flags |= I40E_FLAG_RX_PS_ENABLED;
8465 else
8466 pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008467
Mitch Williamsca99eb92014-04-04 04:43:07 +00008468 /* Set default ITR */
8469 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8470 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8471
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008472 /* Depending on PF configurations, it is possible that the RSS
8473 * maximum might end up larger than the available queues
8474 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008475 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008476 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008477 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008478 pf->rss_size_max = min_t(int, pf->rss_size_max,
8479 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008480 if (pf->hw.func_caps.rss) {
8481 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008482 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8483 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008484 }
8485
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008486 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008487 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008488 pf->flags |= I40E_FLAG_MFP_ENABLED;
8489 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008490 if (i40e_get_npar_bw_setting(pf))
8491 dev_warn(&pf->pdev->dev,
8492 "Could not get NPAR bw settings\n");
8493 else
8494 dev_info(&pf->pdev->dev,
8495 "Min BW = %8.8x, Max BW = %8.8x\n",
8496 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008497 }
8498
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008499 /* FW/NVM is not yet fixed in this regard */
8500 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8501 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8502 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8503 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008504 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8505 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008506 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008507 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008508 else
8509 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008510 pf->fdir_pf_filter_count =
8511 pf->hw.func_caps.fd_filters_guaranteed;
8512 pf->hw.fdir_shared_filter_count =
8513 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008514 }
8515
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008516 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008517 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008518 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008519 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008520 /* No DCB support for FW < v4.33 */
8521 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8522 }
8523
8524 /* Disable FW LLDP if FW < v4.3 */
8525 if (i40e_is_mac_710(&pf->hw) &&
8526 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8527 (pf->hw.aq.fw_maj_ver < 4)))
8528 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8529
8530 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8531 if (i40e_is_mac_710(&pf->hw) &&
8532 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8533 (pf->hw.aq.fw_maj_ver >= 5)))
8534 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008535
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008536 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008537 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008538 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008539 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008540 }
8541
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008542 if (pf->hw.func_caps.iwarp) {
8543 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8544 /* IWARP needs one extra vector for CQP just like MISC.*/
8545 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8546 }
8547
Vasu Dev38e00432014-08-01 13:27:03 -07008548#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008549 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008550
8551#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008552#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008553 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008554 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8555 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8556 pf->num_req_vfs = min_t(int,
8557 pf->hw.func_caps.num_vfs,
8558 I40E_MAX_VF_COUNT);
8559 }
8560#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008561 if (pf->hw.mac.type == I40E_MAC_X722) {
8562 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8563 I40E_FLAG_128_QP_RSS_CAPABLE |
8564 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8565 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8566 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008567 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008568 I40E_FLAG_NO_PCI_LINK_CHECK |
Catherine Sullivanf8db54cc2015-12-22 14:25:14 -08008569 I40E_FLAG_100M_SGMII_CAPABLE |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008570 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008571 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008572 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8573 ((pf->hw.aq.api_maj_ver == 1) &&
8574 (pf->hw.aq.api_min_ver > 4))) {
8575 /* Supported in FW API version higher than 1.4 */
8576 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008577 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8578 } else {
8579 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008580 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008581
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008582 pf->eeprom_version = 0xDEAD;
8583 pf->lan_veb = I40E_NO_VEB;
8584 pf->lan_vsi = I40E_NO_VSI;
8585
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008586 /* By default FW has this off for performance reasons */
8587 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008589 /* set up queue assignment tracking */
8590 size = sizeof(struct i40e_lump_tracking)
8591 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8592 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8593 if (!pf->qp_pile) {
8594 err = -ENOMEM;
8595 goto sw_init_done;
8596 }
8597 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8598 pf->qp_pile->search_hint = 0;
8599
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008600 pf->tx_timeout_recovery_level = 1;
8601
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008602 mutex_init(&pf->switch_mutex);
8603
Greg Rosec668a122015-02-26 16:10:39 +00008604 /* If NPAR is enabled nudge the Tx scheduler */
8605 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8606 i40e_set_npar_bw_setting(pf);
8607
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008608sw_init_done:
8609 return err;
8610}
8611
8612/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008613 * i40e_set_ntuple - set the ntuple feature flag and take action
8614 * @pf: board private structure to initialize
8615 * @features: the feature set that the stack is suggesting
8616 *
8617 * returns a bool to indicate if reset needs to happen
8618 **/
8619bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8620{
8621 bool need_reset = false;
8622
8623 /* Check if Flow Director n-tuple support was enabled or disabled. If
8624 * the state changed, we need to reset.
8625 */
8626 if (features & NETIF_F_NTUPLE) {
8627 /* Enable filters and mark for reset */
8628 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8629 need_reset = true;
8630 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8631 } else {
8632 /* turn off filters, mark for reset and clear SW filter list */
8633 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8634 need_reset = true;
8635 i40e_fdir_filter_exit(pf);
8636 }
8637 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008638 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008639 /* reset fd counters */
8640 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8641 pf->fdir_pf_active_filters = 0;
8642 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04008643 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8644 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008645 /* if ATR was auto disabled it can be re-enabled. */
8646 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8647 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
8648 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008649 }
8650 return need_reset;
8651}
8652
8653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008654 * i40e_set_features - set the netdev feature flags
8655 * @netdev: ptr to the netdev being adjusted
8656 * @features: the feature set that the stack is suggesting
8657 **/
8658static int i40e_set_features(struct net_device *netdev,
8659 netdev_features_t features)
8660{
8661 struct i40e_netdev_priv *np = netdev_priv(netdev);
8662 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008663 struct i40e_pf *pf = vsi->back;
8664 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008665
8666 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8667 i40e_vlan_stripping_enable(vsi);
8668 else
8669 i40e_vlan_stripping_disable(vsi);
8670
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008671 need_reset = i40e_set_ntuple(pf, features);
8672
8673 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008674 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008675
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008676 return 0;
8677}
8678
Singhai, Anjali6a899022015-12-14 12:21:18 -08008679#if IS_ENABLED(CONFIG_VXLAN) || IS_ENABLED(CONFIG_GENEVE)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008680/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008681 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008682 * @pf: board private structure
8683 * @port: The UDP port to look up
8684 *
8685 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8686 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008687static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008688{
8689 u8 i;
8690
8691 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008692 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008693 return i;
8694 }
8695
8696 return i;
8697}
8698
Singhai, Anjali6a899022015-12-14 12:21:18 -08008699#endif
Eric Dumazet5cae7612016-01-20 19:53:02 -08008700
8701#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008702/**
8703 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
8704 * @netdev: This physical port's netdev
8705 * @sa_family: Socket Family that VXLAN is notifying us about
8706 * @port: New UDP port number that VXLAN started listening to
8707 **/
8708static void i40e_add_vxlan_port(struct net_device *netdev,
8709 sa_family_t sa_family, __be16 port)
8710{
8711 struct i40e_netdev_priv *np = netdev_priv(netdev);
8712 struct i40e_vsi *vsi = np->vsi;
8713 struct i40e_pf *pf = vsi->back;
8714 u8 next_idx;
8715 u8 idx;
8716
Singhai, Anjali6a899022015-12-14 12:21:18 -08008717 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008718
8719 /* Check if port already exists */
8720 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008721 netdev_info(netdev, "vxlan port %d already offloaded\n",
8722 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008723 return;
8724 }
8725
8726 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008727 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008728
8729 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008730 netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008731 ntohs(port));
8732 return;
8733 }
8734
8735 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008736 pf->udp_ports[next_idx].index = port;
8737 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8738 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8739 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008740}
8741
8742/**
8743 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
8744 * @netdev: This physical port's netdev
8745 * @sa_family: Socket Family that VXLAN is notifying us about
8746 * @port: UDP port number that VXLAN stopped listening to
8747 **/
8748static void i40e_del_vxlan_port(struct net_device *netdev,
8749 sa_family_t sa_family, __be16 port)
8750{
8751 struct i40e_netdev_priv *np = netdev_priv(netdev);
8752 struct i40e_vsi *vsi = np->vsi;
8753 struct i40e_pf *pf = vsi->back;
8754 u8 idx;
8755
Singhai, Anjali6a899022015-12-14 12:21:18 -08008756 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008757
8758 /* Check if port already exists */
8759 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8760 /* if port exists, set it to 0 (mark for deletion)
8761 * and make it pending
8762 */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008763 pf->udp_ports[idx].index = 0;
8764 pf->pending_udp_bitmap |= BIT_ULL(idx);
8765 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008766 } else {
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008767 netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008768 ntohs(port));
8769 }
8770}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008771#endif
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008772
Eric Dumazet5cae7612016-01-20 19:53:02 -08008773#if IS_ENABLED(CONFIG_GENEVE)
Singhai, Anjali6a899022015-12-14 12:21:18 -08008774/**
8775 * i40e_add_geneve_port - Get notifications about GENEVE ports that come up
8776 * @netdev: This physical port's netdev
8777 * @sa_family: Socket Family that GENEVE is notifying us about
8778 * @port: New UDP port number that GENEVE started listening to
8779 **/
8780static void i40e_add_geneve_port(struct net_device *netdev,
8781 sa_family_t sa_family, __be16 port)
8782{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008783 struct i40e_netdev_priv *np = netdev_priv(netdev);
8784 struct i40e_vsi *vsi = np->vsi;
8785 struct i40e_pf *pf = vsi->back;
8786 u8 next_idx;
8787 u8 idx;
8788
Anjali Singhaia340c782016-01-06 11:49:28 -08008789 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8790 return;
8791
Singhai, Anjali6a899022015-12-14 12:21:18 -08008792 idx = i40e_get_udp_port_idx(pf, port);
8793
8794 /* Check if port already exists */
8795 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8796 netdev_info(netdev, "udp port %d already offloaded\n",
8797 ntohs(port));
8798 return;
8799 }
8800
8801 /* Now check if there is space to add the new port */
8802 next_idx = i40e_get_udp_port_idx(pf, 0);
8803
8804 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8805 netdev_info(netdev, "maximum number of UDP ports reached, not adding port %d\n",
8806 ntohs(port));
8807 return;
8808 }
8809
8810 /* New port: add it and mark its index in the bitmap */
8811 pf->udp_ports[next_idx].index = port;
8812 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8813 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8814 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8815
8816 dev_info(&pf->pdev->dev, "adding geneve port %d\n", ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008817}
8818
8819/**
8820 * i40e_del_geneve_port - Get notifications about GENEVE ports that go away
8821 * @netdev: This physical port's netdev
8822 * @sa_family: Socket Family that GENEVE is notifying us about
8823 * @port: UDP port number that GENEVE stopped listening to
8824 **/
8825static void i40e_del_geneve_port(struct net_device *netdev,
8826 sa_family_t sa_family, __be16 port)
8827{
Singhai, Anjali6a899022015-12-14 12:21:18 -08008828 struct i40e_netdev_priv *np = netdev_priv(netdev);
8829 struct i40e_vsi *vsi = np->vsi;
8830 struct i40e_pf *pf = vsi->back;
8831 u8 idx;
8832
Anjali Singhaia340c782016-01-06 11:49:28 -08008833 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8834 return;
8835
Singhai, Anjali6a899022015-12-14 12:21:18 -08008836 idx = i40e_get_udp_port_idx(pf, port);
8837
8838 /* Check if port already exists */
8839 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
8840 /* if port exists, set it to 0 (mark for deletion)
8841 * and make it pending
8842 */
8843 pf->udp_ports[idx].index = 0;
8844 pf->pending_udp_bitmap |= BIT_ULL(idx);
8845 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8846
8847 dev_info(&pf->pdev->dev, "deleting geneve port %d\n",
8848 ntohs(port));
8849 } else {
8850 netdev_warn(netdev, "geneve port %d was not found, not deleting\n",
8851 ntohs(port));
8852 }
Singhai, Anjali6a899022015-12-14 12:21:18 -08008853}
Eric Dumazet5cae7612016-01-20 19:53:02 -08008854#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08008855
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008856static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008857 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008858{
8859 struct i40e_netdev_priv *np = netdev_priv(netdev);
8860 struct i40e_pf *pf = np->vsi->back;
8861 struct i40e_hw *hw = &pf->hw;
8862
8863 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8864 return -EOPNOTSUPP;
8865
8866 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8867 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8868
8869 return 0;
8870}
8871
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008872/**
8873 * i40e_ndo_fdb_add - add an entry to the hardware database
8874 * @ndm: the input from the stack
8875 * @tb: pointer to array of nladdr (unused)
8876 * @dev: the net device pointer
8877 * @addr: the MAC address entry being added
8878 * @flags: instructions from stack about fdb operation
8879 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008880static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8881 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008882 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008883 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008884{
8885 struct i40e_netdev_priv *np = netdev_priv(dev);
8886 struct i40e_pf *pf = np->vsi->back;
8887 int err = 0;
8888
8889 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8890 return -EOPNOTSUPP;
8891
Or Gerlitz65891fe2014-12-14 18:19:05 +02008892 if (vid) {
8893 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8894 return -EINVAL;
8895 }
8896
Greg Rose4ba0dea2014-03-06 08:59:55 +00008897 /* Hardware does not support aging addresses so if a
8898 * ndm_state is given only allow permanent addresses
8899 */
8900 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8901 netdev_info(dev, "FDB only supports static addresses\n");
8902 return -EINVAL;
8903 }
8904
8905 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8906 err = dev_uc_add_excl(dev, addr);
8907 else if (is_multicast_ether_addr(addr))
8908 err = dev_mc_add_excl(dev, addr);
8909 else
8910 err = -EINVAL;
8911
8912 /* Only return duplicate errors if NLM_F_EXCL is set */
8913 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8914 err = 0;
8915
8916 return err;
8917}
8918
Neerav Parikh51616012015-02-06 08:52:14 +00008919/**
8920 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8921 * @dev: the netdev being configured
8922 * @nlh: RTNL message
8923 *
8924 * Inserts a new hardware bridge if not already created and
8925 * enables the bridging mode requested (VEB or VEPA). If the
8926 * hardware bridge has already been inserted and the request
8927 * is to change the mode then that requires a PF reset to
8928 * allow rebuild of the components with required hardware
8929 * bridge mode enabled.
8930 **/
8931static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008932 struct nlmsghdr *nlh,
8933 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008934{
8935 struct i40e_netdev_priv *np = netdev_priv(dev);
8936 struct i40e_vsi *vsi = np->vsi;
8937 struct i40e_pf *pf = vsi->back;
8938 struct i40e_veb *veb = NULL;
8939 struct nlattr *attr, *br_spec;
8940 int i, rem;
8941
8942 /* Only for PF VSI for now */
8943 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8944 return -EOPNOTSUPP;
8945
8946 /* Find the HW bridge for PF VSI */
8947 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8948 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8949 veb = pf->veb[i];
8950 }
8951
8952 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8953
8954 nla_for_each_nested(attr, br_spec, rem) {
8955 __u16 mode;
8956
8957 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8958 continue;
8959
8960 mode = nla_get_u16(attr);
8961 if ((mode != BRIDGE_MODE_VEPA) &&
8962 (mode != BRIDGE_MODE_VEB))
8963 return -EINVAL;
8964
8965 /* Insert a new HW bridge */
8966 if (!veb) {
8967 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8968 vsi->tc_config.enabled_tc);
8969 if (veb) {
8970 veb->bridge_mode = mode;
8971 i40e_config_bridge_mode(veb);
8972 } else {
8973 /* No Bridge HW offload available */
8974 return -ENOENT;
8975 }
8976 break;
8977 } else if (mode != veb->bridge_mode) {
8978 /* Existing HW bridge but different mode needs reset */
8979 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008980 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8981 if (mode == BRIDGE_MODE_VEB)
8982 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8983 else
8984 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8985 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008986 break;
8987 }
8988 }
8989
8990 return 0;
8991}
8992
8993/**
8994 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8995 * @skb: skb buff
8996 * @pid: process id
8997 * @seq: RTNL message seq #
8998 * @dev: the netdev being configured
8999 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009000 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009001 *
9002 * Return the mode in which the hardware bridge is operating in
9003 * i.e VEB or VEPA.
9004 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009005static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9006 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009007 u32 __always_unused filter_mask,
9008 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009009{
9010 struct i40e_netdev_priv *np = netdev_priv(dev);
9011 struct i40e_vsi *vsi = np->vsi;
9012 struct i40e_pf *pf = vsi->back;
9013 struct i40e_veb *veb = NULL;
9014 int i;
9015
9016 /* Only for PF VSI for now */
9017 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9018 return -EOPNOTSUPP;
9019
9020 /* Find the HW bridge for the PF VSI */
9021 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9022 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9023 veb = pf->veb[i];
9024 }
9025
9026 if (!veb)
9027 return 0;
9028
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009029 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07009030 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009031}
Neerav Parikh51616012015-02-06 08:52:14 +00009032
Singhai, Anjali6a899022015-12-14 12:21:18 -08009033/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9034 * inner mac plus all inner ethertypes.
9035 */
9036#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009037/**
9038 * i40e_features_check - Validate encapsulated packet conforms to limits
9039 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009040 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009041 * @features: Offload features that the stack believes apply
9042 **/
9043static netdev_features_t i40e_features_check(struct sk_buff *skb,
9044 struct net_device *dev,
9045 netdev_features_t features)
9046{
9047 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009048 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009049 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009050 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009051
9052 return features;
9053}
9054
Shannon Nelson37a29732015-02-27 09:15:19 +00009055static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009056 .ndo_open = i40e_open,
9057 .ndo_stop = i40e_close,
9058 .ndo_start_xmit = i40e_lan_xmit_frame,
9059 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9060 .ndo_set_rx_mode = i40e_set_rx_mode,
9061 .ndo_validate_addr = eth_validate_addr,
9062 .ndo_set_mac_address = i40e_set_mac,
9063 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009064 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009065 .ndo_tx_timeout = i40e_tx_timeout,
9066 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9067 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9068#ifdef CONFIG_NET_POLL_CONTROLLER
9069 .ndo_poll_controller = i40e_netpoll,
9070#endif
John Fastabende4c67342016-02-16 21:16:15 -08009071 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009072#ifdef I40E_FCOE
9073 .ndo_fcoe_enable = i40e_fcoe_enable,
9074 .ndo_fcoe_disable = i40e_fcoe_disable,
9075#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009076 .ndo_set_features = i40e_set_features,
9077 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9078 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009079 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009080 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009081 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009082 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009083 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Singhai, Anjali6a899022015-12-14 12:21:18 -08009084#if IS_ENABLED(CONFIG_VXLAN)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00009085 .ndo_add_vxlan_port = i40e_add_vxlan_port,
9086 .ndo_del_vxlan_port = i40e_del_vxlan_port,
9087#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -08009088#if IS_ENABLED(CONFIG_GENEVE)
9089 .ndo_add_geneve_port = i40e_add_geneve_port,
9090 .ndo_del_geneve_port = i40e_del_geneve_port,
9091#endif
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009092 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009093 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009094 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009095 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9096 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009097};
9098
9099/**
9100 * i40e_config_netdev - Setup the netdev flags
9101 * @vsi: the VSI being configured
9102 *
9103 * Returns 0 on success, negative value on failure
9104 **/
9105static int i40e_config_netdev(struct i40e_vsi *vsi)
9106{
Greg Rose1a103702013-11-28 06:42:39 +00009107 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009108 struct i40e_pf *pf = vsi->back;
9109 struct i40e_hw *hw = &pf->hw;
9110 struct i40e_netdev_priv *np;
9111 struct net_device *netdev;
9112 u8 mac_addr[ETH_ALEN];
9113 int etherdev_size;
9114
9115 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009116 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009117 if (!netdev)
9118 return -ENOMEM;
9119
9120 vsi->netdev = netdev;
9121 np = netdev_priv(netdev);
9122 np->vsi = vsi;
9123
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009124 netdev->hw_enc_features |= NETIF_F_SG |
9125 NETIF_F_IP_CSUM |
9126 NETIF_F_IPV6_CSUM |
9127 NETIF_F_HIGHDMA |
9128 NETIF_F_SOFT_FEATURES |
9129 NETIF_F_TSO |
9130 NETIF_F_TSO_ECN |
9131 NETIF_F_TSO6 |
9132 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009133 NETIF_F_GSO_GRE_CSUM |
Alexander Duyck577389a2016-04-02 00:06:56 -07009134 NETIF_F_GSO_IPIP |
9135 NETIF_F_GSO_SIT |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009136 NETIF_F_GSO_UDP_TUNNEL |
9137 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009138 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009139 NETIF_F_SCTP_CRC |
9140 NETIF_F_RXHASH |
9141 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009142 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009143
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009144 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009145 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9146
9147 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009148
9149 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009150 netdev->vlan_features |= netdev->hw_enc_features |
9151 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009152
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009153 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009154 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009155
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009156 netdev->hw_features |= netdev->hw_enc_features |
9157 NETIF_F_HW_VLAN_CTAG_TX |
9158 NETIF_F_HW_VLAN_CTAG_RX;
9159
9160 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009161 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009162
9163 if (vsi->type == I40E_VSI_MAIN) {
9164 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009165 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009166 /* The following steps are necessary to prevent reception
9167 * of tagged packets - some older NVM configurations load a
9168 * default a MAC-VLAN filter that accepts any tagged packet
9169 * which must be replaced by a normal filter.
Greg Rose8c27d422014-05-22 06:31:56 +00009170 */
Kiran Patil21659032015-09-30 14:09:03 -04009171 if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
9172 spin_lock_bh(&vsi->mac_filter_list_lock);
Shannon Nelson30650cc2014-07-29 04:01:50 +00009173 i40e_add_filter(vsi, mac_addr,
9174 I40E_VLAN_ANY, false, true);
Kiran Patil21659032015-09-30 14:09:03 -04009175 spin_unlock_bh(&vsi->mac_filter_list_lock);
9176 }
Anjali Singhai Jain3845cce2016-03-18 12:18:05 -07009177 } else if ((pf->hw.aq.api_maj_ver > 1) ||
9178 ((pf->hw.aq.api_maj_ver == 1) &&
9179 (pf->hw.aq.api_min_ver > 4))) {
9180 /* Supported in FW API version higher than 1.4 */
9181 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
9182 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009183 } else {
9184 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9185 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9186 pf->vsi[pf->lan_vsi]->netdev->name);
9187 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009188
9189 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009190 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009191 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009192 }
Kiran Patil21659032015-09-30 14:09:03 -04009193
9194 spin_lock_bh(&vsi->mac_filter_list_lock);
Greg Rose1a103702013-11-28 06:42:39 +00009195 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009196 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009197
Greg Rose9a173902014-05-22 06:32:02 +00009198 ether_addr_copy(netdev->dev_addr, mac_addr);
9199 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009201 netdev->priv_flags |= IFF_UNICAST_FLT;
9202 netdev->priv_flags |= IFF_SUPP_NOFCS;
9203 /* Setup netdev TC information */
9204 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9205
9206 netdev->netdev_ops = &i40e_netdev_ops;
9207 netdev->watchdog_timeo = 5 * HZ;
9208 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009209#ifdef I40E_FCOE
9210 i40e_fcoe_config_netdev(netdev, vsi);
9211#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009212
9213 return 0;
9214}
9215
9216/**
9217 * i40e_vsi_delete - Delete a VSI from the switch
9218 * @vsi: the VSI being removed
9219 *
9220 * Returns 0 on success, negative value on failure
9221 **/
9222static void i40e_vsi_delete(struct i40e_vsi *vsi)
9223{
9224 /* remove default VSI is not allowed */
9225 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9226 return;
9227
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009228 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009229}
9230
9231/**
Neerav Parikh51616012015-02-06 08:52:14 +00009232 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9233 * @vsi: the VSI being queried
9234 *
9235 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9236 **/
9237int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9238{
9239 struct i40e_veb *veb;
9240 struct i40e_pf *pf = vsi->back;
9241
9242 /* Uplink is not a bridge so default to VEB */
9243 if (vsi->veb_idx == I40E_NO_VEB)
9244 return 1;
9245
9246 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009247 if (!veb) {
9248 dev_info(&pf->pdev->dev,
9249 "There is no veb associated with the bridge\n");
9250 return -ENOENT;
9251 }
Neerav Parikh51616012015-02-06 08:52:14 +00009252
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009253 /* Uplink is a bridge in VEPA mode */
9254 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9255 return 0;
9256 } else {
9257 /* Uplink is a bridge in VEB mode */
9258 return 1;
9259 }
9260
9261 /* VEPA is now default bridge, so return 0 */
9262 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009263}
9264
9265/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009266 * i40e_add_vsi - Add a VSI to the switch
9267 * @vsi: the VSI being configured
9268 *
9269 * This initializes a VSI context depending on the VSI type to be added and
9270 * passes it down to the add_vsi aq command.
9271 **/
9272static int i40e_add_vsi(struct i40e_vsi *vsi)
9273{
9274 int ret = -ENODEV;
Kiran Patil21659032015-09-30 14:09:03 -04009275 u8 laa_macaddr[ETH_ALEN];
9276 bool found_laa_mac_filter = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277 struct i40e_pf *pf = vsi->back;
9278 struct i40e_hw *hw = &pf->hw;
9279 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009280 struct i40e_mac_filter *f, *ftmp;
9281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009282 u8 enabled_tc = 0x1; /* TC0 enabled */
9283 int f_count = 0;
9284
9285 memset(&ctxt, 0, sizeof(ctxt));
9286 switch (vsi->type) {
9287 case I40E_VSI_MAIN:
9288 /* The PF's main VSI is already setup as part of the
9289 * device initialization, so we'll not bother with
9290 * the add_vsi call, but we will retrieve the current
9291 * VSI context.
9292 */
9293 ctxt.seid = pf->main_vsi_seid;
9294 ctxt.pf_num = pf->hw.pf_id;
9295 ctxt.vf_num = 0;
9296 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9297 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9298 if (ret) {
9299 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009300 "couldn't get PF vsi config, err %s aq_err %s\n",
9301 i40e_stat_str(&pf->hw, ret),
9302 i40e_aq_str(&pf->hw,
9303 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009304 return -ENOENT;
9305 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009306 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009307 vsi->info.valid_sections = 0;
9308
9309 vsi->seid = ctxt.seid;
9310 vsi->id = ctxt.vsi_number;
9311
9312 enabled_tc = i40e_pf_get_tc_map(pf);
9313
9314 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009315 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9316 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009317 memset(&ctxt, 0, sizeof(ctxt));
9318 ctxt.seid = pf->main_vsi_seid;
9319 ctxt.pf_num = pf->hw.pf_id;
9320 ctxt.vf_num = 0;
9321 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9322 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9323 if (ret) {
9324 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009325 "update vsi failed, err %s aq_err %s\n",
9326 i40e_stat_str(&pf->hw, ret),
9327 i40e_aq_str(&pf->hw,
9328 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009329 ret = -ENOENT;
9330 goto err;
9331 }
9332 /* update the local VSI info queue map */
9333 i40e_vsi_update_queue_map(vsi, &ctxt);
9334 vsi->info.valid_sections = 0;
9335 } else {
9336 /* Default/Main VSI is only enabled for TC0
9337 * reconfigure it to enable all TCs that are
9338 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009339 * For MFP case the iSCSI PF would use this
9340 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009341 */
9342 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9343 if (ret) {
9344 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009345 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9346 enabled_tc,
9347 i40e_stat_str(&pf->hw, ret),
9348 i40e_aq_str(&pf->hw,
9349 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009350 ret = -ENOENT;
9351 }
9352 }
9353 break;
9354
9355 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009356 ctxt.pf_num = hw->pf_id;
9357 ctxt.vf_num = 0;
9358 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009359 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009360 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009361 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9362 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009363 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009364 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009365 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009366 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009367 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009368 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009369 break;
9370
9371 case I40E_VSI_VMDQ2:
9372 ctxt.pf_num = hw->pf_id;
9373 ctxt.vf_num = 0;
9374 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009375 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009376 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009378 /* This VSI is connected to VEB so the switch_id
9379 * should be set to zero by default.
9380 */
Neerav Parikh51616012015-02-06 08:52:14 +00009381 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9382 ctxt.info.valid_sections |=
9383 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9384 ctxt.info.switch_id =
9385 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9386 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009387
9388 /* Setup the VSI tx/rx queue map for TC0 only for now */
9389 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9390 break;
9391
9392 case I40E_VSI_SRIOV:
9393 ctxt.pf_num = hw->pf_id;
9394 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9395 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009396 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009397 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9398
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009399 /* This VSI is connected to VEB so the switch_id
9400 * should be set to zero by default.
9401 */
Neerav Parikh51616012015-02-06 08:52:14 +00009402 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9403 ctxt.info.valid_sections |=
9404 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9405 ctxt.info.switch_id =
9406 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9407 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009408
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009409 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9410 ctxt.info.valid_sections |=
9411 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9412 ctxt.info.queueing_opt_flags |=
9413 I40E_AQ_VSI_QUE_OPT_TCP_ENA;
9414 }
9415
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009416 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9417 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009418 if (pf->vf[vsi->vf_id].spoofchk) {
9419 ctxt.info.valid_sections |=
9420 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9421 ctxt.info.sec_flags |=
9422 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9423 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9424 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009425 /* Setup the VSI tx/rx queue map for TC0 only for now */
9426 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9427 break;
9428
Vasu Dev38e00432014-08-01 13:27:03 -07009429#ifdef I40E_FCOE
9430 case I40E_VSI_FCOE:
9431 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9432 if (ret) {
9433 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9434 return ret;
9435 }
9436 break;
9437
9438#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009439 case I40E_VSI_IWARP:
9440 /* send down message to iWARP */
9441 break;
9442
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009443 default:
9444 return -ENODEV;
9445 }
9446
9447 if (vsi->type != I40E_VSI_MAIN) {
9448 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9449 if (ret) {
9450 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009451 "add vsi failed, err %s aq_err %s\n",
9452 i40e_stat_str(&pf->hw, ret),
9453 i40e_aq_str(&pf->hw,
9454 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009455 ret = -ENOENT;
9456 goto err;
9457 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009458 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009459 vsi->info.valid_sections = 0;
9460 vsi->seid = ctxt.seid;
9461 vsi->id = ctxt.vsi_number;
9462 }
9463
Kiran Patil21659032015-09-30 14:09:03 -04009464 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009465 /* If macvlan filters already exist, force them to get loaded */
9466 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
9467 f->changed = true;
9468 f_count++;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009469
Kiran Patil21659032015-09-30 14:09:03 -04009470 /* Expected to have only one MAC filter entry for LAA in list */
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009471 if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
Kiran Patil21659032015-09-30 14:09:03 -04009472 ether_addr_copy(laa_macaddr, f->macaddr);
9473 found_laa_mac_filter = true;
Shannon Nelson6252c7e2014-06-04 01:23:23 +00009474 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009475 }
Kiran Patil21659032015-09-30 14:09:03 -04009476 spin_unlock_bh(&vsi->mac_filter_list_lock);
9477
9478 if (found_laa_mac_filter) {
9479 struct i40e_aqc_remove_macvlan_element_data element;
9480
9481 memset(&element, 0, sizeof(element));
9482 ether_addr_copy(element.mac_addr, laa_macaddr);
9483 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
9484 ret = i40e_aq_remove_macvlan(hw, vsi->seid,
9485 &element, 1, NULL);
9486 if (ret) {
9487 /* some older FW has a different default */
9488 element.flags |=
9489 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
9490 i40e_aq_remove_macvlan(hw, vsi->seid,
9491 &element, 1, NULL);
9492 }
9493
9494 i40e_aq_mac_address_write(hw,
9495 I40E_AQC_WRITE_TYPE_LAA_WOL,
9496 laa_macaddr, NULL);
9497 }
9498
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009499 if (f_count) {
9500 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9501 pf->flags |= I40E_FLAG_FILTER_SYNC;
9502 }
9503
9504 /* Update VSI BW information */
9505 ret = i40e_vsi_get_bw_info(vsi);
9506 if (ret) {
9507 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009508 "couldn't get vsi bw info, err %s aq_err %s\n",
9509 i40e_stat_str(&pf->hw, ret),
9510 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009511 /* VSI is already added so not tearing that up */
9512 ret = 0;
9513 }
9514
9515err:
9516 return ret;
9517}
9518
9519/**
9520 * i40e_vsi_release - Delete a VSI and free its resources
9521 * @vsi: the VSI being removed
9522 *
9523 * Returns 0 on success or < 0 on error
9524 **/
9525int i40e_vsi_release(struct i40e_vsi *vsi)
9526{
9527 struct i40e_mac_filter *f, *ftmp;
9528 struct i40e_veb *veb = NULL;
9529 struct i40e_pf *pf;
9530 u16 uplink_seid;
9531 int i, n;
9532
9533 pf = vsi->back;
9534
9535 /* release of a VEB-owner or last VSI is not allowed */
9536 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9537 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9538 vsi->seid, vsi->uplink_seid);
9539 return -ENODEV;
9540 }
9541 if (vsi == pf->vsi[pf->lan_vsi] &&
9542 !test_bit(__I40E_DOWN, &pf->state)) {
9543 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9544 return -ENODEV;
9545 }
9546
9547 uplink_seid = vsi->uplink_seid;
9548 if (vsi->type != I40E_VSI_SRIOV) {
9549 if (vsi->netdev_registered) {
9550 vsi->netdev_registered = false;
9551 if (vsi->netdev) {
9552 /* results in a call to i40e_close() */
9553 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009554 }
9555 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009556 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009557 }
9558 i40e_vsi_disable_irq(vsi);
9559 }
9560
Kiran Patil21659032015-09-30 14:09:03 -04009561 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009562 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9563 i40e_del_filter(vsi, f->macaddr, f->vlan,
9564 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009565 spin_unlock_bh(&vsi->mac_filter_list_lock);
9566
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009567 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009568
9569 i40e_vsi_delete(vsi);
9570 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009571 if (vsi->netdev) {
9572 free_netdev(vsi->netdev);
9573 vsi->netdev = NULL;
9574 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009575 i40e_vsi_clear_rings(vsi);
9576 i40e_vsi_clear(vsi);
9577
9578 /* If this was the last thing on the VEB, except for the
9579 * controlling VSI, remove the VEB, which puts the controlling
9580 * VSI onto the next level down in the switch.
9581 *
9582 * Well, okay, there's one more exception here: don't remove
9583 * the orphan VEBs yet. We'll wait for an explicit remove request
9584 * from up the network stack.
9585 */
Mitch Williams505682c2014-05-20 08:01:37 +00009586 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009587 if (pf->vsi[i] &&
9588 pf->vsi[i]->uplink_seid == uplink_seid &&
9589 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9590 n++; /* count the VSIs */
9591 }
9592 }
9593 for (i = 0; i < I40E_MAX_VEB; i++) {
9594 if (!pf->veb[i])
9595 continue;
9596 if (pf->veb[i]->uplink_seid == uplink_seid)
9597 n++; /* count the VEBs */
9598 if (pf->veb[i]->seid == uplink_seid)
9599 veb = pf->veb[i];
9600 }
9601 if (n == 0 && veb && veb->uplink_seid != 0)
9602 i40e_veb_release(veb);
9603
9604 return 0;
9605}
9606
9607/**
9608 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9609 * @vsi: ptr to the VSI
9610 *
9611 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9612 * corresponding SW VSI structure and initializes num_queue_pairs for the
9613 * newly allocated VSI.
9614 *
9615 * Returns 0 on success or negative on failure
9616 **/
9617static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9618{
9619 int ret = -ENOENT;
9620 struct i40e_pf *pf = vsi->back;
9621
Alexander Duyck493fb302013-09-28 07:01:44 +00009622 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009623 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9624 vsi->seid);
9625 return -EEXIST;
9626 }
9627
9628 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009629 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009630 vsi->seid, vsi->base_vector);
9631 return -EEXIST;
9632 }
9633
Greg Rose90e04072014-03-06 08:59:57 +00009634 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009635 if (ret) {
9636 dev_info(&pf->pdev->dev,
9637 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9638 vsi->num_q_vectors, vsi->seid, ret);
9639 vsi->num_q_vectors = 0;
9640 goto vector_setup_out;
9641 }
9642
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009643 /* In Legacy mode, we do not have to get any other vector since we
9644 * piggyback on the misc/ICR0 for queue interrupts.
9645 */
9646 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9647 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009648 if (vsi->num_q_vectors)
9649 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9650 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009651 if (vsi->base_vector < 0) {
9652 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009653 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9654 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009655 i40e_vsi_free_q_vectors(vsi);
9656 ret = -ENOENT;
9657 goto vector_setup_out;
9658 }
9659
9660vector_setup_out:
9661 return ret;
9662}
9663
9664/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009665 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9666 * @vsi: pointer to the vsi.
9667 *
9668 * This re-allocates a vsi's queue resources.
9669 *
9670 * Returns pointer to the successfully allocated and configured VSI sw struct
9671 * on success, otherwise returns NULL on failure.
9672 **/
9673static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9674{
John Underwoodf5340392016-02-18 09:19:24 -08009675 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009676 u8 enabled_tc;
9677 int ret;
9678
John Underwoodf5340392016-02-18 09:19:24 -08009679 if (!vsi)
9680 return NULL;
9681
9682 pf = vsi->back;
9683
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009684 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9685 i40e_vsi_clear_rings(vsi);
9686
9687 i40e_vsi_free_arrays(vsi, false);
9688 i40e_set_num_rings_in_vsi(vsi);
9689 ret = i40e_vsi_alloc_arrays(vsi, false);
9690 if (ret)
9691 goto err_vsi;
9692
9693 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9694 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009695 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009696 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009697 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009698 goto err_vsi;
9699 }
9700 vsi->base_queue = ret;
9701
9702 /* Update the FW view of the VSI. Force a reset of TC and queue
9703 * layout configurations.
9704 */
9705 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9706 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9707 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9708 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9709
9710 /* assign it some queues */
9711 ret = i40e_alloc_rings(vsi);
9712 if (ret)
9713 goto err_rings;
9714
9715 /* map all of the rings to the q_vectors */
9716 i40e_vsi_map_rings_to_vectors(vsi);
9717 return vsi;
9718
9719err_rings:
9720 i40e_vsi_free_q_vectors(vsi);
9721 if (vsi->netdev_registered) {
9722 vsi->netdev_registered = false;
9723 unregister_netdev(vsi->netdev);
9724 free_netdev(vsi->netdev);
9725 vsi->netdev = NULL;
9726 }
9727 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9728err_vsi:
9729 i40e_vsi_clear(vsi);
9730 return NULL;
9731}
9732
9733/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009734 * i40e_macaddr_init - explicitly write the mac address filters.
9735 *
9736 * @vsi: pointer to the vsi.
9737 * @macaddr: the MAC address
9738 *
9739 * This is needed when the macaddr has been obtained by other
9740 * means than the default, e.g., from Open Firmware or IDPROM.
9741 * Returns 0 on success, negative on failure
9742 **/
9743static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
9744{
9745 int ret;
9746 struct i40e_aqc_add_macvlan_element_data element;
9747
9748 ret = i40e_aq_mac_address_write(&vsi->back->hw,
9749 I40E_AQC_WRITE_TYPE_LAA_WOL,
9750 macaddr, NULL);
9751 if (ret) {
9752 dev_info(&vsi->back->pdev->dev,
9753 "Addr change for VSI failed: %d\n", ret);
9754 return -EADDRNOTAVAIL;
9755 }
9756
9757 memset(&element, 0, sizeof(element));
9758 ether_addr_copy(element.mac_addr, macaddr);
9759 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
9760 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
9761 if (ret) {
9762 dev_info(&vsi->back->pdev->dev,
9763 "add filter failed err %s aq_err %s\n",
9764 i40e_stat_str(&vsi->back->hw, ret),
9765 i40e_aq_str(&vsi->back->hw,
9766 vsi->back->hw.aq.asq_last_status));
9767 }
9768 return ret;
9769}
9770
9771/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009772 * i40e_vsi_setup - Set up a VSI by a given type
9773 * @pf: board private structure
9774 * @type: VSI type
9775 * @uplink_seid: the switch element to link to
9776 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9777 *
9778 * This allocates the sw VSI structure and its queue resources, then add a VSI
9779 * to the identified VEB.
9780 *
9781 * Returns pointer to the successfully allocated and configure VSI sw struct on
9782 * success, otherwise returns NULL on failure.
9783 **/
9784struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9785 u16 uplink_seid, u32 param1)
9786{
9787 struct i40e_vsi *vsi = NULL;
9788 struct i40e_veb *veb = NULL;
9789 int ret, i;
9790 int v_idx;
9791
9792 /* The requested uplink_seid must be either
9793 * - the PF's port seid
9794 * no VEB is needed because this is the PF
9795 * or this is a Flow Director special case VSI
9796 * - seid of an existing VEB
9797 * - seid of a VSI that owns an existing VEB
9798 * - seid of a VSI that doesn't own a VEB
9799 * a new VEB is created and the VSI becomes the owner
9800 * - seid of the PF VSI, which is what creates the first VEB
9801 * this is a special case of the previous
9802 *
9803 * Find which uplink_seid we were given and create a new VEB if needed
9804 */
9805 for (i = 0; i < I40E_MAX_VEB; i++) {
9806 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9807 veb = pf->veb[i];
9808 break;
9809 }
9810 }
9811
9812 if (!veb && uplink_seid != pf->mac_seid) {
9813
Mitch Williams505682c2014-05-20 08:01:37 +00009814 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009815 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9816 vsi = pf->vsi[i];
9817 break;
9818 }
9819 }
9820 if (!vsi) {
9821 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9822 uplink_seid);
9823 return NULL;
9824 }
9825
9826 if (vsi->uplink_seid == pf->mac_seid)
9827 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9828 vsi->tc_config.enabled_tc);
9829 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9830 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9831 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009832 if (veb) {
9833 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9834 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009835 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009836 return NULL;
9837 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009838 /* We come up by default in VEPA mode if SRIOV is not
9839 * already enabled, in which case we can't force VEPA
9840 * mode.
9841 */
9842 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9843 veb->bridge_mode = BRIDGE_MODE_VEPA;
9844 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9845 }
Neerav Parikh51616012015-02-06 08:52:14 +00009846 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009847 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009848 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9849 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9850 veb = pf->veb[i];
9851 }
9852 if (!veb) {
9853 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9854 return NULL;
9855 }
9856
9857 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9858 uplink_seid = veb->seid;
9859 }
9860
9861 /* get vsi sw struct */
9862 v_idx = i40e_vsi_mem_alloc(pf, type);
9863 if (v_idx < 0)
9864 goto err_alloc;
9865 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009866 if (!vsi)
9867 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009868 vsi->type = type;
9869 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9870
9871 if (type == I40E_VSI_MAIN)
9872 pf->lan_vsi = v_idx;
9873 else if (type == I40E_VSI_SRIOV)
9874 vsi->vf_id = param1;
9875 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009876 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9877 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009878 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009879 dev_info(&pf->pdev->dev,
9880 "failed to get tracking for %d queues for VSI %d err=%d\n",
9881 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009882 goto err_vsi;
9883 }
9884 vsi->base_queue = ret;
9885
9886 /* get a VSI from the hardware */
9887 vsi->uplink_seid = uplink_seid;
9888 ret = i40e_add_vsi(vsi);
9889 if (ret)
9890 goto err_vsi;
9891
9892 switch (vsi->type) {
9893 /* setup the netdev if needed */
9894 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009895 /* Apply relevant filters if a platform-specific mac
9896 * address was selected.
9897 */
9898 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9899 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9900 if (ret) {
9901 dev_warn(&pf->pdev->dev,
9902 "could not set up macaddr; err %d\n",
9903 ret);
9904 }
9905 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009906 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009907 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009908 ret = i40e_config_netdev(vsi);
9909 if (ret)
9910 goto err_netdev;
9911 ret = register_netdev(vsi->netdev);
9912 if (ret)
9913 goto err_netdev;
9914 vsi->netdev_registered = true;
9915 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009916#ifdef CONFIG_I40E_DCB
9917 /* Setup DCB netlink interface */
9918 i40e_dcbnl_setup(vsi);
9919#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009920 /* fall through */
9921
9922 case I40E_VSI_FDIR:
9923 /* set up vectors and rings if needed */
9924 ret = i40e_vsi_setup_vectors(vsi);
9925 if (ret)
9926 goto err_msix;
9927
9928 ret = i40e_alloc_rings(vsi);
9929 if (ret)
9930 goto err_rings;
9931
9932 /* map all of the rings to the q_vectors */
9933 i40e_vsi_map_rings_to_vectors(vsi);
9934
9935 i40e_vsi_reset_stats(vsi);
9936 break;
9937
9938 default:
9939 /* no netdev or rings for the other VSI types */
9940 break;
9941 }
9942
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04009943 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9944 (vsi->type == I40E_VSI_VMDQ2)) {
9945 ret = i40e_vsi_config_rss(vsi);
9946 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009947 return vsi;
9948
9949err_rings:
9950 i40e_vsi_free_q_vectors(vsi);
9951err_msix:
9952 if (vsi->netdev_registered) {
9953 vsi->netdev_registered = false;
9954 unregister_netdev(vsi->netdev);
9955 free_netdev(vsi->netdev);
9956 vsi->netdev = NULL;
9957 }
9958err_netdev:
9959 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9960err_vsi:
9961 i40e_vsi_clear(vsi);
9962err_alloc:
9963 return NULL;
9964}
9965
9966/**
9967 * i40e_veb_get_bw_info - Query VEB BW information
9968 * @veb: the veb to query
9969 *
9970 * Query the Tx scheduler BW configuration data for given VEB
9971 **/
9972static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9973{
9974 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9975 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9976 struct i40e_pf *pf = veb->pf;
9977 struct i40e_hw *hw = &pf->hw;
9978 u32 tc_bw_max;
9979 int ret = 0;
9980 int i;
9981
9982 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9983 &bw_data, NULL);
9984 if (ret) {
9985 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009986 "query veb bw config failed, err %s aq_err %s\n",
9987 i40e_stat_str(&pf->hw, ret),
9988 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009989 goto out;
9990 }
9991
9992 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9993 &ets_data, NULL);
9994 if (ret) {
9995 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009996 "query veb bw ets config failed, err %s aq_err %s\n",
9997 i40e_stat_str(&pf->hw, ret),
9998 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009999 goto out;
10000 }
10001
10002 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10003 veb->bw_max_quanta = ets_data.tc_bw_max;
10004 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010005 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010006 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10007 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10008 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10009 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10010 veb->bw_tc_limit_credits[i] =
10011 le16_to_cpu(bw_data.tc_bw_limits[i]);
10012 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10013 }
10014
10015out:
10016 return ret;
10017}
10018
10019/**
10020 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10021 * @pf: board private structure
10022 *
10023 * On error: returns error code (negative)
10024 * On success: returns vsi index in PF (positive)
10025 **/
10026static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10027{
10028 int ret = -ENOENT;
10029 struct i40e_veb *veb;
10030 int i;
10031
10032 /* Need to protect the allocation of switch elements at the PF level */
10033 mutex_lock(&pf->switch_mutex);
10034
10035 /* VEB list may be fragmented if VEB creation/destruction has
10036 * been happening. We can afford to do a quick scan to look
10037 * for any free slots in the list.
10038 *
10039 * find next empty veb slot, looping back around if necessary
10040 */
10041 i = 0;
10042 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10043 i++;
10044 if (i >= I40E_MAX_VEB) {
10045 ret = -ENOMEM;
10046 goto err_alloc_veb; /* out of VEB slots! */
10047 }
10048
10049 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10050 if (!veb) {
10051 ret = -ENOMEM;
10052 goto err_alloc_veb;
10053 }
10054 veb->pf = pf;
10055 veb->idx = i;
10056 veb->enabled_tc = 1;
10057
10058 pf->veb[i] = veb;
10059 ret = i;
10060err_alloc_veb:
10061 mutex_unlock(&pf->switch_mutex);
10062 return ret;
10063}
10064
10065/**
10066 * i40e_switch_branch_release - Delete a branch of the switch tree
10067 * @branch: where to start deleting
10068 *
10069 * This uses recursion to find the tips of the branch to be
10070 * removed, deleting until we get back to and can delete this VEB.
10071 **/
10072static void i40e_switch_branch_release(struct i40e_veb *branch)
10073{
10074 struct i40e_pf *pf = branch->pf;
10075 u16 branch_seid = branch->seid;
10076 u16 veb_idx = branch->idx;
10077 int i;
10078
10079 /* release any VEBs on this VEB - RECURSION */
10080 for (i = 0; i < I40E_MAX_VEB; i++) {
10081 if (!pf->veb[i])
10082 continue;
10083 if (pf->veb[i]->uplink_seid == branch->seid)
10084 i40e_switch_branch_release(pf->veb[i]);
10085 }
10086
10087 /* Release the VSIs on this VEB, but not the owner VSI.
10088 *
10089 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10090 * the VEB itself, so don't use (*branch) after this loop.
10091 */
Mitch Williams505682c2014-05-20 08:01:37 +000010092 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010093 if (!pf->vsi[i])
10094 continue;
10095 if (pf->vsi[i]->uplink_seid == branch_seid &&
10096 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10097 i40e_vsi_release(pf->vsi[i]);
10098 }
10099 }
10100
10101 /* There's one corner case where the VEB might not have been
10102 * removed, so double check it here and remove it if needed.
10103 * This case happens if the veb was created from the debugfs
10104 * commands and no VSIs were added to it.
10105 */
10106 if (pf->veb[veb_idx])
10107 i40e_veb_release(pf->veb[veb_idx]);
10108}
10109
10110/**
10111 * i40e_veb_clear - remove veb struct
10112 * @veb: the veb to remove
10113 **/
10114static void i40e_veb_clear(struct i40e_veb *veb)
10115{
10116 if (!veb)
10117 return;
10118
10119 if (veb->pf) {
10120 struct i40e_pf *pf = veb->pf;
10121
10122 mutex_lock(&pf->switch_mutex);
10123 if (pf->veb[veb->idx] == veb)
10124 pf->veb[veb->idx] = NULL;
10125 mutex_unlock(&pf->switch_mutex);
10126 }
10127
10128 kfree(veb);
10129}
10130
10131/**
10132 * i40e_veb_release - Delete a VEB and free its resources
10133 * @veb: the VEB being removed
10134 **/
10135void i40e_veb_release(struct i40e_veb *veb)
10136{
10137 struct i40e_vsi *vsi = NULL;
10138 struct i40e_pf *pf;
10139 int i, n = 0;
10140
10141 pf = veb->pf;
10142
10143 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010144 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010145 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10146 n++;
10147 vsi = pf->vsi[i];
10148 }
10149 }
10150 if (n != 1) {
10151 dev_info(&pf->pdev->dev,
10152 "can't remove VEB %d with %d VSIs left\n",
10153 veb->seid, n);
10154 return;
10155 }
10156
10157 /* move the remaining VSI to uplink veb */
10158 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10159 if (veb->uplink_seid) {
10160 vsi->uplink_seid = veb->uplink_seid;
10161 if (veb->uplink_seid == pf->mac_seid)
10162 vsi->veb_idx = I40E_NO_VEB;
10163 else
10164 vsi->veb_idx = veb->veb_idx;
10165 } else {
10166 /* floating VEB */
10167 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10168 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10169 }
10170
10171 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10172 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010173}
10174
10175/**
10176 * i40e_add_veb - create the VEB in the switch
10177 * @veb: the VEB to be instantiated
10178 * @vsi: the controlling VSI
10179 **/
10180static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10181{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010182 struct i40e_pf *pf = veb->pf;
Anjali Singhai Jain92faef82015-07-28 13:02:00 -040010183 bool is_default = veb->pf->cur_promisc;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010184 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010185 int ret;
10186
10187 /* get a VEB from the hardware */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010188 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Kevin Scotte1c51b952013-11-20 10:02:51 +000010189 veb->enabled_tc, is_default,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010190 &veb->seid, enable_stats, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010191 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010192 dev_info(&pf->pdev->dev,
10193 "couldn't add VEB, err %s aq_err %s\n",
10194 i40e_stat_str(&pf->hw, ret),
10195 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010196 return -EPERM;
10197 }
10198
10199 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010200 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010201 &veb->stats_idx, NULL, NULL, NULL);
10202 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010203 dev_info(&pf->pdev->dev,
10204 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10205 i40e_stat_str(&pf->hw, ret),
10206 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010207 return -EPERM;
10208 }
10209 ret = i40e_veb_get_bw_info(veb);
10210 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010211 dev_info(&pf->pdev->dev,
10212 "couldn't get VEB bw info, err %s aq_err %s\n",
10213 i40e_stat_str(&pf->hw, ret),
10214 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10215 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010216 return -ENOENT;
10217 }
10218
10219 vsi->uplink_seid = veb->seid;
10220 vsi->veb_idx = veb->idx;
10221 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10222
10223 return 0;
10224}
10225
10226/**
10227 * i40e_veb_setup - Set up a VEB
10228 * @pf: board private structure
10229 * @flags: VEB setup flags
10230 * @uplink_seid: the switch element to link to
10231 * @vsi_seid: the initial VSI seid
10232 * @enabled_tc: Enabled TC bit-map
10233 *
10234 * This allocates the sw VEB structure and links it into the switch
10235 * It is possible and legal for this to be a duplicate of an already
10236 * existing VEB. It is also possible for both uplink and vsi seids
10237 * to be zero, in order to create a floating VEB.
10238 *
10239 * Returns pointer to the successfully allocated VEB sw struct on
10240 * success, otherwise returns NULL on failure.
10241 **/
10242struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10243 u16 uplink_seid, u16 vsi_seid,
10244 u8 enabled_tc)
10245{
10246 struct i40e_veb *veb, *uplink_veb = NULL;
10247 int vsi_idx, veb_idx;
10248 int ret;
10249
10250 /* if one seid is 0, the other must be 0 to create a floating relay */
10251 if ((uplink_seid == 0 || vsi_seid == 0) &&
10252 (uplink_seid + vsi_seid != 0)) {
10253 dev_info(&pf->pdev->dev,
10254 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10255 uplink_seid, vsi_seid);
10256 return NULL;
10257 }
10258
10259 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010260 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010261 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10262 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010263 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010264 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10265 vsi_seid);
10266 return NULL;
10267 }
10268
10269 if (uplink_seid && uplink_seid != pf->mac_seid) {
10270 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10271 if (pf->veb[veb_idx] &&
10272 pf->veb[veb_idx]->seid == uplink_seid) {
10273 uplink_veb = pf->veb[veb_idx];
10274 break;
10275 }
10276 }
10277 if (!uplink_veb) {
10278 dev_info(&pf->pdev->dev,
10279 "uplink seid %d not found\n", uplink_seid);
10280 return NULL;
10281 }
10282 }
10283
10284 /* get veb sw struct */
10285 veb_idx = i40e_veb_mem_alloc(pf);
10286 if (veb_idx < 0)
10287 goto err_alloc;
10288 veb = pf->veb[veb_idx];
10289 veb->flags = flags;
10290 veb->uplink_seid = uplink_seid;
10291 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10292 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10293
10294 /* create the VEB in the switch */
10295 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10296 if (ret)
10297 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010298 if (vsi_idx == pf->lan_vsi)
10299 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010300
10301 return veb;
10302
10303err_veb:
10304 i40e_veb_clear(veb);
10305err_alloc:
10306 return NULL;
10307}
10308
10309/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010310 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010311 * @pf: board private structure
10312 * @ele: element we are building info from
10313 * @num_reported: total number of elements
10314 * @printconfig: should we print the contents
10315 *
10316 * helper function to assist in extracting a few useful SEID values.
10317 **/
10318static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10319 struct i40e_aqc_switch_config_element_resp *ele,
10320 u16 num_reported, bool printconfig)
10321{
10322 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10323 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10324 u8 element_type = ele->element_type;
10325 u16 seid = le16_to_cpu(ele->seid);
10326
10327 if (printconfig)
10328 dev_info(&pf->pdev->dev,
10329 "type=%d seid=%d uplink=%d downlink=%d\n",
10330 element_type, seid, uplink_seid, downlink_seid);
10331
10332 switch (element_type) {
10333 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10334 pf->mac_seid = seid;
10335 break;
10336 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10337 /* Main VEB? */
10338 if (uplink_seid != pf->mac_seid)
10339 break;
10340 if (pf->lan_veb == I40E_NO_VEB) {
10341 int v;
10342
10343 /* find existing or else empty VEB */
10344 for (v = 0; v < I40E_MAX_VEB; v++) {
10345 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10346 pf->lan_veb = v;
10347 break;
10348 }
10349 }
10350 if (pf->lan_veb == I40E_NO_VEB) {
10351 v = i40e_veb_mem_alloc(pf);
10352 if (v < 0)
10353 break;
10354 pf->lan_veb = v;
10355 }
10356 }
10357
10358 pf->veb[pf->lan_veb]->seid = seid;
10359 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10360 pf->veb[pf->lan_veb]->pf = pf;
10361 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10362 break;
10363 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10364 if (num_reported != 1)
10365 break;
10366 /* This is immediately after a reset so we can assume this is
10367 * the PF's VSI
10368 */
10369 pf->mac_seid = uplink_seid;
10370 pf->pf_seid = downlink_seid;
10371 pf->main_vsi_seid = seid;
10372 if (printconfig)
10373 dev_info(&pf->pdev->dev,
10374 "pf_seid=%d main_vsi_seid=%d\n",
10375 pf->pf_seid, pf->main_vsi_seid);
10376 break;
10377 case I40E_SWITCH_ELEMENT_TYPE_PF:
10378 case I40E_SWITCH_ELEMENT_TYPE_VF:
10379 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10380 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10381 case I40E_SWITCH_ELEMENT_TYPE_PE:
10382 case I40E_SWITCH_ELEMENT_TYPE_PA:
10383 /* ignore these for now */
10384 break;
10385 default:
10386 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10387 element_type, seid);
10388 break;
10389 }
10390}
10391
10392/**
10393 * i40e_fetch_switch_configuration - Get switch config from firmware
10394 * @pf: board private structure
10395 * @printconfig: should we print the contents
10396 *
10397 * Get the current switch configuration from the device and
10398 * extract a few useful SEID values.
10399 **/
10400int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10401{
10402 struct i40e_aqc_get_switch_config_resp *sw_config;
10403 u16 next_seid = 0;
10404 int ret = 0;
10405 u8 *aq_buf;
10406 int i;
10407
10408 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10409 if (!aq_buf)
10410 return -ENOMEM;
10411
10412 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10413 do {
10414 u16 num_reported, num_total;
10415
10416 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10417 I40E_AQ_LARGE_BUF,
10418 &next_seid, NULL);
10419 if (ret) {
10420 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010421 "get switch config failed err %s aq_err %s\n",
10422 i40e_stat_str(&pf->hw, ret),
10423 i40e_aq_str(&pf->hw,
10424 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010425 kfree(aq_buf);
10426 return -ENOENT;
10427 }
10428
10429 num_reported = le16_to_cpu(sw_config->header.num_reported);
10430 num_total = le16_to_cpu(sw_config->header.num_total);
10431
10432 if (printconfig)
10433 dev_info(&pf->pdev->dev,
10434 "header: %d reported %d total\n",
10435 num_reported, num_total);
10436
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010437 for (i = 0; i < num_reported; i++) {
10438 struct i40e_aqc_switch_config_element_resp *ele =
10439 &sw_config->element[i];
10440
10441 i40e_setup_pf_switch_element(pf, ele, num_reported,
10442 printconfig);
10443 }
10444 } while (next_seid != 0);
10445
10446 kfree(aq_buf);
10447 return ret;
10448}
10449
10450/**
10451 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10452 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010453 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010454 *
10455 * Returns 0 on success, negative value on failure
10456 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010457static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010458{
10459 int ret;
10460
10461 /* find out what's out there already */
10462 ret = i40e_fetch_switch_configuration(pf, false);
10463 if (ret) {
10464 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010465 "couldn't fetch switch config, err %s aq_err %s\n",
10466 i40e_stat_str(&pf->hw, ret),
10467 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010468 return ret;
10469 }
10470 i40e_pf_reset_stats(pf);
10471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010472 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010473 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010474 struct i40e_vsi *vsi = NULL;
10475 u16 uplink_seid;
10476
10477 /* Set up the PF VSI associated with the PF's main VSI
10478 * that is already in the HW switch
10479 */
10480 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10481 uplink_seid = pf->veb[pf->lan_veb]->seid;
10482 else
10483 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010484 if (pf->lan_vsi == I40E_NO_VSI)
10485 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10486 else if (reinit)
10487 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010488 if (!vsi) {
10489 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10490 i40e_fdir_teardown(pf);
10491 return -EAGAIN;
10492 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010493 } else {
10494 /* force a reset of TC and queue layout configurations */
10495 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010496
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010497 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10498 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10499 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10500 }
10501 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10502
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010503 i40e_fdir_sb_setup(pf);
10504
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010505 /* Setup static PF queue filter control settings */
10506 ret = i40e_setup_pf_filter_control(pf);
10507 if (ret) {
10508 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10509 ret);
10510 /* Failure here should not stop continuing other steps */
10511 }
10512
10513 /* enable RSS in the HW, even for only one queue, as the stack can use
10514 * the hash
10515 */
10516 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010517 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010518
10519 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010520 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010521 i40e_link_event(pf);
10522
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010523 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010524 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10525 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010526
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010527 i40e_ptp_init(pf);
10528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010529 return ret;
10530}
10531
10532/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010533 * i40e_determine_queue_usage - Work out queue distribution
10534 * @pf: board private structure
10535 **/
10536static void i40e_determine_queue_usage(struct i40e_pf *pf)
10537{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010538 int queues_left;
10539
10540 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010541#ifdef I40E_FCOE
10542 pf->num_fcoe_qps = 0;
10543#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010544
10545 /* Find the max queues to be put into basic use. We'll always be
10546 * using TC0, whether or not DCB is running, and TC0 will get the
10547 * big RSS set.
10548 */
10549 queues_left = pf->hw.func_caps.num_tx_qp;
10550
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010551 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010552 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010553 /* one qp for PF, no queues for anything else */
10554 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010555 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010556
10557 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010558 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010559 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010560#ifdef I40E_FCOE
10561 I40E_FLAG_FCOE_ENABLED |
10562#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010563 I40E_FLAG_FD_SB_ENABLED |
10564 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010565 I40E_FLAG_DCB_CAPABLE |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010566 I40E_FLAG_SRIOV_ENABLED |
10567 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010568 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10569 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010570 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010571 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010572 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010573 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010574 queues_left -= pf->num_lan_qps;
10575
10576 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010577 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010578#ifdef I40E_FCOE
10579 I40E_FLAG_FCOE_ENABLED |
10580#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010581 I40E_FLAG_FD_SB_ENABLED |
10582 I40E_FLAG_FD_ATR_ENABLED |
10583 I40E_FLAG_DCB_ENABLED |
10584 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010585 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010586 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010587 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010588 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010589 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010590 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10591 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010592 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10593 num_online_cpus());
10594 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10595 pf->hw.func_caps.num_tx_qp);
10596
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010597 queues_left -= pf->num_lan_qps;
10598 }
10599
Vasu Dev38e00432014-08-01 13:27:03 -070010600#ifdef I40E_FCOE
10601 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10602 if (I40E_DEFAULT_FCOE <= queues_left) {
10603 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10604 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10605 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10606 } else {
10607 pf->num_fcoe_qps = 0;
10608 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10609 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10610 }
10611
10612 queues_left -= pf->num_fcoe_qps;
10613 }
10614
10615#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010616 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10617 if (queues_left > 1) {
10618 queues_left -= 1; /* save 1 queue for FD */
10619 } else {
10620 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10621 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10622 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010623 }
10624
10625 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10626 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010627 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10628 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010629 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10630 }
10631
10632 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10633 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10634 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10635 (queues_left / pf->num_vmdq_qps));
10636 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10637 }
10638
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010639 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010640 dev_dbg(&pf->pdev->dev,
10641 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10642 pf->hw.func_caps.num_tx_qp,
10643 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010644 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10645 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10646 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010647#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010648 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010649#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010650}
10651
10652/**
10653 * i40e_setup_pf_filter_control - Setup PF static filter control
10654 * @pf: PF to be setup
10655 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010656 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010657 * settings. If PE/FCoE are enabled then it will also set the per PF
10658 * based filter sizes required for them. It also enables Flow director,
10659 * ethertype and macvlan type filter settings for the pf.
10660 *
10661 * Returns 0 on success, negative on failure
10662 **/
10663static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10664{
10665 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10666
10667 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10668
10669 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010670 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010671 settings->enable_fdir = true;
10672
10673 /* Ethtype and MACVLAN filters enabled for PF */
10674 settings->enable_ethtype = true;
10675 settings->enable_macvlan = true;
10676
10677 if (i40e_set_filter_control(&pf->hw, settings))
10678 return -ENOENT;
10679
10680 return 0;
10681}
10682
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010683#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010684#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010685static void i40e_print_features(struct i40e_pf *pf)
10686{
10687 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010688 char *buf;
10689 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010690
Joe Perches3b195842015-12-03 04:20:57 -080010691 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10692 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010693 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010694
Joe Perches3b195842015-12-03 04:20:57 -080010695 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010696#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010697 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010698#endif
Joe Perches3b195842015-12-03 04:20:57 -080010699 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d RX: %s",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010700 pf->hw.func_caps.num_vsis,
10701 pf->vsi[pf->lan_vsi]->num_queue_pairs,
10702 pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010703
10704 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010705 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010706 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010707 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010708 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010709 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10710 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010711 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010712 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010713 i += snprintf(&buf[i], REMAIN(i), " DCB");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010714#if IS_ENABLED(CONFIG_VXLAN)
Joe Perches3b195842015-12-03 04:20:57 -080010715 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Jesse Brandeburgce6fcb32015-08-28 17:55:59 -040010716#endif
Singhai, Anjali6a899022015-12-14 12:21:18 -080010717#if IS_ENABLED(CONFIG_GENEVE)
10718 i += snprintf(&buf[i], REMAIN(i), " Geneve");
10719#endif
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010720 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010721 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010722#ifdef I40E_FCOE
10723 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010724 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010725#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010726 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010727 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010728 else
Joe Perches3b195842015-12-03 04:20:57 -080010729 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010730
Joe Perches3b195842015-12-03 04:20:57 -080010731 dev_info(&pf->pdev->dev, "%s\n", buf);
10732 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010733 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010734}
10735
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010736/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010737 * i40e_get_platform_mac_addr - get platform-specific MAC address
10738 *
10739 * @pdev: PCI device information struct
10740 * @pf: board private structure
10741 *
10742 * Look up the MAC address in Open Firmware on systems that support it,
10743 * and use IDPROM on SPARC if no OF address is found. On return, the
10744 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10745 * has been selected.
10746 **/
10747static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10748{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010749 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010750 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010751 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010752}
10753
10754/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010755 * i40e_probe - Device initialization routine
10756 * @pdev: PCI device information struct
10757 * @ent: entry in i40e_pci_tbl
10758 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010759 * i40e_probe initializes a PF identified by a pci_dev structure.
10760 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010761 * and a hardware reset occur.
10762 *
10763 * Returns 0 on success, negative on failure
10764 **/
10765static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10766{
Catherine Sullivane8278452015-02-06 08:52:08 +000010767 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010768 struct i40e_pf *pf;
10769 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010770 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010771 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010772 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010773 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010774 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010775 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010776 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010777
10778 err = pci_enable_device_mem(pdev);
10779 if (err)
10780 return err;
10781
10782 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010783 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010784 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010785 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10786 if (err) {
10787 dev_err(&pdev->dev,
10788 "DMA configuration failed: 0x%x\n", err);
10789 goto err_dma;
10790 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010791 }
10792
10793 /* set up pci connections */
10794 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
10795 IORESOURCE_MEM), i40e_driver_name);
10796 if (err) {
10797 dev_info(&pdev->dev,
10798 "pci_request_selected_regions failed %d\n", err);
10799 goto err_pci_reg;
10800 }
10801
10802 pci_enable_pcie_error_reporting(pdev);
10803 pci_set_master(pdev);
10804
10805 /* Now that we have a PCI connection, we need to do the
10806 * low level device setup. This is primarily setting up
10807 * the Admin Queue structures and then querying for the
10808 * device's current profile information.
10809 */
10810 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10811 if (!pf) {
10812 err = -ENOMEM;
10813 goto err_pf_alloc;
10814 }
10815 pf->next_vsi = 0;
10816 pf->pdev = pdev;
10817 set_bit(__I40E_DOWN, &pf->state);
10818
10819 hw = &pf->hw;
10820 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010821
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010822 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10823 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010824
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010825 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010826 if (!hw->hw_addr) {
10827 err = -EIO;
10828 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10829 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010830 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010831 goto err_ioremap;
10832 }
10833 hw->vendor_id = pdev->vendor;
10834 hw->device_id = pdev->device;
10835 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10836 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10837 hw->subsystem_device_id = pdev->subsystem_device;
10838 hw->bus.device = PCI_SLOT(pdev->devfn);
10839 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010840 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010841
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010842 /* set up the locks for the AQ, do this only once in probe
10843 * and destroy them only once in remove
10844 */
10845 mutex_init(&hw->aq.asq_mutex);
10846 mutex_init(&hw->aq.arq_mutex);
10847
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010848 if (debug != -1) {
10849 pf->msg_enable = pf->hw.debug_mask;
10850 pf->msg_enable = debug;
10851 }
10852
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010853 /* do a special CORER for clearing PXE mode once at init */
10854 if (hw->revision_id == 0 &&
10855 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10856 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10857 i40e_flush(hw);
10858 msleep(200);
10859 pf->corer_count++;
10860
10861 i40e_clear_pxe_mode(hw);
10862 }
10863
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010864 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010865 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010866 err = i40e_pf_reset(hw);
10867 if (err) {
10868 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10869 goto err_pf_reset;
10870 }
10871 pf->pfr_count++;
10872
10873 hw->aq.num_arq_entries = I40E_AQ_LEN;
10874 hw->aq.num_asq_entries = I40E_AQ_LEN;
10875 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10876 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10877 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010878
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010879 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010880 "%s-%s:misc",
10881 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010882
10883 err = i40e_init_shared_code(hw);
10884 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010885 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10886 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010887 goto err_pf_reset;
10888 }
10889
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010890 /* set up a default setting for link flow control */
10891 pf->hw.fc.requested_mode = I40E_FC_NONE;
10892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010893 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010894 if (err) {
10895 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10896 dev_info(&pdev->dev,
10897 "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");
10898 else
10899 dev_info(&pdev->dev,
10900 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10901
10902 goto err_pf_reset;
10903 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010904
Shannon Nelson6dec1012015-09-28 14:12:30 -040010905 /* provide nvm, fw, api versions */
10906 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10907 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10908 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10909 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010910
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010911 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10912 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010913 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010914 "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");
10915 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10916 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010917 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010918 "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 +000010919
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010920 i40e_verify_eeprom(pf);
10921
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010922 /* Rev 0 hardware was never productized */
10923 if (hw->revision_id < 1)
10924 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");
10925
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010926 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010927 err = i40e_get_capabilities(pf);
10928 if (err)
10929 goto err_adminq_setup;
10930
10931 err = i40e_sw_init(pf);
10932 if (err) {
10933 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10934 goto err_sw_init;
10935 }
10936
10937 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10938 hw->func_caps.num_rx_qp,
10939 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10940 if (err) {
10941 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10942 goto err_init_lan_hmc;
10943 }
10944
10945 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10946 if (err) {
10947 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10948 err = -ENOENT;
10949 goto err_configure_lan_hmc;
10950 }
10951
Neerav Parikhb686ece2014-12-14 01:55:11 +000010952 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10953 * Ignore error return codes because if it was already disabled via
10954 * hardware settings this will fail
10955 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010956 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010957 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10958 i40e_aq_stop_lldp(hw, true, NULL);
10959 }
10960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010961 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010962 /* allow a platform config to override the HW addr */
10963 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010964 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010965 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10966 err = -EIO;
10967 goto err_mac_addr;
10968 }
10969 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010970 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010971 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10972 if (is_valid_ether_addr(hw->mac.port_addr))
10973 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010974#ifdef I40E_FCOE
10975 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10976 if (err)
10977 dev_info(&pdev->dev,
10978 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10979 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10980 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10981 hw->mac.san_addr);
10982 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10983 }
10984 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10985#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010986
10987 pci_set_drvdata(pdev, pf);
10988 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010989#ifdef CONFIG_I40E_DCB
10990 err = i40e_init_pf_dcb(pf);
10991 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010992 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010993 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
Neerav Parikh014269f2014-04-01 07:11:48 +000010994 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010995 }
10996#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010997
10998 /* set up periodic task facility */
10999 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11000 pf->service_timer_period = HZ;
11001
11002 INIT_WORK(&pf->service_task, i40e_service_task);
11003 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11004 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011005
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011006 /* NVM bit on means WoL disabled for the port */
11007 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011008 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011009 pf->wol_en = false;
11010 else
11011 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011012 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11013
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011014 /* set up the main switch operations */
11015 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011016 err = i40e_init_interrupt_scheme(pf);
11017 if (err)
11018 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011019
Mitch Williams505682c2014-05-20 08:01:37 +000011020 /* The number of VSIs reported by the FW is the minimum guaranteed
11021 * to us; HW supports far more and we share the remaining pool with
11022 * the other PFs. We allocate space for more than the guarantee with
11023 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011024 */
Mitch Williams505682c2014-05-20 08:01:37 +000011025 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11026 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11027 else
11028 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11029
11030 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011031 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11032 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011033 if (!pf->vsi) {
11034 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011035 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011036 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011037
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011038#ifdef CONFIG_PCI_IOV
11039 /* prep for VF support */
11040 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11041 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11042 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11043 if (pci_num_vf(pdev))
11044 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11045 }
11046#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011047 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011048 if (err) {
11049 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11050 goto err_vsis;
11051 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011052
11053 /* Make sure flow control is set according to current settings */
11054 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11055 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11056 dev_dbg(&pf->pdev->dev,
11057 "Set fc with err %s aq_err %s on get_phy_cap\n",
11058 i40e_stat_str(hw, err),
11059 i40e_aq_str(hw, hw->aq.asq_last_status));
11060 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11061 dev_dbg(&pf->pdev->dev,
11062 "Set fc with err %s aq_err %s on set_phy_config\n",
11063 i40e_stat_str(hw, err),
11064 i40e_aq_str(hw, hw->aq.asq_last_status));
11065 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11066 dev_dbg(&pf->pdev->dev,
11067 "Set fc with err %s aq_err %s on get_link_info\n",
11068 i40e_stat_str(hw, err),
11069 i40e_aq_str(hw, hw->aq.asq_last_status));
11070
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011071 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011072 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011073 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11074 i40e_vsi_open(pf->vsi[i]);
11075 break;
11076 }
11077 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011078
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011079 /* The driver only wants link up/down and module qualification
11080 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011081 */
11082 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011083 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011084 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011085 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011086 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011087 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11088 i40e_stat_str(&pf->hw, err),
11089 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011090
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011091 /* Reconfigure hardware for allowing smaller MSS in the case
11092 * of TSO, so that we avoid the MDD being fired and causing
11093 * a reset in the case of small MSS+TSO.
11094 */
11095 val = rd32(hw, I40E_REG_MSS);
11096 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11097 val &= ~I40E_REG_MSS_MIN_MASK;
11098 val |= I40E_64BYTE_MSS;
11099 wr32(hw, I40E_REG_MSS, val);
11100 }
11101
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011102 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011103 msleep(75);
11104 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11105 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011106 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11107 i40e_stat_str(&pf->hw, err),
11108 i40e_aq_str(&pf->hw,
11109 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011110 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011111 /* The main driver is (mostly) up and happy. We need to set this state
11112 * before setting up the misc vector or we get a race and the vector
11113 * ends up disabled forever.
11114 */
11115 clear_bit(__I40E_DOWN, &pf->state);
11116
11117 /* In case of MSIX we are going to setup the misc vector right here
11118 * to handle admin queue events etc. In case of legacy and MSI
11119 * the misc functionality and queue processing is combined in
11120 * the same vector and that gets setup at open.
11121 */
11122 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11123 err = i40e_setup_misc_vector(pf);
11124 if (err) {
11125 dev_info(&pdev->dev,
11126 "setup of misc vector failed: %d\n", err);
11127 goto err_vsis;
11128 }
11129 }
11130
Greg Rosedf805f62014-04-04 04:43:16 +000011131#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011132 /* prep for VF support */
11133 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011134 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11135 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011136 /* disable link interrupts for VFs */
11137 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11138 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11139 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11140 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011141
11142 if (pci_num_vf(pdev)) {
11143 dev_info(&pdev->dev,
11144 "Active VFs found, allocating resources.\n");
11145 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11146 if (err)
11147 dev_info(&pdev->dev,
11148 "Error %d allocating resources for existing VFs\n",
11149 err);
11150 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011151 }
Greg Rosedf805f62014-04-04 04:43:16 +000011152#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011153
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011154 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11155 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11156 pf->num_iwarp_msix,
11157 I40E_IWARP_IRQ_PILE_ID);
11158 if (pf->iwarp_base_vector < 0) {
11159 dev_info(&pdev->dev,
11160 "failed to get tracking for %d vectors for IWARP err=%d\n",
11161 pf->num_iwarp_msix, pf->iwarp_base_vector);
11162 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11163 }
11164 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011165
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011166 i40e_dbg_pf_init(pf);
11167
11168 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011169 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011170
11171 /* since everything's happy, start the service_task timer */
11172 mod_timer(&pf->service_timer,
11173 round_jiffies(jiffies + pf->service_timer_period));
11174
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011175 /* add this PF to client device list and launch a client service task */
11176 err = i40e_lan_add_device(pf);
11177 if (err)
11178 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11179 err);
11180
Vasu Dev38e00432014-08-01 13:27:03 -070011181#ifdef I40E_FCOE
11182 /* create FCoE interface */
11183 i40e_fcoe_vsi_setup(pf);
11184
11185#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011186#define PCI_SPEED_SIZE 8
11187#define PCI_WIDTH_SIZE 8
11188 /* Devices on the IOSF bus do not have this information
11189 * and will report PCI Gen 1 x 1 by default so don't bother
11190 * checking them.
11191 */
11192 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11193 char speed[PCI_SPEED_SIZE] = "Unknown";
11194 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011195
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011196 /* Get the negotiated link width and speed from PCI config
11197 * space
11198 */
11199 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11200 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011201
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011202 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011203
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011204 switch (hw->bus.speed) {
11205 case i40e_bus_speed_8000:
11206 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11207 case i40e_bus_speed_5000:
11208 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11209 case i40e_bus_speed_2500:
11210 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11211 default:
11212 break;
11213 }
11214 switch (hw->bus.width) {
11215 case i40e_bus_width_pcie_x8:
11216 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11217 case i40e_bus_width_pcie_x4:
11218 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11219 case i40e_bus_width_pcie_x2:
11220 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11221 case i40e_bus_width_pcie_x1:
11222 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11223 default:
11224 break;
11225 }
11226
11227 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11228 speed, width);
11229
11230 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11231 hw->bus.speed < i40e_bus_speed_8000) {
11232 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11233 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11234 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011235 }
11236
Catherine Sullivane8278452015-02-06 08:52:08 +000011237 /* get the requested speeds from the fw */
11238 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11239 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011240 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11241 i40e_stat_str(&pf->hw, err),
11242 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011243 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11244
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011245 /* get the supported phy types from the fw */
11246 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11247 if (err)
11248 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11249 i40e_stat_str(&pf->hw, err),
11250 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11251 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11252
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011253 /* Add a filter to drop all Flow control frames from any VSI from being
11254 * transmitted. By doing so we stop a malicious VF from sending out
11255 * PAUSE or PFC frames and potentially controlling traffic for other
11256 * PF/VF VSIs.
11257 * The FW can still send Flow control frames if enabled.
11258 */
11259 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11260 pf->main_vsi_seid);
11261
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011262 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11263 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11264 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11265
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011266 /* print a string summarizing features */
11267 i40e_print_features(pf);
11268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011269 return 0;
11270
11271 /* Unwind what we've done if something failed in the setup */
11272err_vsis:
11273 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011274 i40e_clear_interrupt_scheme(pf);
11275 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011276err_switch_setup:
11277 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011278 del_timer_sync(&pf->service_timer);
11279err_mac_addr:
11280err_configure_lan_hmc:
11281 (void)i40e_shutdown_lan_hmc(hw);
11282err_init_lan_hmc:
11283 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011284err_sw_init:
11285err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011286err_pf_reset:
11287 iounmap(hw->hw_addr);
11288err_ioremap:
11289 kfree(pf);
11290err_pf_alloc:
11291 pci_disable_pcie_error_reporting(pdev);
11292 pci_release_selected_regions(pdev,
11293 pci_select_bars(pdev, IORESOURCE_MEM));
11294err_pci_reg:
11295err_dma:
11296 pci_disable_device(pdev);
11297 return err;
11298}
11299
11300/**
11301 * i40e_remove - Device removal routine
11302 * @pdev: PCI device information struct
11303 *
11304 * i40e_remove is called by the PCI subsystem to alert the driver
11305 * that is should release a PCI device. This could be caused by a
11306 * Hot-Plug event, or because the driver is going to be removed from
11307 * memory.
11308 **/
11309static void i40e_remove(struct pci_dev *pdev)
11310{
11311 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011312 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011313 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011314 int i;
11315
11316 i40e_dbg_pf_exit(pf);
11317
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011318 i40e_ptp_stop(pf);
11319
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011320 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011321 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11322 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011324 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011325 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011326 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011327 if (pf->service_timer.data)
11328 del_timer_sync(&pf->service_timer);
11329 if (pf->service_task.func)
11330 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011331
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011332 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11333 i40e_free_vfs(pf);
11334 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11335 }
11336
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011337 i40e_fdir_teardown(pf);
11338
11339 /* If there is a switch structure or any orphans, remove them.
11340 * This will leave only the PF's VSI remaining.
11341 */
11342 for (i = 0; i < I40E_MAX_VEB; i++) {
11343 if (!pf->veb[i])
11344 continue;
11345
11346 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11347 pf->veb[i]->uplink_seid == 0)
11348 i40e_switch_branch_release(pf->veb[i]);
11349 }
11350
11351 /* Now we can shutdown the PF's VSI, just before we kill
11352 * adminq and hmc.
11353 */
11354 if (pf->vsi[pf->lan_vsi])
11355 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11356
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011357 /* remove attached clients */
11358 ret_code = i40e_lan_del_device(pf);
11359 if (ret_code) {
11360 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11361 ret_code);
11362 }
11363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011364 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011365 if (hw->hmc.hmc_obj) {
11366 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011367 if (ret_code)
11368 dev_warn(&pdev->dev,
11369 "Failed to destroy the HMC resources: %d\n",
11370 ret_code);
11371 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011372
11373 /* shutdown the adminq */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011374 ret_code = i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011375 if (ret_code)
11376 dev_warn(&pdev->dev,
11377 "Failed to destroy the Admin Queue resources: %d\n",
11378 ret_code);
11379
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011380 /* destroy the locks only once, here */
11381 mutex_destroy(&hw->aq.arq_mutex);
11382 mutex_destroy(&hw->aq.asq_mutex);
11383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011384 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11385 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011386 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011387 if (pf->vsi[i]) {
11388 i40e_vsi_clear_rings(pf->vsi[i]);
11389 i40e_vsi_clear(pf->vsi[i]);
11390 pf->vsi[i] = NULL;
11391 }
11392 }
11393
11394 for (i = 0; i < I40E_MAX_VEB; i++) {
11395 kfree(pf->veb[i]);
11396 pf->veb[i] = NULL;
11397 }
11398
11399 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011400 kfree(pf->vsi);
11401
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011402 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011403 kfree(pf);
11404 pci_release_selected_regions(pdev,
11405 pci_select_bars(pdev, IORESOURCE_MEM));
11406
11407 pci_disable_pcie_error_reporting(pdev);
11408 pci_disable_device(pdev);
11409}
11410
11411/**
11412 * i40e_pci_error_detected - warning that something funky happened in PCI land
11413 * @pdev: PCI device information struct
11414 *
11415 * Called to warn that something happened and the error handling steps
11416 * are in progress. Allows the driver to quiesce things, be ready for
11417 * remediation.
11418 **/
11419static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11420 enum pci_channel_state error)
11421{
11422 struct i40e_pf *pf = pci_get_drvdata(pdev);
11423
11424 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11425
11426 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011427 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11428 rtnl_lock();
11429 i40e_prep_for_reset(pf);
11430 rtnl_unlock();
11431 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011432
11433 /* Request a slot reset */
11434 return PCI_ERS_RESULT_NEED_RESET;
11435}
11436
11437/**
11438 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11439 * @pdev: PCI device information struct
11440 *
11441 * Called to find if the driver can work with the device now that
11442 * the pci slot has been reset. If a basic connection seems good
11443 * (registers are readable and have sane content) then return a
11444 * happy little PCI_ERS_RESULT_xxx.
11445 **/
11446static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11447{
11448 struct i40e_pf *pf = pci_get_drvdata(pdev);
11449 pci_ers_result_t result;
11450 int err;
11451 u32 reg;
11452
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011453 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011454 if (pci_enable_device_mem(pdev)) {
11455 dev_info(&pdev->dev,
11456 "Cannot re-enable PCI device after reset.\n");
11457 result = PCI_ERS_RESULT_DISCONNECT;
11458 } else {
11459 pci_set_master(pdev);
11460 pci_restore_state(pdev);
11461 pci_save_state(pdev);
11462 pci_wake_from_d3(pdev, false);
11463
11464 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11465 if (reg == 0)
11466 result = PCI_ERS_RESULT_RECOVERED;
11467 else
11468 result = PCI_ERS_RESULT_DISCONNECT;
11469 }
11470
11471 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11472 if (err) {
11473 dev_info(&pdev->dev,
11474 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11475 err);
11476 /* non-fatal, continue */
11477 }
11478
11479 return result;
11480}
11481
11482/**
11483 * i40e_pci_error_resume - restart operations after PCI error recovery
11484 * @pdev: PCI device information struct
11485 *
11486 * Called to allow the driver to bring things back up after PCI error
11487 * and/or reset recovery has finished.
11488 **/
11489static void i40e_pci_error_resume(struct pci_dev *pdev)
11490{
11491 struct i40e_pf *pf = pci_get_drvdata(pdev);
11492
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011493 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011494 if (test_bit(__I40E_SUSPENDED, &pf->state))
11495 return;
11496
11497 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011498 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011499 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011500}
11501
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011502/**
11503 * i40e_shutdown - PCI callback for shutting down
11504 * @pdev: PCI device information struct
11505 **/
11506static void i40e_shutdown(struct pci_dev *pdev)
11507{
11508 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011509 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011510
11511 set_bit(__I40E_SUSPENDED, &pf->state);
11512 set_bit(__I40E_DOWN, &pf->state);
11513 rtnl_lock();
11514 i40e_prep_for_reset(pf);
11515 rtnl_unlock();
11516
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011517 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11518 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11519
Catherine Sullivan02b42492015-07-10 19:35:59 -040011520 del_timer_sync(&pf->service_timer);
11521 cancel_work_sync(&pf->service_task);
11522 i40e_fdir_teardown(pf);
11523
11524 rtnl_lock();
11525 i40e_prep_for_reset(pf);
11526 rtnl_unlock();
11527
11528 wr32(hw, I40E_PFPM_APM,
11529 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11530 wr32(hw, I40E_PFPM_WUFC,
11531 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11532
Shannon Nelsone1477582015-02-21 06:44:33 +000011533 i40e_clear_interrupt_scheme(pf);
11534
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011535 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011536 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011537 pci_set_power_state(pdev, PCI_D3hot);
11538 }
11539}
11540
11541#ifdef CONFIG_PM
11542/**
11543 * i40e_suspend - PCI callback for moving to D3
11544 * @pdev: PCI device information struct
11545 **/
11546static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11547{
11548 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011549 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011550
11551 set_bit(__I40E_SUSPENDED, &pf->state);
11552 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011553
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011554 rtnl_lock();
11555 i40e_prep_for_reset(pf);
11556 rtnl_unlock();
11557
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011558 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11559 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11560
11561 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011562 pci_set_power_state(pdev, PCI_D3hot);
11563
11564 return 0;
11565}
11566
11567/**
11568 * i40e_resume - PCI callback for waking up from D3
11569 * @pdev: PCI device information struct
11570 **/
11571static int i40e_resume(struct pci_dev *pdev)
11572{
11573 struct i40e_pf *pf = pci_get_drvdata(pdev);
11574 u32 err;
11575
11576 pci_set_power_state(pdev, PCI_D0);
11577 pci_restore_state(pdev);
11578 /* pci_restore_state() clears dev->state_saves, so
11579 * call pci_save_state() again to restore it.
11580 */
11581 pci_save_state(pdev);
11582
11583 err = pci_enable_device_mem(pdev);
11584 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011585 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011586 return err;
11587 }
11588 pci_set_master(pdev);
11589
11590 /* no wakeup events while running */
11591 pci_wake_from_d3(pdev, false);
11592
11593 /* handling the reset will rebuild the device state */
11594 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11595 clear_bit(__I40E_DOWN, &pf->state);
11596 rtnl_lock();
11597 i40e_reset_and_rebuild(pf, false);
11598 rtnl_unlock();
11599 }
11600
11601 return 0;
11602}
11603
11604#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011605static const struct pci_error_handlers i40e_err_handler = {
11606 .error_detected = i40e_pci_error_detected,
11607 .slot_reset = i40e_pci_error_slot_reset,
11608 .resume = i40e_pci_error_resume,
11609};
11610
11611static struct pci_driver i40e_driver = {
11612 .name = i40e_driver_name,
11613 .id_table = i40e_pci_tbl,
11614 .probe = i40e_probe,
11615 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011616#ifdef CONFIG_PM
11617 .suspend = i40e_suspend,
11618 .resume = i40e_resume,
11619#endif
11620 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011621 .err_handler = &i40e_err_handler,
11622 .sriov_configure = i40e_pci_sriov_configure,
11623};
11624
11625/**
11626 * i40e_init_module - Driver registration routine
11627 *
11628 * i40e_init_module is the first routine called when the driver is
11629 * loaded. All it does is register with the PCI subsystem.
11630 **/
11631static int __init i40e_init_module(void)
11632{
11633 pr_info("%s: %s - version %s\n", i40e_driver_name,
11634 i40e_driver_string, i40e_driver_version_str);
11635 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011636
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011637 /* we will see if single thread per module is enough for now,
11638 * it can't be any worse than using the system workqueue which
11639 * was already single threaded
11640 */
11641 i40e_wq = create_singlethread_workqueue(i40e_driver_name);
11642 if (!i40e_wq) {
11643 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11644 return -ENOMEM;
11645 }
11646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011647 i40e_dbg_init();
11648 return pci_register_driver(&i40e_driver);
11649}
11650module_init(i40e_init_module);
11651
11652/**
11653 * i40e_exit_module - Driver exit cleanup routine
11654 *
11655 * i40e_exit_module is called just before the driver is removed
11656 * from memory.
11657 **/
11658static void __exit i40e_exit_module(void)
11659{
11660 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011661 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011662 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011663}
11664module_exit(i40e_exit_module);