blob: 2d91274f9f62c633ea03ee685f38e2022eb43bf5 [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{
Jacob Kellercbebb852016-10-05 09:30:40 -07001201 /* If we have a PVID, always operate in VLAN mode */
1202 if (vsi->info.pvid)
1203 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001204
Jacob Kellercbebb852016-10-05 09:30:40 -07001205 /* We need to operate in VLAN mode whenever we have any filters with
1206 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1207 * time, incurring search cost repeatedly. However, we can notice two
1208 * things:
1209 *
1210 * 1) the only place where we can gain a VLAN filter is in
1211 * i40e_add_filter.
1212 *
1213 * 2) the only place where filters are actually removed is in
1214 * i40e_vsi_sync_filters_subtask.
1215 *
1216 * Thus, we can simply use a boolean value, has_vlan_filters which we
1217 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1218 * we have to perform the full search after deleting filters in
1219 * i40e_vsi_sync_filters_subtask, but we already have to search
1220 * filters here and can perform the check at the same time. This
1221 * results in avoiding embedding a loop for VLAN mode inside another
1222 * loop over all the filters, and should maintain correctness as noted
1223 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001224 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001225 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001226}
1227
1228/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001229 * i40e_add_filter - Add a mac/vlan filter to the VSI
1230 * @vsi: the VSI to be searched
1231 * @macaddr: the MAC address
1232 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001233 *
1234 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001235 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001236 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001237 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001238 **/
1239struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001240 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001241{
1242 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001243 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001244
1245 if (!vsi || !macaddr)
1246 return NULL;
1247
Kiran Patilf6bd0962016-06-20 09:10:34 -07001248 /* Do not allow broadcast filter to be added since broadcast filter
1249 * is added as part of add VSI for any newly created VSI except
1250 * FDIR VSI
1251 */
1252 if (is_broadcast_ether_addr(macaddr))
1253 return NULL;
1254
Jacob Keller1bc87e82016-10-05 09:30:31 -07001255 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001256 if (!f) {
1257 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1258 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001259 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001260
Jacob Kellercbebb852016-10-05 09:30:40 -07001261 /* Update the boolean indicating if we need to function in
1262 * VLAN mode.
1263 */
1264 if (vlan >= 0)
1265 vsi->has_vlan_filter = true;
1266
Greg Rose9a173902014-05-22 06:32:02 +00001267 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001268 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001269 /* If we're in overflow promisc mode, set the state directly
1270 * to failed, so we don't bother to try sending the filter
1271 * to the hardware.
1272 */
1273 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1274 f->state = I40E_FILTER_FAILED;
1275 else
1276 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001277 INIT_HLIST_NODE(&f->hlist);
1278
1279 key = i40e_addr_to_hkey(macaddr);
1280 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001281
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001282 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1283 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1284 }
1285
Jacob Keller1bc87e82016-10-05 09:30:31 -07001286 /* If we're asked to add a filter that has been marked for removal, it
1287 * is safe to simply restore it to active state. __i40e_del_filter
1288 * will have simply deleted any filters which were previously marked
1289 * NEW or FAILED, so if it is currently marked REMOVE it must have
1290 * previously been ACTIVE. Since we haven't yet run the sync filters
1291 * task, just restore this filter to the ACTIVE state so that the
1292 * sync task leaves it in place
1293 */
1294 if (f->state == I40E_FILTER_REMOVE)
1295 f->state = I40E_FILTER_ACTIVE;
1296
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001297 return f;
1298}
1299
1300/**
Jacob Keller290d2552016-10-05 09:30:36 -07001301 * __i40e_del_filter - Remove a specific filter from the VSI
1302 * @vsi: VSI to remove from
1303 * @f: the filter to remove from the list
1304 *
1305 * This function should be called instead of i40e_del_filter only if you know
1306 * the exact filter you will remove already, such as via i40e_find_filter or
1307 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001308 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001309 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001310 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001311 * ANOTHER NOTE: This function MUST be called from within the context of
1312 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1313 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001314 **/
Jacob Keller290d2552016-10-05 09:30:36 -07001315static void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001316{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001317 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001318 return;
1319
Jacob Keller1bc87e82016-10-05 09:30:31 -07001320 if ((f->state == I40E_FILTER_FAILED) ||
1321 (f->state == I40E_FILTER_NEW)) {
1322 /* this one never got added by the FW. Just remove it,
1323 * no need to sync anything.
1324 */
Jacob Keller278e7d02016-10-05 09:30:37 -07001325 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001326 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001327 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001328 f->state = I40E_FILTER_REMOVE;
1329 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1330 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001331 }
1332}
1333
1334/**
Jacob Keller290d2552016-10-05 09:30:36 -07001335 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1336 * @vsi: the VSI to be searched
1337 * @macaddr: the MAC address
1338 * @vlan: the VLAN
1339 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001340 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001341 * being held.
1342 * ANOTHER NOTE: This function MUST be called from within the context of
1343 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1344 * instead of list_for_each_entry().
1345 **/
1346void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1347{
1348 struct i40e_mac_filter *f;
1349
1350 if (!vsi || !macaddr)
1351 return;
1352
1353 f = i40e_find_filter(vsi, macaddr, vlan);
1354 __i40e_del_filter(vsi, f);
1355}
1356
1357/**
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001358 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1359 * @vsi: the VSI to be searched
1360 * @macaddr: the mac address to be filtered
1361 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001362 * Goes through all the macvlan filters and adds a macvlan filter for each
1363 * unique vlan that already exists. If a PVID has been assigned, instead only
1364 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001365 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001366 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001367 **/
1368struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi,
1369 const u8 *macaddr)
1370{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001371 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001372 struct hlist_node *h;
1373 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001374
1375 if (vsi->info.pvid)
1376 return i40e_add_filter(vsi, macaddr,
1377 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001378
Jacob Keller278e7d02016-10-05 09:30:37 -07001379 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001380 if (f->state == I40E_FILTER_REMOVE)
1381 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001382 add = i40e_add_filter(vsi, macaddr, f->vlan);
1383 if (!add)
1384 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001385 }
1386
Jacob Keller5feb3d72016-10-05 09:30:34 -07001387 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001388}
1389
1390/**
1391 * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
1392 * @vsi: the VSI to be searched
1393 * @macaddr: the mac address to be removed
1394 *
1395 * Removes a given MAC address from a VSI, regardless of VLAN
1396 *
1397 * Returns 0 for success, or error
1398 **/
1399int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, const u8 *macaddr)
1400{
Jacob Keller278e7d02016-10-05 09:30:37 -07001401 struct i40e_mac_filter *f;
1402 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001403 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001404 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001405
Jacob Keller278e7d02016-10-05 09:30:37 -07001406 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1407 "Missing mac_filter_hash_lock\n");
1408 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001409 if (ether_addr_equal(macaddr, f->macaddr)) {
1410 __i40e_del_filter(vsi, f);
1411 found = true;
1412 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001413 }
Jacob Keller290d2552016-10-05 09:30:36 -07001414
1415 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001416 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001417 else
1418 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001419}
1420
1421/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001422 * i40e_set_mac - NDO callback to set mac address
1423 * @netdev: network interface device structure
1424 * @p: pointer to an address structure
1425 *
1426 * Returns 0 on success, negative on failure
1427 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001428#ifdef I40E_FCOE
1429int i40e_set_mac(struct net_device *netdev, void *p)
1430#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001431static int i40e_set_mac(struct net_device *netdev, void *p)
Vasu Dev38e00432014-08-01 13:27:03 -07001432#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001433{
1434 struct i40e_netdev_priv *np = netdev_priv(netdev);
1435 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001436 struct i40e_pf *pf = vsi->back;
1437 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001438 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001439
1440 if (!is_valid_ether_addr(addr->sa_data))
1441 return -EADDRNOTAVAIL;
1442
Shannon Nelson30650cc2014-07-29 04:01:50 +00001443 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1444 netdev_info(netdev, "already using mac address %pM\n",
1445 addr->sa_data);
1446 return 0;
1447 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001448
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001449 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1450 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1451 return -EADDRNOTAVAIL;
1452
Shannon Nelson30650cc2014-07-29 04:01:50 +00001453 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1454 netdev_info(netdev, "returning to hw mac address %pM\n",
1455 hw->mac.addr);
1456 else
1457 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1458
Jacob Keller278e7d02016-10-05 09:30:37 -07001459 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001460 i40e_del_mac_all_vlan(vsi, netdev->dev_addr);
1461 i40e_put_mac_in_vlan(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001462 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001463 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001464 if (vsi->type == I40E_VSI_MAIN) {
1465 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001466
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001467 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001468 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001469 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001470 if (ret)
1471 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1472 i40e_stat_str(hw, ret),
1473 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001474 }
1475
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001476 /* schedule our worker thread which will take care of
1477 * applying the new filter changes
1478 */
1479 i40e_service_event_schedule(vsi->back);
1480 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001481}
1482
1483/**
1484 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1485 * @vsi: the VSI being setup
1486 * @ctxt: VSI context structure
1487 * @enabled_tc: Enabled TCs bitmap
1488 * @is_add: True if called before Add VSI
1489 *
1490 * Setup VSI queue mapping for enabled traffic classes.
1491 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001492#ifdef I40E_FCOE
1493void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1494 struct i40e_vsi_context *ctxt,
1495 u8 enabled_tc,
1496 bool is_add)
1497#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001498static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1499 struct i40e_vsi_context *ctxt,
1500 u8 enabled_tc,
1501 bool is_add)
Vasu Dev38e00432014-08-01 13:27:03 -07001502#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001503{
1504 struct i40e_pf *pf = vsi->back;
1505 u16 sections = 0;
1506 u8 netdev_tc = 0;
1507 u16 numtc = 0;
1508 u16 qcount;
1509 u8 offset;
1510 u16 qmap;
1511 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001512 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001513
1514 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1515 offset = 0;
1516
1517 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1518 /* Find numtc from enabled TC bitmap */
1519 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001520 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001521 numtc++;
1522 }
1523 if (!numtc) {
1524 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1525 numtc = 1;
1526 }
1527 } else {
1528 /* At least TC0 is enabled in case of non-DCB case */
1529 numtc = 1;
1530 }
1531
1532 vsi->tc_config.numtc = numtc;
1533 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001534 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001535 qcount = vsi->alloc_queue_pairs;
1536
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001537 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04001538 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001539
1540 /* Setup queue offset/count for all TCs for given VSI */
1541 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1542 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001543 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001544 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001545 int pow, num_qps;
1546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001547 switch (vsi->type) {
1548 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001549 qcount = min_t(int, pf->alloc_rss_size,
1550 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001551 break;
Vasu Dev38e00432014-08-01 13:27:03 -07001552#ifdef I40E_FCOE
1553 case I40E_VSI_FCOE:
1554 qcount = num_tc_qps;
1555 break;
1556#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001557 case I40E_VSI_FDIR:
1558 case I40E_VSI_SRIOV:
1559 case I40E_VSI_VMDQ2:
1560 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001561 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001562 WARN_ON(i != 0);
1563 break;
1564 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001565 vsi->tc_config.tc_info[i].qoffset = offset;
1566 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001567
Shannon Nelson1e200e42015-02-27 09:15:24 +00001568 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001569 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001570 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001571 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001572 pow++;
1573 num_qps >>= 1;
1574 }
1575
1576 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1577 qmap =
1578 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1579 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1580
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001581 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001582 } else {
1583 /* TC is not enabled so set the offset to
1584 * default queue and allocate one queue
1585 * for the given TC.
1586 */
1587 vsi->tc_config.tc_info[i].qoffset = 0;
1588 vsi->tc_config.tc_info[i].qcount = 1;
1589 vsi->tc_config.tc_info[i].netdev_tc = 0;
1590
1591 qmap = 0;
1592 }
1593 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1594 }
1595
1596 /* Set actual Tx/Rx queue pairs */
1597 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001598 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1599 if (vsi->req_queue_pairs > 0)
1600 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001601 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001602 vsi->num_queue_pairs = pf->num_lan_msix;
1603 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001604
1605 /* Scheduler section valid can only be set for ADD VSI */
1606 if (is_add) {
1607 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1608
1609 ctxt->info.up_enable_bits = enabled_tc;
1610 }
1611 if (vsi->type == I40E_VSI_SRIOV) {
1612 ctxt->info.mapping_flags |=
1613 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1614 for (i = 0; i < vsi->num_queue_pairs; i++)
1615 ctxt->info.queue_mapping[i] =
1616 cpu_to_le16(vsi->base_queue + i);
1617 } else {
1618 ctxt->info.mapping_flags |=
1619 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1620 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1621 }
1622 ctxt->info.valid_sections |= cpu_to_le16(sections);
1623}
1624
1625/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001626 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1627 * @netdev: the netdevice
1628 * @addr: address to add
1629 *
1630 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1631 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1632 */
1633static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1634{
1635 struct i40e_netdev_priv *np = netdev_priv(netdev);
1636 struct i40e_vsi *vsi = np->vsi;
1637 struct i40e_mac_filter *f;
1638
1639 if (i40e_is_vsi_in_vlan(vsi))
1640 f = i40e_put_mac_in_vlan(vsi, addr);
1641 else
1642 f = i40e_add_filter(vsi, addr, I40E_VLAN_ANY);
1643
1644 if (f)
1645 return 0;
1646 else
1647 return -ENOMEM;
1648}
1649
1650/**
1651 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1652 * @netdev: the netdevice
1653 * @addr: address to add
1654 *
1655 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1656 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1657 */
1658static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1659{
1660 struct i40e_netdev_priv *np = netdev_priv(netdev);
1661 struct i40e_vsi *vsi = np->vsi;
1662
1663 if (i40e_is_vsi_in_vlan(vsi))
1664 i40e_del_mac_all_vlan(vsi, addr);
1665 else
1666 i40e_del_filter(vsi, addr, I40E_VLAN_ANY);
1667
1668 return 0;
1669}
1670
1671/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001672 * i40e_set_rx_mode - NDO callback to set the netdev filters
1673 * @netdev: network interface device structure
1674 **/
Vasu Dev38e00432014-08-01 13:27:03 -07001675#ifdef I40E_FCOE
1676void i40e_set_rx_mode(struct net_device *netdev)
1677#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001678static void i40e_set_rx_mode(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07001679#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001680{
1681 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001682 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001683
Jacob Keller278e7d02016-10-05 09:30:37 -07001684 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001685
Jacob Keller6622f5c2016-10-05 09:30:32 -07001686 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1687 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001688
Jacob Keller278e7d02016-10-05 09:30:37 -07001689 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001690
1691 /* check for other flag changes */
1692 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1693 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1694 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1695 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001696
1697 /* schedule our worker thread which will take care of
1698 * applying the new filter changes
1699 */
1700 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001701}
1702
1703/**
Jacob Keller4a2ce272016-10-05 09:30:38 -07001704 * i40e_undo_filter_entries - Undo the changes made to MAC filter entries
1705 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001706 * @from: Pointer to list which contains MAC filter entries - changes to
1707 * those entries needs to be undone.
1708 *
Jacob Keller4a2ce272016-10-05 09:30:38 -07001709 * MAC filter entries from list were slated to be sent to firmware, either for
1710 * addition or deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001711 **/
Jacob Keller4a2ce272016-10-05 09:30:38 -07001712static void i40e_undo_filter_entries(struct i40e_vsi *vsi,
1713 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001714{
Jacob Keller278e7d02016-10-05 09:30:37 -07001715 struct i40e_mac_filter *f;
1716 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001717
Jacob Keller278e7d02016-10-05 09:30:37 -07001718 hlist_for_each_entry_safe(f, h, from, hlist) {
1719 u64 key = i40e_addr_to_hkey(f->macaddr);
1720
Kiran Patil21659032015-09-30 14:09:03 -04001721 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001722 hlist_del(&f->hlist);
1723 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001724 }
1725}
1726
1727/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001728 * i40e_update_filter_state - Update filter state based on return data
1729 * from firmware
1730 * @count: Number of filters added
1731 * @add_list: return data from fw
1732 * @head: pointer to first filter in current batch
1733 * @aq_err: status from fw
Kiran Patil21659032015-09-30 14:09:03 -04001734 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001735 * MAC filter entries from list were slated to be added to device. Returns
1736 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001737 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001738static int
1739i40e_update_filter_state(int count,
1740 struct i40e_aqc_add_macvlan_element_data *add_list,
1741 struct i40e_mac_filter *add_head, int aq_err)
Kiran Patil21659032015-09-30 14:09:03 -04001742{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001743 int retval = 0;
1744 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001745
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001746
1747 if (!aq_err) {
1748 retval = count;
1749 /* Everything's good, mark all filters active. */
1750 for (i = 0; i < count ; i++) {
1751 add_head->state = I40E_FILTER_ACTIVE;
Jacob Keller278e7d02016-10-05 09:30:37 -07001752 add_head = hlist_entry(add_head->hlist.next,
1753 typeof(struct i40e_mac_filter),
1754 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001755 }
1756 } else if (aq_err == I40E_AQ_RC_ENOSPC) {
1757 /* Device ran out of filter space. Check the return value
1758 * for each filter to see which ones are active.
1759 */
1760 for (i = 0; i < count ; i++) {
1761 if (add_list[i].match_method ==
1762 I40E_AQC_MM_ERR_NO_RES) {
1763 add_head->state = I40E_FILTER_FAILED;
1764 } else {
1765 add_head->state = I40E_FILTER_ACTIVE;
1766 retval++;
1767 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001768 add_head = hlist_entry(add_head->hlist.next,
1769 typeof(struct i40e_mac_filter),
1770 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001771 }
1772 } else {
1773 /* Some other horrible thing happened, fail all filters */
1774 retval = 0;
1775 for (i = 0; i < count ; i++) {
1776 add_head->state = I40E_FILTER_FAILED;
Jacob Keller278e7d02016-10-05 09:30:37 -07001777 add_head = hlist_entry(add_head->hlist.next,
1778 typeof(struct i40e_mac_filter),
1779 hlist);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001780 }
Kiran Patil21659032015-09-30 14:09:03 -04001781 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001782 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001783}
1784
1785/**
Jacob Keller00936312016-10-05 09:30:41 -07001786 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1787 * @vsi: ptr to the VSI
1788 * @vsi_name: name to display in messages
1789 * @list: the list of filters to send to firmware
1790 * @num_del: the number of filters to delete
1791 * @retval: Set to -EIO on failure to delete
1792 *
1793 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1794 * *retval instead of a return value so that success does not force ret_val to
1795 * be set to 0. This ensures that a sequence of calls to this function
1796 * preserve the previous value of *retval on successful delete.
1797 */
1798static
1799void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1800 struct i40e_aqc_remove_macvlan_element_data *list,
1801 int num_del, int *retval)
1802{
1803 struct i40e_hw *hw = &vsi->back->hw;
1804 i40e_status aq_ret;
1805 int aq_err;
1806
1807 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1808 aq_err = hw->aq.asq_last_status;
1809
1810 /* Explicitly ignore and do not report when firmware returns ENOENT */
1811 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1812 *retval = -EIO;
1813 dev_info(&vsi->back->pdev->dev,
1814 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1815 vsi_name, i40e_stat_str(hw, aq_ret),
1816 i40e_aq_str(hw, aq_err));
1817 }
1818}
1819
1820/**
1821 * i40e_aqc_add_filters - Request firmware to add a set of filters
1822 * @vsi: ptr to the VSI
1823 * @vsi_name: name to display in messages
1824 * @list: the list of filters to send to firmware
1825 * @add_head: Position in the add hlist
1826 * @num_add: the number of filters to add
1827 * @promisc_change: set to true on exit if promiscuous mode was forced on
1828 *
1829 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1830 * promisc_changed to true if the firmware has run out of space for more
1831 * filters.
1832 */
1833static
1834void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1835 struct i40e_aqc_add_macvlan_element_data *list,
1836 struct i40e_mac_filter *add_head,
1837 int num_add, bool *promisc_changed)
1838{
1839 struct i40e_hw *hw = &vsi->back->hw;
1840 i40e_status aq_ret;
1841 int aq_err, fcnt;
1842
1843 aq_ret = i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
1844 aq_err = hw->aq.asq_last_status;
1845 fcnt = i40e_update_filter_state(num_add, list, add_head, aq_ret);
1846 vsi->active_filters += fcnt;
1847
1848 if (fcnt != num_add) {
1849 *promisc_changed = true;
1850 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
1851 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
1852 dev_warn(&vsi->back->pdev->dev,
1853 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1854 i40e_aq_str(hw, aq_err),
1855 vsi_name);
1856 }
1857}
1858
1859/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001860 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1861 * @vsi: ptr to the VSI
1862 *
1863 * Push any outstanding VSI filter changes through the AdminQ.
1864 *
1865 * Returns 0 or error value
1866 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001867int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001868{
Jacob Keller278e7d02016-10-05 09:30:37 -07001869 struct hlist_head tmp_add_list, tmp_del_list;
Alan Brady84f5ca62016-10-05 09:30:39 -07001870 struct i40e_mac_filter *f, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001871 struct i40e_hw *hw = &vsi->back->hw;
Alan Brady84f5ca62016-10-05 09:30:39 -07001872 unsigned int vlan_any_filters = 0;
1873 unsigned int non_vlan_filters = 0;
1874 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001875 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001876 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001877 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001878 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001879 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001880 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001881 struct i40e_pf *pf;
1882 int num_add = 0;
1883 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001884 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001885 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001886 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07001887 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001888
1889 /* empty array typed pointers, kcalloc later */
1890 struct i40e_aqc_add_macvlan_element_data *add_list;
1891 struct i40e_aqc_remove_macvlan_element_data *del_list;
1892
1893 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1894 usleep_range(1000, 2000);
1895 pf = vsi->back;
1896
1897 if (vsi->netdev) {
1898 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1899 vsi->current_netdev_flags = vsi->netdev->flags;
1900 }
1901
Jacob Keller278e7d02016-10-05 09:30:37 -07001902 INIT_HLIST_HEAD(&tmp_add_list);
1903 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04001904
Shannon Nelson2d1de822016-05-16 10:26:44 -07001905 if (vsi->type == I40E_VSI_SRIOV)
1906 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
1907 else if (vsi->type != I40E_VSI_MAIN)
1908 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
1909
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001910 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1911 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1912
Jacob Keller278e7d02016-10-05 09:30:37 -07001913 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001914 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07001915 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001916 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001917 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07001918 hash_del(&f->hlist);
1919 hlist_add_head(&f->hlist, &tmp_del_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001920 vsi->active_filters--;
Alan Brady84f5ca62016-10-05 09:30:39 -07001921
1922 /* Avoid counting removed filters */
1923 continue;
Kiran Patil21659032015-09-30 14:09:03 -04001924 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001925 if (f->state == I40E_FILTER_NEW) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001926 hash_del(&f->hlist);
1927 hlist_add_head(&f->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001928 }
Alan Brady84f5ca62016-10-05 09:30:39 -07001929
1930 /* Count the number of each type of filter we have
1931 * remaining, ignoring any filters we're about to
1932 * delete.
1933 */
1934 if (f->vlan > 0)
1935 vlan_filters++;
1936 else if (!f->vlan)
1937 non_vlan_filters++;
1938 else
1939 vlan_any_filters++;
1940 }
1941
1942 /* We should never have VLAN=-1 filters at the same time as we
1943 * have either VLAN=0 or VLAN>0 filters, so warn about this
1944 * case here to help catch any issues.
1945 */
1946 WARN_ON(vlan_any_filters && (vlan_filters + non_vlan_filters));
1947
1948 /* If we only have VLAN=0 filters remaining, and don't have
1949 * any other VLAN filters, we need to convert these VLAN=0
1950 * filters into VLAN=-1 (I40E_VLAN_ANY) so that we operate
1951 * correctly in non-VLAN mode and receive all traffic tagged
1952 * or untagged.
1953 */
1954 if (non_vlan_filters && !vlan_filters) {
1955 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
1956 hlist) {
1957 /* Only replace VLAN=0 filters */
1958 if (f->vlan)
1959 continue;
1960
1961 /* Allocate a replacement element */
1962 add_head = kzalloc(sizeof(*add_head),
1963 GFP_KERNEL);
1964 if (!add_head)
1965 goto err_no_memory_locked;
1966
1967 /* Copy the filter, with new state and VLAN */
1968 *add_head = *f;
1969 add_head->state = I40E_FILTER_NEW;
1970 add_head->vlan = I40E_VLAN_ANY;
1971
1972 /* Move the replacement to the add list */
1973 INIT_HLIST_NODE(&add_head->hlist);
1974 hlist_add_head(&add_head->hlist,
1975 &tmp_add_list);
1976
1977 /* Move the original to the delete list */
1978 f->state = I40E_FILTER_REMOVE;
1979 hash_del(&f->hlist);
1980 hlist_add_head(&f->hlist, &tmp_del_list);
1981 vsi->active_filters--;
1982 }
1983
1984 /* Also update any filters on the tmp_add list */
1985 hlist_for_each_entry(f, &tmp_add_list, hlist) {
1986 if (!f->vlan)
1987 f->vlan = I40E_VLAN_ANY;
1988 }
1989 add_head = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04001990 }
Jacob Keller278e7d02016-10-05 09:30:37 -07001991 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001992 }
1993
1994 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07001995 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001996 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04001997 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001998 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08001999 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002000 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002001 if (!del_list)
2002 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002003
Jacob Keller278e7d02016-10-05 09:30:37 -07002004 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002005 cmd_flags = 0;
2006
2007 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002008 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002009 if (f->vlan == I40E_VLAN_ANY) {
2010 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002011 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002012 } else {
2013 del_list[num_del].vlan_tag =
2014 cpu_to_le16((u16)(f->vlan));
2015 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002016
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002017 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2018 del_list[num_del].flags = cmd_flags;
2019 num_del++;
2020
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002021 /* flush a full buffer */
2022 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002023 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2024 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002025 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002026 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002027 }
Kiran Patil21659032015-09-30 14:09:03 -04002028 /* Release memory for MAC filter entries which were
2029 * synced up with HW.
2030 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002031 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002032 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002033 }
Kiran Patil21659032015-09-30 14:09:03 -04002034
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002035 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002036 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2037 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002038 }
2039
2040 kfree(del_list);
2041 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002042 }
2043
Jacob Kellercbebb852016-10-05 09:30:40 -07002044 /* After finishing notifying firmware of the deleted filters, update
2045 * the cached value of vsi->has_vlan_filter. Note that we are safe to
2046 * use just !!vlan_filters here because if we only have VLAN=0 (that
2047 * is, non_vlan_filters) these will all be converted to VLAN=-1 in the
2048 * logic above already so this value would still be correct.
2049 */
2050 vsi->has_vlan_filter = !!vlan_filters;
2051
Jacob Keller278e7d02016-10-05 09:30:37 -07002052 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002053 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002054 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002055 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002056 list_size = filter_list_len *
2057 sizeof(struct i40e_aqc_add_macvlan_element_data);
2058 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002059 if (!add_list)
2060 goto err_no_memory;
2061
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002062 num_add = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07002063 hlist_for_each_entry(f, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002064 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2065 &vsi->state)) {
2066 f->state = I40E_FILTER_FAILED;
2067 continue;
2068 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002069 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002070 if (num_add == 0)
2071 add_head = f;
2072 cmd_flags = 0;
Greg Rose9a173902014-05-22 06:32:02 +00002073 ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002074 if (f->vlan == I40E_VLAN_ANY) {
2075 add_list[num_add].vlan_tag = 0;
2076 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2077 } else {
2078 add_list[num_add].vlan_tag =
2079 cpu_to_le16((u16)(f->vlan));
2080 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002081 add_list[num_add].queue_number = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002082 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002083 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2084 num_add++;
2085
2086 /* flush a full buffer */
2087 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002088 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2089 add_head, num_add,
2090 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002091 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002092 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002093 }
2094 }
2095 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002096 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2097 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002099 /* Now move all of the filters from the temp add list back to
2100 * the VSI's list.
2101 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002102 spin_lock_bh(&vsi->mac_filter_hash_lock);
2103 hlist_for_each_entry_safe(f, h, &tmp_add_list, hlist) {
2104 u64 key = i40e_addr_to_hkey(f->macaddr);
2105
2106 hlist_del(&f->hlist);
2107 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002108 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002109 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002110 kfree(add_list);
2111 add_list = NULL;
2112 }
2113
2114 /* Check to see if we can drop out of overflow promiscuous mode. */
2115 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
2116 (vsi->active_filters < vsi->promisc_threshold)) {
2117 int failed_count = 0;
2118 /* See if we have any failed filters. We can't drop out of
2119 * promiscuous until these have all been deleted.
2120 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002121 spin_lock_bh(&vsi->mac_filter_hash_lock);
2122 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002123 if (f->state == I40E_FILTER_FAILED)
2124 failed_count++;
2125 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002126 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002127 if (!failed_count) {
2128 dev_info(&pf->pdev->dev,
2129 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2130 vsi_name);
2131 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2132 promisc_changed = true;
2133 vsi->promisc_threshold = 0;
2134 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002135 }
2136
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002137 /* if the VF is not trusted do not do promisc */
2138 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2139 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2140 goto out;
2141 }
2142
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002143 /* check for changes in promiscuous modes */
2144 if (changed_flags & IFF_ALLMULTI) {
2145 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002146
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002147 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002148 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2149 vsi->seid,
2150 cur_multipromisc,
2151 NULL);
2152 if (aq_ret) {
2153 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002154 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002155 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002156 "set multi promisc failed on %s, err %s aq_err %s\n",
2157 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002158 i40e_stat_str(hw, aq_ret),
2159 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002160 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002161 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002162 if ((changed_flags & IFF_PROMISC) ||
2163 (promisc_changed &&
2164 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002165 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002166
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002167 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2168 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2169 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002170 if ((vsi->type == I40E_VSI_MAIN) &&
2171 (pf->lan_veb != I40E_NO_VEB) &&
2172 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002173 /* set defport ON for Main VSI instead of true promisc
2174 * this way we will get all unicast/multicast and VLAN
2175 * promisc behavior but will not get VF or VMDq traffic
2176 * replicated on the Main VSI.
2177 */
2178 if (pf->cur_promisc != cur_promisc) {
2179 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002180 if (cur_promisc)
2181 aq_ret =
2182 i40e_aq_set_default_vsi(hw,
2183 vsi->seid,
2184 NULL);
2185 else
2186 aq_ret =
2187 i40e_aq_clear_default_vsi(hw,
2188 vsi->seid,
2189 NULL);
2190 if (aq_ret) {
2191 retval = i40e_aq_rc_to_posix(aq_ret,
2192 hw->aq.asq_last_status);
2193 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002194 "Set default VSI failed on %s, err %s, aq_err %s\n",
2195 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002196 i40e_stat_str(hw, aq_ret),
2197 i40e_aq_str(hw,
2198 hw->aq.asq_last_status));
2199 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002200 }
2201 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002202 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002203 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002204 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002205 cur_promisc, NULL,
2206 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002207 if (aq_ret) {
2208 retval =
2209 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002210 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002211 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002212 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2213 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002214 i40e_stat_str(hw, aq_ret),
2215 i40e_aq_str(hw,
2216 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002217 }
2218 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002219 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002220 vsi->seid,
2221 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002222 if (aq_ret) {
2223 retval =
2224 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002225 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002226 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002227 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2228 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002229 i40e_stat_str(hw, aq_ret),
2230 i40e_aq_str(hw,
2231 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002232 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002233 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002234 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2235 vsi->seid,
2236 cur_promisc, NULL);
2237 if (aq_ret) {
2238 retval = i40e_aq_rc_to_posix(aq_ret,
2239 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002240 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002241 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002242 i40e_stat_str(hw, aq_ret),
2243 i40e_aq_str(hw,
2244 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002245 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002246 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002247out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002248 /* if something went wrong then set the changed flag so we try again */
2249 if (retval)
2250 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2251
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002252 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002253 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002254
2255err_no_memory:
2256 /* Restore elements on the temporary add and delete lists */
2257 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002258err_no_memory_locked:
Jacob Keller4a2ce272016-10-05 09:30:38 -07002259 i40e_undo_filter_entries(vsi, &tmp_del_list);
2260 i40e_undo_filter_entries(vsi, &tmp_add_list);
2261 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2262
2263 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2264 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2265 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002266}
2267
2268/**
2269 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2270 * @pf: board private structure
2271 **/
2272static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2273{
2274 int v;
2275
2276 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2277 return;
2278 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2279
Mitch Williams505682c2014-05-20 08:01:37 +00002280 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002281 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002282 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2283 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2284
2285 if (ret) {
2286 /* come back and try again later */
2287 pf->flags |= I40E_FLAG_FILTER_SYNC;
2288 break;
2289 }
2290 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002291 }
2292}
2293
2294/**
2295 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2296 * @netdev: network interface device structure
2297 * @new_mtu: new value for maximum frame size
2298 *
2299 * Returns 0 on success, negative on failure
2300 **/
2301static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2302{
2303 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002304 struct i40e_vsi *vsi = np->vsi;
2305
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002306 netdev_info(netdev, "changing MTU from %d to %d\n",
2307 netdev->mtu, new_mtu);
2308 netdev->mtu = new_mtu;
2309 if (netif_running(netdev))
2310 i40e_vsi_reinit_locked(vsi);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06002311 i40e_notify_client_of_l2_param_changes(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002312 return 0;
2313}
2314
2315/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002316 * i40e_ioctl - Access the hwtstamp interface
2317 * @netdev: network interface device structure
2318 * @ifr: interface request data
2319 * @cmd: ioctl command
2320 **/
2321int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2322{
2323 struct i40e_netdev_priv *np = netdev_priv(netdev);
2324 struct i40e_pf *pf = np->vsi->back;
2325
2326 switch (cmd) {
2327 case SIOCGHWTSTAMP:
2328 return i40e_ptp_get_ts_config(pf, ifr);
2329 case SIOCSHWTSTAMP:
2330 return i40e_ptp_set_ts_config(pf, ifr);
2331 default:
2332 return -EOPNOTSUPP;
2333 }
2334}
2335
2336/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002337 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2338 * @vsi: the vsi being adjusted
2339 **/
2340void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2341{
2342 struct i40e_vsi_context ctxt;
2343 i40e_status ret;
2344
2345 if ((vsi->info.valid_sections &
2346 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2347 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2348 return; /* already enabled */
2349
2350 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2351 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2352 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2353
2354 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002355 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002356 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2357 if (ret) {
2358 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002359 "update vlan stripping failed, err %s aq_err %s\n",
2360 i40e_stat_str(&vsi->back->hw, ret),
2361 i40e_aq_str(&vsi->back->hw,
2362 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002363 }
2364}
2365
2366/**
2367 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2368 * @vsi: the vsi being adjusted
2369 **/
2370void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2371{
2372 struct i40e_vsi_context ctxt;
2373 i40e_status ret;
2374
2375 if ((vsi->info.valid_sections &
2376 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2377 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2378 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2379 return; /* already disabled */
2380
2381 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2382 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2383 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2384
2385 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002386 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002387 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2388 if (ret) {
2389 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002390 "update vlan stripping failed, err %s aq_err %s\n",
2391 i40e_stat_str(&vsi->back->hw, ret),
2392 i40e_aq_str(&vsi->back->hw,
2393 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002394 }
2395}
2396
2397/**
2398 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2399 * @netdev: network interface to be adjusted
2400 * @features: netdev features to test if VLAN offload is enabled or not
2401 **/
2402static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2403{
2404 struct i40e_netdev_priv *np = netdev_priv(netdev);
2405 struct i40e_vsi *vsi = np->vsi;
2406
2407 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2408 i40e_vlan_stripping_enable(vsi);
2409 else
2410 i40e_vlan_stripping_disable(vsi);
2411}
2412
2413/**
2414 * i40e_vsi_add_vlan - Add vsi membership for given vlan
2415 * @vsi: the vsi being configured
2416 * @vid: vlan id to be added (0 = untagged only , -1 = any)
2417 **/
2418int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
2419{
Jacob Keller278e7d02016-10-05 09:30:37 -07002420 struct i40e_mac_filter *f, *add_f, *del_f;
2421 struct hlist_node *h;
2422 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002423
Kiran Patil21659032015-09-30 14:09:03 -04002424 /* Locked once because all functions invoked below iterates list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07002425 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002426
Jacob Keller1bc87e82016-10-05 09:30:31 -07002427 if (vsi->netdev) {
2428 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002429 if (!add_f) {
2430 dev_info(&vsi->back->pdev->dev,
2431 "Could not add vlan filter %d for %pM\n",
2432 vid, vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002433 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002434 return -ENOMEM;
2435 }
2436 }
2437
Jacob Keller278e7d02016-10-05 09:30:37 -07002438 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002439 if (f->state == I40E_FILTER_REMOVE)
2440 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002441 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002442 if (!add_f) {
2443 dev_info(&vsi->back->pdev->dev,
2444 "Could not add vlan filter %d for %pM\n",
2445 vid, f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002446 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002447 return -ENOMEM;
2448 }
2449 }
2450
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002451 /* Now if we add a vlan tag, make sure to check if it is the first
2452 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
2453 * with 0, so we now accept untagged and specified tagged traffic
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002454 * (and not all tags along with untagged)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002455 */
Jacob Keller290d2552016-10-05 09:30:36 -07002456 if (vid > 0 && vsi->netdev) {
2457 del_f = i40e_find_filter(vsi, vsi->netdev->dev_addr,
2458 I40E_VLAN_ANY);
2459 if (del_f) {
2460 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002461 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002462 if (!add_f) {
2463 dev_info(&vsi->back->pdev->dev,
2464 "Could not add filter 0 for %pM\n",
2465 vsi->netdev->dev_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002466 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002467 return -ENOMEM;
2468 }
2469 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002470 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002471
Greg Rose8d82a7c2014-01-13 16:13:04 -08002472 /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
2473 if (vid > 0 && !vsi->info.pvid) {
Jacob Keller278e7d02016-10-05 09:30:37 -07002474 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002475 if (f->state == I40E_FILTER_REMOVE)
2476 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002477 del_f = i40e_find_filter(vsi, f->macaddr,
2478 I40E_VLAN_ANY);
2479 if (!del_f)
Kiran Patil21659032015-09-30 14:09:03 -04002480 continue;
Jacob Keller290d2552016-10-05 09:30:36 -07002481 __i40e_del_filter(vsi, del_f);
Jacob Keller1bc87e82016-10-05 09:30:31 -07002482 add_f = i40e_add_filter(vsi, f->macaddr, 0);
Kiran Patil21659032015-09-30 14:09:03 -04002483 if (!add_f) {
2484 dev_info(&vsi->back->pdev->dev,
2485 "Could not add filter 0 for %pM\n",
2486 f->macaddr);
Jacob Keller278e7d02016-10-05 09:30:37 -07002487 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002488 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002489 }
2490 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002491 }
2492
Jacob Keller278e7d02016-10-05 09:30:37 -07002493 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002494
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002495 /* schedule our worker thread which will take care of
2496 * applying the new filter changes
2497 */
2498 i40e_service_event_schedule(vsi->back);
2499 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002500}
2501
2502/**
2503 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
2504 * @vsi: the vsi being configured
2505 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2506 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07002507void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002508{
2509 struct net_device *netdev = vsi->netdev;
Alan Brady84f5ca62016-10-05 09:30:39 -07002510 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002511 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002512 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002513
Kiran Patil21659032015-09-30 14:09:03 -04002514 /* Locked once because all functions invoked below iterates list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002515 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002516
Jacob Keller1bc87e82016-10-05 09:30:31 -07002517 if (vsi->netdev)
2518 i40e_del_filter(vsi, netdev->dev_addr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002519
Jacob Keller278e7d02016-10-05 09:30:37 -07002520 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002521 if (f->vlan == vid)
2522 __i40e_del_filter(vsi, f);
2523 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002524
Jacob Keller278e7d02016-10-05 09:30:37 -07002525 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002526
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002527 /* schedule our worker thread which will take care of
2528 * applying the new filter changes
2529 */
2530 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002531}
2532
2533/**
2534 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2535 * @netdev: network interface to be adjusted
2536 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002537 *
2538 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002539 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002540#ifdef I40E_FCOE
2541int i40e_vlan_rx_add_vid(struct net_device *netdev,
2542 __always_unused __be16 proto, u16 vid)
2543#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002544static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2545 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002546#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002547{
2548 struct i40e_netdev_priv *np = netdev_priv(netdev);
2549 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002550 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002551
2552 if (vid > 4095)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002553 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002554
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002555 /* If the network stack called us with vid = 0 then
2556 * it is asking to receive priority tagged packets with
2557 * vlan id 0. Our HW receives them by default when configured
2558 * to receive untagged packets so there is no need to add an
2559 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002560 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002561 if (vid)
2562 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002563
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002564 if (!ret && (vid < VLAN_N_VID))
2565 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002566
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002567 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002568}
2569
2570/**
2571 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2572 * @netdev: network interface to be adjusted
2573 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002574 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002575 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002576 **/
Vasu Dev38e00432014-08-01 13:27:03 -07002577#ifdef I40E_FCOE
2578int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2579 __always_unused __be16 proto, u16 vid)
2580#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002581static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2582 __always_unused __be16 proto, u16 vid)
Vasu Dev38e00432014-08-01 13:27:03 -07002583#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002584{
2585 struct i40e_netdev_priv *np = netdev_priv(netdev);
2586 struct i40e_vsi *vsi = np->vsi;
2587
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002588 /* return code is ignored as there is nothing a user
2589 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002590 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002591 */
2592 i40e_vsi_kill_vlan(vsi, vid);
2593
2594 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002596 return 0;
2597}
2598
2599/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002600 * i40e_macaddr_init - explicitly write the mac address filters
2601 *
2602 * @vsi: pointer to the vsi
2603 * @macaddr: the MAC address
2604 *
2605 * This is needed when the macaddr has been obtained by other
2606 * means than the default, e.g., from Open Firmware or IDPROM.
2607 * Returns 0 on success, negative on failure
2608 **/
2609static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2610{
2611 int ret;
2612 struct i40e_aqc_add_macvlan_element_data element;
2613
2614 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2615 I40E_AQC_WRITE_TYPE_LAA_WOL,
2616 macaddr, NULL);
2617 if (ret) {
2618 dev_info(&vsi->back->pdev->dev,
2619 "Addr change for VSI failed: %d\n", ret);
2620 return -EADDRNOTAVAIL;
2621 }
2622
2623 memset(&element, 0, sizeof(element));
2624 ether_addr_copy(element.mac_addr, macaddr);
2625 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2626 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2627 if (ret) {
2628 dev_info(&vsi->back->pdev->dev,
2629 "add filter failed err %s aq_err %s\n",
2630 i40e_stat_str(&vsi->back->hw, ret),
2631 i40e_aq_str(&vsi->back->hw,
2632 vsi->back->hw.aq.asq_last_status));
2633 }
2634 return ret;
2635}
2636
2637/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002638 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2639 * @vsi: the vsi being brought back up
2640 **/
2641static void i40e_restore_vlan(struct i40e_vsi *vsi)
2642{
2643 u16 vid;
2644
2645 if (!vsi->netdev)
2646 return;
2647
2648 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2649
2650 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2651 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2652 vid);
2653}
2654
2655/**
2656 * i40e_vsi_add_pvid - Add pvid for the VSI
2657 * @vsi: the vsi being adjusted
2658 * @vid: the vlan id to set as a PVID
2659 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002660int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002661{
2662 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002663 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002664
2665 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2666 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002667 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2668 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002669 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002670
2671 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002672 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002673 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2674 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002675 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002676 "add pvid failed, err %s aq_err %s\n",
2677 i40e_stat_str(&vsi->back->hw, ret),
2678 i40e_aq_str(&vsi->back->hw,
2679 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002680 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002681 }
2682
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002683 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002684}
2685
2686/**
2687 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2688 * @vsi: the vsi being adjusted
2689 *
2690 * Just use the vlan_rx_register() service to put it back to normal
2691 **/
2692void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2693{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002694 i40e_vlan_stripping_disable(vsi);
2695
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002696 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002697}
2698
2699/**
2700 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2701 * @vsi: ptr to the VSI
2702 *
2703 * If this function returns with an error, then it's possible one or
2704 * more of the rings is populated (while the rest are not). It is the
2705 * callers duty to clean those orphaned rings.
2706 *
2707 * Return 0 on success, negative on failure
2708 **/
2709static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2710{
2711 int i, err = 0;
2712
2713 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002714 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002715
2716 return err;
2717}
2718
2719/**
2720 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2721 * @vsi: ptr to the VSI
2722 *
2723 * Free VSI's transmit software resources
2724 **/
2725static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2726{
2727 int i;
2728
Greg Rose8e9dca52013-12-18 13:45:53 +00002729 if (!vsi->tx_rings)
2730 return;
2731
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002732 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002733 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002734 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002735}
2736
2737/**
2738 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2739 * @vsi: ptr to the VSI
2740 *
2741 * If this function returns with an error, then it's possible one or
2742 * more of the rings is populated (while the rest are not). It is the
2743 * callers duty to clean those orphaned rings.
2744 *
2745 * Return 0 on success, negative on failure
2746 **/
2747static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2748{
2749 int i, err = 0;
2750
2751 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002752 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002753#ifdef I40E_FCOE
2754 i40e_fcoe_setup_ddp_resources(vsi);
2755#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756 return err;
2757}
2758
2759/**
2760 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2761 * @vsi: ptr to the VSI
2762 *
2763 * Free all receive software resources
2764 **/
2765static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2766{
2767 int i;
2768
Greg Rose8e9dca52013-12-18 13:45:53 +00002769 if (!vsi->rx_rings)
2770 return;
2771
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002773 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002774 i40e_free_rx_resources(vsi->rx_rings[i]);
Vasu Dev38e00432014-08-01 13:27:03 -07002775#ifdef I40E_FCOE
2776 i40e_fcoe_free_ddp_resources(vsi);
2777#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002778}
2779
2780/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002781 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2782 * @ring: The Tx ring to configure
2783 *
2784 * This enables/disables XPS for a given Tx descriptor ring
2785 * based on the TCs enabled for the VSI that ring belongs to.
2786 **/
2787static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2788{
2789 struct i40e_vsi *vsi = ring->vsi;
2790 cpumask_var_t mask;
2791
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002792 if (!ring->q_vector || !ring->netdev)
2793 return;
2794
2795 /* Single TC mode enable XPS */
2796 if (vsi->tc_config.numtc <= 1) {
2797 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002798 netif_set_xps_queue(ring->netdev,
2799 &ring->q_vector->affinity_mask,
2800 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002801 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2802 /* Disable XPS to allow selection based on TC */
2803 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2804 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2805 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002806 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002807
2808 /* schedule our worker thread which will take care of
2809 * applying the new filter changes
2810 */
2811 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002812}
2813
2814/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002815 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2816 * @ring: The Tx ring to configure
2817 *
2818 * Configure the Tx descriptor ring in the HMC context.
2819 **/
2820static int i40e_configure_tx_ring(struct i40e_ring *ring)
2821{
2822 struct i40e_vsi *vsi = ring->vsi;
2823 u16 pf_q = vsi->base_queue + ring->queue_index;
2824 struct i40e_hw *hw = &vsi->back->hw;
2825 struct i40e_hmc_obj_txq tx_ctx;
2826 i40e_status err = 0;
2827 u32 qtx_ctl = 0;
2828
2829 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002830 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002831 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2832 ring->atr_count = 0;
2833 } else {
2834 ring->atr_sample_rate = 0;
2835 }
2836
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002837 /* configure XPS */
2838 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002839
2840 /* clear the context structure first */
2841 memset(&tx_ctx, 0, sizeof(tx_ctx));
2842
2843 tx_ctx.new_context = 1;
2844 tx_ctx.base = (ring->dma / 128);
2845 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002846 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2847 I40E_FLAG_FD_ATR_ENABLED));
Vasu Dev38e00432014-08-01 13:27:03 -07002848#ifdef I40E_FCOE
2849 tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2850#endif
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002851 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002852 /* FDIR VSI tx ring can still use RS bit and writebacks */
2853 if (vsi->type != I40E_VSI_FDIR)
2854 tx_ctx.head_wb_ena = 1;
2855 tx_ctx.head_wb_addr = ring->dma +
2856 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002857
2858 /* As part of VSI creation/update, FW allocates certain
2859 * Tx arbitration queue sets for each TC enabled for
2860 * the VSI. The FW returns the handles to these queue
2861 * sets as part of the response buffer to Add VSI,
2862 * Update VSI, etc. AQ commands. It is expected that
2863 * these queue set handles be associated with the Tx
2864 * queues by the driver as part of the TX queue context
2865 * initialization. This has to be done regardless of
2866 * DCB as by default everything is mapped to TC0.
2867 */
2868 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2869 tx_ctx.rdylist_act = 0;
2870
2871 /* clear the context in the HMC */
2872 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2873 if (err) {
2874 dev_info(&vsi->back->pdev->dev,
2875 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2876 ring->queue_index, pf_q, err);
2877 return -ENOMEM;
2878 }
2879
2880 /* set the context in the HMC */
2881 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2882 if (err) {
2883 dev_info(&vsi->back->pdev->dev,
2884 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2885 ring->queue_index, pf_q, err);
2886 return -ENOMEM;
2887 }
2888
2889 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002890 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002891 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002892 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2893 I40E_QTX_CTL_VFVM_INDX_MASK;
2894 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002895 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002896 }
2897
Shannon Nelson13fd9772013-09-28 07:14:19 +00002898 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2899 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2901 i40e_flush(hw);
2902
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002903 /* cache tail off for easier writes later */
2904 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2905
2906 return 0;
2907}
2908
2909/**
2910 * i40e_configure_rx_ring - Configure a receive ring context
2911 * @ring: The Rx ring to configure
2912 *
2913 * Configure the Rx descriptor ring in the HMC context.
2914 **/
2915static int i40e_configure_rx_ring(struct i40e_ring *ring)
2916{
2917 struct i40e_vsi *vsi = ring->vsi;
2918 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2919 u16 pf_q = vsi->base_queue + ring->queue_index;
2920 struct i40e_hw *hw = &vsi->back->hw;
2921 struct i40e_hmc_obj_rxq rx_ctx;
2922 i40e_status err = 0;
2923
2924 ring->state = 0;
2925
2926 /* clear the context structure first */
2927 memset(&rx_ctx, 0, sizeof(rx_ctx));
2928
2929 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002930
2931 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002932
2933 rx_ctx.base = (ring->dma / 128);
2934 rx_ctx.qlen = ring->count;
2935
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002936 /* use 32 byte descriptors */
2937 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002938
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07002939 /* descriptor type is always zero
2940 * rx_ctx.dtype = 0;
2941 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002942 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002943
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07002944 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00002945 if (hw->revision_id == 0)
2946 rx_ctx.lrxqthresh = 0;
2947 else
2948 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002949 rx_ctx.crcstrip = 1;
2950 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07002951 /* this controls whether VLAN is stripped from inner headers */
2952 rx_ctx.showiv = 0;
Vasu Dev38e00432014-08-01 13:27:03 -07002953#ifdef I40E_FCOE
2954 rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
2955#endif
Catherine Sullivanacb36762014-03-06 09:02:30 +00002956 /* set the prefena field to 1 because the manual says to */
2957 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002958
2959 /* clear the context in the HMC */
2960 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2961 if (err) {
2962 dev_info(&vsi->back->pdev->dev,
2963 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2964 ring->queue_index, pf_q, err);
2965 return -ENOMEM;
2966 }
2967
2968 /* set the context in the HMC */
2969 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2970 if (err) {
2971 dev_info(&vsi->back->pdev->dev,
2972 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2973 ring->queue_index, pf_q, err);
2974 return -ENOMEM;
2975 }
2976
2977 /* cache tail for quicker writes, and clear the reg before use */
2978 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2979 writel(0, ring->tail);
2980
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07002981 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002982
2983 return 0;
2984}
2985
2986/**
2987 * i40e_vsi_configure_tx - Configure the VSI for Tx
2988 * @vsi: VSI structure describing this set of rings and resources
2989 *
2990 * Configure the Tx VSI for operation.
2991 **/
2992static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2993{
2994 int err = 0;
2995 u16 i;
2996
Alexander Duyck9f65e152013-09-28 06:00:58 +00002997 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2998 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002999
3000 return err;
3001}
3002
3003/**
3004 * i40e_vsi_configure_rx - Configure the VSI for Rx
3005 * @vsi: the VSI being configured
3006 *
3007 * Configure the Rx VSI for operation.
3008 **/
3009static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3010{
3011 int err = 0;
3012 u16 i;
3013
3014 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3015 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3016 + ETH_FCS_LEN + VLAN_HLEN;
3017 else
3018 vsi->max_frame = I40E_RXBUFFER_2048;
3019
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003020 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003021
Vasu Dev38e00432014-08-01 13:27:03 -07003022#ifdef I40E_FCOE
3023 /* setup rx buffer for FCoE */
3024 if ((vsi->type == I40E_VSI_FCOE) &&
3025 (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
Vasu Dev38e00432014-08-01 13:27:03 -07003026 vsi->rx_buf_len = I40E_RXBUFFER_3072;
3027 vsi->max_frame = I40E_RXBUFFER_3072;
Vasu Dev38e00432014-08-01 13:27:03 -07003028 }
3029
3030#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003031 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003032 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003033 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003034
3035 /* set up individual rings */
3036 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003037 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003038
3039 return err;
3040}
3041
3042/**
3043 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3044 * @vsi: ptr to the VSI
3045 **/
3046static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3047{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003048 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003049 u16 qoffset, qcount;
3050 int i, n;
3051
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003052 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3053 /* Reset the TC information */
3054 for (i = 0; i < vsi->num_queue_pairs; i++) {
3055 rx_ring = vsi->rx_rings[i];
3056 tx_ring = vsi->tx_rings[i];
3057 rx_ring->dcb_tc = 0;
3058 tx_ring->dcb_tc = 0;
3059 }
3060 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003061
3062 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003063 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003064 continue;
3065
3066 qoffset = vsi->tc_config.tc_info[n].qoffset;
3067 qcount = vsi->tc_config.tc_info[n].qcount;
3068 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003069 rx_ring = vsi->rx_rings[i];
3070 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003071 rx_ring->dcb_tc = n;
3072 tx_ring->dcb_tc = n;
3073 }
3074 }
3075}
3076
3077/**
3078 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3079 * @vsi: ptr to the VSI
3080 **/
3081static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3082{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003083 struct i40e_pf *pf = vsi->back;
3084 int err;
3085
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003086 if (vsi->netdev)
3087 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003088
3089 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3090 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3091 if (err) {
3092 dev_warn(&pf->pdev->dev,
3093 "could not set up macaddr; err %d\n", err);
3094 }
3095 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003096}
3097
3098/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003099 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3100 * @vsi: Pointer to the targeted VSI
3101 *
3102 * This function replays the hlist on the hw where all the SB Flow Director
3103 * filters were saved.
3104 **/
3105static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3106{
3107 struct i40e_fdir_filter *filter;
3108 struct i40e_pf *pf = vsi->back;
3109 struct hlist_node *node;
3110
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003111 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3112 return;
3113
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003114 hlist_for_each_entry_safe(filter, node,
3115 &pf->fdir_filter_list, fdir_node) {
3116 i40e_add_del_fdir(vsi, filter, true);
3117 }
3118}
3119
3120/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121 * i40e_vsi_configure - Set up the VSI for action
3122 * @vsi: the VSI being configured
3123 **/
3124static int i40e_vsi_configure(struct i40e_vsi *vsi)
3125{
3126 int err;
3127
3128 i40e_set_vsi_rx_mode(vsi);
3129 i40e_restore_vlan(vsi);
3130 i40e_vsi_config_dcb_rings(vsi);
3131 err = i40e_vsi_configure_tx(vsi);
3132 if (!err)
3133 err = i40e_vsi_configure_rx(vsi);
3134
3135 return err;
3136}
3137
3138/**
3139 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3140 * @vsi: the VSI being configured
3141 **/
3142static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3143{
3144 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003145 struct i40e_hw *hw = &pf->hw;
3146 u16 vector;
3147 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003148 u32 qp;
3149
3150 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3151 * and PFINT_LNKLSTn registers, e.g.:
3152 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3153 */
3154 qp = vsi->base_queue;
3155 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003156 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003157 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3158
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003159 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003160 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003161 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3162 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3163 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003164 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003165 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3166 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3167 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003168 wr32(hw, I40E_PFINT_RATEN(vector - 1),
3169 INTRL_USEC_TO_REG(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003170
3171 /* Linked list for the queuepairs assigned to this vector */
3172 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3173 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003174 u32 val;
3175
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003176 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3177 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3178 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3179 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3180 (I40E_QUEUE_TYPE_TX
3181 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3182
3183 wr32(hw, I40E_QINT_RQCTL(qp), val);
3184
3185 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3186 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3187 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3188 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3189 (I40E_QUEUE_TYPE_RX
3190 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3191
3192 /* Terminate the linked list */
3193 if (q == (q_vector->num_ringpairs - 1))
3194 val |= (I40E_QUEUE_END_OF_LIST
3195 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3196
3197 wr32(hw, I40E_QINT_TQCTL(qp), val);
3198 qp++;
3199 }
3200 }
3201
3202 i40e_flush(hw);
3203}
3204
3205/**
3206 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3207 * @hw: ptr to the hardware info
3208 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003209static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003210{
Jacob Kellerab437b52014-12-14 01:55:08 +00003211 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003212 u32 val;
3213
3214 /* clear things first */
3215 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3216 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3217
3218 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3219 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3220 I40E_PFINT_ICR0_ENA_GRST_MASK |
3221 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3222 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003223 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3224 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3225 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3226
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003227 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3228 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3229
Jacob Kellerab437b52014-12-14 01:55:08 +00003230 if (pf->flags & I40E_FLAG_PTP)
3231 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3232
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3234
3235 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003236 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3237 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003238
3239 /* OTHER_ITR_IDX = 0 */
3240 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3241}
3242
3243/**
3244 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3245 * @vsi: the VSI being configured
3246 **/
3247static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3248{
Alexander Duyck493fb302013-09-28 07:01:44 +00003249 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003250 struct i40e_pf *pf = vsi->back;
3251 struct i40e_hw *hw = &pf->hw;
3252 u32 val;
3253
3254 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003255 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003256 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003257 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3258 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003259 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003260 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3261 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3262
Jacob Kellerab437b52014-12-14 01:55:08 +00003263 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003264
3265 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3266 wr32(hw, I40E_PFINT_LNKLST0, 0);
3267
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003268 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003269 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3270 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3271 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3272
3273 wr32(hw, I40E_QINT_RQCTL(0), val);
3274
3275 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3276 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3277 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3278
3279 wr32(hw, I40E_QINT_TQCTL(0), val);
3280 i40e_flush(hw);
3281}
3282
3283/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003284 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3285 * @pf: board private structure
3286 **/
3287void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3288{
3289 struct i40e_hw *hw = &pf->hw;
3290
3291 wr32(hw, I40E_PFINT_DYN_CTL0,
3292 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3293 i40e_flush(hw);
3294}
3295
3296/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003297 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3298 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003299 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003300 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003301void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003302{
3303 struct i40e_hw *hw = &pf->hw;
3304 u32 val;
3305
3306 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003307 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003308 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3309
3310 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3311 i40e_flush(hw);
3312}
3313
3314/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003315 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3316 * @irq: interrupt number
3317 * @data: pointer to a q_vector
3318 **/
3319static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3320{
3321 struct i40e_q_vector *q_vector = data;
3322
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003323 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003324 return IRQ_HANDLED;
3325
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003326 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327
3328 return IRQ_HANDLED;
3329}
3330
3331/**
Alan Brady96db7762016-09-14 16:24:38 -07003332 * i40e_irq_affinity_notify - Callback for affinity changes
3333 * @notify: context as to what irq was changed
3334 * @mask: the new affinity mask
3335 *
3336 * This is a callback function used by the irq_set_affinity_notifier function
3337 * so that we may register to receive changes to the irq affinity masks.
3338 **/
3339static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3340 const cpumask_t *mask)
3341{
3342 struct i40e_q_vector *q_vector =
3343 container_of(notify, struct i40e_q_vector, affinity_notify);
3344
3345 q_vector->affinity_mask = *mask;
3346}
3347
3348/**
3349 * i40e_irq_affinity_release - Callback for affinity notifier release
3350 * @ref: internal core kernel usage
3351 *
3352 * This is a callback function used by the irq_set_affinity_notifier function
3353 * to inform the current notification subscriber that they will no longer
3354 * receive notifications.
3355 **/
3356static void i40e_irq_affinity_release(struct kref *ref) {}
3357
3358/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003359 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3360 * @vsi: the VSI being configured
3361 * @basename: name for the vector
3362 *
3363 * Allocates MSI-X vectors and requests interrupts from the kernel.
3364 **/
3365static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3366{
3367 int q_vectors = vsi->num_q_vectors;
3368 struct i40e_pf *pf = vsi->back;
3369 int base = vsi->base_vector;
3370 int rx_int_idx = 0;
3371 int tx_int_idx = 0;
3372 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003373 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003374
3375 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003376 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003377
Alan Brady96db7762016-09-14 16:24:38 -07003378 irq_num = pf->msix_entries[base + vector].vector;
3379
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003380 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003381 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3382 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3383 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003384 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003385 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3386 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003387 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003388 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3389 "%s-%s-%d", basename, "tx", tx_int_idx++);
3390 } else {
3391 /* skip this unused q_vector */
3392 continue;
3393 }
Alan Brady96db7762016-09-14 16:24:38 -07003394 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003395 vsi->irq_handler,
3396 0,
3397 q_vector->name,
3398 q_vector);
3399 if (err) {
3400 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003401 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003402 goto free_queue_irqs;
3403 }
Alan Brady96db7762016-09-14 16:24:38 -07003404
3405 /* register for affinity change notifications */
3406 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3407 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3408 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003409 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003410 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003411 }
3412
Shannon Nelson63741842014-04-23 04:50:16 +00003413 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003414 return 0;
3415
3416free_queue_irqs:
3417 while (vector) {
3418 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003419 irq_num = pf->msix_entries[base + vector].vector;
3420 irq_set_affinity_notifier(irq_num, NULL);
3421 irq_set_affinity_hint(irq_num, NULL);
3422 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003423 }
3424 return err;
3425}
3426
3427/**
3428 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3429 * @vsi: the VSI being un-configured
3430 **/
3431static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3432{
3433 struct i40e_pf *pf = vsi->back;
3434 struct i40e_hw *hw = &pf->hw;
3435 int base = vsi->base_vector;
3436 int i;
3437
3438 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003439 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3440 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003441 }
3442
3443 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3444 for (i = vsi->base_vector;
3445 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3446 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3447
3448 i40e_flush(hw);
3449 for (i = 0; i < vsi->num_q_vectors; i++)
3450 synchronize_irq(pf->msix_entries[i + base].vector);
3451 } else {
3452 /* Legacy and MSI mode - this stops all interrupt handling */
3453 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3454 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3455 i40e_flush(hw);
3456 synchronize_irq(pf->pdev->irq);
3457 }
3458}
3459
3460/**
3461 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3462 * @vsi: the VSI being configured
3463 **/
3464static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3465{
3466 struct i40e_pf *pf = vsi->back;
3467 int i;
3468
3469 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003470 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003471 i40e_irq_dynamic_enable(vsi, i);
3472 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003473 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003474 }
3475
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003476 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 return 0;
3478}
3479
3480/**
3481 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3482 * @pf: board private structure
3483 **/
3484static void i40e_stop_misc_vector(struct i40e_pf *pf)
3485{
3486 /* Disable ICR 0 */
3487 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3488 i40e_flush(&pf->hw);
3489}
3490
3491/**
3492 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3493 * @irq: interrupt number
3494 * @data: pointer to a q_vector
3495 *
3496 * This is the handler used for all MSI/Legacy interrupts, and deals
3497 * with both queue and non-queue interrupts. This is also used in
3498 * MSIX mode to handle the non-queue interrupts.
3499 **/
3500static irqreturn_t i40e_intr(int irq, void *data)
3501{
3502 struct i40e_pf *pf = (struct i40e_pf *)data;
3503 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003504 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003505 u32 icr0, icr0_remaining;
3506 u32 val, ena_mask;
3507
3508 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003509 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003510
Shannon Nelson116a57d2013-09-28 07:13:59 +00003511 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3512 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003513 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003514
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003515 /* if interrupt but no bits showing, must be SWINT */
3516 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3517 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3518 pf->sw_int_count++;
3519
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003520 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3521 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3522 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3523 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3524 dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
3525 }
3526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003527 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3528 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003529 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3530 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003531
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003532 /* We do not have a way to disarm Queue causes while leaving
3533 * interrupt enabled for all other causes, ideally
3534 * interrupt should be disabled while we are in NAPI but
3535 * this is not a performance path and napi_schedule()
3536 * can deal with rescheduling.
3537 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003538 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003539 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540 }
3541
3542 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3543 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3544 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003545 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003546 }
3547
3548 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3549 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3550 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3551 }
3552
3553 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3554 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3555 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3556 }
3557
3558 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3559 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3560 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3561 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3562 val = rd32(hw, I40E_GLGEN_RSTAT);
3563 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3564 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003565 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003567 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003568 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003569 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003570 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003571 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003572 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573 }
3574
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003575 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3576 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3577 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003578 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3579 rd32(hw, I40E_PFHMC_ERRORINFO),
3580 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003581 }
3582
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003583 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3584 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3585
3586 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003587 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003588 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003589 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003590 }
3591
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003592 /* If a critical error is pending we have no choice but to reset the
3593 * device.
3594 * Report and mask out any remaining unexpected interrupts.
3595 */
3596 icr0_remaining = icr0 & ena_mask;
3597 if (icr0_remaining) {
3598 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3599 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003600 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003601 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003602 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003603 dev_info(&pf->pdev->dev, "device will be reset\n");
3604 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3605 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003606 }
3607 ena_mask &= ~icr0_remaining;
3608 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003609 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003610
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003611enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003612 /* re-enable interrupt causes */
3613 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003614 if (!test_bit(__I40E_DOWN, &pf->state)) {
3615 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003616 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003617 }
3618
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003619 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003620}
3621
3622/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003623 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3624 * @tx_ring: tx ring to clean
3625 * @budget: how many cleans we're allowed
3626 *
3627 * Returns true if there's any budget left (e.g. the clean is finished)
3628 **/
3629static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3630{
3631 struct i40e_vsi *vsi = tx_ring->vsi;
3632 u16 i = tx_ring->next_to_clean;
3633 struct i40e_tx_buffer *tx_buf;
3634 struct i40e_tx_desc *tx_desc;
3635
3636 tx_buf = &tx_ring->tx_bi[i];
3637 tx_desc = I40E_TX_DESC(tx_ring, i);
3638 i -= tx_ring->count;
3639
3640 do {
3641 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3642
3643 /* if next_to_watch is not set then there is no work pending */
3644 if (!eop_desc)
3645 break;
3646
3647 /* prevent any other reads prior to eop_desc */
3648 read_barrier_depends();
3649
3650 /* if the descriptor isn't done, no work yet to do */
3651 if (!(eop_desc->cmd_type_offset_bsz &
3652 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3653 break;
3654
3655 /* clear next_to_watch to prevent false hangs */
3656 tx_buf->next_to_watch = NULL;
3657
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003658 tx_desc->buffer_addr = 0;
3659 tx_desc->cmd_type_offset_bsz = 0;
3660 /* move past filter desc */
3661 tx_buf++;
3662 tx_desc++;
3663 i++;
3664 if (unlikely(!i)) {
3665 i -= tx_ring->count;
3666 tx_buf = tx_ring->tx_bi;
3667 tx_desc = I40E_TX_DESC(tx_ring, 0);
3668 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003669 /* unmap skb header data */
3670 dma_unmap_single(tx_ring->dev,
3671 dma_unmap_addr(tx_buf, dma),
3672 dma_unmap_len(tx_buf, len),
3673 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003674 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3675 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003676
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003677 tx_buf->raw_buf = NULL;
3678 tx_buf->tx_flags = 0;
3679 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003680 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003681 tx_desc->buffer_addr = 0;
3682 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003683
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003684 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003685 tx_buf++;
3686 tx_desc++;
3687 i++;
3688 if (unlikely(!i)) {
3689 i -= tx_ring->count;
3690 tx_buf = tx_ring->tx_bi;
3691 tx_desc = I40E_TX_DESC(tx_ring, 0);
3692 }
3693
3694 /* update budget accounting */
3695 budget--;
3696 } while (likely(budget));
3697
3698 i += tx_ring->count;
3699 tx_ring->next_to_clean = i;
3700
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003701 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003702 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003703
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003704 return budget > 0;
3705}
3706
3707/**
3708 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3709 * @irq: interrupt number
3710 * @data: pointer to a q_vector
3711 **/
3712static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3713{
3714 struct i40e_q_vector *q_vector = data;
3715 struct i40e_vsi *vsi;
3716
3717 if (!q_vector->tx.ring)
3718 return IRQ_HANDLED;
3719
3720 vsi = q_vector->tx.ring->vsi;
3721 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3722
3723 return IRQ_HANDLED;
3724}
3725
3726/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003727 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003728 * @vsi: the VSI being configured
3729 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003730 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003731 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003732static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003733{
Alexander Duyck493fb302013-09-28 07:01:44 +00003734 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003735 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3736 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003737
3738 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003739 tx_ring->next = q_vector->tx.ring;
3740 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003741 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003742
3743 rx_ring->q_vector = q_vector;
3744 rx_ring->next = q_vector->rx.ring;
3745 q_vector->rx.ring = rx_ring;
3746 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003747}
3748
3749/**
3750 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3751 * @vsi: the VSI being configured
3752 *
3753 * This function maps descriptor rings to the queue-specific vectors
3754 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3755 * one vector per queue pair, but on a constrained vector budget, we
3756 * group the queue pairs as "efficiently" as possible.
3757 **/
3758static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3759{
3760 int qp_remaining = vsi->num_queue_pairs;
3761 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003762 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003763 int v_start = 0;
3764 int qp_idx = 0;
3765
3766 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3767 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003768 * It is also important to go through all the vectors available to be
3769 * sure that if we don't use all the vectors, that the remaining vectors
3770 * are cleared. This is especially important when decreasing the
3771 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003772 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003773 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003774 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3775
3776 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3777
3778 q_vector->num_ringpairs = num_ringpairs;
3779
3780 q_vector->rx.count = 0;
3781 q_vector->tx.count = 0;
3782 q_vector->rx.ring = NULL;
3783 q_vector->tx.ring = NULL;
3784
3785 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003786 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003787 qp_idx++;
3788 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 }
3790 }
3791}
3792
3793/**
3794 * i40e_vsi_request_irq - Request IRQ from the OS
3795 * @vsi: the VSI being configured
3796 * @basename: name for the vector
3797 **/
3798static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3799{
3800 struct i40e_pf *pf = vsi->back;
3801 int err;
3802
3803 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3804 err = i40e_vsi_request_irq_msix(vsi, basename);
3805 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3806 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003807 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003808 else
3809 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003810 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003811
3812 if (err)
3813 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3814
3815 return err;
3816}
3817
3818#ifdef CONFIG_NET_POLL_CONTROLLER
3819/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003820 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003821 * @netdev: network interface device structure
3822 *
3823 * This is used by netconsole to send skbs without having to re-enable
3824 * interrupts. It's not called while the normal interrupt routine is executing.
3825 **/
Vasu Dev38e00432014-08-01 13:27:03 -07003826#ifdef I40E_FCOE
3827void i40e_netpoll(struct net_device *netdev)
3828#else
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003829static void i40e_netpoll(struct net_device *netdev)
Vasu Dev38e00432014-08-01 13:27:03 -07003830#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003831{
3832 struct i40e_netdev_priv *np = netdev_priv(netdev);
3833 struct i40e_vsi *vsi = np->vsi;
3834 struct i40e_pf *pf = vsi->back;
3835 int i;
3836
3837 /* if interface is down do nothing */
3838 if (test_bit(__I40E_DOWN, &vsi->state))
3839 return;
3840
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003841 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3842 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003843 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003844 } else {
3845 i40e_intr(pf->pdev->irq, netdev);
3846 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003847}
3848#endif
3849
3850/**
Neerav Parikh23527302014-06-03 23:50:15 +00003851 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3852 * @pf: the PF being configured
3853 * @pf_q: the PF queue
3854 * @enable: enable or disable state of the queue
3855 *
3856 * This routine will wait for the given Tx queue of the PF to reach the
3857 * enabled or disabled state.
3858 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3859 * multiple retries; else will return 0 in case of success.
3860 **/
3861static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3862{
3863 int i;
3864 u32 tx_reg;
3865
3866 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3867 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3868 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3869 break;
3870
Neerav Parikhf98a2002014-09-13 07:40:44 +00003871 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003872 }
3873 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3874 return -ETIMEDOUT;
3875
3876 return 0;
3877}
3878
3879/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003880 * i40e_vsi_control_tx - Start or stop a VSI's rings
3881 * @vsi: the VSI being configured
3882 * @enable: start or stop the rings
3883 **/
3884static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3885{
3886 struct i40e_pf *pf = vsi->back;
3887 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003888 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003889 u32 tx_reg;
3890
3891 pf_q = vsi->base_queue;
3892 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003893
3894 /* warn the TX unit of coming changes */
3895 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3896 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003897 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003898
Mitch Williams6c5ef622014-02-20 19:29:16 -08003899 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003900 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003901 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3902 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3903 break;
3904 usleep_range(1000, 2000);
3905 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003906 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003907 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003908 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003909
3910 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003911 if (enable) {
3912 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003913 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003914 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003915 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003916 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003917
3918 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003919 /* No waiting for the Tx queue to disable */
3920 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3921 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003922
3923 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003924 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3925 if (ret) {
3926 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003927 "VSI seid %d Tx ring %d %sable timeout\n",
3928 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003929 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003930 }
3931 }
3932
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003933 if (hw->revision_id == 0)
3934 mdelay(50);
Neerav Parikh23527302014-06-03 23:50:15 +00003935 return ret;
3936}
3937
3938/**
3939 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3940 * @pf: the PF being configured
3941 * @pf_q: the PF queue
3942 * @enable: enable or disable state of the queue
3943 *
3944 * This routine will wait for the given Rx queue of the PF to reach the
3945 * enabled or disabled state.
3946 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3947 * multiple retries; else will return 0 in case of success.
3948 **/
3949static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3950{
3951 int i;
3952 u32 rx_reg;
3953
3954 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3955 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
3956 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3957 break;
3958
Neerav Parikhf98a2002014-09-13 07:40:44 +00003959 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003960 }
3961 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3962 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003963
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003964 return 0;
3965}
3966
3967/**
3968 * i40e_vsi_control_rx - Start or stop a VSI's rings
3969 * @vsi: the VSI being configured
3970 * @enable: start or stop the rings
3971 **/
3972static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3973{
3974 struct i40e_pf *pf = vsi->back;
3975 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003976 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003977 u32 rx_reg;
3978
3979 pf_q = vsi->base_queue;
3980 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08003981 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003982 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003983 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
3984 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
3985 break;
3986 usleep_range(1000, 2000);
3987 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003988
Catherine Sullivan7c122002014-03-14 07:32:29 +00003989 /* Skip if the queue is already in the requested state */
3990 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3991 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003992
3993 /* turn on/off the queue */
3994 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08003995 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003996 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08003997 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003998 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08003999 /* No waiting for the Tx queue to disable */
4000 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4001 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004002
4003 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004004 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4005 if (ret) {
4006 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004007 "VSI seid %d Rx ring %d %sable timeout\n",
4008 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004009 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004010 }
4011 }
4012
Neerav Parikh23527302014-06-03 23:50:15 +00004013 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004014}
4015
4016/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004017 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004018 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004020int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004021{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004022 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004023
4024 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004025 ret = i40e_vsi_control_rx(vsi, true);
4026 if (ret)
4027 return ret;
4028 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004029
4030 return ret;
4031}
4032
4033/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004034 * i40e_vsi_stop_rings - Stop a VSI's rings
4035 * @vsi: the VSI being configured
4036 **/
4037void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4038{
4039 /* do rx first for enable and last for disable
4040 * Ignore return value, we need to shutdown whatever we can
4041 */
4042 i40e_vsi_control_tx(vsi, false);
4043 i40e_vsi_control_rx(vsi, false);
4044}
4045
4046/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004047 * i40e_vsi_free_irq - Free the irq association with the OS
4048 * @vsi: the VSI being configured
4049 **/
4050static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4051{
4052 struct i40e_pf *pf = vsi->back;
4053 struct i40e_hw *hw = &pf->hw;
4054 int base = vsi->base_vector;
4055 u32 val, qp;
4056 int i;
4057
4058 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4059 if (!vsi->q_vectors)
4060 return;
4061
Shannon Nelson63741842014-04-23 04:50:16 +00004062 if (!vsi->irqs_ready)
4063 return;
4064
4065 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004066 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004067 int irq_num;
4068 u16 vector;
4069
4070 vector = i + base;
4071 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004072
4073 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004074 if (!vsi->q_vectors[i] ||
4075 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004076 continue;
4077
Alan Brady96db7762016-09-14 16:24:38 -07004078 /* clear the affinity notifier in the IRQ descriptor */
4079 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004080 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004081 irq_set_affinity_hint(irq_num, NULL);
4082 synchronize_irq(irq_num);
4083 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004084
4085 /* Tear down the interrupt queue link list
4086 *
4087 * We know that they come in pairs and always
4088 * the Rx first, then the Tx. To clear the
4089 * link list, stick the EOL value into the
4090 * next_q field of the registers.
4091 */
4092 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4093 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4094 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4095 val |= I40E_QUEUE_END_OF_LIST
4096 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4097 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4098
4099 while (qp != I40E_QUEUE_END_OF_LIST) {
4100 u32 next;
4101
4102 val = rd32(hw, I40E_QINT_RQCTL(qp));
4103
4104 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4105 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4106 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4107 I40E_QINT_RQCTL_INTEVENT_MASK);
4108
4109 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4110 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4111
4112 wr32(hw, I40E_QINT_RQCTL(qp), val);
4113
4114 val = rd32(hw, I40E_QINT_TQCTL(qp));
4115
4116 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4117 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4118
4119 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4120 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4121 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4122 I40E_QINT_TQCTL_INTEVENT_MASK);
4123
4124 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4125 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4126
4127 wr32(hw, I40E_QINT_TQCTL(qp), val);
4128 qp = next;
4129 }
4130 }
4131 } else {
4132 free_irq(pf->pdev->irq, pf);
4133
4134 val = rd32(hw, I40E_PFINT_LNKLST0);
4135 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4136 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4137 val |= I40E_QUEUE_END_OF_LIST
4138 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4139 wr32(hw, I40E_PFINT_LNKLST0, val);
4140
4141 val = rd32(hw, I40E_QINT_RQCTL(qp));
4142 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4143 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4144 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4145 I40E_QINT_RQCTL_INTEVENT_MASK);
4146
4147 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4148 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4149
4150 wr32(hw, I40E_QINT_RQCTL(qp), val);
4151
4152 val = rd32(hw, I40E_QINT_TQCTL(qp));
4153
4154 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4155 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4156 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4157 I40E_QINT_TQCTL_INTEVENT_MASK);
4158
4159 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4160 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4161
4162 wr32(hw, I40E_QINT_TQCTL(qp), val);
4163 }
4164}
4165
4166/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004167 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4168 * @vsi: the VSI being configured
4169 * @v_idx: Index of vector to be freed
4170 *
4171 * This function frees the memory allocated to the q_vector. In addition if
4172 * NAPI is enabled it will delete any references to the NAPI struct prior
4173 * to freeing the q_vector.
4174 **/
4175static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4176{
4177 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004178 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004179
4180 if (!q_vector)
4181 return;
4182
4183 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004184 i40e_for_each_ring(ring, q_vector->tx)
4185 ring->q_vector = NULL;
4186
4187 i40e_for_each_ring(ring, q_vector->rx)
4188 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004189
4190 /* only VSI w/ an associated netdev is set up w/ NAPI */
4191 if (vsi->netdev)
4192 netif_napi_del(&q_vector->napi);
4193
4194 vsi->q_vectors[v_idx] = NULL;
4195
4196 kfree_rcu(q_vector, rcu);
4197}
4198
4199/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004200 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4201 * @vsi: the VSI being un-configured
4202 *
4203 * This frees the memory allocated to the q_vectors and
4204 * deletes references to the NAPI struct.
4205 **/
4206static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4207{
4208 int v_idx;
4209
Alexander Duyck493fb302013-09-28 07:01:44 +00004210 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4211 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004212}
4213
4214/**
4215 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4216 * @pf: board private structure
4217 **/
4218static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4219{
4220 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4221 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4222 pci_disable_msix(pf->pdev);
4223 kfree(pf->msix_entries);
4224 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004225 kfree(pf->irq_pile);
4226 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004227 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4228 pci_disable_msi(pf->pdev);
4229 }
4230 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4231}
4232
4233/**
4234 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4235 * @pf: board private structure
4236 *
4237 * We go through and clear interrupt specific resources and reset the structure
4238 * to pre-load conditions
4239 **/
4240static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4241{
4242 int i;
4243
Shannon Nelsone1477582015-02-21 06:44:33 +00004244 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004245 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004246 synchronize_irq(pf->msix_entries[0].vector);
4247 free_irq(pf->msix_entries[0].vector, pf);
4248 }
4249
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004250 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4251 I40E_IWARP_IRQ_PILE_ID);
4252
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004253 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004254 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004255 if (pf->vsi[i])
4256 i40e_vsi_free_q_vectors(pf->vsi[i]);
4257 i40e_reset_interrupt_capability(pf);
4258}
4259
4260/**
4261 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4262 * @vsi: the VSI being configured
4263 **/
4264static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4265{
4266 int q_idx;
4267
4268 if (!vsi->netdev)
4269 return;
4270
4271 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004272 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004273}
4274
4275/**
4276 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4277 * @vsi: the VSI being configured
4278 **/
4279static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4280{
4281 int q_idx;
4282
4283 if (!vsi->netdev)
4284 return;
4285
4286 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004287 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004288}
4289
4290/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004291 * i40e_vsi_close - Shut down a VSI
4292 * @vsi: the vsi to be quelled
4293 **/
4294static void i40e_vsi_close(struct i40e_vsi *vsi)
4295{
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004296 bool reset = false;
4297
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004298 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4299 i40e_down(vsi);
4300 i40e_vsi_free_irq(vsi);
4301 i40e_vsi_free_tx_resources(vsi);
4302 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004303 vsi->current_netdev_flags = 0;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004304 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4305 reset = true;
4306 i40e_notify_client_of_netdev_close(vsi, reset);
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004307}
4308
4309/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004310 * i40e_quiesce_vsi - Pause a given VSI
4311 * @vsi: the VSI being paused
4312 **/
4313static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4314{
4315 if (test_bit(__I40E_DOWN, &vsi->state))
4316 return;
4317
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004318 /* No need to disable FCoE VSI when Tx suspended */
4319 if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
4320 vsi->type == I40E_VSI_FCOE) {
4321 dev_dbg(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004322 "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004323 return;
4324 }
4325
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004326 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004327 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004328 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004329 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004330 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004331}
4332
4333/**
4334 * i40e_unquiesce_vsi - Resume a given VSI
4335 * @vsi: the VSI being resumed
4336 **/
4337static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4338{
4339 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4340 return;
4341
4342 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4343 if (vsi->netdev && netif_running(vsi->netdev))
4344 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4345 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004346 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004347}
4348
4349/**
4350 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4351 * @pf: the PF
4352 **/
4353static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4354{
4355 int v;
4356
Mitch Williams505682c2014-05-20 08:01:37 +00004357 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004358 if (pf->vsi[v])
4359 i40e_quiesce_vsi(pf->vsi[v]);
4360 }
4361}
4362
4363/**
4364 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4365 * @pf: the PF
4366 **/
4367static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4368{
4369 int v;
4370
Mitch Williams505682c2014-05-20 08:01:37 +00004371 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004372 if (pf->vsi[v])
4373 i40e_unquiesce_vsi(pf->vsi[v]);
4374 }
4375}
4376
Neerav Parikh69129dc2014-11-12 00:18:46 +00004377#ifdef CONFIG_I40E_DCB
4378/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004379 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004380 * @vsi: the VSI being configured
4381 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004382 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004383 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004384static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004385{
4386 struct i40e_pf *pf = vsi->back;
4387 int i, pf_q, ret;
4388
4389 pf_q = vsi->base_queue;
4390 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4391 /* Check and wait for the disable status of the queue */
4392 ret = i40e_pf_txq_wait(pf, pf_q, false);
4393 if (ret) {
4394 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004395 "VSI seid %d Tx ring %d disable timeout\n",
4396 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004397 return ret;
4398 }
4399 }
4400
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004401 pf_q = vsi->base_queue;
4402 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4403 /* Check and wait for the disable status of the queue */
4404 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4405 if (ret) {
4406 dev_info(&pf->pdev->dev,
4407 "VSI seid %d Rx ring %d disable timeout\n",
4408 vsi->seid, pf_q);
4409 return ret;
4410 }
4411 }
4412
Neerav Parikh69129dc2014-11-12 00:18:46 +00004413 return 0;
4414}
4415
4416/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004417 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004418 * @pf: the PF
4419 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004420 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004421 * VSIs that are managed by this PF.
4422 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004423static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004424{
4425 int v, ret = 0;
4426
4427 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Neerav Parikhd341b7a2014-11-12 00:18:51 +00004428 /* No need to wait for FCoE VSI queues */
4429 if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004430 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004431 if (ret)
4432 break;
4433 }
4434 }
4435
4436 return ret;
4437}
4438
4439#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004440
4441/**
4442 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4443 * @q_idx: TX queue number
4444 * @vsi: Pointer to VSI struct
4445 *
4446 * This function checks specified queue for given VSI. Detects hung condition.
4447 * Sets hung bit since it is two step process. Before next run of service task
4448 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4449 * hung condition remain unchanged and during subsequent run, this function
4450 * issues SW interrupt to recover from hung condition.
4451 **/
4452static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4453{
4454 struct i40e_ring *tx_ring = NULL;
4455 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004456 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004457 int i;
4458
4459 pf = vsi->back;
4460
4461 /* now that we have an index, find the tx_ring struct */
4462 for (i = 0; i < vsi->num_queue_pairs; i++) {
4463 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4464 if (q_idx == vsi->tx_rings[i]->queue_index) {
4465 tx_ring = vsi->tx_rings[i];
4466 break;
4467 }
4468 }
4469 }
4470
4471 if (!tx_ring)
4472 return;
4473
4474 /* Read interrupt register */
4475 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4476 val = rd32(&pf->hw,
4477 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4478 tx_ring->vsi->base_vector - 1));
4479 else
4480 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4481
4482 head = i40e_get_head(tx_ring);
4483
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004484 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004485
Kiran Patil9c6c1252015-11-06 15:26:02 -08004486 /* HW is done executing descriptors, updated HEAD write back,
4487 * but SW hasn't processed those descriptors. If interrupt is
4488 * not generated from this point ON, it could result into
4489 * dev_watchdog detecting timeout on those netdev_queue,
4490 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004491 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004492 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004493 /* NAPI Poll didn't run and clear since it was set */
4494 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4495 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004496 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",
4497 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004498 tx_ring->next_to_clean, head,
4499 tx_ring->next_to_use,
4500 readl(tx_ring->tail));
4501 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4502 vsi->seid, q_idx, val);
4503 i40e_force_wb(vsi, tx_ring->q_vector);
4504 } else {
4505 /* First Chance - detected possible hung */
4506 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4507 &tx_ring->q_vector->hung_detected);
4508 }
4509 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004510
4511 /* This is the case where we have interrupts missing,
4512 * so the tx_pending in HW will most likely be 0, but we
4513 * will have tx_pending in SW since the WB happened but the
4514 * interrupt got lost.
4515 */
4516 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4517 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
4518 if (napi_reschedule(&tx_ring->q_vector->napi))
4519 tx_ring->tx_stats.tx_lost_interrupt++;
4520 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004521}
4522
4523/**
4524 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4525 * @pf: pointer to PF struct
4526 *
4527 * LAN VSI has netdev and netdev has TX queues. This function is to check
4528 * each of those TX queues if they are hung, trigger recovery by issuing
4529 * SW interrupt.
4530 **/
4531static void i40e_detect_recover_hung(struct i40e_pf *pf)
4532{
4533 struct net_device *netdev;
4534 struct i40e_vsi *vsi;
4535 int i;
4536
4537 /* Only for LAN VSI */
4538 vsi = pf->vsi[pf->lan_vsi];
4539
4540 if (!vsi)
4541 return;
4542
4543 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4544 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4545 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4546 return;
4547
4548 /* Make sure type is MAIN VSI */
4549 if (vsi->type != I40E_VSI_MAIN)
4550 return;
4551
4552 netdev = vsi->netdev;
4553 if (!netdev)
4554 return;
4555
4556 /* Bail out if netif_carrier is not OK */
4557 if (!netif_carrier_ok(netdev))
4558 return;
4559
4560 /* Go thru' TX queues for netdev */
4561 for (i = 0; i < netdev->num_tx_queues; i++) {
4562 struct netdev_queue *q;
4563
4564 q = netdev_get_tx_queue(netdev, i);
4565 if (q)
4566 i40e_detect_recover_hung_queue(i, vsi);
4567 }
4568}
4569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004570/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004571 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004572 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004573 *
4574 * Get TC map for ISCSI PF type that will include iSCSI TC
4575 * and LAN TC.
4576 **/
4577static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4578{
4579 struct i40e_dcb_app_priority_table app;
4580 struct i40e_hw *hw = &pf->hw;
4581 u8 enabled_tc = 1; /* TC0 is always enabled */
4582 u8 tc, i;
4583 /* Get the iSCSI APP TLV */
4584 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4585
4586 for (i = 0; i < dcbcfg->numapps; i++) {
4587 app = dcbcfg->app[i];
4588 if (app.selector == I40E_APP_SEL_TCPIP &&
4589 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4590 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004591 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004592 break;
4593 }
4594 }
4595
4596 return enabled_tc;
4597}
4598
4599/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004600 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4601 * @dcbcfg: the corresponding DCBx configuration structure
4602 *
4603 * Return the number of TCs from given DCBx configuration
4604 **/
4605static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4606{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004607 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004608 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004609 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004610
4611 /* Scan the ETS Config Priority Table to find
4612 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004613 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004614 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004615 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4616 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4617
4618 /* Now scan the bitmask to check for
4619 * contiguous TCs starting with TC0
4620 */
4621 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4622 if (num_tc & BIT(i)) {
4623 if (!tc_unused) {
4624 ret++;
4625 } else {
4626 pr_err("Non-contiguous TC - Disabling DCB\n");
4627 return 1;
4628 }
4629 } else {
4630 tc_unused = 1;
4631 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004632 }
4633
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004634 /* There is always at least TC0 */
4635 if (!ret)
4636 ret = 1;
4637
4638 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004639}
4640
4641/**
4642 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4643 * @dcbcfg: the corresponding DCBx configuration structure
4644 *
4645 * Query the current DCB configuration and return the number of
4646 * traffic classes enabled from the given DCBX config
4647 **/
4648static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4649{
4650 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4651 u8 enabled_tc = 1;
4652 u8 i;
4653
4654 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004655 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004656
4657 return enabled_tc;
4658}
4659
4660/**
4661 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4662 * @pf: PF being queried
4663 *
4664 * Return number of traffic classes enabled for the given PF
4665 **/
4666static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4667{
4668 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004669 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004670 u8 num_tc = 0;
4671 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4672
4673 /* If DCB is not enabled then always in single TC */
4674 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4675 return 1;
4676
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004677 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004678 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4679 return i40e_dcb_get_num_tc(dcbcfg);
4680
4681 /* MFP mode return count of enabled TCs for this PF */
4682 if (pf->hw.func_caps.iscsi)
4683 enabled_tc = i40e_get_iscsi_tc_map(pf);
4684 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004685 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004686
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004687 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004688 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004689 num_tc++;
4690 }
4691 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004692}
4693
4694/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004695 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4696 * @pf: PF being queried
4697 *
4698 * Return a bitmap for enabled traffic classes for this PF.
4699 **/
4700static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4701{
4702 /* If DCB is not enabled for this PF then just return default TC */
4703 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004704 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004705
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004706 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004707 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4708 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4709
Neerav Parikhfc51de92015-02-24 06:58:53 +00004710 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004711 if (pf->hw.func_caps.iscsi)
4712 return i40e_get_iscsi_tc_map(pf);
4713 else
David Ertmanea6acb72016-09-20 07:10:50 -07004714 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004715}
4716
4717/**
4718 * i40e_vsi_get_bw_info - Query VSI BW Information
4719 * @vsi: the VSI being queried
4720 *
4721 * Returns 0 on success, negative value on failure
4722 **/
4723static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4724{
4725 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4726 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4727 struct i40e_pf *pf = vsi->back;
4728 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004729 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004730 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004731 int i;
4732
4733 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004734 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4735 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004736 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004737 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4738 i40e_stat_str(&pf->hw, ret),
4739 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004740 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004741 }
4742
4743 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004744 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4745 NULL);
4746 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004747 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004748 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4749 i40e_stat_str(&pf->hw, ret),
4750 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004751 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004752 }
4753
4754 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4755 dev_info(&pf->pdev->dev,
4756 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4757 bw_config.tc_valid_bits,
4758 bw_ets_config.tc_valid_bits);
4759 /* Still continuing */
4760 }
4761
4762 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4763 vsi->bw_max_quanta = bw_config.max_bw;
4764 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4765 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4766 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4767 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4768 vsi->bw_ets_limit_credits[i] =
4769 le16_to_cpu(bw_ets_config.credits[i]);
4770 /* 3 bits out of 4 for each TC */
4771 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4772 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004773
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004774 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004775}
4776
4777/**
4778 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4779 * @vsi: the VSI being configured
4780 * @enabled_tc: TC bitmap
4781 * @bw_credits: BW shared credits per TC
4782 *
4783 * Returns 0 on success, negative value on failure
4784 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004785static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004786 u8 *bw_share)
4787{
4788 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004789 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004790 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004791
4792 bw_data.tc_valid_bits = enabled_tc;
4793 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4794 bw_data.tc_bw_credits[i] = bw_share[i];
4795
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004796 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4797 NULL);
4798 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004799 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004800 "AQ command Config VSI BW allocation per TC failed = %d\n",
4801 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004802 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004803 }
4804
4805 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4806 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4807
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004808 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004809}
4810
4811/**
4812 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4813 * @vsi: the VSI being configured
4814 * @enabled_tc: TC map to be enabled
4815 *
4816 **/
4817static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4818{
4819 struct net_device *netdev = vsi->netdev;
4820 struct i40e_pf *pf = vsi->back;
4821 struct i40e_hw *hw = &pf->hw;
4822 u8 netdev_tc = 0;
4823 int i;
4824 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4825
4826 if (!netdev)
4827 return;
4828
4829 if (!enabled_tc) {
4830 netdev_reset_tc(netdev);
4831 return;
4832 }
4833
4834 /* Set up actual enabled TCs on the VSI */
4835 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4836 return;
4837
4838 /* set per TC queues for the VSI */
4839 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4840 /* Only set TC queues for enabled tcs
4841 *
4842 * e.g. For a VSI that has TC0 and TC3 enabled the
4843 * enabled_tc bitmap would be 0x00001001; the driver
4844 * will set the numtc for netdev as 2 that will be
4845 * referenced by the netdev layer as TC 0 and 1.
4846 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004847 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004848 netdev_set_tc_queue(netdev,
4849 vsi->tc_config.tc_info[i].netdev_tc,
4850 vsi->tc_config.tc_info[i].qcount,
4851 vsi->tc_config.tc_info[i].qoffset);
4852 }
4853
4854 /* Assign UP2TC map for the VSI */
4855 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4856 /* Get the actual TC# for the UP */
4857 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4858 /* Get the mapped netdev TC# for the UP */
4859 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4860 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4861 }
4862}
4863
4864/**
4865 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4866 * @vsi: the VSI being configured
4867 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4868 **/
4869static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4870 struct i40e_vsi_context *ctxt)
4871{
4872 /* copy just the sections touched not the entire info
4873 * since not all sections are valid as returned by
4874 * update vsi params
4875 */
4876 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4877 memcpy(&vsi->info.queue_mapping,
4878 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4879 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4880 sizeof(vsi->info.tc_mapping));
4881}
4882
4883/**
4884 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4885 * @vsi: VSI to be configured
4886 * @enabled_tc: TC bitmap
4887 *
4888 * This configures a particular VSI for TCs that are mapped to the
4889 * given TC bitmap. It uses default bandwidth share for TCs across
4890 * VSIs to configure TC for a particular VSI.
4891 *
4892 * NOTE:
4893 * It is expected that the VSI queues have been quisced before calling
4894 * this function.
4895 **/
4896static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4897{
4898 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4899 struct i40e_vsi_context ctxt;
4900 int ret = 0;
4901 int i;
4902
4903 /* Check if enabled_tc is same as existing or new TCs */
4904 if (vsi->tc_config.enabled_tc == enabled_tc)
4905 return ret;
4906
4907 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4908 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004909 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004910 bw_share[i] = 1;
4911 }
4912
4913 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4914 if (ret) {
4915 dev_info(&vsi->back->pdev->dev,
4916 "Failed configuring TC map %d for VSI %d\n",
4917 enabled_tc, vsi->seid);
4918 goto out;
4919 }
4920
4921 /* Update Queue Pairs Mapping for currently enabled UPs */
4922 ctxt.seid = vsi->seid;
4923 ctxt.pf_num = vsi->back->hw.pf_id;
4924 ctxt.vf_num = 0;
4925 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004926 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004927 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4928
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004929 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4930 ctxt.info.valid_sections |=
4931 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4932 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4933 }
4934
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004935 /* Update the VSI after updating the VSI queue-mapping information */
4936 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4937 if (ret) {
4938 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004939 "Update vsi tc config failed, err %s aq_err %s\n",
4940 i40e_stat_str(&vsi->back->hw, ret),
4941 i40e_aq_str(&vsi->back->hw,
4942 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004943 goto out;
4944 }
4945 /* update the local VSI info with updated queue map */
4946 i40e_vsi_update_queue_map(vsi, &ctxt);
4947 vsi->info.valid_sections = 0;
4948
4949 /* Update current VSI BW information */
4950 ret = i40e_vsi_get_bw_info(vsi);
4951 if (ret) {
4952 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004953 "Failed updating vsi bw info, err %s aq_err %s\n",
4954 i40e_stat_str(&vsi->back->hw, ret),
4955 i40e_aq_str(&vsi->back->hw,
4956 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004957 goto out;
4958 }
4959
4960 /* Update the netdev TC setup */
4961 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4962out:
4963 return ret;
4964}
4965
4966/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004967 * i40e_veb_config_tc - Configure TCs for given VEB
4968 * @veb: given VEB
4969 * @enabled_tc: TC bitmap
4970 *
4971 * Configures given TC bitmap for VEB (switching) element
4972 **/
4973int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
4974{
4975 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
4976 struct i40e_pf *pf = veb->pf;
4977 int ret = 0;
4978 int i;
4979
4980 /* No TCs or already enabled TCs just return */
4981 if (!enabled_tc || veb->enabled_tc == enabled_tc)
4982 return ret;
4983
4984 bw_data.tc_valid_bits = enabled_tc;
4985 /* bw_data.absolute_credits is not set (relative) */
4986
4987 /* Enable ETS TCs with equal BW Share for now */
4988 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004989 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004990 bw_data.tc_bw_share_credits[i] = 1;
4991 }
4992
4993 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
4994 &bw_data, NULL);
4995 if (ret) {
4996 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004997 "VEB bw config failed, err %s aq_err %s\n",
4998 i40e_stat_str(&pf->hw, ret),
4999 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005000 goto out;
5001 }
5002
5003 /* Update the BW information */
5004 ret = i40e_veb_get_bw_info(veb);
5005 if (ret) {
5006 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005007 "Failed getting veb bw config, err %s aq_err %s\n",
5008 i40e_stat_str(&pf->hw, ret),
5009 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005010 }
5011
5012out:
5013 return ret;
5014}
5015
5016#ifdef CONFIG_I40E_DCB
5017/**
5018 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5019 * @pf: PF struct
5020 *
5021 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5022 * the caller would've quiesce all the VSIs before calling
5023 * this function
5024 **/
5025static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5026{
5027 u8 tc_map = 0;
5028 int ret;
5029 u8 v;
5030
5031 /* Enable the TCs available on PF to all VEBs */
5032 tc_map = i40e_pf_get_tc_map(pf);
5033 for (v = 0; v < I40E_MAX_VEB; v++) {
5034 if (!pf->veb[v])
5035 continue;
5036 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5037 if (ret) {
5038 dev_info(&pf->pdev->dev,
5039 "Failed configuring TC for VEB seid=%d\n",
5040 pf->veb[v]->seid);
5041 /* Will try to configure as many components */
5042 }
5043 }
5044
5045 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005046 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005047 if (!pf->vsi[v])
5048 continue;
5049
5050 /* - Enable all TCs for the LAN VSI
Vasu Dev38e00432014-08-01 13:27:03 -07005051#ifdef I40E_FCOE
5052 * - For FCoE VSI only enable the TC configured
5053 * as per the APP TLV
5054#endif
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005055 * - For all others keep them at TC0 for now
5056 */
5057 if (v == pf->lan_vsi)
5058 tc_map = i40e_pf_get_tc_map(pf);
5059 else
David Ertmanea6acb72016-09-20 07:10:50 -07005060 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Vasu Dev38e00432014-08-01 13:27:03 -07005061#ifdef I40E_FCOE
5062 if (pf->vsi[v]->type == I40E_VSI_FCOE)
5063 tc_map = i40e_get_fcoe_tc_map(pf);
5064#endif /* #ifdef I40E_FCOE */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005065
5066 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5067 if (ret) {
5068 dev_info(&pf->pdev->dev,
5069 "Failed configuring TC for VSI seid=%d\n",
5070 pf->vsi[v]->seid);
5071 /* Will try to configure as many components */
5072 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005073 /* Re-configure VSI vectors based on updated TC map */
5074 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005075 if (pf->vsi[v]->netdev)
5076 i40e_dcbnl_set_all(pf->vsi[v]);
5077 }
5078 }
5079}
5080
5081/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005082 * i40e_resume_port_tx - Resume port Tx
5083 * @pf: PF struct
5084 *
5085 * Resume a port's Tx and issue a PF reset in case of failure to
5086 * resume.
5087 **/
5088static int i40e_resume_port_tx(struct i40e_pf *pf)
5089{
5090 struct i40e_hw *hw = &pf->hw;
5091 int ret;
5092
5093 ret = i40e_aq_resume_port_tx(hw, NULL);
5094 if (ret) {
5095 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005096 "Resume Port Tx failed, err %s aq_err %s\n",
5097 i40e_stat_str(&pf->hw, ret),
5098 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005099 /* Schedule PF reset to recover */
5100 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5101 i40e_service_event_schedule(pf);
5102 }
5103
5104 return ret;
5105}
5106
5107/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005108 * i40e_init_pf_dcb - Initialize DCB configuration
5109 * @pf: PF being configured
5110 *
5111 * Query the current DCB configuration and cache it
5112 * in the hardware structure
5113 **/
5114static int i40e_init_pf_dcb(struct i40e_pf *pf)
5115{
5116 struct i40e_hw *hw = &pf->hw;
5117 int err = 0;
5118
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005119 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005120 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005121 goto out;
5122
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005123 /* Get the initial DCB configuration */
5124 err = i40e_init_dcb(hw);
5125 if (!err) {
5126 /* Device/Function is not DCBX capable */
5127 if ((!hw->func_caps.dcb) ||
5128 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5129 dev_info(&pf->pdev->dev,
5130 "DCBX offload is not supported or is disabled for this PF.\n");
5131
5132 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5133 goto out;
5134
5135 } else {
5136 /* When status is not DISABLED then DCBX in FW */
5137 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5138 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005139
5140 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005141 /* Enable DCB tagging only when more than one TC
5142 * or explicitly disable if only one TC
5143 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005144 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5145 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005146 else
5147 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005148 dev_dbg(&pf->pdev->dev,
5149 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005150 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005151 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005152 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005153 "Query for DCB configuration failed, err %s aq_err %s\n",
5154 i40e_stat_str(&pf->hw, err),
5155 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005156 }
5157
5158out:
5159 return err;
5160}
5161#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005162#define SPEED_SIZE 14
5163#define FC_SIZE 8
5164/**
5165 * i40e_print_link_message - print link up or down
5166 * @vsi: the VSI for which link needs a message
5167 */
Matt Jaredc156f852015-08-27 11:42:39 -04005168void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005169{
Shannon Nelsona9165492015-09-03 17:19:00 -04005170 char *speed = "Unknown";
5171 char *fc = "Unknown";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005172
Matt Jaredc156f852015-08-27 11:42:39 -04005173 if (vsi->current_isup == isup)
5174 return;
5175 vsi->current_isup = isup;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005176 if (!isup) {
5177 netdev_info(vsi->netdev, "NIC Link is Down\n");
5178 return;
5179 }
5180
Greg Rose148c2d82014-12-11 07:06:27 +00005181 /* Warn user if link speed on NPAR enabled partition is not at
5182 * least 10GB
5183 */
5184 if (vsi->back->hw.func_caps.npar_enable &&
5185 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5186 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5187 netdev_warn(vsi->netdev,
5188 "The partition detected link speed that is less than 10Gbps\n");
5189
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005190 switch (vsi->back->hw.phy.link_info.link_speed) {
5191 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005192 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005193 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005194 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005195 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005196 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005197 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005198 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005199 break;
5200 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005201 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005202 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005203 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005204 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005205 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005206 default:
5207 break;
5208 }
5209
5210 switch (vsi->back->hw.fc.current_mode) {
5211 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005212 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005213 break;
5214 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005215 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005216 break;
5217 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005218 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005219 break;
5220 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005221 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005222 break;
5223 }
5224
Shannon Nelsona9165492015-09-03 17:19:00 -04005225 netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005226 speed, fc);
5227}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005228
5229/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005230 * i40e_up_complete - Finish the last steps of bringing up a connection
5231 * @vsi: the VSI being configured
5232 **/
5233static int i40e_up_complete(struct i40e_vsi *vsi)
5234{
5235 struct i40e_pf *pf = vsi->back;
5236 int err;
5237
5238 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5239 i40e_vsi_configure_msix(vsi);
5240 else
5241 i40e_configure_msi_and_legacy(vsi);
5242
5243 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005244 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005245 if (err)
5246 return err;
5247
5248 clear_bit(__I40E_DOWN, &vsi->state);
5249 i40e_napi_enable_all(vsi);
5250 i40e_vsi_enable_irq(vsi);
5251
5252 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5253 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005254 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005255 netif_tx_start_all_queues(vsi->netdev);
5256 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005257 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005258 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005259 /* need to check for qualified module here*/
5260 if ((pf->hw.phy.link_info.link_info &
5261 I40E_AQ_MEDIA_AVAILABLE) &&
5262 (!(pf->hw.phy.link_info.an_info &
5263 I40E_AQ_QUALIFIED_MODULE)))
5264 netdev_err(vsi->netdev,
5265 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005266 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005267
5268 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005269 if (vsi->type == I40E_VSI_FDIR) {
5270 /* reset fd counters */
5271 pf->fd_add_err = pf->fd_atr_cnt = 0;
5272 if (pf->fd_tcp_rule > 0) {
Jacob Keller234dc4e2016-09-06 18:05:09 -07005273 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005274 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5275 dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005276 pf->fd_tcp_rule = 0;
5277 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005278 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005279 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005280
5281 /* On the next run of the service_task, notify any clients of the new
5282 * opened netdev
5283 */
5284 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005285 i40e_service_event_schedule(pf);
5286
5287 return 0;
5288}
5289
5290/**
5291 * i40e_vsi_reinit_locked - Reset the VSI
5292 * @vsi: the VSI being configured
5293 *
5294 * Rebuild the ring structs after some configuration
5295 * has changed, e.g. MTU size.
5296 **/
5297static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5298{
5299 struct i40e_pf *pf = vsi->back;
5300
5301 WARN_ON(in_interrupt());
5302 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5303 usleep_range(1000, 2000);
5304 i40e_down(vsi);
5305
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005306 i40e_up(vsi);
5307 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5308}
5309
5310/**
5311 * i40e_up - Bring the connection back up after being down
5312 * @vsi: the VSI being configured
5313 **/
5314int i40e_up(struct i40e_vsi *vsi)
5315{
5316 int err;
5317
5318 err = i40e_vsi_configure(vsi);
5319 if (!err)
5320 err = i40e_up_complete(vsi);
5321
5322 return err;
5323}
5324
5325/**
5326 * i40e_down - Shutdown the connection processing
5327 * @vsi: the VSI being stopped
5328 **/
5329void i40e_down(struct i40e_vsi *vsi)
5330{
5331 int i;
5332
5333 /* It is assumed that the caller of this function
5334 * sets the vsi->state __I40E_DOWN bit.
5335 */
5336 if (vsi->netdev) {
5337 netif_carrier_off(vsi->netdev);
5338 netif_tx_disable(vsi->netdev);
5339 }
5340 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005341 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005342 i40e_napi_disable_all(vsi);
5343
5344 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005345 i40e_clean_tx_ring(vsi->tx_rings[i]);
5346 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005347 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005348
5349 i40e_notify_client_of_netdev_close(vsi, false);
5350
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005351}
5352
5353/**
5354 * i40e_setup_tc - configure multiple traffic classes
5355 * @netdev: net device to configure
5356 * @tc: number of traffic classes to enable
5357 **/
5358static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5359{
5360 struct i40e_netdev_priv *np = netdev_priv(netdev);
5361 struct i40e_vsi *vsi = np->vsi;
5362 struct i40e_pf *pf = vsi->back;
5363 u8 enabled_tc = 0;
5364 int ret = -EINVAL;
5365 int i;
5366
5367 /* Check if DCB enabled to continue */
5368 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5369 netdev_info(netdev, "DCB is not enabled for adapter\n");
5370 goto exit;
5371 }
5372
5373 /* Check if MFP enabled */
5374 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5375 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5376 goto exit;
5377 }
5378
5379 /* Check whether tc count is within enabled limit */
5380 if (tc > i40e_pf_get_num_tc(pf)) {
5381 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5382 goto exit;
5383 }
5384
5385 /* Generate TC map for number of tc requested */
5386 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005387 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005388
5389 /* Requesting same TC configuration as already enabled */
5390 if (enabled_tc == vsi->tc_config.enabled_tc)
5391 return 0;
5392
5393 /* Quiesce VSI queues */
5394 i40e_quiesce_vsi(vsi);
5395
5396 /* Configure VSI for enabled TCs */
5397 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5398 if (ret) {
5399 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5400 vsi->seid);
5401 goto exit;
5402 }
5403
5404 /* Unquiesce VSI */
5405 i40e_unquiesce_vsi(vsi);
5406
5407exit:
5408 return ret;
5409}
5410
John Fastabende4c67342016-02-16 21:16:15 -08005411#ifdef I40E_FCOE
John Fastabend16e5cc62016-02-16 21:16:43 -08005412int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5413 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005414#else
John Fastabend16e5cc62016-02-16 21:16:43 -08005415static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5416 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005417#endif
5418{
John Fastabend16e5cc62016-02-16 21:16:43 -08005419 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005420 return -EINVAL;
John Fastabend16e5cc62016-02-16 21:16:43 -08005421 return i40e_setup_tc(netdev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08005422}
5423
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005424/**
5425 * i40e_open - Called when a network interface is made active
5426 * @netdev: network interface device structure
5427 *
5428 * The open entry point is called when a network interface is made
5429 * active by the system (IFF_UP). At this point all resources needed
5430 * for transmit and receive operations are allocated, the interrupt
5431 * handler is registered with the OS, the netdev watchdog subtask is
5432 * enabled, and the stack is notified that the interface is ready.
5433 *
5434 * Returns 0 on success, negative value on failure
5435 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005436int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005437{
5438 struct i40e_netdev_priv *np = netdev_priv(netdev);
5439 struct i40e_vsi *vsi = np->vsi;
5440 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005441 int err;
5442
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005443 /* disallow open during test or if eeprom is broken */
5444 if (test_bit(__I40E_TESTING, &pf->state) ||
5445 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005446 return -EBUSY;
5447
5448 netif_carrier_off(netdev);
5449
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005450 err = i40e_vsi_open(vsi);
5451 if (err)
5452 return err;
5453
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005454 /* configure global TSO hardware offload settings */
5455 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5456 TCP_FLAG_FIN) >> 16);
5457 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5458 TCP_FLAG_FIN |
5459 TCP_FLAG_CWR) >> 16);
5460 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5461
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005462 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005463
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005464 return 0;
5465}
5466
5467/**
5468 * i40e_vsi_open -
5469 * @vsi: the VSI to open
5470 *
5471 * Finish initialization of the VSI.
5472 *
5473 * Returns 0 on success, negative value on failure
5474 **/
5475int i40e_vsi_open(struct i40e_vsi *vsi)
5476{
5477 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005478 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005479 int err;
5480
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005481 /* allocate descriptors */
5482 err = i40e_vsi_setup_tx_resources(vsi);
5483 if (err)
5484 goto err_setup_tx;
5485 err = i40e_vsi_setup_rx_resources(vsi);
5486 if (err)
5487 goto err_setup_rx;
5488
5489 err = i40e_vsi_configure(vsi);
5490 if (err)
5491 goto err_setup_rx;
5492
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005493 if (vsi->netdev) {
5494 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5495 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5496 err = i40e_vsi_request_irq(vsi, int_name);
5497 if (err)
5498 goto err_setup_rx;
5499
5500 /* Notify the stack of the actual queue counts. */
5501 err = netif_set_real_num_tx_queues(vsi->netdev,
5502 vsi->num_queue_pairs);
5503 if (err)
5504 goto err_set_queues;
5505
5506 err = netif_set_real_num_rx_queues(vsi->netdev,
5507 vsi->num_queue_pairs);
5508 if (err)
5509 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005510
5511 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005512 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005513 dev_driver_string(&pf->pdev->dev),
5514 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005515 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005516
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005517 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005518 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005519 goto err_setup_rx;
5520 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005521
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005522 err = i40e_up_complete(vsi);
5523 if (err)
5524 goto err_up_complete;
5525
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005526 return 0;
5527
5528err_up_complete:
5529 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005530err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005531 i40e_vsi_free_irq(vsi);
5532err_setup_rx:
5533 i40e_vsi_free_rx_resources(vsi);
5534err_setup_tx:
5535 i40e_vsi_free_tx_resources(vsi);
5536 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005537 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005538
5539 return err;
5540}
5541
5542/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005543 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005544 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005545 *
5546 * This function destroys the hlist where all the Flow Director
5547 * filters were saved.
5548 **/
5549static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5550{
5551 struct i40e_fdir_filter *filter;
5552 struct hlist_node *node2;
5553
5554 hlist_for_each_entry_safe(filter, node2,
5555 &pf->fdir_filter_list, fdir_node) {
5556 hlist_del(&filter->fdir_node);
5557 kfree(filter);
5558 }
5559 pf->fdir_pf_active_filters = 0;
5560}
5561
5562/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005563 * i40e_close - Disables a network interface
5564 * @netdev: network interface device structure
5565 *
5566 * The close entry point is called when an interface is de-activated
5567 * by the OS. The hardware is still under the driver's control, but
5568 * this netdev interface is disabled.
5569 *
5570 * Returns 0, this is not allowed to fail
5571 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005572int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005573{
5574 struct i40e_netdev_priv *np = netdev_priv(netdev);
5575 struct i40e_vsi *vsi = np->vsi;
5576
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005577 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005578
5579 return 0;
5580}
5581
5582/**
5583 * i40e_do_reset - Start a PF or Core Reset sequence
5584 * @pf: board private structure
5585 * @reset_flags: which reset is requested
5586 *
5587 * The essential difference in resets is that the PF Reset
5588 * doesn't clear the packet buffers, doesn't reset the PE
5589 * firmware, and doesn't bother the other PFs on the chip.
5590 **/
5591void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5592{
5593 u32 val;
5594
5595 WARN_ON(in_interrupt());
5596
Mitch Williams263fc482014-04-23 04:50:11 +00005597
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005598 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005599 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005600
5601 /* Request a Global Reset
5602 *
5603 * This will start the chip's countdown to the actual full
5604 * chip reset event, and a warning interrupt to be sent
5605 * to all PFs, including the requestor. Our handler
5606 * for the warning interrupt will deal with the shutdown
5607 * and recovery of the switch setup.
5608 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005609 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005610 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5611 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5612 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5613
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005614 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005615
5616 /* Request a Core Reset
5617 *
5618 * Same as Global Reset, except does *not* include the MAC/PHY
5619 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005620 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005621 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5622 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5623 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5624 i40e_flush(&pf->hw);
5625
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005626 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005627
5628 /* Request a PF Reset
5629 *
5630 * Resets only the PF-specific registers
5631 *
5632 * This goes directly to the tear-down and rebuild of
5633 * the switch, since we need to do all the recovery as
5634 * for the Core Reset.
5635 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005636 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005637 i40e_handle_reset_warning(pf);
5638
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005639 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005640 int v;
5641
5642 /* Find the VSI(s) that requested a re-init */
5643 dev_info(&pf->pdev->dev,
5644 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005645 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005646 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005647
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005648 if (vsi != NULL &&
5649 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5650 i40e_vsi_reinit_locked(pf->vsi[v]);
5651 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5652 }
5653 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005654 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005655 int v;
5656
5657 /* Find the VSI(s) that needs to be brought down */
5658 dev_info(&pf->pdev->dev, "VSI down requested\n");
5659 for (v = 0; v < pf->num_alloc_vsi; v++) {
5660 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005661
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005662 if (vsi != NULL &&
5663 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5664 set_bit(__I40E_DOWN, &vsi->state);
5665 i40e_down(vsi);
5666 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5667 }
5668 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005669 } else {
5670 dev_info(&pf->pdev->dev,
5671 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005672 }
5673}
5674
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005675#ifdef CONFIG_I40E_DCB
5676/**
5677 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5678 * @pf: board private structure
5679 * @old_cfg: current DCB config
5680 * @new_cfg: new DCB config
5681 **/
5682bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5683 struct i40e_dcbx_config *old_cfg,
5684 struct i40e_dcbx_config *new_cfg)
5685{
5686 bool need_reconfig = false;
5687
5688 /* Check if ETS configuration has changed */
5689 if (memcmp(&new_cfg->etscfg,
5690 &old_cfg->etscfg,
5691 sizeof(new_cfg->etscfg))) {
5692 /* If Priority Table has changed reconfig is needed */
5693 if (memcmp(&new_cfg->etscfg.prioritytable,
5694 &old_cfg->etscfg.prioritytable,
5695 sizeof(new_cfg->etscfg.prioritytable))) {
5696 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005697 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005698 }
5699
5700 if (memcmp(&new_cfg->etscfg.tcbwtable,
5701 &old_cfg->etscfg.tcbwtable,
5702 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005703 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005704
5705 if (memcmp(&new_cfg->etscfg.tsatable,
5706 &old_cfg->etscfg.tsatable,
5707 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005708 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005709 }
5710
5711 /* Check if PFC configuration has changed */
5712 if (memcmp(&new_cfg->pfc,
5713 &old_cfg->pfc,
5714 sizeof(new_cfg->pfc))) {
5715 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005716 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005717 }
5718
5719 /* Check if APP Table has changed */
5720 if (memcmp(&new_cfg->app,
5721 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005722 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005723 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005724 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005725 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005726
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005727 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005728 return need_reconfig;
5729}
5730
5731/**
5732 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5733 * @pf: board private structure
5734 * @e: event info posted on ARQ
5735 **/
5736static int i40e_handle_lldp_event(struct i40e_pf *pf,
5737 struct i40e_arq_event_info *e)
5738{
5739 struct i40e_aqc_lldp_get_mib *mib =
5740 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5741 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005742 struct i40e_dcbx_config tmp_dcbx_cfg;
5743 bool need_reconfig = false;
5744 int ret = 0;
5745 u8 type;
5746
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005747 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005748 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005749 return ret;
5750
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005751 /* Ignore if event is not for Nearest Bridge */
5752 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5753 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005754 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005755 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5756 return ret;
5757
5758 /* Check MIB Type and return if event for Remote MIB update */
5759 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005760 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005761 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005762 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5763 /* Update the remote cached instance and return */
5764 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5765 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5766 &hw->remote_dcbx_config);
5767 goto exit;
5768 }
5769
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005770 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005771 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005772
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005773 /* Reset the old DCBx configuration data */
5774 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005775 /* Get updated DCBX data from firmware */
5776 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005777 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005778 dev_info(&pf->pdev->dev,
5779 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5780 i40e_stat_str(&pf->hw, ret),
5781 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005782 goto exit;
5783 }
5784
5785 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005786 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5787 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005788 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005789 goto exit;
5790 }
5791
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005792 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5793 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005794
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005795 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005796
5797 if (!need_reconfig)
5798 goto exit;
5799
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005800 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005801 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005802 pf->flags |= I40E_FLAG_DCB_ENABLED;
5803 else
5804 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5805
Neerav Parikh69129dc2014-11-12 00:18:46 +00005806 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005807 /* Reconfiguration needed quiesce all VSIs */
5808 i40e_pf_quiesce_all_vsi(pf);
5809
5810 /* Changes in configuration update VEB/VSI */
5811 i40e_dcb_reconfigure(pf);
5812
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005813 ret = i40e_resume_port_tx(pf);
5814
Neerav Parikh69129dc2014-11-12 00:18:46 +00005815 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005816 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005817 if (ret)
5818 goto exit;
5819
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005820 /* Wait for the PF's queues to be disabled */
5821 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005822 if (ret) {
5823 /* Schedule PF reset to recover */
5824 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5825 i40e_service_event_schedule(pf);
5826 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005827 i40e_pf_unquiesce_all_vsi(pf);
Neerav Parikh85a1aab2016-06-07 09:14:55 -07005828 /* Notify the client for the DCB changes */
5829 i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005830 }
5831
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005832exit:
5833 return ret;
5834}
5835#endif /* CONFIG_I40E_DCB */
5836
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005837/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005838 * i40e_do_reset_safe - Protected reset path for userland calls.
5839 * @pf: board private structure
5840 * @reset_flags: which reset is requested
5841 *
5842 **/
5843void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5844{
5845 rtnl_lock();
5846 i40e_do_reset(pf, reset_flags);
5847 rtnl_unlock();
5848}
5849
5850/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005851 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5852 * @pf: board private structure
5853 * @e: event info posted on ARQ
5854 *
5855 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5856 * and VF queues
5857 **/
5858static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5859 struct i40e_arq_event_info *e)
5860{
5861 struct i40e_aqc_lan_overflow *data =
5862 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5863 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5864 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5865 struct i40e_hw *hw = &pf->hw;
5866 struct i40e_vf *vf;
5867 u16 vf_id;
5868
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005869 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5870 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005871
5872 /* Queue belongs to VF, find the VF and issue VF reset */
5873 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5874 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5875 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5876 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5877 vf_id -= hw->func_caps.vf_base_id;
5878 vf = &pf->vf[vf_id];
5879 i40e_vc_notify_vf_reset(vf);
5880 /* Allow VF to process pending reset notification */
5881 msleep(20);
5882 i40e_reset_vf(vf, false);
5883 }
5884}
5885
5886/**
5887 * i40e_service_event_complete - Finish up the service event
5888 * @pf: board private structure
5889 **/
5890static void i40e_service_event_complete(struct i40e_pf *pf)
5891{
Shannon Nelsonb875f992015-10-21 19:47:03 -04005892 WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005893
5894 /* flush memory to make sure state is correct before next watchog */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005895 smp_mb__before_atomic();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005896 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
5897}
5898
5899/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005900 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5901 * @pf: board private structure
5902 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005903u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005904{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005905 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005906
5907 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5908 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5909 return fcnt_prog;
5910}
5911
5912/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005913 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005914 * @pf: board private structure
5915 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005916u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005917{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005918 u32 val, fcnt_prog;
5919
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005920 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5921 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5922 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5923 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5924 return fcnt_prog;
5925}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005926
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005927/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005928 * i40e_get_global_fd_count - Get total FD filters programmed on device
5929 * @pf: board private structure
5930 **/
5931u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5932{
5933 u32 val, fcnt_prog;
5934
5935 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5936 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5937 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5938 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5939 return fcnt_prog;
5940}
5941
5942/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005943 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
5944 * @pf: board private structure
5945 **/
5946void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
5947{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005948 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005949 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005950 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005951
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005952 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
5953 return;
5954
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005955 /* Check if, FD SB or ATR was auto disabled and if there is enough room
5956 * to re-enable
5957 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005958 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005959 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005960 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
5961 (pf->fd_add_err == 0) ||
5962 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005963 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
5964 (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
5965 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005966 if (I40E_DEBUG_FD & pf->hw.debug_mask)
5967 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 +00005968 }
5969 }
Jacob Kellera3417d22016-09-06 18:05:10 -07005970
5971 /* Wait for some more space to be available to turn on ATR. We also
5972 * must check that no existing ntuple rules for TCP are in effect
5973 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005974 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
5975 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Kellera3417d22016-09-06 18:05:10 -07005976 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
5977 (pf->fd_tcp_rule == 0)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005978 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04005979 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07005980 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 +00005981 }
5982 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04005983
5984 /* if hw had a problem adding a filter, delete it */
5985 if (pf->fd_inv > 0) {
5986 hlist_for_each_entry_safe(filter, node,
5987 &pf->fdir_filter_list, fdir_node) {
5988 if (filter->fd_id == pf->fd_inv) {
5989 hlist_del(&filter->fdir_node);
5990 kfree(filter);
5991 pf->fdir_pf_active_filters--;
5992 }
5993 }
5994 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005995}
5996
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005997#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005998#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005999/**
6000 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6001 * @pf: board private structure
6002 **/
6003static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6004{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006005 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006006 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006007 bool disable_atr = false;
6008 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006009 int reg;
6010
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006011 if (!time_after(jiffies, pf->fd_flush_timestamp +
6012 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6013 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006014
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006015 /* If the flush is happening too quick and we have mostly SB rules we
6016 * should not re-enable ATR for some time.
6017 */
6018 min_flush_time = pf->fd_flush_timestamp +
6019 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6020 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006021
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006022 if (!(time_after(jiffies, min_flush_time)) &&
6023 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6024 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6025 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6026 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006027 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006028
6029 pf->fd_flush_timestamp = jiffies;
Jacob Keller234dc4e2016-09-06 18:05:09 -07006030 pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006031 /* flush all filters */
6032 wr32(&pf->hw, I40E_PFQF_CTL_1,
6033 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6034 i40e_flush(&pf->hw);
6035 pf->fd_flush_cnt++;
6036 pf->fd_add_err = 0;
6037 do {
6038 /* Check FD flush status every 5-6msec */
6039 usleep_range(5000, 6000);
6040 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6041 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6042 break;
6043 } while (flush_wait_retry--);
6044 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6045 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6046 } else {
6047 /* replay sideband filters */
6048 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
6049 if (!disable_atr)
Jacob Keller234dc4e2016-09-06 18:05:09 -07006050 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006051 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6052 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6053 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6054 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006055}
6056
6057/**
6058 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6059 * @pf: board private structure
6060 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006061u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006062{
6063 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6064}
6065
6066/* We can see up to 256 filter programming desc in transit if the filters are
6067 * being applied really fast; before we see the first
6068 * filter miss error on Rx queue 0. Accumulating enough error messages before
6069 * reacting will make sure we don't cause flush too often.
6070 */
6071#define I40E_MAX_FD_PROGRAM_ERROR 256
6072
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006073/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006074 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6075 * @pf: board private structure
6076 **/
6077static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6078{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006079
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006080 /* if interface is down do nothing */
6081 if (test_bit(__I40E_DOWN, &pf->state))
6082 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006083
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006084 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006085 i40e_fdir_flush_and_replay(pf);
6086
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006087 i40e_fdir_check_and_reenable(pf);
6088
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006089}
6090
6091/**
6092 * i40e_vsi_link_event - notify VSI of a link event
6093 * @vsi: vsi to be notified
6094 * @link_up: link up or down
6095 **/
6096static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6097{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006098 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006099 return;
6100
6101 switch (vsi->type) {
6102 case I40E_VSI_MAIN:
Vasu Dev38e00432014-08-01 13:27:03 -07006103#ifdef I40E_FCOE
6104 case I40E_VSI_FCOE:
6105#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006106 if (!vsi->netdev || !vsi->netdev_registered)
6107 break;
6108
6109 if (link_up) {
6110 netif_carrier_on(vsi->netdev);
6111 netif_tx_wake_all_queues(vsi->netdev);
6112 } else {
6113 netif_carrier_off(vsi->netdev);
6114 netif_tx_stop_all_queues(vsi->netdev);
6115 }
6116 break;
6117
6118 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006119 case I40E_VSI_VMDQ2:
6120 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006121 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006122 case I40E_VSI_MIRROR:
6123 default:
6124 /* there is no notification for other VSIs */
6125 break;
6126 }
6127}
6128
6129/**
6130 * i40e_veb_link_event - notify elements on the veb of a link event
6131 * @veb: veb to be notified
6132 * @link_up: link up or down
6133 **/
6134static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6135{
6136 struct i40e_pf *pf;
6137 int i;
6138
6139 if (!veb || !veb->pf)
6140 return;
6141 pf = veb->pf;
6142
6143 /* depth first... */
6144 for (i = 0; i < I40E_MAX_VEB; i++)
6145 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6146 i40e_veb_link_event(pf->veb[i], link_up);
6147
6148 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006149 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006150 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6151 i40e_vsi_link_event(pf->vsi[i], link_up);
6152}
6153
6154/**
6155 * i40e_link_event - Update netif_carrier status
6156 * @pf: board private structure
6157 **/
6158static void i40e_link_event(struct i40e_pf *pf)
6159{
Mitch Williams320684c2014-10-17 03:14:43 +00006160 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006161 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006162 i40e_status status;
6163 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006164
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006165 /* save off old link status information */
6166 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6167
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006168 /* set this to force the get_link_status call to refresh state */
6169 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006170
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006171 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006172
6173 status = i40e_get_link_status(&pf->hw, &new_link);
6174 if (status) {
6175 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6176 status);
6177 return;
6178 }
6179
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006180 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6181 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006182
6183 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006184 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006185 (test_bit(__I40E_DOWN, &vsi->state) ||
6186 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006187 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006188
6189 if (!test_bit(__I40E_DOWN, &vsi->state))
6190 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006191
6192 /* Notify the base of the switch tree connected to
6193 * the link. Floating VEBs are not notified.
6194 */
6195 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6196 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6197 else
Mitch Williams320684c2014-10-17 03:14:43 +00006198 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006199
6200 if (pf->vf)
6201 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006202
6203 if (pf->flags & I40E_FLAG_PTP)
6204 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006205}
6206
6207/**
Shannon Nelson21536712014-10-25 10:35:25 +00006208 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006209 * @pf: board private structure
6210 **/
6211static void i40e_watchdog_subtask(struct i40e_pf *pf)
6212{
6213 int i;
6214
6215 /* if interface is down do nothing */
6216 if (test_bit(__I40E_DOWN, &pf->state) ||
6217 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6218 return;
6219
Shannon Nelson21536712014-10-25 10:35:25 +00006220 /* make sure we don't do these things too often */
6221 if (time_before(jiffies, (pf->service_timer_previous +
6222 pf->service_timer_period)))
6223 return;
6224 pf->service_timer_previous = jiffies;
6225
Shannon Nelson9ac77262015-08-27 11:42:40 -04006226 if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
6227 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006228
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006229 /* Update the stats for active netdevs so the network stack
6230 * can look at updated numbers whenever it cares to
6231 */
Mitch Williams505682c2014-05-20 08:01:37 +00006232 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006233 if (pf->vsi[i] && pf->vsi[i]->netdev)
6234 i40e_update_stats(pf->vsi[i]);
6235
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006236 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6237 /* Update the stats for the active switching components */
6238 for (i = 0; i < I40E_MAX_VEB; i++)
6239 if (pf->veb[i])
6240 i40e_update_veb_stats(pf->veb[i]);
6241 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006242
6243 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006244}
6245
6246/**
6247 * i40e_reset_subtask - Set up for resetting the device and driver
6248 * @pf: board private structure
6249 **/
6250static void i40e_reset_subtask(struct i40e_pf *pf)
6251{
6252 u32 reset_flags = 0;
6253
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006254 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006255 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006256 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006257 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6258 }
6259 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006260 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006261 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6262 }
6263 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006264 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006265 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6266 }
6267 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006268 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006269 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6270 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006271 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006272 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006273 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6274 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006275
6276 /* If there's a recovery already waiting, it takes
6277 * precedence before starting a new reset sequence.
6278 */
6279 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6280 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006281 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006282 }
6283
6284 /* If we're already down or resetting, just bail */
6285 if (reset_flags &&
6286 !test_bit(__I40E_DOWN, &pf->state) &&
6287 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6288 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006289
6290unlock:
6291 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006292}
6293
6294/**
6295 * i40e_handle_link_event - Handle link event
6296 * @pf: board private structure
6297 * @e: event info posted on ARQ
6298 **/
6299static void i40e_handle_link_event(struct i40e_pf *pf,
6300 struct i40e_arq_event_info *e)
6301{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006302 struct i40e_aqc_get_link_status *status =
6303 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006304
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006305 /* Do a new status request to re-enable LSE reporting
6306 * and load new status information into the hw struct
6307 * This completely ignores any state information
6308 * in the ARQ event info, instead choosing to always
6309 * issue the AQ update link status command.
6310 */
6311 i40e_link_event(pf);
6312
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006313 /* check for unqualified module, if link is down */
6314 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6315 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6316 (!(status->link_info & I40E_AQ_LINK_UP)))
6317 dev_err(&pf->pdev->dev,
6318 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006319}
6320
6321/**
6322 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6323 * @pf: board private structure
6324 **/
6325static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6326{
6327 struct i40e_arq_event_info event;
6328 struct i40e_hw *hw = &pf->hw;
6329 u16 pending, i = 0;
6330 i40e_status ret;
6331 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006332 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006333 u32 val;
6334
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006335 /* Do not run clean AQ when PF reset fails */
6336 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6337 return;
6338
Shannon Nelson86df2422014-05-20 08:01:35 +00006339 /* check for error indications */
6340 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6341 oldval = val;
6342 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006343 if (hw->debug_mask & I40E_DEBUG_AQ)
6344 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006345 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6346 }
6347 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006348 if (hw->debug_mask & I40E_DEBUG_AQ)
6349 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006350 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006351 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006352 }
6353 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006354 if (hw->debug_mask & I40E_DEBUG_AQ)
6355 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006356 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6357 }
6358 if (oldval != val)
6359 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6360
6361 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6362 oldval = val;
6363 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006364 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6365 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006366 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6367 }
6368 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006369 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6370 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006371 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6372 }
6373 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006374 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6375 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006376 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6377 }
6378 if (oldval != val)
6379 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6380
Mitch Williams1001dc32014-11-11 20:02:19 +00006381 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6382 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006383 if (!event.msg_buf)
6384 return;
6385
6386 do {
6387 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006388 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389 break;
Mitch Williams56497972014-06-04 08:45:18 +00006390 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006391 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6392 break;
6393 }
6394
6395 opcode = le16_to_cpu(event.desc.opcode);
6396 switch (opcode) {
6397
6398 case i40e_aqc_opc_get_link_status:
6399 i40e_handle_link_event(pf, &event);
6400 break;
6401 case i40e_aqc_opc_send_msg_to_pf:
6402 ret = i40e_vc_process_vf_msg(pf,
6403 le16_to_cpu(event.desc.retval),
6404 le32_to_cpu(event.desc.cookie_high),
6405 le32_to_cpu(event.desc.cookie_low),
6406 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006407 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006408 break;
6409 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006410 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006411#ifdef CONFIG_I40E_DCB
6412 rtnl_lock();
6413 ret = i40e_handle_lldp_event(pf, &event);
6414 rtnl_unlock();
6415#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006416 break;
6417 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006418 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006419 i40e_handle_lan_overflow_event(pf, &event);
6420 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006421 case i40e_aqc_opc_send_msg_to_peer:
6422 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6423 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006424 case i40e_aqc_opc_nvm_erase:
6425 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006426 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006427 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6428 "ARQ NVM operation 0x%04x completed\n",
6429 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006430 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006431 default:
6432 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006433 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006434 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006435 break;
6436 }
6437 } while (pending && (i++ < pf->adminq_work_limit));
6438
6439 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6440 /* re-enable Admin queue interrupt cause */
6441 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6442 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6443 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6444 i40e_flush(hw);
6445
6446 kfree(event.msg_buf);
6447}
6448
6449/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006450 * i40e_verify_eeprom - make sure eeprom is good to use
6451 * @pf: board private structure
6452 **/
6453static void i40e_verify_eeprom(struct i40e_pf *pf)
6454{
6455 int err;
6456
6457 err = i40e_diag_eeprom_test(&pf->hw);
6458 if (err) {
6459 /* retry in case of garbage read */
6460 err = i40e_diag_eeprom_test(&pf->hw);
6461 if (err) {
6462 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6463 err);
6464 set_bit(__I40E_BAD_EEPROM, &pf->state);
6465 }
6466 }
6467
6468 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6469 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6470 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6471 }
6472}
6473
6474/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006475 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006476 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006477 *
6478 * enable switch loop back or die - no point in a return value
6479 **/
6480static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6481{
6482 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6483 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006484 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006485
6486 ctxt.seid = pf->main_vsi_seid;
6487 ctxt.pf_num = pf->hw.pf_id;
6488 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006489 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6490 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006491 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006492 "couldn't get PF vsi config, err %s aq_err %s\n",
6493 i40e_stat_str(&pf->hw, ret),
6494 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006495 return;
6496 }
6497 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6498 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6499 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6500
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006501 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6502 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006503 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006504 "update vsi switch failed, err %s aq_err %s\n",
6505 i40e_stat_str(&pf->hw, ret),
6506 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006507 }
6508}
6509
6510/**
6511 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006512 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006513 *
6514 * disable switch loop back or die - no point in a return value
6515 **/
6516static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6517{
6518 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6519 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006520 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006521
6522 ctxt.seid = pf->main_vsi_seid;
6523 ctxt.pf_num = pf->hw.pf_id;
6524 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006525 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6526 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006527 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006528 "couldn't get PF vsi config, err %s aq_err %s\n",
6529 i40e_stat_str(&pf->hw, ret),
6530 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006531 return;
6532 }
6533 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6534 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6535 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6536
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006537 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6538 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006539 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006540 "update vsi switch failed, err %s aq_err %s\n",
6541 i40e_stat_str(&pf->hw, ret),
6542 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006543 }
6544}
6545
6546/**
Neerav Parikh51616012015-02-06 08:52:14 +00006547 * i40e_config_bridge_mode - Configure the HW bridge mode
6548 * @veb: pointer to the bridge instance
6549 *
6550 * Configure the loop back mode for the LAN VSI that is downlink to the
6551 * specified HW bridge instance. It is expected this function is called
6552 * when a new HW bridge is instantiated.
6553 **/
6554static void i40e_config_bridge_mode(struct i40e_veb *veb)
6555{
6556 struct i40e_pf *pf = veb->pf;
6557
Shannon Nelson6dec1012015-09-28 14:12:30 -04006558 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6559 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6560 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006561 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6562 i40e_disable_pf_switch_lb(pf);
6563 else
6564 i40e_enable_pf_switch_lb(pf);
6565}
6566
6567/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006568 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6569 * @veb: pointer to the VEB instance
6570 *
6571 * This is a recursive function that first builds the attached VSIs then
6572 * recurses in to build the next layer of VEB. We track the connections
6573 * through our own index numbers because the seid's from the HW could
6574 * change across the reset.
6575 **/
6576static int i40e_reconstitute_veb(struct i40e_veb *veb)
6577{
6578 struct i40e_vsi *ctl_vsi = NULL;
6579 struct i40e_pf *pf = veb->pf;
6580 int v, veb_idx;
6581 int ret;
6582
6583 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006584 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006585 if (pf->vsi[v] &&
6586 pf->vsi[v]->veb_idx == veb->idx &&
6587 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6588 ctl_vsi = pf->vsi[v];
6589 break;
6590 }
6591 }
6592 if (!ctl_vsi) {
6593 dev_info(&pf->pdev->dev,
6594 "missing owner VSI for veb_idx %d\n", veb->idx);
6595 ret = -ENOENT;
6596 goto end_reconstitute;
6597 }
6598 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6599 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6600 ret = i40e_add_vsi(ctl_vsi);
6601 if (ret) {
6602 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006603 "rebuild of veb_idx %d owner VSI failed: %d\n",
6604 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006605 goto end_reconstitute;
6606 }
6607 i40e_vsi_reset_stats(ctl_vsi);
6608
6609 /* create the VEB in the switch and move the VSI onto the VEB */
6610 ret = i40e_add_veb(veb, ctl_vsi);
6611 if (ret)
6612 goto end_reconstitute;
6613
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006614 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6615 veb->bridge_mode = BRIDGE_MODE_VEB;
6616 else
6617 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006618 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006619
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006620 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006621 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006622 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6623 continue;
6624
6625 if (pf->vsi[v]->veb_idx == veb->idx) {
6626 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006628 vsi->uplink_seid = veb->seid;
6629 ret = i40e_add_vsi(vsi);
6630 if (ret) {
6631 dev_info(&pf->pdev->dev,
6632 "rebuild of vsi_idx %d failed: %d\n",
6633 v, ret);
6634 goto end_reconstitute;
6635 }
6636 i40e_vsi_reset_stats(vsi);
6637 }
6638 }
6639
6640 /* create any VEBs attached to this VEB - RECURSION */
6641 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6642 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6643 pf->veb[veb_idx]->uplink_seid = veb->seid;
6644 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6645 if (ret)
6646 break;
6647 }
6648 }
6649
6650end_reconstitute:
6651 return ret;
6652}
6653
6654/**
6655 * i40e_get_capabilities - get info about the HW
6656 * @pf: the PF struct
6657 **/
6658static int i40e_get_capabilities(struct i40e_pf *pf)
6659{
6660 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6661 u16 data_size;
6662 int buf_len;
6663 int err;
6664
6665 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6666 do {
6667 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6668 if (!cap_buf)
6669 return -ENOMEM;
6670
6671 /* this loads the data into the hw struct for us */
6672 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6673 &data_size,
6674 i40e_aqc_opc_list_func_capabilities,
6675 NULL);
6676 /* data loaded, buffer no longer needed */
6677 kfree(cap_buf);
6678
6679 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6680 /* retry with a larger buffer */
6681 buf_len = data_size;
6682 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6683 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006684 "capability discovery failed, err %s aq_err %s\n",
6685 i40e_stat_str(&pf->hw, err),
6686 i40e_aq_str(&pf->hw,
6687 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006688 return -ENODEV;
6689 }
6690 } while (err);
6691
6692 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6693 dev_info(&pf->pdev->dev,
6694 "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",
6695 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6696 pf->hw.func_caps.num_msix_vectors,
6697 pf->hw.func_caps.num_msix_vectors_vf,
6698 pf->hw.func_caps.fd_filters_guaranteed,
6699 pf->hw.func_caps.fd_filters_best_effort,
6700 pf->hw.func_caps.num_tx_qp,
6701 pf->hw.func_caps.num_vsis);
6702
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006703#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6704 + pf->hw.func_caps.num_vfs)
6705 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6706 dev_info(&pf->pdev->dev,
6707 "got num_vsis %d, setting num_vsis to %d\n",
6708 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6709 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6710 }
6711
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006712 return 0;
6713}
6714
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006715static int i40e_vsi_clear(struct i40e_vsi *vsi);
6716
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006717/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006718 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006719 * @pf: board private structure
6720 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006721static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006722{
6723 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006724
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006725 /* quick workaround for an NVM issue that leaves a critical register
6726 * uninitialized
6727 */
6728 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6729 static const u32 hkey[] = {
6730 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6731 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6732 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6733 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006734 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006735
6736 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6737 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6738 }
6739
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006740 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006741 return;
6742
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006743 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006744 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006745
6746 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006747 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006748 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6749 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006750 if (!vsi) {
6751 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006752 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6753 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006754 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006755 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006756
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006757 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006758}
6759
6760/**
6761 * i40e_fdir_teardown - release the Flow Director resources
6762 * @pf: board private structure
6763 **/
6764static void i40e_fdir_teardown(struct i40e_pf *pf)
6765{
Alexander Duyck4b816442016-10-11 15:26:53 -07006766 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006767
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006768 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006769 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6770 if (vsi)
6771 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006772}
6773
6774/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006775 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006776 * @pf: board private structure
6777 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006778 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006779 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006780static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006781{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006782 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006783 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006784 u32 v;
6785
6786 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6787 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006788 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006789 if (i40e_check_asq_alive(&pf->hw))
6790 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006791
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006792 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006793
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006794 /* quiesce the VSIs and their queues that are not already DOWN */
6795 i40e_pf_quiesce_all_vsi(pf);
6796
Mitch Williams505682c2014-05-20 08:01:37 +00006797 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006798 if (pf->vsi[v])
6799 pf->vsi[v]->seid = 0;
6800 }
6801
6802 i40e_shutdown_adminq(&pf->hw);
6803
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006804 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006805 if (hw->hmc.hmc_obj) {
6806 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006807 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006808 dev_warn(&pf->pdev->dev,
6809 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006810 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006811}
6812
6813/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006814 * i40e_send_version - update firmware with driver version
6815 * @pf: PF struct
6816 */
6817static void i40e_send_version(struct i40e_pf *pf)
6818{
6819 struct i40e_driver_version dv;
6820
6821 dv.major_version = DRV_VERSION_MAJOR;
6822 dv.minor_version = DRV_VERSION_MINOR;
6823 dv.build_version = DRV_VERSION_BUILD;
6824 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006825 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006826 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6827}
6828
6829/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006830 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006831 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006832 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006833 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006834static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006835{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006836 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006837 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006838 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006839 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006840 u32 v;
6841
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006842 /* Now we wait for GRST to settle out.
6843 * We don't have to delete the VEBs or VSIs from the hw switch
6844 * because the reset will make them disappear.
6845 */
6846 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006847 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006848 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006849 set_bit(__I40E_RESET_FAILED, &pf->state);
6850 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006851 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006852 pf->pfr_count++;
6853
6854 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006855 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006856 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006857
6858 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6859 ret = i40e_init_adminq(&pf->hw);
6860 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006861 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6862 i40e_stat_str(&pf->hw, ret),
6863 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006864 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006865 }
6866
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006867 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006868 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006869 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006870
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006871 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006872 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006873 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006874 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006875
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006876 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
6877 hw->func_caps.num_rx_qp,
6878 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
6879 if (ret) {
6880 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6881 goto end_core_reset;
6882 }
6883 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6884 if (ret) {
6885 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6886 goto end_core_reset;
6887 }
6888
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006889#ifdef CONFIG_I40E_DCB
6890 ret = i40e_init_pf_dcb(pf);
6891 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006892 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6893 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6894 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006895 }
6896#endif /* CONFIG_I40E_DCB */
Vasu Dev38e00432014-08-01 13:27:03 -07006897#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04006898 i40e_init_pf_fcoe(pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006899
Vasu Dev38e00432014-08-01 13:27:03 -07006900#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006901 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006902 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006903 if (ret)
6904 goto end_core_reset;
6905
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006906 /* The driver only wants link up/down and module qualification
6907 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006908 */
6909 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006910 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006911 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006912 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006913 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006914 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6915 i40e_stat_str(&pf->hw, ret),
6916 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006917
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006918 /* make sure our flow control settings are restored */
6919 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
6920 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04006921 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
6922 i40e_stat_str(&pf->hw, ret),
6923 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006924
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006925 /* Rebuild the VSIs and VEBs that existed before reset.
6926 * They are still in our local switch element arrays, so only
6927 * need to rebuild the switch model in the HW.
6928 *
6929 * If there were VEBs but the reconstitution failed, we'll try
6930 * try to recover minimal use by getting the basic PF VSI working.
6931 */
6932 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006933 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006934 /* find the one VEB connected to the MAC, and find orphans */
6935 for (v = 0; v < I40E_MAX_VEB; v++) {
6936 if (!pf->veb[v])
6937 continue;
6938
6939 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
6940 pf->veb[v]->uplink_seid == 0) {
6941 ret = i40e_reconstitute_veb(pf->veb[v]);
6942
6943 if (!ret)
6944 continue;
6945
6946 /* If Main VEB failed, we're in deep doodoo,
6947 * so give up rebuilding the switch and set up
6948 * for minimal rebuild of PF VSI.
6949 * If orphan failed, we'll report the error
6950 * but try to keep going.
6951 */
6952 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
6953 dev_info(&pf->pdev->dev,
6954 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
6955 ret);
6956 pf->vsi[pf->lan_vsi]->uplink_seid
6957 = pf->mac_seid;
6958 break;
6959 } else if (pf->veb[v]->uplink_seid == 0) {
6960 dev_info(&pf->pdev->dev,
6961 "rebuild of orphan VEB failed: %d\n",
6962 ret);
6963 }
6964 }
6965 }
6966 }
6967
6968 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00006969 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006970 /* no VEB, so rebuild only the Main VSI */
6971 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
6972 if (ret) {
6973 dev_info(&pf->pdev->dev,
6974 "rebuild of Main VSI failed: %d\n", ret);
6975 goto end_core_reset;
6976 }
6977 }
6978
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006979 /* Reconfigure hardware for allowing smaller MSS in the case
6980 * of TSO, so that we avoid the MDD being fired and causing
6981 * a reset in the case of small MSS+TSO.
6982 */
6983#define I40E_REG_MSS 0x000E64DC
6984#define I40E_REG_MSS_MIN_MASK 0x3FF0000
6985#define I40E_64BYTE_MSS 0x400000
6986 val = rd32(hw, I40E_REG_MSS);
6987 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
6988 val &= ~I40E_REG_MSS_MIN_MASK;
6989 val |= I40E_64BYTE_MSS;
6990 wr32(hw, I40E_REG_MSS, val);
6991 }
6992
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08006993 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00006994 msleep(75);
6995 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
6996 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006997 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
6998 i40e_stat_str(&pf->hw, ret),
6999 i40e_aq_str(&pf->hw,
7000 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007001 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007002 /* reinit the misc interrupt */
7003 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7004 ret = i40e_setup_misc_vector(pf);
7005
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007006 /* Add a filter to drop all Flow control frames from any VSI from being
7007 * transmitted. By doing so we stop a malicious VF from sending out
7008 * PAUSE or PFC frames and potentially controlling traffic for other
7009 * PF/VF VSIs.
7010 * The FW can still send Flow control frames if enabled.
7011 */
7012 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7013 pf->main_vsi_seid);
7014
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007015 /* restart the VSIs that were rebuilt and running before the reset */
7016 i40e_pf_unquiesce_all_vsi(pf);
7017
Mitch Williams69f64b22014-02-13 03:48:46 -08007018 if (pf->num_alloc_vfs) {
7019 for (v = 0; v < pf->num_alloc_vfs; v++)
7020 i40e_reset_vf(&pf->vf[v], true);
7021 }
7022
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007023 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007024 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007025
7026end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007027 clear_bit(__I40E_RESET_FAILED, &pf->state);
7028clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007029 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7030}
7031
7032/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007033 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007034 * @pf: board private structure
7035 *
7036 * Close up the VFs and other things in prep for a Core Reset,
7037 * then get ready to rebuild the world.
7038 **/
7039static void i40e_handle_reset_warning(struct i40e_pf *pf)
7040{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007041 i40e_prep_for_reset(pf);
7042 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007043}
7044
7045/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007046 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007047 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007048 *
7049 * Called from the MDD irq handler to identify possibly malicious vfs
7050 **/
7051static void i40e_handle_mdd_event(struct i40e_pf *pf)
7052{
7053 struct i40e_hw *hw = &pf->hw;
7054 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007055 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056 struct i40e_vf *vf;
7057 u32 reg;
7058 int i;
7059
7060 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7061 return;
7062
7063 /* find what triggered the MDD event */
7064 reg = rd32(hw, I40E_GL_MDET_TX);
7065 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007066 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7067 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007068 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007069 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007070 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007071 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007072 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7073 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7074 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007075 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007076 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 +00007077 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007078 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7079 mdd_detected = true;
7080 }
7081 reg = rd32(hw, I40E_GL_MDET_RX);
7082 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007083 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7084 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007085 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007086 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007087 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7088 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7089 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007090 if (netif_msg_rx_err(pf))
7091 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7092 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007093 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7094 mdd_detected = true;
7095 }
7096
Neerav Parikhdf430b12014-06-04 01:23:15 +00007097 if (mdd_detected) {
7098 reg = rd32(hw, I40E_PF_MDET_TX);
7099 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7100 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007101 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007102 pf_mdd_detected = true;
7103 }
7104 reg = rd32(hw, I40E_PF_MDET_RX);
7105 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7106 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007107 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007108 pf_mdd_detected = true;
7109 }
7110 /* Queue belongs to the PF, initiate a reset */
7111 if (pf_mdd_detected) {
7112 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7113 i40e_service_event_schedule(pf);
7114 }
7115 }
7116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007117 /* see if one of the VFs needs its hand slapped */
7118 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7119 vf = &(pf->vf[i]);
7120 reg = rd32(hw, I40E_VP_MDET_TX(i));
7121 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7122 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7123 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007124 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7125 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007126 }
7127
7128 reg = rd32(hw, I40E_VP_MDET_RX(i));
7129 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7130 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7131 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007132 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7133 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007134 }
7135
7136 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7137 dev_info(&pf->pdev->dev,
7138 "Too many MDD events on VF %d, disabled\n", i);
7139 dev_info(&pf->pdev->dev,
7140 "Use PF Control I/F to re-enable the VF\n");
7141 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7142 }
7143 }
7144
7145 /* re-enable mdd interrupt cause */
7146 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7147 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7148 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7149 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7150 i40e_flush(hw);
7151}
7152
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007153/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007154 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007155 * @pf: board private structure
7156 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007157static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007158{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007159 struct i40e_hw *hw = &pf->hw;
7160 i40e_status ret;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007161 __be16 port;
7162 int i;
7163
Singhai, Anjali6a899022015-12-14 12:21:18 -08007164 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007165 return;
7166
Singhai, Anjali6a899022015-12-14 12:21:18 -08007167 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007168
7169 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007170 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7171 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7172 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007173 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007174 ret = i40e_aq_add_udp_tunnel(hw, port,
7175 pf->udp_ports[i].type,
7176 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007177 else
7178 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007179
7180 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007181 dev_dbg(&pf->pdev->dev,
7182 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7183 pf->udp_ports[i].type ? "vxlan" : "geneve",
7184 port ? "add" : "delete",
7185 ntohs(port), i,
7186 i40e_stat_str(&pf->hw, ret),
7187 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007188 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007189 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007190 }
7191 }
7192 }
7193}
7194
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007195/**
7196 * i40e_service_task - Run the driver's async subtasks
7197 * @work: pointer to work_struct containing our data
7198 **/
7199static void i40e_service_task(struct work_struct *work)
7200{
7201 struct i40e_pf *pf = container_of(work,
7202 struct i40e_pf,
7203 service_task);
7204 unsigned long start_time = jiffies;
7205
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007206 /* don't bother with service tasks if a reset is in progress */
7207 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7208 i40e_service_event_complete(pf);
7209 return;
7210 }
7211
Kiran Patilb03a8c12015-09-24 18:13:15 -04007212 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007213 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214 i40e_reset_subtask(pf);
7215 i40e_handle_mdd_event(pf);
7216 i40e_vc_process_vflr_event(pf);
7217 i40e_watchdog_subtask(pf);
7218 i40e_fdir_reinit_subtask(pf);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007219 i40e_client_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007220 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007221 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007222 i40e_clean_adminq_subtask(pf);
7223
7224 i40e_service_event_complete(pf);
7225
7226 /* If the tasks have taken longer than one timer cycle or there
7227 * is more work to be done, reschedule the service task now
7228 * rather than wait for the timer to tick again.
7229 */
7230 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7231 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7232 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7233 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7234 i40e_service_event_schedule(pf);
7235}
7236
7237/**
7238 * i40e_service_timer - timer callback
7239 * @data: pointer to PF struct
7240 **/
7241static void i40e_service_timer(unsigned long data)
7242{
7243 struct i40e_pf *pf = (struct i40e_pf *)data;
7244
7245 mod_timer(&pf->service_timer,
7246 round_jiffies(jiffies + pf->service_timer_period));
7247 i40e_service_event_schedule(pf);
7248}
7249
7250/**
7251 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7252 * @vsi: the VSI being configured
7253 **/
7254static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7255{
7256 struct i40e_pf *pf = vsi->back;
7257
7258 switch (vsi->type) {
7259 case I40E_VSI_MAIN:
7260 vsi->alloc_queue_pairs = pf->num_lan_qps;
7261 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7262 I40E_REQ_DESCRIPTOR_MULTIPLE);
7263 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7264 vsi->num_q_vectors = pf->num_lan_msix;
7265 else
7266 vsi->num_q_vectors = 1;
7267
7268 break;
7269
7270 case I40E_VSI_FDIR:
7271 vsi->alloc_queue_pairs = 1;
7272 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7273 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007274 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007275 break;
7276
7277 case I40E_VSI_VMDQ2:
7278 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7279 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7280 I40E_REQ_DESCRIPTOR_MULTIPLE);
7281 vsi->num_q_vectors = pf->num_vmdq_msix;
7282 break;
7283
7284 case I40E_VSI_SRIOV:
7285 vsi->alloc_queue_pairs = pf->num_vf_qps;
7286 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7287 I40E_REQ_DESCRIPTOR_MULTIPLE);
7288 break;
7289
Vasu Dev38e00432014-08-01 13:27:03 -07007290#ifdef I40E_FCOE
7291 case I40E_VSI_FCOE:
7292 vsi->alloc_queue_pairs = pf->num_fcoe_qps;
7293 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7294 I40E_REQ_DESCRIPTOR_MULTIPLE);
7295 vsi->num_q_vectors = pf->num_fcoe_msix;
7296 break;
7297
7298#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007299 default:
7300 WARN_ON(1);
7301 return -ENODATA;
7302 }
7303
7304 return 0;
7305}
7306
7307/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007308 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7309 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007310 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007311 *
7312 * On error: returns error code (negative)
7313 * On success: returns 0
7314 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007315static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007316{
7317 int size;
7318 int ret = 0;
7319
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007320 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007321 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7322 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7323 if (!vsi->tx_rings)
7324 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007325 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7326
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007327 if (alloc_qvectors) {
7328 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007329 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007330 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7331 if (!vsi->q_vectors) {
7332 ret = -ENOMEM;
7333 goto err_vectors;
7334 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007335 }
7336 return ret;
7337
7338err_vectors:
7339 kfree(vsi->tx_rings);
7340 return ret;
7341}
7342
7343/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007344 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7345 * @pf: board private structure
7346 * @type: type of VSI
7347 *
7348 * On error: returns error code (negative)
7349 * On success: returns vsi index in PF (positive)
7350 **/
7351static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7352{
7353 int ret = -ENODEV;
7354 struct i40e_vsi *vsi;
7355 int vsi_idx;
7356 int i;
7357
7358 /* Need to protect the allocation of the VSIs at the PF level */
7359 mutex_lock(&pf->switch_mutex);
7360
7361 /* VSI list may be fragmented if VSI creation/destruction has
7362 * been happening. We can afford to do a quick scan to look
7363 * for any free VSIs in the list.
7364 *
7365 * find next empty vsi slot, looping back around if necessary
7366 */
7367 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007368 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007369 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007370 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007371 i = 0;
7372 while (i < pf->next_vsi && pf->vsi[i])
7373 i++;
7374 }
7375
Mitch Williams505682c2014-05-20 08:01:37 +00007376 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007377 vsi_idx = i; /* Found one! */
7378 } else {
7379 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007380 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007381 }
7382 pf->next_vsi = ++i;
7383
7384 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7385 if (!vsi) {
7386 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007387 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007388 }
7389 vsi->type = type;
7390 vsi->back = pf;
7391 set_bit(__I40E_DOWN, &vsi->state);
7392 vsi->flags = 0;
7393 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007394 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007395 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7396 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007397 vsi->netdev_registered = false;
7398 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007399 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007400 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007401
Alexander Duyck9f65e152013-09-28 06:00:58 +00007402 ret = i40e_set_num_rings_in_vsi(vsi);
7403 if (ret)
7404 goto err_rings;
7405
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007406 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007407 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007408 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007410 /* Setup default MSIX irq handler for VSI */
7411 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7412
Kiran Patil21659032015-09-30 14:09:03 -04007413 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007414 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007415 pf->vsi[vsi_idx] = vsi;
7416 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007417 goto unlock_pf;
7418
Alexander Duyck9f65e152013-09-28 06:00:58 +00007419err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007420 pf->next_vsi = i - 1;
7421 kfree(vsi);
7422unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007423 mutex_unlock(&pf->switch_mutex);
7424 return ret;
7425}
7426
7427/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007428 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7429 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007430 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007431 *
7432 * On error: returns error code (negative)
7433 * On success: returns 0
7434 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007435static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007436{
7437 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007438 if (free_qvectors) {
7439 kfree(vsi->q_vectors);
7440 vsi->q_vectors = NULL;
7441 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007442 kfree(vsi->tx_rings);
7443 vsi->tx_rings = NULL;
7444 vsi->rx_rings = NULL;
7445}
7446
7447/**
Helin Zhang28c58692015-10-26 19:44:27 -04007448 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7449 * and lookup table
7450 * @vsi: Pointer to VSI structure
7451 */
7452static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7453{
7454 if (!vsi)
7455 return;
7456
7457 kfree(vsi->rss_hkey_user);
7458 vsi->rss_hkey_user = NULL;
7459
7460 kfree(vsi->rss_lut_user);
7461 vsi->rss_lut_user = NULL;
7462}
7463
7464/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007465 * i40e_vsi_clear - Deallocate the VSI provided
7466 * @vsi: the VSI being un-configured
7467 **/
7468static int i40e_vsi_clear(struct i40e_vsi *vsi)
7469{
7470 struct i40e_pf *pf;
7471
7472 if (!vsi)
7473 return 0;
7474
7475 if (!vsi->back)
7476 goto free_vsi;
7477 pf = vsi->back;
7478
7479 mutex_lock(&pf->switch_mutex);
7480 if (!pf->vsi[vsi->idx]) {
7481 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7482 vsi->idx, vsi->idx, vsi, vsi->type);
7483 goto unlock_vsi;
7484 }
7485
7486 if (pf->vsi[vsi->idx] != vsi) {
7487 dev_err(&pf->pdev->dev,
7488 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7489 pf->vsi[vsi->idx]->idx,
7490 pf->vsi[vsi->idx],
7491 pf->vsi[vsi->idx]->type,
7492 vsi->idx, vsi, vsi->type);
7493 goto unlock_vsi;
7494 }
7495
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007496 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007497 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7498 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7499
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007500 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007501 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007502
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007503 pf->vsi[vsi->idx] = NULL;
7504 if (vsi->idx < pf->next_vsi)
7505 pf->next_vsi = vsi->idx;
7506
7507unlock_vsi:
7508 mutex_unlock(&pf->switch_mutex);
7509free_vsi:
7510 kfree(vsi);
7511
7512 return 0;
7513}
7514
7515/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007516 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7517 * @vsi: the VSI being cleaned
7518 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007519static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007520{
7521 int i;
7522
Greg Rose8e9dca52013-12-18 13:45:53 +00007523 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007524 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007525 kfree_rcu(vsi->tx_rings[i], rcu);
7526 vsi->tx_rings[i] = NULL;
7527 vsi->rx_rings[i] = NULL;
7528 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007529 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007530}
7531
7532/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007533 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7534 * @vsi: the VSI being configured
7535 **/
7536static int i40e_alloc_rings(struct i40e_vsi *vsi)
7537{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007538 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007539 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007540 int i;
7541
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007542 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007543 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007544 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007545 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7546 if (!tx_ring)
7547 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007548
7549 tx_ring->queue_index = i;
7550 tx_ring->reg_idx = vsi->base_queue + i;
7551 tx_ring->ring_active = false;
7552 tx_ring->vsi = vsi;
7553 tx_ring->netdev = vsi->netdev;
7554 tx_ring->dev = &pf->pdev->dev;
7555 tx_ring->count = vsi->num_desc;
7556 tx_ring->size = 0;
7557 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007558 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7559 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007560 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007561 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007562
Alexander Duyck9f65e152013-09-28 06:00:58 +00007563 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007564 rx_ring->queue_index = i;
7565 rx_ring->reg_idx = vsi->base_queue + i;
7566 rx_ring->ring_active = false;
7567 rx_ring->vsi = vsi;
7568 rx_ring->netdev = vsi->netdev;
7569 rx_ring->dev = &pf->pdev->dev;
7570 rx_ring->count = vsi->num_desc;
7571 rx_ring->size = 0;
7572 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007573 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007574 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007575 }
7576
7577 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007578
7579err_out:
7580 i40e_vsi_clear_rings(vsi);
7581 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007582}
7583
7584/**
7585 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7586 * @pf: board private structure
7587 * @vectors: the number of MSI-X vectors to request
7588 *
7589 * Returns the number of vectors reserved, or error
7590 **/
7591static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7592{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007593 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7594 I40E_MIN_MSIX, vectors);
7595 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007596 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007597 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007598 vectors = 0;
7599 }
7600
7601 return vectors;
7602}
7603
7604/**
7605 * i40e_init_msix - Setup the MSIX capability
7606 * @pf: board private structure
7607 *
7608 * Work with the OS to set up the MSIX vectors needed.
7609 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007610 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007611 **/
7612static int i40e_init_msix(struct i40e_pf *pf)
7613{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007614 struct i40e_hw *hw = &pf->hw;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007615 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007616 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007617 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007618 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007619
7620 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7621 return -ENODEV;
7622
7623 /* The number of vectors we'll request will be comprised of:
7624 * - Add 1 for "other" cause for Admin Queue events, etc.
7625 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007626 * - Queues being used for RSS.
7627 * We don't need as many as max_rss_size vectors.
7628 * use rss_size instead in the calculation since that
7629 * is governed by number of cpus in the system.
7630 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007631 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007632 * - The CPU count within the NUMA node if iWARP is enabled
Vasu Dev38e00432014-08-01 13:27:03 -07007633#ifdef I40E_FCOE
7634 * - The number of FCOE qps.
7635#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007636 * Once we count this up, try the request.
7637 *
7638 * If we can't get what we want, we'll simplify to nearly nothing
7639 * and try again. If that still fails, we punt.
7640 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007641 vectors_left = hw->func_caps.num_msix_vectors;
7642 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007643
Shannon Nelson1e200e42015-02-27 09:15:24 +00007644 /* reserve one vector for miscellaneous handler */
7645 if (vectors_left) {
7646 v_budget++;
7647 vectors_left--;
7648 }
7649
7650 /* reserve vectors for the main PF traffic queues */
7651 pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
7652 vectors_left -= pf->num_lan_msix;
7653 v_budget += pf->num_lan_msix;
7654
7655 /* reserve one vector for sideband flow director */
7656 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7657 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007658 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007659 v_budget++;
7660 vectors_left--;
7661 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007662 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007663 }
7664 }
John W Linville83840e42015-01-14 03:06:28 +00007665
Vasu Dev38e00432014-08-01 13:27:03 -07007666#ifdef I40E_FCOE
Shannon Nelson1e200e42015-02-27 09:15:24 +00007667 /* can we reserve enough for FCoE? */
Vasu Dev38e00432014-08-01 13:27:03 -07007668 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
Shannon Nelson1e200e42015-02-27 09:15:24 +00007669 if (!vectors_left)
7670 pf->num_fcoe_msix = 0;
7671 else if (vectors_left >= pf->num_fcoe_qps)
7672 pf->num_fcoe_msix = pf->num_fcoe_qps;
7673 else
7674 pf->num_fcoe_msix = 1;
Vasu Dev38e00432014-08-01 13:27:03 -07007675 v_budget += pf->num_fcoe_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007676 vectors_left -= pf->num_fcoe_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007677 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007678
Vasu Dev38e00432014-08-01 13:27:03 -07007679#endif
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007680 /* can we reserve enough for iWARP? */
7681 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007682 iwarp_requested = pf->num_iwarp_msix;
7683
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007684 if (!vectors_left)
7685 pf->num_iwarp_msix = 0;
7686 else if (vectors_left < pf->num_iwarp_msix)
7687 pf->num_iwarp_msix = 1;
7688 v_budget += pf->num_iwarp_msix;
7689 vectors_left -= pf->num_iwarp_msix;
7690 }
7691
Shannon Nelson1e200e42015-02-27 09:15:24 +00007692 /* any vectors left over go for VMDq support */
7693 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7694 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7695 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7696
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007697 if (!vectors_left) {
7698 pf->num_vmdq_msix = 0;
7699 pf->num_vmdq_qps = 0;
7700 } else {
7701 /* if we're short on vectors for what's desired, we limit
7702 * the queues per vmdq. If this is still more than are
7703 * available, the user will need to change the number of
7704 * queues/vectors used by the PF later with the ethtool
7705 * channels command
7706 */
7707 if (vmdq_vecs < vmdq_vecs_wanted)
7708 pf->num_vmdq_qps = 1;
7709 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007710
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007711 v_budget += vmdq_vecs;
7712 vectors_left -= vmdq_vecs;
7713 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007714 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007715
7716 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7717 GFP_KERNEL);
7718 if (!pf->msix_entries)
7719 return -ENOMEM;
7720
7721 for (i = 0; i < v_budget; i++)
7722 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007723 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007724
Shannon Nelson3b444392015-02-26 16:15:57 +00007725 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007726 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7727 kfree(pf->msix_entries);
7728 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007729 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007730 return -ENODEV;
7731
Shannon Nelson3b444392015-02-26 16:15:57 +00007732 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007733 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007734 pf->num_vmdq_vsis = 0;
7735 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007736 pf->num_lan_qps = 1;
7737 pf->num_lan_msix = 1;
7738
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007739 } else if (!vectors_left) {
7740 /* If we have limited resources, we will start with no vectors
7741 * for the special features and then allocate vectors to some
7742 * of these features based on the policy and at the end disable
7743 * the features that did not get any vectors.
7744 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007745 int vec;
7746
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007747 dev_info(&pf->pdev->dev,
7748 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007749 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007750 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007751
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007752 /* Scale vector usage down */
7753 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007754 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007755 pf->num_vmdq_qps = 1;
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007756#ifdef I40E_FCOE
7757 pf->num_fcoe_qps = 0;
7758 pf->num_fcoe_msix = 0;
7759#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007760
7761 /* partition out the remaining vectors */
7762 switch (vec) {
7763 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007764 pf->num_lan_msix = 1;
7765 break;
7766 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007767 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7768 pf->num_lan_msix = 1;
7769 pf->num_iwarp_msix = 1;
7770 } else {
7771 pf->num_lan_msix = 2;
7772 }
Vasu Dev38e00432014-08-01 13:27:03 -07007773#ifdef I40E_FCOE
7774 /* give one vector to FCoE */
7775 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7776 pf->num_lan_msix = 1;
7777 pf->num_fcoe_msix = 1;
7778 }
Vasu Dev38e00432014-08-01 13:27:03 -07007779#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007780 break;
7781 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007782 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7783 pf->num_iwarp_msix = min_t(int, (vec / 3),
7784 iwarp_requested);
7785 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7786 I40E_DEFAULT_NUM_VMDQ_VSI);
7787 } else {
7788 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7789 I40E_DEFAULT_NUM_VMDQ_VSI);
7790 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007791 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7792 pf->num_fdsb_msix = 1;
7793 vec--;
7794 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007795 pf->num_lan_msix = min_t(int,
7796 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7797 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007798 pf->num_lan_qps = pf->num_lan_msix;
Vasu Dev38e00432014-08-01 13:27:03 -07007799#ifdef I40E_FCOE
7800 /* give one vector to FCoE */
7801 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
7802 pf->num_fcoe_msix = 1;
7803 vec--;
7804 }
7805#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007806 break;
7807 }
7808 }
7809
Stefan Assmannabd97a92016-09-19 13:37:51 +02007810 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7811 (pf->num_fdsb_msix == 0)) {
7812 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7813 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7814 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007815 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7816 (pf->num_vmdq_msix == 0)) {
7817 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7818 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7819 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007820
7821 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7822 (pf->num_iwarp_msix == 0)) {
7823 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7824 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7825 }
Vasu Dev38e00432014-08-01 13:27:03 -07007826#ifdef I40E_FCOE
7827
7828 if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
7829 dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
7830 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
7831 }
7832#endif
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007833 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7834 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7835 pf->num_lan_msix,
7836 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7837 pf->num_fdsb_msix,
7838 pf->num_iwarp_msix);
7839
Shannon Nelson3b444392015-02-26 16:15:57 +00007840 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007841}
7842
7843/**
Greg Rose90e04072014-03-06 08:59:57 +00007844 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007845 * @vsi: the VSI being configured
7846 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007847 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007848 *
7849 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7850 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007851static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007852{
7853 struct i40e_q_vector *q_vector;
7854
7855 /* allocate q_vector */
7856 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7857 if (!q_vector)
7858 return -ENOMEM;
7859
7860 q_vector->vsi = vsi;
7861 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007862 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7863
Alexander Duyck493fb302013-09-28 07:01:44 +00007864 if (vsi->netdev)
7865 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007866 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007867
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007868 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7869 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7870
Alexander Duyck493fb302013-09-28 07:01:44 +00007871 /* tie q_vector and vsi together */
7872 vsi->q_vectors[v_idx] = q_vector;
7873
7874 return 0;
7875}
7876
7877/**
Greg Rose90e04072014-03-06 08:59:57 +00007878 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007879 * @vsi: the VSI being configured
7880 *
7881 * We allocate one q_vector per queue interrupt. If allocation fails we
7882 * return -ENOMEM.
7883 **/
Greg Rose90e04072014-03-06 08:59:57 +00007884static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007885{
7886 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007887 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007888
7889 /* if not MSIX, give the one vector only to the LAN VSI */
7890 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7891 num_q_vectors = vsi->num_q_vectors;
7892 else if (vsi == pf->vsi[pf->lan_vsi])
7893 num_q_vectors = 1;
7894 else
7895 return -EINVAL;
7896
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007897 current_cpu = cpumask_first(cpu_online_mask);
7898
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007899 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007900 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007901 if (err)
7902 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007903 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7904 if (unlikely(current_cpu >= nr_cpu_ids))
7905 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007906 }
7907
7908 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007909
7910err_out:
7911 while (v_idx--)
7912 i40e_free_q_vector(vsi, v_idx);
7913
7914 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007915}
7916
7917/**
7918 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7919 * @pf: board private structure to initialize
7920 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007921static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007922{
Shannon Nelson3b444392015-02-26 16:15:57 +00007923 int vectors = 0;
7924 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007925
7926 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007927 vectors = i40e_init_msix(pf);
7928 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007929 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007930 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -07007931#ifdef I40E_FCOE
7932 I40E_FLAG_FCOE_ENABLED |
7933#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007934 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00007935 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07007936 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007937 I40E_FLAG_SRIOV_ENABLED |
7938 I40E_FLAG_FD_SB_ENABLED |
7939 I40E_FLAG_FD_ATR_ENABLED |
7940 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007941
7942 /* rework the queue expectations without MSIX */
7943 i40e_determine_queue_usage(pf);
7944 }
7945 }
7946
7947 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
7948 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007949 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00007950 vectors = pci_enable_msi(pf->pdev);
7951 if (vectors < 0) {
7952 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
7953 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007954 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
7955 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007956 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007957 }
7958
Shannon Nelson958a3e32013-09-28 07:13:28 +00007959 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007960 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00007961
Shannon Nelson3b444392015-02-26 16:15:57 +00007962 /* set up vector assignment tracking */
7963 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
7964 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007965 if (!pf->irq_pile) {
7966 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
7967 return -ENOMEM;
7968 }
Shannon Nelson3b444392015-02-26 16:15:57 +00007969 pf->irq_pile->num_entries = vectors;
7970 pf->irq_pile->search_hint = 0;
7971
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007972 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00007973 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007974
7975 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007976}
7977
7978/**
7979 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
7980 * @pf: board private structure
7981 *
7982 * This sets up the handler for MSIX 0, which is used to manage the
7983 * non-queue interrupts, e.g. AdminQ and errors. This is not used
7984 * when in MSI or Legacy interrupt mode.
7985 **/
7986static int i40e_setup_misc_vector(struct i40e_pf *pf)
7987{
7988 struct i40e_hw *hw = &pf->hw;
7989 int err = 0;
7990
7991 /* Only request the irq if this is the first time through, and
7992 * not when we're rebuilding after a Reset
7993 */
7994 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
7995 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00007996 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007997 if (err) {
7998 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08007999 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008000 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008001 return -EFAULT;
8002 }
8003 }
8004
Jacob Kellerab437b52014-12-14 01:55:08 +00008005 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008006
8007 /* associate no queues to the misc vector */
8008 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8009 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8010
8011 i40e_flush(hw);
8012
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008013 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008014
8015 return err;
8016}
8017
8018/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008019 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8020 * @vsi: vsi structure
8021 * @seed: RSS hash seed
8022 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008023static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8024 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008025{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008026 struct i40e_pf *pf = vsi->back;
8027 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008028 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008029
Jacob Keller776b2e12016-07-19 16:23:30 -07008030 if (seed) {
8031 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8032 (struct i40e_aqc_get_set_rss_key_data *)seed;
8033 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8034 if (ret) {
8035 dev_info(&pf->pdev->dev,
8036 "Cannot set RSS key, err %s aq_err %s\n",
8037 i40e_stat_str(hw, ret),
8038 i40e_aq_str(hw, hw->aq.asq_last_status));
8039 return ret;
8040 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008041 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008042 if (lut) {
8043 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008044
Jacob Keller776b2e12016-07-19 16:23:30 -07008045 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8046 if (ret) {
8047 dev_info(&pf->pdev->dev,
8048 "Cannot set RSS lut, err %s aq_err %s\n",
8049 i40e_stat_str(hw, ret),
8050 i40e_aq_str(hw, hw->aq.asq_last_status));
8051 return ret;
8052 }
8053 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008054 return ret;
8055}
8056
8057/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008058 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8059 * @vsi: Pointer to vsi structure
8060 * @seed: Buffter to store the hash keys
8061 * @lut: Buffer to store the lookup table entries
8062 * @lut_size: Size of buffer to store the lookup table entries
8063 *
8064 * Return 0 on success, negative on failure
8065 */
8066static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8067 u8 *lut, u16 lut_size)
8068{
8069 struct i40e_pf *pf = vsi->back;
8070 struct i40e_hw *hw = &pf->hw;
8071 int ret = 0;
8072
8073 if (seed) {
8074 ret = i40e_aq_get_rss_key(hw, vsi->id,
8075 (struct i40e_aqc_get_set_rss_key_data *)seed);
8076 if (ret) {
8077 dev_info(&pf->pdev->dev,
8078 "Cannot get RSS key, err %s aq_err %s\n",
8079 i40e_stat_str(&pf->hw, ret),
8080 i40e_aq_str(&pf->hw,
8081 pf->hw.aq.asq_last_status));
8082 return ret;
8083 }
8084 }
8085
8086 if (lut) {
8087 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8088
8089 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8090 if (ret) {
8091 dev_info(&pf->pdev->dev,
8092 "Cannot get RSS lut, err %s aq_err %s\n",
8093 i40e_stat_str(&pf->hw, ret),
8094 i40e_aq_str(&pf->hw,
8095 pf->hw.aq.asq_last_status));
8096 return ret;
8097 }
8098 }
8099
8100 return ret;
8101}
8102
8103/**
Jacob Keller0582b962016-07-19 16:23:29 -07008104 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8105 * @vsi: VSI structure
8106 **/
8107static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8108{
8109 u8 seed[I40E_HKEY_ARRAY_SIZE];
8110 struct i40e_pf *pf = vsi->back;
8111 u8 *lut;
8112 int ret;
8113
8114 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8115 return 0;
8116
Jacob Keller552b9962016-07-19 16:23:31 -07008117 if (!vsi->rss_size)
8118 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8119 vsi->num_queue_pairs);
8120 if (!vsi->rss_size)
8121 return -EINVAL;
8122
Jacob Keller0582b962016-07-19 16:23:29 -07008123 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8124 if (!lut)
8125 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008126 /* Use the user configured hash keys and lookup table if there is one,
8127 * otherwise use default
8128 */
8129 if (vsi->rss_lut_user)
8130 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8131 else
8132 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8133 if (vsi->rss_hkey_user)
8134 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8135 else
8136 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008137 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8138 kfree(lut);
8139
8140 return ret;
8141}
8142
8143/**
Helin Zhang043dd652015-10-21 19:56:23 -04008144 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008145 * @vsi: Pointer to vsi structure
8146 * @seed: RSS hash seed
8147 * @lut: Lookup table
8148 * @lut_size: Lookup table size
8149 *
8150 * Returns 0 on success, negative on failure
8151 **/
8152static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8153 const u8 *lut, u16 lut_size)
8154{
8155 struct i40e_pf *pf = vsi->back;
8156 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008157 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008158 u8 i;
8159
8160 /* Fill out hash function seed */
8161 if (seed) {
8162 u32 *seed_dw = (u32 *)seed;
8163
Mitch Williamsc4e18682016-04-12 08:30:40 -07008164 if (vsi->type == I40E_VSI_MAIN) {
8165 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
8166 i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
8167 seed_dw[i]);
8168 } else if (vsi->type == I40E_VSI_SRIOV) {
8169 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
8170 i40e_write_rx_ctl(hw,
8171 I40E_VFQF_HKEY1(i, vf_id),
8172 seed_dw[i]);
8173 } else {
8174 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8175 }
Helin Zhange69ff812015-10-21 19:56:22 -04008176 }
8177
8178 if (lut) {
8179 u32 *lut_dw = (u32 *)lut;
8180
Mitch Williamsc4e18682016-04-12 08:30:40 -07008181 if (vsi->type == I40E_VSI_MAIN) {
8182 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8183 return -EINVAL;
8184 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8185 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8186 } else if (vsi->type == I40E_VSI_SRIOV) {
8187 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8188 return -EINVAL;
8189 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8190 i40e_write_rx_ctl(hw,
8191 I40E_VFQF_HLUT1(i, vf_id),
8192 lut_dw[i]);
8193 } else {
8194 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8195 }
Helin Zhange69ff812015-10-21 19:56:22 -04008196 }
8197 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008198
8199 return 0;
8200}
8201
8202/**
Helin Zhang043dd652015-10-21 19:56:23 -04008203 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8204 * @vsi: Pointer to VSI structure
8205 * @seed: Buffer to store the keys
8206 * @lut: Buffer to store the lookup table entries
8207 * @lut_size: Size of buffer to store the lookup table entries
8208 *
8209 * Returns 0 on success, negative on failure
8210 */
8211static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8212 u8 *lut, u16 lut_size)
8213{
8214 struct i40e_pf *pf = vsi->back;
8215 struct i40e_hw *hw = &pf->hw;
8216 u16 i;
8217
8218 if (seed) {
8219 u32 *seed_dw = (u32 *)seed;
8220
8221 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008222 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008223 }
8224 if (lut) {
8225 u32 *lut_dw = (u32 *)lut;
8226
8227 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8228 return -EINVAL;
8229 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8230 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8231 }
8232
8233 return 0;
8234}
8235
8236/**
8237 * i40e_config_rss - Configure RSS keys and lut
8238 * @vsi: Pointer to VSI structure
8239 * @seed: RSS hash seed
8240 * @lut: Lookup table
8241 * @lut_size: Lookup table size
8242 *
8243 * Returns 0 on success, negative on failure
8244 */
8245int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8246{
8247 struct i40e_pf *pf = vsi->back;
8248
8249 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8250 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8251 else
8252 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8253}
8254
8255/**
8256 * i40e_get_rss - Get RSS keys and lut
8257 * @vsi: Pointer to VSI structure
8258 * @seed: Buffer to store the keys
8259 * @lut: Buffer to store the lookup table entries
8260 * lut_size: Size of buffer to store the lookup table entries
8261 *
8262 * Returns 0 on success, negative on failure
8263 */
8264int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8265{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008266 struct i40e_pf *pf = vsi->back;
8267
8268 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8269 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8270 else
8271 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008272}
8273
8274/**
Helin Zhange69ff812015-10-21 19:56:22 -04008275 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8276 * @pf: Pointer to board private structure
8277 * @lut: Lookup table
8278 * @rss_table_size: Lookup table size
8279 * @rss_size: Range of queue number for hashing
8280 */
Alan Bradyf1582352016-08-24 11:33:46 -07008281void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8282 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008283{
Helin Zhange69ff812015-10-21 19:56:22 -04008284 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008285
Helin Zhange69ff812015-10-21 19:56:22 -04008286 for (i = 0; i < rss_table_size; i++)
8287 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008288}
8289
8290/**
Helin Zhang043dd652015-10-21 19:56:23 -04008291 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008292 * @pf: board private structure
8293 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008294static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008295{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008296 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008297 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008298 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008299 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008300 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008301 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008302 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008303
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008304 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008305 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8306 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008307 hena |= i40e_pf_get_default_rss_hena(pf);
8308
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008309 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8310 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008311
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008312 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008313 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008314 reg_val = (pf->rss_table_size == 512) ?
8315 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8316 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008317 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008318
Helin Zhang28c58692015-10-26 19:44:27 -04008319 /* Determine the RSS size of the VSI */
8320 if (!vsi->rss_size)
Helin Zhangacd65442015-10-26 19:44:28 -04008321 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8322 vsi->num_queue_pairs);
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008323 if (!vsi->rss_size)
8324 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008325
Helin Zhange69ff812015-10-21 19:56:22 -04008326 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8327 if (!lut)
8328 return -ENOMEM;
8329
Helin Zhang28c58692015-10-26 19:44:27 -04008330 /* Use user configured lut if there is one, otherwise use default */
8331 if (vsi->rss_lut_user)
8332 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8333 else
8334 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008335
Helin Zhang28c58692015-10-26 19:44:27 -04008336 /* Use user configured hash key if there is one, otherwise
8337 * use default.
8338 */
8339 if (vsi->rss_hkey_user)
8340 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8341 else
8342 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008343 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008344 kfree(lut);
8345
8346 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008347}
8348
8349/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008350 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8351 * @pf: board private structure
8352 * @queue_count: the requested queue count for rss.
8353 *
8354 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8355 * count which may be different from the requested queue count.
8356 **/
8357int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8358{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008359 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8360 int new_rss_size;
8361
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008362 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8363 return 0;
8364
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008365 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008366
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008367 if (queue_count != vsi->num_queue_pairs) {
8368 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008369 i40e_prep_for_reset(pf);
8370
Helin Zhangacd65442015-10-26 19:44:28 -04008371 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008372
8373 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008374
8375 /* Discard the user configured hash keys and lut, if less
8376 * queues are enabled.
8377 */
8378 if (queue_count < vsi->rss_size) {
8379 i40e_clear_rss_config_user(vsi);
8380 dev_dbg(&pf->pdev->dev,
8381 "discard user configured hash keys and lut\n");
8382 }
8383
8384 /* Reset vsi->rss_size, as number of enabled queues changed */
Helin Zhangacd65442015-10-26 19:44:28 -04008385 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8386 vsi->num_queue_pairs);
Helin Zhang28c58692015-10-26 19:44:27 -04008387
Helin Zhang043dd652015-10-21 19:56:23 -04008388 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008389 }
Lihong Yang12815052016-09-27 11:28:48 -07008390 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8391 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008392 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008393}
8394
8395/**
Greg Rosef4492db2015-02-06 08:52:12 +00008396 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8397 * @pf: board private structure
8398 **/
8399i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8400{
8401 i40e_status status;
8402 bool min_valid, max_valid;
8403 u32 max_bw, min_bw;
8404
8405 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8406 &min_valid, &max_valid);
8407
8408 if (!status) {
8409 if (min_valid)
8410 pf->npar_min_bw = min_bw;
8411 if (max_valid)
8412 pf->npar_max_bw = max_bw;
8413 }
8414
8415 return status;
8416}
8417
8418/**
8419 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8420 * @pf: board private structure
8421 **/
8422i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8423{
8424 struct i40e_aqc_configure_partition_bw_data bw_data;
8425 i40e_status status;
8426
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008427 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008428 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008429 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8430 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8431
8432 /* Set the new bandwidths */
8433 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8434
8435 return status;
8436}
8437
8438/**
8439 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8440 * @pf: board private structure
8441 **/
8442i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8443{
8444 /* Commit temporary BW setting to permanent NVM image */
8445 enum i40e_admin_queue_err last_aq_status;
8446 i40e_status ret;
8447 u16 nvm_word;
8448
8449 if (pf->hw.partition_id != 1) {
8450 dev_info(&pf->pdev->dev,
8451 "Commit BW only works on partition 1! This is partition %d",
8452 pf->hw.partition_id);
8453 ret = I40E_NOT_SUPPORTED;
8454 goto bw_commit_out;
8455 }
8456
8457 /* Acquire NVM for read access */
8458 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8459 last_aq_status = pf->hw.aq.asq_last_status;
8460 if (ret) {
8461 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008462 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8463 i40e_stat_str(&pf->hw, ret),
8464 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008465 goto bw_commit_out;
8466 }
8467
8468 /* Read word 0x10 of NVM - SW compatibility word 1 */
8469 ret = i40e_aq_read_nvm(&pf->hw,
8470 I40E_SR_NVM_CONTROL_WORD,
8471 0x10, sizeof(nvm_word), &nvm_word,
8472 false, NULL);
8473 /* Save off last admin queue command status before releasing
8474 * the NVM
8475 */
8476 last_aq_status = pf->hw.aq.asq_last_status;
8477 i40e_release_nvm(&pf->hw);
8478 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008479 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8480 i40e_stat_str(&pf->hw, ret),
8481 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008482 goto bw_commit_out;
8483 }
8484
8485 /* Wait a bit for NVM release to complete */
8486 msleep(50);
8487
8488 /* Acquire NVM for write access */
8489 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8490 last_aq_status = pf->hw.aq.asq_last_status;
8491 if (ret) {
8492 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008493 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8494 i40e_stat_str(&pf->hw, ret),
8495 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008496 goto bw_commit_out;
8497 }
8498 /* Write it back out unchanged to initiate update NVM,
8499 * which will force a write of the shadow (alt) RAM to
8500 * the NVM - thus storing the bandwidth values permanently.
8501 */
8502 ret = i40e_aq_update_nvm(&pf->hw,
8503 I40E_SR_NVM_CONTROL_WORD,
8504 0x10, sizeof(nvm_word),
8505 &nvm_word, true, NULL);
8506 /* Save off last admin queue command status before releasing
8507 * the NVM
8508 */
8509 last_aq_status = pf->hw.aq.asq_last_status;
8510 i40e_release_nvm(&pf->hw);
8511 if (ret)
8512 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008513 "BW settings NOT SAVED, err %s aq_err %s\n",
8514 i40e_stat_str(&pf->hw, ret),
8515 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008516bw_commit_out:
8517
8518 return ret;
8519}
8520
8521/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008522 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8523 * @pf: board private structure to initialize
8524 *
8525 * i40e_sw_init initializes the Adapter private data structure.
8526 * Fields are initialized based on PCI device information and
8527 * OS network device settings (MTU size).
8528 **/
8529static int i40e_sw_init(struct i40e_pf *pf)
8530{
8531 int err = 0;
8532 int size;
8533
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008534 /* Set default capability flags */
8535 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8536 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008537 I40E_FLAG_MSIX_ENABLED;
8538
Mitch Williamsca99eb92014-04-04 04:43:07 +00008539 /* Set default ITR */
8540 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8541 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8542
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008543 /* Depending on PF configurations, it is possible that the RSS
8544 * maximum might end up larger than the available queues
8545 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008546 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008547 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008548 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008549 pf->rss_size_max = min_t(int, pf->rss_size_max,
8550 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008551 if (pf->hw.func_caps.rss) {
8552 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008553 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8554 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008555 }
8556
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008557 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008558 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008559 pf->flags |= I40E_FLAG_MFP_ENABLED;
8560 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008561 if (i40e_get_npar_bw_setting(pf))
8562 dev_warn(&pf->pdev->dev,
8563 "Could not get NPAR bw settings\n");
8564 else
8565 dev_info(&pf->pdev->dev,
8566 "Min BW = %8.8x, Max BW = %8.8x\n",
8567 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008568 }
8569
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008570 /* FW/NVM is not yet fixed in this regard */
8571 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8572 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8573 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8574 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008575 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8576 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008577 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008578 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008579 else
8580 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008581 pf->fdir_pf_filter_count =
8582 pf->hw.func_caps.fd_filters_guaranteed;
8583 pf->hw.fdir_shared_filter_count =
8584 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008585 }
8586
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008587 if (i40e_is_mac_710(&pf->hw) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008588 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008589 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008590 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008591 /* No DCB support for FW < v4.33 */
8592 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8593 }
8594
8595 /* Disable FW LLDP if FW < v4.3 */
8596 if (i40e_is_mac_710(&pf->hw) &&
8597 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8598 (pf->hw.aq.fw_maj_ver < 4)))
8599 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8600
8601 /* Use the FW Set LLDP MIB API if FW > v4.40 */
8602 if (i40e_is_mac_710(&pf->hw) &&
8603 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8604 (pf->hw.aq.fw_maj_ver >= 5)))
8605 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008607 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008608 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008609 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008610 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008611 }
8612
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008613 if (pf->hw.func_caps.iwarp) {
8614 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8615 /* IWARP needs one extra vector for CQP just like MISC.*/
8616 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8617 }
8618
Vasu Dev38e00432014-08-01 13:27:03 -07008619#ifdef I40E_FCOE
Shannon Nelson21364bc2015-08-26 15:14:13 -04008620 i40e_init_pf_fcoe(pf);
Vasu Dev38e00432014-08-01 13:27:03 -07008621
8622#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008623#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008624 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008625 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8626 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8627 pf->num_req_vfs = min_t(int,
8628 pf->hw.func_caps.num_vfs,
8629 I40E_MAX_VF_COUNT);
8630 }
8631#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008632 if (pf->hw.mac.type == I40E_MAC_X722) {
8633 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
8634 I40E_FLAG_128_QP_RSS_CAPABLE |
8635 I40E_FLAG_HW_ATR_EVICT_CAPABLE |
8636 I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
8637 I40E_FLAG_WB_ON_ITR_CAPABLE |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008638 I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE |
Jesse Brandeburg8e2cc0e2016-02-29 11:00:24 -08008639 I40E_FLAG_NO_PCI_LINK_CHECK |
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008640 I40E_FLAG_USE_SET_LLDP_MIB |
Singhai, Anjali6a899022015-12-14 12:21:18 -08008641 I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008642 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8643 ((pf->hw.aq.api_maj_ver == 1) &&
8644 (pf->hw.aq.api_min_ver > 4))) {
8645 /* Supported in FW API version higher than 1.4 */
8646 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008647 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
8648 } else {
8649 pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008650 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008651
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008652 pf->eeprom_version = 0xDEAD;
8653 pf->lan_veb = I40E_NO_VEB;
8654 pf->lan_vsi = I40E_NO_VSI;
8655
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008656 /* By default FW has this off for performance reasons */
8657 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8658
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008659 /* set up queue assignment tracking */
8660 size = sizeof(struct i40e_lump_tracking)
8661 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8662 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8663 if (!pf->qp_pile) {
8664 err = -ENOMEM;
8665 goto sw_init_done;
8666 }
8667 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8668 pf->qp_pile->search_hint = 0;
8669
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008670 pf->tx_timeout_recovery_level = 1;
8671
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008672 mutex_init(&pf->switch_mutex);
8673
Greg Rosec668a122015-02-26 16:10:39 +00008674 /* If NPAR is enabled nudge the Tx scheduler */
8675 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8676 i40e_set_npar_bw_setting(pf);
8677
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008678sw_init_done:
8679 return err;
8680}
8681
8682/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008683 * i40e_set_ntuple - set the ntuple feature flag and take action
8684 * @pf: board private structure to initialize
8685 * @features: the feature set that the stack is suggesting
8686 *
8687 * returns a bool to indicate if reset needs to happen
8688 **/
8689bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8690{
8691 bool need_reset = false;
8692
8693 /* Check if Flow Director n-tuple support was enabled or disabled. If
8694 * the state changed, we need to reset.
8695 */
8696 if (features & NETIF_F_NTUPLE) {
8697 /* Enable filters and mark for reset */
8698 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8699 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008700 /* enable FD_SB only if there is MSI-X vector */
8701 if (pf->num_fdsb_msix > 0)
8702 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008703 } else {
8704 /* turn off filters, mark for reset and clear SW filter list */
8705 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8706 need_reset = true;
8707 i40e_fdir_filter_exit(pf);
8708 }
8709 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008710 pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008711 /* reset fd counters */
8712 pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
8713 pf->fdir_pf_active_filters = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008714 /* if ATR was auto disabled it can be re-enabled. */
8715 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller234dc4e2016-09-06 18:05:09 -07008716 (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008717 pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008718 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8719 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8720 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008721 }
8722 return need_reset;
8723}
8724
8725/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008726 * i40e_clear_rss_lut - clear the rx hash lookup table
8727 * @vsi: the VSI being configured
8728 **/
8729static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8730{
8731 struct i40e_pf *pf = vsi->back;
8732 struct i40e_hw *hw = &pf->hw;
8733 u16 vf_id = vsi->vf_id;
8734 u8 i;
8735
8736 if (vsi->type == I40E_VSI_MAIN) {
8737 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8738 wr32(hw, I40E_PFQF_HLUT(i), 0);
8739 } else if (vsi->type == I40E_VSI_SRIOV) {
8740 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8741 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8742 } else {
8743 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8744 }
8745}
8746
8747/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008748 * i40e_set_features - set the netdev feature flags
8749 * @netdev: ptr to the netdev being adjusted
8750 * @features: the feature set that the stack is suggesting
8751 **/
8752static int i40e_set_features(struct net_device *netdev,
8753 netdev_features_t features)
8754{
8755 struct i40e_netdev_priv *np = netdev_priv(netdev);
8756 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008757 struct i40e_pf *pf = vsi->back;
8758 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008759
Alan Bradyd8ec9862016-07-27 12:02:38 -07008760 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8761 i40e_pf_config_rss(pf);
8762 else if (!(features & NETIF_F_RXHASH) &&
8763 netdev->features & NETIF_F_RXHASH)
8764 i40e_clear_rss_lut(vsi);
8765
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008766 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8767 i40e_vlan_stripping_enable(vsi);
8768 else
8769 i40e_vlan_stripping_disable(vsi);
8770
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008771 need_reset = i40e_set_ntuple(pf, features);
8772
8773 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008774 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008775
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008776 return 0;
8777}
8778
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008779/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008780 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008781 * @pf: board private structure
8782 * @port: The UDP port to look up
8783 *
8784 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8785 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08008786static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008787{
8788 u8 i;
8789
8790 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008791 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008792 return i;
8793 }
8794
8795 return i;
8796}
8797
8798/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008799 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008800 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008801 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008802 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008803static void i40e_udp_tunnel_add(struct net_device *netdev,
8804 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008805{
8806 struct i40e_netdev_priv *np = netdev_priv(netdev);
8807 struct i40e_vsi *vsi = np->vsi;
8808 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008809 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008810 u8 next_idx;
8811 u8 idx;
8812
Singhai, Anjali6a899022015-12-14 12:21:18 -08008813 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008814
8815 /* Check if port already exists */
8816 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008817 netdev_info(netdev, "port %d already offloaded\n",
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07008818 ntohs(port));
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008819 return;
8820 }
8821
8822 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008823 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008824
8825 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008826 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008827 ntohs(port));
8828 return;
8829 }
8830
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008831 switch (ti->type) {
8832 case UDP_TUNNEL_TYPE_VXLAN:
8833 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8834 break;
8835 case UDP_TUNNEL_TYPE_GENEVE:
8836 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8837 return;
8838 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8839 break;
8840 default:
8841 return;
8842 }
8843
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008844 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008845 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008846 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8847 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008848}
8849
8850/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008851 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008852 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008853 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008854 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008855static void i40e_udp_tunnel_del(struct net_device *netdev,
8856 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008857{
8858 struct i40e_netdev_priv *np = netdev_priv(netdev);
8859 struct i40e_vsi *vsi = np->vsi;
8860 struct i40e_pf *pf = vsi->back;
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008861 __be16 port = ti->port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008862 u8 idx;
8863
Singhai, Anjali6a899022015-12-14 12:21:18 -08008864 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008865
8866 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008867 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8868 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008869
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008870 switch (ti->type) {
8871 case UDP_TUNNEL_TYPE_VXLAN:
8872 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8873 goto not_found;
8874 break;
8875 case UDP_TUNNEL_TYPE_GENEVE:
8876 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8877 goto not_found;
8878 break;
8879 default:
8880 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008881 }
8882
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008883 /* if port exists, set it to 0 (mark for deletion)
8884 * and make it pending
8885 */
8886 pf->udp_ports[idx].index = 0;
8887 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008888 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8889
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008890 return;
8891not_found:
8892 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
8893 ntohs(port));
Singhai, Anjali6a899022015-12-14 12:21:18 -08008894}
8895
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008896static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008897 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008898{
8899 struct i40e_netdev_priv *np = netdev_priv(netdev);
8900 struct i40e_pf *pf = np->vsi->back;
8901 struct i40e_hw *hw = &pf->hw;
8902
8903 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8904 return -EOPNOTSUPP;
8905
8906 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8907 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8908
8909 return 0;
8910}
8911
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008912/**
8913 * i40e_ndo_fdb_add - add an entry to the hardware database
8914 * @ndm: the input from the stack
8915 * @tb: pointer to array of nladdr (unused)
8916 * @dev: the net device pointer
8917 * @addr: the MAC address entry being added
8918 * @flags: instructions from stack about fdb operation
8919 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008920static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8921 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008922 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008923 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008924{
8925 struct i40e_netdev_priv *np = netdev_priv(dev);
8926 struct i40e_pf *pf = np->vsi->back;
8927 int err = 0;
8928
8929 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8930 return -EOPNOTSUPP;
8931
Or Gerlitz65891fe2014-12-14 18:19:05 +02008932 if (vid) {
8933 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8934 return -EINVAL;
8935 }
8936
Greg Rose4ba0dea2014-03-06 08:59:55 +00008937 /* Hardware does not support aging addresses so if a
8938 * ndm_state is given only allow permanent addresses
8939 */
8940 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
8941 netdev_info(dev, "FDB only supports static addresses\n");
8942 return -EINVAL;
8943 }
8944
8945 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
8946 err = dev_uc_add_excl(dev, addr);
8947 else if (is_multicast_ether_addr(addr))
8948 err = dev_mc_add_excl(dev, addr);
8949 else
8950 err = -EINVAL;
8951
8952 /* Only return duplicate errors if NLM_F_EXCL is set */
8953 if (err == -EEXIST && !(flags & NLM_F_EXCL))
8954 err = 0;
8955
8956 return err;
8957}
8958
Neerav Parikh51616012015-02-06 08:52:14 +00008959/**
8960 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
8961 * @dev: the netdev being configured
8962 * @nlh: RTNL message
8963 *
8964 * Inserts a new hardware bridge if not already created and
8965 * enables the bridging mode requested (VEB or VEPA). If the
8966 * hardware bridge has already been inserted and the request
8967 * is to change the mode then that requires a PF reset to
8968 * allow rebuild of the components with required hardware
8969 * bridge mode enabled.
8970 **/
8971static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04008972 struct nlmsghdr *nlh,
8973 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00008974{
8975 struct i40e_netdev_priv *np = netdev_priv(dev);
8976 struct i40e_vsi *vsi = np->vsi;
8977 struct i40e_pf *pf = vsi->back;
8978 struct i40e_veb *veb = NULL;
8979 struct nlattr *attr, *br_spec;
8980 int i, rem;
8981
8982 /* Only for PF VSI for now */
8983 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
8984 return -EOPNOTSUPP;
8985
8986 /* Find the HW bridge for PF VSI */
8987 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
8988 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
8989 veb = pf->veb[i];
8990 }
8991
8992 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8993
8994 nla_for_each_nested(attr, br_spec, rem) {
8995 __u16 mode;
8996
8997 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8998 continue;
8999
9000 mode = nla_get_u16(attr);
9001 if ((mode != BRIDGE_MODE_VEPA) &&
9002 (mode != BRIDGE_MODE_VEB))
9003 return -EINVAL;
9004
9005 /* Insert a new HW bridge */
9006 if (!veb) {
9007 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9008 vsi->tc_config.enabled_tc);
9009 if (veb) {
9010 veb->bridge_mode = mode;
9011 i40e_config_bridge_mode(veb);
9012 } else {
9013 /* No Bridge HW offload available */
9014 return -ENOENT;
9015 }
9016 break;
9017 } else if (mode != veb->bridge_mode) {
9018 /* Existing HW bridge but different mode needs reset */
9019 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009020 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9021 if (mode == BRIDGE_MODE_VEB)
9022 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9023 else
9024 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9025 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009026 break;
9027 }
9028 }
9029
9030 return 0;
9031}
9032
9033/**
9034 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9035 * @skb: skb buff
9036 * @pid: process id
9037 * @seq: RTNL message seq #
9038 * @dev: the netdev being configured
9039 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009040 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009041 *
9042 * Return the mode in which the hardware bridge is operating in
9043 * i.e VEB or VEPA.
9044 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009045static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9046 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009047 u32 __always_unused filter_mask,
9048 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009049{
9050 struct i40e_netdev_priv *np = netdev_priv(dev);
9051 struct i40e_vsi *vsi = np->vsi;
9052 struct i40e_pf *pf = vsi->back;
9053 struct i40e_veb *veb = NULL;
9054 int i;
9055
9056 /* Only for PF VSI for now */
9057 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9058 return -EOPNOTSUPP;
9059
9060 /* Find the HW bridge for the PF VSI */
9061 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9062 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9063 veb = pf->veb[i];
9064 }
9065
9066 if (!veb)
9067 return 0;
9068
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009069 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009070 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009071}
Neerav Parikh51616012015-02-06 08:52:14 +00009072
Joe Stringerf44a75e2015-04-14 17:09:14 -07009073/**
9074 * i40e_features_check - Validate encapsulated packet conforms to limits
9075 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009076 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009077 * @features: Offload features that the stack believes apply
9078 **/
9079static netdev_features_t i40e_features_check(struct sk_buff *skb,
9080 struct net_device *dev,
9081 netdev_features_t features)
9082{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009083 size_t len;
9084
9085 /* No point in doing any of this if neither checksum nor GSO are
9086 * being requested for this frame. We can rule out both by just
9087 * checking for CHECKSUM_PARTIAL
9088 */
9089 if (skb->ip_summed != CHECKSUM_PARTIAL)
9090 return features;
9091
9092 /* We cannot support GSO if the MSS is going to be less than
9093 * 64 bytes. If it is then we need to drop support for GSO.
9094 */
9095 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9096 features &= ~NETIF_F_GSO_MASK;
9097
9098 /* MACLEN can support at most 63 words */
9099 len = skb_network_header(skb) - skb->data;
9100 if (len & ~(63 * 2))
9101 goto out_err;
9102
9103 /* IPLEN and EIPLEN can support at most 127 dwords */
9104 len = skb_transport_header(skb) - skb_network_header(skb);
9105 if (len & ~(127 * 4))
9106 goto out_err;
9107
9108 if (skb->encapsulation) {
9109 /* L4TUNLEN can support 127 words */
9110 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9111 if (len & ~(127 * 2))
9112 goto out_err;
9113
9114 /* IPLEN can support at most 127 dwords */
9115 len = skb_inner_transport_header(skb) -
9116 skb_inner_network_header(skb);
9117 if (len & ~(127 * 4))
9118 goto out_err;
9119 }
9120
9121 /* No need to validate L4LEN as TCP is the only protocol with a
9122 * a flexible value and we support all possible values supported
9123 * by TCP, which is at most 15 dwords
9124 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009125
9126 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009127out_err:
9128 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009129}
9130
Shannon Nelson37a29732015-02-27 09:15:19 +00009131static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009132 .ndo_open = i40e_open,
9133 .ndo_stop = i40e_close,
9134 .ndo_start_xmit = i40e_lan_xmit_frame,
9135 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9136 .ndo_set_rx_mode = i40e_set_rx_mode,
9137 .ndo_validate_addr = eth_validate_addr,
9138 .ndo_set_mac_address = i40e_set_mac,
9139 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009140 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009141 .ndo_tx_timeout = i40e_tx_timeout,
9142 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9143 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9144#ifdef CONFIG_NET_POLL_CONTROLLER
9145 .ndo_poll_controller = i40e_netpoll,
9146#endif
John Fastabende4c67342016-02-16 21:16:15 -08009147 .ndo_setup_tc = __i40e_setup_tc,
Vasu Dev38e00432014-08-01 13:27:03 -07009148#ifdef I40E_FCOE
9149 .ndo_fcoe_enable = i40e_fcoe_enable,
9150 .ndo_fcoe_disable = i40e_fcoe_disable,
9151#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009152 .ndo_set_features = i40e_set_features,
9153 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9154 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009155 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009156 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009157 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009158 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009159 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009160 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9161 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009162 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009163 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009164 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009165 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9166 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009167};
9168
9169/**
9170 * i40e_config_netdev - Setup the netdev flags
9171 * @vsi: the VSI being configured
9172 *
9173 * Returns 0 on success, negative value on failure
9174 **/
9175static int i40e_config_netdev(struct i40e_vsi *vsi)
9176{
9177 struct i40e_pf *pf = vsi->back;
9178 struct i40e_hw *hw = &pf->hw;
9179 struct i40e_netdev_priv *np;
9180 struct net_device *netdev;
9181 u8 mac_addr[ETH_ALEN];
9182 int etherdev_size;
9183
9184 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009185 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009186 if (!netdev)
9187 return -ENOMEM;
9188
9189 vsi->netdev = netdev;
9190 np = netdev_priv(netdev);
9191 np->vsi = vsi;
9192
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009193 netdev->hw_enc_features |= NETIF_F_SG |
9194 NETIF_F_IP_CSUM |
9195 NETIF_F_IPV6_CSUM |
9196 NETIF_F_HIGHDMA |
9197 NETIF_F_SOFT_FEATURES |
9198 NETIF_F_TSO |
9199 NETIF_F_TSO_ECN |
9200 NETIF_F_TSO6 |
9201 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009202 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009203 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009204 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009205 NETIF_F_GSO_UDP_TUNNEL |
9206 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009207 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009208 NETIF_F_SCTP_CRC |
9209 NETIF_F_RXHASH |
9210 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009211 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009212
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009213 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009214 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9215
9216 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009217
9218 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009219 netdev->vlan_features |= netdev->hw_enc_features |
9220 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009221
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009222 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009223 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009224
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009225 netdev->hw_features |= netdev->hw_enc_features |
9226 NETIF_F_HW_VLAN_CTAG_TX |
9227 NETIF_F_HW_VLAN_CTAG_RX;
9228
9229 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009230 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009231
9232 if (vsi->type == I40E_VSI_MAIN) {
9233 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009234 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009235 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009236 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009237 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009238 } else {
9239 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9240 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9241 pf->vsi[pf->lan_vsi]->netdev->name);
9242 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009243
Jacob Keller278e7d02016-10-05 09:30:37 -07009244 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller1bc87e82016-10-05 09:30:31 -07009245 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY);
Jacob Keller278e7d02016-10-05 09:30:37 -07009246 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009247 }
Kiran Patil21659032015-09-30 14:09:03 -04009248
Greg Rose9a173902014-05-22 06:32:02 +00009249 ether_addr_copy(netdev->dev_addr, mac_addr);
9250 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009251
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009252 netdev->priv_flags |= IFF_UNICAST_FLT;
9253 netdev->priv_flags |= IFF_SUPP_NOFCS;
9254 /* Setup netdev TC information */
9255 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9256
9257 netdev->netdev_ops = &i40e_netdev_ops;
9258 netdev->watchdog_timeo = 5 * HZ;
9259 i40e_set_ethtool_ops(netdev);
Vasu Dev38e00432014-08-01 13:27:03 -07009260#ifdef I40E_FCOE
9261 i40e_fcoe_config_netdev(netdev, vsi);
9262#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009263
Jarod Wilson91c527a2016-10-17 15:54:05 -04009264 /* MTU range: 68 - 9706 */
9265 netdev->min_mtu = ETH_MIN_MTU;
9266 netdev->max_mtu = I40E_MAX_RXBUFFER -
9267 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9268
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009269 return 0;
9270}
9271
9272/**
9273 * i40e_vsi_delete - Delete a VSI from the switch
9274 * @vsi: the VSI being removed
9275 *
9276 * Returns 0 on success, negative value on failure
9277 **/
9278static void i40e_vsi_delete(struct i40e_vsi *vsi)
9279{
9280 /* remove default VSI is not allowed */
9281 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9282 return;
9283
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009284 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009285}
9286
9287/**
Neerav Parikh51616012015-02-06 08:52:14 +00009288 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9289 * @vsi: the VSI being queried
9290 *
9291 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9292 **/
9293int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9294{
9295 struct i40e_veb *veb;
9296 struct i40e_pf *pf = vsi->back;
9297
9298 /* Uplink is not a bridge so default to VEB */
9299 if (vsi->veb_idx == I40E_NO_VEB)
9300 return 1;
9301
9302 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009303 if (!veb) {
9304 dev_info(&pf->pdev->dev,
9305 "There is no veb associated with the bridge\n");
9306 return -ENOENT;
9307 }
Neerav Parikh51616012015-02-06 08:52:14 +00009308
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009309 /* Uplink is a bridge in VEPA mode */
9310 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9311 return 0;
9312 } else {
9313 /* Uplink is a bridge in VEB mode */
9314 return 1;
9315 }
9316
9317 /* VEPA is now default bridge, so return 0 */
9318 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009319}
9320
9321/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009322 * i40e_add_vsi - Add a VSI to the switch
9323 * @vsi: the VSI being configured
9324 *
9325 * This initializes a VSI context depending on the VSI type to be added and
9326 * passes it down to the add_vsi aq command.
9327 **/
9328static int i40e_add_vsi(struct i40e_vsi *vsi)
9329{
9330 int ret = -ENODEV;
Kiran Patilf6bd0962016-06-20 09:10:34 -07009331 i40e_status aq_ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009332 struct i40e_pf *pf = vsi->back;
9333 struct i40e_hw *hw = &pf->hw;
9334 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009335 struct i40e_mac_filter *f;
9336 struct hlist_node *h;
9337 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009338
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009339 u8 enabled_tc = 0x1; /* TC0 enabled */
9340 int f_count = 0;
9341
9342 memset(&ctxt, 0, sizeof(ctxt));
9343 switch (vsi->type) {
9344 case I40E_VSI_MAIN:
9345 /* The PF's main VSI is already setup as part of the
9346 * device initialization, so we'll not bother with
9347 * the add_vsi call, but we will retrieve the current
9348 * VSI context.
9349 */
9350 ctxt.seid = pf->main_vsi_seid;
9351 ctxt.pf_num = pf->hw.pf_id;
9352 ctxt.vf_num = 0;
9353 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9354 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9355 if (ret) {
9356 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009357 "couldn't get PF vsi config, err %s aq_err %s\n",
9358 i40e_stat_str(&pf->hw, ret),
9359 i40e_aq_str(&pf->hw,
9360 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009361 return -ENOENT;
9362 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009363 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009364 vsi->info.valid_sections = 0;
9365
9366 vsi->seid = ctxt.seid;
9367 vsi->id = ctxt.vsi_number;
9368
9369 enabled_tc = i40e_pf_get_tc_map(pf);
9370
9371 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009372 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9373 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009374 memset(&ctxt, 0, sizeof(ctxt));
9375 ctxt.seid = pf->main_vsi_seid;
9376 ctxt.pf_num = pf->hw.pf_id;
9377 ctxt.vf_num = 0;
9378 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9379 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9380 if (ret) {
9381 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009382 "update vsi failed, err %s aq_err %s\n",
9383 i40e_stat_str(&pf->hw, ret),
9384 i40e_aq_str(&pf->hw,
9385 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009386 ret = -ENOENT;
9387 goto err;
9388 }
9389 /* update the local VSI info queue map */
9390 i40e_vsi_update_queue_map(vsi, &ctxt);
9391 vsi->info.valid_sections = 0;
9392 } else {
9393 /* Default/Main VSI is only enabled for TC0
9394 * reconfigure it to enable all TCs that are
9395 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009396 * For MFP case the iSCSI PF would use this
9397 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009398 */
9399 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9400 if (ret) {
9401 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009402 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9403 enabled_tc,
9404 i40e_stat_str(&pf->hw, ret),
9405 i40e_aq_str(&pf->hw,
9406 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009407 ret = -ENOENT;
9408 }
9409 }
9410 break;
9411
9412 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009413 ctxt.pf_num = hw->pf_id;
9414 ctxt.vf_num = 0;
9415 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009416 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009417 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009418 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9419 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009420 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009421 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009422 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009423 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009424 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009425 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009426 break;
9427
9428 case I40E_VSI_VMDQ2:
9429 ctxt.pf_num = hw->pf_id;
9430 ctxt.vf_num = 0;
9431 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009432 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009433 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9434
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009435 /* This VSI is connected to VEB so the switch_id
9436 * should be set to zero by default.
9437 */
Neerav Parikh51616012015-02-06 08:52:14 +00009438 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9439 ctxt.info.valid_sections |=
9440 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9441 ctxt.info.switch_id =
9442 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9443 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009444
9445 /* Setup the VSI tx/rx queue map for TC0 only for now */
9446 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9447 break;
9448
9449 case I40E_VSI_SRIOV:
9450 ctxt.pf_num = hw->pf_id;
9451 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9452 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009453 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009454 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9455
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009456 /* This VSI is connected to VEB so the switch_id
9457 * should be set to zero by default.
9458 */
Neerav Parikh51616012015-02-06 08:52:14 +00009459 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9460 ctxt.info.valid_sections |=
9461 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9462 ctxt.info.switch_id =
9463 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9464 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009465
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009466 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9467 ctxt.info.valid_sections |=
9468 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9469 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009470 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9471 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009472 }
9473
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009474 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9475 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009476 if (pf->vf[vsi->vf_id].spoofchk) {
9477 ctxt.info.valid_sections |=
9478 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9479 ctxt.info.sec_flags |=
9480 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9481 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9482 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009483 /* Setup the VSI tx/rx queue map for TC0 only for now */
9484 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9485 break;
9486
Vasu Dev38e00432014-08-01 13:27:03 -07009487#ifdef I40E_FCOE
9488 case I40E_VSI_FCOE:
9489 ret = i40e_fcoe_vsi_init(vsi, &ctxt);
9490 if (ret) {
9491 dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
9492 return ret;
9493 }
9494 break;
9495
9496#endif /* I40E_FCOE */
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009497 case I40E_VSI_IWARP:
9498 /* send down message to iWARP */
9499 break;
9500
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009501 default:
9502 return -ENODEV;
9503 }
9504
9505 if (vsi->type != I40E_VSI_MAIN) {
9506 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9507 if (ret) {
9508 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009509 "add vsi failed, err %s aq_err %s\n",
9510 i40e_stat_str(&pf->hw, ret),
9511 i40e_aq_str(&pf->hw,
9512 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009513 ret = -ENOENT;
9514 goto err;
9515 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009516 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009517 vsi->info.valid_sections = 0;
9518 vsi->seid = ctxt.seid;
9519 vsi->id = ctxt.vsi_number;
9520 }
Kiran Patilf6bd0962016-06-20 09:10:34 -07009521 /* Except FDIR VSI, for all othet VSI set the broadcast filter */
9522 if (vsi->type != I40E_VSI_FDIR) {
9523 aq_ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
9524 if (aq_ret) {
9525 ret = i40e_aq_rc_to_posix(aq_ret,
9526 hw->aq.asq_last_status);
9527 dev_info(&pf->pdev->dev,
9528 "set brdcast promisc failed, err %s, aq_err %s\n",
9529 i40e_stat_str(hw, aq_ret),
9530 i40e_aq_str(hw, hw->aq.asq_last_status));
9531 }
9532 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009533
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009534 vsi->active_filters = 0;
9535 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009536 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009537 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009538 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009539 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009540 f_count++;
9541 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009542 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009543
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009544 if (f_count) {
9545 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9546 pf->flags |= I40E_FLAG_FILTER_SYNC;
9547 }
9548
9549 /* Update VSI BW information */
9550 ret = i40e_vsi_get_bw_info(vsi);
9551 if (ret) {
9552 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009553 "couldn't get vsi bw info, err %s aq_err %s\n",
9554 i40e_stat_str(&pf->hw, ret),
9555 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009556 /* VSI is already added so not tearing that up */
9557 ret = 0;
9558 }
9559
9560err:
9561 return ret;
9562}
9563
9564/**
9565 * i40e_vsi_release - Delete a VSI and free its resources
9566 * @vsi: the VSI being removed
9567 *
9568 * Returns 0 on success or < 0 on error
9569 **/
9570int i40e_vsi_release(struct i40e_vsi *vsi)
9571{
Jacob Keller278e7d02016-10-05 09:30:37 -07009572 struct i40e_mac_filter *f;
9573 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009574 struct i40e_veb *veb = NULL;
9575 struct i40e_pf *pf;
9576 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009577 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009578
9579 pf = vsi->back;
9580
9581 /* release of a VEB-owner or last VSI is not allowed */
9582 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9583 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9584 vsi->seid, vsi->uplink_seid);
9585 return -ENODEV;
9586 }
9587 if (vsi == pf->vsi[pf->lan_vsi] &&
9588 !test_bit(__I40E_DOWN, &pf->state)) {
9589 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9590 return -ENODEV;
9591 }
9592
9593 uplink_seid = vsi->uplink_seid;
9594 if (vsi->type != I40E_VSI_SRIOV) {
9595 if (vsi->netdev_registered) {
9596 vsi->netdev_registered = false;
9597 if (vsi->netdev) {
9598 /* results in a call to i40e_close() */
9599 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009600 }
9601 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009602 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009603 }
9604 i40e_vsi_disable_irq(vsi);
9605 }
9606
Jacob Keller278e7d02016-10-05 09:30:37 -07009607 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009608
9609 /* clear the sync flag on all filters */
9610 if (vsi->netdev) {
9611 __dev_uc_unsync(vsi->netdev, NULL);
9612 __dev_mc_unsync(vsi->netdev, NULL);
9613 }
9614
9615 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009616 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009617 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009618
Jacob Keller278e7d02016-10-05 09:30:37 -07009619 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009620
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009621 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009622
9623 i40e_vsi_delete(vsi);
9624 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009625 if (vsi->netdev) {
9626 free_netdev(vsi->netdev);
9627 vsi->netdev = NULL;
9628 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009629 i40e_vsi_clear_rings(vsi);
9630 i40e_vsi_clear(vsi);
9631
9632 /* If this was the last thing on the VEB, except for the
9633 * controlling VSI, remove the VEB, which puts the controlling
9634 * VSI onto the next level down in the switch.
9635 *
9636 * Well, okay, there's one more exception here: don't remove
9637 * the orphan VEBs yet. We'll wait for an explicit remove request
9638 * from up the network stack.
9639 */
Mitch Williams505682c2014-05-20 08:01:37 +00009640 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009641 if (pf->vsi[i] &&
9642 pf->vsi[i]->uplink_seid == uplink_seid &&
9643 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9644 n++; /* count the VSIs */
9645 }
9646 }
9647 for (i = 0; i < I40E_MAX_VEB; i++) {
9648 if (!pf->veb[i])
9649 continue;
9650 if (pf->veb[i]->uplink_seid == uplink_seid)
9651 n++; /* count the VEBs */
9652 if (pf->veb[i]->seid == uplink_seid)
9653 veb = pf->veb[i];
9654 }
9655 if (n == 0 && veb && veb->uplink_seid != 0)
9656 i40e_veb_release(veb);
9657
9658 return 0;
9659}
9660
9661/**
9662 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9663 * @vsi: ptr to the VSI
9664 *
9665 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9666 * corresponding SW VSI structure and initializes num_queue_pairs for the
9667 * newly allocated VSI.
9668 *
9669 * Returns 0 on success or negative on failure
9670 **/
9671static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9672{
9673 int ret = -ENOENT;
9674 struct i40e_pf *pf = vsi->back;
9675
Alexander Duyck493fb302013-09-28 07:01:44 +00009676 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009677 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9678 vsi->seid);
9679 return -EEXIST;
9680 }
9681
9682 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009683 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009684 vsi->seid, vsi->base_vector);
9685 return -EEXIST;
9686 }
9687
Greg Rose90e04072014-03-06 08:59:57 +00009688 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009689 if (ret) {
9690 dev_info(&pf->pdev->dev,
9691 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9692 vsi->num_q_vectors, vsi->seid, ret);
9693 vsi->num_q_vectors = 0;
9694 goto vector_setup_out;
9695 }
9696
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009697 /* In Legacy mode, we do not have to get any other vector since we
9698 * piggyback on the misc/ICR0 for queue interrupts.
9699 */
9700 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9701 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009702 if (vsi->num_q_vectors)
9703 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9704 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009705 if (vsi->base_vector < 0) {
9706 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009707 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9708 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009709 i40e_vsi_free_q_vectors(vsi);
9710 ret = -ENOENT;
9711 goto vector_setup_out;
9712 }
9713
9714vector_setup_out:
9715 return ret;
9716}
9717
9718/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009719 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9720 * @vsi: pointer to the vsi.
9721 *
9722 * This re-allocates a vsi's queue resources.
9723 *
9724 * Returns pointer to the successfully allocated and configured VSI sw struct
9725 * on success, otherwise returns NULL on failure.
9726 **/
9727static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9728{
John Underwoodf5340392016-02-18 09:19:24 -08009729 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009730 u8 enabled_tc;
9731 int ret;
9732
John Underwoodf5340392016-02-18 09:19:24 -08009733 if (!vsi)
9734 return NULL;
9735
9736 pf = vsi->back;
9737
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009738 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9739 i40e_vsi_clear_rings(vsi);
9740
9741 i40e_vsi_free_arrays(vsi, false);
9742 i40e_set_num_rings_in_vsi(vsi);
9743 ret = i40e_vsi_alloc_arrays(vsi, false);
9744 if (ret)
9745 goto err_vsi;
9746
9747 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9748 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009749 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009750 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009751 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009752 goto err_vsi;
9753 }
9754 vsi->base_queue = ret;
9755
9756 /* Update the FW view of the VSI. Force a reset of TC and queue
9757 * layout configurations.
9758 */
9759 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9760 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9761 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9762 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
9763
9764 /* assign it some queues */
9765 ret = i40e_alloc_rings(vsi);
9766 if (ret)
9767 goto err_rings;
9768
9769 /* map all of the rings to the q_vectors */
9770 i40e_vsi_map_rings_to_vectors(vsi);
9771 return vsi;
9772
9773err_rings:
9774 i40e_vsi_free_q_vectors(vsi);
9775 if (vsi->netdev_registered) {
9776 vsi->netdev_registered = false;
9777 unregister_netdev(vsi->netdev);
9778 free_netdev(vsi->netdev);
9779 vsi->netdev = NULL;
9780 }
9781 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9782err_vsi:
9783 i40e_vsi_clear(vsi);
9784 return NULL;
9785}
9786
9787/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009788 * i40e_vsi_setup - Set up a VSI by a given type
9789 * @pf: board private structure
9790 * @type: VSI type
9791 * @uplink_seid: the switch element to link to
9792 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9793 *
9794 * This allocates the sw VSI structure and its queue resources, then add a VSI
9795 * to the identified VEB.
9796 *
9797 * Returns pointer to the successfully allocated and configure VSI sw struct on
9798 * success, otherwise returns NULL on failure.
9799 **/
9800struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9801 u16 uplink_seid, u32 param1)
9802{
9803 struct i40e_vsi *vsi = NULL;
9804 struct i40e_veb *veb = NULL;
9805 int ret, i;
9806 int v_idx;
9807
9808 /* The requested uplink_seid must be either
9809 * - the PF's port seid
9810 * no VEB is needed because this is the PF
9811 * or this is a Flow Director special case VSI
9812 * - seid of an existing VEB
9813 * - seid of a VSI that owns an existing VEB
9814 * - seid of a VSI that doesn't own a VEB
9815 * a new VEB is created and the VSI becomes the owner
9816 * - seid of the PF VSI, which is what creates the first VEB
9817 * this is a special case of the previous
9818 *
9819 * Find which uplink_seid we were given and create a new VEB if needed
9820 */
9821 for (i = 0; i < I40E_MAX_VEB; i++) {
9822 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9823 veb = pf->veb[i];
9824 break;
9825 }
9826 }
9827
9828 if (!veb && uplink_seid != pf->mac_seid) {
9829
Mitch Williams505682c2014-05-20 08:01:37 +00009830 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009831 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9832 vsi = pf->vsi[i];
9833 break;
9834 }
9835 }
9836 if (!vsi) {
9837 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9838 uplink_seid);
9839 return NULL;
9840 }
9841
9842 if (vsi->uplink_seid == pf->mac_seid)
9843 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9844 vsi->tc_config.enabled_tc);
9845 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9846 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9847 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009848 if (veb) {
9849 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9850 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009851 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009852 return NULL;
9853 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009854 /* We come up by default in VEPA mode if SRIOV is not
9855 * already enabled, in which case we can't force VEPA
9856 * mode.
9857 */
9858 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9859 veb->bridge_mode = BRIDGE_MODE_VEPA;
9860 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9861 }
Neerav Parikh51616012015-02-06 08:52:14 +00009862 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009863 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009864 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9865 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9866 veb = pf->veb[i];
9867 }
9868 if (!veb) {
9869 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9870 return NULL;
9871 }
9872
9873 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9874 uplink_seid = veb->seid;
9875 }
9876
9877 /* get vsi sw struct */
9878 v_idx = i40e_vsi_mem_alloc(pf, type);
9879 if (v_idx < 0)
9880 goto err_alloc;
9881 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009882 if (!vsi)
9883 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009884 vsi->type = type;
9885 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9886
9887 if (type == I40E_VSI_MAIN)
9888 pf->lan_vsi = v_idx;
9889 else if (type == I40E_VSI_SRIOV)
9890 vsi->vf_id = param1;
9891 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009892 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9893 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009894 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009895 dev_info(&pf->pdev->dev,
9896 "failed to get tracking for %d queues for VSI %d err=%d\n",
9897 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009898 goto err_vsi;
9899 }
9900 vsi->base_queue = ret;
9901
9902 /* get a VSI from the hardware */
9903 vsi->uplink_seid = uplink_seid;
9904 ret = i40e_add_vsi(vsi);
9905 if (ret)
9906 goto err_vsi;
9907
9908 switch (vsi->type) {
9909 /* setup the netdev if needed */
9910 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009911 /* Apply relevant filters if a platform-specific mac
9912 * address was selected.
9913 */
9914 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9915 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9916 if (ret) {
9917 dev_warn(&pf->pdev->dev,
9918 "could not set up macaddr; err %d\n",
9919 ret);
9920 }
9921 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009922 case I40E_VSI_VMDQ2:
Vasu Dev38e00432014-08-01 13:27:03 -07009923 case I40E_VSI_FCOE:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009924 ret = i40e_config_netdev(vsi);
9925 if (ret)
9926 goto err_netdev;
9927 ret = register_netdev(vsi->netdev);
9928 if (ret)
9929 goto err_netdev;
9930 vsi->netdev_registered = true;
9931 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009932#ifdef CONFIG_I40E_DCB
9933 /* Setup DCB netlink interface */
9934 i40e_dcbnl_setup(vsi);
9935#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009936 /* fall through */
9937
9938 case I40E_VSI_FDIR:
9939 /* set up vectors and rings if needed */
9940 ret = i40e_vsi_setup_vectors(vsi);
9941 if (ret)
9942 goto err_msix;
9943
9944 ret = i40e_alloc_rings(vsi);
9945 if (ret)
9946 goto err_rings;
9947
9948 /* map all of the rings to the q_vectors */
9949 i40e_vsi_map_rings_to_vectors(vsi);
9950
9951 i40e_vsi_reset_stats(vsi);
9952 break;
9953
9954 default:
9955 /* no netdev or rings for the other VSI types */
9956 break;
9957 }
9958
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04009959 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
9960 (vsi->type == I40E_VSI_VMDQ2)) {
9961 ret = i40e_vsi_config_rss(vsi);
9962 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009963 return vsi;
9964
9965err_rings:
9966 i40e_vsi_free_q_vectors(vsi);
9967err_msix:
9968 if (vsi->netdev_registered) {
9969 vsi->netdev_registered = false;
9970 unregister_netdev(vsi->netdev);
9971 free_netdev(vsi->netdev);
9972 vsi->netdev = NULL;
9973 }
9974err_netdev:
9975 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9976err_vsi:
9977 i40e_vsi_clear(vsi);
9978err_alloc:
9979 return NULL;
9980}
9981
9982/**
9983 * i40e_veb_get_bw_info - Query VEB BW information
9984 * @veb: the veb to query
9985 *
9986 * Query the Tx scheduler BW configuration data for given VEB
9987 **/
9988static int i40e_veb_get_bw_info(struct i40e_veb *veb)
9989{
9990 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
9991 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
9992 struct i40e_pf *pf = veb->pf;
9993 struct i40e_hw *hw = &pf->hw;
9994 u32 tc_bw_max;
9995 int ret = 0;
9996 int i;
9997
9998 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
9999 &bw_data, NULL);
10000 if (ret) {
10001 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010002 "query veb bw config failed, err %s aq_err %s\n",
10003 i40e_stat_str(&pf->hw, ret),
10004 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010005 goto out;
10006 }
10007
10008 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10009 &ets_data, NULL);
10010 if (ret) {
10011 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010012 "query veb bw ets config failed, err %s aq_err %s\n",
10013 i40e_stat_str(&pf->hw, ret),
10014 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010015 goto out;
10016 }
10017
10018 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10019 veb->bw_max_quanta = ets_data.tc_bw_max;
10020 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010021 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010022 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10023 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10024 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10025 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10026 veb->bw_tc_limit_credits[i] =
10027 le16_to_cpu(bw_data.tc_bw_limits[i]);
10028 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10029 }
10030
10031out:
10032 return ret;
10033}
10034
10035/**
10036 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10037 * @pf: board private structure
10038 *
10039 * On error: returns error code (negative)
10040 * On success: returns vsi index in PF (positive)
10041 **/
10042static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10043{
10044 int ret = -ENOENT;
10045 struct i40e_veb *veb;
10046 int i;
10047
10048 /* Need to protect the allocation of switch elements at the PF level */
10049 mutex_lock(&pf->switch_mutex);
10050
10051 /* VEB list may be fragmented if VEB creation/destruction has
10052 * been happening. We can afford to do a quick scan to look
10053 * for any free slots in the list.
10054 *
10055 * find next empty veb slot, looping back around if necessary
10056 */
10057 i = 0;
10058 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10059 i++;
10060 if (i >= I40E_MAX_VEB) {
10061 ret = -ENOMEM;
10062 goto err_alloc_veb; /* out of VEB slots! */
10063 }
10064
10065 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10066 if (!veb) {
10067 ret = -ENOMEM;
10068 goto err_alloc_veb;
10069 }
10070 veb->pf = pf;
10071 veb->idx = i;
10072 veb->enabled_tc = 1;
10073
10074 pf->veb[i] = veb;
10075 ret = i;
10076err_alloc_veb:
10077 mutex_unlock(&pf->switch_mutex);
10078 return ret;
10079}
10080
10081/**
10082 * i40e_switch_branch_release - Delete a branch of the switch tree
10083 * @branch: where to start deleting
10084 *
10085 * This uses recursion to find the tips of the branch to be
10086 * removed, deleting until we get back to and can delete this VEB.
10087 **/
10088static void i40e_switch_branch_release(struct i40e_veb *branch)
10089{
10090 struct i40e_pf *pf = branch->pf;
10091 u16 branch_seid = branch->seid;
10092 u16 veb_idx = branch->idx;
10093 int i;
10094
10095 /* release any VEBs on this VEB - RECURSION */
10096 for (i = 0; i < I40E_MAX_VEB; i++) {
10097 if (!pf->veb[i])
10098 continue;
10099 if (pf->veb[i]->uplink_seid == branch->seid)
10100 i40e_switch_branch_release(pf->veb[i]);
10101 }
10102
10103 /* Release the VSIs on this VEB, but not the owner VSI.
10104 *
10105 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10106 * the VEB itself, so don't use (*branch) after this loop.
10107 */
Mitch Williams505682c2014-05-20 08:01:37 +000010108 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010109 if (!pf->vsi[i])
10110 continue;
10111 if (pf->vsi[i]->uplink_seid == branch_seid &&
10112 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10113 i40e_vsi_release(pf->vsi[i]);
10114 }
10115 }
10116
10117 /* There's one corner case where the VEB might not have been
10118 * removed, so double check it here and remove it if needed.
10119 * This case happens if the veb was created from the debugfs
10120 * commands and no VSIs were added to it.
10121 */
10122 if (pf->veb[veb_idx])
10123 i40e_veb_release(pf->veb[veb_idx]);
10124}
10125
10126/**
10127 * i40e_veb_clear - remove veb struct
10128 * @veb: the veb to remove
10129 **/
10130static void i40e_veb_clear(struct i40e_veb *veb)
10131{
10132 if (!veb)
10133 return;
10134
10135 if (veb->pf) {
10136 struct i40e_pf *pf = veb->pf;
10137
10138 mutex_lock(&pf->switch_mutex);
10139 if (pf->veb[veb->idx] == veb)
10140 pf->veb[veb->idx] = NULL;
10141 mutex_unlock(&pf->switch_mutex);
10142 }
10143
10144 kfree(veb);
10145}
10146
10147/**
10148 * i40e_veb_release - Delete a VEB and free its resources
10149 * @veb: the VEB being removed
10150 **/
10151void i40e_veb_release(struct i40e_veb *veb)
10152{
10153 struct i40e_vsi *vsi = NULL;
10154 struct i40e_pf *pf;
10155 int i, n = 0;
10156
10157 pf = veb->pf;
10158
10159 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010160 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010161 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10162 n++;
10163 vsi = pf->vsi[i];
10164 }
10165 }
10166 if (n != 1) {
10167 dev_info(&pf->pdev->dev,
10168 "can't remove VEB %d with %d VSIs left\n",
10169 veb->seid, n);
10170 return;
10171 }
10172
10173 /* move the remaining VSI to uplink veb */
10174 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10175 if (veb->uplink_seid) {
10176 vsi->uplink_seid = veb->uplink_seid;
10177 if (veb->uplink_seid == pf->mac_seid)
10178 vsi->veb_idx = I40E_NO_VEB;
10179 else
10180 vsi->veb_idx = veb->veb_idx;
10181 } else {
10182 /* floating VEB */
10183 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10184 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10185 }
10186
10187 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10188 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010189}
10190
10191/**
10192 * i40e_add_veb - create the VEB in the switch
10193 * @veb: the VEB to be instantiated
10194 * @vsi: the controlling VSI
10195 **/
10196static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10197{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010198 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010199 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010200 int ret;
10201
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010202 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010203 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010204 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010205
10206 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010207 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010208 dev_info(&pf->pdev->dev,
10209 "couldn't add VEB, err %s aq_err %s\n",
10210 i40e_stat_str(&pf->hw, ret),
10211 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010212 return -EPERM;
10213 }
10214
10215 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010216 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010217 &veb->stats_idx, NULL, NULL, NULL);
10218 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010219 dev_info(&pf->pdev->dev,
10220 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10221 i40e_stat_str(&pf->hw, ret),
10222 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010223 return -EPERM;
10224 }
10225 ret = i40e_veb_get_bw_info(veb);
10226 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010227 dev_info(&pf->pdev->dev,
10228 "couldn't get VEB bw info, err %s aq_err %s\n",
10229 i40e_stat_str(&pf->hw, ret),
10230 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10231 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010232 return -ENOENT;
10233 }
10234
10235 vsi->uplink_seid = veb->seid;
10236 vsi->veb_idx = veb->idx;
10237 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10238
10239 return 0;
10240}
10241
10242/**
10243 * i40e_veb_setup - Set up a VEB
10244 * @pf: board private structure
10245 * @flags: VEB setup flags
10246 * @uplink_seid: the switch element to link to
10247 * @vsi_seid: the initial VSI seid
10248 * @enabled_tc: Enabled TC bit-map
10249 *
10250 * This allocates the sw VEB structure and links it into the switch
10251 * It is possible and legal for this to be a duplicate of an already
10252 * existing VEB. It is also possible for both uplink and vsi seids
10253 * to be zero, in order to create a floating VEB.
10254 *
10255 * Returns pointer to the successfully allocated VEB sw struct on
10256 * success, otherwise returns NULL on failure.
10257 **/
10258struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10259 u16 uplink_seid, u16 vsi_seid,
10260 u8 enabled_tc)
10261{
10262 struct i40e_veb *veb, *uplink_veb = NULL;
10263 int vsi_idx, veb_idx;
10264 int ret;
10265
10266 /* if one seid is 0, the other must be 0 to create a floating relay */
10267 if ((uplink_seid == 0 || vsi_seid == 0) &&
10268 (uplink_seid + vsi_seid != 0)) {
10269 dev_info(&pf->pdev->dev,
10270 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10271 uplink_seid, vsi_seid);
10272 return NULL;
10273 }
10274
10275 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010276 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010277 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10278 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010279 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010280 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10281 vsi_seid);
10282 return NULL;
10283 }
10284
10285 if (uplink_seid && uplink_seid != pf->mac_seid) {
10286 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10287 if (pf->veb[veb_idx] &&
10288 pf->veb[veb_idx]->seid == uplink_seid) {
10289 uplink_veb = pf->veb[veb_idx];
10290 break;
10291 }
10292 }
10293 if (!uplink_veb) {
10294 dev_info(&pf->pdev->dev,
10295 "uplink seid %d not found\n", uplink_seid);
10296 return NULL;
10297 }
10298 }
10299
10300 /* get veb sw struct */
10301 veb_idx = i40e_veb_mem_alloc(pf);
10302 if (veb_idx < 0)
10303 goto err_alloc;
10304 veb = pf->veb[veb_idx];
10305 veb->flags = flags;
10306 veb->uplink_seid = uplink_seid;
10307 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10308 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10309
10310 /* create the VEB in the switch */
10311 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10312 if (ret)
10313 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010314 if (vsi_idx == pf->lan_vsi)
10315 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010316
10317 return veb;
10318
10319err_veb:
10320 i40e_veb_clear(veb);
10321err_alloc:
10322 return NULL;
10323}
10324
10325/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010326 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010327 * @pf: board private structure
10328 * @ele: element we are building info from
10329 * @num_reported: total number of elements
10330 * @printconfig: should we print the contents
10331 *
10332 * helper function to assist in extracting a few useful SEID values.
10333 **/
10334static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10335 struct i40e_aqc_switch_config_element_resp *ele,
10336 u16 num_reported, bool printconfig)
10337{
10338 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10339 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10340 u8 element_type = ele->element_type;
10341 u16 seid = le16_to_cpu(ele->seid);
10342
10343 if (printconfig)
10344 dev_info(&pf->pdev->dev,
10345 "type=%d seid=%d uplink=%d downlink=%d\n",
10346 element_type, seid, uplink_seid, downlink_seid);
10347
10348 switch (element_type) {
10349 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10350 pf->mac_seid = seid;
10351 break;
10352 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10353 /* Main VEB? */
10354 if (uplink_seid != pf->mac_seid)
10355 break;
10356 if (pf->lan_veb == I40E_NO_VEB) {
10357 int v;
10358
10359 /* find existing or else empty VEB */
10360 for (v = 0; v < I40E_MAX_VEB; v++) {
10361 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10362 pf->lan_veb = v;
10363 break;
10364 }
10365 }
10366 if (pf->lan_veb == I40E_NO_VEB) {
10367 v = i40e_veb_mem_alloc(pf);
10368 if (v < 0)
10369 break;
10370 pf->lan_veb = v;
10371 }
10372 }
10373
10374 pf->veb[pf->lan_veb]->seid = seid;
10375 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10376 pf->veb[pf->lan_veb]->pf = pf;
10377 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10378 break;
10379 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10380 if (num_reported != 1)
10381 break;
10382 /* This is immediately after a reset so we can assume this is
10383 * the PF's VSI
10384 */
10385 pf->mac_seid = uplink_seid;
10386 pf->pf_seid = downlink_seid;
10387 pf->main_vsi_seid = seid;
10388 if (printconfig)
10389 dev_info(&pf->pdev->dev,
10390 "pf_seid=%d main_vsi_seid=%d\n",
10391 pf->pf_seid, pf->main_vsi_seid);
10392 break;
10393 case I40E_SWITCH_ELEMENT_TYPE_PF:
10394 case I40E_SWITCH_ELEMENT_TYPE_VF:
10395 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10396 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10397 case I40E_SWITCH_ELEMENT_TYPE_PE:
10398 case I40E_SWITCH_ELEMENT_TYPE_PA:
10399 /* ignore these for now */
10400 break;
10401 default:
10402 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10403 element_type, seid);
10404 break;
10405 }
10406}
10407
10408/**
10409 * i40e_fetch_switch_configuration - Get switch config from firmware
10410 * @pf: board private structure
10411 * @printconfig: should we print the contents
10412 *
10413 * Get the current switch configuration from the device and
10414 * extract a few useful SEID values.
10415 **/
10416int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10417{
10418 struct i40e_aqc_get_switch_config_resp *sw_config;
10419 u16 next_seid = 0;
10420 int ret = 0;
10421 u8 *aq_buf;
10422 int i;
10423
10424 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10425 if (!aq_buf)
10426 return -ENOMEM;
10427
10428 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10429 do {
10430 u16 num_reported, num_total;
10431
10432 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10433 I40E_AQ_LARGE_BUF,
10434 &next_seid, NULL);
10435 if (ret) {
10436 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010437 "get switch config failed err %s aq_err %s\n",
10438 i40e_stat_str(&pf->hw, ret),
10439 i40e_aq_str(&pf->hw,
10440 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010441 kfree(aq_buf);
10442 return -ENOENT;
10443 }
10444
10445 num_reported = le16_to_cpu(sw_config->header.num_reported);
10446 num_total = le16_to_cpu(sw_config->header.num_total);
10447
10448 if (printconfig)
10449 dev_info(&pf->pdev->dev,
10450 "header: %d reported %d total\n",
10451 num_reported, num_total);
10452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010453 for (i = 0; i < num_reported; i++) {
10454 struct i40e_aqc_switch_config_element_resp *ele =
10455 &sw_config->element[i];
10456
10457 i40e_setup_pf_switch_element(pf, ele, num_reported,
10458 printconfig);
10459 }
10460 } while (next_seid != 0);
10461
10462 kfree(aq_buf);
10463 return ret;
10464}
10465
10466/**
10467 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10468 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010469 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010470 *
10471 * Returns 0 on success, negative value on failure
10472 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010473static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010474{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010475 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010476 int ret;
10477
10478 /* find out what's out there already */
10479 ret = i40e_fetch_switch_configuration(pf, false);
10480 if (ret) {
10481 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010482 "couldn't fetch switch config, err %s aq_err %s\n",
10483 i40e_stat_str(&pf->hw, ret),
10484 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010485 return ret;
10486 }
10487 i40e_pf_reset_stats(pf);
10488
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010489 /* set the switch config bit for the whole device to
10490 * support limited promisc or true promisc
10491 * when user requests promisc. The default is limited
10492 * promisc.
10493 */
10494
10495 if ((pf->hw.pf_id == 0) &&
10496 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10497 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10498
10499 if (pf->hw.pf_id == 0) {
10500 u16 valid_flags;
10501
10502 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10503 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10504 NULL);
10505 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10506 dev_info(&pf->pdev->dev,
10507 "couldn't set switch config bits, err %s aq_err %s\n",
10508 i40e_stat_str(&pf->hw, ret),
10509 i40e_aq_str(&pf->hw,
10510 pf->hw.aq.asq_last_status));
10511 /* not a fatal problem, just keep going */
10512 }
10513 }
10514
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010515 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010516 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010517 struct i40e_vsi *vsi = NULL;
10518 u16 uplink_seid;
10519
10520 /* Set up the PF VSI associated with the PF's main VSI
10521 * that is already in the HW switch
10522 */
10523 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10524 uplink_seid = pf->veb[pf->lan_veb]->seid;
10525 else
10526 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010527 if (pf->lan_vsi == I40E_NO_VSI)
10528 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10529 else if (reinit)
10530 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010531 if (!vsi) {
10532 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10533 i40e_fdir_teardown(pf);
10534 return -EAGAIN;
10535 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010536 } else {
10537 /* force a reset of TC and queue layout configurations */
10538 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010539
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010540 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10541 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10542 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10543 }
10544 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10545
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010546 i40e_fdir_sb_setup(pf);
10547
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010548 /* Setup static PF queue filter control settings */
10549 ret = i40e_setup_pf_filter_control(pf);
10550 if (ret) {
10551 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10552 ret);
10553 /* Failure here should not stop continuing other steps */
10554 }
10555
10556 /* enable RSS in the HW, even for only one queue, as the stack can use
10557 * the hash
10558 */
10559 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010560 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010561
10562 /* fill in link information and enable LSE reporting */
Catherine Sullivan0a862b42015-08-31 19:54:53 -040010563 i40e_update_link_info(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010564 i40e_link_event(pf);
10565
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010566 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010567 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10568 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010569
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010570 i40e_ptp_init(pf);
10571
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010572 return ret;
10573}
10574
10575/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010576 * i40e_determine_queue_usage - Work out queue distribution
10577 * @pf: board private structure
10578 **/
10579static void i40e_determine_queue_usage(struct i40e_pf *pf)
10580{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010581 int queues_left;
10582
10583 pf->num_lan_qps = 0;
Vasu Dev38e00432014-08-01 13:27:03 -070010584#ifdef I40E_FCOE
10585 pf->num_fcoe_qps = 0;
10586#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010587
10588 /* Find the max queues to be put into basic use. We'll always be
10589 * using TC0, whether or not DCB is running, and TC0 will get the
10590 * big RSS set.
10591 */
10592 queues_left = pf->hw.func_caps.num_tx_qp;
10593
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010594 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010595 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010596 /* one qp for PF, no queues for anything else */
10597 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010598 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010599
10600 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010601 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010602 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010603#ifdef I40E_FCOE
10604 I40E_FLAG_FCOE_ENABLED |
10605#endif
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010606 I40E_FLAG_FD_SB_ENABLED |
10607 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010608 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010609 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010610 I40E_FLAG_SRIOV_ENABLED |
10611 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010612 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10613 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010614 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010615 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010616 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010617 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010618 queues_left -= pf->num_lan_qps;
10619
10620 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010621 I40E_FLAG_IWARP_ENABLED |
Vasu Dev38e00432014-08-01 13:27:03 -070010622#ifdef I40E_FCOE
10623 I40E_FLAG_FCOE_ENABLED |
10624#endif
Frank Zhang9aa7e932014-05-20 08:01:42 +000010625 I40E_FLAG_FD_SB_ENABLED |
10626 I40E_FLAG_FD_ATR_ENABLED |
10627 I40E_FLAG_DCB_ENABLED |
10628 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010629 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010630 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010631 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010632 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010633 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10634 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010635 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10636 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010637 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10638 num_online_cpus());
10639 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10640 pf->hw.func_caps.num_tx_qp);
10641
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010642 queues_left -= pf->num_lan_qps;
10643 }
10644
Vasu Dev38e00432014-08-01 13:27:03 -070010645#ifdef I40E_FCOE
10646 if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
10647 if (I40E_DEFAULT_FCOE <= queues_left) {
10648 pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
10649 } else if (I40E_MINIMUM_FCOE <= queues_left) {
10650 pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
10651 } else {
10652 pf->num_fcoe_qps = 0;
10653 pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
10654 dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
10655 }
10656
10657 queues_left -= pf->num_fcoe_qps;
10658 }
10659
10660#endif
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010661 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10662 if (queues_left > 1) {
10663 queues_left -= 1; /* save 1 queue for FD */
10664 } else {
10665 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10666 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10667 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010668 }
10669
10670 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10671 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010672 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10673 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010674 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10675 }
10676
10677 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10678 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10679 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10680 (queues_left / pf->num_vmdq_qps));
10681 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10682 }
10683
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010684 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010685 dev_dbg(&pf->pdev->dev,
10686 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10687 pf->hw.func_caps.num_tx_qp,
10688 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010689 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10690 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10691 queues_left);
Vasu Dev38e00432014-08-01 13:27:03 -070010692#ifdef I40E_FCOE
Neerav Parikh8279e492015-09-03 17:18:50 -040010693 dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
Vasu Dev38e00432014-08-01 13:27:03 -070010694#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010695}
10696
10697/**
10698 * i40e_setup_pf_filter_control - Setup PF static filter control
10699 * @pf: PF to be setup
10700 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010701 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010702 * settings. If PE/FCoE are enabled then it will also set the per PF
10703 * based filter sizes required for them. It also enables Flow director,
10704 * ethertype and macvlan type filter settings for the pf.
10705 *
10706 * Returns 0 on success, negative on failure
10707 **/
10708static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10709{
10710 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10711
10712 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10713
10714 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010715 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010716 settings->enable_fdir = true;
10717
10718 /* Ethtype and MACVLAN filters enabled for PF */
10719 settings->enable_ethtype = true;
10720 settings->enable_macvlan = true;
10721
10722 if (i40e_set_filter_control(&pf->hw, settings))
10723 return -ENOENT;
10724
10725 return 0;
10726}
10727
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010728#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010729#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010730static void i40e_print_features(struct i40e_pf *pf)
10731{
10732 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010733 char *buf;
10734 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010735
Joe Perches3b195842015-12-03 04:20:57 -080010736 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10737 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010738 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010739
Joe Perches3b195842015-12-03 04:20:57 -080010740 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010741#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010742 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010743#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010744 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010745 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010746 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010747 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010748 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010749 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010750 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010751 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010752 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10753 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010754 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010755 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010756 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010757 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010758 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010759 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010760 i += snprintf(&buf[i], REMAIN(i), " PTP");
Vasu Dev38e00432014-08-01 13:27:03 -070010761#ifdef I40E_FCOE
10762 if (pf->flags & I40E_FLAG_FCOE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010763 i += snprintf(&buf[i], REMAIN(i), " FCOE");
Vasu Dev38e00432014-08-01 13:27:03 -070010764#endif
Shannon Nelson6dec1012015-09-28 14:12:30 -040010765 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010766 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010767 else
Joe Perches3b195842015-12-03 04:20:57 -080010768 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010769
Joe Perches3b195842015-12-03 04:20:57 -080010770 dev_info(&pf->pdev->dev, "%s\n", buf);
10771 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010772 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010773}
10774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010775/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010776 * i40e_get_platform_mac_addr - get platform-specific MAC address
10777 *
10778 * @pdev: PCI device information struct
10779 * @pf: board private structure
10780 *
10781 * Look up the MAC address in Open Firmware on systems that support it,
10782 * and use IDPROM on SPARC if no OF address is found. On return, the
10783 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10784 * has been selected.
10785 **/
10786static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10787{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010788 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010789 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010790 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010791}
10792
10793/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010794 * i40e_probe - Device initialization routine
10795 * @pdev: PCI device information struct
10796 * @ent: entry in i40e_pci_tbl
10797 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010798 * i40e_probe initializes a PF identified by a pci_dev structure.
10799 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010800 * and a hardware reset occur.
10801 *
10802 * Returns 0 on success, negative on failure
10803 **/
10804static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10805{
Catherine Sullivane8278452015-02-06 08:52:08 +000010806 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010807 struct i40e_pf *pf;
10808 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010809 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010810 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010811 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010812 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010813 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010814 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010815 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010816
10817 err = pci_enable_device_mem(pdev);
10818 if (err)
10819 return err;
10820
10821 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010822 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010823 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010824 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10825 if (err) {
10826 dev_err(&pdev->dev,
10827 "DMA configuration failed: 0x%x\n", err);
10828 goto err_dma;
10829 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010830 }
10831
10832 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010833 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010834 if (err) {
10835 dev_info(&pdev->dev,
10836 "pci_request_selected_regions failed %d\n", err);
10837 goto err_pci_reg;
10838 }
10839
10840 pci_enable_pcie_error_reporting(pdev);
10841 pci_set_master(pdev);
10842
10843 /* Now that we have a PCI connection, we need to do the
10844 * low level device setup. This is primarily setting up
10845 * the Admin Queue structures and then querying for the
10846 * device's current profile information.
10847 */
10848 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10849 if (!pf) {
10850 err = -ENOMEM;
10851 goto err_pf_alloc;
10852 }
10853 pf->next_vsi = 0;
10854 pf->pdev = pdev;
10855 set_bit(__I40E_DOWN, &pf->state);
10856
10857 hw = &pf->hw;
10858 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010859
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010860 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10861 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010862
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010863 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010864 if (!hw->hw_addr) {
10865 err = -EIO;
10866 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10867 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010868 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010869 goto err_ioremap;
10870 }
10871 hw->vendor_id = pdev->vendor;
10872 hw->device_id = pdev->device;
10873 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10874 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10875 hw->subsystem_device_id = pdev->subsystem_device;
10876 hw->bus.device = PCI_SLOT(pdev->devfn);
10877 hw->bus.func = PCI_FUNC(pdev->devfn);
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010878 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010879
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010880 /* set up the locks for the AQ, do this only once in probe
10881 * and destroy them only once in remove
10882 */
10883 mutex_init(&hw->aq.asq_mutex);
10884 mutex_init(&hw->aq.arq_mutex);
10885
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010886 pf->msg_enable = netif_msg_init(debug,
10887 NETIF_MSG_DRV |
10888 NETIF_MSG_PROBE |
10889 NETIF_MSG_LINK);
10890 if (debug < -1)
10891 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010892
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010893 /* do a special CORER for clearing PXE mode once at init */
10894 if (hw->revision_id == 0 &&
10895 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10896 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10897 i40e_flush(hw);
10898 msleep(200);
10899 pf->corer_count++;
10900
10901 i40e_clear_pxe_mode(hw);
10902 }
10903
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010904 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010905 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010906 err = i40e_pf_reset(hw);
10907 if (err) {
10908 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10909 goto err_pf_reset;
10910 }
10911 pf->pfr_count++;
10912
10913 hw->aq.num_arq_entries = I40E_AQ_LEN;
10914 hw->aq.num_asq_entries = I40E_AQ_LEN;
10915 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10916 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10917 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010918
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010919 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010920 "%s-%s:misc",
10921 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010922
10923 err = i40e_init_shared_code(hw);
10924 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010925 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10926 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010927 goto err_pf_reset;
10928 }
10929
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010930 /* set up a default setting for link flow control */
10931 pf->hw.fc.requested_mode = I40E_FC_NONE;
10932
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010933 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010934 if (err) {
10935 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10936 dev_info(&pdev->dev,
10937 "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");
10938 else
10939 dev_info(&pdev->dev,
10940 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10941
10942 goto err_pf_reset;
10943 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010944
Shannon Nelson6dec1012015-09-28 14:12:30 -040010945 /* provide nvm, fw, api versions */
10946 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10947 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10948 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10949 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010950
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010951 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10952 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010953 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010954 "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");
10955 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10956 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010957 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010958 "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 +000010959
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010960 i40e_verify_eeprom(pf);
10961
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010962 /* Rev 0 hardware was never productized */
10963 if (hw->revision_id < 1)
10964 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");
10965
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010966 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010967 err = i40e_get_capabilities(pf);
10968 if (err)
10969 goto err_adminq_setup;
10970
10971 err = i40e_sw_init(pf);
10972 if (err) {
10973 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
10974 goto err_sw_init;
10975 }
10976
10977 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10978 hw->func_caps.num_rx_qp,
10979 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
10980 if (err) {
10981 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
10982 goto err_init_lan_hmc;
10983 }
10984
10985 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10986 if (err) {
10987 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
10988 err = -ENOENT;
10989 goto err_configure_lan_hmc;
10990 }
10991
Neerav Parikhb686ece2014-12-14 01:55:11 +000010992 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
10993 * Ignore error return codes because if it was already disabled via
10994 * hardware settings this will fail
10995 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080010996 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000010997 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
10998 i40e_aq_stop_lldp(hw, true, NULL);
10999 }
11000
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011001 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011002 /* allow a platform config to override the HW addr */
11003 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011004 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011005 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11006 err = -EIO;
11007 goto err_mac_addr;
11008 }
11009 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011010 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011011 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11012 if (is_valid_ether_addr(hw->mac.port_addr))
11013 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Vasu Dev38e00432014-08-01 13:27:03 -070011014#ifdef I40E_FCOE
11015 err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
11016 if (err)
11017 dev_info(&pdev->dev,
11018 "(non-fatal) SAN MAC retrieval failed: %d\n", err);
11019 if (!is_valid_ether_addr(hw->mac.san_addr)) {
11020 dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
11021 hw->mac.san_addr);
11022 ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
11023 }
11024 dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
11025#endif /* I40E_FCOE */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011026
11027 pci_set_drvdata(pdev, pf);
11028 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011029#ifdef CONFIG_I40E_DCB
11030 err = i40e_init_pf_dcb(pf);
11031 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011032 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011033 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011034 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011035 }
11036#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011037
11038 /* set up periodic task facility */
11039 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11040 pf->service_timer_period = HZ;
11041
11042 INIT_WORK(&pf->service_task, i40e_service_task);
11043 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11044 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011045
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011046 /* NVM bit on means WoL disabled for the port */
11047 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011048 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011049 pf->wol_en = false;
11050 else
11051 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011052 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11053
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011054 /* set up the main switch operations */
11055 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011056 err = i40e_init_interrupt_scheme(pf);
11057 if (err)
11058 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011059
Mitch Williams505682c2014-05-20 08:01:37 +000011060 /* The number of VSIs reported by the FW is the minimum guaranteed
11061 * to us; HW supports far more and we share the remaining pool with
11062 * the other PFs. We allocate space for more than the guarantee with
11063 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011064 */
Mitch Williams505682c2014-05-20 08:01:37 +000011065 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11066 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11067 else
11068 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11069
11070 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011071 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11072 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011073 if (!pf->vsi) {
11074 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011075 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011076 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011077
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011078#ifdef CONFIG_PCI_IOV
11079 /* prep for VF support */
11080 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11081 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11082 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11083 if (pci_num_vf(pdev))
11084 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11085 }
11086#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011087 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011088 if (err) {
11089 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11090 goto err_vsis;
11091 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011092
11093 /* Make sure flow control is set according to current settings */
11094 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11095 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11096 dev_dbg(&pf->pdev->dev,
11097 "Set fc with err %s aq_err %s on get_phy_cap\n",
11098 i40e_stat_str(hw, err),
11099 i40e_aq_str(hw, hw->aq.asq_last_status));
11100 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11101 dev_dbg(&pf->pdev->dev,
11102 "Set fc with err %s aq_err %s on set_phy_config\n",
11103 i40e_stat_str(hw, err),
11104 i40e_aq_str(hw, hw->aq.asq_last_status));
11105 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11106 dev_dbg(&pf->pdev->dev,
11107 "Set fc with err %s aq_err %s on get_link_info\n",
11108 i40e_stat_str(hw, err),
11109 i40e_aq_str(hw, hw->aq.asq_last_status));
11110
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011111 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011112 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011113 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11114 i40e_vsi_open(pf->vsi[i]);
11115 break;
11116 }
11117 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011118
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011119 /* The driver only wants link up/down and module qualification
11120 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011121 */
11122 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011123 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011124 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011125 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011126 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011127 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11128 i40e_stat_str(&pf->hw, err),
11129 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011130
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011131 /* Reconfigure hardware for allowing smaller MSS in the case
11132 * of TSO, so that we avoid the MDD being fired and causing
11133 * a reset in the case of small MSS+TSO.
11134 */
11135 val = rd32(hw, I40E_REG_MSS);
11136 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11137 val &= ~I40E_REG_MSS_MIN_MASK;
11138 val |= I40E_64BYTE_MSS;
11139 wr32(hw, I40E_REG_MSS, val);
11140 }
11141
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011142 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011143 msleep(75);
11144 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11145 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011146 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11147 i40e_stat_str(&pf->hw, err),
11148 i40e_aq_str(&pf->hw,
11149 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011150 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011151 /* The main driver is (mostly) up and happy. We need to set this state
11152 * before setting up the misc vector or we get a race and the vector
11153 * ends up disabled forever.
11154 */
11155 clear_bit(__I40E_DOWN, &pf->state);
11156
11157 /* In case of MSIX we are going to setup the misc vector right here
11158 * to handle admin queue events etc. In case of legacy and MSI
11159 * the misc functionality and queue processing is combined in
11160 * the same vector and that gets setup at open.
11161 */
11162 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11163 err = i40e_setup_misc_vector(pf);
11164 if (err) {
11165 dev_info(&pdev->dev,
11166 "setup of misc vector failed: %d\n", err);
11167 goto err_vsis;
11168 }
11169 }
11170
Greg Rosedf805f62014-04-04 04:43:16 +000011171#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011172 /* prep for VF support */
11173 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011174 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11175 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011176 /* disable link interrupts for VFs */
11177 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11178 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11179 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11180 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011181
11182 if (pci_num_vf(pdev)) {
11183 dev_info(&pdev->dev,
11184 "Active VFs found, allocating resources.\n");
11185 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11186 if (err)
11187 dev_info(&pdev->dev,
11188 "Error %d allocating resources for existing VFs\n",
11189 err);
11190 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011191 }
Greg Rosedf805f62014-04-04 04:43:16 +000011192#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011193
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011194 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11195 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11196 pf->num_iwarp_msix,
11197 I40E_IWARP_IRQ_PILE_ID);
11198 if (pf->iwarp_base_vector < 0) {
11199 dev_info(&pdev->dev,
11200 "failed to get tracking for %d vectors for IWARP err=%d\n",
11201 pf->num_iwarp_msix, pf->iwarp_base_vector);
11202 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11203 }
11204 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011205
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011206 i40e_dbg_pf_init(pf);
11207
11208 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011209 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011210
11211 /* since everything's happy, start the service_task timer */
11212 mod_timer(&pf->service_timer,
11213 round_jiffies(jiffies + pf->service_timer_period));
11214
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011215 /* add this PF to client device list and launch a client service task */
11216 err = i40e_lan_add_device(pf);
11217 if (err)
11218 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11219 err);
11220
Vasu Dev38e00432014-08-01 13:27:03 -070011221#ifdef I40E_FCOE
11222 /* create FCoE interface */
11223 i40e_fcoe_vsi_setup(pf);
11224
11225#endif
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011226#define PCI_SPEED_SIZE 8
11227#define PCI_WIDTH_SIZE 8
11228 /* Devices on the IOSF bus do not have this information
11229 * and will report PCI Gen 1 x 1 by default so don't bother
11230 * checking them.
11231 */
11232 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11233 char speed[PCI_SPEED_SIZE] = "Unknown";
11234 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011235
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011236 /* Get the negotiated link width and speed from PCI config
11237 * space
11238 */
11239 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11240 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011241
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011242 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011243
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011244 switch (hw->bus.speed) {
11245 case i40e_bus_speed_8000:
11246 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11247 case i40e_bus_speed_5000:
11248 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11249 case i40e_bus_speed_2500:
11250 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11251 default:
11252 break;
11253 }
11254 switch (hw->bus.width) {
11255 case i40e_bus_width_pcie_x8:
11256 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11257 case i40e_bus_width_pcie_x4:
11258 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11259 case i40e_bus_width_pcie_x2:
11260 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11261 case i40e_bus_width_pcie_x1:
11262 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11263 default:
11264 break;
11265 }
11266
11267 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11268 speed, width);
11269
11270 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11271 hw->bus.speed < i40e_bus_speed_8000) {
11272 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11273 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11274 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011275 }
11276
Catherine Sullivane8278452015-02-06 08:52:08 +000011277 /* get the requested speeds from the fw */
11278 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11279 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011280 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11281 i40e_stat_str(&pf->hw, err),
11282 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011283 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11284
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011285 /* get the supported phy types from the fw */
11286 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11287 if (err)
11288 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11289 i40e_stat_str(&pf->hw, err),
11290 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11291 pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
11292
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011293 /* Add a filter to drop all Flow control frames from any VSI from being
11294 * transmitted. By doing so we stop a malicious VF from sending out
11295 * PAUSE or PFC frames and potentially controlling traffic for other
11296 * PF/VF VSIs.
11297 * The FW can still send Flow control frames if enabled.
11298 */
11299 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11300 pf->main_vsi_seid);
11301
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011302 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
11303 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11304 pf->flags |= I40E_FLAG_HAVE_10GBASET_PHY;
11305
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011306 /* print a string summarizing features */
11307 i40e_print_features(pf);
11308
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011309 return 0;
11310
11311 /* Unwind what we've done if something failed in the setup */
11312err_vsis:
11313 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011314 i40e_clear_interrupt_scheme(pf);
11315 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011316err_switch_setup:
11317 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011318 del_timer_sync(&pf->service_timer);
11319err_mac_addr:
11320err_configure_lan_hmc:
11321 (void)i40e_shutdown_lan_hmc(hw);
11322err_init_lan_hmc:
11323 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011324err_sw_init:
11325err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011326err_pf_reset:
11327 iounmap(hw->hw_addr);
11328err_ioremap:
11329 kfree(pf);
11330err_pf_alloc:
11331 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011332 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011333err_pci_reg:
11334err_dma:
11335 pci_disable_device(pdev);
11336 return err;
11337}
11338
11339/**
11340 * i40e_remove - Device removal routine
11341 * @pdev: PCI device information struct
11342 *
11343 * i40e_remove is called by the PCI subsystem to alert the driver
11344 * that is should release a PCI device. This could be caused by a
11345 * Hot-Plug event, or because the driver is going to be removed from
11346 * memory.
11347 **/
11348static void i40e_remove(struct pci_dev *pdev)
11349{
11350 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011351 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011352 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011353 int i;
11354
11355 i40e_dbg_pf_exit(pf);
11356
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011357 i40e_ptp_stop(pf);
11358
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011359 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011360 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11361 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011362
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011363 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011364 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011365 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011366 if (pf->service_timer.data)
11367 del_timer_sync(&pf->service_timer);
11368 if (pf->service_task.func)
11369 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011370
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011371 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11372 i40e_free_vfs(pf);
11373 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11374 }
11375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011376 i40e_fdir_teardown(pf);
11377
11378 /* If there is a switch structure or any orphans, remove them.
11379 * This will leave only the PF's VSI remaining.
11380 */
11381 for (i = 0; i < I40E_MAX_VEB; i++) {
11382 if (!pf->veb[i])
11383 continue;
11384
11385 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11386 pf->veb[i]->uplink_seid == 0)
11387 i40e_switch_branch_release(pf->veb[i]);
11388 }
11389
11390 /* Now we can shutdown the PF's VSI, just before we kill
11391 * adminq and hmc.
11392 */
11393 if (pf->vsi[pf->lan_vsi])
11394 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11395
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011396 /* remove attached clients */
11397 ret_code = i40e_lan_del_device(pf);
11398 if (ret_code) {
11399 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11400 ret_code);
11401 }
11402
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011403 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011404 if (hw->hmc.hmc_obj) {
11405 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011406 if (ret_code)
11407 dev_warn(&pdev->dev,
11408 "Failed to destroy the HMC resources: %d\n",
11409 ret_code);
11410 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011411
11412 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011413 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011414
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011415 /* destroy the locks only once, here */
11416 mutex_destroy(&hw->aq.arq_mutex);
11417 mutex_destroy(&hw->aq.asq_mutex);
11418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011419 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11420 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011421 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011422 if (pf->vsi[i]) {
11423 i40e_vsi_clear_rings(pf->vsi[i]);
11424 i40e_vsi_clear(pf->vsi[i]);
11425 pf->vsi[i] = NULL;
11426 }
11427 }
11428
11429 for (i = 0; i < I40E_MAX_VEB; i++) {
11430 kfree(pf->veb[i]);
11431 pf->veb[i] = NULL;
11432 }
11433
11434 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011435 kfree(pf->vsi);
11436
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011437 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011438 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011439 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011440
11441 pci_disable_pcie_error_reporting(pdev);
11442 pci_disable_device(pdev);
11443}
11444
11445/**
11446 * i40e_pci_error_detected - warning that something funky happened in PCI land
11447 * @pdev: PCI device information struct
11448 *
11449 * Called to warn that something happened and the error handling steps
11450 * are in progress. Allows the driver to quiesce things, be ready for
11451 * remediation.
11452 **/
11453static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11454 enum pci_channel_state error)
11455{
11456 struct i40e_pf *pf = pci_get_drvdata(pdev);
11457
11458 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11459
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011460 if (!pf) {
11461 dev_info(&pdev->dev,
11462 "Cannot recover - error happened during device probe\n");
11463 return PCI_ERS_RESULT_DISCONNECT;
11464 }
11465
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011466 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011467 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11468 rtnl_lock();
11469 i40e_prep_for_reset(pf);
11470 rtnl_unlock();
11471 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011472
11473 /* Request a slot reset */
11474 return PCI_ERS_RESULT_NEED_RESET;
11475}
11476
11477/**
11478 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11479 * @pdev: PCI device information struct
11480 *
11481 * Called to find if the driver can work with the device now that
11482 * the pci slot has been reset. If a basic connection seems good
11483 * (registers are readable and have sane content) then return a
11484 * happy little PCI_ERS_RESULT_xxx.
11485 **/
11486static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11487{
11488 struct i40e_pf *pf = pci_get_drvdata(pdev);
11489 pci_ers_result_t result;
11490 int err;
11491 u32 reg;
11492
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011493 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011494 if (pci_enable_device_mem(pdev)) {
11495 dev_info(&pdev->dev,
11496 "Cannot re-enable PCI device after reset.\n");
11497 result = PCI_ERS_RESULT_DISCONNECT;
11498 } else {
11499 pci_set_master(pdev);
11500 pci_restore_state(pdev);
11501 pci_save_state(pdev);
11502 pci_wake_from_d3(pdev, false);
11503
11504 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11505 if (reg == 0)
11506 result = PCI_ERS_RESULT_RECOVERED;
11507 else
11508 result = PCI_ERS_RESULT_DISCONNECT;
11509 }
11510
11511 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11512 if (err) {
11513 dev_info(&pdev->dev,
11514 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11515 err);
11516 /* non-fatal, continue */
11517 }
11518
11519 return result;
11520}
11521
11522/**
11523 * i40e_pci_error_resume - restart operations after PCI error recovery
11524 * @pdev: PCI device information struct
11525 *
11526 * Called to allow the driver to bring things back up after PCI error
11527 * and/or reset recovery has finished.
11528 **/
11529static void i40e_pci_error_resume(struct pci_dev *pdev)
11530{
11531 struct i40e_pf *pf = pci_get_drvdata(pdev);
11532
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011533 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011534 if (test_bit(__I40E_SUSPENDED, &pf->state))
11535 return;
11536
11537 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011538 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011539 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011540}
11541
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011542/**
11543 * i40e_shutdown - PCI callback for shutting down
11544 * @pdev: PCI device information struct
11545 **/
11546static void i40e_shutdown(struct pci_dev *pdev)
11547{
11548 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011549 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011550
11551 set_bit(__I40E_SUSPENDED, &pf->state);
11552 set_bit(__I40E_DOWN, &pf->state);
11553 rtnl_lock();
11554 i40e_prep_for_reset(pf);
11555 rtnl_unlock();
11556
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011557 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11558 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11559
Catherine Sullivan02b42492015-07-10 19:35:59 -040011560 del_timer_sync(&pf->service_timer);
11561 cancel_work_sync(&pf->service_task);
11562 i40e_fdir_teardown(pf);
11563
11564 rtnl_lock();
11565 i40e_prep_for_reset(pf);
11566 rtnl_unlock();
11567
11568 wr32(hw, I40E_PFPM_APM,
11569 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11570 wr32(hw, I40E_PFPM_WUFC,
11571 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11572
Shannon Nelsone1477582015-02-21 06:44:33 +000011573 i40e_clear_interrupt_scheme(pf);
11574
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011575 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011576 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011577 pci_set_power_state(pdev, PCI_D3hot);
11578 }
11579}
11580
11581#ifdef CONFIG_PM
11582/**
11583 * i40e_suspend - PCI callback for moving to D3
11584 * @pdev: PCI device information struct
11585 **/
11586static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11587{
11588 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011589 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011590 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011591
11592 set_bit(__I40E_SUSPENDED, &pf->state);
11593 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011594
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011595 rtnl_lock();
11596 i40e_prep_for_reset(pf);
11597 rtnl_unlock();
11598
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011599 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11600 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11601
Greg Roseb33d3b72016-05-16 10:26:37 -070011602 i40e_stop_misc_vector(pf);
11603
Greg Rose059ff692016-05-16 10:26:38 -070011604 retval = pci_save_state(pdev);
11605 if (retval)
11606 return retval;
11607
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011608 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011609 pci_set_power_state(pdev, PCI_D3hot);
11610
Greg Rose059ff692016-05-16 10:26:38 -070011611 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011612}
11613
11614/**
11615 * i40e_resume - PCI callback for waking up from D3
11616 * @pdev: PCI device information struct
11617 **/
11618static int i40e_resume(struct pci_dev *pdev)
11619{
11620 struct i40e_pf *pf = pci_get_drvdata(pdev);
11621 u32 err;
11622
11623 pci_set_power_state(pdev, PCI_D0);
11624 pci_restore_state(pdev);
11625 /* pci_restore_state() clears dev->state_saves, so
11626 * call pci_save_state() again to restore it.
11627 */
11628 pci_save_state(pdev);
11629
11630 err = pci_enable_device_mem(pdev);
11631 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011632 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011633 return err;
11634 }
11635 pci_set_master(pdev);
11636
11637 /* no wakeup events while running */
11638 pci_wake_from_d3(pdev, false);
11639
11640 /* handling the reset will rebuild the device state */
11641 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11642 clear_bit(__I40E_DOWN, &pf->state);
11643 rtnl_lock();
11644 i40e_reset_and_rebuild(pf, false);
11645 rtnl_unlock();
11646 }
11647
11648 return 0;
11649}
11650
11651#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011652static const struct pci_error_handlers i40e_err_handler = {
11653 .error_detected = i40e_pci_error_detected,
11654 .slot_reset = i40e_pci_error_slot_reset,
11655 .resume = i40e_pci_error_resume,
11656};
11657
11658static struct pci_driver i40e_driver = {
11659 .name = i40e_driver_name,
11660 .id_table = i40e_pci_tbl,
11661 .probe = i40e_probe,
11662 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011663#ifdef CONFIG_PM
11664 .suspend = i40e_suspend,
11665 .resume = i40e_resume,
11666#endif
11667 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011668 .err_handler = &i40e_err_handler,
11669 .sriov_configure = i40e_pci_sriov_configure,
11670};
11671
11672/**
11673 * i40e_init_module - Driver registration routine
11674 *
11675 * i40e_init_module is the first routine called when the driver is
11676 * loaded. All it does is register with the PCI subsystem.
11677 **/
11678static int __init i40e_init_module(void)
11679{
11680 pr_info("%s: %s - version %s\n", i40e_driver_name,
11681 i40e_driver_string, i40e_driver_version_str);
11682 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011683
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011684 /* we will see if single thread per module is enough for now,
11685 * it can't be any worse than using the system workqueue which
11686 * was already single threaded
11687 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011688 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11689 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011690 if (!i40e_wq) {
11691 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11692 return -ENOMEM;
11693 }
11694
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011695 i40e_dbg_init();
11696 return pci_register_driver(&i40e_driver);
11697}
11698module_init(i40e_init_module);
11699
11700/**
11701 * i40e_exit_module - Driver exit cleanup routine
11702 *
11703 * i40e_exit_module is called just before the driver is removed
11704 * from memory.
11705 **/
11706static void __exit i40e_exit_module(void)
11707{
11708 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011709 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011710 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011711}
11712module_exit(i40e_exit_module);