blob: ea4dc9f9c4163d465ab71c53ea632ec2796ddfae [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/**
Kiran Patil21659032015-09-30 14:09:03 -04001686 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
1687 * @vsi: pointer to vsi struct
1688 * @from: Pointer to list which contains MAC filter entries - changes to
1689 * those entries needs to be undone.
1690 *
1691 * MAC filter entries from list were slated to be removed from device.
1692 **/
1693static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
Jacob Keller278e7d02016-10-05 09:30:37 -07001694 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001695{
Jacob Keller278e7d02016-10-05 09:30:37 -07001696 struct i40e_mac_filter *f;
1697 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001698
Jacob Keller278e7d02016-10-05 09:30:37 -07001699 hlist_for_each_entry_safe(f, h, from, hlist) {
1700 u64 key = i40e_addr_to_hkey(f->macaddr);
1701
Kiran Patil21659032015-09-30 14:09:03 -04001702 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001703 hlist_del(&f->hlist);
1704 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001705 }
1706}
1707
1708/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001709 * i40e_update_filter_state - Update filter state based on return data
1710 * from firmware
1711 * @count: Number of filters added
1712 * @add_list: return data from fw
1713 * @head: pointer to first filter in current batch
1714 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001715 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001716 * MAC filter entries from list were slated to be added to device. Returns
1717 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001718 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001719static int
1720i40e_update_filter_state(int count,
1721 struct i40e_aqc_add_macvlan_element_data *add_list,
1722 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001723{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001724 int retval = 0;
1725 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001726
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001727
1728 if (!aq_err) {
1729 retval = count;
1730 /* Everything's good, mark all filters active. */
1731 for (i = 0; i < count ; i++) {
1732 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001733 add_head = hlist_entry(add_head->hlist.next,
1734 typeof(struct i40e_mac_filter),
1735 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001736 }
1737 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1738 /* Device ran out of filter space. Check the return value
1739 * for each filter to see which ones are active.
1740 */
1741 for (i = 0; i < count ; i++) {
1742 if (add_list[i].match_method ==
1743 I40E_AQC_MM_ERR_NO_RES) {
1744 add_head->state = I40E_FILTER_FAILED;
1745 } else {
1746 add_head->state = I40E_FILTER_ACTIVE;
1747 retval++;
1748 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001749 add_head = hlist_entry(add_head->hlist.next,
1750 typeof(struct i40e_mac_filter),
1751 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001752 }
1753 } else {
1754 /* Some other horrible thing happened, fail all filters */
1755 retval = 0;
1756 for (i = 0; i < count ; i++) {
1757 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001758 add_head = hlist_entry(add_head->hlist.next,
1759 typeof(struct i40e_mac_filter),
1760 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001761 }
Kiran Patil21659032015-09-30 14:09:03 -04001762 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001763 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001764}
1765
1766/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001767 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1768 * @vsi: ptr to the VSI
1769 *
1770 * Push any outstanding VSI filter changes through the AdminQ.
1771 *
1772 * Returns 0 or error value
1773 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001774int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001775{
Jacob Keller278e7d02016-10-05 09:30:37 -07001776 struct i40e_mac_filter *f, *add_head = NULL;
1777 struct hlist_head tmp_add_list, tmp_del_list;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001778 struct i40e_hw *hw = &vsi->back->hw;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001779 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001780 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001781 int filter_list_len = 0;
1782 u32 changed_flags = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001783 i40e_status aq_ret = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001784 struct hlist_node *h;
Mitch Williamsea02e902015-11-09 15:35:50 -08001785 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001786 struct i40e_pf *pf;
1787 int num_add = 0;
1788 int num_del = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04001789 int aq_err = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001790 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001791 int list_size;
1792 int fcnt;
Jacob Keller278e7d02016-10-05 09:30:37 -07001793 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001794
1795 /* empty array typed pointers, kcalloc later */
1796 struct i40e_aqc_add_macvlan_element_data *add_list;
1797 struct i40e_aqc_remove_macvlan_element_data *del_list;
1798
1799 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1800 usleep_range(1000, 2000);
1801 pf = vsi->back;
1802
1803 if (vsi->netdev) {
1804 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1805 vsi->current_netdev_flags = vsi->netdev->flags;
1806 }
1807
Jacob Keller278e7d02016-10-05 09:30:37 -07001808 INIT_HLIST_HEAD(&tmp_add_list);
1809 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001810
Shannon Nelson2d1de822016-05-16 10:26:44 -07001811 if (vsi->type == I40E_VSI_SRIOV)
1812 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1813 else if (vsi->type != I40E_VSI_MAIN)
1814 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1815
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001816 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1817 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1818
Jacob Keller278e7d02016-10-05 09:30:37 -07001819 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001820 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001821 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001822 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001823 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001824 hash_del(&f->hlist);
1825 hlist_add_head(&f->hlist, &tmp_del_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001826 vsi->active_filters--;
Kiran Patil21659032015-09-30 14:09:03 -04001827 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001828 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001829 hash_del(&f->hlist);
1830 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001831 }
Kiran Patil21659032015-09-30 14:09:03 -04001832 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001833 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001834 }
1835
1836 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07001837 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001838 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04001839 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001840 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08001841 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001842 del_list = kzalloc(list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001843 if (!del_list) {
Kiran Patil21659032015-09-30 14:09:03 -04001844 /* Undo VSI's MAC filter entry element updates */
Jacob Keller278e7d02016-10-05 09:30:37 -07001845 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001846 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
Jacob Keller278e7d02016-10-05 09:30:37 -07001847 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsea02e902015-11-09 15:35:50 -08001848 retval = -ENOMEM;
1849 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001850 }
1851
Jacob Keller278e7d02016-10-05 09:30:37 -07001852 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001853 cmd_flags = 0;
1854
1855 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00001856 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001857 if (f->vlan == I40E_VLAN_ANY) {
1858 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07001859 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001860 } else {
1861 del_list[num_del].vlan_tag =
1862 cpu_to_le16((u16)(f->vlan));
1863 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001864
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001865 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1866 del_list[num_del].flags = cmd_flags;
1867 num_del++;
1868
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001869 /* flush a full buffer */
1870 if (num_del == filter_list_len) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001871 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid,
1872 del_list,
1873 num_del, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001874 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001875 num_del = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001876 memset(del_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001877
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001878 /* Explicitly ignore and do not report when
1879 * firmware returns ENOENT.
1880 */
1881 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001882 retval = -EIO;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001883 dev_info(&pf->pdev->dev,
1884 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
Shannon Nelson2d1de822016-05-16 10:26:44 -07001885 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001886 i40e_stat_str(hw, aq_ret),
1887 i40e_aq_str(hw, aq_err));
Mitch Williamsea02e902015-11-09 15:35:50 -08001888 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001889 }
Kiran Patil21659032015-09-30 14:09:03 -04001890 /* Release memory for MAC filter entries which were
1891 * synced up with HW.
1892 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001893 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04001894 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001895 }
Kiran Patil21659032015-09-30 14:09:03 -04001896
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001897 if (num_del) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001898 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, del_list,
1899 num_del, NULL);
1900 aq_err = hw->aq.asq_last_status;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001901 num_del = 0;
1902
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001903 /* Explicitly ignore and do not report when firmware
1904 * returns ENOENT.
1905 */
1906 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1907 retval = -EIO;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001908 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07001909 "ignoring delete macvlan error on %s, err %s aq_err %s\n",
1910 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001911 i40e_stat_str(hw, aq_ret),
1912 i40e_aq_str(hw, aq_err));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001913 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001914 }
1915
1916 kfree(del_list);
1917 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001918 }
1919
Jacob Keller278e7d02016-10-05 09:30:37 -07001920 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001921 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001922 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08001923 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001924 list_size = filter_list_len *
1925 sizeof(struct i40e_aqc_add_macvlan_element_data);
1926 add_list = kzalloc(list_size, GFP_ATOMIC);
Kiran Patil21659032015-09-30 14:09:03 -04001927 if (!add_list) {
Mitch Williamsea02e902015-11-09 15:35:50 -08001928 retval = -ENOMEM;
1929 goto out;
Kiran Patil21659032015-09-30 14:09:03 -04001930 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001931 num_add = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001932 hlist_for_each_entry(f, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001933 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1934 &vsi->state)) {
1935 f->state = I40E_FILTER_FAILED;
1936 continue;
1937 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001938 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001939 if (num_add == 0)
1940 add_head = f;
1941 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00001942 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001943 if (f->vlan == I40E_VLAN_ANY) {
1944 add_list[num_add].vlan_tag = 0;
1945 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
1946 } else {
1947 add_list[num_add].vlan_tag =
1948 cpu_to_le16((u16)(f->vlan));
1949 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001950 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001951 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001952 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1953 num_add++;
1954
1955 /* flush a full buffer */
1956 if (num_add == filter_list_len) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001957 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08001958 add_list, num_add,
1959 NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001960 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001961 fcnt = i40e_update_filter_state(num_add,
1962 add_list,
1963 add_head,
1964 aq_ret);
1965 vsi->active_filters += fcnt;
1966
1967 if (fcnt != num_add) {
1968 promisc_changed = true;
1969 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1970 &vsi->state);
1971 vsi->promisc_threshold =
1972 (vsi->active_filters * 3) / 4;
1973 dev_warn(&pf->pdev->dev,
1974 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1975 i40e_aq_str(hw, aq_err),
1976 vsi_name);
1977 }
1978 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001979 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001980 }
1981 }
1982 if (num_add) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001983 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid,
Mitch Williamsea02e902015-11-09 15:35:50 -08001984 add_list, num_add, NULL);
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001985 aq_err = hw->aq.asq_last_status;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001986 fcnt = i40e_update_filter_state(num_add, add_list,
1987 add_head, aq_ret);
1988 vsi->active_filters += fcnt;
1989 if (fcnt != num_add) {
1990 promisc_changed = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001991 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1992 &vsi->state);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001993 vsi->promisc_threshold =
1994 (vsi->active_filters * 3) / 4;
1995 dev_warn(&pf->pdev->dev,
1996 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1997 i40e_aq_str(hw, aq_err), vsi_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001998 }
1999 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002000 /* Now move all of the filters from the temp add list back to
2001 * the VSI's list.
2002 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002003 spin_lock_bh(&vsi->mac_filter_hash_lock);
2004 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2005 u64 key = i40e_addr_to_hkey(f->macaddr);
2006
2007 hlist_del(&f->hlist);
2008 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002009 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002010 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002011 kfree(add_list);
2012 add_list = NULL;
2013 }
2014
2015 /* Check to see if we can drop out of overflow promiscuous mode. */
2016 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2017 (vsi->active_filters < vsi->promisc_threshold)) {
2018 int failed_count = 0;
2019 /* See if we have any failed filters. We can't drop out of
2020 * promiscuous until these have all been deleted.
2021 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002022 spin_lock_bh(&vsi->mac_filter_hash_lock);
2023 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002024 if (f->state == I40E_FILTER_FAILED)
2025 failed_count++;
2026 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002027 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002028 if (!failed_count) {
2029 dev_info(&pf->pdev->dev,
2030 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2031 vsi_name);
2032 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2033 promisc_changed = true;
2034 vsi->promisc_threshold = 0;
2035 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002036 }
2037
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002038 /* if the VF is not trusted do not do promisc */
2039 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2040 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2041 goto out;
2042 }
2043
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002044 /* check for changes in promiscuous modes */
2045 if (changed_flags & IFF_ALLMULTI) {
2046 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002047
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002048 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002049 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2050 vsi->seid,
2051 cur_multipromisc,
2052 NULL);
2053 if (aq_ret) {
2054 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002055 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002056 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002057 "set multi promisc failed on %s, err %s aq_err %s\n",
2058 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002059 i40e_stat_str(hw, aq_ret),
2060 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002061 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002062 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002063 if ((changed_flags & IFF_PROMISC) ||
2064 (promisc_changed &&
2065 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002066 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002067
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002068 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2069 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2070 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002071 if ((vsi->type == I40E_VSI_MAIN) &&
2072 (pf->lan_veb != I40E_NO_VEB) &&
2073 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002074 /* set defport ON for Main VSI instead of true promisc
2075 * this way we will get all unicast/multicast and VLAN
2076 * promisc behavior but will not get VF or VMDq traffic
2077 * replicated on the Main VSI.
2078 */
2079 if (pf->cur_promisc != cur_promisc) {
2080 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002081 if (cur_promisc)
2082 aq_ret =
2083 i40e_aq_set_default_vsi(hw,
2084 vsi->seid,
2085 NULL);
2086 else
2087 aq_ret =
2088 i40e_aq_clear_default_vsi(hw,
2089 vsi->seid,
2090 NULL);
2091 if (aq_ret) {
2092 retval = i40e_aq_rc_to_posix(aq_ret,
2093 hw->aq.asq_last_status);
2094 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002095 "Set default VSI failed on %s, err %s, aq_err %s\n",
2096 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002097 i40e_stat_str(hw, aq_ret),
2098 i40e_aq_str(hw,
2099 hw->aq.asq_last_status));
2100 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002101 }
2102 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002103 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002104 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002105 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002106 cur_promisc, NULL,
2107 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002108 if (aq_ret) {
2109 retval =
2110 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002111 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002112 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002113 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2114 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002115 i40e_stat_str(hw, aq_ret),
2116 i40e_aq_str(hw,
2117 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002118 }
2119 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002120 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002121 vsi->seid,
2122 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002123 if (aq_ret) {
2124 retval =
2125 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002126 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002127 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002128 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2129 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002130 i40e_stat_str(hw, aq_ret),
2131 i40e_aq_str(hw,
2132 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002133 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002134 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002135 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2136 vsi->seid,
2137 cur_promisc, NULL);
2138 if (aq_ret) {
2139 retval = i40e_aq_rc_to_posix(aq_ret,
2140 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002141 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002142 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002143 i40e_stat_str(hw, aq_ret),
2144 i40e_aq_str(hw,
2145 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002146 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002148out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002149 /* if something went wrong then set the changed flag so we try again */
2150 if (retval)
2151 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2152
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002153 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002154 return retval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002155}
2156
2157/**
2158 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2159 * @pf: board private structure
2160 **/
2161static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2162{
2163 int v;
2164
2165 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2166 return;
2167 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2168
Mitch Williams505682c2014-05-20 08:01:37 +00002169 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002170 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002171 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2172 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2173
2174 if (ret) {
2175 /* come back and try again later */
2176 pf->flags |= I40E_FLAG_FILTER_SYNC;
2177 break;
2178 }
2179 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002180 }
2181}
2182
2183/**
2184 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2185 * @netdev: network interface device structure
2186 * @new_mtu: new value for maximum frame size
2187 *
2188 * Returns 0 on success, negative on failure
2189 **/
2190static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2191{
2192 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002193 struct i40e_vsi *vsi = np->vsi;
2194
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002195 netdev_info(netdev, "changing MTU from %d to %d\n",
2196 netdev->mtu, new_mtu);
2197 netdev->mtu = new_mtu;
2198 if (netif_running(netdev))
2199 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002200 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002201 return 0;
2202}
2203
2204/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002205 * i40e_ioctl - Access the hwtstamp interface
2206 * @netdev: network interface device structure
2207 * @ifr: interface request data
2208 * @cmd: ioctl command
2209 **/
2210int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2211{
2212 struct i40e_netdev_priv *np = netdev_priv(netdev);
2213 struct i40e_pf *pf = np->vsi->back;
2214
2215 switch (cmd) {
2216 case SIOCGHWTSTAMP:
2217 return i40e_ptp_get_ts_config(pf, ifr);
2218 case SIOCSHWTSTAMP:
2219 return i40e_ptp_set_ts_config(pf, ifr);
2220 default:
2221 return -EOPNOTSUPP;
2222 }
2223}
2224
2225/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002226 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2227 * @vsi: the vsi being adjusted
2228 **/
2229void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2230{
2231 struct i40e_vsi_context ctxt;
2232 i40e_status ret;
2233
2234 if ((vsi->info.valid_sections &
2235 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2236 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2237 return; /* already enabled */
2238
2239 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2240 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2241 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2242
2243 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002244 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002245 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2246 if (ret) {
2247 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002248 "update vlan stripping failed, err %s aq_err %s\n",
2249 i40e_stat_str(&vsi->back->hw, ret),
2250 i40e_aq_str(&vsi->back->hw,
2251 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002252 }
2253}
2254
2255/**
2256 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2257 * @vsi: the vsi being adjusted
2258 **/
2259void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2260{
2261 struct i40e_vsi_context ctxt;
2262 i40e_status ret;
2263
2264 if ((vsi->info.valid_sections &
2265 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2266 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2267 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2268 return; /* already disabled */
2269
2270 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2271 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2272 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2273
2274 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002275 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002276 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2277 if (ret) {
2278 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002279 "update vlan stripping failed, err %s aq_err %s\n",
2280 i40e_stat_str(&vsi->back->hw, ret),
2281 i40e_aq_str(&vsi->back->hw,
2282 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002283 }
2284}
2285
2286/**
2287 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2288 * @netdev: network interface to be adjusted
2289 * @features: netdev features to test if VLAN offload is enabled or not
2290 **/
2291static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2292{
2293 struct i40e_netdev_priv *np = netdev_priv(netdev);
2294 struct i40e_vsi *vsi = np->vsi;
2295
2296 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2297 i40e_vlan_stripping_enable(vsi);
2298 else
2299 i40e_vlan_stripping_disable(vsi);
2300}
2301
2302/**
2303 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2304 * @vsi: the vsi being configured
2305 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2306 **/
2307int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2308{
Jacob Keller278e7d02016-10-05 09:30:37 -07002309 struct i40e_mac_filter *f, *add_f, *del_f;
2310 struct hlist_node *h;
2311 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002312
Kiran Patil21659032015-09-30 14:09:03 -04002313 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002314 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002315
Jacob Keller1bc87e82016-10-05 09:30:31 -07002316 if (vsi->netdev) {
2317 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002318 if (!add_f) {
2319 dev_info(&vsi->back->pdev->dev,
2320 "Could not add vlan filter %d for %pM\n",
2321 vid, vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002322 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002323 return -ENOMEM;
2324 }
2325 }
2326
Jacob Keller278e7d02016-10-05 09:30:37 -07002327 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002328 if (f->state == I40E_FILTER_REMOVE)
2329 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002330 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002331 if (!add_f) {
2332 dev_info(&vsi->back->pdev->dev,
2333 "Could not add vlan filter %d for %pM\n",
2334 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002335 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002336 return -ENOMEM;
2337 }
2338 }
2339
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002340 /* Now if we add a vlan tag, make sure to check if it is the first
2341 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2342 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002343 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002344 */
Jacob Keller290d2552016-10-05 09:30:36 -07002345 if (vid > 0 && vsi->netdev) {
2346 del_f = i40e_find_filter(vsi, vsi->netdev->dev_addr,
2347 I40E_VLAN_ANY);
2348 if (del_f) {
2349 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002350 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002351 if (!add_f) {
2352 dev_info(&vsi->back->pdev->dev,
2353 "Could not add filter 0 for %pM\n",
2354 vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002355 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002356 return -ENOMEM;
2357 }
2358 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002359 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002360
Greg Rose8d82a7c2014-01-13 16:13:04 -08002361 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2362 if (vid > 0 && !vsi->info.pvid) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002363 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002364 if (f->state == I40E_FILTER_REMOVE)
2365 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002366 del_f = i40e_find_filter(vsi, f->macaddr,
2367 I40E_VLAN_ANY);
2368 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002369 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002370 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002371 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002372 if (!add_f) {
2373 dev_info(&vsi->back->pdev->dev,
2374 "Could not add filter 0 for %pM\n",
2375 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002376 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002377 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002378 }
2379 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002380 }
2381
Jacob Keller278e7d02016-10-05 09:30:37 -07002382 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002383
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002384 /* schedule our worker thread which will take care of
2385 * applying the new filter changes
2386 */
2387 i40e_service_event_schedule(vsi->back);
2388 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002389}
2390
2391/**
2392 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2393 * @vsi: the vsi being configured
2394 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002395 *
2396 * Return: 0 on success or negative otherwise
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002397 **/
2398int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
2399{
2400 struct net_device *netdev = vsi->netdev;
Jacob Keller278e7d02016-10-05 09:30:37 -07002401 struct i40e_mac_filter *f, *add_f;
2402 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403 int filter_count = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002404 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405
Kiran Patil21659032015-09-30 14:09:03 -04002406 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002407 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002408
Jacob Keller1bc87e82016-10-05 09:30:31 -07002409 if (vsi->netdev)
2410 i40e_del_filter(vsi, netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002411
Jacob Keller278e7d02016-10-05 09:30:37 -07002412 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002413 if (f->vlan == vid)
2414 __i40e_del_filter(vsi, f);
2415 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002416
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002417 /* go through all the filters for this VSI and if there is only
2418 * vid == 0 it means there are no other filters, so vid 0 must
2419 * be replaced with -1. This signifies that we should from now
2420 * on accept any traffic (with any tag present, or untagged)
2421 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002422 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07002423 if (vsi->netdev) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002424 if (f->vlan &&
2425 ether_addr_equal(netdev->dev_addr, f->macaddr))
2426 filter_count++;
2427 }
2428
2429 if (f->vlan)
2430 filter_count++;
2431 }
2432
Jacob Keller1bc87e82016-10-05 09:30:31 -07002433 if (!filter_count && vsi->netdev) {
2434 i40e_del_filter(vsi, netdev->dev_addr, 0);
2435 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002436 if (!f) {
2437 dev_info(&vsi->back->pdev->dev,
2438 "Could not add filter %d for %pM\n",
2439 I40E_VLAN_ANY, netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002440 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002441 return -ENOMEM;
2442 }
2443 }
2444
2445 if (!filter_count) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002446 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002447 if (!f->vlan)
2448 __i40e_del_filter(vsi, f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002449 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002450 if (!add_f) {
2451 dev_info(&vsi->back->pdev->dev,
2452 "Could not add filter %d for %pM\n",
2453 I40E_VLAN_ANY, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002454 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002455 return -ENOMEM;
2456 }
2457 }
2458 }
2459
Jacob Keller278e7d02016-10-05 09:30:37 -07002460 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002461
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002462 /* schedule our worker thread which will take care of
2463 * applying the new filter changes
2464 */
2465 i40e_service_event_schedule(vsi->back);
2466 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002467}
2468
2469/**
2470 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2471 * @netdev: network interface to be adjusted
2472 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002473 *
2474 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002475 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002476#ifdef I40E_FCOE
2477int i40e_vlan_rx_add_vid(struct net_device *netdev,
2478 __always_unused __be16 proto, u16 vid)
2479#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002480static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2481 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002482#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002483{
2484 struct i40e_netdev_priv *np = netdev_priv(netdev);
2485 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002486 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002487
2488 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002489 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002490
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002491 /* If the network stack called us with vid = 0 then
2492 * it is asking to receive priority tagged packets with
2493 * vlan id 0. Our HW receives them by default when configured
2494 * to receive untagged packets so there is no need to add an
2495 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002496 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002497 if (vid)
2498 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002499
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002500 if (!ret && (vid < VLAN_N_VID))
2501 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002502
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002503 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002504}
2505
2506/**
2507 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2508 * @netdev: network interface to be adjusted
2509 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002510 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002511 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002512 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002513#ifdef I40E_FCOE
2514int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2515 __always_unused __be16 proto, u16 vid)
2516#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002517static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2518 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002519#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002520{
2521 struct i40e_netdev_priv *np = netdev_priv(netdev);
2522 struct i40e_vsi *vsi = np->vsi;
2523
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524 /* return code is ignored as there is nothing a user
2525 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002526 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002527 */
2528 i40e_vsi_kill_vlan(vsi, vid);
2529
2530 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002531
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532 return 0;
2533}
2534
2535/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002536 * i40e_macaddr_init - explicitly write the mac address filters
2537 *
2538 * @vsi: pointer to the vsi
2539 * @macaddr: the MAC address
2540 *
2541 * This is needed when the macaddr has been obtained by other
2542 * means than the default, e.g., from Open Firmware or IDPROM.
2543 * Returns 0 on success, negative on failure
2544 **/
2545static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2546{
2547 int ret;
2548 struct i40e_aqc_add_macvlan_element_data element;
2549
2550 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2551 I40E_AQC_WRITE_TYPE_LAA_WOL,
2552 macaddr, NULL);
2553 if (ret) {
2554 dev_info(&vsi->back->pdev->dev,
2555 "Addr change for VSI failed: %d\n", ret);
2556 return -EADDRNOTAVAIL;
2557 }
2558
2559 memset(&element, 0, sizeof(element));
2560 ether_addr_copy(element.mac_addr, macaddr);
2561 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2562 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2563 if (ret) {
2564 dev_info(&vsi->back->pdev->dev,
2565 "add filter failed err %s aq_err %s\n",
2566 i40e_stat_str(&vsi->back->hw, ret),
2567 i40e_aq_str(&vsi->back->hw,
2568 vsi->back->hw.aq.asq_last_status));
2569 }
2570 return ret;
2571}
2572
2573/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002574 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2575 * @vsi: the vsi being brought back up
2576 **/
2577static void i40e_restore_vlan(struct i40e_vsi *vsi)
2578{
2579 u16 vid;
2580
2581 if (!vsi->netdev)
2582 return;
2583
2584 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2585
2586 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2587 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2588 vid);
2589}
2590
2591/**
2592 * i40e_vsi_add_pvid - Add pvid for the VSI
2593 * @vsi: the vsi being adjusted
2594 * @vid: the vlan id to set as a PVID
2595 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002596int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002597{
2598 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002599 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002600
2601 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2602 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002603 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2604 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002605 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002606
2607 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002608 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002609 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2610 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002611 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002612 "add pvid failed, err %s aq_err %s\n",
2613 i40e_stat_str(&vsi->back->hw, ret),
2614 i40e_aq_str(&vsi->back->hw,
2615 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002616 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002617 }
2618
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002619 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002620}
2621
2622/**
2623 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2624 * @vsi: the vsi being adjusted
2625 *
2626 * Just use the vlan_rx_register() service to put it back to normal
2627 **/
2628void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2629{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002630 i40e_vlan_stripping_disable(vsi);
2631
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002632 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002633}
2634
2635/**
2636 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2637 * @vsi: ptr to the VSI
2638 *
2639 * If this function returns with an error, then it's possible one or
2640 * more of the rings is populated (while the rest are not). It is the
2641 * callers duty to clean those orphaned rings.
2642 *
2643 * Return 0 on success, negative on failure
2644 **/
2645static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2646{
2647 int i, err = 0;
2648
2649 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002650 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002651
2652 return err;
2653}
2654
2655/**
2656 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2657 * @vsi: ptr to the VSI
2658 *
2659 * Free VSI's transmit software resources
2660 **/
2661static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2662{
2663 int i;
2664
Greg Rose8e9dca52013-12-18 13:45:53 +00002665 if (!vsi->tx_rings)
2666 return;
2667
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002668 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002669 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002670 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002671}
2672
2673/**
2674 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2675 * @vsi: ptr to the VSI
2676 *
2677 * If this function returns with an error, then it's possible one or
2678 * more of the rings is populated (while the rest are not). It is the
2679 * callers duty to clean those orphaned rings.
2680 *
2681 * Return 0 on success, negative on failure
2682 **/
2683static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2684{
2685 int i, err = 0;
2686
2687 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002688 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002689#ifdef I40E_FCOE
2690 i40e_fcoe_setup_ddp_resources(vsi);
2691#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002692 return err;
2693}
2694
2695/**
2696 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2697 * @vsi: ptr to the VSI
2698 *
2699 * Free all receive software resources
2700 **/
2701static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2702{
2703 int i;
2704
Greg Rose8e9dca52013-12-18 13:45:53 +00002705 if (!vsi->rx_rings)
2706 return;
2707
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002708 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002709 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002710 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002711#ifdef I40E_FCOE
2712 i40e_fcoe_free_ddp_resources(vsi);
2713#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002714}
2715
2716/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002717 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2718 * @ring: The Tx ring to configure
2719 *
2720 * This enables/disables XPS for a given Tx descriptor ring
2721 * based on the TCs enabled for the VSI that ring belongs to.
2722 **/
2723static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2724{
2725 struct i40e_vsi *vsi = ring->vsi;
2726 cpumask_var_t mask;
2727
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002728 if (!ring->q_vector || !ring->netdev)
2729 return;
2730
2731 /* Single TC mode enable XPS */
2732 if (vsi->tc_config.numtc <= 1) {
2733 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002734 netif_set_xps_queue(ring->netdev,
2735 &ring->q_vector->affinity_mask,
2736 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002737 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2738 /* Disable XPS to allow selection based on TC */
2739 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2740 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2741 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002742 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002743
2744 /* schedule our worker thread which will take care of
2745 * applying the new filter changes
2746 */
2747 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002748}
2749
2750/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002751 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2752 * @ring: The Tx ring to configure
2753 *
2754 * Configure the Tx descriptor ring in the HMC context.
2755 **/
2756static int i40e_configure_tx_ring(struct i40e_ring *ring)
2757{
2758 struct i40e_vsi *vsi = ring->vsi;
2759 u16 pf_q = vsi->base_queue + ring->queue_index;
2760 struct i40e_hw *hw = &vsi->back->hw;
2761 struct i40e_hmc_obj_txq tx_ctx;
2762 i40e_status err = 0;
2763 u32 qtx_ctl = 0;
2764
2765 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002766 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002767 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2768 ring->atr_count = 0;
2769 } else {
2770 ring->atr_sample_rate = 0;
2771 }
2772
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002773 /* configure XPS */
2774 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002775
2776 /* clear the context structure first */
2777 memset(&tx_ctx, 0, sizeof(tx_ctx));
2778
2779 tx_ctx.new_context = 1;
2780 tx_ctx.base = (ring->dma / 128);
2781 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002782 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2783 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002784#ifdef I40E_FCOE
2785 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2786#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002787 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002788 /* FDIR VSI tx ring can still use RS bit and writebacks */
2789 if (vsi->type != I40E_VSI_FDIR)
2790 tx_ctx.head_wb_ena = 1;
2791 tx_ctx.head_wb_addr = ring->dma +
2792 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002793
2794 /* As part of VSI creation/update, FW allocates certain
2795 * Tx arbitration queue sets for each TC enabled for
2796 * the VSI. The FW returns the handles to these queue
2797 * sets as part of the response buffer to Add VSI,
2798 * Update VSI, etc. AQ commands. It is expected that
2799 * these queue set handles be associated with the Tx
2800 * queues by the driver as part of the TX queue context
2801 * initialization. This has to be done regardless of
2802 * DCB as by default everything is mapped to TC0.
2803 */
2804 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2805 tx_ctx.rdylist_act = 0;
2806
2807 /* clear the context in the HMC */
2808 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2809 if (err) {
2810 dev_info(&vsi->back->pdev->dev,
2811 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2812 ring->queue_index, pf_q, err);
2813 return -ENOMEM;
2814 }
2815
2816 /* set the context in the HMC */
2817 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2818 if (err) {
2819 dev_info(&vsi->back->pdev->dev,
2820 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2821 ring->queue_index, pf_q, err);
2822 return -ENOMEM;
2823 }
2824
2825 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002826 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002827 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002828 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2829 I40E_QTX_CTL_VFVM_INDX_MASK;
2830 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002831 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002832 }
2833
Shannon Nelson13fd9772013-09-28 07:14:19 +00002834 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2835 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002836 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2837 i40e_flush(hw);
2838
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839 /* cache tail off for easier writes later */
2840 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2841
2842 return 0;
2843}
2844
2845/**
2846 * i40e_configure_rx_ring - Configure a receive ring context
2847 * @ring: The Rx ring to configure
2848 *
2849 * Configure the Rx descriptor ring in the HMC context.
2850 **/
2851static int i40e_configure_rx_ring(struct i40e_ring *ring)
2852{
2853 struct i40e_vsi *vsi = ring->vsi;
2854 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2855 u16 pf_q = vsi->base_queue + ring->queue_index;
2856 struct i40e_hw *hw = &vsi->back->hw;
2857 struct i40e_hmc_obj_rxq rx_ctx;
2858 i40e_status err = 0;
2859
2860 ring->state = 0;
2861
2862 /* clear the context structure first */
2863 memset(&rx_ctx, 0, sizeof(rx_ctx));
2864
2865 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002866
2867 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002868
2869 rx_ctx.base = (ring->dma / 128);
2870 rx_ctx.qlen = ring->count;
2871
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002872 /* use 32 byte descriptors */
2873 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002874
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002875 /* descriptor type is always zero
2876 * rx_ctx.dtype = 0;
2877 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002878 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002879
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002880 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002881 if (hw->revision_id == 0)
2882 rx_ctx.lrxqthresh = 0;
2883 else
2884 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002885 rx_ctx.crcstrip = 1;
2886 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002887 /* this controls whether VLAN is stripped from inner headers */
2888 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002889#ifdef I40E_FCOE
2890 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2891#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002892 /* set the prefena field to 1 because the manual says to */
2893 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002894
2895 /* clear the context in the HMC */
2896 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2897 if (err) {
2898 dev_info(&vsi->back->pdev->dev,
2899 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2900 ring->queue_index, pf_q, err);
2901 return -ENOMEM;
2902 }
2903
2904 /* set the context in the HMC */
2905 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2906 if (err) {
2907 dev_info(&vsi->back->pdev->dev,
2908 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2909 ring->queue_index, pf_q, err);
2910 return -ENOMEM;
2911 }
2912
2913 /* cache tail for quicker writes, and clear the reg before use */
2914 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2915 writel(0, ring->tail);
2916
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002917 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002918
2919 return 0;
2920}
2921
2922/**
2923 * i40e_vsi_configure_tx - Configure the VSI for Tx
2924 * @vsi: VSI structure describing this set of rings and resources
2925 *
2926 * Configure the Tx VSI for operation.
2927 **/
2928static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2929{
2930 int err = 0;
2931 u16 i;
2932
Alexander Duyck9f65e152013-09-28 06:00:58 +00002933 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2934 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002935
2936 return err;
2937}
2938
2939/**
2940 * i40e_vsi_configure_rx - Configure the VSI for Rx
2941 * @vsi: the VSI being configured
2942 *
2943 * Configure the Rx VSI for operation.
2944 **/
2945static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2946{
2947 int err = 0;
2948 u16 i;
2949
2950 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2951 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2952 + ETH_FCS_LEN + VLAN_HLEN;
2953 else
2954 vsi->max_frame = I40E_RXBUFFER_2048;
2955
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002956 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002957
Vasu Dev38e00432014-08-01 13:27:03 -07002958#ifdef I40E_FCOE
2959 /* setup rx buffer for FCoE */
2960 if ((vsi->type == I40E_VSI_FCOE) &&
2961 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07002962 vsi->rx_buf_len = I40E_RXBUFFER_3072;
2963 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07002964 }
2965
2966#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002967 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002968 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002969 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002970
2971 /* set up individual rings */
2972 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002973 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002974
2975 return err;
2976}
2977
2978/**
2979 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2980 * @vsi: ptr to the VSI
2981 **/
2982static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2983{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00002984 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002985 u16 qoffset, qcount;
2986 int i, n;
2987
Parikh, Neeravcd238a32015-02-21 06:43:37 +00002988 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2989 /* Reset the TC information */
2990 for (i = 0; i < vsi->num_queue_pairs; i++) {
2991 rx_ring = vsi->rx_rings[i];
2992 tx_ring = vsi->tx_rings[i];
2993 rx_ring->dcb_tc = 0;
2994 tx_ring->dcb_tc = 0;
2995 }
2996 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997
2998 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002999 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000 continue;
3001
3002 qoffset = vsi->tc_config.tc_info[n].qoffset;
3003 qcount = vsi->tc_config.tc_info[n].qcount;
3004 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003005 rx_ring = vsi->rx_rings[i];
3006 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003007 rx_ring->dcb_tc = n;
3008 tx_ring->dcb_tc = n;
3009 }
3010 }
3011}
3012
3013/**
3014 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3015 * @vsi: ptr to the VSI
3016 **/
3017static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3018{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003019 struct i40e_pf *pf = vsi->back;
3020 int err;
3021
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022 if (vsi->netdev)
3023 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003024
3025 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3026 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3027 if (err) {
3028 dev_warn(&pf->pdev->dev,
3029 "could not set up macaddr; err %d\n", err);
3030 }
3031 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032}
3033
3034/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003035 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3036 * @vsi: Pointer to the targeted VSI
3037 *
3038 * This function replays the hlist on the hw where all the SB Flow Director
3039 * filters were saved.
3040 **/
3041static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3042{
3043 struct i40e_fdir_filter *filter;
3044 struct i40e_pf *pf = vsi->back;
3045 struct hlist_node *node;
3046
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003047 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3048 return;
3049
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003050 hlist_for_each_entry_safe(filter, node,
3051 &pf->fdir_filter_list, fdir_node) {
3052 i40e_add_del_fdir(vsi, filter, true);
3053 }
3054}
3055
3056/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003057 * i40e_vsi_configure - Set up the VSI for action
3058 * @vsi: the VSI being configured
3059 **/
3060static int i40e_vsi_configure(struct i40e_vsi *vsi)
3061{
3062 int err;
3063
3064 i40e_set_vsi_rx_mode(vsi);
3065 i40e_restore_vlan(vsi);
3066 i40e_vsi_config_dcb_rings(vsi);
3067 err = i40e_vsi_configure_tx(vsi);
3068 if (!err)
3069 err = i40e_vsi_configure_rx(vsi);
3070
3071 return err;
3072}
3073
3074/**
3075 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3076 * @vsi: the VSI being configured
3077 **/
3078static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3079{
3080 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003081 struct i40e_hw *hw = &pf->hw;
3082 u16 vector;
3083 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003084 u32 qp;
3085
3086 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3087 * and PFINT_LNKLSTn registers, e.g.:
3088 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3089 */
3090 qp = vsi->base_queue;
3091 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003092 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003093 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3094
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003095 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003096 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003097 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3098 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3099 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003100 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003101 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3102 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3103 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003104 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3105 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003106
3107 /* Linked list for the queuepairs assigned to this vector */
3108 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3109 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003110 u32 val;
3111
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003112 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3113 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3114 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3115 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3116 (I40E_QUEUE_TYPE_TX
3117 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3118
3119 wr32(hw, I40E_QINT_RQCTL(qp), val);
3120
3121 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3122 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3123 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3124 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3125 (I40E_QUEUE_TYPE_RX
3126 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3127
3128 /* Terminate the linked list */
3129 if (q == (q_vector->num_ringpairs - 1))
3130 val |= (I40E_QUEUE_END_OF_LIST
3131 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3132
3133 wr32(hw, I40E_QINT_TQCTL(qp), val);
3134 qp++;
3135 }
3136 }
3137
3138 i40e_flush(hw);
3139}
3140
3141/**
3142 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3143 * @hw: ptr to the hardware info
3144 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003145static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003146{
Jacob Kellerab437b52014-12-14 01:55:08 +00003147 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003148 u32 val;
3149
3150 /* clear things first */
3151 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3152 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3153
3154 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3155 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3156 I40E_PFINT_ICR0_ENA_GRST_MASK |
3157 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3158 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003159 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3160 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3161 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3162
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003163 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3164 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3165
Jacob Kellerab437b52014-12-14 01:55:08 +00003166 if (pf->flags & I40E_FLAG_PTP)
3167 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3168
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003169 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3170
3171 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003172 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3173 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003174
3175 /* OTHER_ITR_IDX = 0 */
3176 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3177}
3178
3179/**
3180 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3181 * @vsi: the VSI being configured
3182 **/
3183static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3184{
Alexander Duyck493fb302013-09-28 07:01:44 +00003185 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003186 struct i40e_pf *pf = vsi->back;
3187 struct i40e_hw *hw = &pf->hw;
3188 u32 val;
3189
3190 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003191 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003192 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003193 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3194 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003195 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003196 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3197 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3198
Jacob Kellerab437b52014-12-14 01:55:08 +00003199 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003200
3201 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3202 wr32(hw, I40E_PFINT_LNKLST0, 0);
3203
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003204 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003205 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3206 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3207 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3208
3209 wr32(hw, I40E_QINT_RQCTL(0), val);
3210
3211 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3212 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3213 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3214
3215 wr32(hw, I40E_QINT_TQCTL(0), val);
3216 i40e_flush(hw);
3217}
3218
3219/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003220 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3221 * @pf: board private structure
3222 **/
3223void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3224{
3225 struct i40e_hw *hw = &pf->hw;
3226
3227 wr32(hw, I40E_PFINT_DYN_CTL0,
3228 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3229 i40e_flush(hw);
3230}
3231
3232/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3234 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003235 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003236 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003237void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238{
3239 struct i40e_hw *hw = &pf->hw;
3240 u32 val;
3241
3242 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003243 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003244 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3245
3246 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3247 i40e_flush(hw);
3248}
3249
3250/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003251 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3252 * @irq: interrupt number
3253 * @data: pointer to a q_vector
3254 **/
3255static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3256{
3257 struct i40e_q_vector *q_vector = data;
3258
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003259 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003260 return IRQ_HANDLED;
3261
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003262 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003263
3264 return IRQ_HANDLED;
3265}
3266
3267/**
Alan Brady96db7762016-09-14 16:24:38 -07003268 * i40e_irq_affinity_notify - Callback for affinity changes
3269 * @notify: context as to what irq was changed
3270 * @mask: the new affinity mask
3271 *
3272 * This is a callback function used by the irq_set_affinity_notifier function
3273 * so that we may register to receive changes to the irq affinity masks.
3274 **/
3275static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3276 const cpumask_t *mask)
3277{
3278 struct i40e_q_vector *q_vector =
3279 container_of(notify, struct i40e_q_vector, affinity_notify);
3280
3281 q_vector->affinity_mask = *mask;
3282}
3283
3284/**
3285 * i40e_irq_affinity_release - Callback for affinity notifier release
3286 * @ref: internal core kernel usage
3287 *
3288 * This is a callback function used by the irq_set_affinity_notifier function
3289 * to inform the current notification subscriber that they will no longer
3290 * receive notifications.
3291 **/
3292static void i40e_irq_affinity_release(struct kref *ref) {}
3293
3294/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003295 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3296 * @vsi: the VSI being configured
3297 * @basename: name for the vector
3298 *
3299 * Allocates MSI-X vectors and requests interrupts from the kernel.
3300 **/
3301static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3302{
3303 int q_vectors = vsi->num_q_vectors;
3304 struct i40e_pf *pf = vsi->back;
3305 int base = vsi->base_vector;
3306 int rx_int_idx = 0;
3307 int tx_int_idx = 0;
3308 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003309 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003310
3311 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003312 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003313
Alan Brady96db7762016-09-14 16:24:38 -07003314 irq_num = pf->msix_entries[base + vector].vector;
3315
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003316 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003317 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3318 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3319 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003320 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003321 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3322 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003323 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003324 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3325 "%s-%s-%d", basename, "tx", tx_int_idx++);
3326 } else {
3327 /* skip this unused q_vector */
3328 continue;
3329 }
Alan Brady96db7762016-09-14 16:24:38 -07003330 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003331 vsi->irq_handler,
3332 0,
3333 q_vector->name,
3334 q_vector);
3335 if (err) {
3336 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003337 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003338 goto free_queue_irqs;
3339 }
Alan Brady96db7762016-09-14 16:24:38 -07003340
3341 /* register for affinity change notifications */
3342 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3343 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3344 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003345 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003346 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003347 }
3348
Shannon Nelson63741842014-04-23 04:50:16 +00003349 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003350 return 0;
3351
3352free_queue_irqs:
3353 while (vector) {
3354 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003355 irq_num = pf->msix_entries[base + vector].vector;
3356 irq_set_affinity_notifier(irq_num, NULL);
3357 irq_set_affinity_hint(irq_num, NULL);
3358 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003359 }
3360 return err;
3361}
3362
3363/**
3364 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3365 * @vsi: the VSI being un-configured
3366 **/
3367static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3368{
3369 struct i40e_pf *pf = vsi->back;
3370 struct i40e_hw *hw = &pf->hw;
3371 int base = vsi->base_vector;
3372 int i;
3373
3374 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003375 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3376 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003377 }
3378
3379 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3380 for (i = vsi->base_vector;
3381 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3382 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3383
3384 i40e_flush(hw);
3385 for (i = 0; i < vsi->num_q_vectors; i++)
3386 synchronize_irq(pf->msix_entries[i + base].vector);
3387 } else {
3388 /* Legacy and MSI mode - this stops all interrupt handling */
3389 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3390 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3391 i40e_flush(hw);
3392 synchronize_irq(pf->pdev->irq);
3393 }
3394}
3395
3396/**
3397 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3398 * @vsi: the VSI being configured
3399 **/
3400static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3401{
3402 struct i40e_pf *pf = vsi->back;
3403 int i;
3404
3405 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003406 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003407 i40e_irq_dynamic_enable(vsi, i);
3408 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003409 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003410 }
3411
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003412 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003413 return 0;
3414}
3415
3416/**
3417 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3418 * @pf: board private structure
3419 **/
3420static void i40e_stop_misc_vector(struct i40e_pf *pf)
3421{
3422 /* Disable ICR 0 */
3423 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3424 i40e_flush(&pf->hw);
3425}
3426
3427/**
3428 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3429 * @irq: interrupt number
3430 * @data: pointer to a q_vector
3431 *
3432 * This is the handler used for all MSI/Legacy interrupts, and deals
3433 * with both queue and non-queue interrupts. This is also used in
3434 * MSIX mode to handle the non-queue interrupts.
3435 **/
3436static irqreturn_t i40e_intr(int irq, void *data)
3437{
3438 struct i40e_pf *pf = (struct i40e_pf *)data;
3439 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003440 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003441 u32 icr0, icr0_remaining;
3442 u32 val, ena_mask;
3443
3444 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003445 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446
Shannon Nelson116a57d2013-09-28 07:13:59 +00003447 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3448 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003449 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003450
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003451 /* if interrupt but no bits showing, must be SWINT */
3452 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3453 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3454 pf->sw_int_count++;
3455
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003456 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3457 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3458 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3459 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3460 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3461 }
3462
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003463 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3464 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003465 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3466 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003467
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003468 /* We do not have a way to disarm Queue causes while leaving
3469 * interrupt enabled for all other causes, ideally
3470 * interrupt should be disabled while we are in NAPI but
3471 * this is not a performance path and napi_schedule()
3472 * can deal with rescheduling.
3473 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003474 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003475 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003476 }
3477
3478 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3479 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3480 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003481 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003482 }
3483
3484 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3485 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3486 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3487 }
3488
3489 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3490 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3491 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3492 }
3493
3494 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3495 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3496 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3497 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3498 val = rd32(hw, I40E_GLGEN_RSTAT);
3499 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3500 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003501 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003502 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003503 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003504 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003505 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003506 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003507 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003508 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003509 }
3510
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003511 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3512 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3513 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003514 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3515 rd32(hw, I40E_PFHMC_ERRORINFO),
3516 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003517 }
3518
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003519 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3520 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3521
3522 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003523 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003524 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003525 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003526 }
3527
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003528 /* If a critical error is pending we have no choice but to reset the
3529 * device.
3530 * Report and mask out any remaining unexpected interrupts.
3531 */
3532 icr0_remaining = icr0 & ena_mask;
3533 if (icr0_remaining) {
3534 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3535 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003536 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003538 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003539 dev_info(&pf->pdev->dev, "device will be reset\n");
3540 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3541 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003542 }
3543 ena_mask &= ~icr0_remaining;
3544 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003545 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003547enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003548 /* re-enable interrupt causes */
3549 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003550 if (!test_bit(__I40E_DOWN, &pf->state)) {
3551 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003552 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003553 }
3554
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003555 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003556}
3557
3558/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003559 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3560 * @tx_ring: tx ring to clean
3561 * @budget: how many cleans we're allowed
3562 *
3563 * Returns true if there's any budget left (e.g. the clean is finished)
3564 **/
3565static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3566{
3567 struct i40e_vsi *vsi = tx_ring->vsi;
3568 u16 i = tx_ring->next_to_clean;
3569 struct i40e_tx_buffer *tx_buf;
3570 struct i40e_tx_desc *tx_desc;
3571
3572 tx_buf = &tx_ring->tx_bi[i];
3573 tx_desc = I40E_TX_DESC(tx_ring, i);
3574 i -= tx_ring->count;
3575
3576 do {
3577 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3578
3579 /* if next_to_watch is not set then there is no work pending */
3580 if (!eop_desc)
3581 break;
3582
3583 /* prevent any other reads prior to eop_desc */
3584 read_barrier_depends();
3585
3586 /* if the descriptor isn't done, no work yet to do */
3587 if (!(eop_desc->cmd_type_offset_bsz &
3588 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3589 break;
3590
3591 /* clear next_to_watch to prevent false hangs */
3592 tx_buf->next_to_watch = NULL;
3593
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003594 tx_desc->buffer_addr = 0;
3595 tx_desc->cmd_type_offset_bsz = 0;
3596 /* move past filter desc */
3597 tx_buf++;
3598 tx_desc++;
3599 i++;
3600 if (unlikely(!i)) {
3601 i -= tx_ring->count;
3602 tx_buf = tx_ring->tx_bi;
3603 tx_desc = I40E_TX_DESC(tx_ring, 0);
3604 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003605 /* unmap skb header data */
3606 dma_unmap_single(tx_ring->dev,
3607 dma_unmap_addr(tx_buf, dma),
3608 dma_unmap_len(tx_buf, len),
3609 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003610 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3611 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003612
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003613 tx_buf->raw_buf = NULL;
3614 tx_buf->tx_flags = 0;
3615 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003616 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003617 tx_desc->buffer_addr = 0;
3618 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003619
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003620 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003621 tx_buf++;
3622 tx_desc++;
3623 i++;
3624 if (unlikely(!i)) {
3625 i -= tx_ring->count;
3626 tx_buf = tx_ring->tx_bi;
3627 tx_desc = I40E_TX_DESC(tx_ring, 0);
3628 }
3629
3630 /* update budget accounting */
3631 budget--;
3632 } while (likely(budget));
3633
3634 i += tx_ring->count;
3635 tx_ring->next_to_clean = i;
3636
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003637 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003638 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003639
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003640 return budget > 0;
3641}
3642
3643/**
3644 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3645 * @irq: interrupt number
3646 * @data: pointer to a q_vector
3647 **/
3648static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3649{
3650 struct i40e_q_vector *q_vector = data;
3651 struct i40e_vsi *vsi;
3652
3653 if (!q_vector->tx.ring)
3654 return IRQ_HANDLED;
3655
3656 vsi = q_vector->tx.ring->vsi;
3657 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3658
3659 return IRQ_HANDLED;
3660}
3661
3662/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003663 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664 * @vsi: the VSI being configured
3665 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003666 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003667 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003668static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003669{
Alexander Duyck493fb302013-09-28 07:01:44 +00003670 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003671 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3672 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673
3674 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003675 tx_ring->next = q_vector->tx.ring;
3676 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003678
3679 rx_ring->q_vector = q_vector;
3680 rx_ring->next = q_vector->rx.ring;
3681 q_vector->rx.ring = rx_ring;
3682 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683}
3684
3685/**
3686 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3687 * @vsi: the VSI being configured
3688 *
3689 * This function maps descriptor rings to the queue-specific vectors
3690 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3691 * one vector per queue pair, but on a constrained vector budget, we
3692 * group the queue pairs as "efficiently" as possible.
3693 **/
3694static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3695{
3696 int qp_remaining = vsi->num_queue_pairs;
3697 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003698 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003699 int v_start = 0;
3700 int qp_idx = 0;
3701
3702 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3703 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003704 * It is also important to go through all the vectors available to be
3705 * sure that if we don't use all the vectors, that the remaining vectors
3706 * are cleared. This is especially important when decreasing the
3707 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003708 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003709 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003710 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3711
3712 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3713
3714 q_vector->num_ringpairs = num_ringpairs;
3715
3716 q_vector->rx.count = 0;
3717 q_vector->tx.count = 0;
3718 q_vector->rx.ring = NULL;
3719 q_vector->tx.ring = NULL;
3720
3721 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003722 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003723 qp_idx++;
3724 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003725 }
3726 }
3727}
3728
3729/**
3730 * i40e_vsi_request_irq - Request IRQ from the OS
3731 * @vsi: the VSI being configured
3732 * @basename: name for the vector
3733 **/
3734static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3735{
3736 struct i40e_pf *pf = vsi->back;
3737 int err;
3738
3739 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3740 err = i40e_vsi_request_irq_msix(vsi, basename);
3741 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3742 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003743 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003744 else
3745 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003746 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747
3748 if (err)
3749 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3750
3751 return err;
3752}
3753
3754#ifdef CONFIG_NET_POLL_CONTROLLER
3755/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003756 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003757 * @netdev: network interface device structure
3758 *
3759 * This is used by netconsole to send skbs without having to re-enable
3760 * interrupts. It's not called while the normal interrupt routine is executing.
3761 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003762#ifdef I40E_FCOE
3763void i40e_netpoll(struct net_device *netdev)
3764#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003765static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003766#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003767{
3768 struct i40e_netdev_priv *np = netdev_priv(netdev);
3769 struct i40e_vsi *vsi = np->vsi;
3770 struct i40e_pf *pf = vsi->back;
3771 int i;
3772
3773 /* if interface is down do nothing */
3774 if (test_bit(__I40E_DOWN, &vsi->state))
3775 return;
3776
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003777 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3778 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003779 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003780 } else {
3781 i40e_intr(pf->pdev->irq, netdev);
3782 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003783}
3784#endif
3785
3786/**
Neerav Parikh23527302014-06-03 23:50:15 +00003787 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3788 * @pf: the PF being configured
3789 * @pf_q: the PF queue
3790 * @enable: enable or disable state of the queue
3791 *
3792 * This routine will wait for the given Tx queue of the PF to reach the
3793 * enabled or disabled state.
3794 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3795 * multiple retries; else will return 0 in case of success.
3796 **/
3797static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3798{
3799 int i;
3800 u32 tx_reg;
3801
3802 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3803 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3804 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3805 break;
3806
Neerav Parikhf98a2002014-09-13 07:40:44 +00003807 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003808 }
3809 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3810 return -ETIMEDOUT;
3811
3812 return 0;
3813}
3814
3815/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003816 * i40e_vsi_control_tx - Start or stop a VSI's rings
3817 * @vsi: the VSI being configured
3818 * @enable: start or stop the rings
3819 **/
3820static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3821{
3822 struct i40e_pf *pf = vsi->back;
3823 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003824 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003825 u32 tx_reg;
3826
3827 pf_q = vsi->base_queue;
3828 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003829
3830 /* warn the TX unit of coming changes */
3831 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3832 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003833 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003834
Mitch Williams6c5ef622014-02-20 19:29:16 -08003835 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003836 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003837 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3838 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3839 break;
3840 usleep_range(1000, 2000);
3841 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003842 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003843 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003844 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003845
3846 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003847 if (enable) {
3848 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003849 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003850 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003851 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003852 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003853
3854 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003855 /* No waiting for the Tx queue to disable */
3856 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3857 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003858
3859 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003860 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3861 if (ret) {
3862 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003863 "VSI seid %d Tx ring %d %sable timeout\n",
3864 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003865 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003866 }
3867 }
3868
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003869 if (hw->revision_id == 0)
3870 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003871 return ret;
3872}
3873
3874/**
3875 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3876 * @pf: the PF being configured
3877 * @pf_q: the PF queue
3878 * @enable: enable or disable state of the queue
3879 *
3880 * This routine will wait for the given Rx queue of the PF to reach the
3881 * enabled or disabled state.
3882 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3883 * multiple retries; else will return 0 in case of success.
3884 **/
3885static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3886{
3887 int i;
3888 u32 rx_reg;
3889
3890 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3891 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3892 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3893 break;
3894
Neerav Parikhf98a2002014-09-13 07:40:44 +00003895 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003896 }
3897 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3898 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003899
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003900 return 0;
3901}
3902
3903/**
3904 * i40e_vsi_control_rx - Start or stop a VSI's rings
3905 * @vsi: the VSI being configured
3906 * @enable: start or stop the rings
3907 **/
3908static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3909{
3910 struct i40e_pf *pf = vsi->back;
3911 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003912 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003913 u32 rx_reg;
3914
3915 pf_q = vsi->base_queue;
3916 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003917 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003918 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003919 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3920 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3921 break;
3922 usleep_range(1000, 2000);
3923 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003924
Catherine Sullivan7c122002014-03-14 07:32:29 +00003925 /* Skip if the queue is already in the requested state */
3926 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3927 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003928
3929 /* turn on/off the queue */
3930 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003931 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003932 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003933 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003934 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003935 /* No waiting for the Tx queue to disable */
3936 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3937 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003938
3939 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003940 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
3941 if (ret) {
3942 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003943 "VSI seid %d Rx ring %d %sable timeout\n",
3944 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003945 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003946 }
3947 }
3948
Neerav Parikh23527302014-06-03 23:50:15 +00003949 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003950}
3951
3952/**
3953 * i40e_vsi_control_rings - Start or stop a VSI's rings
3954 * @vsi: the VSI being configured
3955 * @enable: start or stop the rings
3956 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00003957int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003958{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003959 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003960
3961 /* do rx first for enable and last for disable */
3962 if (request) {
3963 ret = i40e_vsi_control_rx(vsi, request);
3964 if (ret)
3965 return ret;
3966 ret = i40e_vsi_control_tx(vsi, request);
3967 } else {
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00003968 /* Ignore return value, we need to shutdown whatever we can */
3969 i40e_vsi_control_tx(vsi, request);
3970 i40e_vsi_control_rx(vsi, request);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003971 }
3972
3973 return ret;
3974}
3975
3976/**
3977 * i40e_vsi_free_irq - Free the irq association with the OS
3978 * @vsi: the VSI being configured
3979 **/
3980static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3981{
3982 struct i40e_pf *pf = vsi->back;
3983 struct i40e_hw *hw = &pf->hw;
3984 int base = vsi->base_vector;
3985 u32 val, qp;
3986 int i;
3987
3988 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3989 if (!vsi->q_vectors)
3990 return;
3991
Shannon Nelson63741842014-04-23 04:50:16 +00003992 if (!vsi->irqs_ready)
3993 return;
3994
3995 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003996 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07003997 int irq_num;
3998 u16 vector;
3999
4000 vector = i + base;
4001 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004002
4003 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004004 if (!vsi->q_vectors[i] ||
4005 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004006 continue;
4007
Alan Brady96db7762016-09-14 16:24:38 -07004008 /* clear the affinity notifier in the IRQ descriptor */
4009 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004010 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004011 irq_set_affinity_hint(irq_num, NULL);
4012 synchronize_irq(irq_num);
4013 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004014
4015 /* Tear down the interrupt queue link list
4016 *
4017 * We know that they come in pairs and always
4018 * the Rx first, then the Tx. To clear the
4019 * link list, stick the EOL value into the
4020 * next_q field of the registers.
4021 */
4022 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4023 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4024 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4025 val |= I40E_QUEUE_END_OF_LIST
4026 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4027 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4028
4029 while (qp != I40E_QUEUE_END_OF_LIST) {
4030 u32 next;
4031
4032 val = rd32(hw, I40E_QINT_RQCTL(qp));
4033
4034 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4035 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4036 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4037 I40E_QINT_RQCTL_INTEVENT_MASK);
4038
4039 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4040 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4041
4042 wr32(hw, I40E_QINT_RQCTL(qp), val);
4043
4044 val = rd32(hw, I40E_QINT_TQCTL(qp));
4045
4046 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4047 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4048
4049 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4050 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4051 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4052 I40E_QINT_TQCTL_INTEVENT_MASK);
4053
4054 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4055 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4056
4057 wr32(hw, I40E_QINT_TQCTL(qp), val);
4058 qp = next;
4059 }
4060 }
4061 } else {
4062 free_irq(pf->pdev->irq, pf);
4063
4064 val = rd32(hw, I40E_PFINT_LNKLST0);
4065 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4066 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4067 val |= I40E_QUEUE_END_OF_LIST
4068 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4069 wr32(hw, I40E_PFINT_LNKLST0, val);
4070
4071 val = rd32(hw, I40E_QINT_RQCTL(qp));
4072 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4073 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4074 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4075 I40E_QINT_RQCTL_INTEVENT_MASK);
4076
4077 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4078 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4079
4080 wr32(hw, I40E_QINT_RQCTL(qp), val);
4081
4082 val = rd32(hw, I40E_QINT_TQCTL(qp));
4083
4084 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4085 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4086 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4087 I40E_QINT_TQCTL_INTEVENT_MASK);
4088
4089 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4090 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4091
4092 wr32(hw, I40E_QINT_TQCTL(qp), val);
4093 }
4094}
4095
4096/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004097 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4098 * @vsi: the VSI being configured
4099 * @v_idx: Index of vector to be freed
4100 *
4101 * This function frees the memory allocated to the q_vector. In addition if
4102 * NAPI is enabled it will delete any references to the NAPI struct prior
4103 * to freeing the q_vector.
4104 **/
4105static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4106{
4107 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004108 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004109
4110 if (!q_vector)
4111 return;
4112
4113 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004114 i40e_for_each_ring(ring, q_vector->tx)
4115 ring->q_vector = NULL;
4116
4117 i40e_for_each_ring(ring, q_vector->rx)
4118 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004119
4120 /* only VSI w/ an associated netdev is set up w/ NAPI */
4121 if (vsi->netdev)
4122 netif_napi_del(&q_vector->napi);
4123
4124 vsi->q_vectors[v_idx] = NULL;
4125
4126 kfree_rcu(q_vector, rcu);
4127}
4128
4129/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004130 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4131 * @vsi: the VSI being un-configured
4132 *
4133 * This frees the memory allocated to the q_vectors and
4134 * deletes references to the NAPI struct.
4135 **/
4136static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4137{
4138 int v_idx;
4139
Alexander Duyck493fb302013-09-28 07:01:44 +00004140 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4141 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004142}
4143
4144/**
4145 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4146 * @pf: board private structure
4147 **/
4148static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4149{
4150 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4151 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4152 pci_disable_msix(pf->pdev);
4153 kfree(pf->msix_entries);
4154 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004155 kfree(pf->irq_pile);
4156 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004157 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4158 pci_disable_msi(pf->pdev);
4159 }
4160 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4161}
4162
4163/**
4164 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4165 * @pf: board private structure
4166 *
4167 * We go through and clear interrupt specific resources and reset the structure
4168 * to pre-load conditions
4169 **/
4170static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4171{
4172 int i;
4173
Shannon Nelsone1477582015-02-21 06:44:33 +00004174 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004175 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004176 synchronize_irq(pf->msix_entries[0].vector);
4177 free_irq(pf->msix_entries[0].vector, pf);
4178 }
4179
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004180 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4181 I40E_IWARP_IRQ_PILE_ID);
4182
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004183 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004184 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004185 if (pf->vsi[i])
4186 i40e_vsi_free_q_vectors(pf->vsi[i]);
4187 i40e_reset_interrupt_capability(pf);
4188}
4189
4190/**
4191 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4192 * @vsi: the VSI being configured
4193 **/
4194static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4195{
4196 int q_idx;
4197
4198 if (!vsi->netdev)
4199 return;
4200
4201 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004202 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004203}
4204
4205/**
4206 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4207 * @vsi: the VSI being configured
4208 **/
4209static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4210{
4211 int q_idx;
4212
4213 if (!vsi->netdev)
4214 return;
4215
4216 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004217 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004218}
4219
4220/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004221 * i40e_vsi_close - Shut down a VSI
4222 * @vsi: the vsi to be quelled
4223 **/
4224static void i40e_vsi_close(struct i40e_vsi *vsi)
4225{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004226 bool reset = false;
4227
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004228 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4229 i40e_down(vsi);
4230 i40e_vsi_free_irq(vsi);
4231 i40e_vsi_free_tx_resources(vsi);
4232 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004233 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004234 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4235 reset = true;
4236 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004237}
4238
4239/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004240 * i40e_quiesce_vsi - Pause a given VSI
4241 * @vsi: the VSI being paused
4242 **/
4243static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4244{
4245 if (test_bit(__I40E_DOWN, &vsi->state))
4246 return;
4247
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004248 /* No need to disable FCoE VSI when Tx suspended */
4249 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4250 vsi->type == I40E_VSI_FCOE) {
4251 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004252 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004253 return;
4254 }
4255
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004256 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004257 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004258 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004259 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004260 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004261}
4262
4263/**
4264 * i40e_unquiesce_vsi - Resume a given VSI
4265 * @vsi: the VSI being resumed
4266 **/
4267static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4268{
4269 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4270 return;
4271
4272 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4273 if (vsi->netdev && netif_running(vsi->netdev))
4274 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4275 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004276 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004277}
4278
4279/**
4280 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4281 * @pf: the PF
4282 **/
4283static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4284{
4285 int v;
4286
Mitch Williams505682c2014-05-20 08:01:37 +00004287 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004288 if (pf->vsi[v])
4289 i40e_quiesce_vsi(pf->vsi[v]);
4290 }
4291}
4292
4293/**
4294 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4295 * @pf: the PF
4296 **/
4297static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4298{
4299 int v;
4300
Mitch Williams505682c2014-05-20 08:01:37 +00004301 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004302 if (pf->vsi[v])
4303 i40e_unquiesce_vsi(pf->vsi[v]);
4304 }
4305}
4306
Neerav Parikh69129dc2014-11-12 00:18:46 +00004307#ifdef CONFIG_I40E_DCB
4308/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004309 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004310 * @vsi: the VSI being configured
4311 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004312 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004313 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004314static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004315{
4316 struct i40e_pf *pf = vsi->back;
4317 int i, pf_q, ret;
4318
4319 pf_q = vsi->base_queue;
4320 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4321 /* Check and wait for the disable status of the queue */
4322 ret = i40e_pf_txq_wait(pf, pf_q, false);
4323 if (ret) {
4324 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004325 "VSI seid %d Tx ring %d disable timeout\n",
4326 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004327 return ret;
4328 }
4329 }
4330
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004331 pf_q = vsi->base_queue;
4332 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4333 /* Check and wait for the disable status of the queue */
4334 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4335 if (ret) {
4336 dev_info(&pf->pdev->dev,
4337 "VSI seid %d Rx ring %d disable timeout\n",
4338 vsi->seid, pf_q);
4339 return ret;
4340 }
4341 }
4342
Neerav Parikh69129dc2014-11-12 00:18:46 +00004343 return 0;
4344}
4345
4346/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004347 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004348 * @pf: the PF
4349 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004350 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004351 * VSIs that are managed by this PF.
4352 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004353static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004354{
4355 int v, ret = 0;
4356
4357 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004358 /* No need to wait for FCoE VSI queues */
4359 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004360 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004361 if (ret)
4362 break;
4363 }
4364 }
4365
4366 return ret;
4367}
4368
4369#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004370
4371/**
4372 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4373 * @q_idx: TX queue number
4374 * @vsi: Pointer to VSI struct
4375 *
4376 * This function checks specified queue for given VSI. Detects hung condition.
4377 * Sets hung bit since it is two step process. Before next run of service task
4378 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4379 * hung condition remain unchanged and during subsequent run, this function
4380 * issues SW interrupt to recover from hung condition.
4381 **/
4382static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4383{
4384 struct i40e_ring *tx_ring = NULL;
4385 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004386 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004387 int i;
4388
4389 pf = vsi->back;
4390
4391 /* now that we have an index, find the tx_ring struct */
4392 for (i = 0; i < vsi->num_queue_pairs; i++) {
4393 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4394 if (q_idx == vsi->tx_rings[i]->queue_index) {
4395 tx_ring = vsi->tx_rings[i];
4396 break;
4397 }
4398 }
4399 }
4400
4401 if (!tx_ring)
4402 return;
4403
4404 /* Read interrupt register */
4405 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4406 val = rd32(&pf->hw,
4407 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4408 tx_ring->vsi->base_vector - 1));
4409 else
4410 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4411
4412 head = i40e_get_head(tx_ring);
4413
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004414 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004415
Kiran Patil9c6c1252015-11-06 15:26:02 -08004416 /* HW is done executing descriptors, updated HEAD write back,
4417 * but SW hasn't processed those descriptors. If interrupt is
4418 * not generated from this point ON, it could result into
4419 * dev_watchdog detecting timeout on those netdev_queue,
4420 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004421 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004422 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004423 /* NAPI Poll didn't run and clear since it was set */
4424 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4425 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004426 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",
4427 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004428 tx_ring->next_to_clean, head,
4429 tx_ring->next_to_use,
4430 readl(tx_ring->tail));
4431 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4432 vsi->seid, q_idx, val);
4433 i40e_force_wb(vsi, tx_ring->q_vector);
4434 } else {
4435 /* First Chance - detected possible hung */
4436 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4437 &tx_ring->q_vector->hung_detected);
4438 }
4439 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004440
4441 /* This is the case where we have interrupts missing,
4442 * so the tx_pending in HW will most likely be 0, but we
4443 * will have tx_pending in SW since the WB happened but the
4444 * interrupt got lost.
4445 */
4446 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4447 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4448 if (napi_reschedule(&tx_ring->q_vector->napi))
4449 tx_ring->tx_stats.tx_lost_interrupt++;
4450 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004451}
4452
4453/**
4454 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4455 * @pf: pointer to PF struct
4456 *
4457 * LAN VSI has netdev and netdev has TX queues. This function is to check
4458 * each of those TX queues if they are hung, trigger recovery by issuing
4459 * SW interrupt.
4460 **/
4461static void i40e_detect_recover_hung(struct i40e_pf *pf)
4462{
4463 struct net_device *netdev;
4464 struct i40e_vsi *vsi;
4465 int i;
4466
4467 /* Only for LAN VSI */
4468 vsi = pf->vsi[pf->lan_vsi];
4469
4470 if (!vsi)
4471 return;
4472
4473 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4474 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4475 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4476 return;
4477
4478 /* Make sure type is MAIN VSI */
4479 if (vsi->type != I40E_VSI_MAIN)
4480 return;
4481
4482 netdev = vsi->netdev;
4483 if (!netdev)
4484 return;
4485
4486 /* Bail out if netif_carrier is not OK */
4487 if (!netif_carrier_ok(netdev))
4488 return;
4489
4490 /* Go thru' TX queues for netdev */
4491 for (i = 0; i < netdev->num_tx_queues; i++) {
4492 struct netdev_queue *q;
4493
4494 q = netdev_get_tx_queue(netdev, i);
4495 if (q)
4496 i40e_detect_recover_hung_queue(i, vsi);
4497 }
4498}
4499
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004500/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004501 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004502 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004503 *
4504 * Get TC map for ISCSI PF type that will include iSCSI TC
4505 * and LAN TC.
4506 **/
4507static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4508{
4509 struct i40e_dcb_app_priority_table app;
4510 struct i40e_hw *hw = &pf->hw;
4511 u8 enabled_tc = 1; /* TC0 is always enabled */
4512 u8 tc, i;
4513 /* Get the iSCSI APP TLV */
4514 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4515
4516 for (i = 0; i < dcbcfg->numapps; i++) {
4517 app = dcbcfg->app[i];
4518 if (app.selector == I40E_APP_SEL_TCPIP &&
4519 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4520 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004521 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004522 break;
4523 }
4524 }
4525
4526 return enabled_tc;
4527}
4528
4529/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004530 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4531 * @dcbcfg: the corresponding DCBx configuration structure
4532 *
4533 * Return the number of TCs from given DCBx configuration
4534 **/
4535static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4536{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004537 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004538 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004539 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004540
4541 /* Scan the ETS Config Priority Table to find
4542 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004543 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004544 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004545 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4546 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4547
4548 /* Now scan the bitmask to check for
4549 * contiguous TCs starting with TC0
4550 */
4551 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4552 if (num_tc & BIT(i)) {
4553 if (!tc_unused) {
4554 ret++;
4555 } else {
4556 pr_err("Non-contiguous TC - Disabling DCB\n");
4557 return 1;
4558 }
4559 } else {
4560 tc_unused = 1;
4561 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004562 }
4563
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004564 /* There is always at least TC0 */
4565 if (!ret)
4566 ret = 1;
4567
4568 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004569}
4570
4571/**
4572 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4573 * @dcbcfg: the corresponding DCBx configuration structure
4574 *
4575 * Query the current DCB configuration and return the number of
4576 * traffic classes enabled from the given DCBX config
4577 **/
4578static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4579{
4580 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4581 u8 enabled_tc = 1;
4582 u8 i;
4583
4584 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004585 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004586
4587 return enabled_tc;
4588}
4589
4590/**
4591 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4592 * @pf: PF being queried
4593 *
4594 * Return number of traffic classes enabled for the given PF
4595 **/
4596static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4597{
4598 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004599 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004600 u8 num_tc = 0;
4601 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4602
4603 /* If DCB is not enabled then always in single TC */
4604 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4605 return 1;
4606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004607 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004608 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4609 return i40e_dcb_get_num_tc(dcbcfg);
4610
4611 /* MFP mode return count of enabled TCs for this PF */
4612 if (pf->hw.func_caps.iscsi)
4613 enabled_tc = i40e_get_iscsi_tc_map(pf);
4614 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004615 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004616
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004617 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004618 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004619 num_tc++;
4620 }
4621 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004622}
4623
4624/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004625 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4626 * @pf: PF being queried
4627 *
4628 * Return a bitmap for enabled traffic classes for this PF.
4629 **/
4630static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4631{
4632 /* If DCB is not enabled for this PF then just return default TC */
4633 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004634 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004635
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004636 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004637 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4638 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4639
Neerav Parikhfc51de92015-02-24 06:58:53 +00004640 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004641 if (pf->hw.func_caps.iscsi)
4642 return i40e_get_iscsi_tc_map(pf);
4643 else
David Ertmanea6acb72016-09-20 07:10:50 -07004644 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004645}
4646
4647/**
4648 * i40e_vsi_get_bw_info - Query VSI BW Information
4649 * @vsi: the VSI being queried
4650 *
4651 * Returns 0 on success, negative value on failure
4652 **/
4653static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4654{
4655 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4656 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4657 struct i40e_pf *pf = vsi->back;
4658 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004659 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004660 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004661 int i;
4662
4663 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004664 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4665 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004666 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004667 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4668 i40e_stat_str(&pf->hw, ret),
4669 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004670 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004671 }
4672
4673 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004674 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4675 NULL);
4676 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004677 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004678 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4679 i40e_stat_str(&pf->hw, ret),
4680 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004681 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004682 }
4683
4684 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4685 dev_info(&pf->pdev->dev,
4686 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4687 bw_config.tc_valid_bits,
4688 bw_ets_config.tc_valid_bits);
4689 /* Still continuing */
4690 }
4691
4692 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4693 vsi->bw_max_quanta = bw_config.max_bw;
4694 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4695 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4696 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4697 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4698 vsi->bw_ets_limit_credits[i] =
4699 le16_to_cpu(bw_ets_config.credits[i]);
4700 /* 3 bits out of 4 for each TC */
4701 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4702 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004703
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004704 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004705}
4706
4707/**
4708 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4709 * @vsi: the VSI being configured
4710 * @enabled_tc: TC bitmap
4711 * @bw_credits: BW shared credits per TC
4712 *
4713 * Returns 0 on success, negative value on failure
4714 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004715static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004716 u8 *bw_share)
4717{
4718 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004719 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004720 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004721
4722 bw_data.tc_valid_bits = enabled_tc;
4723 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4724 bw_data.tc_bw_credits[i] = bw_share[i];
4725
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004726 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4727 NULL);
4728 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004729 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004730 "AQ command Config VSI BW allocation per TC failed = %d\n",
4731 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004732 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004733 }
4734
4735 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4736 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4737
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004738 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004739}
4740
4741/**
4742 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4743 * @vsi: the VSI being configured
4744 * @enabled_tc: TC map to be enabled
4745 *
4746 **/
4747static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4748{
4749 struct net_device *netdev = vsi->netdev;
4750 struct i40e_pf *pf = vsi->back;
4751 struct i40e_hw *hw = &pf->hw;
4752 u8 netdev_tc = 0;
4753 int i;
4754 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4755
4756 if (!netdev)
4757 return;
4758
4759 if (!enabled_tc) {
4760 netdev_reset_tc(netdev);
4761 return;
4762 }
4763
4764 /* Set up actual enabled TCs on the VSI */
4765 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4766 return;
4767
4768 /* set per TC queues for the VSI */
4769 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4770 /* Only set TC queues for enabled tcs
4771 *
4772 * e.g. For a VSI that has TC0 and TC3 enabled the
4773 * enabled_tc bitmap would be 0x00001001; the driver
4774 * will set the numtc for netdev as 2 that will be
4775 * referenced by the netdev layer as TC 0 and 1.
4776 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004777 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778 netdev_set_tc_queue(netdev,
4779 vsi->tc_config.tc_info[i].netdev_tc,
4780 vsi->tc_config.tc_info[i].qcount,
4781 vsi->tc_config.tc_info[i].qoffset);
4782 }
4783
4784 /* Assign UP2TC map for the VSI */
4785 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4786 /* Get the actual TC# for the UP */
4787 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4788 /* Get the mapped netdev TC# for the UP */
4789 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4790 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4791 }
4792}
4793
4794/**
4795 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4796 * @vsi: the VSI being configured
4797 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4798 **/
4799static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4800 struct i40e_vsi_context *ctxt)
4801{
4802 /* copy just the sections touched not the entire info
4803 * since not all sections are valid as returned by
4804 * update vsi params
4805 */
4806 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4807 memcpy(&vsi->info.queue_mapping,
4808 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4809 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4810 sizeof(vsi->info.tc_mapping));
4811}
4812
4813/**
4814 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4815 * @vsi: VSI to be configured
4816 * @enabled_tc: TC bitmap
4817 *
4818 * This configures a particular VSI for TCs that are mapped to the
4819 * given TC bitmap. It uses default bandwidth share for TCs across
4820 * VSIs to configure TC for a particular VSI.
4821 *
4822 * NOTE:
4823 * It is expected that the VSI queues have been quisced before calling
4824 * this function.
4825 **/
4826static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4827{
4828 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4829 struct i40e_vsi_context ctxt;
4830 int ret = 0;
4831 int i;
4832
4833 /* Check if enabled_tc is same as existing or new TCs */
4834 if (vsi->tc_config.enabled_tc == enabled_tc)
4835 return ret;
4836
4837 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4838 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004839 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004840 bw_share[i] = 1;
4841 }
4842
4843 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4844 if (ret) {
4845 dev_info(&vsi->back->pdev->dev,
4846 "Failed configuring TC map %d for VSI %d\n",
4847 enabled_tc, vsi->seid);
4848 goto out;
4849 }
4850
4851 /* Update Queue Pairs Mapping for currently enabled UPs */
4852 ctxt.seid = vsi->seid;
4853 ctxt.pf_num = vsi->back->hw.pf_id;
4854 ctxt.vf_num = 0;
4855 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004856 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004857 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4858
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004859 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4860 ctxt.info.valid_sections |=
4861 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4862 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4863 }
4864
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004865 /* Update the VSI after updating the VSI queue-mapping information */
4866 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4867 if (ret) {
4868 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004869 "Update vsi tc config failed, err %s aq_err %s\n",
4870 i40e_stat_str(&vsi->back->hw, ret),
4871 i40e_aq_str(&vsi->back->hw,
4872 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004873 goto out;
4874 }
4875 /* update the local VSI info with updated queue map */
4876 i40e_vsi_update_queue_map(vsi, &ctxt);
4877 vsi->info.valid_sections = 0;
4878
4879 /* Update current VSI BW information */
4880 ret = i40e_vsi_get_bw_info(vsi);
4881 if (ret) {
4882 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004883 "Failed updating vsi bw info, err %s aq_err %s\n",
4884 i40e_stat_str(&vsi->back->hw, ret),
4885 i40e_aq_str(&vsi->back->hw,
4886 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004887 goto out;
4888 }
4889
4890 /* Update the netdev TC setup */
4891 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4892out:
4893 return ret;
4894}
4895
4896/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004897 * i40e_veb_config_tc - Configure TCs for given VEB
4898 * @veb: given VEB
4899 * @enabled_tc: TC bitmap
4900 *
4901 * Configures given TC bitmap for VEB (switching) element
4902 **/
4903int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4904{
4905 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4906 struct i40e_pf *pf = veb->pf;
4907 int ret = 0;
4908 int i;
4909
4910 /* No TCs or already enabled TCs just return */
4911 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4912 return ret;
4913
4914 bw_data.tc_valid_bits = enabled_tc;
4915 /* bw_data.absolute_credits is not set (relative) */
4916
4917 /* Enable ETS TCs with equal BW Share for now */
4918 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004919 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004920 bw_data.tc_bw_share_credits[i] = 1;
4921 }
4922
4923 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4924 &bw_data, NULL);
4925 if (ret) {
4926 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004927 "VEB bw config failed, err %s aq_err %s\n",
4928 i40e_stat_str(&pf->hw, ret),
4929 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004930 goto out;
4931 }
4932
4933 /* Update the BW information */
4934 ret = i40e_veb_get_bw_info(veb);
4935 if (ret) {
4936 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004937 "Failed getting veb bw config, err %s aq_err %s\n",
4938 i40e_stat_str(&pf->hw, ret),
4939 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004940 }
4941
4942out:
4943 return ret;
4944}
4945
4946#ifdef CONFIG_I40E_DCB
4947/**
4948 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
4949 * @pf: PF struct
4950 *
4951 * Reconfigure VEB/VSIs on a given PF; it is assumed that
4952 * the caller would've quiesce all the VSIs before calling
4953 * this function
4954 **/
4955static void i40e_dcb_reconfigure(struct i40e_pf *pf)
4956{
4957 u8 tc_map = 0;
4958 int ret;
4959 u8 v;
4960
4961 /* Enable the TCs available on PF to all VEBs */
4962 tc_map = i40e_pf_get_tc_map(pf);
4963 for (v = 0; v < I40E_MAX_VEB; v++) {
4964 if (!pf->veb[v])
4965 continue;
4966 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
4967 if (ret) {
4968 dev_info(&pf->pdev->dev,
4969 "Failed configuring TC for VEB seid=%d\n",
4970 pf->veb[v]->seid);
4971 /* Will try to configure as many components */
4972 }
4973 }
4974
4975 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00004976 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004977 if (!pf->vsi[v])
4978 continue;
4979
4980 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07004981#ifdef I40E_FCOE
4982 * - For FCoE VSI only enable the TC configured
4983 * as per the APP TLV
4984#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004985 * - For all others keep them at TC0 for now
4986 */
4987 if (v == pf->lan_vsi)
4988 tc_map = i40e_pf_get_tc_map(pf);
4989 else
David Ertmanea6acb72016-09-20 07:10:50 -07004990 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07004991#ifdef I40E_FCOE
4992 if (pf->vsi[v]->type == I40E_VSI_FCOE)
4993 tc_map = i40e_get_fcoe_tc_map(pf);
4994#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004995
4996 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
4997 if (ret) {
4998 dev_info(&pf->pdev->dev,
4999 "Failed configuring TC for VSI seid=%d\n",
5000 pf->vsi[v]->seid);
5001 /* Will try to configure as many components */
5002 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005003 /* Re-configure VSI vectors based on updated TC map */
5004 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005005 if (pf->vsi[v]->netdev)
5006 i40e_dcbnl_set_all(pf->vsi[v]);
5007 }
5008 }
5009}
5010
5011/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005012 * i40e_resume_port_tx - Resume port Tx
5013 * @pf: PF struct
5014 *
5015 * Resume a port's Tx and issue a PF reset in case of failure to
5016 * resume.
5017 **/
5018static int i40e_resume_port_tx(struct i40e_pf *pf)
5019{
5020 struct i40e_hw *hw = &pf->hw;
5021 int ret;
5022
5023 ret = i40e_aq_resume_port_tx(hw, NULL);
5024 if (ret) {
5025 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005026 "Resume Port Tx failed, err %s aq_err %s\n",
5027 i40e_stat_str(&pf->hw, ret),
5028 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005029 /* Schedule PF reset to recover */
5030 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5031 i40e_service_event_schedule(pf);
5032 }
5033
5034 return ret;
5035}
5036
5037/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005038 * i40e_init_pf_dcb - Initialize DCB configuration
5039 * @pf: PF being configured
5040 *
5041 * Query the current DCB configuration and cache it
5042 * in the hardware structure
5043 **/
5044static int i40e_init_pf_dcb(struct i40e_pf *pf)
5045{
5046 struct i40e_hw *hw = &pf->hw;
5047 int err = 0;
5048
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005049 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005050 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005051 goto out;
5052
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005053 /* Get the initial DCB configuration */
5054 err = i40e_init_dcb(hw);
5055 if (!err) {
5056 /* Device/Function is not DCBX capable */
5057 if ((!hw->func_caps.dcb) ||
5058 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5059 dev_info(&pf->pdev->dev,
5060 "DCBX offload is not supported or is disabled for this PF.\n");
5061
5062 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5063 goto out;
5064
5065 } else {
5066 /* When status is not DISABLED then DCBX in FW */
5067 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5068 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005069
5070 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005071 /* Enable DCB tagging only when more than one TC
5072 * or explicitly disable if only one TC
5073 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005074 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5075 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005076 else
5077 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005078 dev_dbg(&pf->pdev->dev,
5079 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005080 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005081 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005082 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005083 "Query for DCB configuration failed, err %s aq_err %s\n",
5084 i40e_stat_str(&pf->hw, err),
5085 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005086 }
5087
5088out:
5089 return err;
5090}
5091#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005092#define SPEED_SIZE 14
5093#define FC_SIZE 8
5094/**
5095 * i40e_print_link_message - print link up or down
5096 * @vsi: the VSI for which link needs a message
5097 */
Matt Jaredc156f852015-08-27 11:42:39 -04005098void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005099{
Shannon Nelsona9165492015-09-03 17:19:00 -04005100 char *speed = "Unknown";
5101 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005102
Matt Jaredc156f852015-08-27 11:42:39 -04005103 if (vsi->current_isup == isup)
5104 return;
5105 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005106 if (!isup) {
5107 netdev_info(vsi->netdev, "NIC Link is Down\n");
5108 return;
5109 }
5110
Greg Rose148c2d82014-12-11 07:06:27 +00005111 /* Warn user if link speed on NPAR enabled partition is not at
5112 * least 10GB
5113 */
5114 if (vsi->back->hw.func_caps.npar_enable &&
5115 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5116 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5117 netdev_warn(vsi->netdev,
5118 "The partition detected link speed that is less than 10Gbps\n");
5119
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005120 switch (vsi->back->hw.phy.link_info.link_speed) {
5121 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005122 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005123 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005124 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005125 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005126 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005127 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005128 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005129 break;
5130 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005131 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005132 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005133 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005134 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005135 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005136 default:
5137 break;
5138 }
5139
5140 switch (vsi->back->hw.fc.current_mode) {
5141 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005142 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005143 break;
5144 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005145 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005146 break;
5147 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005148 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005149 break;
5150 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005151 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005152 break;
5153 }
5154
Shannon Nelsona9165492015-09-03 17:19:00 -04005155 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005156 speed, fc);
5157}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005158
5159/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005160 * i40e_up_complete - Finish the last steps of bringing up a connection
5161 * @vsi: the VSI being configured
5162 **/
5163static int i40e_up_complete(struct i40e_vsi *vsi)
5164{
5165 struct i40e_pf *pf = vsi->back;
5166 int err;
5167
5168 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5169 i40e_vsi_configure_msix(vsi);
5170 else
5171 i40e_configure_msi_and_legacy(vsi);
5172
5173 /* start rings */
5174 err = i40e_vsi_control_rings(vsi, true);
5175 if (err)
5176 return err;
5177
5178 clear_bit(__I40E_DOWN, &vsi->state);
5179 i40e_napi_enable_all(vsi);
5180 i40e_vsi_enable_irq(vsi);
5181
5182 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5183 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005184 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005185 netif_tx_start_all_queues(vsi->netdev);
5186 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005187 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005188 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005189 /* need to check for qualified module here*/
5190 if ((pf->hw.phy.link_info.link_info &
5191 I40E_AQ_MEDIA_AVAILABLE) &&
5192 (!(pf->hw.phy.link_info.an_info &
5193 I40E_AQ_QUALIFIED_MODULE)))
5194 netdev_err(vsi->netdev,
5195 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005196 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005197
5198 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005199 if (vsi->type == I40E_VSI_FDIR) {
5200 /* reset fd counters */
5201 pf->fd_add_err = pf->fd_atr_cnt = 0;
5202 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005203 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005204 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5205 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005206 pf->fd_tcp_rule = 0;
5207 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005208 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005209 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005210
5211 /* On the next run of the service_task, notify any clients of the new
5212 * opened netdev
5213 */
5214 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005215 i40e_service_event_schedule(pf);
5216
5217 return 0;
5218}
5219
5220/**
5221 * i40e_vsi_reinit_locked - Reset the VSI
5222 * @vsi: the VSI being configured
5223 *
5224 * Rebuild the ring structs after some configuration
5225 * has changed, e.g. MTU size.
5226 **/
5227static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5228{
5229 struct i40e_pf *pf = vsi->back;
5230
5231 WARN_ON(in_interrupt());
5232 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5233 usleep_range(1000, 2000);
5234 i40e_down(vsi);
5235
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005236 i40e_up(vsi);
5237 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5238}
5239
5240/**
5241 * i40e_up - Bring the connection back up after being down
5242 * @vsi: the VSI being configured
5243 **/
5244int i40e_up(struct i40e_vsi *vsi)
5245{
5246 int err;
5247
5248 err = i40e_vsi_configure(vsi);
5249 if (!err)
5250 err = i40e_up_complete(vsi);
5251
5252 return err;
5253}
5254
5255/**
5256 * i40e_down - Shutdown the connection processing
5257 * @vsi: the VSI being stopped
5258 **/
5259void i40e_down(struct i40e_vsi *vsi)
5260{
5261 int i;
5262
5263 /* It is assumed that the caller of this function
5264 * sets the vsi->state __I40E_DOWN bit.
5265 */
5266 if (vsi->netdev) {
5267 netif_carrier_off(vsi->netdev);
5268 netif_tx_disable(vsi->netdev);
5269 }
5270 i40e_vsi_disable_irq(vsi);
5271 i40e_vsi_control_rings(vsi, false);
5272 i40e_napi_disable_all(vsi);
5273
5274 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005275 i40e_clean_tx_ring(vsi->tx_rings[i]);
5276 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005277 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005278
5279 i40e_notify_client_of_netdev_close(vsi, false);
5280
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005281}
5282
5283/**
5284 * i40e_setup_tc - configure multiple traffic classes
5285 * @netdev: net device to configure
5286 * @tc: number of traffic classes to enable
5287 **/
5288static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5289{
5290 struct i40e_netdev_priv *np = netdev_priv(netdev);
5291 struct i40e_vsi *vsi = np->vsi;
5292 struct i40e_pf *pf = vsi->back;
5293 u8 enabled_tc = 0;
5294 int ret = -EINVAL;
5295 int i;
5296
5297 /* Check if DCB enabled to continue */
5298 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5299 netdev_info(netdev, "DCB is not enabled for adapter\n");
5300 goto exit;
5301 }
5302
5303 /* Check if MFP enabled */
5304 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5305 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5306 goto exit;
5307 }
5308
5309 /* Check whether tc count is within enabled limit */
5310 if (tc > i40e_pf_get_num_tc(pf)) {
5311 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5312 goto exit;
5313 }
5314
5315 /* Generate TC map for number of tc requested */
5316 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005317 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005318
5319 /* Requesting same TC configuration as already enabled */
5320 if (enabled_tc == vsi->tc_config.enabled_tc)
5321 return 0;
5322
5323 /* Quiesce VSI queues */
5324 i40e_quiesce_vsi(vsi);
5325
5326 /* Configure VSI for enabled TCs */
5327 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5328 if (ret) {
5329 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5330 vsi->seid);
5331 goto exit;
5332 }
5333
5334 /* Unquiesce VSI */
5335 i40e_unquiesce_vsi(vsi);
5336
5337exit:
5338 return ret;
5339}
5340
John Fastabende4c67342016-02-16 21:16:15 -08005341#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005342int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5343 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005344#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005345static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5346 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005347#endif
5348{
John Fastabend16e5cc62016-02-16 21:16:43 -08005349 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005350 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005351 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005352}
5353
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005354/**
5355 * i40e_open - Called when a network interface is made active
5356 * @netdev: network interface device structure
5357 *
5358 * The open entry point is called when a network interface is made
5359 * active by the system (IFF_UP). At this point all resources needed
5360 * for transmit and receive operations are allocated, the interrupt
5361 * handler is registered with the OS, the netdev watchdog subtask is
5362 * enabled, and the stack is notified that the interface is ready.
5363 *
5364 * Returns 0 on success, negative value on failure
5365 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005366int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005367{
5368 struct i40e_netdev_priv *np = netdev_priv(netdev);
5369 struct i40e_vsi *vsi = np->vsi;
5370 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005371 int err;
5372
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005373 /* disallow open during test or if eeprom is broken */
5374 if (test_bit(__I40E_TESTING, &pf->state) ||
5375 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376 return -EBUSY;
5377
5378 netif_carrier_off(netdev);
5379
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005380 err = i40e_vsi_open(vsi);
5381 if (err)
5382 return err;
5383
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005384 /* configure global TSO hardware offload settings */
5385 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5386 TCP_FLAG_FIN) >> 16);
5387 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5388 TCP_FLAG_FIN |
5389 TCP_FLAG_CWR) >> 16);
5390 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5391
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005392 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005393
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005394 return 0;
5395}
5396
5397/**
5398 * i40e_vsi_open -
5399 * @vsi: the VSI to open
5400 *
5401 * Finish initialization of the VSI.
5402 *
5403 * Returns 0 on success, negative value on failure
5404 **/
5405int i40e_vsi_open(struct i40e_vsi *vsi)
5406{
5407 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005408 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005409 int err;
5410
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005411 /* allocate descriptors */
5412 err = i40e_vsi_setup_tx_resources(vsi);
5413 if (err)
5414 goto err_setup_tx;
5415 err = i40e_vsi_setup_rx_resources(vsi);
5416 if (err)
5417 goto err_setup_rx;
5418
5419 err = i40e_vsi_configure(vsi);
5420 if (err)
5421 goto err_setup_rx;
5422
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005423 if (vsi->netdev) {
5424 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5425 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5426 err = i40e_vsi_request_irq(vsi, int_name);
5427 if (err)
5428 goto err_setup_rx;
5429
5430 /* Notify the stack of the actual queue counts. */
5431 err = netif_set_real_num_tx_queues(vsi->netdev,
5432 vsi->num_queue_pairs);
5433 if (err)
5434 goto err_set_queues;
5435
5436 err = netif_set_real_num_rx_queues(vsi->netdev,
5437 vsi->num_queue_pairs);
5438 if (err)
5439 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005440
5441 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005442 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005443 dev_driver_string(&pf->pdev->dev),
5444 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005445 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005446
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005447 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005448 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005449 goto err_setup_rx;
5450 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005451
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005452 err = i40e_up_complete(vsi);
5453 if (err)
5454 goto err_up_complete;
5455
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456 return 0;
5457
5458err_up_complete:
5459 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005460err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005461 i40e_vsi_free_irq(vsi);
5462err_setup_rx:
5463 i40e_vsi_free_rx_resources(vsi);
5464err_setup_tx:
5465 i40e_vsi_free_tx_resources(vsi);
5466 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005467 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005468
5469 return err;
5470}
5471
5472/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005473 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005474 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005475 *
5476 * This function destroys the hlist where all the Flow Director
5477 * filters were saved.
5478 **/
5479static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5480{
5481 struct i40e_fdir_filter *filter;
5482 struct hlist_node *node2;
5483
5484 hlist_for_each_entry_safe(filter, node2,
5485 &pf->fdir_filter_list, fdir_node) {
5486 hlist_del(&filter->fdir_node);
5487 kfree(filter);
5488 }
5489 pf->fdir_pf_active_filters = 0;
5490}
5491
5492/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005493 * i40e_close - Disables a network interface
5494 * @netdev: network interface device structure
5495 *
5496 * The close entry point is called when an interface is de-activated
5497 * by the OS. The hardware is still under the driver's control, but
5498 * this netdev interface is disabled.
5499 *
5500 * Returns 0, this is not allowed to fail
5501 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005502int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503{
5504 struct i40e_netdev_priv *np = netdev_priv(netdev);
5505 struct i40e_vsi *vsi = np->vsi;
5506
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005507 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005508
5509 return 0;
5510}
5511
5512/**
5513 * i40e_do_reset - Start a PF or Core Reset sequence
5514 * @pf: board private structure
5515 * @reset_flags: which reset is requested
5516 *
5517 * The essential difference in resets is that the PF Reset
5518 * doesn't clear the packet buffers, doesn't reset the PE
5519 * firmware, and doesn't bother the other PFs on the chip.
5520 **/
5521void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5522{
5523 u32 val;
5524
5525 WARN_ON(in_interrupt());
5526
Mitch Williams263fc482014-04-23 04:50:11 +00005527
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005528 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005529 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005530
5531 /* Request a Global Reset
5532 *
5533 * This will start the chip's countdown to the actual full
5534 * chip reset event, and a warning interrupt to be sent
5535 * to all PFs, including the requestor. Our handler
5536 * for the warning interrupt will deal with the shutdown
5537 * and recovery of the switch setup.
5538 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005539 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005540 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5541 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5542 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5543
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005544 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005545
5546 /* Request a Core Reset
5547 *
5548 * Same as Global Reset, except does *not* include the MAC/PHY
5549 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005550 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005551 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5552 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5553 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5554 i40e_flush(&pf->hw);
5555
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005556 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557
5558 /* Request a PF Reset
5559 *
5560 * Resets only the PF-specific registers
5561 *
5562 * This goes directly to the tear-down and rebuild of
5563 * the switch, since we need to do all the recovery as
5564 * for the Core Reset.
5565 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005566 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005567 i40e_handle_reset_warning(pf);
5568
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005569 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005570 int v;
5571
5572 /* Find the VSI(s) that requested a re-init */
5573 dev_info(&pf->pdev->dev,
5574 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005575 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005576 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005577
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005578 if (vsi != NULL &&
5579 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5580 i40e_vsi_reinit_locked(pf->vsi[v]);
5581 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5582 }
5583 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005584 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005585 int v;
5586
5587 /* Find the VSI(s) that needs to be brought down */
5588 dev_info(&pf->pdev->dev, "VSI down requested\n");
5589 for (v = 0; v < pf->num_alloc_vsi; v++) {
5590 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005591
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005592 if (vsi != NULL &&
5593 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5594 set_bit(__I40E_DOWN, &vsi->state);
5595 i40e_down(vsi);
5596 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5597 }
5598 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005599 } else {
5600 dev_info(&pf->pdev->dev,
5601 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005602 }
5603}
5604
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005605#ifdef CONFIG_I40E_DCB
5606/**
5607 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5608 * @pf: board private structure
5609 * @old_cfg: current DCB config
5610 * @new_cfg: new DCB config
5611 **/
5612bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5613 struct i40e_dcbx_config *old_cfg,
5614 struct i40e_dcbx_config *new_cfg)
5615{
5616 bool need_reconfig = false;
5617
5618 /* Check if ETS configuration has changed */
5619 if (memcmp(&new_cfg->etscfg,
5620 &old_cfg->etscfg,
5621 sizeof(new_cfg->etscfg))) {
5622 /* If Priority Table has changed reconfig is needed */
5623 if (memcmp(&new_cfg->etscfg.prioritytable,
5624 &old_cfg->etscfg.prioritytable,
5625 sizeof(new_cfg->etscfg.prioritytable))) {
5626 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005627 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005628 }
5629
5630 if (memcmp(&new_cfg->etscfg.tcbwtable,
5631 &old_cfg->etscfg.tcbwtable,
5632 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005633 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005634
5635 if (memcmp(&new_cfg->etscfg.tsatable,
5636 &old_cfg->etscfg.tsatable,
5637 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005638 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005639 }
5640
5641 /* Check if PFC configuration has changed */
5642 if (memcmp(&new_cfg->pfc,
5643 &old_cfg->pfc,
5644 sizeof(new_cfg->pfc))) {
5645 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005646 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005647 }
5648
5649 /* Check if APP Table has changed */
5650 if (memcmp(&new_cfg->app,
5651 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005652 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005653 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005654 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005655 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005656
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005657 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005658 return need_reconfig;
5659}
5660
5661/**
5662 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5663 * @pf: board private structure
5664 * @e: event info posted on ARQ
5665 **/
5666static int i40e_handle_lldp_event(struct i40e_pf *pf,
5667 struct i40e_arq_event_info *e)
5668{
5669 struct i40e_aqc_lldp_get_mib *mib =
5670 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5671 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005672 struct i40e_dcbx_config tmp_dcbx_cfg;
5673 bool need_reconfig = false;
5674 int ret = 0;
5675 u8 type;
5676
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005677 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005678 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005679 return ret;
5680
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005681 /* Ignore if event is not for Nearest Bridge */
5682 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5683 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005684 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005685 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5686 return ret;
5687
5688 /* Check MIB Type and return if event for Remote MIB update */
5689 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005690 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005691 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005692 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5693 /* Update the remote cached instance and return */
5694 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5695 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5696 &hw->remote_dcbx_config);
5697 goto exit;
5698 }
5699
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005700 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005701 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005702
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005703 /* Reset the old DCBx configuration data */
5704 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005705 /* Get updated DCBX data from firmware */
5706 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005707 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005708 dev_info(&pf->pdev->dev,
5709 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5710 i40e_stat_str(&pf->hw, ret),
5711 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005712 goto exit;
5713 }
5714
5715 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005716 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5717 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005718 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005719 goto exit;
5720 }
5721
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005722 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5723 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005724
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005725 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005726
5727 if (!need_reconfig)
5728 goto exit;
5729
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005730 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005731 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005732 pf->flags |= I40E_FLAG_DCB_ENABLED;
5733 else
5734 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5735
Neerav Parikh69129dc2014-11-12 00:18:46 +00005736 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005737 /* Reconfiguration needed quiesce all VSIs */
5738 i40e_pf_quiesce_all_vsi(pf);
5739
5740 /* Changes in configuration update VEB/VSI */
5741 i40e_dcb_reconfigure(pf);
5742
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005743 ret = i40e_resume_port_tx(pf);
5744
Neerav Parikh69129dc2014-11-12 00:18:46 +00005745 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005746 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005747 if (ret)
5748 goto exit;
5749
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005750 /* Wait for the PF's queues to be disabled */
5751 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005752 if (ret) {
5753 /* Schedule PF reset to recover */
5754 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5755 i40e_service_event_schedule(pf);
5756 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005757 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005758 /* Notify the client for the DCB changes */
5759 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005760 }
5761
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005762exit:
5763 return ret;
5764}
5765#endif /* CONFIG_I40E_DCB */
5766
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005767/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005768 * i40e_do_reset_safe - Protected reset path for userland calls.
5769 * @pf: board private structure
5770 * @reset_flags: which reset is requested
5771 *
5772 **/
5773void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5774{
5775 rtnl_lock();
5776 i40e_do_reset(pf, reset_flags);
5777 rtnl_unlock();
5778}
5779
5780/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005781 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5782 * @pf: board private structure
5783 * @e: event info posted on ARQ
5784 *
5785 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5786 * and VF queues
5787 **/
5788static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5789 struct i40e_arq_event_info *e)
5790{
5791 struct i40e_aqc_lan_overflow *data =
5792 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5793 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5794 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5795 struct i40e_hw *hw = &pf->hw;
5796 struct i40e_vf *vf;
5797 u16 vf_id;
5798
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005799 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5800 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005801
5802 /* Queue belongs to VF, find the VF and issue VF reset */
5803 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5804 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5805 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5806 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5807 vf_id -= hw->func_caps.vf_base_id;
5808 vf = &pf->vf[vf_id];
5809 i40e_vc_notify_vf_reset(vf);
5810 /* Allow VF to process pending reset notification */
5811 msleep(20);
5812 i40e_reset_vf(vf, false);
5813 }
5814}
5815
5816/**
5817 * i40e_service_event_complete - Finish up the service event
5818 * @pf: board private structure
5819 **/
5820static void i40e_service_event_complete(struct i40e_pf *pf)
5821{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005822 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005823
5824 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005825 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005826 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5827}
5828
5829/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005830 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5831 * @pf: board private structure
5832 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005833u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005834{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005835 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005836
5837 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5838 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5839 return fcnt_prog;
5840}
5841
5842/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005843 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005844 * @pf: board private structure
5845 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005846u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005847{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005848 u32 val, fcnt_prog;
5849
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005850 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5851 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5852 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5853 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5854 return fcnt_prog;
5855}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005856
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005857/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005858 * i40e_get_global_fd_count - Get total FD filters programmed on device
5859 * @pf: board private structure
5860 **/
5861u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5862{
5863 u32 val, fcnt_prog;
5864
5865 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5866 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5867 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5868 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5869 return fcnt_prog;
5870}
5871
5872/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005873 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5874 * @pf: board private structure
5875 **/
5876void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5877{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005878 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005879 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005880 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005881
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005882 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5883 return;
5884
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005885 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5886 * to re-enable
5887 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005888 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005889 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005890 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5891 (pf->fd_add_err == 0) ||
5892 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005893 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5894 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5895 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005896 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5897 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 +00005898 }
5899 }
Jacob Kellera3417d22016-09-06 18:05:10 -07005900
5901 /* Wait for some more space to be available to turn on ATR. We also
5902 * must check that no existing ntuple rules for TCP are in effect
5903 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005904 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5905 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07005906 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
5907 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005908 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005909 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07005910 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 +00005911 }
5912 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005913
5914 /* if hw had a problem adding a filter, delete it */
5915 if (pf->fd_inv > 0) {
5916 hlist_for_each_entry_safe(filter, node,
5917 &pf->fdir_filter_list, fdir_node) {
5918 if (filter->fd_id == pf->fd_inv) {
5919 hlist_del(&filter->fdir_node);
5920 kfree(filter);
5921 pf->fdir_pf_active_filters--;
5922 }
5923 }
5924 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005925}
5926
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005927#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005928#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005929/**
5930 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
5931 * @pf: board private structure
5932 **/
5933static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
5934{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005935 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005936 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005937 bool disable_atr = false;
5938 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005939 int reg;
5940
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005941 if (!time_after(jiffies, pf->fd_flush_timestamp +
5942 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
5943 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005944
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005945 /* If the flush is happening too quick and we have mostly SB rules we
5946 * should not re-enable ATR for some time.
5947 */
5948 min_flush_time = pf->fd_flush_timestamp +
5949 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
5950 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005951
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005952 if (!(time_after(jiffies, min_flush_time)) &&
5953 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
5954 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5955 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
5956 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005957 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005958
5959 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07005960 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005961 /* flush all filters */
5962 wr32(&pf->hw, I40E_PFQF_CTL_1,
5963 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
5964 i40e_flush(&pf->hw);
5965 pf->fd_flush_cnt++;
5966 pf->fd_add_err = 0;
5967 do {
5968 /* Check FD flush status every 5-6msec */
5969 usleep_range(5000, 6000);
5970 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
5971 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
5972 break;
5973 } while (flush_wait_retry--);
5974 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
5975 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
5976 } else {
5977 /* replay sideband filters */
5978 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
5979 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07005980 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04005981 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
5982 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5983 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
5984 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005985}
5986
5987/**
5988 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
5989 * @pf: board private structure
5990 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005991u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005992{
5993 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
5994}
5995
5996/* We can see up to 256 filter programming desc in transit if the filters are
5997 * being applied really fast; before we see the first
5998 * filter miss error on Rx queue 0. Accumulating enough error messages before
5999 * reacting will make sure we don't cause flush too often.
6000 */
6001#define I40E_MAX_FD_PROGRAM_ERROR 256
6002
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006003/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006004 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6005 * @pf: board private structure
6006 **/
6007static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6008{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006009
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006010 /* if interface is down do nothing */
6011 if (test_bit(__I40E_DOWN, &pf->state))
6012 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006013
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006014 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006015 i40e_fdir_flush_and_replay(pf);
6016
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006017 i40e_fdir_check_and_reenable(pf);
6018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006019}
6020
6021/**
6022 * i40e_vsi_link_event - notify VSI of a link event
6023 * @vsi: vsi to be notified
6024 * @link_up: link up or down
6025 **/
6026static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6027{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006028 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006029 return;
6030
6031 switch (vsi->type) {
6032 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006033#ifdef I40E_FCOE
6034 case I40E_VSI_FCOE:
6035#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006036 if (!vsi->netdev || !vsi->netdev_registered)
6037 break;
6038
6039 if (link_up) {
6040 netif_carrier_on(vsi->netdev);
6041 netif_tx_wake_all_queues(vsi->netdev);
6042 } else {
6043 netif_carrier_off(vsi->netdev);
6044 netif_tx_stop_all_queues(vsi->netdev);
6045 }
6046 break;
6047
6048 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006049 case I40E_VSI_VMDQ2:
6050 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006051 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006052 case I40E_VSI_MIRROR:
6053 default:
6054 /* there is no notification for other VSIs */
6055 break;
6056 }
6057}
6058
6059/**
6060 * i40e_veb_link_event - notify elements on the veb of a link event
6061 * @veb: veb to be notified
6062 * @link_up: link up or down
6063 **/
6064static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6065{
6066 struct i40e_pf *pf;
6067 int i;
6068
6069 if (!veb || !veb->pf)
6070 return;
6071 pf = veb->pf;
6072
6073 /* depth first... */
6074 for (i = 0; i < I40E_MAX_VEB; i++)
6075 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6076 i40e_veb_link_event(pf->veb[i], link_up);
6077
6078 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006079 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006080 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6081 i40e_vsi_link_event(pf->vsi[i], link_up);
6082}
6083
6084/**
6085 * i40e_link_event - Update netif_carrier status
6086 * @pf: board private structure
6087 **/
6088static void i40e_link_event(struct i40e_pf *pf)
6089{
Mitch Williams320684c2014-10-17 03:14:43 +00006090 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006091 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006092 i40e_status status;
6093 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006094
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006095 /* save off old link status information */
6096 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6097
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006098 /* set this to force the get_link_status call to refresh state */
6099 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006100
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006101 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006102
6103 status = i40e_get_link_status(&pf->hw, &new_link);
6104 if (status) {
6105 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6106 status);
6107 return;
6108 }
6109
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006110 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6111 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006112
6113 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006114 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006115 (test_bit(__I40E_DOWN, &vsi->state) ||
6116 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006117 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006118
6119 if (!test_bit(__I40E_DOWN, &vsi->state))
6120 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006121
6122 /* Notify the base of the switch tree connected to
6123 * the link. Floating VEBs are not notified.
6124 */
6125 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6126 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6127 else
Mitch Williams320684c2014-10-17 03:14:43 +00006128 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006129
6130 if (pf->vf)
6131 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006132
6133 if (pf->flags & I40E_FLAG_PTP)
6134 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006135}
6136
6137/**
Shannon Nelson21536712014-10-25 10:35:25 +00006138 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006139 * @pf: board private structure
6140 **/
6141static void i40e_watchdog_subtask(struct i40e_pf *pf)
6142{
6143 int i;
6144
6145 /* if interface is down do nothing */
6146 if (test_bit(__I40E_DOWN, &pf->state) ||
6147 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6148 return;
6149
Shannon Nelson21536712014-10-25 10:35:25 +00006150 /* make sure we don't do these things too often */
6151 if (time_before(jiffies, (pf->service_timer_previous +
6152 pf->service_timer_period)))
6153 return;
6154 pf->service_timer_previous = jiffies;
6155
Shannon Nelson9ac77262015-08-27 11:42:40 -04006156 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6157 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006158
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006159 /* Update the stats for active netdevs so the network stack
6160 * can look at updated numbers whenever it cares to
6161 */
Mitch Williams505682c2014-05-20 08:01:37 +00006162 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006163 if (pf->vsi[i] && pf->vsi[i]->netdev)
6164 i40e_update_stats(pf->vsi[i]);
6165
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006166 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6167 /* Update the stats for the active switching components */
6168 for (i = 0; i < I40E_MAX_VEB; i++)
6169 if (pf->veb[i])
6170 i40e_update_veb_stats(pf->veb[i]);
6171 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006172
6173 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006174}
6175
6176/**
6177 * i40e_reset_subtask - Set up for resetting the device and driver
6178 * @pf: board private structure
6179 **/
6180static void i40e_reset_subtask(struct i40e_pf *pf)
6181{
6182 u32 reset_flags = 0;
6183
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006184 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006185 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006186 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006187 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6188 }
6189 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006190 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006191 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6192 }
6193 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006194 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006195 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6196 }
6197 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006198 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006199 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6200 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006201 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006202 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006203 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6204 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006205
6206 /* If there's a recovery already waiting, it takes
6207 * precedence before starting a new reset sequence.
6208 */
6209 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6210 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006211 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006212 }
6213
6214 /* If we're already down or resetting, just bail */
6215 if (reset_flags &&
6216 !test_bit(__I40E_DOWN, &pf->state) &&
6217 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6218 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006219
6220unlock:
6221 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006222}
6223
6224/**
6225 * i40e_handle_link_event - Handle link event
6226 * @pf: board private structure
6227 * @e: event info posted on ARQ
6228 **/
6229static void i40e_handle_link_event(struct i40e_pf *pf,
6230 struct i40e_arq_event_info *e)
6231{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006232 struct i40e_aqc_get_link_status *status =
6233 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006234
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006235 /* Do a new status request to re-enable LSE reporting
6236 * and load new status information into the hw struct
6237 * This completely ignores any state information
6238 * in the ARQ event info, instead choosing to always
6239 * issue the AQ update link status command.
6240 */
6241 i40e_link_event(pf);
6242
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006243 /* check for unqualified module, if link is down */
6244 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6245 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6246 (!(status->link_info & I40E_AQ_LINK_UP)))
6247 dev_err(&pf->pdev->dev,
6248 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006249}
6250
6251/**
6252 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6253 * @pf: board private structure
6254 **/
6255static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6256{
6257 struct i40e_arq_event_info event;
6258 struct i40e_hw *hw = &pf->hw;
6259 u16 pending, i = 0;
6260 i40e_status ret;
6261 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006262 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006263 u32 val;
6264
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006265 /* Do not run clean AQ when PF reset fails */
6266 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6267 return;
6268
Shannon Nelson86df2422014-05-20 08:01:35 +00006269 /* check for error indications */
6270 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6271 oldval = val;
6272 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006273 if (hw->debug_mask & I40E_DEBUG_AQ)
6274 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006275 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6276 }
6277 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006278 if (hw->debug_mask & I40E_DEBUG_AQ)
6279 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006280 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006281 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006282 }
6283 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006284 if (hw->debug_mask & I40E_DEBUG_AQ)
6285 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006286 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6287 }
6288 if (oldval != val)
6289 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6290
6291 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6292 oldval = val;
6293 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006294 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6295 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006296 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6297 }
6298 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006299 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6300 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006301 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6302 }
6303 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006304 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6305 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006306 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6307 }
6308 if (oldval != val)
6309 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6310
Mitch Williams1001dc32014-11-11 20:02:19 +00006311 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6312 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006313 if (!event.msg_buf)
6314 return;
6315
6316 do {
6317 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006318 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006319 break;
Mitch Williams56497972014-06-04 08:45:18 +00006320 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006321 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6322 break;
6323 }
6324
6325 opcode = le16_to_cpu(event.desc.opcode);
6326 switch (opcode) {
6327
6328 case i40e_aqc_opc_get_link_status:
6329 i40e_handle_link_event(pf, &event);
6330 break;
6331 case i40e_aqc_opc_send_msg_to_pf:
6332 ret = i40e_vc_process_vf_msg(pf,
6333 le16_to_cpu(event.desc.retval),
6334 le32_to_cpu(event.desc.cookie_high),
6335 le32_to_cpu(event.desc.cookie_low),
6336 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006337 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006338 break;
6339 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006340 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006341#ifdef CONFIG_I40E_DCB
6342 rtnl_lock();
6343 ret = i40e_handle_lldp_event(pf, &event);
6344 rtnl_unlock();
6345#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006346 break;
6347 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006348 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006349 i40e_handle_lan_overflow_event(pf, &event);
6350 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006351 case i40e_aqc_opc_send_msg_to_peer:
6352 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6353 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006354 case i40e_aqc_opc_nvm_erase:
6355 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006356 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006357 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6358 "ARQ NVM operation 0x%04x completed\n",
6359 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006360 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006361 default:
6362 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006363 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006364 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006365 break;
6366 }
6367 } while (pending && (i++ < pf->adminq_work_limit));
6368
6369 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6370 /* re-enable Admin queue interrupt cause */
6371 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6372 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6373 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6374 i40e_flush(hw);
6375
6376 kfree(event.msg_buf);
6377}
6378
6379/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006380 * i40e_verify_eeprom - make sure eeprom is good to use
6381 * @pf: board private structure
6382 **/
6383static void i40e_verify_eeprom(struct i40e_pf *pf)
6384{
6385 int err;
6386
6387 err = i40e_diag_eeprom_test(&pf->hw);
6388 if (err) {
6389 /* retry in case of garbage read */
6390 err = i40e_diag_eeprom_test(&pf->hw);
6391 if (err) {
6392 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6393 err);
6394 set_bit(__I40E_BAD_EEPROM, &pf->state);
6395 }
6396 }
6397
6398 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6399 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6400 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6401 }
6402}
6403
6404/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006405 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006406 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006407 *
6408 * enable switch loop back or die - no point in a return value
6409 **/
6410static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6411{
6412 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6413 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006414 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006415
6416 ctxt.seid = pf->main_vsi_seid;
6417 ctxt.pf_num = pf->hw.pf_id;
6418 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006419 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6420 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006421 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006422 "couldn't get PF vsi config, err %s aq_err %s\n",
6423 i40e_stat_str(&pf->hw, ret),
6424 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006425 return;
6426 }
6427 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6428 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6429 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6430
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006431 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6432 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006433 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006434 "update vsi switch failed, err %s aq_err %s\n",
6435 i40e_stat_str(&pf->hw, ret),
6436 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006437 }
6438}
6439
6440/**
6441 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006442 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006443 *
6444 * disable switch loop back or die - no point in a return value
6445 **/
6446static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6447{
6448 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6449 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006450 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006451
6452 ctxt.seid = pf->main_vsi_seid;
6453 ctxt.pf_num = pf->hw.pf_id;
6454 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006455 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6456 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006457 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006458 "couldn't get PF vsi config, err %s aq_err %s\n",
6459 i40e_stat_str(&pf->hw, ret),
6460 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006461 return;
6462 }
6463 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6464 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6465 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6466
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006467 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6468 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006469 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006470 "update vsi switch failed, err %s aq_err %s\n",
6471 i40e_stat_str(&pf->hw, ret),
6472 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006473 }
6474}
6475
6476/**
Neerav Parikh51616012015-02-06 08:52:14 +00006477 * i40e_config_bridge_mode - Configure the HW bridge mode
6478 * @veb: pointer to the bridge instance
6479 *
6480 * Configure the loop back mode for the LAN VSI that is downlink to the
6481 * specified HW bridge instance. It is expected this function is called
6482 * when a new HW bridge is instantiated.
6483 **/
6484static void i40e_config_bridge_mode(struct i40e_veb *veb)
6485{
6486 struct i40e_pf *pf = veb->pf;
6487
Shannon Nelson6dec1012015-09-28 14:12:30 -04006488 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6489 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6490 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006491 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6492 i40e_disable_pf_switch_lb(pf);
6493 else
6494 i40e_enable_pf_switch_lb(pf);
6495}
6496
6497/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006498 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6499 * @veb: pointer to the VEB instance
6500 *
6501 * This is a recursive function that first builds the attached VSIs then
6502 * recurses in to build the next layer of VEB. We track the connections
6503 * through our own index numbers because the seid's from the HW could
6504 * change across the reset.
6505 **/
6506static int i40e_reconstitute_veb(struct i40e_veb *veb)
6507{
6508 struct i40e_vsi *ctl_vsi = NULL;
6509 struct i40e_pf *pf = veb->pf;
6510 int v, veb_idx;
6511 int ret;
6512
6513 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006514 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006515 if (pf->vsi[v] &&
6516 pf->vsi[v]->veb_idx == veb->idx &&
6517 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6518 ctl_vsi = pf->vsi[v];
6519 break;
6520 }
6521 }
6522 if (!ctl_vsi) {
6523 dev_info(&pf->pdev->dev,
6524 "missing owner VSI for veb_idx %d\n", veb->idx);
6525 ret = -ENOENT;
6526 goto end_reconstitute;
6527 }
6528 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6529 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6530 ret = i40e_add_vsi(ctl_vsi);
6531 if (ret) {
6532 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006533 "rebuild of veb_idx %d owner VSI failed: %d\n",
6534 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006535 goto end_reconstitute;
6536 }
6537 i40e_vsi_reset_stats(ctl_vsi);
6538
6539 /* create the VEB in the switch and move the VSI onto the VEB */
6540 ret = i40e_add_veb(veb, ctl_vsi);
6541 if (ret)
6542 goto end_reconstitute;
6543
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006544 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6545 veb->bridge_mode = BRIDGE_MODE_VEB;
6546 else
6547 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006548 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006549
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006550 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006551 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006552 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6553 continue;
6554
6555 if (pf->vsi[v]->veb_idx == veb->idx) {
6556 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006557
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006558 vsi->uplink_seid = veb->seid;
6559 ret = i40e_add_vsi(vsi);
6560 if (ret) {
6561 dev_info(&pf->pdev->dev,
6562 "rebuild of vsi_idx %d failed: %d\n",
6563 v, ret);
6564 goto end_reconstitute;
6565 }
6566 i40e_vsi_reset_stats(vsi);
6567 }
6568 }
6569
6570 /* create any VEBs attached to this VEB - RECURSION */
6571 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6572 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6573 pf->veb[veb_idx]->uplink_seid = veb->seid;
6574 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6575 if (ret)
6576 break;
6577 }
6578 }
6579
6580end_reconstitute:
6581 return ret;
6582}
6583
6584/**
6585 * i40e_get_capabilities - get info about the HW
6586 * @pf: the PF struct
6587 **/
6588static int i40e_get_capabilities(struct i40e_pf *pf)
6589{
6590 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6591 u16 data_size;
6592 int buf_len;
6593 int err;
6594
6595 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6596 do {
6597 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6598 if (!cap_buf)
6599 return -ENOMEM;
6600
6601 /* this loads the data into the hw struct for us */
6602 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6603 &data_size,
6604 i40e_aqc_opc_list_func_capabilities,
6605 NULL);
6606 /* data loaded, buffer no longer needed */
6607 kfree(cap_buf);
6608
6609 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6610 /* retry with a larger buffer */
6611 buf_len = data_size;
6612 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6613 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006614 "capability discovery failed, err %s aq_err %s\n",
6615 i40e_stat_str(&pf->hw, err),
6616 i40e_aq_str(&pf->hw,
6617 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006618 return -ENODEV;
6619 }
6620 } while (err);
6621
6622 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6623 dev_info(&pf->pdev->dev,
6624 "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",
6625 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6626 pf->hw.func_caps.num_msix_vectors,
6627 pf->hw.func_caps.num_msix_vectors_vf,
6628 pf->hw.func_caps.fd_filters_guaranteed,
6629 pf->hw.func_caps.fd_filters_best_effort,
6630 pf->hw.func_caps.num_tx_qp,
6631 pf->hw.func_caps.num_vsis);
6632
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006633#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6634 + pf->hw.func_caps.num_vfs)
6635 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6636 dev_info(&pf->pdev->dev,
6637 "got num_vsis %d, setting num_vsis to %d\n",
6638 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6639 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6640 }
6641
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006642 return 0;
6643}
6644
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006645static int i40e_vsi_clear(struct i40e_vsi *vsi);
6646
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006647/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006648 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006649 * @pf: board private structure
6650 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006651static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006652{
6653 struct i40e_vsi *vsi;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006654 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006655
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006656 /* quick workaround for an NVM issue that leaves a critical register
6657 * uninitialized
6658 */
6659 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6660 static const u32 hkey[] = {
6661 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6662 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6663 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6664 0x95b3a76d};
6665
6666 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6667 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6668 }
6669
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006670 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006671 return;
6672
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006673 /* find existing VSI and see if it needs configuring */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006674 vsi = NULL;
Mitch Williams505682c2014-05-20 08:01:37 +00006675 for (i = 0; i < pf->num_alloc_vsi; i++) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006676 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006677 vsi = pf->vsi[i];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006678 break;
6679 }
6680 }
6681
6682 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006683 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006684 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6685 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006686 if (!vsi) {
6687 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006688 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6689 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006691 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006692
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006693 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006694}
6695
6696/**
6697 * i40e_fdir_teardown - release the Flow Director resources
6698 * @pf: board private structure
6699 **/
6700static void i40e_fdir_teardown(struct i40e_pf *pf)
6701{
6702 int i;
6703
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006704 i40e_fdir_filter_exit(pf);
Mitch Williams505682c2014-05-20 08:01:37 +00006705 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006706 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
6707 i40e_vsi_release(pf->vsi[i]);
6708 break;
6709 }
6710 }
6711}
6712
6713/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006714 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006715 * @pf: board private structure
6716 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006717 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006718 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006719static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006720{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006721 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006722 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006723 u32 v;
6724
6725 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6726 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006727 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006728 if (i40e_check_asq_alive(&pf->hw))
6729 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006730
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006731 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006732
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006733 /* quiesce the VSIs and their queues that are not already DOWN */
6734 i40e_pf_quiesce_all_vsi(pf);
6735
Mitch Williams505682c2014-05-20 08:01:37 +00006736 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006737 if (pf->vsi[v])
6738 pf->vsi[v]->seid = 0;
6739 }
6740
6741 i40e_shutdown_adminq(&pf->hw);
6742
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006743 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006744 if (hw->hmc.hmc_obj) {
6745 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006746 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006747 dev_warn(&pf->pdev->dev,
6748 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006749 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006750}
6751
6752/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006753 * i40e_send_version - update firmware with driver version
6754 * @pf: PF struct
6755 */
6756static void i40e_send_version(struct i40e_pf *pf)
6757{
6758 struct i40e_driver_version dv;
6759
6760 dv.major_version = DRV_VERSION_MAJOR;
6761 dv.minor_version = DRV_VERSION_MINOR;
6762 dv.build_version = DRV_VERSION_BUILD;
6763 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006764 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006765 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6766}
6767
6768/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006769 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006770 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006771 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006772 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006773static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006774{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006775 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006776 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006777 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006778 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006779 u32 v;
6780
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781 /* Now we wait for GRST to settle out.
6782 * We don't have to delete the VEBs or VSIs from the hw switch
6783 * because the reset will make them disappear.
6784 */
6785 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006786 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006787 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006788 set_bit(__I40E_RESET_FAILED, &pf->state);
6789 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006790 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006791 pf->pfr_count++;
6792
6793 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006794 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006795 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006796
6797 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6798 ret = i40e_init_adminq(&pf->hw);
6799 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006800 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6801 i40e_stat_str(&pf->hw, ret),
6802 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006803 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804 }
6805
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006806 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006807 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006808 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006809
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006810 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006811 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006812 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006813 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006814
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006815 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6816 hw->func_caps.num_rx_qp,
6817 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6818 if (ret) {
6819 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6820 goto end_core_reset;
6821 }
6822 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6823 if (ret) {
6824 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6825 goto end_core_reset;
6826 }
6827
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006828#ifdef CONFIG_I40E_DCB
6829 ret = i40e_init_pf_dcb(pf);
6830 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006831 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6832 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6833 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006834 }
6835#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006836#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006837 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006838
Vasu Dev38e00432014-08-01 13:27:03 -07006839#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006840 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006841 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006842 if (ret)
6843 goto end_core_reset;
6844
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006845 /* The driver only wants link up/down and module qualification
6846 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006847 */
6848 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006849 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006850 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006851 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006852 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006853 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6854 i40e_stat_str(&pf->hw, ret),
6855 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006856
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006857 /* make sure our flow control settings are restored */
6858 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6859 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006860 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6861 i40e_stat_str(&pf->hw, ret),
6862 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006863
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006864 /* Rebuild the VSIs and VEBs that existed before reset.
6865 * They are still in our local switch element arrays, so only
6866 * need to rebuild the switch model in the HW.
6867 *
6868 * If there were VEBs but the reconstitution failed, we'll try
6869 * try to recover minimal use by getting the basic PF VSI working.
6870 */
6871 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006872 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006873 /* find the one VEB connected to the MAC, and find orphans */
6874 for (v = 0; v < I40E_MAX_VEB; v++) {
6875 if (!pf->veb[v])
6876 continue;
6877
6878 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6879 pf->veb[v]->uplink_seid == 0) {
6880 ret = i40e_reconstitute_veb(pf->veb[v]);
6881
6882 if (!ret)
6883 continue;
6884
6885 /* If Main VEB failed, we're in deep doodoo,
6886 * so give up rebuilding the switch and set up
6887 * for minimal rebuild of PF VSI.
6888 * If orphan failed, we'll report the error
6889 * but try to keep going.
6890 */
6891 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6892 dev_info(&pf->pdev->dev,
6893 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6894 ret);
6895 pf->vsi[pf->lan_vsi]->uplink_seid
6896 = pf->mac_seid;
6897 break;
6898 } else if (pf->veb[v]->uplink_seid == 0) {
6899 dev_info(&pf->pdev->dev,
6900 "rebuild of orphan VEB failed: %d\n",
6901 ret);
6902 }
6903 }
6904 }
6905 }
6906
6907 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006908 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006909 /* no VEB, so rebuild only the Main VSI */
6910 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6911 if (ret) {
6912 dev_info(&pf->pdev->dev,
6913 "rebuild of Main VSI failed: %d\n", ret);
6914 goto end_core_reset;
6915 }
6916 }
6917
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006918 /* Reconfigure hardware for allowing smaller MSS in the case
6919 * of TSO, so that we avoid the MDD being fired and causing
6920 * a reset in the case of small MSS+TSO.
6921 */
6922#define I40E_REG_MSS 0x000E64DC
6923#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6924#define I40E_64BYTE_MSS 0x400000
6925 val = rd32(hw, I40E_REG_MSS);
6926 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6927 val &= ~I40E_REG_MSS_MIN_MASK;
6928 val |= I40E_64BYTE_MSS;
6929 wr32(hw, I40E_REG_MSS, val);
6930 }
6931
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006932 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006933 msleep(75);
6934 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6935 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006936 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6937 i40e_stat_str(&pf->hw, ret),
6938 i40e_aq_str(&pf->hw,
6939 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006940 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006941 /* reinit the misc interrupt */
6942 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6943 ret = i40e_setup_misc_vector(pf);
6944
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04006945 /* Add a filter to drop all Flow control frames from any VSI from being
6946 * transmitted. By doing so we stop a malicious VF from sending out
6947 * PAUSE or PFC frames and potentially controlling traffic for other
6948 * PF/VF VSIs.
6949 * The FW can still send Flow control frames if enabled.
6950 */
6951 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
6952 pf->main_vsi_seid);
6953
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006954 /* restart the VSIs that were rebuilt and running before the reset */
6955 i40e_pf_unquiesce_all_vsi(pf);
6956
Mitch Williams69f64b22014-02-13 03:48:46 -08006957 if (pf->num_alloc_vfs) {
6958 for (v = 0; v < pf->num_alloc_vfs; v++)
6959 i40e_reset_vf(&pf->vf[v], true);
6960 }
6961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006962 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006963 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964
6965end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006966 clear_bit(__I40E_RESET_FAILED, &pf->state);
6967clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006968 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6969}
6970
6971/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006972 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006973 * @pf: board private structure
6974 *
6975 * Close up the VFs and other things in prep for a Core Reset,
6976 * then get ready to rebuild the world.
6977 **/
6978static void i40e_handle_reset_warning(struct i40e_pf *pf)
6979{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006980 i40e_prep_for_reset(pf);
6981 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006982}
6983
6984/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006985 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006986 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006987 *
6988 * Called from the MDD irq handler to identify possibly malicious vfs
6989 **/
6990static void i40e_handle_mdd_event(struct i40e_pf *pf)
6991{
6992 struct i40e_hw *hw = &pf->hw;
6993 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00006994 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006995 struct i40e_vf *vf;
6996 u32 reg;
6997 int i;
6998
6999 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7000 return;
7001
7002 /* find what triggered the MDD event */
7003 reg = rd32(hw, I40E_GL_MDET_TX);
7004 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007005 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7006 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007007 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007008 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007009 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007010 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007011 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7012 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7013 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007014 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007015 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 +00007016 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007017 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7018 mdd_detected = true;
7019 }
7020 reg = rd32(hw, I40E_GL_MDET_RX);
7021 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007022 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7023 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007024 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007025 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007026 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7027 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7028 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007029 if (netif_msg_rx_err(pf))
7030 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7031 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007032 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7033 mdd_detected = true;
7034 }
7035
Neerav Parikhdf430b12014-06-04 01:23:15 +00007036 if (mdd_detected) {
7037 reg = rd32(hw, I40E_PF_MDET_TX);
7038 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7039 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007040 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007041 pf_mdd_detected = true;
7042 }
7043 reg = rd32(hw, I40E_PF_MDET_RX);
7044 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7045 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007046 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007047 pf_mdd_detected = true;
7048 }
7049 /* Queue belongs to the PF, initiate a reset */
7050 if (pf_mdd_detected) {
7051 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7052 i40e_service_event_schedule(pf);
7053 }
7054 }
7055
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056 /* see if one of the VFs needs its hand slapped */
7057 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7058 vf = &(pf->vf[i]);
7059 reg = rd32(hw, I40E_VP_MDET_TX(i));
7060 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7061 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7062 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007063 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7064 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007065 }
7066
7067 reg = rd32(hw, I40E_VP_MDET_RX(i));
7068 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7069 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7070 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007071 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7072 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007073 }
7074
7075 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7076 dev_info(&pf->pdev->dev,
7077 "Too many MDD events on VF %d, disabled\n", i);
7078 dev_info(&pf->pdev->dev,
7079 "Use PF Control I/F to re-enable the VF\n");
7080 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7081 }
7082 }
7083
7084 /* re-enable mdd interrupt cause */
7085 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7086 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7087 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7088 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7089 i40e_flush(hw);
7090}
7091
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007092/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007093 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007094 * @pf: board private structure
7095 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007096static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007097{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007098 struct i40e_hw *hw = &pf->hw;
7099 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007100 __be16 port;
7101 int i;
7102
Singhai, Anjali6a899022015-12-14 12:21:18 -08007103 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007104 return;
7105
Singhai, Anjali6a899022015-12-14 12:21:18 -08007106 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007107
7108 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007109 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7110 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7111 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007112 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007113 ret = i40e_aq_add_udp_tunnel(hw, port,
7114 pf->udp_ports[i].type,
7115 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007116 else
7117 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007118
7119 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007120 dev_dbg(&pf->pdev->dev,
7121 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7122 pf->udp_ports[i].type ? "vxlan" : "geneve",
7123 port ? "add" : "delete",
7124 ntohs(port), i,
7125 i40e_stat_str(&pf->hw, ret),
7126 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007127 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007128 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007129 }
7130 }
7131 }
7132}
7133
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007134/**
7135 * i40e_service_task - Run the driver's async subtasks
7136 * @work: pointer to work_struct containing our data
7137 **/
7138static void i40e_service_task(struct work_struct *work)
7139{
7140 struct i40e_pf *pf = container_of(work,
7141 struct i40e_pf,
7142 service_task);
7143 unsigned long start_time = jiffies;
7144
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007145 /* don't bother with service tasks if a reset is in progress */
7146 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7147 i40e_service_event_complete(pf);
7148 return;
7149 }
7150
Kiran Patilb03a8c12015-09-24 18:13:15 -04007151 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007152 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007153 i40e_reset_subtask(pf);
7154 i40e_handle_mdd_event(pf);
7155 i40e_vc_process_vflr_event(pf);
7156 i40e_watchdog_subtask(pf);
7157 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007158 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007159 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007160 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007161 i40e_clean_adminq_subtask(pf);
7162
7163 i40e_service_event_complete(pf);
7164
7165 /* If the tasks have taken longer than one timer cycle or there
7166 * is more work to be done, reschedule the service task now
7167 * rather than wait for the timer to tick again.
7168 */
7169 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7170 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7171 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7172 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7173 i40e_service_event_schedule(pf);
7174}
7175
7176/**
7177 * i40e_service_timer - timer callback
7178 * @data: pointer to PF struct
7179 **/
7180static void i40e_service_timer(unsigned long data)
7181{
7182 struct i40e_pf *pf = (struct i40e_pf *)data;
7183
7184 mod_timer(&pf->service_timer,
7185 round_jiffies(jiffies + pf->service_timer_period));
7186 i40e_service_event_schedule(pf);
7187}
7188
7189/**
7190 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7191 * @vsi: the VSI being configured
7192 **/
7193static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7194{
7195 struct i40e_pf *pf = vsi->back;
7196
7197 switch (vsi->type) {
7198 case I40E_VSI_MAIN:
7199 vsi->alloc_queue_pairs = pf->num_lan_qps;
7200 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7201 I40E_REQ_DESCRIPTOR_MULTIPLE);
7202 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7203 vsi->num_q_vectors = pf->num_lan_msix;
7204 else
7205 vsi->num_q_vectors = 1;
7206
7207 break;
7208
7209 case I40E_VSI_FDIR:
7210 vsi->alloc_queue_pairs = 1;
7211 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7212 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007213 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214 break;
7215
7216 case I40E_VSI_VMDQ2:
7217 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7218 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7219 I40E_REQ_DESCRIPTOR_MULTIPLE);
7220 vsi->num_q_vectors = pf->num_vmdq_msix;
7221 break;
7222
7223 case I40E_VSI_SRIOV:
7224 vsi->alloc_queue_pairs = pf->num_vf_qps;
7225 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7226 I40E_REQ_DESCRIPTOR_MULTIPLE);
7227 break;
7228
Vasu Dev38e00432014-08-01 13:27:03 -07007229#ifdef I40E_FCOE
7230 case I40E_VSI_FCOE:
7231 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7232 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7233 I40E_REQ_DESCRIPTOR_MULTIPLE);
7234 vsi->num_q_vectors = pf->num_fcoe_msix;
7235 break;
7236
7237#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007238 default:
7239 WARN_ON(1);
7240 return -ENODATA;
7241 }
7242
7243 return 0;
7244}
7245
7246/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007247 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7248 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007249 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007250 *
7251 * On error: returns error code (negative)
7252 * On success: returns 0
7253 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007254static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007255{
7256 int size;
7257 int ret = 0;
7258
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007259 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007260 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7261 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7262 if (!vsi->tx_rings)
7263 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007264 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7265
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007266 if (alloc_qvectors) {
7267 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007268 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007269 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7270 if (!vsi->q_vectors) {
7271 ret = -ENOMEM;
7272 goto err_vectors;
7273 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007274 }
7275 return ret;
7276
7277err_vectors:
7278 kfree(vsi->tx_rings);
7279 return ret;
7280}
7281
7282/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007283 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7284 * @pf: board private structure
7285 * @type: type of VSI
7286 *
7287 * On error: returns error code (negative)
7288 * On success: returns vsi index in PF (positive)
7289 **/
7290static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7291{
7292 int ret = -ENODEV;
7293 struct i40e_vsi *vsi;
7294 int vsi_idx;
7295 int i;
7296
7297 /* Need to protect the allocation of the VSIs at the PF level */
7298 mutex_lock(&pf->switch_mutex);
7299
7300 /* VSI list may be fragmented if VSI creation/destruction has
7301 * been happening. We can afford to do a quick scan to look
7302 * for any free VSIs in the list.
7303 *
7304 * find next empty vsi slot, looping back around if necessary
7305 */
7306 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007307 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007308 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007309 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007310 i = 0;
7311 while (i < pf->next_vsi && pf->vsi[i])
7312 i++;
7313 }
7314
Mitch Williams505682c2014-05-20 08:01:37 +00007315 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007316 vsi_idx = i; /* Found one! */
7317 } else {
7318 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007319 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007320 }
7321 pf->next_vsi = ++i;
7322
7323 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7324 if (!vsi) {
7325 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007326 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007327 }
7328 vsi->type = type;
7329 vsi->back = pf;
7330 set_bit(__I40E_DOWN, &vsi->state);
7331 vsi->flags = 0;
7332 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007333 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007334 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7335 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007336 vsi->netdev_registered = false;
7337 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007338 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007339 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007340
Alexander Duyck9f65e152013-09-28 06:00:58 +00007341 ret = i40e_set_num_rings_in_vsi(vsi);
7342 if (ret)
7343 goto err_rings;
7344
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007345 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007346 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007347 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007348
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007349 /* Setup default MSIX irq handler for VSI */
7350 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7351
Kiran Patil21659032015-09-30 14:09:03 -04007352 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007353 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007354 pf->vsi[vsi_idx] = vsi;
7355 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007356 goto unlock_pf;
7357
Alexander Duyck9f65e152013-09-28 06:00:58 +00007358err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007359 pf->next_vsi = i - 1;
7360 kfree(vsi);
7361unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007362 mutex_unlock(&pf->switch_mutex);
7363 return ret;
7364}
7365
7366/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007367 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7368 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007369 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007370 *
7371 * On error: returns error code (negative)
7372 * On success: returns 0
7373 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007374static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007375{
7376 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007377 if (free_qvectors) {
7378 kfree(vsi->q_vectors);
7379 vsi->q_vectors = NULL;
7380 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007381 kfree(vsi->tx_rings);
7382 vsi->tx_rings = NULL;
7383 vsi->rx_rings = NULL;
7384}
7385
7386/**
Helin Zhang28c58692015-10-26 19:44:27 -04007387 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7388 * and lookup table
7389 * @vsi: Pointer to VSI structure
7390 */
7391static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7392{
7393 if (!vsi)
7394 return;
7395
7396 kfree(vsi->rss_hkey_user);
7397 vsi->rss_hkey_user = NULL;
7398
7399 kfree(vsi->rss_lut_user);
7400 vsi->rss_lut_user = NULL;
7401}
7402
7403/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007404 * i40e_vsi_clear - Deallocate the VSI provided
7405 * @vsi: the VSI being un-configured
7406 **/
7407static int i40e_vsi_clear(struct i40e_vsi *vsi)
7408{
7409 struct i40e_pf *pf;
7410
7411 if (!vsi)
7412 return 0;
7413
7414 if (!vsi->back)
7415 goto free_vsi;
7416 pf = vsi->back;
7417
7418 mutex_lock(&pf->switch_mutex);
7419 if (!pf->vsi[vsi->idx]) {
7420 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7421 vsi->idx, vsi->idx, vsi, vsi->type);
7422 goto unlock_vsi;
7423 }
7424
7425 if (pf->vsi[vsi->idx] != vsi) {
7426 dev_err(&pf->pdev->dev,
7427 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7428 pf->vsi[vsi->idx]->idx,
7429 pf->vsi[vsi->idx],
7430 pf->vsi[vsi->idx]->type,
7431 vsi->idx, vsi, vsi->type);
7432 goto unlock_vsi;
7433 }
7434
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007435 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007436 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7437 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7438
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007439 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007440 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007441
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007442 pf->vsi[vsi->idx] = NULL;
7443 if (vsi->idx < pf->next_vsi)
7444 pf->next_vsi = vsi->idx;
7445
7446unlock_vsi:
7447 mutex_unlock(&pf->switch_mutex);
7448free_vsi:
7449 kfree(vsi);
7450
7451 return 0;
7452}
7453
7454/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007455 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7456 * @vsi: the VSI being cleaned
7457 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007458static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007459{
7460 int i;
7461
Greg Rose8e9dca52013-12-18 13:45:53 +00007462 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007463 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007464 kfree_rcu(vsi->tx_rings[i], rcu);
7465 vsi->tx_rings[i] = NULL;
7466 vsi->rx_rings[i] = NULL;
7467 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007468 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007469}
7470
7471/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007472 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7473 * @vsi: the VSI being configured
7474 **/
7475static int i40e_alloc_rings(struct i40e_vsi *vsi)
7476{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007477 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007478 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007479 int i;
7480
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007482 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007483 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007484 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7485 if (!tx_ring)
7486 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007487
7488 tx_ring->queue_index = i;
7489 tx_ring->reg_idx = vsi->base_queue + i;
7490 tx_ring->ring_active = false;
7491 tx_ring->vsi = vsi;
7492 tx_ring->netdev = vsi->netdev;
7493 tx_ring->dev = &pf->pdev->dev;
7494 tx_ring->count = vsi->num_desc;
7495 tx_ring->size = 0;
7496 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007497 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7498 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007499 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007500 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501
Alexander Duyck9f65e152013-09-28 06:00:58 +00007502 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007503 rx_ring->queue_index = i;
7504 rx_ring->reg_idx = vsi->base_queue + i;
7505 rx_ring->ring_active = false;
7506 rx_ring->vsi = vsi;
7507 rx_ring->netdev = vsi->netdev;
7508 rx_ring->dev = &pf->pdev->dev;
7509 rx_ring->count = vsi->num_desc;
7510 rx_ring->size = 0;
7511 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007512 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007513 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007514 }
7515
7516 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007517
7518err_out:
7519 i40e_vsi_clear_rings(vsi);
7520 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007521}
7522
7523/**
7524 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7525 * @pf: board private structure
7526 * @vectors: the number of MSI-X vectors to request
7527 *
7528 * Returns the number of vectors reserved, or error
7529 **/
7530static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7531{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007532 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7533 I40E_MIN_MSIX, vectors);
7534 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007535 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007536 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007537 vectors = 0;
7538 }
7539
7540 return vectors;
7541}
7542
7543/**
7544 * i40e_init_msix - Setup the MSIX capability
7545 * @pf: board private structure
7546 *
7547 * Work with the OS to set up the MSIX vectors needed.
7548 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007549 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007550 **/
7551static int i40e_init_msix(struct i40e_pf *pf)
7552{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007553 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007554 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007555 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007556 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007557 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007558
7559 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7560 return -ENODEV;
7561
7562 /* The number of vectors we'll request will be comprised of:
7563 * - Add 1 for "other" cause for Admin Queue events, etc.
7564 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007565 * - Queues being used for RSS.
7566 * We don't need as many as max_rss_size vectors.
7567 * use rss_size instead in the calculation since that
7568 * is governed by number of cpus in the system.
7569 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007570 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007571 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007572#ifdef I40E_FCOE
7573 * - The number of FCOE qps.
7574#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007575 * Once we count this up, try the request.
7576 *
7577 * If we can't get what we want, we'll simplify to nearly nothing
7578 * and try again. If that still fails, we punt.
7579 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007580 vectors_left = hw->func_caps.num_msix_vectors;
7581 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582
Shannon Nelson1e200e42015-02-27 09:15:24 +00007583 /* reserve one vector for miscellaneous handler */
7584 if (vectors_left) {
7585 v_budget++;
7586 vectors_left--;
7587 }
7588
7589 /* reserve vectors for the main PF traffic queues */
7590 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7591 vectors_left -= pf->num_lan_msix;
7592 v_budget += pf->num_lan_msix;
7593
7594 /* reserve one vector for sideband flow director */
7595 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7596 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007597 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007598 v_budget++;
7599 vectors_left--;
7600 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007601 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007602 }
7603 }
John W Linville83840e42015-01-14 03:06:28 +00007604
Vasu Dev38e00432014-08-01 13:27:03 -07007605#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007606 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007607 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007608 if (!vectors_left)
7609 pf->num_fcoe_msix = 0;
7610 else if (vectors_left >= pf->num_fcoe_qps)
7611 pf->num_fcoe_msix = pf->num_fcoe_qps;
7612 else
7613 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007614 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007615 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007616 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007617
Vasu Dev38e00432014-08-01 13:27:03 -07007618#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007619 /* can we reserve enough for iWARP? */
7620 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007621 iwarp_requested = pf->num_iwarp_msix;
7622
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007623 if (!vectors_left)
7624 pf->num_iwarp_msix = 0;
7625 else if (vectors_left < pf->num_iwarp_msix)
7626 pf->num_iwarp_msix = 1;
7627 v_budget += pf->num_iwarp_msix;
7628 vectors_left -= pf->num_iwarp_msix;
7629 }
7630
Shannon Nelson1e200e42015-02-27 09:15:24 +00007631 /* any vectors left over go for VMDq support */
7632 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7633 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7634 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7635
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007636 if (!vectors_left) {
7637 pf->num_vmdq_msix = 0;
7638 pf->num_vmdq_qps = 0;
7639 } else {
7640 /* if we're short on vectors for what's desired, we limit
7641 * the queues per vmdq. If this is still more than are
7642 * available, the user will need to change the number of
7643 * queues/vectors used by the PF later with the ethtool
7644 * channels command
7645 */
7646 if (vmdq_vecs < vmdq_vecs_wanted)
7647 pf->num_vmdq_qps = 1;
7648 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007649
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007650 v_budget += vmdq_vecs;
7651 vectors_left -= vmdq_vecs;
7652 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007653 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007654
7655 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7656 GFP_KERNEL);
7657 if (!pf->msix_entries)
7658 return -ENOMEM;
7659
7660 for (i = 0; i < v_budget; i++)
7661 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007662 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007663
Shannon Nelson3b444392015-02-26 16:15:57 +00007664 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007665 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7666 kfree(pf->msix_entries);
7667 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007668 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007669 return -ENODEV;
7670
Shannon Nelson3b444392015-02-26 16:15:57 +00007671 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007672 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007673 pf->num_vmdq_vsis = 0;
7674 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007675 pf->num_lan_qps = 1;
7676 pf->num_lan_msix = 1;
7677
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007678 } else if (!vectors_left) {
7679 /* If we have limited resources, we will start with no vectors
7680 * for the special features and then allocate vectors to some
7681 * of these features based on the policy and at the end disable
7682 * the features that did not get any vectors.
7683 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007684 int vec;
7685
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007686 dev_info(&pf->pdev->dev,
7687 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007688 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007689 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007690
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007691 /* Scale vector usage down */
7692 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007693 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007694 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007695#ifdef I40E_FCOE
7696 pf->num_fcoe_qps = 0;
7697 pf->num_fcoe_msix = 0;
7698#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007699
7700 /* partition out the remaining vectors */
7701 switch (vec) {
7702 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007703 pf->num_lan_msix = 1;
7704 break;
7705 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007706 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7707 pf->num_lan_msix = 1;
7708 pf->num_iwarp_msix = 1;
7709 } else {
7710 pf->num_lan_msix = 2;
7711 }
Vasu Dev38e00432014-08-01 13:27:03 -07007712#ifdef I40E_FCOE
7713 /* give one vector to FCoE */
7714 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7715 pf->num_lan_msix = 1;
7716 pf->num_fcoe_msix = 1;
7717 }
Vasu Dev38e00432014-08-01 13:27:03 -07007718#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007719 break;
7720 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007721 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7722 pf->num_iwarp_msix = min_t(int, (vec / 3),
7723 iwarp_requested);
7724 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7725 I40E_DEFAULT_NUM_VMDQ_VSI);
7726 } else {
7727 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7728 I40E_DEFAULT_NUM_VMDQ_VSI);
7729 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007730 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7731 pf->num_fdsb_msix = 1;
7732 vec--;
7733 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007734 pf->num_lan_msix = min_t(int,
7735 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7736 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007737 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007738#ifdef I40E_FCOE
7739 /* give one vector to FCoE */
7740 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7741 pf->num_fcoe_msix = 1;
7742 vec--;
7743 }
7744#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007745 break;
7746 }
7747 }
7748
Stefan Assmannabd97a92016-09-19 13:37:51 +02007749 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7750 (pf->num_fdsb_msix == 0)) {
7751 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7752 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7753 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007754 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7755 (pf->num_vmdq_msix == 0)) {
7756 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7757 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7758 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007759
7760 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7761 (pf->num_iwarp_msix == 0)) {
7762 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7763 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7764 }
Vasu Dev38e00432014-08-01 13:27:03 -07007765#ifdef I40E_FCOE
7766
7767 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7768 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7769 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7770 }
7771#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007772 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7773 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7774 pf->num_lan_msix,
7775 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7776 pf->num_fdsb_msix,
7777 pf->num_iwarp_msix);
7778
Shannon Nelson3b444392015-02-26 16:15:57 +00007779 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007780}
7781
7782/**
Greg Rose90e04072014-03-06 08:59:57 +00007783 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007784 * @vsi: the VSI being configured
7785 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007786 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007787 *
7788 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7789 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007790static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007791{
7792 struct i40e_q_vector *q_vector;
7793
7794 /* allocate q_vector */
7795 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7796 if (!q_vector)
7797 return -ENOMEM;
7798
7799 q_vector->vsi = vsi;
7800 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007801 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7802
Alexander Duyck493fb302013-09-28 07:01:44 +00007803 if (vsi->netdev)
7804 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007805 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007806
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007807 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7808 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7809
Alexander Duyck493fb302013-09-28 07:01:44 +00007810 /* tie q_vector and vsi together */
7811 vsi->q_vectors[v_idx] = q_vector;
7812
7813 return 0;
7814}
7815
7816/**
Greg Rose90e04072014-03-06 08:59:57 +00007817 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007818 * @vsi: the VSI being configured
7819 *
7820 * We allocate one q_vector per queue interrupt. If allocation fails we
7821 * return -ENOMEM.
7822 **/
Greg Rose90e04072014-03-06 08:59:57 +00007823static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007824{
7825 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007826 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007827
7828 /* if not MSIX, give the one vector only to the LAN VSI */
7829 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7830 num_q_vectors = vsi->num_q_vectors;
7831 else if (vsi == pf->vsi[pf->lan_vsi])
7832 num_q_vectors = 1;
7833 else
7834 return -EINVAL;
7835
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007836 current_cpu = cpumask_first(cpu_online_mask);
7837
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007838 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007839 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007840 if (err)
7841 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007842 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7843 if (unlikely(current_cpu >= nr_cpu_ids))
7844 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007845 }
7846
7847 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007848
7849err_out:
7850 while (v_idx--)
7851 i40e_free_q_vector(vsi, v_idx);
7852
7853 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007854}
7855
7856/**
7857 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7858 * @pf: board private structure to initialize
7859 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007860static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007861{
Shannon Nelson3b444392015-02-26 16:15:57 +00007862 int vectors = 0;
7863 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007864
7865 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007866 vectors = i40e_init_msix(pf);
7867 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007868 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007869 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007870#ifdef I40E_FCOE
7871 I40E_FLAG_FCOE_ENABLED |
7872#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007873 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007874 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007875 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007876 I40E_FLAG_SRIOV_ENABLED |
7877 I40E_FLAG_FD_SB_ENABLED |
7878 I40E_FLAG_FD_ATR_ENABLED |
7879 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007880
7881 /* rework the queue expectations without MSIX */
7882 i40e_determine_queue_usage(pf);
7883 }
7884 }
7885
7886 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7887 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007888 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007889 vectors = pci_enable_msi(pf->pdev);
7890 if (vectors < 0) {
7891 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7892 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007893 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7894 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007895 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007896 }
7897
Shannon Nelson958a3e32013-09-28 07:13:28 +00007898 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007899 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007900
Shannon Nelson3b444392015-02-26 16:15:57 +00007901 /* set up vector assignment tracking */
7902 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7903 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007904 if (!pf->irq_pile) {
7905 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7906 return -ENOMEM;
7907 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007908 pf->irq_pile->num_entries = vectors;
7909 pf->irq_pile->search_hint = 0;
7910
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007911 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007912 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007913
7914 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007915}
7916
7917/**
7918 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7919 * @pf: board private structure
7920 *
7921 * This sets up the handler for MSIX 0, which is used to manage the
7922 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7923 * when in MSI or Legacy interrupt mode.
7924 **/
7925static int i40e_setup_misc_vector(struct i40e_pf *pf)
7926{
7927 struct i40e_hw *hw = &pf->hw;
7928 int err = 0;
7929
7930 /* Only request the irq if this is the first time through, and
7931 * not when we're rebuilding after a Reset
7932 */
7933 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7934 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007935 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007936 if (err) {
7937 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007938 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007939 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007940 return -EFAULT;
7941 }
7942 }
7943
Jacob Kellerab437b52014-12-14 01:55:08 +00007944 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007945
7946 /* associate no queues to the misc vector */
7947 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
7948 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
7949
7950 i40e_flush(hw);
7951
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08007952 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007953
7954 return err;
7955}
7956
7957/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007958 * i40e_config_rss_aq - Prepare for RSS using AQ commands
7959 * @vsi: vsi structure
7960 * @seed: RSS hash seed
7961 **/
Helin Zhange69ff812015-10-21 19:56:22 -04007962static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
7963 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007964{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007965 struct i40e_pf *pf = vsi->back;
7966 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07007967 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007968
Jacob Keller776b2e12016-07-19 16:23:30 -07007969 if (seed) {
7970 struct i40e_aqc_get_set_rss_key_data *seed_dw =
7971 (struct i40e_aqc_get_set_rss_key_data *)seed;
7972 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
7973 if (ret) {
7974 dev_info(&pf->pdev->dev,
7975 "Cannot set RSS key, err %s aq_err %s\n",
7976 i40e_stat_str(hw, ret),
7977 i40e_aq_str(hw, hw->aq.asq_last_status));
7978 return ret;
7979 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007980 }
Jacob Keller776b2e12016-07-19 16:23:30 -07007981 if (lut) {
7982 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007983
Jacob Keller776b2e12016-07-19 16:23:30 -07007984 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
7985 if (ret) {
7986 dev_info(&pf->pdev->dev,
7987 "Cannot set RSS lut, err %s aq_err %s\n",
7988 i40e_stat_str(hw, ret),
7989 i40e_aq_str(hw, hw->aq.asq_last_status));
7990 return ret;
7991 }
7992 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04007993 return ret;
7994}
7995
7996/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08007997 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
7998 * @vsi: Pointer to vsi structure
7999 * @seed: Buffter to store the hash keys
8000 * @lut: Buffer to store the lookup table entries
8001 * @lut_size: Size of buffer to store the lookup table entries
8002 *
8003 * Return 0 on success, negative on failure
8004 */
8005static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8006 u8 *lut, u16 lut_size)
8007{
8008 struct i40e_pf *pf = vsi->back;
8009 struct i40e_hw *hw = &pf->hw;
8010 int ret = 0;
8011
8012 if (seed) {
8013 ret = i40e_aq_get_rss_key(hw, vsi->id,
8014 (struct i40e_aqc_get_set_rss_key_data *)seed);
8015 if (ret) {
8016 dev_info(&pf->pdev->dev,
8017 "Cannot get RSS key, err %s aq_err %s\n",
8018 i40e_stat_str(&pf->hw, ret),
8019 i40e_aq_str(&pf->hw,
8020 pf->hw.aq.asq_last_status));
8021 return ret;
8022 }
8023 }
8024
8025 if (lut) {
8026 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8027
8028 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8029 if (ret) {
8030 dev_info(&pf->pdev->dev,
8031 "Cannot get RSS lut, err %s aq_err %s\n",
8032 i40e_stat_str(&pf->hw, ret),
8033 i40e_aq_str(&pf->hw,
8034 pf->hw.aq.asq_last_status));
8035 return ret;
8036 }
8037 }
8038
8039 return ret;
8040}
8041
8042/**
Jacob Keller0582b962016-07-19 16:23:29 -07008043 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8044 * @vsi: VSI structure
8045 **/
8046static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8047{
8048 u8 seed[I40E_HKEY_ARRAY_SIZE];
8049 struct i40e_pf *pf = vsi->back;
8050 u8 *lut;
8051 int ret;
8052
8053 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8054 return 0;
8055
Jacob Keller552b9962016-07-19 16:23:31 -07008056 if (!vsi->rss_size)
8057 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8058 vsi->num_queue_pairs);
8059 if (!vsi->rss_size)
8060 return -EINVAL;
8061
Jacob Keller0582b962016-07-19 16:23:29 -07008062 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8063 if (!lut)
8064 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008065 /* Use the user configured hash keys and lookup table if there is one,
8066 * otherwise use default
8067 */
8068 if (vsi->rss_lut_user)
8069 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8070 else
8071 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8072 if (vsi->rss_hkey_user)
8073 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8074 else
8075 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008076 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8077 kfree(lut);
8078
8079 return ret;
8080}
8081
8082/**
Helin Zhang043dd652015-10-21 19:56:23 -04008083 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008084 * @vsi: Pointer to vsi structure
8085 * @seed: RSS hash seed
8086 * @lut: Lookup table
8087 * @lut_size: Lookup table size
8088 *
8089 * Returns 0 on success, negative on failure
8090 **/
8091static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8092 const u8 *lut, u16 lut_size)
8093{
8094 struct i40e_pf *pf = vsi->back;
8095 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008096 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008097 u8 i;
8098
8099 /* Fill out hash function seed */
8100 if (seed) {
8101 u32 *seed_dw = (u32 *)seed;
8102
Mitch Williamsc4e18682016-04-12 08:30:40 -07008103 if (vsi->type == I40E_VSI_MAIN) {
8104 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8105 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8106 seed_dw[i]);
8107 } else if (vsi->type == I40E_VSI_SRIOV) {
8108 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8109 i40e_write_rx_ctl(hw,
8110 I40E_VFQF_HKEY1(i, vf_id),
8111 seed_dw[i]);
8112 } else {
8113 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8114 }
Helin Zhange69ff812015-10-21 19:56:22 -04008115 }
8116
8117 if (lut) {
8118 u32 *lut_dw = (u32 *)lut;
8119
Mitch Williamsc4e18682016-04-12 08:30:40 -07008120 if (vsi->type == I40E_VSI_MAIN) {
8121 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8122 return -EINVAL;
8123 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8124 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8125 } else if (vsi->type == I40E_VSI_SRIOV) {
8126 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8127 return -EINVAL;
8128 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8129 i40e_write_rx_ctl(hw,
8130 I40E_VFQF_HLUT1(i, vf_id),
8131 lut_dw[i]);
8132 } else {
8133 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8134 }
Helin Zhange69ff812015-10-21 19:56:22 -04008135 }
8136 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008137
8138 return 0;
8139}
8140
8141/**
Helin Zhang043dd652015-10-21 19:56:23 -04008142 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8143 * @vsi: Pointer to VSI structure
8144 * @seed: Buffer to store the keys
8145 * @lut: Buffer to store the lookup table entries
8146 * @lut_size: Size of buffer to store the lookup table entries
8147 *
8148 * Returns 0 on success, negative on failure
8149 */
8150static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8151 u8 *lut, u16 lut_size)
8152{
8153 struct i40e_pf *pf = vsi->back;
8154 struct i40e_hw *hw = &pf->hw;
8155 u16 i;
8156
8157 if (seed) {
8158 u32 *seed_dw = (u32 *)seed;
8159
8160 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008161 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008162 }
8163 if (lut) {
8164 u32 *lut_dw = (u32 *)lut;
8165
8166 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8167 return -EINVAL;
8168 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8169 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8170 }
8171
8172 return 0;
8173}
8174
8175/**
8176 * i40e_config_rss - Configure RSS keys and lut
8177 * @vsi: Pointer to VSI structure
8178 * @seed: RSS hash seed
8179 * @lut: Lookup table
8180 * @lut_size: Lookup table size
8181 *
8182 * Returns 0 on success, negative on failure
8183 */
8184int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8185{
8186 struct i40e_pf *pf = vsi->back;
8187
8188 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8189 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8190 else
8191 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8192}
8193
8194/**
8195 * i40e_get_rss - Get RSS keys and lut
8196 * @vsi: Pointer to VSI structure
8197 * @seed: Buffer to store the keys
8198 * @lut: Buffer to store the lookup table entries
8199 * lut_size: Size of buffer to store the lookup table entries
8200 *
8201 * Returns 0 on success, negative on failure
8202 */
8203int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8204{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008205 struct i40e_pf *pf = vsi->back;
8206
8207 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8208 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8209 else
8210 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008211}
8212
8213/**
Helin Zhange69ff812015-10-21 19:56:22 -04008214 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8215 * @pf: Pointer to board private structure
8216 * @lut: Lookup table
8217 * @rss_table_size: Lookup table size
8218 * @rss_size: Range of queue number for hashing
8219 */
Alan Bradyf1582352016-08-24 11:33:46 -07008220void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8221 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008222{
Helin Zhange69ff812015-10-21 19:56:22 -04008223 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008224
Helin Zhange69ff812015-10-21 19:56:22 -04008225 for (i = 0; i < rss_table_size; i++)
8226 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008227}
8228
8229/**
Helin Zhang043dd652015-10-21 19:56:23 -04008230 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008231 * @pf: board private structure
8232 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008233static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008234{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008235 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008236 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008237 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008238 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008239 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008240 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008241 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008242
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008243 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008244 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8245 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008246 hena |= i40e_pf_get_default_rss_hena(pf);
8247
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008248 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8249 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008250
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008251 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008252 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008253 reg_val = (pf->rss_table_size == 512) ?
8254 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8255 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008256 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008257
Helin Zhang28c58692015-10-26 19:44:27 -04008258 /* Determine the RSS size of the VSI */
8259 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008260 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8261 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008262 if (!vsi->rss_size)
8263 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008264
Helin Zhange69ff812015-10-21 19:56:22 -04008265 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8266 if (!lut)
8267 return -ENOMEM;
8268
Helin Zhang28c58692015-10-26 19:44:27 -04008269 /* Use user configured lut if there is one, otherwise use default */
8270 if (vsi->rss_lut_user)
8271 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8272 else
8273 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008274
Helin Zhang28c58692015-10-26 19:44:27 -04008275 /* Use user configured hash key if there is one, otherwise
8276 * use default.
8277 */
8278 if (vsi->rss_hkey_user)
8279 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8280 else
8281 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008282 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008283 kfree(lut);
8284
8285 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008286}
8287
8288/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008289 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8290 * @pf: board private structure
8291 * @queue_count: the requested queue count for rss.
8292 *
8293 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8294 * count which may be different from the requested queue count.
8295 **/
8296int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8297{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008298 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8299 int new_rss_size;
8300
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008301 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8302 return 0;
8303
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008304 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008305
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008306 if (queue_count != vsi->num_queue_pairs) {
8307 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008308 i40e_prep_for_reset(pf);
8309
Helin Zhangacd65442015-10-26 19:44:28 -04008310 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008311
8312 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008313
8314 /* Discard the user configured hash keys and lut, if less
8315 * queues are enabled.
8316 */
8317 if (queue_count < vsi->rss_size) {
8318 i40e_clear_rss_config_user(vsi);
8319 dev_dbg(&pf->pdev->dev,
8320 "discard user configured hash keys and lut\n");
8321 }
8322
8323 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008324 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8325 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008326
Helin Zhang043dd652015-10-21 19:56:23 -04008327 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008328 }
Lihong Yang12815052016-09-27 11:28:48 -07008329 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8330 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008331 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008332}
8333
8334/**
Greg Rosef4492db2015-02-06 08:52:12 +00008335 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8336 * @pf: board private structure
8337 **/
8338i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8339{
8340 i40e_status status;
8341 bool min_valid, max_valid;
8342 u32 max_bw, min_bw;
8343
8344 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8345 &min_valid, &max_valid);
8346
8347 if (!status) {
8348 if (min_valid)
8349 pf->npar_min_bw = min_bw;
8350 if (max_valid)
8351 pf->npar_max_bw = max_bw;
8352 }
8353
8354 return status;
8355}
8356
8357/**
8358 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8359 * @pf: board private structure
8360 **/
8361i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8362{
8363 struct i40e_aqc_configure_partition_bw_data bw_data;
8364 i40e_status status;
8365
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008366 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008367 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008368 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8369 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8370
8371 /* Set the new bandwidths */
8372 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8373
8374 return status;
8375}
8376
8377/**
8378 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8379 * @pf: board private structure
8380 **/
8381i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8382{
8383 /* Commit temporary BW setting to permanent NVM image */
8384 enum i40e_admin_queue_err last_aq_status;
8385 i40e_status ret;
8386 u16 nvm_word;
8387
8388 if (pf->hw.partition_id != 1) {
8389 dev_info(&pf->pdev->dev,
8390 "Commit BW only works on partition 1! This is partition %d",
8391 pf->hw.partition_id);
8392 ret = I40E_NOT_SUPPORTED;
8393 goto bw_commit_out;
8394 }
8395
8396 /* Acquire NVM for read access */
8397 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8398 last_aq_status = pf->hw.aq.asq_last_status;
8399 if (ret) {
8400 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008401 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8402 i40e_stat_str(&pf->hw, ret),
8403 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008404 goto bw_commit_out;
8405 }
8406
8407 /* Read word 0x10 of NVM - SW compatibility word 1 */
8408 ret = i40e_aq_read_nvm(&pf->hw,
8409 I40E_SR_NVM_CONTROL_WORD,
8410 0x10, sizeof(nvm_word), &nvm_word,
8411 false, NULL);
8412 /* Save off last admin queue command status before releasing
8413 * the NVM
8414 */
8415 last_aq_status = pf->hw.aq.asq_last_status;
8416 i40e_release_nvm(&pf->hw);
8417 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008418 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8419 i40e_stat_str(&pf->hw, ret),
8420 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008421 goto bw_commit_out;
8422 }
8423
8424 /* Wait a bit for NVM release to complete */
8425 msleep(50);
8426
8427 /* Acquire NVM for write access */
8428 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8429 last_aq_status = pf->hw.aq.asq_last_status;
8430 if (ret) {
8431 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008432 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8433 i40e_stat_str(&pf->hw, ret),
8434 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008435 goto bw_commit_out;
8436 }
8437 /* Write it back out unchanged to initiate update NVM,
8438 * which will force a write of the shadow (alt) RAM to
8439 * the NVM - thus storing the bandwidth values permanently.
8440 */
8441 ret = i40e_aq_update_nvm(&pf->hw,
8442 I40E_SR_NVM_CONTROL_WORD,
8443 0x10, sizeof(nvm_word),
8444 &nvm_word, true, NULL);
8445 /* Save off last admin queue command status before releasing
8446 * the NVM
8447 */
8448 last_aq_status = pf->hw.aq.asq_last_status;
8449 i40e_release_nvm(&pf->hw);
8450 if (ret)
8451 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008452 "BW settings NOT SAVED, err %s aq_err %s\n",
8453 i40e_stat_str(&pf->hw, ret),
8454 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008455bw_commit_out:
8456
8457 return ret;
8458}
8459
8460/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008461 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8462 * @pf: board private structure to initialize
8463 *
8464 * i40e_sw_init initializes the Adapter private data structure.
8465 * Fields are initialized based on PCI device information and
8466 * OS network device settings (MTU size).
8467 **/
8468static int i40e_sw_init(struct i40e_pf *pf)
8469{
8470 int err = 0;
8471 int size;
8472
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008473 /* Set default capability flags */
8474 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8475 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008476 I40E_FLAG_MSIX_ENABLED;
8477
Mitch Williamsca99eb92014-04-04 04:43:07 +00008478 /* Set default ITR */
8479 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8480 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8481
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008482 /* Depending on PF configurations, it is possible that the RSS
8483 * maximum might end up larger than the available queues
8484 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008485 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008486 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008487 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008488 pf->rss_size_max = min_t(int, pf->rss_size_max,
8489 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008490 if (pf->hw.func_caps.rss) {
8491 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008492 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8493 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008494 }
8495
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008496 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008497 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008498 pf->flags |= I40E_FLAG_MFP_ENABLED;
8499 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008500 if (i40e_get_npar_bw_setting(pf))
8501 dev_warn(&pf->pdev->dev,
8502 "Could not get NPAR bw settings\n");
8503 else
8504 dev_info(&pf->pdev->dev,
8505 "Min BW = %8.8x, Max BW = %8.8x\n",
8506 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008507 }
8508
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008509 /* FW/NVM is not yet fixed in this regard */
8510 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8511 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8512 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8513 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008514 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8515 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008516 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008517 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008518 else
8519 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008520 pf->fdir_pf_filter_count =
8521 pf->hw.func_caps.fd_filters_guaranteed;
8522 pf->hw.fdir_shared_filter_count =
8523 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008524 }
8525
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008526 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008527 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008528 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008529 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008530 /* No DCB support for FW < v4.33 */
8531 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8532 }
8533
8534 /* Disable FW LLDP if FW < v4.3 */
8535 if (i40e_is_mac_710(&pf->hw) &&
8536 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8537 (pf->hw.aq.fw_maj_ver < 4)))
8538 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8539
8540 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8541 if (i40e_is_mac_710(&pf->hw) &&
8542 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8543 (pf->hw.aq.fw_maj_ver >= 5)))
8544 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008545
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008546 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008547 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008548 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008549 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008550 }
8551
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008552 if (pf->hw.func_caps.iwarp) {
8553 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8554 /* IWARP needs one extra vector for CQP just like MISC.*/
8555 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8556 }
8557
Vasu Dev38e00432014-08-01 13:27:03 -07008558#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008559 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008560
8561#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008562#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008563 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008564 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8565 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8566 pf->num_req_vfs = min_t(int,
8567 pf->hw.func_caps.num_vfs,
8568 I40E_MAX_VF_COUNT);
8569 }
8570#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008571 if (pf->hw.mac.type == I40E_MAC_X722) {
8572 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8573 I40E_FLAG_128_QP_RSS_CAPABLE |
8574 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8575 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8576 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008577 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008578 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008579 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008580 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008581 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8582 ((pf->hw.aq.api_maj_ver == 1) &&
8583 (pf->hw.aq.api_min_ver > 4))) {
8584 /* Supported in FW API version higher than 1.4 */
8585 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008586 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8587 } else {
8588 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008589 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008590
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008591 pf->eeprom_version = 0xDEAD;
8592 pf->lan_veb = I40E_NO_VEB;
8593 pf->lan_vsi = I40E_NO_VSI;
8594
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008595 /* By default FW has this off for performance reasons */
8596 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8597
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008598 /* set up queue assignment tracking */
8599 size = sizeof(struct i40e_lump_tracking)
8600 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8601 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8602 if (!pf->qp_pile) {
8603 err = -ENOMEM;
8604 goto sw_init_done;
8605 }
8606 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8607 pf->qp_pile->search_hint = 0;
8608
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008609 pf->tx_timeout_recovery_level = 1;
8610
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008611 mutex_init(&pf->switch_mutex);
8612
Greg Rosec668a122015-02-26 16:10:39 +00008613 /* If NPAR is enabled nudge the Tx scheduler */
8614 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8615 i40e_set_npar_bw_setting(pf);
8616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008617sw_init_done:
8618 return err;
8619}
8620
8621/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008622 * i40e_set_ntuple - set the ntuple feature flag and take action
8623 * @pf: board private structure to initialize
8624 * @features: the feature set that the stack is suggesting
8625 *
8626 * returns a bool to indicate if reset needs to happen
8627 **/
8628bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8629{
8630 bool need_reset = false;
8631
8632 /* Check if Flow Director n-tuple support was enabled or disabled. If
8633 * the state changed, we need to reset.
8634 */
8635 if (features & NETIF_F_NTUPLE) {
8636 /* Enable filters and mark for reset */
8637 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8638 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008639 /* enable FD_SB only if there is MSI-X vector */
8640 if (pf->num_fdsb_msix > 0)
8641 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008642 } else {
8643 /* turn off filters, mark for reset and clear SW filter list */
8644 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8645 need_reset = true;
8646 i40e_fdir_filter_exit(pf);
8647 }
8648 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008649 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008650 /* reset fd counters */
8651 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8652 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008653 /* if ATR was auto disabled it can be re-enabled. */
8654 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008655 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008656 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008657 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8658 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8659 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008660 }
8661 return need_reset;
8662}
8663
8664/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008665 * i40e_clear_rss_lut - clear the rx hash lookup table
8666 * @vsi: the VSI being configured
8667 **/
8668static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8669{
8670 struct i40e_pf *pf = vsi->back;
8671 struct i40e_hw *hw = &pf->hw;
8672 u16 vf_id = vsi->vf_id;
8673 u8 i;
8674
8675 if (vsi->type == I40E_VSI_MAIN) {
8676 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8677 wr32(hw, I40E_PFQF_HLUT(i), 0);
8678 } else if (vsi->type == I40E_VSI_SRIOV) {
8679 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8680 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8681 } else {
8682 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8683 }
8684}
8685
8686/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008687 * i40e_set_features - set the netdev feature flags
8688 * @netdev: ptr to the netdev being adjusted
8689 * @features: the feature set that the stack is suggesting
8690 **/
8691static int i40e_set_features(struct net_device *netdev,
8692 netdev_features_t features)
8693{
8694 struct i40e_netdev_priv *np = netdev_priv(netdev);
8695 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008696 struct i40e_pf *pf = vsi->back;
8697 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008698
Alan Bradyd8ec9862016-07-27 12:02:38 -07008699 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8700 i40e_pf_config_rss(pf);
8701 else if (!(features & NETIF_F_RXHASH) &&
8702 netdev->features & NETIF_F_RXHASH)
8703 i40e_clear_rss_lut(vsi);
8704
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008705 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8706 i40e_vlan_stripping_enable(vsi);
8707 else
8708 i40e_vlan_stripping_disable(vsi);
8709
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008710 need_reset = i40e_set_ntuple(pf, features);
8711
8712 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008713 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008714
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008715 return 0;
8716}
8717
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008718/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008719 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008720 * @pf: board private structure
8721 * @port: The UDP port to look up
8722 *
8723 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8724 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008725static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008726{
8727 u8 i;
8728
8729 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008730 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008731 return i;
8732 }
8733
8734 return i;
8735}
8736
8737/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008738 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008739 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008740 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008741 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008742static void i40e_udp_tunnel_add(struct net_device *netdev,
8743 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008744{
8745 struct i40e_netdev_priv *np = netdev_priv(netdev);
8746 struct i40e_vsi *vsi = np->vsi;
8747 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008748 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008749 u8 next_idx;
8750 u8 idx;
8751
Singhai, Anjali6a899022015-12-14 12:21:18 -08008752 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008753
8754 /* Check if port already exists */
8755 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008756 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008757 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008758 return;
8759 }
8760
8761 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008762 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008763
8764 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008765 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008766 ntohs(port));
8767 return;
8768 }
8769
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008770 switch (ti->type) {
8771 case UDP_TUNNEL_TYPE_VXLAN:
8772 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8773 break;
8774 case UDP_TUNNEL_TYPE_GENEVE:
8775 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8776 return;
8777 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8778 break;
8779 default:
8780 return;
8781 }
8782
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008783 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008784 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008785 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8786 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008787}
8788
8789/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008790 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008791 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008792 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008793 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008794static void i40e_udp_tunnel_del(struct net_device *netdev,
8795 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008796{
8797 struct i40e_netdev_priv *np = netdev_priv(netdev);
8798 struct i40e_vsi *vsi = np->vsi;
8799 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008800 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008801 u8 idx;
8802
Singhai, Anjali6a899022015-12-14 12:21:18 -08008803 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008804
8805 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008806 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8807 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008808
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008809 switch (ti->type) {
8810 case UDP_TUNNEL_TYPE_VXLAN:
8811 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8812 goto not_found;
8813 break;
8814 case UDP_TUNNEL_TYPE_GENEVE:
8815 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8816 goto not_found;
8817 break;
8818 default:
8819 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008820 }
8821
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008822 /* if port exists, set it to 0 (mark for deletion)
8823 * and make it pending
8824 */
8825 pf->udp_ports[idx].index = 0;
8826 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008827 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8828
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008829 return;
8830not_found:
8831 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8832 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008833}
8834
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008835static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008836 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008837{
8838 struct i40e_netdev_priv *np = netdev_priv(netdev);
8839 struct i40e_pf *pf = np->vsi->back;
8840 struct i40e_hw *hw = &pf->hw;
8841
8842 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8843 return -EOPNOTSUPP;
8844
8845 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8846 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8847
8848 return 0;
8849}
8850
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008851/**
8852 * i40e_ndo_fdb_add - add an entry to the hardware database
8853 * @ndm: the input from the stack
8854 * @tb: pointer to array of nladdr (unused)
8855 * @dev: the net device pointer
8856 * @addr: the MAC address entry being added
8857 * @flags: instructions from stack about fdb operation
8858 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008859static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8860 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008861 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008862 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008863{
8864 struct i40e_netdev_priv *np = netdev_priv(dev);
8865 struct i40e_pf *pf = np->vsi->back;
8866 int err = 0;
8867
8868 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8869 return -EOPNOTSUPP;
8870
Or Gerlitz65891fe2014-12-14 18:19:05 +02008871 if (vid) {
8872 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8873 return -EINVAL;
8874 }
8875
Greg Rose4ba0dea2014-03-06 08:59:55 +00008876 /* Hardware does not support aging addresses so if a
8877 * ndm_state is given only allow permanent addresses
8878 */
8879 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8880 netdev_info(dev, "FDB only supports static addresses\n");
8881 return -EINVAL;
8882 }
8883
8884 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8885 err = dev_uc_add_excl(dev, addr);
8886 else if (is_multicast_ether_addr(addr))
8887 err = dev_mc_add_excl(dev, addr);
8888 else
8889 err = -EINVAL;
8890
8891 /* Only return duplicate errors if NLM_F_EXCL is set */
8892 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8893 err = 0;
8894
8895 return err;
8896}
8897
Neerav Parikh51616012015-02-06 08:52:14 +00008898/**
8899 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8900 * @dev: the netdev being configured
8901 * @nlh: RTNL message
8902 *
8903 * Inserts a new hardware bridge if not already created and
8904 * enables the bridging mode requested (VEB or VEPA). If the
8905 * hardware bridge has already been inserted and the request
8906 * is to change the mode then that requires a PF reset to
8907 * allow rebuild of the components with required hardware
8908 * bridge mode enabled.
8909 **/
8910static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008911 struct nlmsghdr *nlh,
8912 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008913{
8914 struct i40e_netdev_priv *np = netdev_priv(dev);
8915 struct i40e_vsi *vsi = np->vsi;
8916 struct i40e_pf *pf = vsi->back;
8917 struct i40e_veb *veb = NULL;
8918 struct nlattr *attr, *br_spec;
8919 int i, rem;
8920
8921 /* Only for PF VSI for now */
8922 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8923 return -EOPNOTSUPP;
8924
8925 /* Find the HW bridge for PF VSI */
8926 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8927 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8928 veb = pf->veb[i];
8929 }
8930
8931 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8932
8933 nla_for_each_nested(attr, br_spec, rem) {
8934 __u16 mode;
8935
8936 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8937 continue;
8938
8939 mode = nla_get_u16(attr);
8940 if ((mode != BRIDGE_MODE_VEPA) &&
8941 (mode != BRIDGE_MODE_VEB))
8942 return -EINVAL;
8943
8944 /* Insert a new HW bridge */
8945 if (!veb) {
8946 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
8947 vsi->tc_config.enabled_tc);
8948 if (veb) {
8949 veb->bridge_mode = mode;
8950 i40e_config_bridge_mode(veb);
8951 } else {
8952 /* No Bridge HW offload available */
8953 return -ENOENT;
8954 }
8955 break;
8956 } else if (mode != veb->bridge_mode) {
8957 /* Existing HW bridge but different mode needs reset */
8958 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07008959 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
8960 if (mode == BRIDGE_MODE_VEB)
8961 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
8962 else
8963 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
8964 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00008965 break;
8966 }
8967 }
8968
8969 return 0;
8970}
8971
8972/**
8973 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
8974 * @skb: skb buff
8975 * @pid: process id
8976 * @seq: RTNL message seq #
8977 * @dev: the netdev being configured
8978 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04008979 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00008980 *
8981 * Return the mode in which the hardware bridge is operating in
8982 * i.e VEB or VEPA.
8983 **/
Neerav Parikh51616012015-02-06 08:52:14 +00008984static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8985 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04008986 u32 __always_unused filter_mask,
8987 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00008988{
8989 struct i40e_netdev_priv *np = netdev_priv(dev);
8990 struct i40e_vsi *vsi = np->vsi;
8991 struct i40e_pf *pf = vsi->back;
8992 struct i40e_veb *veb = NULL;
8993 int i;
8994
8995 /* Only for PF VSI for now */
8996 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8997 return -EOPNOTSUPP;
8998
8999 /* Find the HW bridge for the PF VSI */
9000 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9001 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9002 veb = pf->veb[i];
9003 }
9004
9005 if (!veb)
9006 return 0;
9007
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009008 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009009 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009010}
Neerav Parikh51616012015-02-06 08:52:14 +00009011
Singhai, Anjali6a899022015-12-14 12:21:18 -08009012/* Hardware supports L4 tunnel length of 128B (=2^7) which includes
9013 * inner mac plus all inner ethertypes.
9014 */
9015#define I40E_MAX_TUNNEL_HDR_LEN 128
Joe Stringerf44a75e2015-04-14 17:09:14 -07009016/**
9017 * i40e_features_check - Validate encapsulated packet conforms to limits
9018 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009019 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009020 * @features: Offload features that the stack believes apply
9021 **/
9022static netdev_features_t i40e_features_check(struct sk_buff *skb,
9023 struct net_device *dev,
9024 netdev_features_t features)
9025{
9026 if (skb->encapsulation &&
Singhai, Anjali6a899022015-12-14 12:21:18 -08009027 ((skb_inner_network_header(skb) - skb_transport_header(skb)) >
Joe Stringerf44a75e2015-04-14 17:09:14 -07009028 I40E_MAX_TUNNEL_HDR_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08009029 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009030
9031 return features;
9032}
9033
Shannon Nelson37a29732015-02-27 09:15:19 +00009034static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009035 .ndo_open = i40e_open,
9036 .ndo_stop = i40e_close,
9037 .ndo_start_xmit = i40e_lan_xmit_frame,
9038 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9039 .ndo_set_rx_mode = i40e_set_rx_mode,
9040 .ndo_validate_addr = eth_validate_addr,
9041 .ndo_set_mac_address = i40e_set_mac,
9042 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009043 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009044 .ndo_tx_timeout = i40e_tx_timeout,
9045 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9046 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9047#ifdef CONFIG_NET_POLL_CONTROLLER
9048 .ndo_poll_controller = i40e_netpoll,
9049#endif
John Fastabende4c67342016-02-16 21:16:15 -08009050 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009051#ifdef I40E_FCOE
9052 .ndo_fcoe_enable = i40e_fcoe_enable,
9053 .ndo_fcoe_disable = i40e_fcoe_disable,
9054#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009055 .ndo_set_features = i40e_set_features,
9056 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9057 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009058 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009059 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009060 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009061 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009062 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009063 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9064 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009065 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009066 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009067 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009068 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9069 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009070};
9071
9072/**
9073 * i40e_config_netdev - Setup the netdev flags
9074 * @vsi: the VSI being configured
9075 *
9076 * Returns 0 on success, negative value on failure
9077 **/
9078static int i40e_config_netdev(struct i40e_vsi *vsi)
9079{
9080 struct i40e_pf *pf = vsi->back;
9081 struct i40e_hw *hw = &pf->hw;
9082 struct i40e_netdev_priv *np;
9083 struct net_device *netdev;
9084 u8 mac_addr[ETH_ALEN];
9085 int etherdev_size;
9086
9087 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009088 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009089 if (!netdev)
9090 return -ENOMEM;
9091
9092 vsi->netdev = netdev;
9093 np = netdev_priv(netdev);
9094 np->vsi = vsi;
9095
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009096 netdev->hw_enc_features |= NETIF_F_SG |
9097 NETIF_F_IP_CSUM |
9098 NETIF_F_IPV6_CSUM |
9099 NETIF_F_HIGHDMA |
9100 NETIF_F_SOFT_FEATURES |
9101 NETIF_F_TSO |
9102 NETIF_F_TSO_ECN |
9103 NETIF_F_TSO6 |
9104 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009105 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009106 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009107 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009108 NETIF_F_GSO_UDP_TUNNEL |
9109 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009110 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009111 NETIF_F_SCTP_CRC |
9112 NETIF_F_RXHASH |
9113 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009114 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009115
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009116 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009117 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9118
9119 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009120
9121 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009122 netdev->vlan_features |= netdev->hw_enc_features |
9123 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009124
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009125 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009126 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009127
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009128 netdev->hw_features |= netdev->hw_enc_features |
9129 NETIF_F_HW_VLAN_CTAG_TX |
9130 NETIF_F_HW_VLAN_CTAG_RX;
9131
9132 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009133 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009134
9135 if (vsi->type == I40E_VSI_MAIN) {
9136 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009137 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009138 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009139 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009140 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009141 } else {
9142 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9143 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9144 pf->vsi[pf->lan_vsi]->netdev->name);
9145 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009146
Jacob Keller278e7d02016-10-05 09:30:37 -07009147 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009148 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009149 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009150 }
Kiran Patil21659032015-09-30 14:09:03 -04009151
Greg Rose9a173902014-05-22 06:32:02 +00009152 ether_addr_copy(netdev->dev_addr, mac_addr);
9153 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009154
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009155 netdev->priv_flags |= IFF_UNICAST_FLT;
9156 netdev->priv_flags |= IFF_SUPP_NOFCS;
9157 /* Setup netdev TC information */
9158 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9159
9160 netdev->netdev_ops = &i40e_netdev_ops;
9161 netdev->watchdog_timeo = 5 * HZ;
9162 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009163#ifdef I40E_FCOE
9164 i40e_fcoe_config_netdev(netdev, vsi);
9165#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009166
Jarod Wilson91c527a2016-10-17 15:54:05 -04009167 /* MTU range: 68 - 9706 */
9168 netdev->min_mtu = ETH_MIN_MTU;
9169 netdev->max_mtu = I40E_MAX_RXBUFFER -
9170 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9171
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009172 return 0;
9173}
9174
9175/**
9176 * i40e_vsi_delete - Delete a VSI from the switch
9177 * @vsi: the VSI being removed
9178 *
9179 * Returns 0 on success, negative value on failure
9180 **/
9181static void i40e_vsi_delete(struct i40e_vsi *vsi)
9182{
9183 /* remove default VSI is not allowed */
9184 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9185 return;
9186
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009187 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009188}
9189
9190/**
Neerav Parikh51616012015-02-06 08:52:14 +00009191 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9192 * @vsi: the VSI being queried
9193 *
9194 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9195 **/
9196int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9197{
9198 struct i40e_veb *veb;
9199 struct i40e_pf *pf = vsi->back;
9200
9201 /* Uplink is not a bridge so default to VEB */
9202 if (vsi->veb_idx == I40E_NO_VEB)
9203 return 1;
9204
9205 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009206 if (!veb) {
9207 dev_info(&pf->pdev->dev,
9208 "There is no veb associated with the bridge\n");
9209 return -ENOENT;
9210 }
Neerav Parikh51616012015-02-06 08:52:14 +00009211
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009212 /* Uplink is a bridge in VEPA mode */
9213 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9214 return 0;
9215 } else {
9216 /* Uplink is a bridge in VEB mode */
9217 return 1;
9218 }
9219
9220 /* VEPA is now default bridge, so return 0 */
9221 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009222}
9223
9224/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009225 * i40e_add_vsi - Add a VSI to the switch
9226 * @vsi: the VSI being configured
9227 *
9228 * This initializes a VSI context depending on the VSI type to be added and
9229 * passes it down to the add_vsi aq command.
9230 **/
9231static int i40e_add_vsi(struct i40e_vsi *vsi)
9232{
9233 int ret = -ENODEV;
Kiran Patilf6bd0962016-06-20 09:10:34 -07009234 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009235 struct i40e_pf *pf = vsi->back;
9236 struct i40e_hw *hw = &pf->hw;
9237 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009238 struct i40e_mac_filter *f;
9239 struct hlist_node *h;
9240 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009241
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009242 u8 enabled_tc = 0x1; /* TC0 enabled */
9243 int f_count = 0;
9244
9245 memset(&ctxt, 0, sizeof(ctxt));
9246 switch (vsi->type) {
9247 case I40E_VSI_MAIN:
9248 /* The PF's main VSI is already setup as part of the
9249 * device initialization, so we'll not bother with
9250 * the add_vsi call, but we will retrieve the current
9251 * VSI context.
9252 */
9253 ctxt.seid = pf->main_vsi_seid;
9254 ctxt.pf_num = pf->hw.pf_id;
9255 ctxt.vf_num = 0;
9256 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9257 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9258 if (ret) {
9259 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009260 "couldn't get PF vsi config, err %s aq_err %s\n",
9261 i40e_stat_str(&pf->hw, ret),
9262 i40e_aq_str(&pf->hw,
9263 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009264 return -ENOENT;
9265 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009266 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009267 vsi->info.valid_sections = 0;
9268
9269 vsi->seid = ctxt.seid;
9270 vsi->id = ctxt.vsi_number;
9271
9272 enabled_tc = i40e_pf_get_tc_map(pf);
9273
9274 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009275 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9276 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277 memset(&ctxt, 0, sizeof(ctxt));
9278 ctxt.seid = pf->main_vsi_seid;
9279 ctxt.pf_num = pf->hw.pf_id;
9280 ctxt.vf_num = 0;
9281 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9282 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9283 if (ret) {
9284 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009285 "update vsi failed, err %s aq_err %s\n",
9286 i40e_stat_str(&pf->hw, ret),
9287 i40e_aq_str(&pf->hw,
9288 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009289 ret = -ENOENT;
9290 goto err;
9291 }
9292 /* update the local VSI info queue map */
9293 i40e_vsi_update_queue_map(vsi, &ctxt);
9294 vsi->info.valid_sections = 0;
9295 } else {
9296 /* Default/Main VSI is only enabled for TC0
9297 * reconfigure it to enable all TCs that are
9298 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009299 * For MFP case the iSCSI PF would use this
9300 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009301 */
9302 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9303 if (ret) {
9304 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009305 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9306 enabled_tc,
9307 i40e_stat_str(&pf->hw, ret),
9308 i40e_aq_str(&pf->hw,
9309 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009310 ret = -ENOENT;
9311 }
9312 }
9313 break;
9314
9315 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009316 ctxt.pf_num = hw->pf_id;
9317 ctxt.vf_num = 0;
9318 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009319 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009320 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009321 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9322 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009323 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009324 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009325 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009326 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009327 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009328 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009329 break;
9330
9331 case I40E_VSI_VMDQ2:
9332 ctxt.pf_num = hw->pf_id;
9333 ctxt.vf_num = 0;
9334 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009335 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009336 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9337
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009338 /* This VSI is connected to VEB so the switch_id
9339 * should be set to zero by default.
9340 */
Neerav Parikh51616012015-02-06 08:52:14 +00009341 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9342 ctxt.info.valid_sections |=
9343 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9344 ctxt.info.switch_id =
9345 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9346 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009347
9348 /* Setup the VSI tx/rx queue map for TC0 only for now */
9349 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9350 break;
9351
9352 case I40E_VSI_SRIOV:
9353 ctxt.pf_num = hw->pf_id;
9354 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9355 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009356 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009357 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9358
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009359 /* This VSI is connected to VEB so the switch_id
9360 * should be set to zero by default.
9361 */
Neerav Parikh51616012015-02-06 08:52:14 +00009362 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9363 ctxt.info.valid_sections |=
9364 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9365 ctxt.info.switch_id =
9366 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9367 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009368
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009369 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9370 ctxt.info.valid_sections |=
9371 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9372 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009373 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9374 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009375 }
9376
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009377 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9378 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009379 if (pf->vf[vsi->vf_id].spoofchk) {
9380 ctxt.info.valid_sections |=
9381 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9382 ctxt.info.sec_flags |=
9383 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9384 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9385 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009386 /* Setup the VSI tx/rx queue map for TC0 only for now */
9387 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9388 break;
9389
Vasu Dev38e00432014-08-01 13:27:03 -07009390#ifdef I40E_FCOE
9391 case I40E_VSI_FCOE:
9392 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9393 if (ret) {
9394 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9395 return ret;
9396 }
9397 break;
9398
9399#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009400 case I40E_VSI_IWARP:
9401 /* send down message to iWARP */
9402 break;
9403
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009404 default:
9405 return -ENODEV;
9406 }
9407
9408 if (vsi->type != I40E_VSI_MAIN) {
9409 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9410 if (ret) {
9411 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009412 "add vsi failed, err %s aq_err %s\n",
9413 i40e_stat_str(&pf->hw, ret),
9414 i40e_aq_str(&pf->hw,
9415 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009416 ret = -ENOENT;
9417 goto err;
9418 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009419 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009420 vsi->info.valid_sections = 0;
9421 vsi->seid = ctxt.seid;
9422 vsi->id = ctxt.vsi_number;
9423 }
Kiran Patilf6bd0962016-06-20 09:10:34 -07009424 /* Except FDIR VSI, for all othet VSI set the broadcast filter */
9425 if (vsi->type != I40E_VSI_FDIR) {
9426 aq_ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
9427 if (aq_ret) {
9428 ret = i40e_aq_rc_to_posix(aq_ret,
9429 hw->aq.asq_last_status);
9430 dev_info(&pf->pdev->dev,
9431 "set brdcast promisc failed, err %s, aq_err %s\n",
9432 i40e_stat_str(hw, aq_ret),
9433 i40e_aq_str(hw, hw->aq.asq_last_status));
9434 }
9435 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009436
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009437 vsi->active_filters = 0;
9438 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009439 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009440 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009441 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009442 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009443 f_count++;
9444 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009445 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009446
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009447 if (f_count) {
9448 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9449 pf->flags |= I40E_FLAG_FILTER_SYNC;
9450 }
9451
9452 /* Update VSI BW information */
9453 ret = i40e_vsi_get_bw_info(vsi);
9454 if (ret) {
9455 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009456 "couldn't get vsi bw info, err %s aq_err %s\n",
9457 i40e_stat_str(&pf->hw, ret),
9458 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009459 /* VSI is already added so not tearing that up */
9460 ret = 0;
9461 }
9462
9463err:
9464 return ret;
9465}
9466
9467/**
9468 * i40e_vsi_release - Delete a VSI and free its resources
9469 * @vsi: the VSI being removed
9470 *
9471 * Returns 0 on success or < 0 on error
9472 **/
9473int i40e_vsi_release(struct i40e_vsi *vsi)
9474{
Jacob Keller278e7d02016-10-05 09:30:37 -07009475 struct i40e_mac_filter *f;
9476 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009477 struct i40e_veb *veb = NULL;
9478 struct i40e_pf *pf;
9479 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009480 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009481
9482 pf = vsi->back;
9483
9484 /* release of a VEB-owner or last VSI is not allowed */
9485 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9486 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9487 vsi->seid, vsi->uplink_seid);
9488 return -ENODEV;
9489 }
9490 if (vsi == pf->vsi[pf->lan_vsi] &&
9491 !test_bit(__I40E_DOWN, &pf->state)) {
9492 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9493 return -ENODEV;
9494 }
9495
9496 uplink_seid = vsi->uplink_seid;
9497 if (vsi->type != I40E_VSI_SRIOV) {
9498 if (vsi->netdev_registered) {
9499 vsi->netdev_registered = false;
9500 if (vsi->netdev) {
9501 /* results in a call to i40e_close() */
9502 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009503 }
9504 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009505 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009506 }
9507 i40e_vsi_disable_irq(vsi);
9508 }
9509
Jacob Keller278e7d02016-10-05 09:30:37 -07009510 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009511
9512 /* clear the sync flag on all filters */
9513 if (vsi->netdev) {
9514 __dev_uc_unsync(vsi->netdev, NULL);
9515 __dev_mc_unsync(vsi->netdev, NULL);
9516 }
9517
9518 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009519 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009520 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009521
Jacob Keller278e7d02016-10-05 09:30:37 -07009522 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009523
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009524 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009525
9526 i40e_vsi_delete(vsi);
9527 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009528 if (vsi->netdev) {
9529 free_netdev(vsi->netdev);
9530 vsi->netdev = NULL;
9531 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009532 i40e_vsi_clear_rings(vsi);
9533 i40e_vsi_clear(vsi);
9534
9535 /* If this was the last thing on the VEB, except for the
9536 * controlling VSI, remove the VEB, which puts the controlling
9537 * VSI onto the next level down in the switch.
9538 *
9539 * Well, okay, there's one more exception here: don't remove
9540 * the orphan VEBs yet. We'll wait for an explicit remove request
9541 * from up the network stack.
9542 */
Mitch Williams505682c2014-05-20 08:01:37 +00009543 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009544 if (pf->vsi[i] &&
9545 pf->vsi[i]->uplink_seid == uplink_seid &&
9546 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9547 n++; /* count the VSIs */
9548 }
9549 }
9550 for (i = 0; i < I40E_MAX_VEB; i++) {
9551 if (!pf->veb[i])
9552 continue;
9553 if (pf->veb[i]->uplink_seid == uplink_seid)
9554 n++; /* count the VEBs */
9555 if (pf->veb[i]->seid == uplink_seid)
9556 veb = pf->veb[i];
9557 }
9558 if (n == 0 && veb && veb->uplink_seid != 0)
9559 i40e_veb_release(veb);
9560
9561 return 0;
9562}
9563
9564/**
9565 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9566 * @vsi: ptr to the VSI
9567 *
9568 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9569 * corresponding SW VSI structure and initializes num_queue_pairs for the
9570 * newly allocated VSI.
9571 *
9572 * Returns 0 on success or negative on failure
9573 **/
9574static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9575{
9576 int ret = -ENOENT;
9577 struct i40e_pf *pf = vsi->back;
9578
Alexander Duyck493fb302013-09-28 07:01:44 +00009579 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009580 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9581 vsi->seid);
9582 return -EEXIST;
9583 }
9584
9585 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009586 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009587 vsi->seid, vsi->base_vector);
9588 return -EEXIST;
9589 }
9590
Greg Rose90e04072014-03-06 08:59:57 +00009591 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009592 if (ret) {
9593 dev_info(&pf->pdev->dev,
9594 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9595 vsi->num_q_vectors, vsi->seid, ret);
9596 vsi->num_q_vectors = 0;
9597 goto vector_setup_out;
9598 }
9599
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009600 /* In Legacy mode, we do not have to get any other vector since we
9601 * piggyback on the misc/ICR0 for queue interrupts.
9602 */
9603 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9604 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009605 if (vsi->num_q_vectors)
9606 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9607 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009608 if (vsi->base_vector < 0) {
9609 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009610 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9611 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009612 i40e_vsi_free_q_vectors(vsi);
9613 ret = -ENOENT;
9614 goto vector_setup_out;
9615 }
9616
9617vector_setup_out:
9618 return ret;
9619}
9620
9621/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009622 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9623 * @vsi: pointer to the vsi.
9624 *
9625 * This re-allocates a vsi's queue resources.
9626 *
9627 * Returns pointer to the successfully allocated and configured VSI sw struct
9628 * on success, otherwise returns NULL on failure.
9629 **/
9630static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9631{
John Underwoodf5340392016-02-18 09:19:24 -08009632 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009633 u8 enabled_tc;
9634 int ret;
9635
John Underwoodf5340392016-02-18 09:19:24 -08009636 if (!vsi)
9637 return NULL;
9638
9639 pf = vsi->back;
9640
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009641 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9642 i40e_vsi_clear_rings(vsi);
9643
9644 i40e_vsi_free_arrays(vsi, false);
9645 i40e_set_num_rings_in_vsi(vsi);
9646 ret = i40e_vsi_alloc_arrays(vsi, false);
9647 if (ret)
9648 goto err_vsi;
9649
9650 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9651 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009652 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009653 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009654 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009655 goto err_vsi;
9656 }
9657 vsi->base_queue = ret;
9658
9659 /* Update the FW view of the VSI. Force a reset of TC and queue
9660 * layout configurations.
9661 */
9662 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9663 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9664 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9665 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9666
9667 /* assign it some queues */
9668 ret = i40e_alloc_rings(vsi);
9669 if (ret)
9670 goto err_rings;
9671
9672 /* map all of the rings to the q_vectors */
9673 i40e_vsi_map_rings_to_vectors(vsi);
9674 return vsi;
9675
9676err_rings:
9677 i40e_vsi_free_q_vectors(vsi);
9678 if (vsi->netdev_registered) {
9679 vsi->netdev_registered = false;
9680 unregister_netdev(vsi->netdev);
9681 free_netdev(vsi->netdev);
9682 vsi->netdev = NULL;
9683 }
9684 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9685err_vsi:
9686 i40e_vsi_clear(vsi);
9687 return NULL;
9688}
9689
9690/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009691 * i40e_vsi_setup - Set up a VSI by a given type
9692 * @pf: board private structure
9693 * @type: VSI type
9694 * @uplink_seid: the switch element to link to
9695 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9696 *
9697 * This allocates the sw VSI structure and its queue resources, then add a VSI
9698 * to the identified VEB.
9699 *
9700 * Returns pointer to the successfully allocated and configure VSI sw struct on
9701 * success, otherwise returns NULL on failure.
9702 **/
9703struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9704 u16 uplink_seid, u32 param1)
9705{
9706 struct i40e_vsi *vsi = NULL;
9707 struct i40e_veb *veb = NULL;
9708 int ret, i;
9709 int v_idx;
9710
9711 /* The requested uplink_seid must be either
9712 * - the PF's port seid
9713 * no VEB is needed because this is the PF
9714 * or this is a Flow Director special case VSI
9715 * - seid of an existing VEB
9716 * - seid of a VSI that owns an existing VEB
9717 * - seid of a VSI that doesn't own a VEB
9718 * a new VEB is created and the VSI becomes the owner
9719 * - seid of the PF VSI, which is what creates the first VEB
9720 * this is a special case of the previous
9721 *
9722 * Find which uplink_seid we were given and create a new VEB if needed
9723 */
9724 for (i = 0; i < I40E_MAX_VEB; i++) {
9725 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9726 veb = pf->veb[i];
9727 break;
9728 }
9729 }
9730
9731 if (!veb && uplink_seid != pf->mac_seid) {
9732
Mitch Williams505682c2014-05-20 08:01:37 +00009733 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009734 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9735 vsi = pf->vsi[i];
9736 break;
9737 }
9738 }
9739 if (!vsi) {
9740 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9741 uplink_seid);
9742 return NULL;
9743 }
9744
9745 if (vsi->uplink_seid == pf->mac_seid)
9746 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9747 vsi->tc_config.enabled_tc);
9748 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9749 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9750 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009751 if (veb) {
9752 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9753 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009754 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009755 return NULL;
9756 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009757 /* We come up by default in VEPA mode if SRIOV is not
9758 * already enabled, in which case we can't force VEPA
9759 * mode.
9760 */
9761 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9762 veb->bridge_mode = BRIDGE_MODE_VEPA;
9763 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9764 }
Neerav Parikh51616012015-02-06 08:52:14 +00009765 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009766 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009767 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9768 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9769 veb = pf->veb[i];
9770 }
9771 if (!veb) {
9772 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9773 return NULL;
9774 }
9775
9776 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9777 uplink_seid = veb->seid;
9778 }
9779
9780 /* get vsi sw struct */
9781 v_idx = i40e_vsi_mem_alloc(pf, type);
9782 if (v_idx < 0)
9783 goto err_alloc;
9784 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009785 if (!vsi)
9786 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009787 vsi->type = type;
9788 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9789
9790 if (type == I40E_VSI_MAIN)
9791 pf->lan_vsi = v_idx;
9792 else if (type == I40E_VSI_SRIOV)
9793 vsi->vf_id = param1;
9794 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009795 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9796 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009797 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009798 dev_info(&pf->pdev->dev,
9799 "failed to get tracking for %d queues for VSI %d err=%d\n",
9800 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009801 goto err_vsi;
9802 }
9803 vsi->base_queue = ret;
9804
9805 /* get a VSI from the hardware */
9806 vsi->uplink_seid = uplink_seid;
9807 ret = i40e_add_vsi(vsi);
9808 if (ret)
9809 goto err_vsi;
9810
9811 switch (vsi->type) {
9812 /* setup the netdev if needed */
9813 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009814 /* Apply relevant filters if a platform-specific mac
9815 * address was selected.
9816 */
9817 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9818 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9819 if (ret) {
9820 dev_warn(&pf->pdev->dev,
9821 "could not set up macaddr; err %d\n",
9822 ret);
9823 }
9824 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009825 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009826 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009827 ret = i40e_config_netdev(vsi);
9828 if (ret)
9829 goto err_netdev;
9830 ret = register_netdev(vsi->netdev);
9831 if (ret)
9832 goto err_netdev;
9833 vsi->netdev_registered = true;
9834 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009835#ifdef CONFIG_I40E_DCB
9836 /* Setup DCB netlink interface */
9837 i40e_dcbnl_setup(vsi);
9838#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009839 /* fall through */
9840
9841 case I40E_VSI_FDIR:
9842 /* set up vectors and rings if needed */
9843 ret = i40e_vsi_setup_vectors(vsi);
9844 if (ret)
9845 goto err_msix;
9846
9847 ret = i40e_alloc_rings(vsi);
9848 if (ret)
9849 goto err_rings;
9850
9851 /* map all of the rings to the q_vectors */
9852 i40e_vsi_map_rings_to_vectors(vsi);
9853
9854 i40e_vsi_reset_stats(vsi);
9855 break;
9856
9857 default:
9858 /* no netdev or rings for the other VSI types */
9859 break;
9860 }
9861
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009862 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9863 (vsi->type == I40E_VSI_VMDQ2)) {
9864 ret = i40e_vsi_config_rss(vsi);
9865 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009866 return vsi;
9867
9868err_rings:
9869 i40e_vsi_free_q_vectors(vsi);
9870err_msix:
9871 if (vsi->netdev_registered) {
9872 vsi->netdev_registered = false;
9873 unregister_netdev(vsi->netdev);
9874 free_netdev(vsi->netdev);
9875 vsi->netdev = NULL;
9876 }
9877err_netdev:
9878 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9879err_vsi:
9880 i40e_vsi_clear(vsi);
9881err_alloc:
9882 return NULL;
9883}
9884
9885/**
9886 * i40e_veb_get_bw_info - Query VEB BW information
9887 * @veb: the veb to query
9888 *
9889 * Query the Tx scheduler BW configuration data for given VEB
9890 **/
9891static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9892{
9893 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9894 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9895 struct i40e_pf *pf = veb->pf;
9896 struct i40e_hw *hw = &pf->hw;
9897 u32 tc_bw_max;
9898 int ret = 0;
9899 int i;
9900
9901 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9902 &bw_data, NULL);
9903 if (ret) {
9904 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009905 "query veb bw config failed, err %s aq_err %s\n",
9906 i40e_stat_str(&pf->hw, ret),
9907 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009908 goto out;
9909 }
9910
9911 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
9912 &ets_data, NULL);
9913 if (ret) {
9914 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009915 "query veb bw ets config failed, err %s aq_err %s\n",
9916 i40e_stat_str(&pf->hw, ret),
9917 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009918 goto out;
9919 }
9920
9921 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
9922 veb->bw_max_quanta = ets_data.tc_bw_max;
9923 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +00009924 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009925 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
9926 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
9927 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
9928 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
9929 veb->bw_tc_limit_credits[i] =
9930 le16_to_cpu(bw_data.tc_bw_limits[i]);
9931 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
9932 }
9933
9934out:
9935 return ret;
9936}
9937
9938/**
9939 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
9940 * @pf: board private structure
9941 *
9942 * On error: returns error code (negative)
9943 * On success: returns vsi index in PF (positive)
9944 **/
9945static int i40e_veb_mem_alloc(struct i40e_pf *pf)
9946{
9947 int ret = -ENOENT;
9948 struct i40e_veb *veb;
9949 int i;
9950
9951 /* Need to protect the allocation of switch elements at the PF level */
9952 mutex_lock(&pf->switch_mutex);
9953
9954 /* VEB list may be fragmented if VEB creation/destruction has
9955 * been happening. We can afford to do a quick scan to look
9956 * for any free slots in the list.
9957 *
9958 * find next empty veb slot, looping back around if necessary
9959 */
9960 i = 0;
9961 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
9962 i++;
9963 if (i >= I40E_MAX_VEB) {
9964 ret = -ENOMEM;
9965 goto err_alloc_veb; /* out of VEB slots! */
9966 }
9967
9968 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
9969 if (!veb) {
9970 ret = -ENOMEM;
9971 goto err_alloc_veb;
9972 }
9973 veb->pf = pf;
9974 veb->idx = i;
9975 veb->enabled_tc = 1;
9976
9977 pf->veb[i] = veb;
9978 ret = i;
9979err_alloc_veb:
9980 mutex_unlock(&pf->switch_mutex);
9981 return ret;
9982}
9983
9984/**
9985 * i40e_switch_branch_release - Delete a branch of the switch tree
9986 * @branch: where to start deleting
9987 *
9988 * This uses recursion to find the tips of the branch to be
9989 * removed, deleting until we get back to and can delete this VEB.
9990 **/
9991static void i40e_switch_branch_release(struct i40e_veb *branch)
9992{
9993 struct i40e_pf *pf = branch->pf;
9994 u16 branch_seid = branch->seid;
9995 u16 veb_idx = branch->idx;
9996 int i;
9997
9998 /* release any VEBs on this VEB - RECURSION */
9999 for (i = 0; i < I40E_MAX_VEB; i++) {
10000 if (!pf->veb[i])
10001 continue;
10002 if (pf->veb[i]->uplink_seid == branch->seid)
10003 i40e_switch_branch_release(pf->veb[i]);
10004 }
10005
10006 /* Release the VSIs on this VEB, but not the owner VSI.
10007 *
10008 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10009 * the VEB itself, so don't use (*branch) after this loop.
10010 */
Mitch Williams505682c2014-05-20 08:01:37 +000010011 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010012 if (!pf->vsi[i])
10013 continue;
10014 if (pf->vsi[i]->uplink_seid == branch_seid &&
10015 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10016 i40e_vsi_release(pf->vsi[i]);
10017 }
10018 }
10019
10020 /* There's one corner case where the VEB might not have been
10021 * removed, so double check it here and remove it if needed.
10022 * This case happens if the veb was created from the debugfs
10023 * commands and no VSIs were added to it.
10024 */
10025 if (pf->veb[veb_idx])
10026 i40e_veb_release(pf->veb[veb_idx]);
10027}
10028
10029/**
10030 * i40e_veb_clear - remove veb struct
10031 * @veb: the veb to remove
10032 **/
10033static void i40e_veb_clear(struct i40e_veb *veb)
10034{
10035 if (!veb)
10036 return;
10037
10038 if (veb->pf) {
10039 struct i40e_pf *pf = veb->pf;
10040
10041 mutex_lock(&pf->switch_mutex);
10042 if (pf->veb[veb->idx] == veb)
10043 pf->veb[veb->idx] = NULL;
10044 mutex_unlock(&pf->switch_mutex);
10045 }
10046
10047 kfree(veb);
10048}
10049
10050/**
10051 * i40e_veb_release - Delete a VEB and free its resources
10052 * @veb: the VEB being removed
10053 **/
10054void i40e_veb_release(struct i40e_veb *veb)
10055{
10056 struct i40e_vsi *vsi = NULL;
10057 struct i40e_pf *pf;
10058 int i, n = 0;
10059
10060 pf = veb->pf;
10061
10062 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010063 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010064 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10065 n++;
10066 vsi = pf->vsi[i];
10067 }
10068 }
10069 if (n != 1) {
10070 dev_info(&pf->pdev->dev,
10071 "can't remove VEB %d with %d VSIs left\n",
10072 veb->seid, n);
10073 return;
10074 }
10075
10076 /* move the remaining VSI to uplink veb */
10077 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10078 if (veb->uplink_seid) {
10079 vsi->uplink_seid = veb->uplink_seid;
10080 if (veb->uplink_seid == pf->mac_seid)
10081 vsi->veb_idx = I40E_NO_VEB;
10082 else
10083 vsi->veb_idx = veb->veb_idx;
10084 } else {
10085 /* floating VEB */
10086 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10087 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10088 }
10089
10090 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10091 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010092}
10093
10094/**
10095 * i40e_add_veb - create the VEB in the switch
10096 * @veb: the VEB to be instantiated
10097 * @vsi: the controlling VSI
10098 **/
10099static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10100{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010101 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010102 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010103 int ret;
10104
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010105 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010106 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010107 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010108
10109 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010110 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010111 dev_info(&pf->pdev->dev,
10112 "couldn't add VEB, err %s aq_err %s\n",
10113 i40e_stat_str(&pf->hw, ret),
10114 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010115 return -EPERM;
10116 }
10117
10118 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010119 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010120 &veb->stats_idx, NULL, NULL, NULL);
10121 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010122 dev_info(&pf->pdev->dev,
10123 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10124 i40e_stat_str(&pf->hw, ret),
10125 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010126 return -EPERM;
10127 }
10128 ret = i40e_veb_get_bw_info(veb);
10129 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010130 dev_info(&pf->pdev->dev,
10131 "couldn't get VEB bw info, err %s aq_err %s\n",
10132 i40e_stat_str(&pf->hw, ret),
10133 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10134 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010135 return -ENOENT;
10136 }
10137
10138 vsi->uplink_seid = veb->seid;
10139 vsi->veb_idx = veb->idx;
10140 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10141
10142 return 0;
10143}
10144
10145/**
10146 * i40e_veb_setup - Set up a VEB
10147 * @pf: board private structure
10148 * @flags: VEB setup flags
10149 * @uplink_seid: the switch element to link to
10150 * @vsi_seid: the initial VSI seid
10151 * @enabled_tc: Enabled TC bit-map
10152 *
10153 * This allocates the sw VEB structure and links it into the switch
10154 * It is possible and legal for this to be a duplicate of an already
10155 * existing VEB. It is also possible for both uplink and vsi seids
10156 * to be zero, in order to create a floating VEB.
10157 *
10158 * Returns pointer to the successfully allocated VEB sw struct on
10159 * success, otherwise returns NULL on failure.
10160 **/
10161struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10162 u16 uplink_seid, u16 vsi_seid,
10163 u8 enabled_tc)
10164{
10165 struct i40e_veb *veb, *uplink_veb = NULL;
10166 int vsi_idx, veb_idx;
10167 int ret;
10168
10169 /* if one seid is 0, the other must be 0 to create a floating relay */
10170 if ((uplink_seid == 0 || vsi_seid == 0) &&
10171 (uplink_seid + vsi_seid != 0)) {
10172 dev_info(&pf->pdev->dev,
10173 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10174 uplink_seid, vsi_seid);
10175 return NULL;
10176 }
10177
10178 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010179 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010180 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10181 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010182 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010183 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10184 vsi_seid);
10185 return NULL;
10186 }
10187
10188 if (uplink_seid && uplink_seid != pf->mac_seid) {
10189 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10190 if (pf->veb[veb_idx] &&
10191 pf->veb[veb_idx]->seid == uplink_seid) {
10192 uplink_veb = pf->veb[veb_idx];
10193 break;
10194 }
10195 }
10196 if (!uplink_veb) {
10197 dev_info(&pf->pdev->dev,
10198 "uplink seid %d not found\n", uplink_seid);
10199 return NULL;
10200 }
10201 }
10202
10203 /* get veb sw struct */
10204 veb_idx = i40e_veb_mem_alloc(pf);
10205 if (veb_idx < 0)
10206 goto err_alloc;
10207 veb = pf->veb[veb_idx];
10208 veb->flags = flags;
10209 veb->uplink_seid = uplink_seid;
10210 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10211 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10212
10213 /* create the VEB in the switch */
10214 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10215 if (ret)
10216 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010217 if (vsi_idx == pf->lan_vsi)
10218 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010219
10220 return veb;
10221
10222err_veb:
10223 i40e_veb_clear(veb);
10224err_alloc:
10225 return NULL;
10226}
10227
10228/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010229 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010230 * @pf: board private structure
10231 * @ele: element we are building info from
10232 * @num_reported: total number of elements
10233 * @printconfig: should we print the contents
10234 *
10235 * helper function to assist in extracting a few useful SEID values.
10236 **/
10237static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10238 struct i40e_aqc_switch_config_element_resp *ele,
10239 u16 num_reported, bool printconfig)
10240{
10241 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10242 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10243 u8 element_type = ele->element_type;
10244 u16 seid = le16_to_cpu(ele->seid);
10245
10246 if (printconfig)
10247 dev_info(&pf->pdev->dev,
10248 "type=%d seid=%d uplink=%d downlink=%d\n",
10249 element_type, seid, uplink_seid, downlink_seid);
10250
10251 switch (element_type) {
10252 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10253 pf->mac_seid = seid;
10254 break;
10255 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10256 /* Main VEB? */
10257 if (uplink_seid != pf->mac_seid)
10258 break;
10259 if (pf->lan_veb == I40E_NO_VEB) {
10260 int v;
10261
10262 /* find existing or else empty VEB */
10263 for (v = 0; v < I40E_MAX_VEB; v++) {
10264 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10265 pf->lan_veb = v;
10266 break;
10267 }
10268 }
10269 if (pf->lan_veb == I40E_NO_VEB) {
10270 v = i40e_veb_mem_alloc(pf);
10271 if (v < 0)
10272 break;
10273 pf->lan_veb = v;
10274 }
10275 }
10276
10277 pf->veb[pf->lan_veb]->seid = seid;
10278 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10279 pf->veb[pf->lan_veb]->pf = pf;
10280 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10281 break;
10282 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10283 if (num_reported != 1)
10284 break;
10285 /* This is immediately after a reset so we can assume this is
10286 * the PF's VSI
10287 */
10288 pf->mac_seid = uplink_seid;
10289 pf->pf_seid = downlink_seid;
10290 pf->main_vsi_seid = seid;
10291 if (printconfig)
10292 dev_info(&pf->pdev->dev,
10293 "pf_seid=%d main_vsi_seid=%d\n",
10294 pf->pf_seid, pf->main_vsi_seid);
10295 break;
10296 case I40E_SWITCH_ELEMENT_TYPE_PF:
10297 case I40E_SWITCH_ELEMENT_TYPE_VF:
10298 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10299 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10300 case I40E_SWITCH_ELEMENT_TYPE_PE:
10301 case I40E_SWITCH_ELEMENT_TYPE_PA:
10302 /* ignore these for now */
10303 break;
10304 default:
10305 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10306 element_type, seid);
10307 break;
10308 }
10309}
10310
10311/**
10312 * i40e_fetch_switch_configuration - Get switch config from firmware
10313 * @pf: board private structure
10314 * @printconfig: should we print the contents
10315 *
10316 * Get the current switch configuration from the device and
10317 * extract a few useful SEID values.
10318 **/
10319int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10320{
10321 struct i40e_aqc_get_switch_config_resp *sw_config;
10322 u16 next_seid = 0;
10323 int ret = 0;
10324 u8 *aq_buf;
10325 int i;
10326
10327 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10328 if (!aq_buf)
10329 return -ENOMEM;
10330
10331 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10332 do {
10333 u16 num_reported, num_total;
10334
10335 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10336 I40E_AQ_LARGE_BUF,
10337 &next_seid, NULL);
10338 if (ret) {
10339 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010340 "get switch config failed err %s aq_err %s\n",
10341 i40e_stat_str(&pf->hw, ret),
10342 i40e_aq_str(&pf->hw,
10343 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010344 kfree(aq_buf);
10345 return -ENOENT;
10346 }
10347
10348 num_reported = le16_to_cpu(sw_config->header.num_reported);
10349 num_total = le16_to_cpu(sw_config->header.num_total);
10350
10351 if (printconfig)
10352 dev_info(&pf->pdev->dev,
10353 "header: %d reported %d total\n",
10354 num_reported, num_total);
10355
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010356 for (i = 0; i < num_reported; i++) {
10357 struct i40e_aqc_switch_config_element_resp *ele =
10358 &sw_config->element[i];
10359
10360 i40e_setup_pf_switch_element(pf, ele, num_reported,
10361 printconfig);
10362 }
10363 } while (next_seid != 0);
10364
10365 kfree(aq_buf);
10366 return ret;
10367}
10368
10369/**
10370 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10371 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010372 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010373 *
10374 * Returns 0 on success, negative value on failure
10375 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010376static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010377{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010378 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010379 int ret;
10380
10381 /* find out what's out there already */
10382 ret = i40e_fetch_switch_configuration(pf, false);
10383 if (ret) {
10384 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010385 "couldn't fetch switch config, err %s aq_err %s\n",
10386 i40e_stat_str(&pf->hw, ret),
10387 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010388 return ret;
10389 }
10390 i40e_pf_reset_stats(pf);
10391
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010392 /* set the switch config bit for the whole device to
10393 * support limited promisc or true promisc
10394 * when user requests promisc. The default is limited
10395 * promisc.
10396 */
10397
10398 if ((pf->hw.pf_id == 0) &&
10399 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10400 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10401
10402 if (pf->hw.pf_id == 0) {
10403 u16 valid_flags;
10404
10405 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10406 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10407 NULL);
10408 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10409 dev_info(&pf->pdev->dev,
10410 "couldn't set switch config bits, err %s aq_err %s\n",
10411 i40e_stat_str(&pf->hw, ret),
10412 i40e_aq_str(&pf->hw,
10413 pf->hw.aq.asq_last_status));
10414 /* not a fatal problem, just keep going */
10415 }
10416 }
10417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010418 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010419 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010420 struct i40e_vsi *vsi = NULL;
10421 u16 uplink_seid;
10422
10423 /* Set up the PF VSI associated with the PF's main VSI
10424 * that is already in the HW switch
10425 */
10426 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10427 uplink_seid = pf->veb[pf->lan_veb]->seid;
10428 else
10429 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010430 if (pf->lan_vsi == I40E_NO_VSI)
10431 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10432 else if (reinit)
10433 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010434 if (!vsi) {
10435 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10436 i40e_fdir_teardown(pf);
10437 return -EAGAIN;
10438 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010439 } else {
10440 /* force a reset of TC and queue layout configurations */
10441 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010442
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010443 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10444 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10445 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10446 }
10447 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10448
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010449 i40e_fdir_sb_setup(pf);
10450
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010451 /* Setup static PF queue filter control settings */
10452 ret = i40e_setup_pf_filter_control(pf);
10453 if (ret) {
10454 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10455 ret);
10456 /* Failure here should not stop continuing other steps */
10457 }
10458
10459 /* enable RSS in the HW, even for only one queue, as the stack can use
10460 * the hash
10461 */
10462 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010463 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010464
10465 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010466 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010467 i40e_link_event(pf);
10468
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010469 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010470 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10471 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010472
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010473 i40e_ptp_init(pf);
10474
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010475 return ret;
10476}
10477
10478/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010479 * i40e_determine_queue_usage - Work out queue distribution
10480 * @pf: board private structure
10481 **/
10482static void i40e_determine_queue_usage(struct i40e_pf *pf)
10483{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010484 int queues_left;
10485
10486 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010487#ifdef I40E_FCOE
10488 pf->num_fcoe_qps = 0;
10489#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010490
10491 /* Find the max queues to be put into basic use. We'll always be
10492 * using TC0, whether or not DCB is running, and TC0 will get the
10493 * big RSS set.
10494 */
10495 queues_left = pf->hw.func_caps.num_tx_qp;
10496
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010497 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010498 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499 /* one qp for PF, no queues for anything else */
10500 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010501 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010502
10503 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010504 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010505 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010506#ifdef I40E_FCOE
10507 I40E_FLAG_FCOE_ENABLED |
10508#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010509 I40E_FLAG_FD_SB_ENABLED |
10510 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010511 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010512 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010513 I40E_FLAG_SRIOV_ENABLED |
10514 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010515 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10516 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010517 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010518 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010519 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010520 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010521 queues_left -= pf->num_lan_qps;
10522
10523 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010524 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010525#ifdef I40E_FCOE
10526 I40E_FLAG_FCOE_ENABLED |
10527#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010528 I40E_FLAG_FD_SB_ENABLED |
10529 I40E_FLAG_FD_ATR_ENABLED |
10530 I40E_FLAG_DCB_ENABLED |
10531 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010532 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010533 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010534 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010535 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010536 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10537 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010538 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10539 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010540 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10541 num_online_cpus());
10542 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10543 pf->hw.func_caps.num_tx_qp);
10544
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010545 queues_left -= pf->num_lan_qps;
10546 }
10547
Vasu Dev38e00432014-08-01 13:27:03 -070010548#ifdef I40E_FCOE
10549 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10550 if (I40E_DEFAULT_FCOE <= queues_left) {
10551 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10552 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10553 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10554 } else {
10555 pf->num_fcoe_qps = 0;
10556 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10557 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10558 }
10559
10560 queues_left -= pf->num_fcoe_qps;
10561 }
10562
10563#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010564 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10565 if (queues_left > 1) {
10566 queues_left -= 1; /* save 1 queue for FD */
10567 } else {
10568 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10569 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10570 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010571 }
10572
10573 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10574 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010575 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10576 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010577 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10578 }
10579
10580 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10581 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10582 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10583 (queues_left / pf->num_vmdq_qps));
10584 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10585 }
10586
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010587 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010588 dev_dbg(&pf->pdev->dev,
10589 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10590 pf->hw.func_caps.num_tx_qp,
10591 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010592 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10593 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10594 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010595#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010596 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010597#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010598}
10599
10600/**
10601 * i40e_setup_pf_filter_control - Setup PF static filter control
10602 * @pf: PF to be setup
10603 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010604 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010605 * settings. If PE/FCoE are enabled then it will also set the per PF
10606 * based filter sizes required for them. It also enables Flow director,
10607 * ethertype and macvlan type filter settings for the pf.
10608 *
10609 * Returns 0 on success, negative on failure
10610 **/
10611static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10612{
10613 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10614
10615 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10616
10617 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010618 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010619 settings->enable_fdir = true;
10620
10621 /* Ethtype and MACVLAN filters enabled for PF */
10622 settings->enable_ethtype = true;
10623 settings->enable_macvlan = true;
10624
10625 if (i40e_set_filter_control(&pf->hw, settings))
10626 return -ENOENT;
10627
10628 return 0;
10629}
10630
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010631#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010632#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010633static void i40e_print_features(struct i40e_pf *pf)
10634{
10635 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010636 char *buf;
10637 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010638
Joe Perches3b195842015-12-03 04:20:57 -080010639 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10640 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010641 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010642
Joe Perches3b195842015-12-03 04:20:57 -080010643 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010644#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010645 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010646#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010647 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010648 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010649 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010650 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010651 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010652 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010653 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010654 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010655 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10656 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010657 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010658 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010659 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010660 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010661 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010662 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010663 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010664#ifdef I40E_FCOE
10665 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010666 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010667#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010668 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010669 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010670 else
Joe Perches3b195842015-12-03 04:20:57 -080010671 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010672
Joe Perches3b195842015-12-03 04:20:57 -080010673 dev_info(&pf->pdev->dev, "%s\n", buf);
10674 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010675 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010676}
10677
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010678/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010679 * i40e_get_platform_mac_addr - get platform-specific MAC address
10680 *
10681 * @pdev: PCI device information struct
10682 * @pf: board private structure
10683 *
10684 * Look up the MAC address in Open Firmware on systems that support it,
10685 * and use IDPROM on SPARC if no OF address is found. On return, the
10686 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10687 * has been selected.
10688 **/
10689static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10690{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010691 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010692 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010693 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010694}
10695
10696/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010697 * i40e_probe - Device initialization routine
10698 * @pdev: PCI device information struct
10699 * @ent: entry in i40e_pci_tbl
10700 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010701 * i40e_probe initializes a PF identified by a pci_dev structure.
10702 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010703 * and a hardware reset occur.
10704 *
10705 * Returns 0 on success, negative on failure
10706 **/
10707static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10708{
Catherine Sullivane8278452015-02-06 08:52:08 +000010709 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010710 struct i40e_pf *pf;
10711 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010712 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010713 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010714 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010715 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010716 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010717 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010718 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010719
10720 err = pci_enable_device_mem(pdev);
10721 if (err)
10722 return err;
10723
10724 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010725 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010726 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010727 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10728 if (err) {
10729 dev_err(&pdev->dev,
10730 "DMA configuration failed: 0x%x\n", err);
10731 goto err_dma;
10732 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010733 }
10734
10735 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010736 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010737 if (err) {
10738 dev_info(&pdev->dev,
10739 "pci_request_selected_regions failed %d\n", err);
10740 goto err_pci_reg;
10741 }
10742
10743 pci_enable_pcie_error_reporting(pdev);
10744 pci_set_master(pdev);
10745
10746 /* Now that we have a PCI connection, we need to do the
10747 * low level device setup. This is primarily setting up
10748 * the Admin Queue structures and then querying for the
10749 * device's current profile information.
10750 */
10751 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10752 if (!pf) {
10753 err = -ENOMEM;
10754 goto err_pf_alloc;
10755 }
10756 pf->next_vsi = 0;
10757 pf->pdev = pdev;
10758 set_bit(__I40E_DOWN, &pf->state);
10759
10760 hw = &pf->hw;
10761 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010762
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010763 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10764 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010765
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010766 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010767 if (!hw->hw_addr) {
10768 err = -EIO;
10769 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10770 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010771 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010772 goto err_ioremap;
10773 }
10774 hw->vendor_id = pdev->vendor;
10775 hw->device_id = pdev->device;
10776 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10777 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10778 hw->subsystem_device_id = pdev->subsystem_device;
10779 hw->bus.device = PCI_SLOT(pdev->devfn);
10780 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010781 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010782
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010783 /* set up the locks for the AQ, do this only once in probe
10784 * and destroy them only once in remove
10785 */
10786 mutex_init(&hw->aq.asq_mutex);
10787 mutex_init(&hw->aq.arq_mutex);
10788
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010789 pf->msg_enable = netif_msg_init(debug,
10790 NETIF_MSG_DRV |
10791 NETIF_MSG_PROBE |
10792 NETIF_MSG_LINK);
10793 if (debug < -1)
10794 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010795
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010796 /* do a special CORER for clearing PXE mode once at init */
10797 if (hw->revision_id == 0 &&
10798 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10799 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10800 i40e_flush(hw);
10801 msleep(200);
10802 pf->corer_count++;
10803
10804 i40e_clear_pxe_mode(hw);
10805 }
10806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010807 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010808 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010809 err = i40e_pf_reset(hw);
10810 if (err) {
10811 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10812 goto err_pf_reset;
10813 }
10814 pf->pfr_count++;
10815
10816 hw->aq.num_arq_entries = I40E_AQ_LEN;
10817 hw->aq.num_asq_entries = I40E_AQ_LEN;
10818 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10819 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10820 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010821
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010822 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010823 "%s-%s:misc",
10824 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010825
10826 err = i40e_init_shared_code(hw);
10827 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010828 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10829 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010830 goto err_pf_reset;
10831 }
10832
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010833 /* set up a default setting for link flow control */
10834 pf->hw.fc.requested_mode = I40E_FC_NONE;
10835
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010836 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010837 if (err) {
10838 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10839 dev_info(&pdev->dev,
10840 "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");
10841 else
10842 dev_info(&pdev->dev,
10843 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10844
10845 goto err_pf_reset;
10846 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010847
Shannon Nelson6dec1012015-09-28 14:12:30 -040010848 /* provide nvm, fw, api versions */
10849 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10850 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10851 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10852 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010853
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010854 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10855 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010856 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010857 "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");
10858 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10859 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010860 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010861 "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 +000010862
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010863 i40e_verify_eeprom(pf);
10864
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010865 /* Rev 0 hardware was never productized */
10866 if (hw->revision_id < 1)
10867 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");
10868
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010869 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010870 err = i40e_get_capabilities(pf);
10871 if (err)
10872 goto err_adminq_setup;
10873
10874 err = i40e_sw_init(pf);
10875 if (err) {
10876 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10877 goto err_sw_init;
10878 }
10879
10880 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10881 hw->func_caps.num_rx_qp,
10882 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10883 if (err) {
10884 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10885 goto err_init_lan_hmc;
10886 }
10887
10888 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10889 if (err) {
10890 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10891 err = -ENOENT;
10892 goto err_configure_lan_hmc;
10893 }
10894
Neerav Parikhb686ece2014-12-14 01:55:11 +000010895 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10896 * Ignore error return codes because if it was already disabled via
10897 * hardware settings this will fail
10898 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010899 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010900 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10901 i40e_aq_stop_lldp(hw, true, NULL);
10902 }
10903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010904 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010905 /* allow a platform config to override the HW addr */
10906 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000010907 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010908 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
10909 err = -EIO;
10910 goto err_mac_addr;
10911 }
10912 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000010913 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000010914 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
10915 if (is_valid_ether_addr(hw->mac.port_addr))
10916 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070010917#ifdef I40E_FCOE
10918 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
10919 if (err)
10920 dev_info(&pdev->dev,
10921 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
10922 if (!is_valid_ether_addr(hw->mac.san_addr)) {
10923 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
10924 hw->mac.san_addr);
10925 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
10926 }
10927 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
10928#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010929
10930 pci_set_drvdata(pdev, pf);
10931 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010932#ifdef CONFIG_I40E_DCB
10933 err = i40e_init_pf_dcb(pf);
10934 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000010935 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070010936 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000010937 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010938 }
10939#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010940
10941 /* set up periodic task facility */
10942 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
10943 pf->service_timer_period = HZ;
10944
10945 INIT_WORK(&pf->service_task, i40e_service_task);
10946 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
10947 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010948
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010949 /* NVM bit on means WoL disabled for the port */
10950 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080010951 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010952 pf->wol_en = false;
10953 else
10954 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000010955 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
10956
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010957 /* set up the main switch operations */
10958 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040010959 err = i40e_init_interrupt_scheme(pf);
10960 if (err)
10961 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010962
Mitch Williams505682c2014-05-20 08:01:37 +000010963 /* The number of VSIs reported by the FW is the minimum guaranteed
10964 * to us; HW supports far more and we share the remaining pool with
10965 * the other PFs. We allocate space for more than the guarantee with
10966 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010967 */
Mitch Williams505682c2014-05-20 08:01:37 +000010968 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
10969 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
10970 else
10971 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
10972
10973 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080010974 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
10975 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000010976 if (!pf->vsi) {
10977 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010978 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000010979 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010980
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040010981#ifdef CONFIG_PCI_IOV
10982 /* prep for VF support */
10983 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10984 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
10985 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
10986 if (pci_num_vf(pdev))
10987 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
10988 }
10989#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010990 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010991 if (err) {
10992 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
10993 goto err_vsis;
10994 }
Helin Zhang58fc3262015-10-01 14:37:38 -040010995
10996 /* Make sure flow control is set according to current settings */
10997 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
10998 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
10999 dev_dbg(&pf->pdev->dev,
11000 "Set fc with err %s aq_err %s on get_phy_cap\n",
11001 i40e_stat_str(hw, err),
11002 i40e_aq_str(hw, hw->aq.asq_last_status));
11003 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11004 dev_dbg(&pf->pdev->dev,
11005 "Set fc with err %s aq_err %s on set_phy_config\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_UPDATE)
11009 dev_dbg(&pf->pdev->dev,
11010 "Set fc with err %s aq_err %s on get_link_info\n",
11011 i40e_stat_str(hw, err),
11012 i40e_aq_str(hw, hw->aq.asq_last_status));
11013
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011014 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011015 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011016 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11017 i40e_vsi_open(pf->vsi[i]);
11018 break;
11019 }
11020 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011021
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011022 /* The driver only wants link up/down and module qualification
11023 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011024 */
11025 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011026 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011027 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011028 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011029 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011030 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11031 i40e_stat_str(&pf->hw, err),
11032 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011033
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011034 /* Reconfigure hardware for allowing smaller MSS in the case
11035 * of TSO, so that we avoid the MDD being fired and causing
11036 * a reset in the case of small MSS+TSO.
11037 */
11038 val = rd32(hw, I40E_REG_MSS);
11039 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11040 val &= ~I40E_REG_MSS_MIN_MASK;
11041 val |= I40E_64BYTE_MSS;
11042 wr32(hw, I40E_REG_MSS, val);
11043 }
11044
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011045 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011046 msleep(75);
11047 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11048 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011049 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11050 i40e_stat_str(&pf->hw, err),
11051 i40e_aq_str(&pf->hw,
11052 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011053 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011054 /* The main driver is (mostly) up and happy. We need to set this state
11055 * before setting up the misc vector or we get a race and the vector
11056 * ends up disabled forever.
11057 */
11058 clear_bit(__I40E_DOWN, &pf->state);
11059
11060 /* In case of MSIX we are going to setup the misc vector right here
11061 * to handle admin queue events etc. In case of legacy and MSI
11062 * the misc functionality and queue processing is combined in
11063 * the same vector and that gets setup at open.
11064 */
11065 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11066 err = i40e_setup_misc_vector(pf);
11067 if (err) {
11068 dev_info(&pdev->dev,
11069 "setup of misc vector failed: %d\n", err);
11070 goto err_vsis;
11071 }
11072 }
11073
Greg Rosedf805f62014-04-04 04:43:16 +000011074#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011075 /* prep for VF support */
11076 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011077 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11078 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011079 /* disable link interrupts for VFs */
11080 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11081 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11082 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11083 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011084
11085 if (pci_num_vf(pdev)) {
11086 dev_info(&pdev->dev,
11087 "Active VFs found, allocating resources.\n");
11088 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11089 if (err)
11090 dev_info(&pdev->dev,
11091 "Error %d allocating resources for existing VFs\n",
11092 err);
11093 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011094 }
Greg Rosedf805f62014-04-04 04:43:16 +000011095#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011096
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011097 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11098 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11099 pf->num_iwarp_msix,
11100 I40E_IWARP_IRQ_PILE_ID);
11101 if (pf->iwarp_base_vector < 0) {
11102 dev_info(&pdev->dev,
11103 "failed to get tracking for %d vectors for IWARP err=%d\n",
11104 pf->num_iwarp_msix, pf->iwarp_base_vector);
11105 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11106 }
11107 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011108
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011109 i40e_dbg_pf_init(pf);
11110
11111 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011112 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011113
11114 /* since everything's happy, start the service_task timer */
11115 mod_timer(&pf->service_timer,
11116 round_jiffies(jiffies + pf->service_timer_period));
11117
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011118 /* add this PF to client device list and launch a client service task */
11119 err = i40e_lan_add_device(pf);
11120 if (err)
11121 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11122 err);
11123
Vasu Dev38e00432014-08-01 13:27:03 -070011124#ifdef I40E_FCOE
11125 /* create FCoE interface */
11126 i40e_fcoe_vsi_setup(pf);
11127
11128#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011129#define PCI_SPEED_SIZE 8
11130#define PCI_WIDTH_SIZE 8
11131 /* Devices on the IOSF bus do not have this information
11132 * and will report PCI Gen 1 x 1 by default so don't bother
11133 * checking them.
11134 */
11135 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11136 char speed[PCI_SPEED_SIZE] = "Unknown";
11137 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011138
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011139 /* Get the negotiated link width and speed from PCI config
11140 * space
11141 */
11142 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11143 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011144
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011145 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011146
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011147 switch (hw->bus.speed) {
11148 case i40e_bus_speed_8000:
11149 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11150 case i40e_bus_speed_5000:
11151 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11152 case i40e_bus_speed_2500:
11153 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11154 default:
11155 break;
11156 }
11157 switch (hw->bus.width) {
11158 case i40e_bus_width_pcie_x8:
11159 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11160 case i40e_bus_width_pcie_x4:
11161 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11162 case i40e_bus_width_pcie_x2:
11163 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11164 case i40e_bus_width_pcie_x1:
11165 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11166 default:
11167 break;
11168 }
11169
11170 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11171 speed, width);
11172
11173 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11174 hw->bus.speed < i40e_bus_speed_8000) {
11175 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11176 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11177 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011178 }
11179
Catherine Sullivane8278452015-02-06 08:52:08 +000011180 /* get the requested speeds from the fw */
11181 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11182 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011183 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11184 i40e_stat_str(&pf->hw, err),
11185 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011186 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11187
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011188 /* get the supported phy types from the fw */
11189 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11190 if (err)
11191 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11192 i40e_stat_str(&pf->hw, err),
11193 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11194 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11195
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011196 /* Add a filter to drop all Flow control frames from any VSI from being
11197 * transmitted. By doing so we stop a malicious VF from sending out
11198 * PAUSE or PFC frames and potentially controlling traffic for other
11199 * PF/VF VSIs.
11200 * The FW can still send Flow control frames if enabled.
11201 */
11202 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11203 pf->main_vsi_seid);
11204
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011205 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11206 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11207 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11208
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011209 /* print a string summarizing features */
11210 i40e_print_features(pf);
11211
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011212 return 0;
11213
11214 /* Unwind what we've done if something failed in the setup */
11215err_vsis:
11216 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011217 i40e_clear_interrupt_scheme(pf);
11218 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011219err_switch_setup:
11220 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011221 del_timer_sync(&pf->service_timer);
11222err_mac_addr:
11223err_configure_lan_hmc:
11224 (void)i40e_shutdown_lan_hmc(hw);
11225err_init_lan_hmc:
11226 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011227err_sw_init:
11228err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011229err_pf_reset:
11230 iounmap(hw->hw_addr);
11231err_ioremap:
11232 kfree(pf);
11233err_pf_alloc:
11234 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011235 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011236err_pci_reg:
11237err_dma:
11238 pci_disable_device(pdev);
11239 return err;
11240}
11241
11242/**
11243 * i40e_remove - Device removal routine
11244 * @pdev: PCI device information struct
11245 *
11246 * i40e_remove is called by the PCI subsystem to alert the driver
11247 * that is should release a PCI device. This could be caused by a
11248 * Hot-Plug event, or because the driver is going to be removed from
11249 * memory.
11250 **/
11251static void i40e_remove(struct pci_dev *pdev)
11252{
11253 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011254 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011255 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011256 int i;
11257
11258 i40e_dbg_pf_exit(pf);
11259
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011260 i40e_ptp_stop(pf);
11261
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011262 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011263 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11264 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011265
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011266 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011267 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011268 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011269 if (pf->service_timer.data)
11270 del_timer_sync(&pf->service_timer);
11271 if (pf->service_task.func)
11272 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011273
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011274 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11275 i40e_free_vfs(pf);
11276 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11277 }
11278
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011279 i40e_fdir_teardown(pf);
11280
11281 /* If there is a switch structure or any orphans, remove them.
11282 * This will leave only the PF's VSI remaining.
11283 */
11284 for (i = 0; i < I40E_MAX_VEB; i++) {
11285 if (!pf->veb[i])
11286 continue;
11287
11288 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11289 pf->veb[i]->uplink_seid == 0)
11290 i40e_switch_branch_release(pf->veb[i]);
11291 }
11292
11293 /* Now we can shutdown the PF's VSI, just before we kill
11294 * adminq and hmc.
11295 */
11296 if (pf->vsi[pf->lan_vsi])
11297 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11298
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011299 /* remove attached clients */
11300 ret_code = i40e_lan_del_device(pf);
11301 if (ret_code) {
11302 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11303 ret_code);
11304 }
11305
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011306 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011307 if (hw->hmc.hmc_obj) {
11308 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011309 if (ret_code)
11310 dev_warn(&pdev->dev,
11311 "Failed to destroy the HMC resources: %d\n",
11312 ret_code);
11313 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011314
11315 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011316 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011317
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011318 /* destroy the locks only once, here */
11319 mutex_destroy(&hw->aq.arq_mutex);
11320 mutex_destroy(&hw->aq.asq_mutex);
11321
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011322 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11323 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011324 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011325 if (pf->vsi[i]) {
11326 i40e_vsi_clear_rings(pf->vsi[i]);
11327 i40e_vsi_clear(pf->vsi[i]);
11328 pf->vsi[i] = NULL;
11329 }
11330 }
11331
11332 for (i = 0; i < I40E_MAX_VEB; i++) {
11333 kfree(pf->veb[i]);
11334 pf->veb[i] = NULL;
11335 }
11336
11337 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011338 kfree(pf->vsi);
11339
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011340 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011341 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011342 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011343
11344 pci_disable_pcie_error_reporting(pdev);
11345 pci_disable_device(pdev);
11346}
11347
11348/**
11349 * i40e_pci_error_detected - warning that something funky happened in PCI land
11350 * @pdev: PCI device information struct
11351 *
11352 * Called to warn that something happened and the error handling steps
11353 * are in progress. Allows the driver to quiesce things, be ready for
11354 * remediation.
11355 **/
11356static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11357 enum pci_channel_state error)
11358{
11359 struct i40e_pf *pf = pci_get_drvdata(pdev);
11360
11361 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11362
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011363 if (!pf) {
11364 dev_info(&pdev->dev,
11365 "Cannot recover - error happened during device probe\n");
11366 return PCI_ERS_RESULT_DISCONNECT;
11367 }
11368
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011369 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011370 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11371 rtnl_lock();
11372 i40e_prep_for_reset(pf);
11373 rtnl_unlock();
11374 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011375
11376 /* Request a slot reset */
11377 return PCI_ERS_RESULT_NEED_RESET;
11378}
11379
11380/**
11381 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11382 * @pdev: PCI device information struct
11383 *
11384 * Called to find if the driver can work with the device now that
11385 * the pci slot has been reset. If a basic connection seems good
11386 * (registers are readable and have sane content) then return a
11387 * happy little PCI_ERS_RESULT_xxx.
11388 **/
11389static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11390{
11391 struct i40e_pf *pf = pci_get_drvdata(pdev);
11392 pci_ers_result_t result;
11393 int err;
11394 u32 reg;
11395
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011396 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011397 if (pci_enable_device_mem(pdev)) {
11398 dev_info(&pdev->dev,
11399 "Cannot re-enable PCI device after reset.\n");
11400 result = PCI_ERS_RESULT_DISCONNECT;
11401 } else {
11402 pci_set_master(pdev);
11403 pci_restore_state(pdev);
11404 pci_save_state(pdev);
11405 pci_wake_from_d3(pdev, false);
11406
11407 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11408 if (reg == 0)
11409 result = PCI_ERS_RESULT_RECOVERED;
11410 else
11411 result = PCI_ERS_RESULT_DISCONNECT;
11412 }
11413
11414 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11415 if (err) {
11416 dev_info(&pdev->dev,
11417 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11418 err);
11419 /* non-fatal, continue */
11420 }
11421
11422 return result;
11423}
11424
11425/**
11426 * i40e_pci_error_resume - restart operations after PCI error recovery
11427 * @pdev: PCI device information struct
11428 *
11429 * Called to allow the driver to bring things back up after PCI error
11430 * and/or reset recovery has finished.
11431 **/
11432static void i40e_pci_error_resume(struct pci_dev *pdev)
11433{
11434 struct i40e_pf *pf = pci_get_drvdata(pdev);
11435
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011436 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011437 if (test_bit(__I40E_SUSPENDED, &pf->state))
11438 return;
11439
11440 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011441 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011442 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011443}
11444
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011445/**
11446 * i40e_shutdown - PCI callback for shutting down
11447 * @pdev: PCI device information struct
11448 **/
11449static void i40e_shutdown(struct pci_dev *pdev)
11450{
11451 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011452 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011453
11454 set_bit(__I40E_SUSPENDED, &pf->state);
11455 set_bit(__I40E_DOWN, &pf->state);
11456 rtnl_lock();
11457 i40e_prep_for_reset(pf);
11458 rtnl_unlock();
11459
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011460 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11461 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11462
Catherine Sullivan02b42492015-07-10 19:35:59 -040011463 del_timer_sync(&pf->service_timer);
11464 cancel_work_sync(&pf->service_task);
11465 i40e_fdir_teardown(pf);
11466
11467 rtnl_lock();
11468 i40e_prep_for_reset(pf);
11469 rtnl_unlock();
11470
11471 wr32(hw, I40E_PFPM_APM,
11472 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11473 wr32(hw, I40E_PFPM_WUFC,
11474 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11475
Shannon Nelsone1477582015-02-21 06:44:33 +000011476 i40e_clear_interrupt_scheme(pf);
11477
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011478 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011479 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011480 pci_set_power_state(pdev, PCI_D3hot);
11481 }
11482}
11483
11484#ifdef CONFIG_PM
11485/**
11486 * i40e_suspend - PCI callback for moving to D3
11487 * @pdev: PCI device information struct
11488 **/
11489static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11490{
11491 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011492 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011493 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011494
11495 set_bit(__I40E_SUSPENDED, &pf->state);
11496 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011497
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011498 rtnl_lock();
11499 i40e_prep_for_reset(pf);
11500 rtnl_unlock();
11501
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011502 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11503 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11504
Greg Roseb33d3b72016-05-16 10:26:37 -070011505 i40e_stop_misc_vector(pf);
11506
Greg Rose059ff692016-05-16 10:26:38 -070011507 retval = pci_save_state(pdev);
11508 if (retval)
11509 return retval;
11510
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011511 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011512 pci_set_power_state(pdev, PCI_D3hot);
11513
Greg Rose059ff692016-05-16 10:26:38 -070011514 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011515}
11516
11517/**
11518 * i40e_resume - PCI callback for waking up from D3
11519 * @pdev: PCI device information struct
11520 **/
11521static int i40e_resume(struct pci_dev *pdev)
11522{
11523 struct i40e_pf *pf = pci_get_drvdata(pdev);
11524 u32 err;
11525
11526 pci_set_power_state(pdev, PCI_D0);
11527 pci_restore_state(pdev);
11528 /* pci_restore_state() clears dev->state_saves, so
11529 * call pci_save_state() again to restore it.
11530 */
11531 pci_save_state(pdev);
11532
11533 err = pci_enable_device_mem(pdev);
11534 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011535 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011536 return err;
11537 }
11538 pci_set_master(pdev);
11539
11540 /* no wakeup events while running */
11541 pci_wake_from_d3(pdev, false);
11542
11543 /* handling the reset will rebuild the device state */
11544 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11545 clear_bit(__I40E_DOWN, &pf->state);
11546 rtnl_lock();
11547 i40e_reset_and_rebuild(pf, false);
11548 rtnl_unlock();
11549 }
11550
11551 return 0;
11552}
11553
11554#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011555static const struct pci_error_handlers i40e_err_handler = {
11556 .error_detected = i40e_pci_error_detected,
11557 .slot_reset = i40e_pci_error_slot_reset,
11558 .resume = i40e_pci_error_resume,
11559};
11560
11561static struct pci_driver i40e_driver = {
11562 .name = i40e_driver_name,
11563 .id_table = i40e_pci_tbl,
11564 .probe = i40e_probe,
11565 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011566#ifdef CONFIG_PM
11567 .suspend = i40e_suspend,
11568 .resume = i40e_resume,
11569#endif
11570 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011571 .err_handler = &i40e_err_handler,
11572 .sriov_configure = i40e_pci_sriov_configure,
11573};
11574
11575/**
11576 * i40e_init_module - Driver registration routine
11577 *
11578 * i40e_init_module is the first routine called when the driver is
11579 * loaded. All it does is register with the PCI subsystem.
11580 **/
11581static int __init i40e_init_module(void)
11582{
11583 pr_info("%s: %s - version %s\n", i40e_driver_name,
11584 i40e_driver_string, i40e_driver_version_str);
11585 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011586
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011587 /* we will see if single thread per module is enough for now,
11588 * it can't be any worse than using the system workqueue which
11589 * was already single threaded
11590 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011591 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11592 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011593 if (!i40e_wq) {
11594 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11595 return -ENOMEM;
11596 }
11597
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011598 i40e_dbg_init();
11599 return pci_register_driver(&i40e_driver);
11600}
11601module_init(i40e_init_module);
11602
11603/**
11604 * i40e_exit_module - Driver exit cleanup routine
11605 *
11606 * i40e_exit_module is called just before the driver is removed
11607 * from memory.
11608 **/
11609static void __exit i40e_exit_module(void)
11610{
11611 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011612 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011613 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011614}
11615module_exit(i40e_exit_module);