blob: 6abc1301a8e4473fa233f5c014c925ce1e407515 [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"
Alexander Duyck06a5f7f2016-06-16 12:22:06 -070034#include <net/udp_tunnel.h>
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000035
36const char i40e_driver_name[] = "i40e";
37static const char i40e_driver_string[] =
38 "Intel(R) Ethernet Connection XL710 Network Driver";
39
40#define DRV_KERN "-k"
41
Catherine Sullivane8e724d2014-07-10 07:58:26 +000042#define DRV_VERSION_MAJOR 1
Bimmy Pujari07061952016-05-16 10:26:45 -070043#define DRV_VERSION_MINOR 6
Bimmy Pujarifa90efa2016-09-06 18:05:12 -070044#define DRV_VERSION_BUILD 16
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000045#define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
46 __stringify(DRV_VERSION_MINOR) "." \
47 __stringify(DRV_VERSION_BUILD) DRV_KERN
48const char i40e_driver_version_str[] = DRV_VERSION;
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -080049static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000050
51/* a bit of forward declarations */
52static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
53static void i40e_handle_reset_warning(struct i40e_pf *pf);
54static int i40e_add_vsi(struct i40e_vsi *vsi);
55static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000056static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000057static int i40e_setup_misc_vector(struct i40e_pf *pf);
58static void i40e_determine_queue_usage(struct i40e_pf *pf);
59static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080060static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080061static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000062
63/* i40e_pci_tbl - PCI Device ID Table
64 *
65 * Last entry must be all 0s
66 *
67 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
68 * Class, Class Mask, private data (not used) }
69 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020070static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080071 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080072 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080073 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
74 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
76 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040079 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Jesse Brandeburgae24b402015-03-27 00:12:09 -070080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
82 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
85 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040087 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
88 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000089 /* required last entry */
90 {0, }
91};
92MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
93
94#define I40E_MAX_VF_COUNT 128
95static int debug = -1;
96module_param(debug, int, 0);
97MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
98
99MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
100MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
101MODULE_LICENSE("GPL");
102MODULE_VERSION(DRV_VERSION);
103
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800104static struct workqueue_struct *i40e_wq;
105
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000106/**
107 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
108 * @hw: pointer to the HW structure
109 * @mem: ptr to mem struct to fill out
110 * @size: size of memory requested
111 * @alignment: what to align the allocation to
112 **/
113int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
114 u64 size, u32 alignment)
115{
116 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
117
118 mem->size = ALIGN(size, alignment);
119 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
120 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000121 if (!mem->va)
122 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000123
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000124 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000125}
126
127/**
128 * i40e_free_dma_mem_d - OS specific memory free for shared code
129 * @hw: pointer to the HW structure
130 * @mem: ptr to mem struct to free
131 **/
132int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
133{
134 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
135
136 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
137 mem->va = NULL;
138 mem->pa = 0;
139 mem->size = 0;
140
141 return 0;
142}
143
144/**
145 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
146 * @hw: pointer to the HW structure
147 * @mem: ptr to mem struct to fill out
148 * @size: size of memory requested
149 **/
150int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
151 u32 size)
152{
153 mem->size = size;
154 mem->va = kzalloc(size, GFP_KERNEL);
155
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000156 if (!mem->va)
157 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000158
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000159 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000160}
161
162/**
163 * i40e_free_virt_mem_d - OS specific memory free for shared code
164 * @hw: pointer to the HW structure
165 * @mem: ptr to mem struct to free
166 **/
167int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
168{
169 /* it's ok to kfree a NULL pointer */
170 kfree(mem->va);
171 mem->va = NULL;
172 mem->size = 0;
173
174 return 0;
175}
176
177/**
178 * i40e_get_lump - find a lump of free generic resource
179 * @pf: board private structure
180 * @pile: the pile of resource to search
181 * @needed: the number of items needed
182 * @id: an owner id to stick on the items assigned
183 *
184 * Returns the base item index of the lump, or negative for error
185 *
186 * The search_hint trick and lack of advanced fit-finding only work
187 * because we're highly likely to have all the same size lump requests.
188 * Linear search time and any fragmentation should be minimal.
189 **/
190static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
191 u16 needed, u16 id)
192{
193 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000194 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000195
196 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
197 dev_info(&pf->pdev->dev,
198 "param err: pile=%p needed=%d id=0x%04x\n",
199 pile, needed, id);
200 return -EINVAL;
201 }
202
203 /* start the linear search with an imperfect hint */
204 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000205 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000206 /* skip already allocated entries */
207 if (pile->list[i] & I40E_PILE_VALID_BIT) {
208 i++;
209 continue;
210 }
211
212 /* do we have enough in this lump? */
213 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
214 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
215 break;
216 }
217
218 if (j == needed) {
219 /* there was enough, so assign it to the requestor */
220 for (j = 0; j < needed; j++)
221 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
222 ret = i;
223 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000224 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000225 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400226
227 /* not enough, so skip over it and continue looking */
228 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000229 }
230
231 return ret;
232}
233
234/**
235 * i40e_put_lump - return a lump of generic resource
236 * @pile: the pile of resource to search
237 * @index: the base item index
238 * @id: the owner id of the items assigned
239 *
240 * Returns the count of items in the lump
241 **/
242static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
243{
244 int valid_id = (id | I40E_PILE_VALID_BIT);
245 int count = 0;
246 int i;
247
248 if (!pile || index >= pile->num_entries)
249 return -EINVAL;
250
251 for (i = index;
252 i < pile->num_entries && pile->list[i] == valid_id;
253 i++) {
254 pile->list[i] = 0;
255 count++;
256 }
257
258 if (count && index < pile->search_hint)
259 pile->search_hint = index;
260
261 return count;
262}
263
264/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700265 * i40e_find_vsi_from_id - searches for the vsi with the given id
266 * @pf - the pf structure to search for the vsi
267 * @id - id of the vsi it is searching for
268 **/
269struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
270{
271 int i;
272
273 for (i = 0; i < pf->num_alloc_vsi; i++)
274 if (pf->vsi[i] && (pf->vsi[i]->id == id))
275 return pf->vsi[i];
276
277 return NULL;
278}
279
280/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000281 * i40e_service_event_schedule - Schedule the service task to wake up
282 * @pf: board private structure
283 *
284 * If not already scheduled, this puts the task into the work queue
285 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600286void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000287{
288 if (!test_bit(__I40E_DOWN, &pf->state) &&
289 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
290 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800291 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000292}
293
294/**
295 * i40e_tx_timeout - Respond to a Tx Hang
296 * @netdev: network interface device structure
297 *
298 * If any port has noticed a Tx timeout, it is likely that the whole
299 * device is munged, not just the one netdev port, so go for the full
300 * reset.
301 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700302#ifdef I40E_FCOE
303void i40e_tx_timeout(struct net_device *netdev)
304#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000305static void i40e_tx_timeout(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -0700306#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000307{
308 struct i40e_netdev_priv *np = netdev_priv(netdev);
309 struct i40e_vsi *vsi = np->vsi;
310 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400311 struct i40e_ring *tx_ring = NULL;
312 unsigned int i, hung_queue = 0;
313 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000314
315 pf->tx_timeout_count++;
316
Kiran Patilb03a8c12015-09-24 18:13:15 -0400317 /* find the stopped queue the same way the stack does */
318 for (i = 0; i < netdev->num_tx_queues; i++) {
319 struct netdev_queue *q;
320 unsigned long trans_start;
321
322 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200323 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400324 if (netif_xmit_stopped(q) &&
325 time_after(jiffies,
326 (trans_start + netdev->watchdog_timeo))) {
327 hung_queue = i;
328 break;
329 }
330 }
331
332 if (i == netdev->num_tx_queues) {
333 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
334 } else {
335 /* now that we have an index, find the tx_ring struct */
336 for (i = 0; i < vsi->num_queue_pairs; i++) {
337 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
338 if (hung_queue ==
339 vsi->tx_rings[i]->queue_index) {
340 tx_ring = vsi->tx_rings[i];
341 break;
342 }
343 }
344 }
345 }
346
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000347 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400348 pf->tx_timeout_recovery_level = 1; /* reset after some time */
349 else if (time_before(jiffies,
350 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 return; /* don't do any new action before the next timeout */
352
353 if (tx_ring) {
354 head = i40e_get_head(tx_ring);
355 /* Read interrupt register */
356 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
357 val = rd32(&pf->hw,
358 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
359 tx_ring->vsi->base_vector - 1));
360 else
361 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
362
363 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",
364 vsi->seid, hung_queue, tx_ring->next_to_clean,
365 head, tx_ring->next_to_use,
366 readl(tx_ring->tail), val);
367 }
368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000369 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400370 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
371 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000372
373 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000374 case 1:
375 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
376 break;
377 case 2:
378 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
379 break;
380 case 3:
381 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
382 break;
383 default:
384 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000385 break;
386 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400387
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000388 i40e_service_event_schedule(pf);
389 pf->tx_timeout_recovery_level++;
390}
391
392/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000393 * i40e_get_vsi_stats_struct - Get System Network Statistics
394 * @vsi: the VSI we care about
395 *
396 * Returns the address of the device statistics structure.
397 * The statistics are actually updated from the service task.
398 **/
399struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
400{
401 return &vsi->net_stats;
402}
403
404/**
405 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
406 * @netdev: network interface device structure
407 *
408 * Returns the address of the device statistics structure.
409 * The statistics are actually updated from the service task.
410 **/
Vasu Dev38e00432014-08-01 13:27:03 -0700411#ifdef I40E_FCOE
412struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
413 struct net_device *netdev,
414 struct rtnl_link_stats64 *stats)
415#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000416static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
417 struct net_device *netdev,
Alexander Duyck980e9b12013-09-28 06:01:03 +0000418 struct rtnl_link_stats64 *stats)
Vasu Dev38e00432014-08-01 13:27:03 -0700419#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000420{
421 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000422 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000423 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000424 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
425 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000426
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000427 if (test_bit(__I40E_DOWN, &vsi->state))
428 return stats;
429
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800430 if (!vsi->tx_rings)
431 return stats;
432
Alexander Duyck980e9b12013-09-28 06:01:03 +0000433 rcu_read_lock();
434 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000435 u64 bytes, packets;
436 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000437
Alexander Duyck980e9b12013-09-28 06:01:03 +0000438 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
439 if (!tx_ring)
440 continue;
441
442 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700443 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000444 packets = tx_ring->stats.packets;
445 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700446 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000447
448 stats->tx_packets += packets;
449 stats->tx_bytes += bytes;
450 rx_ring = &tx_ring[1];
451
452 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700453 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000454 packets = rx_ring->stats.packets;
455 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700456 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000457
458 stats->rx_packets += packets;
459 stats->rx_bytes += bytes;
460 }
461 rcu_read_unlock();
462
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000463 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000464 stats->multicast = vsi_stats->multicast;
465 stats->tx_errors = vsi_stats->tx_errors;
466 stats->tx_dropped = vsi_stats->tx_dropped;
467 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400468 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000469 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
470 stats->rx_length_errors = vsi_stats->rx_length_errors;
471
472 return stats;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000473}
474
475/**
476 * i40e_vsi_reset_stats - Resets all stats of the given vsi
477 * @vsi: the VSI to have its stats reset
478 **/
479void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
480{
481 struct rtnl_link_stats64 *ns;
482 int i;
483
484 if (!vsi)
485 return;
486
487 ns = i40e_get_vsi_stats_struct(vsi);
488 memset(ns, 0, sizeof(*ns));
489 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
490 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
491 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000492 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000493 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400494 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000495 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400496 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000497 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400498 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000499 sizeof(vsi->tx_rings[i]->stats));
500 memset(&vsi->tx_rings[i]->tx_stats, 0,
501 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000502 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000503 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000504 vsi->stat_offsets_loaded = false;
505}
506
507/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000508 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000509 * @pf: the PF to be reset
510 **/
511void i40e_pf_reset_stats(struct i40e_pf *pf)
512{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000513 int i;
514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000515 memset(&pf->stats, 0, sizeof(pf->stats));
516 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
517 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000518
519 for (i = 0; i < I40E_MAX_VEB; i++) {
520 if (pf->veb[i]) {
521 memset(&pf->veb[i]->stats, 0,
522 sizeof(pf->veb[i]->stats));
523 memset(&pf->veb[i]->stats_offsets, 0,
524 sizeof(pf->veb[i]->stats_offsets));
525 pf->veb[i]->stat_offsets_loaded = false;
526 }
527 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700528 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000529}
530
531/**
532 * i40e_stat_update48 - read and update a 48 bit stat from the chip
533 * @hw: ptr to the hardware info
534 * @hireg: the high 32 bit reg to read
535 * @loreg: the low 32 bit reg to read
536 * @offset_loaded: has the initial offset been loaded yet
537 * @offset: ptr to current offset value
538 * @stat: ptr to the stat
539 *
540 * Since the device stats are not reset at PFReset, they likely will not
541 * be zeroed when the driver starts. We'll save the first values read
542 * and use them as offsets to be subtracted from the raw values in order
543 * to report stats that count from zero. In the process, we also manage
544 * the potential roll-over.
545 **/
546static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
547 bool offset_loaded, u64 *offset, u64 *stat)
548{
549 u64 new_data;
550
Shannon Nelsonab600852014-01-17 15:36:39 -0800551 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000552 new_data = rd32(hw, loreg);
553 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
554 } else {
555 new_data = rd64(hw, loreg);
556 }
557 if (!offset_loaded)
558 *offset = new_data;
559 if (likely(new_data >= *offset))
560 *stat = new_data - *offset;
561 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400562 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000563 *stat &= 0xFFFFFFFFFFFFULL;
564}
565
566/**
567 * i40e_stat_update32 - read and update a 32 bit stat from the chip
568 * @hw: ptr to the hardware info
569 * @reg: the hw reg to read
570 * @offset_loaded: has the initial offset been loaded yet
571 * @offset: ptr to current offset value
572 * @stat: ptr to the stat
573 **/
574static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
575 bool offset_loaded, u64 *offset, u64 *stat)
576{
577 u32 new_data;
578
579 new_data = rd32(hw, reg);
580 if (!offset_loaded)
581 *offset = new_data;
582 if (likely(new_data >= *offset))
583 *stat = (u32)(new_data - *offset);
584 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400585 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000586}
587
588/**
589 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
590 * @vsi: the VSI to be updated
591 **/
592void i40e_update_eth_stats(struct i40e_vsi *vsi)
593{
594 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
595 struct i40e_pf *pf = vsi->back;
596 struct i40e_hw *hw = &pf->hw;
597 struct i40e_eth_stats *oes;
598 struct i40e_eth_stats *es; /* device's eth stats */
599
600 es = &vsi->eth_stats;
601 oes = &vsi->eth_stats_offsets;
602
603 /* Gather up the stats that the hw collects */
604 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
605 vsi->stat_offsets_loaded,
606 &oes->tx_errors, &es->tx_errors);
607 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
608 vsi->stat_offsets_loaded,
609 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000610 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
611 vsi->stat_offsets_loaded,
612 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
613 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
614 vsi->stat_offsets_loaded,
615 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000616
617 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
618 I40E_GLV_GORCL(stat_idx),
619 vsi->stat_offsets_loaded,
620 &oes->rx_bytes, &es->rx_bytes);
621 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
622 I40E_GLV_UPRCL(stat_idx),
623 vsi->stat_offsets_loaded,
624 &oes->rx_unicast, &es->rx_unicast);
625 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
626 I40E_GLV_MPRCL(stat_idx),
627 vsi->stat_offsets_loaded,
628 &oes->rx_multicast, &es->rx_multicast);
629 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
630 I40E_GLV_BPRCL(stat_idx),
631 vsi->stat_offsets_loaded,
632 &oes->rx_broadcast, &es->rx_broadcast);
633
634 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
635 I40E_GLV_GOTCL(stat_idx),
636 vsi->stat_offsets_loaded,
637 &oes->tx_bytes, &es->tx_bytes);
638 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
639 I40E_GLV_UPTCL(stat_idx),
640 vsi->stat_offsets_loaded,
641 &oes->tx_unicast, &es->tx_unicast);
642 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
643 I40E_GLV_MPTCL(stat_idx),
644 vsi->stat_offsets_loaded,
645 &oes->tx_multicast, &es->tx_multicast);
646 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
647 I40E_GLV_BPTCL(stat_idx),
648 vsi->stat_offsets_loaded,
649 &oes->tx_broadcast, &es->tx_broadcast);
650 vsi->stat_offsets_loaded = true;
651}
652
653/**
654 * i40e_update_veb_stats - Update Switch component statistics
655 * @veb: the VEB being updated
656 **/
657static void i40e_update_veb_stats(struct i40e_veb *veb)
658{
659 struct i40e_pf *pf = veb->pf;
660 struct i40e_hw *hw = &pf->hw;
661 struct i40e_eth_stats *oes;
662 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400663 struct i40e_veb_tc_stats *veb_oes;
664 struct i40e_veb_tc_stats *veb_es;
665 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000666
667 idx = veb->stats_idx;
668 es = &veb->stats;
669 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400670 veb_es = &veb->tc_stats;
671 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000672
673 /* Gather up the stats that the hw collects */
674 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
675 veb->stat_offsets_loaded,
676 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000677 if (hw->revision_id > 0)
678 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
679 veb->stat_offsets_loaded,
680 &oes->rx_unknown_protocol,
681 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000682 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
683 veb->stat_offsets_loaded,
684 &oes->rx_bytes, &es->rx_bytes);
685 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
686 veb->stat_offsets_loaded,
687 &oes->rx_unicast, &es->rx_unicast);
688 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
689 veb->stat_offsets_loaded,
690 &oes->rx_multicast, &es->rx_multicast);
691 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
692 veb->stat_offsets_loaded,
693 &oes->rx_broadcast, &es->rx_broadcast);
694
695 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
696 veb->stat_offsets_loaded,
697 &oes->tx_bytes, &es->tx_bytes);
698 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
699 veb->stat_offsets_loaded,
700 &oes->tx_unicast, &es->tx_unicast);
701 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
702 veb->stat_offsets_loaded,
703 &oes->tx_multicast, &es->tx_multicast);
704 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
705 veb->stat_offsets_loaded,
706 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400707 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
708 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
709 I40E_GLVEBTC_RPCL(i, idx),
710 veb->stat_offsets_loaded,
711 &veb_oes->tc_rx_packets[i],
712 &veb_es->tc_rx_packets[i]);
713 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
714 I40E_GLVEBTC_RBCL(i, idx),
715 veb->stat_offsets_loaded,
716 &veb_oes->tc_rx_bytes[i],
717 &veb_es->tc_rx_bytes[i]);
718 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
719 I40E_GLVEBTC_TPCL(i, idx),
720 veb->stat_offsets_loaded,
721 &veb_oes->tc_tx_packets[i],
722 &veb_es->tc_tx_packets[i]);
723 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
724 I40E_GLVEBTC_TBCL(i, idx),
725 veb->stat_offsets_loaded,
726 &veb_oes->tc_tx_bytes[i],
727 &veb_es->tc_tx_bytes[i]);
728 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000729 veb->stat_offsets_loaded = true;
730}
731
Vasu Dev38e00432014-08-01 13:27:03 -0700732#ifdef I40E_FCOE
733/**
734 * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
735 * @vsi: the VSI that is capable of doing FCoE
736 **/
737static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
738{
739 struct i40e_pf *pf = vsi->back;
740 struct i40e_hw *hw = &pf->hw;
741 struct i40e_fcoe_stats *ofs;
742 struct i40e_fcoe_stats *fs; /* device's eth stats */
743 int idx;
744
745 if (vsi->type != I40E_VSI_FCOE)
746 return;
747
Kiran Patil4147e2c2016-01-15 14:33:14 -0800748 idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
Vasu Dev38e00432014-08-01 13:27:03 -0700749 fs = &vsi->fcoe_stats;
750 ofs = &vsi->fcoe_stats_offsets;
751
752 i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
753 vsi->fcoe_stat_offsets_loaded,
754 &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
755 i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
756 vsi->fcoe_stat_offsets_loaded,
757 &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
758 i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
759 vsi->fcoe_stat_offsets_loaded,
760 &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
761 i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
762 vsi->fcoe_stat_offsets_loaded,
763 &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
764 i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
765 vsi->fcoe_stat_offsets_loaded,
766 &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
767 i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
768 vsi->fcoe_stat_offsets_loaded,
769 &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
770 i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
771 vsi->fcoe_stat_offsets_loaded,
772 &ofs->fcoe_last_error, &fs->fcoe_last_error);
773 i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
774 vsi->fcoe_stat_offsets_loaded,
775 &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
776
777 vsi->fcoe_stat_offsets_loaded = true;
778}
779
780#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000781/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000782 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000783 * @vsi: the VSI to be updated
784 *
785 * There are a few instances where we store the same stat in a
786 * couple of different structs. This is partly because we have
787 * the netdev stats that need to be filled out, which is slightly
788 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000789 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000790 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000791static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000792{
793 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000794 struct rtnl_link_stats64 *ons;
795 struct rtnl_link_stats64 *ns; /* netdev stats */
796 struct i40e_eth_stats *oes;
797 struct i40e_eth_stats *es; /* device's eth stats */
798 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800799 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000800 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000802 u64 bytes, packets;
803 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400804 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400805 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000806 u64 rx_p, rx_b;
807 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000808 u16 q;
809
810 if (test_bit(__I40E_DOWN, &vsi->state) ||
811 test_bit(__I40E_CONFIG_BUSY, &pf->state))
812 return;
813
814 ns = i40e_get_vsi_stats_struct(vsi);
815 ons = &vsi->net_stats_offsets;
816 es = &vsi->eth_stats;
817 oes = &vsi->eth_stats_offsets;
818
819 /* Gather up the netdev and vsi stats that the driver collects
820 * on the fly during packet processing
821 */
822 rx_b = rx_p = 0;
823 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400824 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800825 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000826 rx_page = 0;
827 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000828 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000829 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000830 /* locate Tx ring */
831 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000832
Alexander Duyck980e9b12013-09-28 06:01:03 +0000833 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700834 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000835 packets = p->stats.packets;
836 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700837 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000838 tx_b += bytes;
839 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000840 tx_restart += p->tx_stats.restart_queue;
841 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400842 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400843 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800844 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000845
846 /* Rx queue is part of the same block as Tx queue */
847 p = &p[1];
848 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700849 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000850 packets = p->stats.packets;
851 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700852 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000853 rx_b += bytes;
854 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000855 rx_buf += p->rx_stats.alloc_buff_failed;
856 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000857 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000858 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000859 vsi->tx_restart = tx_restart;
860 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400861 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400862 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800863 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000864 vsi->rx_page_failed = rx_page;
865 vsi->rx_buf_failed = rx_buf;
866
867 ns->rx_packets = rx_p;
868 ns->rx_bytes = rx_b;
869 ns->tx_packets = tx_p;
870 ns->tx_bytes = tx_b;
871
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000872 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000873 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000874 ons->tx_errors = oes->tx_errors;
875 ns->tx_errors = es->tx_errors;
876 ons->multicast = oes->rx_multicast;
877 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000878 ons->rx_dropped = oes->rx_discards;
879 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000880 ons->tx_dropped = oes->tx_discards;
881 ns->tx_dropped = es->tx_discards;
882
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000883 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000884 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000885 ns->rx_crc_errors = pf->stats.crc_errors;
886 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
887 ns->rx_length_errors = pf->stats.rx_length_errors;
888 }
889}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000890
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000891/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000892 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000893 * @pf: the PF to be updated
894 **/
895static void i40e_update_pf_stats(struct i40e_pf *pf)
896{
897 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
898 struct i40e_hw_port_stats *nsd = &pf->stats;
899 struct i40e_hw *hw = &pf->hw;
900 u32 val;
901 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000902
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000903 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
904 I40E_GLPRT_GORCL(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
907 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
908 I40E_GLPRT_GOTCL(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
911 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->eth.rx_discards,
914 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000915 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
916 I40E_GLPRT_UPRCL(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->eth.rx_unicast,
919 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000920 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
921 I40E_GLPRT_MPRCL(hw->port),
922 pf->stat_offsets_loaded,
923 &osd->eth.rx_multicast,
924 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000925 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
926 I40E_GLPRT_BPRCL(hw->port),
927 pf->stat_offsets_loaded,
928 &osd->eth.rx_broadcast,
929 &nsd->eth.rx_broadcast);
930 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
931 I40E_GLPRT_UPTCL(hw->port),
932 pf->stat_offsets_loaded,
933 &osd->eth.tx_unicast,
934 &nsd->eth.tx_unicast);
935 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
936 I40E_GLPRT_MPTCL(hw->port),
937 pf->stat_offsets_loaded,
938 &osd->eth.tx_multicast,
939 &nsd->eth.tx_multicast);
940 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
941 I40E_GLPRT_BPTCL(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->eth.tx_broadcast,
944 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000945
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000946 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
947 pf->stat_offsets_loaded,
948 &osd->tx_dropped_link_down,
949 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000950
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000951 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
952 pf->stat_offsets_loaded,
953 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000954
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000955 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
956 pf->stat_offsets_loaded,
957 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000958
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000959 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
960 pf->stat_offsets_loaded,
961 &osd->mac_local_faults,
962 &nsd->mac_local_faults);
963 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
964 pf->stat_offsets_loaded,
965 &osd->mac_remote_faults,
966 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000967
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000968 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
969 pf->stat_offsets_loaded,
970 &osd->rx_length_errors,
971 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000972
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000973 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->link_xon_rx, &nsd->link_xon_rx);
976 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
977 pf->stat_offsets_loaded,
978 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800979 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
980 pf->stat_offsets_loaded,
981 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000982 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000985
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000986 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800987 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
988 pf->stat_offsets_loaded,
989 &osd->priority_xoff_rx[i],
990 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000991 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000992 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000993 &osd->priority_xon_rx[i],
994 &nsd->priority_xon_rx[i]);
995 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000996 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000997 &osd->priority_xon_tx[i],
998 &nsd->priority_xon_tx[i]);
999 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001000 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001001 &osd->priority_xoff_tx[i],
1002 &nsd->priority_xoff_tx[i]);
1003 i40e_stat_update32(hw,
1004 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001005 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001006 &osd->priority_xon_2_xoff[i],
1007 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001008 }
1009
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001010 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1011 I40E_GLPRT_PRC64L(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->rx_size_64, &nsd->rx_size_64);
1014 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1015 I40E_GLPRT_PRC127L(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->rx_size_127, &nsd->rx_size_127);
1018 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1019 I40E_GLPRT_PRC255L(hw->port),
1020 pf->stat_offsets_loaded,
1021 &osd->rx_size_255, &nsd->rx_size_255);
1022 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1023 I40E_GLPRT_PRC511L(hw->port),
1024 pf->stat_offsets_loaded,
1025 &osd->rx_size_511, &nsd->rx_size_511);
1026 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1027 I40E_GLPRT_PRC1023L(hw->port),
1028 pf->stat_offsets_loaded,
1029 &osd->rx_size_1023, &nsd->rx_size_1023);
1030 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1031 I40E_GLPRT_PRC1522L(hw->port),
1032 pf->stat_offsets_loaded,
1033 &osd->rx_size_1522, &nsd->rx_size_1522);
1034 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1035 I40E_GLPRT_PRC9522L(hw->port),
1036 pf->stat_offsets_loaded,
1037 &osd->rx_size_big, &nsd->rx_size_big);
1038
1039 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1040 I40E_GLPRT_PTC64L(hw->port),
1041 pf->stat_offsets_loaded,
1042 &osd->tx_size_64, &nsd->tx_size_64);
1043 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1044 I40E_GLPRT_PTC127L(hw->port),
1045 pf->stat_offsets_loaded,
1046 &osd->tx_size_127, &nsd->tx_size_127);
1047 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1048 I40E_GLPRT_PTC255L(hw->port),
1049 pf->stat_offsets_loaded,
1050 &osd->tx_size_255, &nsd->tx_size_255);
1051 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1052 I40E_GLPRT_PTC511L(hw->port),
1053 pf->stat_offsets_loaded,
1054 &osd->tx_size_511, &nsd->tx_size_511);
1055 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1056 I40E_GLPRT_PTC1023L(hw->port),
1057 pf->stat_offsets_loaded,
1058 &osd->tx_size_1023, &nsd->tx_size_1023);
1059 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1060 I40E_GLPRT_PTC1522L(hw->port),
1061 pf->stat_offsets_loaded,
1062 &osd->tx_size_1522, &nsd->tx_size_1522);
1063 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1064 I40E_GLPRT_PTC9522L(hw->port),
1065 pf->stat_offsets_loaded,
1066 &osd->tx_size_big, &nsd->tx_size_big);
1067
1068 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1069 pf->stat_offsets_loaded,
1070 &osd->rx_undersize, &nsd->rx_undersize);
1071 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1072 pf->stat_offsets_loaded,
1073 &osd->rx_fragments, &nsd->rx_fragments);
1074 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1075 pf->stat_offsets_loaded,
1076 &osd->rx_oversize, &nsd->rx_oversize);
1077 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1078 pf->stat_offsets_loaded,
1079 &osd->rx_jabber, &nsd->rx_jabber);
1080
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001081 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001082 i40e_stat_update32(hw,
1083 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001084 pf->stat_offsets_loaded,
1085 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001086 i40e_stat_update32(hw,
1087 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001088 pf->stat_offsets_loaded,
1089 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001090 i40e_stat_update32(hw,
1091 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1092 pf->stat_offsets_loaded,
1093 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001094
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001095 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1096 nsd->tx_lpi_status =
1097 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1098 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1099 nsd->rx_lpi_status =
1100 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1101 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1102 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1103 pf->stat_offsets_loaded,
1104 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1105 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1106 pf->stat_offsets_loaded,
1107 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1108
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001109 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1110 !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
1111 nsd->fd_sb_status = true;
1112 else
1113 nsd->fd_sb_status = false;
1114
1115 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1116 !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
1117 nsd->fd_atr_status = true;
1118 else
1119 nsd->fd_atr_status = false;
1120
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001121 pf->stat_offsets_loaded = true;
1122}
1123
1124/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001125 * i40e_update_stats - Update the various statistics counters.
1126 * @vsi: the VSI to be updated
1127 *
1128 * Update the various stats for this VSI and its related entities.
1129 **/
1130void i40e_update_stats(struct i40e_vsi *vsi)
1131{
1132 struct i40e_pf *pf = vsi->back;
1133
1134 if (vsi == pf->vsi[pf->lan_vsi])
1135 i40e_update_pf_stats(pf);
1136
1137 i40e_update_vsi_stats(vsi);
Vasu Dev38e00432014-08-01 13:27:03 -07001138#ifdef I40E_FCOE
1139 i40e_update_fcoe_stats(vsi);
1140#endif
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001141}
1142
1143/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001144 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1145 * @vsi: the VSI to be searched
1146 * @macaddr: the MAC address
1147 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001148 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001149 * @is_netdev: make sure its a netdev filter, else doesn't matter
1150 *
1151 * Returns ptr to the filter object or NULL
1152 **/
1153static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1154 u8 *macaddr, s16 vlan,
1155 bool is_vf, bool is_netdev)
1156{
1157 struct i40e_mac_filter *f;
1158
1159 if (!vsi || !macaddr)
1160 return NULL;
1161
1162 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1163 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1164 (vlan == f->vlan) &&
1165 (!is_vf || f->is_vf) &&
1166 (!is_netdev || f->is_netdev))
1167 return f;
1168 }
1169 return NULL;
1170}
1171
1172/**
1173 * i40e_find_mac - Find a mac addr in the macvlan filters list
1174 * @vsi: the VSI to be searched
1175 * @macaddr: the MAC address we are searching for
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001176 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001177 * @is_netdev: make sure its a netdev filter, else doesn't matter
1178 *
1179 * Returns the first filter with the provided MAC address or NULL if
1180 * MAC address was not found
1181 **/
1182struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1183 bool is_vf, bool is_netdev)
1184{
1185 struct i40e_mac_filter *f;
1186
1187 if (!vsi || !macaddr)
1188 return NULL;
1189
1190 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1191 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1192 (!is_vf || f->is_vf) &&
1193 (!is_netdev || f->is_netdev))
1194 return f;
1195 }
1196 return NULL;
1197}
1198
1199/**
1200 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1201 * @vsi: the VSI to be searched
1202 *
1203 * Returns true if VSI is in vlan mode or false otherwise
1204 **/
1205bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1206{
1207 struct i40e_mac_filter *f;
1208
1209 /* Only -1 for all the filters denotes not in vlan mode
1210 * so we have to go through all the list in order to make sure
1211 */
1212 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001213 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001214 return true;
1215 }
1216
1217 return false;
1218}
1219
1220/**
1221 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1222 * @vsi: the VSI to be searched
1223 * @macaddr: the mac address to be filtered
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001224 * @is_vf: true if it is a VF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001225 * @is_netdev: true if it is a netdev
1226 *
1227 * Goes through all the macvlan filters and adds a
1228 * macvlan filter for each unique vlan that already exists
1229 *
1230 * Returns first filter found on success, else NULL
1231 **/
1232struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1233 bool is_vf, bool is_netdev)
1234{
1235 struct i40e_mac_filter *f;
1236
1237 list_for_each_entry(f, &vsi->mac_filter_list, list) {
Mitch Williamsecbb44e2015-07-10 19:35:56 -04001238 if (vsi->info.pvid)
1239 f->vlan = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001240 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1241 is_vf, is_netdev)) {
1242 if (!i40e_add_filter(vsi, macaddr, f->vlan,
Jesse Brandeburg8fb905b2014-01-17 15:36:33 -08001243 is_vf, is_netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244 return NULL;
1245 }
1246 }
1247
1248 return list_first_entry_or_null(&vsi->mac_filter_list,
1249 struct i40e_mac_filter, list);
1250}
1251
1252/**
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001253 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1254 * @vsi: the VSI to be searched
1255 * @macaddr: the mac address to be removed
1256 * @is_vf: true if it is a VF
1257 * @is_netdev: true if it is a netdev
1258 *
1259 * Removes a given MAC address from a VSI, regardless of VLAN
1260 *
1261 * Returns 0 for success, or error
1262 **/
1263int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1264 bool is_vf, bool is_netdev)
1265{
1266 struct i40e_mac_filter *f = NULL;
1267 int changed = 0;
1268
1269 WARN(!spin_is_locked(&vsi->mac_filter_list_lock),
1270 "Missing mac_filter_list_lock\n");
1271 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1272 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1273 (is_vf == f->is_vf) &&
1274 (is_netdev == f->is_netdev)) {
1275 f->counter--;
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001276 changed = 1;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001277 if (f->counter == 0)
1278 f->state = I40E_FILTER_REMOVE;
Mitch Williamsb36e9ab2015-11-19 11:34:16 -08001279 }
1280 }
1281 if (changed) {
1282 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1283 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1284 return 0;
1285 }
1286 return -ENOENT;
1287}
1288
1289/**
Greg Rose8c27d422014-05-22 06:31:56 +00001290 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1291 * @vsi: the PF Main VSI - inappropriate for any other VSI
1292 * @macaddr: the MAC address
Shannon Nelson30650cc2014-07-29 04:01:50 +00001293 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001294 * Remove whatever filter the firmware set up so the driver can manage
1295 * its own filtering intelligently.
Greg Rose8c27d422014-05-22 06:31:56 +00001296 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001297static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
Greg Rose8c27d422014-05-22 06:31:56 +00001298{
1299 struct i40e_aqc_remove_macvlan_element_data element;
1300 struct i40e_pf *pf = vsi->back;
Greg Rose8c27d422014-05-22 06:31:56 +00001301
1302 /* Only appropriate for the PF main VSI */
1303 if (vsi->type != I40E_VSI_MAIN)
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001304 return;
Greg Rose8c27d422014-05-22 06:31:56 +00001305
Shannon Nelson30650cc2014-07-29 04:01:50 +00001306 memset(&element, 0, sizeof(element));
Greg Rose8c27d422014-05-22 06:31:56 +00001307 ether_addr_copy(element.mac_addr, macaddr);
1308 element.vlan_tag = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001309 /* Ignore error returns, some firmware does it this way... */
1310 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1311 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
Shannon Nelson30650cc2014-07-29 04:01:50 +00001312
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001313 memset(&element, 0, sizeof(element));
1314 ether_addr_copy(element.mac_addr, macaddr);
1315 element.vlan_tag = 0;
1316 /* ...and some firmware does it this way. */
1317 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
Alan Bradya6cb9142016-09-06 18:05:07 -07001318 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001319 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
Greg Rose8c27d422014-05-22 06:31:56 +00001320}
1321
1322/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001323 * i40e_add_filter - Add a mac/vlan filter to the VSI
1324 * @vsi: the VSI to be searched
1325 * @macaddr: the MAC address
1326 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001327 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001328 * @is_netdev: make sure its a netdev filter, else doesn't matter
1329 *
1330 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001331 *
1332 * NOTE: This function is expected to be called with mac_filter_list_lock
1333 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001334 **/
1335struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1336 u8 *macaddr, s16 vlan,
1337 bool is_vf, bool is_netdev)
1338{
1339 struct i40e_mac_filter *f;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001340 int changed = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001341
1342 if (!vsi || !macaddr)
1343 return NULL;
1344
Kiran Patilf6bd0962016-06-20 09:10:34 -07001345 /* Do not allow broadcast filter to be added since broadcast filter
1346 * is added as part of add VSI for any newly created VSI except
1347 * FDIR VSI
1348 */
1349 if (is_broadcast_ether_addr(macaddr))
1350 return NULL;
1351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001352 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1353 if (!f) {
1354 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1355 if (!f)
1356 goto add_filter_out;
1357
Greg Rose9a173902014-05-22 06:32:02 +00001358 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001359 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001360 /* If we're in overflow promisc mode, set the state directly
1361 * to failed, so we don't bother to try sending the filter
1362 * to the hardware.
1363 */
1364 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1365 f->state = I40E_FILTER_FAILED;
1366 else
1367 f->state = I40E_FILTER_NEW;
1368 changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001369 INIT_LIST_HEAD(&f->list);
Kiran Patil04d5a212015-12-09 15:50:23 -08001370 list_add_tail(&f->list, &vsi->mac_filter_list);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001371 }
1372
1373 /* increment counter and add a new flag if needed */
1374 if (is_vf) {
1375 if (!f->is_vf) {
1376 f->is_vf = true;
1377 f->counter++;
1378 }
1379 } else if (is_netdev) {
1380 if (!f->is_netdev) {
1381 f->is_netdev = true;
1382 f->counter++;
1383 }
1384 } else {
1385 f->counter++;
1386 }
1387
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001388 if (changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001389 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1390 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1391 }
1392
1393add_filter_out:
1394 return f;
1395}
1396
1397/**
1398 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1399 * @vsi: the VSI to be searched
1400 * @macaddr: the MAC address
1401 * @vlan: the vlan
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001402 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001404 *
1405 * NOTE: This function is expected to be called with mac_filter_list_lock
1406 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001407 * ANOTHER NOTE: This function MUST be called from within the context of
1408 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1409 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001410 **/
1411void i40e_del_filter(struct i40e_vsi *vsi,
1412 u8 *macaddr, s16 vlan,
1413 bool is_vf, bool is_netdev)
1414{
1415 struct i40e_mac_filter *f;
1416
1417 if (!vsi || !macaddr)
1418 return;
1419
1420 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1421 if (!f || f->counter == 0)
1422 return;
1423
1424 if (is_vf) {
1425 if (f->is_vf) {
1426 f->is_vf = false;
1427 f->counter--;
1428 }
1429 } else if (is_netdev) {
1430 if (f->is_netdev) {
1431 f->is_netdev = false;
1432 f->counter--;
1433 }
1434 } else {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001435 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001436 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001437
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001438 min_f += (f->is_vf ? 1 : 0);
1439 min_f += (f->is_netdev ? 1 : 0);
1440
1441 if (f->counter > min_f)
1442 f->counter--;
1443 }
1444
1445 /* counter == 0 tells sync_filters_subtask to
1446 * remove the filter from the firmware's list
1447 */
1448 if (f->counter == 0) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001449 if ((f->state == I40E_FILTER_FAILED) ||
1450 (f->state == I40E_FILTER_NEW)) {
1451 /* this one never got added by the FW. Just remove it,
1452 * no need to sync anything.
1453 */
1454 list_del(&f->list);
1455 kfree(f);
1456 } else {
1457 f->state = I40E_FILTER_REMOVE;
1458 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1459 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1460 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001461 }
1462}
1463
1464/**
1465 * i40e_set_mac - NDO callback to set mac address
1466 * @netdev: network interface device structure
1467 * @p: pointer to an address structure
1468 *
1469 * Returns 0 on success, negative on failure
1470 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001471#ifdef I40E_FCOE
1472int i40e_set_mac(struct net_device *netdev, void *p)
1473#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001474static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001475#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001476{
1477 struct i40e_netdev_priv *np = netdev_priv(netdev);
1478 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001479 struct i40e_pf *pf = vsi->back;
1480 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001481 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001482
1483 if (!is_valid_ether_addr(addr->sa_data))
1484 return -EADDRNOTAVAIL;
1485
Shannon Nelson30650cc2014-07-29 04:01:50 +00001486 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1487 netdev_info(netdev, "already using mac address %pM\n",
1488 addr->sa_data);
1489 return 0;
1490 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001491
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001492 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1493 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1494 return -EADDRNOTAVAIL;
1495
Shannon Nelson30650cc2014-07-29 04:01:50 +00001496 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1497 netdev_info(netdev, "returning to hw mac address %pM\n",
1498 hw->mac.addr);
1499 else
1500 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1501
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001502 spin_lock_bh(&vsi->mac_filter_list_lock);
1503 i40e_del_mac_all_vlan(vsi, netdev->dev_addr, false, true);
1504 i40e_put_mac_in_vlan(vsi, addr->sa_data, false, true);
1505 spin_unlock_bh(&vsi->mac_filter_list_lock);
1506 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001507 if (vsi->type == I40E_VSI_MAIN) {
1508 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001510 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001511 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001512 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001513 if (ret)
1514 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1515 i40e_stat_str(hw, ret),
1516 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001517 }
1518
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001519 /* schedule our worker thread which will take care of
1520 * applying the new filter changes
1521 */
1522 i40e_service_event_schedule(vsi->back);
1523 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001524}
1525
1526/**
1527 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1528 * @vsi: the VSI being setup
1529 * @ctxt: VSI context structure
1530 * @enabled_tc: Enabled TCs bitmap
1531 * @is_add: True if called before Add VSI
1532 *
1533 * Setup VSI queue mapping for enabled traffic classes.
1534 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001535#ifdef I40E_FCOE
1536void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1537 struct i40e_vsi_context *ctxt,
1538 u8 enabled_tc,
1539 bool is_add)
1540#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001541static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1542 struct i40e_vsi_context *ctxt,
1543 u8 enabled_tc,
1544 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001545#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001546{
1547 struct i40e_pf *pf = vsi->back;
1548 u16 sections = 0;
1549 u8 netdev_tc = 0;
1550 u16 numtc = 0;
1551 u16 qcount;
1552 u8 offset;
1553 u16 qmap;
1554 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001555 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001556
1557 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1558 offset = 0;
1559
1560 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1561 /* Find numtc from enabled TC bitmap */
1562 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001563 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001564 numtc++;
1565 }
1566 if (!numtc) {
1567 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1568 numtc = 1;
1569 }
1570 } else {
1571 /* At least TC0 is enabled in case of non-DCB case */
1572 numtc = 1;
1573 }
1574
1575 vsi->tc_config.numtc = numtc;
1576 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001577 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001578 qcount = vsi->alloc_queue_pairs;
1579
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001580 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001581 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582
1583 /* Setup queue offset/count for all TCs for given VSI */
1584 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1585 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001586 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001587 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001588 int pow, num_qps;
1589
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001590 switch (vsi->type) {
1591 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001592 qcount = min_t(int, pf->alloc_rss_size,
1593 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001594 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001595#ifdef I40E_FCOE
1596 case I40E_VSI_FCOE:
1597 qcount = num_tc_qps;
1598 break;
1599#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001600 case I40E_VSI_FDIR:
1601 case I40E_VSI_SRIOV:
1602 case I40E_VSI_VMDQ2:
1603 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001604 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001605 WARN_ON(i != 0);
1606 break;
1607 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001608 vsi->tc_config.tc_info[i].qoffset = offset;
1609 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001610
Shannon Nelson1e200e42015-02-27 09:15:24 +00001611 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001612 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001613 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001614 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001615 pow++;
1616 num_qps >>= 1;
1617 }
1618
1619 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1620 qmap =
1621 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1622 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1623
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001624 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001625 } else {
1626 /* TC is not enabled so set the offset to
1627 * default queue and allocate one queue
1628 * for the given TC.
1629 */
1630 vsi->tc_config.tc_info[i].qoffset = 0;
1631 vsi->tc_config.tc_info[i].qcount = 1;
1632 vsi->tc_config.tc_info[i].netdev_tc = 0;
1633
1634 qmap = 0;
1635 }
1636 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1637 }
1638
1639 /* Set actual Tx/Rx queue pairs */
1640 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001641 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1642 if (vsi->req_queue_pairs > 0)
1643 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001644 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001645 vsi->num_queue_pairs = pf->num_lan_msix;
1646 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001647
1648 /* Scheduler section valid can only be set for ADD VSI */
1649 if (is_add) {
1650 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1651
1652 ctxt->info.up_enable_bits = enabled_tc;
1653 }
1654 if (vsi->type == I40E_VSI_SRIOV) {
1655 ctxt->info.mapping_flags |=
1656 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1657 for (i = 0; i < vsi->num_queue_pairs; i++)
1658 ctxt->info.queue_mapping[i] =
1659 cpu_to_le16(vsi->base_queue + i);
1660 } else {
1661 ctxt->info.mapping_flags |=
1662 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1663 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1664 }
1665 ctxt->info.valid_sections |= cpu_to_le16(sections);
1666}
1667
1668/**
1669 * i40e_set_rx_mode - NDO callback to set the netdev filters
1670 * @netdev: network interface device structure
1671 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001672#ifdef I40E_FCOE
1673void i40e_set_rx_mode(struct net_device *netdev)
1674#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001675static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001676#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001677{
1678 struct i40e_netdev_priv *np = netdev_priv(netdev);
1679 struct i40e_mac_filter *f, *ftmp;
1680 struct i40e_vsi *vsi = np->vsi;
1681 struct netdev_hw_addr *uca;
1682 struct netdev_hw_addr *mca;
1683 struct netdev_hw_addr *ha;
1684
Kiran Patil21659032015-09-30 14:09:03 -04001685 spin_lock_bh(&vsi->mac_filter_list_lock);
1686
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001687 /* add addr if not already in the filter list */
1688 netdev_for_each_uc_addr(uca, netdev) {
1689 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1690 if (i40e_is_vsi_in_vlan(vsi))
1691 i40e_put_mac_in_vlan(vsi, uca->addr,
1692 false, true);
1693 else
1694 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1695 false, true);
1696 }
1697 }
1698
1699 netdev_for_each_mc_addr(mca, netdev) {
1700 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1701 if (i40e_is_vsi_in_vlan(vsi))
1702 i40e_put_mac_in_vlan(vsi, mca->addr,
1703 false, true);
1704 else
1705 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1706 false, true);
1707 }
1708 }
1709
1710 /* remove filter if not in netdev list */
1711 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001712
1713 if (!f->is_netdev)
1714 continue;
1715
Shannon Nelson2f41f332015-08-26 15:14:20 -04001716 netdev_for_each_mc_addr(mca, netdev)
1717 if (ether_addr_equal(mca->addr, f->macaddr))
1718 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001719
Shannon Nelson2f41f332015-08-26 15:14:20 -04001720 netdev_for_each_uc_addr(uca, netdev)
1721 if (ether_addr_equal(uca->addr, f->macaddr))
1722 goto bottom_of_search_loop;
1723
1724 for_each_dev_addr(netdev, ha)
1725 if (ether_addr_equal(ha->addr, f->macaddr))
1726 goto bottom_of_search_loop;
1727
1728 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1729 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1730
1731bottom_of_search_loop:
1732 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001733 }
Kiran Patil21659032015-09-30 14:09:03 -04001734 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001735
1736 /* check for other flag changes */
1737 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1738 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1739 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1740 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001741
1742 /* schedule our worker thread which will take care of
1743 * applying the new filter changes
1744 */
1745 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001746}
1747
1748/**
Kiran Patil21659032015-09-30 14:09:03 -04001749 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1750 * @vsi: pointer to vsi struct
1751 * @from: Pointer to list which contains MAC filter entries - changes to
1752 * those entries needs to be undone.
1753 *
1754 * MAC filter entries from list were slated to be removed from device.
1755 **/
1756static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1757 struct list_head *from)
1758{
1759 struct i40e_mac_filter *f, *ftmp;
1760
1761 list_for_each_entry_safe(f, ftmp, from, list) {
Kiran Patil21659032015-09-30 14:09:03 -04001762 /* Move the element back into MAC filter list*/
1763 list_move_tail(&f->list, &vsi->mac_filter_list);
1764 }
1765}
1766
1767/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001768 * i40e_update_filter_state - Update filter state based on return data
1769 * from firmware
1770 * @count: Number of filters added
1771 * @add_list: return data from fw
1772 * @head: pointer to first filter in current batch
1773 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001774 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001775 * MAC filter entries from list were slated to be added to device. Returns
1776 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001777 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001778static int
1779i40e_update_filter_state(int count,
1780 struct i40e_aqc_add_macvlan_element_data *add_list,
1781 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001782{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001783 int retval = 0;
1784 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001785
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001786
1787 if (!aq_err) {
1788 retval = count;
1789 /* Everything's good, mark all filters active. */
1790 for (i = 0; i < count ; i++) {
1791 add_head->state = I40E_FILTER_ACTIVE;
1792 add_head = list_next_entry(add_head, list);
1793 }
1794 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1795 /* Device ran out of filter space. Check the return value
1796 * for each filter to see which ones are active.
1797 */
1798 for (i = 0; i < count ; i++) {
1799 if (add_list[i].match_method ==
1800 I40E_AQC_MM_ERR_NO_RES) {
1801 add_head->state = I40E_FILTER_FAILED;
1802 } else {
1803 add_head->state = I40E_FILTER_ACTIVE;
1804 retval++;
1805 }
1806 add_head = list_next_entry(add_head, list);
1807 }
1808 } else {
1809 /* Some other horrible thing happened, fail all filters */
1810 retval = 0;
1811 for (i = 0; i < count ; i++) {
1812 add_head->state = I40E_FILTER_FAILED;
1813 add_head = list_next_entry(add_head, list);
1814 }
Kiran Patil21659032015-09-30 14:09:03 -04001815 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001816 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001817}
1818
1819/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001820 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1821 * @vsi: ptr to the VSI
1822 *
1823 * Push any outstanding VSI filter changes through the AdminQ.
1824 *
1825 * Returns 0 or error value
1826 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001827int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001828{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001829 struct i40e_mac_filter *f, *ftmp, *add_head = NULL;
1830 struct list_head tmp_add_list, tmp_del_list;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001831 struct i40e_hw *hw = &vsi->back->hw;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001832 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001833 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001834 int filter_list_len = 0;
1835 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001836 i40e_status aq_ret = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001837 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001838 struct i40e_pf *pf;
1839 int num_add = 0;
1840 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001841 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001842 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001843 int list_size;
1844 int fcnt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001845
1846 /* empty array typed pointers, kcalloc later */
1847 struct i40e_aqc_add_macvlan_element_data *add_list;
1848 struct i40e_aqc_remove_macvlan_element_data *del_list;
1849
1850 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1851 usleep_range(1000, 2000);
1852 pf = vsi->back;
1853
1854 if (vsi->netdev) {
1855 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1856 vsi->current_netdev_flags = vsi->netdev->flags;
1857 }
1858
Kiran Patil21659032015-09-30 14:09:03 -04001859 INIT_LIST_HEAD(&tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001860 INIT_LIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001861
Shannon Nelson2d1de822016-05-16 10:26:44 -07001862 if (vsi->type == I40E_VSI_SRIOV)
1863 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1864 else if (vsi->type != I40E_VSI_MAIN)
1865 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1866
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001867 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1868 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1869
Kiran Patil21659032015-09-30 14:09:03 -04001870 spin_lock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001871 /* Create a list of filters to delete. */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001872 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001873 if (f->state == I40E_FILTER_REMOVE) {
1874 WARN_ON(f->counter != 0);
1875 /* Move the element into temporary del_list */
1876 list_move_tail(&f->list, &tmp_del_list);
1877 vsi->active_filters--;
Kiran Patil21659032015-09-30 14:09:03 -04001878 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001879 if (f->state == I40E_FILTER_NEW) {
1880 WARN_ON(f->counter == 0);
1881 /* Move the element into temporary add_list */
1882 list_move_tail(&f->list, &tmp_add_list);
1883 }
Kiran Patil21659032015-09-30 14:09:03 -04001884 }
1885 spin_unlock_bh(&vsi->mac_filter_list_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001886 }
1887
1888 /* Now process 'del_list' outside the lock */
1889 if (!list_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001890 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04001891 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001892 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08001893 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001894 del_list = kzalloc(list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001895 if (!del_list) {
Kiran Patil21659032015-09-30 14:09:03 -04001896 /* Undo VSI's MAC filter entry element updates */
1897 spin_lock_bh(&vsi->mac_filter_list_lock);
1898 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001899 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001900 retval = -ENOMEM;
1901 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001902 }
1903
1904 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001905 cmd_flags = 0;
1906
1907 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001908 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001909 if (f->vlan == I40E_VLAN_ANY) {
1910 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07001911 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001912 } else {
1913 del_list[num_del].vlan_tag =
1914 cpu_to_le16((u16)(f->vlan));
1915 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001916
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001917 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1918 del_list[num_del].flags = cmd_flags;
1919 num_del++;
1920
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001921 /* flush a full buffer */
1922 if (num_del == filter_list_len) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001923 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid,
1924 del_list,
1925 num_del, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001926 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001927 num_del = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001928 memset(del_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001929
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001930 /* Explicitly ignore and do not report when
1931 * firmware returns ENOENT.
1932 */
1933 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001934 retval = -EIO;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001935 dev_info(&pf->pdev->dev,
1936 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
Shannon Nelson2d1de822016-05-16 10:26:44 -07001937 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001938 i40e_stat_str(hw, aq_ret),
1939 i40e_aq_str(hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001940 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001941 }
Kiran Patil21659032015-09-30 14:09:03 -04001942 /* Release memory for MAC filter entries which were
1943 * synced up with HW.
1944 */
1945 list_del(&f->list);
1946 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001947 }
Kiran Patil21659032015-09-30 14:09:03 -04001948
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001949 if (num_del) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001950 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, del_list,
1951 num_del, NULL);
1952 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001953 num_del = 0;
1954
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001955 /* Explicitly ignore and do not report when firmware
1956 * returns ENOENT.
1957 */
1958 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1959 retval = -EIO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001960 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07001961 "ignoring delete macvlan error on %s, err %s aq_err %s\n",
1962 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001963 i40e_stat_str(hw, aq_ret),
1964 i40e_aq_str(hw, aq_err));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001965 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001966 }
1967
1968 kfree(del_list);
1969 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001970 }
1971
1972 if (!list_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001973 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001974 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08001975 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001976 list_size = filter_list_len *
1977 sizeof(struct i40e_aqc_add_macvlan_element_data);
1978 add_list = kzalloc(list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001979 if (!add_list) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001980 retval = -ENOMEM;
1981 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001982 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001983 num_add = 0;
1984 list_for_each_entry(f, &tmp_add_list, list) {
1985 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1986 &vsi->state)) {
1987 f->state = I40E_FILTER_FAILED;
1988 continue;
1989 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001990 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001991 if (num_add == 0)
1992 add_head = f;
1993 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00001994 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001995 if (f->vlan == I40E_VLAN_ANY) {
1996 add_list[num_add].vlan_tag = 0;
1997 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1998 } else {
1999 add_list[num_add].vlan_tag =
2000 cpu_to_le16((u16)(f->vlan));
2001 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002002 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002003 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002004 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2005 num_add++;
2006
2007 /* flush a full buffer */
2008 if (num_add == filter_list_len) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002009 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08002010 add_list, num_add,
2011 NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002012 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002013 fcnt = i40e_update_filter_state(num_add,
2014 add_list,
2015 add_head,
2016 aq_ret);
2017 vsi->active_filters += fcnt;
2018
2019 if (fcnt != num_add) {
2020 promisc_changed = true;
2021 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2022 &vsi->state);
2023 vsi->promisc_threshold =
2024 (vsi->active_filters * 3) / 4;
2025 dev_warn(&pf->pdev->dev,
2026 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2027 i40e_aq_str(hw, aq_err),
2028 vsi_name);
2029 }
2030 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002031 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002032 }
2033 }
2034 if (num_add) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002035 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08002036 add_list, num_add, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002037 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002038 fcnt = i40e_update_filter_state(num_add, add_list,
2039 add_head, aq_ret);
2040 vsi->active_filters += fcnt;
2041 if (fcnt != num_add) {
2042 promisc_changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002043 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2044 &vsi->state);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002045 vsi->promisc_threshold =
2046 (vsi->active_filters * 3) / 4;
2047 dev_warn(&pf->pdev->dev,
2048 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2049 i40e_aq_str(hw, aq_err), vsi_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002050 }
2051 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002052 /* Now move all of the filters from the temp add list back to
2053 * the VSI's list.
2054 */
2055 spin_lock_bh(&vsi->mac_filter_list_lock);
2056 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
2057 list_move_tail(&f->list, &vsi->mac_filter_list);
2058 }
2059 spin_unlock_bh(&vsi->mac_filter_list_lock);
2060 kfree(add_list);
2061 add_list = NULL;
2062 }
2063
2064 /* Check to see if we can drop out of overflow promiscuous mode. */
2065 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2066 (vsi->active_filters < vsi->promisc_threshold)) {
2067 int failed_count = 0;
2068 /* See if we have any failed filters. We can't drop out of
2069 * promiscuous until these have all been deleted.
2070 */
2071 spin_lock_bh(&vsi->mac_filter_list_lock);
2072 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2073 if (f->state == I40E_FILTER_FAILED)
2074 failed_count++;
2075 }
2076 spin_unlock_bh(&vsi->mac_filter_list_lock);
2077 if (!failed_count) {
2078 dev_info(&pf->pdev->dev,
2079 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2080 vsi_name);
2081 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2082 promisc_changed = true;
2083 vsi->promisc_threshold = 0;
2084 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002085 }
2086
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002087 /* if the VF is not trusted do not do promisc */
2088 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2089 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2090 goto out;
2091 }
2092
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002093 /* check for changes in promiscuous modes */
2094 if (changed_flags & IFF_ALLMULTI) {
2095 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002098 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2099 vsi->seid,
2100 cur_multipromisc,
2101 NULL);
2102 if (aq_ret) {
2103 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002104 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002105 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002106 "set multi promisc failed on %s, err %s aq_err %s\n",
2107 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002108 i40e_stat_str(hw, aq_ret),
2109 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002110 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002111 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002112 if ((changed_flags & IFF_PROMISC) ||
2113 (promisc_changed &&
2114 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002115 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002117 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2118 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2119 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002120 if ((vsi->type == I40E_VSI_MAIN) &&
2121 (pf->lan_veb != I40E_NO_VEB) &&
2122 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002123 /* set defport ON for Main VSI instead of true promisc
2124 * this way we will get all unicast/multicast and VLAN
2125 * promisc behavior but will not get VF or VMDq traffic
2126 * replicated on the Main VSI.
2127 */
2128 if (pf->cur_promisc != cur_promisc) {
2129 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002130 if (cur_promisc)
2131 aq_ret =
2132 i40e_aq_set_default_vsi(hw,
2133 vsi->seid,
2134 NULL);
2135 else
2136 aq_ret =
2137 i40e_aq_clear_default_vsi(hw,
2138 vsi->seid,
2139 NULL);
2140 if (aq_ret) {
2141 retval = i40e_aq_rc_to_posix(aq_ret,
2142 hw->aq.asq_last_status);
2143 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002144 "Set default VSI failed on %s, err %s, aq_err %s\n",
2145 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002146 i40e_stat_str(hw, aq_ret),
2147 i40e_aq_str(hw,
2148 hw->aq.asq_last_status));
2149 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002150 }
2151 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002152 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002153 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002154 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002155 cur_promisc, NULL,
2156 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002157 if (aq_ret) {
2158 retval =
2159 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002160 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002161 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002162 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2163 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002164 i40e_stat_str(hw, aq_ret),
2165 i40e_aq_str(hw,
2166 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002167 }
2168 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002169 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002170 vsi->seid,
2171 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002172 if (aq_ret) {
2173 retval =
2174 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002175 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002176 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002177 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2178 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002179 i40e_stat_str(hw, aq_ret),
2180 i40e_aq_str(hw,
2181 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002182 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002183 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002184 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2185 vsi->seid,
2186 cur_promisc, NULL);
2187 if (aq_ret) {
2188 retval = i40e_aq_rc_to_posix(aq_ret,
2189 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002190 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002191 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002192 i40e_stat_str(hw, aq_ret),
2193 i40e_aq_str(hw,
2194 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002195 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002196 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002197out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002198 /* if something went wrong then set the changed flag so we try again */
2199 if (retval)
2200 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002202 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002203 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002204}
2205
2206/**
2207 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2208 * @pf: board private structure
2209 **/
2210static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2211{
2212 int v;
2213
2214 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2215 return;
2216 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2217
Mitch Williams505682c2014-05-20 08:01:37 +00002218 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002219 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002220 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2221 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2222
2223 if (ret) {
2224 /* come back and try again later */
2225 pf->flags |= I40E_FLAG_FILTER_SYNC;
2226 break;
2227 }
2228 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002229 }
2230}
2231
2232/**
2233 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2234 * @netdev: network interface device structure
2235 * @new_mtu: new value for maximum frame size
2236 *
2237 * Returns 0 on success, negative on failure
2238 **/
2239static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2240{
2241 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg61a46a42014-04-23 04:50:05 +00002242 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002243 struct i40e_vsi *vsi = np->vsi;
2244
2245 /* MTU < 68 is an error and causes problems on some kernels */
2246 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
2247 return -EINVAL;
2248
2249 netdev_info(netdev, "changing MTU from %d to %d\n",
2250 netdev->mtu, new_mtu);
2251 netdev->mtu = new_mtu;
2252 if (netif_running(netdev))
2253 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002254 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002255 return 0;
2256}
2257
2258/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002259 * i40e_ioctl - Access the hwtstamp interface
2260 * @netdev: network interface device structure
2261 * @ifr: interface request data
2262 * @cmd: ioctl command
2263 **/
2264int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2265{
2266 struct i40e_netdev_priv *np = netdev_priv(netdev);
2267 struct i40e_pf *pf = np->vsi->back;
2268
2269 switch (cmd) {
2270 case SIOCGHWTSTAMP:
2271 return i40e_ptp_get_ts_config(pf, ifr);
2272 case SIOCSHWTSTAMP:
2273 return i40e_ptp_set_ts_config(pf, ifr);
2274 default:
2275 return -EOPNOTSUPP;
2276 }
2277}
2278
2279/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002280 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2281 * @vsi: the vsi being adjusted
2282 **/
2283void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2284{
2285 struct i40e_vsi_context ctxt;
2286 i40e_status ret;
2287
2288 if ((vsi->info.valid_sections &
2289 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2290 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2291 return; /* already enabled */
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_STR_BOTH;
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_stripping_disable - Turn off vlan stripping for the VSI
2311 * @vsi: the vsi being adjusted
2312 **/
2313void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2314{
2315 struct i40e_vsi_context ctxt;
2316 i40e_status ret;
2317
2318 if ((vsi->info.valid_sections &
2319 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2320 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2321 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2322 return; /* already disabled */
2323
2324 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2325 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2326 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2327
2328 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002329 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002330 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2331 if (ret) {
2332 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002333 "update vlan stripping failed, err %s aq_err %s\n",
2334 i40e_stat_str(&vsi->back->hw, ret),
2335 i40e_aq_str(&vsi->back->hw,
2336 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002337 }
2338}
2339
2340/**
2341 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2342 * @netdev: network interface to be adjusted
2343 * @features: netdev features to test if VLAN offload is enabled or not
2344 **/
2345static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2346{
2347 struct i40e_netdev_priv *np = netdev_priv(netdev);
2348 struct i40e_vsi *vsi = np->vsi;
2349
2350 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2351 i40e_vlan_stripping_enable(vsi);
2352 else
2353 i40e_vlan_stripping_disable(vsi);
2354}
2355
2356/**
2357 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2358 * @vsi: the vsi being configured
2359 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2360 **/
2361int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2362{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002363 struct i40e_mac_filter *f, *ftmp, *add_f;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002365
2366 is_vf = (vsi->type == I40E_VSI_SRIOV);
2367 is_netdev = !!(vsi->netdev);
2368
Kiran Patil21659032015-09-30 14:09:03 -04002369 /* Locked once because all functions invoked below iterates list*/
2370 spin_lock_bh(&vsi->mac_filter_list_lock);
2371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002372 if (is_netdev) {
2373 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2374 is_vf, is_netdev);
2375 if (!add_f) {
2376 dev_info(&vsi->back->pdev->dev,
2377 "Could not add vlan filter %d for %pM\n",
2378 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002379 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002380 return -ENOMEM;
2381 }
2382 }
2383
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002384 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002385 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2386 if (!add_f) {
2387 dev_info(&vsi->back->pdev->dev,
2388 "Could not add vlan filter %d for %pM\n",
2389 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002390 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002391 return -ENOMEM;
2392 }
2393 }
2394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002395 /* Now if we add a vlan tag, make sure to check if it is the first
2396 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2397 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002398 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002399 */
2400 if (vid > 0) {
2401 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2402 I40E_VLAN_ANY,
2403 is_vf, is_netdev)) {
2404 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2405 I40E_VLAN_ANY, is_vf, is_netdev);
2406 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2407 is_vf, is_netdev);
2408 if (!add_f) {
2409 dev_info(&vsi->back->pdev->dev,
2410 "Could not add filter 0 for %pM\n",
2411 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002412 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002413 return -ENOMEM;
2414 }
2415 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002416 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002417
Greg Rose8d82a7c2014-01-13 16:13:04 -08002418 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2419 if (vid > 0 && !vsi->info.pvid) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002420 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002421 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2422 is_vf, is_netdev))
2423 continue;
2424 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2425 is_vf, is_netdev);
2426 add_f = i40e_add_filter(vsi, f->macaddr,
2427 0, is_vf, is_netdev);
2428 if (!add_f) {
2429 dev_info(&vsi->back->pdev->dev,
2430 "Could not add filter 0 for %pM\n",
2431 f->macaddr);
2432 spin_unlock_bh(&vsi->mac_filter_list_lock);
2433 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002434 }
2435 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002436 }
2437
Kiran Patil21659032015-09-30 14:09:03 -04002438 spin_unlock_bh(&vsi->mac_filter_list_lock);
2439
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002440 /* schedule our worker thread which will take care of
2441 * applying the new filter changes
2442 */
2443 i40e_service_event_schedule(vsi->back);
2444 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002445}
2446
2447/**
2448 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2449 * @vsi: the vsi being configured
2450 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002451 *
2452 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002453 **/
2454int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2455{
2456 struct net_device *netdev = vsi->netdev;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002457 struct i40e_mac_filter *f, *ftmp, *add_f;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002458 bool is_vf, is_netdev;
2459 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002460
2461 is_vf = (vsi->type == I40E_VSI_SRIOV);
2462 is_netdev = !!(netdev);
2463
Kiran Patil21659032015-09-30 14:09:03 -04002464 /* Locked once because all functions invoked below iterates list */
2465 spin_lock_bh(&vsi->mac_filter_list_lock);
2466
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002467 if (is_netdev)
2468 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2469
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002470 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002471 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002473 /* go through all the filters for this VSI and if there is only
2474 * vid == 0 it means there are no other filters, so vid 0 must
2475 * be replaced with -1. This signifies that we should from now
2476 * on accept any traffic (with any tag present, or untagged)
2477 */
2478 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2479 if (is_netdev) {
2480 if (f->vlan &&
2481 ether_addr_equal(netdev->dev_addr, f->macaddr))
2482 filter_count++;
2483 }
2484
2485 if (f->vlan)
2486 filter_count++;
2487 }
2488
2489 if (!filter_count && is_netdev) {
2490 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2491 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2492 is_vf, is_netdev);
2493 if (!f) {
2494 dev_info(&vsi->back->pdev->dev,
2495 "Could not add filter %d for %pM\n",
2496 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002497 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002498 return -ENOMEM;
2499 }
2500 }
2501
2502 if (!filter_count) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002503 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002504 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2505 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002506 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002507 if (!add_f) {
2508 dev_info(&vsi->back->pdev->dev,
2509 "Could not add filter %d for %pM\n",
2510 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002511 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002512 return -ENOMEM;
2513 }
2514 }
2515 }
2516
Kiran Patil21659032015-09-30 14:09:03 -04002517 spin_unlock_bh(&vsi->mac_filter_list_lock);
2518
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002519 /* schedule our worker thread which will take care of
2520 * applying the new filter changes
2521 */
2522 i40e_service_event_schedule(vsi->back);
2523 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524}
2525
2526/**
2527 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2528 * @netdev: network interface to be adjusted
2529 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002530 *
2531 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002533#ifdef I40E_FCOE
2534int i40e_vlan_rx_add_vid(struct net_device *netdev,
2535 __always_unused __be16 proto, u16 vid)
2536#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002537static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2538 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002539#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002540{
2541 struct i40e_netdev_priv *np = netdev_priv(netdev);
2542 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002543 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002544
2545 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002546 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002548 /* If the network stack called us with vid = 0 then
2549 * it is asking to receive priority tagged packets with
2550 * vlan id 0. Our HW receives them by default when configured
2551 * to receive untagged packets so there is no need to add an
2552 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002553 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002554 if (vid)
2555 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002556
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002557 if (!ret && (vid < VLAN_N_VID))
2558 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002559
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002560 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002561}
2562
2563/**
2564 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2565 * @netdev: network interface to be adjusted
2566 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002567 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002568 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002569 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002570#ifdef I40E_FCOE
2571int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2572 __always_unused __be16 proto, u16 vid)
2573#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002574static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2575 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002576#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002577{
2578 struct i40e_netdev_priv *np = netdev_priv(netdev);
2579 struct i40e_vsi *vsi = np->vsi;
2580
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002581 /* return code is ignored as there is nothing a user
2582 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002583 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002584 */
2585 i40e_vsi_kill_vlan(vsi, vid);
2586
2587 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002589 return 0;
2590}
2591
2592/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002593 * i40e_macaddr_init - explicitly write the mac address filters
2594 *
2595 * @vsi: pointer to the vsi
2596 * @macaddr: the MAC address
2597 *
2598 * This is needed when the macaddr has been obtained by other
2599 * means than the default, e.g., from Open Firmware or IDPROM.
2600 * Returns 0 on success, negative on failure
2601 **/
2602static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2603{
2604 int ret;
2605 struct i40e_aqc_add_macvlan_element_data element;
2606
2607 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2608 I40E_AQC_WRITE_TYPE_LAA_WOL,
2609 macaddr, NULL);
2610 if (ret) {
2611 dev_info(&vsi->back->pdev->dev,
2612 "Addr change for VSI failed: %d\n", ret);
2613 return -EADDRNOTAVAIL;
2614 }
2615
2616 memset(&element, 0, sizeof(element));
2617 ether_addr_copy(element.mac_addr, macaddr);
2618 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2619 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2620 if (ret) {
2621 dev_info(&vsi->back->pdev->dev,
2622 "add filter failed err %s aq_err %s\n",
2623 i40e_stat_str(&vsi->back->hw, ret),
2624 i40e_aq_str(&vsi->back->hw,
2625 vsi->back->hw.aq.asq_last_status));
2626 }
2627 return ret;
2628}
2629
2630/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002631 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2632 * @vsi: the vsi being brought back up
2633 **/
2634static void i40e_restore_vlan(struct i40e_vsi *vsi)
2635{
2636 u16 vid;
2637
2638 if (!vsi->netdev)
2639 return;
2640
2641 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2642
2643 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2644 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2645 vid);
2646}
2647
2648/**
2649 * i40e_vsi_add_pvid - Add pvid for the VSI
2650 * @vsi: the vsi being adjusted
2651 * @vid: the vlan id to set as a PVID
2652 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002653int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002654{
2655 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002656 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002657
2658 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2659 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002660 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2661 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002662 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002663
2664 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002665 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002666 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2667 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002668 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002669 "add pvid failed, err %s aq_err %s\n",
2670 i40e_stat_str(&vsi->back->hw, ret),
2671 i40e_aq_str(&vsi->back->hw,
2672 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002673 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002674 }
2675
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002676 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002677}
2678
2679/**
2680 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2681 * @vsi: the vsi being adjusted
2682 *
2683 * Just use the vlan_rx_register() service to put it back to normal
2684 **/
2685void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2686{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002687 i40e_vlan_stripping_disable(vsi);
2688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002689 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690}
2691
2692/**
2693 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2694 * @vsi: ptr to the VSI
2695 *
2696 * If this function returns with an error, then it's possible one or
2697 * more of the rings is populated (while the rest are not). It is the
2698 * callers duty to clean those orphaned rings.
2699 *
2700 * Return 0 on success, negative on failure
2701 **/
2702static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2703{
2704 int i, err = 0;
2705
2706 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002707 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002708
2709 return err;
2710}
2711
2712/**
2713 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2714 * @vsi: ptr to the VSI
2715 *
2716 * Free VSI's transmit software resources
2717 **/
2718static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2719{
2720 int i;
2721
Greg Rose8e9dca52013-12-18 13:45:53 +00002722 if (!vsi->tx_rings)
2723 return;
2724
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002725 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002726 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002727 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002728}
2729
2730/**
2731 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2732 * @vsi: ptr to the VSI
2733 *
2734 * If this function returns with an error, then it's possible one or
2735 * more of the rings is populated (while the rest are not). It is the
2736 * callers duty to clean those orphaned rings.
2737 *
2738 * Return 0 on success, negative on failure
2739 **/
2740static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2741{
2742 int i, err = 0;
2743
2744 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002745 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002746#ifdef I40E_FCOE
2747 i40e_fcoe_setup_ddp_resources(vsi);
2748#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002749 return err;
2750}
2751
2752/**
2753 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2754 * @vsi: ptr to the VSI
2755 *
2756 * Free all receive software resources
2757 **/
2758static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2759{
2760 int i;
2761
Greg Rose8e9dca52013-12-18 13:45:53 +00002762 if (!vsi->rx_rings)
2763 return;
2764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002765 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002766 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002767 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002768#ifdef I40E_FCOE
2769 i40e_fcoe_free_ddp_resources(vsi);
2770#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002771}
2772
2773/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002774 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2775 * @ring: The Tx ring to configure
2776 *
2777 * This enables/disables XPS for a given Tx descriptor ring
2778 * based on the TCs enabled for the VSI that ring belongs to.
2779 **/
2780static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2781{
2782 struct i40e_vsi *vsi = ring->vsi;
2783 cpumask_var_t mask;
2784
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002785 if (!ring->q_vector || !ring->netdev)
2786 return;
2787
2788 /* Single TC mode enable XPS */
2789 if (vsi->tc_config.numtc <= 1) {
2790 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002791 netif_set_xps_queue(ring->netdev,
2792 &ring->q_vector->affinity_mask,
2793 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002794 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2795 /* Disable XPS to allow selection based on TC */
2796 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2797 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2798 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002799 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002800
2801 /* schedule our worker thread which will take care of
2802 * applying the new filter changes
2803 */
2804 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002805}
2806
2807/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002808 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2809 * @ring: The Tx ring to configure
2810 *
2811 * Configure the Tx descriptor ring in the HMC context.
2812 **/
2813static int i40e_configure_tx_ring(struct i40e_ring *ring)
2814{
2815 struct i40e_vsi *vsi = ring->vsi;
2816 u16 pf_q = vsi->base_queue + ring->queue_index;
2817 struct i40e_hw *hw = &vsi->back->hw;
2818 struct i40e_hmc_obj_txq tx_ctx;
2819 i40e_status err = 0;
2820 u32 qtx_ctl = 0;
2821
2822 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002823 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002824 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2825 ring->atr_count = 0;
2826 } else {
2827 ring->atr_sample_rate = 0;
2828 }
2829
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002830 /* configure XPS */
2831 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002832
2833 /* clear the context structure first */
2834 memset(&tx_ctx, 0, sizeof(tx_ctx));
2835
2836 tx_ctx.new_context = 1;
2837 tx_ctx.base = (ring->dma / 128);
2838 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002839 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2840 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002841#ifdef I40E_FCOE
2842 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2843#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002844 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002845 /* FDIR VSI tx ring can still use RS bit and writebacks */
2846 if (vsi->type != I40E_VSI_FDIR)
2847 tx_ctx.head_wb_ena = 1;
2848 tx_ctx.head_wb_addr = ring->dma +
2849 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002850
2851 /* As part of VSI creation/update, FW allocates certain
2852 * Tx arbitration queue sets for each TC enabled for
2853 * the VSI. The FW returns the handles to these queue
2854 * sets as part of the response buffer to Add VSI,
2855 * Update VSI, etc. AQ commands. It is expected that
2856 * these queue set handles be associated with the Tx
2857 * queues by the driver as part of the TX queue context
2858 * initialization. This has to be done regardless of
2859 * DCB as by default everything is mapped to TC0.
2860 */
2861 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2862 tx_ctx.rdylist_act = 0;
2863
2864 /* clear the context in the HMC */
2865 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2866 if (err) {
2867 dev_info(&vsi->back->pdev->dev,
2868 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2869 ring->queue_index, pf_q, err);
2870 return -ENOMEM;
2871 }
2872
2873 /* set the context in the HMC */
2874 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2875 if (err) {
2876 dev_info(&vsi->back->pdev->dev,
2877 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2878 ring->queue_index, pf_q, err);
2879 return -ENOMEM;
2880 }
2881
2882 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002883 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002884 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002885 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2886 I40E_QTX_CTL_VFVM_INDX_MASK;
2887 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002888 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002889 }
2890
Shannon Nelson13fd977492013-09-28 07:14:19 +00002891 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2892 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002893 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2894 i40e_flush(hw);
2895
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002896 /* cache tail off for easier writes later */
2897 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2898
2899 return 0;
2900}
2901
2902/**
2903 * i40e_configure_rx_ring - Configure a receive ring context
2904 * @ring: The Rx ring to configure
2905 *
2906 * Configure the Rx descriptor ring in the HMC context.
2907 **/
2908static int i40e_configure_rx_ring(struct i40e_ring *ring)
2909{
2910 struct i40e_vsi *vsi = ring->vsi;
2911 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2912 u16 pf_q = vsi->base_queue + ring->queue_index;
2913 struct i40e_hw *hw = &vsi->back->hw;
2914 struct i40e_hmc_obj_rxq rx_ctx;
2915 i40e_status err = 0;
2916
2917 ring->state = 0;
2918
2919 /* clear the context structure first */
2920 memset(&rx_ctx, 0, sizeof(rx_ctx));
2921
2922 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002923
2924 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002925
2926 rx_ctx.base = (ring->dma / 128);
2927 rx_ctx.qlen = ring->count;
2928
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002929 /* use 32 byte descriptors */
2930 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002931
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002932 /* descriptor type is always zero
2933 * rx_ctx.dtype = 0;
2934 */
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07002935 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002936
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07002937 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002938 if (hw->revision_id == 0)
2939 rx_ctx.lrxqthresh = 0;
2940 else
2941 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002942 rx_ctx.crcstrip = 1;
2943 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002944 /* this controls whether VLAN is stripped from inner headers */
2945 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002946#ifdef I40E_FCOE
2947 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2948#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002949 /* set the prefena field to 1 because the manual says to */
2950 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002951
2952 /* clear the context in the HMC */
2953 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2954 if (err) {
2955 dev_info(&vsi->back->pdev->dev,
2956 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2957 ring->queue_index, pf_q, err);
2958 return -ENOMEM;
2959 }
2960
2961 /* set the context in the HMC */
2962 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2963 if (err) {
2964 dev_info(&vsi->back->pdev->dev,
2965 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2966 ring->queue_index, pf_q, err);
2967 return -ENOMEM;
2968 }
2969
2970 /* cache tail for quicker writes, and clear the reg before use */
2971 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2972 writel(0, ring->tail);
2973
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002974 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002975
2976 return 0;
2977}
2978
2979/**
2980 * i40e_vsi_configure_tx - Configure the VSI for Tx
2981 * @vsi: VSI structure describing this set of rings and resources
2982 *
2983 * Configure the Tx VSI for operation.
2984 **/
2985static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2986{
2987 int err = 0;
2988 u16 i;
2989
Alexander Duyck9f65e152013-09-28 06:00:58 +00002990 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2991 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002992
2993 return err;
2994}
2995
2996/**
2997 * i40e_vsi_configure_rx - Configure the VSI for Rx
2998 * @vsi: the VSI being configured
2999 *
3000 * Configure the Rx VSI for operation.
3001 **/
3002static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3003{
3004 int err = 0;
3005 u16 i;
3006
3007 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3008 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3009 + ETH_FCS_LEN + VLAN_HLEN;
3010 else
3011 vsi->max_frame = I40E_RXBUFFER_2048;
3012
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003013 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003014
Vasu Dev38e00432014-08-01 13:27:03 -07003015#ifdef I40E_FCOE
3016 /* setup rx buffer for FCoE */
3017 if ((vsi->type == I40E_VSI_FCOE) &&
3018 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003019 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3020 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003021 }
3022
3023#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003024 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003025 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003026 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027
3028 /* set up individual rings */
3029 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003030 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003031
3032 return err;
3033}
3034
3035/**
3036 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3037 * @vsi: ptr to the VSI
3038 **/
3039static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3040{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003041 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003042 u16 qoffset, qcount;
3043 int i, n;
3044
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003045 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3046 /* Reset the TC information */
3047 for (i = 0; i < vsi->num_queue_pairs; i++) {
3048 rx_ring = vsi->rx_rings[i];
3049 tx_ring = vsi->tx_rings[i];
3050 rx_ring->dcb_tc = 0;
3051 tx_ring->dcb_tc = 0;
3052 }
3053 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003054
3055 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003056 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003057 continue;
3058
3059 qoffset = vsi->tc_config.tc_info[n].qoffset;
3060 qcount = vsi->tc_config.tc_info[n].qcount;
3061 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003062 rx_ring = vsi->rx_rings[i];
3063 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003064 rx_ring->dcb_tc = n;
3065 tx_ring->dcb_tc = n;
3066 }
3067 }
3068}
3069
3070/**
3071 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3072 * @vsi: ptr to the VSI
3073 **/
3074static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3075{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003076 struct i40e_pf *pf = vsi->back;
3077 int err;
3078
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003079 if (vsi->netdev)
3080 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003081
3082 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3083 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3084 if (err) {
3085 dev_warn(&pf->pdev->dev,
3086 "could not set up macaddr; err %d\n", err);
3087 }
3088 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089}
3090
3091/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003092 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3093 * @vsi: Pointer to the targeted VSI
3094 *
3095 * This function replays the hlist on the hw where all the SB Flow Director
3096 * filters were saved.
3097 **/
3098static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3099{
3100 struct i40e_fdir_filter *filter;
3101 struct i40e_pf *pf = vsi->back;
3102 struct hlist_node *node;
3103
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003104 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3105 return;
3106
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003107 hlist_for_each_entry_safe(filter, node,
3108 &pf->fdir_filter_list, fdir_node) {
3109 i40e_add_del_fdir(vsi, filter, true);
3110 }
3111}
3112
3113/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003114 * i40e_vsi_configure - Set up the VSI for action
3115 * @vsi: the VSI being configured
3116 **/
3117static int i40e_vsi_configure(struct i40e_vsi *vsi)
3118{
3119 int err;
3120
3121 i40e_set_vsi_rx_mode(vsi);
3122 i40e_restore_vlan(vsi);
3123 i40e_vsi_config_dcb_rings(vsi);
3124 err = i40e_vsi_configure_tx(vsi);
3125 if (!err)
3126 err = i40e_vsi_configure_rx(vsi);
3127
3128 return err;
3129}
3130
3131/**
3132 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3133 * @vsi: the VSI being configured
3134 **/
3135static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3136{
3137 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003138 struct i40e_hw *hw = &pf->hw;
3139 u16 vector;
3140 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 u32 qp;
3142
3143 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3144 * and PFINT_LNKLSTn registers, e.g.:
3145 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3146 */
3147 qp = vsi->base_queue;
3148 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003149 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003150 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3151
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003152 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003153 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003154 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3155 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3156 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003157 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003158 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3159 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3160 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003161 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3162 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003163
3164 /* Linked list for the queuepairs assigned to this vector */
3165 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3166 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003167 u32 val;
3168
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003169 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3170 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3171 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3172 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3173 (I40E_QUEUE_TYPE_TX
3174 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3175
3176 wr32(hw, I40E_QINT_RQCTL(qp), val);
3177
3178 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3179 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3180 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3181 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3182 (I40E_QUEUE_TYPE_RX
3183 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3184
3185 /* Terminate the linked list */
3186 if (q == (q_vector->num_ringpairs - 1))
3187 val |= (I40E_QUEUE_END_OF_LIST
3188 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3189
3190 wr32(hw, I40E_QINT_TQCTL(qp), val);
3191 qp++;
3192 }
3193 }
3194
3195 i40e_flush(hw);
3196}
3197
3198/**
3199 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3200 * @hw: ptr to the hardware info
3201 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003202static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003203{
Jacob Kellerab437b52014-12-14 01:55:08 +00003204 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205 u32 val;
3206
3207 /* clear things first */
3208 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3209 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3210
3211 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3212 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3213 I40E_PFINT_ICR0_ENA_GRST_MASK |
3214 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3215 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003216 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3217 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3218 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3219
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003220 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3221 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3222
Jacob Kellerab437b52014-12-14 01:55:08 +00003223 if (pf->flags & I40E_FLAG_PTP)
3224 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3225
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3227
3228 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003229 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3230 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231
3232 /* OTHER_ITR_IDX = 0 */
3233 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3234}
3235
3236/**
3237 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3238 * @vsi: the VSI being configured
3239 **/
3240static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3241{
Alexander Duyck493fb302013-09-28 07:01:44 +00003242 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243 struct i40e_pf *pf = vsi->back;
3244 struct i40e_hw *hw = &pf->hw;
3245 u32 val;
3246
3247 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003248 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003249 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003250 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3251 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003252 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003253 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3254 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3255
Jacob Kellerab437b52014-12-14 01:55:08 +00003256 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003257
3258 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3259 wr32(hw, I40E_PFINT_LNKLST0, 0);
3260
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003261 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003262 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3263 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3264 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3265
3266 wr32(hw, I40E_QINT_RQCTL(0), val);
3267
3268 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3269 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3270 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3271
3272 wr32(hw, I40E_QINT_TQCTL(0), val);
3273 i40e_flush(hw);
3274}
3275
3276/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003277 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3278 * @pf: board private structure
3279 **/
3280void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3281{
3282 struct i40e_hw *hw = &pf->hw;
3283
3284 wr32(hw, I40E_PFINT_DYN_CTL0,
3285 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3286 i40e_flush(hw);
3287}
3288
3289/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003290 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3291 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003292 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003293 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003294void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003295{
3296 struct i40e_hw *hw = &pf->hw;
3297 u32 val;
3298
3299 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003300 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3302
3303 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3304 i40e_flush(hw);
3305}
3306
3307/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003308 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3309 * @irq: interrupt number
3310 * @data: pointer to a q_vector
3311 **/
3312static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3313{
3314 struct i40e_q_vector *q_vector = data;
3315
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003316 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003317 return IRQ_HANDLED;
3318
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003319 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320
3321 return IRQ_HANDLED;
3322}
3323
3324/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3326 * @vsi: the VSI being configured
3327 * @basename: name for the vector
3328 *
3329 * Allocates MSI-X vectors and requests interrupts from the kernel.
3330 **/
3331static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3332{
3333 int q_vectors = vsi->num_q_vectors;
3334 struct i40e_pf *pf = vsi->back;
3335 int base = vsi->base_vector;
3336 int rx_int_idx = 0;
3337 int tx_int_idx = 0;
3338 int vector, err;
3339
3340 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003341 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003342
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003343 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003344 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3345 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3346 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003347 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003348 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3349 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003350 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003351 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3352 "%s-%s-%d", basename, "tx", tx_int_idx++);
3353 } else {
3354 /* skip this unused q_vector */
3355 continue;
3356 }
3357 err = request_irq(pf->msix_entries[base + vector].vector,
3358 vsi->irq_handler,
3359 0,
3360 q_vector->name,
3361 q_vector);
3362 if (err) {
3363 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003364 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365 goto free_queue_irqs;
3366 }
3367 /* assign the mask for this irq */
3368 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3369 &q_vector->affinity_mask);
3370 }
3371
Shannon Nelson63741842014-04-23 04:50:16 +00003372 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003373 return 0;
3374
3375free_queue_irqs:
3376 while (vector) {
3377 vector--;
3378 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
3379 NULL);
3380 free_irq(pf->msix_entries[base + vector].vector,
3381 &(vsi->q_vectors[vector]));
3382 }
3383 return err;
3384}
3385
3386/**
3387 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3388 * @vsi: the VSI being un-configured
3389 **/
3390static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3391{
3392 struct i40e_pf *pf = vsi->back;
3393 struct i40e_hw *hw = &pf->hw;
3394 int base = vsi->base_vector;
3395 int i;
3396
3397 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003398 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3399 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003400 }
3401
3402 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3403 for (i = vsi->base_vector;
3404 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3405 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3406
3407 i40e_flush(hw);
3408 for (i = 0; i < vsi->num_q_vectors; i++)
3409 synchronize_irq(pf->msix_entries[i + base].vector);
3410 } else {
3411 /* Legacy and MSI mode - this stops all interrupt handling */
3412 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3413 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3414 i40e_flush(hw);
3415 synchronize_irq(pf->pdev->irq);
3416 }
3417}
3418
3419/**
3420 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3421 * @vsi: the VSI being configured
3422 **/
3423static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3424{
3425 struct i40e_pf *pf = vsi->back;
3426 int i;
3427
3428 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003429 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003430 i40e_irq_dynamic_enable(vsi, i);
3431 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003432 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003433 }
3434
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003435 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003436 return 0;
3437}
3438
3439/**
3440 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3441 * @pf: board private structure
3442 **/
3443static void i40e_stop_misc_vector(struct i40e_pf *pf)
3444{
3445 /* Disable ICR 0 */
3446 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3447 i40e_flush(&pf->hw);
3448}
3449
3450/**
3451 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3452 * @irq: interrupt number
3453 * @data: pointer to a q_vector
3454 *
3455 * This is the handler used for all MSI/Legacy interrupts, and deals
3456 * with both queue and non-queue interrupts. This is also used in
3457 * MSIX mode to handle the non-queue interrupts.
3458 **/
3459static irqreturn_t i40e_intr(int irq, void *data)
3460{
3461 struct i40e_pf *pf = (struct i40e_pf *)data;
3462 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003463 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003464 u32 icr0, icr0_remaining;
3465 u32 val, ena_mask;
3466
3467 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003468 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469
Shannon Nelson116a57d2013-09-28 07:13:59 +00003470 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3471 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003472 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003473
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003474 /* if interrupt but no bits showing, must be SWINT */
3475 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3476 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3477 pf->sw_int_count++;
3478
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003479 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3480 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3481 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3482 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3483 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3484 }
3485
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3487 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003488 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3489 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003490
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003491 /* We do not have a way to disarm Queue causes while leaving
3492 * interrupt enabled for all other causes, ideally
3493 * interrupt should be disabled while we are in NAPI but
3494 * this is not a performance path and napi_schedule()
3495 * can deal with rescheduling.
3496 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003497 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003498 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003499 }
3500
3501 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3502 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3503 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003504 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003505 }
3506
3507 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3508 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3509 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3510 }
3511
3512 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3513 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3514 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3515 }
3516
3517 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3518 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3519 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3520 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3521 val = rd32(hw, I40E_GLGEN_RSTAT);
3522 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3523 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003524 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003525 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003526 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003527 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003528 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003529 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003530 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003531 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 }
3533
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003534 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3535 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3536 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003537 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3538 rd32(hw, I40E_PFHMC_ERRORINFO),
3539 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003540 }
3541
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003542 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3543 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3544
3545 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003546 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003547 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003548 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003549 }
3550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003551 /* If a critical error is pending we have no choice but to reset the
3552 * device.
3553 * Report and mask out any remaining unexpected interrupts.
3554 */
3555 icr0_remaining = icr0 & ena_mask;
3556 if (icr0_remaining) {
3557 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3558 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003559 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003560 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003561 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003562 dev_info(&pf->pdev->dev, "device will be reset\n");
3563 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3564 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003565 }
3566 ena_mask &= ~icr0_remaining;
3567 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003568 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003569
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003570enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003571 /* re-enable interrupt causes */
3572 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573 if (!test_bit(__I40E_DOWN, &pf->state)) {
3574 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003575 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003576 }
3577
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003578 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003579}
3580
3581/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003582 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3583 * @tx_ring: tx ring to clean
3584 * @budget: how many cleans we're allowed
3585 *
3586 * Returns true if there's any budget left (e.g. the clean is finished)
3587 **/
3588static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3589{
3590 struct i40e_vsi *vsi = tx_ring->vsi;
3591 u16 i = tx_ring->next_to_clean;
3592 struct i40e_tx_buffer *tx_buf;
3593 struct i40e_tx_desc *tx_desc;
3594
3595 tx_buf = &tx_ring->tx_bi[i];
3596 tx_desc = I40E_TX_DESC(tx_ring, i);
3597 i -= tx_ring->count;
3598
3599 do {
3600 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3601
3602 /* if next_to_watch is not set then there is no work pending */
3603 if (!eop_desc)
3604 break;
3605
3606 /* prevent any other reads prior to eop_desc */
3607 read_barrier_depends();
3608
3609 /* if the descriptor isn't done, no work yet to do */
3610 if (!(eop_desc->cmd_type_offset_bsz &
3611 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3612 break;
3613
3614 /* clear next_to_watch to prevent false hangs */
3615 tx_buf->next_to_watch = NULL;
3616
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003617 tx_desc->buffer_addr = 0;
3618 tx_desc->cmd_type_offset_bsz = 0;
3619 /* move past filter desc */
3620 tx_buf++;
3621 tx_desc++;
3622 i++;
3623 if (unlikely(!i)) {
3624 i -= tx_ring->count;
3625 tx_buf = tx_ring->tx_bi;
3626 tx_desc = I40E_TX_DESC(tx_ring, 0);
3627 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003628 /* unmap skb header data */
3629 dma_unmap_single(tx_ring->dev,
3630 dma_unmap_addr(tx_buf, dma),
3631 dma_unmap_len(tx_buf, len),
3632 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003633 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3634 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003635
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003636 tx_buf->raw_buf = NULL;
3637 tx_buf->tx_flags = 0;
3638 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003639 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003640 tx_desc->buffer_addr = 0;
3641 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003642
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003643 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003644 tx_buf++;
3645 tx_desc++;
3646 i++;
3647 if (unlikely(!i)) {
3648 i -= tx_ring->count;
3649 tx_buf = tx_ring->tx_bi;
3650 tx_desc = I40E_TX_DESC(tx_ring, 0);
3651 }
3652
3653 /* update budget accounting */
3654 budget--;
3655 } while (likely(budget));
3656
3657 i += tx_ring->count;
3658 tx_ring->next_to_clean = i;
3659
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003660 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003661 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003662
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003663 return budget > 0;
3664}
3665
3666/**
3667 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3668 * @irq: interrupt number
3669 * @data: pointer to a q_vector
3670 **/
3671static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3672{
3673 struct i40e_q_vector *q_vector = data;
3674 struct i40e_vsi *vsi;
3675
3676 if (!q_vector->tx.ring)
3677 return IRQ_HANDLED;
3678
3679 vsi = q_vector->tx.ring->vsi;
3680 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3681
3682 return IRQ_HANDLED;
3683}
3684
3685/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003686 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003687 * @vsi: the VSI being configured
3688 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003689 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003690 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003691static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692{
Alexander Duyck493fb302013-09-28 07:01:44 +00003693 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003694 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3695 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696
3697 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003698 tx_ring->next = q_vector->tx.ring;
3699 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003700 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003701
3702 rx_ring->q_vector = q_vector;
3703 rx_ring->next = q_vector->rx.ring;
3704 q_vector->rx.ring = rx_ring;
3705 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003706}
3707
3708/**
3709 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3710 * @vsi: the VSI being configured
3711 *
3712 * This function maps descriptor rings to the queue-specific vectors
3713 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3714 * one vector per queue pair, but on a constrained vector budget, we
3715 * group the queue pairs as "efficiently" as possible.
3716 **/
3717static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3718{
3719 int qp_remaining = vsi->num_queue_pairs;
3720 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003721 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003722 int v_start = 0;
3723 int qp_idx = 0;
3724
3725 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3726 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003727 * It is also important to go through all the vectors available to be
3728 * sure that if we don't use all the vectors, that the remaining vectors
3729 * are cleared. This is especially important when decreasing the
3730 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003731 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003732 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003733 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3734
3735 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3736
3737 q_vector->num_ringpairs = num_ringpairs;
3738
3739 q_vector->rx.count = 0;
3740 q_vector->tx.count = 0;
3741 q_vector->rx.ring = NULL;
3742 q_vector->tx.ring = NULL;
3743
3744 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003745 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003746 qp_idx++;
3747 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003748 }
3749 }
3750}
3751
3752/**
3753 * i40e_vsi_request_irq - Request IRQ from the OS
3754 * @vsi: the VSI being configured
3755 * @basename: name for the vector
3756 **/
3757static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3758{
3759 struct i40e_pf *pf = vsi->back;
3760 int err;
3761
3762 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3763 err = i40e_vsi_request_irq_msix(vsi, basename);
3764 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3765 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003766 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003767 else
3768 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003769 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770
3771 if (err)
3772 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3773
3774 return err;
3775}
3776
3777#ifdef CONFIG_NET_POLL_CONTROLLER
3778/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003779 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003780 * @netdev: network interface device structure
3781 *
3782 * This is used by netconsole to send skbs without having to re-enable
3783 * interrupts. It's not called while the normal interrupt routine is executing.
3784 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003785#ifdef I40E_FCOE
3786void i40e_netpoll(struct net_device *netdev)
3787#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003788static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003789#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003790{
3791 struct i40e_netdev_priv *np = netdev_priv(netdev);
3792 struct i40e_vsi *vsi = np->vsi;
3793 struct i40e_pf *pf = vsi->back;
3794 int i;
3795
3796 /* if interface is down do nothing */
3797 if (test_bit(__I40E_DOWN, &vsi->state))
3798 return;
3799
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3801 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003802 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003803 } else {
3804 i40e_intr(pf->pdev->irq, netdev);
3805 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003806}
3807#endif
3808
3809/**
Neerav Parikh23527302014-06-03 23:50:15 +00003810 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3811 * @pf: the PF being configured
3812 * @pf_q: the PF queue
3813 * @enable: enable or disable state of the queue
3814 *
3815 * This routine will wait for the given Tx queue of the PF to reach the
3816 * enabled or disabled state.
3817 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3818 * multiple retries; else will return 0 in case of success.
3819 **/
3820static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3821{
3822 int i;
3823 u32 tx_reg;
3824
3825 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3826 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3827 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3828 break;
3829
Neerav Parikhf98a2002014-09-13 07:40:44 +00003830 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003831 }
3832 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3833 return -ETIMEDOUT;
3834
3835 return 0;
3836}
3837
3838/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003839 * i40e_vsi_control_tx - Start or stop a VSI's rings
3840 * @vsi: the VSI being configured
3841 * @enable: start or stop the rings
3842 **/
3843static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3844{
3845 struct i40e_pf *pf = vsi->back;
3846 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003847 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003848 u32 tx_reg;
3849
3850 pf_q = vsi->base_queue;
3851 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003852
3853 /* warn the TX unit of coming changes */
3854 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3855 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003856 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003857
Mitch Williams6c5ef622014-02-20 19:29:16 -08003858 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003859 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003860 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3861 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3862 break;
3863 usleep_range(1000, 2000);
3864 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003865 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003866 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003867 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003868
3869 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003870 if (enable) {
3871 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003872 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003873 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003875 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003876
3877 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003878 /* No waiting for the Tx queue to disable */
3879 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3880 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003881
3882 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003883 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3884 if (ret) {
3885 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003886 "VSI seid %d Tx ring %d %sable timeout\n",
3887 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003888 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003889 }
3890 }
3891
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003892 if (hw->revision_id == 0)
3893 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003894 return ret;
3895}
3896
3897/**
3898 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3899 * @pf: the PF being configured
3900 * @pf_q: the PF queue
3901 * @enable: enable or disable state of the queue
3902 *
3903 * This routine will wait for the given Rx queue of the PF to reach the
3904 * enabled or disabled state.
3905 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3906 * multiple retries; else will return 0 in case of success.
3907 **/
3908static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3909{
3910 int i;
3911 u32 rx_reg;
3912
3913 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3914 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3915 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3916 break;
3917
Neerav Parikhf98a2002014-09-13 07:40:44 +00003918 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003919 }
3920 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3921 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003922
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003923 return 0;
3924}
3925
3926/**
3927 * i40e_vsi_control_rx - Start or stop a VSI's rings
3928 * @vsi: the VSI being configured
3929 * @enable: start or stop the rings
3930 **/
3931static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3932{
3933 struct i40e_pf *pf = vsi->back;
3934 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003935 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003936 u32 rx_reg;
3937
3938 pf_q = vsi->base_queue;
3939 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003940 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003941 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003942 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3943 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3944 break;
3945 usleep_range(1000, 2000);
3946 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003948 /* Skip if the queue is already in the requested state */
3949 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3950 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951
3952 /* turn on/off the queue */
3953 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003954 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003955 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003956 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003958 /* No waiting for the Tx queue to disable */
3959 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3960 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003961
3962 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003963 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3964 if (ret) {
3965 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003966 "VSI seid %d Rx ring %d %sable timeout\n",
3967 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003968 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003969 }
3970 }
3971
Neerav Parikh23527302014-06-03 23:50:15 +00003972 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003973}
3974
3975/**
3976 * i40e_vsi_control_rings - Start or stop a VSI's rings
3977 * @vsi: the VSI being configured
3978 * @enable: start or stop the rings
3979 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003980int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003981{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003982 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003983
3984 /* do rx first for enable and last for disable */
3985 if (request) {
3986 ret = i40e_vsi_control_rx(vsi, request);
3987 if (ret)
3988 return ret;
3989 ret = i40e_vsi_control_tx(vsi, request);
3990 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003991 /* Ignore return value, we need to shutdown whatever we can */
3992 i40e_vsi_control_tx(vsi, request);
3993 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003994 }
3995
3996 return ret;
3997}
3998
3999/**
4000 * i40e_vsi_free_irq - Free the irq association with the OS
4001 * @vsi: the VSI being configured
4002 **/
4003static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4004{
4005 struct i40e_pf *pf = vsi->back;
4006 struct i40e_hw *hw = &pf->hw;
4007 int base = vsi->base_vector;
4008 u32 val, qp;
4009 int i;
4010
4011 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4012 if (!vsi->q_vectors)
4013 return;
4014
Shannon Nelson63741842014-04-23 04:50:16 +00004015 if (!vsi->irqs_ready)
4016 return;
4017
4018 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019 for (i = 0; i < vsi->num_q_vectors; i++) {
4020 u16 vector = i + base;
4021
4022 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004023 if (!vsi->q_vectors[i] ||
4024 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004025 continue;
4026
4027 /* clear the affinity_mask in the IRQ descriptor */
4028 irq_set_affinity_hint(pf->msix_entries[vector].vector,
4029 NULL);
Greg Roseb33d3b72016-05-16 10:26:37 -07004030 synchronize_irq(pf->msix_entries[vector].vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004031 free_irq(pf->msix_entries[vector].vector,
Alexander Duyck493fb302013-09-28 07:01:44 +00004032 vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004033
4034 /* Tear down the interrupt queue link list
4035 *
4036 * We know that they come in pairs and always
4037 * the Rx first, then the Tx. To clear the
4038 * link list, stick the EOL value into the
4039 * next_q field of the registers.
4040 */
4041 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4042 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4043 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4044 val |= I40E_QUEUE_END_OF_LIST
4045 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4046 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4047
4048 while (qp != I40E_QUEUE_END_OF_LIST) {
4049 u32 next;
4050
4051 val = rd32(hw, I40E_QINT_RQCTL(qp));
4052
4053 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4054 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4055 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4056 I40E_QINT_RQCTL_INTEVENT_MASK);
4057
4058 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4059 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4060
4061 wr32(hw, I40E_QINT_RQCTL(qp), val);
4062
4063 val = rd32(hw, I40E_QINT_TQCTL(qp));
4064
4065 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4066 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4067
4068 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4069 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4070 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4071 I40E_QINT_TQCTL_INTEVENT_MASK);
4072
4073 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4074 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4075
4076 wr32(hw, I40E_QINT_TQCTL(qp), val);
4077 qp = next;
4078 }
4079 }
4080 } else {
4081 free_irq(pf->pdev->irq, pf);
4082
4083 val = rd32(hw, I40E_PFINT_LNKLST0);
4084 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4085 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4086 val |= I40E_QUEUE_END_OF_LIST
4087 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4088 wr32(hw, I40E_PFINT_LNKLST0, val);
4089
4090 val = rd32(hw, I40E_QINT_RQCTL(qp));
4091 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4092 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4093 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4094 I40E_QINT_RQCTL_INTEVENT_MASK);
4095
4096 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4097 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4098
4099 wr32(hw, I40E_QINT_RQCTL(qp), val);
4100
4101 val = rd32(hw, I40E_QINT_TQCTL(qp));
4102
4103 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4104 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4105 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4106 I40E_QINT_TQCTL_INTEVENT_MASK);
4107
4108 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4109 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4110
4111 wr32(hw, I40E_QINT_TQCTL(qp), val);
4112 }
4113}
4114
4115/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004116 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4117 * @vsi: the VSI being configured
4118 * @v_idx: Index of vector to be freed
4119 *
4120 * This function frees the memory allocated to the q_vector. In addition if
4121 * NAPI is enabled it will delete any references to the NAPI struct prior
4122 * to freeing the q_vector.
4123 **/
4124static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4125{
4126 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004127 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004128
4129 if (!q_vector)
4130 return;
4131
4132 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004133 i40e_for_each_ring(ring, q_vector->tx)
4134 ring->q_vector = NULL;
4135
4136 i40e_for_each_ring(ring, q_vector->rx)
4137 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004138
4139 /* only VSI w/ an associated netdev is set up w/ NAPI */
4140 if (vsi->netdev)
4141 netif_napi_del(&q_vector->napi);
4142
4143 vsi->q_vectors[v_idx] = NULL;
4144
4145 kfree_rcu(q_vector, rcu);
4146}
4147
4148/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004149 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4150 * @vsi: the VSI being un-configured
4151 *
4152 * This frees the memory allocated to the q_vectors and
4153 * deletes references to the NAPI struct.
4154 **/
4155static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4156{
4157 int v_idx;
4158
Alexander Duyck493fb302013-09-28 07:01:44 +00004159 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4160 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004161}
4162
4163/**
4164 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4165 * @pf: board private structure
4166 **/
4167static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4168{
4169 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4170 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4171 pci_disable_msix(pf->pdev);
4172 kfree(pf->msix_entries);
4173 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004174 kfree(pf->irq_pile);
4175 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004176 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4177 pci_disable_msi(pf->pdev);
4178 }
4179 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4180}
4181
4182/**
4183 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4184 * @pf: board private structure
4185 *
4186 * We go through and clear interrupt specific resources and reset the structure
4187 * to pre-load conditions
4188 **/
4189static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4190{
4191 int i;
4192
Shannon Nelsone1477582015-02-21 06:44:33 +00004193 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004194 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004195 synchronize_irq(pf->msix_entries[0].vector);
4196 free_irq(pf->msix_entries[0].vector, pf);
4197 }
4198
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004199 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4200 I40E_IWARP_IRQ_PILE_ID);
4201
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004202 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004203 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004204 if (pf->vsi[i])
4205 i40e_vsi_free_q_vectors(pf->vsi[i]);
4206 i40e_reset_interrupt_capability(pf);
4207}
4208
4209/**
4210 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4211 * @vsi: the VSI being configured
4212 **/
4213static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4214{
4215 int q_idx;
4216
4217 if (!vsi->netdev)
4218 return;
4219
4220 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004221 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004222}
4223
4224/**
4225 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4226 * @vsi: the VSI being configured
4227 **/
4228static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4229{
4230 int q_idx;
4231
4232 if (!vsi->netdev)
4233 return;
4234
4235 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004236 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004237}
4238
4239/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004240 * i40e_vsi_close - Shut down a VSI
4241 * @vsi: the vsi to be quelled
4242 **/
4243static void i40e_vsi_close(struct i40e_vsi *vsi)
4244{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004245 bool reset = false;
4246
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004247 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4248 i40e_down(vsi);
4249 i40e_vsi_free_irq(vsi);
4250 i40e_vsi_free_tx_resources(vsi);
4251 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004252 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004253 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4254 reset = true;
4255 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004256}
4257
4258/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004259 * i40e_quiesce_vsi - Pause a given VSI
4260 * @vsi: the VSI being paused
4261 **/
4262static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4263{
4264 if (test_bit(__I40E_DOWN, &vsi->state))
4265 return;
4266
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004267 /* No need to disable FCoE VSI when Tx suspended */
4268 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4269 vsi->type == I40E_VSI_FCOE) {
4270 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004271 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004272 return;
4273 }
4274
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004275 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004276 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004277 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004278 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004279 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004280}
4281
4282/**
4283 * i40e_unquiesce_vsi - Resume a given VSI
4284 * @vsi: the VSI being resumed
4285 **/
4286static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4287{
4288 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4289 return;
4290
4291 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4292 if (vsi->netdev && netif_running(vsi->netdev))
4293 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4294 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004295 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004296}
4297
4298/**
4299 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4300 * @pf: the PF
4301 **/
4302static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4303{
4304 int v;
4305
Mitch Williams505682c2014-05-20 08:01:37 +00004306 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004307 if (pf->vsi[v])
4308 i40e_quiesce_vsi(pf->vsi[v]);
4309 }
4310}
4311
4312/**
4313 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4314 * @pf: the PF
4315 **/
4316static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4317{
4318 int v;
4319
Mitch Williams505682c2014-05-20 08:01:37 +00004320 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004321 if (pf->vsi[v])
4322 i40e_unquiesce_vsi(pf->vsi[v]);
4323 }
4324}
4325
Neerav Parikh69129dc2014-11-12 00:18:46 +00004326#ifdef CONFIG_I40E_DCB
4327/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004328 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004329 * @vsi: the VSI being configured
4330 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004331 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004332 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004333static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004334{
4335 struct i40e_pf *pf = vsi->back;
4336 int i, pf_q, ret;
4337
4338 pf_q = vsi->base_queue;
4339 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4340 /* Check and wait for the disable status of the queue */
4341 ret = i40e_pf_txq_wait(pf, pf_q, false);
4342 if (ret) {
4343 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004344 "VSI seid %d Tx ring %d disable timeout\n",
4345 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004346 return ret;
4347 }
4348 }
4349
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004350 pf_q = vsi->base_queue;
4351 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4352 /* Check and wait for the disable status of the queue */
4353 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4354 if (ret) {
4355 dev_info(&pf->pdev->dev,
4356 "VSI seid %d Rx ring %d disable timeout\n",
4357 vsi->seid, pf_q);
4358 return ret;
4359 }
4360 }
4361
Neerav Parikh69129dc2014-11-12 00:18:46 +00004362 return 0;
4363}
4364
4365/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004366 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004367 * @pf: the PF
4368 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004369 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004370 * VSIs that are managed by this PF.
4371 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004372static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004373{
4374 int v, ret = 0;
4375
4376 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004377 /* No need to wait for FCoE VSI queues */
4378 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004379 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004380 if (ret)
4381 break;
4382 }
4383 }
4384
4385 return ret;
4386}
4387
4388#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004389
4390/**
4391 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4392 * @q_idx: TX queue number
4393 * @vsi: Pointer to VSI struct
4394 *
4395 * This function checks specified queue for given VSI. Detects hung condition.
4396 * Sets hung bit since it is two step process. Before next run of service task
4397 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4398 * hung condition remain unchanged and during subsequent run, this function
4399 * issues SW interrupt to recover from hung condition.
4400 **/
4401static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4402{
4403 struct i40e_ring *tx_ring = NULL;
4404 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004405 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004406 int i;
4407
4408 pf = vsi->back;
4409
4410 /* now that we have an index, find the tx_ring struct */
4411 for (i = 0; i < vsi->num_queue_pairs; i++) {
4412 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4413 if (q_idx == vsi->tx_rings[i]->queue_index) {
4414 tx_ring = vsi->tx_rings[i];
4415 break;
4416 }
4417 }
4418 }
4419
4420 if (!tx_ring)
4421 return;
4422
4423 /* Read interrupt register */
4424 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4425 val = rd32(&pf->hw,
4426 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4427 tx_ring->vsi->base_vector - 1));
4428 else
4429 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4430
4431 head = i40e_get_head(tx_ring);
4432
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004433 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004434
Kiran Patil9c6c1252015-11-06 15:26:02 -08004435 /* HW is done executing descriptors, updated HEAD write back,
4436 * but SW hasn't processed those descriptors. If interrupt is
4437 * not generated from this point ON, it could result into
4438 * dev_watchdog detecting timeout on those netdev_queue,
4439 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004440 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004441 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004442 /* NAPI Poll didn't run and clear since it was set */
4443 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4444 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004445 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",
4446 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004447 tx_ring->next_to_clean, head,
4448 tx_ring->next_to_use,
4449 readl(tx_ring->tail));
4450 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4451 vsi->seid, q_idx, val);
4452 i40e_force_wb(vsi, tx_ring->q_vector);
4453 } else {
4454 /* First Chance - detected possible hung */
4455 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4456 &tx_ring->q_vector->hung_detected);
4457 }
4458 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004459
4460 /* This is the case where we have interrupts missing,
4461 * so the tx_pending in HW will most likely be 0, but we
4462 * will have tx_pending in SW since the WB happened but the
4463 * interrupt got lost.
4464 */
4465 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4466 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4467 if (napi_reschedule(&tx_ring->q_vector->napi))
4468 tx_ring->tx_stats.tx_lost_interrupt++;
4469 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004470}
4471
4472/**
4473 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4474 * @pf: pointer to PF struct
4475 *
4476 * LAN VSI has netdev and netdev has TX queues. This function is to check
4477 * each of those TX queues if they are hung, trigger recovery by issuing
4478 * SW interrupt.
4479 **/
4480static void i40e_detect_recover_hung(struct i40e_pf *pf)
4481{
4482 struct net_device *netdev;
4483 struct i40e_vsi *vsi;
4484 int i;
4485
4486 /* Only for LAN VSI */
4487 vsi = pf->vsi[pf->lan_vsi];
4488
4489 if (!vsi)
4490 return;
4491
4492 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4493 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4494 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4495 return;
4496
4497 /* Make sure type is MAIN VSI */
4498 if (vsi->type != I40E_VSI_MAIN)
4499 return;
4500
4501 netdev = vsi->netdev;
4502 if (!netdev)
4503 return;
4504
4505 /* Bail out if netif_carrier is not OK */
4506 if (!netif_carrier_ok(netdev))
4507 return;
4508
4509 /* Go thru' TX queues for netdev */
4510 for (i = 0; i < netdev->num_tx_queues; i++) {
4511 struct netdev_queue *q;
4512
4513 q = netdev_get_tx_queue(netdev, i);
4514 if (q)
4515 i40e_detect_recover_hung_queue(i, vsi);
4516 }
4517}
4518
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004519/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004520 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004521 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004522 *
4523 * Get TC map for ISCSI PF type that will include iSCSI TC
4524 * and LAN TC.
4525 **/
4526static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4527{
4528 struct i40e_dcb_app_priority_table app;
4529 struct i40e_hw *hw = &pf->hw;
4530 u8 enabled_tc = 1; /* TC0 is always enabled */
4531 u8 tc, i;
4532 /* Get the iSCSI APP TLV */
4533 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4534
4535 for (i = 0; i < dcbcfg->numapps; i++) {
4536 app = dcbcfg->app[i];
4537 if (app.selector == I40E_APP_SEL_TCPIP &&
4538 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4539 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004540 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004541 break;
4542 }
4543 }
4544
4545 return enabled_tc;
4546}
4547
4548/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004549 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4550 * @dcbcfg: the corresponding DCBx configuration structure
4551 *
4552 * Return the number of TCs from given DCBx configuration
4553 **/
4554static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4555{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004556 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004557 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004558 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004559
4560 /* Scan the ETS Config Priority Table to find
4561 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004562 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004563 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004564 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4565 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4566
4567 /* Now scan the bitmask to check for
4568 * contiguous TCs starting with TC0
4569 */
4570 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4571 if (num_tc & BIT(i)) {
4572 if (!tc_unused) {
4573 ret++;
4574 } else {
4575 pr_err("Non-contiguous TC - Disabling DCB\n");
4576 return 1;
4577 }
4578 } else {
4579 tc_unused = 1;
4580 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004581 }
4582
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004583 /* There is always at least TC0 */
4584 if (!ret)
4585 ret = 1;
4586
4587 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004588}
4589
4590/**
4591 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4592 * @dcbcfg: the corresponding DCBx configuration structure
4593 *
4594 * Query the current DCB configuration and return the number of
4595 * traffic classes enabled from the given DCBX config
4596 **/
4597static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4598{
4599 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4600 u8 enabled_tc = 1;
4601 u8 i;
4602
4603 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004604 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004605
4606 return enabled_tc;
4607}
4608
4609/**
4610 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4611 * @pf: PF being queried
4612 *
4613 * Return number of traffic classes enabled for the given PF
4614 **/
4615static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4616{
4617 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004618 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004619 u8 num_tc = 0;
4620 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4621
4622 /* If DCB is not enabled then always in single TC */
4623 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4624 return 1;
4625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004626 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004627 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4628 return i40e_dcb_get_num_tc(dcbcfg);
4629
4630 /* MFP mode return count of enabled TCs for this PF */
4631 if (pf->hw.func_caps.iscsi)
4632 enabled_tc = i40e_get_iscsi_tc_map(pf);
4633 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004634 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004635
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004636 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004637 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004638 num_tc++;
4639 }
4640 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004641}
4642
4643/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004644 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4645 * @pf: PF being queried
4646 *
4647 * Return a bitmap for enabled traffic classes for this PF.
4648 **/
4649static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4650{
4651 /* If DCB is not enabled for this PF then just return default TC */
4652 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004653 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004654
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004655 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004656 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4657 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4658
Neerav Parikhfc51de92015-02-24 06:58:53 +00004659 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004660 if (pf->hw.func_caps.iscsi)
4661 return i40e_get_iscsi_tc_map(pf);
4662 else
David Ertmanea6acb72016-09-20 07:10:50 -07004663 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004664}
4665
4666/**
4667 * i40e_vsi_get_bw_info - Query VSI BW Information
4668 * @vsi: the VSI being queried
4669 *
4670 * Returns 0 on success, negative value on failure
4671 **/
4672static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4673{
4674 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4675 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4676 struct i40e_pf *pf = vsi->back;
4677 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004678 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004679 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004680 int i;
4681
4682 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004683 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4684 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004685 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004686 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4687 i40e_stat_str(&pf->hw, ret),
4688 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004689 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004690 }
4691
4692 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004693 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4694 NULL);
4695 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004696 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004697 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4698 i40e_stat_str(&pf->hw, ret),
4699 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004700 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004701 }
4702
4703 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4704 dev_info(&pf->pdev->dev,
4705 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4706 bw_config.tc_valid_bits,
4707 bw_ets_config.tc_valid_bits);
4708 /* Still continuing */
4709 }
4710
4711 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4712 vsi->bw_max_quanta = bw_config.max_bw;
4713 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4714 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4715 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4716 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4717 vsi->bw_ets_limit_credits[i] =
4718 le16_to_cpu(bw_ets_config.credits[i]);
4719 /* 3 bits out of 4 for each TC */
4720 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4721 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004722
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004723 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004724}
4725
4726/**
4727 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4728 * @vsi: the VSI being configured
4729 * @enabled_tc: TC bitmap
4730 * @bw_credits: BW shared credits per TC
4731 *
4732 * Returns 0 on success, negative value on failure
4733 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004734static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004735 u8 *bw_share)
4736{
4737 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004738 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004739 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004740
4741 bw_data.tc_valid_bits = enabled_tc;
4742 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4743 bw_data.tc_bw_credits[i] = bw_share[i];
4744
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004745 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4746 NULL);
4747 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004748 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004749 "AQ command Config VSI BW allocation per TC failed = %d\n",
4750 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004751 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004752 }
4753
4754 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4755 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4756
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004757 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758}
4759
4760/**
4761 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4762 * @vsi: the VSI being configured
4763 * @enabled_tc: TC map to be enabled
4764 *
4765 **/
4766static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4767{
4768 struct net_device *netdev = vsi->netdev;
4769 struct i40e_pf *pf = vsi->back;
4770 struct i40e_hw *hw = &pf->hw;
4771 u8 netdev_tc = 0;
4772 int i;
4773 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4774
4775 if (!netdev)
4776 return;
4777
4778 if (!enabled_tc) {
4779 netdev_reset_tc(netdev);
4780 return;
4781 }
4782
4783 /* Set up actual enabled TCs on the VSI */
4784 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4785 return;
4786
4787 /* set per TC queues for the VSI */
4788 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4789 /* Only set TC queues for enabled tcs
4790 *
4791 * e.g. For a VSI that has TC0 and TC3 enabled the
4792 * enabled_tc bitmap would be 0x00001001; the driver
4793 * will set the numtc for netdev as 2 that will be
4794 * referenced by the netdev layer as TC 0 and 1.
4795 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004796 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004797 netdev_set_tc_queue(netdev,
4798 vsi->tc_config.tc_info[i].netdev_tc,
4799 vsi->tc_config.tc_info[i].qcount,
4800 vsi->tc_config.tc_info[i].qoffset);
4801 }
4802
4803 /* Assign UP2TC map for the VSI */
4804 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4805 /* Get the actual TC# for the UP */
4806 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4807 /* Get the mapped netdev TC# for the UP */
4808 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4809 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4810 }
4811}
4812
4813/**
4814 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4815 * @vsi: the VSI being configured
4816 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4817 **/
4818static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4819 struct i40e_vsi_context *ctxt)
4820{
4821 /* copy just the sections touched not the entire info
4822 * since not all sections are valid as returned by
4823 * update vsi params
4824 */
4825 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4826 memcpy(&vsi->info.queue_mapping,
4827 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4828 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4829 sizeof(vsi->info.tc_mapping));
4830}
4831
4832/**
4833 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4834 * @vsi: VSI to be configured
4835 * @enabled_tc: TC bitmap
4836 *
4837 * This configures a particular VSI for TCs that are mapped to the
4838 * given TC bitmap. It uses default bandwidth share for TCs across
4839 * VSIs to configure TC for a particular VSI.
4840 *
4841 * NOTE:
4842 * It is expected that the VSI queues have been quisced before calling
4843 * this function.
4844 **/
4845static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4846{
4847 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4848 struct i40e_vsi_context ctxt;
4849 int ret = 0;
4850 int i;
4851
4852 /* Check if enabled_tc is same as existing or new TCs */
4853 if (vsi->tc_config.enabled_tc == enabled_tc)
4854 return ret;
4855
4856 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4857 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004858 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004859 bw_share[i] = 1;
4860 }
4861
4862 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4863 if (ret) {
4864 dev_info(&vsi->back->pdev->dev,
4865 "Failed configuring TC map %d for VSI %d\n",
4866 enabled_tc, vsi->seid);
4867 goto out;
4868 }
4869
4870 /* Update Queue Pairs Mapping for currently enabled UPs */
4871 ctxt.seid = vsi->seid;
4872 ctxt.pf_num = vsi->back->hw.pf_id;
4873 ctxt.vf_num = 0;
4874 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004875 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004876 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4877
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004878 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4879 ctxt.info.valid_sections |=
4880 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4881 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4882 }
4883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004884 /* Update the VSI after updating the VSI queue-mapping information */
4885 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4886 if (ret) {
4887 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004888 "Update vsi tc config failed, err %s aq_err %s\n",
4889 i40e_stat_str(&vsi->back->hw, ret),
4890 i40e_aq_str(&vsi->back->hw,
4891 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004892 goto out;
4893 }
4894 /* update the local VSI info with updated queue map */
4895 i40e_vsi_update_queue_map(vsi, &ctxt);
4896 vsi->info.valid_sections = 0;
4897
4898 /* Update current VSI BW information */
4899 ret = i40e_vsi_get_bw_info(vsi);
4900 if (ret) {
4901 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004902 "Failed updating vsi bw info, err %s aq_err %s\n",
4903 i40e_stat_str(&vsi->back->hw, ret),
4904 i40e_aq_str(&vsi->back->hw,
4905 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004906 goto out;
4907 }
4908
4909 /* Update the netdev TC setup */
4910 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4911out:
4912 return ret;
4913}
4914
4915/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004916 * i40e_veb_config_tc - Configure TCs for given VEB
4917 * @veb: given VEB
4918 * @enabled_tc: TC bitmap
4919 *
4920 * Configures given TC bitmap for VEB (switching) element
4921 **/
4922int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4923{
4924 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4925 struct i40e_pf *pf = veb->pf;
4926 int ret = 0;
4927 int i;
4928
4929 /* No TCs or already enabled TCs just return */
4930 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4931 return ret;
4932
4933 bw_data.tc_valid_bits = enabled_tc;
4934 /* bw_data.absolute_credits is not set (relative) */
4935
4936 /* Enable ETS TCs with equal BW Share for now */
4937 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004938 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004939 bw_data.tc_bw_share_credits[i] = 1;
4940 }
4941
4942 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4943 &bw_data, NULL);
4944 if (ret) {
4945 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004946 "VEB bw config failed, err %s aq_err %s\n",
4947 i40e_stat_str(&pf->hw, ret),
4948 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004949 goto out;
4950 }
4951
4952 /* Update the BW information */
4953 ret = i40e_veb_get_bw_info(veb);
4954 if (ret) {
4955 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004956 "Failed getting veb bw config, err %s aq_err %s\n",
4957 i40e_stat_str(&pf->hw, ret),
4958 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004959 }
4960
4961out:
4962 return ret;
4963}
4964
4965#ifdef CONFIG_I40E_DCB
4966/**
4967 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4968 * @pf: PF struct
4969 *
4970 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4971 * the caller would've quiesce all the VSIs before calling
4972 * this function
4973 **/
4974static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4975{
4976 u8 tc_map = 0;
4977 int ret;
4978 u8 v;
4979
4980 /* Enable the TCs available on PF to all VEBs */
4981 tc_map = i40e_pf_get_tc_map(pf);
4982 for (v = 0; v < I40E_MAX_VEB; v++) {
4983 if (!pf->veb[v])
4984 continue;
4985 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4986 if (ret) {
4987 dev_info(&pf->pdev->dev,
4988 "Failed configuring TC for VEB seid=%d\n",
4989 pf->veb[v]->seid);
4990 /* Will try to configure as many components */
4991 }
4992 }
4993
4994 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004995 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004996 if (!pf->vsi[v])
4997 continue;
4998
4999 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005000#ifdef I40E_FCOE
5001 * - For FCoE VSI only enable the TC configured
5002 * as per the APP TLV
5003#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005004 * - For all others keep them at TC0 for now
5005 */
5006 if (v == pf->lan_vsi)
5007 tc_map = i40e_pf_get_tc_map(pf);
5008 else
David Ertmanea6acb72016-09-20 07:10:50 -07005009 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005010#ifdef I40E_FCOE
5011 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5012 tc_map = i40e_get_fcoe_tc_map(pf);
5013#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005014
5015 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5016 if (ret) {
5017 dev_info(&pf->pdev->dev,
5018 "Failed configuring TC for VSI seid=%d\n",
5019 pf->vsi[v]->seid);
5020 /* Will try to configure as many components */
5021 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005022 /* Re-configure VSI vectors based on updated TC map */
5023 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005024 if (pf->vsi[v]->netdev)
5025 i40e_dcbnl_set_all(pf->vsi[v]);
5026 }
5027 }
5028}
5029
5030/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005031 * i40e_resume_port_tx - Resume port Tx
5032 * @pf: PF struct
5033 *
5034 * Resume a port's Tx and issue a PF reset in case of failure to
5035 * resume.
5036 **/
5037static int i40e_resume_port_tx(struct i40e_pf *pf)
5038{
5039 struct i40e_hw *hw = &pf->hw;
5040 int ret;
5041
5042 ret = i40e_aq_resume_port_tx(hw, NULL);
5043 if (ret) {
5044 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005045 "Resume Port Tx failed, err %s aq_err %s\n",
5046 i40e_stat_str(&pf->hw, ret),
5047 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005048 /* Schedule PF reset to recover */
5049 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5050 i40e_service_event_schedule(pf);
5051 }
5052
5053 return ret;
5054}
5055
5056/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005057 * i40e_init_pf_dcb - Initialize DCB configuration
5058 * @pf: PF being configured
5059 *
5060 * Query the current DCB configuration and cache it
5061 * in the hardware structure
5062 **/
5063static int i40e_init_pf_dcb(struct i40e_pf *pf)
5064{
5065 struct i40e_hw *hw = &pf->hw;
5066 int err = 0;
5067
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005068 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005069 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005070 goto out;
5071
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005072 /* Get the initial DCB configuration */
5073 err = i40e_init_dcb(hw);
5074 if (!err) {
5075 /* Device/Function is not DCBX capable */
5076 if ((!hw->func_caps.dcb) ||
5077 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5078 dev_info(&pf->pdev->dev,
5079 "DCBX offload is not supported or is disabled for this PF.\n");
5080
5081 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5082 goto out;
5083
5084 } else {
5085 /* When status is not DISABLED then DCBX in FW */
5086 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5087 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005088
5089 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005090 /* Enable DCB tagging only when more than one TC
5091 * or explicitly disable if only one TC
5092 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005093 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5094 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005095 else
5096 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005097 dev_dbg(&pf->pdev->dev,
5098 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005099 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005100 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005101 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005102 "Query for DCB configuration failed, err %s aq_err %s\n",
5103 i40e_stat_str(&pf->hw, err),
5104 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005105 }
5106
5107out:
5108 return err;
5109}
5110#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005111#define SPEED_SIZE 14
5112#define FC_SIZE 8
5113/**
5114 * i40e_print_link_message - print link up or down
5115 * @vsi: the VSI for which link needs a message
5116 */
Matt Jaredc156f852015-08-27 11:42:39 -04005117void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005118{
Shannon Nelsona9165492015-09-03 17:19:00 -04005119 char *speed = "Unknown";
5120 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005121
Matt Jaredc156f852015-08-27 11:42:39 -04005122 if (vsi->current_isup == isup)
5123 return;
5124 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005125 if (!isup) {
5126 netdev_info(vsi->netdev, "NIC Link is Down\n");
5127 return;
5128 }
5129
Greg Rose148c2d82014-12-11 07:06:27 +00005130 /* Warn user if link speed on NPAR enabled partition is not at
5131 * least 10GB
5132 */
5133 if (vsi->back->hw.func_caps.npar_enable &&
5134 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5135 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5136 netdev_warn(vsi->netdev,
5137 "The partition detected link speed that is less than 10Gbps\n");
5138
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005139 switch (vsi->back->hw.phy.link_info.link_speed) {
5140 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005141 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005142 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005143 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005144 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005145 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005146 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005147 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005148 break;
5149 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005150 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005151 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005152 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005153 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005154 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005155 default:
5156 break;
5157 }
5158
5159 switch (vsi->back->hw.fc.current_mode) {
5160 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005161 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005162 break;
5163 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005164 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005165 break;
5166 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005167 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005168 break;
5169 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005170 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005171 break;
5172 }
5173
Shannon Nelsona9165492015-09-03 17:19:00 -04005174 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005175 speed, fc);
5176}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005177
5178/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005179 * i40e_up_complete - Finish the last steps of bringing up a connection
5180 * @vsi: the VSI being configured
5181 **/
5182static int i40e_up_complete(struct i40e_vsi *vsi)
5183{
5184 struct i40e_pf *pf = vsi->back;
5185 int err;
5186
5187 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5188 i40e_vsi_configure_msix(vsi);
5189 else
5190 i40e_configure_msi_and_legacy(vsi);
5191
5192 /* start rings */
5193 err = i40e_vsi_control_rings(vsi, true);
5194 if (err)
5195 return err;
5196
5197 clear_bit(__I40E_DOWN, &vsi->state);
5198 i40e_napi_enable_all(vsi);
5199 i40e_vsi_enable_irq(vsi);
5200
5201 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5202 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005203 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005204 netif_tx_start_all_queues(vsi->netdev);
5205 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005206 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005207 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005208 /* need to check for qualified module here*/
5209 if ((pf->hw.phy.link_info.link_info &
5210 I40E_AQ_MEDIA_AVAILABLE) &&
5211 (!(pf->hw.phy.link_info.an_info &
5212 I40E_AQ_QUALIFIED_MODULE)))
5213 netdev_err(vsi->netdev,
5214 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005215 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005216
5217 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005218 if (vsi->type == I40E_VSI_FDIR) {
5219 /* reset fd counters */
5220 pf->fd_add_err = pf->fd_atr_cnt = 0;
5221 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005222 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005223 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5224 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005225 pf->fd_tcp_rule = 0;
5226 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005227 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005228 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005229
5230 /* On the next run of the service_task, notify any clients of the new
5231 * opened netdev
5232 */
5233 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005234 i40e_service_event_schedule(pf);
5235
5236 return 0;
5237}
5238
5239/**
5240 * i40e_vsi_reinit_locked - Reset the VSI
5241 * @vsi: the VSI being configured
5242 *
5243 * Rebuild the ring structs after some configuration
5244 * has changed, e.g. MTU size.
5245 **/
5246static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5247{
5248 struct i40e_pf *pf = vsi->back;
5249
5250 WARN_ON(in_interrupt());
5251 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5252 usleep_range(1000, 2000);
5253 i40e_down(vsi);
5254
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005255 i40e_up(vsi);
5256 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5257}
5258
5259/**
5260 * i40e_up - Bring the connection back up after being down
5261 * @vsi: the VSI being configured
5262 **/
5263int i40e_up(struct i40e_vsi *vsi)
5264{
5265 int err;
5266
5267 err = i40e_vsi_configure(vsi);
5268 if (!err)
5269 err = i40e_up_complete(vsi);
5270
5271 return err;
5272}
5273
5274/**
5275 * i40e_down - Shutdown the connection processing
5276 * @vsi: the VSI being stopped
5277 **/
5278void i40e_down(struct i40e_vsi *vsi)
5279{
5280 int i;
5281
5282 /* It is assumed that the caller of this function
5283 * sets the vsi->state __I40E_DOWN bit.
5284 */
5285 if (vsi->netdev) {
5286 netif_carrier_off(vsi->netdev);
5287 netif_tx_disable(vsi->netdev);
5288 }
5289 i40e_vsi_disable_irq(vsi);
5290 i40e_vsi_control_rings(vsi, false);
5291 i40e_napi_disable_all(vsi);
5292
5293 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005294 i40e_clean_tx_ring(vsi->tx_rings[i]);
5295 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005296 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005297
5298 i40e_notify_client_of_netdev_close(vsi, false);
5299
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005300}
5301
5302/**
5303 * i40e_setup_tc - configure multiple traffic classes
5304 * @netdev: net device to configure
5305 * @tc: number of traffic classes to enable
5306 **/
5307static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5308{
5309 struct i40e_netdev_priv *np = netdev_priv(netdev);
5310 struct i40e_vsi *vsi = np->vsi;
5311 struct i40e_pf *pf = vsi->back;
5312 u8 enabled_tc = 0;
5313 int ret = -EINVAL;
5314 int i;
5315
5316 /* Check if DCB enabled to continue */
5317 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5318 netdev_info(netdev, "DCB is not enabled for adapter\n");
5319 goto exit;
5320 }
5321
5322 /* Check if MFP enabled */
5323 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5324 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5325 goto exit;
5326 }
5327
5328 /* Check whether tc count is within enabled limit */
5329 if (tc > i40e_pf_get_num_tc(pf)) {
5330 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5331 goto exit;
5332 }
5333
5334 /* Generate TC map for number of tc requested */
5335 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005336 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005337
5338 /* Requesting same TC configuration as already enabled */
5339 if (enabled_tc == vsi->tc_config.enabled_tc)
5340 return 0;
5341
5342 /* Quiesce VSI queues */
5343 i40e_quiesce_vsi(vsi);
5344
5345 /* Configure VSI for enabled TCs */
5346 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5347 if (ret) {
5348 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5349 vsi->seid);
5350 goto exit;
5351 }
5352
5353 /* Unquiesce VSI */
5354 i40e_unquiesce_vsi(vsi);
5355
5356exit:
5357 return ret;
5358}
5359
John Fastabende4c67342016-02-16 21:16:15 -08005360#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005361int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5362 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005363#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005364static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5365 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005366#endif
5367{
John Fastabend16e5cc62016-02-16 21:16:43 -08005368 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005369 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005370 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005371}
5372
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005373/**
5374 * i40e_open - Called when a network interface is made active
5375 * @netdev: network interface device structure
5376 *
5377 * The open entry point is called when a network interface is made
5378 * active by the system (IFF_UP). At this point all resources needed
5379 * for transmit and receive operations are allocated, the interrupt
5380 * handler is registered with the OS, the netdev watchdog subtask is
5381 * enabled, and the stack is notified that the interface is ready.
5382 *
5383 * Returns 0 on success, negative value on failure
5384 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005385int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005386{
5387 struct i40e_netdev_priv *np = netdev_priv(netdev);
5388 struct i40e_vsi *vsi = np->vsi;
5389 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005390 int err;
5391
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005392 /* disallow open during test or if eeprom is broken */
5393 if (test_bit(__I40E_TESTING, &pf->state) ||
5394 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005395 return -EBUSY;
5396
5397 netif_carrier_off(netdev);
5398
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005399 err = i40e_vsi_open(vsi);
5400 if (err)
5401 return err;
5402
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005403 /* configure global TSO hardware offload settings */
5404 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5405 TCP_FLAG_FIN) >> 16);
5406 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5407 TCP_FLAG_FIN |
5408 TCP_FLAG_CWR) >> 16);
5409 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5410
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005411 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005412
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005413 return 0;
5414}
5415
5416/**
5417 * i40e_vsi_open -
5418 * @vsi: the VSI to open
5419 *
5420 * Finish initialization of the VSI.
5421 *
5422 * Returns 0 on success, negative value on failure
5423 **/
5424int i40e_vsi_open(struct i40e_vsi *vsi)
5425{
5426 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005427 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005428 int err;
5429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005430 /* allocate descriptors */
5431 err = i40e_vsi_setup_tx_resources(vsi);
5432 if (err)
5433 goto err_setup_tx;
5434 err = i40e_vsi_setup_rx_resources(vsi);
5435 if (err)
5436 goto err_setup_rx;
5437
5438 err = i40e_vsi_configure(vsi);
5439 if (err)
5440 goto err_setup_rx;
5441
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005442 if (vsi->netdev) {
5443 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5444 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5445 err = i40e_vsi_request_irq(vsi, int_name);
5446 if (err)
5447 goto err_setup_rx;
5448
5449 /* Notify the stack of the actual queue counts. */
5450 err = netif_set_real_num_tx_queues(vsi->netdev,
5451 vsi->num_queue_pairs);
5452 if (err)
5453 goto err_set_queues;
5454
5455 err = netif_set_real_num_rx_queues(vsi->netdev,
5456 vsi->num_queue_pairs);
5457 if (err)
5458 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005459
5460 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005461 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005462 dev_driver_string(&pf->pdev->dev),
5463 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005464 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005465
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005466 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005467 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005468 goto err_setup_rx;
5469 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005471 err = i40e_up_complete(vsi);
5472 if (err)
5473 goto err_up_complete;
5474
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005475 return 0;
5476
5477err_up_complete:
5478 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005479err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005480 i40e_vsi_free_irq(vsi);
5481err_setup_rx:
5482 i40e_vsi_free_rx_resources(vsi);
5483err_setup_tx:
5484 i40e_vsi_free_tx_resources(vsi);
5485 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005486 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005487
5488 return err;
5489}
5490
5491/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005492 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005493 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005494 *
5495 * This function destroys the hlist where all the Flow Director
5496 * filters were saved.
5497 **/
5498static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5499{
5500 struct i40e_fdir_filter *filter;
5501 struct hlist_node *node2;
5502
5503 hlist_for_each_entry_safe(filter, node2,
5504 &pf->fdir_filter_list, fdir_node) {
5505 hlist_del(&filter->fdir_node);
5506 kfree(filter);
5507 }
5508 pf->fdir_pf_active_filters = 0;
5509}
5510
5511/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005512 * i40e_close - Disables a network interface
5513 * @netdev: network interface device structure
5514 *
5515 * The close entry point is called when an interface is de-activated
5516 * by the OS. The hardware is still under the driver's control, but
5517 * this netdev interface is disabled.
5518 *
5519 * Returns 0, this is not allowed to fail
5520 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005521int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005522{
5523 struct i40e_netdev_priv *np = netdev_priv(netdev);
5524 struct i40e_vsi *vsi = np->vsi;
5525
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005526 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005527
5528 return 0;
5529}
5530
5531/**
5532 * i40e_do_reset - Start a PF or Core Reset sequence
5533 * @pf: board private structure
5534 * @reset_flags: which reset is requested
5535 *
5536 * The essential difference in resets is that the PF Reset
5537 * doesn't clear the packet buffers, doesn't reset the PE
5538 * firmware, and doesn't bother the other PFs on the chip.
5539 **/
5540void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5541{
5542 u32 val;
5543
5544 WARN_ON(in_interrupt());
5545
Mitch Williams263fc482014-04-23 04:50:11 +00005546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005547 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005548 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005549
5550 /* Request a Global Reset
5551 *
5552 * This will start the chip's countdown to the actual full
5553 * chip reset event, and a warning interrupt to be sent
5554 * to all PFs, including the requestor. Our handler
5555 * for the warning interrupt will deal with the shutdown
5556 * and recovery of the switch setup.
5557 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005558 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005559 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5560 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5561 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5562
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005563 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005564
5565 /* Request a Core Reset
5566 *
5567 * Same as Global Reset, except does *not* include the MAC/PHY
5568 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005569 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005570 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5571 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5572 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5573 i40e_flush(&pf->hw);
5574
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005575 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005576
5577 /* Request a PF Reset
5578 *
5579 * Resets only the PF-specific registers
5580 *
5581 * This goes directly to the tear-down and rebuild of
5582 * the switch, since we need to do all the recovery as
5583 * for the Core Reset.
5584 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005585 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005586 i40e_handle_reset_warning(pf);
5587
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005588 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005589 int v;
5590
5591 /* Find the VSI(s) that requested a re-init */
5592 dev_info(&pf->pdev->dev,
5593 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005594 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005595 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005596
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005597 if (vsi != NULL &&
5598 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5599 i40e_vsi_reinit_locked(pf->vsi[v]);
5600 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5601 }
5602 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005603 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005604 int v;
5605
5606 /* Find the VSI(s) that needs to be brought down */
5607 dev_info(&pf->pdev->dev, "VSI down requested\n");
5608 for (v = 0; v < pf->num_alloc_vsi; v++) {
5609 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005610
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005611 if (vsi != NULL &&
5612 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5613 set_bit(__I40E_DOWN, &vsi->state);
5614 i40e_down(vsi);
5615 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5616 }
5617 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005618 } else {
5619 dev_info(&pf->pdev->dev,
5620 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005621 }
5622}
5623
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005624#ifdef CONFIG_I40E_DCB
5625/**
5626 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5627 * @pf: board private structure
5628 * @old_cfg: current DCB config
5629 * @new_cfg: new DCB config
5630 **/
5631bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5632 struct i40e_dcbx_config *old_cfg,
5633 struct i40e_dcbx_config *new_cfg)
5634{
5635 bool need_reconfig = false;
5636
5637 /* Check if ETS configuration has changed */
5638 if (memcmp(&new_cfg->etscfg,
5639 &old_cfg->etscfg,
5640 sizeof(new_cfg->etscfg))) {
5641 /* If Priority Table has changed reconfig is needed */
5642 if (memcmp(&new_cfg->etscfg.prioritytable,
5643 &old_cfg->etscfg.prioritytable,
5644 sizeof(new_cfg->etscfg.prioritytable))) {
5645 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005646 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005647 }
5648
5649 if (memcmp(&new_cfg->etscfg.tcbwtable,
5650 &old_cfg->etscfg.tcbwtable,
5651 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005652 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005653
5654 if (memcmp(&new_cfg->etscfg.tsatable,
5655 &old_cfg->etscfg.tsatable,
5656 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005657 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005658 }
5659
5660 /* Check if PFC configuration has changed */
5661 if (memcmp(&new_cfg->pfc,
5662 &old_cfg->pfc,
5663 sizeof(new_cfg->pfc))) {
5664 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005665 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005666 }
5667
5668 /* Check if APP Table has changed */
5669 if (memcmp(&new_cfg->app,
5670 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005671 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005672 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005673 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005674 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005675
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005676 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005677 return need_reconfig;
5678}
5679
5680/**
5681 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5682 * @pf: board private structure
5683 * @e: event info posted on ARQ
5684 **/
5685static int i40e_handle_lldp_event(struct i40e_pf *pf,
5686 struct i40e_arq_event_info *e)
5687{
5688 struct i40e_aqc_lldp_get_mib *mib =
5689 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5690 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005691 struct i40e_dcbx_config tmp_dcbx_cfg;
5692 bool need_reconfig = false;
5693 int ret = 0;
5694 u8 type;
5695
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005696 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005697 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005698 return ret;
5699
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005700 /* Ignore if event is not for Nearest Bridge */
5701 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5702 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005703 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005704 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5705 return ret;
5706
5707 /* Check MIB Type and return if event for Remote MIB update */
5708 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005709 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005710 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005711 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5712 /* Update the remote cached instance and return */
5713 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5714 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5715 &hw->remote_dcbx_config);
5716 goto exit;
5717 }
5718
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005719 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005720 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005721
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005722 /* Reset the old DCBx configuration data */
5723 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005724 /* Get updated DCBX data from firmware */
5725 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005726 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005727 dev_info(&pf->pdev->dev,
5728 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5729 i40e_stat_str(&pf->hw, ret),
5730 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005731 goto exit;
5732 }
5733
5734 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005735 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5736 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005737 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005738 goto exit;
5739 }
5740
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005741 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5742 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005743
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005744 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005745
5746 if (!need_reconfig)
5747 goto exit;
5748
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005749 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005750 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005751 pf->flags |= I40E_FLAG_DCB_ENABLED;
5752 else
5753 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5754
Neerav Parikh69129dc2014-11-12 00:18:46 +00005755 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005756 /* Reconfiguration needed quiesce all VSIs */
5757 i40e_pf_quiesce_all_vsi(pf);
5758
5759 /* Changes in configuration update VEB/VSI */
5760 i40e_dcb_reconfigure(pf);
5761
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005762 ret = i40e_resume_port_tx(pf);
5763
Neerav Parikh69129dc2014-11-12 00:18:46 +00005764 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005765 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005766 if (ret)
5767 goto exit;
5768
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005769 /* Wait for the PF's queues to be disabled */
5770 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005771 if (ret) {
5772 /* Schedule PF reset to recover */
5773 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5774 i40e_service_event_schedule(pf);
5775 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005776 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005777 /* Notify the client for the DCB changes */
5778 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005779 }
5780
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005781exit:
5782 return ret;
5783}
5784#endif /* CONFIG_I40E_DCB */
5785
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005786/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005787 * i40e_do_reset_safe - Protected reset path for userland calls.
5788 * @pf: board private structure
5789 * @reset_flags: which reset is requested
5790 *
5791 **/
5792void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5793{
5794 rtnl_lock();
5795 i40e_do_reset(pf, reset_flags);
5796 rtnl_unlock();
5797}
5798
5799/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005800 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5801 * @pf: board private structure
5802 * @e: event info posted on ARQ
5803 *
5804 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5805 * and VF queues
5806 **/
5807static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5808 struct i40e_arq_event_info *e)
5809{
5810 struct i40e_aqc_lan_overflow *data =
5811 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5812 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5813 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5814 struct i40e_hw *hw = &pf->hw;
5815 struct i40e_vf *vf;
5816 u16 vf_id;
5817
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005818 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5819 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005820
5821 /* Queue belongs to VF, find the VF and issue VF reset */
5822 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5823 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5824 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5825 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5826 vf_id -= hw->func_caps.vf_base_id;
5827 vf = &pf->vf[vf_id];
5828 i40e_vc_notify_vf_reset(vf);
5829 /* Allow VF to process pending reset notification */
5830 msleep(20);
5831 i40e_reset_vf(vf, false);
5832 }
5833}
5834
5835/**
5836 * i40e_service_event_complete - Finish up the service event
5837 * @pf: board private structure
5838 **/
5839static void i40e_service_event_complete(struct i40e_pf *pf)
5840{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005841 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005842
5843 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005844 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005845 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5846}
5847
5848/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005849 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5850 * @pf: board private structure
5851 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005852u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005853{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005854 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005855
5856 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5857 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5858 return fcnt_prog;
5859}
5860
5861/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005862 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005863 * @pf: board private structure
5864 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005865u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005866{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005867 u32 val, fcnt_prog;
5868
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005869 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5870 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5871 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5872 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5873 return fcnt_prog;
5874}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005875
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005876/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005877 * i40e_get_global_fd_count - Get total FD filters programmed on device
5878 * @pf: board private structure
5879 **/
5880u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5881{
5882 u32 val, fcnt_prog;
5883
5884 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5885 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5886 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5887 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5888 return fcnt_prog;
5889}
5890
5891/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005892 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5893 * @pf: board private structure
5894 **/
5895void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5896{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005897 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005898 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005899 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005900
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005901 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5902 return;
5903
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005904 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5905 * to re-enable
5906 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005907 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005908 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005909 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5910 (pf->fd_add_err == 0) ||
5911 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005912 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5913 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5914 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005915 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5916 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 +00005917 }
5918 }
Jacob Kellera3417d22016-09-06 18:05:10 -07005919
5920 /* Wait for some more space to be available to turn on ATR. We also
5921 * must check that no existing ntuple rules for TCP are in effect
5922 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005923 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5924 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07005925 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
5926 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005927 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005928 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07005929 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005930 }
5931 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005932
5933 /* if hw had a problem adding a filter, delete it */
5934 if (pf->fd_inv > 0) {
5935 hlist_for_each_entry_safe(filter, node,
5936 &pf->fdir_filter_list, fdir_node) {
5937 if (filter->fd_id == pf->fd_inv) {
5938 hlist_del(&filter->fdir_node);
5939 kfree(filter);
5940 pf->fdir_pf_active_filters--;
5941 }
5942 }
5943 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005944}
5945
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005946#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005947#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005948/**
5949 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5950 * @pf: board private structure
5951 **/
5952static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5953{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005954 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005955 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005956 bool disable_atr = false;
5957 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005958 int reg;
5959
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005960 if (!time_after(jiffies, pf->fd_flush_timestamp +
5961 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5962 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005963
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005964 /* If the flush is happening too quick and we have mostly SB rules we
5965 * should not re-enable ATR for some time.
5966 */
5967 min_flush_time = pf->fd_flush_timestamp +
5968 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5969 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005970
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005971 if (!(time_after(jiffies, min_flush_time)) &&
5972 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5973 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5974 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5975 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005976 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005977
5978 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07005979 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005980 /* flush all filters */
5981 wr32(&pf->hw, I40E_PFQF_CTL_1,
5982 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5983 i40e_flush(&pf->hw);
5984 pf->fd_flush_cnt++;
5985 pf->fd_add_err = 0;
5986 do {
5987 /* Check FD flush status every 5-6msec */
5988 usleep_range(5000, 6000);
5989 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5990 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5991 break;
5992 } while (flush_wait_retry--);
5993 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5994 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5995 } else {
5996 /* replay sideband filters */
5997 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5998 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07005999 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006000 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6001 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6002 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6003 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006004}
6005
6006/**
6007 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6008 * @pf: board private structure
6009 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006010u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006011{
6012 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6013}
6014
6015/* We can see up to 256 filter programming desc in transit if the filters are
6016 * being applied really fast; before we see the first
6017 * filter miss error on Rx queue 0. Accumulating enough error messages before
6018 * reacting will make sure we don't cause flush too often.
6019 */
6020#define I40E_MAX_FD_PROGRAM_ERROR 256
6021
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006022/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006023 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6024 * @pf: board private structure
6025 **/
6026static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6027{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006028
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006029 /* if interface is down do nothing */
6030 if (test_bit(__I40E_DOWN, &pf->state))
6031 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006032
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006033 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006034 i40e_fdir_flush_and_replay(pf);
6035
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006036 i40e_fdir_check_and_reenable(pf);
6037
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006038}
6039
6040/**
6041 * i40e_vsi_link_event - notify VSI of a link event
6042 * @vsi: vsi to be notified
6043 * @link_up: link up or down
6044 **/
6045static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6046{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006047 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006048 return;
6049
6050 switch (vsi->type) {
6051 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006052#ifdef I40E_FCOE
6053 case I40E_VSI_FCOE:
6054#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006055 if (!vsi->netdev || !vsi->netdev_registered)
6056 break;
6057
6058 if (link_up) {
6059 netif_carrier_on(vsi->netdev);
6060 netif_tx_wake_all_queues(vsi->netdev);
6061 } else {
6062 netif_carrier_off(vsi->netdev);
6063 netif_tx_stop_all_queues(vsi->netdev);
6064 }
6065 break;
6066
6067 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006068 case I40E_VSI_VMDQ2:
6069 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006070 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006071 case I40E_VSI_MIRROR:
6072 default:
6073 /* there is no notification for other VSIs */
6074 break;
6075 }
6076}
6077
6078/**
6079 * i40e_veb_link_event - notify elements on the veb of a link event
6080 * @veb: veb to be notified
6081 * @link_up: link up or down
6082 **/
6083static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6084{
6085 struct i40e_pf *pf;
6086 int i;
6087
6088 if (!veb || !veb->pf)
6089 return;
6090 pf = veb->pf;
6091
6092 /* depth first... */
6093 for (i = 0; i < I40E_MAX_VEB; i++)
6094 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6095 i40e_veb_link_event(pf->veb[i], link_up);
6096
6097 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006098 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006099 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6100 i40e_vsi_link_event(pf->vsi[i], link_up);
6101}
6102
6103/**
6104 * i40e_link_event - Update netif_carrier status
6105 * @pf: board private structure
6106 **/
6107static void i40e_link_event(struct i40e_pf *pf)
6108{
Mitch Williams320684c2014-10-17 03:14:43 +00006109 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006110 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006111 i40e_status status;
6112 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006113
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006114 /* save off old link status information */
6115 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6116
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006117 /* set this to force the get_link_status call to refresh state */
6118 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006119
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006120 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006121
6122 status = i40e_get_link_status(&pf->hw, &new_link);
6123 if (status) {
6124 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6125 status);
6126 return;
6127 }
6128
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006129 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6130 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006131
6132 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006133 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006134 (test_bit(__I40E_DOWN, &vsi->state) ||
6135 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006137
6138 if (!test_bit(__I40E_DOWN, &vsi->state))
6139 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006140
6141 /* Notify the base of the switch tree connected to
6142 * the link. Floating VEBs are not notified.
6143 */
6144 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6145 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6146 else
Mitch Williams320684c2014-10-17 03:14:43 +00006147 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006148
6149 if (pf->vf)
6150 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006151
6152 if (pf->flags & I40E_FLAG_PTP)
6153 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006154}
6155
6156/**
Shannon Nelson21536712014-10-25 10:35:25 +00006157 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006158 * @pf: board private structure
6159 **/
6160static void i40e_watchdog_subtask(struct i40e_pf *pf)
6161{
6162 int i;
6163
6164 /* if interface is down do nothing */
6165 if (test_bit(__I40E_DOWN, &pf->state) ||
6166 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6167 return;
6168
Shannon Nelson21536712014-10-25 10:35:25 +00006169 /* make sure we don't do these things too often */
6170 if (time_before(jiffies, (pf->service_timer_previous +
6171 pf->service_timer_period)))
6172 return;
6173 pf->service_timer_previous = jiffies;
6174
Shannon Nelson9ac77262015-08-27 11:42:40 -04006175 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6176 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006177
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006178 /* Update the stats for active netdevs so the network stack
6179 * can look at updated numbers whenever it cares to
6180 */
Mitch Williams505682c2014-05-20 08:01:37 +00006181 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006182 if (pf->vsi[i] && pf->vsi[i]->netdev)
6183 i40e_update_stats(pf->vsi[i]);
6184
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006185 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6186 /* Update the stats for the active switching components */
6187 for (i = 0; i < I40E_MAX_VEB; i++)
6188 if (pf->veb[i])
6189 i40e_update_veb_stats(pf->veb[i]);
6190 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006191
6192 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006193}
6194
6195/**
6196 * i40e_reset_subtask - Set up for resetting the device and driver
6197 * @pf: board private structure
6198 **/
6199static void i40e_reset_subtask(struct i40e_pf *pf)
6200{
6201 u32 reset_flags = 0;
6202
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006203 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006204 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006205 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6207 }
6208 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006209 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006210 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6211 }
6212 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006213 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006214 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6215 }
6216 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006217 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006218 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6219 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006220 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006221 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006222 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6223 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006224
6225 /* If there's a recovery already waiting, it takes
6226 * precedence before starting a new reset sequence.
6227 */
6228 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6229 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006230 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006231 }
6232
6233 /* If we're already down or resetting, just bail */
6234 if (reset_flags &&
6235 !test_bit(__I40E_DOWN, &pf->state) &&
6236 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6237 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006238
6239unlock:
6240 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006241}
6242
6243/**
6244 * i40e_handle_link_event - Handle link event
6245 * @pf: board private structure
6246 * @e: event info posted on ARQ
6247 **/
6248static void i40e_handle_link_event(struct i40e_pf *pf,
6249 struct i40e_arq_event_info *e)
6250{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006251 struct i40e_aqc_get_link_status *status =
6252 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006253
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006254 /* Do a new status request to re-enable LSE reporting
6255 * and load new status information into the hw struct
6256 * This completely ignores any state information
6257 * in the ARQ event info, instead choosing to always
6258 * issue the AQ update link status command.
6259 */
6260 i40e_link_event(pf);
6261
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006262 /* check for unqualified module, if link is down */
6263 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6264 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6265 (!(status->link_info & I40E_AQ_LINK_UP)))
6266 dev_err(&pf->pdev->dev,
6267 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006268}
6269
6270/**
6271 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6272 * @pf: board private structure
6273 **/
6274static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6275{
6276 struct i40e_arq_event_info event;
6277 struct i40e_hw *hw = &pf->hw;
6278 u16 pending, i = 0;
6279 i40e_status ret;
6280 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006281 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006282 u32 val;
6283
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006284 /* Do not run clean AQ when PF reset fails */
6285 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6286 return;
6287
Shannon Nelson86df2422014-05-20 08:01:35 +00006288 /* check for error indications */
6289 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6290 oldval = val;
6291 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006292 if (hw->debug_mask & I40E_DEBUG_AQ)
6293 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006294 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6295 }
6296 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006297 if (hw->debug_mask & I40E_DEBUG_AQ)
6298 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006299 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006300 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006301 }
6302 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006303 if (hw->debug_mask & I40E_DEBUG_AQ)
6304 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006305 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6306 }
6307 if (oldval != val)
6308 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6309
6310 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6311 oldval = val;
6312 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006313 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6314 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006315 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6316 }
6317 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006318 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6319 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006320 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6321 }
6322 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006323 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6324 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006325 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6326 }
6327 if (oldval != val)
6328 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6329
Mitch Williams1001dc32014-11-11 20:02:19 +00006330 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6331 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006332 if (!event.msg_buf)
6333 return;
6334
6335 do {
6336 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006337 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006338 break;
Mitch Williams56497972014-06-04 08:45:18 +00006339 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006340 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6341 break;
6342 }
6343
6344 opcode = le16_to_cpu(event.desc.opcode);
6345 switch (opcode) {
6346
6347 case i40e_aqc_opc_get_link_status:
6348 i40e_handle_link_event(pf, &event);
6349 break;
6350 case i40e_aqc_opc_send_msg_to_pf:
6351 ret = i40e_vc_process_vf_msg(pf,
6352 le16_to_cpu(event.desc.retval),
6353 le32_to_cpu(event.desc.cookie_high),
6354 le32_to_cpu(event.desc.cookie_low),
6355 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006356 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006357 break;
6358 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006359 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006360#ifdef CONFIG_I40E_DCB
6361 rtnl_lock();
6362 ret = i40e_handle_lldp_event(pf, &event);
6363 rtnl_unlock();
6364#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 break;
6366 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006367 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006368 i40e_handle_lan_overflow_event(pf, &event);
6369 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006370 case i40e_aqc_opc_send_msg_to_peer:
6371 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6372 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006373 case i40e_aqc_opc_nvm_erase:
6374 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006375 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006376 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6377 "ARQ NVM operation 0x%04x completed\n",
6378 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006379 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006380 default:
6381 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006382 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006383 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006384 break;
6385 }
6386 } while (pending && (i++ < pf->adminq_work_limit));
6387
6388 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6389 /* re-enable Admin queue interrupt cause */
6390 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6391 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6392 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6393 i40e_flush(hw);
6394
6395 kfree(event.msg_buf);
6396}
6397
6398/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006399 * i40e_verify_eeprom - make sure eeprom is good to use
6400 * @pf: board private structure
6401 **/
6402static void i40e_verify_eeprom(struct i40e_pf *pf)
6403{
6404 int err;
6405
6406 err = i40e_diag_eeprom_test(&pf->hw);
6407 if (err) {
6408 /* retry in case of garbage read */
6409 err = i40e_diag_eeprom_test(&pf->hw);
6410 if (err) {
6411 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6412 err);
6413 set_bit(__I40E_BAD_EEPROM, &pf->state);
6414 }
6415 }
6416
6417 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6418 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6419 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6420 }
6421}
6422
6423/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006424 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006425 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006426 *
6427 * enable switch loop back or die - no point in a return value
6428 **/
6429static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6430{
6431 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6432 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006433 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006434
6435 ctxt.seid = pf->main_vsi_seid;
6436 ctxt.pf_num = pf->hw.pf_id;
6437 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006438 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6439 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006440 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006441 "couldn't get PF vsi config, err %s aq_err %s\n",
6442 i40e_stat_str(&pf->hw, ret),
6443 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006444 return;
6445 }
6446 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6447 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6448 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6449
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006450 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6451 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006452 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006453 "update vsi switch failed, err %s aq_err %s\n",
6454 i40e_stat_str(&pf->hw, ret),
6455 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006456 }
6457}
6458
6459/**
6460 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006461 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006462 *
6463 * disable switch loop back or die - no point in a return value
6464 **/
6465static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6466{
6467 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6468 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006469 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006470
6471 ctxt.seid = pf->main_vsi_seid;
6472 ctxt.pf_num = pf->hw.pf_id;
6473 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006474 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6475 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006476 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006477 "couldn't get PF vsi config, err %s aq_err %s\n",
6478 i40e_stat_str(&pf->hw, ret),
6479 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006480 return;
6481 }
6482 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6483 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6484 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6485
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006486 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6487 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006488 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006489 "update vsi switch failed, err %s aq_err %s\n",
6490 i40e_stat_str(&pf->hw, ret),
6491 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006492 }
6493}
6494
6495/**
Neerav Parikh51616012015-02-06 08:52:14 +00006496 * i40e_config_bridge_mode - Configure the HW bridge mode
6497 * @veb: pointer to the bridge instance
6498 *
6499 * Configure the loop back mode for the LAN VSI that is downlink to the
6500 * specified HW bridge instance. It is expected this function is called
6501 * when a new HW bridge is instantiated.
6502 **/
6503static void i40e_config_bridge_mode(struct i40e_veb *veb)
6504{
6505 struct i40e_pf *pf = veb->pf;
6506
Shannon Nelson6dec1012015-09-28 14:12:30 -04006507 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6508 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6509 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006510 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6511 i40e_disable_pf_switch_lb(pf);
6512 else
6513 i40e_enable_pf_switch_lb(pf);
6514}
6515
6516/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006517 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6518 * @veb: pointer to the VEB instance
6519 *
6520 * This is a recursive function that first builds the attached VSIs then
6521 * recurses in to build the next layer of VEB. We track the connections
6522 * through our own index numbers because the seid's from the HW could
6523 * change across the reset.
6524 **/
6525static int i40e_reconstitute_veb(struct i40e_veb *veb)
6526{
6527 struct i40e_vsi *ctl_vsi = NULL;
6528 struct i40e_pf *pf = veb->pf;
6529 int v, veb_idx;
6530 int ret;
6531
6532 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006533 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006534 if (pf->vsi[v] &&
6535 pf->vsi[v]->veb_idx == veb->idx &&
6536 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6537 ctl_vsi = pf->vsi[v];
6538 break;
6539 }
6540 }
6541 if (!ctl_vsi) {
6542 dev_info(&pf->pdev->dev,
6543 "missing owner VSI for veb_idx %d\n", veb->idx);
6544 ret = -ENOENT;
6545 goto end_reconstitute;
6546 }
6547 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6548 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6549 ret = i40e_add_vsi(ctl_vsi);
6550 if (ret) {
6551 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006552 "rebuild of veb_idx %d owner VSI failed: %d\n",
6553 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006554 goto end_reconstitute;
6555 }
6556 i40e_vsi_reset_stats(ctl_vsi);
6557
6558 /* create the VEB in the switch and move the VSI onto the VEB */
6559 ret = i40e_add_veb(veb, ctl_vsi);
6560 if (ret)
6561 goto end_reconstitute;
6562
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006563 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6564 veb->bridge_mode = BRIDGE_MODE_VEB;
6565 else
6566 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006567 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006568
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006569 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006570 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006571 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6572 continue;
6573
6574 if (pf->vsi[v]->veb_idx == veb->idx) {
6575 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006576
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006577 vsi->uplink_seid = veb->seid;
6578 ret = i40e_add_vsi(vsi);
6579 if (ret) {
6580 dev_info(&pf->pdev->dev,
6581 "rebuild of vsi_idx %d failed: %d\n",
6582 v, ret);
6583 goto end_reconstitute;
6584 }
6585 i40e_vsi_reset_stats(vsi);
6586 }
6587 }
6588
6589 /* create any VEBs attached to this VEB - RECURSION */
6590 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6591 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6592 pf->veb[veb_idx]->uplink_seid = veb->seid;
6593 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6594 if (ret)
6595 break;
6596 }
6597 }
6598
6599end_reconstitute:
6600 return ret;
6601}
6602
6603/**
6604 * i40e_get_capabilities - get info about the HW
6605 * @pf: the PF struct
6606 **/
6607static int i40e_get_capabilities(struct i40e_pf *pf)
6608{
6609 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6610 u16 data_size;
6611 int buf_len;
6612 int err;
6613
6614 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6615 do {
6616 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6617 if (!cap_buf)
6618 return -ENOMEM;
6619
6620 /* this loads the data into the hw struct for us */
6621 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6622 &data_size,
6623 i40e_aqc_opc_list_func_capabilities,
6624 NULL);
6625 /* data loaded, buffer no longer needed */
6626 kfree(cap_buf);
6627
6628 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6629 /* retry with a larger buffer */
6630 buf_len = data_size;
6631 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6632 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006633 "capability discovery failed, err %s aq_err %s\n",
6634 i40e_stat_str(&pf->hw, err),
6635 i40e_aq_str(&pf->hw,
6636 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006637 return -ENODEV;
6638 }
6639 } while (err);
6640
6641 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6642 dev_info(&pf->pdev->dev,
6643 "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",
6644 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6645 pf->hw.func_caps.num_msix_vectors,
6646 pf->hw.func_caps.num_msix_vectors_vf,
6647 pf->hw.func_caps.fd_filters_guaranteed,
6648 pf->hw.func_caps.fd_filters_best_effort,
6649 pf->hw.func_caps.num_tx_qp,
6650 pf->hw.func_caps.num_vsis);
6651
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006652#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6653 + pf->hw.func_caps.num_vfs)
6654 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6655 dev_info(&pf->pdev->dev,
6656 "got num_vsis %d, setting num_vsis to %d\n",
6657 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6658 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6659 }
6660
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006661 return 0;
6662}
6663
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006664static int i40e_vsi_clear(struct i40e_vsi *vsi);
6665
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006666/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006667 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006668 * @pf: board private structure
6669 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006670static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006671{
6672 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006673 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006674
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006675 /* quick workaround for an NVM issue that leaves a critical register
6676 * uninitialized
6677 */
6678 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6679 static const u32 hkey[] = {
6680 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6681 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6682 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6683 0x95b3a76d};
6684
6685 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6686 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6687 }
6688
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006689 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690 return;
6691
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006692 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006693 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006694 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006695 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006696 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006697 break;
6698 }
6699 }
6700
6701 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006702 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006703 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6704 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 if (!vsi) {
6706 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006707 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6708 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006709 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006710 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006711
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006712 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006713}
6714
6715/**
6716 * i40e_fdir_teardown - release the Flow Director resources
6717 * @pf: board private structure
6718 **/
6719static void i40e_fdir_teardown(struct i40e_pf *pf)
6720{
6721 int i;
6722
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006723 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006724 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006725 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6726 i40e_vsi_release(pf->vsi[i]);
6727 break;
6728 }
6729 }
6730}
6731
6732/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006733 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006734 * @pf: board private structure
6735 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006736 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006737 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006738static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006739{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006740 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006741 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006742 u32 v;
6743
6744 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6745 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006746 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006747 if (i40e_check_asq_alive(&pf->hw))
6748 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006749
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006750 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006751
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006752 /* quiesce the VSIs and their queues that are not already DOWN */
6753 i40e_pf_quiesce_all_vsi(pf);
6754
Mitch Williams505682c2014-05-20 08:01:37 +00006755 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006756 if (pf->vsi[v])
6757 pf->vsi[v]->seid = 0;
6758 }
6759
6760 i40e_shutdown_adminq(&pf->hw);
6761
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006762 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006763 if (hw->hmc.hmc_obj) {
6764 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006765 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006766 dev_warn(&pf->pdev->dev,
6767 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006768 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006769}
6770
6771/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006772 * i40e_send_version - update firmware with driver version
6773 * @pf: PF struct
6774 */
6775static void i40e_send_version(struct i40e_pf *pf)
6776{
6777 struct i40e_driver_version dv;
6778
6779 dv.major_version = DRV_VERSION_MAJOR;
6780 dv.minor_version = DRV_VERSION_MINOR;
6781 dv.build_version = DRV_VERSION_BUILD;
6782 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006783 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006784 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6785}
6786
6787/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006788 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006789 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006790 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006791 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006792static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006793{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006794 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006795 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006796 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006797 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006798 u32 v;
6799
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006800 /* Now we wait for GRST to settle out.
6801 * We don't have to delete the VEBs or VSIs from the hw switch
6802 * because the reset will make them disappear.
6803 */
6804 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006805 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006806 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006807 set_bit(__I40E_RESET_FAILED, &pf->state);
6808 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006809 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006810 pf->pfr_count++;
6811
6812 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006813 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006814 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815
6816 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6817 ret = i40e_init_adminq(&pf->hw);
6818 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006819 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6820 i40e_stat_str(&pf->hw, ret),
6821 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006822 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006823 }
6824
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006825 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006826 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006827 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006828
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006829 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006830 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006831 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006832 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006834 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6835 hw->func_caps.num_rx_qp,
6836 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6837 if (ret) {
6838 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6839 goto end_core_reset;
6840 }
6841 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6842 if (ret) {
6843 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6844 goto end_core_reset;
6845 }
6846
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006847#ifdef CONFIG_I40E_DCB
6848 ret = i40e_init_pf_dcb(pf);
6849 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006850 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6851 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6852 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006853 }
6854#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006855#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006856 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006857
Vasu Dev38e00432014-08-01 13:27:03 -07006858#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006859 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006860 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861 if (ret)
6862 goto end_core_reset;
6863
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006864 /* The driver only wants link up/down and module qualification
6865 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006866 */
6867 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006868 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006869 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006870 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006871 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006872 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6873 i40e_stat_str(&pf->hw, ret),
6874 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006875
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006876 /* make sure our flow control settings are restored */
6877 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6878 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006879 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6880 i40e_stat_str(&pf->hw, ret),
6881 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006882
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006883 /* Rebuild the VSIs and VEBs that existed before reset.
6884 * They are still in our local switch element arrays, so only
6885 * need to rebuild the switch model in the HW.
6886 *
6887 * If there were VEBs but the reconstitution failed, we'll try
6888 * try to recover minimal use by getting the basic PF VSI working.
6889 */
6890 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006891 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006892 /* find the one VEB connected to the MAC, and find orphans */
6893 for (v = 0; v < I40E_MAX_VEB; v++) {
6894 if (!pf->veb[v])
6895 continue;
6896
6897 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6898 pf->veb[v]->uplink_seid == 0) {
6899 ret = i40e_reconstitute_veb(pf->veb[v]);
6900
6901 if (!ret)
6902 continue;
6903
6904 /* If Main VEB failed, we're in deep doodoo,
6905 * so give up rebuilding the switch and set up
6906 * for minimal rebuild of PF VSI.
6907 * If orphan failed, we'll report the error
6908 * but try to keep going.
6909 */
6910 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6911 dev_info(&pf->pdev->dev,
6912 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6913 ret);
6914 pf->vsi[pf->lan_vsi]->uplink_seid
6915 = pf->mac_seid;
6916 break;
6917 } else if (pf->veb[v]->uplink_seid == 0) {
6918 dev_info(&pf->pdev->dev,
6919 "rebuild of orphan VEB failed: %d\n",
6920 ret);
6921 }
6922 }
6923 }
6924 }
6925
6926 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006927 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006928 /* no VEB, so rebuild only the Main VSI */
6929 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6930 if (ret) {
6931 dev_info(&pf->pdev->dev,
6932 "rebuild of Main VSI failed: %d\n", ret);
6933 goto end_core_reset;
6934 }
6935 }
6936
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006937 /* Reconfigure hardware for allowing smaller MSS in the case
6938 * of TSO, so that we avoid the MDD being fired and causing
6939 * a reset in the case of small MSS+TSO.
6940 */
6941#define I40E_REG_MSS 0x000E64DC
6942#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6943#define I40E_64BYTE_MSS 0x400000
6944 val = rd32(hw, I40E_REG_MSS);
6945 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6946 val &= ~I40E_REG_MSS_MIN_MASK;
6947 val |= I40E_64BYTE_MSS;
6948 wr32(hw, I40E_REG_MSS, val);
6949 }
6950
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006951 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006952 msleep(75);
6953 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6954 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006955 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6956 i40e_stat_str(&pf->hw, ret),
6957 i40e_aq_str(&pf->hw,
6958 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006959 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960 /* reinit the misc interrupt */
6961 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6962 ret = i40e_setup_misc_vector(pf);
6963
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006964 /* Add a filter to drop all Flow control frames from any VSI from being
6965 * transmitted. By doing so we stop a malicious VF from sending out
6966 * PAUSE or PFC frames and potentially controlling traffic for other
6967 * PF/VF VSIs.
6968 * The FW can still send Flow control frames if enabled.
6969 */
6970 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6971 pf->main_vsi_seid);
6972
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973 /* restart the VSIs that were rebuilt and running before the reset */
6974 i40e_pf_unquiesce_all_vsi(pf);
6975
Mitch Williams69f64b22014-02-13 03:48:46 -08006976 if (pf->num_alloc_vfs) {
6977 for (v = 0; v < pf->num_alloc_vfs; v++)
6978 i40e_reset_vf(&pf->vf[v], true);
6979 }
6980
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006981 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006982 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983
6984end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006985 clear_bit(__I40E_RESET_FAILED, &pf->state);
6986clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006987 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6988}
6989
6990/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006991 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006992 * @pf: board private structure
6993 *
6994 * Close up the VFs and other things in prep for a Core Reset,
6995 * then get ready to rebuild the world.
6996 **/
6997static void i40e_handle_reset_warning(struct i40e_pf *pf)
6998{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006999 i40e_prep_for_reset(pf);
7000 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007001}
7002
7003/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007004 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007005 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007006 *
7007 * Called from the MDD irq handler to identify possibly malicious vfs
7008 **/
7009static void i40e_handle_mdd_event(struct i40e_pf *pf)
7010{
7011 struct i40e_hw *hw = &pf->hw;
7012 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007013 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007014 struct i40e_vf *vf;
7015 u32 reg;
7016 int i;
7017
7018 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7019 return;
7020
7021 /* find what triggered the MDD event */
7022 reg = rd32(hw, I40E_GL_MDET_TX);
7023 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007024 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7025 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007026 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007027 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007028 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007029 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007030 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7031 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7032 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007033 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007034 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 +00007035 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007036 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7037 mdd_detected = true;
7038 }
7039 reg = rd32(hw, I40E_GL_MDET_RX);
7040 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007041 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7042 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007043 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007044 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007045 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7046 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7047 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007048 if (netif_msg_rx_err(pf))
7049 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7050 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007051 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7052 mdd_detected = true;
7053 }
7054
Neerav Parikhdf430b12014-06-04 01:23:15 +00007055 if (mdd_detected) {
7056 reg = rd32(hw, I40E_PF_MDET_TX);
7057 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7058 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007059 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007060 pf_mdd_detected = true;
7061 }
7062 reg = rd32(hw, I40E_PF_MDET_RX);
7063 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7064 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007065 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007066 pf_mdd_detected = true;
7067 }
7068 /* Queue belongs to the PF, initiate a reset */
7069 if (pf_mdd_detected) {
7070 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7071 i40e_service_event_schedule(pf);
7072 }
7073 }
7074
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007075 /* see if one of the VFs needs its hand slapped */
7076 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7077 vf = &(pf->vf[i]);
7078 reg = rd32(hw, I40E_VP_MDET_TX(i));
7079 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7080 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7081 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007082 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7083 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007084 }
7085
7086 reg = rd32(hw, I40E_VP_MDET_RX(i));
7087 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7088 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7089 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007090 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7091 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007092 }
7093
7094 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7095 dev_info(&pf->pdev->dev,
7096 "Too many MDD events on VF %d, disabled\n", i);
7097 dev_info(&pf->pdev->dev,
7098 "Use PF Control I/F to re-enable the VF\n");
7099 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7100 }
7101 }
7102
7103 /* re-enable mdd interrupt cause */
7104 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7105 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7106 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7107 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7108 i40e_flush(hw);
7109}
7110
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007111/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007112 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007113 * @pf: board private structure
7114 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007115static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007116{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007117 struct i40e_hw *hw = &pf->hw;
7118 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007119 __be16 port;
7120 int i;
7121
Singhai, Anjali6a899022015-12-14 12:21:18 -08007122 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007123 return;
7124
Singhai, Anjali6a899022015-12-14 12:21:18 -08007125 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007126
7127 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007128 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7129 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7130 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007131 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007132 ret = i40e_aq_add_udp_tunnel(hw, port,
7133 pf->udp_ports[i].type,
7134 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007135 else
7136 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007137
7138 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007139 dev_dbg(&pf->pdev->dev,
7140 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7141 pf->udp_ports[i].type ? "vxlan" : "geneve",
7142 port ? "add" : "delete",
7143 ntohs(port), i,
7144 i40e_stat_str(&pf->hw, ret),
7145 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007146 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007147 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007148 }
7149 }
7150 }
7151}
7152
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007153/**
7154 * i40e_service_task - Run the driver's async subtasks
7155 * @work: pointer to work_struct containing our data
7156 **/
7157static void i40e_service_task(struct work_struct *work)
7158{
7159 struct i40e_pf *pf = container_of(work,
7160 struct i40e_pf,
7161 service_task);
7162 unsigned long start_time = jiffies;
7163
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007164 /* don't bother with service tasks if a reset is in progress */
7165 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7166 i40e_service_event_complete(pf);
7167 return;
7168 }
7169
Kiran Patilb03a8c12015-09-24 18:13:15 -04007170 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007171 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007172 i40e_reset_subtask(pf);
7173 i40e_handle_mdd_event(pf);
7174 i40e_vc_process_vflr_event(pf);
7175 i40e_watchdog_subtask(pf);
7176 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007177 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007178 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007179 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007180 i40e_clean_adminq_subtask(pf);
7181
7182 i40e_service_event_complete(pf);
7183
7184 /* If the tasks have taken longer than one timer cycle or there
7185 * is more work to be done, reschedule the service task now
7186 * rather than wait for the timer to tick again.
7187 */
7188 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7189 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7190 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7191 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7192 i40e_service_event_schedule(pf);
7193}
7194
7195/**
7196 * i40e_service_timer - timer callback
7197 * @data: pointer to PF struct
7198 **/
7199static void i40e_service_timer(unsigned long data)
7200{
7201 struct i40e_pf *pf = (struct i40e_pf *)data;
7202
7203 mod_timer(&pf->service_timer,
7204 round_jiffies(jiffies + pf->service_timer_period));
7205 i40e_service_event_schedule(pf);
7206}
7207
7208/**
7209 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7210 * @vsi: the VSI being configured
7211 **/
7212static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7213{
7214 struct i40e_pf *pf = vsi->back;
7215
7216 switch (vsi->type) {
7217 case I40E_VSI_MAIN:
7218 vsi->alloc_queue_pairs = pf->num_lan_qps;
7219 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7220 I40E_REQ_DESCRIPTOR_MULTIPLE);
7221 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7222 vsi->num_q_vectors = pf->num_lan_msix;
7223 else
7224 vsi->num_q_vectors = 1;
7225
7226 break;
7227
7228 case I40E_VSI_FDIR:
7229 vsi->alloc_queue_pairs = 1;
7230 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7231 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007232 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007233 break;
7234
7235 case I40E_VSI_VMDQ2:
7236 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7237 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7238 I40E_REQ_DESCRIPTOR_MULTIPLE);
7239 vsi->num_q_vectors = pf->num_vmdq_msix;
7240 break;
7241
7242 case I40E_VSI_SRIOV:
7243 vsi->alloc_queue_pairs = pf->num_vf_qps;
7244 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7245 I40E_REQ_DESCRIPTOR_MULTIPLE);
7246 break;
7247
Vasu Dev38e00432014-08-01 13:27:03 -07007248#ifdef I40E_FCOE
7249 case I40E_VSI_FCOE:
7250 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7251 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7252 I40E_REQ_DESCRIPTOR_MULTIPLE);
7253 vsi->num_q_vectors = pf->num_fcoe_msix;
7254 break;
7255
7256#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007257 default:
7258 WARN_ON(1);
7259 return -ENODATA;
7260 }
7261
7262 return 0;
7263}
7264
7265/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007266 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7267 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007268 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007269 *
7270 * On error: returns error code (negative)
7271 * On success: returns 0
7272 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007273static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007274{
7275 int size;
7276 int ret = 0;
7277
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007278 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007279 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7280 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7281 if (!vsi->tx_rings)
7282 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007283 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7284
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007285 if (alloc_qvectors) {
7286 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007287 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007288 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7289 if (!vsi->q_vectors) {
7290 ret = -ENOMEM;
7291 goto err_vectors;
7292 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007293 }
7294 return ret;
7295
7296err_vectors:
7297 kfree(vsi->tx_rings);
7298 return ret;
7299}
7300
7301/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007302 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7303 * @pf: board private structure
7304 * @type: type of VSI
7305 *
7306 * On error: returns error code (negative)
7307 * On success: returns vsi index in PF (positive)
7308 **/
7309static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7310{
7311 int ret = -ENODEV;
7312 struct i40e_vsi *vsi;
7313 int vsi_idx;
7314 int i;
7315
7316 /* Need to protect the allocation of the VSIs at the PF level */
7317 mutex_lock(&pf->switch_mutex);
7318
7319 /* VSI list may be fragmented if VSI creation/destruction has
7320 * been happening. We can afford to do a quick scan to look
7321 * for any free VSIs in the list.
7322 *
7323 * find next empty vsi slot, looping back around if necessary
7324 */
7325 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007326 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007327 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007328 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007329 i = 0;
7330 while (i < pf->next_vsi && pf->vsi[i])
7331 i++;
7332 }
7333
Mitch Williams505682c2014-05-20 08:01:37 +00007334 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007335 vsi_idx = i; /* Found one! */
7336 } else {
7337 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007338 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007339 }
7340 pf->next_vsi = ++i;
7341
7342 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7343 if (!vsi) {
7344 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007345 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007346 }
7347 vsi->type = type;
7348 vsi->back = pf;
7349 set_bit(__I40E_DOWN, &vsi->state);
7350 vsi->flags = 0;
7351 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007352 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007353 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7354 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007355 vsi->netdev_registered = false;
7356 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7357 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007358 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007359
Alexander Duyck9f65e152013-09-28 06:00:58 +00007360 ret = i40e_set_num_rings_in_vsi(vsi);
7361 if (ret)
7362 goto err_rings;
7363
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007364 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007365 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007366 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007368 /* Setup default MSIX irq handler for VSI */
7369 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7370
Kiran Patil21659032015-09-30 14:09:03 -04007371 /* Initialize VSI lock */
7372 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007373 pf->vsi[vsi_idx] = vsi;
7374 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007375 goto unlock_pf;
7376
Alexander Duyck9f65e152013-09-28 06:00:58 +00007377err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007378 pf->next_vsi = i - 1;
7379 kfree(vsi);
7380unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007381 mutex_unlock(&pf->switch_mutex);
7382 return ret;
7383}
7384
7385/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007386 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7387 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007388 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007389 *
7390 * On error: returns error code (negative)
7391 * On success: returns 0
7392 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007393static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007394{
7395 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007396 if (free_qvectors) {
7397 kfree(vsi->q_vectors);
7398 vsi->q_vectors = NULL;
7399 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007400 kfree(vsi->tx_rings);
7401 vsi->tx_rings = NULL;
7402 vsi->rx_rings = NULL;
7403}
7404
7405/**
Helin Zhang28c58692015-10-26 19:44:27 -04007406 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7407 * and lookup table
7408 * @vsi: Pointer to VSI structure
7409 */
7410static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7411{
7412 if (!vsi)
7413 return;
7414
7415 kfree(vsi->rss_hkey_user);
7416 vsi->rss_hkey_user = NULL;
7417
7418 kfree(vsi->rss_lut_user);
7419 vsi->rss_lut_user = NULL;
7420}
7421
7422/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007423 * i40e_vsi_clear - Deallocate the VSI provided
7424 * @vsi: the VSI being un-configured
7425 **/
7426static int i40e_vsi_clear(struct i40e_vsi *vsi)
7427{
7428 struct i40e_pf *pf;
7429
7430 if (!vsi)
7431 return 0;
7432
7433 if (!vsi->back)
7434 goto free_vsi;
7435 pf = vsi->back;
7436
7437 mutex_lock(&pf->switch_mutex);
7438 if (!pf->vsi[vsi->idx]) {
7439 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7440 vsi->idx, vsi->idx, vsi, vsi->type);
7441 goto unlock_vsi;
7442 }
7443
7444 if (pf->vsi[vsi->idx] != vsi) {
7445 dev_err(&pf->pdev->dev,
7446 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7447 pf->vsi[vsi->idx]->idx,
7448 pf->vsi[vsi->idx],
7449 pf->vsi[vsi->idx]->type,
7450 vsi->idx, vsi, vsi->type);
7451 goto unlock_vsi;
7452 }
7453
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007454 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007455 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7456 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7457
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007458 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007459 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007460
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007461 pf->vsi[vsi->idx] = NULL;
7462 if (vsi->idx < pf->next_vsi)
7463 pf->next_vsi = vsi->idx;
7464
7465unlock_vsi:
7466 mutex_unlock(&pf->switch_mutex);
7467free_vsi:
7468 kfree(vsi);
7469
7470 return 0;
7471}
7472
7473/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007474 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7475 * @vsi: the VSI being cleaned
7476 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007477static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007478{
7479 int i;
7480
Greg Rose8e9dca52013-12-18 13:45:53 +00007481 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007482 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007483 kfree_rcu(vsi->tx_rings[i], rcu);
7484 vsi->tx_rings[i] = NULL;
7485 vsi->rx_rings[i] = NULL;
7486 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007487 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007488}
7489
7490/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007491 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7492 * @vsi: the VSI being configured
7493 **/
7494static int i40e_alloc_rings(struct i40e_vsi *vsi)
7495{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007496 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007497 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007498 int i;
7499
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007500 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007501 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007502 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007503 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7504 if (!tx_ring)
7505 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007506
7507 tx_ring->queue_index = i;
7508 tx_ring->reg_idx = vsi->base_queue + i;
7509 tx_ring->ring_active = false;
7510 tx_ring->vsi = vsi;
7511 tx_ring->netdev = vsi->netdev;
7512 tx_ring->dev = &pf->pdev->dev;
7513 tx_ring->count = vsi->num_desc;
7514 tx_ring->size = 0;
7515 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007516 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7517 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007518 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007519 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007520
Alexander Duyck9f65e152013-09-28 06:00:58 +00007521 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007522 rx_ring->queue_index = i;
7523 rx_ring->reg_idx = vsi->base_queue + i;
7524 rx_ring->ring_active = false;
7525 rx_ring->vsi = vsi;
7526 rx_ring->netdev = vsi->netdev;
7527 rx_ring->dev = &pf->pdev->dev;
7528 rx_ring->count = vsi->num_desc;
7529 rx_ring->size = 0;
7530 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007531 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007532 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007533 }
7534
7535 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007536
7537err_out:
7538 i40e_vsi_clear_rings(vsi);
7539 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007540}
7541
7542/**
7543 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7544 * @pf: board private structure
7545 * @vectors: the number of MSI-X vectors to request
7546 *
7547 * Returns the number of vectors reserved, or error
7548 **/
7549static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7550{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007551 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7552 I40E_MIN_MSIX, vectors);
7553 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007554 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007555 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007556 vectors = 0;
7557 }
7558
7559 return vectors;
7560}
7561
7562/**
7563 * i40e_init_msix - Setup the MSIX capability
7564 * @pf: board private structure
7565 *
7566 * Work with the OS to set up the MSIX vectors needed.
7567 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007568 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007569 **/
7570static int i40e_init_msix(struct i40e_pf *pf)
7571{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007572 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007573 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007574 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007575 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007576 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577
7578 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7579 return -ENODEV;
7580
7581 /* The number of vectors we'll request will be comprised of:
7582 * - Add 1 for "other" cause for Admin Queue events, etc.
7583 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007584 * - Queues being used for RSS.
7585 * We don't need as many as max_rss_size vectors.
7586 * use rss_size instead in the calculation since that
7587 * is governed by number of cpus in the system.
7588 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007589 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007590 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007591#ifdef I40E_FCOE
7592 * - The number of FCOE qps.
7593#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007594 * Once we count this up, try the request.
7595 *
7596 * If we can't get what we want, we'll simplify to nearly nothing
7597 * and try again. If that still fails, we punt.
7598 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007599 vectors_left = hw->func_caps.num_msix_vectors;
7600 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007601
Shannon Nelson1e200e42015-02-27 09:15:24 +00007602 /* reserve one vector for miscellaneous handler */
7603 if (vectors_left) {
7604 v_budget++;
7605 vectors_left--;
7606 }
7607
7608 /* reserve vectors for the main PF traffic queues */
7609 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7610 vectors_left -= pf->num_lan_msix;
7611 v_budget += pf->num_lan_msix;
7612
7613 /* reserve one vector for sideband flow director */
7614 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7615 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007616 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007617 v_budget++;
7618 vectors_left--;
7619 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007620 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007621 }
7622 }
John W Linville83840e42015-01-14 03:06:28 +00007623
Vasu Dev38e00432014-08-01 13:27:03 -07007624#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007625 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007626 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007627 if (!vectors_left)
7628 pf->num_fcoe_msix = 0;
7629 else if (vectors_left >= pf->num_fcoe_qps)
7630 pf->num_fcoe_msix = pf->num_fcoe_qps;
7631 else
7632 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007633 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007634 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007635 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007636
Vasu Dev38e00432014-08-01 13:27:03 -07007637#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007638 /* can we reserve enough for iWARP? */
7639 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007640 iwarp_requested = pf->num_iwarp_msix;
7641
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007642 if (!vectors_left)
7643 pf->num_iwarp_msix = 0;
7644 else if (vectors_left < pf->num_iwarp_msix)
7645 pf->num_iwarp_msix = 1;
7646 v_budget += pf->num_iwarp_msix;
7647 vectors_left -= pf->num_iwarp_msix;
7648 }
7649
Shannon Nelson1e200e42015-02-27 09:15:24 +00007650 /* any vectors left over go for VMDq support */
7651 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7652 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7653 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7654
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007655 if (!vectors_left) {
7656 pf->num_vmdq_msix = 0;
7657 pf->num_vmdq_qps = 0;
7658 } else {
7659 /* if we're short on vectors for what's desired, we limit
7660 * the queues per vmdq. If this is still more than are
7661 * available, the user will need to change the number of
7662 * queues/vectors used by the PF later with the ethtool
7663 * channels command
7664 */
7665 if (vmdq_vecs < vmdq_vecs_wanted)
7666 pf->num_vmdq_qps = 1;
7667 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007668
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007669 v_budget += vmdq_vecs;
7670 vectors_left -= vmdq_vecs;
7671 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007672 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007673
7674 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7675 GFP_KERNEL);
7676 if (!pf->msix_entries)
7677 return -ENOMEM;
7678
7679 for (i = 0; i < v_budget; i++)
7680 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007681 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007682
Shannon Nelson3b444392015-02-26 16:15:57 +00007683 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007684 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7685 kfree(pf->msix_entries);
7686 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007687 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007688 return -ENODEV;
7689
Shannon Nelson3b444392015-02-26 16:15:57 +00007690 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007691 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007692 pf->num_vmdq_vsis = 0;
7693 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007694 pf->num_lan_qps = 1;
7695 pf->num_lan_msix = 1;
7696
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007697 } else if (!vectors_left) {
7698 /* If we have limited resources, we will start with no vectors
7699 * for the special features and then allocate vectors to some
7700 * of these features based on the policy and at the end disable
7701 * the features that did not get any vectors.
7702 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007703 int vec;
7704
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007705 dev_info(&pf->pdev->dev,
7706 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007707 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007708 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007710 /* Scale vector usage down */
7711 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007712 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007713 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007714#ifdef I40E_FCOE
7715 pf->num_fcoe_qps = 0;
7716 pf->num_fcoe_msix = 0;
7717#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007718
7719 /* partition out the remaining vectors */
7720 switch (vec) {
7721 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007722 pf->num_lan_msix = 1;
7723 break;
7724 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007725 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7726 pf->num_lan_msix = 1;
7727 pf->num_iwarp_msix = 1;
7728 } else {
7729 pf->num_lan_msix = 2;
7730 }
Vasu Dev38e00432014-08-01 13:27:03 -07007731#ifdef I40E_FCOE
7732 /* give one vector to FCoE */
7733 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7734 pf->num_lan_msix = 1;
7735 pf->num_fcoe_msix = 1;
7736 }
Vasu Dev38e00432014-08-01 13:27:03 -07007737#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007738 break;
7739 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007740 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7741 pf->num_iwarp_msix = min_t(int, (vec / 3),
7742 iwarp_requested);
7743 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7744 I40E_DEFAULT_NUM_VMDQ_VSI);
7745 } else {
7746 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7747 I40E_DEFAULT_NUM_VMDQ_VSI);
7748 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007749 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7750 pf->num_fdsb_msix = 1;
7751 vec--;
7752 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007753 pf->num_lan_msix = min_t(int,
7754 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7755 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007756 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007757#ifdef I40E_FCOE
7758 /* give one vector to FCoE */
7759 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7760 pf->num_fcoe_msix = 1;
7761 vec--;
7762 }
7763#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007764 break;
7765 }
7766 }
7767
Stefan Assmannabd97a92016-09-19 13:37:51 +02007768 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7769 (pf->num_fdsb_msix == 0)) {
7770 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7771 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7772 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007773 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7774 (pf->num_vmdq_msix == 0)) {
7775 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7776 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7777 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007778
7779 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7780 (pf->num_iwarp_msix == 0)) {
7781 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7782 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7783 }
Vasu Dev38e00432014-08-01 13:27:03 -07007784#ifdef I40E_FCOE
7785
7786 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7787 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7788 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7789 }
7790#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007791 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7792 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7793 pf->num_lan_msix,
7794 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7795 pf->num_fdsb_msix,
7796 pf->num_iwarp_msix);
7797
Shannon Nelson3b444392015-02-26 16:15:57 +00007798 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007799}
7800
7801/**
Greg Rose90e04072014-03-06 08:59:57 +00007802 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007803 * @vsi: the VSI being configured
7804 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007805 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007806 *
7807 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7808 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007809static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007810{
7811 struct i40e_q_vector *q_vector;
7812
7813 /* allocate q_vector */
7814 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7815 if (!q_vector)
7816 return -ENOMEM;
7817
7818 q_vector->vsi = vsi;
7819 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007820 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7821
Alexander Duyck493fb302013-09-28 07:01:44 +00007822 if (vsi->netdev)
7823 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007824 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007825
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007826 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7827 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7828
Alexander Duyck493fb302013-09-28 07:01:44 +00007829 /* tie q_vector and vsi together */
7830 vsi->q_vectors[v_idx] = q_vector;
7831
7832 return 0;
7833}
7834
7835/**
Greg Rose90e04072014-03-06 08:59:57 +00007836 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837 * @vsi: the VSI being configured
7838 *
7839 * We allocate one q_vector per queue interrupt. If allocation fails we
7840 * return -ENOMEM.
7841 **/
Greg Rose90e04072014-03-06 08:59:57 +00007842static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007843{
7844 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007845 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007846
7847 /* if not MSIX, give the one vector only to the LAN VSI */
7848 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7849 num_q_vectors = vsi->num_q_vectors;
7850 else if (vsi == pf->vsi[pf->lan_vsi])
7851 num_q_vectors = 1;
7852 else
7853 return -EINVAL;
7854
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007855 current_cpu = cpumask_first(cpu_online_mask);
7856
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007857 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007858 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007859 if (err)
7860 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007861 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7862 if (unlikely(current_cpu >= nr_cpu_ids))
7863 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007864 }
7865
7866 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007867
7868err_out:
7869 while (v_idx--)
7870 i40e_free_q_vector(vsi, v_idx);
7871
7872 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007873}
7874
7875/**
7876 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7877 * @pf: board private structure to initialize
7878 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007879static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007880{
Shannon Nelson3b444392015-02-26 16:15:57 +00007881 int vectors = 0;
7882 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007883
7884 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007885 vectors = i40e_init_msix(pf);
7886 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007887 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007888 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007889#ifdef I40E_FCOE
7890 I40E_FLAG_FCOE_ENABLED |
7891#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007892 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007893 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007894 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007895 I40E_FLAG_SRIOV_ENABLED |
7896 I40E_FLAG_FD_SB_ENABLED |
7897 I40E_FLAG_FD_ATR_ENABLED |
7898 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007899
7900 /* rework the queue expectations without MSIX */
7901 i40e_determine_queue_usage(pf);
7902 }
7903 }
7904
7905 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7906 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007907 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007908 vectors = pci_enable_msi(pf->pdev);
7909 if (vectors < 0) {
7910 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7911 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007912 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7913 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007914 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007915 }
7916
Shannon Nelson958a3e32013-09-28 07:13:28 +00007917 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007918 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007919
Shannon Nelson3b444392015-02-26 16:15:57 +00007920 /* set up vector assignment tracking */
7921 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7922 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007923 if (!pf->irq_pile) {
7924 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7925 return -ENOMEM;
7926 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007927 pf->irq_pile->num_entries = vectors;
7928 pf->irq_pile->search_hint = 0;
7929
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007930 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007931 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007932
7933 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007934}
7935
7936/**
7937 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7938 * @pf: board private structure
7939 *
7940 * This sets up the handler for MSIX 0, which is used to manage the
7941 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7942 * when in MSI or Legacy interrupt mode.
7943 **/
7944static int i40e_setup_misc_vector(struct i40e_pf *pf)
7945{
7946 struct i40e_hw *hw = &pf->hw;
7947 int err = 0;
7948
7949 /* Only request the irq if this is the first time through, and
7950 * not when we're rebuilding after a Reset
7951 */
7952 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7953 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007954 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007955 if (err) {
7956 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007957 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007958 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007959 return -EFAULT;
7960 }
7961 }
7962
Jacob Kellerab437b52014-12-14 01:55:08 +00007963 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007964
7965 /* associate no queues to the misc vector */
7966 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7967 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7968
7969 i40e_flush(hw);
7970
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007971 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007972
7973 return err;
7974}
7975
7976/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007977 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7978 * @vsi: vsi structure
7979 * @seed: RSS hash seed
7980 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007981static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7982 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007983{
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007984 struct i40e_pf *pf = vsi->back;
7985 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07007986 int ret = 0;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007987
Jacob Keller776b2e12016-07-19 16:23:30 -07007988 if (seed) {
7989 struct i40e_aqc_get_set_rss_key_data *seed_dw =
7990 (struct i40e_aqc_get_set_rss_key_data *)seed;
7991 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
7992 if (ret) {
7993 dev_info(&pf->pdev->dev,
7994 "Cannot set RSS key, err %s aq_err %s\n",
7995 i40e_stat_str(hw, ret),
7996 i40e_aq_str(hw, hw->aq.asq_last_status));
7997 return ret;
7998 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04007999 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008000 if (lut) {
8001 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008002
Jacob Keller776b2e12016-07-19 16:23:30 -07008003 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8004 if (ret) {
8005 dev_info(&pf->pdev->dev,
8006 "Cannot set RSS lut, err %s aq_err %s\n",
8007 i40e_stat_str(hw, ret),
8008 i40e_aq_str(hw, hw->aq.asq_last_status));
8009 return ret;
8010 }
8011 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008012 return ret;
8013}
8014
8015/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008016 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8017 * @vsi: Pointer to vsi structure
8018 * @seed: Buffter to store the hash keys
8019 * @lut: Buffer to store the lookup table entries
8020 * @lut_size: Size of buffer to store the lookup table entries
8021 *
8022 * Return 0 on success, negative on failure
8023 */
8024static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8025 u8 *lut, u16 lut_size)
8026{
8027 struct i40e_pf *pf = vsi->back;
8028 struct i40e_hw *hw = &pf->hw;
8029 int ret = 0;
8030
8031 if (seed) {
8032 ret = i40e_aq_get_rss_key(hw, vsi->id,
8033 (struct i40e_aqc_get_set_rss_key_data *)seed);
8034 if (ret) {
8035 dev_info(&pf->pdev->dev,
8036 "Cannot get RSS key, err %s aq_err %s\n",
8037 i40e_stat_str(&pf->hw, ret),
8038 i40e_aq_str(&pf->hw,
8039 pf->hw.aq.asq_last_status));
8040 return ret;
8041 }
8042 }
8043
8044 if (lut) {
8045 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8046
8047 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8048 if (ret) {
8049 dev_info(&pf->pdev->dev,
8050 "Cannot get RSS lut, err %s aq_err %s\n",
8051 i40e_stat_str(&pf->hw, ret),
8052 i40e_aq_str(&pf->hw,
8053 pf->hw.aq.asq_last_status));
8054 return ret;
8055 }
8056 }
8057
8058 return ret;
8059}
8060
8061/**
Jacob Keller0582b962016-07-19 16:23:29 -07008062 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8063 * @vsi: VSI structure
8064 **/
8065static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8066{
8067 u8 seed[I40E_HKEY_ARRAY_SIZE];
8068 struct i40e_pf *pf = vsi->back;
8069 u8 *lut;
8070 int ret;
8071
8072 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8073 return 0;
8074
Jacob Keller552b9962016-07-19 16:23:31 -07008075 if (!vsi->rss_size)
8076 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8077 vsi->num_queue_pairs);
8078 if (!vsi->rss_size)
8079 return -EINVAL;
8080
Jacob Keller0582b962016-07-19 16:23:29 -07008081 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8082 if (!lut)
8083 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008084 /* Use the user configured hash keys and lookup table if there is one,
8085 * otherwise use default
8086 */
8087 if (vsi->rss_lut_user)
8088 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8089 else
8090 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8091 if (vsi->rss_hkey_user)
8092 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8093 else
8094 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008095 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8096 kfree(lut);
8097
8098 return ret;
8099}
8100
8101/**
Helin Zhang043dd652015-10-21 19:56:23 -04008102 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008103 * @vsi: Pointer to vsi structure
8104 * @seed: RSS hash seed
8105 * @lut: Lookup table
8106 * @lut_size: Lookup table size
8107 *
8108 * Returns 0 on success, negative on failure
8109 **/
8110static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8111 const u8 *lut, u16 lut_size)
8112{
8113 struct i40e_pf *pf = vsi->back;
8114 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008115 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008116 u8 i;
8117
8118 /* Fill out hash function seed */
8119 if (seed) {
8120 u32 *seed_dw = (u32 *)seed;
8121
Mitch Williamsc4e18682016-04-12 08:30:40 -07008122 if (vsi->type == I40E_VSI_MAIN) {
8123 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8124 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8125 seed_dw[i]);
8126 } else if (vsi->type == I40E_VSI_SRIOV) {
8127 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8128 i40e_write_rx_ctl(hw,
8129 I40E_VFQF_HKEY1(i, vf_id),
8130 seed_dw[i]);
8131 } else {
8132 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8133 }
Helin Zhange69ff812015-10-21 19:56:22 -04008134 }
8135
8136 if (lut) {
8137 u32 *lut_dw = (u32 *)lut;
8138
Mitch Williamsc4e18682016-04-12 08:30:40 -07008139 if (vsi->type == I40E_VSI_MAIN) {
8140 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8141 return -EINVAL;
8142 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8143 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8144 } else if (vsi->type == I40E_VSI_SRIOV) {
8145 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8146 return -EINVAL;
8147 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8148 i40e_write_rx_ctl(hw,
8149 I40E_VFQF_HLUT1(i, vf_id),
8150 lut_dw[i]);
8151 } else {
8152 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8153 }
Helin Zhange69ff812015-10-21 19:56:22 -04008154 }
8155 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008156
8157 return 0;
8158}
8159
8160/**
Helin Zhang043dd652015-10-21 19:56:23 -04008161 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8162 * @vsi: Pointer to VSI structure
8163 * @seed: Buffer to store the keys
8164 * @lut: Buffer to store the lookup table entries
8165 * @lut_size: Size of buffer to store the lookup table entries
8166 *
8167 * Returns 0 on success, negative on failure
8168 */
8169static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8170 u8 *lut, u16 lut_size)
8171{
8172 struct i40e_pf *pf = vsi->back;
8173 struct i40e_hw *hw = &pf->hw;
8174 u16 i;
8175
8176 if (seed) {
8177 u32 *seed_dw = (u32 *)seed;
8178
8179 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008180 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008181 }
8182 if (lut) {
8183 u32 *lut_dw = (u32 *)lut;
8184
8185 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8186 return -EINVAL;
8187 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8188 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8189 }
8190
8191 return 0;
8192}
8193
8194/**
8195 * i40e_config_rss - Configure RSS keys and lut
8196 * @vsi: Pointer to VSI structure
8197 * @seed: RSS hash seed
8198 * @lut: Lookup table
8199 * @lut_size: Lookup table size
8200 *
8201 * Returns 0 on success, negative on failure
8202 */
8203int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8204{
8205 struct i40e_pf *pf = vsi->back;
8206
8207 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8208 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8209 else
8210 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8211}
8212
8213/**
8214 * i40e_get_rss - Get RSS keys and lut
8215 * @vsi: Pointer to VSI structure
8216 * @seed: Buffer to store the keys
8217 * @lut: Buffer to store the lookup table entries
8218 * lut_size: Size of buffer to store the lookup table entries
8219 *
8220 * Returns 0 on success, negative on failure
8221 */
8222int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8223{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008224 struct i40e_pf *pf = vsi->back;
8225
8226 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8227 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8228 else
8229 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008230}
8231
8232/**
Helin Zhange69ff812015-10-21 19:56:22 -04008233 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8234 * @pf: Pointer to board private structure
8235 * @lut: Lookup table
8236 * @rss_table_size: Lookup table size
8237 * @rss_size: Range of queue number for hashing
8238 */
Alan Bradyf1582352016-08-24 11:33:46 -07008239void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8240 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008241{
Helin Zhange69ff812015-10-21 19:56:22 -04008242 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008243
Helin Zhange69ff812015-10-21 19:56:22 -04008244 for (i = 0; i < rss_table_size; i++)
8245 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008246}
8247
8248/**
Helin Zhang043dd652015-10-21 19:56:23 -04008249 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008250 * @pf: board private structure
8251 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008252static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008253{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008254 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008255 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008256 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008257 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008258 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008259 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008260 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008261
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008262 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008263 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8264 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008265 hena |= i40e_pf_get_default_rss_hena(pf);
8266
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008267 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8268 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008269
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008270 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008271 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008272 reg_val = (pf->rss_table_size == 512) ?
8273 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8274 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008275 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008276
Helin Zhang28c58692015-10-26 19:44:27 -04008277 /* Determine the RSS size of the VSI */
8278 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008279 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8280 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008281 if (!vsi->rss_size)
8282 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008283
Helin Zhange69ff812015-10-21 19:56:22 -04008284 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8285 if (!lut)
8286 return -ENOMEM;
8287
Helin Zhang28c58692015-10-26 19:44:27 -04008288 /* Use user configured lut if there is one, otherwise use default */
8289 if (vsi->rss_lut_user)
8290 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8291 else
8292 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008293
Helin Zhang28c58692015-10-26 19:44:27 -04008294 /* Use user configured hash key if there is one, otherwise
8295 * use default.
8296 */
8297 if (vsi->rss_hkey_user)
8298 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8299 else
8300 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008301 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008302 kfree(lut);
8303
8304 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008305}
8306
8307/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008308 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8309 * @pf: board private structure
8310 * @queue_count: the requested queue count for rss.
8311 *
8312 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8313 * count which may be different from the requested queue count.
8314 **/
8315int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8316{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008317 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8318 int new_rss_size;
8319
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008320 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8321 return 0;
8322
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008323 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008324
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008325 if (queue_count != vsi->num_queue_pairs) {
8326 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008327 i40e_prep_for_reset(pf);
8328
Helin Zhangacd65442015-10-26 19:44:28 -04008329 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008330
8331 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008332
8333 /* Discard the user configured hash keys and lut, if less
8334 * queues are enabled.
8335 */
8336 if (queue_count < vsi->rss_size) {
8337 i40e_clear_rss_config_user(vsi);
8338 dev_dbg(&pf->pdev->dev,
8339 "discard user configured hash keys and lut\n");
8340 }
8341
8342 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008343 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8344 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008345
Helin Zhang043dd652015-10-21 19:56:23 -04008346 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008347 }
Helin Zhange36b0b12015-10-26 19:44:36 -04008348 dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
8349 pf->alloc_rss_size, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008350 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008351}
8352
8353/**
Greg Rosef4492db2015-02-06 08:52:12 +00008354 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8355 * @pf: board private structure
8356 **/
8357i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8358{
8359 i40e_status status;
8360 bool min_valid, max_valid;
8361 u32 max_bw, min_bw;
8362
8363 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8364 &min_valid, &max_valid);
8365
8366 if (!status) {
8367 if (min_valid)
8368 pf->npar_min_bw = min_bw;
8369 if (max_valid)
8370 pf->npar_max_bw = max_bw;
8371 }
8372
8373 return status;
8374}
8375
8376/**
8377 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8378 * @pf: board private structure
8379 **/
8380i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8381{
8382 struct i40e_aqc_configure_partition_bw_data bw_data;
8383 i40e_status status;
8384
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008385 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008386 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008387 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8388 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8389
8390 /* Set the new bandwidths */
8391 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8392
8393 return status;
8394}
8395
8396/**
8397 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8398 * @pf: board private structure
8399 **/
8400i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8401{
8402 /* Commit temporary BW setting to permanent NVM image */
8403 enum i40e_admin_queue_err last_aq_status;
8404 i40e_status ret;
8405 u16 nvm_word;
8406
8407 if (pf->hw.partition_id != 1) {
8408 dev_info(&pf->pdev->dev,
8409 "Commit BW only works on partition 1! This is partition %d",
8410 pf->hw.partition_id);
8411 ret = I40E_NOT_SUPPORTED;
8412 goto bw_commit_out;
8413 }
8414
8415 /* Acquire NVM for read access */
8416 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8417 last_aq_status = pf->hw.aq.asq_last_status;
8418 if (ret) {
8419 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008420 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8421 i40e_stat_str(&pf->hw, ret),
8422 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008423 goto bw_commit_out;
8424 }
8425
8426 /* Read word 0x10 of NVM - SW compatibility word 1 */
8427 ret = i40e_aq_read_nvm(&pf->hw,
8428 I40E_SR_NVM_CONTROL_WORD,
8429 0x10, sizeof(nvm_word), &nvm_word,
8430 false, NULL);
8431 /* Save off last admin queue command status before releasing
8432 * the NVM
8433 */
8434 last_aq_status = pf->hw.aq.asq_last_status;
8435 i40e_release_nvm(&pf->hw);
8436 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008437 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8438 i40e_stat_str(&pf->hw, ret),
8439 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008440 goto bw_commit_out;
8441 }
8442
8443 /* Wait a bit for NVM release to complete */
8444 msleep(50);
8445
8446 /* Acquire NVM for write access */
8447 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8448 last_aq_status = pf->hw.aq.asq_last_status;
8449 if (ret) {
8450 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008451 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8452 i40e_stat_str(&pf->hw, ret),
8453 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008454 goto bw_commit_out;
8455 }
8456 /* Write it back out unchanged to initiate update NVM,
8457 * which will force a write of the shadow (alt) RAM to
8458 * the NVM - thus storing the bandwidth values permanently.
8459 */
8460 ret = i40e_aq_update_nvm(&pf->hw,
8461 I40E_SR_NVM_CONTROL_WORD,
8462 0x10, sizeof(nvm_word),
8463 &nvm_word, true, NULL);
8464 /* Save off last admin queue command status before releasing
8465 * the NVM
8466 */
8467 last_aq_status = pf->hw.aq.asq_last_status;
8468 i40e_release_nvm(&pf->hw);
8469 if (ret)
8470 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008471 "BW settings NOT SAVED, err %s aq_err %s\n",
8472 i40e_stat_str(&pf->hw, ret),
8473 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008474bw_commit_out:
8475
8476 return ret;
8477}
8478
8479/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008480 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8481 * @pf: board private structure to initialize
8482 *
8483 * i40e_sw_init initializes the Adapter private data structure.
8484 * Fields are initialized based on PCI device information and
8485 * OS network device settings (MTU size).
8486 **/
8487static int i40e_sw_init(struct i40e_pf *pf)
8488{
8489 int err = 0;
8490 int size;
8491
8492 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8493 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
8494 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8495 if (I40E_DEBUG_USER & debug)
8496 pf->hw.debug_mask = debug;
8497 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8498 I40E_DEFAULT_MSG_ENABLE);
8499 }
8500
8501 /* Set default capability flags */
8502 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8503 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008504 I40E_FLAG_MSIX_ENABLED;
8505
Mitch Williamsca99eb92014-04-04 04:43:07 +00008506 /* Set default ITR */
8507 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8508 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8509
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008510 /* Depending on PF configurations, it is possible that the RSS
8511 * maximum might end up larger than the available queues
8512 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008513 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008514 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008515 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008516 pf->rss_size_max = min_t(int, pf->rss_size_max,
8517 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008518 if (pf->hw.func_caps.rss) {
8519 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008520 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8521 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008522 }
8523
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008524 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008525 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008526 pf->flags |= I40E_FLAG_MFP_ENABLED;
8527 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008528 if (i40e_get_npar_bw_setting(pf))
8529 dev_warn(&pf->pdev->dev,
8530 "Could not get NPAR bw settings\n");
8531 else
8532 dev_info(&pf->pdev->dev,
8533 "Min BW = %8.8x, Max BW = %8.8x\n",
8534 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008535 }
8536
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008537 /* FW/NVM is not yet fixed in this regard */
8538 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8539 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8540 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8541 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008542 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8543 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008544 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008545 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008546 else
8547 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008548 pf->fdir_pf_filter_count =
8549 pf->hw.func_caps.fd_filters_guaranteed;
8550 pf->hw.fdir_shared_filter_count =
8551 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008552 }
8553
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008554 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008555 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008556 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008557 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008558 /* No DCB support for FW < v4.33 */
8559 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8560 }
8561
8562 /* Disable FW LLDP if FW < v4.3 */
8563 if (i40e_is_mac_710(&pf->hw) &&
8564 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8565 (pf->hw.aq.fw_maj_ver < 4)))
8566 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8567
8568 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8569 if (i40e_is_mac_710(&pf->hw) &&
8570 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8571 (pf->hw.aq.fw_maj_ver >= 5)))
8572 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008573
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008574 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008575 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008576 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008577 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008578 }
8579
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008580 if (pf->hw.func_caps.iwarp) {
8581 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8582 /* IWARP needs one extra vector for CQP just like MISC.*/
8583 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8584 }
8585
Vasu Dev38e00432014-08-01 13:27:03 -07008586#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008587 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008588
8589#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008590#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008591 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008592 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8593 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8594 pf->num_req_vfs = min_t(int,
8595 pf->hw.func_caps.num_vfs,
8596 I40E_MAX_VF_COUNT);
8597 }
8598#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008599 if (pf->hw.mac.type == I40E_MAC_X722) {
8600 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8601 I40E_FLAG_128_QP_RSS_CAPABLE |
8602 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8603 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8604 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008605 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008606 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008607 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008608 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008609 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8610 ((pf->hw.aq.api_maj_ver == 1) &&
8611 (pf->hw.aq.api_min_ver > 4))) {
8612 /* Supported in FW API version higher than 1.4 */
8613 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008614 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8615 } else {
8616 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008617 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008618
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008619 pf->eeprom_version = 0xDEAD;
8620 pf->lan_veb = I40E_NO_VEB;
8621 pf->lan_vsi = I40E_NO_VSI;
8622
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008623 /* By default FW has this off for performance reasons */
8624 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8625
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008626 /* set up queue assignment tracking */
8627 size = sizeof(struct i40e_lump_tracking)
8628 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8629 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8630 if (!pf->qp_pile) {
8631 err = -ENOMEM;
8632 goto sw_init_done;
8633 }
8634 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8635 pf->qp_pile->search_hint = 0;
8636
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008637 pf->tx_timeout_recovery_level = 1;
8638
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008639 mutex_init(&pf->switch_mutex);
8640
Greg Rosec668a122015-02-26 16:10:39 +00008641 /* If NPAR is enabled nudge the Tx scheduler */
8642 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8643 i40e_set_npar_bw_setting(pf);
8644
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008645sw_init_done:
8646 return err;
8647}
8648
8649/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008650 * i40e_set_ntuple - set the ntuple feature flag and take action
8651 * @pf: board private structure to initialize
8652 * @features: the feature set that the stack is suggesting
8653 *
8654 * returns a bool to indicate if reset needs to happen
8655 **/
8656bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8657{
8658 bool need_reset = false;
8659
8660 /* Check if Flow Director n-tuple support was enabled or disabled. If
8661 * the state changed, we need to reset.
8662 */
8663 if (features & NETIF_F_NTUPLE) {
8664 /* Enable filters and mark for reset */
8665 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8666 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008667 /* enable FD_SB only if there is MSI-X vector */
8668 if (pf->num_fdsb_msix > 0)
8669 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008670 } else {
8671 /* turn off filters, mark for reset and clear SW filter list */
8672 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8673 need_reset = true;
8674 i40e_fdir_filter_exit(pf);
8675 }
8676 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008677 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008678 /* reset fd counters */
8679 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8680 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008681 /* if ATR was auto disabled it can be re-enabled. */
8682 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008683 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008684 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008685 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8686 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8687 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008688 }
8689 return need_reset;
8690}
8691
8692/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008693 * i40e_clear_rss_lut - clear the rx hash lookup table
8694 * @vsi: the VSI being configured
8695 **/
8696static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8697{
8698 struct i40e_pf *pf = vsi->back;
8699 struct i40e_hw *hw = &pf->hw;
8700 u16 vf_id = vsi->vf_id;
8701 u8 i;
8702
8703 if (vsi->type == I40E_VSI_MAIN) {
8704 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8705 wr32(hw, I40E_PFQF_HLUT(i), 0);
8706 } else if (vsi->type == I40E_VSI_SRIOV) {
8707 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8708 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8709 } else {
8710 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8711 }
8712}
8713
8714/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008715 * i40e_set_features - set the netdev feature flags
8716 * @netdev: ptr to the netdev being adjusted
8717 * @features: the feature set that the stack is suggesting
8718 **/
8719static int i40e_set_features(struct net_device *netdev,
8720 netdev_features_t features)
8721{
8722 struct i40e_netdev_priv *np = netdev_priv(netdev);
8723 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008724 struct i40e_pf *pf = vsi->back;
8725 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008726
Alan Bradyd8ec9862016-07-27 12:02:38 -07008727 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8728 i40e_pf_config_rss(pf);
8729 else if (!(features & NETIF_F_RXHASH) &&
8730 netdev->features & NETIF_F_RXHASH)
8731 i40e_clear_rss_lut(vsi);
8732
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008733 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8734 i40e_vlan_stripping_enable(vsi);
8735 else
8736 i40e_vlan_stripping_disable(vsi);
8737
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008738 need_reset = i40e_set_ntuple(pf, features);
8739
8740 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008741 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008742
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008743 return 0;
8744}
8745
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008746/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008747 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008748 * @pf: board private structure
8749 * @port: The UDP port to look up
8750 *
8751 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8752 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008753static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008754{
8755 u8 i;
8756
8757 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008758 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008759 return i;
8760 }
8761
8762 return i;
8763}
8764
8765/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008766 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008767 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008768 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008769 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008770static void i40e_udp_tunnel_add(struct net_device *netdev,
8771 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008772{
8773 struct i40e_netdev_priv *np = netdev_priv(netdev);
8774 struct i40e_vsi *vsi = np->vsi;
8775 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008776 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008777 u8 next_idx;
8778 u8 idx;
8779
Singhai, Anjali6a899022015-12-14 12:21:18 -08008780 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008781
8782 /* Check if port already exists */
8783 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008784 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008785 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008786 return;
8787 }
8788
8789 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008790 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008791
8792 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008793 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008794 ntohs(port));
8795 return;
8796 }
8797
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008798 switch (ti->type) {
8799 case UDP_TUNNEL_TYPE_VXLAN:
8800 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8801 break;
8802 case UDP_TUNNEL_TYPE_GENEVE:
8803 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8804 return;
8805 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8806 break;
8807 default:
8808 return;
8809 }
8810
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008811 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008812 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008813 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8814 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008815}
8816
8817/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008818 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008819 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008820 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008821 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008822static void i40e_udp_tunnel_del(struct net_device *netdev,
8823 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008824{
8825 struct i40e_netdev_priv *np = netdev_priv(netdev);
8826 struct i40e_vsi *vsi = np->vsi;
8827 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008828 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008829 u8 idx;
8830
Singhai, Anjali6a899022015-12-14 12:21:18 -08008831 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008832
8833 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008834 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8835 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008836
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008837 switch (ti->type) {
8838 case UDP_TUNNEL_TYPE_VXLAN:
8839 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8840 goto not_found;
8841 break;
8842 case UDP_TUNNEL_TYPE_GENEVE:
8843 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8844 goto not_found;
8845 break;
8846 default:
8847 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008848 }
8849
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008850 /* if port exists, set it to 0 (mark for deletion)
8851 * and make it pending
8852 */
8853 pf->udp_ports[idx].index = 0;
8854 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008855 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8856
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008857 return;
8858not_found:
8859 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8860 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008861}
8862
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008863static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008864 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008865{
8866 struct i40e_netdev_priv *np = netdev_priv(netdev);
8867 struct i40e_pf *pf = np->vsi->back;
8868 struct i40e_hw *hw = &pf->hw;
8869
8870 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8871 return -EOPNOTSUPP;
8872
8873 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8874 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8875
8876 return 0;
8877}
8878
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008879/**
8880 * i40e_ndo_fdb_add - add an entry to the hardware database
8881 * @ndm: the input from the stack
8882 * @tb: pointer to array of nladdr (unused)
8883 * @dev: the net device pointer
8884 * @addr: the MAC address entry being added
8885 * @flags: instructions from stack about fdb operation
8886 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008887static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8888 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008889 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008890 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008891{
8892 struct i40e_netdev_priv *np = netdev_priv(dev);
8893 struct i40e_pf *pf = np->vsi->back;
8894 int err = 0;
8895
8896 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8897 return -EOPNOTSUPP;
8898
Or Gerlitz65891fe2014-12-14 18:19:05 +02008899 if (vid) {
8900 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8901 return -EINVAL;
8902 }
8903
Greg Rose4ba0dea2014-03-06 08:59:55 +00008904 /* Hardware does not support aging addresses so if a
8905 * ndm_state is given only allow permanent addresses
8906 */
8907 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8908 netdev_info(dev, "FDB only supports static addresses\n");
8909 return -EINVAL;
8910 }
8911
8912 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8913 err = dev_uc_add_excl(dev, addr);
8914 else if (is_multicast_ether_addr(addr))
8915 err = dev_mc_add_excl(dev, addr);
8916 else
8917 err = -EINVAL;
8918
8919 /* Only return duplicate errors if NLM_F_EXCL is set */
8920 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8921 err = 0;
8922
8923 return err;
8924}
8925
Neerav Parikh51616012015-02-06 08:52:14 +00008926/**
8927 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8928 * @dev: the netdev being configured
8929 * @nlh: RTNL message
8930 *
8931 * Inserts a new hardware bridge if not already created and
8932 * enables the bridging mode requested (VEB or VEPA). If the
8933 * hardware bridge has already been inserted and the request
8934 * is to change the mode then that requires a PF reset to
8935 * allow rebuild of the components with required hardware
8936 * bridge mode enabled.
8937 **/
8938static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008939 struct nlmsghdr *nlh,
8940 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008941{
8942 struct i40e_netdev_priv *np = netdev_priv(dev);
8943 struct i40e_vsi *vsi = np->vsi;
8944 struct i40e_pf *pf = vsi->back;
8945 struct i40e_veb *veb = NULL;
8946 struct nlattr *attr, *br_spec;
8947 int i, rem;
8948
8949 /* Only for PF VSI for now */
8950 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8951 return -EOPNOTSUPP;
8952
8953 /* Find the HW bridge for PF VSI */
8954 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8955 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8956 veb = pf->veb[i];
8957 }
8958
8959 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8960
8961 nla_for_each_nested(attr, br_spec, rem) {
8962 __u16 mode;
8963
8964 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8965 continue;
8966
8967 mode = nla_get_u16(attr);
8968 if ((mode != BRIDGE_MODE_VEPA) &&
8969 (mode != BRIDGE_MODE_VEB))
8970 return -EINVAL;
8971
8972 /* Insert a new HW bridge */
8973 if (!veb) {
8974 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8975 vsi->tc_config.enabled_tc);
8976 if (veb) {
8977 veb->bridge_mode = mode;
8978 i40e_config_bridge_mode(veb);
8979 } else {
8980 /* No Bridge HW offload available */
8981 return -ENOENT;
8982 }
8983 break;
8984 } else if (mode != veb->bridge_mode) {
8985 /* Existing HW bridge but different mode needs reset */
8986 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008987 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8988 if (mode == BRIDGE_MODE_VEB)
8989 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8990 else
8991 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8992 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008993 break;
8994 }
8995 }
8996
8997 return 0;
8998}
8999
9000/**
9001 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9002 * @skb: skb buff
9003 * @pid: process id
9004 * @seq: RTNL message seq #
9005 * @dev: the netdev being configured
9006 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009007 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009008 *
9009 * Return the mode in which the hardware bridge is operating in
9010 * i.e VEB or VEPA.
9011 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009012static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9013 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009014 u32 __always_unused filter_mask,
9015 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009016{
9017 struct i40e_netdev_priv *np = netdev_priv(dev);
9018 struct i40e_vsi *vsi = np->vsi;
9019 struct i40e_pf *pf = vsi->back;
9020 struct i40e_veb *veb = NULL;
9021 int i;
9022
9023 /* Only for PF VSI for now */
9024 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9025 return -EOPNOTSUPP;
9026
9027 /* Find the HW bridge for the PF VSI */
9028 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9029 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9030 veb = pf->veb[i];
9031 }
9032
9033 if (!veb)
9034 return 0;
9035
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009036 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07009037 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009038}
Neerav Parikh51616012015-02-06 08:52:14 +00009039
Singhai, Anjali6a899022015-12-14 12:21:18 -08009040/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9041 * inner mac plus all inner ethertypes.
9042 */
9043#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009044/**
9045 * i40e_features_check - Validate encapsulated packet conforms to limits
9046 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009047 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009048 * @features: Offload features that the stack believes apply
9049 **/
9050static netdev_features_t i40e_features_check(struct sk_buff *skb,
9051 struct net_device *dev,
9052 netdev_features_t features)
9053{
9054 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009055 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009056 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009057 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009058
9059 return features;
9060}
9061
Shannon Nelson37a29732015-02-27 09:15:19 +00009062static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009063 .ndo_open = i40e_open,
9064 .ndo_stop = i40e_close,
9065 .ndo_start_xmit = i40e_lan_xmit_frame,
9066 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9067 .ndo_set_rx_mode = i40e_set_rx_mode,
9068 .ndo_validate_addr = eth_validate_addr,
9069 .ndo_set_mac_address = i40e_set_mac,
9070 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009071 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009072 .ndo_tx_timeout = i40e_tx_timeout,
9073 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9074 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9075#ifdef CONFIG_NET_POLL_CONTROLLER
9076 .ndo_poll_controller = i40e_netpoll,
9077#endif
John Fastabende4c67342016-02-16 21:16:15 -08009078 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009079#ifdef I40E_FCOE
9080 .ndo_fcoe_enable = i40e_fcoe_enable,
9081 .ndo_fcoe_disable = i40e_fcoe_disable,
9082#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009083 .ndo_set_features = i40e_set_features,
9084 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9085 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009086 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009087 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009088 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009089 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009090 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009091 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9092 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009093 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009094 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009095 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009096 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9097 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009098};
9099
9100/**
9101 * i40e_config_netdev - Setup the netdev flags
9102 * @vsi: the VSI being configured
9103 *
9104 * Returns 0 on success, negative value on failure
9105 **/
9106static int i40e_config_netdev(struct i40e_vsi *vsi)
9107{
9108 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 |
Tom Herbert7e133182016-05-18 09:06:10 -07009134 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009135 NETIF_F_GSO_IPXIP6 |
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 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009171 i40e_rm_default_mac_filter(vsi, mac_addr);
9172 spin_lock_bh(&vsi->mac_filter_list_lock);
9173 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
9174 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009175 } else {
9176 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9177 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9178 pf->vsi[pf->lan_vsi]->netdev->name);
9179 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009180
9181 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009182 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009183 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009184 }
Kiran Patil21659032015-09-30 14:09:03 -04009185
Greg Rose9a173902014-05-22 06:32:02 +00009186 ether_addr_copy(netdev->dev_addr, mac_addr);
9187 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009188
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009189 netdev->priv_flags |= IFF_UNICAST_FLT;
9190 netdev->priv_flags |= IFF_SUPP_NOFCS;
9191 /* Setup netdev TC information */
9192 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9193
9194 netdev->netdev_ops = &i40e_netdev_ops;
9195 netdev->watchdog_timeo = 5 * HZ;
9196 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009197#ifdef I40E_FCOE
9198 i40e_fcoe_config_netdev(netdev, vsi);
9199#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009200
9201 return 0;
9202}
9203
9204/**
9205 * i40e_vsi_delete - Delete a VSI from the switch
9206 * @vsi: the VSI being removed
9207 *
9208 * Returns 0 on success, negative value on failure
9209 **/
9210static void i40e_vsi_delete(struct i40e_vsi *vsi)
9211{
9212 /* remove default VSI is not allowed */
9213 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9214 return;
9215
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009216 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009217}
9218
9219/**
Neerav Parikh51616012015-02-06 08:52:14 +00009220 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9221 * @vsi: the VSI being queried
9222 *
9223 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9224 **/
9225int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9226{
9227 struct i40e_veb *veb;
9228 struct i40e_pf *pf = vsi->back;
9229
9230 /* Uplink is not a bridge so default to VEB */
9231 if (vsi->veb_idx == I40E_NO_VEB)
9232 return 1;
9233
9234 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009235 if (!veb) {
9236 dev_info(&pf->pdev->dev,
9237 "There is no veb associated with the bridge\n");
9238 return -ENOENT;
9239 }
Neerav Parikh51616012015-02-06 08:52:14 +00009240
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009241 /* Uplink is a bridge in VEPA mode */
9242 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9243 return 0;
9244 } else {
9245 /* Uplink is a bridge in VEB mode */
9246 return 1;
9247 }
9248
9249 /* VEPA is now default bridge, so return 0 */
9250 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009251}
9252
9253/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009254 * i40e_add_vsi - Add a VSI to the switch
9255 * @vsi: the VSI being configured
9256 *
9257 * This initializes a VSI context depending on the VSI type to be added and
9258 * passes it down to the add_vsi aq command.
9259 **/
9260static int i40e_add_vsi(struct i40e_vsi *vsi)
9261{
9262 int ret = -ENODEV;
Kiran Patilf6bd0962016-06-20 09:10:34 -07009263 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009264 struct i40e_pf *pf = vsi->back;
9265 struct i40e_hw *hw = &pf->hw;
9266 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009267 struct i40e_mac_filter *f, *ftmp;
9268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009269 u8 enabled_tc = 0x1; /* TC0 enabled */
9270 int f_count = 0;
9271
9272 memset(&ctxt, 0, sizeof(ctxt));
9273 switch (vsi->type) {
9274 case I40E_VSI_MAIN:
9275 /* The PF's main VSI is already setup as part of the
9276 * device initialization, so we'll not bother with
9277 * the add_vsi call, but we will retrieve the current
9278 * VSI context.
9279 */
9280 ctxt.seid = pf->main_vsi_seid;
9281 ctxt.pf_num = pf->hw.pf_id;
9282 ctxt.vf_num = 0;
9283 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9284 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9285 if (ret) {
9286 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009287 "couldn't get PF vsi config, err %s aq_err %s\n",
9288 i40e_stat_str(&pf->hw, ret),
9289 i40e_aq_str(&pf->hw,
9290 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009291 return -ENOENT;
9292 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009293 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009294 vsi->info.valid_sections = 0;
9295
9296 vsi->seid = ctxt.seid;
9297 vsi->id = ctxt.vsi_number;
9298
9299 enabled_tc = i40e_pf_get_tc_map(pf);
9300
9301 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009302 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9303 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009304 memset(&ctxt, 0, sizeof(ctxt));
9305 ctxt.seid = pf->main_vsi_seid;
9306 ctxt.pf_num = pf->hw.pf_id;
9307 ctxt.vf_num = 0;
9308 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9309 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9310 if (ret) {
9311 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009312 "update vsi failed, err %s aq_err %s\n",
9313 i40e_stat_str(&pf->hw, ret),
9314 i40e_aq_str(&pf->hw,
9315 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009316 ret = -ENOENT;
9317 goto err;
9318 }
9319 /* update the local VSI info queue map */
9320 i40e_vsi_update_queue_map(vsi, &ctxt);
9321 vsi->info.valid_sections = 0;
9322 } else {
9323 /* Default/Main VSI is only enabled for TC0
9324 * reconfigure it to enable all TCs that are
9325 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009326 * For MFP case the iSCSI PF would use this
9327 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009328 */
9329 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9330 if (ret) {
9331 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009332 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9333 enabled_tc,
9334 i40e_stat_str(&pf->hw, ret),
9335 i40e_aq_str(&pf->hw,
9336 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009337 ret = -ENOENT;
9338 }
9339 }
9340 break;
9341
9342 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009343 ctxt.pf_num = hw->pf_id;
9344 ctxt.vf_num = 0;
9345 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009346 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009347 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009348 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9349 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009350 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009351 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009352 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009353 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009354 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009355 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009356 break;
9357
9358 case I40E_VSI_VMDQ2:
9359 ctxt.pf_num = hw->pf_id;
9360 ctxt.vf_num = 0;
9361 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009362 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009363 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365 /* This VSI is connected to VEB so the switch_id
9366 * should be set to zero by default.
9367 */
Neerav Parikh51616012015-02-06 08:52:14 +00009368 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9369 ctxt.info.valid_sections |=
9370 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9371 ctxt.info.switch_id =
9372 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9373 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009374
9375 /* Setup the VSI tx/rx queue map for TC0 only for now */
9376 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9377 break;
9378
9379 case I40E_VSI_SRIOV:
9380 ctxt.pf_num = hw->pf_id;
9381 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9382 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009383 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009384 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9385
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009386 /* This VSI is connected to VEB so the switch_id
9387 * should be set to zero by default.
9388 */
Neerav Parikh51616012015-02-06 08:52:14 +00009389 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9390 ctxt.info.valid_sections |=
9391 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9392 ctxt.info.switch_id =
9393 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9394 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009395
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009396 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9397 ctxt.info.valid_sections |=
9398 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9399 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009400 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9401 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009402 }
9403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009404 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9405 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009406 if (pf->vf[vsi->vf_id].spoofchk) {
9407 ctxt.info.valid_sections |=
9408 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9409 ctxt.info.sec_flags |=
9410 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9411 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9412 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009413 /* Setup the VSI tx/rx queue map for TC0 only for now */
9414 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9415 break;
9416
Vasu Dev38e00432014-08-01 13:27:03 -07009417#ifdef I40E_FCOE
9418 case I40E_VSI_FCOE:
9419 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9420 if (ret) {
9421 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9422 return ret;
9423 }
9424 break;
9425
9426#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009427 case I40E_VSI_IWARP:
9428 /* send down message to iWARP */
9429 break;
9430
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009431 default:
9432 return -ENODEV;
9433 }
9434
9435 if (vsi->type != I40E_VSI_MAIN) {
9436 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9437 if (ret) {
9438 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009439 "add vsi failed, err %s aq_err %s\n",
9440 i40e_stat_str(&pf->hw, ret),
9441 i40e_aq_str(&pf->hw,
9442 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009443 ret = -ENOENT;
9444 goto err;
9445 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009446 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447 vsi->info.valid_sections = 0;
9448 vsi->seid = ctxt.seid;
9449 vsi->id = ctxt.vsi_number;
9450 }
Kiran Patilf6bd0962016-06-20 09:10:34 -07009451 /* Except FDIR VSI, for all othet VSI set the broadcast filter */
9452 if (vsi->type != I40E_VSI_FDIR) {
9453 aq_ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
9454 if (aq_ret) {
9455 ret = i40e_aq_rc_to_posix(aq_ret,
9456 hw->aq.asq_last_status);
9457 dev_info(&pf->pdev->dev,
9458 "set brdcast promisc failed, err %s, aq_err %s\n",
9459 i40e_stat_str(hw, aq_ret),
9460 i40e_aq_str(hw, hw->aq.asq_last_status));
9461 }
9462 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009463
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009464 vsi->active_filters = 0;
9465 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Kiran Patil21659032015-09-30 14:09:03 -04009466 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009467 /* If macvlan filters already exist, force them to get loaded */
9468 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009469 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009470 f_count++;
9471 }
Kiran Patil21659032015-09-30 14:09:03 -04009472 spin_unlock_bh(&vsi->mac_filter_list_lock);
9473
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009474 if (f_count) {
9475 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9476 pf->flags |= I40E_FLAG_FILTER_SYNC;
9477 }
9478
9479 /* Update VSI BW information */
9480 ret = i40e_vsi_get_bw_info(vsi);
9481 if (ret) {
9482 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009483 "couldn't get vsi bw info, err %s aq_err %s\n",
9484 i40e_stat_str(&pf->hw, ret),
9485 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009486 /* VSI is already added so not tearing that up */
9487 ret = 0;
9488 }
9489
9490err:
9491 return ret;
9492}
9493
9494/**
9495 * i40e_vsi_release - Delete a VSI and free its resources
9496 * @vsi: the VSI being removed
9497 *
9498 * Returns 0 on success or < 0 on error
9499 **/
9500int i40e_vsi_release(struct i40e_vsi *vsi)
9501{
9502 struct i40e_mac_filter *f, *ftmp;
9503 struct i40e_veb *veb = NULL;
9504 struct i40e_pf *pf;
9505 u16 uplink_seid;
9506 int i, n;
9507
9508 pf = vsi->back;
9509
9510 /* release of a VEB-owner or last VSI is not allowed */
9511 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9512 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9513 vsi->seid, vsi->uplink_seid);
9514 return -ENODEV;
9515 }
9516 if (vsi == pf->vsi[pf->lan_vsi] &&
9517 !test_bit(__I40E_DOWN, &pf->state)) {
9518 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9519 return -ENODEV;
9520 }
9521
9522 uplink_seid = vsi->uplink_seid;
9523 if (vsi->type != I40E_VSI_SRIOV) {
9524 if (vsi->netdev_registered) {
9525 vsi->netdev_registered = false;
9526 if (vsi->netdev) {
9527 /* results in a call to i40e_close() */
9528 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009529 }
9530 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009531 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009532 }
9533 i40e_vsi_disable_irq(vsi);
9534 }
9535
Kiran Patil21659032015-09-30 14:09:03 -04009536 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009537 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9538 i40e_del_filter(vsi, f->macaddr, f->vlan,
9539 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009540 spin_unlock_bh(&vsi->mac_filter_list_lock);
9541
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009542 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009543
9544 i40e_vsi_delete(vsi);
9545 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009546 if (vsi->netdev) {
9547 free_netdev(vsi->netdev);
9548 vsi->netdev = NULL;
9549 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009550 i40e_vsi_clear_rings(vsi);
9551 i40e_vsi_clear(vsi);
9552
9553 /* If this was the last thing on the VEB, except for the
9554 * controlling VSI, remove the VEB, which puts the controlling
9555 * VSI onto the next level down in the switch.
9556 *
9557 * Well, okay, there's one more exception here: don't remove
9558 * the orphan VEBs yet. We'll wait for an explicit remove request
9559 * from up the network stack.
9560 */
Mitch Williams505682c2014-05-20 08:01:37 +00009561 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009562 if (pf->vsi[i] &&
9563 pf->vsi[i]->uplink_seid == uplink_seid &&
9564 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9565 n++; /* count the VSIs */
9566 }
9567 }
9568 for (i = 0; i < I40E_MAX_VEB; i++) {
9569 if (!pf->veb[i])
9570 continue;
9571 if (pf->veb[i]->uplink_seid == uplink_seid)
9572 n++; /* count the VEBs */
9573 if (pf->veb[i]->seid == uplink_seid)
9574 veb = pf->veb[i];
9575 }
9576 if (n == 0 && veb && veb->uplink_seid != 0)
9577 i40e_veb_release(veb);
9578
9579 return 0;
9580}
9581
9582/**
9583 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9584 * @vsi: ptr to the VSI
9585 *
9586 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9587 * corresponding SW VSI structure and initializes num_queue_pairs for the
9588 * newly allocated VSI.
9589 *
9590 * Returns 0 on success or negative on failure
9591 **/
9592static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9593{
9594 int ret = -ENOENT;
9595 struct i40e_pf *pf = vsi->back;
9596
Alexander Duyck493fb302013-09-28 07:01:44 +00009597 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009598 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9599 vsi->seid);
9600 return -EEXIST;
9601 }
9602
9603 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009604 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009605 vsi->seid, vsi->base_vector);
9606 return -EEXIST;
9607 }
9608
Greg Rose90e04072014-03-06 08:59:57 +00009609 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009610 if (ret) {
9611 dev_info(&pf->pdev->dev,
9612 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9613 vsi->num_q_vectors, vsi->seid, ret);
9614 vsi->num_q_vectors = 0;
9615 goto vector_setup_out;
9616 }
9617
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009618 /* In Legacy mode, we do not have to get any other vector since we
9619 * piggyback on the misc/ICR0 for queue interrupts.
9620 */
9621 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9622 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009623 if (vsi->num_q_vectors)
9624 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9625 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009626 if (vsi->base_vector < 0) {
9627 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009628 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9629 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009630 i40e_vsi_free_q_vectors(vsi);
9631 ret = -ENOENT;
9632 goto vector_setup_out;
9633 }
9634
9635vector_setup_out:
9636 return ret;
9637}
9638
9639/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009640 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9641 * @vsi: pointer to the vsi.
9642 *
9643 * This re-allocates a vsi's queue resources.
9644 *
9645 * Returns pointer to the successfully allocated and configured VSI sw struct
9646 * on success, otherwise returns NULL on failure.
9647 **/
9648static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9649{
John Underwoodf5340392016-02-18 09:19:24 -08009650 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009651 u8 enabled_tc;
9652 int ret;
9653
John Underwoodf5340392016-02-18 09:19:24 -08009654 if (!vsi)
9655 return NULL;
9656
9657 pf = vsi->back;
9658
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009659 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9660 i40e_vsi_clear_rings(vsi);
9661
9662 i40e_vsi_free_arrays(vsi, false);
9663 i40e_set_num_rings_in_vsi(vsi);
9664 ret = i40e_vsi_alloc_arrays(vsi, false);
9665 if (ret)
9666 goto err_vsi;
9667
9668 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9669 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009670 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009671 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009672 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009673 goto err_vsi;
9674 }
9675 vsi->base_queue = ret;
9676
9677 /* Update the FW view of the VSI. Force a reset of TC and queue
9678 * layout configurations.
9679 */
9680 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9681 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9682 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9683 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009684 if (vsi->type == I40E_VSI_MAIN)
9685 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009686
9687 /* assign it some queues */
9688 ret = i40e_alloc_rings(vsi);
9689 if (ret)
9690 goto err_rings;
9691
9692 /* map all of the rings to the q_vectors */
9693 i40e_vsi_map_rings_to_vectors(vsi);
9694 return vsi;
9695
9696err_rings:
9697 i40e_vsi_free_q_vectors(vsi);
9698 if (vsi->netdev_registered) {
9699 vsi->netdev_registered = false;
9700 unregister_netdev(vsi->netdev);
9701 free_netdev(vsi->netdev);
9702 vsi->netdev = NULL;
9703 }
9704 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9705err_vsi:
9706 i40e_vsi_clear(vsi);
9707 return NULL;
9708}
9709
9710/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009711 * i40e_vsi_setup - Set up a VSI by a given type
9712 * @pf: board private structure
9713 * @type: VSI type
9714 * @uplink_seid: the switch element to link to
9715 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9716 *
9717 * This allocates the sw VSI structure and its queue resources, then add a VSI
9718 * to the identified VEB.
9719 *
9720 * Returns pointer to the successfully allocated and configure VSI sw struct on
9721 * success, otherwise returns NULL on failure.
9722 **/
9723struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9724 u16 uplink_seid, u32 param1)
9725{
9726 struct i40e_vsi *vsi = NULL;
9727 struct i40e_veb *veb = NULL;
9728 int ret, i;
9729 int v_idx;
9730
9731 /* The requested uplink_seid must be either
9732 * - the PF's port seid
9733 * no VEB is needed because this is the PF
9734 * or this is a Flow Director special case VSI
9735 * - seid of an existing VEB
9736 * - seid of a VSI that owns an existing VEB
9737 * - seid of a VSI that doesn't own a VEB
9738 * a new VEB is created and the VSI becomes the owner
9739 * - seid of the PF VSI, which is what creates the first VEB
9740 * this is a special case of the previous
9741 *
9742 * Find which uplink_seid we were given and create a new VEB if needed
9743 */
9744 for (i = 0; i < I40E_MAX_VEB; i++) {
9745 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9746 veb = pf->veb[i];
9747 break;
9748 }
9749 }
9750
9751 if (!veb && uplink_seid != pf->mac_seid) {
9752
Mitch Williams505682c2014-05-20 08:01:37 +00009753 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009754 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9755 vsi = pf->vsi[i];
9756 break;
9757 }
9758 }
9759 if (!vsi) {
9760 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9761 uplink_seid);
9762 return NULL;
9763 }
9764
9765 if (vsi->uplink_seid == pf->mac_seid)
9766 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9767 vsi->tc_config.enabled_tc);
9768 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9769 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9770 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009771 if (veb) {
9772 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9773 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009774 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009775 return NULL;
9776 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009777 /* We come up by default in VEPA mode if SRIOV is not
9778 * already enabled, in which case we can't force VEPA
9779 * mode.
9780 */
9781 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9782 veb->bridge_mode = BRIDGE_MODE_VEPA;
9783 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9784 }
Neerav Parikh51616012015-02-06 08:52:14 +00009785 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009786 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009787 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9788 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9789 veb = pf->veb[i];
9790 }
9791 if (!veb) {
9792 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9793 return NULL;
9794 }
9795
9796 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9797 uplink_seid = veb->seid;
9798 }
9799
9800 /* get vsi sw struct */
9801 v_idx = i40e_vsi_mem_alloc(pf, type);
9802 if (v_idx < 0)
9803 goto err_alloc;
9804 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009805 if (!vsi)
9806 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009807 vsi->type = type;
9808 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9809
9810 if (type == I40E_VSI_MAIN)
9811 pf->lan_vsi = v_idx;
9812 else if (type == I40E_VSI_SRIOV)
9813 vsi->vf_id = param1;
9814 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009815 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9816 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009817 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009818 dev_info(&pf->pdev->dev,
9819 "failed to get tracking for %d queues for VSI %d err=%d\n",
9820 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009821 goto err_vsi;
9822 }
9823 vsi->base_queue = ret;
9824
9825 /* get a VSI from the hardware */
9826 vsi->uplink_seid = uplink_seid;
9827 ret = i40e_add_vsi(vsi);
9828 if (ret)
9829 goto err_vsi;
9830
9831 switch (vsi->type) {
9832 /* setup the netdev if needed */
9833 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009834 /* Apply relevant filters if a platform-specific mac
9835 * address was selected.
9836 */
9837 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9838 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9839 if (ret) {
9840 dev_warn(&pf->pdev->dev,
9841 "could not set up macaddr; err %d\n",
9842 ret);
9843 }
9844 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009845 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009846 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009847 ret = i40e_config_netdev(vsi);
9848 if (ret)
9849 goto err_netdev;
9850 ret = register_netdev(vsi->netdev);
9851 if (ret)
9852 goto err_netdev;
9853 vsi->netdev_registered = true;
9854 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009855#ifdef CONFIG_I40E_DCB
9856 /* Setup DCB netlink interface */
9857 i40e_dcbnl_setup(vsi);
9858#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009859 /* fall through */
9860
9861 case I40E_VSI_FDIR:
9862 /* set up vectors and rings if needed */
9863 ret = i40e_vsi_setup_vectors(vsi);
9864 if (ret)
9865 goto err_msix;
9866
9867 ret = i40e_alloc_rings(vsi);
9868 if (ret)
9869 goto err_rings;
9870
9871 /* map all of the rings to the q_vectors */
9872 i40e_vsi_map_rings_to_vectors(vsi);
9873
9874 i40e_vsi_reset_stats(vsi);
9875 break;
9876
9877 default:
9878 /* no netdev or rings for the other VSI types */
9879 break;
9880 }
9881
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04009882 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9883 (vsi->type == I40E_VSI_VMDQ2)) {
9884 ret = i40e_vsi_config_rss(vsi);
9885 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009886 return vsi;
9887
9888err_rings:
9889 i40e_vsi_free_q_vectors(vsi);
9890err_msix:
9891 if (vsi->netdev_registered) {
9892 vsi->netdev_registered = false;
9893 unregister_netdev(vsi->netdev);
9894 free_netdev(vsi->netdev);
9895 vsi->netdev = NULL;
9896 }
9897err_netdev:
9898 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9899err_vsi:
9900 i40e_vsi_clear(vsi);
9901err_alloc:
9902 return NULL;
9903}
9904
9905/**
9906 * i40e_veb_get_bw_info - Query VEB BW information
9907 * @veb: the veb to query
9908 *
9909 * Query the Tx scheduler BW configuration data for given VEB
9910 **/
9911static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9912{
9913 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9914 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9915 struct i40e_pf *pf = veb->pf;
9916 struct i40e_hw *hw = &pf->hw;
9917 u32 tc_bw_max;
9918 int ret = 0;
9919 int i;
9920
9921 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9922 &bw_data, NULL);
9923 if (ret) {
9924 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009925 "query veb bw config failed, err %s aq_err %s\n",
9926 i40e_stat_str(&pf->hw, ret),
9927 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009928 goto out;
9929 }
9930
9931 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9932 &ets_data, NULL);
9933 if (ret) {
9934 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009935 "query veb bw ets config failed, err %s aq_err %s\n",
9936 i40e_stat_str(&pf->hw, ret),
9937 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009938 goto out;
9939 }
9940
9941 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9942 veb->bw_max_quanta = ets_data.tc_bw_max;
9943 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009944 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009945 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9946 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9947 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9948 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9949 veb->bw_tc_limit_credits[i] =
9950 le16_to_cpu(bw_data.tc_bw_limits[i]);
9951 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9952 }
9953
9954out:
9955 return ret;
9956}
9957
9958/**
9959 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9960 * @pf: board private structure
9961 *
9962 * On error: returns error code (negative)
9963 * On success: returns vsi index in PF (positive)
9964 **/
9965static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9966{
9967 int ret = -ENOENT;
9968 struct i40e_veb *veb;
9969 int i;
9970
9971 /* Need to protect the allocation of switch elements at the PF level */
9972 mutex_lock(&pf->switch_mutex);
9973
9974 /* VEB list may be fragmented if VEB creation/destruction has
9975 * been happening. We can afford to do a quick scan to look
9976 * for any free slots in the list.
9977 *
9978 * find next empty veb slot, looping back around if necessary
9979 */
9980 i = 0;
9981 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9982 i++;
9983 if (i >= I40E_MAX_VEB) {
9984 ret = -ENOMEM;
9985 goto err_alloc_veb; /* out of VEB slots! */
9986 }
9987
9988 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9989 if (!veb) {
9990 ret = -ENOMEM;
9991 goto err_alloc_veb;
9992 }
9993 veb->pf = pf;
9994 veb->idx = i;
9995 veb->enabled_tc = 1;
9996
9997 pf->veb[i] = veb;
9998 ret = i;
9999err_alloc_veb:
10000 mutex_unlock(&pf->switch_mutex);
10001 return ret;
10002}
10003
10004/**
10005 * i40e_switch_branch_release - Delete a branch of the switch tree
10006 * @branch: where to start deleting
10007 *
10008 * This uses recursion to find the tips of the branch to be
10009 * removed, deleting until we get back to and can delete this VEB.
10010 **/
10011static void i40e_switch_branch_release(struct i40e_veb *branch)
10012{
10013 struct i40e_pf *pf = branch->pf;
10014 u16 branch_seid = branch->seid;
10015 u16 veb_idx = branch->idx;
10016 int i;
10017
10018 /* release any VEBs on this VEB - RECURSION */
10019 for (i = 0; i < I40E_MAX_VEB; i++) {
10020 if (!pf->veb[i])
10021 continue;
10022 if (pf->veb[i]->uplink_seid == branch->seid)
10023 i40e_switch_branch_release(pf->veb[i]);
10024 }
10025
10026 /* Release the VSIs on this VEB, but not the owner VSI.
10027 *
10028 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10029 * the VEB itself, so don't use (*branch) after this loop.
10030 */
Mitch Williams505682c2014-05-20 08:01:37 +000010031 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010032 if (!pf->vsi[i])
10033 continue;
10034 if (pf->vsi[i]->uplink_seid == branch_seid &&
10035 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10036 i40e_vsi_release(pf->vsi[i]);
10037 }
10038 }
10039
10040 /* There's one corner case where the VEB might not have been
10041 * removed, so double check it here and remove it if needed.
10042 * This case happens if the veb was created from the debugfs
10043 * commands and no VSIs were added to it.
10044 */
10045 if (pf->veb[veb_idx])
10046 i40e_veb_release(pf->veb[veb_idx]);
10047}
10048
10049/**
10050 * i40e_veb_clear - remove veb struct
10051 * @veb: the veb to remove
10052 **/
10053static void i40e_veb_clear(struct i40e_veb *veb)
10054{
10055 if (!veb)
10056 return;
10057
10058 if (veb->pf) {
10059 struct i40e_pf *pf = veb->pf;
10060
10061 mutex_lock(&pf->switch_mutex);
10062 if (pf->veb[veb->idx] == veb)
10063 pf->veb[veb->idx] = NULL;
10064 mutex_unlock(&pf->switch_mutex);
10065 }
10066
10067 kfree(veb);
10068}
10069
10070/**
10071 * i40e_veb_release - Delete a VEB and free its resources
10072 * @veb: the VEB being removed
10073 **/
10074void i40e_veb_release(struct i40e_veb *veb)
10075{
10076 struct i40e_vsi *vsi = NULL;
10077 struct i40e_pf *pf;
10078 int i, n = 0;
10079
10080 pf = veb->pf;
10081
10082 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010083 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010084 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10085 n++;
10086 vsi = pf->vsi[i];
10087 }
10088 }
10089 if (n != 1) {
10090 dev_info(&pf->pdev->dev,
10091 "can't remove VEB %d with %d VSIs left\n",
10092 veb->seid, n);
10093 return;
10094 }
10095
10096 /* move the remaining VSI to uplink veb */
10097 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10098 if (veb->uplink_seid) {
10099 vsi->uplink_seid = veb->uplink_seid;
10100 if (veb->uplink_seid == pf->mac_seid)
10101 vsi->veb_idx = I40E_NO_VEB;
10102 else
10103 vsi->veb_idx = veb->veb_idx;
10104 } else {
10105 /* floating VEB */
10106 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10107 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10108 }
10109
10110 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10111 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010112}
10113
10114/**
10115 * i40e_add_veb - create the VEB in the switch
10116 * @veb: the VEB to be instantiated
10117 * @vsi: the controlling VSI
10118 **/
10119static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10120{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010121 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010122 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010123 int ret;
10124
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010125 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010126 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010127 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010128
10129 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010130 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010131 dev_info(&pf->pdev->dev,
10132 "couldn't add VEB, err %s aq_err %s\n",
10133 i40e_stat_str(&pf->hw, ret),
10134 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010135 return -EPERM;
10136 }
10137
10138 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010139 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010140 &veb->stats_idx, NULL, NULL, NULL);
10141 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010142 dev_info(&pf->pdev->dev,
10143 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10144 i40e_stat_str(&pf->hw, ret),
10145 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010146 return -EPERM;
10147 }
10148 ret = i40e_veb_get_bw_info(veb);
10149 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010150 dev_info(&pf->pdev->dev,
10151 "couldn't get VEB bw info, err %s aq_err %s\n",
10152 i40e_stat_str(&pf->hw, ret),
10153 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10154 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010155 return -ENOENT;
10156 }
10157
10158 vsi->uplink_seid = veb->seid;
10159 vsi->veb_idx = veb->idx;
10160 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10161
10162 return 0;
10163}
10164
10165/**
10166 * i40e_veb_setup - Set up a VEB
10167 * @pf: board private structure
10168 * @flags: VEB setup flags
10169 * @uplink_seid: the switch element to link to
10170 * @vsi_seid: the initial VSI seid
10171 * @enabled_tc: Enabled TC bit-map
10172 *
10173 * This allocates the sw VEB structure and links it into the switch
10174 * It is possible and legal for this to be a duplicate of an already
10175 * existing VEB. It is also possible for both uplink and vsi seids
10176 * to be zero, in order to create a floating VEB.
10177 *
10178 * Returns pointer to the successfully allocated VEB sw struct on
10179 * success, otherwise returns NULL on failure.
10180 **/
10181struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10182 u16 uplink_seid, u16 vsi_seid,
10183 u8 enabled_tc)
10184{
10185 struct i40e_veb *veb, *uplink_veb = NULL;
10186 int vsi_idx, veb_idx;
10187 int ret;
10188
10189 /* if one seid is 0, the other must be 0 to create a floating relay */
10190 if ((uplink_seid == 0 || vsi_seid == 0) &&
10191 (uplink_seid + vsi_seid != 0)) {
10192 dev_info(&pf->pdev->dev,
10193 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10194 uplink_seid, vsi_seid);
10195 return NULL;
10196 }
10197
10198 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010199 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010200 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10201 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010202 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010203 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10204 vsi_seid);
10205 return NULL;
10206 }
10207
10208 if (uplink_seid && uplink_seid != pf->mac_seid) {
10209 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10210 if (pf->veb[veb_idx] &&
10211 pf->veb[veb_idx]->seid == uplink_seid) {
10212 uplink_veb = pf->veb[veb_idx];
10213 break;
10214 }
10215 }
10216 if (!uplink_veb) {
10217 dev_info(&pf->pdev->dev,
10218 "uplink seid %d not found\n", uplink_seid);
10219 return NULL;
10220 }
10221 }
10222
10223 /* get veb sw struct */
10224 veb_idx = i40e_veb_mem_alloc(pf);
10225 if (veb_idx < 0)
10226 goto err_alloc;
10227 veb = pf->veb[veb_idx];
10228 veb->flags = flags;
10229 veb->uplink_seid = uplink_seid;
10230 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10231 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10232
10233 /* create the VEB in the switch */
10234 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10235 if (ret)
10236 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010237 if (vsi_idx == pf->lan_vsi)
10238 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010239
10240 return veb;
10241
10242err_veb:
10243 i40e_veb_clear(veb);
10244err_alloc:
10245 return NULL;
10246}
10247
10248/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010249 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010250 * @pf: board private structure
10251 * @ele: element we are building info from
10252 * @num_reported: total number of elements
10253 * @printconfig: should we print the contents
10254 *
10255 * helper function to assist in extracting a few useful SEID values.
10256 **/
10257static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10258 struct i40e_aqc_switch_config_element_resp *ele,
10259 u16 num_reported, bool printconfig)
10260{
10261 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10262 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10263 u8 element_type = ele->element_type;
10264 u16 seid = le16_to_cpu(ele->seid);
10265
10266 if (printconfig)
10267 dev_info(&pf->pdev->dev,
10268 "type=%d seid=%d uplink=%d downlink=%d\n",
10269 element_type, seid, uplink_seid, downlink_seid);
10270
10271 switch (element_type) {
10272 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10273 pf->mac_seid = seid;
10274 break;
10275 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10276 /* Main VEB? */
10277 if (uplink_seid != pf->mac_seid)
10278 break;
10279 if (pf->lan_veb == I40E_NO_VEB) {
10280 int v;
10281
10282 /* find existing or else empty VEB */
10283 for (v = 0; v < I40E_MAX_VEB; v++) {
10284 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10285 pf->lan_veb = v;
10286 break;
10287 }
10288 }
10289 if (pf->lan_veb == I40E_NO_VEB) {
10290 v = i40e_veb_mem_alloc(pf);
10291 if (v < 0)
10292 break;
10293 pf->lan_veb = v;
10294 }
10295 }
10296
10297 pf->veb[pf->lan_veb]->seid = seid;
10298 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10299 pf->veb[pf->lan_veb]->pf = pf;
10300 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10301 break;
10302 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10303 if (num_reported != 1)
10304 break;
10305 /* This is immediately after a reset so we can assume this is
10306 * the PF's VSI
10307 */
10308 pf->mac_seid = uplink_seid;
10309 pf->pf_seid = downlink_seid;
10310 pf->main_vsi_seid = seid;
10311 if (printconfig)
10312 dev_info(&pf->pdev->dev,
10313 "pf_seid=%d main_vsi_seid=%d\n",
10314 pf->pf_seid, pf->main_vsi_seid);
10315 break;
10316 case I40E_SWITCH_ELEMENT_TYPE_PF:
10317 case I40E_SWITCH_ELEMENT_TYPE_VF:
10318 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10319 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10320 case I40E_SWITCH_ELEMENT_TYPE_PE:
10321 case I40E_SWITCH_ELEMENT_TYPE_PA:
10322 /* ignore these for now */
10323 break;
10324 default:
10325 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10326 element_type, seid);
10327 break;
10328 }
10329}
10330
10331/**
10332 * i40e_fetch_switch_configuration - Get switch config from firmware
10333 * @pf: board private structure
10334 * @printconfig: should we print the contents
10335 *
10336 * Get the current switch configuration from the device and
10337 * extract a few useful SEID values.
10338 **/
10339int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10340{
10341 struct i40e_aqc_get_switch_config_resp *sw_config;
10342 u16 next_seid = 0;
10343 int ret = 0;
10344 u8 *aq_buf;
10345 int i;
10346
10347 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10348 if (!aq_buf)
10349 return -ENOMEM;
10350
10351 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10352 do {
10353 u16 num_reported, num_total;
10354
10355 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10356 I40E_AQ_LARGE_BUF,
10357 &next_seid, NULL);
10358 if (ret) {
10359 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010360 "get switch config failed err %s aq_err %s\n",
10361 i40e_stat_str(&pf->hw, ret),
10362 i40e_aq_str(&pf->hw,
10363 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010364 kfree(aq_buf);
10365 return -ENOENT;
10366 }
10367
10368 num_reported = le16_to_cpu(sw_config->header.num_reported);
10369 num_total = le16_to_cpu(sw_config->header.num_total);
10370
10371 if (printconfig)
10372 dev_info(&pf->pdev->dev,
10373 "header: %d reported %d total\n",
10374 num_reported, num_total);
10375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010376 for (i = 0; i < num_reported; i++) {
10377 struct i40e_aqc_switch_config_element_resp *ele =
10378 &sw_config->element[i];
10379
10380 i40e_setup_pf_switch_element(pf, ele, num_reported,
10381 printconfig);
10382 }
10383 } while (next_seid != 0);
10384
10385 kfree(aq_buf);
10386 return ret;
10387}
10388
10389/**
10390 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10391 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010392 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010393 *
10394 * Returns 0 on success, negative value on failure
10395 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010396static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010397{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010398 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010399 int ret;
10400
10401 /* find out what's out there already */
10402 ret = i40e_fetch_switch_configuration(pf, false);
10403 if (ret) {
10404 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010405 "couldn't fetch switch config, err %s aq_err %s\n",
10406 i40e_stat_str(&pf->hw, ret),
10407 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010408 return ret;
10409 }
10410 i40e_pf_reset_stats(pf);
10411
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010412 /* set the switch config bit for the whole device to
10413 * support limited promisc or true promisc
10414 * when user requests promisc. The default is limited
10415 * promisc.
10416 */
10417
10418 if ((pf->hw.pf_id == 0) &&
10419 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10420 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10421
10422 if (pf->hw.pf_id == 0) {
10423 u16 valid_flags;
10424
10425 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10426 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10427 NULL);
10428 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10429 dev_info(&pf->pdev->dev,
10430 "couldn't set switch config bits, err %s aq_err %s\n",
10431 i40e_stat_str(&pf->hw, ret),
10432 i40e_aq_str(&pf->hw,
10433 pf->hw.aq.asq_last_status));
10434 /* not a fatal problem, just keep going */
10435 }
10436 }
10437
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010438 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010439 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010440 struct i40e_vsi *vsi = NULL;
10441 u16 uplink_seid;
10442
10443 /* Set up the PF VSI associated with the PF's main VSI
10444 * that is already in the HW switch
10445 */
10446 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10447 uplink_seid = pf->veb[pf->lan_veb]->seid;
10448 else
10449 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010450 if (pf->lan_vsi == I40E_NO_VSI)
10451 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10452 else if (reinit)
10453 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010454 if (!vsi) {
10455 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10456 i40e_fdir_teardown(pf);
10457 return -EAGAIN;
10458 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010459 } else {
10460 /* force a reset of TC and queue layout configurations */
10461 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010462
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010463 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10464 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10465 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10466 }
10467 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10468
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010469 i40e_fdir_sb_setup(pf);
10470
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010471 /* Setup static PF queue filter control settings */
10472 ret = i40e_setup_pf_filter_control(pf);
10473 if (ret) {
10474 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10475 ret);
10476 /* Failure here should not stop continuing other steps */
10477 }
10478
10479 /* enable RSS in the HW, even for only one queue, as the stack can use
10480 * the hash
10481 */
10482 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010483 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010484
10485 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010486 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010487 i40e_link_event(pf);
10488
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010489 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010490 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10491 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010492
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010493 i40e_ptp_init(pf);
10494
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010495 return ret;
10496}
10497
10498/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499 * i40e_determine_queue_usage - Work out queue distribution
10500 * @pf: board private structure
10501 **/
10502static void i40e_determine_queue_usage(struct i40e_pf *pf)
10503{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010504 int queues_left;
10505
10506 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010507#ifdef I40E_FCOE
10508 pf->num_fcoe_qps = 0;
10509#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010510
10511 /* Find the max queues to be put into basic use. We'll always be
10512 * using TC0, whether or not DCB is running, and TC0 will get the
10513 * big RSS set.
10514 */
10515 queues_left = pf->hw.func_caps.num_tx_qp;
10516
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010517 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010518 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010519 /* one qp for PF, no queues for anything else */
10520 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010521 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010522
10523 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010524 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010525 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010526#ifdef I40E_FCOE
10527 I40E_FLAG_FCOE_ENABLED |
10528#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010529 I40E_FLAG_FD_SB_ENABLED |
10530 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010531 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010532 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010533 I40E_FLAG_SRIOV_ENABLED |
10534 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010535 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10536 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010537 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010538 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010539 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010540 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010541 queues_left -= pf->num_lan_qps;
10542
10543 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010544 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010545#ifdef I40E_FCOE
10546 I40E_FLAG_FCOE_ENABLED |
10547#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010548 I40E_FLAG_FD_SB_ENABLED |
10549 I40E_FLAG_FD_ATR_ENABLED |
10550 I40E_FLAG_DCB_ENABLED |
10551 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010552 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010553 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010554 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010555 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010556 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10557 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010558 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10559 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010560 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10561 num_online_cpus());
10562 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10563 pf->hw.func_caps.num_tx_qp);
10564
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010565 queues_left -= pf->num_lan_qps;
10566 }
10567
Vasu Dev38e00432014-08-01 13:27:03 -070010568#ifdef I40E_FCOE
10569 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10570 if (I40E_DEFAULT_FCOE <= queues_left) {
10571 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10572 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10573 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10574 } else {
10575 pf->num_fcoe_qps = 0;
10576 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10577 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10578 }
10579
10580 queues_left -= pf->num_fcoe_qps;
10581 }
10582
10583#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010584 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10585 if (queues_left > 1) {
10586 queues_left -= 1; /* save 1 queue for FD */
10587 } else {
10588 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10589 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10590 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010591 }
10592
10593 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10594 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010595 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10596 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010597 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10598 }
10599
10600 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10601 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10602 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10603 (queues_left / pf->num_vmdq_qps));
10604 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10605 }
10606
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010607 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010608 dev_dbg(&pf->pdev->dev,
10609 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10610 pf->hw.func_caps.num_tx_qp,
10611 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010612 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10613 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10614 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010615#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010616 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010617#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010618}
10619
10620/**
10621 * i40e_setup_pf_filter_control - Setup PF static filter control
10622 * @pf: PF to be setup
10623 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010624 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010625 * settings. If PE/FCoE are enabled then it will also set the per PF
10626 * based filter sizes required for them. It also enables Flow director,
10627 * ethertype and macvlan type filter settings for the pf.
10628 *
10629 * Returns 0 on success, negative on failure
10630 **/
10631static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10632{
10633 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10634
10635 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10636
10637 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010638 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010639 settings->enable_fdir = true;
10640
10641 /* Ethtype and MACVLAN filters enabled for PF */
10642 settings->enable_ethtype = true;
10643 settings->enable_macvlan = true;
10644
10645 if (i40e_set_filter_control(&pf->hw, settings))
10646 return -ENOENT;
10647
10648 return 0;
10649}
10650
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010651#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010652#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010653static void i40e_print_features(struct i40e_pf *pf)
10654{
10655 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010656 char *buf;
10657 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010658
Joe Perches3b195842015-12-03 04:20:57 -080010659 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10660 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010661 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010662
Joe Perches3b195842015-12-03 04:20:57 -080010663 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010664#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010665 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010666#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010667 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010668 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010669 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010670 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010671 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010672 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010673 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010674 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010675 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10676 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010677 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010678 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010679 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010680 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010681 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010682 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010683 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010684#ifdef I40E_FCOE
10685 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010686 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010687#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010688 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010689 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010690 else
Joe Perches3b195842015-12-03 04:20:57 -080010691 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010692
Joe Perches3b195842015-12-03 04:20:57 -080010693 dev_info(&pf->pdev->dev, "%s\n", buf);
10694 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010695 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010696}
10697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010698/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010699 * i40e_get_platform_mac_addr - get platform-specific MAC address
10700 *
10701 * @pdev: PCI device information struct
10702 * @pf: board private structure
10703 *
10704 * Look up the MAC address in Open Firmware on systems that support it,
10705 * and use IDPROM on SPARC if no OF address is found. On return, the
10706 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10707 * has been selected.
10708 **/
10709static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10710{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010711 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010712 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010713 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010714}
10715
10716/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010717 * i40e_probe - Device initialization routine
10718 * @pdev: PCI device information struct
10719 * @ent: entry in i40e_pci_tbl
10720 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010721 * i40e_probe initializes a PF identified by a pci_dev structure.
10722 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010723 * and a hardware reset occur.
10724 *
10725 * Returns 0 on success, negative on failure
10726 **/
10727static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10728{
Catherine Sullivane8278452015-02-06 08:52:08 +000010729 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010730 struct i40e_pf *pf;
10731 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010732 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010733 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010734 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010735 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010736 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010737 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010738 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010739
10740 err = pci_enable_device_mem(pdev);
10741 if (err)
10742 return err;
10743
10744 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010745 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010746 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010747 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10748 if (err) {
10749 dev_err(&pdev->dev,
10750 "DMA configuration failed: 0x%x\n", err);
10751 goto err_dma;
10752 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010753 }
10754
10755 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010756 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010757 if (err) {
10758 dev_info(&pdev->dev,
10759 "pci_request_selected_regions failed %d\n", err);
10760 goto err_pci_reg;
10761 }
10762
10763 pci_enable_pcie_error_reporting(pdev);
10764 pci_set_master(pdev);
10765
10766 /* Now that we have a PCI connection, we need to do the
10767 * low level device setup. This is primarily setting up
10768 * the Admin Queue structures and then querying for the
10769 * device's current profile information.
10770 */
10771 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10772 if (!pf) {
10773 err = -ENOMEM;
10774 goto err_pf_alloc;
10775 }
10776 pf->next_vsi = 0;
10777 pf->pdev = pdev;
10778 set_bit(__I40E_DOWN, &pf->state);
10779
10780 hw = &pf->hw;
10781 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010782
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010783 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10784 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010785
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010786 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010787 if (!hw->hw_addr) {
10788 err = -EIO;
10789 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10790 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010791 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010792 goto err_ioremap;
10793 }
10794 hw->vendor_id = pdev->vendor;
10795 hw->device_id = pdev->device;
10796 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10797 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10798 hw->subsystem_device_id = pdev->subsystem_device;
10799 hw->bus.device = PCI_SLOT(pdev->devfn);
10800 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010801 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010802
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010803 /* set up the locks for the AQ, do this only once in probe
10804 * and destroy them only once in remove
10805 */
10806 mutex_init(&hw->aq.asq_mutex);
10807 mutex_init(&hw->aq.arq_mutex);
10808
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010809 if (debug != -1) {
10810 pf->msg_enable = pf->hw.debug_mask;
10811 pf->msg_enable = debug;
10812 }
10813
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010814 /* do a special CORER for clearing PXE mode once at init */
10815 if (hw->revision_id == 0 &&
10816 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10817 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10818 i40e_flush(hw);
10819 msleep(200);
10820 pf->corer_count++;
10821
10822 i40e_clear_pxe_mode(hw);
10823 }
10824
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010825 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010826 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010827 err = i40e_pf_reset(hw);
10828 if (err) {
10829 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10830 goto err_pf_reset;
10831 }
10832 pf->pfr_count++;
10833
10834 hw->aq.num_arq_entries = I40E_AQ_LEN;
10835 hw->aq.num_asq_entries = I40E_AQ_LEN;
10836 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10837 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10838 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010839
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010840 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010841 "%s-%s:misc",
10842 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010843
10844 err = i40e_init_shared_code(hw);
10845 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010846 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10847 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010848 goto err_pf_reset;
10849 }
10850
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010851 /* set up a default setting for link flow control */
10852 pf->hw.fc.requested_mode = I40E_FC_NONE;
10853
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010854 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010855 if (err) {
10856 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10857 dev_info(&pdev->dev,
10858 "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");
10859 else
10860 dev_info(&pdev->dev,
10861 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10862
10863 goto err_pf_reset;
10864 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010865
Shannon Nelson6dec1012015-09-28 14:12:30 -040010866 /* provide nvm, fw, api versions */
10867 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10868 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10869 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10870 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010871
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010872 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10873 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010874 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010875 "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");
10876 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10877 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010878 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010879 "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 +000010880
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010881 i40e_verify_eeprom(pf);
10882
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010883 /* Rev 0 hardware was never productized */
10884 if (hw->revision_id < 1)
10885 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");
10886
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010887 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010888 err = i40e_get_capabilities(pf);
10889 if (err)
10890 goto err_adminq_setup;
10891
10892 err = i40e_sw_init(pf);
10893 if (err) {
10894 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10895 goto err_sw_init;
10896 }
10897
10898 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10899 hw->func_caps.num_rx_qp,
10900 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10901 if (err) {
10902 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10903 goto err_init_lan_hmc;
10904 }
10905
10906 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10907 if (err) {
10908 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10909 err = -ENOENT;
10910 goto err_configure_lan_hmc;
10911 }
10912
Neerav Parikhb686ece2014-12-14 01:55:11 +000010913 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10914 * Ignore error return codes because if it was already disabled via
10915 * hardware settings this will fail
10916 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010917 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010918 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10919 i40e_aq_stop_lldp(hw, true, NULL);
10920 }
10921
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010922 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010923 /* allow a platform config to override the HW addr */
10924 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010925 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010926 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10927 err = -EIO;
10928 goto err_mac_addr;
10929 }
10930 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010931 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010932 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10933 if (is_valid_ether_addr(hw->mac.port_addr))
10934 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010935#ifdef I40E_FCOE
10936 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10937 if (err)
10938 dev_info(&pdev->dev,
10939 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10940 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10941 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10942 hw->mac.san_addr);
10943 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10944 }
10945 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10946#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010947
10948 pci_set_drvdata(pdev, pf);
10949 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010950#ifdef CONFIG_I40E_DCB
10951 err = i40e_init_pf_dcb(pf);
10952 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010953 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
Dave Ertmana0362442016-08-29 17:38:26 -070010954 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE & I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000010955 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010956 }
10957#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010958
10959 /* set up periodic task facility */
10960 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10961 pf->service_timer_period = HZ;
10962
10963 INIT_WORK(&pf->service_task, i40e_service_task);
10964 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10965 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010966
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010967 /* NVM bit on means WoL disabled for the port */
10968 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010969 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010970 pf->wol_en = false;
10971 else
10972 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010973 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10974
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010975 /* set up the main switch operations */
10976 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010977 err = i40e_init_interrupt_scheme(pf);
10978 if (err)
10979 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010980
Mitch Williams505682c2014-05-20 08:01:37 +000010981 /* The number of VSIs reported by the FW is the minimum guaranteed
10982 * to us; HW supports far more and we share the remaining pool with
10983 * the other PFs. We allocate space for more than the guarantee with
10984 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010985 */
Mitch Williams505682c2014-05-20 08:01:37 +000010986 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10987 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10988 else
10989 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10990
10991 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080010992 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
10993 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010994 if (!pf->vsi) {
10995 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010996 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010997 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010998
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010999#ifdef CONFIG_PCI_IOV
11000 /* prep for VF support */
11001 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11002 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11003 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11004 if (pci_num_vf(pdev))
11005 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11006 }
11007#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011008 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011009 if (err) {
11010 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11011 goto err_vsis;
11012 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011013
11014 /* Make sure flow control is set according to current settings */
11015 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11016 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11017 dev_dbg(&pf->pdev->dev,
11018 "Set fc with err %s aq_err %s on get_phy_cap\n",
11019 i40e_stat_str(hw, err),
11020 i40e_aq_str(hw, hw->aq.asq_last_status));
11021 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11022 dev_dbg(&pf->pdev->dev,
11023 "Set fc with err %s aq_err %s on set_phy_config\n",
11024 i40e_stat_str(hw, err),
11025 i40e_aq_str(hw, hw->aq.asq_last_status));
11026 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11027 dev_dbg(&pf->pdev->dev,
11028 "Set fc with err %s aq_err %s on get_link_info\n",
11029 i40e_stat_str(hw, err),
11030 i40e_aq_str(hw, hw->aq.asq_last_status));
11031
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011032 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011033 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011034 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11035 i40e_vsi_open(pf->vsi[i]);
11036 break;
11037 }
11038 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011039
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011040 /* The driver only wants link up/down and module qualification
11041 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011042 */
11043 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011044 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011045 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011046 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011047 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011048 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11049 i40e_stat_str(&pf->hw, err),
11050 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011051
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011052 /* Reconfigure hardware for allowing smaller MSS in the case
11053 * of TSO, so that we avoid the MDD being fired and causing
11054 * a reset in the case of small MSS+TSO.
11055 */
11056 val = rd32(hw, I40E_REG_MSS);
11057 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11058 val &= ~I40E_REG_MSS_MIN_MASK;
11059 val |= I40E_64BYTE_MSS;
11060 wr32(hw, I40E_REG_MSS, val);
11061 }
11062
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011063 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011064 msleep(75);
11065 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11066 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011067 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11068 i40e_stat_str(&pf->hw, err),
11069 i40e_aq_str(&pf->hw,
11070 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011071 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011072 /* The main driver is (mostly) up and happy. We need to set this state
11073 * before setting up the misc vector or we get a race and the vector
11074 * ends up disabled forever.
11075 */
11076 clear_bit(__I40E_DOWN, &pf->state);
11077
11078 /* In case of MSIX we are going to setup the misc vector right here
11079 * to handle admin queue events etc. In case of legacy and MSI
11080 * the misc functionality and queue processing is combined in
11081 * the same vector and that gets setup at open.
11082 */
11083 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11084 err = i40e_setup_misc_vector(pf);
11085 if (err) {
11086 dev_info(&pdev->dev,
11087 "setup of misc vector failed: %d\n", err);
11088 goto err_vsis;
11089 }
11090 }
11091
Greg Rosedf805f62014-04-04 04:43:16 +000011092#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011093 /* prep for VF support */
11094 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011095 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11096 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011097 /* disable link interrupts for VFs */
11098 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11099 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11100 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11101 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011102
11103 if (pci_num_vf(pdev)) {
11104 dev_info(&pdev->dev,
11105 "Active VFs found, allocating resources.\n");
11106 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11107 if (err)
11108 dev_info(&pdev->dev,
11109 "Error %d allocating resources for existing VFs\n",
11110 err);
11111 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011112 }
Greg Rosedf805f62014-04-04 04:43:16 +000011113#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011114
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011115 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11116 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11117 pf->num_iwarp_msix,
11118 I40E_IWARP_IRQ_PILE_ID);
11119 if (pf->iwarp_base_vector < 0) {
11120 dev_info(&pdev->dev,
11121 "failed to get tracking for %d vectors for IWARP err=%d\n",
11122 pf->num_iwarp_msix, pf->iwarp_base_vector);
11123 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11124 }
11125 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011126
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011127 i40e_dbg_pf_init(pf);
11128
11129 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011130 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011131
11132 /* since everything's happy, start the service_task timer */
11133 mod_timer(&pf->service_timer,
11134 round_jiffies(jiffies + pf->service_timer_period));
11135
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011136 /* add this PF to client device list and launch a client service task */
11137 err = i40e_lan_add_device(pf);
11138 if (err)
11139 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11140 err);
11141
Vasu Dev38e00432014-08-01 13:27:03 -070011142#ifdef I40E_FCOE
11143 /* create FCoE interface */
11144 i40e_fcoe_vsi_setup(pf);
11145
11146#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011147#define PCI_SPEED_SIZE 8
11148#define PCI_WIDTH_SIZE 8
11149 /* Devices on the IOSF bus do not have this information
11150 * and will report PCI Gen 1 x 1 by default so don't bother
11151 * checking them.
11152 */
11153 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11154 char speed[PCI_SPEED_SIZE] = "Unknown";
11155 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011156
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011157 /* Get the negotiated link width and speed from PCI config
11158 * space
11159 */
11160 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11161 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011162
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011163 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011164
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011165 switch (hw->bus.speed) {
11166 case i40e_bus_speed_8000:
11167 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11168 case i40e_bus_speed_5000:
11169 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11170 case i40e_bus_speed_2500:
11171 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11172 default:
11173 break;
11174 }
11175 switch (hw->bus.width) {
11176 case i40e_bus_width_pcie_x8:
11177 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11178 case i40e_bus_width_pcie_x4:
11179 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11180 case i40e_bus_width_pcie_x2:
11181 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11182 case i40e_bus_width_pcie_x1:
11183 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11184 default:
11185 break;
11186 }
11187
11188 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11189 speed, width);
11190
11191 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11192 hw->bus.speed < i40e_bus_speed_8000) {
11193 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11194 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11195 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011196 }
11197
Catherine Sullivane8278452015-02-06 08:52:08 +000011198 /* get the requested speeds from the fw */
11199 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11200 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011201 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11202 i40e_stat_str(&pf->hw, err),
11203 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011204 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11205
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011206 /* get the supported phy types from the fw */
11207 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11208 if (err)
11209 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11210 i40e_stat_str(&pf->hw, err),
11211 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11212 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11213
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011214 /* Add a filter to drop all Flow control frames from any VSI from being
11215 * transmitted. By doing so we stop a malicious VF from sending out
11216 * PAUSE or PFC frames and potentially controlling traffic for other
11217 * PF/VF VSIs.
11218 * The FW can still send Flow control frames if enabled.
11219 */
11220 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11221 pf->main_vsi_seid);
11222
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011223 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11224 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11225 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11226
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011227 /* print a string summarizing features */
11228 i40e_print_features(pf);
11229
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011230 return 0;
11231
11232 /* Unwind what we've done if something failed in the setup */
11233err_vsis:
11234 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011235 i40e_clear_interrupt_scheme(pf);
11236 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011237err_switch_setup:
11238 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011239 del_timer_sync(&pf->service_timer);
11240err_mac_addr:
11241err_configure_lan_hmc:
11242 (void)i40e_shutdown_lan_hmc(hw);
11243err_init_lan_hmc:
11244 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011245err_sw_init:
11246err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011247err_pf_reset:
11248 iounmap(hw->hw_addr);
11249err_ioremap:
11250 kfree(pf);
11251err_pf_alloc:
11252 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011253 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011254err_pci_reg:
11255err_dma:
11256 pci_disable_device(pdev);
11257 return err;
11258}
11259
11260/**
11261 * i40e_remove - Device removal routine
11262 * @pdev: PCI device information struct
11263 *
11264 * i40e_remove is called by the PCI subsystem to alert the driver
11265 * that is should release a PCI device. This could be caused by a
11266 * Hot-Plug event, or because the driver is going to be removed from
11267 * memory.
11268 **/
11269static void i40e_remove(struct pci_dev *pdev)
11270{
11271 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011272 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011273 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011274 int i;
11275
11276 i40e_dbg_pf_exit(pf);
11277
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011278 i40e_ptp_stop(pf);
11279
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011280 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011281 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11282 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011284 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011285 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011286 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011287 if (pf->service_timer.data)
11288 del_timer_sync(&pf->service_timer);
11289 if (pf->service_task.func)
11290 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011291
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011292 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11293 i40e_free_vfs(pf);
11294 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11295 }
11296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011297 i40e_fdir_teardown(pf);
11298
11299 /* If there is a switch structure or any orphans, remove them.
11300 * This will leave only the PF's VSI remaining.
11301 */
11302 for (i = 0; i < I40E_MAX_VEB; i++) {
11303 if (!pf->veb[i])
11304 continue;
11305
11306 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11307 pf->veb[i]->uplink_seid == 0)
11308 i40e_switch_branch_release(pf->veb[i]);
11309 }
11310
11311 /* Now we can shutdown the PF's VSI, just before we kill
11312 * adminq and hmc.
11313 */
11314 if (pf->vsi[pf->lan_vsi])
11315 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11316
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011317 /* remove attached clients */
11318 ret_code = i40e_lan_del_device(pf);
11319 if (ret_code) {
11320 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11321 ret_code);
11322 }
11323
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011324 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011325 if (hw->hmc.hmc_obj) {
11326 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011327 if (ret_code)
11328 dev_warn(&pdev->dev,
11329 "Failed to destroy the HMC resources: %d\n",
11330 ret_code);
11331 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011332
11333 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011334 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011335
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011336 /* destroy the locks only once, here */
11337 mutex_destroy(&hw->aq.arq_mutex);
11338 mutex_destroy(&hw->aq.asq_mutex);
11339
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011340 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11341 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011342 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011343 if (pf->vsi[i]) {
11344 i40e_vsi_clear_rings(pf->vsi[i]);
11345 i40e_vsi_clear(pf->vsi[i]);
11346 pf->vsi[i] = NULL;
11347 }
11348 }
11349
11350 for (i = 0; i < I40E_MAX_VEB; i++) {
11351 kfree(pf->veb[i]);
11352 pf->veb[i] = NULL;
11353 }
11354
11355 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011356 kfree(pf->vsi);
11357
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011358 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011359 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011360 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011361
11362 pci_disable_pcie_error_reporting(pdev);
11363 pci_disable_device(pdev);
11364}
11365
11366/**
11367 * i40e_pci_error_detected - warning that something funky happened in PCI land
11368 * @pdev: PCI device information struct
11369 *
11370 * Called to warn that something happened and the error handling steps
11371 * are in progress. Allows the driver to quiesce things, be ready for
11372 * remediation.
11373 **/
11374static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11375 enum pci_channel_state error)
11376{
11377 struct i40e_pf *pf = pci_get_drvdata(pdev);
11378
11379 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11380
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011381 if (!pf) {
11382 dev_info(&pdev->dev,
11383 "Cannot recover - error happened during device probe\n");
11384 return PCI_ERS_RESULT_DISCONNECT;
11385 }
11386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011387 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011388 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11389 rtnl_lock();
11390 i40e_prep_for_reset(pf);
11391 rtnl_unlock();
11392 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011393
11394 /* Request a slot reset */
11395 return PCI_ERS_RESULT_NEED_RESET;
11396}
11397
11398/**
11399 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11400 * @pdev: PCI device information struct
11401 *
11402 * Called to find if the driver can work with the device now that
11403 * the pci slot has been reset. If a basic connection seems good
11404 * (registers are readable and have sane content) then return a
11405 * happy little PCI_ERS_RESULT_xxx.
11406 **/
11407static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11408{
11409 struct i40e_pf *pf = pci_get_drvdata(pdev);
11410 pci_ers_result_t result;
11411 int err;
11412 u32 reg;
11413
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011414 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011415 if (pci_enable_device_mem(pdev)) {
11416 dev_info(&pdev->dev,
11417 "Cannot re-enable PCI device after reset.\n");
11418 result = PCI_ERS_RESULT_DISCONNECT;
11419 } else {
11420 pci_set_master(pdev);
11421 pci_restore_state(pdev);
11422 pci_save_state(pdev);
11423 pci_wake_from_d3(pdev, false);
11424
11425 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11426 if (reg == 0)
11427 result = PCI_ERS_RESULT_RECOVERED;
11428 else
11429 result = PCI_ERS_RESULT_DISCONNECT;
11430 }
11431
11432 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11433 if (err) {
11434 dev_info(&pdev->dev,
11435 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11436 err);
11437 /* non-fatal, continue */
11438 }
11439
11440 return result;
11441}
11442
11443/**
11444 * i40e_pci_error_resume - restart operations after PCI error recovery
11445 * @pdev: PCI device information struct
11446 *
11447 * Called to allow the driver to bring things back up after PCI error
11448 * and/or reset recovery has finished.
11449 **/
11450static void i40e_pci_error_resume(struct pci_dev *pdev)
11451{
11452 struct i40e_pf *pf = pci_get_drvdata(pdev);
11453
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011454 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011455 if (test_bit(__I40E_SUSPENDED, &pf->state))
11456 return;
11457
11458 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011459 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011460 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011461}
11462
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011463/**
11464 * i40e_shutdown - PCI callback for shutting down
11465 * @pdev: PCI device information struct
11466 **/
11467static void i40e_shutdown(struct pci_dev *pdev)
11468{
11469 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011470 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011471
11472 set_bit(__I40E_SUSPENDED, &pf->state);
11473 set_bit(__I40E_DOWN, &pf->state);
11474 rtnl_lock();
11475 i40e_prep_for_reset(pf);
11476 rtnl_unlock();
11477
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011478 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11479 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11480
Catherine Sullivan02b42492015-07-10 19:35:59 -040011481 del_timer_sync(&pf->service_timer);
11482 cancel_work_sync(&pf->service_task);
11483 i40e_fdir_teardown(pf);
11484
11485 rtnl_lock();
11486 i40e_prep_for_reset(pf);
11487 rtnl_unlock();
11488
11489 wr32(hw, I40E_PFPM_APM,
11490 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11491 wr32(hw, I40E_PFPM_WUFC,
11492 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11493
Shannon Nelsone1477582015-02-21 06:44:33 +000011494 i40e_clear_interrupt_scheme(pf);
11495
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011496 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011497 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011498 pci_set_power_state(pdev, PCI_D3hot);
11499 }
11500}
11501
11502#ifdef CONFIG_PM
11503/**
11504 * i40e_suspend - PCI callback for moving to D3
11505 * @pdev: PCI device information struct
11506 **/
11507static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11508{
11509 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011510 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011511 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011512
11513 set_bit(__I40E_SUSPENDED, &pf->state);
11514 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011515
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011516 rtnl_lock();
11517 i40e_prep_for_reset(pf);
11518 rtnl_unlock();
11519
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011520 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11521 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11522
Greg Roseb33d3b72016-05-16 10:26:37 -070011523 i40e_stop_misc_vector(pf);
11524
Greg Rose059ff692016-05-16 10:26:38 -070011525 retval = pci_save_state(pdev);
11526 if (retval)
11527 return retval;
11528
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011529 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011530 pci_set_power_state(pdev, PCI_D3hot);
11531
Greg Rose059ff692016-05-16 10:26:38 -070011532 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011533}
11534
11535/**
11536 * i40e_resume - PCI callback for waking up from D3
11537 * @pdev: PCI device information struct
11538 **/
11539static int i40e_resume(struct pci_dev *pdev)
11540{
11541 struct i40e_pf *pf = pci_get_drvdata(pdev);
11542 u32 err;
11543
11544 pci_set_power_state(pdev, PCI_D0);
11545 pci_restore_state(pdev);
11546 /* pci_restore_state() clears dev->state_saves, so
11547 * call pci_save_state() again to restore it.
11548 */
11549 pci_save_state(pdev);
11550
11551 err = pci_enable_device_mem(pdev);
11552 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011553 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011554 return err;
11555 }
11556 pci_set_master(pdev);
11557
11558 /* no wakeup events while running */
11559 pci_wake_from_d3(pdev, false);
11560
11561 /* handling the reset will rebuild the device state */
11562 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11563 clear_bit(__I40E_DOWN, &pf->state);
11564 rtnl_lock();
11565 i40e_reset_and_rebuild(pf, false);
11566 rtnl_unlock();
11567 }
11568
11569 return 0;
11570}
11571
11572#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011573static const struct pci_error_handlers i40e_err_handler = {
11574 .error_detected = i40e_pci_error_detected,
11575 .slot_reset = i40e_pci_error_slot_reset,
11576 .resume = i40e_pci_error_resume,
11577};
11578
11579static struct pci_driver i40e_driver = {
11580 .name = i40e_driver_name,
11581 .id_table = i40e_pci_tbl,
11582 .probe = i40e_probe,
11583 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011584#ifdef CONFIG_PM
11585 .suspend = i40e_suspend,
11586 .resume = i40e_resume,
11587#endif
11588 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011589 .err_handler = &i40e_err_handler,
11590 .sriov_configure = i40e_pci_sriov_configure,
11591};
11592
11593/**
11594 * i40e_init_module - Driver registration routine
11595 *
11596 * i40e_init_module is the first routine called when the driver is
11597 * loaded. All it does is register with the PCI subsystem.
11598 **/
11599static int __init i40e_init_module(void)
11600{
11601 pr_info("%s: %s - version %s\n", i40e_driver_name,
11602 i40e_driver_string, i40e_driver_version_str);
11603 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011604
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011605 /* we will see if single thread per module is enough for now,
11606 * it can't be any worse than using the system workqueue which
11607 * was already single threaded
11608 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011609 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11610 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011611 if (!i40e_wq) {
11612 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11613 return -ENOMEM;
11614 }
11615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011616 i40e_dbg_init();
11617 return pci_register_driver(&i40e_driver);
11618}
11619module_init(i40e_init_module);
11620
11621/**
11622 * i40e_exit_module - Driver exit cleanup routine
11623 *
11624 * i40e_exit_module is called just before the driver is removed
11625 * from memory.
11626 **/
11627static void __exit i40e_exit_module(void)
11628{
11629 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011630 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011631 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011632}
11633module_exit(i40e_exit_module);