blob: 35f06ba6bc4e3ee2f11447233b213e2c2c7a73fe [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;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040096module_param(debug, uint, 0);
97MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000098
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
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001148 *
1149 * Returns ptr to the filter object or NULL
1150 **/
1151static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001152 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001153{
1154 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001155 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001156
1157 if (!vsi || !macaddr)
1158 return NULL;
1159
Jacob Keller278e7d02016-10-05 09:30:37 -07001160 key = i40e_addr_to_hkey(macaddr);
1161 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001162 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001163 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001164 return f;
1165 }
1166 return NULL;
1167}
1168
1169/**
1170 * i40e_find_mac - Find a mac addr in the macvlan filters list
1171 * @vsi: the VSI to be searched
1172 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001173 *
1174 * Returns the first filter with the provided MAC address or NULL if
1175 * MAC address was not found
1176 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001177struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001178{
1179 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001180 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001181
1182 if (!vsi || !macaddr)
1183 return NULL;
1184
Jacob Keller278e7d02016-10-05 09:30:37 -07001185 key = i40e_addr_to_hkey(macaddr);
1186 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001187 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001188 return f;
1189 }
1190 return NULL;
1191}
1192
1193/**
1194 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1195 * @vsi: the VSI to be searched
1196 *
1197 * Returns true if VSI is in vlan mode or false otherwise
1198 **/
1199bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1200{
1201 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001202 struct hlist_node *h;
1203 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001204
1205 /* Only -1 for all the filters denotes not in vlan mode
1206 * so we have to go through all the list in order to make sure
1207 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001208 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Greg Rosed9b68f82015-07-23 16:54:31 -04001209 if (f->vlan >= 0 || vsi->info.pvid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001210 return true;
1211 }
1212
1213 return false;
1214}
1215
1216/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001217 * i40e_add_filter - Add a mac/vlan filter to the VSI
1218 * @vsi: the VSI to be searched
1219 * @macaddr: the MAC address
1220 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001221 *
1222 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001223 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001224 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001225 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001226 **/
1227struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001228 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001229{
1230 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001231 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001232
1233 if (!vsi || !macaddr)
1234 return NULL;
1235
Kiran Patilf6bd0962016-06-20 09:10:34 -07001236 /* Do not allow broadcast filter to be added since broadcast filter
1237 * is added as part of add VSI for any newly created VSI except
1238 * FDIR VSI
1239 */
1240 if (is_broadcast_ether_addr(macaddr))
1241 return NULL;
1242
Jacob Keller1bc87e82016-10-05 09:30:31 -07001243 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244 if (!f) {
1245 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1246 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001247 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001248
Greg Rose9a173902014-05-22 06:32:02 +00001249 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001250 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001251 /* If we're in overflow promisc mode, set the state directly
1252 * to failed, so we don't bother to try sending the filter
1253 * to the hardware.
1254 */
1255 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1256 f->state = I40E_FILTER_FAILED;
1257 else
1258 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001259 INIT_HLIST_NODE(&f->hlist);
1260
1261 key = i40e_addr_to_hkey(macaddr);
1262 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001263
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001264 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1265 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1266 }
1267
Jacob Keller1bc87e82016-10-05 09:30:31 -07001268 /* If we're asked to add a filter that has been marked for removal, it
1269 * is safe to simply restore it to active state. __i40e_del_filter
1270 * will have simply deleted any filters which were previously marked
1271 * NEW or FAILED, so if it is currently marked REMOVE it must have
1272 * previously been ACTIVE. Since we haven't yet run the sync filters
1273 * task, just restore this filter to the ACTIVE state so that the
1274 * sync task leaves it in place
1275 */
1276 if (f->state == I40E_FILTER_REMOVE)
1277 f->state = I40E_FILTER_ACTIVE;
1278
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001279 return f;
1280}
1281
1282/**
Jacob Keller290d2552016-10-05 09:30:36 -07001283 * __i40e_del_filter - Remove a specific filter from the VSI
1284 * @vsi: VSI to remove from
1285 * @f: the filter to remove from the list
1286 *
1287 * This function should be called instead of i40e_del_filter only if you know
1288 * the exact filter you will remove already, such as via i40e_find_filter or
1289 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001290 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001291 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001292 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001293 * ANOTHER NOTE: This function MUST be called from within the context of
1294 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1295 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001296 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001297static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001298{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001299 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001300 return;
1301
Jacob Keller1bc87e82016-10-05 09:30:31 -07001302 if ((f->state == I40E_FILTER_FAILED) ||
1303 (f->state == I40E_FILTER_NEW)) {
1304 /* this one never got added by the FW. Just remove it,
1305 * no need to sync anything.
1306 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001307 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001308 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001309 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001310 f->state = I40E_FILTER_REMOVE;
1311 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1312 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001313 }
1314}
1315
1316/**
Jacob Keller290d2552016-10-05 09:30:36 -07001317 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1318 * @vsi: the VSI to be searched
1319 * @macaddr: the MAC address
1320 * @vlan: the VLAN
1321 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001322 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001323 * being held.
1324 * ANOTHER NOTE: This function MUST be called from within the context of
1325 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1326 * instead of list_for_each_entry().
1327 **/
1328void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1329{
1330 struct i40e_mac_filter *f;
1331
1332 if (!vsi || !macaddr)
1333 return;
1334
1335 f = i40e_find_filter(vsi, macaddr, vlan);
1336 __i40e_del_filter(vsi, f);
1337}
1338
1339/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001340 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1341 * @vsi: the VSI to be searched
1342 * @macaddr: the mac address to be filtered
1343 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001344 * Goes through all the macvlan filters and adds a macvlan filter for each
1345 * unique vlan that already exists. If a PVID has been assigned, instead only
1346 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001347 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001348 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001349 **/
1350struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1351 const u8 *macaddr)
1352{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001353 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001354 struct hlist_node *h;
1355 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001356
1357 if (vsi->info.pvid)
1358 return i40e_add_filter(vsi, macaddr,
1359 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001360
Jacob Keller278e7d02016-10-05 09:30:37 -07001361 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001362 if (f->state == I40E_FILTER_REMOVE)
1363 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001364 add = i40e_add_filter(vsi, macaddr, f->vlan);
1365 if (!add)
1366 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001367 }
1368
Jacob Keller5feb3d72016-10-05 09:30:34 -07001369 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001370}
1371
1372/**
1373 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1374 * @vsi: the VSI to be searched
1375 * @macaddr: the mac address to be removed
1376 *
1377 * Removes a given MAC address from a VSI, regardless of VLAN
1378 *
1379 * Returns 0 for success, or error
1380 **/
1381int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1382{
Jacob Keller278e7d02016-10-05 09:30:37 -07001383 struct i40e_mac_filter *f;
1384 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001385 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001386 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001387
Jacob Keller278e7d02016-10-05 09:30:37 -07001388 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1389 "Missing mac_filter_hash_lock\n");
1390 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001391 if (ether_addr_equal(macaddr, f->macaddr)) {
1392 __i40e_del_filter(vsi, f);
1393 found = true;
1394 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001395 }
Jacob Keller290d2552016-10-05 09:30:36 -07001396
1397 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001398 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001399 else
1400 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001401}
1402
1403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001404 * i40e_set_mac - NDO callback to set mac address
1405 * @netdev: network interface device structure
1406 * @p: pointer to an address structure
1407 *
1408 * Returns 0 on success, negative on failure
1409 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001410#ifdef I40E_FCOE
1411int i40e_set_mac(struct net_device *netdev, void *p)
1412#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001413static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001414#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001415{
1416 struct i40e_netdev_priv *np = netdev_priv(netdev);
1417 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001418 struct i40e_pf *pf = vsi->back;
1419 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001420 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001421
1422 if (!is_valid_ether_addr(addr->sa_data))
1423 return -EADDRNOTAVAIL;
1424
Shannon Nelson30650cc2014-07-29 04:01:50 +00001425 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1426 netdev_info(netdev, "already using mac address %pM\n",
1427 addr->sa_data);
1428 return 0;
1429 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001430
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001431 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1432 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1433 return -EADDRNOTAVAIL;
1434
Shannon Nelson30650cc2014-07-29 04:01:50 +00001435 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1436 netdev_info(netdev, "returning to hw mac address %pM\n",
1437 hw->mac.addr);
1438 else
1439 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1440
Jacob Keller278e7d02016-10-05 09:30:37 -07001441 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001442 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1443 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001444 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001445 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001446 if (vsi->type == I40E_VSI_MAIN) {
1447 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001448
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001449 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001450 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001451 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001452 if (ret)
1453 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1454 i40e_stat_str(hw, ret),
1455 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001456 }
1457
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001458 /* schedule our worker thread which will take care of
1459 * applying the new filter changes
1460 */
1461 i40e_service_event_schedule(vsi->back);
1462 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001463}
1464
1465/**
1466 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1467 * @vsi: the VSI being setup
1468 * @ctxt: VSI context structure
1469 * @enabled_tc: Enabled TCs bitmap
1470 * @is_add: True if called before Add VSI
1471 *
1472 * Setup VSI queue mapping for enabled traffic classes.
1473 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001474#ifdef I40E_FCOE
1475void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1476 struct i40e_vsi_context *ctxt,
1477 u8 enabled_tc,
1478 bool is_add)
1479#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001480static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1481 struct i40e_vsi_context *ctxt,
1482 u8 enabled_tc,
1483 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001484#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001485{
1486 struct i40e_pf *pf = vsi->back;
1487 u16 sections = 0;
1488 u8 netdev_tc = 0;
1489 u16 numtc = 0;
1490 u16 qcount;
1491 u8 offset;
1492 u16 qmap;
1493 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001494 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001495
1496 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1497 offset = 0;
1498
1499 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1500 /* Find numtc from enabled TC bitmap */
1501 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001502 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001503 numtc++;
1504 }
1505 if (!numtc) {
1506 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1507 numtc = 1;
1508 }
1509 } else {
1510 /* At least TC0 is enabled in case of non-DCB case */
1511 numtc = 1;
1512 }
1513
1514 vsi->tc_config.numtc = numtc;
1515 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001516 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001517 qcount = vsi->alloc_queue_pairs;
1518
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001519 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001520 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001521
1522 /* Setup queue offset/count for all TCs for given VSI */
1523 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1524 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001525 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001526 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001527 int pow, num_qps;
1528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001529 switch (vsi->type) {
1530 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001531 qcount = min_t(int, pf->alloc_rss_size,
1532 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001533 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001534#ifdef I40E_FCOE
1535 case I40E_VSI_FCOE:
1536 qcount = num_tc_qps;
1537 break;
1538#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001539 case I40E_VSI_FDIR:
1540 case I40E_VSI_SRIOV:
1541 case I40E_VSI_VMDQ2:
1542 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001543 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001544 WARN_ON(i != 0);
1545 break;
1546 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001547 vsi->tc_config.tc_info[i].qoffset = offset;
1548 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001549
Shannon Nelson1e200e42015-02-27 09:15:24 +00001550 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001551 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001552 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001553 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001554 pow++;
1555 num_qps >>= 1;
1556 }
1557
1558 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1559 qmap =
1560 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1561 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1562
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001563 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001564 } else {
1565 /* TC is not enabled so set the offset to
1566 * default queue and allocate one queue
1567 * for the given TC.
1568 */
1569 vsi->tc_config.tc_info[i].qoffset = 0;
1570 vsi->tc_config.tc_info[i].qcount = 1;
1571 vsi->tc_config.tc_info[i].netdev_tc = 0;
1572
1573 qmap = 0;
1574 }
1575 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1576 }
1577
1578 /* Set actual Tx/Rx queue pairs */
1579 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001580 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1581 if (vsi->req_queue_pairs > 0)
1582 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001583 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001584 vsi->num_queue_pairs = pf->num_lan_msix;
1585 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001586
1587 /* Scheduler section valid can only be set for ADD VSI */
1588 if (is_add) {
1589 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1590
1591 ctxt->info.up_enable_bits = enabled_tc;
1592 }
1593 if (vsi->type == I40E_VSI_SRIOV) {
1594 ctxt->info.mapping_flags |=
1595 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1596 for (i = 0; i < vsi->num_queue_pairs; i++)
1597 ctxt->info.queue_mapping[i] =
1598 cpu_to_le16(vsi->base_queue + i);
1599 } else {
1600 ctxt->info.mapping_flags |=
1601 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1602 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1603 }
1604 ctxt->info.valid_sections |= cpu_to_le16(sections);
1605}
1606
1607/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001608 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1609 * @netdev: the netdevice
1610 * @addr: address to add
1611 *
1612 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1613 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1614 */
1615static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1616{
1617 struct i40e_netdev_priv *np = netdev_priv(netdev);
1618 struct i40e_vsi *vsi = np->vsi;
1619 struct i40e_mac_filter *f;
1620
1621 if (i40e_is_vsi_in_vlan(vsi))
1622 f = i40e_put_mac_in_vlan(vsi, addr);
1623 else
1624 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1625
1626 if (f)
1627 return 0;
1628 else
1629 return -ENOMEM;
1630}
1631
1632/**
1633 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1634 * @netdev: the netdevice
1635 * @addr: address to add
1636 *
1637 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1638 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1639 */
1640static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1641{
1642 struct i40e_netdev_priv *np = netdev_priv(netdev);
1643 struct i40e_vsi *vsi = np->vsi;
1644
1645 if (i40e_is_vsi_in_vlan(vsi))
1646 i40e_del_mac_all_vlan(vsi, addr);
1647 else
1648 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1649
1650 return 0;
1651}
1652
1653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001654 * i40e_set_rx_mode - NDO callback to set the netdev filters
1655 * @netdev: network interface device structure
1656 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001657#ifdef I40E_FCOE
1658void i40e_set_rx_mode(struct net_device *netdev)
1659#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001660static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001661#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001662{
1663 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001664 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001665
Jacob Keller278e7d02016-10-05 09:30:37 -07001666 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001667
Jacob Keller6622f5c2016-10-05 09:30:32 -07001668 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1669 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001670
Jacob Keller278e7d02016-10-05 09:30:37 -07001671 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672
1673 /* check for other flag changes */
1674 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1675 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1676 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1677 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001678
1679 /* schedule our worker thread which will take care of
1680 * applying the new filter changes
1681 */
1682 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001683}
1684
1685/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001686 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1687 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001688 * @from: Pointer to list which contains MAC filter entries - changes to
1689 * those entries needs to be undone.
1690 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001691 * MAC filter entries from list were slated to be sent to firmware, either for
1692 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001693 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001694static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1695 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001696{
Jacob Keller278e7d02016-10-05 09:30:37 -07001697 struct i40e_mac_filter *f;
1698 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001699
Jacob Keller278e7d02016-10-05 09:30:37 -07001700 hlist_for_each_entry_safe(f, h, from, hlist) {
1701 u64 key = i40e_addr_to_hkey(f->macaddr);
1702
Kiran Patil21659032015-09-30 14:09:03 -04001703 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001704 hlist_del(&f->hlist);
1705 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001706 }
1707}
1708
1709/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001710 * i40e_update_filter_state - Update filter state based on return data
1711 * from firmware
1712 * @count: Number of filters added
1713 * @add_list: return data from fw
1714 * @head: pointer to first filter in current batch
1715 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001716 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001717 * MAC filter entries from list were slated to be added to device. Returns
1718 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001719 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001720static int
1721i40e_update_filter_state(int count,
1722 struct i40e_aqc_add_macvlan_element_data *add_list,
1723 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001724{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001725 int retval = 0;
1726 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001727
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001728
1729 if (!aq_err) {
1730 retval = count;
1731 /* Everything's good, mark all filters active. */
1732 for (i = 0; i < count ; i++) {
1733 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001734 add_head = hlist_entry(add_head->hlist.next,
1735 typeof(struct i40e_mac_filter),
1736 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001737 }
1738 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1739 /* Device ran out of filter space. Check the return value
1740 * for each filter to see which ones are active.
1741 */
1742 for (i = 0; i < count ; i++) {
1743 if (add_list[i].match_method ==
1744 I40E_AQC_MM_ERR_NO_RES) {
1745 add_head->state = I40E_FILTER_FAILED;
1746 } else {
1747 add_head->state = I40E_FILTER_ACTIVE;
1748 retval++;
1749 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001750 add_head = hlist_entry(add_head->hlist.next,
1751 typeof(struct i40e_mac_filter),
1752 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001753 }
1754 } else {
1755 /* Some other horrible thing happened, fail all filters */
1756 retval = 0;
1757 for (i = 0; i < count ; i++) {
1758 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001759 add_head = hlist_entry(add_head->hlist.next,
1760 typeof(struct i40e_mac_filter),
1761 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001762 }
Kiran Patil21659032015-09-30 14:09:03 -04001763 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001764 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001765}
1766
1767/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001768 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1769 * @vsi: ptr to the VSI
1770 *
1771 * Push any outstanding VSI filter changes through the AdminQ.
1772 *
1773 * Returns 0 or error value
1774 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001775int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001776{
Jacob Keller278e7d02016-10-05 09:30:37 -07001777 struct i40e_mac_filter *f, *add_head = NULL;
1778 struct hlist_head tmp_add_list, tmp_del_list;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001779 struct i40e_hw *hw = &vsi->back->hw;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001780 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001781 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001782 int filter_list_len = 0;
1783 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001784 i40e_status aq_ret = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001785 struct hlist_node *h;
Mitch Williamsea02e902015-11-09 15:35:50 -08001786 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001787 struct i40e_pf *pf;
1788 int num_add = 0;
1789 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001790 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001791 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001792 int list_size;
1793 int fcnt;
Jacob Keller278e7d02016-10-05 09:30:37 -07001794 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001795
1796 /* empty array typed pointers, kcalloc later */
1797 struct i40e_aqc_add_macvlan_element_data *add_list;
1798 struct i40e_aqc_remove_macvlan_element_data *del_list;
1799
1800 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1801 usleep_range(1000, 2000);
1802 pf = vsi->back;
1803
1804 if (vsi->netdev) {
1805 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1806 vsi->current_netdev_flags = vsi->netdev->flags;
1807 }
1808
Jacob Keller278e7d02016-10-05 09:30:37 -07001809 INIT_HLIST_HEAD(&tmp_add_list);
1810 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001811
Shannon Nelson2d1de822016-05-16 10:26:44 -07001812 if (vsi->type == I40E_VSI_SRIOV)
1813 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1814 else if (vsi->type != I40E_VSI_MAIN)
1815 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1816
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001817 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1818 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1819
Jacob Keller278e7d02016-10-05 09:30:37 -07001820 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001821 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001822 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001823 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001824 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001825 hash_del(&f->hlist);
1826 hlist_add_head(&f->hlist, &tmp_del_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001827 vsi->active_filters--;
Kiran Patil21659032015-09-30 14:09:03 -04001828 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001829 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001830 hash_del(&f->hlist);
1831 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001832 }
Kiran Patil21659032015-09-30 14:09:03 -04001833 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001834 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001835 }
1836
1837 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07001838 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001839 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04001840 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001841 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08001842 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001843 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07001844 if (!del_list)
1845 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04001846
Jacob Keller278e7d02016-10-05 09:30:37 -07001847 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001848 cmd_flags = 0;
1849
1850 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001851 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001852 if (f->vlan == I40E_VLAN_ANY) {
1853 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07001854 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001855 } else {
1856 del_list[num_del].vlan_tag =
1857 cpu_to_le16((u16)(f->vlan));
1858 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001859
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001860 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1861 del_list[num_del].flags = cmd_flags;
1862 num_del++;
1863
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001864 /* flush a full buffer */
1865 if (num_del == filter_list_len) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001866 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid,
1867 del_list,
1868 num_del, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001869 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001870 num_del = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001871 memset(del_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001872
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001873 /* Explicitly ignore and do not report when
1874 * firmware returns ENOENT.
1875 */
1876 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001877 retval = -EIO;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001878 dev_info(&pf->pdev->dev,
1879 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
Shannon Nelson2d1de822016-05-16 10:26:44 -07001880 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001881 i40e_stat_str(hw, aq_ret),
1882 i40e_aq_str(hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001883 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001884 }
Kiran Patil21659032015-09-30 14:09:03 -04001885 /* Release memory for MAC filter entries which were
1886 * synced up with HW.
1887 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001888 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04001889 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001890 }
Kiran Patil21659032015-09-30 14:09:03 -04001891
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001892 if (num_del) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001893 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, del_list,
1894 num_del, NULL);
1895 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001896 num_del = 0;
1897
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001898 /* Explicitly ignore and do not report when firmware
1899 * returns ENOENT.
1900 */
1901 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1902 retval = -EIO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001903 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07001904 "ignoring delete macvlan error on %s, err %s aq_err %s\n",
1905 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001906 i40e_stat_str(hw, aq_ret),
1907 i40e_aq_str(hw, aq_err));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001908 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001909 }
1910
1911 kfree(del_list);
1912 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001913 }
1914
Jacob Keller278e7d02016-10-05 09:30:37 -07001915 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001916 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001917 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08001918 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001919 list_size = filter_list_len *
1920 sizeof(struct i40e_aqc_add_macvlan_element_data);
1921 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07001922 if (!add_list)
1923 goto err_no_memory;
1924
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001925 num_add = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001926 hlist_for_each_entry(f, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001927 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1928 &vsi->state)) {
1929 f->state = I40E_FILTER_FAILED;
1930 continue;
1931 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001932 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001933 if (num_add == 0)
1934 add_head = f;
1935 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00001936 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001937 if (f->vlan == I40E_VLAN_ANY) {
1938 add_list[num_add].vlan_tag = 0;
1939 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1940 } else {
1941 add_list[num_add].vlan_tag =
1942 cpu_to_le16((u16)(f->vlan));
1943 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001944 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001945 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001946 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1947 num_add++;
1948
1949 /* flush a full buffer */
1950 if (num_add == filter_list_len) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001951 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08001952 add_list, num_add,
1953 NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001954 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001955 fcnt = i40e_update_filter_state(num_add,
1956 add_list,
1957 add_head,
1958 aq_ret);
1959 vsi->active_filters += fcnt;
1960
1961 if (fcnt != num_add) {
1962 promisc_changed = true;
1963 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1964 &vsi->state);
1965 vsi->promisc_threshold =
1966 (vsi->active_filters * 3) / 4;
1967 dev_warn(&pf->pdev->dev,
1968 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1969 i40e_aq_str(hw, aq_err),
1970 vsi_name);
1971 }
1972 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001973 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001974 }
1975 }
1976 if (num_add) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001977 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08001978 add_list, num_add, 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, add_list,
1981 add_head, aq_ret);
1982 vsi->active_filters += fcnt;
1983 if (fcnt != num_add) {
1984 promisc_changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1986 &vsi->state);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001987 vsi->promisc_threshold =
1988 (vsi->active_filters * 3) / 4;
1989 dev_warn(&pf->pdev->dev,
1990 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1991 i40e_aq_str(hw, aq_err), vsi_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992 }
1993 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001994 /* Now move all of the filters from the temp add list back to
1995 * the VSI's list.
1996 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001997 spin_lock_bh(&vsi->mac_filter_hash_lock);
1998 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
1999 u64 key = i40e_addr_to_hkey(f->macaddr);
2000
2001 hlist_del(&f->hlist);
2002 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002003 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002004 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002005 kfree(add_list);
2006 add_list = NULL;
2007 }
2008
2009 /* Check to see if we can drop out of overflow promiscuous mode. */
2010 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2011 (vsi->active_filters < vsi->promisc_threshold)) {
2012 int failed_count = 0;
2013 /* See if we have any failed filters. We can't drop out of
2014 * promiscuous until these have all been deleted.
2015 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002016 spin_lock_bh(&vsi->mac_filter_hash_lock);
2017 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002018 if (f->state == I40E_FILTER_FAILED)
2019 failed_count++;
2020 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002021 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002022 if (!failed_count) {
2023 dev_info(&pf->pdev->dev,
2024 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2025 vsi_name);
2026 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2027 promisc_changed = true;
2028 vsi->promisc_threshold = 0;
2029 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002030 }
2031
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002032 /* if the VF is not trusted do not do promisc */
2033 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2034 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2035 goto out;
2036 }
2037
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002038 /* check for changes in promiscuous modes */
2039 if (changed_flags & IFF_ALLMULTI) {
2040 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002041
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002042 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002043 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2044 vsi->seid,
2045 cur_multipromisc,
2046 NULL);
2047 if (aq_ret) {
2048 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002049 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002050 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002051 "set multi promisc failed on %s, err %s aq_err %s\n",
2052 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002053 i40e_stat_str(hw, aq_ret),
2054 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002055 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002056 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002057 if ((changed_flags & IFF_PROMISC) ||
2058 (promisc_changed &&
2059 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002060 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002061
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2063 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2064 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002065 if ((vsi->type == I40E_VSI_MAIN) &&
2066 (pf->lan_veb != I40E_NO_VEB) &&
2067 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002068 /* set defport ON for Main VSI instead of true promisc
2069 * this way we will get all unicast/multicast and VLAN
2070 * promisc behavior but will not get VF or VMDq traffic
2071 * replicated on the Main VSI.
2072 */
2073 if (pf->cur_promisc != cur_promisc) {
2074 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002075 if (cur_promisc)
2076 aq_ret =
2077 i40e_aq_set_default_vsi(hw,
2078 vsi->seid,
2079 NULL);
2080 else
2081 aq_ret =
2082 i40e_aq_clear_default_vsi(hw,
2083 vsi->seid,
2084 NULL);
2085 if (aq_ret) {
2086 retval = i40e_aq_rc_to_posix(aq_ret,
2087 hw->aq.asq_last_status);
2088 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002089 "Set default VSI failed on %s, err %s, aq_err %s\n",
2090 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002091 i40e_stat_str(hw, aq_ret),
2092 i40e_aq_str(hw,
2093 hw->aq.asq_last_status));
2094 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002095 }
2096 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002097 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002098 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002099 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002100 cur_promisc, NULL,
2101 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002102 if (aq_ret) {
2103 retval =
2104 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002105 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002106 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002107 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2108 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002109 i40e_stat_str(hw, aq_ret),
2110 i40e_aq_str(hw,
2111 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002112 }
2113 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002114 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002115 vsi->seid,
2116 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002117 if (aq_ret) {
2118 retval =
2119 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002120 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002121 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002122 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2123 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002124 i40e_stat_str(hw, aq_ret),
2125 i40e_aq_str(hw,
2126 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002127 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002128 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002129 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2130 vsi->seid,
2131 cur_promisc, NULL);
2132 if (aq_ret) {
2133 retval = i40e_aq_rc_to_posix(aq_ret,
2134 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002135 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002136 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002137 i40e_stat_str(hw, aq_ret),
2138 i40e_aq_str(hw,
2139 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002140 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002141 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002142out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002143 /* if something went wrong then set the changed flag so we try again */
2144 if (retval)
2145 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2146
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002148 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002149
2150err_no_memory:
2151 /* Restore elements on the temporary add and delete lists */
2152 spin_lock_bh(&vsi->mac_filter_hash_lock);
2153 i40e_undo_filter_entries(vsi, &tmp_del_list);
2154 i40e_undo_filter_entries(vsi, &tmp_add_list);
2155 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2156
2157 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2158 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2159 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002160}
2161
2162/**
2163 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2164 * @pf: board private structure
2165 **/
2166static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2167{
2168 int v;
2169
2170 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2171 return;
2172 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2173
Mitch Williams505682c2014-05-20 08:01:37 +00002174 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002175 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002176 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2177 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2178
2179 if (ret) {
2180 /* come back and try again later */
2181 pf->flags |= I40E_FLAG_FILTER_SYNC;
2182 break;
2183 }
2184 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002185 }
2186}
2187
2188/**
2189 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2190 * @netdev: network interface device structure
2191 * @new_mtu: new value for maximum frame size
2192 *
2193 * Returns 0 on success, negative on failure
2194 **/
2195static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2196{
2197 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002198 struct i40e_vsi *vsi = np->vsi;
2199
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002200 netdev_info(netdev, "changing MTU from %d to %d\n",
2201 netdev->mtu, new_mtu);
2202 netdev->mtu = new_mtu;
2203 if (netif_running(netdev))
2204 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002205 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002206 return 0;
2207}
2208
2209/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002210 * i40e_ioctl - Access the hwtstamp interface
2211 * @netdev: network interface device structure
2212 * @ifr: interface request data
2213 * @cmd: ioctl command
2214 **/
2215int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2216{
2217 struct i40e_netdev_priv *np = netdev_priv(netdev);
2218 struct i40e_pf *pf = np->vsi->back;
2219
2220 switch (cmd) {
2221 case SIOCGHWTSTAMP:
2222 return i40e_ptp_get_ts_config(pf, ifr);
2223 case SIOCSHWTSTAMP:
2224 return i40e_ptp_set_ts_config(pf, ifr);
2225 default:
2226 return -EOPNOTSUPP;
2227 }
2228}
2229
2230/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002231 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2232 * @vsi: the vsi being adjusted
2233 **/
2234void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2235{
2236 struct i40e_vsi_context ctxt;
2237 i40e_status ret;
2238
2239 if ((vsi->info.valid_sections &
2240 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2241 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2242 return; /* already enabled */
2243
2244 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2245 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2246 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2247
2248 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002249 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002250 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2251 if (ret) {
2252 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002253 "update vlan stripping failed, err %s aq_err %s\n",
2254 i40e_stat_str(&vsi->back->hw, ret),
2255 i40e_aq_str(&vsi->back->hw,
2256 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002257 }
2258}
2259
2260/**
2261 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2262 * @vsi: the vsi being adjusted
2263 **/
2264void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2265{
2266 struct i40e_vsi_context ctxt;
2267 i40e_status ret;
2268
2269 if ((vsi->info.valid_sections &
2270 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2271 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2272 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2273 return; /* already disabled */
2274
2275 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2276 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2277 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2278
2279 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002280 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002281 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2282 if (ret) {
2283 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002284 "update vlan stripping failed, err %s aq_err %s\n",
2285 i40e_stat_str(&vsi->back->hw, ret),
2286 i40e_aq_str(&vsi->back->hw,
2287 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002288 }
2289}
2290
2291/**
2292 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2293 * @netdev: network interface to be adjusted
2294 * @features: netdev features to test if VLAN offload is enabled or not
2295 **/
2296static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2297{
2298 struct i40e_netdev_priv *np = netdev_priv(netdev);
2299 struct i40e_vsi *vsi = np->vsi;
2300
2301 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2302 i40e_vlan_stripping_enable(vsi);
2303 else
2304 i40e_vlan_stripping_disable(vsi);
2305}
2306
2307/**
2308 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2309 * @vsi: the vsi being configured
2310 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2311 **/
2312int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2313{
Jacob Keller278e7d02016-10-05 09:30:37 -07002314 struct i40e_mac_filter *f, *add_f, *del_f;
2315 struct hlist_node *h;
2316 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002317
Kiran Patil21659032015-09-30 14:09:03 -04002318 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002319 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002320
Jacob Keller1bc87e82016-10-05 09:30:31 -07002321 if (vsi->netdev) {
2322 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002323 if (!add_f) {
2324 dev_info(&vsi->back->pdev->dev,
2325 "Could not add vlan filter %d for %pM\n",
2326 vid, vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002327 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002328 return -ENOMEM;
2329 }
2330 }
2331
Jacob Keller278e7d02016-10-05 09:30:37 -07002332 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002333 if (f->state == I40E_FILTER_REMOVE)
2334 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002335 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002336 if (!add_f) {
2337 dev_info(&vsi->back->pdev->dev,
2338 "Could not add vlan filter %d for %pM\n",
2339 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002340 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002341 return -ENOMEM;
2342 }
2343 }
2344
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002345 /* Now if we add a vlan tag, make sure to check if it is the first
2346 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2347 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002348 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002349 */
Jacob Keller290d2552016-10-05 09:30:36 -07002350 if (vid > 0 && vsi->netdev) {
2351 del_f = i40e_find_filter(vsi, vsi->netdev->dev_addr,
2352 I40E_VLAN_ANY);
2353 if (del_f) {
2354 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002355 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002356 if (!add_f) {
2357 dev_info(&vsi->back->pdev->dev,
2358 "Could not add filter 0 for %pM\n",
2359 vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002360 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002361 return -ENOMEM;
2362 }
2363 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002364 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002365
Greg Rose8d82a7c2014-01-13 16:13:04 -08002366 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2367 if (vid > 0 && !vsi->info.pvid) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002368 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002369 if (f->state == I40E_FILTER_REMOVE)
2370 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002371 del_f = i40e_find_filter(vsi, f->macaddr,
2372 I40E_VLAN_ANY);
2373 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002374 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002375 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002376 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002377 if (!add_f) {
2378 dev_info(&vsi->back->pdev->dev,
2379 "Could not add filter 0 for %pM\n",
2380 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002381 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002382 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002383 }
2384 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002385 }
2386
Jacob Keller278e7d02016-10-05 09:30:37 -07002387 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002388
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002389 /* schedule our worker thread which will take care of
2390 * applying the new filter changes
2391 */
2392 i40e_service_event_schedule(vsi->back);
2393 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002394}
2395
2396/**
2397 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2398 * @vsi: the vsi being configured
2399 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002400 *
2401 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002402 **/
2403int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2404{
2405 struct net_device *netdev = vsi->netdev;
Jacob Keller278e7d02016-10-05 09:30:37 -07002406 struct i40e_mac_filter *f, *add_f;
2407 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002408 int filter_count = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002409 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002410
Kiran Patil21659032015-09-30 14:09:03 -04002411 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002412 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002413
Jacob Keller1bc87e82016-10-05 09:30:31 -07002414 if (vsi->netdev)
2415 i40e_del_filter(vsi, netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002416
Jacob Keller278e7d02016-10-05 09:30:37 -07002417 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002418 if (f->vlan == vid)
2419 __i40e_del_filter(vsi, f);
2420 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002421
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002422 /* go through all the filters for this VSI and if there is only
2423 * vid == 0 it means there are no other filters, so vid 0 must
2424 * be replaced with -1. This signifies that we should from now
2425 * on accept any traffic (with any tag present, or untagged)
2426 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002427 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07002428 if (vsi->netdev) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429 if (f->vlan &&
2430 ether_addr_equal(netdev->dev_addr, f->macaddr))
2431 filter_count++;
2432 }
2433
2434 if (f->vlan)
2435 filter_count++;
2436 }
2437
Jacob Keller1bc87e82016-10-05 09:30:31 -07002438 if (!filter_count && vsi->netdev) {
2439 i40e_del_filter(vsi, netdev->dev_addr, 0);
2440 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002441 if (!f) {
2442 dev_info(&vsi->back->pdev->dev,
2443 "Could not add filter %d for %pM\n",
2444 I40E_VLAN_ANY, netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002445 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002446 return -ENOMEM;
2447 }
2448 }
2449
2450 if (!filter_count) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002451 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002452 if (!f->vlan)
2453 __i40e_del_filter(vsi, f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002454 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002455 if (!add_f) {
2456 dev_info(&vsi->back->pdev->dev,
2457 "Could not add filter %d for %pM\n",
2458 I40E_VLAN_ANY, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002459 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002460 return -ENOMEM;
2461 }
2462 }
2463 }
2464
Jacob Keller278e7d02016-10-05 09:30:37 -07002465 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002466
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002467 /* schedule our worker thread which will take care of
2468 * applying the new filter changes
2469 */
2470 i40e_service_event_schedule(vsi->back);
2471 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002472}
2473
2474/**
2475 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2476 * @netdev: network interface to be adjusted
2477 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002478 *
2479 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002480 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002481#ifdef I40E_FCOE
2482int i40e_vlan_rx_add_vid(struct net_device *netdev,
2483 __always_unused __be16 proto, u16 vid)
2484#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002485static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2486 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002487#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002488{
2489 struct i40e_netdev_priv *np = netdev_priv(netdev);
2490 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002491 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002492
2493 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002494 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002495
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002496 /* If the network stack called us with vid = 0 then
2497 * it is asking to receive priority tagged packets with
2498 * vlan id 0. Our HW receives them by default when configured
2499 * to receive untagged packets so there is no need to add an
2500 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002501 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002502 if (vid)
2503 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002504
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002505 if (!ret && (vid < VLAN_N_VID))
2506 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002507
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002508 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002509}
2510
2511/**
2512 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2513 * @netdev: network interface to be adjusted
2514 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002515 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002516 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002517 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002518#ifdef I40E_FCOE
2519int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2520 __always_unused __be16 proto, u16 vid)
2521#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002522static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2523 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002524#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002525{
2526 struct i40e_netdev_priv *np = netdev_priv(netdev);
2527 struct i40e_vsi *vsi = np->vsi;
2528
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002529 /* return code is ignored as there is nothing a user
2530 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002531 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532 */
2533 i40e_vsi_kill_vlan(vsi, vid);
2534
2535 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002536
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002537 return 0;
2538}
2539
2540/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002541 * i40e_macaddr_init - explicitly write the mac address filters
2542 *
2543 * @vsi: pointer to the vsi
2544 * @macaddr: the MAC address
2545 *
2546 * This is needed when the macaddr has been obtained by other
2547 * means than the default, e.g., from Open Firmware or IDPROM.
2548 * Returns 0 on success, negative on failure
2549 **/
2550static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2551{
2552 int ret;
2553 struct i40e_aqc_add_macvlan_element_data element;
2554
2555 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2556 I40E_AQC_WRITE_TYPE_LAA_WOL,
2557 macaddr, NULL);
2558 if (ret) {
2559 dev_info(&vsi->back->pdev->dev,
2560 "Addr change for VSI failed: %d\n", ret);
2561 return -EADDRNOTAVAIL;
2562 }
2563
2564 memset(&element, 0, sizeof(element));
2565 ether_addr_copy(element.mac_addr, macaddr);
2566 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2567 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2568 if (ret) {
2569 dev_info(&vsi->back->pdev->dev,
2570 "add filter failed err %s aq_err %s\n",
2571 i40e_stat_str(&vsi->back->hw, ret),
2572 i40e_aq_str(&vsi->back->hw,
2573 vsi->back->hw.aq.asq_last_status));
2574 }
2575 return ret;
2576}
2577
2578/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002579 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2580 * @vsi: the vsi being brought back up
2581 **/
2582static void i40e_restore_vlan(struct i40e_vsi *vsi)
2583{
2584 u16 vid;
2585
2586 if (!vsi->netdev)
2587 return;
2588
2589 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2590
2591 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2592 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2593 vid);
2594}
2595
2596/**
2597 * i40e_vsi_add_pvid - Add pvid for the VSI
2598 * @vsi: the vsi being adjusted
2599 * @vid: the vlan id to set as a PVID
2600 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002601int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002602{
2603 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002604 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002605
2606 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2607 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002608 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2609 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002610 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002611
2612 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002613 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002614 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2615 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002616 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002617 "add pvid failed, err %s aq_err %s\n",
2618 i40e_stat_str(&vsi->back->hw, ret),
2619 i40e_aq_str(&vsi->back->hw,
2620 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002621 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002622 }
2623
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002624 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002625}
2626
2627/**
2628 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2629 * @vsi: the vsi being adjusted
2630 *
2631 * Just use the vlan_rx_register() service to put it back to normal
2632 **/
2633void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2634{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002635 i40e_vlan_stripping_disable(vsi);
2636
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002637 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002638}
2639
2640/**
2641 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2642 * @vsi: ptr to the VSI
2643 *
2644 * If this function returns with an error, then it's possible one or
2645 * more of the rings is populated (while the rest are not). It is the
2646 * callers duty to clean those orphaned rings.
2647 *
2648 * Return 0 on success, negative on failure
2649 **/
2650static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2651{
2652 int i, err = 0;
2653
2654 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002655 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002656
2657 return err;
2658}
2659
2660/**
2661 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2662 * @vsi: ptr to the VSI
2663 *
2664 * Free VSI's transmit software resources
2665 **/
2666static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2667{
2668 int i;
2669
Greg Rose8e9dca52013-12-18 13:45:53 +00002670 if (!vsi->tx_rings)
2671 return;
2672
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002673 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002674 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002675 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002676}
2677
2678/**
2679 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2680 * @vsi: ptr to the VSI
2681 *
2682 * If this function returns with an error, then it's possible one or
2683 * more of the rings is populated (while the rest are not). It is the
2684 * callers duty to clean those orphaned rings.
2685 *
2686 * Return 0 on success, negative on failure
2687 **/
2688static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2689{
2690 int i, err = 0;
2691
2692 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002693 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002694#ifdef I40E_FCOE
2695 i40e_fcoe_setup_ddp_resources(vsi);
2696#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002697 return err;
2698}
2699
2700/**
2701 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2702 * @vsi: ptr to the VSI
2703 *
2704 * Free all receive software resources
2705 **/
2706static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2707{
2708 int i;
2709
Greg Rose8e9dca52013-12-18 13:45:53 +00002710 if (!vsi->rx_rings)
2711 return;
2712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002713 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002714 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002715 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002716#ifdef I40E_FCOE
2717 i40e_fcoe_free_ddp_resources(vsi);
2718#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002719}
2720
2721/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002722 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2723 * @ring: The Tx ring to configure
2724 *
2725 * This enables/disables XPS for a given Tx descriptor ring
2726 * based on the TCs enabled for the VSI that ring belongs to.
2727 **/
2728static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2729{
2730 struct i40e_vsi *vsi = ring->vsi;
2731 cpumask_var_t mask;
2732
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002733 if (!ring->q_vector || !ring->netdev)
2734 return;
2735
2736 /* Single TC mode enable XPS */
2737 if (vsi->tc_config.numtc <= 1) {
2738 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002739 netif_set_xps_queue(ring->netdev,
2740 &ring->q_vector->affinity_mask,
2741 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002742 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2743 /* Disable XPS to allow selection based on TC */
2744 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2745 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2746 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002747 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002748
2749 /* schedule our worker thread which will take care of
2750 * applying the new filter changes
2751 */
2752 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002753}
2754
2755/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2757 * @ring: The Tx ring to configure
2758 *
2759 * Configure the Tx descriptor ring in the HMC context.
2760 **/
2761static int i40e_configure_tx_ring(struct i40e_ring *ring)
2762{
2763 struct i40e_vsi *vsi = ring->vsi;
2764 u16 pf_q = vsi->base_queue + ring->queue_index;
2765 struct i40e_hw *hw = &vsi->back->hw;
2766 struct i40e_hmc_obj_txq tx_ctx;
2767 i40e_status err = 0;
2768 u32 qtx_ctl = 0;
2769
2770 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002771 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2773 ring->atr_count = 0;
2774 } else {
2775 ring->atr_sample_rate = 0;
2776 }
2777
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002778 /* configure XPS */
2779 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002780
2781 /* clear the context structure first */
2782 memset(&tx_ctx, 0, sizeof(tx_ctx));
2783
2784 tx_ctx.new_context = 1;
2785 tx_ctx.base = (ring->dma / 128);
2786 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002787 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2788 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002789#ifdef I40E_FCOE
2790 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2791#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002792 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002793 /* FDIR VSI tx ring can still use RS bit and writebacks */
2794 if (vsi->type != I40E_VSI_FDIR)
2795 tx_ctx.head_wb_ena = 1;
2796 tx_ctx.head_wb_addr = ring->dma +
2797 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002798
2799 /* As part of VSI creation/update, FW allocates certain
2800 * Tx arbitration queue sets for each TC enabled for
2801 * the VSI. The FW returns the handles to these queue
2802 * sets as part of the response buffer to Add VSI,
2803 * Update VSI, etc. AQ commands. It is expected that
2804 * these queue set handles be associated with the Tx
2805 * queues by the driver as part of the TX queue context
2806 * initialization. This has to be done regardless of
2807 * DCB as by default everything is mapped to TC0.
2808 */
2809 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2810 tx_ctx.rdylist_act = 0;
2811
2812 /* clear the context in the HMC */
2813 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2814 if (err) {
2815 dev_info(&vsi->back->pdev->dev,
2816 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2817 ring->queue_index, pf_q, err);
2818 return -ENOMEM;
2819 }
2820
2821 /* set the context in the HMC */
2822 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2823 if (err) {
2824 dev_info(&vsi->back->pdev->dev,
2825 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2826 ring->queue_index, pf_q, err);
2827 return -ENOMEM;
2828 }
2829
2830 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002831 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002832 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002833 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2834 I40E_QTX_CTL_VFVM_INDX_MASK;
2835 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002836 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002837 }
2838
Shannon Nelson13fd9772013-09-28 07:14:19 +00002839 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2840 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002841 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2842 i40e_flush(hw);
2843
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002844 /* cache tail off for easier writes later */
2845 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2846
2847 return 0;
2848}
2849
2850/**
2851 * i40e_configure_rx_ring - Configure a receive ring context
2852 * @ring: The Rx ring to configure
2853 *
2854 * Configure the Rx descriptor ring in the HMC context.
2855 **/
2856static int i40e_configure_rx_ring(struct i40e_ring *ring)
2857{
2858 struct i40e_vsi *vsi = ring->vsi;
2859 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2860 u16 pf_q = vsi->base_queue + ring->queue_index;
2861 struct i40e_hw *hw = &vsi->back->hw;
2862 struct i40e_hmc_obj_rxq rx_ctx;
2863 i40e_status err = 0;
2864
2865 ring->state = 0;
2866
2867 /* clear the context structure first */
2868 memset(&rx_ctx, 0, sizeof(rx_ctx));
2869
2870 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002871
2872 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002873
2874 rx_ctx.base = (ring->dma / 128);
2875 rx_ctx.qlen = ring->count;
2876
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002877 /* use 32 byte descriptors */
2878 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002879
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002880 /* descriptor type is always zero
2881 * rx_ctx.dtype = 0;
2882 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002883 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002884
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002885 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002886 if (hw->revision_id == 0)
2887 rx_ctx.lrxqthresh = 0;
2888 else
2889 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002890 rx_ctx.crcstrip = 1;
2891 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002892 /* this controls whether VLAN is stripped from inner headers */
2893 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002894#ifdef I40E_FCOE
2895 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2896#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002897 /* set the prefena field to 1 because the manual says to */
2898 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002899
2900 /* clear the context in the HMC */
2901 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2902 if (err) {
2903 dev_info(&vsi->back->pdev->dev,
2904 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2905 ring->queue_index, pf_q, err);
2906 return -ENOMEM;
2907 }
2908
2909 /* set the context in the HMC */
2910 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2911 if (err) {
2912 dev_info(&vsi->back->pdev->dev,
2913 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2914 ring->queue_index, pf_q, err);
2915 return -ENOMEM;
2916 }
2917
2918 /* cache tail for quicker writes, and clear the reg before use */
2919 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2920 writel(0, ring->tail);
2921
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002922 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002923
2924 return 0;
2925}
2926
2927/**
2928 * i40e_vsi_configure_tx - Configure the VSI for Tx
2929 * @vsi: VSI structure describing this set of rings and resources
2930 *
2931 * Configure the Tx VSI for operation.
2932 **/
2933static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2934{
2935 int err = 0;
2936 u16 i;
2937
Alexander Duyck9f65e152013-09-28 06:00:58 +00002938 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2939 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002940
2941 return err;
2942}
2943
2944/**
2945 * i40e_vsi_configure_rx - Configure the VSI for Rx
2946 * @vsi: the VSI being configured
2947 *
2948 * Configure the Rx VSI for operation.
2949 **/
2950static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2951{
2952 int err = 0;
2953 u16 i;
2954
2955 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2956 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2957 + ETH_FCS_LEN + VLAN_HLEN;
2958 else
2959 vsi->max_frame = I40E_RXBUFFER_2048;
2960
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002961 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002962
Vasu Dev38e00432014-08-01 13:27:03 -07002963#ifdef I40E_FCOE
2964 /* setup rx buffer for FCoE */
2965 if ((vsi->type == I40E_VSI_FCOE) &&
2966 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07002967 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2968 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07002969 }
2970
2971#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002972 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002973 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002974 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002975
2976 /* set up individual rings */
2977 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002978 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002979
2980 return err;
2981}
2982
2983/**
2984 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2985 * @vsi: ptr to the VSI
2986 **/
2987static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2988{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002989 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002990 u16 qoffset, qcount;
2991 int i, n;
2992
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002993 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2994 /* Reset the TC information */
2995 for (i = 0; i < vsi->num_queue_pairs; i++) {
2996 rx_ring = vsi->rx_rings[i];
2997 tx_ring = vsi->tx_rings[i];
2998 rx_ring->dcb_tc = 0;
2999 tx_ring->dcb_tc = 0;
3000 }
3001 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003002
3003 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003004 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003005 continue;
3006
3007 qoffset = vsi->tc_config.tc_info[n].qoffset;
3008 qcount = vsi->tc_config.tc_info[n].qcount;
3009 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003010 rx_ring = vsi->rx_rings[i];
3011 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003012 rx_ring->dcb_tc = n;
3013 tx_ring->dcb_tc = n;
3014 }
3015 }
3016}
3017
3018/**
3019 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3020 * @vsi: ptr to the VSI
3021 **/
3022static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3023{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003024 struct i40e_pf *pf = vsi->back;
3025 int err;
3026
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003027 if (vsi->netdev)
3028 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003029
3030 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3031 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3032 if (err) {
3033 dev_warn(&pf->pdev->dev,
3034 "could not set up macaddr; err %d\n", err);
3035 }
3036 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003037}
3038
3039/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003040 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3041 * @vsi: Pointer to the targeted VSI
3042 *
3043 * This function replays the hlist on the hw where all the SB Flow Director
3044 * filters were saved.
3045 **/
3046static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3047{
3048 struct i40e_fdir_filter *filter;
3049 struct i40e_pf *pf = vsi->back;
3050 struct hlist_node *node;
3051
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003052 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3053 return;
3054
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003055 hlist_for_each_entry_safe(filter, node,
3056 &pf->fdir_filter_list, fdir_node) {
3057 i40e_add_del_fdir(vsi, filter, true);
3058 }
3059}
3060
3061/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003062 * i40e_vsi_configure - Set up the VSI for action
3063 * @vsi: the VSI being configured
3064 **/
3065static int i40e_vsi_configure(struct i40e_vsi *vsi)
3066{
3067 int err;
3068
3069 i40e_set_vsi_rx_mode(vsi);
3070 i40e_restore_vlan(vsi);
3071 i40e_vsi_config_dcb_rings(vsi);
3072 err = i40e_vsi_configure_tx(vsi);
3073 if (!err)
3074 err = i40e_vsi_configure_rx(vsi);
3075
3076 return err;
3077}
3078
3079/**
3080 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3081 * @vsi: the VSI being configured
3082 **/
3083static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3084{
3085 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003086 struct i40e_hw *hw = &pf->hw;
3087 u16 vector;
3088 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089 u32 qp;
3090
3091 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3092 * and PFINT_LNKLSTn registers, e.g.:
3093 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3094 */
3095 qp = vsi->base_queue;
3096 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003097 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003098 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3099
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003100 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003101 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003102 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3103 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3104 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003105 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003106 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3107 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3108 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003109 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3110 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003111
3112 /* Linked list for the queuepairs assigned to this vector */
3113 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3114 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003115 u32 val;
3116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003117 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3118 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3119 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3120 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3121 (I40E_QUEUE_TYPE_TX
3122 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3123
3124 wr32(hw, I40E_QINT_RQCTL(qp), val);
3125
3126 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3127 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3128 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3129 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3130 (I40E_QUEUE_TYPE_RX
3131 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3132
3133 /* Terminate the linked list */
3134 if (q == (q_vector->num_ringpairs - 1))
3135 val |= (I40E_QUEUE_END_OF_LIST
3136 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3137
3138 wr32(hw, I40E_QINT_TQCTL(qp), val);
3139 qp++;
3140 }
3141 }
3142
3143 i40e_flush(hw);
3144}
3145
3146/**
3147 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3148 * @hw: ptr to the hardware info
3149 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003150static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003151{
Jacob Kellerab437b52014-12-14 01:55:08 +00003152 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003153 u32 val;
3154
3155 /* clear things first */
3156 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3157 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3158
3159 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3160 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3161 I40E_PFINT_ICR0_ENA_GRST_MASK |
3162 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3163 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003164 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3165 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3166 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3167
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003168 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3169 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3170
Jacob Kellerab437b52014-12-14 01:55:08 +00003171 if (pf->flags & I40E_FLAG_PTP)
3172 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3173
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003174 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3175
3176 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003177 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3178 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003179
3180 /* OTHER_ITR_IDX = 0 */
3181 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3182}
3183
3184/**
3185 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3186 * @vsi: the VSI being configured
3187 **/
3188static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3189{
Alexander Duyck493fb302013-09-28 07:01:44 +00003190 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003191 struct i40e_pf *pf = vsi->back;
3192 struct i40e_hw *hw = &pf->hw;
3193 u32 val;
3194
3195 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003196 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003197 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003198 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3199 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003200 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003201 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3202 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3203
Jacob Kellerab437b52014-12-14 01:55:08 +00003204 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205
3206 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3207 wr32(hw, I40E_PFINT_LNKLST0, 0);
3208
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003209 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3211 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3212 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3213
3214 wr32(hw, I40E_QINT_RQCTL(0), val);
3215
3216 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3217 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3218 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3219
3220 wr32(hw, I40E_QINT_TQCTL(0), val);
3221 i40e_flush(hw);
3222}
3223
3224/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003225 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3226 * @pf: board private structure
3227 **/
3228void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3229{
3230 struct i40e_hw *hw = &pf->hw;
3231
3232 wr32(hw, I40E_PFINT_DYN_CTL0,
3233 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3234 i40e_flush(hw);
3235}
3236
3237/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3239 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003240 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003241 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003242void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003243{
3244 struct i40e_hw *hw = &pf->hw;
3245 u32 val;
3246
3247 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003248 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003249 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3250
3251 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3252 i40e_flush(hw);
3253}
3254
3255/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003256 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3257 * @irq: interrupt number
3258 * @data: pointer to a q_vector
3259 **/
3260static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3261{
3262 struct i40e_q_vector *q_vector = data;
3263
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003264 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003265 return IRQ_HANDLED;
3266
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003267 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003268
3269 return IRQ_HANDLED;
3270}
3271
3272/**
Alan Brady96db7762016-09-14 16:24:38 -07003273 * i40e_irq_affinity_notify - Callback for affinity changes
3274 * @notify: context as to what irq was changed
3275 * @mask: the new affinity mask
3276 *
3277 * This is a callback function used by the irq_set_affinity_notifier function
3278 * so that we may register to receive changes to the irq affinity masks.
3279 **/
3280static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3281 const cpumask_t *mask)
3282{
3283 struct i40e_q_vector *q_vector =
3284 container_of(notify, struct i40e_q_vector, affinity_notify);
3285
3286 q_vector->affinity_mask = *mask;
3287}
3288
3289/**
3290 * i40e_irq_affinity_release - Callback for affinity notifier release
3291 * @ref: internal core kernel usage
3292 *
3293 * This is a callback function used by the irq_set_affinity_notifier function
3294 * to inform the current notification subscriber that they will no longer
3295 * receive notifications.
3296 **/
3297static void i40e_irq_affinity_release(struct kref *ref) {}
3298
3299/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3301 * @vsi: the VSI being configured
3302 * @basename: name for the vector
3303 *
3304 * Allocates MSI-X vectors and requests interrupts from the kernel.
3305 **/
3306static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3307{
3308 int q_vectors = vsi->num_q_vectors;
3309 struct i40e_pf *pf = vsi->back;
3310 int base = vsi->base_vector;
3311 int rx_int_idx = 0;
3312 int tx_int_idx = 0;
3313 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003314 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315
3316 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003317 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003318
Alan Brady96db7762016-09-14 16:24:38 -07003319 irq_num = pf->msix_entries[base + vector].vector;
3320
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003321 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003322 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3323 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3324 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003325 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003326 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3327 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003328 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003329 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3330 "%s-%s-%d", basename, "tx", tx_int_idx++);
3331 } else {
3332 /* skip this unused q_vector */
3333 continue;
3334 }
Alan Brady96db7762016-09-14 16:24:38 -07003335 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003336 vsi->irq_handler,
3337 0,
3338 q_vector->name,
3339 q_vector);
3340 if (err) {
3341 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003342 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003343 goto free_queue_irqs;
3344 }
Alan Brady96db7762016-09-14 16:24:38 -07003345
3346 /* register for affinity change notifications */
3347 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3348 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3349 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003350 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003351 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003352 }
3353
Shannon Nelson63741842014-04-23 04:50:16 +00003354 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003355 return 0;
3356
3357free_queue_irqs:
3358 while (vector) {
3359 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003360 irq_num = pf->msix_entries[base + vector].vector;
3361 irq_set_affinity_notifier(irq_num, NULL);
3362 irq_set_affinity_hint(irq_num, NULL);
3363 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003364 }
3365 return err;
3366}
3367
3368/**
3369 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3370 * @vsi: the VSI being un-configured
3371 **/
3372static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3373{
3374 struct i40e_pf *pf = vsi->back;
3375 struct i40e_hw *hw = &pf->hw;
3376 int base = vsi->base_vector;
3377 int i;
3378
3379 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003380 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3381 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003382 }
3383
3384 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3385 for (i = vsi->base_vector;
3386 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3387 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3388
3389 i40e_flush(hw);
3390 for (i = 0; i < vsi->num_q_vectors; i++)
3391 synchronize_irq(pf->msix_entries[i + base].vector);
3392 } else {
3393 /* Legacy and MSI mode - this stops all interrupt handling */
3394 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3395 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3396 i40e_flush(hw);
3397 synchronize_irq(pf->pdev->irq);
3398 }
3399}
3400
3401/**
3402 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3403 * @vsi: the VSI being configured
3404 **/
3405static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3406{
3407 struct i40e_pf *pf = vsi->back;
3408 int i;
3409
3410 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003411 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003412 i40e_irq_dynamic_enable(vsi, i);
3413 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003414 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003415 }
3416
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003417 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003418 return 0;
3419}
3420
3421/**
3422 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3423 * @pf: board private structure
3424 **/
3425static void i40e_stop_misc_vector(struct i40e_pf *pf)
3426{
3427 /* Disable ICR 0 */
3428 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3429 i40e_flush(&pf->hw);
3430}
3431
3432/**
3433 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3434 * @irq: interrupt number
3435 * @data: pointer to a q_vector
3436 *
3437 * This is the handler used for all MSI/Legacy interrupts, and deals
3438 * with both queue and non-queue interrupts. This is also used in
3439 * MSIX mode to handle the non-queue interrupts.
3440 **/
3441static irqreturn_t i40e_intr(int irq, void *data)
3442{
3443 struct i40e_pf *pf = (struct i40e_pf *)data;
3444 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003445 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446 u32 icr0, icr0_remaining;
3447 u32 val, ena_mask;
3448
3449 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003450 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003451
Shannon Nelson116a57d2013-09-28 07:13:59 +00003452 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3453 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003454 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003455
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003456 /* if interrupt but no bits showing, must be SWINT */
3457 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3458 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3459 pf->sw_int_count++;
3460
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003461 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3462 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3463 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3464 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3465 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3466 }
3467
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003468 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3469 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003470 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3471 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003472
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003473 /* We do not have a way to disarm Queue causes while leaving
3474 * interrupt enabled for all other causes, ideally
3475 * interrupt should be disabled while we are in NAPI but
3476 * this is not a performance path and napi_schedule()
3477 * can deal with rescheduling.
3478 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003479 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003480 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003481 }
3482
3483 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3484 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3485 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003486 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003487 }
3488
3489 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3490 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3491 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3492 }
3493
3494 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3495 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3496 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3497 }
3498
3499 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3500 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3501 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3502 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3503 val = rd32(hw, I40E_GLGEN_RSTAT);
3504 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3505 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003506 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003507 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003508 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003509 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003510 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003511 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003512 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003513 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003514 }
3515
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003516 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3517 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3518 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003519 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3520 rd32(hw, I40E_PFHMC_ERRORINFO),
3521 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003522 }
3523
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003524 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3525 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3526
3527 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003528 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003529 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003530 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003531 }
3532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003533 /* If a critical error is pending we have no choice but to reset the
3534 * device.
3535 * Report and mask out any remaining unexpected interrupts.
3536 */
3537 icr0_remaining = icr0 & ena_mask;
3538 if (icr0_remaining) {
3539 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3540 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003541 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003542 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003543 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003544 dev_info(&pf->pdev->dev, "device will be reset\n");
3545 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3546 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003547 }
3548 ena_mask &= ~icr0_remaining;
3549 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003550 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003551
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003552enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003553 /* re-enable interrupt causes */
3554 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003555 if (!test_bit(__I40E_DOWN, &pf->state)) {
3556 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003557 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003558 }
3559
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003560 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003561}
3562
3563/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003564 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3565 * @tx_ring: tx ring to clean
3566 * @budget: how many cleans we're allowed
3567 *
3568 * Returns true if there's any budget left (e.g. the clean is finished)
3569 **/
3570static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3571{
3572 struct i40e_vsi *vsi = tx_ring->vsi;
3573 u16 i = tx_ring->next_to_clean;
3574 struct i40e_tx_buffer *tx_buf;
3575 struct i40e_tx_desc *tx_desc;
3576
3577 tx_buf = &tx_ring->tx_bi[i];
3578 tx_desc = I40E_TX_DESC(tx_ring, i);
3579 i -= tx_ring->count;
3580
3581 do {
3582 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3583
3584 /* if next_to_watch is not set then there is no work pending */
3585 if (!eop_desc)
3586 break;
3587
3588 /* prevent any other reads prior to eop_desc */
3589 read_barrier_depends();
3590
3591 /* if the descriptor isn't done, no work yet to do */
3592 if (!(eop_desc->cmd_type_offset_bsz &
3593 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3594 break;
3595
3596 /* clear next_to_watch to prevent false hangs */
3597 tx_buf->next_to_watch = NULL;
3598
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003599 tx_desc->buffer_addr = 0;
3600 tx_desc->cmd_type_offset_bsz = 0;
3601 /* move past filter desc */
3602 tx_buf++;
3603 tx_desc++;
3604 i++;
3605 if (unlikely(!i)) {
3606 i -= tx_ring->count;
3607 tx_buf = tx_ring->tx_bi;
3608 tx_desc = I40E_TX_DESC(tx_ring, 0);
3609 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003610 /* unmap skb header data */
3611 dma_unmap_single(tx_ring->dev,
3612 dma_unmap_addr(tx_buf, dma),
3613 dma_unmap_len(tx_buf, len),
3614 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003615 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3616 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003617
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003618 tx_buf->raw_buf = NULL;
3619 tx_buf->tx_flags = 0;
3620 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003621 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003622 tx_desc->buffer_addr = 0;
3623 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003624
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003625 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003626 tx_buf++;
3627 tx_desc++;
3628 i++;
3629 if (unlikely(!i)) {
3630 i -= tx_ring->count;
3631 tx_buf = tx_ring->tx_bi;
3632 tx_desc = I40E_TX_DESC(tx_ring, 0);
3633 }
3634
3635 /* update budget accounting */
3636 budget--;
3637 } while (likely(budget));
3638
3639 i += tx_ring->count;
3640 tx_ring->next_to_clean = i;
3641
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003642 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003643 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003644
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003645 return budget > 0;
3646}
3647
3648/**
3649 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3650 * @irq: interrupt number
3651 * @data: pointer to a q_vector
3652 **/
3653static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3654{
3655 struct i40e_q_vector *q_vector = data;
3656 struct i40e_vsi *vsi;
3657
3658 if (!q_vector->tx.ring)
3659 return IRQ_HANDLED;
3660
3661 vsi = q_vector->tx.ring->vsi;
3662 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3663
3664 return IRQ_HANDLED;
3665}
3666
3667/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003668 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003669 * @vsi: the VSI being configured
3670 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003671 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003672 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003673static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003674{
Alexander Duyck493fb302013-09-28 07:01:44 +00003675 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003676 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3677 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003678
3679 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003680 tx_ring->next = q_vector->tx.ring;
3681 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003682 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003683
3684 rx_ring->q_vector = q_vector;
3685 rx_ring->next = q_vector->rx.ring;
3686 q_vector->rx.ring = rx_ring;
3687 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003688}
3689
3690/**
3691 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3692 * @vsi: the VSI being configured
3693 *
3694 * This function maps descriptor rings to the queue-specific vectors
3695 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3696 * one vector per queue pair, but on a constrained vector budget, we
3697 * group the queue pairs as "efficiently" as possible.
3698 **/
3699static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3700{
3701 int qp_remaining = vsi->num_queue_pairs;
3702 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003703 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003704 int v_start = 0;
3705 int qp_idx = 0;
3706
3707 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3708 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003709 * It is also important to go through all the vectors available to be
3710 * sure that if we don't use all the vectors, that the remaining vectors
3711 * are cleared. This is especially important when decreasing the
3712 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003713 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003714 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003715 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3716
3717 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3718
3719 q_vector->num_ringpairs = num_ringpairs;
3720
3721 q_vector->rx.count = 0;
3722 q_vector->tx.count = 0;
3723 q_vector->rx.ring = NULL;
3724 q_vector->tx.ring = NULL;
3725
3726 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003727 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003728 qp_idx++;
3729 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003730 }
3731 }
3732}
3733
3734/**
3735 * i40e_vsi_request_irq - Request IRQ from the OS
3736 * @vsi: the VSI being configured
3737 * @basename: name for the vector
3738 **/
3739static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3740{
3741 struct i40e_pf *pf = vsi->back;
3742 int err;
3743
3744 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3745 err = i40e_vsi_request_irq_msix(vsi, basename);
3746 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3747 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003748 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003749 else
3750 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003751 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003752
3753 if (err)
3754 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3755
3756 return err;
3757}
3758
3759#ifdef CONFIG_NET_POLL_CONTROLLER
3760/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003761 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003762 * @netdev: network interface device structure
3763 *
3764 * This is used by netconsole to send skbs without having to re-enable
3765 * interrupts. It's not called while the normal interrupt routine is executing.
3766 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003767#ifdef I40E_FCOE
3768void i40e_netpoll(struct net_device *netdev)
3769#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003770static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003771#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003772{
3773 struct i40e_netdev_priv *np = netdev_priv(netdev);
3774 struct i40e_vsi *vsi = np->vsi;
3775 struct i40e_pf *pf = vsi->back;
3776 int i;
3777
3778 /* if interface is down do nothing */
3779 if (test_bit(__I40E_DOWN, &vsi->state))
3780 return;
3781
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003782 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3783 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003784 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003785 } else {
3786 i40e_intr(pf->pdev->irq, netdev);
3787 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003788}
3789#endif
3790
3791/**
Neerav Parikh23527302014-06-03 23:50:15 +00003792 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3793 * @pf: the PF being configured
3794 * @pf_q: the PF queue
3795 * @enable: enable or disable state of the queue
3796 *
3797 * This routine will wait for the given Tx queue of the PF to reach the
3798 * enabled or disabled state.
3799 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3800 * multiple retries; else will return 0 in case of success.
3801 **/
3802static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3803{
3804 int i;
3805 u32 tx_reg;
3806
3807 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3808 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3809 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3810 break;
3811
Neerav Parikhf98a2002014-09-13 07:40:44 +00003812 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003813 }
3814 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3815 return -ETIMEDOUT;
3816
3817 return 0;
3818}
3819
3820/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003821 * i40e_vsi_control_tx - Start or stop a VSI's rings
3822 * @vsi: the VSI being configured
3823 * @enable: start or stop the rings
3824 **/
3825static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3826{
3827 struct i40e_pf *pf = vsi->back;
3828 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003829 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003830 u32 tx_reg;
3831
3832 pf_q = vsi->base_queue;
3833 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003834
3835 /* warn the TX unit of coming changes */
3836 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3837 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003838 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003839
Mitch Williams6c5ef622014-02-20 19:29:16 -08003840 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003841 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003842 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3843 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3844 break;
3845 usleep_range(1000, 2000);
3846 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003847 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003848 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003849 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850
3851 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003852 if (enable) {
3853 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003854 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003855 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003856 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003857 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003858
3859 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003860 /* No waiting for the Tx queue to disable */
3861 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3862 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003863
3864 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003865 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3866 if (ret) {
3867 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003868 "VSI seid %d Tx ring %d %sable timeout\n",
3869 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003870 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003871 }
3872 }
3873
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003874 if (hw->revision_id == 0)
3875 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003876 return ret;
3877}
3878
3879/**
3880 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3881 * @pf: the PF being configured
3882 * @pf_q: the PF queue
3883 * @enable: enable or disable state of the queue
3884 *
3885 * This routine will wait for the given Rx queue of the PF to reach the
3886 * enabled or disabled state.
3887 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3888 * multiple retries; else will return 0 in case of success.
3889 **/
3890static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3891{
3892 int i;
3893 u32 rx_reg;
3894
3895 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3896 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3897 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3898 break;
3899
Neerav Parikhf98a2002014-09-13 07:40:44 +00003900 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003901 }
3902 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3903 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003904
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003905 return 0;
3906}
3907
3908/**
3909 * i40e_vsi_control_rx - Start or stop a VSI's rings
3910 * @vsi: the VSI being configured
3911 * @enable: start or stop the rings
3912 **/
3913static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3914{
3915 struct i40e_pf *pf = vsi->back;
3916 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003917 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003918 u32 rx_reg;
3919
3920 pf_q = vsi->base_queue;
3921 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003922 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003923 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003924 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3925 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3926 break;
3927 usleep_range(1000, 2000);
3928 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003929
Catherine Sullivan7c122002014-03-14 07:32:29 +00003930 /* Skip if the queue is already in the requested state */
3931 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3932 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003933
3934 /* turn on/off the queue */
3935 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003936 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003937 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003938 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003939 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003940 /* No waiting for the Tx queue to disable */
3941 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3942 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003943
3944 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003945 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3946 if (ret) {
3947 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003948 "VSI seid %d Rx ring %d %sable timeout\n",
3949 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003950 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003951 }
3952 }
3953
Neerav Parikh23527302014-06-03 23:50:15 +00003954 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003955}
3956
3957/**
3958 * i40e_vsi_control_rings - Start or stop a VSI's rings
3959 * @vsi: the VSI being configured
3960 * @enable: start or stop the rings
3961 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003962int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003963{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003964 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003965
3966 /* do rx first for enable and last for disable */
3967 if (request) {
3968 ret = i40e_vsi_control_rx(vsi, request);
3969 if (ret)
3970 return ret;
3971 ret = i40e_vsi_control_tx(vsi, request);
3972 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003973 /* Ignore return value, we need to shutdown whatever we can */
3974 i40e_vsi_control_tx(vsi, request);
3975 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003976 }
3977
3978 return ret;
3979}
3980
3981/**
3982 * i40e_vsi_free_irq - Free the irq association with the OS
3983 * @vsi: the VSI being configured
3984 **/
3985static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3986{
3987 struct i40e_pf *pf = vsi->back;
3988 struct i40e_hw *hw = &pf->hw;
3989 int base = vsi->base_vector;
3990 u32 val, qp;
3991 int i;
3992
3993 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3994 if (!vsi->q_vectors)
3995 return;
3996
Shannon Nelson63741842014-04-23 04:50:16 +00003997 if (!vsi->irqs_ready)
3998 return;
3999
4000 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004001 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004002 int irq_num;
4003 u16 vector;
4004
4005 vector = i + base;
4006 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004007
4008 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004009 if (!vsi->q_vectors[i] ||
4010 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004011 continue;
4012
Alan Brady96db7762016-09-14 16:24:38 -07004013 /* clear the affinity notifier in the IRQ descriptor */
4014 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004015 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004016 irq_set_affinity_hint(irq_num, NULL);
4017 synchronize_irq(irq_num);
4018 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019
4020 /* Tear down the interrupt queue link list
4021 *
4022 * We know that they come in pairs and always
4023 * the Rx first, then the Tx. To clear the
4024 * link list, stick the EOL value into the
4025 * next_q field of the registers.
4026 */
4027 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4028 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4029 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4030 val |= I40E_QUEUE_END_OF_LIST
4031 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4032 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4033
4034 while (qp != I40E_QUEUE_END_OF_LIST) {
4035 u32 next;
4036
4037 val = rd32(hw, I40E_QINT_RQCTL(qp));
4038
4039 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4040 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4041 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4042 I40E_QINT_RQCTL_INTEVENT_MASK);
4043
4044 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4045 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4046
4047 wr32(hw, I40E_QINT_RQCTL(qp), val);
4048
4049 val = rd32(hw, I40E_QINT_TQCTL(qp));
4050
4051 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4052 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4053
4054 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4055 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4056 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4057 I40E_QINT_TQCTL_INTEVENT_MASK);
4058
4059 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4060 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4061
4062 wr32(hw, I40E_QINT_TQCTL(qp), val);
4063 qp = next;
4064 }
4065 }
4066 } else {
4067 free_irq(pf->pdev->irq, pf);
4068
4069 val = rd32(hw, I40E_PFINT_LNKLST0);
4070 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4071 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4072 val |= I40E_QUEUE_END_OF_LIST
4073 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4074 wr32(hw, I40E_PFINT_LNKLST0, val);
4075
4076 val = rd32(hw, I40E_QINT_RQCTL(qp));
4077 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4078 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4079 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4080 I40E_QINT_RQCTL_INTEVENT_MASK);
4081
4082 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4083 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4084
4085 wr32(hw, I40E_QINT_RQCTL(qp), val);
4086
4087 val = rd32(hw, I40E_QINT_TQCTL(qp));
4088
4089 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4090 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4091 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4092 I40E_QINT_TQCTL_INTEVENT_MASK);
4093
4094 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4095 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4096
4097 wr32(hw, I40E_QINT_TQCTL(qp), val);
4098 }
4099}
4100
4101/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004102 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4103 * @vsi: the VSI being configured
4104 * @v_idx: Index of vector to be freed
4105 *
4106 * This function frees the memory allocated to the q_vector. In addition if
4107 * NAPI is enabled it will delete any references to the NAPI struct prior
4108 * to freeing the q_vector.
4109 **/
4110static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4111{
4112 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004113 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004114
4115 if (!q_vector)
4116 return;
4117
4118 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004119 i40e_for_each_ring(ring, q_vector->tx)
4120 ring->q_vector = NULL;
4121
4122 i40e_for_each_ring(ring, q_vector->rx)
4123 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004124
4125 /* only VSI w/ an associated netdev is set up w/ NAPI */
4126 if (vsi->netdev)
4127 netif_napi_del(&q_vector->napi);
4128
4129 vsi->q_vectors[v_idx] = NULL;
4130
4131 kfree_rcu(q_vector, rcu);
4132}
4133
4134/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4136 * @vsi: the VSI being un-configured
4137 *
4138 * This frees the memory allocated to the q_vectors and
4139 * deletes references to the NAPI struct.
4140 **/
4141static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4142{
4143 int v_idx;
4144
Alexander Duyck493fb302013-09-28 07:01:44 +00004145 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4146 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004147}
4148
4149/**
4150 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4151 * @pf: board private structure
4152 **/
4153static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4154{
4155 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4156 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4157 pci_disable_msix(pf->pdev);
4158 kfree(pf->msix_entries);
4159 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004160 kfree(pf->irq_pile);
4161 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004162 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4163 pci_disable_msi(pf->pdev);
4164 }
4165 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4166}
4167
4168/**
4169 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4170 * @pf: board private structure
4171 *
4172 * We go through and clear interrupt specific resources and reset the structure
4173 * to pre-load conditions
4174 **/
4175static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4176{
4177 int i;
4178
Shannon Nelsone1477582015-02-21 06:44:33 +00004179 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004180 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004181 synchronize_irq(pf->msix_entries[0].vector);
4182 free_irq(pf->msix_entries[0].vector, pf);
4183 }
4184
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004185 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4186 I40E_IWARP_IRQ_PILE_ID);
4187
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004188 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004189 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004190 if (pf->vsi[i])
4191 i40e_vsi_free_q_vectors(pf->vsi[i]);
4192 i40e_reset_interrupt_capability(pf);
4193}
4194
4195/**
4196 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4197 * @vsi: the VSI being configured
4198 **/
4199static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4200{
4201 int q_idx;
4202
4203 if (!vsi->netdev)
4204 return;
4205
4206 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004207 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004208}
4209
4210/**
4211 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4212 * @vsi: the VSI being configured
4213 **/
4214static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4215{
4216 int q_idx;
4217
4218 if (!vsi->netdev)
4219 return;
4220
4221 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004222 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004223}
4224
4225/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004226 * i40e_vsi_close - Shut down a VSI
4227 * @vsi: the vsi to be quelled
4228 **/
4229static void i40e_vsi_close(struct i40e_vsi *vsi)
4230{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004231 bool reset = false;
4232
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004233 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4234 i40e_down(vsi);
4235 i40e_vsi_free_irq(vsi);
4236 i40e_vsi_free_tx_resources(vsi);
4237 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004238 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004239 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4240 reset = true;
4241 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004242}
4243
4244/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004245 * i40e_quiesce_vsi - Pause a given VSI
4246 * @vsi: the VSI being paused
4247 **/
4248static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4249{
4250 if (test_bit(__I40E_DOWN, &vsi->state))
4251 return;
4252
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004253 /* No need to disable FCoE VSI when Tx suspended */
4254 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4255 vsi->type == I40E_VSI_FCOE) {
4256 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004257 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004258 return;
4259 }
4260
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004261 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004262 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004263 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004264 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004265 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004266}
4267
4268/**
4269 * i40e_unquiesce_vsi - Resume a given VSI
4270 * @vsi: the VSI being resumed
4271 **/
4272static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4273{
4274 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4275 return;
4276
4277 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4278 if (vsi->netdev && netif_running(vsi->netdev))
4279 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4280 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004281 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004282}
4283
4284/**
4285 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4286 * @pf: the PF
4287 **/
4288static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4289{
4290 int v;
4291
Mitch Williams505682c2014-05-20 08:01:37 +00004292 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004293 if (pf->vsi[v])
4294 i40e_quiesce_vsi(pf->vsi[v]);
4295 }
4296}
4297
4298/**
4299 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4300 * @pf: the PF
4301 **/
4302static void i40e_pf_unquiesce_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_unquiesce_vsi(pf->vsi[v]);
4309 }
4310}
4311
Neerav Parikh69129dc2014-11-12 00:18:46 +00004312#ifdef CONFIG_I40E_DCB
4313/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004314 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004315 * @vsi: the VSI being configured
4316 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004317 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004318 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004319static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004320{
4321 struct i40e_pf *pf = vsi->back;
4322 int i, pf_q, ret;
4323
4324 pf_q = vsi->base_queue;
4325 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4326 /* Check and wait for the disable status of the queue */
4327 ret = i40e_pf_txq_wait(pf, pf_q, false);
4328 if (ret) {
4329 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004330 "VSI seid %d Tx ring %d disable timeout\n",
4331 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004332 return ret;
4333 }
4334 }
4335
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004336 pf_q = vsi->base_queue;
4337 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4338 /* Check and wait for the disable status of the queue */
4339 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4340 if (ret) {
4341 dev_info(&pf->pdev->dev,
4342 "VSI seid %d Rx ring %d disable timeout\n",
4343 vsi->seid, pf_q);
4344 return ret;
4345 }
4346 }
4347
Neerav Parikh69129dc2014-11-12 00:18:46 +00004348 return 0;
4349}
4350
4351/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004352 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004353 * @pf: the PF
4354 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004355 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004356 * VSIs that are managed by this PF.
4357 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004358static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004359{
4360 int v, ret = 0;
4361
4362 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004363 /* No need to wait for FCoE VSI queues */
4364 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004365 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004366 if (ret)
4367 break;
4368 }
4369 }
4370
4371 return ret;
4372}
4373
4374#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004375
4376/**
4377 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4378 * @q_idx: TX queue number
4379 * @vsi: Pointer to VSI struct
4380 *
4381 * This function checks specified queue for given VSI. Detects hung condition.
4382 * Sets hung bit since it is two step process. Before next run of service task
4383 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4384 * hung condition remain unchanged and during subsequent run, this function
4385 * issues SW interrupt to recover from hung condition.
4386 **/
4387static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4388{
4389 struct i40e_ring *tx_ring = NULL;
4390 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004391 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004392 int i;
4393
4394 pf = vsi->back;
4395
4396 /* now that we have an index, find the tx_ring struct */
4397 for (i = 0; i < vsi->num_queue_pairs; i++) {
4398 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4399 if (q_idx == vsi->tx_rings[i]->queue_index) {
4400 tx_ring = vsi->tx_rings[i];
4401 break;
4402 }
4403 }
4404 }
4405
4406 if (!tx_ring)
4407 return;
4408
4409 /* Read interrupt register */
4410 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4411 val = rd32(&pf->hw,
4412 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4413 tx_ring->vsi->base_vector - 1));
4414 else
4415 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4416
4417 head = i40e_get_head(tx_ring);
4418
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004419 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004420
Kiran Patil9c6c1252015-11-06 15:26:02 -08004421 /* HW is done executing descriptors, updated HEAD write back,
4422 * but SW hasn't processed those descriptors. If interrupt is
4423 * not generated from this point ON, it could result into
4424 * dev_watchdog detecting timeout on those netdev_queue,
4425 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004426 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004427 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004428 /* NAPI Poll didn't run and clear since it was set */
4429 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4430 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004431 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",
4432 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004433 tx_ring->next_to_clean, head,
4434 tx_ring->next_to_use,
4435 readl(tx_ring->tail));
4436 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4437 vsi->seid, q_idx, val);
4438 i40e_force_wb(vsi, tx_ring->q_vector);
4439 } else {
4440 /* First Chance - detected possible hung */
4441 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4442 &tx_ring->q_vector->hung_detected);
4443 }
4444 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004445
4446 /* This is the case where we have interrupts missing,
4447 * so the tx_pending in HW will most likely be 0, but we
4448 * will have tx_pending in SW since the WB happened but the
4449 * interrupt got lost.
4450 */
4451 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4452 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4453 if (napi_reschedule(&tx_ring->q_vector->napi))
4454 tx_ring->tx_stats.tx_lost_interrupt++;
4455 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004456}
4457
4458/**
4459 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4460 * @pf: pointer to PF struct
4461 *
4462 * LAN VSI has netdev and netdev has TX queues. This function is to check
4463 * each of those TX queues if they are hung, trigger recovery by issuing
4464 * SW interrupt.
4465 **/
4466static void i40e_detect_recover_hung(struct i40e_pf *pf)
4467{
4468 struct net_device *netdev;
4469 struct i40e_vsi *vsi;
4470 int i;
4471
4472 /* Only for LAN VSI */
4473 vsi = pf->vsi[pf->lan_vsi];
4474
4475 if (!vsi)
4476 return;
4477
4478 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4479 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4480 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4481 return;
4482
4483 /* Make sure type is MAIN VSI */
4484 if (vsi->type != I40E_VSI_MAIN)
4485 return;
4486
4487 netdev = vsi->netdev;
4488 if (!netdev)
4489 return;
4490
4491 /* Bail out if netif_carrier is not OK */
4492 if (!netif_carrier_ok(netdev))
4493 return;
4494
4495 /* Go thru' TX queues for netdev */
4496 for (i = 0; i < netdev->num_tx_queues; i++) {
4497 struct netdev_queue *q;
4498
4499 q = netdev_get_tx_queue(netdev, i);
4500 if (q)
4501 i40e_detect_recover_hung_queue(i, vsi);
4502 }
4503}
4504
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004505/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004506 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004507 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004508 *
4509 * Get TC map for ISCSI PF type that will include iSCSI TC
4510 * and LAN TC.
4511 **/
4512static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4513{
4514 struct i40e_dcb_app_priority_table app;
4515 struct i40e_hw *hw = &pf->hw;
4516 u8 enabled_tc = 1; /* TC0 is always enabled */
4517 u8 tc, i;
4518 /* Get the iSCSI APP TLV */
4519 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4520
4521 for (i = 0; i < dcbcfg->numapps; i++) {
4522 app = dcbcfg->app[i];
4523 if (app.selector == I40E_APP_SEL_TCPIP &&
4524 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4525 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004526 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004527 break;
4528 }
4529 }
4530
4531 return enabled_tc;
4532}
4533
4534/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004535 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4536 * @dcbcfg: the corresponding DCBx configuration structure
4537 *
4538 * Return the number of TCs from given DCBx configuration
4539 **/
4540static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4541{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004542 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004543 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004544 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004545
4546 /* Scan the ETS Config Priority Table to find
4547 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004548 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004549 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004550 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4551 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4552
4553 /* Now scan the bitmask to check for
4554 * contiguous TCs starting with TC0
4555 */
4556 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4557 if (num_tc & BIT(i)) {
4558 if (!tc_unused) {
4559 ret++;
4560 } else {
4561 pr_err("Non-contiguous TC - Disabling DCB\n");
4562 return 1;
4563 }
4564 } else {
4565 tc_unused = 1;
4566 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004567 }
4568
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004569 /* There is always at least TC0 */
4570 if (!ret)
4571 ret = 1;
4572
4573 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004574}
4575
4576/**
4577 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4578 * @dcbcfg: the corresponding DCBx configuration structure
4579 *
4580 * Query the current DCB configuration and return the number of
4581 * traffic classes enabled from the given DCBX config
4582 **/
4583static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4584{
4585 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4586 u8 enabled_tc = 1;
4587 u8 i;
4588
4589 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004590 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004591
4592 return enabled_tc;
4593}
4594
4595/**
4596 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4597 * @pf: PF being queried
4598 *
4599 * Return number of traffic classes enabled for the given PF
4600 **/
4601static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4602{
4603 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004604 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004605 u8 num_tc = 0;
4606 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4607
4608 /* If DCB is not enabled then always in single TC */
4609 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4610 return 1;
4611
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004612 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004613 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4614 return i40e_dcb_get_num_tc(dcbcfg);
4615
4616 /* MFP mode return count of enabled TCs for this PF */
4617 if (pf->hw.func_caps.iscsi)
4618 enabled_tc = i40e_get_iscsi_tc_map(pf);
4619 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004620 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004621
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004622 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004623 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004624 num_tc++;
4625 }
4626 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004627}
4628
4629/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004630 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4631 * @pf: PF being queried
4632 *
4633 * Return a bitmap for enabled traffic classes for this PF.
4634 **/
4635static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4636{
4637 /* If DCB is not enabled for this PF then just return default TC */
4638 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004639 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004640
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004641 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004642 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4643 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4644
Neerav Parikhfc51de92015-02-24 06:58:53 +00004645 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004646 if (pf->hw.func_caps.iscsi)
4647 return i40e_get_iscsi_tc_map(pf);
4648 else
David Ertmanea6acb72016-09-20 07:10:50 -07004649 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004650}
4651
4652/**
4653 * i40e_vsi_get_bw_info - Query VSI BW Information
4654 * @vsi: the VSI being queried
4655 *
4656 * Returns 0 on success, negative value on failure
4657 **/
4658static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4659{
4660 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4661 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4662 struct i40e_pf *pf = vsi->back;
4663 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004664 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004665 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004666 int i;
4667
4668 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004669 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4670 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004671 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004672 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4673 i40e_stat_str(&pf->hw, ret),
4674 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004675 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004676 }
4677
4678 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004679 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4680 NULL);
4681 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004682 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004683 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4684 i40e_stat_str(&pf->hw, ret),
4685 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004686 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004687 }
4688
4689 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4690 dev_info(&pf->pdev->dev,
4691 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4692 bw_config.tc_valid_bits,
4693 bw_ets_config.tc_valid_bits);
4694 /* Still continuing */
4695 }
4696
4697 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4698 vsi->bw_max_quanta = bw_config.max_bw;
4699 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4700 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4701 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4702 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4703 vsi->bw_ets_limit_credits[i] =
4704 le16_to_cpu(bw_ets_config.credits[i]);
4705 /* 3 bits out of 4 for each TC */
4706 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4707 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004708
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004709 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004710}
4711
4712/**
4713 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4714 * @vsi: the VSI being configured
4715 * @enabled_tc: TC bitmap
4716 * @bw_credits: BW shared credits per TC
4717 *
4718 * Returns 0 on success, negative value on failure
4719 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004720static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004721 u8 *bw_share)
4722{
4723 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004724 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004725 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004726
4727 bw_data.tc_valid_bits = enabled_tc;
4728 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4729 bw_data.tc_bw_credits[i] = bw_share[i];
4730
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004731 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4732 NULL);
4733 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004734 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004735 "AQ command Config VSI BW allocation per TC failed = %d\n",
4736 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004737 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004738 }
4739
4740 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4741 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4742
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004743 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004744}
4745
4746/**
4747 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4748 * @vsi: the VSI being configured
4749 * @enabled_tc: TC map to be enabled
4750 *
4751 **/
4752static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4753{
4754 struct net_device *netdev = vsi->netdev;
4755 struct i40e_pf *pf = vsi->back;
4756 struct i40e_hw *hw = &pf->hw;
4757 u8 netdev_tc = 0;
4758 int i;
4759 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4760
4761 if (!netdev)
4762 return;
4763
4764 if (!enabled_tc) {
4765 netdev_reset_tc(netdev);
4766 return;
4767 }
4768
4769 /* Set up actual enabled TCs on the VSI */
4770 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4771 return;
4772
4773 /* set per TC queues for the VSI */
4774 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4775 /* Only set TC queues for enabled tcs
4776 *
4777 * e.g. For a VSI that has TC0 and TC3 enabled the
4778 * enabled_tc bitmap would be 0x00001001; the driver
4779 * will set the numtc for netdev as 2 that will be
4780 * referenced by the netdev layer as TC 0 and 1.
4781 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004782 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004783 netdev_set_tc_queue(netdev,
4784 vsi->tc_config.tc_info[i].netdev_tc,
4785 vsi->tc_config.tc_info[i].qcount,
4786 vsi->tc_config.tc_info[i].qoffset);
4787 }
4788
4789 /* Assign UP2TC map for the VSI */
4790 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4791 /* Get the actual TC# for the UP */
4792 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4793 /* Get the mapped netdev TC# for the UP */
4794 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4795 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4796 }
4797}
4798
4799/**
4800 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4801 * @vsi: the VSI being configured
4802 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4803 **/
4804static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4805 struct i40e_vsi_context *ctxt)
4806{
4807 /* copy just the sections touched not the entire info
4808 * since not all sections are valid as returned by
4809 * update vsi params
4810 */
4811 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4812 memcpy(&vsi->info.queue_mapping,
4813 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4814 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4815 sizeof(vsi->info.tc_mapping));
4816}
4817
4818/**
4819 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4820 * @vsi: VSI to be configured
4821 * @enabled_tc: TC bitmap
4822 *
4823 * This configures a particular VSI for TCs that are mapped to the
4824 * given TC bitmap. It uses default bandwidth share for TCs across
4825 * VSIs to configure TC for a particular VSI.
4826 *
4827 * NOTE:
4828 * It is expected that the VSI queues have been quisced before calling
4829 * this function.
4830 **/
4831static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4832{
4833 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4834 struct i40e_vsi_context ctxt;
4835 int ret = 0;
4836 int i;
4837
4838 /* Check if enabled_tc is same as existing or new TCs */
4839 if (vsi->tc_config.enabled_tc == enabled_tc)
4840 return ret;
4841
4842 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4843 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004844 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004845 bw_share[i] = 1;
4846 }
4847
4848 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4849 if (ret) {
4850 dev_info(&vsi->back->pdev->dev,
4851 "Failed configuring TC map %d for VSI %d\n",
4852 enabled_tc, vsi->seid);
4853 goto out;
4854 }
4855
4856 /* Update Queue Pairs Mapping for currently enabled UPs */
4857 ctxt.seid = vsi->seid;
4858 ctxt.pf_num = vsi->back->hw.pf_id;
4859 ctxt.vf_num = 0;
4860 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004861 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004862 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4863
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004864 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4865 ctxt.info.valid_sections |=
4866 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4867 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4868 }
4869
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004870 /* Update the VSI after updating the VSI queue-mapping information */
4871 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4872 if (ret) {
4873 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004874 "Update vsi tc config failed, err %s aq_err %s\n",
4875 i40e_stat_str(&vsi->back->hw, ret),
4876 i40e_aq_str(&vsi->back->hw,
4877 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004878 goto out;
4879 }
4880 /* update the local VSI info with updated queue map */
4881 i40e_vsi_update_queue_map(vsi, &ctxt);
4882 vsi->info.valid_sections = 0;
4883
4884 /* Update current VSI BW information */
4885 ret = i40e_vsi_get_bw_info(vsi);
4886 if (ret) {
4887 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004888 "Failed updating vsi bw info, err %s aq_err %s\n",
4889 i40e_stat_str(&vsi->back->hw, ret),
4890 i40e_aq_str(&vsi->back->hw,
4891 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004892 goto out;
4893 }
4894
4895 /* Update the netdev TC setup */
4896 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4897out:
4898 return ret;
4899}
4900
4901/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004902 * i40e_veb_config_tc - Configure TCs for given VEB
4903 * @veb: given VEB
4904 * @enabled_tc: TC bitmap
4905 *
4906 * Configures given TC bitmap for VEB (switching) element
4907 **/
4908int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4909{
4910 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4911 struct i40e_pf *pf = veb->pf;
4912 int ret = 0;
4913 int i;
4914
4915 /* No TCs or already enabled TCs just return */
4916 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4917 return ret;
4918
4919 bw_data.tc_valid_bits = enabled_tc;
4920 /* bw_data.absolute_credits is not set (relative) */
4921
4922 /* Enable ETS TCs with equal BW Share for now */
4923 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004924 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004925 bw_data.tc_bw_share_credits[i] = 1;
4926 }
4927
4928 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4929 &bw_data, NULL);
4930 if (ret) {
4931 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004932 "VEB bw config failed, err %s aq_err %s\n",
4933 i40e_stat_str(&pf->hw, ret),
4934 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004935 goto out;
4936 }
4937
4938 /* Update the BW information */
4939 ret = i40e_veb_get_bw_info(veb);
4940 if (ret) {
4941 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004942 "Failed getting veb bw config, err %s aq_err %s\n",
4943 i40e_stat_str(&pf->hw, ret),
4944 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004945 }
4946
4947out:
4948 return ret;
4949}
4950
4951#ifdef CONFIG_I40E_DCB
4952/**
4953 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4954 * @pf: PF struct
4955 *
4956 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4957 * the caller would've quiesce all the VSIs before calling
4958 * this function
4959 **/
4960static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4961{
4962 u8 tc_map = 0;
4963 int ret;
4964 u8 v;
4965
4966 /* Enable the TCs available on PF to all VEBs */
4967 tc_map = i40e_pf_get_tc_map(pf);
4968 for (v = 0; v < I40E_MAX_VEB; v++) {
4969 if (!pf->veb[v])
4970 continue;
4971 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4972 if (ret) {
4973 dev_info(&pf->pdev->dev,
4974 "Failed configuring TC for VEB seid=%d\n",
4975 pf->veb[v]->seid);
4976 /* Will try to configure as many components */
4977 }
4978 }
4979
4980 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004981 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004982 if (!pf->vsi[v])
4983 continue;
4984
4985 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004986#ifdef I40E_FCOE
4987 * - For FCoE VSI only enable the TC configured
4988 * as per the APP TLV
4989#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004990 * - For all others keep them at TC0 for now
4991 */
4992 if (v == pf->lan_vsi)
4993 tc_map = i40e_pf_get_tc_map(pf);
4994 else
David Ertmanea6acb72016-09-20 07:10:50 -07004995 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07004996#ifdef I40E_FCOE
4997 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4998 tc_map = i40e_get_fcoe_tc_map(pf);
4999#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005000
5001 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5002 if (ret) {
5003 dev_info(&pf->pdev->dev,
5004 "Failed configuring TC for VSI seid=%d\n",
5005 pf->vsi[v]->seid);
5006 /* Will try to configure as many components */
5007 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005008 /* Re-configure VSI vectors based on updated TC map */
5009 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005010 if (pf->vsi[v]->netdev)
5011 i40e_dcbnl_set_all(pf->vsi[v]);
5012 }
5013 }
5014}
5015
5016/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005017 * i40e_resume_port_tx - Resume port Tx
5018 * @pf: PF struct
5019 *
5020 * Resume a port's Tx and issue a PF reset in case of failure to
5021 * resume.
5022 **/
5023static int i40e_resume_port_tx(struct i40e_pf *pf)
5024{
5025 struct i40e_hw *hw = &pf->hw;
5026 int ret;
5027
5028 ret = i40e_aq_resume_port_tx(hw, NULL);
5029 if (ret) {
5030 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005031 "Resume Port Tx failed, err %s aq_err %s\n",
5032 i40e_stat_str(&pf->hw, ret),
5033 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005034 /* Schedule PF reset to recover */
5035 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5036 i40e_service_event_schedule(pf);
5037 }
5038
5039 return ret;
5040}
5041
5042/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005043 * i40e_init_pf_dcb - Initialize DCB configuration
5044 * @pf: PF being configured
5045 *
5046 * Query the current DCB configuration and cache it
5047 * in the hardware structure
5048 **/
5049static int i40e_init_pf_dcb(struct i40e_pf *pf)
5050{
5051 struct i40e_hw *hw = &pf->hw;
5052 int err = 0;
5053
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005054 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005055 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005056 goto out;
5057
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005058 /* Get the initial DCB configuration */
5059 err = i40e_init_dcb(hw);
5060 if (!err) {
5061 /* Device/Function is not DCBX capable */
5062 if ((!hw->func_caps.dcb) ||
5063 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5064 dev_info(&pf->pdev->dev,
5065 "DCBX offload is not supported or is disabled for this PF.\n");
5066
5067 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5068 goto out;
5069
5070 } else {
5071 /* When status is not DISABLED then DCBX in FW */
5072 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5073 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005074
5075 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005076 /* Enable DCB tagging only when more than one TC
5077 * or explicitly disable if only one TC
5078 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005079 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5080 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005081 else
5082 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005083 dev_dbg(&pf->pdev->dev,
5084 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005085 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005086 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005087 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005088 "Query for DCB configuration failed, err %s aq_err %s\n",
5089 i40e_stat_str(&pf->hw, err),
5090 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005091 }
5092
5093out:
5094 return err;
5095}
5096#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005097#define SPEED_SIZE 14
5098#define FC_SIZE 8
5099/**
5100 * i40e_print_link_message - print link up or down
5101 * @vsi: the VSI for which link needs a message
5102 */
Matt Jaredc156f852015-08-27 11:42:39 -04005103void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005104{
Shannon Nelsona9165492015-09-03 17:19:00 -04005105 char *speed = "Unknown";
5106 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005107
Matt Jaredc156f852015-08-27 11:42:39 -04005108 if (vsi->current_isup == isup)
5109 return;
5110 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005111 if (!isup) {
5112 netdev_info(vsi->netdev, "NIC Link is Down\n");
5113 return;
5114 }
5115
Greg Rose148c2d82014-12-11 07:06:27 +00005116 /* Warn user if link speed on NPAR enabled partition is not at
5117 * least 10GB
5118 */
5119 if (vsi->back->hw.func_caps.npar_enable &&
5120 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5121 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5122 netdev_warn(vsi->netdev,
5123 "The partition detected link speed that is less than 10Gbps\n");
5124
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005125 switch (vsi->back->hw.phy.link_info.link_speed) {
5126 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005127 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005128 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005129 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005130 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005131 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005132 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005133 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005134 break;
5135 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005136 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005137 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005138 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005139 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005140 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005141 default:
5142 break;
5143 }
5144
5145 switch (vsi->back->hw.fc.current_mode) {
5146 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005147 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005148 break;
5149 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005150 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005151 break;
5152 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005153 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005154 break;
5155 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005156 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005157 break;
5158 }
5159
Shannon Nelsona9165492015-09-03 17:19:00 -04005160 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005161 speed, fc);
5162}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005163
5164/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005165 * i40e_up_complete - Finish the last steps of bringing up a connection
5166 * @vsi: the VSI being configured
5167 **/
5168static int i40e_up_complete(struct i40e_vsi *vsi)
5169{
5170 struct i40e_pf *pf = vsi->back;
5171 int err;
5172
5173 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5174 i40e_vsi_configure_msix(vsi);
5175 else
5176 i40e_configure_msi_and_legacy(vsi);
5177
5178 /* start rings */
5179 err = i40e_vsi_control_rings(vsi, true);
5180 if (err)
5181 return err;
5182
5183 clear_bit(__I40E_DOWN, &vsi->state);
5184 i40e_napi_enable_all(vsi);
5185 i40e_vsi_enable_irq(vsi);
5186
5187 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5188 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005189 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005190 netif_tx_start_all_queues(vsi->netdev);
5191 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005192 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005193 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005194 /* need to check for qualified module here*/
5195 if ((pf->hw.phy.link_info.link_info &
5196 I40E_AQ_MEDIA_AVAILABLE) &&
5197 (!(pf->hw.phy.link_info.an_info &
5198 I40E_AQ_QUALIFIED_MODULE)))
5199 netdev_err(vsi->netdev,
5200 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005201 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005202
5203 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005204 if (vsi->type == I40E_VSI_FDIR) {
5205 /* reset fd counters */
5206 pf->fd_add_err = pf->fd_atr_cnt = 0;
5207 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005208 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005209 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5210 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005211 pf->fd_tcp_rule = 0;
5212 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005213 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005214 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005215
5216 /* On the next run of the service_task, notify any clients of the new
5217 * opened netdev
5218 */
5219 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005220 i40e_service_event_schedule(pf);
5221
5222 return 0;
5223}
5224
5225/**
5226 * i40e_vsi_reinit_locked - Reset the VSI
5227 * @vsi: the VSI being configured
5228 *
5229 * Rebuild the ring structs after some configuration
5230 * has changed, e.g. MTU size.
5231 **/
5232static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5233{
5234 struct i40e_pf *pf = vsi->back;
5235
5236 WARN_ON(in_interrupt());
5237 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5238 usleep_range(1000, 2000);
5239 i40e_down(vsi);
5240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005241 i40e_up(vsi);
5242 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5243}
5244
5245/**
5246 * i40e_up - Bring the connection back up after being down
5247 * @vsi: the VSI being configured
5248 **/
5249int i40e_up(struct i40e_vsi *vsi)
5250{
5251 int err;
5252
5253 err = i40e_vsi_configure(vsi);
5254 if (!err)
5255 err = i40e_up_complete(vsi);
5256
5257 return err;
5258}
5259
5260/**
5261 * i40e_down - Shutdown the connection processing
5262 * @vsi: the VSI being stopped
5263 **/
5264void i40e_down(struct i40e_vsi *vsi)
5265{
5266 int i;
5267
5268 /* It is assumed that the caller of this function
5269 * sets the vsi->state __I40E_DOWN bit.
5270 */
5271 if (vsi->netdev) {
5272 netif_carrier_off(vsi->netdev);
5273 netif_tx_disable(vsi->netdev);
5274 }
5275 i40e_vsi_disable_irq(vsi);
5276 i40e_vsi_control_rings(vsi, false);
5277 i40e_napi_disable_all(vsi);
5278
5279 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005280 i40e_clean_tx_ring(vsi->tx_rings[i]);
5281 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005282 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005283
5284 i40e_notify_client_of_netdev_close(vsi, false);
5285
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005286}
5287
5288/**
5289 * i40e_setup_tc - configure multiple traffic classes
5290 * @netdev: net device to configure
5291 * @tc: number of traffic classes to enable
5292 **/
5293static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5294{
5295 struct i40e_netdev_priv *np = netdev_priv(netdev);
5296 struct i40e_vsi *vsi = np->vsi;
5297 struct i40e_pf *pf = vsi->back;
5298 u8 enabled_tc = 0;
5299 int ret = -EINVAL;
5300 int i;
5301
5302 /* Check if DCB enabled to continue */
5303 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5304 netdev_info(netdev, "DCB is not enabled for adapter\n");
5305 goto exit;
5306 }
5307
5308 /* Check if MFP enabled */
5309 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5310 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5311 goto exit;
5312 }
5313
5314 /* Check whether tc count is within enabled limit */
5315 if (tc > i40e_pf_get_num_tc(pf)) {
5316 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5317 goto exit;
5318 }
5319
5320 /* Generate TC map for number of tc requested */
5321 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005322 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005323
5324 /* Requesting same TC configuration as already enabled */
5325 if (enabled_tc == vsi->tc_config.enabled_tc)
5326 return 0;
5327
5328 /* Quiesce VSI queues */
5329 i40e_quiesce_vsi(vsi);
5330
5331 /* Configure VSI for enabled TCs */
5332 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5333 if (ret) {
5334 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5335 vsi->seid);
5336 goto exit;
5337 }
5338
5339 /* Unquiesce VSI */
5340 i40e_unquiesce_vsi(vsi);
5341
5342exit:
5343 return ret;
5344}
5345
John Fastabende4c67342016-02-16 21:16:15 -08005346#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005347int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5348 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005349#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005350static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5351 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005352#endif
5353{
John Fastabend16e5cc62016-02-16 21:16:43 -08005354 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005355 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005356 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005357}
5358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005359/**
5360 * i40e_open - Called when a network interface is made active
5361 * @netdev: network interface device structure
5362 *
5363 * The open entry point is called when a network interface is made
5364 * active by the system (IFF_UP). At this point all resources needed
5365 * for transmit and receive operations are allocated, the interrupt
5366 * handler is registered with the OS, the netdev watchdog subtask is
5367 * enabled, and the stack is notified that the interface is ready.
5368 *
5369 * Returns 0 on success, negative value on failure
5370 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005371int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005372{
5373 struct i40e_netdev_priv *np = netdev_priv(netdev);
5374 struct i40e_vsi *vsi = np->vsi;
5375 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376 int err;
5377
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005378 /* disallow open during test or if eeprom is broken */
5379 if (test_bit(__I40E_TESTING, &pf->state) ||
5380 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005381 return -EBUSY;
5382
5383 netif_carrier_off(netdev);
5384
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005385 err = i40e_vsi_open(vsi);
5386 if (err)
5387 return err;
5388
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005389 /* configure global TSO hardware offload settings */
5390 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5391 TCP_FLAG_FIN) >> 16);
5392 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5393 TCP_FLAG_FIN |
5394 TCP_FLAG_CWR) >> 16);
5395 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5396
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005397 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005398
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005399 return 0;
5400}
5401
5402/**
5403 * i40e_vsi_open -
5404 * @vsi: the VSI to open
5405 *
5406 * Finish initialization of the VSI.
5407 *
5408 * Returns 0 on success, negative value on failure
5409 **/
5410int i40e_vsi_open(struct i40e_vsi *vsi)
5411{
5412 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005413 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005414 int err;
5415
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005416 /* allocate descriptors */
5417 err = i40e_vsi_setup_tx_resources(vsi);
5418 if (err)
5419 goto err_setup_tx;
5420 err = i40e_vsi_setup_rx_resources(vsi);
5421 if (err)
5422 goto err_setup_rx;
5423
5424 err = i40e_vsi_configure(vsi);
5425 if (err)
5426 goto err_setup_rx;
5427
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005428 if (vsi->netdev) {
5429 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5430 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5431 err = i40e_vsi_request_irq(vsi, int_name);
5432 if (err)
5433 goto err_setup_rx;
5434
5435 /* Notify the stack of the actual queue counts. */
5436 err = netif_set_real_num_tx_queues(vsi->netdev,
5437 vsi->num_queue_pairs);
5438 if (err)
5439 goto err_set_queues;
5440
5441 err = netif_set_real_num_rx_queues(vsi->netdev,
5442 vsi->num_queue_pairs);
5443 if (err)
5444 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005445
5446 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005447 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005448 dev_driver_string(&pf->pdev->dev),
5449 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005450 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005451
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005452 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005453 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005454 goto err_setup_rx;
5455 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005456
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005457 err = i40e_up_complete(vsi);
5458 if (err)
5459 goto err_up_complete;
5460
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005461 return 0;
5462
5463err_up_complete:
5464 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005465err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005466 i40e_vsi_free_irq(vsi);
5467err_setup_rx:
5468 i40e_vsi_free_rx_resources(vsi);
5469err_setup_tx:
5470 i40e_vsi_free_tx_resources(vsi);
5471 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005472 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005473
5474 return err;
5475}
5476
5477/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005478 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005479 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005480 *
5481 * This function destroys the hlist where all the Flow Director
5482 * filters were saved.
5483 **/
5484static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5485{
5486 struct i40e_fdir_filter *filter;
5487 struct hlist_node *node2;
5488
5489 hlist_for_each_entry_safe(filter, node2,
5490 &pf->fdir_filter_list, fdir_node) {
5491 hlist_del(&filter->fdir_node);
5492 kfree(filter);
5493 }
5494 pf->fdir_pf_active_filters = 0;
5495}
5496
5497/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005498 * i40e_close - Disables a network interface
5499 * @netdev: network interface device structure
5500 *
5501 * The close entry point is called when an interface is de-activated
5502 * by the OS. The hardware is still under the driver's control, but
5503 * this netdev interface is disabled.
5504 *
5505 * Returns 0, this is not allowed to fail
5506 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005507int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508{
5509 struct i40e_netdev_priv *np = netdev_priv(netdev);
5510 struct i40e_vsi *vsi = np->vsi;
5511
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005512 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005513
5514 return 0;
5515}
5516
5517/**
5518 * i40e_do_reset - Start a PF or Core Reset sequence
5519 * @pf: board private structure
5520 * @reset_flags: which reset is requested
5521 *
5522 * The essential difference in resets is that the PF Reset
5523 * doesn't clear the packet buffers, doesn't reset the PE
5524 * firmware, and doesn't bother the other PFs on the chip.
5525 **/
5526void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5527{
5528 u32 val;
5529
5530 WARN_ON(in_interrupt());
5531
Mitch Williams263fc482014-04-23 04:50:11 +00005532
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005533 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005534 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005535
5536 /* Request a Global Reset
5537 *
5538 * This will start the chip's countdown to the actual full
5539 * chip reset event, and a warning interrupt to be sent
5540 * to all PFs, including the requestor. Our handler
5541 * for the warning interrupt will deal with the shutdown
5542 * and recovery of the switch setup.
5543 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005544 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005545 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5546 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5547 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5548
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005549 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005550
5551 /* Request a Core Reset
5552 *
5553 * Same as Global Reset, except does *not* include the MAC/PHY
5554 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005555 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005556 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5557 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5558 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5559 i40e_flush(&pf->hw);
5560
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005561 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005562
5563 /* Request a PF Reset
5564 *
5565 * Resets only the PF-specific registers
5566 *
5567 * This goes directly to the tear-down and rebuild of
5568 * the switch, since we need to do all the recovery as
5569 * for the Core Reset.
5570 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005571 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005572 i40e_handle_reset_warning(pf);
5573
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005574 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005575 int v;
5576
5577 /* Find the VSI(s) that requested a re-init */
5578 dev_info(&pf->pdev->dev,
5579 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005580 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005581 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005582
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005583 if (vsi != NULL &&
5584 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5585 i40e_vsi_reinit_locked(pf->vsi[v]);
5586 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5587 }
5588 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005589 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005590 int v;
5591
5592 /* Find the VSI(s) that needs to be brought down */
5593 dev_info(&pf->pdev->dev, "VSI down requested\n");
5594 for (v = 0; v < pf->num_alloc_vsi; v++) {
5595 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005596
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005597 if (vsi != NULL &&
5598 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5599 set_bit(__I40E_DOWN, &vsi->state);
5600 i40e_down(vsi);
5601 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5602 }
5603 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005604 } else {
5605 dev_info(&pf->pdev->dev,
5606 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005607 }
5608}
5609
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005610#ifdef CONFIG_I40E_DCB
5611/**
5612 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5613 * @pf: board private structure
5614 * @old_cfg: current DCB config
5615 * @new_cfg: new DCB config
5616 **/
5617bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5618 struct i40e_dcbx_config *old_cfg,
5619 struct i40e_dcbx_config *new_cfg)
5620{
5621 bool need_reconfig = false;
5622
5623 /* Check if ETS configuration has changed */
5624 if (memcmp(&new_cfg->etscfg,
5625 &old_cfg->etscfg,
5626 sizeof(new_cfg->etscfg))) {
5627 /* If Priority Table has changed reconfig is needed */
5628 if (memcmp(&new_cfg->etscfg.prioritytable,
5629 &old_cfg->etscfg.prioritytable,
5630 sizeof(new_cfg->etscfg.prioritytable))) {
5631 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005632 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005633 }
5634
5635 if (memcmp(&new_cfg->etscfg.tcbwtable,
5636 &old_cfg->etscfg.tcbwtable,
5637 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005638 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005639
5640 if (memcmp(&new_cfg->etscfg.tsatable,
5641 &old_cfg->etscfg.tsatable,
5642 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005643 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005644 }
5645
5646 /* Check if PFC configuration has changed */
5647 if (memcmp(&new_cfg->pfc,
5648 &old_cfg->pfc,
5649 sizeof(new_cfg->pfc))) {
5650 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005651 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005652 }
5653
5654 /* Check if APP Table has changed */
5655 if (memcmp(&new_cfg->app,
5656 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005657 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005658 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005659 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005660 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005661
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005662 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005663 return need_reconfig;
5664}
5665
5666/**
5667 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5668 * @pf: board private structure
5669 * @e: event info posted on ARQ
5670 **/
5671static int i40e_handle_lldp_event(struct i40e_pf *pf,
5672 struct i40e_arq_event_info *e)
5673{
5674 struct i40e_aqc_lldp_get_mib *mib =
5675 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5676 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005677 struct i40e_dcbx_config tmp_dcbx_cfg;
5678 bool need_reconfig = false;
5679 int ret = 0;
5680 u8 type;
5681
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005682 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005683 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005684 return ret;
5685
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005686 /* Ignore if event is not for Nearest Bridge */
5687 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5688 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005689 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005690 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5691 return ret;
5692
5693 /* Check MIB Type and return if event for Remote MIB update */
5694 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005695 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005696 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005697 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5698 /* Update the remote cached instance and return */
5699 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5700 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5701 &hw->remote_dcbx_config);
5702 goto exit;
5703 }
5704
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005705 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005706 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005707
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005708 /* Reset the old DCBx configuration data */
5709 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005710 /* Get updated DCBX data from firmware */
5711 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005712 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005713 dev_info(&pf->pdev->dev,
5714 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5715 i40e_stat_str(&pf->hw, ret),
5716 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005717 goto exit;
5718 }
5719
5720 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005721 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5722 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005723 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005724 goto exit;
5725 }
5726
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005727 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5728 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005729
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005730 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005731
5732 if (!need_reconfig)
5733 goto exit;
5734
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005735 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005736 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005737 pf->flags |= I40E_FLAG_DCB_ENABLED;
5738 else
5739 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5740
Neerav Parikh69129dc2014-11-12 00:18:46 +00005741 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005742 /* Reconfiguration needed quiesce all VSIs */
5743 i40e_pf_quiesce_all_vsi(pf);
5744
5745 /* Changes in configuration update VEB/VSI */
5746 i40e_dcb_reconfigure(pf);
5747
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005748 ret = i40e_resume_port_tx(pf);
5749
Neerav Parikh69129dc2014-11-12 00:18:46 +00005750 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005751 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005752 if (ret)
5753 goto exit;
5754
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005755 /* Wait for the PF's queues to be disabled */
5756 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005757 if (ret) {
5758 /* Schedule PF reset to recover */
5759 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5760 i40e_service_event_schedule(pf);
5761 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005762 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005763 /* Notify the client for the DCB changes */
5764 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005765 }
5766
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005767exit:
5768 return ret;
5769}
5770#endif /* CONFIG_I40E_DCB */
5771
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005772/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005773 * i40e_do_reset_safe - Protected reset path for userland calls.
5774 * @pf: board private structure
5775 * @reset_flags: which reset is requested
5776 *
5777 **/
5778void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5779{
5780 rtnl_lock();
5781 i40e_do_reset(pf, reset_flags);
5782 rtnl_unlock();
5783}
5784
5785/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005786 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5787 * @pf: board private structure
5788 * @e: event info posted on ARQ
5789 *
5790 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5791 * and VF queues
5792 **/
5793static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5794 struct i40e_arq_event_info *e)
5795{
5796 struct i40e_aqc_lan_overflow *data =
5797 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5798 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5799 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5800 struct i40e_hw *hw = &pf->hw;
5801 struct i40e_vf *vf;
5802 u16 vf_id;
5803
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005804 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5805 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005806
5807 /* Queue belongs to VF, find the VF and issue VF reset */
5808 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5809 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5810 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5811 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5812 vf_id -= hw->func_caps.vf_base_id;
5813 vf = &pf->vf[vf_id];
5814 i40e_vc_notify_vf_reset(vf);
5815 /* Allow VF to process pending reset notification */
5816 msleep(20);
5817 i40e_reset_vf(vf, false);
5818 }
5819}
5820
5821/**
5822 * i40e_service_event_complete - Finish up the service event
5823 * @pf: board private structure
5824 **/
5825static void i40e_service_event_complete(struct i40e_pf *pf)
5826{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005827 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005828
5829 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005830 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005831 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5832}
5833
5834/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005835 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5836 * @pf: board private structure
5837 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005838u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005839{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005840 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005841
5842 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5843 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5844 return fcnt_prog;
5845}
5846
5847/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005848 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005849 * @pf: board private structure
5850 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005851u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005852{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005853 u32 val, fcnt_prog;
5854
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005855 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5856 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5857 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5858 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5859 return fcnt_prog;
5860}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005861
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005862/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005863 * i40e_get_global_fd_count - Get total FD filters programmed on device
5864 * @pf: board private structure
5865 **/
5866u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5867{
5868 u32 val, fcnt_prog;
5869
5870 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5871 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5872 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5873 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5874 return fcnt_prog;
5875}
5876
5877/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005878 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5879 * @pf: board private structure
5880 **/
5881void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5882{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005883 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005884 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005885 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005886
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005887 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5888 return;
5889
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005890 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5891 * to re-enable
5892 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005893 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005894 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005895 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5896 (pf->fd_add_err == 0) ||
5897 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005898 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5899 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5900 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005901 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5902 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 +00005903 }
5904 }
Jacob Kellera3417d22016-09-06 18:05:10 -07005905
5906 /* Wait for some more space to be available to turn on ATR. We also
5907 * must check that no existing ntuple rules for TCP are in effect
5908 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005909 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5910 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07005911 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
5912 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005913 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005914 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07005915 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 +00005916 }
5917 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005918
5919 /* if hw had a problem adding a filter, delete it */
5920 if (pf->fd_inv > 0) {
5921 hlist_for_each_entry_safe(filter, node,
5922 &pf->fdir_filter_list, fdir_node) {
5923 if (filter->fd_id == pf->fd_inv) {
5924 hlist_del(&filter->fdir_node);
5925 kfree(filter);
5926 pf->fdir_pf_active_filters--;
5927 }
5928 }
5929 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005930}
5931
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005932#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005933#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005934/**
5935 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5936 * @pf: board private structure
5937 **/
5938static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5939{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005940 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005941 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005942 bool disable_atr = false;
5943 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005944 int reg;
5945
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005946 if (!time_after(jiffies, pf->fd_flush_timestamp +
5947 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5948 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005949
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005950 /* If the flush is happening too quick and we have mostly SB rules we
5951 * should not re-enable ATR for some time.
5952 */
5953 min_flush_time = pf->fd_flush_timestamp +
5954 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5955 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005956
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005957 if (!(time_after(jiffies, min_flush_time)) &&
5958 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5959 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5960 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5961 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005962 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005963
5964 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07005965 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005966 /* flush all filters */
5967 wr32(&pf->hw, I40E_PFQF_CTL_1,
5968 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5969 i40e_flush(&pf->hw);
5970 pf->fd_flush_cnt++;
5971 pf->fd_add_err = 0;
5972 do {
5973 /* Check FD flush status every 5-6msec */
5974 usleep_range(5000, 6000);
5975 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5976 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5977 break;
5978 } while (flush_wait_retry--);
5979 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5980 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5981 } else {
5982 /* replay sideband filters */
5983 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5984 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07005985 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005986 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5987 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5988 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5989 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005990}
5991
5992/**
5993 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5994 * @pf: board private structure
5995 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005996u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005997{
5998 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5999}
6000
6001/* We can see up to 256 filter programming desc in transit if the filters are
6002 * being applied really fast; before we see the first
6003 * filter miss error on Rx queue 0. Accumulating enough error messages before
6004 * reacting will make sure we don't cause flush too often.
6005 */
6006#define I40E_MAX_FD_PROGRAM_ERROR 256
6007
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006008/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006009 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6010 * @pf: board private structure
6011 **/
6012static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6013{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006014
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006015 /* if interface is down do nothing */
6016 if (test_bit(__I40E_DOWN, &pf->state))
6017 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006018
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006019 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006020 i40e_fdir_flush_and_replay(pf);
6021
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006022 i40e_fdir_check_and_reenable(pf);
6023
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006024}
6025
6026/**
6027 * i40e_vsi_link_event - notify VSI of a link event
6028 * @vsi: vsi to be notified
6029 * @link_up: link up or down
6030 **/
6031static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6032{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006033 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006034 return;
6035
6036 switch (vsi->type) {
6037 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006038#ifdef I40E_FCOE
6039 case I40E_VSI_FCOE:
6040#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006041 if (!vsi->netdev || !vsi->netdev_registered)
6042 break;
6043
6044 if (link_up) {
6045 netif_carrier_on(vsi->netdev);
6046 netif_tx_wake_all_queues(vsi->netdev);
6047 } else {
6048 netif_carrier_off(vsi->netdev);
6049 netif_tx_stop_all_queues(vsi->netdev);
6050 }
6051 break;
6052
6053 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006054 case I40E_VSI_VMDQ2:
6055 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006056 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006057 case I40E_VSI_MIRROR:
6058 default:
6059 /* there is no notification for other VSIs */
6060 break;
6061 }
6062}
6063
6064/**
6065 * i40e_veb_link_event - notify elements on the veb of a link event
6066 * @veb: veb to be notified
6067 * @link_up: link up or down
6068 **/
6069static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6070{
6071 struct i40e_pf *pf;
6072 int i;
6073
6074 if (!veb || !veb->pf)
6075 return;
6076 pf = veb->pf;
6077
6078 /* depth first... */
6079 for (i = 0; i < I40E_MAX_VEB; i++)
6080 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6081 i40e_veb_link_event(pf->veb[i], link_up);
6082
6083 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006084 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006085 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6086 i40e_vsi_link_event(pf->vsi[i], link_up);
6087}
6088
6089/**
6090 * i40e_link_event - Update netif_carrier status
6091 * @pf: board private structure
6092 **/
6093static void i40e_link_event(struct i40e_pf *pf)
6094{
Mitch Williams320684c2014-10-17 03:14:43 +00006095 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006096 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006097 i40e_status status;
6098 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006099
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006100 /* save off old link status information */
6101 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6102
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006103 /* set this to force the get_link_status call to refresh state */
6104 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006105
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006106 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006107
6108 status = i40e_get_link_status(&pf->hw, &new_link);
6109 if (status) {
6110 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6111 status);
6112 return;
6113 }
6114
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006115 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6116 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006117
6118 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006119 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006120 (test_bit(__I40E_DOWN, &vsi->state) ||
6121 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006122 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006123
6124 if (!test_bit(__I40E_DOWN, &vsi->state))
6125 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006126
6127 /* Notify the base of the switch tree connected to
6128 * the link. Floating VEBs are not notified.
6129 */
6130 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6131 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6132 else
Mitch Williams320684c2014-10-17 03:14:43 +00006133 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006134
6135 if (pf->vf)
6136 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006137
6138 if (pf->flags & I40E_FLAG_PTP)
6139 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006140}
6141
6142/**
Shannon Nelson21536712014-10-25 10:35:25 +00006143 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006144 * @pf: board private structure
6145 **/
6146static void i40e_watchdog_subtask(struct i40e_pf *pf)
6147{
6148 int i;
6149
6150 /* if interface is down do nothing */
6151 if (test_bit(__I40E_DOWN, &pf->state) ||
6152 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6153 return;
6154
Shannon Nelson21536712014-10-25 10:35:25 +00006155 /* make sure we don't do these things too often */
6156 if (time_before(jiffies, (pf->service_timer_previous +
6157 pf->service_timer_period)))
6158 return;
6159 pf->service_timer_previous = jiffies;
6160
Shannon Nelson9ac77262015-08-27 11:42:40 -04006161 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6162 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006163
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006164 /* Update the stats for active netdevs so the network stack
6165 * can look at updated numbers whenever it cares to
6166 */
Mitch Williams505682c2014-05-20 08:01:37 +00006167 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006168 if (pf->vsi[i] && pf->vsi[i]->netdev)
6169 i40e_update_stats(pf->vsi[i]);
6170
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006171 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6172 /* Update the stats for the active switching components */
6173 for (i = 0; i < I40E_MAX_VEB; i++)
6174 if (pf->veb[i])
6175 i40e_update_veb_stats(pf->veb[i]);
6176 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006177
6178 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006179}
6180
6181/**
6182 * i40e_reset_subtask - Set up for resetting the device and driver
6183 * @pf: board private structure
6184 **/
6185static void i40e_reset_subtask(struct i40e_pf *pf)
6186{
6187 u32 reset_flags = 0;
6188
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006189 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006190 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006191 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006192 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6193 }
6194 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006195 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006196 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6197 }
6198 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006199 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006200 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6201 }
6202 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006203 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006204 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6205 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006206 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006207 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006208 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6209 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006210
6211 /* If there's a recovery already waiting, it takes
6212 * precedence before starting a new reset sequence.
6213 */
6214 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6215 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006216 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006217 }
6218
6219 /* If we're already down or resetting, just bail */
6220 if (reset_flags &&
6221 !test_bit(__I40E_DOWN, &pf->state) &&
6222 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6223 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006224
6225unlock:
6226 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006227}
6228
6229/**
6230 * i40e_handle_link_event - Handle link event
6231 * @pf: board private structure
6232 * @e: event info posted on ARQ
6233 **/
6234static void i40e_handle_link_event(struct i40e_pf *pf,
6235 struct i40e_arq_event_info *e)
6236{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006237 struct i40e_aqc_get_link_status *status =
6238 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006239
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006240 /* Do a new status request to re-enable LSE reporting
6241 * and load new status information into the hw struct
6242 * This completely ignores any state information
6243 * in the ARQ event info, instead choosing to always
6244 * issue the AQ update link status command.
6245 */
6246 i40e_link_event(pf);
6247
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006248 /* check for unqualified module, if link is down */
6249 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6250 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6251 (!(status->link_info & I40E_AQ_LINK_UP)))
6252 dev_err(&pf->pdev->dev,
6253 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254}
6255
6256/**
6257 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6258 * @pf: board private structure
6259 **/
6260static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6261{
6262 struct i40e_arq_event_info event;
6263 struct i40e_hw *hw = &pf->hw;
6264 u16 pending, i = 0;
6265 i40e_status ret;
6266 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006267 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006268 u32 val;
6269
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006270 /* Do not run clean AQ when PF reset fails */
6271 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6272 return;
6273
Shannon Nelson86df2422014-05-20 08:01:35 +00006274 /* check for error indications */
6275 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6276 oldval = val;
6277 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006278 if (hw->debug_mask & I40E_DEBUG_AQ)
6279 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006280 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6281 }
6282 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006283 if (hw->debug_mask & I40E_DEBUG_AQ)
6284 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006285 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006286 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006287 }
6288 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006289 if (hw->debug_mask & I40E_DEBUG_AQ)
6290 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006291 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6292 }
6293 if (oldval != val)
6294 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6295
6296 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6297 oldval = val;
6298 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006299 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6300 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006301 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6302 }
6303 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006304 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6305 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006306 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6307 }
6308 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006309 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6310 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006311 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6312 }
6313 if (oldval != val)
6314 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6315
Mitch Williams1001dc32014-11-11 20:02:19 +00006316 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6317 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318 if (!event.msg_buf)
6319 return;
6320
6321 do {
6322 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006323 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006324 break;
Mitch Williams56497972014-06-04 08:45:18 +00006325 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006326 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6327 break;
6328 }
6329
6330 opcode = le16_to_cpu(event.desc.opcode);
6331 switch (opcode) {
6332
6333 case i40e_aqc_opc_get_link_status:
6334 i40e_handle_link_event(pf, &event);
6335 break;
6336 case i40e_aqc_opc_send_msg_to_pf:
6337 ret = i40e_vc_process_vf_msg(pf,
6338 le16_to_cpu(event.desc.retval),
6339 le32_to_cpu(event.desc.cookie_high),
6340 le32_to_cpu(event.desc.cookie_low),
6341 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006342 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006343 break;
6344 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006345 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006346#ifdef CONFIG_I40E_DCB
6347 rtnl_lock();
6348 ret = i40e_handle_lldp_event(pf, &event);
6349 rtnl_unlock();
6350#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006351 break;
6352 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006353 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006354 i40e_handle_lan_overflow_event(pf, &event);
6355 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006356 case i40e_aqc_opc_send_msg_to_peer:
6357 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6358 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006359 case i40e_aqc_opc_nvm_erase:
6360 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006361 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006362 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6363 "ARQ NVM operation 0x%04x completed\n",
6364 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006365 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006366 default:
6367 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006368 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006369 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006370 break;
6371 }
6372 } while (pending && (i++ < pf->adminq_work_limit));
6373
6374 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6375 /* re-enable Admin queue interrupt cause */
6376 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6377 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6378 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6379 i40e_flush(hw);
6380
6381 kfree(event.msg_buf);
6382}
6383
6384/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006385 * i40e_verify_eeprom - make sure eeprom is good to use
6386 * @pf: board private structure
6387 **/
6388static void i40e_verify_eeprom(struct i40e_pf *pf)
6389{
6390 int err;
6391
6392 err = i40e_diag_eeprom_test(&pf->hw);
6393 if (err) {
6394 /* retry in case of garbage read */
6395 err = i40e_diag_eeprom_test(&pf->hw);
6396 if (err) {
6397 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6398 err);
6399 set_bit(__I40E_BAD_EEPROM, &pf->state);
6400 }
6401 }
6402
6403 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6404 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6405 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6406 }
6407}
6408
6409/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006410 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006411 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006412 *
6413 * enable switch loop back or die - no point in a return value
6414 **/
6415static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6416{
6417 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6418 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006419 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006420
6421 ctxt.seid = pf->main_vsi_seid;
6422 ctxt.pf_num = pf->hw.pf_id;
6423 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006424 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6425 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006426 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006427 "couldn't get PF vsi config, err %s aq_err %s\n",
6428 i40e_stat_str(&pf->hw, ret),
6429 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006430 return;
6431 }
6432 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6433 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6434 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6435
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006436 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6437 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006438 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006439 "update vsi switch failed, err %s aq_err %s\n",
6440 i40e_stat_str(&pf->hw, ret),
6441 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006442 }
6443}
6444
6445/**
6446 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006447 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006448 *
6449 * disable switch loop back or die - no point in a return value
6450 **/
6451static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6452{
6453 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6454 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006455 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006456
6457 ctxt.seid = pf->main_vsi_seid;
6458 ctxt.pf_num = pf->hw.pf_id;
6459 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006460 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6461 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006462 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006463 "couldn't get PF vsi config, err %s aq_err %s\n",
6464 i40e_stat_str(&pf->hw, ret),
6465 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006466 return;
6467 }
6468 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6469 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6470 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6471
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006472 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6473 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006474 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006475 "update vsi switch failed, err %s aq_err %s\n",
6476 i40e_stat_str(&pf->hw, ret),
6477 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006478 }
6479}
6480
6481/**
Neerav Parikh51616012015-02-06 08:52:14 +00006482 * i40e_config_bridge_mode - Configure the HW bridge mode
6483 * @veb: pointer to the bridge instance
6484 *
6485 * Configure the loop back mode for the LAN VSI that is downlink to the
6486 * specified HW bridge instance. It is expected this function is called
6487 * when a new HW bridge is instantiated.
6488 **/
6489static void i40e_config_bridge_mode(struct i40e_veb *veb)
6490{
6491 struct i40e_pf *pf = veb->pf;
6492
Shannon Nelson6dec1012015-09-28 14:12:30 -04006493 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6494 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6495 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006496 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6497 i40e_disable_pf_switch_lb(pf);
6498 else
6499 i40e_enable_pf_switch_lb(pf);
6500}
6501
6502/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006503 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6504 * @veb: pointer to the VEB instance
6505 *
6506 * This is a recursive function that first builds the attached VSIs then
6507 * recurses in to build the next layer of VEB. We track the connections
6508 * through our own index numbers because the seid's from the HW could
6509 * change across the reset.
6510 **/
6511static int i40e_reconstitute_veb(struct i40e_veb *veb)
6512{
6513 struct i40e_vsi *ctl_vsi = NULL;
6514 struct i40e_pf *pf = veb->pf;
6515 int v, veb_idx;
6516 int ret;
6517
6518 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006519 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006520 if (pf->vsi[v] &&
6521 pf->vsi[v]->veb_idx == veb->idx &&
6522 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6523 ctl_vsi = pf->vsi[v];
6524 break;
6525 }
6526 }
6527 if (!ctl_vsi) {
6528 dev_info(&pf->pdev->dev,
6529 "missing owner VSI for veb_idx %d\n", veb->idx);
6530 ret = -ENOENT;
6531 goto end_reconstitute;
6532 }
6533 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6534 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6535 ret = i40e_add_vsi(ctl_vsi);
6536 if (ret) {
6537 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006538 "rebuild of veb_idx %d owner VSI failed: %d\n",
6539 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006540 goto end_reconstitute;
6541 }
6542 i40e_vsi_reset_stats(ctl_vsi);
6543
6544 /* create the VEB in the switch and move the VSI onto the VEB */
6545 ret = i40e_add_veb(veb, ctl_vsi);
6546 if (ret)
6547 goto end_reconstitute;
6548
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006549 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6550 veb->bridge_mode = BRIDGE_MODE_VEB;
6551 else
6552 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006553 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006554
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006555 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006556 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006557 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6558 continue;
6559
6560 if (pf->vsi[v]->veb_idx == veb->idx) {
6561 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006562
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006563 vsi->uplink_seid = veb->seid;
6564 ret = i40e_add_vsi(vsi);
6565 if (ret) {
6566 dev_info(&pf->pdev->dev,
6567 "rebuild of vsi_idx %d failed: %d\n",
6568 v, ret);
6569 goto end_reconstitute;
6570 }
6571 i40e_vsi_reset_stats(vsi);
6572 }
6573 }
6574
6575 /* create any VEBs attached to this VEB - RECURSION */
6576 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6577 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6578 pf->veb[veb_idx]->uplink_seid = veb->seid;
6579 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6580 if (ret)
6581 break;
6582 }
6583 }
6584
6585end_reconstitute:
6586 return ret;
6587}
6588
6589/**
6590 * i40e_get_capabilities - get info about the HW
6591 * @pf: the PF struct
6592 **/
6593static int i40e_get_capabilities(struct i40e_pf *pf)
6594{
6595 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6596 u16 data_size;
6597 int buf_len;
6598 int err;
6599
6600 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6601 do {
6602 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6603 if (!cap_buf)
6604 return -ENOMEM;
6605
6606 /* this loads the data into the hw struct for us */
6607 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6608 &data_size,
6609 i40e_aqc_opc_list_func_capabilities,
6610 NULL);
6611 /* data loaded, buffer no longer needed */
6612 kfree(cap_buf);
6613
6614 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6615 /* retry with a larger buffer */
6616 buf_len = data_size;
6617 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6618 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006619 "capability discovery failed, err %s aq_err %s\n",
6620 i40e_stat_str(&pf->hw, err),
6621 i40e_aq_str(&pf->hw,
6622 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006623 return -ENODEV;
6624 }
6625 } while (err);
6626
6627 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6628 dev_info(&pf->pdev->dev,
6629 "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",
6630 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6631 pf->hw.func_caps.num_msix_vectors,
6632 pf->hw.func_caps.num_msix_vectors_vf,
6633 pf->hw.func_caps.fd_filters_guaranteed,
6634 pf->hw.func_caps.fd_filters_best_effort,
6635 pf->hw.func_caps.num_tx_qp,
6636 pf->hw.func_caps.num_vsis);
6637
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006638#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6639 + pf->hw.func_caps.num_vfs)
6640 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6641 dev_info(&pf->pdev->dev,
6642 "got num_vsis %d, setting num_vsis to %d\n",
6643 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6644 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6645 }
6646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647 return 0;
6648}
6649
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006650static int i40e_vsi_clear(struct i40e_vsi *vsi);
6651
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006652/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006653 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006654 * @pf: board private structure
6655 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006656static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006657{
6658 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006659 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006660
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006661 /* quick workaround for an NVM issue that leaves a critical register
6662 * uninitialized
6663 */
6664 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6665 static const u32 hkey[] = {
6666 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6667 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6668 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6669 0x95b3a76d};
6670
6671 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6672 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6673 }
6674
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006675 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006676 return;
6677
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006678 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006679 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006680 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006681 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006682 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006683 break;
6684 }
6685 }
6686
6687 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006688 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006689 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6690 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 if (!vsi) {
6692 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006693 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6694 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006695 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006696 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006697
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006698 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006699}
6700
6701/**
6702 * i40e_fdir_teardown - release the Flow Director resources
6703 * @pf: board private structure
6704 **/
6705static void i40e_fdir_teardown(struct i40e_pf *pf)
6706{
6707 int i;
6708
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006709 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006710 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006711 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6712 i40e_vsi_release(pf->vsi[i]);
6713 break;
6714 }
6715 }
6716}
6717
6718/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006719 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006720 * @pf: board private structure
6721 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006722 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006723 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006724static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006725{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006726 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006727 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006728 u32 v;
6729
6730 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6731 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006732 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006733 if (i40e_check_asq_alive(&pf->hw))
6734 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006735
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006736 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006737
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006738 /* quiesce the VSIs and their queues that are not already DOWN */
6739 i40e_pf_quiesce_all_vsi(pf);
6740
Mitch Williams505682c2014-05-20 08:01:37 +00006741 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006742 if (pf->vsi[v])
6743 pf->vsi[v]->seid = 0;
6744 }
6745
6746 i40e_shutdown_adminq(&pf->hw);
6747
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006748 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006749 if (hw->hmc.hmc_obj) {
6750 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006751 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006752 dev_warn(&pf->pdev->dev,
6753 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006754 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006755}
6756
6757/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006758 * i40e_send_version - update firmware with driver version
6759 * @pf: PF struct
6760 */
6761static void i40e_send_version(struct i40e_pf *pf)
6762{
6763 struct i40e_driver_version dv;
6764
6765 dv.major_version = DRV_VERSION_MAJOR;
6766 dv.minor_version = DRV_VERSION_MINOR;
6767 dv.build_version = DRV_VERSION_BUILD;
6768 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006769 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006770 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6771}
6772
6773/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006774 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006775 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006776 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006777 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006778static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006779{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006780 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006781 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006782 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006783 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006784 u32 v;
6785
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006786 /* Now we wait for GRST to settle out.
6787 * We don't have to delete the VEBs or VSIs from the hw switch
6788 * because the reset will make them disappear.
6789 */
6790 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006791 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006792 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006793 set_bit(__I40E_RESET_FAILED, &pf->state);
6794 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006795 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006796 pf->pfr_count++;
6797
6798 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006799 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006800 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006801
6802 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6803 ret = i40e_init_adminq(&pf->hw);
6804 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006805 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6806 i40e_stat_str(&pf->hw, ret),
6807 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006808 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006809 }
6810
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006811 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006812 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006813 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006814
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006815 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006817 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006818 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006819
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6821 hw->func_caps.num_rx_qp,
6822 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6823 if (ret) {
6824 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6825 goto end_core_reset;
6826 }
6827 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6828 if (ret) {
6829 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6830 goto end_core_reset;
6831 }
6832
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006833#ifdef CONFIG_I40E_DCB
6834 ret = i40e_init_pf_dcb(pf);
6835 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006836 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6837 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6838 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006839 }
6840#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006841#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006842 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006843
Vasu Dev38e00432014-08-01 13:27:03 -07006844#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006845 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006846 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006847 if (ret)
6848 goto end_core_reset;
6849
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006850 /* The driver only wants link up/down and module qualification
6851 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006852 */
6853 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006854 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006855 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006856 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006857 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006858 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6859 i40e_stat_str(&pf->hw, ret),
6860 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006861
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006862 /* make sure our flow control settings are restored */
6863 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6864 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006865 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6866 i40e_stat_str(&pf->hw, ret),
6867 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006868
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 /* Rebuild the VSIs and VEBs that existed before reset.
6870 * They are still in our local switch element arrays, so only
6871 * need to rebuild the switch model in the HW.
6872 *
6873 * If there were VEBs but the reconstitution failed, we'll try
6874 * try to recover minimal use by getting the basic PF VSI working.
6875 */
6876 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006877 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878 /* find the one VEB connected to the MAC, and find orphans */
6879 for (v = 0; v < I40E_MAX_VEB; v++) {
6880 if (!pf->veb[v])
6881 continue;
6882
6883 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6884 pf->veb[v]->uplink_seid == 0) {
6885 ret = i40e_reconstitute_veb(pf->veb[v]);
6886
6887 if (!ret)
6888 continue;
6889
6890 /* If Main VEB failed, we're in deep doodoo,
6891 * so give up rebuilding the switch and set up
6892 * for minimal rebuild of PF VSI.
6893 * If orphan failed, we'll report the error
6894 * but try to keep going.
6895 */
6896 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6897 dev_info(&pf->pdev->dev,
6898 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6899 ret);
6900 pf->vsi[pf->lan_vsi]->uplink_seid
6901 = pf->mac_seid;
6902 break;
6903 } else if (pf->veb[v]->uplink_seid == 0) {
6904 dev_info(&pf->pdev->dev,
6905 "rebuild of orphan VEB failed: %d\n",
6906 ret);
6907 }
6908 }
6909 }
6910 }
6911
6912 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006913 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006914 /* no VEB, so rebuild only the Main VSI */
6915 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6916 if (ret) {
6917 dev_info(&pf->pdev->dev,
6918 "rebuild of Main VSI failed: %d\n", ret);
6919 goto end_core_reset;
6920 }
6921 }
6922
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006923 /* Reconfigure hardware for allowing smaller MSS in the case
6924 * of TSO, so that we avoid the MDD being fired and causing
6925 * a reset in the case of small MSS+TSO.
6926 */
6927#define I40E_REG_MSS 0x000E64DC
6928#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6929#define I40E_64BYTE_MSS 0x400000
6930 val = rd32(hw, I40E_REG_MSS);
6931 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6932 val &= ~I40E_REG_MSS_MIN_MASK;
6933 val |= I40E_64BYTE_MSS;
6934 wr32(hw, I40E_REG_MSS, val);
6935 }
6936
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006937 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006938 msleep(75);
6939 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6940 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006941 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6942 i40e_stat_str(&pf->hw, ret),
6943 i40e_aq_str(&pf->hw,
6944 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006945 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006946 /* reinit the misc interrupt */
6947 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6948 ret = i40e_setup_misc_vector(pf);
6949
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006950 /* Add a filter to drop all Flow control frames from any VSI from being
6951 * transmitted. By doing so we stop a malicious VF from sending out
6952 * PAUSE or PFC frames and potentially controlling traffic for other
6953 * PF/VF VSIs.
6954 * The FW can still send Flow control frames if enabled.
6955 */
6956 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6957 pf->main_vsi_seid);
6958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 /* restart the VSIs that were rebuilt and running before the reset */
6960 i40e_pf_unquiesce_all_vsi(pf);
6961
Mitch Williams69f64b22014-02-13 03:48:46 -08006962 if (pf->num_alloc_vfs) {
6963 for (v = 0; v < pf->num_alloc_vfs; v++)
6964 i40e_reset_vf(&pf->vf[v], true);
6965 }
6966
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006967 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006968 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006969
6970end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006971 clear_bit(__I40E_RESET_FAILED, &pf->state);
6972clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6974}
6975
6976/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006977 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006978 * @pf: board private structure
6979 *
6980 * Close up the VFs and other things in prep for a Core Reset,
6981 * then get ready to rebuild the world.
6982 **/
6983static void i40e_handle_reset_warning(struct i40e_pf *pf)
6984{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006985 i40e_prep_for_reset(pf);
6986 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006987}
6988
6989/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006990 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006991 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006992 *
6993 * Called from the MDD irq handler to identify possibly malicious vfs
6994 **/
6995static void i40e_handle_mdd_event(struct i40e_pf *pf)
6996{
6997 struct i40e_hw *hw = &pf->hw;
6998 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006999 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007000 struct i40e_vf *vf;
7001 u32 reg;
7002 int i;
7003
7004 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7005 return;
7006
7007 /* find what triggered the MDD event */
7008 reg = rd32(hw, I40E_GL_MDET_TX);
7009 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007010 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7011 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007012 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007013 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007014 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007015 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007016 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7017 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7018 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007019 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007020 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 +00007021 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007022 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7023 mdd_detected = true;
7024 }
7025 reg = rd32(hw, I40E_GL_MDET_RX);
7026 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007027 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7028 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007029 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007030 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007031 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7032 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7033 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007034 if (netif_msg_rx_err(pf))
7035 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7036 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007037 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7038 mdd_detected = true;
7039 }
7040
Neerav Parikhdf430b12014-06-04 01:23:15 +00007041 if (mdd_detected) {
7042 reg = rd32(hw, I40E_PF_MDET_TX);
7043 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7044 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007045 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007046 pf_mdd_detected = true;
7047 }
7048 reg = rd32(hw, I40E_PF_MDET_RX);
7049 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7050 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007051 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007052 pf_mdd_detected = true;
7053 }
7054 /* Queue belongs to the PF, initiate a reset */
7055 if (pf_mdd_detected) {
7056 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7057 i40e_service_event_schedule(pf);
7058 }
7059 }
7060
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007061 /* see if one of the VFs needs its hand slapped */
7062 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7063 vf = &(pf->vf[i]);
7064 reg = rd32(hw, I40E_VP_MDET_TX(i));
7065 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7066 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7067 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007068 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7069 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007070 }
7071
7072 reg = rd32(hw, I40E_VP_MDET_RX(i));
7073 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7074 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7075 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007076 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7077 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007078 }
7079
7080 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7081 dev_info(&pf->pdev->dev,
7082 "Too many MDD events on VF %d, disabled\n", i);
7083 dev_info(&pf->pdev->dev,
7084 "Use PF Control I/F to re-enable the VF\n");
7085 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7086 }
7087 }
7088
7089 /* re-enable mdd interrupt cause */
7090 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7091 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7092 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7093 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7094 i40e_flush(hw);
7095}
7096
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007097/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007098 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007099 * @pf: board private structure
7100 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007101static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007102{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007103 struct i40e_hw *hw = &pf->hw;
7104 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007105 __be16 port;
7106 int i;
7107
Singhai, Anjali6a899022015-12-14 12:21:18 -08007108 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007109 return;
7110
Singhai, Anjali6a899022015-12-14 12:21:18 -08007111 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007112
7113 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007114 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7115 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7116 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007117 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007118 ret = i40e_aq_add_udp_tunnel(hw, port,
7119 pf->udp_ports[i].type,
7120 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007121 else
7122 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007123
7124 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007125 dev_dbg(&pf->pdev->dev,
7126 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7127 pf->udp_ports[i].type ? "vxlan" : "geneve",
7128 port ? "add" : "delete",
7129 ntohs(port), i,
7130 i40e_stat_str(&pf->hw, ret),
7131 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007132 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007133 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007134 }
7135 }
7136 }
7137}
7138
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007139/**
7140 * i40e_service_task - Run the driver's async subtasks
7141 * @work: pointer to work_struct containing our data
7142 **/
7143static void i40e_service_task(struct work_struct *work)
7144{
7145 struct i40e_pf *pf = container_of(work,
7146 struct i40e_pf,
7147 service_task);
7148 unsigned long start_time = jiffies;
7149
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007150 /* don't bother with service tasks if a reset is in progress */
7151 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7152 i40e_service_event_complete(pf);
7153 return;
7154 }
7155
Kiran Patilb03a8c12015-09-24 18:13:15 -04007156 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007157 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007158 i40e_reset_subtask(pf);
7159 i40e_handle_mdd_event(pf);
7160 i40e_vc_process_vflr_event(pf);
7161 i40e_watchdog_subtask(pf);
7162 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007163 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007164 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007165 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007166 i40e_clean_adminq_subtask(pf);
7167
7168 i40e_service_event_complete(pf);
7169
7170 /* If the tasks have taken longer than one timer cycle or there
7171 * is more work to be done, reschedule the service task now
7172 * rather than wait for the timer to tick again.
7173 */
7174 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7175 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7176 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7177 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7178 i40e_service_event_schedule(pf);
7179}
7180
7181/**
7182 * i40e_service_timer - timer callback
7183 * @data: pointer to PF struct
7184 **/
7185static void i40e_service_timer(unsigned long data)
7186{
7187 struct i40e_pf *pf = (struct i40e_pf *)data;
7188
7189 mod_timer(&pf->service_timer,
7190 round_jiffies(jiffies + pf->service_timer_period));
7191 i40e_service_event_schedule(pf);
7192}
7193
7194/**
7195 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7196 * @vsi: the VSI being configured
7197 **/
7198static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7199{
7200 struct i40e_pf *pf = vsi->back;
7201
7202 switch (vsi->type) {
7203 case I40E_VSI_MAIN:
7204 vsi->alloc_queue_pairs = pf->num_lan_qps;
7205 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7206 I40E_REQ_DESCRIPTOR_MULTIPLE);
7207 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7208 vsi->num_q_vectors = pf->num_lan_msix;
7209 else
7210 vsi->num_q_vectors = 1;
7211
7212 break;
7213
7214 case I40E_VSI_FDIR:
7215 vsi->alloc_queue_pairs = 1;
7216 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7217 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007218 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007219 break;
7220
7221 case I40E_VSI_VMDQ2:
7222 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7223 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7224 I40E_REQ_DESCRIPTOR_MULTIPLE);
7225 vsi->num_q_vectors = pf->num_vmdq_msix;
7226 break;
7227
7228 case I40E_VSI_SRIOV:
7229 vsi->alloc_queue_pairs = pf->num_vf_qps;
7230 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7231 I40E_REQ_DESCRIPTOR_MULTIPLE);
7232 break;
7233
Vasu Dev38e00432014-08-01 13:27:03 -07007234#ifdef I40E_FCOE
7235 case I40E_VSI_FCOE:
7236 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7237 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7238 I40E_REQ_DESCRIPTOR_MULTIPLE);
7239 vsi->num_q_vectors = pf->num_fcoe_msix;
7240 break;
7241
7242#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007243 default:
7244 WARN_ON(1);
7245 return -ENODATA;
7246 }
7247
7248 return 0;
7249}
7250
7251/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007252 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7253 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007254 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007255 *
7256 * On error: returns error code (negative)
7257 * On success: returns 0
7258 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007259static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007260{
7261 int size;
7262 int ret = 0;
7263
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007264 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007265 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7266 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7267 if (!vsi->tx_rings)
7268 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007269 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7270
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007271 if (alloc_qvectors) {
7272 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007273 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007274 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7275 if (!vsi->q_vectors) {
7276 ret = -ENOMEM;
7277 goto err_vectors;
7278 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007279 }
7280 return ret;
7281
7282err_vectors:
7283 kfree(vsi->tx_rings);
7284 return ret;
7285}
7286
7287/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007288 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7289 * @pf: board private structure
7290 * @type: type of VSI
7291 *
7292 * On error: returns error code (negative)
7293 * On success: returns vsi index in PF (positive)
7294 **/
7295static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7296{
7297 int ret = -ENODEV;
7298 struct i40e_vsi *vsi;
7299 int vsi_idx;
7300 int i;
7301
7302 /* Need to protect the allocation of the VSIs at the PF level */
7303 mutex_lock(&pf->switch_mutex);
7304
7305 /* VSI list may be fragmented if VSI creation/destruction has
7306 * been happening. We can afford to do a quick scan to look
7307 * for any free VSIs in the list.
7308 *
7309 * find next empty vsi slot, looping back around if necessary
7310 */
7311 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007312 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007313 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007314 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007315 i = 0;
7316 while (i < pf->next_vsi && pf->vsi[i])
7317 i++;
7318 }
7319
Mitch Williams505682c2014-05-20 08:01:37 +00007320 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007321 vsi_idx = i; /* Found one! */
7322 } else {
7323 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007324 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007325 }
7326 pf->next_vsi = ++i;
7327
7328 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7329 if (!vsi) {
7330 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007331 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007332 }
7333 vsi->type = type;
7334 vsi->back = pf;
7335 set_bit(__I40E_DOWN, &vsi->state);
7336 vsi->flags = 0;
7337 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007338 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007339 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7340 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007341 vsi->netdev_registered = false;
7342 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007343 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007344 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007345
Alexander Duyck9f65e152013-09-28 06:00:58 +00007346 ret = i40e_set_num_rings_in_vsi(vsi);
7347 if (ret)
7348 goto err_rings;
7349
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007350 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007351 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007352 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007353
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007354 /* Setup default MSIX irq handler for VSI */
7355 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7356
Kiran Patil21659032015-09-30 14:09:03 -04007357 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007358 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007359 pf->vsi[vsi_idx] = vsi;
7360 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007361 goto unlock_pf;
7362
Alexander Duyck9f65e152013-09-28 06:00:58 +00007363err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007364 pf->next_vsi = i - 1;
7365 kfree(vsi);
7366unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007367 mutex_unlock(&pf->switch_mutex);
7368 return ret;
7369}
7370
7371/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007372 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7373 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007374 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007375 *
7376 * On error: returns error code (negative)
7377 * On success: returns 0
7378 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007379static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007380{
7381 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007382 if (free_qvectors) {
7383 kfree(vsi->q_vectors);
7384 vsi->q_vectors = NULL;
7385 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007386 kfree(vsi->tx_rings);
7387 vsi->tx_rings = NULL;
7388 vsi->rx_rings = NULL;
7389}
7390
7391/**
Helin Zhang28c58692015-10-26 19:44:27 -04007392 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7393 * and lookup table
7394 * @vsi: Pointer to VSI structure
7395 */
7396static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7397{
7398 if (!vsi)
7399 return;
7400
7401 kfree(vsi->rss_hkey_user);
7402 vsi->rss_hkey_user = NULL;
7403
7404 kfree(vsi->rss_lut_user);
7405 vsi->rss_lut_user = NULL;
7406}
7407
7408/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007409 * i40e_vsi_clear - Deallocate the VSI provided
7410 * @vsi: the VSI being un-configured
7411 **/
7412static int i40e_vsi_clear(struct i40e_vsi *vsi)
7413{
7414 struct i40e_pf *pf;
7415
7416 if (!vsi)
7417 return 0;
7418
7419 if (!vsi->back)
7420 goto free_vsi;
7421 pf = vsi->back;
7422
7423 mutex_lock(&pf->switch_mutex);
7424 if (!pf->vsi[vsi->idx]) {
7425 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7426 vsi->idx, vsi->idx, vsi, vsi->type);
7427 goto unlock_vsi;
7428 }
7429
7430 if (pf->vsi[vsi->idx] != vsi) {
7431 dev_err(&pf->pdev->dev,
7432 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7433 pf->vsi[vsi->idx]->idx,
7434 pf->vsi[vsi->idx],
7435 pf->vsi[vsi->idx]->type,
7436 vsi->idx, vsi, vsi->type);
7437 goto unlock_vsi;
7438 }
7439
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007440 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007441 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7442 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7443
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007444 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007445 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007447 pf->vsi[vsi->idx] = NULL;
7448 if (vsi->idx < pf->next_vsi)
7449 pf->next_vsi = vsi->idx;
7450
7451unlock_vsi:
7452 mutex_unlock(&pf->switch_mutex);
7453free_vsi:
7454 kfree(vsi);
7455
7456 return 0;
7457}
7458
7459/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007460 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7461 * @vsi: the VSI being cleaned
7462 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007463static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007464{
7465 int i;
7466
Greg Rose8e9dca52013-12-18 13:45:53 +00007467 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007468 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007469 kfree_rcu(vsi->tx_rings[i], rcu);
7470 vsi->tx_rings[i] = NULL;
7471 vsi->rx_rings[i] = NULL;
7472 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007473 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007474}
7475
7476/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007477 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7478 * @vsi: the VSI being configured
7479 **/
7480static int i40e_alloc_rings(struct i40e_vsi *vsi)
7481{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007482 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007483 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007484 int i;
7485
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007486 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007487 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007488 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007489 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7490 if (!tx_ring)
7491 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007492
7493 tx_ring->queue_index = i;
7494 tx_ring->reg_idx = vsi->base_queue + i;
7495 tx_ring->ring_active = false;
7496 tx_ring->vsi = vsi;
7497 tx_ring->netdev = vsi->netdev;
7498 tx_ring->dev = &pf->pdev->dev;
7499 tx_ring->count = vsi->num_desc;
7500 tx_ring->size = 0;
7501 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007502 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7503 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007504 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007505 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007506
Alexander Duyck9f65e152013-09-28 06:00:58 +00007507 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007508 rx_ring->queue_index = i;
7509 rx_ring->reg_idx = vsi->base_queue + i;
7510 rx_ring->ring_active = false;
7511 rx_ring->vsi = vsi;
7512 rx_ring->netdev = vsi->netdev;
7513 rx_ring->dev = &pf->pdev->dev;
7514 rx_ring->count = vsi->num_desc;
7515 rx_ring->size = 0;
7516 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007517 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007518 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007519 }
7520
7521 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007522
7523err_out:
7524 i40e_vsi_clear_rings(vsi);
7525 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007526}
7527
7528/**
7529 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7530 * @pf: board private structure
7531 * @vectors: the number of MSI-X vectors to request
7532 *
7533 * Returns the number of vectors reserved, or error
7534 **/
7535static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7536{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007537 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7538 I40E_MIN_MSIX, vectors);
7539 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007540 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007541 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007542 vectors = 0;
7543 }
7544
7545 return vectors;
7546}
7547
7548/**
7549 * i40e_init_msix - Setup the MSIX capability
7550 * @pf: board private structure
7551 *
7552 * Work with the OS to set up the MSIX vectors needed.
7553 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007554 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007555 **/
7556static int i40e_init_msix(struct i40e_pf *pf)
7557{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007558 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007559 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007560 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007561 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007562 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007563
7564 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7565 return -ENODEV;
7566
7567 /* The number of vectors we'll request will be comprised of:
7568 * - Add 1 for "other" cause for Admin Queue events, etc.
7569 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007570 * - Queues being used for RSS.
7571 * We don't need as many as max_rss_size vectors.
7572 * use rss_size instead in the calculation since that
7573 * is governed by number of cpus in the system.
7574 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007575 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007576 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007577#ifdef I40E_FCOE
7578 * - The number of FCOE qps.
7579#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007580 * Once we count this up, try the request.
7581 *
7582 * If we can't get what we want, we'll simplify to nearly nothing
7583 * and try again. If that still fails, we punt.
7584 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007585 vectors_left = hw->func_caps.num_msix_vectors;
7586 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007587
Shannon Nelson1e200e42015-02-27 09:15:24 +00007588 /* reserve one vector for miscellaneous handler */
7589 if (vectors_left) {
7590 v_budget++;
7591 vectors_left--;
7592 }
7593
7594 /* reserve vectors for the main PF traffic queues */
7595 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7596 vectors_left -= pf->num_lan_msix;
7597 v_budget += pf->num_lan_msix;
7598
7599 /* reserve one vector for sideband flow director */
7600 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7601 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007602 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007603 v_budget++;
7604 vectors_left--;
7605 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007606 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007607 }
7608 }
John W Linville83840e42015-01-14 03:06:28 +00007609
Vasu Dev38e00432014-08-01 13:27:03 -07007610#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007611 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007612 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007613 if (!vectors_left)
7614 pf->num_fcoe_msix = 0;
7615 else if (vectors_left >= pf->num_fcoe_qps)
7616 pf->num_fcoe_msix = pf->num_fcoe_qps;
7617 else
7618 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007619 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007620 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007621 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007622
Vasu Dev38e00432014-08-01 13:27:03 -07007623#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007624 /* can we reserve enough for iWARP? */
7625 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007626 iwarp_requested = pf->num_iwarp_msix;
7627
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007628 if (!vectors_left)
7629 pf->num_iwarp_msix = 0;
7630 else if (vectors_left < pf->num_iwarp_msix)
7631 pf->num_iwarp_msix = 1;
7632 v_budget += pf->num_iwarp_msix;
7633 vectors_left -= pf->num_iwarp_msix;
7634 }
7635
Shannon Nelson1e200e42015-02-27 09:15:24 +00007636 /* any vectors left over go for VMDq support */
7637 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7638 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7639 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7640
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007641 if (!vectors_left) {
7642 pf->num_vmdq_msix = 0;
7643 pf->num_vmdq_qps = 0;
7644 } else {
7645 /* if we're short on vectors for what's desired, we limit
7646 * the queues per vmdq. If this is still more than are
7647 * available, the user will need to change the number of
7648 * queues/vectors used by the PF later with the ethtool
7649 * channels command
7650 */
7651 if (vmdq_vecs < vmdq_vecs_wanted)
7652 pf->num_vmdq_qps = 1;
7653 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007654
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007655 v_budget += vmdq_vecs;
7656 vectors_left -= vmdq_vecs;
7657 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007658 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007659
7660 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7661 GFP_KERNEL);
7662 if (!pf->msix_entries)
7663 return -ENOMEM;
7664
7665 for (i = 0; i < v_budget; i++)
7666 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007667 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007668
Shannon Nelson3b444392015-02-26 16:15:57 +00007669 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007670 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7671 kfree(pf->msix_entries);
7672 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007673 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007674 return -ENODEV;
7675
Shannon Nelson3b444392015-02-26 16:15:57 +00007676 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007677 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007678 pf->num_vmdq_vsis = 0;
7679 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007680 pf->num_lan_qps = 1;
7681 pf->num_lan_msix = 1;
7682
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007683 } else if (!vectors_left) {
7684 /* If we have limited resources, we will start with no vectors
7685 * for the special features and then allocate vectors to some
7686 * of these features based on the policy and at the end disable
7687 * the features that did not get any vectors.
7688 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007689 int vec;
7690
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007691 dev_info(&pf->pdev->dev,
7692 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007693 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007694 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007695
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 /* Scale vector usage down */
7697 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007698 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007699 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007700#ifdef I40E_FCOE
7701 pf->num_fcoe_qps = 0;
7702 pf->num_fcoe_msix = 0;
7703#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007704
7705 /* partition out the remaining vectors */
7706 switch (vec) {
7707 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007708 pf->num_lan_msix = 1;
7709 break;
7710 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007711 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7712 pf->num_lan_msix = 1;
7713 pf->num_iwarp_msix = 1;
7714 } else {
7715 pf->num_lan_msix = 2;
7716 }
Vasu Dev38e00432014-08-01 13:27:03 -07007717#ifdef I40E_FCOE
7718 /* give one vector to FCoE */
7719 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7720 pf->num_lan_msix = 1;
7721 pf->num_fcoe_msix = 1;
7722 }
Vasu Dev38e00432014-08-01 13:27:03 -07007723#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007724 break;
7725 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007726 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7727 pf->num_iwarp_msix = min_t(int, (vec / 3),
7728 iwarp_requested);
7729 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7730 I40E_DEFAULT_NUM_VMDQ_VSI);
7731 } else {
7732 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7733 I40E_DEFAULT_NUM_VMDQ_VSI);
7734 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007735 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7736 pf->num_fdsb_msix = 1;
7737 vec--;
7738 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007739 pf->num_lan_msix = min_t(int,
7740 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7741 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007742 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007743#ifdef I40E_FCOE
7744 /* give one vector to FCoE */
7745 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7746 pf->num_fcoe_msix = 1;
7747 vec--;
7748 }
7749#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007750 break;
7751 }
7752 }
7753
Stefan Assmannabd97a92016-09-19 13:37:51 +02007754 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7755 (pf->num_fdsb_msix == 0)) {
7756 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7757 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7758 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007759 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7760 (pf->num_vmdq_msix == 0)) {
7761 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7762 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7763 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007764
7765 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7766 (pf->num_iwarp_msix == 0)) {
7767 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7768 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7769 }
Vasu Dev38e00432014-08-01 13:27:03 -07007770#ifdef I40E_FCOE
7771
7772 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7773 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7774 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7775 }
7776#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007777 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7778 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7779 pf->num_lan_msix,
7780 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7781 pf->num_fdsb_msix,
7782 pf->num_iwarp_msix);
7783
Shannon Nelson3b444392015-02-26 16:15:57 +00007784 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007785}
7786
7787/**
Greg Rose90e04072014-03-06 08:59:57 +00007788 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007789 * @vsi: the VSI being configured
7790 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007791 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007792 *
7793 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7794 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007795static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007796{
7797 struct i40e_q_vector *q_vector;
7798
7799 /* allocate q_vector */
7800 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7801 if (!q_vector)
7802 return -ENOMEM;
7803
7804 q_vector->vsi = vsi;
7805 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007806 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7807
Alexander Duyck493fb302013-09-28 07:01:44 +00007808 if (vsi->netdev)
7809 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007810 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007811
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007812 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7813 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7814
Alexander Duyck493fb302013-09-28 07:01:44 +00007815 /* tie q_vector and vsi together */
7816 vsi->q_vectors[v_idx] = q_vector;
7817
7818 return 0;
7819}
7820
7821/**
Greg Rose90e04072014-03-06 08:59:57 +00007822 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007823 * @vsi: the VSI being configured
7824 *
7825 * We allocate one q_vector per queue interrupt. If allocation fails we
7826 * return -ENOMEM.
7827 **/
Greg Rose90e04072014-03-06 08:59:57 +00007828static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007829{
7830 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007831 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007832
7833 /* if not MSIX, give the one vector only to the LAN VSI */
7834 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7835 num_q_vectors = vsi->num_q_vectors;
7836 else if (vsi == pf->vsi[pf->lan_vsi])
7837 num_q_vectors = 1;
7838 else
7839 return -EINVAL;
7840
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007841 current_cpu = cpumask_first(cpu_online_mask);
7842
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007843 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007844 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007845 if (err)
7846 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007847 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7848 if (unlikely(current_cpu >= nr_cpu_ids))
7849 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007850 }
7851
7852 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007853
7854err_out:
7855 while (v_idx--)
7856 i40e_free_q_vector(vsi, v_idx);
7857
7858 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007859}
7860
7861/**
7862 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7863 * @pf: board private structure to initialize
7864 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007865static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007866{
Shannon Nelson3b444392015-02-26 16:15:57 +00007867 int vectors = 0;
7868 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007869
7870 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007871 vectors = i40e_init_msix(pf);
7872 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007873 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007874 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007875#ifdef I40E_FCOE
7876 I40E_FLAG_FCOE_ENABLED |
7877#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007878 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007879 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007880 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007881 I40E_FLAG_SRIOV_ENABLED |
7882 I40E_FLAG_FD_SB_ENABLED |
7883 I40E_FLAG_FD_ATR_ENABLED |
7884 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007885
7886 /* rework the queue expectations without MSIX */
7887 i40e_determine_queue_usage(pf);
7888 }
7889 }
7890
7891 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7892 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007893 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007894 vectors = pci_enable_msi(pf->pdev);
7895 if (vectors < 0) {
7896 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7897 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007898 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7899 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007900 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007901 }
7902
Shannon Nelson958a3e32013-09-28 07:13:28 +00007903 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007904 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007905
Shannon Nelson3b444392015-02-26 16:15:57 +00007906 /* set up vector assignment tracking */
7907 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7908 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007909 if (!pf->irq_pile) {
7910 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7911 return -ENOMEM;
7912 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007913 pf->irq_pile->num_entries = vectors;
7914 pf->irq_pile->search_hint = 0;
7915
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007916 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007917 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007918
7919 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007920}
7921
7922/**
7923 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7924 * @pf: board private structure
7925 *
7926 * This sets up the handler for MSIX 0, which is used to manage the
7927 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7928 * when in MSI or Legacy interrupt mode.
7929 **/
7930static int i40e_setup_misc_vector(struct i40e_pf *pf)
7931{
7932 struct i40e_hw *hw = &pf->hw;
7933 int err = 0;
7934
7935 /* Only request the irq if this is the first time through, and
7936 * not when we're rebuilding after a Reset
7937 */
7938 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7939 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007940 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007941 if (err) {
7942 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007943 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007944 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007945 return -EFAULT;
7946 }
7947 }
7948
Jacob Kellerab437b52014-12-14 01:55:08 +00007949 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007950
7951 /* associate no queues to the misc vector */
7952 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7953 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7954
7955 i40e_flush(hw);
7956
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007957 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007958
7959 return err;
7960}
7961
7962/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007963 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7964 * @vsi: vsi structure
7965 * @seed: RSS hash seed
7966 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007967static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7968 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007969{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007970 struct i40e_pf *pf = vsi->back;
7971 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07007972 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007973
Jacob Keller776b2e12016-07-19 16:23:30 -07007974 if (seed) {
7975 struct i40e_aqc_get_set_rss_key_data *seed_dw =
7976 (struct i40e_aqc_get_set_rss_key_data *)seed;
7977 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
7978 if (ret) {
7979 dev_info(&pf->pdev->dev,
7980 "Cannot set RSS key, err %s aq_err %s\n",
7981 i40e_stat_str(hw, ret),
7982 i40e_aq_str(hw, hw->aq.asq_last_status));
7983 return ret;
7984 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007985 }
Jacob Keller776b2e12016-07-19 16:23:30 -07007986 if (lut) {
7987 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007988
Jacob Keller776b2e12016-07-19 16:23:30 -07007989 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
7990 if (ret) {
7991 dev_info(&pf->pdev->dev,
7992 "Cannot set RSS lut, err %s aq_err %s\n",
7993 i40e_stat_str(hw, ret),
7994 i40e_aq_str(hw, hw->aq.asq_last_status));
7995 return ret;
7996 }
7997 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007998 return ret;
7999}
8000
8001/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008002 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8003 * @vsi: Pointer to vsi structure
8004 * @seed: Buffter to store the hash keys
8005 * @lut: Buffer to store the lookup table entries
8006 * @lut_size: Size of buffer to store the lookup table entries
8007 *
8008 * Return 0 on success, negative on failure
8009 */
8010static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8011 u8 *lut, u16 lut_size)
8012{
8013 struct i40e_pf *pf = vsi->back;
8014 struct i40e_hw *hw = &pf->hw;
8015 int ret = 0;
8016
8017 if (seed) {
8018 ret = i40e_aq_get_rss_key(hw, vsi->id,
8019 (struct i40e_aqc_get_set_rss_key_data *)seed);
8020 if (ret) {
8021 dev_info(&pf->pdev->dev,
8022 "Cannot get RSS key, err %s aq_err %s\n",
8023 i40e_stat_str(&pf->hw, ret),
8024 i40e_aq_str(&pf->hw,
8025 pf->hw.aq.asq_last_status));
8026 return ret;
8027 }
8028 }
8029
8030 if (lut) {
8031 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8032
8033 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8034 if (ret) {
8035 dev_info(&pf->pdev->dev,
8036 "Cannot get RSS lut, err %s aq_err %s\n",
8037 i40e_stat_str(&pf->hw, ret),
8038 i40e_aq_str(&pf->hw,
8039 pf->hw.aq.asq_last_status));
8040 return ret;
8041 }
8042 }
8043
8044 return ret;
8045}
8046
8047/**
Jacob Keller0582b962016-07-19 16:23:29 -07008048 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8049 * @vsi: VSI structure
8050 **/
8051static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8052{
8053 u8 seed[I40E_HKEY_ARRAY_SIZE];
8054 struct i40e_pf *pf = vsi->back;
8055 u8 *lut;
8056 int ret;
8057
8058 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8059 return 0;
8060
Jacob Keller552b9962016-07-19 16:23:31 -07008061 if (!vsi->rss_size)
8062 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8063 vsi->num_queue_pairs);
8064 if (!vsi->rss_size)
8065 return -EINVAL;
8066
Jacob Keller0582b962016-07-19 16:23:29 -07008067 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8068 if (!lut)
8069 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008070 /* Use the user configured hash keys and lookup table if there is one,
8071 * otherwise use default
8072 */
8073 if (vsi->rss_lut_user)
8074 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8075 else
8076 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8077 if (vsi->rss_hkey_user)
8078 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8079 else
8080 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008081 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8082 kfree(lut);
8083
8084 return ret;
8085}
8086
8087/**
Helin Zhang043dd652015-10-21 19:56:23 -04008088 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008089 * @vsi: Pointer to vsi structure
8090 * @seed: RSS hash seed
8091 * @lut: Lookup table
8092 * @lut_size: Lookup table size
8093 *
8094 * Returns 0 on success, negative on failure
8095 **/
8096static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8097 const u8 *lut, u16 lut_size)
8098{
8099 struct i40e_pf *pf = vsi->back;
8100 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008101 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008102 u8 i;
8103
8104 /* Fill out hash function seed */
8105 if (seed) {
8106 u32 *seed_dw = (u32 *)seed;
8107
Mitch Williamsc4e18682016-04-12 08:30:40 -07008108 if (vsi->type == I40E_VSI_MAIN) {
8109 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8110 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8111 seed_dw[i]);
8112 } else if (vsi->type == I40E_VSI_SRIOV) {
8113 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8114 i40e_write_rx_ctl(hw,
8115 I40E_VFQF_HKEY1(i, vf_id),
8116 seed_dw[i]);
8117 } else {
8118 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8119 }
Helin Zhange69ff812015-10-21 19:56:22 -04008120 }
8121
8122 if (lut) {
8123 u32 *lut_dw = (u32 *)lut;
8124
Mitch Williamsc4e18682016-04-12 08:30:40 -07008125 if (vsi->type == I40E_VSI_MAIN) {
8126 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8127 return -EINVAL;
8128 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8129 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8130 } else if (vsi->type == I40E_VSI_SRIOV) {
8131 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8132 return -EINVAL;
8133 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8134 i40e_write_rx_ctl(hw,
8135 I40E_VFQF_HLUT1(i, vf_id),
8136 lut_dw[i]);
8137 } else {
8138 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8139 }
Helin Zhange69ff812015-10-21 19:56:22 -04008140 }
8141 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008142
8143 return 0;
8144}
8145
8146/**
Helin Zhang043dd652015-10-21 19:56:23 -04008147 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8148 * @vsi: Pointer to VSI structure
8149 * @seed: Buffer to store the keys
8150 * @lut: Buffer to store the lookup table entries
8151 * @lut_size: Size of buffer to store the lookup table entries
8152 *
8153 * Returns 0 on success, negative on failure
8154 */
8155static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8156 u8 *lut, u16 lut_size)
8157{
8158 struct i40e_pf *pf = vsi->back;
8159 struct i40e_hw *hw = &pf->hw;
8160 u16 i;
8161
8162 if (seed) {
8163 u32 *seed_dw = (u32 *)seed;
8164
8165 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008166 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008167 }
8168 if (lut) {
8169 u32 *lut_dw = (u32 *)lut;
8170
8171 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8172 return -EINVAL;
8173 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8174 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8175 }
8176
8177 return 0;
8178}
8179
8180/**
8181 * i40e_config_rss - Configure RSS keys and lut
8182 * @vsi: Pointer to VSI structure
8183 * @seed: RSS hash seed
8184 * @lut: Lookup table
8185 * @lut_size: Lookup table size
8186 *
8187 * Returns 0 on success, negative on failure
8188 */
8189int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8190{
8191 struct i40e_pf *pf = vsi->back;
8192
8193 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8194 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8195 else
8196 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8197}
8198
8199/**
8200 * i40e_get_rss - Get RSS keys and lut
8201 * @vsi: Pointer to VSI structure
8202 * @seed: Buffer to store the keys
8203 * @lut: Buffer to store the lookup table entries
8204 * lut_size: Size of buffer to store the lookup table entries
8205 *
8206 * Returns 0 on success, negative on failure
8207 */
8208int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8209{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008210 struct i40e_pf *pf = vsi->back;
8211
8212 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8213 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8214 else
8215 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008216}
8217
8218/**
Helin Zhange69ff812015-10-21 19:56:22 -04008219 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8220 * @pf: Pointer to board private structure
8221 * @lut: Lookup table
8222 * @rss_table_size: Lookup table size
8223 * @rss_size: Range of queue number for hashing
8224 */
Alan Bradyf1582352016-08-24 11:33:46 -07008225void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8226 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008227{
Helin Zhange69ff812015-10-21 19:56:22 -04008228 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008229
Helin Zhange69ff812015-10-21 19:56:22 -04008230 for (i = 0; i < rss_table_size; i++)
8231 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008232}
8233
8234/**
Helin Zhang043dd652015-10-21 19:56:23 -04008235 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008236 * @pf: board private structure
8237 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008238static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008239{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008240 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008241 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008242 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008243 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008244 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008245 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008246 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008247
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008248 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008249 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8250 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008251 hena |= i40e_pf_get_default_rss_hena(pf);
8252
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008253 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8254 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008255
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008256 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008257 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008258 reg_val = (pf->rss_table_size == 512) ?
8259 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8260 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008261 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008262
Helin Zhang28c58692015-10-26 19:44:27 -04008263 /* Determine the RSS size of the VSI */
8264 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008265 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8266 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008267 if (!vsi->rss_size)
8268 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008269
Helin Zhange69ff812015-10-21 19:56:22 -04008270 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8271 if (!lut)
8272 return -ENOMEM;
8273
Helin Zhang28c58692015-10-26 19:44:27 -04008274 /* Use user configured lut if there is one, otherwise use default */
8275 if (vsi->rss_lut_user)
8276 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8277 else
8278 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008279
Helin Zhang28c58692015-10-26 19:44:27 -04008280 /* Use user configured hash key if there is one, otherwise
8281 * use default.
8282 */
8283 if (vsi->rss_hkey_user)
8284 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8285 else
8286 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008287 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008288 kfree(lut);
8289
8290 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008291}
8292
8293/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008294 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8295 * @pf: board private structure
8296 * @queue_count: the requested queue count for rss.
8297 *
8298 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8299 * count which may be different from the requested queue count.
8300 **/
8301int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8302{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008303 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8304 int new_rss_size;
8305
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008306 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8307 return 0;
8308
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008309 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008310
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008311 if (queue_count != vsi->num_queue_pairs) {
8312 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008313 i40e_prep_for_reset(pf);
8314
Helin Zhangacd65442015-10-26 19:44:28 -04008315 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008316
8317 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008318
8319 /* Discard the user configured hash keys and lut, if less
8320 * queues are enabled.
8321 */
8322 if (queue_count < vsi->rss_size) {
8323 i40e_clear_rss_config_user(vsi);
8324 dev_dbg(&pf->pdev->dev,
8325 "discard user configured hash keys and lut\n");
8326 }
8327
8328 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008329 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8330 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008331
Helin Zhang043dd652015-10-21 19:56:23 -04008332 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008333 }
Lihong Yang12815052016-09-27 11:28:48 -07008334 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8335 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008336 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008337}
8338
8339/**
Greg Rosef4492db2015-02-06 08:52:12 +00008340 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8341 * @pf: board private structure
8342 **/
8343i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8344{
8345 i40e_status status;
8346 bool min_valid, max_valid;
8347 u32 max_bw, min_bw;
8348
8349 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8350 &min_valid, &max_valid);
8351
8352 if (!status) {
8353 if (min_valid)
8354 pf->npar_min_bw = min_bw;
8355 if (max_valid)
8356 pf->npar_max_bw = max_bw;
8357 }
8358
8359 return status;
8360}
8361
8362/**
8363 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8364 * @pf: board private structure
8365 **/
8366i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8367{
8368 struct i40e_aqc_configure_partition_bw_data bw_data;
8369 i40e_status status;
8370
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008371 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008372 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008373 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8374 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8375
8376 /* Set the new bandwidths */
8377 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8378
8379 return status;
8380}
8381
8382/**
8383 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8384 * @pf: board private structure
8385 **/
8386i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8387{
8388 /* Commit temporary BW setting to permanent NVM image */
8389 enum i40e_admin_queue_err last_aq_status;
8390 i40e_status ret;
8391 u16 nvm_word;
8392
8393 if (pf->hw.partition_id != 1) {
8394 dev_info(&pf->pdev->dev,
8395 "Commit BW only works on partition 1! This is partition %d",
8396 pf->hw.partition_id);
8397 ret = I40E_NOT_SUPPORTED;
8398 goto bw_commit_out;
8399 }
8400
8401 /* Acquire NVM for read access */
8402 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8403 last_aq_status = pf->hw.aq.asq_last_status;
8404 if (ret) {
8405 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008406 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8407 i40e_stat_str(&pf->hw, ret),
8408 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008409 goto bw_commit_out;
8410 }
8411
8412 /* Read word 0x10 of NVM - SW compatibility word 1 */
8413 ret = i40e_aq_read_nvm(&pf->hw,
8414 I40E_SR_NVM_CONTROL_WORD,
8415 0x10, sizeof(nvm_word), &nvm_word,
8416 false, NULL);
8417 /* Save off last admin queue command status before releasing
8418 * the NVM
8419 */
8420 last_aq_status = pf->hw.aq.asq_last_status;
8421 i40e_release_nvm(&pf->hw);
8422 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008423 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8424 i40e_stat_str(&pf->hw, ret),
8425 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008426 goto bw_commit_out;
8427 }
8428
8429 /* Wait a bit for NVM release to complete */
8430 msleep(50);
8431
8432 /* Acquire NVM for write access */
8433 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8434 last_aq_status = pf->hw.aq.asq_last_status;
8435 if (ret) {
8436 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008437 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8438 i40e_stat_str(&pf->hw, ret),
8439 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008440 goto bw_commit_out;
8441 }
8442 /* Write it back out unchanged to initiate update NVM,
8443 * which will force a write of the shadow (alt) RAM to
8444 * the NVM - thus storing the bandwidth values permanently.
8445 */
8446 ret = i40e_aq_update_nvm(&pf->hw,
8447 I40E_SR_NVM_CONTROL_WORD,
8448 0x10, sizeof(nvm_word),
8449 &nvm_word, true, NULL);
8450 /* Save off last admin queue command status before releasing
8451 * the NVM
8452 */
8453 last_aq_status = pf->hw.aq.asq_last_status;
8454 i40e_release_nvm(&pf->hw);
8455 if (ret)
8456 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008457 "BW settings NOT SAVED, err %s aq_err %s\n",
8458 i40e_stat_str(&pf->hw, ret),
8459 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008460bw_commit_out:
8461
8462 return ret;
8463}
8464
8465/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008466 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8467 * @pf: board private structure to initialize
8468 *
8469 * i40e_sw_init initializes the Adapter private data structure.
8470 * Fields are initialized based on PCI device information and
8471 * OS network device settings (MTU size).
8472 **/
8473static int i40e_sw_init(struct i40e_pf *pf)
8474{
8475 int err = 0;
8476 int size;
8477
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008478 /* Set default capability flags */
8479 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8480 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008481 I40E_FLAG_MSIX_ENABLED;
8482
Mitch Williamsca99eb92014-04-04 04:43:07 +00008483 /* Set default ITR */
8484 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8485 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8486
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008487 /* Depending on PF configurations, it is possible that the RSS
8488 * maximum might end up larger than the available queues
8489 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008490 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008491 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008492 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008493 pf->rss_size_max = min_t(int, pf->rss_size_max,
8494 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008495 if (pf->hw.func_caps.rss) {
8496 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008497 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8498 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008499 }
8500
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008501 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008502 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008503 pf->flags |= I40E_FLAG_MFP_ENABLED;
8504 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008505 if (i40e_get_npar_bw_setting(pf))
8506 dev_warn(&pf->pdev->dev,
8507 "Could not get NPAR bw settings\n");
8508 else
8509 dev_info(&pf->pdev->dev,
8510 "Min BW = %8.8x, Max BW = %8.8x\n",
8511 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008512 }
8513
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008514 /* FW/NVM is not yet fixed in this regard */
8515 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8516 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8517 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8518 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008519 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8520 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008521 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008522 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008523 else
8524 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008525 pf->fdir_pf_filter_count =
8526 pf->hw.func_caps.fd_filters_guaranteed;
8527 pf->hw.fdir_shared_filter_count =
8528 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008529 }
8530
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008531 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008532 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008533 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008534 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008535 /* No DCB support for FW < v4.33 */
8536 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8537 }
8538
8539 /* Disable FW LLDP if FW < v4.3 */
8540 if (i40e_is_mac_710(&pf->hw) &&
8541 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8542 (pf->hw.aq.fw_maj_ver < 4)))
8543 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8544
8545 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8546 if (i40e_is_mac_710(&pf->hw) &&
8547 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8548 (pf->hw.aq.fw_maj_ver >= 5)))
8549 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008551 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008552 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008553 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008554 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555 }
8556
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008557 if (pf->hw.func_caps.iwarp) {
8558 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8559 /* IWARP needs one extra vector for CQP just like MISC.*/
8560 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8561 }
8562
Vasu Dev38e00432014-08-01 13:27:03 -07008563#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008564 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008565
8566#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008567#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008568 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008569 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8570 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8571 pf->num_req_vfs = min_t(int,
8572 pf->hw.func_caps.num_vfs,
8573 I40E_MAX_VF_COUNT);
8574 }
8575#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008576 if (pf->hw.mac.type == I40E_MAC_X722) {
8577 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8578 I40E_FLAG_128_QP_RSS_CAPABLE |
8579 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8580 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8581 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008582 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008583 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008584 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008585 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008586 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8587 ((pf->hw.aq.api_maj_ver == 1) &&
8588 (pf->hw.aq.api_min_ver > 4))) {
8589 /* Supported in FW API version higher than 1.4 */
8590 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008591 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8592 } else {
8593 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008594 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008596 pf->eeprom_version = 0xDEAD;
8597 pf->lan_veb = I40E_NO_VEB;
8598 pf->lan_vsi = I40E_NO_VSI;
8599
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008600 /* By default FW has this off for performance reasons */
8601 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008603 /* set up queue assignment tracking */
8604 size = sizeof(struct i40e_lump_tracking)
8605 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8606 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8607 if (!pf->qp_pile) {
8608 err = -ENOMEM;
8609 goto sw_init_done;
8610 }
8611 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8612 pf->qp_pile->search_hint = 0;
8613
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008614 pf->tx_timeout_recovery_level = 1;
8615
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008616 mutex_init(&pf->switch_mutex);
8617
Greg Rosec668a122015-02-26 16:10:39 +00008618 /* If NPAR is enabled nudge the Tx scheduler */
8619 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8620 i40e_set_npar_bw_setting(pf);
8621
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008622sw_init_done:
8623 return err;
8624}
8625
8626/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008627 * i40e_set_ntuple - set the ntuple feature flag and take action
8628 * @pf: board private structure to initialize
8629 * @features: the feature set that the stack is suggesting
8630 *
8631 * returns a bool to indicate if reset needs to happen
8632 **/
8633bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8634{
8635 bool need_reset = false;
8636
8637 /* Check if Flow Director n-tuple support was enabled or disabled. If
8638 * the state changed, we need to reset.
8639 */
8640 if (features & NETIF_F_NTUPLE) {
8641 /* Enable filters and mark for reset */
8642 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8643 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008644 /* enable FD_SB only if there is MSI-X vector */
8645 if (pf->num_fdsb_msix > 0)
8646 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008647 } else {
8648 /* turn off filters, mark for reset and clear SW filter list */
8649 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8650 need_reset = true;
8651 i40e_fdir_filter_exit(pf);
8652 }
8653 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008654 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008655 /* reset fd counters */
8656 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8657 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008658 /* if ATR was auto disabled it can be re-enabled. */
8659 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008660 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008661 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008662 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8663 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8664 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008665 }
8666 return need_reset;
8667}
8668
8669/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008670 * i40e_clear_rss_lut - clear the rx hash lookup table
8671 * @vsi: the VSI being configured
8672 **/
8673static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8674{
8675 struct i40e_pf *pf = vsi->back;
8676 struct i40e_hw *hw = &pf->hw;
8677 u16 vf_id = vsi->vf_id;
8678 u8 i;
8679
8680 if (vsi->type == I40E_VSI_MAIN) {
8681 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8682 wr32(hw, I40E_PFQF_HLUT(i), 0);
8683 } else if (vsi->type == I40E_VSI_SRIOV) {
8684 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8685 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8686 } else {
8687 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8688 }
8689}
8690
8691/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008692 * i40e_set_features - set the netdev feature flags
8693 * @netdev: ptr to the netdev being adjusted
8694 * @features: the feature set that the stack is suggesting
8695 **/
8696static int i40e_set_features(struct net_device *netdev,
8697 netdev_features_t features)
8698{
8699 struct i40e_netdev_priv *np = netdev_priv(netdev);
8700 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008701 struct i40e_pf *pf = vsi->back;
8702 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008703
Alan Bradyd8ec9862016-07-27 12:02:38 -07008704 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8705 i40e_pf_config_rss(pf);
8706 else if (!(features & NETIF_F_RXHASH) &&
8707 netdev->features & NETIF_F_RXHASH)
8708 i40e_clear_rss_lut(vsi);
8709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008710 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8711 i40e_vlan_stripping_enable(vsi);
8712 else
8713 i40e_vlan_stripping_disable(vsi);
8714
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008715 need_reset = i40e_set_ntuple(pf, features);
8716
8717 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008718 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008719
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008720 return 0;
8721}
8722
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008723/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008724 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008725 * @pf: board private structure
8726 * @port: The UDP port to look up
8727 *
8728 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8729 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008730static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008731{
8732 u8 i;
8733
8734 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008735 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008736 return i;
8737 }
8738
8739 return i;
8740}
8741
8742/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008743 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008744 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008745 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008746 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008747static void i40e_udp_tunnel_add(struct net_device *netdev,
8748 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008749{
8750 struct i40e_netdev_priv *np = netdev_priv(netdev);
8751 struct i40e_vsi *vsi = np->vsi;
8752 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008753 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008754 u8 next_idx;
8755 u8 idx;
8756
Singhai, Anjali6a899022015-12-14 12:21:18 -08008757 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008758
8759 /* Check if port already exists */
8760 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008761 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008762 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008763 return;
8764 }
8765
8766 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008767 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008768
8769 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008770 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008771 ntohs(port));
8772 return;
8773 }
8774
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008775 switch (ti->type) {
8776 case UDP_TUNNEL_TYPE_VXLAN:
8777 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8778 break;
8779 case UDP_TUNNEL_TYPE_GENEVE:
8780 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8781 return;
8782 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8783 break;
8784 default:
8785 return;
8786 }
8787
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008788 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008789 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008790 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8791 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008792}
8793
8794/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008795 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008796 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008797 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008798 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008799static void i40e_udp_tunnel_del(struct net_device *netdev,
8800 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008801{
8802 struct i40e_netdev_priv *np = netdev_priv(netdev);
8803 struct i40e_vsi *vsi = np->vsi;
8804 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008805 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008806 u8 idx;
8807
Singhai, Anjali6a899022015-12-14 12:21:18 -08008808 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008809
8810 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008811 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8812 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008813
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008814 switch (ti->type) {
8815 case UDP_TUNNEL_TYPE_VXLAN:
8816 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8817 goto not_found;
8818 break;
8819 case UDP_TUNNEL_TYPE_GENEVE:
8820 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8821 goto not_found;
8822 break;
8823 default:
8824 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008825 }
8826
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008827 /* if port exists, set it to 0 (mark for deletion)
8828 * and make it pending
8829 */
8830 pf->udp_ports[idx].index = 0;
8831 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008832 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8833
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008834 return;
8835not_found:
8836 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8837 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008838}
8839
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008840static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008841 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008842{
8843 struct i40e_netdev_priv *np = netdev_priv(netdev);
8844 struct i40e_pf *pf = np->vsi->back;
8845 struct i40e_hw *hw = &pf->hw;
8846
8847 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8848 return -EOPNOTSUPP;
8849
8850 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8851 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8852
8853 return 0;
8854}
8855
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008856/**
8857 * i40e_ndo_fdb_add - add an entry to the hardware database
8858 * @ndm: the input from the stack
8859 * @tb: pointer to array of nladdr (unused)
8860 * @dev: the net device pointer
8861 * @addr: the MAC address entry being added
8862 * @flags: instructions from stack about fdb operation
8863 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008864static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8865 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008866 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008867 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008868{
8869 struct i40e_netdev_priv *np = netdev_priv(dev);
8870 struct i40e_pf *pf = np->vsi->back;
8871 int err = 0;
8872
8873 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8874 return -EOPNOTSUPP;
8875
Or Gerlitz65891fe2014-12-14 18:19:05 +02008876 if (vid) {
8877 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8878 return -EINVAL;
8879 }
8880
Greg Rose4ba0dea2014-03-06 08:59:55 +00008881 /* Hardware does not support aging addresses so if a
8882 * ndm_state is given only allow permanent addresses
8883 */
8884 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8885 netdev_info(dev, "FDB only supports static addresses\n");
8886 return -EINVAL;
8887 }
8888
8889 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8890 err = dev_uc_add_excl(dev, addr);
8891 else if (is_multicast_ether_addr(addr))
8892 err = dev_mc_add_excl(dev, addr);
8893 else
8894 err = -EINVAL;
8895
8896 /* Only return duplicate errors if NLM_F_EXCL is set */
8897 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8898 err = 0;
8899
8900 return err;
8901}
8902
Neerav Parikh51616012015-02-06 08:52:14 +00008903/**
8904 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8905 * @dev: the netdev being configured
8906 * @nlh: RTNL message
8907 *
8908 * Inserts a new hardware bridge if not already created and
8909 * enables the bridging mode requested (VEB or VEPA). If the
8910 * hardware bridge has already been inserted and the request
8911 * is to change the mode then that requires a PF reset to
8912 * allow rebuild of the components with required hardware
8913 * bridge mode enabled.
8914 **/
8915static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008916 struct nlmsghdr *nlh,
8917 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008918{
8919 struct i40e_netdev_priv *np = netdev_priv(dev);
8920 struct i40e_vsi *vsi = np->vsi;
8921 struct i40e_pf *pf = vsi->back;
8922 struct i40e_veb *veb = NULL;
8923 struct nlattr *attr, *br_spec;
8924 int i, rem;
8925
8926 /* Only for PF VSI for now */
8927 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8928 return -EOPNOTSUPP;
8929
8930 /* Find the HW bridge for PF VSI */
8931 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8932 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8933 veb = pf->veb[i];
8934 }
8935
8936 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8937
8938 nla_for_each_nested(attr, br_spec, rem) {
8939 __u16 mode;
8940
8941 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8942 continue;
8943
8944 mode = nla_get_u16(attr);
8945 if ((mode != BRIDGE_MODE_VEPA) &&
8946 (mode != BRIDGE_MODE_VEB))
8947 return -EINVAL;
8948
8949 /* Insert a new HW bridge */
8950 if (!veb) {
8951 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8952 vsi->tc_config.enabled_tc);
8953 if (veb) {
8954 veb->bridge_mode = mode;
8955 i40e_config_bridge_mode(veb);
8956 } else {
8957 /* No Bridge HW offload available */
8958 return -ENOENT;
8959 }
8960 break;
8961 } else if (mode != veb->bridge_mode) {
8962 /* Existing HW bridge but different mode needs reset */
8963 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008964 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8965 if (mode == BRIDGE_MODE_VEB)
8966 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8967 else
8968 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8969 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008970 break;
8971 }
8972 }
8973
8974 return 0;
8975}
8976
8977/**
8978 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8979 * @skb: skb buff
8980 * @pid: process id
8981 * @seq: RTNL message seq #
8982 * @dev: the netdev being configured
8983 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008984 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008985 *
8986 * Return the mode in which the hardware bridge is operating in
8987 * i.e VEB or VEPA.
8988 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008989static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8990 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008991 u32 __always_unused filter_mask,
8992 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008993{
8994 struct i40e_netdev_priv *np = netdev_priv(dev);
8995 struct i40e_vsi *vsi = np->vsi;
8996 struct i40e_pf *pf = vsi->back;
8997 struct i40e_veb *veb = NULL;
8998 int i;
8999
9000 /* Only for PF VSI for now */
9001 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9002 return -EOPNOTSUPP;
9003
9004 /* Find the HW bridge for the PF VSI */
9005 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9006 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9007 veb = pf->veb[i];
9008 }
9009
9010 if (!veb)
9011 return 0;
9012
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009013 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009014 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009015}
Neerav Parikh51616012015-02-06 08:52:14 +00009016
Singhai, Anjali6a899022015-12-14 12:21:18 -08009017/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9018 * inner mac plus all inner ethertypes.
9019 */
9020#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009021/**
9022 * i40e_features_check - Validate encapsulated packet conforms to limits
9023 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009024 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009025 * @features: Offload features that the stack believes apply
9026 **/
9027static netdev_features_t i40e_features_check(struct sk_buff *skb,
9028 struct net_device *dev,
9029 netdev_features_t features)
9030{
9031 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009032 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009033 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009034 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009035
9036 return features;
9037}
9038
Shannon Nelson37a29732015-02-27 09:15:19 +00009039static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009040 .ndo_open = i40e_open,
9041 .ndo_stop = i40e_close,
9042 .ndo_start_xmit = i40e_lan_xmit_frame,
9043 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9044 .ndo_set_rx_mode = i40e_set_rx_mode,
9045 .ndo_validate_addr = eth_validate_addr,
9046 .ndo_set_mac_address = i40e_set_mac,
9047 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009048 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009049 .ndo_tx_timeout = i40e_tx_timeout,
9050 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9051 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9052#ifdef CONFIG_NET_POLL_CONTROLLER
9053 .ndo_poll_controller = i40e_netpoll,
9054#endif
John Fastabende4c67342016-02-16 21:16:15 -08009055 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009056#ifdef I40E_FCOE
9057 .ndo_fcoe_enable = i40e_fcoe_enable,
9058 .ndo_fcoe_disable = i40e_fcoe_disable,
9059#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009060 .ndo_set_features = i40e_set_features,
9061 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9062 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009063 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009064 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009065 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009066 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009067 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009068 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9069 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009070 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009071 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009072 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009073 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9074 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009075};
9076
9077/**
9078 * i40e_config_netdev - Setup the netdev flags
9079 * @vsi: the VSI being configured
9080 *
9081 * Returns 0 on success, negative value on failure
9082 **/
9083static int i40e_config_netdev(struct i40e_vsi *vsi)
9084{
9085 struct i40e_pf *pf = vsi->back;
9086 struct i40e_hw *hw = &pf->hw;
9087 struct i40e_netdev_priv *np;
9088 struct net_device *netdev;
9089 u8 mac_addr[ETH_ALEN];
9090 int etherdev_size;
9091
9092 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009093 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009094 if (!netdev)
9095 return -ENOMEM;
9096
9097 vsi->netdev = netdev;
9098 np = netdev_priv(netdev);
9099 np->vsi = vsi;
9100
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009101 netdev->hw_enc_features |= NETIF_F_SG |
9102 NETIF_F_IP_CSUM |
9103 NETIF_F_IPV6_CSUM |
9104 NETIF_F_HIGHDMA |
9105 NETIF_F_SOFT_FEATURES |
9106 NETIF_F_TSO |
9107 NETIF_F_TSO_ECN |
9108 NETIF_F_TSO6 |
9109 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009110 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009111 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009112 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009113 NETIF_F_GSO_UDP_TUNNEL |
9114 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009115 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009116 NETIF_F_SCTP_CRC |
9117 NETIF_F_RXHASH |
9118 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009119 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009120
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009121 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009122 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9123
9124 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009125
9126 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009127 netdev->vlan_features |= netdev->hw_enc_features |
9128 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009129
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009130 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009131 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009132
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009133 netdev->hw_features |= netdev->hw_enc_features |
9134 NETIF_F_HW_VLAN_CTAG_TX |
9135 NETIF_F_HW_VLAN_CTAG_RX;
9136
9137 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009138 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009139
9140 if (vsi->type == I40E_VSI_MAIN) {
9141 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009142 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009143 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009144 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009145 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009146 } else {
9147 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9148 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9149 pf->vsi[pf->lan_vsi]->netdev->name);
9150 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009151
Jacob Keller278e7d02016-10-05 09:30:37 -07009152 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009153 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009154 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009155 }
Kiran Patil21659032015-09-30 14:09:03 -04009156
Greg Rose9a173902014-05-22 06:32:02 +00009157 ether_addr_copy(netdev->dev_addr, mac_addr);
9158 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009159
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009160 netdev->priv_flags |= IFF_UNICAST_FLT;
9161 netdev->priv_flags |= IFF_SUPP_NOFCS;
9162 /* Setup netdev TC information */
9163 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9164
9165 netdev->netdev_ops = &i40e_netdev_ops;
9166 netdev->watchdog_timeo = 5 * HZ;
9167 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009168#ifdef I40E_FCOE
9169 i40e_fcoe_config_netdev(netdev, vsi);
9170#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009171
Jarod Wilson91c527a2016-10-17 15:54:05 -04009172 /* MTU range: 68 - 9706 */
9173 netdev->min_mtu = ETH_MIN_MTU;
9174 netdev->max_mtu = I40E_MAX_RXBUFFER -
9175 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9176
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009177 return 0;
9178}
9179
9180/**
9181 * i40e_vsi_delete - Delete a VSI from the switch
9182 * @vsi: the VSI being removed
9183 *
9184 * Returns 0 on success, negative value on failure
9185 **/
9186static void i40e_vsi_delete(struct i40e_vsi *vsi)
9187{
9188 /* remove default VSI is not allowed */
9189 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9190 return;
9191
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009192 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009193}
9194
9195/**
Neerav Parikh51616012015-02-06 08:52:14 +00009196 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9197 * @vsi: the VSI being queried
9198 *
9199 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9200 **/
9201int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9202{
9203 struct i40e_veb *veb;
9204 struct i40e_pf *pf = vsi->back;
9205
9206 /* Uplink is not a bridge so default to VEB */
9207 if (vsi->veb_idx == I40E_NO_VEB)
9208 return 1;
9209
9210 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009211 if (!veb) {
9212 dev_info(&pf->pdev->dev,
9213 "There is no veb associated with the bridge\n");
9214 return -ENOENT;
9215 }
Neerav Parikh51616012015-02-06 08:52:14 +00009216
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009217 /* Uplink is a bridge in VEPA mode */
9218 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9219 return 0;
9220 } else {
9221 /* Uplink is a bridge in VEB mode */
9222 return 1;
9223 }
9224
9225 /* VEPA is now default bridge, so return 0 */
9226 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009227}
9228
9229/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009230 * i40e_add_vsi - Add a VSI to the switch
9231 * @vsi: the VSI being configured
9232 *
9233 * This initializes a VSI context depending on the VSI type to be added and
9234 * passes it down to the add_vsi aq command.
9235 **/
9236static int i40e_add_vsi(struct i40e_vsi *vsi)
9237{
9238 int ret = -ENODEV;
Kiran Patilf6bd0962016-06-20 09:10:34 -07009239 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009240 struct i40e_pf *pf = vsi->back;
9241 struct i40e_hw *hw = &pf->hw;
9242 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009243 struct i40e_mac_filter *f;
9244 struct hlist_node *h;
9245 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009246
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009247 u8 enabled_tc = 0x1; /* TC0 enabled */
9248 int f_count = 0;
9249
9250 memset(&ctxt, 0, sizeof(ctxt));
9251 switch (vsi->type) {
9252 case I40E_VSI_MAIN:
9253 /* The PF's main VSI is already setup as part of the
9254 * device initialization, so we'll not bother with
9255 * the add_vsi call, but we will retrieve the current
9256 * VSI context.
9257 */
9258 ctxt.seid = pf->main_vsi_seid;
9259 ctxt.pf_num = pf->hw.pf_id;
9260 ctxt.vf_num = 0;
9261 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9262 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9263 if (ret) {
9264 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009265 "couldn't get PF vsi config, err %s aq_err %s\n",
9266 i40e_stat_str(&pf->hw, ret),
9267 i40e_aq_str(&pf->hw,
9268 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009269 return -ENOENT;
9270 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009271 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009272 vsi->info.valid_sections = 0;
9273
9274 vsi->seid = ctxt.seid;
9275 vsi->id = ctxt.vsi_number;
9276
9277 enabled_tc = i40e_pf_get_tc_map(pf);
9278
9279 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009280 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9281 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009282 memset(&ctxt, 0, sizeof(ctxt));
9283 ctxt.seid = pf->main_vsi_seid;
9284 ctxt.pf_num = pf->hw.pf_id;
9285 ctxt.vf_num = 0;
9286 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9287 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9288 if (ret) {
9289 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009290 "update vsi failed, err %s aq_err %s\n",
9291 i40e_stat_str(&pf->hw, ret),
9292 i40e_aq_str(&pf->hw,
9293 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009294 ret = -ENOENT;
9295 goto err;
9296 }
9297 /* update the local VSI info queue map */
9298 i40e_vsi_update_queue_map(vsi, &ctxt);
9299 vsi->info.valid_sections = 0;
9300 } else {
9301 /* Default/Main VSI is only enabled for TC0
9302 * reconfigure it to enable all TCs that are
9303 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009304 * For MFP case the iSCSI PF would use this
9305 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009306 */
9307 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9308 if (ret) {
9309 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009310 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9311 enabled_tc,
9312 i40e_stat_str(&pf->hw, ret),
9313 i40e_aq_str(&pf->hw,
9314 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009315 ret = -ENOENT;
9316 }
9317 }
9318 break;
9319
9320 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009321 ctxt.pf_num = hw->pf_id;
9322 ctxt.vf_num = 0;
9323 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009324 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009325 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009326 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9327 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009328 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009329 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009330 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009331 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009332 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009333 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009334 break;
9335
9336 case I40E_VSI_VMDQ2:
9337 ctxt.pf_num = hw->pf_id;
9338 ctxt.vf_num = 0;
9339 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009340 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009341 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9342
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009343 /* This VSI is connected to VEB so the switch_id
9344 * should be set to zero by default.
9345 */
Neerav Parikh51616012015-02-06 08:52:14 +00009346 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9347 ctxt.info.valid_sections |=
9348 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9349 ctxt.info.switch_id =
9350 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9351 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009352
9353 /* Setup the VSI tx/rx queue map for TC0 only for now */
9354 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9355 break;
9356
9357 case I40E_VSI_SRIOV:
9358 ctxt.pf_num = hw->pf_id;
9359 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9360 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009361 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009362 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009364 /* This VSI is connected to VEB so the switch_id
9365 * should be set to zero by default.
9366 */
Neerav Parikh51616012015-02-06 08:52:14 +00009367 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9368 ctxt.info.valid_sections |=
9369 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9370 ctxt.info.switch_id =
9371 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9372 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009373
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009374 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9375 ctxt.info.valid_sections |=
9376 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9377 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009378 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9379 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009380 }
9381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009382 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9383 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009384 if (pf->vf[vsi->vf_id].spoofchk) {
9385 ctxt.info.valid_sections |=
9386 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9387 ctxt.info.sec_flags |=
9388 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9389 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9390 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009391 /* Setup the VSI tx/rx queue map for TC0 only for now */
9392 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9393 break;
9394
Vasu Dev38e00432014-08-01 13:27:03 -07009395#ifdef I40E_FCOE
9396 case I40E_VSI_FCOE:
9397 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9398 if (ret) {
9399 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9400 return ret;
9401 }
9402 break;
9403
9404#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009405 case I40E_VSI_IWARP:
9406 /* send down message to iWARP */
9407 break;
9408
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009409 default:
9410 return -ENODEV;
9411 }
9412
9413 if (vsi->type != I40E_VSI_MAIN) {
9414 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9415 if (ret) {
9416 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009417 "add vsi failed, err %s aq_err %s\n",
9418 i40e_stat_str(&pf->hw, ret),
9419 i40e_aq_str(&pf->hw,
9420 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009421 ret = -ENOENT;
9422 goto err;
9423 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009424 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009425 vsi->info.valid_sections = 0;
9426 vsi->seid = ctxt.seid;
9427 vsi->id = ctxt.vsi_number;
9428 }
Kiran Patilf6bd0962016-06-20 09:10:34 -07009429 /* Except FDIR VSI, for all othet VSI set the broadcast filter */
9430 if (vsi->type != I40E_VSI_FDIR) {
9431 aq_ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
9432 if (aq_ret) {
9433 ret = i40e_aq_rc_to_posix(aq_ret,
9434 hw->aq.asq_last_status);
9435 dev_info(&pf->pdev->dev,
9436 "set brdcast promisc failed, err %s, aq_err %s\n",
9437 i40e_stat_str(hw, aq_ret),
9438 i40e_aq_str(hw, hw->aq.asq_last_status));
9439 }
9440 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009441
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009442 vsi->active_filters = 0;
9443 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009444 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009445 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009446 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009447 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009448 f_count++;
9449 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009450 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009452 if (f_count) {
9453 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9454 pf->flags |= I40E_FLAG_FILTER_SYNC;
9455 }
9456
9457 /* Update VSI BW information */
9458 ret = i40e_vsi_get_bw_info(vsi);
9459 if (ret) {
9460 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009461 "couldn't get vsi bw info, err %s aq_err %s\n",
9462 i40e_stat_str(&pf->hw, ret),
9463 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009464 /* VSI is already added so not tearing that up */
9465 ret = 0;
9466 }
9467
9468err:
9469 return ret;
9470}
9471
9472/**
9473 * i40e_vsi_release - Delete a VSI and free its resources
9474 * @vsi: the VSI being removed
9475 *
9476 * Returns 0 on success or < 0 on error
9477 **/
9478int i40e_vsi_release(struct i40e_vsi *vsi)
9479{
Jacob Keller278e7d02016-10-05 09:30:37 -07009480 struct i40e_mac_filter *f;
9481 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009482 struct i40e_veb *veb = NULL;
9483 struct i40e_pf *pf;
9484 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009485 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009486
9487 pf = vsi->back;
9488
9489 /* release of a VEB-owner or last VSI is not allowed */
9490 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9491 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9492 vsi->seid, vsi->uplink_seid);
9493 return -ENODEV;
9494 }
9495 if (vsi == pf->vsi[pf->lan_vsi] &&
9496 !test_bit(__I40E_DOWN, &pf->state)) {
9497 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9498 return -ENODEV;
9499 }
9500
9501 uplink_seid = vsi->uplink_seid;
9502 if (vsi->type != I40E_VSI_SRIOV) {
9503 if (vsi->netdev_registered) {
9504 vsi->netdev_registered = false;
9505 if (vsi->netdev) {
9506 /* results in a call to i40e_close() */
9507 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009508 }
9509 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009510 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009511 }
9512 i40e_vsi_disable_irq(vsi);
9513 }
9514
Jacob Keller278e7d02016-10-05 09:30:37 -07009515 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009516
9517 /* clear the sync flag on all filters */
9518 if (vsi->netdev) {
9519 __dev_uc_unsync(vsi->netdev, NULL);
9520 __dev_mc_unsync(vsi->netdev, NULL);
9521 }
9522
9523 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009524 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009525 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009526
Jacob Keller278e7d02016-10-05 09:30:37 -07009527 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009528
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009529 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530
9531 i40e_vsi_delete(vsi);
9532 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009533 if (vsi->netdev) {
9534 free_netdev(vsi->netdev);
9535 vsi->netdev = NULL;
9536 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009537 i40e_vsi_clear_rings(vsi);
9538 i40e_vsi_clear(vsi);
9539
9540 /* If this was the last thing on the VEB, except for the
9541 * controlling VSI, remove the VEB, which puts the controlling
9542 * VSI onto the next level down in the switch.
9543 *
9544 * Well, okay, there's one more exception here: don't remove
9545 * the orphan VEBs yet. We'll wait for an explicit remove request
9546 * from up the network stack.
9547 */
Mitch Williams505682c2014-05-20 08:01:37 +00009548 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009549 if (pf->vsi[i] &&
9550 pf->vsi[i]->uplink_seid == uplink_seid &&
9551 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9552 n++; /* count the VSIs */
9553 }
9554 }
9555 for (i = 0; i < I40E_MAX_VEB; i++) {
9556 if (!pf->veb[i])
9557 continue;
9558 if (pf->veb[i]->uplink_seid == uplink_seid)
9559 n++; /* count the VEBs */
9560 if (pf->veb[i]->seid == uplink_seid)
9561 veb = pf->veb[i];
9562 }
9563 if (n == 0 && veb && veb->uplink_seid != 0)
9564 i40e_veb_release(veb);
9565
9566 return 0;
9567}
9568
9569/**
9570 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9571 * @vsi: ptr to the VSI
9572 *
9573 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9574 * corresponding SW VSI structure and initializes num_queue_pairs for the
9575 * newly allocated VSI.
9576 *
9577 * Returns 0 on success or negative on failure
9578 **/
9579static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9580{
9581 int ret = -ENOENT;
9582 struct i40e_pf *pf = vsi->back;
9583
Alexander Duyck493fb302013-09-28 07:01:44 +00009584 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009585 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9586 vsi->seid);
9587 return -EEXIST;
9588 }
9589
9590 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009591 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009592 vsi->seid, vsi->base_vector);
9593 return -EEXIST;
9594 }
9595
Greg Rose90e04072014-03-06 08:59:57 +00009596 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009597 if (ret) {
9598 dev_info(&pf->pdev->dev,
9599 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9600 vsi->num_q_vectors, vsi->seid, ret);
9601 vsi->num_q_vectors = 0;
9602 goto vector_setup_out;
9603 }
9604
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009605 /* In Legacy mode, we do not have to get any other vector since we
9606 * piggyback on the misc/ICR0 for queue interrupts.
9607 */
9608 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9609 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009610 if (vsi->num_q_vectors)
9611 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9612 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009613 if (vsi->base_vector < 0) {
9614 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009615 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9616 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 i40e_vsi_free_q_vectors(vsi);
9618 ret = -ENOENT;
9619 goto vector_setup_out;
9620 }
9621
9622vector_setup_out:
9623 return ret;
9624}
9625
9626/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009627 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9628 * @vsi: pointer to the vsi.
9629 *
9630 * This re-allocates a vsi's queue resources.
9631 *
9632 * Returns pointer to the successfully allocated and configured VSI sw struct
9633 * on success, otherwise returns NULL on failure.
9634 **/
9635static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9636{
John Underwoodf5340392016-02-18 09:19:24 -08009637 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009638 u8 enabled_tc;
9639 int ret;
9640
John Underwoodf5340392016-02-18 09:19:24 -08009641 if (!vsi)
9642 return NULL;
9643
9644 pf = vsi->back;
9645
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009646 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9647 i40e_vsi_clear_rings(vsi);
9648
9649 i40e_vsi_free_arrays(vsi, false);
9650 i40e_set_num_rings_in_vsi(vsi);
9651 ret = i40e_vsi_alloc_arrays(vsi, false);
9652 if (ret)
9653 goto err_vsi;
9654
9655 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9656 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009657 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009658 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009659 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009660 goto err_vsi;
9661 }
9662 vsi->base_queue = ret;
9663
9664 /* Update the FW view of the VSI. Force a reset of TC and queue
9665 * layout configurations.
9666 */
9667 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9668 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9669 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9670 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9671
9672 /* assign it some queues */
9673 ret = i40e_alloc_rings(vsi);
9674 if (ret)
9675 goto err_rings;
9676
9677 /* map all of the rings to the q_vectors */
9678 i40e_vsi_map_rings_to_vectors(vsi);
9679 return vsi;
9680
9681err_rings:
9682 i40e_vsi_free_q_vectors(vsi);
9683 if (vsi->netdev_registered) {
9684 vsi->netdev_registered = false;
9685 unregister_netdev(vsi->netdev);
9686 free_netdev(vsi->netdev);
9687 vsi->netdev = NULL;
9688 }
9689 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9690err_vsi:
9691 i40e_vsi_clear(vsi);
9692 return NULL;
9693}
9694
9695/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009696 * i40e_vsi_setup - Set up a VSI by a given type
9697 * @pf: board private structure
9698 * @type: VSI type
9699 * @uplink_seid: the switch element to link to
9700 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9701 *
9702 * This allocates the sw VSI structure and its queue resources, then add a VSI
9703 * to the identified VEB.
9704 *
9705 * Returns pointer to the successfully allocated and configure VSI sw struct on
9706 * success, otherwise returns NULL on failure.
9707 **/
9708struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9709 u16 uplink_seid, u32 param1)
9710{
9711 struct i40e_vsi *vsi = NULL;
9712 struct i40e_veb *veb = NULL;
9713 int ret, i;
9714 int v_idx;
9715
9716 /* The requested uplink_seid must be either
9717 * - the PF's port seid
9718 * no VEB is needed because this is the PF
9719 * or this is a Flow Director special case VSI
9720 * - seid of an existing VEB
9721 * - seid of a VSI that owns an existing VEB
9722 * - seid of a VSI that doesn't own a VEB
9723 * a new VEB is created and the VSI becomes the owner
9724 * - seid of the PF VSI, which is what creates the first VEB
9725 * this is a special case of the previous
9726 *
9727 * Find which uplink_seid we were given and create a new VEB if needed
9728 */
9729 for (i = 0; i < I40E_MAX_VEB; i++) {
9730 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9731 veb = pf->veb[i];
9732 break;
9733 }
9734 }
9735
9736 if (!veb && uplink_seid != pf->mac_seid) {
9737
Mitch Williams505682c2014-05-20 08:01:37 +00009738 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009739 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9740 vsi = pf->vsi[i];
9741 break;
9742 }
9743 }
9744 if (!vsi) {
9745 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9746 uplink_seid);
9747 return NULL;
9748 }
9749
9750 if (vsi->uplink_seid == pf->mac_seid)
9751 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9752 vsi->tc_config.enabled_tc);
9753 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9754 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9755 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009756 if (veb) {
9757 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9758 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009759 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009760 return NULL;
9761 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009762 /* We come up by default in VEPA mode if SRIOV is not
9763 * already enabled, in which case we can't force VEPA
9764 * mode.
9765 */
9766 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9767 veb->bridge_mode = BRIDGE_MODE_VEPA;
9768 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9769 }
Neerav Parikh51616012015-02-06 08:52:14 +00009770 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009771 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009772 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9773 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9774 veb = pf->veb[i];
9775 }
9776 if (!veb) {
9777 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9778 return NULL;
9779 }
9780
9781 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9782 uplink_seid = veb->seid;
9783 }
9784
9785 /* get vsi sw struct */
9786 v_idx = i40e_vsi_mem_alloc(pf, type);
9787 if (v_idx < 0)
9788 goto err_alloc;
9789 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009790 if (!vsi)
9791 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009792 vsi->type = type;
9793 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9794
9795 if (type == I40E_VSI_MAIN)
9796 pf->lan_vsi = v_idx;
9797 else if (type == I40E_VSI_SRIOV)
9798 vsi->vf_id = param1;
9799 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009800 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9801 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009802 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009803 dev_info(&pf->pdev->dev,
9804 "failed to get tracking for %d queues for VSI %d err=%d\n",
9805 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009806 goto err_vsi;
9807 }
9808 vsi->base_queue = ret;
9809
9810 /* get a VSI from the hardware */
9811 vsi->uplink_seid = uplink_seid;
9812 ret = i40e_add_vsi(vsi);
9813 if (ret)
9814 goto err_vsi;
9815
9816 switch (vsi->type) {
9817 /* setup the netdev if needed */
9818 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009819 /* Apply relevant filters if a platform-specific mac
9820 * address was selected.
9821 */
9822 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9823 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9824 if (ret) {
9825 dev_warn(&pf->pdev->dev,
9826 "could not set up macaddr; err %d\n",
9827 ret);
9828 }
9829 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009830 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009831 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009832 ret = i40e_config_netdev(vsi);
9833 if (ret)
9834 goto err_netdev;
9835 ret = register_netdev(vsi->netdev);
9836 if (ret)
9837 goto err_netdev;
9838 vsi->netdev_registered = true;
9839 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009840#ifdef CONFIG_I40E_DCB
9841 /* Setup DCB netlink interface */
9842 i40e_dcbnl_setup(vsi);
9843#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009844 /* fall through */
9845
9846 case I40E_VSI_FDIR:
9847 /* set up vectors and rings if needed */
9848 ret = i40e_vsi_setup_vectors(vsi);
9849 if (ret)
9850 goto err_msix;
9851
9852 ret = i40e_alloc_rings(vsi);
9853 if (ret)
9854 goto err_rings;
9855
9856 /* map all of the rings to the q_vectors */
9857 i40e_vsi_map_rings_to_vectors(vsi);
9858
9859 i40e_vsi_reset_stats(vsi);
9860 break;
9861
9862 default:
9863 /* no netdev or rings for the other VSI types */
9864 break;
9865 }
9866
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009867 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9868 (vsi->type == I40E_VSI_VMDQ2)) {
9869 ret = i40e_vsi_config_rss(vsi);
9870 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009871 return vsi;
9872
9873err_rings:
9874 i40e_vsi_free_q_vectors(vsi);
9875err_msix:
9876 if (vsi->netdev_registered) {
9877 vsi->netdev_registered = false;
9878 unregister_netdev(vsi->netdev);
9879 free_netdev(vsi->netdev);
9880 vsi->netdev = NULL;
9881 }
9882err_netdev:
9883 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9884err_vsi:
9885 i40e_vsi_clear(vsi);
9886err_alloc:
9887 return NULL;
9888}
9889
9890/**
9891 * i40e_veb_get_bw_info - Query VEB BW information
9892 * @veb: the veb to query
9893 *
9894 * Query the Tx scheduler BW configuration data for given VEB
9895 **/
9896static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9897{
9898 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9899 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9900 struct i40e_pf *pf = veb->pf;
9901 struct i40e_hw *hw = &pf->hw;
9902 u32 tc_bw_max;
9903 int ret = 0;
9904 int i;
9905
9906 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9907 &bw_data, NULL);
9908 if (ret) {
9909 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009910 "query veb bw config failed, err %s aq_err %s\n",
9911 i40e_stat_str(&pf->hw, ret),
9912 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009913 goto out;
9914 }
9915
9916 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9917 &ets_data, NULL);
9918 if (ret) {
9919 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009920 "query veb bw ets config failed, err %s aq_err %s\n",
9921 i40e_stat_str(&pf->hw, ret),
9922 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009923 goto out;
9924 }
9925
9926 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9927 veb->bw_max_quanta = ets_data.tc_bw_max;
9928 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009929 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009930 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9931 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9932 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9933 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9934 veb->bw_tc_limit_credits[i] =
9935 le16_to_cpu(bw_data.tc_bw_limits[i]);
9936 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9937 }
9938
9939out:
9940 return ret;
9941}
9942
9943/**
9944 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9945 * @pf: board private structure
9946 *
9947 * On error: returns error code (negative)
9948 * On success: returns vsi index in PF (positive)
9949 **/
9950static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9951{
9952 int ret = -ENOENT;
9953 struct i40e_veb *veb;
9954 int i;
9955
9956 /* Need to protect the allocation of switch elements at the PF level */
9957 mutex_lock(&pf->switch_mutex);
9958
9959 /* VEB list may be fragmented if VEB creation/destruction has
9960 * been happening. We can afford to do a quick scan to look
9961 * for any free slots in the list.
9962 *
9963 * find next empty veb slot, looping back around if necessary
9964 */
9965 i = 0;
9966 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9967 i++;
9968 if (i >= I40E_MAX_VEB) {
9969 ret = -ENOMEM;
9970 goto err_alloc_veb; /* out of VEB slots! */
9971 }
9972
9973 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9974 if (!veb) {
9975 ret = -ENOMEM;
9976 goto err_alloc_veb;
9977 }
9978 veb->pf = pf;
9979 veb->idx = i;
9980 veb->enabled_tc = 1;
9981
9982 pf->veb[i] = veb;
9983 ret = i;
9984err_alloc_veb:
9985 mutex_unlock(&pf->switch_mutex);
9986 return ret;
9987}
9988
9989/**
9990 * i40e_switch_branch_release - Delete a branch of the switch tree
9991 * @branch: where to start deleting
9992 *
9993 * This uses recursion to find the tips of the branch to be
9994 * removed, deleting until we get back to and can delete this VEB.
9995 **/
9996static void i40e_switch_branch_release(struct i40e_veb *branch)
9997{
9998 struct i40e_pf *pf = branch->pf;
9999 u16 branch_seid = branch->seid;
10000 u16 veb_idx = branch->idx;
10001 int i;
10002
10003 /* release any VEBs on this VEB - RECURSION */
10004 for (i = 0; i < I40E_MAX_VEB; i++) {
10005 if (!pf->veb[i])
10006 continue;
10007 if (pf->veb[i]->uplink_seid == branch->seid)
10008 i40e_switch_branch_release(pf->veb[i]);
10009 }
10010
10011 /* Release the VSIs on this VEB, but not the owner VSI.
10012 *
10013 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10014 * the VEB itself, so don't use (*branch) after this loop.
10015 */
Mitch Williams505682c2014-05-20 08:01:37 +000010016 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010017 if (!pf->vsi[i])
10018 continue;
10019 if (pf->vsi[i]->uplink_seid == branch_seid &&
10020 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10021 i40e_vsi_release(pf->vsi[i]);
10022 }
10023 }
10024
10025 /* There's one corner case where the VEB might not have been
10026 * removed, so double check it here and remove it if needed.
10027 * This case happens if the veb was created from the debugfs
10028 * commands and no VSIs were added to it.
10029 */
10030 if (pf->veb[veb_idx])
10031 i40e_veb_release(pf->veb[veb_idx]);
10032}
10033
10034/**
10035 * i40e_veb_clear - remove veb struct
10036 * @veb: the veb to remove
10037 **/
10038static void i40e_veb_clear(struct i40e_veb *veb)
10039{
10040 if (!veb)
10041 return;
10042
10043 if (veb->pf) {
10044 struct i40e_pf *pf = veb->pf;
10045
10046 mutex_lock(&pf->switch_mutex);
10047 if (pf->veb[veb->idx] == veb)
10048 pf->veb[veb->idx] = NULL;
10049 mutex_unlock(&pf->switch_mutex);
10050 }
10051
10052 kfree(veb);
10053}
10054
10055/**
10056 * i40e_veb_release - Delete a VEB and free its resources
10057 * @veb: the VEB being removed
10058 **/
10059void i40e_veb_release(struct i40e_veb *veb)
10060{
10061 struct i40e_vsi *vsi = NULL;
10062 struct i40e_pf *pf;
10063 int i, n = 0;
10064
10065 pf = veb->pf;
10066
10067 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010068 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010069 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10070 n++;
10071 vsi = pf->vsi[i];
10072 }
10073 }
10074 if (n != 1) {
10075 dev_info(&pf->pdev->dev,
10076 "can't remove VEB %d with %d VSIs left\n",
10077 veb->seid, n);
10078 return;
10079 }
10080
10081 /* move the remaining VSI to uplink veb */
10082 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10083 if (veb->uplink_seid) {
10084 vsi->uplink_seid = veb->uplink_seid;
10085 if (veb->uplink_seid == pf->mac_seid)
10086 vsi->veb_idx = I40E_NO_VEB;
10087 else
10088 vsi->veb_idx = veb->veb_idx;
10089 } else {
10090 /* floating VEB */
10091 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10092 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10093 }
10094
10095 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10096 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010097}
10098
10099/**
10100 * i40e_add_veb - create the VEB in the switch
10101 * @veb: the VEB to be instantiated
10102 * @vsi: the controlling VSI
10103 **/
10104static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10105{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010106 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010107 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010108 int ret;
10109
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010110 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010111 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010112 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010113
10114 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010115 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010116 dev_info(&pf->pdev->dev,
10117 "couldn't add VEB, err %s aq_err %s\n",
10118 i40e_stat_str(&pf->hw, ret),
10119 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010120 return -EPERM;
10121 }
10122
10123 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010124 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010125 &veb->stats_idx, NULL, NULL, NULL);
10126 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010127 dev_info(&pf->pdev->dev,
10128 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10129 i40e_stat_str(&pf->hw, ret),
10130 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010131 return -EPERM;
10132 }
10133 ret = i40e_veb_get_bw_info(veb);
10134 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010135 dev_info(&pf->pdev->dev,
10136 "couldn't get VEB bw info, err %s aq_err %s\n",
10137 i40e_stat_str(&pf->hw, ret),
10138 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10139 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010140 return -ENOENT;
10141 }
10142
10143 vsi->uplink_seid = veb->seid;
10144 vsi->veb_idx = veb->idx;
10145 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10146
10147 return 0;
10148}
10149
10150/**
10151 * i40e_veb_setup - Set up a VEB
10152 * @pf: board private structure
10153 * @flags: VEB setup flags
10154 * @uplink_seid: the switch element to link to
10155 * @vsi_seid: the initial VSI seid
10156 * @enabled_tc: Enabled TC bit-map
10157 *
10158 * This allocates the sw VEB structure and links it into the switch
10159 * It is possible and legal for this to be a duplicate of an already
10160 * existing VEB. It is also possible for both uplink and vsi seids
10161 * to be zero, in order to create a floating VEB.
10162 *
10163 * Returns pointer to the successfully allocated VEB sw struct on
10164 * success, otherwise returns NULL on failure.
10165 **/
10166struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10167 u16 uplink_seid, u16 vsi_seid,
10168 u8 enabled_tc)
10169{
10170 struct i40e_veb *veb, *uplink_veb = NULL;
10171 int vsi_idx, veb_idx;
10172 int ret;
10173
10174 /* if one seid is 0, the other must be 0 to create a floating relay */
10175 if ((uplink_seid == 0 || vsi_seid == 0) &&
10176 (uplink_seid + vsi_seid != 0)) {
10177 dev_info(&pf->pdev->dev,
10178 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10179 uplink_seid, vsi_seid);
10180 return NULL;
10181 }
10182
10183 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010184 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010185 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10186 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010187 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010188 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10189 vsi_seid);
10190 return NULL;
10191 }
10192
10193 if (uplink_seid && uplink_seid != pf->mac_seid) {
10194 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10195 if (pf->veb[veb_idx] &&
10196 pf->veb[veb_idx]->seid == uplink_seid) {
10197 uplink_veb = pf->veb[veb_idx];
10198 break;
10199 }
10200 }
10201 if (!uplink_veb) {
10202 dev_info(&pf->pdev->dev,
10203 "uplink seid %d not found\n", uplink_seid);
10204 return NULL;
10205 }
10206 }
10207
10208 /* get veb sw struct */
10209 veb_idx = i40e_veb_mem_alloc(pf);
10210 if (veb_idx < 0)
10211 goto err_alloc;
10212 veb = pf->veb[veb_idx];
10213 veb->flags = flags;
10214 veb->uplink_seid = uplink_seid;
10215 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10216 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10217
10218 /* create the VEB in the switch */
10219 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10220 if (ret)
10221 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010222 if (vsi_idx == pf->lan_vsi)
10223 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010224
10225 return veb;
10226
10227err_veb:
10228 i40e_veb_clear(veb);
10229err_alloc:
10230 return NULL;
10231}
10232
10233/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010234 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010235 * @pf: board private structure
10236 * @ele: element we are building info from
10237 * @num_reported: total number of elements
10238 * @printconfig: should we print the contents
10239 *
10240 * helper function to assist in extracting a few useful SEID values.
10241 **/
10242static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10243 struct i40e_aqc_switch_config_element_resp *ele,
10244 u16 num_reported, bool printconfig)
10245{
10246 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10247 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10248 u8 element_type = ele->element_type;
10249 u16 seid = le16_to_cpu(ele->seid);
10250
10251 if (printconfig)
10252 dev_info(&pf->pdev->dev,
10253 "type=%d seid=%d uplink=%d downlink=%d\n",
10254 element_type, seid, uplink_seid, downlink_seid);
10255
10256 switch (element_type) {
10257 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10258 pf->mac_seid = seid;
10259 break;
10260 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10261 /* Main VEB? */
10262 if (uplink_seid != pf->mac_seid)
10263 break;
10264 if (pf->lan_veb == I40E_NO_VEB) {
10265 int v;
10266
10267 /* find existing or else empty VEB */
10268 for (v = 0; v < I40E_MAX_VEB; v++) {
10269 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10270 pf->lan_veb = v;
10271 break;
10272 }
10273 }
10274 if (pf->lan_veb == I40E_NO_VEB) {
10275 v = i40e_veb_mem_alloc(pf);
10276 if (v < 0)
10277 break;
10278 pf->lan_veb = v;
10279 }
10280 }
10281
10282 pf->veb[pf->lan_veb]->seid = seid;
10283 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10284 pf->veb[pf->lan_veb]->pf = pf;
10285 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10286 break;
10287 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10288 if (num_reported != 1)
10289 break;
10290 /* This is immediately after a reset so we can assume this is
10291 * the PF's VSI
10292 */
10293 pf->mac_seid = uplink_seid;
10294 pf->pf_seid = downlink_seid;
10295 pf->main_vsi_seid = seid;
10296 if (printconfig)
10297 dev_info(&pf->pdev->dev,
10298 "pf_seid=%d main_vsi_seid=%d\n",
10299 pf->pf_seid, pf->main_vsi_seid);
10300 break;
10301 case I40E_SWITCH_ELEMENT_TYPE_PF:
10302 case I40E_SWITCH_ELEMENT_TYPE_VF:
10303 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10304 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10305 case I40E_SWITCH_ELEMENT_TYPE_PE:
10306 case I40E_SWITCH_ELEMENT_TYPE_PA:
10307 /* ignore these for now */
10308 break;
10309 default:
10310 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10311 element_type, seid);
10312 break;
10313 }
10314}
10315
10316/**
10317 * i40e_fetch_switch_configuration - Get switch config from firmware
10318 * @pf: board private structure
10319 * @printconfig: should we print the contents
10320 *
10321 * Get the current switch configuration from the device and
10322 * extract a few useful SEID values.
10323 **/
10324int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10325{
10326 struct i40e_aqc_get_switch_config_resp *sw_config;
10327 u16 next_seid = 0;
10328 int ret = 0;
10329 u8 *aq_buf;
10330 int i;
10331
10332 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10333 if (!aq_buf)
10334 return -ENOMEM;
10335
10336 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10337 do {
10338 u16 num_reported, num_total;
10339
10340 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10341 I40E_AQ_LARGE_BUF,
10342 &next_seid, NULL);
10343 if (ret) {
10344 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010345 "get switch config failed err %s aq_err %s\n",
10346 i40e_stat_str(&pf->hw, ret),
10347 i40e_aq_str(&pf->hw,
10348 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010349 kfree(aq_buf);
10350 return -ENOENT;
10351 }
10352
10353 num_reported = le16_to_cpu(sw_config->header.num_reported);
10354 num_total = le16_to_cpu(sw_config->header.num_total);
10355
10356 if (printconfig)
10357 dev_info(&pf->pdev->dev,
10358 "header: %d reported %d total\n",
10359 num_reported, num_total);
10360
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010361 for (i = 0; i < num_reported; i++) {
10362 struct i40e_aqc_switch_config_element_resp *ele =
10363 &sw_config->element[i];
10364
10365 i40e_setup_pf_switch_element(pf, ele, num_reported,
10366 printconfig);
10367 }
10368 } while (next_seid != 0);
10369
10370 kfree(aq_buf);
10371 return ret;
10372}
10373
10374/**
10375 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10376 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010377 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010378 *
10379 * Returns 0 on success, negative value on failure
10380 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010381static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010382{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010383 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010384 int ret;
10385
10386 /* find out what's out there already */
10387 ret = i40e_fetch_switch_configuration(pf, false);
10388 if (ret) {
10389 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010390 "couldn't fetch switch config, err %s aq_err %s\n",
10391 i40e_stat_str(&pf->hw, ret),
10392 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010393 return ret;
10394 }
10395 i40e_pf_reset_stats(pf);
10396
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010397 /* set the switch config bit for the whole device to
10398 * support limited promisc or true promisc
10399 * when user requests promisc. The default is limited
10400 * promisc.
10401 */
10402
10403 if ((pf->hw.pf_id == 0) &&
10404 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10405 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10406
10407 if (pf->hw.pf_id == 0) {
10408 u16 valid_flags;
10409
10410 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10411 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10412 NULL);
10413 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10414 dev_info(&pf->pdev->dev,
10415 "couldn't set switch config bits, err %s aq_err %s\n",
10416 i40e_stat_str(&pf->hw, ret),
10417 i40e_aq_str(&pf->hw,
10418 pf->hw.aq.asq_last_status));
10419 /* not a fatal problem, just keep going */
10420 }
10421 }
10422
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010423 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010424 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010425 struct i40e_vsi *vsi = NULL;
10426 u16 uplink_seid;
10427
10428 /* Set up the PF VSI associated with the PF's main VSI
10429 * that is already in the HW switch
10430 */
10431 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10432 uplink_seid = pf->veb[pf->lan_veb]->seid;
10433 else
10434 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010435 if (pf->lan_vsi == I40E_NO_VSI)
10436 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10437 else if (reinit)
10438 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010439 if (!vsi) {
10440 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10441 i40e_fdir_teardown(pf);
10442 return -EAGAIN;
10443 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010444 } else {
10445 /* force a reset of TC and queue layout configurations */
10446 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010447
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010448 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10449 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10450 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10451 }
10452 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10453
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010454 i40e_fdir_sb_setup(pf);
10455
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010456 /* Setup static PF queue filter control settings */
10457 ret = i40e_setup_pf_filter_control(pf);
10458 if (ret) {
10459 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10460 ret);
10461 /* Failure here should not stop continuing other steps */
10462 }
10463
10464 /* enable RSS in the HW, even for only one queue, as the stack can use
10465 * the hash
10466 */
10467 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010468 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010469
10470 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010471 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010472 i40e_link_event(pf);
10473
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010474 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010475 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10476 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010477
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010478 i40e_ptp_init(pf);
10479
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010480 return ret;
10481}
10482
10483/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010484 * i40e_determine_queue_usage - Work out queue distribution
10485 * @pf: board private structure
10486 **/
10487static void i40e_determine_queue_usage(struct i40e_pf *pf)
10488{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010489 int queues_left;
10490
10491 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010492#ifdef I40E_FCOE
10493 pf->num_fcoe_qps = 0;
10494#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010495
10496 /* Find the max queues to be put into basic use. We'll always be
10497 * using TC0, whether or not DCB is running, and TC0 will get the
10498 * big RSS set.
10499 */
10500 queues_left = pf->hw.func_caps.num_tx_qp;
10501
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010502 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010503 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010504 /* one qp for PF, no queues for anything else */
10505 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010506 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010507
10508 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010509 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010510 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010511#ifdef I40E_FCOE
10512 I40E_FLAG_FCOE_ENABLED |
10513#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010514 I40E_FLAG_FD_SB_ENABLED |
10515 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010516 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010517 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010518 I40E_FLAG_SRIOV_ENABLED |
10519 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010520 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10521 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010522 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010523 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010524 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010525 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010526 queues_left -= pf->num_lan_qps;
10527
10528 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010529 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010530#ifdef I40E_FCOE
10531 I40E_FLAG_FCOE_ENABLED |
10532#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010533 I40E_FLAG_FD_SB_ENABLED |
10534 I40E_FLAG_FD_ATR_ENABLED |
10535 I40E_FLAG_DCB_ENABLED |
10536 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010537 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010538 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010539 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010540 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010541 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10542 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010543 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10544 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010545 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10546 num_online_cpus());
10547 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10548 pf->hw.func_caps.num_tx_qp);
10549
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010550 queues_left -= pf->num_lan_qps;
10551 }
10552
Vasu Dev38e00432014-08-01 13:27:03 -070010553#ifdef I40E_FCOE
10554 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10555 if (I40E_DEFAULT_FCOE <= queues_left) {
10556 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10557 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10558 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10559 } else {
10560 pf->num_fcoe_qps = 0;
10561 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10562 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10563 }
10564
10565 queues_left -= pf->num_fcoe_qps;
10566 }
10567
10568#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010569 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10570 if (queues_left > 1) {
10571 queues_left -= 1; /* save 1 queue for FD */
10572 } else {
10573 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10574 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10575 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010576 }
10577
10578 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10579 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010580 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10581 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010582 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10583 }
10584
10585 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10586 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10587 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10588 (queues_left / pf->num_vmdq_qps));
10589 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10590 }
10591
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010592 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010593 dev_dbg(&pf->pdev->dev,
10594 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10595 pf->hw.func_caps.num_tx_qp,
10596 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010597 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10598 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10599 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010600#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010601 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010602#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010603}
10604
10605/**
10606 * i40e_setup_pf_filter_control - Setup PF static filter control
10607 * @pf: PF to be setup
10608 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010609 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010610 * settings. If PE/FCoE are enabled then it will also set the per PF
10611 * based filter sizes required for them. It also enables Flow director,
10612 * ethertype and macvlan type filter settings for the pf.
10613 *
10614 * Returns 0 on success, negative on failure
10615 **/
10616static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10617{
10618 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10619
10620 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10621
10622 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010623 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010624 settings->enable_fdir = true;
10625
10626 /* Ethtype and MACVLAN filters enabled for PF */
10627 settings->enable_ethtype = true;
10628 settings->enable_macvlan = true;
10629
10630 if (i40e_set_filter_control(&pf->hw, settings))
10631 return -ENOENT;
10632
10633 return 0;
10634}
10635
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010636#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010637#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010638static void i40e_print_features(struct i40e_pf *pf)
10639{
10640 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010641 char *buf;
10642 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010643
Joe Perches3b195842015-12-03 04:20:57 -080010644 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10645 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010646 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010647
Joe Perches3b195842015-12-03 04:20:57 -080010648 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010649#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010650 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010651#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010652 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010653 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010654 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010655 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010656 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010657 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010658 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010659 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010660 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10661 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010662 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010663 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010664 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010665 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010666 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010667 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010668 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010669#ifdef I40E_FCOE
10670 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010671 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010672#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010673 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010674 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010675 else
Joe Perches3b195842015-12-03 04:20:57 -080010676 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010677
Joe Perches3b195842015-12-03 04:20:57 -080010678 dev_info(&pf->pdev->dev, "%s\n", buf);
10679 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010680 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010681}
10682
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010683/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010684 * i40e_get_platform_mac_addr - get platform-specific MAC address
10685 *
10686 * @pdev: PCI device information struct
10687 * @pf: board private structure
10688 *
10689 * Look up the MAC address in Open Firmware on systems that support it,
10690 * and use IDPROM on SPARC if no OF address is found. On return, the
10691 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10692 * has been selected.
10693 **/
10694static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10695{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010696 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010697 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010698 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010699}
10700
10701/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010702 * i40e_probe - Device initialization routine
10703 * @pdev: PCI device information struct
10704 * @ent: entry in i40e_pci_tbl
10705 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010706 * i40e_probe initializes a PF identified by a pci_dev structure.
10707 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010708 * and a hardware reset occur.
10709 *
10710 * Returns 0 on success, negative on failure
10711 **/
10712static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10713{
Catherine Sullivane8278452015-02-06 08:52:08 +000010714 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010715 struct i40e_pf *pf;
10716 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010717 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010718 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010719 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010720 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010721 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010722 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010723 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010724
10725 err = pci_enable_device_mem(pdev);
10726 if (err)
10727 return err;
10728
10729 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010730 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010731 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010732 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10733 if (err) {
10734 dev_err(&pdev->dev,
10735 "DMA configuration failed: 0x%x\n", err);
10736 goto err_dma;
10737 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010738 }
10739
10740 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010741 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010742 if (err) {
10743 dev_info(&pdev->dev,
10744 "pci_request_selected_regions failed %d\n", err);
10745 goto err_pci_reg;
10746 }
10747
10748 pci_enable_pcie_error_reporting(pdev);
10749 pci_set_master(pdev);
10750
10751 /* Now that we have a PCI connection, we need to do the
10752 * low level device setup. This is primarily setting up
10753 * the Admin Queue structures and then querying for the
10754 * device's current profile information.
10755 */
10756 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10757 if (!pf) {
10758 err = -ENOMEM;
10759 goto err_pf_alloc;
10760 }
10761 pf->next_vsi = 0;
10762 pf->pdev = pdev;
10763 set_bit(__I40E_DOWN, &pf->state);
10764
10765 hw = &pf->hw;
10766 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010767
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010768 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10769 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010770
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010771 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010772 if (!hw->hw_addr) {
10773 err = -EIO;
10774 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10775 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010776 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010777 goto err_ioremap;
10778 }
10779 hw->vendor_id = pdev->vendor;
10780 hw->device_id = pdev->device;
10781 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10782 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10783 hw->subsystem_device_id = pdev->subsystem_device;
10784 hw->bus.device = PCI_SLOT(pdev->devfn);
10785 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010786 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010787
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010788 /* set up the locks for the AQ, do this only once in probe
10789 * and destroy them only once in remove
10790 */
10791 mutex_init(&hw->aq.asq_mutex);
10792 mutex_init(&hw->aq.arq_mutex);
10793
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010794 pf->msg_enable = netif_msg_init(debug,
10795 NETIF_MSG_DRV |
10796 NETIF_MSG_PROBE |
10797 NETIF_MSG_LINK);
10798 if (debug < -1)
10799 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010800
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010801 /* do a special CORER for clearing PXE mode once at init */
10802 if (hw->revision_id == 0 &&
10803 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10804 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10805 i40e_flush(hw);
10806 msleep(200);
10807 pf->corer_count++;
10808
10809 i40e_clear_pxe_mode(hw);
10810 }
10811
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010812 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010813 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010814 err = i40e_pf_reset(hw);
10815 if (err) {
10816 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10817 goto err_pf_reset;
10818 }
10819 pf->pfr_count++;
10820
10821 hw->aq.num_arq_entries = I40E_AQ_LEN;
10822 hw->aq.num_asq_entries = I40E_AQ_LEN;
10823 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10824 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10825 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010826
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010827 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010828 "%s-%s:misc",
10829 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010830
10831 err = i40e_init_shared_code(hw);
10832 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010833 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10834 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010835 goto err_pf_reset;
10836 }
10837
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010838 /* set up a default setting for link flow control */
10839 pf->hw.fc.requested_mode = I40E_FC_NONE;
10840
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010841 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010842 if (err) {
10843 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10844 dev_info(&pdev->dev,
10845 "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");
10846 else
10847 dev_info(&pdev->dev,
10848 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10849
10850 goto err_pf_reset;
10851 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010852
Shannon Nelson6dec1012015-09-28 14:12:30 -040010853 /* provide nvm, fw, api versions */
10854 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10855 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10856 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10857 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010858
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010859 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10860 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010861 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010862 "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");
10863 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10864 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010865 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010866 "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 +000010867
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010868 i40e_verify_eeprom(pf);
10869
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010870 /* Rev 0 hardware was never productized */
10871 if (hw->revision_id < 1)
10872 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");
10873
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010874 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875 err = i40e_get_capabilities(pf);
10876 if (err)
10877 goto err_adminq_setup;
10878
10879 err = i40e_sw_init(pf);
10880 if (err) {
10881 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10882 goto err_sw_init;
10883 }
10884
10885 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10886 hw->func_caps.num_rx_qp,
10887 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10888 if (err) {
10889 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10890 goto err_init_lan_hmc;
10891 }
10892
10893 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10894 if (err) {
10895 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10896 err = -ENOENT;
10897 goto err_configure_lan_hmc;
10898 }
10899
Neerav Parikhb686ece2014-12-14 01:55:11 +000010900 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10901 * Ignore error return codes because if it was already disabled via
10902 * hardware settings this will fail
10903 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010904 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010905 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10906 i40e_aq_stop_lldp(hw, true, NULL);
10907 }
10908
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010909 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010910 /* allow a platform config to override the HW addr */
10911 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010912 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010913 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10914 err = -EIO;
10915 goto err_mac_addr;
10916 }
10917 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010918 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010919 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10920 if (is_valid_ether_addr(hw->mac.port_addr))
10921 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010922#ifdef I40E_FCOE
10923 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10924 if (err)
10925 dev_info(&pdev->dev,
10926 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10927 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10928 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10929 hw->mac.san_addr);
10930 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10931 }
10932 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10933#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010934
10935 pci_set_drvdata(pdev, pf);
10936 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010937#ifdef CONFIG_I40E_DCB
10938 err = i40e_init_pf_dcb(pf);
10939 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010940 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070010941 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000010942 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010943 }
10944#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010945
10946 /* set up periodic task facility */
10947 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10948 pf->service_timer_period = HZ;
10949
10950 INIT_WORK(&pf->service_task, i40e_service_task);
10951 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10952 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010953
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010954 /* NVM bit on means WoL disabled for the port */
10955 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010956 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010957 pf->wol_en = false;
10958 else
10959 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010960 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010962 /* set up the main switch operations */
10963 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010964 err = i40e_init_interrupt_scheme(pf);
10965 if (err)
10966 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010967
Mitch Williams505682c2014-05-20 08:01:37 +000010968 /* The number of VSIs reported by the FW is the minimum guaranteed
10969 * to us; HW supports far more and we share the remaining pool with
10970 * the other PFs. We allocate space for more than the guarantee with
10971 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010972 */
Mitch Williams505682c2014-05-20 08:01:37 +000010973 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10974 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10975 else
10976 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10977
10978 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080010979 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
10980 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010981 if (!pf->vsi) {
10982 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010983 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010984 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010985
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010986#ifdef CONFIG_PCI_IOV
10987 /* prep for VF support */
10988 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10989 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10990 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10991 if (pci_num_vf(pdev))
10992 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10993 }
10994#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010995 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010996 if (err) {
10997 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10998 goto err_vsis;
10999 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011000
11001 /* Make sure flow control is set according to current settings */
11002 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11003 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11004 dev_dbg(&pf->pdev->dev,
11005 "Set fc with err %s aq_err %s on get_phy_cap\n",
11006 i40e_stat_str(hw, err),
11007 i40e_aq_str(hw, hw->aq.asq_last_status));
11008 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11009 dev_dbg(&pf->pdev->dev,
11010 "Set fc with err %s aq_err %s on set_phy_config\n",
11011 i40e_stat_str(hw, err),
11012 i40e_aq_str(hw, hw->aq.asq_last_status));
11013 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11014 dev_dbg(&pf->pdev->dev,
11015 "Set fc with err %s aq_err %s on get_link_info\n",
11016 i40e_stat_str(hw, err),
11017 i40e_aq_str(hw, hw->aq.asq_last_status));
11018
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011019 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011020 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011021 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11022 i40e_vsi_open(pf->vsi[i]);
11023 break;
11024 }
11025 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011026
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011027 /* The driver only wants link up/down and module qualification
11028 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011029 */
11030 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011031 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011032 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011033 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011034 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011035 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11036 i40e_stat_str(&pf->hw, err),
11037 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011038
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011039 /* Reconfigure hardware for allowing smaller MSS in the case
11040 * of TSO, so that we avoid the MDD being fired and causing
11041 * a reset in the case of small MSS+TSO.
11042 */
11043 val = rd32(hw, I40E_REG_MSS);
11044 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11045 val &= ~I40E_REG_MSS_MIN_MASK;
11046 val |= I40E_64BYTE_MSS;
11047 wr32(hw, I40E_REG_MSS, val);
11048 }
11049
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011050 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011051 msleep(75);
11052 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11053 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011054 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11055 i40e_stat_str(&pf->hw, err),
11056 i40e_aq_str(&pf->hw,
11057 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011058 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011059 /* The main driver is (mostly) up and happy. We need to set this state
11060 * before setting up the misc vector or we get a race and the vector
11061 * ends up disabled forever.
11062 */
11063 clear_bit(__I40E_DOWN, &pf->state);
11064
11065 /* In case of MSIX we are going to setup the misc vector right here
11066 * to handle admin queue events etc. In case of legacy and MSI
11067 * the misc functionality and queue processing is combined in
11068 * the same vector and that gets setup at open.
11069 */
11070 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11071 err = i40e_setup_misc_vector(pf);
11072 if (err) {
11073 dev_info(&pdev->dev,
11074 "setup of misc vector failed: %d\n", err);
11075 goto err_vsis;
11076 }
11077 }
11078
Greg Rosedf805f62014-04-04 04:43:16 +000011079#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011080 /* prep for VF support */
11081 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011082 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11083 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011084 /* disable link interrupts for VFs */
11085 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11086 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11087 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11088 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011089
11090 if (pci_num_vf(pdev)) {
11091 dev_info(&pdev->dev,
11092 "Active VFs found, allocating resources.\n");
11093 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11094 if (err)
11095 dev_info(&pdev->dev,
11096 "Error %d allocating resources for existing VFs\n",
11097 err);
11098 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011099 }
Greg Rosedf805f62014-04-04 04:43:16 +000011100#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011101
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011102 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11103 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11104 pf->num_iwarp_msix,
11105 I40E_IWARP_IRQ_PILE_ID);
11106 if (pf->iwarp_base_vector < 0) {
11107 dev_info(&pdev->dev,
11108 "failed to get tracking for %d vectors for IWARP err=%d\n",
11109 pf->num_iwarp_msix, pf->iwarp_base_vector);
11110 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11111 }
11112 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011113
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011114 i40e_dbg_pf_init(pf);
11115
11116 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011117 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011118
11119 /* since everything's happy, start the service_task timer */
11120 mod_timer(&pf->service_timer,
11121 round_jiffies(jiffies + pf->service_timer_period));
11122
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011123 /* add this PF to client device list and launch a client service task */
11124 err = i40e_lan_add_device(pf);
11125 if (err)
11126 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11127 err);
11128
Vasu Dev38e00432014-08-01 13:27:03 -070011129#ifdef I40E_FCOE
11130 /* create FCoE interface */
11131 i40e_fcoe_vsi_setup(pf);
11132
11133#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011134#define PCI_SPEED_SIZE 8
11135#define PCI_WIDTH_SIZE 8
11136 /* Devices on the IOSF bus do not have this information
11137 * and will report PCI Gen 1 x 1 by default so don't bother
11138 * checking them.
11139 */
11140 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11141 char speed[PCI_SPEED_SIZE] = "Unknown";
11142 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011143
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011144 /* Get the negotiated link width and speed from PCI config
11145 * space
11146 */
11147 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11148 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011149
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011150 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011151
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011152 switch (hw->bus.speed) {
11153 case i40e_bus_speed_8000:
11154 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11155 case i40e_bus_speed_5000:
11156 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11157 case i40e_bus_speed_2500:
11158 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11159 default:
11160 break;
11161 }
11162 switch (hw->bus.width) {
11163 case i40e_bus_width_pcie_x8:
11164 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11165 case i40e_bus_width_pcie_x4:
11166 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11167 case i40e_bus_width_pcie_x2:
11168 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11169 case i40e_bus_width_pcie_x1:
11170 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11171 default:
11172 break;
11173 }
11174
11175 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11176 speed, width);
11177
11178 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11179 hw->bus.speed < i40e_bus_speed_8000) {
11180 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11181 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11182 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011183 }
11184
Catherine Sullivane8278452015-02-06 08:52:08 +000011185 /* get the requested speeds from the fw */
11186 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11187 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011188 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11189 i40e_stat_str(&pf->hw, err),
11190 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011191 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11192
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011193 /* get the supported phy types from the fw */
11194 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11195 if (err)
11196 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11197 i40e_stat_str(&pf->hw, err),
11198 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11199 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11200
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011201 /* Add a filter to drop all Flow control frames from any VSI from being
11202 * transmitted. By doing so we stop a malicious VF from sending out
11203 * PAUSE or PFC frames and potentially controlling traffic for other
11204 * PF/VF VSIs.
11205 * The FW can still send Flow control frames if enabled.
11206 */
11207 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11208 pf->main_vsi_seid);
11209
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011210 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11211 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11212 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11213
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011214 /* print a string summarizing features */
11215 i40e_print_features(pf);
11216
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011217 return 0;
11218
11219 /* Unwind what we've done if something failed in the setup */
11220err_vsis:
11221 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011222 i40e_clear_interrupt_scheme(pf);
11223 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011224err_switch_setup:
11225 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011226 del_timer_sync(&pf->service_timer);
11227err_mac_addr:
11228err_configure_lan_hmc:
11229 (void)i40e_shutdown_lan_hmc(hw);
11230err_init_lan_hmc:
11231 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011232err_sw_init:
11233err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011234err_pf_reset:
11235 iounmap(hw->hw_addr);
11236err_ioremap:
11237 kfree(pf);
11238err_pf_alloc:
11239 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011240 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011241err_pci_reg:
11242err_dma:
11243 pci_disable_device(pdev);
11244 return err;
11245}
11246
11247/**
11248 * i40e_remove - Device removal routine
11249 * @pdev: PCI device information struct
11250 *
11251 * i40e_remove is called by the PCI subsystem to alert the driver
11252 * that is should release a PCI device. This could be caused by a
11253 * Hot-Plug event, or because the driver is going to be removed from
11254 * memory.
11255 **/
11256static void i40e_remove(struct pci_dev *pdev)
11257{
11258 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011259 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011260 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011261 int i;
11262
11263 i40e_dbg_pf_exit(pf);
11264
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011265 i40e_ptp_stop(pf);
11266
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011267 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011268 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11269 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011270
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011271 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011272 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011273 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011274 if (pf->service_timer.data)
11275 del_timer_sync(&pf->service_timer);
11276 if (pf->service_task.func)
11277 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011278
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011279 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11280 i40e_free_vfs(pf);
11281 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11282 }
11283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011284 i40e_fdir_teardown(pf);
11285
11286 /* If there is a switch structure or any orphans, remove them.
11287 * This will leave only the PF's VSI remaining.
11288 */
11289 for (i = 0; i < I40E_MAX_VEB; i++) {
11290 if (!pf->veb[i])
11291 continue;
11292
11293 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11294 pf->veb[i]->uplink_seid == 0)
11295 i40e_switch_branch_release(pf->veb[i]);
11296 }
11297
11298 /* Now we can shutdown the PF's VSI, just before we kill
11299 * adminq and hmc.
11300 */
11301 if (pf->vsi[pf->lan_vsi])
11302 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11303
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011304 /* remove attached clients */
11305 ret_code = i40e_lan_del_device(pf);
11306 if (ret_code) {
11307 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11308 ret_code);
11309 }
11310
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011311 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011312 if (hw->hmc.hmc_obj) {
11313 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011314 if (ret_code)
11315 dev_warn(&pdev->dev,
11316 "Failed to destroy the HMC resources: %d\n",
11317 ret_code);
11318 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011319
11320 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011321 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011322
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011323 /* destroy the locks only once, here */
11324 mutex_destroy(&hw->aq.arq_mutex);
11325 mutex_destroy(&hw->aq.asq_mutex);
11326
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011327 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11328 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011329 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011330 if (pf->vsi[i]) {
11331 i40e_vsi_clear_rings(pf->vsi[i]);
11332 i40e_vsi_clear(pf->vsi[i]);
11333 pf->vsi[i] = NULL;
11334 }
11335 }
11336
11337 for (i = 0; i < I40E_MAX_VEB; i++) {
11338 kfree(pf->veb[i]);
11339 pf->veb[i] = NULL;
11340 }
11341
11342 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011343 kfree(pf->vsi);
11344
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011345 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011346 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011347 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011348
11349 pci_disable_pcie_error_reporting(pdev);
11350 pci_disable_device(pdev);
11351}
11352
11353/**
11354 * i40e_pci_error_detected - warning that something funky happened in PCI land
11355 * @pdev: PCI device information struct
11356 *
11357 * Called to warn that something happened and the error handling steps
11358 * are in progress. Allows the driver to quiesce things, be ready for
11359 * remediation.
11360 **/
11361static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11362 enum pci_channel_state error)
11363{
11364 struct i40e_pf *pf = pci_get_drvdata(pdev);
11365
11366 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11367
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011368 if (!pf) {
11369 dev_info(&pdev->dev,
11370 "Cannot recover - error happened during device probe\n");
11371 return PCI_ERS_RESULT_DISCONNECT;
11372 }
11373
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011374 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011375 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11376 rtnl_lock();
11377 i40e_prep_for_reset(pf);
11378 rtnl_unlock();
11379 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011380
11381 /* Request a slot reset */
11382 return PCI_ERS_RESULT_NEED_RESET;
11383}
11384
11385/**
11386 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11387 * @pdev: PCI device information struct
11388 *
11389 * Called to find if the driver can work with the device now that
11390 * the pci slot has been reset. If a basic connection seems good
11391 * (registers are readable and have sane content) then return a
11392 * happy little PCI_ERS_RESULT_xxx.
11393 **/
11394static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11395{
11396 struct i40e_pf *pf = pci_get_drvdata(pdev);
11397 pci_ers_result_t result;
11398 int err;
11399 u32 reg;
11400
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011401 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011402 if (pci_enable_device_mem(pdev)) {
11403 dev_info(&pdev->dev,
11404 "Cannot re-enable PCI device after reset.\n");
11405 result = PCI_ERS_RESULT_DISCONNECT;
11406 } else {
11407 pci_set_master(pdev);
11408 pci_restore_state(pdev);
11409 pci_save_state(pdev);
11410 pci_wake_from_d3(pdev, false);
11411
11412 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11413 if (reg == 0)
11414 result = PCI_ERS_RESULT_RECOVERED;
11415 else
11416 result = PCI_ERS_RESULT_DISCONNECT;
11417 }
11418
11419 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11420 if (err) {
11421 dev_info(&pdev->dev,
11422 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11423 err);
11424 /* non-fatal, continue */
11425 }
11426
11427 return result;
11428}
11429
11430/**
11431 * i40e_pci_error_resume - restart operations after PCI error recovery
11432 * @pdev: PCI device information struct
11433 *
11434 * Called to allow the driver to bring things back up after PCI error
11435 * and/or reset recovery has finished.
11436 **/
11437static void i40e_pci_error_resume(struct pci_dev *pdev)
11438{
11439 struct i40e_pf *pf = pci_get_drvdata(pdev);
11440
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011441 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011442 if (test_bit(__I40E_SUSPENDED, &pf->state))
11443 return;
11444
11445 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011447 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011448}
11449
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011450/**
11451 * i40e_shutdown - PCI callback for shutting down
11452 * @pdev: PCI device information struct
11453 **/
11454static void i40e_shutdown(struct pci_dev *pdev)
11455{
11456 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011457 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011458
11459 set_bit(__I40E_SUSPENDED, &pf->state);
11460 set_bit(__I40E_DOWN, &pf->state);
11461 rtnl_lock();
11462 i40e_prep_for_reset(pf);
11463 rtnl_unlock();
11464
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011465 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11466 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11467
Catherine Sullivan02b42492015-07-10 19:35:59 -040011468 del_timer_sync(&pf->service_timer);
11469 cancel_work_sync(&pf->service_task);
11470 i40e_fdir_teardown(pf);
11471
11472 rtnl_lock();
11473 i40e_prep_for_reset(pf);
11474 rtnl_unlock();
11475
11476 wr32(hw, I40E_PFPM_APM,
11477 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11478 wr32(hw, I40E_PFPM_WUFC,
11479 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11480
Shannon Nelsone1477582015-02-21 06:44:33 +000011481 i40e_clear_interrupt_scheme(pf);
11482
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011483 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011484 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011485 pci_set_power_state(pdev, PCI_D3hot);
11486 }
11487}
11488
11489#ifdef CONFIG_PM
11490/**
11491 * i40e_suspend - PCI callback for moving to D3
11492 * @pdev: PCI device information struct
11493 **/
11494static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11495{
11496 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011497 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011498 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011499
11500 set_bit(__I40E_SUSPENDED, &pf->state);
11501 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011502
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011503 rtnl_lock();
11504 i40e_prep_for_reset(pf);
11505 rtnl_unlock();
11506
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011507 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11508 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11509
Greg Roseb33d3b72016-05-16 10:26:37 -070011510 i40e_stop_misc_vector(pf);
11511
Greg Rose059ff692016-05-16 10:26:38 -070011512 retval = pci_save_state(pdev);
11513 if (retval)
11514 return retval;
11515
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
Greg Rose059ff692016-05-16 10:26:38 -070011519 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011520}
11521
11522/**
11523 * i40e_resume - PCI callback for waking up from D3
11524 * @pdev: PCI device information struct
11525 **/
11526static int i40e_resume(struct pci_dev *pdev)
11527{
11528 struct i40e_pf *pf = pci_get_drvdata(pdev);
11529 u32 err;
11530
11531 pci_set_power_state(pdev, PCI_D0);
11532 pci_restore_state(pdev);
11533 /* pci_restore_state() clears dev->state_saves, so
11534 * call pci_save_state() again to restore it.
11535 */
11536 pci_save_state(pdev);
11537
11538 err = pci_enable_device_mem(pdev);
11539 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011540 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011541 return err;
11542 }
11543 pci_set_master(pdev);
11544
11545 /* no wakeup events while running */
11546 pci_wake_from_d3(pdev, false);
11547
11548 /* handling the reset will rebuild the device state */
11549 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11550 clear_bit(__I40E_DOWN, &pf->state);
11551 rtnl_lock();
11552 i40e_reset_and_rebuild(pf, false);
11553 rtnl_unlock();
11554 }
11555
11556 return 0;
11557}
11558
11559#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011560static const struct pci_error_handlers i40e_err_handler = {
11561 .error_detected = i40e_pci_error_detected,
11562 .slot_reset = i40e_pci_error_slot_reset,
11563 .resume = i40e_pci_error_resume,
11564};
11565
11566static struct pci_driver i40e_driver = {
11567 .name = i40e_driver_name,
11568 .id_table = i40e_pci_tbl,
11569 .probe = i40e_probe,
11570 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011571#ifdef CONFIG_PM
11572 .suspend = i40e_suspend,
11573 .resume = i40e_resume,
11574#endif
11575 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011576 .err_handler = &i40e_err_handler,
11577 .sriov_configure = i40e_pci_sriov_configure,
11578};
11579
11580/**
11581 * i40e_init_module - Driver registration routine
11582 *
11583 * i40e_init_module is the first routine called when the driver is
11584 * loaded. All it does is register with the PCI subsystem.
11585 **/
11586static int __init i40e_init_module(void)
11587{
11588 pr_info("%s: %s - version %s\n", i40e_driver_name,
11589 i40e_driver_string, i40e_driver_version_str);
11590 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011591
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011592 /* we will see if single thread per module is enough for now,
11593 * it can't be any worse than using the system workqueue which
11594 * was already single threaded
11595 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011596 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11597 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011598 if (!i40e_wq) {
11599 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11600 return -ENOMEM;
11601 }
11602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011603 i40e_dbg_init();
11604 return pci_register_driver(&i40e_driver);
11605}
11606module_init(i40e_init_module);
11607
11608/**
11609 * i40e_exit_module - Driver exit cleanup routine
11610 *
11611 * i40e_exit_module is called just before the driver is removed
11612 * from memory.
11613 **/
11614static void __exit i40e_exit_module(void)
11615{
11616 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011617 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011618 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011619}
11620module_exit(i40e_exit_module);