blob: 0c2328dab8f7963b3cf15555a9551548d6bade56 [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 Pujaricf465fe2016-09-27 11:28:54 -070044#define DRV_VERSION_BUILD 21
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 Kirsherb40c82e62015-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 Kirsherb40c82e62015-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 Kirsherb40c82e62015-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 Kirsherb40c82e62015-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 Kirsherb40c82e62015-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/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001290 * i40e_add_filter - Add a mac/vlan filter to the VSI
1291 * @vsi: the VSI to be searched
1292 * @macaddr: the MAC address
1293 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001294 * @is_vf: make sure its a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001295 * @is_netdev: make sure its a netdev filter, else doesn't matter
1296 *
1297 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001298 *
1299 * NOTE: This function is expected to be called with mac_filter_list_lock
1300 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001301 **/
1302struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1303 u8 *macaddr, s16 vlan,
1304 bool is_vf, bool is_netdev)
1305{
1306 struct i40e_mac_filter *f;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001307 int changed = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001308
1309 if (!vsi || !macaddr)
1310 return NULL;
1311
Kiran Patilf6bd0962016-06-20 09:10:34 -07001312 /* Do not allow broadcast filter to be added since broadcast filter
1313 * is added as part of add VSI for any newly created VSI except
1314 * FDIR VSI
1315 */
1316 if (is_broadcast_ether_addr(macaddr))
1317 return NULL;
1318
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001319 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1320 if (!f) {
1321 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1322 if (!f)
1323 goto add_filter_out;
1324
Greg Rose9a173902014-05-22 06:32:02 +00001325 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001326 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001327 /* If we're in overflow promisc mode, set the state directly
1328 * to failed, so we don't bother to try sending the filter
1329 * to the hardware.
1330 */
1331 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1332 f->state = I40E_FILTER_FAILED;
1333 else
1334 f->state = I40E_FILTER_NEW;
1335 changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001336 INIT_LIST_HEAD(&f->list);
Kiran Patil04d5a212015-12-09 15:50:23 -08001337 list_add_tail(&f->list, &vsi->mac_filter_list);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001338 }
1339
1340 /* increment counter and add a new flag if needed */
1341 if (is_vf) {
1342 if (!f->is_vf) {
1343 f->is_vf = true;
1344 f->counter++;
1345 }
1346 } else if (is_netdev) {
1347 if (!f->is_netdev) {
1348 f->is_netdev = true;
1349 f->counter++;
1350 }
1351 } else {
1352 f->counter++;
1353 }
1354
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001355 if (changed) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001356 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1357 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1358 }
1359
1360add_filter_out:
1361 return f;
1362}
1363
1364/**
1365 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1366 * @vsi: the VSI to be searched
1367 * @macaddr: the MAC address
1368 * @vlan: the vlan
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001369 * @is_vf: make sure it's a VF filter, else doesn't matter
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001370 * @is_netdev: make sure it's a netdev filter, else doesn't matter
Kiran Patil21659032015-09-30 14:09:03 -04001371 *
1372 * NOTE: This function is expected to be called with mac_filter_list_lock
1373 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001374 * ANOTHER NOTE: This function MUST be called from within the context of
1375 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1376 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001377 **/
1378void i40e_del_filter(struct i40e_vsi *vsi,
1379 u8 *macaddr, s16 vlan,
1380 bool is_vf, bool is_netdev)
1381{
1382 struct i40e_mac_filter *f;
1383
1384 if (!vsi || !macaddr)
1385 return;
1386
1387 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1388 if (!f || f->counter == 0)
1389 return;
1390
1391 if (is_vf) {
1392 if (f->is_vf) {
1393 f->is_vf = false;
1394 f->counter--;
1395 }
1396 } else if (is_netdev) {
1397 if (f->is_netdev) {
1398 f->is_netdev = false;
1399 f->counter--;
1400 }
1401 } else {
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00001402 /* make sure we don't remove a filter in use by VF or netdev */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001403 int min_f = 0;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001404
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001405 min_f += (f->is_vf ? 1 : 0);
1406 min_f += (f->is_netdev ? 1 : 0);
1407
1408 if (f->counter > min_f)
1409 f->counter--;
1410 }
1411
1412 /* counter == 0 tells sync_filters_subtask to
1413 * remove the filter from the firmware's list
1414 */
1415 if (f->counter == 0) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001416 if ((f->state == I40E_FILTER_FAILED) ||
1417 (f->state == I40E_FILTER_NEW)) {
1418 /* this one never got added by the FW. Just remove it,
1419 * no need to sync anything.
1420 */
1421 list_del(&f->list);
1422 kfree(f);
1423 } else {
1424 f->state = I40E_FILTER_REMOVE;
1425 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1426 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1427 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001428 }
1429}
1430
1431/**
1432 * i40e_set_mac - NDO callback to set mac address
1433 * @netdev: network interface device structure
1434 * @p: pointer to an address structure
1435 *
1436 * Returns 0 on success, negative on failure
1437 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001438#ifdef I40E_FCOE
1439int i40e_set_mac(struct net_device *netdev, void *p)
1440#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001441static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001442#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001443{
1444 struct i40e_netdev_priv *np = netdev_priv(netdev);
1445 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001446 struct i40e_pf *pf = vsi->back;
1447 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001448 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449
1450 if (!is_valid_ether_addr(addr->sa_data))
1451 return -EADDRNOTAVAIL;
1452
Shannon Nelson30650cc2014-07-29 04:01:50 +00001453 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1454 netdev_info(netdev, "already using mac address %pM\n",
1455 addr->sa_data);
1456 return 0;
1457 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001458
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001459 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1460 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1461 return -EADDRNOTAVAIL;
1462
Shannon Nelson30650cc2014-07-29 04:01:50 +00001463 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1464 netdev_info(netdev, "returning to hw mac address %pM\n",
1465 hw->mac.addr);
1466 else
1467 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1468
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001469 spin_lock_bh(&vsi->mac_filter_list_lock);
1470 i40e_del_mac_all_vlan(vsi, netdev->dev_addr, false, true);
1471 i40e_put_mac_in_vlan(vsi, addr->sa_data, false, true);
1472 spin_unlock_bh(&vsi->mac_filter_list_lock);
1473 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001474 if (vsi->type == I40E_VSI_MAIN) {
1475 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001476
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001477 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001478 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001479 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001480 if (ret)
1481 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1482 i40e_stat_str(hw, ret),
1483 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001484 }
1485
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001486 /* schedule our worker thread which will take care of
1487 * applying the new filter changes
1488 */
1489 i40e_service_event_schedule(vsi->back);
1490 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001491}
1492
1493/**
1494 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1495 * @vsi: the VSI being setup
1496 * @ctxt: VSI context structure
1497 * @enabled_tc: Enabled TCs bitmap
1498 * @is_add: True if called before Add VSI
1499 *
1500 * Setup VSI queue mapping for enabled traffic classes.
1501 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001502#ifdef I40E_FCOE
1503void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1504 struct i40e_vsi_context *ctxt,
1505 u8 enabled_tc,
1506 bool is_add)
1507#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001508static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1509 struct i40e_vsi_context *ctxt,
1510 u8 enabled_tc,
1511 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001512#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001513{
1514 struct i40e_pf *pf = vsi->back;
1515 u16 sections = 0;
1516 u8 netdev_tc = 0;
1517 u16 numtc = 0;
1518 u16 qcount;
1519 u8 offset;
1520 u16 qmap;
1521 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001522 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001523
1524 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1525 offset = 0;
1526
1527 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1528 /* Find numtc from enabled TC bitmap */
1529 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001530 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001531 numtc++;
1532 }
1533 if (!numtc) {
1534 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1535 numtc = 1;
1536 }
1537 } else {
1538 /* At least TC0 is enabled in case of non-DCB case */
1539 numtc = 1;
1540 }
1541
1542 vsi->tc_config.numtc = numtc;
1543 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001544 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001545 qcount = vsi->alloc_queue_pairs;
1546
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001547 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001548 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549
1550 /* Setup queue offset/count for all TCs for given VSI */
1551 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1552 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001553 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001554 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555 int pow, num_qps;
1556
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001557 switch (vsi->type) {
1558 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001559 qcount = min_t(int, pf->alloc_rss_size,
1560 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001561 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001562#ifdef I40E_FCOE
1563 case I40E_VSI_FCOE:
1564 qcount = num_tc_qps;
1565 break;
1566#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001567 case I40E_VSI_FDIR:
1568 case I40E_VSI_SRIOV:
1569 case I40E_VSI_VMDQ2:
1570 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001571 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 WARN_ON(i != 0);
1573 break;
1574 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001575 vsi->tc_config.tc_info[i].qoffset = offset;
1576 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001577
Shannon Nelson1e200e42015-02-27 09:15:24 +00001578 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001579 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001580 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001581 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 pow++;
1583 num_qps >>= 1;
1584 }
1585
1586 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1587 qmap =
1588 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1589 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1590
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001591 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001592 } else {
1593 /* TC is not enabled so set the offset to
1594 * default queue and allocate one queue
1595 * for the given TC.
1596 */
1597 vsi->tc_config.tc_info[i].qoffset = 0;
1598 vsi->tc_config.tc_info[i].qcount = 1;
1599 vsi->tc_config.tc_info[i].netdev_tc = 0;
1600
1601 qmap = 0;
1602 }
1603 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1604 }
1605
1606 /* Set actual Tx/Rx queue pairs */
1607 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001608 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1609 if (vsi->req_queue_pairs > 0)
1610 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001611 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001612 vsi->num_queue_pairs = pf->num_lan_msix;
1613 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614
1615 /* Scheduler section valid can only be set for ADD VSI */
1616 if (is_add) {
1617 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1618
1619 ctxt->info.up_enable_bits = enabled_tc;
1620 }
1621 if (vsi->type == I40E_VSI_SRIOV) {
1622 ctxt->info.mapping_flags |=
1623 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1624 for (i = 0; i < vsi->num_queue_pairs; i++)
1625 ctxt->info.queue_mapping[i] =
1626 cpu_to_le16(vsi->base_queue + i);
1627 } else {
1628 ctxt->info.mapping_flags |=
1629 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1630 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1631 }
1632 ctxt->info.valid_sections |= cpu_to_le16(sections);
1633}
1634
1635/**
1636 * i40e_set_rx_mode - NDO callback to set the netdev filters
1637 * @netdev: network interface device structure
1638 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001639#ifdef I40E_FCOE
1640void i40e_set_rx_mode(struct net_device *netdev)
1641#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001642static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001643#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001644{
1645 struct i40e_netdev_priv *np = netdev_priv(netdev);
1646 struct i40e_mac_filter *f, *ftmp;
1647 struct i40e_vsi *vsi = np->vsi;
1648 struct netdev_hw_addr *uca;
1649 struct netdev_hw_addr *mca;
1650 struct netdev_hw_addr *ha;
1651
Kiran Patil21659032015-09-30 14:09:03 -04001652 spin_lock_bh(&vsi->mac_filter_list_lock);
1653
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001654 /* add addr if not already in the filter list */
1655 netdev_for_each_uc_addr(uca, netdev) {
1656 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1657 if (i40e_is_vsi_in_vlan(vsi))
1658 i40e_put_mac_in_vlan(vsi, uca->addr,
1659 false, true);
1660 else
1661 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1662 false, true);
1663 }
1664 }
1665
1666 netdev_for_each_mc_addr(mca, netdev) {
1667 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1668 if (i40e_is_vsi_in_vlan(vsi))
1669 i40e_put_mac_in_vlan(vsi, mca->addr,
1670 false, true);
1671 else
1672 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1673 false, true);
1674 }
1675 }
1676
1677 /* remove filter if not in netdev list */
1678 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001679
1680 if (!f->is_netdev)
1681 continue;
1682
Shannon Nelson2f41f332015-08-26 15:14:20 -04001683 netdev_for_each_mc_addr(mca, netdev)
1684 if (ether_addr_equal(mca->addr, f->macaddr))
1685 goto bottom_of_search_loop;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001686
Shannon Nelson2f41f332015-08-26 15:14:20 -04001687 netdev_for_each_uc_addr(uca, netdev)
1688 if (ether_addr_equal(uca->addr, f->macaddr))
1689 goto bottom_of_search_loop;
1690
1691 for_each_dev_addr(netdev, ha)
1692 if (ether_addr_equal(ha->addr, f->macaddr))
1693 goto bottom_of_search_loop;
1694
1695 /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
1696 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1697
1698bottom_of_search_loop:
1699 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001700 }
Kiran Patil21659032015-09-30 14:09:03 -04001701 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001702
1703 /* check for other flag changes */
1704 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1705 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1706 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1707 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001708
1709 /* schedule our worker thread which will take care of
1710 * applying the new filter changes
1711 */
1712 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001713}
1714
1715/**
Kiran Patil21659032015-09-30 14:09:03 -04001716 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1717 * @vsi: pointer to vsi struct
1718 * @from: Pointer to list which contains MAC filter entries - changes to
1719 * those entries needs to be undone.
1720 *
1721 * MAC filter entries from list were slated to be removed from device.
1722 **/
1723static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1724 struct list_head *from)
1725{
1726 struct i40e_mac_filter *f, *ftmp;
1727
1728 list_for_each_entry_safe(f, ftmp, from, list) {
Kiran Patil21659032015-09-30 14:09:03 -04001729 /* Move the element back into MAC filter list*/
1730 list_move_tail(&f->list, &vsi->mac_filter_list);
1731 }
1732}
1733
1734/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001735 * i40e_update_filter_state - Update filter state based on return data
1736 * from firmware
1737 * @count: Number of filters added
1738 * @add_list: return data from fw
1739 * @head: pointer to first filter in current batch
1740 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001741 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001742 * MAC filter entries from list were slated to be added to device. Returns
1743 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001744 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001745static int
1746i40e_update_filter_state(int count,
1747 struct i40e_aqc_add_macvlan_element_data *add_list,
1748 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001749{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001750 int retval = 0;
1751 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001752
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001753
1754 if (!aq_err) {
1755 retval = count;
1756 /* Everything's good, mark all filters active. */
1757 for (i = 0; i < count ; i++) {
1758 add_head->state = I40E_FILTER_ACTIVE;
1759 add_head = list_next_entry(add_head, list);
1760 }
1761 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1762 /* Device ran out of filter space. Check the return value
1763 * for each filter to see which ones are active.
1764 */
1765 for (i = 0; i < count ; i++) {
1766 if (add_list[i].match_method ==
1767 I40E_AQC_MM_ERR_NO_RES) {
1768 add_head->state = I40E_FILTER_FAILED;
1769 } else {
1770 add_head->state = I40E_FILTER_ACTIVE;
1771 retval++;
1772 }
1773 add_head = list_next_entry(add_head, list);
1774 }
1775 } else {
1776 /* Some other horrible thing happened, fail all filters */
1777 retval = 0;
1778 for (i = 0; i < count ; i++) {
1779 add_head->state = I40E_FILTER_FAILED;
1780 add_head = list_next_entry(add_head, list);
1781 }
Kiran Patil21659032015-09-30 14:09:03 -04001782 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001783 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001784}
1785
1786/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001787 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1788 * @vsi: ptr to the VSI
1789 *
1790 * Push any outstanding VSI filter changes through the AdminQ.
1791 *
1792 * Returns 0 or error value
1793 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001794int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001795{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001796 struct i40e_mac_filter *f, *ftmp, *add_head = NULL;
1797 struct list_head tmp_add_list, tmp_del_list;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001798 struct i40e_hw *hw = &vsi->back->hw;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001799 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001800 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001801 int filter_list_len = 0;
1802 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001803 i40e_status aq_ret = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001804 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001805 struct i40e_pf *pf;
1806 int num_add = 0;
1807 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001808 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001809 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001810 int list_size;
1811 int fcnt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001812
1813 /* empty array typed pointers, kcalloc later */
1814 struct i40e_aqc_add_macvlan_element_data *add_list;
1815 struct i40e_aqc_remove_macvlan_element_data *del_list;
1816
1817 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1818 usleep_range(1000, 2000);
1819 pf = vsi->back;
1820
1821 if (vsi->netdev) {
1822 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1823 vsi->current_netdev_flags = vsi->netdev->flags;
1824 }
1825
Kiran Patil21659032015-09-30 14:09:03 -04001826 INIT_LIST_HEAD(&tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001827 INIT_LIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001828
Shannon Nelson2d1de822016-05-16 10:26:44 -07001829 if (vsi->type == I40E_VSI_SRIOV)
1830 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1831 else if (vsi->type != I40E_VSI_MAIN)
1832 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1833
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001834 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1835 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1836
Kiran Patil21659032015-09-30 14:09:03 -04001837 spin_lock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001838 /* Create a list of filters to delete. */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001839 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001840 if (f->state == I40E_FILTER_REMOVE) {
1841 WARN_ON(f->counter != 0);
1842 /* Move the element into temporary del_list */
1843 list_move_tail(&f->list, &tmp_del_list);
1844 vsi->active_filters--;
Kiran Patil21659032015-09-30 14:09:03 -04001845 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001846 if (f->state == I40E_FILTER_NEW) {
1847 WARN_ON(f->counter == 0);
1848 /* Move the element into temporary add_list */
1849 list_move_tail(&f->list, &tmp_add_list);
1850 }
Kiran Patil21659032015-09-30 14:09:03 -04001851 }
1852 spin_unlock_bh(&vsi->mac_filter_list_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001853 }
1854
1855 /* Now process 'del_list' outside the lock */
1856 if (!list_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001857 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04001858 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001859 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08001860 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001861 del_list = kzalloc(list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001862 if (!del_list) {
Kiran Patil21659032015-09-30 14:09:03 -04001863 /* Undo VSI's MAC filter entry element updates */
1864 spin_lock_bh(&vsi->mac_filter_list_lock);
1865 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001866 spin_unlock_bh(&vsi->mac_filter_list_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001867 retval = -ENOMEM;
1868 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001869 }
1870
1871 list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001872 cmd_flags = 0;
1873
1874 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001875 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001876 if (f->vlan == I40E_VLAN_ANY) {
1877 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07001878 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001879 } else {
1880 del_list[num_del].vlan_tag =
1881 cpu_to_le16((u16)(f->vlan));
1882 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001883
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001884 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1885 del_list[num_del].flags = cmd_flags;
1886 num_del++;
1887
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001888 /* flush a full buffer */
1889 if (num_del == filter_list_len) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001890 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid,
1891 del_list,
1892 num_del, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001893 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001894 num_del = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001895 memset(del_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001896
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001897 /* Explicitly ignore and do not report when
1898 * firmware returns ENOENT.
1899 */
1900 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001901 retval = -EIO;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001902 dev_info(&pf->pdev->dev,
1903 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
Shannon Nelson2d1de822016-05-16 10:26:44 -07001904 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001905 i40e_stat_str(hw, aq_ret),
1906 i40e_aq_str(hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001907 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001908 }
Kiran Patil21659032015-09-30 14:09:03 -04001909 /* Release memory for MAC filter entries which were
1910 * synced up with HW.
1911 */
1912 list_del(&f->list);
1913 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001914 }
Kiran Patil21659032015-09-30 14:09:03 -04001915
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001916 if (num_del) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001917 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, del_list,
1918 num_del, NULL);
1919 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001920 num_del = 0;
1921
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001922 /* Explicitly ignore and do not report when firmware
1923 * returns ENOENT.
1924 */
1925 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1926 retval = -EIO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001927 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07001928 "ignoring delete macvlan error on %s, err %s aq_err %s\n",
1929 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001930 i40e_stat_str(hw, aq_ret),
1931 i40e_aq_str(hw, aq_err));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001932 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001933 }
1934
1935 kfree(del_list);
1936 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001937 }
1938
1939 if (!list_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001940 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001941 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08001942 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001943 list_size = filter_list_len *
1944 sizeof(struct i40e_aqc_add_macvlan_element_data);
1945 add_list = kzalloc(list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001946 if (!add_list) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001947 retval = -ENOMEM;
1948 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001949 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001950 num_add = 0;
1951 list_for_each_entry(f, &tmp_add_list, list) {
1952 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1953 &vsi->state)) {
1954 f->state = I40E_FILTER_FAILED;
1955 continue;
1956 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001957 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001958 if (num_add == 0)
1959 add_head = f;
1960 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00001961 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001962 if (f->vlan == I40E_VLAN_ANY) {
1963 add_list[num_add].vlan_tag = 0;
1964 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1965 } else {
1966 add_list[num_add].vlan_tag =
1967 cpu_to_le16((u16)(f->vlan));
1968 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001969 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001970 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001971 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1972 num_add++;
1973
1974 /* flush a full buffer */
1975 if (num_add == filter_list_len) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001976 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08001977 add_list, num_add,
1978 NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001979 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001980 fcnt = i40e_update_filter_state(num_add,
1981 add_list,
1982 add_head,
1983 aq_ret);
1984 vsi->active_filters += fcnt;
1985
1986 if (fcnt != num_add) {
1987 promisc_changed = true;
1988 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1989 &vsi->state);
1990 vsi->promisc_threshold =
1991 (vsi->active_filters * 3) / 4;
1992 dev_warn(&pf->pdev->dev,
1993 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1994 i40e_aq_str(hw, aq_err),
1995 vsi_name);
1996 }
1997 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001998 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001999 }
2000 }
2001 if (num_add) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002002 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08002003 add_list, num_add, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002004 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002005 fcnt = i40e_update_filter_state(num_add, add_list,
2006 add_head, aq_ret);
2007 vsi->active_filters += fcnt;
2008 if (fcnt != num_add) {
2009 promisc_changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002010 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2011 &vsi->state);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002012 vsi->promisc_threshold =
2013 (vsi->active_filters * 3) / 4;
2014 dev_warn(&pf->pdev->dev,
2015 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2016 i40e_aq_str(hw, aq_err), vsi_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002017 }
2018 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002019 /* Now move all of the filters from the temp add list back to
2020 * the VSI's list.
2021 */
2022 spin_lock_bh(&vsi->mac_filter_list_lock);
2023 list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
2024 list_move_tail(&f->list, &vsi->mac_filter_list);
2025 }
2026 spin_unlock_bh(&vsi->mac_filter_list_lock);
2027 kfree(add_list);
2028 add_list = NULL;
2029 }
2030
2031 /* Check to see if we can drop out of overflow promiscuous mode. */
2032 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2033 (vsi->active_filters < vsi->promisc_threshold)) {
2034 int failed_count = 0;
2035 /* See if we have any failed filters. We can't drop out of
2036 * promiscuous until these have all been deleted.
2037 */
2038 spin_lock_bh(&vsi->mac_filter_list_lock);
2039 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2040 if (f->state == I40E_FILTER_FAILED)
2041 failed_count++;
2042 }
2043 spin_unlock_bh(&vsi->mac_filter_list_lock);
2044 if (!failed_count) {
2045 dev_info(&pf->pdev->dev,
2046 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2047 vsi_name);
2048 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2049 promisc_changed = true;
2050 vsi->promisc_threshold = 0;
2051 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002052 }
2053
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002054 /* if the VF is not trusted do not do promisc */
2055 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2056 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2057 goto out;
2058 }
2059
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002060 /* check for changes in promiscuous modes */
2061 if (changed_flags & IFF_ALLMULTI) {
2062 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002063
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002064 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002065 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2066 vsi->seid,
2067 cur_multipromisc,
2068 NULL);
2069 if (aq_ret) {
2070 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002071 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002072 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002073 "set multi promisc failed on %s, err %s aq_err %s\n",
2074 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002075 i40e_stat_str(hw, aq_ret),
2076 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002077 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002078 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002079 if ((changed_flags & IFF_PROMISC) ||
2080 (promisc_changed &&
2081 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002082 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002083
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002084 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2085 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2086 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002087 if ((vsi->type == I40E_VSI_MAIN) &&
2088 (pf->lan_veb != I40E_NO_VEB) &&
2089 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002090 /* set defport ON for Main VSI instead of true promisc
2091 * this way we will get all unicast/multicast and VLAN
2092 * promisc behavior but will not get VF or VMDq traffic
2093 * replicated on the Main VSI.
2094 */
2095 if (pf->cur_promisc != cur_promisc) {
2096 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002097 if (cur_promisc)
2098 aq_ret =
2099 i40e_aq_set_default_vsi(hw,
2100 vsi->seid,
2101 NULL);
2102 else
2103 aq_ret =
2104 i40e_aq_clear_default_vsi(hw,
2105 vsi->seid,
2106 NULL);
2107 if (aq_ret) {
2108 retval = i40e_aq_rc_to_posix(aq_ret,
2109 hw->aq.asq_last_status);
2110 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002111 "Set default VSI failed on %s, err %s, aq_err %s\n",
2112 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002113 i40e_stat_str(hw, aq_ret),
2114 i40e_aq_str(hw,
2115 hw->aq.asq_last_status));
2116 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002117 }
2118 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002119 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002120 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002121 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002122 cur_promisc, NULL,
2123 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002124 if (aq_ret) {
2125 retval =
2126 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002127 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002128 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002129 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2130 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002131 i40e_stat_str(hw, aq_ret),
2132 i40e_aq_str(hw,
2133 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002134 }
2135 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002136 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002137 vsi->seid,
2138 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002139 if (aq_ret) {
2140 retval =
2141 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002142 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002143 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002144 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2145 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002146 i40e_stat_str(hw, aq_ret),
2147 i40e_aq_str(hw,
2148 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002149 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002150 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002151 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2152 vsi->seid,
2153 cur_promisc, NULL);
2154 if (aq_ret) {
2155 retval = i40e_aq_rc_to_posix(aq_ret,
2156 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002157 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002158 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002159 i40e_stat_str(hw, aq_ret),
2160 i40e_aq_str(hw,
2161 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002162 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002163 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002164out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002165 /* if something went wrong then set the changed flag so we try again */
2166 if (retval)
2167 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2168
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002169 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002170 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002171}
2172
2173/**
2174 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2175 * @pf: board private structure
2176 **/
2177static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2178{
2179 int v;
2180
2181 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2182 return;
2183 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2184
Mitch Williams505682c2014-05-20 08:01:37 +00002185 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002186 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002187 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2188 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2189
2190 if (ret) {
2191 /* come back and try again later */
2192 pf->flags |= I40E_FLAG_FILTER_SYNC;
2193 break;
2194 }
2195 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002196 }
2197}
2198
2199/**
2200 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2201 * @netdev: network interface device structure
2202 * @new_mtu: new value for maximum frame size
2203 *
2204 * Returns 0 on success, negative on failure
2205 **/
2206static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2207{
2208 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002209 struct i40e_vsi *vsi = np->vsi;
2210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002211 netdev_info(netdev, "changing MTU from %d to %d\n",
2212 netdev->mtu, new_mtu);
2213 netdev->mtu = new_mtu;
2214 if (netif_running(netdev))
2215 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002216 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002217 return 0;
2218}
2219
2220/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002221 * i40e_ioctl - Access the hwtstamp interface
2222 * @netdev: network interface device structure
2223 * @ifr: interface request data
2224 * @cmd: ioctl command
2225 **/
2226int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2227{
2228 struct i40e_netdev_priv *np = netdev_priv(netdev);
2229 struct i40e_pf *pf = np->vsi->back;
2230
2231 switch (cmd) {
2232 case SIOCGHWTSTAMP:
2233 return i40e_ptp_get_ts_config(pf, ifr);
2234 case SIOCSHWTSTAMP:
2235 return i40e_ptp_set_ts_config(pf, ifr);
2236 default:
2237 return -EOPNOTSUPP;
2238 }
2239}
2240
2241/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002242 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2243 * @vsi: the vsi being adjusted
2244 **/
2245void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2246{
2247 struct i40e_vsi_context ctxt;
2248 i40e_status ret;
2249
2250 if ((vsi->info.valid_sections &
2251 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2252 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2253 return; /* already enabled */
2254
2255 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2256 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2257 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2258
2259 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002260 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002261 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2262 if (ret) {
2263 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002264 "update vlan stripping failed, err %s aq_err %s\n",
2265 i40e_stat_str(&vsi->back->hw, ret),
2266 i40e_aq_str(&vsi->back->hw,
2267 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002268 }
2269}
2270
2271/**
2272 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2273 * @vsi: the vsi being adjusted
2274 **/
2275void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2276{
2277 struct i40e_vsi_context ctxt;
2278 i40e_status ret;
2279
2280 if ((vsi->info.valid_sections &
2281 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2282 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2283 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2284 return; /* already disabled */
2285
2286 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2287 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2288 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2289
2290 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002291 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002292 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2293 if (ret) {
2294 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002295 "update vlan stripping failed, err %s aq_err %s\n",
2296 i40e_stat_str(&vsi->back->hw, ret),
2297 i40e_aq_str(&vsi->back->hw,
2298 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002299 }
2300}
2301
2302/**
2303 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2304 * @netdev: network interface to be adjusted
2305 * @features: netdev features to test if VLAN offload is enabled or not
2306 **/
2307static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2308{
2309 struct i40e_netdev_priv *np = netdev_priv(netdev);
2310 struct i40e_vsi *vsi = np->vsi;
2311
2312 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2313 i40e_vlan_stripping_enable(vsi);
2314 else
2315 i40e_vlan_stripping_disable(vsi);
2316}
2317
2318/**
2319 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2320 * @vsi: the vsi being configured
2321 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2322 **/
2323int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2324{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002325 struct i40e_mac_filter *f, *ftmp, *add_f;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002326 bool is_netdev, is_vf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002327
2328 is_vf = (vsi->type == I40E_VSI_SRIOV);
2329 is_netdev = !!(vsi->netdev);
2330
Kiran Patil21659032015-09-30 14:09:03 -04002331 /* Locked once because all functions invoked below iterates list*/
2332 spin_lock_bh(&vsi->mac_filter_list_lock);
2333
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002334 if (is_netdev) {
2335 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
2336 is_vf, is_netdev);
2337 if (!add_f) {
2338 dev_info(&vsi->back->pdev->dev,
2339 "Could not add vlan filter %d for %pM\n",
2340 vid, vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002341 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002342 return -ENOMEM;
2343 }
2344 }
2345
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002346 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002347 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2348 if (!add_f) {
2349 dev_info(&vsi->back->pdev->dev,
2350 "Could not add vlan filter %d for %pM\n",
2351 vid, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002352 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002353 return -ENOMEM;
2354 }
2355 }
2356
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002357 /* Now if we add a vlan tag, make sure to check if it is the first
2358 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2359 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002360 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002361 */
2362 if (vid > 0) {
2363 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
2364 I40E_VLAN_ANY,
2365 is_vf, is_netdev)) {
2366 i40e_del_filter(vsi, vsi->netdev->dev_addr,
2367 I40E_VLAN_ANY, is_vf, is_netdev);
2368 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
2369 is_vf, is_netdev);
2370 if (!add_f) {
2371 dev_info(&vsi->back->pdev->dev,
2372 "Could not add filter 0 for %pM\n",
2373 vsi->netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002374 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002375 return -ENOMEM;
2376 }
2377 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002378 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002379
Greg Rose8d82a7c2014-01-13 16:13:04 -08002380 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2381 if (vid > 0 && !vsi->info.pvid) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002382 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Kiran Patil21659032015-09-30 14:09:03 -04002383 if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2384 is_vf, is_netdev))
2385 continue;
2386 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
2387 is_vf, is_netdev);
2388 add_f = i40e_add_filter(vsi, f->macaddr,
2389 0, is_vf, is_netdev);
2390 if (!add_f) {
2391 dev_info(&vsi->back->pdev->dev,
2392 "Could not add filter 0 for %pM\n",
2393 f->macaddr);
2394 spin_unlock_bh(&vsi->mac_filter_list_lock);
2395 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002396 }
2397 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002398 }
2399
Kiran Patil21659032015-09-30 14:09:03 -04002400 spin_unlock_bh(&vsi->mac_filter_list_lock);
2401
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002402 /* schedule our worker thread which will take care of
2403 * applying the new filter changes
2404 */
2405 i40e_service_event_schedule(vsi->back);
2406 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002407}
2408
2409/**
2410 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2411 * @vsi: the vsi being configured
2412 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002413 *
2414 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002415 **/
2416int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2417{
2418 struct net_device *netdev = vsi->netdev;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002419 struct i40e_mac_filter *f, *ftmp, *add_f;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002420 bool is_vf, is_netdev;
2421 int filter_count = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002422
2423 is_vf = (vsi->type == I40E_VSI_SRIOV);
2424 is_netdev = !!(netdev);
2425
Kiran Patil21659032015-09-30 14:09:03 -04002426 /* Locked once because all functions invoked below iterates list */
2427 spin_lock_bh(&vsi->mac_filter_list_lock);
2428
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429 if (is_netdev)
2430 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
2431
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002432 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002433 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
2434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002435 /* go through all the filters for this VSI and if there is only
2436 * vid == 0 it means there are no other filters, so vid 0 must
2437 * be replaced with -1. This signifies that we should from now
2438 * on accept any traffic (with any tag present, or untagged)
2439 */
2440 list_for_each_entry(f, &vsi->mac_filter_list, list) {
2441 if (is_netdev) {
2442 if (f->vlan &&
2443 ether_addr_equal(netdev->dev_addr, f->macaddr))
2444 filter_count++;
2445 }
2446
2447 if (f->vlan)
2448 filter_count++;
2449 }
2450
2451 if (!filter_count && is_netdev) {
2452 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
2453 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
2454 is_vf, is_netdev);
2455 if (!f) {
2456 dev_info(&vsi->back->pdev->dev,
2457 "Could not add filter %d for %pM\n",
2458 I40E_VLAN_ANY, netdev->dev_addr);
Kiran Patil21659032015-09-30 14:09:03 -04002459 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002460 return -ENOMEM;
2461 }
2462 }
2463
2464 if (!filter_count) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002465 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002466 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
2467 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
Kiran Patil21659032015-09-30 14:09:03 -04002468 is_vf, is_netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002469 if (!add_f) {
2470 dev_info(&vsi->back->pdev->dev,
2471 "Could not add filter %d for %pM\n",
2472 I40E_VLAN_ANY, f->macaddr);
Kiran Patil21659032015-09-30 14:09:03 -04002473 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002474 return -ENOMEM;
2475 }
2476 }
2477 }
2478
Kiran Patil21659032015-09-30 14:09:03 -04002479 spin_unlock_bh(&vsi->mac_filter_list_lock);
2480
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002481 /* schedule our worker thread which will take care of
2482 * applying the new filter changes
2483 */
2484 i40e_service_event_schedule(vsi->back);
2485 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002486}
2487
2488/**
2489 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2490 * @netdev: network interface to be adjusted
2491 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002492 *
2493 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002494 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002495#ifdef I40E_FCOE
2496int i40e_vlan_rx_add_vid(struct net_device *netdev,
2497 __always_unused __be16 proto, u16 vid)
2498#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002499static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2500 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002501#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002502{
2503 struct i40e_netdev_priv *np = netdev_priv(netdev);
2504 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002505 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002506
2507 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002508 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002510 /* If the network stack called us with vid = 0 then
2511 * it is asking to receive priority tagged packets with
2512 * vlan id 0. Our HW receives them by default when configured
2513 * to receive untagged packets so there is no need to add an
2514 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002515 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002516 if (vid)
2517 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002518
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002519 if (!ret && (vid < VLAN_N_VID))
2520 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002521
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002522 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002523}
2524
2525/**
2526 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2527 * @netdev: network interface to be adjusted
2528 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002529 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002530 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002531 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002532#ifdef I40E_FCOE
2533int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2534 __always_unused __be16 proto, u16 vid)
2535#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2537 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002538#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002539{
2540 struct i40e_netdev_priv *np = netdev_priv(netdev);
2541 struct i40e_vsi *vsi = np->vsi;
2542
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002543 /* return code is ignored as there is nothing a user
2544 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002545 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002546 */
2547 i40e_vsi_kill_vlan(vsi, vid);
2548
2549 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002551 return 0;
2552}
2553
2554/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002555 * i40e_macaddr_init - explicitly write the mac address filters
2556 *
2557 * @vsi: pointer to the vsi
2558 * @macaddr: the MAC address
2559 *
2560 * This is needed when the macaddr has been obtained by other
2561 * means than the default, e.g., from Open Firmware or IDPROM.
2562 * Returns 0 on success, negative on failure
2563 **/
2564static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2565{
2566 int ret;
2567 struct i40e_aqc_add_macvlan_element_data element;
2568
2569 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2570 I40E_AQC_WRITE_TYPE_LAA_WOL,
2571 macaddr, NULL);
2572 if (ret) {
2573 dev_info(&vsi->back->pdev->dev,
2574 "Addr change for VSI failed: %d\n", ret);
2575 return -EADDRNOTAVAIL;
2576 }
2577
2578 memset(&element, 0, sizeof(element));
2579 ether_addr_copy(element.mac_addr, macaddr);
2580 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2581 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2582 if (ret) {
2583 dev_info(&vsi->back->pdev->dev,
2584 "add filter failed err %s aq_err %s\n",
2585 i40e_stat_str(&vsi->back->hw, ret),
2586 i40e_aq_str(&vsi->back->hw,
2587 vsi->back->hw.aq.asq_last_status));
2588 }
2589 return ret;
2590}
2591
2592/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002593 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2594 * @vsi: the vsi being brought back up
2595 **/
2596static void i40e_restore_vlan(struct i40e_vsi *vsi)
2597{
2598 u16 vid;
2599
2600 if (!vsi->netdev)
2601 return;
2602
2603 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2604
2605 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2606 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2607 vid);
2608}
2609
2610/**
2611 * i40e_vsi_add_pvid - Add pvid for the VSI
2612 * @vsi: the vsi being adjusted
2613 * @vid: the vlan id to set as a PVID
2614 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002615int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002616{
2617 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002618 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002619
2620 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2621 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002622 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2623 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002624 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002625
2626 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002627 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002628 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2629 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002630 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002631 "add pvid failed, err %s aq_err %s\n",
2632 i40e_stat_str(&vsi->back->hw, ret),
2633 i40e_aq_str(&vsi->back->hw,
2634 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002635 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002636 }
2637
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002638 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002639}
2640
2641/**
2642 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2643 * @vsi: the vsi being adjusted
2644 *
2645 * Just use the vlan_rx_register() service to put it back to normal
2646 **/
2647void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2648{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002649 i40e_vlan_stripping_disable(vsi);
2650
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002652}
2653
2654/**
2655 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2656 * @vsi: ptr to the VSI
2657 *
2658 * If this function returns with an error, then it's possible one or
2659 * more of the rings is populated (while the rest are not). It is the
2660 * callers duty to clean those orphaned rings.
2661 *
2662 * Return 0 on success, negative on failure
2663 **/
2664static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2665{
2666 int i, err = 0;
2667
2668 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002669 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002670
2671 return err;
2672}
2673
2674/**
2675 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2676 * @vsi: ptr to the VSI
2677 *
2678 * Free VSI's transmit software resources
2679 **/
2680static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2681{
2682 int i;
2683
Greg Rose8e9dca52013-12-18 13:45:53 +00002684 if (!vsi->tx_rings)
2685 return;
2686
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002687 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002688 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002689 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002690}
2691
2692/**
2693 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx 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_rx_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_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002708#ifdef I40E_FCOE
2709 i40e_fcoe_setup_ddp_resources(vsi);
2710#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002711 return err;
2712}
2713
2714/**
2715 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2716 * @vsi: ptr to the VSI
2717 *
2718 * Free all receive software resources
2719 **/
2720static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2721{
2722 int i;
2723
Greg Rose8e9dca52013-12-18 13:45:53 +00002724 if (!vsi->rx_rings)
2725 return;
2726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002727 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002728 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002729 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002730#ifdef I40E_FCOE
2731 i40e_fcoe_free_ddp_resources(vsi);
2732#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002733}
2734
2735/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002736 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2737 * @ring: The Tx ring to configure
2738 *
2739 * This enables/disables XPS for a given Tx descriptor ring
2740 * based on the TCs enabled for the VSI that ring belongs to.
2741 **/
2742static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2743{
2744 struct i40e_vsi *vsi = ring->vsi;
2745 cpumask_var_t mask;
2746
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002747 if (!ring->q_vector || !ring->netdev)
2748 return;
2749
2750 /* Single TC mode enable XPS */
2751 if (vsi->tc_config.numtc <= 1) {
2752 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002753 netif_set_xps_queue(ring->netdev,
2754 &ring->q_vector->affinity_mask,
2755 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002756 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2757 /* Disable XPS to allow selection based on TC */
2758 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2759 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2760 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002761 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002762
2763 /* schedule our worker thread which will take care of
2764 * applying the new filter changes
2765 */
2766 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002767}
2768
2769/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002770 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2771 * @ring: The Tx ring to configure
2772 *
2773 * Configure the Tx descriptor ring in the HMC context.
2774 **/
2775static int i40e_configure_tx_ring(struct i40e_ring *ring)
2776{
2777 struct i40e_vsi *vsi = ring->vsi;
2778 u16 pf_q = vsi->base_queue + ring->queue_index;
2779 struct i40e_hw *hw = &vsi->back->hw;
2780 struct i40e_hmc_obj_txq tx_ctx;
2781 i40e_status err = 0;
2782 u32 qtx_ctl = 0;
2783
2784 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002785 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002786 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2787 ring->atr_count = 0;
2788 } else {
2789 ring->atr_sample_rate = 0;
2790 }
2791
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002792 /* configure XPS */
2793 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002794
2795 /* clear the context structure first */
2796 memset(&tx_ctx, 0, sizeof(tx_ctx));
2797
2798 tx_ctx.new_context = 1;
2799 tx_ctx.base = (ring->dma / 128);
2800 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002801 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2802 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002803#ifdef I40E_FCOE
2804 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2805#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002806 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002807 /* FDIR VSI tx ring can still use RS bit and writebacks */
2808 if (vsi->type != I40E_VSI_FDIR)
2809 tx_ctx.head_wb_ena = 1;
2810 tx_ctx.head_wb_addr = ring->dma +
2811 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002812
2813 /* As part of VSI creation/update, FW allocates certain
2814 * Tx arbitration queue sets for each TC enabled for
2815 * the VSI. The FW returns the handles to these queue
2816 * sets as part of the response buffer to Add VSI,
2817 * Update VSI, etc. AQ commands. It is expected that
2818 * these queue set handles be associated with the Tx
2819 * queues by the driver as part of the TX queue context
2820 * initialization. This has to be done regardless of
2821 * DCB as by default everything is mapped to TC0.
2822 */
2823 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2824 tx_ctx.rdylist_act = 0;
2825
2826 /* clear the context in the HMC */
2827 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2828 if (err) {
2829 dev_info(&vsi->back->pdev->dev,
2830 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2831 ring->queue_index, pf_q, err);
2832 return -ENOMEM;
2833 }
2834
2835 /* set the context in the HMC */
2836 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2837 if (err) {
2838 dev_info(&vsi->back->pdev->dev,
2839 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2840 ring->queue_index, pf_q, err);
2841 return -ENOMEM;
2842 }
2843
2844 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002845 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002846 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002847 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2848 I40E_QTX_CTL_VFVM_INDX_MASK;
2849 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002850 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002851 }
2852
Shannon Nelson13fd9772013-09-28 07:14:19 +00002853 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2854 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002855 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2856 i40e_flush(hw);
2857
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002858 /* cache tail off for easier writes later */
2859 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2860
2861 return 0;
2862}
2863
2864/**
2865 * i40e_configure_rx_ring - Configure a receive ring context
2866 * @ring: The Rx ring to configure
2867 *
2868 * Configure the Rx descriptor ring in the HMC context.
2869 **/
2870static int i40e_configure_rx_ring(struct i40e_ring *ring)
2871{
2872 struct i40e_vsi *vsi = ring->vsi;
2873 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2874 u16 pf_q = vsi->base_queue + ring->queue_index;
2875 struct i40e_hw *hw = &vsi->back->hw;
2876 struct i40e_hmc_obj_rxq rx_ctx;
2877 i40e_status err = 0;
2878
2879 ring->state = 0;
2880
2881 /* clear the context structure first */
2882 memset(&rx_ctx, 0, sizeof(rx_ctx));
2883
2884 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002885
2886 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002887
2888 rx_ctx.base = (ring->dma / 128);
2889 rx_ctx.qlen = ring->count;
2890
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002891 /* use 32 byte descriptors */
2892 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002893
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002894 /* descriptor type is always zero
2895 * rx_ctx.dtype = 0;
2896 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002897 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002898
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002899 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002900 if (hw->revision_id == 0)
2901 rx_ctx.lrxqthresh = 0;
2902 else
2903 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002904 rx_ctx.crcstrip = 1;
2905 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002906 /* this controls whether VLAN is stripped from inner headers */
2907 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002908#ifdef I40E_FCOE
2909 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2910#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002911 /* set the prefena field to 1 because the manual says to */
2912 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002913
2914 /* clear the context in the HMC */
2915 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2916 if (err) {
2917 dev_info(&vsi->back->pdev->dev,
2918 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2919 ring->queue_index, pf_q, err);
2920 return -ENOMEM;
2921 }
2922
2923 /* set the context in the HMC */
2924 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2925 if (err) {
2926 dev_info(&vsi->back->pdev->dev,
2927 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2928 ring->queue_index, pf_q, err);
2929 return -ENOMEM;
2930 }
2931
2932 /* cache tail for quicker writes, and clear the reg before use */
2933 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2934 writel(0, ring->tail);
2935
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002936 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002937
2938 return 0;
2939}
2940
2941/**
2942 * i40e_vsi_configure_tx - Configure the VSI for Tx
2943 * @vsi: VSI structure describing this set of rings and resources
2944 *
2945 * Configure the Tx VSI for operation.
2946 **/
2947static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2948{
2949 int err = 0;
2950 u16 i;
2951
Alexander Duyck9f65e152013-09-28 06:00:58 +00002952 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2953 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002954
2955 return err;
2956}
2957
2958/**
2959 * i40e_vsi_configure_rx - Configure the VSI for Rx
2960 * @vsi: the VSI being configured
2961 *
2962 * Configure the Rx VSI for operation.
2963 **/
2964static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2965{
2966 int err = 0;
2967 u16 i;
2968
2969 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2970 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2971 + ETH_FCS_LEN + VLAN_HLEN;
2972 else
2973 vsi->max_frame = I40E_RXBUFFER_2048;
2974
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002975 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002976
Vasu Dev38e00432014-08-01 13:27:03 -07002977#ifdef I40E_FCOE
2978 /* setup rx buffer for FCoE */
2979 if ((vsi->type == I40E_VSI_FCOE) &&
2980 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07002981 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2982 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07002983 }
2984
2985#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002986 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002987 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002988 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002989
2990 /* set up individual rings */
2991 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002992 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002993
2994 return err;
2995}
2996
2997/**
2998 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2999 * @vsi: ptr to the VSI
3000 **/
3001static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3002{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003003 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003004 u16 qoffset, qcount;
3005 int i, n;
3006
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003007 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3008 /* Reset the TC information */
3009 for (i = 0; i < vsi->num_queue_pairs; i++) {
3010 rx_ring = vsi->rx_rings[i];
3011 tx_ring = vsi->tx_rings[i];
3012 rx_ring->dcb_tc = 0;
3013 tx_ring->dcb_tc = 0;
3014 }
3015 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016
3017 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003018 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003019 continue;
3020
3021 qoffset = vsi->tc_config.tc_info[n].qoffset;
3022 qcount = vsi->tc_config.tc_info[n].qcount;
3023 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003024 rx_ring = vsi->rx_rings[i];
3025 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003026 rx_ring->dcb_tc = n;
3027 tx_ring->dcb_tc = n;
3028 }
3029 }
3030}
3031
3032/**
3033 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3034 * @vsi: ptr to the VSI
3035 **/
3036static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3037{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003038 struct i40e_pf *pf = vsi->back;
3039 int err;
3040
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003041 if (vsi->netdev)
3042 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003043
3044 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3045 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3046 if (err) {
3047 dev_warn(&pf->pdev->dev,
3048 "could not set up macaddr; err %d\n", err);
3049 }
3050 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003051}
3052
3053/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003054 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3055 * @vsi: Pointer to the targeted VSI
3056 *
3057 * This function replays the hlist on the hw where all the SB Flow Director
3058 * filters were saved.
3059 **/
3060static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3061{
3062 struct i40e_fdir_filter *filter;
3063 struct i40e_pf *pf = vsi->back;
3064 struct hlist_node *node;
3065
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003066 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3067 return;
3068
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003069 hlist_for_each_entry_safe(filter, node,
3070 &pf->fdir_filter_list, fdir_node) {
3071 i40e_add_del_fdir(vsi, filter, true);
3072 }
3073}
3074
3075/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003076 * i40e_vsi_configure - Set up the VSI for action
3077 * @vsi: the VSI being configured
3078 **/
3079static int i40e_vsi_configure(struct i40e_vsi *vsi)
3080{
3081 int err;
3082
3083 i40e_set_vsi_rx_mode(vsi);
3084 i40e_restore_vlan(vsi);
3085 i40e_vsi_config_dcb_rings(vsi);
3086 err = i40e_vsi_configure_tx(vsi);
3087 if (!err)
3088 err = i40e_vsi_configure_rx(vsi);
3089
3090 return err;
3091}
3092
3093/**
3094 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3095 * @vsi: the VSI being configured
3096 **/
3097static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3098{
3099 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003100 struct i40e_hw *hw = &pf->hw;
3101 u16 vector;
3102 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003103 u32 qp;
3104
3105 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3106 * and PFINT_LNKLSTn registers, e.g.:
3107 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3108 */
3109 qp = vsi->base_queue;
3110 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003111 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003112 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3113
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003114 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003115 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003116 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3117 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3118 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003119 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003120 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3121 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3122 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003123 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3124 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003125
3126 /* Linked list for the queuepairs assigned to this vector */
3127 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3128 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003129 u32 val;
3130
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003131 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3132 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3133 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3134 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3135 (I40E_QUEUE_TYPE_TX
3136 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3137
3138 wr32(hw, I40E_QINT_RQCTL(qp), val);
3139
3140 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3141 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3142 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3143 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3144 (I40E_QUEUE_TYPE_RX
3145 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3146
3147 /* Terminate the linked list */
3148 if (q == (q_vector->num_ringpairs - 1))
3149 val |= (I40E_QUEUE_END_OF_LIST
3150 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3151
3152 wr32(hw, I40E_QINT_TQCTL(qp), val);
3153 qp++;
3154 }
3155 }
3156
3157 i40e_flush(hw);
3158}
3159
3160/**
3161 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3162 * @hw: ptr to the hardware info
3163 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003164static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165{
Jacob Kellerab437b52014-12-14 01:55:08 +00003166 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003167 u32 val;
3168
3169 /* clear things first */
3170 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3171 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3172
3173 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3174 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3175 I40E_PFINT_ICR0_ENA_GRST_MASK |
3176 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3177 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003178 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3179 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3180 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3181
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003182 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3183 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3184
Jacob Kellerab437b52014-12-14 01:55:08 +00003185 if (pf->flags & I40E_FLAG_PTP)
3186 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003188 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3189
3190 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003191 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3192 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193
3194 /* OTHER_ITR_IDX = 0 */
3195 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3196}
3197
3198/**
3199 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3200 * @vsi: the VSI being configured
3201 **/
3202static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3203{
Alexander Duyck493fb302013-09-28 07:01:44 +00003204 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205 struct i40e_pf *pf = vsi->back;
3206 struct i40e_hw *hw = &pf->hw;
3207 u32 val;
3208
3209 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003210 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003211 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003212 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3213 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003214 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003215 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3216 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3217
Jacob Kellerab437b52014-12-14 01:55:08 +00003218 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003219
3220 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3221 wr32(hw, I40E_PFINT_LNKLST0, 0);
3222
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003223 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003224 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3225 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3226 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3227
3228 wr32(hw, I40E_QINT_RQCTL(0), val);
3229
3230 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3231 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3232 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3233
3234 wr32(hw, I40E_QINT_TQCTL(0), val);
3235 i40e_flush(hw);
3236}
3237
3238/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003239 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3240 * @pf: board private structure
3241 **/
3242void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3243{
3244 struct i40e_hw *hw = &pf->hw;
3245
3246 wr32(hw, I40E_PFINT_DYN_CTL0,
3247 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3248 i40e_flush(hw);
3249}
3250
3251/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003252 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3253 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003254 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003255 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003256void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003257{
3258 struct i40e_hw *hw = &pf->hw;
3259 u32 val;
3260
3261 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003262 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003263 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3264
3265 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3266 i40e_flush(hw);
3267}
3268
3269/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003270 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3271 * @irq: interrupt number
3272 * @data: pointer to a q_vector
3273 **/
3274static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3275{
3276 struct i40e_q_vector *q_vector = data;
3277
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003278 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003279 return IRQ_HANDLED;
3280
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003281 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003282
3283 return IRQ_HANDLED;
3284}
3285
3286/**
Alan Brady96db7762016-09-14 16:24:38 -07003287 * i40e_irq_affinity_notify - Callback for affinity changes
3288 * @notify: context as to what irq was changed
3289 * @mask: the new affinity mask
3290 *
3291 * This is a callback function used by the irq_set_affinity_notifier function
3292 * so that we may register to receive changes to the irq affinity masks.
3293 **/
3294static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3295 const cpumask_t *mask)
3296{
3297 struct i40e_q_vector *q_vector =
3298 container_of(notify, struct i40e_q_vector, affinity_notify);
3299
3300 q_vector->affinity_mask = *mask;
3301}
3302
3303/**
3304 * i40e_irq_affinity_release - Callback for affinity notifier release
3305 * @ref: internal core kernel usage
3306 *
3307 * This is a callback function used by the irq_set_affinity_notifier function
3308 * to inform the current notification subscriber that they will no longer
3309 * receive notifications.
3310 **/
3311static void i40e_irq_affinity_release(struct kref *ref) {}
3312
3313/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003314 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3315 * @vsi: the VSI being configured
3316 * @basename: name for the vector
3317 *
3318 * Allocates MSI-X vectors and requests interrupts from the kernel.
3319 **/
3320static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3321{
3322 int q_vectors = vsi->num_q_vectors;
3323 struct i40e_pf *pf = vsi->back;
3324 int base = vsi->base_vector;
3325 int rx_int_idx = 0;
3326 int tx_int_idx = 0;
3327 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003328 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329
3330 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003331 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332
Alan Brady96db7762016-09-14 16:24:38 -07003333 irq_num = pf->msix_entries[base + vector].vector;
3334
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003335 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003336 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3337 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3338 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003339 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003340 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3341 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003342 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003343 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3344 "%s-%s-%d", basename, "tx", tx_int_idx++);
3345 } else {
3346 /* skip this unused q_vector */
3347 continue;
3348 }
Alan Brady96db7762016-09-14 16:24:38 -07003349 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003350 vsi->irq_handler,
3351 0,
3352 q_vector->name,
3353 q_vector);
3354 if (err) {
3355 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003356 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003357 goto free_queue_irqs;
3358 }
Alan Brady96db7762016-09-14 16:24:38 -07003359
3360 /* register for affinity change notifications */
3361 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3362 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3363 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003365 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003366 }
3367
Shannon Nelson63741842014-04-23 04:50:16 +00003368 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003369 return 0;
3370
3371free_queue_irqs:
3372 while (vector) {
3373 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003374 irq_num = pf->msix_entries[base + vector].vector;
3375 irq_set_affinity_notifier(irq_num, NULL);
3376 irq_set_affinity_hint(irq_num, NULL);
3377 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003378 }
3379 return err;
3380}
3381
3382/**
3383 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3384 * @vsi: the VSI being un-configured
3385 **/
3386static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3387{
3388 struct i40e_pf *pf = vsi->back;
3389 struct i40e_hw *hw = &pf->hw;
3390 int base = vsi->base_vector;
3391 int i;
3392
3393 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003394 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3395 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003396 }
3397
3398 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3399 for (i = vsi->base_vector;
3400 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3401 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3402
3403 i40e_flush(hw);
3404 for (i = 0; i < vsi->num_q_vectors; i++)
3405 synchronize_irq(pf->msix_entries[i + base].vector);
3406 } else {
3407 /* Legacy and MSI mode - this stops all interrupt handling */
3408 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3409 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3410 i40e_flush(hw);
3411 synchronize_irq(pf->pdev->irq);
3412 }
3413}
3414
3415/**
3416 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3417 * @vsi: the VSI being configured
3418 **/
3419static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3420{
3421 struct i40e_pf *pf = vsi->back;
3422 int i;
3423
3424 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003425 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003426 i40e_irq_dynamic_enable(vsi, i);
3427 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003428 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003429 }
3430
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003431 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003432 return 0;
3433}
3434
3435/**
3436 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3437 * @pf: board private structure
3438 **/
3439static void i40e_stop_misc_vector(struct i40e_pf *pf)
3440{
3441 /* Disable ICR 0 */
3442 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3443 i40e_flush(&pf->hw);
3444}
3445
3446/**
3447 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3448 * @irq: interrupt number
3449 * @data: pointer to a q_vector
3450 *
3451 * This is the handler used for all MSI/Legacy interrupts, and deals
3452 * with both queue and non-queue interrupts. This is also used in
3453 * MSIX mode to handle the non-queue interrupts.
3454 **/
3455static irqreturn_t i40e_intr(int irq, void *data)
3456{
3457 struct i40e_pf *pf = (struct i40e_pf *)data;
3458 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003459 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003460 u32 icr0, icr0_remaining;
3461 u32 val, ena_mask;
3462
3463 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003464 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465
Shannon Nelson116a57d2013-09-28 07:13:59 +00003466 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3467 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003468 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003469
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003470 /* if interrupt but no bits showing, must be SWINT */
3471 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3472 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3473 pf->sw_int_count++;
3474
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003475 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3476 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3477 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3478 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3479 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3480 }
3481
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003482 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3483 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003484 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3485 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003487 /* We do not have a way to disarm Queue causes while leaving
3488 * interrupt enabled for all other causes, ideally
3489 * interrupt should be disabled while we are in NAPI but
3490 * this is not a performance path and napi_schedule()
3491 * can deal with rescheduling.
3492 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003493 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003494 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003495 }
3496
3497 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3498 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3499 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003500 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003501 }
3502
3503 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3504 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3505 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3506 }
3507
3508 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3509 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3510 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3511 }
3512
3513 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3514 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3515 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3516 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3517 val = rd32(hw, I40E_GLGEN_RSTAT);
3518 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3519 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003520 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003521 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003522 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003523 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003524 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003525 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003526 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003527 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003528 }
3529
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003530 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3531 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3532 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003533 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3534 rd32(hw, I40E_PFHMC_ERRORINFO),
3535 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003536 }
3537
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003538 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3539 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3540
3541 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003542 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003543 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003544 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003545 }
3546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003547 /* If a critical error is pending we have no choice but to reset the
3548 * device.
3549 * Report and mask out any remaining unexpected interrupts.
3550 */
3551 icr0_remaining = icr0 & ena_mask;
3552 if (icr0_remaining) {
3553 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3554 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003555 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003556 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003557 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003558 dev_info(&pf->pdev->dev, "device will be reset\n");
3559 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3560 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003561 }
3562 ena_mask &= ~icr0_remaining;
3563 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003564 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003565
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003566enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003567 /* re-enable interrupt causes */
3568 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003569 if (!test_bit(__I40E_DOWN, &pf->state)) {
3570 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003571 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003572 }
3573
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003574 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003575}
3576
3577/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003578 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3579 * @tx_ring: tx ring to clean
3580 * @budget: how many cleans we're allowed
3581 *
3582 * Returns true if there's any budget left (e.g. the clean is finished)
3583 **/
3584static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3585{
3586 struct i40e_vsi *vsi = tx_ring->vsi;
3587 u16 i = tx_ring->next_to_clean;
3588 struct i40e_tx_buffer *tx_buf;
3589 struct i40e_tx_desc *tx_desc;
3590
3591 tx_buf = &tx_ring->tx_bi[i];
3592 tx_desc = I40E_TX_DESC(tx_ring, i);
3593 i -= tx_ring->count;
3594
3595 do {
3596 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3597
3598 /* if next_to_watch is not set then there is no work pending */
3599 if (!eop_desc)
3600 break;
3601
3602 /* prevent any other reads prior to eop_desc */
3603 read_barrier_depends();
3604
3605 /* if the descriptor isn't done, no work yet to do */
3606 if (!(eop_desc->cmd_type_offset_bsz &
3607 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3608 break;
3609
3610 /* clear next_to_watch to prevent false hangs */
3611 tx_buf->next_to_watch = NULL;
3612
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003613 tx_desc->buffer_addr = 0;
3614 tx_desc->cmd_type_offset_bsz = 0;
3615 /* move past filter desc */
3616 tx_buf++;
3617 tx_desc++;
3618 i++;
3619 if (unlikely(!i)) {
3620 i -= tx_ring->count;
3621 tx_buf = tx_ring->tx_bi;
3622 tx_desc = I40E_TX_DESC(tx_ring, 0);
3623 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003624 /* unmap skb header data */
3625 dma_unmap_single(tx_ring->dev,
3626 dma_unmap_addr(tx_buf, dma),
3627 dma_unmap_len(tx_buf, len),
3628 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003629 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3630 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003631
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003632 tx_buf->raw_buf = NULL;
3633 tx_buf->tx_flags = 0;
3634 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003635 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003636 tx_desc->buffer_addr = 0;
3637 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003638
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003639 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003640 tx_buf++;
3641 tx_desc++;
3642 i++;
3643 if (unlikely(!i)) {
3644 i -= tx_ring->count;
3645 tx_buf = tx_ring->tx_bi;
3646 tx_desc = I40E_TX_DESC(tx_ring, 0);
3647 }
3648
3649 /* update budget accounting */
3650 budget--;
3651 } while (likely(budget));
3652
3653 i += tx_ring->count;
3654 tx_ring->next_to_clean = i;
3655
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003656 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003657 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003658
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003659 return budget > 0;
3660}
3661
3662/**
3663 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3664 * @irq: interrupt number
3665 * @data: pointer to a q_vector
3666 **/
3667static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3668{
3669 struct i40e_q_vector *q_vector = data;
3670 struct i40e_vsi *vsi;
3671
3672 if (!q_vector->tx.ring)
3673 return IRQ_HANDLED;
3674
3675 vsi = q_vector->tx.ring->vsi;
3676 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3677
3678 return IRQ_HANDLED;
3679}
3680
3681/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003682 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683 * @vsi: the VSI being configured
3684 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003685 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003686 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003687static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688{
Alexander Duyck493fb302013-09-28 07:01:44 +00003689 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003690 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3691 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003692
3693 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003694 tx_ring->next = q_vector->tx.ring;
3695 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003696 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003697
3698 rx_ring->q_vector = q_vector;
3699 rx_ring->next = q_vector->rx.ring;
3700 q_vector->rx.ring = rx_ring;
3701 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003702}
3703
3704/**
3705 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3706 * @vsi: the VSI being configured
3707 *
3708 * This function maps descriptor rings to the queue-specific vectors
3709 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3710 * one vector per queue pair, but on a constrained vector budget, we
3711 * group the queue pairs as "efficiently" as possible.
3712 **/
3713static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3714{
3715 int qp_remaining = vsi->num_queue_pairs;
3716 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003717 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003718 int v_start = 0;
3719 int qp_idx = 0;
3720
3721 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3722 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003723 * It is also important to go through all the vectors available to be
3724 * sure that if we don't use all the vectors, that the remaining vectors
3725 * are cleared. This is especially important when decreasing the
3726 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003727 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003728 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003729 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3730
3731 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3732
3733 q_vector->num_ringpairs = num_ringpairs;
3734
3735 q_vector->rx.count = 0;
3736 q_vector->tx.count = 0;
3737 q_vector->rx.ring = NULL;
3738 q_vector->tx.ring = NULL;
3739
3740 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003741 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003742 qp_idx++;
3743 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744 }
3745 }
3746}
3747
3748/**
3749 * i40e_vsi_request_irq - Request IRQ from the OS
3750 * @vsi: the VSI being configured
3751 * @basename: name for the vector
3752 **/
3753static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3754{
3755 struct i40e_pf *pf = vsi->back;
3756 int err;
3757
3758 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3759 err = i40e_vsi_request_irq_msix(vsi, basename);
3760 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3761 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003762 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003763 else
3764 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003765 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003766
3767 if (err)
3768 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3769
3770 return err;
3771}
3772
3773#ifdef CONFIG_NET_POLL_CONTROLLER
3774/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003775 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003776 * @netdev: network interface device structure
3777 *
3778 * This is used by netconsole to send skbs without having to re-enable
3779 * interrupts. It's not called while the normal interrupt routine is executing.
3780 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003781#ifdef I40E_FCOE
3782void i40e_netpoll(struct net_device *netdev)
3783#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003784static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003785#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003786{
3787 struct i40e_netdev_priv *np = netdev_priv(netdev);
3788 struct i40e_vsi *vsi = np->vsi;
3789 struct i40e_pf *pf = vsi->back;
3790 int i;
3791
3792 /* if interface is down do nothing */
3793 if (test_bit(__I40E_DOWN, &vsi->state))
3794 return;
3795
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003796 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3797 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003798 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003799 } else {
3800 i40e_intr(pf->pdev->irq, netdev);
3801 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003802}
3803#endif
3804
3805/**
Neerav Parikh23527302014-06-03 23:50:15 +00003806 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3807 * @pf: the PF being configured
3808 * @pf_q: the PF queue
3809 * @enable: enable or disable state of the queue
3810 *
3811 * This routine will wait for the given Tx queue of the PF to reach the
3812 * enabled or disabled state.
3813 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3814 * multiple retries; else will return 0 in case of success.
3815 **/
3816static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3817{
3818 int i;
3819 u32 tx_reg;
3820
3821 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3822 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3823 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3824 break;
3825
Neerav Parikhf98a2002014-09-13 07:40:44 +00003826 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003827 }
3828 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3829 return -ETIMEDOUT;
3830
3831 return 0;
3832}
3833
3834/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003835 * i40e_vsi_control_tx - Start or stop a VSI's rings
3836 * @vsi: the VSI being configured
3837 * @enable: start or stop the rings
3838 **/
3839static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3840{
3841 struct i40e_pf *pf = vsi->back;
3842 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003843 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003844 u32 tx_reg;
3845
3846 pf_q = vsi->base_queue;
3847 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003848
3849 /* warn the TX unit of coming changes */
3850 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3851 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003852 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003853
Mitch Williams6c5ef622014-02-20 19:29:16 -08003854 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003855 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003856 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3857 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3858 break;
3859 usleep_range(1000, 2000);
3860 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003861 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003862 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003863 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003864
3865 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003866 if (enable) {
3867 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003868 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003869 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003870 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003871 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003872
3873 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003874 /* No waiting for the Tx queue to disable */
3875 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3876 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003877
3878 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003879 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3880 if (ret) {
3881 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003882 "VSI seid %d Tx ring %d %sable timeout\n",
3883 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003884 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003885 }
3886 }
3887
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003888 if (hw->revision_id == 0)
3889 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003890 return ret;
3891}
3892
3893/**
3894 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3895 * @pf: the PF being configured
3896 * @pf_q: the PF queue
3897 * @enable: enable or disable state of the queue
3898 *
3899 * This routine will wait for the given Rx queue of the PF to reach the
3900 * enabled or disabled state.
3901 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3902 * multiple retries; else will return 0 in case of success.
3903 **/
3904static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3905{
3906 int i;
3907 u32 rx_reg;
3908
3909 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3910 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3911 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3912 break;
3913
Neerav Parikhf98a2002014-09-13 07:40:44 +00003914 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003915 }
3916 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3917 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003918
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003919 return 0;
3920}
3921
3922/**
3923 * i40e_vsi_control_rx - Start or stop a VSI's rings
3924 * @vsi: the VSI being configured
3925 * @enable: start or stop the rings
3926 **/
3927static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3928{
3929 struct i40e_pf *pf = vsi->back;
3930 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003931 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003932 u32 rx_reg;
3933
3934 pf_q = vsi->base_queue;
3935 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003936 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003937 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003938 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3939 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3940 break;
3941 usleep_range(1000, 2000);
3942 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003943
Catherine Sullivan7c122002014-03-14 07:32:29 +00003944 /* Skip if the queue is already in the requested state */
3945 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3946 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003947
3948 /* turn on/off the queue */
3949 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003950 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003952 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003953 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003954 /* No waiting for the Tx queue to disable */
3955 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3956 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957
3958 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003959 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3960 if (ret) {
3961 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003962 "VSI seid %d Rx ring %d %sable timeout\n",
3963 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003964 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003965 }
3966 }
3967
Neerav Parikh23527302014-06-03 23:50:15 +00003968 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003969}
3970
3971/**
3972 * i40e_vsi_control_rings - Start or stop a VSI's rings
3973 * @vsi: the VSI being configured
3974 * @enable: start or stop the rings
3975 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003976int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003977{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003978 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003979
3980 /* do rx first for enable and last for disable */
3981 if (request) {
3982 ret = i40e_vsi_control_rx(vsi, request);
3983 if (ret)
3984 return ret;
3985 ret = i40e_vsi_control_tx(vsi, request);
3986 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003987 /* Ignore return value, we need to shutdown whatever we can */
3988 i40e_vsi_control_tx(vsi, request);
3989 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003990 }
3991
3992 return ret;
3993}
3994
3995/**
3996 * i40e_vsi_free_irq - Free the irq association with the OS
3997 * @vsi: the VSI being configured
3998 **/
3999static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4000{
4001 struct i40e_pf *pf = vsi->back;
4002 struct i40e_hw *hw = &pf->hw;
4003 int base = vsi->base_vector;
4004 u32 val, qp;
4005 int i;
4006
4007 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4008 if (!vsi->q_vectors)
4009 return;
4010
Shannon Nelson63741842014-04-23 04:50:16 +00004011 if (!vsi->irqs_ready)
4012 return;
4013
4014 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004015 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004016 int irq_num;
4017 u16 vector;
4018
4019 vector = i + base;
4020 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004021
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
Alan Brady96db7762016-09-14 16:24:38 -07004027 /* clear the affinity notifier in the IRQ descriptor */
4028 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004029 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004030 irq_set_affinity_hint(irq_num, NULL);
4031 synchronize_irq(irq_num);
4032 free_irq(irq_num, 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 Kirsherb40c82e62015-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/**
4644 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
4645 * @pf: PF being queried
4646 *
4647 * Return a bitmap for first enabled traffic class for this PF.
4648 **/
4649static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
4650{
4651 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
4652 u8 i = 0;
4653
4654 if (!enabled_tc)
4655 return 0x1; /* TC0 */
4656
4657 /* Find the first enabled TC */
4658 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004659 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004660 break;
4661 }
4662
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004663 return BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004664}
4665
4666/**
4667 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4668 * @pf: PF being queried
4669 *
4670 * Return a bitmap for enabled traffic classes for this PF.
4671 **/
4672static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4673{
4674 /* If DCB is not enabled for this PF then just return default TC */
4675 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4676 return i40e_pf_get_default_tc(pf);
4677
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004678 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004679 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4680 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4681
Neerav Parikhfc51de92015-02-24 06:58:53 +00004682 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004683 if (pf->hw.func_caps.iscsi)
4684 return i40e_get_iscsi_tc_map(pf);
4685 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004686 return i40e_pf_get_default_tc(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004687}
4688
4689/**
4690 * i40e_vsi_get_bw_info - Query VSI BW Information
4691 * @vsi: the VSI being queried
4692 *
4693 * Returns 0 on success, negative value on failure
4694 **/
4695static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4696{
4697 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4698 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4699 struct i40e_pf *pf = vsi->back;
4700 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004701 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004702 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004703 int i;
4704
4705 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004706 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4707 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004708 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004709 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4710 i40e_stat_str(&pf->hw, ret),
4711 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004712 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004713 }
4714
4715 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004716 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4717 NULL);
4718 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004719 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004720 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4721 i40e_stat_str(&pf->hw, ret),
4722 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004723 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004724 }
4725
4726 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4727 dev_info(&pf->pdev->dev,
4728 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4729 bw_config.tc_valid_bits,
4730 bw_ets_config.tc_valid_bits);
4731 /* Still continuing */
4732 }
4733
4734 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4735 vsi->bw_max_quanta = bw_config.max_bw;
4736 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4737 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4738 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4739 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4740 vsi->bw_ets_limit_credits[i] =
4741 le16_to_cpu(bw_ets_config.credits[i]);
4742 /* 3 bits out of 4 for each TC */
4743 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4744 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004745
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004746 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004747}
4748
4749/**
4750 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4751 * @vsi: the VSI being configured
4752 * @enabled_tc: TC bitmap
4753 * @bw_credits: BW shared credits per TC
4754 *
4755 * Returns 0 on success, negative value on failure
4756 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004757static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758 u8 *bw_share)
4759{
4760 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004761 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004762 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004763
4764 bw_data.tc_valid_bits = enabled_tc;
4765 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4766 bw_data.tc_bw_credits[i] = bw_share[i];
4767
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004768 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4769 NULL);
4770 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004771 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004772 "AQ command Config VSI BW allocation per TC failed = %d\n",
4773 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004774 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004775 }
4776
4777 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4778 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4779
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004780 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004781}
4782
4783/**
4784 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4785 * @vsi: the VSI being configured
4786 * @enabled_tc: TC map to be enabled
4787 *
4788 **/
4789static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4790{
4791 struct net_device *netdev = vsi->netdev;
4792 struct i40e_pf *pf = vsi->back;
4793 struct i40e_hw *hw = &pf->hw;
4794 u8 netdev_tc = 0;
4795 int i;
4796 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4797
4798 if (!netdev)
4799 return;
4800
4801 if (!enabled_tc) {
4802 netdev_reset_tc(netdev);
4803 return;
4804 }
4805
4806 /* Set up actual enabled TCs on the VSI */
4807 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4808 return;
4809
4810 /* set per TC queues for the VSI */
4811 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4812 /* Only set TC queues for enabled tcs
4813 *
4814 * e.g. For a VSI that has TC0 and TC3 enabled the
4815 * enabled_tc bitmap would be 0x00001001; the driver
4816 * will set the numtc for netdev as 2 that will be
4817 * referenced by the netdev layer as TC 0 and 1.
4818 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004819 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004820 netdev_set_tc_queue(netdev,
4821 vsi->tc_config.tc_info[i].netdev_tc,
4822 vsi->tc_config.tc_info[i].qcount,
4823 vsi->tc_config.tc_info[i].qoffset);
4824 }
4825
4826 /* Assign UP2TC map for the VSI */
4827 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4828 /* Get the actual TC# for the UP */
4829 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4830 /* Get the mapped netdev TC# for the UP */
4831 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4832 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4833 }
4834}
4835
4836/**
4837 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4838 * @vsi: the VSI being configured
4839 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4840 **/
4841static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4842 struct i40e_vsi_context *ctxt)
4843{
4844 /* copy just the sections touched not the entire info
4845 * since not all sections are valid as returned by
4846 * update vsi params
4847 */
4848 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4849 memcpy(&vsi->info.queue_mapping,
4850 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4851 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4852 sizeof(vsi->info.tc_mapping));
4853}
4854
4855/**
4856 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4857 * @vsi: VSI to be configured
4858 * @enabled_tc: TC bitmap
4859 *
4860 * This configures a particular VSI for TCs that are mapped to the
4861 * given TC bitmap. It uses default bandwidth share for TCs across
4862 * VSIs to configure TC for a particular VSI.
4863 *
4864 * NOTE:
4865 * It is expected that the VSI queues have been quisced before calling
4866 * this function.
4867 **/
4868static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4869{
4870 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4871 struct i40e_vsi_context ctxt;
4872 int ret = 0;
4873 int i;
4874
4875 /* Check if enabled_tc is same as existing or new TCs */
4876 if (vsi->tc_config.enabled_tc == enabled_tc)
4877 return ret;
4878
4879 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4880 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004881 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004882 bw_share[i] = 1;
4883 }
4884
4885 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4886 if (ret) {
4887 dev_info(&vsi->back->pdev->dev,
4888 "Failed configuring TC map %d for VSI %d\n",
4889 enabled_tc, vsi->seid);
4890 goto out;
4891 }
4892
4893 /* Update Queue Pairs Mapping for currently enabled UPs */
4894 ctxt.seid = vsi->seid;
4895 ctxt.pf_num = vsi->back->hw.pf_id;
4896 ctxt.vf_num = 0;
4897 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004898 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004899 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4900
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004901 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4902 ctxt.info.valid_sections |=
4903 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4904 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4905 }
4906
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004907 /* Update the VSI after updating the VSI queue-mapping information */
4908 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4909 if (ret) {
4910 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004911 "Update vsi tc config failed, err %s aq_err %s\n",
4912 i40e_stat_str(&vsi->back->hw, ret),
4913 i40e_aq_str(&vsi->back->hw,
4914 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004915 goto out;
4916 }
4917 /* update the local VSI info with updated queue map */
4918 i40e_vsi_update_queue_map(vsi, &ctxt);
4919 vsi->info.valid_sections = 0;
4920
4921 /* Update current VSI BW information */
4922 ret = i40e_vsi_get_bw_info(vsi);
4923 if (ret) {
4924 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004925 "Failed updating vsi bw info, err %s aq_err %s\n",
4926 i40e_stat_str(&vsi->back->hw, ret),
4927 i40e_aq_str(&vsi->back->hw,
4928 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004929 goto out;
4930 }
4931
4932 /* Update the netdev TC setup */
4933 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4934out:
4935 return ret;
4936}
4937
4938/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004939 * i40e_veb_config_tc - Configure TCs for given VEB
4940 * @veb: given VEB
4941 * @enabled_tc: TC bitmap
4942 *
4943 * Configures given TC bitmap for VEB (switching) element
4944 **/
4945int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4946{
4947 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4948 struct i40e_pf *pf = veb->pf;
4949 int ret = 0;
4950 int i;
4951
4952 /* No TCs or already enabled TCs just return */
4953 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4954 return ret;
4955
4956 bw_data.tc_valid_bits = enabled_tc;
4957 /* bw_data.absolute_credits is not set (relative) */
4958
4959 /* Enable ETS TCs with equal BW Share for now */
4960 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004961 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004962 bw_data.tc_bw_share_credits[i] = 1;
4963 }
4964
4965 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4966 &bw_data, NULL);
4967 if (ret) {
4968 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004969 "VEB bw config failed, err %s aq_err %s\n",
4970 i40e_stat_str(&pf->hw, ret),
4971 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004972 goto out;
4973 }
4974
4975 /* Update the BW information */
4976 ret = i40e_veb_get_bw_info(veb);
4977 if (ret) {
4978 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004979 "Failed getting veb bw config, err %s aq_err %s\n",
4980 i40e_stat_str(&pf->hw, ret),
4981 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004982 }
4983
4984out:
4985 return ret;
4986}
4987
4988#ifdef CONFIG_I40E_DCB
4989/**
4990 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4991 * @pf: PF struct
4992 *
4993 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4994 * the caller would've quiesce all the VSIs before calling
4995 * this function
4996 **/
4997static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4998{
4999 u8 tc_map = 0;
5000 int ret;
5001 u8 v;
5002
5003 /* Enable the TCs available on PF to all VEBs */
5004 tc_map = i40e_pf_get_tc_map(pf);
5005 for (v = 0; v < I40E_MAX_VEB; v++) {
5006 if (!pf->veb[v])
5007 continue;
5008 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5009 if (ret) {
5010 dev_info(&pf->pdev->dev,
5011 "Failed configuring TC for VEB seid=%d\n",
5012 pf->veb[v]->seid);
5013 /* Will try to configure as many components */
5014 }
5015 }
5016
5017 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005018 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005019 if (!pf->vsi[v])
5020 continue;
5021
5022 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005023#ifdef I40E_FCOE
5024 * - For FCoE VSI only enable the TC configured
5025 * as per the APP TLV
5026#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005027 * - For all others keep them at TC0 for now
5028 */
5029 if (v == pf->lan_vsi)
5030 tc_map = i40e_pf_get_tc_map(pf);
5031 else
5032 tc_map = i40e_pf_get_default_tc(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07005033#ifdef I40E_FCOE
5034 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5035 tc_map = i40e_get_fcoe_tc_map(pf);
5036#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005037
5038 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5039 if (ret) {
5040 dev_info(&pf->pdev->dev,
5041 "Failed configuring TC for VSI seid=%d\n",
5042 pf->vsi[v]->seid);
5043 /* Will try to configure as many components */
5044 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005045 /* Re-configure VSI vectors based on updated TC map */
5046 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005047 if (pf->vsi[v]->netdev)
5048 i40e_dcbnl_set_all(pf->vsi[v]);
5049 }
5050 }
5051}
5052
5053/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005054 * i40e_resume_port_tx - Resume port Tx
5055 * @pf: PF struct
5056 *
5057 * Resume a port's Tx and issue a PF reset in case of failure to
5058 * resume.
5059 **/
5060static int i40e_resume_port_tx(struct i40e_pf *pf)
5061{
5062 struct i40e_hw *hw = &pf->hw;
5063 int ret;
5064
5065 ret = i40e_aq_resume_port_tx(hw, NULL);
5066 if (ret) {
5067 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005068 "Resume Port Tx failed, err %s aq_err %s\n",
5069 i40e_stat_str(&pf->hw, ret),
5070 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005071 /* Schedule PF reset to recover */
5072 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5073 i40e_service_event_schedule(pf);
5074 }
5075
5076 return ret;
5077}
5078
5079/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005080 * i40e_init_pf_dcb - Initialize DCB configuration
5081 * @pf: PF being configured
5082 *
5083 * Query the current DCB configuration and cache it
5084 * in the hardware structure
5085 **/
5086static int i40e_init_pf_dcb(struct i40e_pf *pf)
5087{
5088 struct i40e_hw *hw = &pf->hw;
5089 int err = 0;
5090
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005091 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005092 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005093 goto out;
5094
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005095 /* Get the initial DCB configuration */
5096 err = i40e_init_dcb(hw);
5097 if (!err) {
5098 /* Device/Function is not DCBX capable */
5099 if ((!hw->func_caps.dcb) ||
5100 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5101 dev_info(&pf->pdev->dev,
5102 "DCBX offload is not supported or is disabled for this PF.\n");
5103
5104 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5105 goto out;
5106
5107 } else {
5108 /* When status is not DISABLED then DCBX in FW */
5109 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5110 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005111
5112 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005113 /* Enable DCB tagging only when more than one TC
5114 * or explicitly disable if only one TC
5115 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005116 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5117 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005118 else
5119 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005120 dev_dbg(&pf->pdev->dev,
5121 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005122 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005123 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005124 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005125 "Query for DCB configuration failed, err %s aq_err %s\n",
5126 i40e_stat_str(&pf->hw, err),
5127 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005128 }
5129
5130out:
5131 return err;
5132}
5133#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005134#define SPEED_SIZE 14
5135#define FC_SIZE 8
5136/**
5137 * i40e_print_link_message - print link up or down
5138 * @vsi: the VSI for which link needs a message
5139 */
Matt Jaredc156f852015-08-27 11:42:39 -04005140void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005141{
Shannon Nelsona9165492015-09-03 17:19:00 -04005142 char *speed = "Unknown";
5143 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005144
Matt Jaredc156f852015-08-27 11:42:39 -04005145 if (vsi->current_isup == isup)
5146 return;
5147 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005148 if (!isup) {
5149 netdev_info(vsi->netdev, "NIC Link is Down\n");
5150 return;
5151 }
5152
Greg Rose148c2d82014-12-11 07:06:27 +00005153 /* Warn user if link speed on NPAR enabled partition is not at
5154 * least 10GB
5155 */
5156 if (vsi->back->hw.func_caps.npar_enable &&
5157 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5158 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5159 netdev_warn(vsi->netdev,
5160 "The partition detected link speed that is less than 10Gbps\n");
5161
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005162 switch (vsi->back->hw.phy.link_info.link_speed) {
5163 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005164 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005165 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005166 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005167 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005168 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005169 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005170 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005171 break;
5172 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005173 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005174 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005175 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005176 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005177 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005178 default:
5179 break;
5180 }
5181
5182 switch (vsi->back->hw.fc.current_mode) {
5183 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005184 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005185 break;
5186 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005187 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005188 break;
5189 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005190 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005191 break;
5192 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005193 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005194 break;
5195 }
5196
Shannon Nelsona9165492015-09-03 17:19:00 -04005197 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005198 speed, fc);
5199}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005200
5201/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005202 * i40e_up_complete - Finish the last steps of bringing up a connection
5203 * @vsi: the VSI being configured
5204 **/
5205static int i40e_up_complete(struct i40e_vsi *vsi)
5206{
5207 struct i40e_pf *pf = vsi->back;
5208 int err;
5209
5210 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5211 i40e_vsi_configure_msix(vsi);
5212 else
5213 i40e_configure_msi_and_legacy(vsi);
5214
5215 /* start rings */
5216 err = i40e_vsi_control_rings(vsi, true);
5217 if (err)
5218 return err;
5219
5220 clear_bit(__I40E_DOWN, &vsi->state);
5221 i40e_napi_enable_all(vsi);
5222 i40e_vsi_enable_irq(vsi);
5223
5224 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5225 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005226 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005227 netif_tx_start_all_queues(vsi->netdev);
5228 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005229 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005230 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005231 /* need to check for qualified module here*/
5232 if ((pf->hw.phy.link_info.link_info &
5233 I40E_AQ_MEDIA_AVAILABLE) &&
5234 (!(pf->hw.phy.link_info.an_info &
5235 I40E_AQ_QUALIFIED_MODULE)))
5236 netdev_err(vsi->netdev,
5237 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005238 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005239
5240 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005241 if (vsi->type == I40E_VSI_FDIR) {
5242 /* reset fd counters */
5243 pf->fd_add_err = pf->fd_atr_cnt = 0;
5244 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005245 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005246 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5247 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005248 pf->fd_tcp_rule = 0;
5249 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005250 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005251 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005252
5253 /* On the next run of the service_task, notify any clients of the new
5254 * opened netdev
5255 */
5256 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005257 i40e_service_event_schedule(pf);
5258
5259 return 0;
5260}
5261
5262/**
5263 * i40e_vsi_reinit_locked - Reset the VSI
5264 * @vsi: the VSI being configured
5265 *
5266 * Rebuild the ring structs after some configuration
5267 * has changed, e.g. MTU size.
5268 **/
5269static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5270{
5271 struct i40e_pf *pf = vsi->back;
5272
5273 WARN_ON(in_interrupt());
5274 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5275 usleep_range(1000, 2000);
5276 i40e_down(vsi);
5277
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005278 i40e_up(vsi);
5279 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5280}
5281
5282/**
5283 * i40e_up - Bring the connection back up after being down
5284 * @vsi: the VSI being configured
5285 **/
5286int i40e_up(struct i40e_vsi *vsi)
5287{
5288 int err;
5289
5290 err = i40e_vsi_configure(vsi);
5291 if (!err)
5292 err = i40e_up_complete(vsi);
5293
5294 return err;
5295}
5296
5297/**
5298 * i40e_down - Shutdown the connection processing
5299 * @vsi: the VSI being stopped
5300 **/
5301void i40e_down(struct i40e_vsi *vsi)
5302{
5303 int i;
5304
5305 /* It is assumed that the caller of this function
5306 * sets the vsi->state __I40E_DOWN bit.
5307 */
5308 if (vsi->netdev) {
5309 netif_carrier_off(vsi->netdev);
5310 netif_tx_disable(vsi->netdev);
5311 }
5312 i40e_vsi_disable_irq(vsi);
5313 i40e_vsi_control_rings(vsi, false);
5314 i40e_napi_disable_all(vsi);
5315
5316 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005317 i40e_clean_tx_ring(vsi->tx_rings[i]);
5318 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005319 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005320
5321 i40e_notify_client_of_netdev_close(vsi, false);
5322
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005323}
5324
5325/**
5326 * i40e_setup_tc - configure multiple traffic classes
5327 * @netdev: net device to configure
5328 * @tc: number of traffic classes to enable
5329 **/
5330static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5331{
5332 struct i40e_netdev_priv *np = netdev_priv(netdev);
5333 struct i40e_vsi *vsi = np->vsi;
5334 struct i40e_pf *pf = vsi->back;
5335 u8 enabled_tc = 0;
5336 int ret = -EINVAL;
5337 int i;
5338
5339 /* Check if DCB enabled to continue */
5340 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5341 netdev_info(netdev, "DCB is not enabled for adapter\n");
5342 goto exit;
5343 }
5344
5345 /* Check if MFP enabled */
5346 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5347 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5348 goto exit;
5349 }
5350
5351 /* Check whether tc count is within enabled limit */
5352 if (tc > i40e_pf_get_num_tc(pf)) {
5353 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5354 goto exit;
5355 }
5356
5357 /* Generate TC map for number of tc requested */
5358 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005359 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005360
5361 /* Requesting same TC configuration as already enabled */
5362 if (enabled_tc == vsi->tc_config.enabled_tc)
5363 return 0;
5364
5365 /* Quiesce VSI queues */
5366 i40e_quiesce_vsi(vsi);
5367
5368 /* Configure VSI for enabled TCs */
5369 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5370 if (ret) {
5371 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5372 vsi->seid);
5373 goto exit;
5374 }
5375
5376 /* Unquiesce VSI */
5377 i40e_unquiesce_vsi(vsi);
5378
5379exit:
5380 return ret;
5381}
5382
John Fastabende4c67342016-02-16 21:16:15 -08005383#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005384int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5385 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005386#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005387static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5388 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005389#endif
5390{
John Fastabend16e5cc62016-02-16 21:16:43 -08005391 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005392 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005393 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005394}
5395
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005396/**
5397 * i40e_open - Called when a network interface is made active
5398 * @netdev: network interface device structure
5399 *
5400 * The open entry point is called when a network interface is made
5401 * active by the system (IFF_UP). At this point all resources needed
5402 * for transmit and receive operations are allocated, the interrupt
5403 * handler is registered with the OS, the netdev watchdog subtask is
5404 * enabled, and the stack is notified that the interface is ready.
5405 *
5406 * Returns 0 on success, negative value on failure
5407 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005408int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005409{
5410 struct i40e_netdev_priv *np = netdev_priv(netdev);
5411 struct i40e_vsi *vsi = np->vsi;
5412 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005413 int err;
5414
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005415 /* disallow open during test or if eeprom is broken */
5416 if (test_bit(__I40E_TESTING, &pf->state) ||
5417 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005418 return -EBUSY;
5419
5420 netif_carrier_off(netdev);
5421
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005422 err = i40e_vsi_open(vsi);
5423 if (err)
5424 return err;
5425
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005426 /* configure global TSO hardware offload settings */
5427 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5428 TCP_FLAG_FIN) >> 16);
5429 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5430 TCP_FLAG_FIN |
5431 TCP_FLAG_CWR) >> 16);
5432 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5433
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005434 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005435
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005436 return 0;
5437}
5438
5439/**
5440 * i40e_vsi_open -
5441 * @vsi: the VSI to open
5442 *
5443 * Finish initialization of the VSI.
5444 *
5445 * Returns 0 on success, negative value on failure
5446 **/
5447int i40e_vsi_open(struct i40e_vsi *vsi)
5448{
5449 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005450 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005451 int err;
5452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005453 /* allocate descriptors */
5454 err = i40e_vsi_setup_tx_resources(vsi);
5455 if (err)
5456 goto err_setup_tx;
5457 err = i40e_vsi_setup_rx_resources(vsi);
5458 if (err)
5459 goto err_setup_rx;
5460
5461 err = i40e_vsi_configure(vsi);
5462 if (err)
5463 goto err_setup_rx;
5464
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005465 if (vsi->netdev) {
5466 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5467 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5468 err = i40e_vsi_request_irq(vsi, int_name);
5469 if (err)
5470 goto err_setup_rx;
5471
5472 /* Notify the stack of the actual queue counts. */
5473 err = netif_set_real_num_tx_queues(vsi->netdev,
5474 vsi->num_queue_pairs);
5475 if (err)
5476 goto err_set_queues;
5477
5478 err = netif_set_real_num_rx_queues(vsi->netdev,
5479 vsi->num_queue_pairs);
5480 if (err)
5481 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005482
5483 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005484 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005485 dev_driver_string(&pf->pdev->dev),
5486 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005487 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005488
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005489 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005490 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005491 goto err_setup_rx;
5492 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005493
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005494 err = i40e_up_complete(vsi);
5495 if (err)
5496 goto err_up_complete;
5497
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005498 return 0;
5499
5500err_up_complete:
5501 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005502err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503 i40e_vsi_free_irq(vsi);
5504err_setup_rx:
5505 i40e_vsi_free_rx_resources(vsi);
5506err_setup_tx:
5507 i40e_vsi_free_tx_resources(vsi);
5508 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005509 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005510
5511 return err;
5512}
5513
5514/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005515 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005516 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005517 *
5518 * This function destroys the hlist where all the Flow Director
5519 * filters were saved.
5520 **/
5521static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5522{
5523 struct i40e_fdir_filter *filter;
5524 struct hlist_node *node2;
5525
5526 hlist_for_each_entry_safe(filter, node2,
5527 &pf->fdir_filter_list, fdir_node) {
5528 hlist_del(&filter->fdir_node);
5529 kfree(filter);
5530 }
5531 pf->fdir_pf_active_filters = 0;
5532}
5533
5534/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005535 * i40e_close - Disables a network interface
5536 * @netdev: network interface device structure
5537 *
5538 * The close entry point is called when an interface is de-activated
5539 * by the OS. The hardware is still under the driver's control, but
5540 * this netdev interface is disabled.
5541 *
5542 * Returns 0, this is not allowed to fail
5543 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005544int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005545{
5546 struct i40e_netdev_priv *np = netdev_priv(netdev);
5547 struct i40e_vsi *vsi = np->vsi;
5548
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005549 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005550
5551 return 0;
5552}
5553
5554/**
5555 * i40e_do_reset - Start a PF or Core Reset sequence
5556 * @pf: board private structure
5557 * @reset_flags: which reset is requested
5558 *
5559 * The essential difference in resets is that the PF Reset
5560 * doesn't clear the packet buffers, doesn't reset the PE
5561 * firmware, and doesn't bother the other PFs on the chip.
5562 **/
5563void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5564{
5565 u32 val;
5566
5567 WARN_ON(in_interrupt());
5568
Mitch Williams263fc482014-04-23 04:50:11 +00005569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005570 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005571 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005572
5573 /* Request a Global Reset
5574 *
5575 * This will start the chip's countdown to the actual full
5576 * chip reset event, and a warning interrupt to be sent
5577 * to all PFs, including the requestor. Our handler
5578 * for the warning interrupt will deal with the shutdown
5579 * and recovery of the switch setup.
5580 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005581 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005582 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5583 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5584 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5585
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005586 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005587
5588 /* Request a Core Reset
5589 *
5590 * Same as Global Reset, except does *not* include the MAC/PHY
5591 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005592 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005593 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5594 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5595 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5596 i40e_flush(&pf->hw);
5597
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005598 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599
5600 /* Request a PF Reset
5601 *
5602 * Resets only the PF-specific registers
5603 *
5604 * This goes directly to the tear-down and rebuild of
5605 * the switch, since we need to do all the recovery as
5606 * for the Core Reset.
5607 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005608 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005609 i40e_handle_reset_warning(pf);
5610
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005611 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005612 int v;
5613
5614 /* Find the VSI(s) that requested a re-init */
5615 dev_info(&pf->pdev->dev,
5616 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005617 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005618 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005619
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005620 if (vsi != NULL &&
5621 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5622 i40e_vsi_reinit_locked(pf->vsi[v]);
5623 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5624 }
5625 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005626 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005627 int v;
5628
5629 /* Find the VSI(s) that needs to be brought down */
5630 dev_info(&pf->pdev->dev, "VSI down requested\n");
5631 for (v = 0; v < pf->num_alloc_vsi; v++) {
5632 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005633
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005634 if (vsi != NULL &&
5635 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5636 set_bit(__I40E_DOWN, &vsi->state);
5637 i40e_down(vsi);
5638 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5639 }
5640 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005641 } else {
5642 dev_info(&pf->pdev->dev,
5643 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005644 }
5645}
5646
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005647#ifdef CONFIG_I40E_DCB
5648/**
5649 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5650 * @pf: board private structure
5651 * @old_cfg: current DCB config
5652 * @new_cfg: new DCB config
5653 **/
5654bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5655 struct i40e_dcbx_config *old_cfg,
5656 struct i40e_dcbx_config *new_cfg)
5657{
5658 bool need_reconfig = false;
5659
5660 /* Check if ETS configuration has changed */
5661 if (memcmp(&new_cfg->etscfg,
5662 &old_cfg->etscfg,
5663 sizeof(new_cfg->etscfg))) {
5664 /* If Priority Table has changed reconfig is needed */
5665 if (memcmp(&new_cfg->etscfg.prioritytable,
5666 &old_cfg->etscfg.prioritytable,
5667 sizeof(new_cfg->etscfg.prioritytable))) {
5668 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005669 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005670 }
5671
5672 if (memcmp(&new_cfg->etscfg.tcbwtable,
5673 &old_cfg->etscfg.tcbwtable,
5674 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005675 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005676
5677 if (memcmp(&new_cfg->etscfg.tsatable,
5678 &old_cfg->etscfg.tsatable,
5679 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005680 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005681 }
5682
5683 /* Check if PFC configuration has changed */
5684 if (memcmp(&new_cfg->pfc,
5685 &old_cfg->pfc,
5686 sizeof(new_cfg->pfc))) {
5687 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005688 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005689 }
5690
5691 /* Check if APP Table has changed */
5692 if (memcmp(&new_cfg->app,
5693 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005694 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005695 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005696 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005697 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005698
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005699 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005700 return need_reconfig;
5701}
5702
5703/**
5704 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5705 * @pf: board private structure
5706 * @e: event info posted on ARQ
5707 **/
5708static int i40e_handle_lldp_event(struct i40e_pf *pf,
5709 struct i40e_arq_event_info *e)
5710{
5711 struct i40e_aqc_lldp_get_mib *mib =
5712 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5713 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005714 struct i40e_dcbx_config tmp_dcbx_cfg;
5715 bool need_reconfig = false;
5716 int ret = 0;
5717 u8 type;
5718
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005719 /* Not DCB capable or capability disabled */
Dave Ertmana0362442016-08-29 17:38:26 -07005720 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005721 return ret;
5722
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005723 /* Ignore if event is not for Nearest Bridge */
5724 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5725 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005726 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005727 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5728 return ret;
5729
5730 /* Check MIB Type and return if event for Remote MIB update */
5731 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005732 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005733 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005734 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5735 /* Update the remote cached instance and return */
5736 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5737 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5738 &hw->remote_dcbx_config);
5739 goto exit;
5740 }
5741
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005742 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005743 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005744
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005745 /* Reset the old DCBx configuration data */
5746 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005747 /* Get updated DCBX data from firmware */
5748 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005749 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005750 dev_info(&pf->pdev->dev,
5751 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5752 i40e_stat_str(&pf->hw, ret),
5753 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005754 goto exit;
5755 }
5756
5757 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005758 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5759 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005760 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005761 goto exit;
5762 }
5763
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005764 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5765 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005766
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005767 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005768
5769 if (!need_reconfig)
5770 goto exit;
5771
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005772 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005773 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005774 pf->flags |= I40E_FLAG_DCB_ENABLED;
5775 else
5776 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5777
Neerav Parikh69129dc2014-11-12 00:18:46 +00005778 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005779 /* Reconfiguration needed quiesce all VSIs */
5780 i40e_pf_quiesce_all_vsi(pf);
5781
5782 /* Changes in configuration update VEB/VSI */
5783 i40e_dcb_reconfigure(pf);
5784
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005785 ret = i40e_resume_port_tx(pf);
5786
Neerav Parikh69129dc2014-11-12 00:18:46 +00005787 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005788 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005789 if (ret)
5790 goto exit;
5791
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005792 /* Wait for the PF's queues to be disabled */
5793 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005794 if (ret) {
5795 /* Schedule PF reset to recover */
5796 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5797 i40e_service_event_schedule(pf);
5798 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005799 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005800 /* Notify the client for the DCB changes */
5801 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005802 }
5803
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005804exit:
5805 return ret;
5806}
5807#endif /* CONFIG_I40E_DCB */
5808
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005809/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005810 * i40e_do_reset_safe - Protected reset path for userland calls.
5811 * @pf: board private structure
5812 * @reset_flags: which reset is requested
5813 *
5814 **/
5815void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5816{
5817 rtnl_lock();
5818 i40e_do_reset(pf, reset_flags);
5819 rtnl_unlock();
5820}
5821
5822/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005823 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5824 * @pf: board private structure
5825 * @e: event info posted on ARQ
5826 *
5827 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5828 * and VF queues
5829 **/
5830static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5831 struct i40e_arq_event_info *e)
5832{
5833 struct i40e_aqc_lan_overflow *data =
5834 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5835 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5836 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5837 struct i40e_hw *hw = &pf->hw;
5838 struct i40e_vf *vf;
5839 u16 vf_id;
5840
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005841 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5842 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005843
5844 /* Queue belongs to VF, find the VF and issue VF reset */
5845 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5846 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5847 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5848 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5849 vf_id -= hw->func_caps.vf_base_id;
5850 vf = &pf->vf[vf_id];
5851 i40e_vc_notify_vf_reset(vf);
5852 /* Allow VF to process pending reset notification */
5853 msleep(20);
5854 i40e_reset_vf(vf, false);
5855 }
5856}
5857
5858/**
5859 * i40e_service_event_complete - Finish up the service event
5860 * @pf: board private structure
5861 **/
5862static void i40e_service_event_complete(struct i40e_pf *pf)
5863{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005864 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005865
5866 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005867 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005868 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5869}
5870
5871/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005872 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5873 * @pf: board private structure
5874 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005875u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005876{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005877 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005878
5879 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5880 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5881 return fcnt_prog;
5882}
5883
5884/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005885 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005886 * @pf: board private structure
5887 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005888u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005889{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005890 u32 val, fcnt_prog;
5891
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005892 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5893 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5894 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5895 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5896 return fcnt_prog;
5897}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005898
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005899/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005900 * i40e_get_global_fd_count - Get total FD filters programmed on device
5901 * @pf: board private structure
5902 **/
5903u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5904{
5905 u32 val, fcnt_prog;
5906
5907 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5908 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5909 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5910 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5911 return fcnt_prog;
5912}
5913
5914/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005915 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5916 * @pf: board private structure
5917 **/
5918void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5919{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005920 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005921 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005922 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005923
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005924 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5925 return;
5926
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005927 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5928 * to re-enable
5929 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005930 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005931 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005932 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5933 (pf->fd_add_err == 0) ||
5934 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005935 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5936 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5937 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005938 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5939 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 +00005940 }
5941 }
Jacob Kellera3417d22016-09-06 18:05:10 -07005942
5943 /* Wait for some more space to be available to turn on ATR. We also
5944 * must check that no existing ntuple rules for TCP are in effect
5945 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005946 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5947 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07005948 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
5949 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005950 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005951 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07005952 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 +00005953 }
5954 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005955
5956 /* if hw had a problem adding a filter, delete it */
5957 if (pf->fd_inv > 0) {
5958 hlist_for_each_entry_safe(filter, node,
5959 &pf->fdir_filter_list, fdir_node) {
5960 if (filter->fd_id == pf->fd_inv) {
5961 hlist_del(&filter->fdir_node);
5962 kfree(filter);
5963 pf->fdir_pf_active_filters--;
5964 }
5965 }
5966 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005967}
5968
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005969#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005970#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005971/**
5972 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5973 * @pf: board private structure
5974 **/
5975static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5976{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005977 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005978 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005979 bool disable_atr = false;
5980 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005981 int reg;
5982
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005983 if (!time_after(jiffies, pf->fd_flush_timestamp +
5984 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5985 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005986
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005987 /* If the flush is happening too quick and we have mostly SB rules we
5988 * should not re-enable ATR for some time.
5989 */
5990 min_flush_time = pf->fd_flush_timestamp +
5991 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5992 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005993
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005994 if (!(time_after(jiffies, min_flush_time)) &&
5995 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5996 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5997 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5998 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005999 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006000
6001 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006002 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006003 /* flush all filters */
6004 wr32(&pf->hw, I40E_PFQF_CTL_1,
6005 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6006 i40e_flush(&pf->hw);
6007 pf->fd_flush_cnt++;
6008 pf->fd_add_err = 0;
6009 do {
6010 /* Check FD flush status every 5-6msec */
6011 usleep_range(5000, 6000);
6012 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6013 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6014 break;
6015 } while (flush_wait_retry--);
6016 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6017 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6018 } else {
6019 /* replay sideband filters */
6020 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6021 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006022 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006023 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6024 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6025 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6026 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006027}
6028
6029/**
6030 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6031 * @pf: board private structure
6032 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006033u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006034{
6035 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6036}
6037
6038/* We can see up to 256 filter programming desc in transit if the filters are
6039 * being applied really fast; before we see the first
6040 * filter miss error on Rx queue 0. Accumulating enough error messages before
6041 * reacting will make sure we don't cause flush too often.
6042 */
6043#define I40E_MAX_FD_PROGRAM_ERROR 256
6044
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006045/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006046 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6047 * @pf: board private structure
6048 **/
6049static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6050{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006051
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006052 /* if interface is down do nothing */
6053 if (test_bit(__I40E_DOWN, &pf->state))
6054 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006055
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006056 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006057 i40e_fdir_flush_and_replay(pf);
6058
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006059 i40e_fdir_check_and_reenable(pf);
6060
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006061}
6062
6063/**
6064 * i40e_vsi_link_event - notify VSI of a link event
6065 * @vsi: vsi to be notified
6066 * @link_up: link up or down
6067 **/
6068static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6069{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006070 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006071 return;
6072
6073 switch (vsi->type) {
6074 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006075#ifdef I40E_FCOE
6076 case I40E_VSI_FCOE:
6077#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006078 if (!vsi->netdev || !vsi->netdev_registered)
6079 break;
6080
6081 if (link_up) {
6082 netif_carrier_on(vsi->netdev);
6083 netif_tx_wake_all_queues(vsi->netdev);
6084 } else {
6085 netif_carrier_off(vsi->netdev);
6086 netif_tx_stop_all_queues(vsi->netdev);
6087 }
6088 break;
6089
6090 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006091 case I40E_VSI_VMDQ2:
6092 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006093 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006094 case I40E_VSI_MIRROR:
6095 default:
6096 /* there is no notification for other VSIs */
6097 break;
6098 }
6099}
6100
6101/**
6102 * i40e_veb_link_event - notify elements on the veb of a link event
6103 * @veb: veb to be notified
6104 * @link_up: link up or down
6105 **/
6106static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6107{
6108 struct i40e_pf *pf;
6109 int i;
6110
6111 if (!veb || !veb->pf)
6112 return;
6113 pf = veb->pf;
6114
6115 /* depth first... */
6116 for (i = 0; i < I40E_MAX_VEB; i++)
6117 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6118 i40e_veb_link_event(pf->veb[i], link_up);
6119
6120 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006121 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006122 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6123 i40e_vsi_link_event(pf->vsi[i], link_up);
6124}
6125
6126/**
6127 * i40e_link_event - Update netif_carrier status
6128 * @pf: board private structure
6129 **/
6130static void i40e_link_event(struct i40e_pf *pf)
6131{
Mitch Williams320684c2014-10-17 03:14:43 +00006132 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006133 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006134 i40e_status status;
6135 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006137 /* save off old link status information */
6138 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6139
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006140 /* set this to force the get_link_status call to refresh state */
6141 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006142
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006143 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006144
6145 status = i40e_get_link_status(&pf->hw, &new_link);
6146 if (status) {
6147 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6148 status);
6149 return;
6150 }
6151
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006152 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6153 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006154
6155 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006156 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006157 (test_bit(__I40E_DOWN, &vsi->state) ||
6158 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006160
6161 if (!test_bit(__I40E_DOWN, &vsi->state))
6162 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163
6164 /* Notify the base of the switch tree connected to
6165 * the link. Floating VEBs are not notified.
6166 */
6167 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6168 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6169 else
Mitch Williams320684c2014-10-17 03:14:43 +00006170 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006171
6172 if (pf->vf)
6173 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006174
6175 if (pf->flags & I40E_FLAG_PTP)
6176 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006177}
6178
6179/**
Shannon Nelson21536712014-10-25 10:35:25 +00006180 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006181 * @pf: board private structure
6182 **/
6183static void i40e_watchdog_subtask(struct i40e_pf *pf)
6184{
6185 int i;
6186
6187 /* if interface is down do nothing */
6188 if (test_bit(__I40E_DOWN, &pf->state) ||
6189 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6190 return;
6191
Shannon Nelson21536712014-10-25 10:35:25 +00006192 /* make sure we don't do these things too often */
6193 if (time_before(jiffies, (pf->service_timer_previous +
6194 pf->service_timer_period)))
6195 return;
6196 pf->service_timer_previous = jiffies;
6197
Shannon Nelson9ac77262015-08-27 11:42:40 -04006198 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6199 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006200
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006201 /* Update the stats for active netdevs so the network stack
6202 * can look at updated numbers whenever it cares to
6203 */
Mitch Williams505682c2014-05-20 08:01:37 +00006204 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006205 if (pf->vsi[i] && pf->vsi[i]->netdev)
6206 i40e_update_stats(pf->vsi[i]);
6207
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006208 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6209 /* Update the stats for the active switching components */
6210 for (i = 0; i < I40E_MAX_VEB; i++)
6211 if (pf->veb[i])
6212 i40e_update_veb_stats(pf->veb[i]);
6213 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006214
6215 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006216}
6217
6218/**
6219 * i40e_reset_subtask - Set up for resetting the device and driver
6220 * @pf: board private structure
6221 **/
6222static void i40e_reset_subtask(struct i40e_pf *pf)
6223{
6224 u32 reset_flags = 0;
6225
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006226 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006227 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006228 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006229 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6230 }
6231 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006232 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006233 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6234 }
6235 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006236 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006237 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6238 }
6239 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006240 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006241 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6242 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006243 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006244 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006245 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6246 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006247
6248 /* If there's a recovery already waiting, it takes
6249 * precedence before starting a new reset sequence.
6250 */
6251 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6252 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006253 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254 }
6255
6256 /* If we're already down or resetting, just bail */
6257 if (reset_flags &&
6258 !test_bit(__I40E_DOWN, &pf->state) &&
6259 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6260 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006261
6262unlock:
6263 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006264}
6265
6266/**
6267 * i40e_handle_link_event - Handle link event
6268 * @pf: board private structure
6269 * @e: event info posted on ARQ
6270 **/
6271static void i40e_handle_link_event(struct i40e_pf *pf,
6272 struct i40e_arq_event_info *e)
6273{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006274 struct i40e_aqc_get_link_status *status =
6275 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006276
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006277 /* Do a new status request to re-enable LSE reporting
6278 * and load new status information into the hw struct
6279 * This completely ignores any state information
6280 * in the ARQ event info, instead choosing to always
6281 * issue the AQ update link status command.
6282 */
6283 i40e_link_event(pf);
6284
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006285 /* check for unqualified module, if link is down */
6286 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6287 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6288 (!(status->link_info & I40E_AQ_LINK_UP)))
6289 dev_err(&pf->pdev->dev,
6290 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006291}
6292
6293/**
6294 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6295 * @pf: board private structure
6296 **/
6297static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6298{
6299 struct i40e_arq_event_info event;
6300 struct i40e_hw *hw = &pf->hw;
6301 u16 pending, i = 0;
6302 i40e_status ret;
6303 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006304 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006305 u32 val;
6306
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006307 /* Do not run clean AQ when PF reset fails */
6308 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6309 return;
6310
Shannon Nelson86df2422014-05-20 08:01:35 +00006311 /* check for error indications */
6312 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6313 oldval = val;
6314 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006315 if (hw->debug_mask & I40E_DEBUG_AQ)
6316 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006317 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6318 }
6319 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006320 if (hw->debug_mask & I40E_DEBUG_AQ)
6321 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006322 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006323 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006324 }
6325 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006326 if (hw->debug_mask & I40E_DEBUG_AQ)
6327 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006328 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6329 }
6330 if (oldval != val)
6331 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6332
6333 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6334 oldval = val;
6335 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006336 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6337 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006338 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6339 }
6340 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006341 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6342 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006343 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6344 }
6345 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006346 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6347 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006348 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6349 }
6350 if (oldval != val)
6351 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6352
Mitch Williams1001dc32014-11-11 20:02:19 +00006353 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6354 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006355 if (!event.msg_buf)
6356 return;
6357
6358 do {
6359 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006360 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361 break;
Mitch Williams56497972014-06-04 08:45:18 +00006362 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006363 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6364 break;
6365 }
6366
6367 opcode = le16_to_cpu(event.desc.opcode);
6368 switch (opcode) {
6369
6370 case i40e_aqc_opc_get_link_status:
6371 i40e_handle_link_event(pf, &event);
6372 break;
6373 case i40e_aqc_opc_send_msg_to_pf:
6374 ret = i40e_vc_process_vf_msg(pf,
6375 le16_to_cpu(event.desc.retval),
6376 le32_to_cpu(event.desc.cookie_high),
6377 le32_to_cpu(event.desc.cookie_low),
6378 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006379 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006380 break;
6381 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006382 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006383#ifdef CONFIG_I40E_DCB
6384 rtnl_lock();
6385 ret = i40e_handle_lldp_event(pf, &event);
6386 rtnl_unlock();
6387#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006388 break;
6389 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006390 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391 i40e_handle_lan_overflow_event(pf, &event);
6392 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006393 case i40e_aqc_opc_send_msg_to_peer:
6394 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6395 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006396 case i40e_aqc_opc_nvm_erase:
6397 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006398 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006399 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6400 "ARQ NVM operation 0x%04x completed\n",
6401 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006402 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006403 default:
6404 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006405 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006406 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006407 break;
6408 }
6409 } while (pending && (i++ < pf->adminq_work_limit));
6410
6411 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6412 /* re-enable Admin queue interrupt cause */
6413 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6414 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6415 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6416 i40e_flush(hw);
6417
6418 kfree(event.msg_buf);
6419}
6420
6421/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006422 * i40e_verify_eeprom - make sure eeprom is good to use
6423 * @pf: board private structure
6424 **/
6425static void i40e_verify_eeprom(struct i40e_pf *pf)
6426{
6427 int err;
6428
6429 err = i40e_diag_eeprom_test(&pf->hw);
6430 if (err) {
6431 /* retry in case of garbage read */
6432 err = i40e_diag_eeprom_test(&pf->hw);
6433 if (err) {
6434 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6435 err);
6436 set_bit(__I40E_BAD_EEPROM, &pf->state);
6437 }
6438 }
6439
6440 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6441 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6442 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6443 }
6444}
6445
6446/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006447 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006448 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006449 *
6450 * enable switch loop back or die - no point in a return value
6451 **/
6452static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6453{
6454 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6455 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006456 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006457
6458 ctxt.seid = pf->main_vsi_seid;
6459 ctxt.pf_num = pf->hw.pf_id;
6460 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006461 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6462 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006463 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006464 "couldn't get PF vsi config, err %s aq_err %s\n",
6465 i40e_stat_str(&pf->hw, ret),
6466 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006467 return;
6468 }
6469 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6470 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6471 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6472
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006473 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6474 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006475 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006476 "update vsi switch failed, err %s aq_err %s\n",
6477 i40e_stat_str(&pf->hw, ret),
6478 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006479 }
6480}
6481
6482/**
6483 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006484 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006485 *
6486 * disable switch loop back or die - no point in a return value
6487 **/
6488static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6489{
6490 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6491 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006492 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006493
6494 ctxt.seid = pf->main_vsi_seid;
6495 ctxt.pf_num = pf->hw.pf_id;
6496 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006497 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6498 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006499 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006500 "couldn't get PF vsi config, err %s aq_err %s\n",
6501 i40e_stat_str(&pf->hw, ret),
6502 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006503 return;
6504 }
6505 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6506 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6507 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6508
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006509 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6510 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006511 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006512 "update vsi switch failed, err %s aq_err %s\n",
6513 i40e_stat_str(&pf->hw, ret),
6514 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006515 }
6516}
6517
6518/**
Neerav Parikh51616012015-02-06 08:52:14 +00006519 * i40e_config_bridge_mode - Configure the HW bridge mode
6520 * @veb: pointer to the bridge instance
6521 *
6522 * Configure the loop back mode for the LAN VSI that is downlink to the
6523 * specified HW bridge instance. It is expected this function is called
6524 * when a new HW bridge is instantiated.
6525 **/
6526static void i40e_config_bridge_mode(struct i40e_veb *veb)
6527{
6528 struct i40e_pf *pf = veb->pf;
6529
Shannon Nelson6dec1012015-09-28 14:12:30 -04006530 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6531 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6532 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006533 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6534 i40e_disable_pf_switch_lb(pf);
6535 else
6536 i40e_enable_pf_switch_lb(pf);
6537}
6538
6539/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006540 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6541 * @veb: pointer to the VEB instance
6542 *
6543 * This is a recursive function that first builds the attached VSIs then
6544 * recurses in to build the next layer of VEB. We track the connections
6545 * through our own index numbers because the seid's from the HW could
6546 * change across the reset.
6547 **/
6548static int i40e_reconstitute_veb(struct i40e_veb *veb)
6549{
6550 struct i40e_vsi *ctl_vsi = NULL;
6551 struct i40e_pf *pf = veb->pf;
6552 int v, veb_idx;
6553 int ret;
6554
6555 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006556 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006557 if (pf->vsi[v] &&
6558 pf->vsi[v]->veb_idx == veb->idx &&
6559 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6560 ctl_vsi = pf->vsi[v];
6561 break;
6562 }
6563 }
6564 if (!ctl_vsi) {
6565 dev_info(&pf->pdev->dev,
6566 "missing owner VSI for veb_idx %d\n", veb->idx);
6567 ret = -ENOENT;
6568 goto end_reconstitute;
6569 }
6570 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6571 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6572 ret = i40e_add_vsi(ctl_vsi);
6573 if (ret) {
6574 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006575 "rebuild of veb_idx %d owner VSI failed: %d\n",
6576 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006577 goto end_reconstitute;
6578 }
6579 i40e_vsi_reset_stats(ctl_vsi);
6580
6581 /* create the VEB in the switch and move the VSI onto the VEB */
6582 ret = i40e_add_veb(veb, ctl_vsi);
6583 if (ret)
6584 goto end_reconstitute;
6585
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006586 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6587 veb->bridge_mode = BRIDGE_MODE_VEB;
6588 else
6589 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006590 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006591
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006592 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006593 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006594 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6595 continue;
6596
6597 if (pf->vsi[v]->veb_idx == veb->idx) {
6598 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006599
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006600 vsi->uplink_seid = veb->seid;
6601 ret = i40e_add_vsi(vsi);
6602 if (ret) {
6603 dev_info(&pf->pdev->dev,
6604 "rebuild of vsi_idx %d failed: %d\n",
6605 v, ret);
6606 goto end_reconstitute;
6607 }
6608 i40e_vsi_reset_stats(vsi);
6609 }
6610 }
6611
6612 /* create any VEBs attached to this VEB - RECURSION */
6613 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6614 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6615 pf->veb[veb_idx]->uplink_seid = veb->seid;
6616 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6617 if (ret)
6618 break;
6619 }
6620 }
6621
6622end_reconstitute:
6623 return ret;
6624}
6625
6626/**
6627 * i40e_get_capabilities - get info about the HW
6628 * @pf: the PF struct
6629 **/
6630static int i40e_get_capabilities(struct i40e_pf *pf)
6631{
6632 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6633 u16 data_size;
6634 int buf_len;
6635 int err;
6636
6637 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6638 do {
6639 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6640 if (!cap_buf)
6641 return -ENOMEM;
6642
6643 /* this loads the data into the hw struct for us */
6644 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6645 &data_size,
6646 i40e_aqc_opc_list_func_capabilities,
6647 NULL);
6648 /* data loaded, buffer no longer needed */
6649 kfree(cap_buf);
6650
6651 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6652 /* retry with a larger buffer */
6653 buf_len = data_size;
6654 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6655 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006656 "capability discovery failed, err %s aq_err %s\n",
6657 i40e_stat_str(&pf->hw, err),
6658 i40e_aq_str(&pf->hw,
6659 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006660 return -ENODEV;
6661 }
6662 } while (err);
6663
6664 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6665 dev_info(&pf->pdev->dev,
6666 "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",
6667 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6668 pf->hw.func_caps.num_msix_vectors,
6669 pf->hw.func_caps.num_msix_vectors_vf,
6670 pf->hw.func_caps.fd_filters_guaranteed,
6671 pf->hw.func_caps.fd_filters_best_effort,
6672 pf->hw.func_caps.num_tx_qp,
6673 pf->hw.func_caps.num_vsis);
6674
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006675#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6676 + pf->hw.func_caps.num_vfs)
6677 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6678 dev_info(&pf->pdev->dev,
6679 "got num_vsis %d, setting num_vsis to %d\n",
6680 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6681 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6682 }
6683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006684 return 0;
6685}
6686
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006687static int i40e_vsi_clear(struct i40e_vsi *vsi);
6688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006689/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006690 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 * @pf: board private structure
6692 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006693static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006694{
6695 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006696 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006697
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006698 /* quick workaround for an NVM issue that leaves a critical register
6699 * uninitialized
6700 */
6701 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6702 static const u32 hkey[] = {
6703 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6704 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6705 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6706 0x95b3a76d};
6707
6708 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6709 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6710 }
6711
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006712 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006713 return;
6714
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006715 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006716 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006717 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006718 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006719 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006720 break;
6721 }
6722 }
6723
6724 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006725 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006726 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6727 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006728 if (!vsi) {
6729 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006730 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6731 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006732 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006733 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006734
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006735 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006736}
6737
6738/**
6739 * i40e_fdir_teardown - release the Flow Director resources
6740 * @pf: board private structure
6741 **/
6742static void i40e_fdir_teardown(struct i40e_pf *pf)
6743{
6744 int i;
6745
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006746 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006747 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006748 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6749 i40e_vsi_release(pf->vsi[i]);
6750 break;
6751 }
6752 }
6753}
6754
6755/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006756 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006757 * @pf: board private structure
6758 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006759 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006760 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006761static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006762{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006763 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006764 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006765 u32 v;
6766
6767 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6768 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006769 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006770 if (i40e_check_asq_alive(&pf->hw))
6771 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006772
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006773 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006775 /* quiesce the VSIs and their queues that are not already DOWN */
6776 i40e_pf_quiesce_all_vsi(pf);
6777
Mitch Williams505682c2014-05-20 08:01:37 +00006778 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006779 if (pf->vsi[v])
6780 pf->vsi[v]->seid = 0;
6781 }
6782
6783 i40e_shutdown_adminq(&pf->hw);
6784
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006785 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006786 if (hw->hmc.hmc_obj) {
6787 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006788 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006789 dev_warn(&pf->pdev->dev,
6790 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006791 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006792}
6793
6794/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006795 * i40e_send_version - update firmware with driver version
6796 * @pf: PF struct
6797 */
6798static void i40e_send_version(struct i40e_pf *pf)
6799{
6800 struct i40e_driver_version dv;
6801
6802 dv.major_version = DRV_VERSION_MAJOR;
6803 dv.minor_version = DRV_VERSION_MINOR;
6804 dv.build_version = DRV_VERSION_BUILD;
6805 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006806 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006807 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6808}
6809
6810/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006811 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006812 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006813 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006814 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006815static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006816{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006817 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006818 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006819 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006820 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006821 u32 v;
6822
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006823 /* Now we wait for GRST to settle out.
6824 * We don't have to delete the VEBs or VSIs from the hw switch
6825 * because the reset will make them disappear.
6826 */
6827 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006828 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006829 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006830 set_bit(__I40E_RESET_FAILED, &pf->state);
6831 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006832 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833 pf->pfr_count++;
6834
6835 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006836 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006837 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838
6839 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6840 ret = i40e_init_adminq(&pf->hw);
6841 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006842 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6843 i40e_stat_str(&pf->hw, ret),
6844 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006845 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 }
6847
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006848 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006849 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006850 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006851
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006852 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006853 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006854 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006855 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006856
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006857 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6858 hw->func_caps.num_rx_qp,
6859 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6860 if (ret) {
6861 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6862 goto end_core_reset;
6863 }
6864 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6865 if (ret) {
6866 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6867 goto end_core_reset;
6868 }
6869
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006870#ifdef CONFIG_I40E_DCB
6871 ret = i40e_init_pf_dcb(pf);
6872 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006873 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6874 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6875 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006876 }
6877#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006878#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006879 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006880
Vasu Dev38e00432014-08-01 13:27:03 -07006881#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006882 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006883 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006884 if (ret)
6885 goto end_core_reset;
6886
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006887 /* The driver only wants link up/down and module qualification
6888 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006889 */
6890 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006891 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006892 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006893 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006894 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006895 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6896 i40e_stat_str(&pf->hw, ret),
6897 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006898
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006899 /* make sure our flow control settings are restored */
6900 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6901 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006902 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6903 i40e_stat_str(&pf->hw, ret),
6904 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006905
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006906 /* Rebuild the VSIs and VEBs that existed before reset.
6907 * They are still in our local switch element arrays, so only
6908 * need to rebuild the switch model in the HW.
6909 *
6910 * If there were VEBs but the reconstitution failed, we'll try
6911 * try to recover minimal use by getting the basic PF VSI working.
6912 */
6913 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006914 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006915 /* find the one VEB connected to the MAC, and find orphans */
6916 for (v = 0; v < I40E_MAX_VEB; v++) {
6917 if (!pf->veb[v])
6918 continue;
6919
6920 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6921 pf->veb[v]->uplink_seid == 0) {
6922 ret = i40e_reconstitute_veb(pf->veb[v]);
6923
6924 if (!ret)
6925 continue;
6926
6927 /* If Main VEB failed, we're in deep doodoo,
6928 * so give up rebuilding the switch and set up
6929 * for minimal rebuild of PF VSI.
6930 * If orphan failed, we'll report the error
6931 * but try to keep going.
6932 */
6933 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6934 dev_info(&pf->pdev->dev,
6935 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6936 ret);
6937 pf->vsi[pf->lan_vsi]->uplink_seid
6938 = pf->mac_seid;
6939 break;
6940 } else if (pf->veb[v]->uplink_seid == 0) {
6941 dev_info(&pf->pdev->dev,
6942 "rebuild of orphan VEB failed: %d\n",
6943 ret);
6944 }
6945 }
6946 }
6947 }
6948
6949 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006950 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006951 /* no VEB, so rebuild only the Main VSI */
6952 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6953 if (ret) {
6954 dev_info(&pf->pdev->dev,
6955 "rebuild of Main VSI failed: %d\n", ret);
6956 goto end_core_reset;
6957 }
6958 }
6959
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006960 /* Reconfigure hardware for allowing smaller MSS in the case
6961 * of TSO, so that we avoid the MDD being fired and causing
6962 * a reset in the case of small MSS+TSO.
6963 */
6964#define I40E_REG_MSS 0x000E64DC
6965#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6966#define I40E_64BYTE_MSS 0x400000
6967 val = rd32(hw, I40E_REG_MSS);
6968 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6969 val &= ~I40E_REG_MSS_MIN_MASK;
6970 val |= I40E_64BYTE_MSS;
6971 wr32(hw, I40E_REG_MSS, val);
6972 }
6973
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006974 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006975 msleep(75);
6976 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6977 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006978 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6979 i40e_stat_str(&pf->hw, ret),
6980 i40e_aq_str(&pf->hw,
6981 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006982 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983 /* reinit the misc interrupt */
6984 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6985 ret = i40e_setup_misc_vector(pf);
6986
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006987 /* Add a filter to drop all Flow control frames from any VSI from being
6988 * transmitted. By doing so we stop a malicious VF from sending out
6989 * PAUSE or PFC frames and potentially controlling traffic for other
6990 * PF/VF VSIs.
6991 * The FW can still send Flow control frames if enabled.
6992 */
6993 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6994 pf->main_vsi_seid);
6995
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006996 /* restart the VSIs that were rebuilt and running before the reset */
6997 i40e_pf_unquiesce_all_vsi(pf);
6998
Mitch Williams69f64b22014-02-13 03:48:46 -08006999 if (pf->num_alloc_vfs) {
7000 for (v = 0; v < pf->num_alloc_vfs; v++)
7001 i40e_reset_vf(&pf->vf[v], true);
7002 }
7003
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007004 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007005 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007006
7007end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007008 clear_bit(__I40E_RESET_FAILED, &pf->state);
7009clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007010 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7011}
7012
7013/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007014 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007015 * @pf: board private structure
7016 *
7017 * Close up the VFs and other things in prep for a Core Reset,
7018 * then get ready to rebuild the world.
7019 **/
7020static void i40e_handle_reset_warning(struct i40e_pf *pf)
7021{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007022 i40e_prep_for_reset(pf);
7023 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007024}
7025
7026/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007027 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007028 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007029 *
7030 * Called from the MDD irq handler to identify possibly malicious vfs
7031 **/
7032static void i40e_handle_mdd_event(struct i40e_pf *pf)
7033{
7034 struct i40e_hw *hw = &pf->hw;
7035 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007036 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007037 struct i40e_vf *vf;
7038 u32 reg;
7039 int i;
7040
7041 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7042 return;
7043
7044 /* find what triggered the MDD event */
7045 reg = rd32(hw, I40E_GL_MDET_TX);
7046 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007047 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7048 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007049 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007050 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007051 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007052 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007053 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7054 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7055 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007056 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007057 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 +00007058 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007059 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7060 mdd_detected = true;
7061 }
7062 reg = rd32(hw, I40E_GL_MDET_RX);
7063 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007064 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7065 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007066 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007067 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007068 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7069 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7070 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007071 if (netif_msg_rx_err(pf))
7072 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7073 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007074 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7075 mdd_detected = true;
7076 }
7077
Neerav Parikhdf430b12014-06-04 01:23:15 +00007078 if (mdd_detected) {
7079 reg = rd32(hw, I40E_PF_MDET_TX);
7080 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7081 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007082 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007083 pf_mdd_detected = true;
7084 }
7085 reg = rd32(hw, I40E_PF_MDET_RX);
7086 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7087 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007088 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007089 pf_mdd_detected = true;
7090 }
7091 /* Queue belongs to the PF, initiate a reset */
7092 if (pf_mdd_detected) {
7093 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7094 i40e_service_event_schedule(pf);
7095 }
7096 }
7097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007098 /* see if one of the VFs needs its hand slapped */
7099 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7100 vf = &(pf->vf[i]);
7101 reg = rd32(hw, I40E_VP_MDET_TX(i));
7102 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7103 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7104 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007105 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7106 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007107 }
7108
7109 reg = rd32(hw, I40E_VP_MDET_RX(i));
7110 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7111 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7112 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007113 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7114 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007115 }
7116
7117 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7118 dev_info(&pf->pdev->dev,
7119 "Too many MDD events on VF %d, disabled\n", i);
7120 dev_info(&pf->pdev->dev,
7121 "Use PF Control I/F to re-enable the VF\n");
7122 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7123 }
7124 }
7125
7126 /* re-enable mdd interrupt cause */
7127 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7128 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7129 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7130 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7131 i40e_flush(hw);
7132}
7133
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007134/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007135 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007136 * @pf: board private structure
7137 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007138static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007139{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007140 struct i40e_hw *hw = &pf->hw;
7141 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007142 __be16 port;
7143 int i;
7144
Singhai, Anjali6a899022015-12-14 12:21:18 -08007145 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007146 return;
7147
Singhai, Anjali6a899022015-12-14 12:21:18 -08007148 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007149
7150 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007151 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7152 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7153 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007154 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007155 ret = i40e_aq_add_udp_tunnel(hw, port,
7156 pf->udp_ports[i].type,
7157 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007158 else
7159 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007160
7161 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007162 dev_dbg(&pf->pdev->dev,
7163 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7164 pf->udp_ports[i].type ? "vxlan" : "geneve",
7165 port ? "add" : "delete",
7166 ntohs(port), i,
7167 i40e_stat_str(&pf->hw, ret),
7168 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007169 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007170 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007171 }
7172 }
7173 }
7174}
7175
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007176/**
7177 * i40e_service_task - Run the driver's async subtasks
7178 * @work: pointer to work_struct containing our data
7179 **/
7180static void i40e_service_task(struct work_struct *work)
7181{
7182 struct i40e_pf *pf = container_of(work,
7183 struct i40e_pf,
7184 service_task);
7185 unsigned long start_time = jiffies;
7186
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007187 /* don't bother with service tasks if a reset is in progress */
7188 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7189 i40e_service_event_complete(pf);
7190 return;
7191 }
7192
Kiran Patilb03a8c12015-09-24 18:13:15 -04007193 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007194 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007195 i40e_reset_subtask(pf);
7196 i40e_handle_mdd_event(pf);
7197 i40e_vc_process_vflr_event(pf);
7198 i40e_watchdog_subtask(pf);
7199 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007200 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007201 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007202 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007203 i40e_clean_adminq_subtask(pf);
7204
7205 i40e_service_event_complete(pf);
7206
7207 /* If the tasks have taken longer than one timer cycle or there
7208 * is more work to be done, reschedule the service task now
7209 * rather than wait for the timer to tick again.
7210 */
7211 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7212 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7213 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7214 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7215 i40e_service_event_schedule(pf);
7216}
7217
7218/**
7219 * i40e_service_timer - timer callback
7220 * @data: pointer to PF struct
7221 **/
7222static void i40e_service_timer(unsigned long data)
7223{
7224 struct i40e_pf *pf = (struct i40e_pf *)data;
7225
7226 mod_timer(&pf->service_timer,
7227 round_jiffies(jiffies + pf->service_timer_period));
7228 i40e_service_event_schedule(pf);
7229}
7230
7231/**
7232 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7233 * @vsi: the VSI being configured
7234 **/
7235static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7236{
7237 struct i40e_pf *pf = vsi->back;
7238
7239 switch (vsi->type) {
7240 case I40E_VSI_MAIN:
7241 vsi->alloc_queue_pairs = pf->num_lan_qps;
7242 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7243 I40E_REQ_DESCRIPTOR_MULTIPLE);
7244 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7245 vsi->num_q_vectors = pf->num_lan_msix;
7246 else
7247 vsi->num_q_vectors = 1;
7248
7249 break;
7250
7251 case I40E_VSI_FDIR:
7252 vsi->alloc_queue_pairs = 1;
7253 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7254 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007255 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007256 break;
7257
7258 case I40E_VSI_VMDQ2:
7259 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7260 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7261 I40E_REQ_DESCRIPTOR_MULTIPLE);
7262 vsi->num_q_vectors = pf->num_vmdq_msix;
7263 break;
7264
7265 case I40E_VSI_SRIOV:
7266 vsi->alloc_queue_pairs = pf->num_vf_qps;
7267 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7268 I40E_REQ_DESCRIPTOR_MULTIPLE);
7269 break;
7270
Vasu Dev38e00432014-08-01 13:27:03 -07007271#ifdef I40E_FCOE
7272 case I40E_VSI_FCOE:
7273 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7274 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7275 I40E_REQ_DESCRIPTOR_MULTIPLE);
7276 vsi->num_q_vectors = pf->num_fcoe_msix;
7277 break;
7278
7279#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007280 default:
7281 WARN_ON(1);
7282 return -ENODATA;
7283 }
7284
7285 return 0;
7286}
7287
7288/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007289 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7290 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007291 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007292 *
7293 * On error: returns error code (negative)
7294 * On success: returns 0
7295 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007296static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007297{
7298 int size;
7299 int ret = 0;
7300
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007301 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007302 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7303 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7304 if (!vsi->tx_rings)
7305 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007306 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7307
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007308 if (alloc_qvectors) {
7309 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007310 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007311 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7312 if (!vsi->q_vectors) {
7313 ret = -ENOMEM;
7314 goto err_vectors;
7315 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007316 }
7317 return ret;
7318
7319err_vectors:
7320 kfree(vsi->tx_rings);
7321 return ret;
7322}
7323
7324/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007325 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7326 * @pf: board private structure
7327 * @type: type of VSI
7328 *
7329 * On error: returns error code (negative)
7330 * On success: returns vsi index in PF (positive)
7331 **/
7332static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7333{
7334 int ret = -ENODEV;
7335 struct i40e_vsi *vsi;
7336 int vsi_idx;
7337 int i;
7338
7339 /* Need to protect the allocation of the VSIs at the PF level */
7340 mutex_lock(&pf->switch_mutex);
7341
7342 /* VSI list may be fragmented if VSI creation/destruction has
7343 * been happening. We can afford to do a quick scan to look
7344 * for any free VSIs in the list.
7345 *
7346 * find next empty vsi slot, looping back around if necessary
7347 */
7348 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007349 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007350 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007351 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007352 i = 0;
7353 while (i < pf->next_vsi && pf->vsi[i])
7354 i++;
7355 }
7356
Mitch Williams505682c2014-05-20 08:01:37 +00007357 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007358 vsi_idx = i; /* Found one! */
7359 } else {
7360 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007361 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007362 }
7363 pf->next_vsi = ++i;
7364
7365 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7366 if (!vsi) {
7367 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007368 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007369 }
7370 vsi->type = type;
7371 vsi->back = pf;
7372 set_bit(__I40E_DOWN, &vsi->state);
7373 vsi->flags = 0;
7374 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007375 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007376 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7377 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007378 vsi->netdev_registered = false;
7379 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
7380 INIT_LIST_HEAD(&vsi->mac_filter_list);
Shannon Nelson63741842014-04-23 04:50:16 +00007381 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007382
Alexander Duyck9f65e152013-09-28 06:00:58 +00007383 ret = i40e_set_num_rings_in_vsi(vsi);
7384 if (ret)
7385 goto err_rings;
7386
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007387 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007388 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007389 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007390
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007391 /* Setup default MSIX irq handler for VSI */
7392 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7393
Kiran Patil21659032015-09-30 14:09:03 -04007394 /* Initialize VSI lock */
7395 spin_lock_init(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007396 pf->vsi[vsi_idx] = vsi;
7397 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007398 goto unlock_pf;
7399
Alexander Duyck9f65e152013-09-28 06:00:58 +00007400err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007401 pf->next_vsi = i - 1;
7402 kfree(vsi);
7403unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007404 mutex_unlock(&pf->switch_mutex);
7405 return ret;
7406}
7407
7408/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007409 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7410 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007411 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007412 *
7413 * On error: returns error code (negative)
7414 * On success: returns 0
7415 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007416static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007417{
7418 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007419 if (free_qvectors) {
7420 kfree(vsi->q_vectors);
7421 vsi->q_vectors = NULL;
7422 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007423 kfree(vsi->tx_rings);
7424 vsi->tx_rings = NULL;
7425 vsi->rx_rings = NULL;
7426}
7427
7428/**
Helin Zhang28c58692015-10-26 19:44:27 -04007429 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7430 * and lookup table
7431 * @vsi: Pointer to VSI structure
7432 */
7433static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7434{
7435 if (!vsi)
7436 return;
7437
7438 kfree(vsi->rss_hkey_user);
7439 vsi->rss_hkey_user = NULL;
7440
7441 kfree(vsi->rss_lut_user);
7442 vsi->rss_lut_user = NULL;
7443}
7444
7445/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007446 * i40e_vsi_clear - Deallocate the VSI provided
7447 * @vsi: the VSI being un-configured
7448 **/
7449static int i40e_vsi_clear(struct i40e_vsi *vsi)
7450{
7451 struct i40e_pf *pf;
7452
7453 if (!vsi)
7454 return 0;
7455
7456 if (!vsi->back)
7457 goto free_vsi;
7458 pf = vsi->back;
7459
7460 mutex_lock(&pf->switch_mutex);
7461 if (!pf->vsi[vsi->idx]) {
7462 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7463 vsi->idx, vsi->idx, vsi, vsi->type);
7464 goto unlock_vsi;
7465 }
7466
7467 if (pf->vsi[vsi->idx] != vsi) {
7468 dev_err(&pf->pdev->dev,
7469 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7470 pf->vsi[vsi->idx]->idx,
7471 pf->vsi[vsi->idx],
7472 pf->vsi[vsi->idx]->type,
7473 vsi->idx, vsi, vsi->type);
7474 goto unlock_vsi;
7475 }
7476
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007477 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007478 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7479 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7480
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007481 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007482 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007483
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007484 pf->vsi[vsi->idx] = NULL;
7485 if (vsi->idx < pf->next_vsi)
7486 pf->next_vsi = vsi->idx;
7487
7488unlock_vsi:
7489 mutex_unlock(&pf->switch_mutex);
7490free_vsi:
7491 kfree(vsi);
7492
7493 return 0;
7494}
7495
7496/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007497 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7498 * @vsi: the VSI being cleaned
7499 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007500static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007501{
7502 int i;
7503
Greg Rose8e9dca52013-12-18 13:45:53 +00007504 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007505 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007506 kfree_rcu(vsi->tx_rings[i], rcu);
7507 vsi->tx_rings[i] = NULL;
7508 vsi->rx_rings[i] = NULL;
7509 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007510 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007511}
7512
7513/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007514 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7515 * @vsi: the VSI being configured
7516 **/
7517static int i40e_alloc_rings(struct i40e_vsi *vsi)
7518{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007519 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007520 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007521 int i;
7522
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007523 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007524 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007525 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007526 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7527 if (!tx_ring)
7528 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007529
7530 tx_ring->queue_index = i;
7531 tx_ring->reg_idx = vsi->base_queue + i;
7532 tx_ring->ring_active = false;
7533 tx_ring->vsi = vsi;
7534 tx_ring->netdev = vsi->netdev;
7535 tx_ring->dev = &pf->pdev->dev;
7536 tx_ring->count = vsi->num_desc;
7537 tx_ring->size = 0;
7538 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007539 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7540 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007541 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007542 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007543
Alexander Duyck9f65e152013-09-28 06:00:58 +00007544 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007545 rx_ring->queue_index = i;
7546 rx_ring->reg_idx = vsi->base_queue + i;
7547 rx_ring->ring_active = false;
7548 rx_ring->vsi = vsi;
7549 rx_ring->netdev = vsi->netdev;
7550 rx_ring->dev = &pf->pdev->dev;
7551 rx_ring->count = vsi->num_desc;
7552 rx_ring->size = 0;
7553 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007554 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007555 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007556 }
7557
7558 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007559
7560err_out:
7561 i40e_vsi_clear_rings(vsi);
7562 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007563}
7564
7565/**
7566 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7567 * @pf: board private structure
7568 * @vectors: the number of MSI-X vectors to request
7569 *
7570 * Returns the number of vectors reserved, or error
7571 **/
7572static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7573{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007574 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7575 I40E_MIN_MSIX, vectors);
7576 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007577 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007578 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007579 vectors = 0;
7580 }
7581
7582 return vectors;
7583}
7584
7585/**
7586 * i40e_init_msix - Setup the MSIX capability
7587 * @pf: board private structure
7588 *
7589 * Work with the OS to set up the MSIX vectors needed.
7590 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007591 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007592 **/
7593static int i40e_init_msix(struct i40e_pf *pf)
7594{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007595 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007596 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007597 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007598 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007599 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007600
7601 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7602 return -ENODEV;
7603
7604 /* The number of vectors we'll request will be comprised of:
7605 * - Add 1 for "other" cause for Admin Queue events, etc.
7606 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007607 * - Queues being used for RSS.
7608 * We don't need as many as max_rss_size vectors.
7609 * use rss_size instead in the calculation since that
7610 * is governed by number of cpus in the system.
7611 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007612 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007613 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007614#ifdef I40E_FCOE
7615 * - The number of FCOE qps.
7616#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007617 * Once we count this up, try the request.
7618 *
7619 * If we can't get what we want, we'll simplify to nearly nothing
7620 * and try again. If that still fails, we punt.
7621 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007622 vectors_left = hw->func_caps.num_msix_vectors;
7623 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007624
Shannon Nelson1e200e42015-02-27 09:15:24 +00007625 /* reserve one vector for miscellaneous handler */
7626 if (vectors_left) {
7627 v_budget++;
7628 vectors_left--;
7629 }
7630
7631 /* reserve vectors for the main PF traffic queues */
7632 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7633 vectors_left -= pf->num_lan_msix;
7634 v_budget += pf->num_lan_msix;
7635
7636 /* reserve one vector for sideband flow director */
7637 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7638 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007639 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007640 v_budget++;
7641 vectors_left--;
7642 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007643 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007644 }
7645 }
John W Linville83840e42015-01-14 03:06:28 +00007646
Vasu Dev38e00432014-08-01 13:27:03 -07007647#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007648 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007649 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007650 if (!vectors_left)
7651 pf->num_fcoe_msix = 0;
7652 else if (vectors_left >= pf->num_fcoe_qps)
7653 pf->num_fcoe_msix = pf->num_fcoe_qps;
7654 else
7655 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007656 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007657 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007658 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007659
Vasu Dev38e00432014-08-01 13:27:03 -07007660#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007661 /* can we reserve enough for iWARP? */
7662 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007663 iwarp_requested = pf->num_iwarp_msix;
7664
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007665 if (!vectors_left)
7666 pf->num_iwarp_msix = 0;
7667 else if (vectors_left < pf->num_iwarp_msix)
7668 pf->num_iwarp_msix = 1;
7669 v_budget += pf->num_iwarp_msix;
7670 vectors_left -= pf->num_iwarp_msix;
7671 }
7672
Shannon Nelson1e200e42015-02-27 09:15:24 +00007673 /* any vectors left over go for VMDq support */
7674 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7675 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7676 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7677
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007678 if (!vectors_left) {
7679 pf->num_vmdq_msix = 0;
7680 pf->num_vmdq_qps = 0;
7681 } else {
7682 /* if we're short on vectors for what's desired, we limit
7683 * the queues per vmdq. If this is still more than are
7684 * available, the user will need to change the number of
7685 * queues/vectors used by the PF later with the ethtool
7686 * channels command
7687 */
7688 if (vmdq_vecs < vmdq_vecs_wanted)
7689 pf->num_vmdq_qps = 1;
7690 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007691
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007692 v_budget += vmdq_vecs;
7693 vectors_left -= vmdq_vecs;
7694 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007695 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696
7697 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7698 GFP_KERNEL);
7699 if (!pf->msix_entries)
7700 return -ENOMEM;
7701
7702 for (i = 0; i < v_budget; i++)
7703 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007704 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007705
Shannon Nelson3b444392015-02-26 16:15:57 +00007706 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007707 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7708 kfree(pf->msix_entries);
7709 pf->msix_entries = NULL;
7710 return -ENODEV;
7711
Shannon Nelson3b444392015-02-26 16:15:57 +00007712 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007713 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714 pf->num_vmdq_vsis = 0;
7715 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007716 pf->num_lan_qps = 1;
7717 pf->num_lan_msix = 1;
7718
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007719 } else if (!vectors_left) {
7720 /* If we have limited resources, we will start with no vectors
7721 * for the special features and then allocate vectors to some
7722 * of these features based on the policy and at the end disable
7723 * the features that did not get any vectors.
7724 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007725 int vec;
7726
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007727 dev_info(&pf->pdev->dev,
7728 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007729 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007730 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007731
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007732 /* Scale vector usage down */
7733 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007734 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007735 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007736#ifdef I40E_FCOE
7737 pf->num_fcoe_qps = 0;
7738 pf->num_fcoe_msix = 0;
7739#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007740
7741 /* partition out the remaining vectors */
7742 switch (vec) {
7743 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007744 pf->num_lan_msix = 1;
7745 break;
7746 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007747 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7748 pf->num_lan_msix = 1;
7749 pf->num_iwarp_msix = 1;
7750 } else {
7751 pf->num_lan_msix = 2;
7752 }
Vasu Dev38e00432014-08-01 13:27:03 -07007753#ifdef I40E_FCOE
7754 /* give one vector to FCoE */
7755 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7756 pf->num_lan_msix = 1;
7757 pf->num_fcoe_msix = 1;
7758 }
Vasu Dev38e00432014-08-01 13:27:03 -07007759#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760 break;
7761 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007762 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7763 pf->num_iwarp_msix = min_t(int, (vec / 3),
7764 iwarp_requested);
7765 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7766 I40E_DEFAULT_NUM_VMDQ_VSI);
7767 } else {
7768 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7769 I40E_DEFAULT_NUM_VMDQ_VSI);
7770 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007771 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7772 pf->num_fdsb_msix = 1;
7773 vec--;
7774 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007775 pf->num_lan_msix = min_t(int,
7776 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7777 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007778 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007779#ifdef I40E_FCOE
7780 /* give one vector to FCoE */
7781 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7782 pf->num_fcoe_msix = 1;
7783 vec--;
7784 }
7785#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007786 break;
7787 }
7788 }
7789
Stefan Assmannabd97a92016-09-19 13:37:51 +02007790 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7791 (pf->num_fdsb_msix == 0)) {
7792 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7793 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7794 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007795 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7796 (pf->num_vmdq_msix == 0)) {
7797 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7798 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7799 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007800
7801 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7802 (pf->num_iwarp_msix == 0)) {
7803 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7804 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7805 }
Vasu Dev38e00432014-08-01 13:27:03 -07007806#ifdef I40E_FCOE
7807
7808 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7809 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7810 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7811 }
7812#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007813 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7814 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7815 pf->num_lan_msix,
7816 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7817 pf->num_fdsb_msix,
7818 pf->num_iwarp_msix);
7819
Shannon Nelson3b444392015-02-26 16:15:57 +00007820 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007821}
7822
7823/**
Greg Rose90e04072014-03-06 08:59:57 +00007824 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007825 * @vsi: the VSI being configured
7826 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007827 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007828 *
7829 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7830 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007831static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007832{
7833 struct i40e_q_vector *q_vector;
7834
7835 /* allocate q_vector */
7836 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7837 if (!q_vector)
7838 return -ENOMEM;
7839
7840 q_vector->vsi = vsi;
7841 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007842 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7843
Alexander Duyck493fb302013-09-28 07:01:44 +00007844 if (vsi->netdev)
7845 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007846 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007847
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007848 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7849 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7850
Alexander Duyck493fb302013-09-28 07:01:44 +00007851 /* tie q_vector and vsi together */
7852 vsi->q_vectors[v_idx] = q_vector;
7853
7854 return 0;
7855}
7856
7857/**
Greg Rose90e04072014-03-06 08:59:57 +00007858 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007859 * @vsi: the VSI being configured
7860 *
7861 * We allocate one q_vector per queue interrupt. If allocation fails we
7862 * return -ENOMEM.
7863 **/
Greg Rose90e04072014-03-06 08:59:57 +00007864static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007865{
7866 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007867 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007868
7869 /* if not MSIX, give the one vector only to the LAN VSI */
7870 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7871 num_q_vectors = vsi->num_q_vectors;
7872 else if (vsi == pf->vsi[pf->lan_vsi])
7873 num_q_vectors = 1;
7874 else
7875 return -EINVAL;
7876
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007877 current_cpu = cpumask_first(cpu_online_mask);
7878
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007879 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007880 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007881 if (err)
7882 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007883 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7884 if (unlikely(current_cpu >= nr_cpu_ids))
7885 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007886 }
7887
7888 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007889
7890err_out:
7891 while (v_idx--)
7892 i40e_free_q_vector(vsi, v_idx);
7893
7894 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007895}
7896
7897/**
7898 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7899 * @pf: board private structure to initialize
7900 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007901static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007902{
Shannon Nelson3b444392015-02-26 16:15:57 +00007903 int vectors = 0;
7904 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007905
7906 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007907 vectors = i40e_init_msix(pf);
7908 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007909 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007910 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007911#ifdef I40E_FCOE
7912 I40E_FLAG_FCOE_ENABLED |
7913#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007914 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007915 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007916 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007917 I40E_FLAG_SRIOV_ENABLED |
7918 I40E_FLAG_FD_SB_ENABLED |
7919 I40E_FLAG_FD_ATR_ENABLED |
7920 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007921
7922 /* rework the queue expectations without MSIX */
7923 i40e_determine_queue_usage(pf);
7924 }
7925 }
7926
7927 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7928 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007929 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007930 vectors = pci_enable_msi(pf->pdev);
7931 if (vectors < 0) {
7932 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7933 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007934 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7935 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007936 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007937 }
7938
Shannon Nelson958a3e32013-09-28 07:13:28 +00007939 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007940 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007941
Shannon Nelson3b444392015-02-26 16:15:57 +00007942 /* set up vector assignment tracking */
7943 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7944 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007945 if (!pf->irq_pile) {
7946 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7947 return -ENOMEM;
7948 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007949 pf->irq_pile->num_entries = vectors;
7950 pf->irq_pile->search_hint = 0;
7951
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007952 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007953 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007954
7955 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007956}
7957
7958/**
7959 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7960 * @pf: board private structure
7961 *
7962 * This sets up the handler for MSIX 0, which is used to manage the
7963 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7964 * when in MSI or Legacy interrupt mode.
7965 **/
7966static int i40e_setup_misc_vector(struct i40e_pf *pf)
7967{
7968 struct i40e_hw *hw = &pf->hw;
7969 int err = 0;
7970
7971 /* Only request the irq if this is the first time through, and
7972 * not when we're rebuilding after a Reset
7973 */
7974 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7975 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007976 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007977 if (err) {
7978 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007979 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007980 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981 return -EFAULT;
7982 }
7983 }
7984
Jacob Kellerab437b52014-12-14 01:55:08 +00007985 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007986
7987 /* associate no queues to the misc vector */
7988 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7989 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7990
7991 i40e_flush(hw);
7992
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007993 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007994
7995 return err;
7996}
7997
7998/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007999 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8000 * @vsi: vsi structure
8001 * @seed: RSS hash seed
8002 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008003static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8004 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008005{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008006 struct i40e_pf *pf = vsi->back;
8007 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008008 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008009
Jacob Keller776b2e12016-07-19 16:23:30 -07008010 if (seed) {
8011 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8012 (struct i40e_aqc_get_set_rss_key_data *)seed;
8013 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8014 if (ret) {
8015 dev_info(&pf->pdev->dev,
8016 "Cannot set RSS key, err %s aq_err %s\n",
8017 i40e_stat_str(hw, ret),
8018 i40e_aq_str(hw, hw->aq.asq_last_status));
8019 return ret;
8020 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008021 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008022 if (lut) {
8023 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008024
Jacob Keller776b2e12016-07-19 16:23:30 -07008025 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8026 if (ret) {
8027 dev_info(&pf->pdev->dev,
8028 "Cannot set RSS lut, err %s aq_err %s\n",
8029 i40e_stat_str(hw, ret),
8030 i40e_aq_str(hw, hw->aq.asq_last_status));
8031 return ret;
8032 }
8033 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008034 return ret;
8035}
8036
8037/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008038 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8039 * @vsi: Pointer to vsi structure
8040 * @seed: Buffter to store the hash keys
8041 * @lut: Buffer to store the lookup table entries
8042 * @lut_size: Size of buffer to store the lookup table entries
8043 *
8044 * Return 0 on success, negative on failure
8045 */
8046static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8047 u8 *lut, u16 lut_size)
8048{
8049 struct i40e_pf *pf = vsi->back;
8050 struct i40e_hw *hw = &pf->hw;
8051 int ret = 0;
8052
8053 if (seed) {
8054 ret = i40e_aq_get_rss_key(hw, vsi->id,
8055 (struct i40e_aqc_get_set_rss_key_data *)seed);
8056 if (ret) {
8057 dev_info(&pf->pdev->dev,
8058 "Cannot get RSS key, err %s aq_err %s\n",
8059 i40e_stat_str(&pf->hw, ret),
8060 i40e_aq_str(&pf->hw,
8061 pf->hw.aq.asq_last_status));
8062 return ret;
8063 }
8064 }
8065
8066 if (lut) {
8067 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8068
8069 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8070 if (ret) {
8071 dev_info(&pf->pdev->dev,
8072 "Cannot get RSS lut, err %s aq_err %s\n",
8073 i40e_stat_str(&pf->hw, ret),
8074 i40e_aq_str(&pf->hw,
8075 pf->hw.aq.asq_last_status));
8076 return ret;
8077 }
8078 }
8079
8080 return ret;
8081}
8082
8083/**
Jacob Keller0582b962016-07-19 16:23:29 -07008084 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8085 * @vsi: VSI structure
8086 **/
8087static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8088{
8089 u8 seed[I40E_HKEY_ARRAY_SIZE];
8090 struct i40e_pf *pf = vsi->back;
8091 u8 *lut;
8092 int ret;
8093
8094 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8095 return 0;
8096
Jacob Keller552b9962016-07-19 16:23:31 -07008097 if (!vsi->rss_size)
8098 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8099 vsi->num_queue_pairs);
8100 if (!vsi->rss_size)
8101 return -EINVAL;
8102
Jacob Keller0582b962016-07-19 16:23:29 -07008103 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8104 if (!lut)
8105 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008106 /* Use the user configured hash keys and lookup table if there is one,
8107 * otherwise use default
8108 */
8109 if (vsi->rss_lut_user)
8110 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8111 else
8112 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8113 if (vsi->rss_hkey_user)
8114 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8115 else
8116 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008117 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8118 kfree(lut);
8119
8120 return ret;
8121}
8122
8123/**
Helin Zhang043dd652015-10-21 19:56:23 -04008124 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008125 * @vsi: Pointer to vsi structure
8126 * @seed: RSS hash seed
8127 * @lut: Lookup table
8128 * @lut_size: Lookup table size
8129 *
8130 * Returns 0 on success, negative on failure
8131 **/
8132static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8133 const u8 *lut, u16 lut_size)
8134{
8135 struct i40e_pf *pf = vsi->back;
8136 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008137 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008138 u8 i;
8139
8140 /* Fill out hash function seed */
8141 if (seed) {
8142 u32 *seed_dw = (u32 *)seed;
8143
Mitch Williamsc4e18682016-04-12 08:30:40 -07008144 if (vsi->type == I40E_VSI_MAIN) {
8145 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8146 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8147 seed_dw[i]);
8148 } else if (vsi->type == I40E_VSI_SRIOV) {
8149 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8150 i40e_write_rx_ctl(hw,
8151 I40E_VFQF_HKEY1(i, vf_id),
8152 seed_dw[i]);
8153 } else {
8154 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8155 }
Helin Zhange69ff812015-10-21 19:56:22 -04008156 }
8157
8158 if (lut) {
8159 u32 *lut_dw = (u32 *)lut;
8160
Mitch Williamsc4e18682016-04-12 08:30:40 -07008161 if (vsi->type == I40E_VSI_MAIN) {
8162 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8163 return -EINVAL;
8164 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8165 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8166 } else if (vsi->type == I40E_VSI_SRIOV) {
8167 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8168 return -EINVAL;
8169 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8170 i40e_write_rx_ctl(hw,
8171 I40E_VFQF_HLUT1(i, vf_id),
8172 lut_dw[i]);
8173 } else {
8174 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8175 }
Helin Zhange69ff812015-10-21 19:56:22 -04008176 }
8177 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008178
8179 return 0;
8180}
8181
8182/**
Helin Zhang043dd652015-10-21 19:56:23 -04008183 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8184 * @vsi: Pointer to VSI structure
8185 * @seed: Buffer to store the keys
8186 * @lut: Buffer to store the lookup table entries
8187 * @lut_size: Size of buffer to store the lookup table entries
8188 *
8189 * Returns 0 on success, negative on failure
8190 */
8191static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8192 u8 *lut, u16 lut_size)
8193{
8194 struct i40e_pf *pf = vsi->back;
8195 struct i40e_hw *hw = &pf->hw;
8196 u16 i;
8197
8198 if (seed) {
8199 u32 *seed_dw = (u32 *)seed;
8200
8201 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008202 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008203 }
8204 if (lut) {
8205 u32 *lut_dw = (u32 *)lut;
8206
8207 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8208 return -EINVAL;
8209 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8210 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8211 }
8212
8213 return 0;
8214}
8215
8216/**
8217 * i40e_config_rss - Configure RSS keys and lut
8218 * @vsi: Pointer to VSI structure
8219 * @seed: RSS hash seed
8220 * @lut: Lookup table
8221 * @lut_size: Lookup table size
8222 *
8223 * Returns 0 on success, negative on failure
8224 */
8225int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8226{
8227 struct i40e_pf *pf = vsi->back;
8228
8229 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8230 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8231 else
8232 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8233}
8234
8235/**
8236 * i40e_get_rss - Get RSS keys and lut
8237 * @vsi: Pointer to VSI structure
8238 * @seed: Buffer to store the keys
8239 * @lut: Buffer to store the lookup table entries
8240 * lut_size: Size of buffer to store the lookup table entries
8241 *
8242 * Returns 0 on success, negative on failure
8243 */
8244int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8245{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008246 struct i40e_pf *pf = vsi->back;
8247
8248 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8249 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8250 else
8251 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008252}
8253
8254/**
Helin Zhange69ff812015-10-21 19:56:22 -04008255 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8256 * @pf: Pointer to board private structure
8257 * @lut: Lookup table
8258 * @rss_table_size: Lookup table size
8259 * @rss_size: Range of queue number for hashing
8260 */
Alan Bradyf1582352016-08-24 11:33:46 -07008261void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8262 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008263{
Helin Zhange69ff812015-10-21 19:56:22 -04008264 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008265
Helin Zhange69ff812015-10-21 19:56:22 -04008266 for (i = 0; i < rss_table_size; i++)
8267 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008268}
8269
8270/**
Helin Zhang043dd652015-10-21 19:56:23 -04008271 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008272 * @pf: board private structure
8273 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008274static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008275{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008276 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008277 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008278 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008279 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008280 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008281 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008282 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008284 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008285 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8286 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008287 hena |= i40e_pf_get_default_rss_hena(pf);
8288
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008289 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8290 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008291
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008292 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008293 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008294 reg_val = (pf->rss_table_size == 512) ?
8295 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8296 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008297 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008298
Helin Zhang28c58692015-10-26 19:44:27 -04008299 /* Determine the RSS size of the VSI */
8300 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008301 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8302 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008303 if (!vsi->rss_size)
8304 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008305
Helin Zhange69ff812015-10-21 19:56:22 -04008306 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8307 if (!lut)
8308 return -ENOMEM;
8309
Helin Zhang28c58692015-10-26 19:44:27 -04008310 /* Use user configured lut if there is one, otherwise use default */
8311 if (vsi->rss_lut_user)
8312 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8313 else
8314 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008315
Helin Zhang28c58692015-10-26 19:44:27 -04008316 /* Use user configured hash key if there is one, otherwise
8317 * use default.
8318 */
8319 if (vsi->rss_hkey_user)
8320 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8321 else
8322 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008323 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008324 kfree(lut);
8325
8326 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008327}
8328
8329/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008330 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8331 * @pf: board private structure
8332 * @queue_count: the requested queue count for rss.
8333 *
8334 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8335 * count which may be different from the requested queue count.
8336 **/
8337int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8338{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008339 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8340 int new_rss_size;
8341
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008342 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8343 return 0;
8344
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008345 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008346
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008347 if (queue_count != vsi->num_queue_pairs) {
8348 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008349 i40e_prep_for_reset(pf);
8350
Helin Zhangacd65442015-10-26 19:44:28 -04008351 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008352
8353 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008354
8355 /* Discard the user configured hash keys and lut, if less
8356 * queues are enabled.
8357 */
8358 if (queue_count < vsi->rss_size) {
8359 i40e_clear_rss_config_user(vsi);
8360 dev_dbg(&pf->pdev->dev,
8361 "discard user configured hash keys and lut\n");
8362 }
8363
8364 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008365 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8366 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008367
Helin Zhang043dd652015-10-21 19:56:23 -04008368 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008369 }
Lihong Yang12815052016-09-27 11:28:48 -07008370 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8371 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008372 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008373}
8374
8375/**
Greg Rosef4492db2015-02-06 08:52:12 +00008376 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8377 * @pf: board private structure
8378 **/
8379i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8380{
8381 i40e_status status;
8382 bool min_valid, max_valid;
8383 u32 max_bw, min_bw;
8384
8385 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8386 &min_valid, &max_valid);
8387
8388 if (!status) {
8389 if (min_valid)
8390 pf->npar_min_bw = min_bw;
8391 if (max_valid)
8392 pf->npar_max_bw = max_bw;
8393 }
8394
8395 return status;
8396}
8397
8398/**
8399 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8400 * @pf: board private structure
8401 **/
8402i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8403{
8404 struct i40e_aqc_configure_partition_bw_data bw_data;
8405 i40e_status status;
8406
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008407 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008408 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008409 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8410 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8411
8412 /* Set the new bandwidths */
8413 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8414
8415 return status;
8416}
8417
8418/**
8419 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8420 * @pf: board private structure
8421 **/
8422i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8423{
8424 /* Commit temporary BW setting to permanent NVM image */
8425 enum i40e_admin_queue_err last_aq_status;
8426 i40e_status ret;
8427 u16 nvm_word;
8428
8429 if (pf->hw.partition_id != 1) {
8430 dev_info(&pf->pdev->dev,
8431 "Commit BW only works on partition 1! This is partition %d",
8432 pf->hw.partition_id);
8433 ret = I40E_NOT_SUPPORTED;
8434 goto bw_commit_out;
8435 }
8436
8437 /* Acquire NVM for read access */
8438 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8439 last_aq_status = pf->hw.aq.asq_last_status;
8440 if (ret) {
8441 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008442 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8443 i40e_stat_str(&pf->hw, ret),
8444 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008445 goto bw_commit_out;
8446 }
8447
8448 /* Read word 0x10 of NVM - SW compatibility word 1 */
8449 ret = i40e_aq_read_nvm(&pf->hw,
8450 I40E_SR_NVM_CONTROL_WORD,
8451 0x10, sizeof(nvm_word), &nvm_word,
8452 false, NULL);
8453 /* Save off last admin queue command status before releasing
8454 * the NVM
8455 */
8456 last_aq_status = pf->hw.aq.asq_last_status;
8457 i40e_release_nvm(&pf->hw);
8458 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008459 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8460 i40e_stat_str(&pf->hw, ret),
8461 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008462 goto bw_commit_out;
8463 }
8464
8465 /* Wait a bit for NVM release to complete */
8466 msleep(50);
8467
8468 /* Acquire NVM for write access */
8469 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8470 last_aq_status = pf->hw.aq.asq_last_status;
8471 if (ret) {
8472 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008473 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8474 i40e_stat_str(&pf->hw, ret),
8475 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008476 goto bw_commit_out;
8477 }
8478 /* Write it back out unchanged to initiate update NVM,
8479 * which will force a write of the shadow (alt) RAM to
8480 * the NVM - thus storing the bandwidth values permanently.
8481 */
8482 ret = i40e_aq_update_nvm(&pf->hw,
8483 I40E_SR_NVM_CONTROL_WORD,
8484 0x10, sizeof(nvm_word),
8485 &nvm_word, true, NULL);
8486 /* Save off last admin queue command status before releasing
8487 * the NVM
8488 */
8489 last_aq_status = pf->hw.aq.asq_last_status;
8490 i40e_release_nvm(&pf->hw);
8491 if (ret)
8492 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008493 "BW settings NOT SAVED, err %s aq_err %s\n",
8494 i40e_stat_str(&pf->hw, ret),
8495 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008496bw_commit_out:
8497
8498 return ret;
8499}
8500
8501/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008502 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8503 * @pf: board private structure to initialize
8504 *
8505 * i40e_sw_init initializes the Adapter private data structure.
8506 * Fields are initialized based on PCI device information and
8507 * OS network device settings (MTU size).
8508 **/
8509static int i40e_sw_init(struct i40e_pf *pf)
8510{
8511 int err = 0;
8512 int size;
8513
8514 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
8515 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
8516 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
8517 if (I40E_DEBUG_USER & debug)
8518 pf->hw.debug_mask = debug;
8519 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
8520 I40E_DEFAULT_MSG_ENABLE);
8521 }
8522
8523 /* Set default capability flags */
8524 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8525 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008526 I40E_FLAG_MSIX_ENABLED;
8527
Mitch Williamsca99eb92014-04-04 04:43:07 +00008528 /* Set default ITR */
8529 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8530 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8531
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008532 /* Depending on PF configurations, it is possible that the RSS
8533 * maximum might end up larger than the available queues
8534 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008535 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008536 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008537 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008538 pf->rss_size_max = min_t(int, pf->rss_size_max,
8539 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008540 if (pf->hw.func_caps.rss) {
8541 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008542 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8543 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008544 }
8545
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008546 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008547 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008548 pf->flags |= I40E_FLAG_MFP_ENABLED;
8549 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008550 if (i40e_get_npar_bw_setting(pf))
8551 dev_warn(&pf->pdev->dev,
8552 "Could not get NPAR bw settings\n");
8553 else
8554 dev_info(&pf->pdev->dev,
8555 "Min BW = %8.8x, Max BW = %8.8x\n",
8556 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008557 }
8558
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008559 /* FW/NVM is not yet fixed in this regard */
8560 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8561 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8562 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8563 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008564 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8565 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008566 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008567 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008568 else
8569 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008570 pf->fdir_pf_filter_count =
8571 pf->hw.func_caps.fd_filters_guaranteed;
8572 pf->hw.fdir_shared_filter_count =
8573 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008574 }
8575
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008576 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008577 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008578 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008579 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008580 /* No DCB support for FW < v4.33 */
8581 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8582 }
8583
8584 /* Disable FW LLDP if FW < v4.3 */
8585 if (i40e_is_mac_710(&pf->hw) &&
8586 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8587 (pf->hw.aq.fw_maj_ver < 4)))
8588 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8589
8590 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8591 if (i40e_is_mac_710(&pf->hw) &&
8592 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8593 (pf->hw.aq.fw_maj_ver >= 5)))
8594 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008596 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008597 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008598 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008599 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008600 }
8601
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008602 if (pf->hw.func_caps.iwarp) {
8603 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8604 /* IWARP needs one extra vector for CQP just like MISC.*/
8605 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8606 }
8607
Vasu Dev38e00432014-08-01 13:27:03 -07008608#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008609 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008610
8611#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008612#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008613 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008614 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8615 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8616 pf->num_req_vfs = min_t(int,
8617 pf->hw.func_caps.num_vfs,
8618 I40E_MAX_VF_COUNT);
8619 }
8620#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008621 if (pf->hw.mac.type == I40E_MAC_X722) {
8622 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8623 I40E_FLAG_128_QP_RSS_CAPABLE |
8624 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8625 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8626 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008627 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008628 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008629 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008630 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008631 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8632 ((pf->hw.aq.api_maj_ver == 1) &&
8633 (pf->hw.aq.api_min_ver > 4))) {
8634 /* Supported in FW API version higher than 1.4 */
8635 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008636 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8637 } else {
8638 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008639 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008641 pf->eeprom_version = 0xDEAD;
8642 pf->lan_veb = I40E_NO_VEB;
8643 pf->lan_vsi = I40E_NO_VSI;
8644
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008645 /* By default FW has this off for performance reasons */
8646 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008648 /* set up queue assignment tracking */
8649 size = sizeof(struct i40e_lump_tracking)
8650 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8651 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8652 if (!pf->qp_pile) {
8653 err = -ENOMEM;
8654 goto sw_init_done;
8655 }
8656 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8657 pf->qp_pile->search_hint = 0;
8658
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008659 pf->tx_timeout_recovery_level = 1;
8660
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008661 mutex_init(&pf->switch_mutex);
8662
Greg Rosec668a122015-02-26 16:10:39 +00008663 /* If NPAR is enabled nudge the Tx scheduler */
8664 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8665 i40e_set_npar_bw_setting(pf);
8666
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008667sw_init_done:
8668 return err;
8669}
8670
8671/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008672 * i40e_set_ntuple - set the ntuple feature flag and take action
8673 * @pf: board private structure to initialize
8674 * @features: the feature set that the stack is suggesting
8675 *
8676 * returns a bool to indicate if reset needs to happen
8677 **/
8678bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8679{
8680 bool need_reset = false;
8681
8682 /* Check if Flow Director n-tuple support was enabled or disabled. If
8683 * the state changed, we need to reset.
8684 */
8685 if (features & NETIF_F_NTUPLE) {
8686 /* Enable filters and mark for reset */
8687 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8688 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008689 /* enable FD_SB only if there is MSI-X vector */
8690 if (pf->num_fdsb_msix > 0)
8691 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008692 } else {
8693 /* turn off filters, mark for reset and clear SW filter list */
8694 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8695 need_reset = true;
8696 i40e_fdir_filter_exit(pf);
8697 }
8698 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008699 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008700 /* reset fd counters */
8701 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8702 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008703 /* if ATR was auto disabled it can be re-enabled. */
8704 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008705 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008706 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008707 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8708 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8709 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008710 }
8711 return need_reset;
8712}
8713
8714/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008715 * i40e_clear_rss_lut - clear the rx hash lookup table
8716 * @vsi: the VSI being configured
8717 **/
8718static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8719{
8720 struct i40e_pf *pf = vsi->back;
8721 struct i40e_hw *hw = &pf->hw;
8722 u16 vf_id = vsi->vf_id;
8723 u8 i;
8724
8725 if (vsi->type == I40E_VSI_MAIN) {
8726 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8727 wr32(hw, I40E_PFQF_HLUT(i), 0);
8728 } else if (vsi->type == I40E_VSI_SRIOV) {
8729 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8730 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8731 } else {
8732 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8733 }
8734}
8735
8736/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008737 * i40e_set_features - set the netdev feature flags
8738 * @netdev: ptr to the netdev being adjusted
8739 * @features: the feature set that the stack is suggesting
8740 **/
8741static int i40e_set_features(struct net_device *netdev,
8742 netdev_features_t features)
8743{
8744 struct i40e_netdev_priv *np = netdev_priv(netdev);
8745 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008746 struct i40e_pf *pf = vsi->back;
8747 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008748
Alan Bradyd8ec9862016-07-27 12:02:38 -07008749 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8750 i40e_pf_config_rss(pf);
8751 else if (!(features & NETIF_F_RXHASH) &&
8752 netdev->features & NETIF_F_RXHASH)
8753 i40e_clear_rss_lut(vsi);
8754
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008755 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8756 i40e_vlan_stripping_enable(vsi);
8757 else
8758 i40e_vlan_stripping_disable(vsi);
8759
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008760 need_reset = i40e_set_ntuple(pf, features);
8761
8762 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008763 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008764
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008765 return 0;
8766}
8767
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008768/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008769 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008770 * @pf: board private structure
8771 * @port: The UDP port to look up
8772 *
8773 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8774 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008775static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008776{
8777 u8 i;
8778
8779 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008780 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008781 return i;
8782 }
8783
8784 return i;
8785}
8786
8787/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008788 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008789 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008790 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008791 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008792static void i40e_udp_tunnel_add(struct net_device *netdev,
8793 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008794{
8795 struct i40e_netdev_priv *np = netdev_priv(netdev);
8796 struct i40e_vsi *vsi = np->vsi;
8797 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008798 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008799 u8 next_idx;
8800 u8 idx;
8801
Singhai, Anjali6a899022015-12-14 12:21:18 -08008802 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008803
8804 /* Check if port already exists */
8805 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008806 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008807 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008808 return;
8809 }
8810
8811 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008812 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008813
8814 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008815 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008816 ntohs(port));
8817 return;
8818 }
8819
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008820 switch (ti->type) {
8821 case UDP_TUNNEL_TYPE_VXLAN:
8822 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8823 break;
8824 case UDP_TUNNEL_TYPE_GENEVE:
8825 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8826 return;
8827 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8828 break;
8829 default:
8830 return;
8831 }
8832
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008833 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008834 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008835 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8836 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008837}
8838
8839/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008840 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008841 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008842 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008843 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008844static void i40e_udp_tunnel_del(struct net_device *netdev,
8845 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008846{
8847 struct i40e_netdev_priv *np = netdev_priv(netdev);
8848 struct i40e_vsi *vsi = np->vsi;
8849 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008850 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008851 u8 idx;
8852
Singhai, Anjali6a899022015-12-14 12:21:18 -08008853 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008854
8855 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008856 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8857 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008858
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008859 switch (ti->type) {
8860 case UDP_TUNNEL_TYPE_VXLAN:
8861 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8862 goto not_found;
8863 break;
8864 case UDP_TUNNEL_TYPE_GENEVE:
8865 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8866 goto not_found;
8867 break;
8868 default:
8869 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008870 }
8871
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008872 /* if port exists, set it to 0 (mark for deletion)
8873 * and make it pending
8874 */
8875 pf->udp_ports[idx].index = 0;
8876 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008877 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8878
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008879 return;
8880not_found:
8881 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8882 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008883}
8884
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008885static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008886 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008887{
8888 struct i40e_netdev_priv *np = netdev_priv(netdev);
8889 struct i40e_pf *pf = np->vsi->back;
8890 struct i40e_hw *hw = &pf->hw;
8891
8892 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8893 return -EOPNOTSUPP;
8894
8895 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8896 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8897
8898 return 0;
8899}
8900
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008901/**
8902 * i40e_ndo_fdb_add - add an entry to the hardware database
8903 * @ndm: the input from the stack
8904 * @tb: pointer to array of nladdr (unused)
8905 * @dev: the net device pointer
8906 * @addr: the MAC address entry being added
8907 * @flags: instructions from stack about fdb operation
8908 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008909static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8910 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008911 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008912 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008913{
8914 struct i40e_netdev_priv *np = netdev_priv(dev);
8915 struct i40e_pf *pf = np->vsi->back;
8916 int err = 0;
8917
8918 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8919 return -EOPNOTSUPP;
8920
Or Gerlitz65891fe2014-12-14 18:19:05 +02008921 if (vid) {
8922 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8923 return -EINVAL;
8924 }
8925
Greg Rose4ba0dea2014-03-06 08:59:55 +00008926 /* Hardware does not support aging addresses so if a
8927 * ndm_state is given only allow permanent addresses
8928 */
8929 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8930 netdev_info(dev, "FDB only supports static addresses\n");
8931 return -EINVAL;
8932 }
8933
8934 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8935 err = dev_uc_add_excl(dev, addr);
8936 else if (is_multicast_ether_addr(addr))
8937 err = dev_mc_add_excl(dev, addr);
8938 else
8939 err = -EINVAL;
8940
8941 /* Only return duplicate errors if NLM_F_EXCL is set */
8942 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8943 err = 0;
8944
8945 return err;
8946}
8947
Neerav Parikh51616012015-02-06 08:52:14 +00008948/**
8949 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8950 * @dev: the netdev being configured
8951 * @nlh: RTNL message
8952 *
8953 * Inserts a new hardware bridge if not already created and
8954 * enables the bridging mode requested (VEB or VEPA). If the
8955 * hardware bridge has already been inserted and the request
8956 * is to change the mode then that requires a PF reset to
8957 * allow rebuild of the components with required hardware
8958 * bridge mode enabled.
8959 **/
8960static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008961 struct nlmsghdr *nlh,
8962 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008963{
8964 struct i40e_netdev_priv *np = netdev_priv(dev);
8965 struct i40e_vsi *vsi = np->vsi;
8966 struct i40e_pf *pf = vsi->back;
8967 struct i40e_veb *veb = NULL;
8968 struct nlattr *attr, *br_spec;
8969 int i, rem;
8970
8971 /* Only for PF VSI for now */
8972 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8973 return -EOPNOTSUPP;
8974
8975 /* Find the HW bridge for PF VSI */
8976 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8977 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8978 veb = pf->veb[i];
8979 }
8980
8981 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8982
8983 nla_for_each_nested(attr, br_spec, rem) {
8984 __u16 mode;
8985
8986 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8987 continue;
8988
8989 mode = nla_get_u16(attr);
8990 if ((mode != BRIDGE_MODE_VEPA) &&
8991 (mode != BRIDGE_MODE_VEB))
8992 return -EINVAL;
8993
8994 /* Insert a new HW bridge */
8995 if (!veb) {
8996 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8997 vsi->tc_config.enabled_tc);
8998 if (veb) {
8999 veb->bridge_mode = mode;
9000 i40e_config_bridge_mode(veb);
9001 } else {
9002 /* No Bridge HW offload available */
9003 return -ENOENT;
9004 }
9005 break;
9006 } else if (mode != veb->bridge_mode) {
9007 /* Existing HW bridge but different mode needs reset */
9008 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009009 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9010 if (mode == BRIDGE_MODE_VEB)
9011 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9012 else
9013 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9014 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009015 break;
9016 }
9017 }
9018
9019 return 0;
9020}
9021
9022/**
9023 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9024 * @skb: skb buff
9025 * @pid: process id
9026 * @seq: RTNL message seq #
9027 * @dev: the netdev being configured
9028 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009029 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009030 *
9031 * Return the mode in which the hardware bridge is operating in
9032 * i.e VEB or VEPA.
9033 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009034static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9035 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009036 u32 __always_unused filter_mask,
9037 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009038{
9039 struct i40e_netdev_priv *np = netdev_priv(dev);
9040 struct i40e_vsi *vsi = np->vsi;
9041 struct i40e_pf *pf = vsi->back;
9042 struct i40e_veb *veb = NULL;
9043 int i;
9044
9045 /* Only for PF VSI for now */
9046 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9047 return -EOPNOTSUPP;
9048
9049 /* Find the HW bridge for the PF VSI */
9050 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9051 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9052 veb = pf->veb[i];
9053 }
9054
9055 if (!veb)
9056 return 0;
9057
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009058 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07009059 nlflags, 0, 0, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009060}
Neerav Parikh51616012015-02-06 08:52:14 +00009061
Singhai, Anjali6a899022015-12-14 12:21:18 -08009062/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9063 * inner mac plus all inner ethertypes.
9064 */
9065#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009066/**
9067 * i40e_features_check - Validate encapsulated packet conforms to limits
9068 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009069 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009070 * @features: Offload features that the stack believes apply
9071 **/
9072static netdev_features_t i40e_features_check(struct sk_buff *skb,
9073 struct net_device *dev,
9074 netdev_features_t features)
9075{
9076 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009077 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009078 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009079 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009080
9081 return features;
9082}
9083
Shannon Nelson37a29732015-02-27 09:15:19 +00009084static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009085 .ndo_open = i40e_open,
9086 .ndo_stop = i40e_close,
9087 .ndo_start_xmit = i40e_lan_xmit_frame,
9088 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9089 .ndo_set_rx_mode = i40e_set_rx_mode,
9090 .ndo_validate_addr = eth_validate_addr,
9091 .ndo_set_mac_address = i40e_set_mac,
9092 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009093 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009094 .ndo_tx_timeout = i40e_tx_timeout,
9095 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9096 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9097#ifdef CONFIG_NET_POLL_CONTROLLER
9098 .ndo_poll_controller = i40e_netpoll,
9099#endif
John Fastabende4c67342016-02-16 21:16:15 -08009100 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009101#ifdef I40E_FCOE
9102 .ndo_fcoe_enable = i40e_fcoe_enable,
9103 .ndo_fcoe_disable = i40e_fcoe_disable,
9104#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009105 .ndo_set_features = i40e_set_features,
9106 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9107 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009108 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009109 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009110 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009111 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009112 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009113 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9114 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009115 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009116 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009117 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009118 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9119 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009120};
9121
9122/**
9123 * i40e_config_netdev - Setup the netdev flags
9124 * @vsi: the VSI being configured
9125 *
9126 * Returns 0 on success, negative value on failure
9127 **/
9128static int i40e_config_netdev(struct i40e_vsi *vsi)
9129{
9130 struct i40e_pf *pf = vsi->back;
9131 struct i40e_hw *hw = &pf->hw;
9132 struct i40e_netdev_priv *np;
9133 struct net_device *netdev;
9134 u8 mac_addr[ETH_ALEN];
9135 int etherdev_size;
9136
9137 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009138 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009139 if (!netdev)
9140 return -ENOMEM;
9141
9142 vsi->netdev = netdev;
9143 np = netdev_priv(netdev);
9144 np->vsi = vsi;
9145
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009146 netdev->hw_enc_features |= NETIF_F_SG |
9147 NETIF_F_IP_CSUM |
9148 NETIF_F_IPV6_CSUM |
9149 NETIF_F_HIGHDMA |
9150 NETIF_F_SOFT_FEATURES |
9151 NETIF_F_TSO |
9152 NETIF_F_TSO_ECN |
9153 NETIF_F_TSO6 |
9154 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009155 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009156 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009157 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009158 NETIF_F_GSO_UDP_TUNNEL |
9159 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009160 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009161 NETIF_F_SCTP_CRC |
9162 NETIF_F_RXHASH |
9163 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009164 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009165
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009166 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009167 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9168
9169 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009170
9171 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009172 netdev->vlan_features |= netdev->hw_enc_features |
9173 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009174
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009175 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009176 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009177
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009178 netdev->hw_features |= netdev->hw_enc_features |
9179 NETIF_F_HW_VLAN_CTAG_TX |
9180 NETIF_F_HW_VLAN_CTAG_RX;
9181
9182 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009183 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009184
9185 if (vsi->type == I40E_VSI_MAIN) {
9186 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009187 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009188 spin_lock_bh(&vsi->mac_filter_list_lock);
9189 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
9190 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009191 } else {
9192 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9193 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9194 pf->vsi[pf->lan_vsi]->netdev->name);
9195 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009196
9197 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009198 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
Kiran Patil21659032015-09-30 14:09:03 -04009199 spin_unlock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009200 }
Kiran Patil21659032015-09-30 14:09:03 -04009201
Greg Rose9a173902014-05-22 06:32:02 +00009202 ether_addr_copy(netdev->dev_addr, mac_addr);
9203 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009204
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009205 netdev->priv_flags |= IFF_UNICAST_FLT;
9206 netdev->priv_flags |= IFF_SUPP_NOFCS;
9207 /* Setup netdev TC information */
9208 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9209
9210 netdev->netdev_ops = &i40e_netdev_ops;
9211 netdev->watchdog_timeo = 5 * HZ;
9212 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009213#ifdef I40E_FCOE
9214 i40e_fcoe_config_netdev(netdev, vsi);
9215#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009216
Jarod Wilson91c527a2016-10-17 15:54:05 -04009217 /* MTU range: 68 - 9706 */
9218 netdev->min_mtu = ETH_MIN_MTU;
9219 netdev->max_mtu = I40E_MAX_RXBUFFER -
9220 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9221
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009222 return 0;
9223}
9224
9225/**
9226 * i40e_vsi_delete - Delete a VSI from the switch
9227 * @vsi: the VSI being removed
9228 *
9229 * Returns 0 on success, negative value on failure
9230 **/
9231static void i40e_vsi_delete(struct i40e_vsi *vsi)
9232{
9233 /* remove default VSI is not allowed */
9234 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9235 return;
9236
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009237 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009238}
9239
9240/**
Neerav Parikh51616012015-02-06 08:52:14 +00009241 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9242 * @vsi: the VSI being queried
9243 *
9244 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9245 **/
9246int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9247{
9248 struct i40e_veb *veb;
9249 struct i40e_pf *pf = vsi->back;
9250
9251 /* Uplink is not a bridge so default to VEB */
9252 if (vsi->veb_idx == I40E_NO_VEB)
9253 return 1;
9254
9255 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009256 if (!veb) {
9257 dev_info(&pf->pdev->dev,
9258 "There is no veb associated with the bridge\n");
9259 return -ENOENT;
9260 }
Neerav Parikh51616012015-02-06 08:52:14 +00009261
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009262 /* Uplink is a bridge in VEPA mode */
9263 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9264 return 0;
9265 } else {
9266 /* Uplink is a bridge in VEB mode */
9267 return 1;
9268 }
9269
9270 /* VEPA is now default bridge, so return 0 */
9271 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009272}
9273
9274/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009275 * i40e_add_vsi - Add a VSI to the switch
9276 * @vsi: the VSI being configured
9277 *
9278 * This initializes a VSI context depending on the VSI type to be added and
9279 * passes it down to the add_vsi aq command.
9280 **/
9281static int i40e_add_vsi(struct i40e_vsi *vsi)
9282{
9283 int ret = -ENODEV;
Kiran Patilf6bd0962016-06-20 09:10:34 -07009284 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009285 struct i40e_pf *pf = vsi->back;
9286 struct i40e_hw *hw = &pf->hw;
9287 struct i40e_vsi_context ctxt;
Kiran Patil21659032015-09-30 14:09:03 -04009288 struct i40e_mac_filter *f, *ftmp;
9289
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009290 u8 enabled_tc = 0x1; /* TC0 enabled */
9291 int f_count = 0;
9292
9293 memset(&ctxt, 0, sizeof(ctxt));
9294 switch (vsi->type) {
9295 case I40E_VSI_MAIN:
9296 /* The PF's main VSI is already setup as part of the
9297 * device initialization, so we'll not bother with
9298 * the add_vsi call, but we will retrieve the current
9299 * VSI context.
9300 */
9301 ctxt.seid = pf->main_vsi_seid;
9302 ctxt.pf_num = pf->hw.pf_id;
9303 ctxt.vf_num = 0;
9304 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9305 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9306 if (ret) {
9307 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009308 "couldn't get PF vsi config, err %s aq_err %s\n",
9309 i40e_stat_str(&pf->hw, ret),
9310 i40e_aq_str(&pf->hw,
9311 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009312 return -ENOENT;
9313 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009314 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009315 vsi->info.valid_sections = 0;
9316
9317 vsi->seid = ctxt.seid;
9318 vsi->id = ctxt.vsi_number;
9319
9320 enabled_tc = i40e_pf_get_tc_map(pf);
9321
9322 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009323 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9324 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009325 memset(&ctxt, 0, sizeof(ctxt));
9326 ctxt.seid = pf->main_vsi_seid;
9327 ctxt.pf_num = pf->hw.pf_id;
9328 ctxt.vf_num = 0;
9329 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9330 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9331 if (ret) {
9332 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009333 "update vsi failed, err %s aq_err %s\n",
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 goto err;
9339 }
9340 /* update the local VSI info queue map */
9341 i40e_vsi_update_queue_map(vsi, &ctxt);
9342 vsi->info.valid_sections = 0;
9343 } else {
9344 /* Default/Main VSI is only enabled for TC0
9345 * reconfigure it to enable all TCs that are
9346 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009347 * For MFP case the iSCSI PF would use this
9348 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009349 */
9350 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9351 if (ret) {
9352 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009353 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9354 enabled_tc,
9355 i40e_stat_str(&pf->hw, ret),
9356 i40e_aq_str(&pf->hw,
9357 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009358 ret = -ENOENT;
9359 }
9360 }
9361 break;
9362
9363 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009364 ctxt.pf_num = hw->pf_id;
9365 ctxt.vf_num = 0;
9366 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009367 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009368 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009369 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9370 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009371 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009372 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009373 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009374 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009375 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009376 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009377 break;
9378
9379 case I40E_VSI_VMDQ2:
9380 ctxt.pf_num = hw->pf_id;
9381 ctxt.vf_num = 0;
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_VMDQ2;
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
9396 /* Setup the VSI tx/rx queue map for TC0 only for now */
9397 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9398 break;
9399
9400 case I40E_VSI_SRIOV:
9401 ctxt.pf_num = hw->pf_id;
9402 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9403 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009404 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009405 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9406
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009407 /* This VSI is connected to VEB so the switch_id
9408 * should be set to zero by default.
9409 */
Neerav Parikh51616012015-02-06 08:52:14 +00009410 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9411 ctxt.info.valid_sections |=
9412 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9413 ctxt.info.switch_id =
9414 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9415 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009416
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009417 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9418 ctxt.info.valid_sections |=
9419 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9420 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009421 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9422 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009423 }
9424
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009425 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9426 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009427 if (pf->vf[vsi->vf_id].spoofchk) {
9428 ctxt.info.valid_sections |=
9429 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9430 ctxt.info.sec_flags |=
9431 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9432 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9433 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009434 /* Setup the VSI tx/rx queue map for TC0 only for now */
9435 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9436 break;
9437
Vasu Dev38e00432014-08-01 13:27:03 -07009438#ifdef I40E_FCOE
9439 case I40E_VSI_FCOE:
9440 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9441 if (ret) {
9442 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9443 return ret;
9444 }
9445 break;
9446
9447#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009448 case I40E_VSI_IWARP:
9449 /* send down message to iWARP */
9450 break;
9451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009452 default:
9453 return -ENODEV;
9454 }
9455
9456 if (vsi->type != I40E_VSI_MAIN) {
9457 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9458 if (ret) {
9459 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009460 "add vsi failed, err %s aq_err %s\n",
9461 i40e_stat_str(&pf->hw, ret),
9462 i40e_aq_str(&pf->hw,
9463 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009464 ret = -ENOENT;
9465 goto err;
9466 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009467 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009468 vsi->info.valid_sections = 0;
9469 vsi->seid = ctxt.seid;
9470 vsi->id = ctxt.vsi_number;
9471 }
Kiran Patilf6bd0962016-06-20 09:10:34 -07009472 /* Except FDIR VSI, for all othet VSI set the broadcast filter */
9473 if (vsi->type != I40E_VSI_FDIR) {
9474 aq_ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
9475 if (aq_ret) {
9476 ret = i40e_aq_rc_to_posix(aq_ret,
9477 hw->aq.asq_last_status);
9478 dev_info(&pf->pdev->dev,
9479 "set brdcast promisc failed, err %s, aq_err %s\n",
9480 i40e_stat_str(hw, aq_ret),
9481 i40e_aq_str(hw, hw->aq.asq_last_status));
9482 }
9483 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009484
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009485 vsi->active_filters = 0;
9486 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Kiran Patil21659032015-09-30 14:09:03 -04009487 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009488 /* If macvlan filters already exist, force them to get loaded */
9489 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009490 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009491 f_count++;
9492 }
Kiran Patil21659032015-09-30 14:09:03 -04009493 spin_unlock_bh(&vsi->mac_filter_list_lock);
9494
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009495 if (f_count) {
9496 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9497 pf->flags |= I40E_FLAG_FILTER_SYNC;
9498 }
9499
9500 /* Update VSI BW information */
9501 ret = i40e_vsi_get_bw_info(vsi);
9502 if (ret) {
9503 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009504 "couldn't get vsi bw info, err %s aq_err %s\n",
9505 i40e_stat_str(&pf->hw, ret),
9506 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009507 /* VSI is already added so not tearing that up */
9508 ret = 0;
9509 }
9510
9511err:
9512 return ret;
9513}
9514
9515/**
9516 * i40e_vsi_release - Delete a VSI and free its resources
9517 * @vsi: the VSI being removed
9518 *
9519 * Returns 0 on success or < 0 on error
9520 **/
9521int i40e_vsi_release(struct i40e_vsi *vsi)
9522{
9523 struct i40e_mac_filter *f, *ftmp;
9524 struct i40e_veb *veb = NULL;
9525 struct i40e_pf *pf;
9526 u16 uplink_seid;
9527 int i, n;
9528
9529 pf = vsi->back;
9530
9531 /* release of a VEB-owner or last VSI is not allowed */
9532 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9533 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9534 vsi->seid, vsi->uplink_seid);
9535 return -ENODEV;
9536 }
9537 if (vsi == pf->vsi[pf->lan_vsi] &&
9538 !test_bit(__I40E_DOWN, &pf->state)) {
9539 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9540 return -ENODEV;
9541 }
9542
9543 uplink_seid = vsi->uplink_seid;
9544 if (vsi->type != I40E_VSI_SRIOV) {
9545 if (vsi->netdev_registered) {
9546 vsi->netdev_registered = false;
9547 if (vsi->netdev) {
9548 /* results in a call to i40e_close() */
9549 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009550 }
9551 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009552 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009553 }
9554 i40e_vsi_disable_irq(vsi);
9555 }
9556
Kiran Patil21659032015-09-30 14:09:03 -04009557 spin_lock_bh(&vsi->mac_filter_list_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009558 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
9559 i40e_del_filter(vsi, f->macaddr, f->vlan,
9560 f->is_vf, f->is_netdev);
Kiran Patil21659032015-09-30 14:09:03 -04009561 spin_unlock_bh(&vsi->mac_filter_list_lock);
9562
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009563 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009564
9565 i40e_vsi_delete(vsi);
9566 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009567 if (vsi->netdev) {
9568 free_netdev(vsi->netdev);
9569 vsi->netdev = NULL;
9570 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009571 i40e_vsi_clear_rings(vsi);
9572 i40e_vsi_clear(vsi);
9573
9574 /* If this was the last thing on the VEB, except for the
9575 * controlling VSI, remove the VEB, which puts the controlling
9576 * VSI onto the next level down in the switch.
9577 *
9578 * Well, okay, there's one more exception here: don't remove
9579 * the orphan VEBs yet. We'll wait for an explicit remove request
9580 * from up the network stack.
9581 */
Mitch Williams505682c2014-05-20 08:01:37 +00009582 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009583 if (pf->vsi[i] &&
9584 pf->vsi[i]->uplink_seid == uplink_seid &&
9585 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9586 n++; /* count the VSIs */
9587 }
9588 }
9589 for (i = 0; i < I40E_MAX_VEB; i++) {
9590 if (!pf->veb[i])
9591 continue;
9592 if (pf->veb[i]->uplink_seid == uplink_seid)
9593 n++; /* count the VEBs */
9594 if (pf->veb[i]->seid == uplink_seid)
9595 veb = pf->veb[i];
9596 }
9597 if (n == 0 && veb && veb->uplink_seid != 0)
9598 i40e_veb_release(veb);
9599
9600 return 0;
9601}
9602
9603/**
9604 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9605 * @vsi: ptr to the VSI
9606 *
9607 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9608 * corresponding SW VSI structure and initializes num_queue_pairs for the
9609 * newly allocated VSI.
9610 *
9611 * Returns 0 on success or negative on failure
9612 **/
9613static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9614{
9615 int ret = -ENOENT;
9616 struct i40e_pf *pf = vsi->back;
9617
Alexander Duyck493fb302013-09-28 07:01:44 +00009618 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009619 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9620 vsi->seid);
9621 return -EEXIST;
9622 }
9623
9624 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009625 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009626 vsi->seid, vsi->base_vector);
9627 return -EEXIST;
9628 }
9629
Greg Rose90e04072014-03-06 08:59:57 +00009630 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009631 if (ret) {
9632 dev_info(&pf->pdev->dev,
9633 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9634 vsi->num_q_vectors, vsi->seid, ret);
9635 vsi->num_q_vectors = 0;
9636 goto vector_setup_out;
9637 }
9638
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009639 /* In Legacy mode, we do not have to get any other vector since we
9640 * piggyback on the misc/ICR0 for queue interrupts.
9641 */
9642 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9643 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009644 if (vsi->num_q_vectors)
9645 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9646 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009647 if (vsi->base_vector < 0) {
9648 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009649 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9650 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009651 i40e_vsi_free_q_vectors(vsi);
9652 ret = -ENOENT;
9653 goto vector_setup_out;
9654 }
9655
9656vector_setup_out:
9657 return ret;
9658}
9659
9660/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009661 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9662 * @vsi: pointer to the vsi.
9663 *
9664 * This re-allocates a vsi's queue resources.
9665 *
9666 * Returns pointer to the successfully allocated and configured VSI sw struct
9667 * on success, otherwise returns NULL on failure.
9668 **/
9669static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9670{
John Underwoodf5340392016-02-18 09:19:24 -08009671 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009672 u8 enabled_tc;
9673 int ret;
9674
John Underwoodf5340392016-02-18 09:19:24 -08009675 if (!vsi)
9676 return NULL;
9677
9678 pf = vsi->back;
9679
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009680 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9681 i40e_vsi_clear_rings(vsi);
9682
9683 i40e_vsi_free_arrays(vsi, false);
9684 i40e_set_num_rings_in_vsi(vsi);
9685 ret = i40e_vsi_alloc_arrays(vsi, false);
9686 if (ret)
9687 goto err_vsi;
9688
9689 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9690 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009691 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009692 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009693 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009694 goto err_vsi;
9695 }
9696 vsi->base_queue = ret;
9697
9698 /* Update the FW view of the VSI. Force a reset of TC and queue
9699 * layout configurations.
9700 */
9701 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9702 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9703 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9704 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9705
9706 /* assign it some queues */
9707 ret = i40e_alloc_rings(vsi);
9708 if (ret)
9709 goto err_rings;
9710
9711 /* map all of the rings to the q_vectors */
9712 i40e_vsi_map_rings_to_vectors(vsi);
9713 return vsi;
9714
9715err_rings:
9716 i40e_vsi_free_q_vectors(vsi);
9717 if (vsi->netdev_registered) {
9718 vsi->netdev_registered = false;
9719 unregister_netdev(vsi->netdev);
9720 free_netdev(vsi->netdev);
9721 vsi->netdev = NULL;
9722 }
9723 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9724err_vsi:
9725 i40e_vsi_clear(vsi);
9726 return NULL;
9727}
9728
9729/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009730 * i40e_vsi_setup - Set up a VSI by a given type
9731 * @pf: board private structure
9732 * @type: VSI type
9733 * @uplink_seid: the switch element to link to
9734 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9735 *
9736 * This allocates the sw VSI structure and its queue resources, then add a VSI
9737 * to the identified VEB.
9738 *
9739 * Returns pointer to the successfully allocated and configure VSI sw struct on
9740 * success, otherwise returns NULL on failure.
9741 **/
9742struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9743 u16 uplink_seid, u32 param1)
9744{
9745 struct i40e_vsi *vsi = NULL;
9746 struct i40e_veb *veb = NULL;
9747 int ret, i;
9748 int v_idx;
9749
9750 /* The requested uplink_seid must be either
9751 * - the PF's port seid
9752 * no VEB is needed because this is the PF
9753 * or this is a Flow Director special case VSI
9754 * - seid of an existing VEB
9755 * - seid of a VSI that owns an existing VEB
9756 * - seid of a VSI that doesn't own a VEB
9757 * a new VEB is created and the VSI becomes the owner
9758 * - seid of the PF VSI, which is what creates the first VEB
9759 * this is a special case of the previous
9760 *
9761 * Find which uplink_seid we were given and create a new VEB if needed
9762 */
9763 for (i = 0; i < I40E_MAX_VEB; i++) {
9764 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9765 veb = pf->veb[i];
9766 break;
9767 }
9768 }
9769
9770 if (!veb && uplink_seid != pf->mac_seid) {
9771
Mitch Williams505682c2014-05-20 08:01:37 +00009772 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009773 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9774 vsi = pf->vsi[i];
9775 break;
9776 }
9777 }
9778 if (!vsi) {
9779 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9780 uplink_seid);
9781 return NULL;
9782 }
9783
9784 if (vsi->uplink_seid == pf->mac_seid)
9785 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9786 vsi->tc_config.enabled_tc);
9787 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9788 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9789 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009790 if (veb) {
9791 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9792 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009793 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009794 return NULL;
9795 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009796 /* We come up by default in VEPA mode if SRIOV is not
9797 * already enabled, in which case we can't force VEPA
9798 * mode.
9799 */
9800 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9801 veb->bridge_mode = BRIDGE_MODE_VEPA;
9802 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9803 }
Neerav Parikh51616012015-02-06 08:52:14 +00009804 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009805 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009806 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9807 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9808 veb = pf->veb[i];
9809 }
9810 if (!veb) {
9811 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9812 return NULL;
9813 }
9814
9815 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9816 uplink_seid = veb->seid;
9817 }
9818
9819 /* get vsi sw struct */
9820 v_idx = i40e_vsi_mem_alloc(pf, type);
9821 if (v_idx < 0)
9822 goto err_alloc;
9823 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009824 if (!vsi)
9825 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009826 vsi->type = type;
9827 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9828
9829 if (type == I40E_VSI_MAIN)
9830 pf->lan_vsi = v_idx;
9831 else if (type == I40E_VSI_SRIOV)
9832 vsi->vf_id = param1;
9833 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009834 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9835 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009836 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009837 dev_info(&pf->pdev->dev,
9838 "failed to get tracking for %d queues for VSI %d err=%d\n",
9839 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009840 goto err_vsi;
9841 }
9842 vsi->base_queue = ret;
9843
9844 /* get a VSI from the hardware */
9845 vsi->uplink_seid = uplink_seid;
9846 ret = i40e_add_vsi(vsi);
9847 if (ret)
9848 goto err_vsi;
9849
9850 switch (vsi->type) {
9851 /* setup the netdev if needed */
9852 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009853 /* Apply relevant filters if a platform-specific mac
9854 * address was selected.
9855 */
9856 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9857 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9858 if (ret) {
9859 dev_warn(&pf->pdev->dev,
9860 "could not set up macaddr; err %d\n",
9861 ret);
9862 }
9863 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009864 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009865 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009866 ret = i40e_config_netdev(vsi);
9867 if (ret)
9868 goto err_netdev;
9869 ret = register_netdev(vsi->netdev);
9870 if (ret)
9871 goto err_netdev;
9872 vsi->netdev_registered = true;
9873 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009874#ifdef CONFIG_I40E_DCB
9875 /* Setup DCB netlink interface */
9876 i40e_dcbnl_setup(vsi);
9877#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009878 /* fall through */
9879
9880 case I40E_VSI_FDIR:
9881 /* set up vectors and rings if needed */
9882 ret = i40e_vsi_setup_vectors(vsi);
9883 if (ret)
9884 goto err_msix;
9885
9886 ret = i40e_alloc_rings(vsi);
9887 if (ret)
9888 goto err_rings;
9889
9890 /* map all of the rings to the q_vectors */
9891 i40e_vsi_map_rings_to_vectors(vsi);
9892
9893 i40e_vsi_reset_stats(vsi);
9894 break;
9895
9896 default:
9897 /* no netdev or rings for the other VSI types */
9898 break;
9899 }
9900
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009901 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9902 (vsi->type == I40E_VSI_VMDQ2)) {
9903 ret = i40e_vsi_config_rss(vsi);
9904 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009905 return vsi;
9906
9907err_rings:
9908 i40e_vsi_free_q_vectors(vsi);
9909err_msix:
9910 if (vsi->netdev_registered) {
9911 vsi->netdev_registered = false;
9912 unregister_netdev(vsi->netdev);
9913 free_netdev(vsi->netdev);
9914 vsi->netdev = NULL;
9915 }
9916err_netdev:
9917 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9918err_vsi:
9919 i40e_vsi_clear(vsi);
9920err_alloc:
9921 return NULL;
9922}
9923
9924/**
9925 * i40e_veb_get_bw_info - Query VEB BW information
9926 * @veb: the veb to query
9927 *
9928 * Query the Tx scheduler BW configuration data for given VEB
9929 **/
9930static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9931{
9932 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9933 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9934 struct i40e_pf *pf = veb->pf;
9935 struct i40e_hw *hw = &pf->hw;
9936 u32 tc_bw_max;
9937 int ret = 0;
9938 int i;
9939
9940 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9941 &bw_data, NULL);
9942 if (ret) {
9943 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009944 "query veb bw config failed, err %s aq_err %s\n",
9945 i40e_stat_str(&pf->hw, ret),
9946 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009947 goto out;
9948 }
9949
9950 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9951 &ets_data, NULL);
9952 if (ret) {
9953 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009954 "query veb bw ets config failed, err %s aq_err %s\n",
9955 i40e_stat_str(&pf->hw, ret),
9956 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009957 goto out;
9958 }
9959
9960 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9961 veb->bw_max_quanta = ets_data.tc_bw_max;
9962 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009963 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009964 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9965 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9966 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9967 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9968 veb->bw_tc_limit_credits[i] =
9969 le16_to_cpu(bw_data.tc_bw_limits[i]);
9970 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9971 }
9972
9973out:
9974 return ret;
9975}
9976
9977/**
9978 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9979 * @pf: board private structure
9980 *
9981 * On error: returns error code (negative)
9982 * On success: returns vsi index in PF (positive)
9983 **/
9984static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9985{
9986 int ret = -ENOENT;
9987 struct i40e_veb *veb;
9988 int i;
9989
9990 /* Need to protect the allocation of switch elements at the PF level */
9991 mutex_lock(&pf->switch_mutex);
9992
9993 /* VEB list may be fragmented if VEB creation/destruction has
9994 * been happening. We can afford to do a quick scan to look
9995 * for any free slots in the list.
9996 *
9997 * find next empty veb slot, looping back around if necessary
9998 */
9999 i = 0;
10000 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10001 i++;
10002 if (i >= I40E_MAX_VEB) {
10003 ret = -ENOMEM;
10004 goto err_alloc_veb; /* out of VEB slots! */
10005 }
10006
10007 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10008 if (!veb) {
10009 ret = -ENOMEM;
10010 goto err_alloc_veb;
10011 }
10012 veb->pf = pf;
10013 veb->idx = i;
10014 veb->enabled_tc = 1;
10015
10016 pf->veb[i] = veb;
10017 ret = i;
10018err_alloc_veb:
10019 mutex_unlock(&pf->switch_mutex);
10020 return ret;
10021}
10022
10023/**
10024 * i40e_switch_branch_release - Delete a branch of the switch tree
10025 * @branch: where to start deleting
10026 *
10027 * This uses recursion to find the tips of the branch to be
10028 * removed, deleting until we get back to and can delete this VEB.
10029 **/
10030static void i40e_switch_branch_release(struct i40e_veb *branch)
10031{
10032 struct i40e_pf *pf = branch->pf;
10033 u16 branch_seid = branch->seid;
10034 u16 veb_idx = branch->idx;
10035 int i;
10036
10037 /* release any VEBs on this VEB - RECURSION */
10038 for (i = 0; i < I40E_MAX_VEB; i++) {
10039 if (!pf->veb[i])
10040 continue;
10041 if (pf->veb[i]->uplink_seid == branch->seid)
10042 i40e_switch_branch_release(pf->veb[i]);
10043 }
10044
10045 /* Release the VSIs on this VEB, but not the owner VSI.
10046 *
10047 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10048 * the VEB itself, so don't use (*branch) after this loop.
10049 */
Mitch Williams505682c2014-05-20 08:01:37 +000010050 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010051 if (!pf->vsi[i])
10052 continue;
10053 if (pf->vsi[i]->uplink_seid == branch_seid &&
10054 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10055 i40e_vsi_release(pf->vsi[i]);
10056 }
10057 }
10058
10059 /* There's one corner case where the VEB might not have been
10060 * removed, so double check it here and remove it if needed.
10061 * This case happens if the veb was created from the debugfs
10062 * commands and no VSIs were added to it.
10063 */
10064 if (pf->veb[veb_idx])
10065 i40e_veb_release(pf->veb[veb_idx]);
10066}
10067
10068/**
10069 * i40e_veb_clear - remove veb struct
10070 * @veb: the veb to remove
10071 **/
10072static void i40e_veb_clear(struct i40e_veb *veb)
10073{
10074 if (!veb)
10075 return;
10076
10077 if (veb->pf) {
10078 struct i40e_pf *pf = veb->pf;
10079
10080 mutex_lock(&pf->switch_mutex);
10081 if (pf->veb[veb->idx] == veb)
10082 pf->veb[veb->idx] = NULL;
10083 mutex_unlock(&pf->switch_mutex);
10084 }
10085
10086 kfree(veb);
10087}
10088
10089/**
10090 * i40e_veb_release - Delete a VEB and free its resources
10091 * @veb: the VEB being removed
10092 **/
10093void i40e_veb_release(struct i40e_veb *veb)
10094{
10095 struct i40e_vsi *vsi = NULL;
10096 struct i40e_pf *pf;
10097 int i, n = 0;
10098
10099 pf = veb->pf;
10100
10101 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010102 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010103 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10104 n++;
10105 vsi = pf->vsi[i];
10106 }
10107 }
10108 if (n != 1) {
10109 dev_info(&pf->pdev->dev,
10110 "can't remove VEB %d with %d VSIs left\n",
10111 veb->seid, n);
10112 return;
10113 }
10114
10115 /* move the remaining VSI to uplink veb */
10116 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10117 if (veb->uplink_seid) {
10118 vsi->uplink_seid = veb->uplink_seid;
10119 if (veb->uplink_seid == pf->mac_seid)
10120 vsi->veb_idx = I40E_NO_VEB;
10121 else
10122 vsi->veb_idx = veb->veb_idx;
10123 } else {
10124 /* floating VEB */
10125 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10126 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10127 }
10128
10129 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10130 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010131}
10132
10133/**
10134 * i40e_add_veb - create the VEB in the switch
10135 * @veb: the VEB to be instantiated
10136 * @vsi: the controlling VSI
10137 **/
10138static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10139{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010140 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010141 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010142 int ret;
10143
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010144 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010145 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010146 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010147
10148 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010149 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010150 dev_info(&pf->pdev->dev,
10151 "couldn't add VEB, 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));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010154 return -EPERM;
10155 }
10156
10157 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010158 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010159 &veb->stats_idx, NULL, NULL, NULL);
10160 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010161 dev_info(&pf->pdev->dev,
10162 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10163 i40e_stat_str(&pf->hw, ret),
10164 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010165 return -EPERM;
10166 }
10167 ret = i40e_veb_get_bw_info(veb);
10168 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010169 dev_info(&pf->pdev->dev,
10170 "couldn't get VEB bw info, err %s aq_err %s\n",
10171 i40e_stat_str(&pf->hw, ret),
10172 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10173 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010174 return -ENOENT;
10175 }
10176
10177 vsi->uplink_seid = veb->seid;
10178 vsi->veb_idx = veb->idx;
10179 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10180
10181 return 0;
10182}
10183
10184/**
10185 * i40e_veb_setup - Set up a VEB
10186 * @pf: board private structure
10187 * @flags: VEB setup flags
10188 * @uplink_seid: the switch element to link to
10189 * @vsi_seid: the initial VSI seid
10190 * @enabled_tc: Enabled TC bit-map
10191 *
10192 * This allocates the sw VEB structure and links it into the switch
10193 * It is possible and legal for this to be a duplicate of an already
10194 * existing VEB. It is also possible for both uplink and vsi seids
10195 * to be zero, in order to create a floating VEB.
10196 *
10197 * Returns pointer to the successfully allocated VEB sw struct on
10198 * success, otherwise returns NULL on failure.
10199 **/
10200struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10201 u16 uplink_seid, u16 vsi_seid,
10202 u8 enabled_tc)
10203{
10204 struct i40e_veb *veb, *uplink_veb = NULL;
10205 int vsi_idx, veb_idx;
10206 int ret;
10207
10208 /* if one seid is 0, the other must be 0 to create a floating relay */
10209 if ((uplink_seid == 0 || vsi_seid == 0) &&
10210 (uplink_seid + vsi_seid != 0)) {
10211 dev_info(&pf->pdev->dev,
10212 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10213 uplink_seid, vsi_seid);
10214 return NULL;
10215 }
10216
10217 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010218 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010219 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10220 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010221 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010222 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10223 vsi_seid);
10224 return NULL;
10225 }
10226
10227 if (uplink_seid && uplink_seid != pf->mac_seid) {
10228 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10229 if (pf->veb[veb_idx] &&
10230 pf->veb[veb_idx]->seid == uplink_seid) {
10231 uplink_veb = pf->veb[veb_idx];
10232 break;
10233 }
10234 }
10235 if (!uplink_veb) {
10236 dev_info(&pf->pdev->dev,
10237 "uplink seid %d not found\n", uplink_seid);
10238 return NULL;
10239 }
10240 }
10241
10242 /* get veb sw struct */
10243 veb_idx = i40e_veb_mem_alloc(pf);
10244 if (veb_idx < 0)
10245 goto err_alloc;
10246 veb = pf->veb[veb_idx];
10247 veb->flags = flags;
10248 veb->uplink_seid = uplink_seid;
10249 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10250 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10251
10252 /* create the VEB in the switch */
10253 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10254 if (ret)
10255 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010256 if (vsi_idx == pf->lan_vsi)
10257 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010258
10259 return veb;
10260
10261err_veb:
10262 i40e_veb_clear(veb);
10263err_alloc:
10264 return NULL;
10265}
10266
10267/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010268 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010269 * @pf: board private structure
10270 * @ele: element we are building info from
10271 * @num_reported: total number of elements
10272 * @printconfig: should we print the contents
10273 *
10274 * helper function to assist in extracting a few useful SEID values.
10275 **/
10276static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10277 struct i40e_aqc_switch_config_element_resp *ele,
10278 u16 num_reported, bool printconfig)
10279{
10280 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10281 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10282 u8 element_type = ele->element_type;
10283 u16 seid = le16_to_cpu(ele->seid);
10284
10285 if (printconfig)
10286 dev_info(&pf->pdev->dev,
10287 "type=%d seid=%d uplink=%d downlink=%d\n",
10288 element_type, seid, uplink_seid, downlink_seid);
10289
10290 switch (element_type) {
10291 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10292 pf->mac_seid = seid;
10293 break;
10294 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10295 /* Main VEB? */
10296 if (uplink_seid != pf->mac_seid)
10297 break;
10298 if (pf->lan_veb == I40E_NO_VEB) {
10299 int v;
10300
10301 /* find existing or else empty VEB */
10302 for (v = 0; v < I40E_MAX_VEB; v++) {
10303 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10304 pf->lan_veb = v;
10305 break;
10306 }
10307 }
10308 if (pf->lan_veb == I40E_NO_VEB) {
10309 v = i40e_veb_mem_alloc(pf);
10310 if (v < 0)
10311 break;
10312 pf->lan_veb = v;
10313 }
10314 }
10315
10316 pf->veb[pf->lan_veb]->seid = seid;
10317 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10318 pf->veb[pf->lan_veb]->pf = pf;
10319 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10320 break;
10321 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10322 if (num_reported != 1)
10323 break;
10324 /* This is immediately after a reset so we can assume this is
10325 * the PF's VSI
10326 */
10327 pf->mac_seid = uplink_seid;
10328 pf->pf_seid = downlink_seid;
10329 pf->main_vsi_seid = seid;
10330 if (printconfig)
10331 dev_info(&pf->pdev->dev,
10332 "pf_seid=%d main_vsi_seid=%d\n",
10333 pf->pf_seid, pf->main_vsi_seid);
10334 break;
10335 case I40E_SWITCH_ELEMENT_TYPE_PF:
10336 case I40E_SWITCH_ELEMENT_TYPE_VF:
10337 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10338 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10339 case I40E_SWITCH_ELEMENT_TYPE_PE:
10340 case I40E_SWITCH_ELEMENT_TYPE_PA:
10341 /* ignore these for now */
10342 break;
10343 default:
10344 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10345 element_type, seid);
10346 break;
10347 }
10348}
10349
10350/**
10351 * i40e_fetch_switch_configuration - Get switch config from firmware
10352 * @pf: board private structure
10353 * @printconfig: should we print the contents
10354 *
10355 * Get the current switch configuration from the device and
10356 * extract a few useful SEID values.
10357 **/
10358int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10359{
10360 struct i40e_aqc_get_switch_config_resp *sw_config;
10361 u16 next_seid = 0;
10362 int ret = 0;
10363 u8 *aq_buf;
10364 int i;
10365
10366 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10367 if (!aq_buf)
10368 return -ENOMEM;
10369
10370 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10371 do {
10372 u16 num_reported, num_total;
10373
10374 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10375 I40E_AQ_LARGE_BUF,
10376 &next_seid, NULL);
10377 if (ret) {
10378 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010379 "get switch config failed err %s aq_err %s\n",
10380 i40e_stat_str(&pf->hw, ret),
10381 i40e_aq_str(&pf->hw,
10382 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010383 kfree(aq_buf);
10384 return -ENOENT;
10385 }
10386
10387 num_reported = le16_to_cpu(sw_config->header.num_reported);
10388 num_total = le16_to_cpu(sw_config->header.num_total);
10389
10390 if (printconfig)
10391 dev_info(&pf->pdev->dev,
10392 "header: %d reported %d total\n",
10393 num_reported, num_total);
10394
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010395 for (i = 0; i < num_reported; i++) {
10396 struct i40e_aqc_switch_config_element_resp *ele =
10397 &sw_config->element[i];
10398
10399 i40e_setup_pf_switch_element(pf, ele, num_reported,
10400 printconfig);
10401 }
10402 } while (next_seid != 0);
10403
10404 kfree(aq_buf);
10405 return ret;
10406}
10407
10408/**
10409 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10410 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010411 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010412 *
10413 * Returns 0 on success, negative value on failure
10414 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010415static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010416{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010417 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010418 int ret;
10419
10420 /* find out what's out there already */
10421 ret = i40e_fetch_switch_configuration(pf, false);
10422 if (ret) {
10423 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010424 "couldn't fetch switch config, err %s aq_err %s\n",
10425 i40e_stat_str(&pf->hw, ret),
10426 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010427 return ret;
10428 }
10429 i40e_pf_reset_stats(pf);
10430
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010431 /* set the switch config bit for the whole device to
10432 * support limited promisc or true promisc
10433 * when user requests promisc. The default is limited
10434 * promisc.
10435 */
10436
10437 if ((pf->hw.pf_id == 0) &&
10438 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10439 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10440
10441 if (pf->hw.pf_id == 0) {
10442 u16 valid_flags;
10443
10444 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10445 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10446 NULL);
10447 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10448 dev_info(&pf->pdev->dev,
10449 "couldn't set switch config bits, err %s aq_err %s\n",
10450 i40e_stat_str(&pf->hw, ret),
10451 i40e_aq_str(&pf->hw,
10452 pf->hw.aq.asq_last_status));
10453 /* not a fatal problem, just keep going */
10454 }
10455 }
10456
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010457 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010458 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010459 struct i40e_vsi *vsi = NULL;
10460 u16 uplink_seid;
10461
10462 /* Set up the PF VSI associated with the PF's main VSI
10463 * that is already in the HW switch
10464 */
10465 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10466 uplink_seid = pf->veb[pf->lan_veb]->seid;
10467 else
10468 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010469 if (pf->lan_vsi == I40E_NO_VSI)
10470 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10471 else if (reinit)
10472 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010473 if (!vsi) {
10474 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10475 i40e_fdir_teardown(pf);
10476 return -EAGAIN;
10477 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010478 } else {
10479 /* force a reset of TC and queue layout configurations */
10480 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010481
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010482 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10483 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10484 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10485 }
10486 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10487
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010488 i40e_fdir_sb_setup(pf);
10489
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010490 /* Setup static PF queue filter control settings */
10491 ret = i40e_setup_pf_filter_control(pf);
10492 if (ret) {
10493 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10494 ret);
10495 /* Failure here should not stop continuing other steps */
10496 }
10497
10498 /* enable RSS in the HW, even for only one queue, as the stack can use
10499 * the hash
10500 */
10501 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010502 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010503
10504 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010505 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010506 i40e_link_event(pf);
10507
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010508 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010509 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10510 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010511
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010512 i40e_ptp_init(pf);
10513
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010514 return ret;
10515}
10516
10517/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010518 * i40e_determine_queue_usage - Work out queue distribution
10519 * @pf: board private structure
10520 **/
10521static void i40e_determine_queue_usage(struct i40e_pf *pf)
10522{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010523 int queues_left;
10524
10525 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010526#ifdef I40E_FCOE
10527 pf->num_fcoe_qps = 0;
10528#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010529
10530 /* Find the max queues to be put into basic use. We'll always be
10531 * using TC0, whether or not DCB is running, and TC0 will get the
10532 * big RSS set.
10533 */
10534 queues_left = pf->hw.func_caps.num_tx_qp;
10535
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010536 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010537 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010538 /* one qp for PF, no queues for anything else */
10539 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010540 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010541
10542 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010543 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
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010548 I40E_FLAG_FD_SB_ENABLED |
10549 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010550 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010551 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010552 I40E_FLAG_SRIOV_ENABLED |
10553 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010554 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10555 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010556 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010557 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010558 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010559 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010560 queues_left -= pf->num_lan_qps;
10561
10562 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010563 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010564#ifdef I40E_FCOE
10565 I40E_FLAG_FCOE_ENABLED |
10566#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010567 I40E_FLAG_FD_SB_ENABLED |
10568 I40E_FLAG_FD_ATR_ENABLED |
10569 I40E_FLAG_DCB_ENABLED |
10570 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010571 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010572 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010573 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010574 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010575 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10576 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010577 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10578 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010579 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10580 num_online_cpus());
10581 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10582 pf->hw.func_caps.num_tx_qp);
10583
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010584 queues_left -= pf->num_lan_qps;
10585 }
10586
Vasu Dev38e00432014-08-01 13:27:03 -070010587#ifdef I40E_FCOE
10588 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10589 if (I40E_DEFAULT_FCOE <= queues_left) {
10590 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10591 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10592 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10593 } else {
10594 pf->num_fcoe_qps = 0;
10595 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10596 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10597 }
10598
10599 queues_left -= pf->num_fcoe_qps;
10600 }
10601
10602#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010603 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10604 if (queues_left > 1) {
10605 queues_left -= 1; /* save 1 queue for FD */
10606 } else {
10607 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10608 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10609 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010610 }
10611
10612 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10613 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010614 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10615 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010616 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10617 }
10618
10619 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10620 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10621 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10622 (queues_left / pf->num_vmdq_qps));
10623 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10624 }
10625
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010626 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010627 dev_dbg(&pf->pdev->dev,
10628 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10629 pf->hw.func_caps.num_tx_qp,
10630 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010631 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10632 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10633 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010634#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010635 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010636#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010637}
10638
10639/**
10640 * i40e_setup_pf_filter_control - Setup PF static filter control
10641 * @pf: PF to be setup
10642 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010643 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010644 * settings. If PE/FCoE are enabled then it will also set the per PF
10645 * based filter sizes required for them. It also enables Flow director,
10646 * ethertype and macvlan type filter settings for the pf.
10647 *
10648 * Returns 0 on success, negative on failure
10649 **/
10650static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10651{
10652 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10653
10654 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10655
10656 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010657 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010658 settings->enable_fdir = true;
10659
10660 /* Ethtype and MACVLAN filters enabled for PF */
10661 settings->enable_ethtype = true;
10662 settings->enable_macvlan = true;
10663
10664 if (i40e_set_filter_control(&pf->hw, settings))
10665 return -ENOENT;
10666
10667 return 0;
10668}
10669
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010670#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010671#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010672static void i40e_print_features(struct i40e_pf *pf)
10673{
10674 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010675 char *buf;
10676 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010677
Joe Perches3b195842015-12-03 04:20:57 -080010678 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10679 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010680 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010681
Joe Perches3b195842015-12-03 04:20:57 -080010682 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010683#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010684 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010685#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010686 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010687 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010688 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010689 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010690 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010691 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010692 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010693 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010694 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10695 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010696 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010697 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010698 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010699 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010700 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010701 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010702 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010703#ifdef I40E_FCOE
10704 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010705 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010706#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010707 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010708 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010709 else
Joe Perches3b195842015-12-03 04:20:57 -080010710 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010711
Joe Perches3b195842015-12-03 04:20:57 -080010712 dev_info(&pf->pdev->dev, "%s\n", buf);
10713 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010714 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010715}
10716
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010717/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010718 * i40e_get_platform_mac_addr - get platform-specific MAC address
10719 *
10720 * @pdev: PCI device information struct
10721 * @pf: board private structure
10722 *
10723 * Look up the MAC address in Open Firmware on systems that support it,
10724 * and use IDPROM on SPARC if no OF address is found. On return, the
10725 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10726 * has been selected.
10727 **/
10728static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10729{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010730 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010731 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010732 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010733}
10734
10735/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010736 * i40e_probe - Device initialization routine
10737 * @pdev: PCI device information struct
10738 * @ent: entry in i40e_pci_tbl
10739 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010740 * i40e_probe initializes a PF identified by a pci_dev structure.
10741 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010742 * and a hardware reset occur.
10743 *
10744 * Returns 0 on success, negative on failure
10745 **/
10746static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10747{
Catherine Sullivane8278452015-02-06 08:52:08 +000010748 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010749 struct i40e_pf *pf;
10750 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010751 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010752 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010753 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010754 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010755 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010756 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010757 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010758
10759 err = pci_enable_device_mem(pdev);
10760 if (err)
10761 return err;
10762
10763 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010764 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010765 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010766 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10767 if (err) {
10768 dev_err(&pdev->dev,
10769 "DMA configuration failed: 0x%x\n", err);
10770 goto err_dma;
10771 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010772 }
10773
10774 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010775 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010776 if (err) {
10777 dev_info(&pdev->dev,
10778 "pci_request_selected_regions failed %d\n", err);
10779 goto err_pci_reg;
10780 }
10781
10782 pci_enable_pcie_error_reporting(pdev);
10783 pci_set_master(pdev);
10784
10785 /* Now that we have a PCI connection, we need to do the
10786 * low level device setup. This is primarily setting up
10787 * the Admin Queue structures and then querying for the
10788 * device's current profile information.
10789 */
10790 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10791 if (!pf) {
10792 err = -ENOMEM;
10793 goto err_pf_alloc;
10794 }
10795 pf->next_vsi = 0;
10796 pf->pdev = pdev;
10797 set_bit(__I40E_DOWN, &pf->state);
10798
10799 hw = &pf->hw;
10800 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010801
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010802 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10803 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010804
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010805 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010806 if (!hw->hw_addr) {
10807 err = -EIO;
10808 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10809 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010810 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010811 goto err_ioremap;
10812 }
10813 hw->vendor_id = pdev->vendor;
10814 hw->device_id = pdev->device;
10815 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10816 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10817 hw->subsystem_device_id = pdev->subsystem_device;
10818 hw->bus.device = PCI_SLOT(pdev->devfn);
10819 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010820 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010821
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010822 /* set up the locks for the AQ, do this only once in probe
10823 * and destroy them only once in remove
10824 */
10825 mutex_init(&hw->aq.asq_mutex);
10826 mutex_init(&hw->aq.arq_mutex);
10827
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010828 if (debug != -1) {
10829 pf->msg_enable = pf->hw.debug_mask;
10830 pf->msg_enable = debug;
10831 }
10832
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010833 /* do a special CORER for clearing PXE mode once at init */
10834 if (hw->revision_id == 0 &&
10835 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10836 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10837 i40e_flush(hw);
10838 msleep(200);
10839 pf->corer_count++;
10840
10841 i40e_clear_pxe_mode(hw);
10842 }
10843
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010844 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010845 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010846 err = i40e_pf_reset(hw);
10847 if (err) {
10848 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10849 goto err_pf_reset;
10850 }
10851 pf->pfr_count++;
10852
10853 hw->aq.num_arq_entries = I40E_AQ_LEN;
10854 hw->aq.num_asq_entries = I40E_AQ_LEN;
10855 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10856 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10857 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010858
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010859 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010860 "%s-%s:misc",
10861 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010862
10863 err = i40e_init_shared_code(hw);
10864 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010865 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10866 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010867 goto err_pf_reset;
10868 }
10869
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010870 /* set up a default setting for link flow control */
10871 pf->hw.fc.requested_mode = I40E_FC_NONE;
10872
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010873 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010874 if (err) {
10875 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10876 dev_info(&pdev->dev,
10877 "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");
10878 else
10879 dev_info(&pdev->dev,
10880 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10881
10882 goto err_pf_reset;
10883 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010884
Shannon Nelson6dec1012015-09-28 14:12:30 -040010885 /* provide nvm, fw, api versions */
10886 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10887 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10888 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10889 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010890
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010891 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10892 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010893 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010894 "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");
10895 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10896 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010897 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010898 "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 +000010899
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010900 i40e_verify_eeprom(pf);
10901
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010902 /* Rev 0 hardware was never productized */
10903 if (hw->revision_id < 1)
10904 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");
10905
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010906 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010907 err = i40e_get_capabilities(pf);
10908 if (err)
10909 goto err_adminq_setup;
10910
10911 err = i40e_sw_init(pf);
10912 if (err) {
10913 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10914 goto err_sw_init;
10915 }
10916
10917 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10918 hw->func_caps.num_rx_qp,
10919 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10920 if (err) {
10921 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10922 goto err_init_lan_hmc;
10923 }
10924
10925 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10926 if (err) {
10927 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10928 err = -ENOENT;
10929 goto err_configure_lan_hmc;
10930 }
10931
Neerav Parikhb686ece2014-12-14 01:55:11 +000010932 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10933 * Ignore error return codes because if it was already disabled via
10934 * hardware settings this will fail
10935 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010936 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010937 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10938 i40e_aq_stop_lldp(hw, true, NULL);
10939 }
10940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010941 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010942 /* allow a platform config to override the HW addr */
10943 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010944 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010945 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10946 err = -EIO;
10947 goto err_mac_addr;
10948 }
10949 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010950 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010951 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10952 if (is_valid_ether_addr(hw->mac.port_addr))
10953 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010954#ifdef I40E_FCOE
10955 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10956 if (err)
10957 dev_info(&pdev->dev,
10958 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10959 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10960 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10961 hw->mac.san_addr);
10962 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10963 }
10964 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10965#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010966
10967 pci_set_drvdata(pdev, pf);
10968 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010969#ifdef CONFIG_I40E_DCB
10970 err = i40e_init_pf_dcb(pf);
10971 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010972 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070010973 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000010974 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010975 }
10976#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010977
10978 /* set up periodic task facility */
10979 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10980 pf->service_timer_period = HZ;
10981
10982 INIT_WORK(&pf->service_task, i40e_service_task);
10983 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10984 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010985
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010986 /* NVM bit on means WoL disabled for the port */
10987 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010988 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010989 pf->wol_en = false;
10990 else
10991 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010992 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10993
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010994 /* set up the main switch operations */
10995 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010996 err = i40e_init_interrupt_scheme(pf);
10997 if (err)
10998 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010999
Mitch Williams505682c2014-05-20 08:01:37 +000011000 /* The number of VSIs reported by the FW is the minimum guaranteed
11001 * to us; HW supports far more and we share the remaining pool with
11002 * the other PFs. We allocate space for more than the guarantee with
11003 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011004 */
Mitch Williams505682c2014-05-20 08:01:37 +000011005 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11006 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11007 else
11008 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11009
11010 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011011 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11012 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011013 if (!pf->vsi) {
11014 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011015 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011016 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011017
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011018#ifdef CONFIG_PCI_IOV
11019 /* prep for VF support */
11020 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11021 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11022 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11023 if (pci_num_vf(pdev))
11024 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11025 }
11026#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011027 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011028 if (err) {
11029 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11030 goto err_vsis;
11031 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011032
11033 /* Make sure flow control is set according to current settings */
11034 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11035 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11036 dev_dbg(&pf->pdev->dev,
11037 "Set fc with err %s aq_err %s on get_phy_cap\n",
11038 i40e_stat_str(hw, err),
11039 i40e_aq_str(hw, hw->aq.asq_last_status));
11040 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11041 dev_dbg(&pf->pdev->dev,
11042 "Set fc with err %s aq_err %s on set_phy_config\n",
11043 i40e_stat_str(hw, err),
11044 i40e_aq_str(hw, hw->aq.asq_last_status));
11045 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11046 dev_dbg(&pf->pdev->dev,
11047 "Set fc with err %s aq_err %s on get_link_info\n",
11048 i40e_stat_str(hw, err),
11049 i40e_aq_str(hw, hw->aq.asq_last_status));
11050
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011051 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011052 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011053 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11054 i40e_vsi_open(pf->vsi[i]);
11055 break;
11056 }
11057 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011058
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011059 /* The driver only wants link up/down and module qualification
11060 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011061 */
11062 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011063 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011064 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011065 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011066 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011067 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11068 i40e_stat_str(&pf->hw, err),
11069 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011070
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011071 /* Reconfigure hardware for allowing smaller MSS in the case
11072 * of TSO, so that we avoid the MDD being fired and causing
11073 * a reset in the case of small MSS+TSO.
11074 */
11075 val = rd32(hw, I40E_REG_MSS);
11076 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11077 val &= ~I40E_REG_MSS_MIN_MASK;
11078 val |= I40E_64BYTE_MSS;
11079 wr32(hw, I40E_REG_MSS, val);
11080 }
11081
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011082 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011083 msleep(75);
11084 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11085 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011086 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11087 i40e_stat_str(&pf->hw, err),
11088 i40e_aq_str(&pf->hw,
11089 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011090 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011091 /* The main driver is (mostly) up and happy. We need to set this state
11092 * before setting up the misc vector or we get a race and the vector
11093 * ends up disabled forever.
11094 */
11095 clear_bit(__I40E_DOWN, &pf->state);
11096
11097 /* In case of MSIX we are going to setup the misc vector right here
11098 * to handle admin queue events etc. In case of legacy and MSI
11099 * the misc functionality and queue processing is combined in
11100 * the same vector and that gets setup at open.
11101 */
11102 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11103 err = i40e_setup_misc_vector(pf);
11104 if (err) {
11105 dev_info(&pdev->dev,
11106 "setup of misc vector failed: %d\n", err);
11107 goto err_vsis;
11108 }
11109 }
11110
Greg Rosedf805f62014-04-04 04:43:16 +000011111#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011112 /* prep for VF support */
11113 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011114 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11115 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011116 /* disable link interrupts for VFs */
11117 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11118 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11119 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11120 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011121
11122 if (pci_num_vf(pdev)) {
11123 dev_info(&pdev->dev,
11124 "Active VFs found, allocating resources.\n");
11125 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11126 if (err)
11127 dev_info(&pdev->dev,
11128 "Error %d allocating resources for existing VFs\n",
11129 err);
11130 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011131 }
Greg Rosedf805f62014-04-04 04:43:16 +000011132#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011133
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011134 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11135 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11136 pf->num_iwarp_msix,
11137 I40E_IWARP_IRQ_PILE_ID);
11138 if (pf->iwarp_base_vector < 0) {
11139 dev_info(&pdev->dev,
11140 "failed to get tracking for %d vectors for IWARP err=%d\n",
11141 pf->num_iwarp_msix, pf->iwarp_base_vector);
11142 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11143 }
11144 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011145
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011146 i40e_dbg_pf_init(pf);
11147
11148 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011149 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011150
11151 /* since everything's happy, start the service_task timer */
11152 mod_timer(&pf->service_timer,
11153 round_jiffies(jiffies + pf->service_timer_period));
11154
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011155 /* add this PF to client device list and launch a client service task */
11156 err = i40e_lan_add_device(pf);
11157 if (err)
11158 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11159 err);
11160
Vasu Dev38e00432014-08-01 13:27:03 -070011161#ifdef I40E_FCOE
11162 /* create FCoE interface */
11163 i40e_fcoe_vsi_setup(pf);
11164
11165#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011166#define PCI_SPEED_SIZE 8
11167#define PCI_WIDTH_SIZE 8
11168 /* Devices on the IOSF bus do not have this information
11169 * and will report PCI Gen 1 x 1 by default so don't bother
11170 * checking them.
11171 */
11172 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11173 char speed[PCI_SPEED_SIZE] = "Unknown";
11174 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011175
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011176 /* Get the negotiated link width and speed from PCI config
11177 * space
11178 */
11179 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11180 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011181
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011182 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011183
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011184 switch (hw->bus.speed) {
11185 case i40e_bus_speed_8000:
11186 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11187 case i40e_bus_speed_5000:
11188 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11189 case i40e_bus_speed_2500:
11190 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11191 default:
11192 break;
11193 }
11194 switch (hw->bus.width) {
11195 case i40e_bus_width_pcie_x8:
11196 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11197 case i40e_bus_width_pcie_x4:
11198 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11199 case i40e_bus_width_pcie_x2:
11200 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11201 case i40e_bus_width_pcie_x1:
11202 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11203 default:
11204 break;
11205 }
11206
11207 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11208 speed, width);
11209
11210 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11211 hw->bus.speed < i40e_bus_speed_8000) {
11212 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11213 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11214 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011215 }
11216
Catherine Sullivane8278452015-02-06 08:52:08 +000011217 /* get the requested speeds from the fw */
11218 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11219 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011220 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11221 i40e_stat_str(&pf->hw, err),
11222 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011223 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11224
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011225 /* get the supported phy types from the fw */
11226 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11227 if (err)
11228 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11229 i40e_stat_str(&pf->hw, err),
11230 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11231 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11232
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011233 /* Add a filter to drop all Flow control frames from any VSI from being
11234 * transmitted. By doing so we stop a malicious VF from sending out
11235 * PAUSE or PFC frames and potentially controlling traffic for other
11236 * PF/VF VSIs.
11237 * The FW can still send Flow control frames if enabled.
11238 */
11239 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11240 pf->main_vsi_seid);
11241
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011242 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11243 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11244 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11245
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011246 /* print a string summarizing features */
11247 i40e_print_features(pf);
11248
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011249 return 0;
11250
11251 /* Unwind what we've done if something failed in the setup */
11252err_vsis:
11253 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011254 i40e_clear_interrupt_scheme(pf);
11255 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011256err_switch_setup:
11257 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011258 del_timer_sync(&pf->service_timer);
11259err_mac_addr:
11260err_configure_lan_hmc:
11261 (void)i40e_shutdown_lan_hmc(hw);
11262err_init_lan_hmc:
11263 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011264err_sw_init:
11265err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011266err_pf_reset:
11267 iounmap(hw->hw_addr);
11268err_ioremap:
11269 kfree(pf);
11270err_pf_alloc:
11271 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011272 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011273err_pci_reg:
11274err_dma:
11275 pci_disable_device(pdev);
11276 return err;
11277}
11278
11279/**
11280 * i40e_remove - Device removal routine
11281 * @pdev: PCI device information struct
11282 *
11283 * i40e_remove is called by the PCI subsystem to alert the driver
11284 * that is should release a PCI device. This could be caused by a
11285 * Hot-Plug event, or because the driver is going to be removed from
11286 * memory.
11287 **/
11288static void i40e_remove(struct pci_dev *pdev)
11289{
11290 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011291 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011292 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011293 int i;
11294
11295 i40e_dbg_pf_exit(pf);
11296
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011297 i40e_ptp_stop(pf);
11298
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011299 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011300 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11301 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011302
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011303 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011304 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011305 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011306 if (pf->service_timer.data)
11307 del_timer_sync(&pf->service_timer);
11308 if (pf->service_task.func)
11309 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011310
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011311 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11312 i40e_free_vfs(pf);
11313 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11314 }
11315
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011316 i40e_fdir_teardown(pf);
11317
11318 /* If there is a switch structure or any orphans, remove them.
11319 * This will leave only the PF's VSI remaining.
11320 */
11321 for (i = 0; i < I40E_MAX_VEB; i++) {
11322 if (!pf->veb[i])
11323 continue;
11324
11325 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11326 pf->veb[i]->uplink_seid == 0)
11327 i40e_switch_branch_release(pf->veb[i]);
11328 }
11329
11330 /* Now we can shutdown the PF's VSI, just before we kill
11331 * adminq and hmc.
11332 */
11333 if (pf->vsi[pf->lan_vsi])
11334 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11335
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011336 /* remove attached clients */
11337 ret_code = i40e_lan_del_device(pf);
11338 if (ret_code) {
11339 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11340 ret_code);
11341 }
11342
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011343 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011344 if (hw->hmc.hmc_obj) {
11345 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011346 if (ret_code)
11347 dev_warn(&pdev->dev,
11348 "Failed to destroy the HMC resources: %d\n",
11349 ret_code);
11350 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011351
11352 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011353 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011354
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011355 /* destroy the locks only once, here */
11356 mutex_destroy(&hw->aq.arq_mutex);
11357 mutex_destroy(&hw->aq.asq_mutex);
11358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011359 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11360 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011361 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011362 if (pf->vsi[i]) {
11363 i40e_vsi_clear_rings(pf->vsi[i]);
11364 i40e_vsi_clear(pf->vsi[i]);
11365 pf->vsi[i] = NULL;
11366 }
11367 }
11368
11369 for (i = 0; i < I40E_MAX_VEB; i++) {
11370 kfree(pf->veb[i]);
11371 pf->veb[i] = NULL;
11372 }
11373
11374 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011375 kfree(pf->vsi);
11376
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011377 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011378 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011379 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011380
11381 pci_disable_pcie_error_reporting(pdev);
11382 pci_disable_device(pdev);
11383}
11384
11385/**
11386 * i40e_pci_error_detected - warning that something funky happened in PCI land
11387 * @pdev: PCI device information struct
11388 *
11389 * Called to warn that something happened and the error handling steps
11390 * are in progress. Allows the driver to quiesce things, be ready for
11391 * remediation.
11392 **/
11393static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11394 enum pci_channel_state error)
11395{
11396 struct i40e_pf *pf = pci_get_drvdata(pdev);
11397
11398 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11399
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011400 if (!pf) {
11401 dev_info(&pdev->dev,
11402 "Cannot recover - error happened during device probe\n");
11403 return PCI_ERS_RESULT_DISCONNECT;
11404 }
11405
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011406 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011407 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11408 rtnl_lock();
11409 i40e_prep_for_reset(pf);
11410 rtnl_unlock();
11411 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011412
11413 /* Request a slot reset */
11414 return PCI_ERS_RESULT_NEED_RESET;
11415}
11416
11417/**
11418 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11419 * @pdev: PCI device information struct
11420 *
11421 * Called to find if the driver can work with the device now that
11422 * the pci slot has been reset. If a basic connection seems good
11423 * (registers are readable and have sane content) then return a
11424 * happy little PCI_ERS_RESULT_xxx.
11425 **/
11426static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11427{
11428 struct i40e_pf *pf = pci_get_drvdata(pdev);
11429 pci_ers_result_t result;
11430 int err;
11431 u32 reg;
11432
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011433 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011434 if (pci_enable_device_mem(pdev)) {
11435 dev_info(&pdev->dev,
11436 "Cannot re-enable PCI device after reset.\n");
11437 result = PCI_ERS_RESULT_DISCONNECT;
11438 } else {
11439 pci_set_master(pdev);
11440 pci_restore_state(pdev);
11441 pci_save_state(pdev);
11442 pci_wake_from_d3(pdev, false);
11443
11444 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11445 if (reg == 0)
11446 result = PCI_ERS_RESULT_RECOVERED;
11447 else
11448 result = PCI_ERS_RESULT_DISCONNECT;
11449 }
11450
11451 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11452 if (err) {
11453 dev_info(&pdev->dev,
11454 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11455 err);
11456 /* non-fatal, continue */
11457 }
11458
11459 return result;
11460}
11461
11462/**
11463 * i40e_pci_error_resume - restart operations after PCI error recovery
11464 * @pdev: PCI device information struct
11465 *
11466 * Called to allow the driver to bring things back up after PCI error
11467 * and/or reset recovery has finished.
11468 **/
11469static void i40e_pci_error_resume(struct pci_dev *pdev)
11470{
11471 struct i40e_pf *pf = pci_get_drvdata(pdev);
11472
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011473 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011474 if (test_bit(__I40E_SUSPENDED, &pf->state))
11475 return;
11476
11477 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011478 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011479 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011480}
11481
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011482/**
11483 * i40e_shutdown - PCI callback for shutting down
11484 * @pdev: PCI device information struct
11485 **/
11486static void i40e_shutdown(struct pci_dev *pdev)
11487{
11488 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011489 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011490
11491 set_bit(__I40E_SUSPENDED, &pf->state);
11492 set_bit(__I40E_DOWN, &pf->state);
11493 rtnl_lock();
11494 i40e_prep_for_reset(pf);
11495 rtnl_unlock();
11496
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011497 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11498 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11499
Catherine Sullivan02b42492015-07-10 19:35:59 -040011500 del_timer_sync(&pf->service_timer);
11501 cancel_work_sync(&pf->service_task);
11502 i40e_fdir_teardown(pf);
11503
11504 rtnl_lock();
11505 i40e_prep_for_reset(pf);
11506 rtnl_unlock();
11507
11508 wr32(hw, I40E_PFPM_APM,
11509 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11510 wr32(hw, I40E_PFPM_WUFC,
11511 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11512
Shannon Nelsone1477582015-02-21 06:44:33 +000011513 i40e_clear_interrupt_scheme(pf);
11514
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011515 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011516 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011517 pci_set_power_state(pdev, PCI_D3hot);
11518 }
11519}
11520
11521#ifdef CONFIG_PM
11522/**
11523 * i40e_suspend - PCI callback for moving to D3
11524 * @pdev: PCI device information struct
11525 **/
11526static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11527{
11528 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011529 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011530 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011531
11532 set_bit(__I40E_SUSPENDED, &pf->state);
11533 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011534
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011535 rtnl_lock();
11536 i40e_prep_for_reset(pf);
11537 rtnl_unlock();
11538
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011539 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11540 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11541
Greg Roseb33d3b72016-05-16 10:26:37 -070011542 i40e_stop_misc_vector(pf);
11543
Greg Rose059ff692016-05-16 10:26:38 -070011544 retval = pci_save_state(pdev);
11545 if (retval)
11546 return retval;
11547
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011548 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011549 pci_set_power_state(pdev, PCI_D3hot);
11550
Greg Rose059ff692016-05-16 10:26:38 -070011551 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011552}
11553
11554/**
11555 * i40e_resume - PCI callback for waking up from D3
11556 * @pdev: PCI device information struct
11557 **/
11558static int i40e_resume(struct pci_dev *pdev)
11559{
11560 struct i40e_pf *pf = pci_get_drvdata(pdev);
11561 u32 err;
11562
11563 pci_set_power_state(pdev, PCI_D0);
11564 pci_restore_state(pdev);
11565 /* pci_restore_state() clears dev->state_saves, so
11566 * call pci_save_state() again to restore it.
11567 */
11568 pci_save_state(pdev);
11569
11570 err = pci_enable_device_mem(pdev);
11571 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011572 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011573 return err;
11574 }
11575 pci_set_master(pdev);
11576
11577 /* no wakeup events while running */
11578 pci_wake_from_d3(pdev, false);
11579
11580 /* handling the reset will rebuild the device state */
11581 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11582 clear_bit(__I40E_DOWN, &pf->state);
11583 rtnl_lock();
11584 i40e_reset_and_rebuild(pf, false);
11585 rtnl_unlock();
11586 }
11587
11588 return 0;
11589}
11590
11591#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011592static const struct pci_error_handlers i40e_err_handler = {
11593 .error_detected = i40e_pci_error_detected,
11594 .slot_reset = i40e_pci_error_slot_reset,
11595 .resume = i40e_pci_error_resume,
11596};
11597
11598static struct pci_driver i40e_driver = {
11599 .name = i40e_driver_name,
11600 .id_table = i40e_pci_tbl,
11601 .probe = i40e_probe,
11602 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011603#ifdef CONFIG_PM
11604 .suspend = i40e_suspend,
11605 .resume = i40e_resume,
11606#endif
11607 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011608 .err_handler = &i40e_err_handler,
11609 .sriov_configure = i40e_pci_sriov_configure,
11610};
11611
11612/**
11613 * i40e_init_module - Driver registration routine
11614 *
11615 * i40e_init_module is the first routine called when the driver is
11616 * loaded. All it does is register with the PCI subsystem.
11617 **/
11618static int __init i40e_init_module(void)
11619{
11620 pr_info("%s: %s - version %s\n", i40e_driver_name,
11621 i40e_driver_string, i40e_driver_version_str);
11622 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011623
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011624 /* we will see if single thread per module is enough for now,
11625 * it can't be any worse than using the system workqueue which
11626 * was already single threaded
11627 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011628 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11629 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011630 if (!i40e_wq) {
11631 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11632 return -ENOMEM;
11633 }
11634
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011635 i40e_dbg_init();
11636 return pci_register_driver(&i40e_driver);
11637}
11638module_init(i40e_init_module);
11639
11640/**
11641 * i40e_exit_module - Driver exit cleanup routine
11642 *
11643 * i40e_exit_module is called just before the driver is removed
11644 * from memory.
11645 **/
11646static void __exit i40e_exit_module(void)
11647{
11648 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011649 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011650 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011651}
11652module_exit(i40e_exit_module);