blob: 96bedb54701cbe6428be1428f8d99559021c7265 [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
Bimmy Pujari15990832017-01-30 12:29:37 -080042#define DRV_VERSION_MAJOR 2
43#define DRV_VERSION_MINOR 1
44#define DRV_VERSION_BUILD 7
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},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080080 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
81 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
83 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040086 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
89 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000090 /* required last entry */
91 {0, }
92};
93MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
94
95#define I40E_MAX_VF_COUNT 128
96static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -040097module_param(debug, uint, 0);
98MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000099
100MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
101MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102MODULE_LICENSE("GPL");
103MODULE_VERSION(DRV_VERSION);
104
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800105static struct workqueue_struct *i40e_wq;
106
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000107/**
108 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
109 * @hw: pointer to the HW structure
110 * @mem: ptr to mem struct to fill out
111 * @size: size of memory requested
112 * @alignment: what to align the allocation to
113 **/
114int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
115 u64 size, u32 alignment)
116{
117 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
118
119 mem->size = ALIGN(size, alignment);
120 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
121 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000122 if (!mem->va)
123 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000124
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000125 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000126}
127
128/**
129 * i40e_free_dma_mem_d - OS specific memory free for shared code
130 * @hw: pointer to the HW structure
131 * @mem: ptr to mem struct to free
132 **/
133int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
134{
135 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
136
137 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
138 mem->va = NULL;
139 mem->pa = 0;
140 mem->size = 0;
141
142 return 0;
143}
144
145/**
146 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
147 * @hw: pointer to the HW structure
148 * @mem: ptr to mem struct to fill out
149 * @size: size of memory requested
150 **/
151int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
152 u32 size)
153{
154 mem->size = size;
155 mem->va = kzalloc(size, GFP_KERNEL);
156
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000157 if (!mem->va)
158 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000159
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000160 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000161}
162
163/**
164 * i40e_free_virt_mem_d - OS specific memory free for shared code
165 * @hw: pointer to the HW structure
166 * @mem: ptr to mem struct to free
167 **/
168int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
169{
170 /* it's ok to kfree a NULL pointer */
171 kfree(mem->va);
172 mem->va = NULL;
173 mem->size = 0;
174
175 return 0;
176}
177
178/**
179 * i40e_get_lump - find a lump of free generic resource
180 * @pf: board private structure
181 * @pile: the pile of resource to search
182 * @needed: the number of items needed
183 * @id: an owner id to stick on the items assigned
184 *
185 * Returns the base item index of the lump, or negative for error
186 *
187 * The search_hint trick and lack of advanced fit-finding only work
188 * because we're highly likely to have all the same size lump requests.
189 * Linear search time and any fragmentation should be minimal.
190 **/
191static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
192 u16 needed, u16 id)
193{
194 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000195 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000196
197 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
198 dev_info(&pf->pdev->dev,
199 "param err: pile=%p needed=%d id=0x%04x\n",
200 pile, needed, id);
201 return -EINVAL;
202 }
203
204 /* start the linear search with an imperfect hint */
205 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000206 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000207 /* skip already allocated entries */
208 if (pile->list[i] & I40E_PILE_VALID_BIT) {
209 i++;
210 continue;
211 }
212
213 /* do we have enough in this lump? */
214 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
215 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
216 break;
217 }
218
219 if (j == needed) {
220 /* there was enough, so assign it to the requestor */
221 for (j = 0; j < needed; j++)
222 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
223 ret = i;
224 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000225 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000226 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400227
228 /* not enough, so skip over it and continue looking */
229 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000230 }
231
232 return ret;
233}
234
235/**
236 * i40e_put_lump - return a lump of generic resource
237 * @pile: the pile of resource to search
238 * @index: the base item index
239 * @id: the owner id of the items assigned
240 *
241 * Returns the count of items in the lump
242 **/
243static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
244{
245 int valid_id = (id | I40E_PILE_VALID_BIT);
246 int count = 0;
247 int i;
248
249 if (!pile || index >= pile->num_entries)
250 return -EINVAL;
251
252 for (i = index;
253 i < pile->num_entries && pile->list[i] == valid_id;
254 i++) {
255 pile->list[i] = 0;
256 count++;
257 }
258
259 if (count && index < pile->search_hint)
260 pile->search_hint = index;
261
262 return count;
263}
264
265/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700266 * i40e_find_vsi_from_id - searches for the vsi with the given id
267 * @pf - the pf structure to search for the vsi
268 * @id - id of the vsi it is searching for
269 **/
270struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
271{
272 int i;
273
274 for (i = 0; i < pf->num_alloc_vsi; i++)
275 if (pf->vsi[i] && (pf->vsi[i]->id == id))
276 return pf->vsi[i];
277
278 return NULL;
279}
280
281/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000282 * i40e_service_event_schedule - Schedule the service task to wake up
283 * @pf: board private structure
284 *
285 * If not already scheduled, this puts the task into the work queue
286 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600287void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000288{
289 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800290 !test_bit(__I40E_RESET_RECOVERY_PENDING, &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 **/
302static void i40e_tx_timeout(struct net_device *netdev)
303{
304 struct i40e_netdev_priv *np = netdev_priv(netdev);
305 struct i40e_vsi *vsi = np->vsi;
306 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400307 struct i40e_ring *tx_ring = NULL;
308 unsigned int i, hung_queue = 0;
309 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000310
311 pf->tx_timeout_count++;
312
Kiran Patilb03a8c12015-09-24 18:13:15 -0400313 /* find the stopped queue the same way the stack does */
314 for (i = 0; i < netdev->num_tx_queues; i++) {
315 struct netdev_queue *q;
316 unsigned long trans_start;
317
318 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200319 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400320 if (netif_xmit_stopped(q) &&
321 time_after(jiffies,
322 (trans_start + netdev->watchdog_timeo))) {
323 hung_queue = i;
324 break;
325 }
326 }
327
328 if (i == netdev->num_tx_queues) {
329 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
330 } else {
331 /* now that we have an index, find the tx_ring struct */
332 for (i = 0; i < vsi->num_queue_pairs; i++) {
333 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
334 if (hung_queue ==
335 vsi->tx_rings[i]->queue_index) {
336 tx_ring = vsi->tx_rings[i];
337 break;
338 }
339 }
340 }
341 }
342
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000343 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400344 pf->tx_timeout_recovery_level = 1; /* reset after some time */
345 else if (time_before(jiffies,
346 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
347 return; /* don't do any new action before the next timeout */
348
349 if (tx_ring) {
350 head = i40e_get_head(tx_ring);
351 /* Read interrupt register */
352 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
353 val = rd32(&pf->hw,
354 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
355 tx_ring->vsi->base_vector - 1));
356 else
357 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
358
359 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",
360 vsi->seid, hung_queue, tx_ring->next_to_clean,
361 head, tx_ring->next_to_use,
362 readl(tx_ring->tail), val);
363 }
364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000365 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400366 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
367 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000368
369 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000370 case 1:
371 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
372 break;
373 case 2:
374 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
375 break;
376 case 3:
377 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
378 break;
379 default:
380 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000381 break;
382 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400383
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000384 i40e_service_event_schedule(pf);
385 pf->tx_timeout_recovery_level++;
386}
387
388/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000389 * i40e_get_vsi_stats_struct - Get System Network Statistics
390 * @vsi: the VSI we care about
391 *
392 * Returns the address of the device statistics structure.
393 * The statistics are actually updated from the service task.
394 **/
395struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
396{
397 return &vsi->net_stats;
398}
399
400/**
401 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
402 * @netdev: network interface device structure
403 *
404 * Returns the address of the device statistics structure.
405 * The statistics are actually updated from the service task.
406 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800407static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800408 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000409{
410 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000411 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000412 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000413 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
414 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000415
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000416 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800417 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000418
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800419 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800420 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800421
Alexander Duyck980e9b12013-09-28 06:01:03 +0000422 rcu_read_lock();
423 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000424 u64 bytes, packets;
425 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000426
Alexander Duyck980e9b12013-09-28 06:01:03 +0000427 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
428 if (!tx_ring)
429 continue;
430
431 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700432 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000433 packets = tx_ring->stats.packets;
434 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700435 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000436
437 stats->tx_packets += packets;
438 stats->tx_bytes += bytes;
439 rx_ring = &tx_ring[1];
440
441 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700442 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000443 packets = rx_ring->stats.packets;
444 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700445 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000446
447 stats->rx_packets += packets;
448 stats->rx_bytes += bytes;
449 }
450 rcu_read_unlock();
451
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000452 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000453 stats->multicast = vsi_stats->multicast;
454 stats->tx_errors = vsi_stats->tx_errors;
455 stats->tx_dropped = vsi_stats->tx_dropped;
456 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400457 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000458 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
459 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000460}
461
462/**
463 * i40e_vsi_reset_stats - Resets all stats of the given vsi
464 * @vsi: the VSI to have its stats reset
465 **/
466void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
467{
468 struct rtnl_link_stats64 *ns;
469 int i;
470
471 if (!vsi)
472 return;
473
474 ns = i40e_get_vsi_stats_struct(vsi);
475 memset(ns, 0, sizeof(*ns));
476 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
477 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
478 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000479 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000480 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400481 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000482 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400483 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000484 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400485 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000486 sizeof(vsi->tx_rings[i]->stats));
487 memset(&vsi->tx_rings[i]->tx_stats, 0,
488 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000489 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000490 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000491 vsi->stat_offsets_loaded = false;
492}
493
494/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000495 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000496 * @pf: the PF to be reset
497 **/
498void i40e_pf_reset_stats(struct i40e_pf *pf)
499{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000500 int i;
501
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000502 memset(&pf->stats, 0, sizeof(pf->stats));
503 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
504 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000505
506 for (i = 0; i < I40E_MAX_VEB; i++) {
507 if (pf->veb[i]) {
508 memset(&pf->veb[i]->stats, 0,
509 sizeof(pf->veb[i]->stats));
510 memset(&pf->veb[i]->stats_offsets, 0,
511 sizeof(pf->veb[i]->stats_offsets));
512 pf->veb[i]->stat_offsets_loaded = false;
513 }
514 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700515 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000516}
517
518/**
519 * i40e_stat_update48 - read and update a 48 bit stat from the chip
520 * @hw: ptr to the hardware info
521 * @hireg: the high 32 bit reg to read
522 * @loreg: the low 32 bit reg to read
523 * @offset_loaded: has the initial offset been loaded yet
524 * @offset: ptr to current offset value
525 * @stat: ptr to the stat
526 *
527 * Since the device stats are not reset at PFReset, they likely will not
528 * be zeroed when the driver starts. We'll save the first values read
529 * and use them as offsets to be subtracted from the raw values in order
530 * to report stats that count from zero. In the process, we also manage
531 * the potential roll-over.
532 **/
533static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
534 bool offset_loaded, u64 *offset, u64 *stat)
535{
536 u64 new_data;
537
Shannon Nelsonab600852014-01-17 15:36:39 -0800538 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000539 new_data = rd32(hw, loreg);
540 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
541 } else {
542 new_data = rd64(hw, loreg);
543 }
544 if (!offset_loaded)
545 *offset = new_data;
546 if (likely(new_data >= *offset))
547 *stat = new_data - *offset;
548 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400549 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000550 *stat &= 0xFFFFFFFFFFFFULL;
551}
552
553/**
554 * i40e_stat_update32 - read and update a 32 bit stat from the chip
555 * @hw: ptr to the hardware info
556 * @reg: the hw reg to read
557 * @offset_loaded: has the initial offset been loaded yet
558 * @offset: ptr to current offset value
559 * @stat: ptr to the stat
560 **/
561static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
562 bool offset_loaded, u64 *offset, u64 *stat)
563{
564 u32 new_data;
565
566 new_data = rd32(hw, reg);
567 if (!offset_loaded)
568 *offset = new_data;
569 if (likely(new_data >= *offset))
570 *stat = (u32)(new_data - *offset);
571 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400572 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000573}
574
575/**
576 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
577 * @vsi: the VSI to be updated
578 **/
579void i40e_update_eth_stats(struct i40e_vsi *vsi)
580{
581 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
582 struct i40e_pf *pf = vsi->back;
583 struct i40e_hw *hw = &pf->hw;
584 struct i40e_eth_stats *oes;
585 struct i40e_eth_stats *es; /* device's eth stats */
586
587 es = &vsi->eth_stats;
588 oes = &vsi->eth_stats_offsets;
589
590 /* Gather up the stats that the hw collects */
591 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
592 vsi->stat_offsets_loaded,
593 &oes->tx_errors, &es->tx_errors);
594 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
595 vsi->stat_offsets_loaded,
596 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000597 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
598 vsi->stat_offsets_loaded,
599 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
600 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
601 vsi->stat_offsets_loaded,
602 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000603
604 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
605 I40E_GLV_GORCL(stat_idx),
606 vsi->stat_offsets_loaded,
607 &oes->rx_bytes, &es->rx_bytes);
608 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
609 I40E_GLV_UPRCL(stat_idx),
610 vsi->stat_offsets_loaded,
611 &oes->rx_unicast, &es->rx_unicast);
612 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
613 I40E_GLV_MPRCL(stat_idx),
614 vsi->stat_offsets_loaded,
615 &oes->rx_multicast, &es->rx_multicast);
616 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
617 I40E_GLV_BPRCL(stat_idx),
618 vsi->stat_offsets_loaded,
619 &oes->rx_broadcast, &es->rx_broadcast);
620
621 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
622 I40E_GLV_GOTCL(stat_idx),
623 vsi->stat_offsets_loaded,
624 &oes->tx_bytes, &es->tx_bytes);
625 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
626 I40E_GLV_UPTCL(stat_idx),
627 vsi->stat_offsets_loaded,
628 &oes->tx_unicast, &es->tx_unicast);
629 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
630 I40E_GLV_MPTCL(stat_idx),
631 vsi->stat_offsets_loaded,
632 &oes->tx_multicast, &es->tx_multicast);
633 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
634 I40E_GLV_BPTCL(stat_idx),
635 vsi->stat_offsets_loaded,
636 &oes->tx_broadcast, &es->tx_broadcast);
637 vsi->stat_offsets_loaded = true;
638}
639
640/**
641 * i40e_update_veb_stats - Update Switch component statistics
642 * @veb: the VEB being updated
643 **/
644static void i40e_update_veb_stats(struct i40e_veb *veb)
645{
646 struct i40e_pf *pf = veb->pf;
647 struct i40e_hw *hw = &pf->hw;
648 struct i40e_eth_stats *oes;
649 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400650 struct i40e_veb_tc_stats *veb_oes;
651 struct i40e_veb_tc_stats *veb_es;
652 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000653
654 idx = veb->stats_idx;
655 es = &veb->stats;
656 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400657 veb_es = &veb->tc_stats;
658 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000659
660 /* Gather up the stats that the hw collects */
661 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
662 veb->stat_offsets_loaded,
663 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000664 if (hw->revision_id > 0)
665 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
666 veb->stat_offsets_loaded,
667 &oes->rx_unknown_protocol,
668 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000669 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
670 veb->stat_offsets_loaded,
671 &oes->rx_bytes, &es->rx_bytes);
672 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
673 veb->stat_offsets_loaded,
674 &oes->rx_unicast, &es->rx_unicast);
675 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
676 veb->stat_offsets_loaded,
677 &oes->rx_multicast, &es->rx_multicast);
678 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
679 veb->stat_offsets_loaded,
680 &oes->rx_broadcast, &es->rx_broadcast);
681
682 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
683 veb->stat_offsets_loaded,
684 &oes->tx_bytes, &es->tx_bytes);
685 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
686 veb->stat_offsets_loaded,
687 &oes->tx_unicast, &es->tx_unicast);
688 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
689 veb->stat_offsets_loaded,
690 &oes->tx_multicast, &es->tx_multicast);
691 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
692 veb->stat_offsets_loaded,
693 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400694 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
695 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
696 I40E_GLVEBTC_RPCL(i, idx),
697 veb->stat_offsets_loaded,
698 &veb_oes->tc_rx_packets[i],
699 &veb_es->tc_rx_packets[i]);
700 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
701 I40E_GLVEBTC_RBCL(i, idx),
702 veb->stat_offsets_loaded,
703 &veb_oes->tc_rx_bytes[i],
704 &veb_es->tc_rx_bytes[i]);
705 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
706 I40E_GLVEBTC_TPCL(i, idx),
707 veb->stat_offsets_loaded,
708 &veb_oes->tc_tx_packets[i],
709 &veb_es->tc_tx_packets[i]);
710 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
711 I40E_GLVEBTC_TBCL(i, idx),
712 veb->stat_offsets_loaded,
713 &veb_oes->tc_tx_bytes[i],
714 &veb_es->tc_tx_bytes[i]);
715 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000716 veb->stat_offsets_loaded = true;
717}
718
719/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000720 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000721 * @vsi: the VSI to be updated
722 *
723 * There are a few instances where we store the same stat in a
724 * couple of different structs. This is partly because we have
725 * the netdev stats that need to be filled out, which is slightly
726 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000727 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000728 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000729static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000730{
731 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000732 struct rtnl_link_stats64 *ons;
733 struct rtnl_link_stats64 *ns; /* netdev stats */
734 struct i40e_eth_stats *oes;
735 struct i40e_eth_stats *es; /* device's eth stats */
736 u32 tx_restart, tx_busy;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800737 u64 tx_lost_interrupt;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000738 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000739 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000740 u64 bytes, packets;
741 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400742 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400743 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000744 u64 rx_p, rx_b;
745 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000746 u16 q;
747
748 if (test_bit(__I40E_DOWN, &vsi->state) ||
749 test_bit(__I40E_CONFIG_BUSY, &pf->state))
750 return;
751
752 ns = i40e_get_vsi_stats_struct(vsi);
753 ons = &vsi->net_stats_offsets;
754 es = &vsi->eth_stats;
755 oes = &vsi->eth_stats_offsets;
756
757 /* Gather up the netdev and vsi stats that the driver collects
758 * on the fly during packet processing
759 */
760 rx_b = rx_p = 0;
761 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400762 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800763 tx_lost_interrupt = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000764 rx_page = 0;
765 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000766 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000767 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000768 /* locate Tx ring */
769 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000770
Alexander Duyck980e9b12013-09-28 06:01:03 +0000771 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700772 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000773 packets = p->stats.packets;
774 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700775 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000776 tx_b += bytes;
777 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000778 tx_restart += p->tx_stats.restart_queue;
779 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400780 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400781 tx_force_wb += p->tx_stats.tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800782 tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000783
784 /* Rx queue is part of the same block as Tx queue */
785 p = &p[1];
786 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700787 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000788 packets = p->stats.packets;
789 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700790 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000791 rx_b += bytes;
792 rx_p += packets;
Mitch Williams420136c2013-12-18 13:45:59 +0000793 rx_buf += p->rx_stats.alloc_buff_failed;
794 rx_page += p->rx_stats.alloc_page_failed;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000795 }
Alexander Duyck980e9b12013-09-28 06:01:03 +0000796 rcu_read_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000797 vsi->tx_restart = tx_restart;
798 vsi->tx_busy = tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400799 vsi->tx_linearize = tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400800 vsi->tx_force_wb = tx_force_wb;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -0800801 vsi->tx_lost_interrupt = tx_lost_interrupt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000802 vsi->rx_page_failed = rx_page;
803 vsi->rx_buf_failed = rx_buf;
804
805 ns->rx_packets = rx_p;
806 ns->rx_bytes = rx_b;
807 ns->tx_packets = tx_p;
808 ns->tx_bytes = tx_b;
809
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000810 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000811 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000812 ons->tx_errors = oes->tx_errors;
813 ns->tx_errors = es->tx_errors;
814 ons->multicast = oes->rx_multicast;
815 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000816 ons->rx_dropped = oes->rx_discards;
817 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000818 ons->tx_dropped = oes->tx_discards;
819 ns->tx_dropped = es->tx_discards;
820
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000821 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000822 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000823 ns->rx_crc_errors = pf->stats.crc_errors;
824 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
825 ns->rx_length_errors = pf->stats.rx_length_errors;
826 }
827}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000828
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000829/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000830 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000831 * @pf: the PF to be updated
832 **/
833static void i40e_update_pf_stats(struct i40e_pf *pf)
834{
835 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
836 struct i40e_hw_port_stats *nsd = &pf->stats;
837 struct i40e_hw *hw = &pf->hw;
838 u32 val;
839 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000840
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000841 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
842 I40E_GLPRT_GORCL(hw->port),
843 pf->stat_offsets_loaded,
844 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
845 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
846 I40E_GLPRT_GOTCL(hw->port),
847 pf->stat_offsets_loaded,
848 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
849 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
850 pf->stat_offsets_loaded,
851 &osd->eth.rx_discards,
852 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000853 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
854 I40E_GLPRT_UPRCL(hw->port),
855 pf->stat_offsets_loaded,
856 &osd->eth.rx_unicast,
857 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000858 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
859 I40E_GLPRT_MPRCL(hw->port),
860 pf->stat_offsets_loaded,
861 &osd->eth.rx_multicast,
862 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000863 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
864 I40E_GLPRT_BPRCL(hw->port),
865 pf->stat_offsets_loaded,
866 &osd->eth.rx_broadcast,
867 &nsd->eth.rx_broadcast);
868 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
869 I40E_GLPRT_UPTCL(hw->port),
870 pf->stat_offsets_loaded,
871 &osd->eth.tx_unicast,
872 &nsd->eth.tx_unicast);
873 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
874 I40E_GLPRT_MPTCL(hw->port),
875 pf->stat_offsets_loaded,
876 &osd->eth.tx_multicast,
877 &nsd->eth.tx_multicast);
878 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
879 I40E_GLPRT_BPTCL(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->eth.tx_broadcast,
882 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000883
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000884 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
885 pf->stat_offsets_loaded,
886 &osd->tx_dropped_link_down,
887 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000888
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000889 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
890 pf->stat_offsets_loaded,
891 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000892
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000893 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
894 pf->stat_offsets_loaded,
895 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000896
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000897 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
898 pf->stat_offsets_loaded,
899 &osd->mac_local_faults,
900 &nsd->mac_local_faults);
901 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
902 pf->stat_offsets_loaded,
903 &osd->mac_remote_faults,
904 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000905
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000906 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
907 pf->stat_offsets_loaded,
908 &osd->rx_length_errors,
909 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000910
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000911 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
912 pf->stat_offsets_loaded,
913 &osd->link_xon_rx, &nsd->link_xon_rx);
914 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
915 pf->stat_offsets_loaded,
916 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800917 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
918 pf->stat_offsets_loaded,
919 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000920 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000923
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000924 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800925 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
926 pf->stat_offsets_loaded,
927 &osd->priority_xoff_rx[i],
928 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000929 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000930 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000931 &osd->priority_xon_rx[i],
932 &nsd->priority_xon_rx[i]);
933 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000934 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000935 &osd->priority_xon_tx[i],
936 &nsd->priority_xon_tx[i]);
937 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000938 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000939 &osd->priority_xoff_tx[i],
940 &nsd->priority_xoff_tx[i]);
941 i40e_stat_update32(hw,
942 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000943 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000944 &osd->priority_xon_2_xoff[i],
945 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000946 }
947
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000948 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
949 I40E_GLPRT_PRC64L(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->rx_size_64, &nsd->rx_size_64);
952 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
953 I40E_GLPRT_PRC127L(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->rx_size_127, &nsd->rx_size_127);
956 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
957 I40E_GLPRT_PRC255L(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->rx_size_255, &nsd->rx_size_255);
960 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
961 I40E_GLPRT_PRC511L(hw->port),
962 pf->stat_offsets_loaded,
963 &osd->rx_size_511, &nsd->rx_size_511);
964 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
965 I40E_GLPRT_PRC1023L(hw->port),
966 pf->stat_offsets_loaded,
967 &osd->rx_size_1023, &nsd->rx_size_1023);
968 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
969 I40E_GLPRT_PRC1522L(hw->port),
970 pf->stat_offsets_loaded,
971 &osd->rx_size_1522, &nsd->rx_size_1522);
972 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
973 I40E_GLPRT_PRC9522L(hw->port),
974 pf->stat_offsets_loaded,
975 &osd->rx_size_big, &nsd->rx_size_big);
976
977 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
978 I40E_GLPRT_PTC64L(hw->port),
979 pf->stat_offsets_loaded,
980 &osd->tx_size_64, &nsd->tx_size_64);
981 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
982 I40E_GLPRT_PTC127L(hw->port),
983 pf->stat_offsets_loaded,
984 &osd->tx_size_127, &nsd->tx_size_127);
985 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
986 I40E_GLPRT_PTC255L(hw->port),
987 pf->stat_offsets_loaded,
988 &osd->tx_size_255, &nsd->tx_size_255);
989 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
990 I40E_GLPRT_PTC511L(hw->port),
991 pf->stat_offsets_loaded,
992 &osd->tx_size_511, &nsd->tx_size_511);
993 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
994 I40E_GLPRT_PTC1023L(hw->port),
995 pf->stat_offsets_loaded,
996 &osd->tx_size_1023, &nsd->tx_size_1023);
997 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
998 I40E_GLPRT_PTC1522L(hw->port),
999 pf->stat_offsets_loaded,
1000 &osd->tx_size_1522, &nsd->tx_size_1522);
1001 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1002 I40E_GLPRT_PTC9522L(hw->port),
1003 pf->stat_offsets_loaded,
1004 &osd->tx_size_big, &nsd->tx_size_big);
1005
1006 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1007 pf->stat_offsets_loaded,
1008 &osd->rx_undersize, &nsd->rx_undersize);
1009 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1010 pf->stat_offsets_loaded,
1011 &osd->rx_fragments, &nsd->rx_fragments);
1012 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1013 pf->stat_offsets_loaded,
1014 &osd->rx_oversize, &nsd->rx_oversize);
1015 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1016 pf->stat_offsets_loaded,
1017 &osd->rx_jabber, &nsd->rx_jabber);
1018
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001019 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001020 i40e_stat_update32(hw,
1021 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001022 pf->stat_offsets_loaded,
1023 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001024 i40e_stat_update32(hw,
1025 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001026 pf->stat_offsets_loaded,
1027 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001028 i40e_stat_update32(hw,
1029 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1030 pf->stat_offsets_loaded,
1031 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001032
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001033 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1034 nsd->tx_lpi_status =
1035 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1036 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1037 nsd->rx_lpi_status =
1038 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1039 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1040 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1041 pf->stat_offsets_loaded,
1042 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1043 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1044 pf->stat_offsets_loaded,
1045 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1046
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001047 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001048 !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001049 nsd->fd_sb_status = true;
1050 else
1051 nsd->fd_sb_status = false;
1052
1053 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001054 !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001055 nsd->fd_atr_status = true;
1056 else
1057 nsd->fd_atr_status = false;
1058
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001059 pf->stat_offsets_loaded = true;
1060}
1061
1062/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001063 * i40e_update_stats - Update the various statistics counters.
1064 * @vsi: the VSI to be updated
1065 *
1066 * Update the various stats for this VSI and its related entities.
1067 **/
1068void i40e_update_stats(struct i40e_vsi *vsi)
1069{
1070 struct i40e_pf *pf = vsi->back;
1071
1072 if (vsi == pf->vsi[pf->lan_vsi])
1073 i40e_update_pf_stats(pf);
1074
1075 i40e_update_vsi_stats(vsi);
1076}
1077
1078/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001079 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1080 * @vsi: the VSI to be searched
1081 * @macaddr: the MAC address
1082 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001083 *
1084 * Returns ptr to the filter object or NULL
1085 **/
1086static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001087 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001088{
1089 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001090 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001091
1092 if (!vsi || !macaddr)
1093 return NULL;
1094
Jacob Keller278e7d02016-10-05 09:30:37 -07001095 key = i40e_addr_to_hkey(macaddr);
1096 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001097 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001098 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001099 return f;
1100 }
1101 return NULL;
1102}
1103
1104/**
1105 * i40e_find_mac - Find a mac addr in the macvlan filters list
1106 * @vsi: the VSI to be searched
1107 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001108 *
1109 * Returns the first filter with the provided MAC address or NULL if
1110 * MAC address was not found
1111 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001112struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001113{
1114 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001115 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001116
1117 if (!vsi || !macaddr)
1118 return NULL;
1119
Jacob Keller278e7d02016-10-05 09:30:37 -07001120 key = i40e_addr_to_hkey(macaddr);
1121 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001122 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001123 return f;
1124 }
1125 return NULL;
1126}
1127
1128/**
1129 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1130 * @vsi: the VSI to be searched
1131 *
1132 * Returns true if VSI is in vlan mode or false otherwise
1133 **/
1134bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1135{
Jacob Kellercbebb852016-10-05 09:30:40 -07001136 /* If we have a PVID, always operate in VLAN mode */
1137 if (vsi->info.pvid)
1138 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001139
Jacob Kellercbebb852016-10-05 09:30:40 -07001140 /* We need to operate in VLAN mode whenever we have any filters with
1141 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1142 * time, incurring search cost repeatedly. However, we can notice two
1143 * things:
1144 *
1145 * 1) the only place where we can gain a VLAN filter is in
1146 * i40e_add_filter.
1147 *
1148 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001149 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001150 *
1151 * Thus, we can simply use a boolean value, has_vlan_filters which we
1152 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1153 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001154 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001155 * filters here and can perform the check at the same time. This
1156 * results in avoiding embedding a loop for VLAN mode inside another
1157 * loop over all the filters, and should maintain correctness as noted
1158 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001159 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001160 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001161}
1162
1163/**
Jacob Keller489a3262016-11-11 12:39:31 -08001164 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1165 * @vsi: the VSI to configure
1166 * @tmp_add_list: list of filters ready to be added
1167 * @tmp_del_list: list of filters ready to be deleted
1168 * @vlan_filters: the number of active VLAN filters
1169 *
1170 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1171 * behave as expected. If we have any active VLAN filters remaining or about
1172 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1173 * so that they only match against untagged traffic. If we no longer have any
1174 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1175 * so that they match against both tagged and untagged traffic. In this way,
1176 * we ensure that we correctly receive the desired traffic. This ensures that
1177 * when we have an active VLAN we will receive only untagged traffic and
1178 * traffic matching active VLANs. If we have no active VLANs then we will
1179 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1180 *
1181 * Finally, in a similar fashion, this function also corrects filters when
1182 * there is an active PVID assigned to this VSI.
1183 *
1184 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1185 *
1186 * This function is only expected to be called from within
1187 * i40e_sync_vsi_filters.
1188 *
1189 * NOTE: This function expects to be called while under the
1190 * mac_filter_hash_lock
1191 */
1192static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1193 struct hlist_head *tmp_add_list,
1194 struct hlist_head *tmp_del_list,
1195 int vlan_filters)
1196{
Jacob Keller5cb25902016-12-12 15:44:15 -08001197 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001198 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001199 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001200 struct hlist_node *h;
1201 int bkt, new_vlan;
1202
1203 /* To determine if a particular filter needs to be replaced we
1204 * have the three following conditions:
1205 *
1206 * a) if we have a PVID assigned, then all filters which are
1207 * not marked as VLAN=PVID must be replaced with filters that
1208 * are.
1209 * b) otherwise, if we have any active VLANS, all filters
1210 * which are marked as VLAN=-1 must be replaced with
1211 * filters marked as VLAN=0
1212 * c) finally, if we do not have any active VLANS, all filters
1213 * which are marked as VLAN=0 must be replaced with filters
1214 * marked as VLAN=-1
1215 */
1216
1217 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001218 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001219 if (pvid && new->f->vlan != pvid)
1220 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001221 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1222 new->f->vlan = 0;
1223 else if (!vlan_filters && new->f->vlan == 0)
1224 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001225 }
1226
1227 /* Update the remaining active filters */
1228 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1229 /* Combine the checks for whether a filter needs to be changed
1230 * and then determine the new VLAN inside the if block, in
1231 * order to avoid duplicating code for adding the new filter
1232 * then deleting the old filter.
1233 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001234 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001235 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1236 (!vlan_filters && f->vlan == 0)) {
1237 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001238 if (pvid)
1239 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001240 else if (vlan_filters)
1241 new_vlan = 0;
1242 else
1243 new_vlan = I40E_VLAN_ANY;
1244
1245 /* Create the new filter */
1246 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1247 if (!add_head)
1248 return -ENOMEM;
1249
Jacob Keller671889e2016-12-02 12:33:00 -08001250 /* Create a temporary i40e_new_mac_filter */
1251 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1252 if (!new)
1253 return -ENOMEM;
1254
1255 new->f = add_head;
1256 new->state = add_head->state;
1257
1258 /* Add the new filter to the tmp list */
1259 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001260
1261 /* Put the original filter into the delete list */
1262 f->state = I40E_FILTER_REMOVE;
1263 hash_del(&f->hlist);
1264 hlist_add_head(&f->hlist, tmp_del_list);
1265 }
1266 }
1267
1268 vsi->has_vlan_filter = !!vlan_filters;
1269
1270 return 0;
1271}
1272
1273/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001274 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1275 * @vsi: the PF Main VSI - inappropriate for any other VSI
1276 * @macaddr: the MAC address
1277 *
1278 * Remove whatever filter the firmware set up so the driver can manage
1279 * its own filtering intelligently.
1280 **/
1281static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1282{
1283 struct i40e_aqc_remove_macvlan_element_data element;
1284 struct i40e_pf *pf = vsi->back;
1285
1286 /* Only appropriate for the PF main VSI */
1287 if (vsi->type != I40E_VSI_MAIN)
1288 return;
1289
1290 memset(&element, 0, sizeof(element));
1291 ether_addr_copy(element.mac_addr, macaddr);
1292 element.vlan_tag = 0;
1293 /* Ignore error returns, some firmware does it this way... */
1294 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1295 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1296
1297 memset(&element, 0, sizeof(element));
1298 ether_addr_copy(element.mac_addr, macaddr);
1299 element.vlan_tag = 0;
1300 /* ...and some firmware does it this way. */
1301 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1302 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1303 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1304}
1305
1306/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001307 * i40e_add_filter - Add a mac/vlan filter to the VSI
1308 * @vsi: the VSI to be searched
1309 * @macaddr: the MAC address
1310 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001311 *
1312 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001313 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001314 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001315 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001316 **/
1317struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001318 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001319{
1320 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001321 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001322
1323 if (!vsi || !macaddr)
1324 return NULL;
1325
Jacob Keller1bc87e82016-10-05 09:30:31 -07001326 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001327 if (!f) {
1328 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1329 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001330 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001331
Jacob Kellercbebb852016-10-05 09:30:40 -07001332 /* Update the boolean indicating if we need to function in
1333 * VLAN mode.
1334 */
1335 if (vlan >= 0)
1336 vsi->has_vlan_filter = true;
1337
Greg Rose9a173902014-05-22 06:32:02 +00001338 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001339 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001340 /* If we're in overflow promisc mode, set the state directly
1341 * to failed, so we don't bother to try sending the filter
1342 * to the hardware.
1343 */
1344 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1345 f->state = I40E_FILTER_FAILED;
1346 else
1347 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001348 INIT_HLIST_NODE(&f->hlist);
1349
1350 key = i40e_addr_to_hkey(macaddr);
1351 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001352
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001353 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1354 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1355 }
1356
Jacob Keller1bc87e82016-10-05 09:30:31 -07001357 /* If we're asked to add a filter that has been marked for removal, it
1358 * is safe to simply restore it to active state. __i40e_del_filter
1359 * will have simply deleted any filters which were previously marked
1360 * NEW or FAILED, so if it is currently marked REMOVE it must have
1361 * previously been ACTIVE. Since we haven't yet run the sync filters
1362 * task, just restore this filter to the ACTIVE state so that the
1363 * sync task leaves it in place
1364 */
1365 if (f->state == I40E_FILTER_REMOVE)
1366 f->state = I40E_FILTER_ACTIVE;
1367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001368 return f;
1369}
1370
1371/**
Jacob Keller290d2552016-10-05 09:30:36 -07001372 * __i40e_del_filter - Remove a specific filter from the VSI
1373 * @vsi: VSI to remove from
1374 * @f: the filter to remove from the list
1375 *
1376 * This function should be called instead of i40e_del_filter only if you know
1377 * the exact filter you will remove already, such as via i40e_find_filter or
1378 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001379 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001380 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001381 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001382 * ANOTHER NOTE: This function MUST be called from within the context of
1383 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1384 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001385 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001386void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001387{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001388 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001389 return;
1390
Alan Bradya410c822016-12-12 15:44:07 -08001391 /* If the filter was never added to firmware then we can just delete it
1392 * directly and we don't want to set the status to remove or else an
1393 * admin queue command will unnecessarily fire.
1394 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001395 if ((f->state == I40E_FILTER_FAILED) ||
1396 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001397 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001398 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001399 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001400 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001401 }
Alan Bradya410c822016-12-12 15:44:07 -08001402
1403 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1404 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001405}
1406
1407/**
Jacob Keller290d2552016-10-05 09:30:36 -07001408 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1409 * @vsi: the VSI to be searched
1410 * @macaddr: the MAC address
1411 * @vlan: the VLAN
1412 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001413 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001414 * being held.
1415 * ANOTHER NOTE: This function MUST be called from within the context of
1416 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1417 * instead of list_for_each_entry().
1418 **/
1419void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1420{
1421 struct i40e_mac_filter *f;
1422
1423 if (!vsi || !macaddr)
1424 return;
1425
1426 f = i40e_find_filter(vsi, macaddr, vlan);
1427 __i40e_del_filter(vsi, f);
1428}
1429
1430/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001431 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001432 * @vsi: the VSI to be searched
1433 * @macaddr: the mac address to be filtered
1434 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001435 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1436 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001437 * unique vlan that already exists. If a PVID has been assigned, instead only
1438 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001439 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001440 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001441 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001442struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1443 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001444{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001445 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001446 struct hlist_node *h;
1447 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001448
1449 if (vsi->info.pvid)
1450 return i40e_add_filter(vsi, macaddr,
1451 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001452
Jacob Keller7aaf95362016-11-11 12:39:33 -08001453 if (!i40e_is_vsi_in_vlan(vsi))
1454 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1455
Jacob Keller278e7d02016-10-05 09:30:37 -07001456 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001457 if (f->state == I40E_FILTER_REMOVE)
1458 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001459 add = i40e_add_filter(vsi, macaddr, f->vlan);
1460 if (!add)
1461 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001462 }
1463
Jacob Keller5feb3d72016-10-05 09:30:34 -07001464 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001465}
1466
1467/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001468 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001469 * @vsi: the VSI to be searched
1470 * @macaddr: the mac address to be removed
1471 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001472 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1473 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001474 *
1475 * Returns 0 for success, or error
1476 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001477int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001478{
Jacob Keller278e7d02016-10-05 09:30:37 -07001479 struct i40e_mac_filter *f;
1480 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001481 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001482 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001483
Jacob Keller278e7d02016-10-05 09:30:37 -07001484 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1485 "Missing mac_filter_hash_lock\n");
1486 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001487 if (ether_addr_equal(macaddr, f->macaddr)) {
1488 __i40e_del_filter(vsi, f);
1489 found = true;
1490 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001491 }
Jacob Keller290d2552016-10-05 09:30:36 -07001492
1493 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001494 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001495 else
1496 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001497}
1498
1499/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001500 * i40e_set_mac - NDO callback to set mac address
1501 * @netdev: network interface device structure
1502 * @p: pointer to an address structure
1503 *
1504 * Returns 0 on success, negative on failure
1505 **/
1506static int i40e_set_mac(struct net_device *netdev, void *p)
1507{
1508 struct i40e_netdev_priv *np = netdev_priv(netdev);
1509 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001510 struct i40e_pf *pf = vsi->back;
1511 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001512 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001513
1514 if (!is_valid_ether_addr(addr->sa_data))
1515 return -EADDRNOTAVAIL;
1516
Shannon Nelson30650cc2014-07-29 04:01:50 +00001517 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1518 netdev_info(netdev, "already using mac address %pM\n",
1519 addr->sa_data);
1520 return 0;
1521 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001522
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001523 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1524 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1525 return -EADDRNOTAVAIL;
1526
Shannon Nelson30650cc2014-07-29 04:01:50 +00001527 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1528 netdev_info(netdev, "returning to hw mac address %pM\n",
1529 hw->mac.addr);
1530 else
1531 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1532
Jacob Keller278e7d02016-10-05 09:30:37 -07001533 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001534 i40e_del_mac_filter(vsi, netdev->dev_addr);
1535 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001536 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001537 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001538 if (vsi->type == I40E_VSI_MAIN) {
1539 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001540
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001541 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001542 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001543 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001544 if (ret)
1545 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1546 i40e_stat_str(hw, ret),
1547 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001548 }
1549
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001550 /* schedule our worker thread which will take care of
1551 * applying the new filter changes
1552 */
1553 i40e_service_event_schedule(vsi->back);
1554 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001555}
1556
1557/**
1558 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1559 * @vsi: the VSI being setup
1560 * @ctxt: VSI context structure
1561 * @enabled_tc: Enabled TCs bitmap
1562 * @is_add: True if called before Add VSI
1563 *
1564 * Setup VSI queue mapping for enabled traffic classes.
1565 **/
1566static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1567 struct i40e_vsi_context *ctxt,
1568 u8 enabled_tc,
1569 bool is_add)
1570{
1571 struct i40e_pf *pf = vsi->back;
1572 u16 sections = 0;
1573 u8 netdev_tc = 0;
1574 u16 numtc = 0;
1575 u16 qcount;
1576 u8 offset;
1577 u16 qmap;
1578 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001579 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001580
1581 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1582 offset = 0;
1583
1584 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1585 /* Find numtc from enabled TC bitmap */
1586 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001587 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001588 numtc++;
1589 }
1590 if (!numtc) {
1591 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1592 numtc = 1;
1593 }
1594 } else {
1595 /* At least TC0 is enabled in case of non-DCB case */
1596 numtc = 1;
1597 }
1598
1599 vsi->tc_config.numtc = numtc;
1600 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001601 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001602 qcount = vsi->alloc_queue_pairs;
1603
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001604 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001605 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001606
1607 /* Setup queue offset/count for all TCs for given VSI */
1608 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1609 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001610 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001611 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001612 int pow, num_qps;
1613
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001614 switch (vsi->type) {
1615 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001616 qcount = min_t(int, pf->alloc_rss_size,
1617 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001618 break;
1619 case I40E_VSI_FDIR:
1620 case I40E_VSI_SRIOV:
1621 case I40E_VSI_VMDQ2:
1622 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001623 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001624 WARN_ON(i != 0);
1625 break;
1626 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001627 vsi->tc_config.tc_info[i].qoffset = offset;
1628 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001629
Shannon Nelson1e200e42015-02-27 09:15:24 +00001630 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001631 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001632 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001633 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001634 pow++;
1635 num_qps >>= 1;
1636 }
1637
1638 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1639 qmap =
1640 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1641 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1642
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001643 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001644 } else {
1645 /* TC is not enabled so set the offset to
1646 * default queue and allocate one queue
1647 * for the given TC.
1648 */
1649 vsi->tc_config.tc_info[i].qoffset = 0;
1650 vsi->tc_config.tc_info[i].qcount = 1;
1651 vsi->tc_config.tc_info[i].netdev_tc = 0;
1652
1653 qmap = 0;
1654 }
1655 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1656 }
1657
1658 /* Set actual Tx/Rx queue pairs */
1659 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001660 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1661 if (vsi->req_queue_pairs > 0)
1662 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001663 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001664 vsi->num_queue_pairs = pf->num_lan_msix;
1665 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001666
1667 /* Scheduler section valid can only be set for ADD VSI */
1668 if (is_add) {
1669 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1670
1671 ctxt->info.up_enable_bits = enabled_tc;
1672 }
1673 if (vsi->type == I40E_VSI_SRIOV) {
1674 ctxt->info.mapping_flags |=
1675 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1676 for (i = 0; i < vsi->num_queue_pairs; i++)
1677 ctxt->info.queue_mapping[i] =
1678 cpu_to_le16(vsi->base_queue + i);
1679 } else {
1680 ctxt->info.mapping_flags |=
1681 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1682 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1683 }
1684 ctxt->info.valid_sections |= cpu_to_le16(sections);
1685}
1686
1687/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001688 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1689 * @netdev: the netdevice
1690 * @addr: address to add
1691 *
1692 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1693 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1694 */
1695static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1696{
1697 struct i40e_netdev_priv *np = netdev_priv(netdev);
1698 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001699
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001700 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001701 return 0;
1702 else
1703 return -ENOMEM;
1704}
1705
1706/**
1707 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1708 * @netdev: the netdevice
1709 * @addr: address to add
1710 *
1711 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1712 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1713 */
1714static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1715{
1716 struct i40e_netdev_priv *np = netdev_priv(netdev);
1717 struct i40e_vsi *vsi = np->vsi;
1718
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001719 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001720
1721 return 0;
1722}
1723
1724/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001725 * i40e_set_rx_mode - NDO callback to set the netdev filters
1726 * @netdev: network interface device structure
1727 **/
1728static void i40e_set_rx_mode(struct net_device *netdev)
1729{
1730 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001731 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001732
Jacob Keller278e7d02016-10-05 09:30:37 -07001733 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001734
Jacob Keller6622f5c2016-10-05 09:30:32 -07001735 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1736 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001737
Jacob Keller278e7d02016-10-05 09:30:37 -07001738 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001739
1740 /* check for other flag changes */
1741 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1742 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1743 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1744 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001745
1746 /* schedule our worker thread which will take care of
1747 * applying the new filter changes
1748 */
1749 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001750}
1751
1752/**
Jacob Keller671889e2016-12-02 12:33:00 -08001753 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001754 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001755 * @from: Pointer to list which contains MAC filter entries - changes to
1756 * those entries needs to be undone.
1757 *
Jacob Keller671889e2016-12-02 12:33:00 -08001758 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001759 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001760static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1761 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001762{
Jacob Keller278e7d02016-10-05 09:30:37 -07001763 struct i40e_mac_filter *f;
1764 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001765
Jacob Keller278e7d02016-10-05 09:30:37 -07001766 hlist_for_each_entry_safe(f, h, from, hlist) {
1767 u64 key = i40e_addr_to_hkey(f->macaddr);
1768
Kiran Patil21659032015-09-30 14:09:03 -04001769 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001770 hlist_del(&f->hlist);
1771 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001772 }
1773}
1774
1775/**
Jacob Keller671889e2016-12-02 12:33:00 -08001776 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1777 * @vsi: Pointer to vsi struct
1778 * @from: Pointer to list which contains MAC filter entries - changes to
1779 * those entries needs to be undone.
1780 *
1781 * MAC filter entries from this list were slated for addition.
1782 **/
1783static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1784 struct hlist_head *from)
1785{
1786 struct i40e_new_mac_filter *new;
1787 struct hlist_node *h;
1788
1789 hlist_for_each_entry_safe(new, h, from, hlist) {
1790 /* We can simply free the wrapper structure */
1791 hlist_del(&new->hlist);
1792 kfree(new);
1793 }
1794}
1795
1796/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001797 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001798 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001799 *
1800 * Returns the next non-broadcast filter in the list. Required so that we
1801 * ignore broadcast filters within the list, since these are not handled via
1802 * the normal firmware update path.
1803 */
Jacob Keller671889e2016-12-02 12:33:00 -08001804static
1805struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001806{
Jacob Keller584a8872017-03-07 15:17:52 -08001807 hlist_for_each_entry_continue(next, hlist) {
1808 if (!is_broadcast_ether_addr(next->f->macaddr))
1809 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001810 }
1811
Jacob Keller584a8872017-03-07 15:17:52 -08001812 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001813}
1814
1815/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001816 * i40e_update_filter_state - Update filter state based on return data
1817 * from firmware
1818 * @count: Number of filters added
1819 * @add_list: return data from fw
1820 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001821 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001822 * MAC filter entries from list were slated to be added to device. Returns
1823 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001824 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001825static int
1826i40e_update_filter_state(int count,
1827 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001828 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001829{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001830 int retval = 0;
1831 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001832
Jacob Kellerac9e2392016-11-11 12:39:27 -08001833 for (i = 0; i < count; i++) {
1834 /* Always check status of each filter. We don't need to check
1835 * the firmware return status because we pre-set the filter
1836 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1837 * request to the adminq. Thus, if it no longer matches then
1838 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001839 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001840 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001841 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001842 } else {
1843 add_head->state = I40E_FILTER_ACTIVE;
1844 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001845 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001846
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001847 add_head = i40e_next_filter(add_head);
1848 if (!add_head)
1849 break;
Kiran Patil21659032015-09-30 14:09:03 -04001850 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001851
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001852 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001853}
1854
1855/**
Jacob Keller00936312016-10-05 09:30:41 -07001856 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1857 * @vsi: ptr to the VSI
1858 * @vsi_name: name to display in messages
1859 * @list: the list of filters to send to firmware
1860 * @num_del: the number of filters to delete
1861 * @retval: Set to -EIO on failure to delete
1862 *
1863 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1864 * *retval instead of a return value so that success does not force ret_val to
1865 * be set to 0. This ensures that a sequence of calls to this function
1866 * preserve the previous value of *retval on successful delete.
1867 */
1868static
1869void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1870 struct i40e_aqc_remove_macvlan_element_data *list,
1871 int num_del, int *retval)
1872{
1873 struct i40e_hw *hw = &vsi->back->hw;
1874 i40e_status aq_ret;
1875 int aq_err;
1876
1877 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1878 aq_err = hw->aq.asq_last_status;
1879
1880 /* Explicitly ignore and do not report when firmware returns ENOENT */
1881 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1882 *retval = -EIO;
1883 dev_info(&vsi->back->pdev->dev,
1884 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1885 vsi_name, i40e_stat_str(hw, aq_ret),
1886 i40e_aq_str(hw, aq_err));
1887 }
1888}
1889
1890/**
1891 * i40e_aqc_add_filters - Request firmware to add a set of filters
1892 * @vsi: ptr to the VSI
1893 * @vsi_name: name to display in messages
1894 * @list: the list of filters to send to firmware
1895 * @add_head: Position in the add hlist
1896 * @num_add: the number of filters to add
1897 * @promisc_change: set to true on exit if promiscuous mode was forced on
1898 *
1899 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1900 * promisc_changed to true if the firmware has run out of space for more
1901 * filters.
1902 */
1903static
1904void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1905 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001906 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001907 int num_add, bool *promisc_changed)
1908{
1909 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001910 int aq_err, fcnt;
1911
Jacob Kellerac9e2392016-11-11 12:39:27 -08001912 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001913 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001914 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001915
1916 if (fcnt != num_add) {
1917 *promisc_changed = true;
1918 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001919 dev_warn(&vsi->back->pdev->dev,
1920 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1921 i40e_aq_str(hw, aq_err),
1922 vsi_name);
1923 }
1924}
1925
1926/**
Jacob Keller435c0842016-11-08 13:05:10 -08001927 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1928 * @vsi: pointer to the VSI
1929 * @f: filter data
1930 *
1931 * This function sets or clears the promiscuous broadcast flags for VLAN
1932 * filters in order to properly receive broadcast frames. Assumes that only
1933 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001934 *
1935 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001936 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001937static i40e_status
1938i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1939 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001940{
1941 bool enable = f->state == I40E_FILTER_NEW;
1942 struct i40e_hw *hw = &vsi->back->hw;
1943 i40e_status aq_ret;
1944
1945 if (f->vlan == I40E_VLAN_ANY) {
1946 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1947 vsi->seid,
1948 enable,
1949 NULL);
1950 } else {
1951 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1952 vsi->seid,
1953 enable,
1954 f->vlan,
1955 NULL);
1956 }
1957
Jacob Keller671889e2016-12-02 12:33:00 -08001958 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001959 dev_warn(&vsi->back->pdev->dev,
1960 "Error %s setting broadcast promiscuous mode on %s\n",
1961 i40e_aq_str(hw, hw->aq.asq_last_status),
1962 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001963
1964 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001965}
1966
1967/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001968 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1969 * @vsi: ptr to the VSI
1970 *
1971 * Push any outstanding VSI filter changes through the AdminQ.
1972 *
1973 * Returns 0 or error value
1974 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001975int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001976{
Jacob Keller278e7d02016-10-05 09:30:37 -07001977 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08001978 struct i40e_mac_filter *f;
1979 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001980 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08001981 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001982 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001983 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001984 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001985 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001986 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001987 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001988 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001989 struct i40e_pf *pf;
1990 int num_add = 0;
1991 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001992 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001993 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001994 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07001995 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001996
1997 /* empty array typed pointers, kcalloc later */
1998 struct i40e_aqc_add_macvlan_element_data *add_list;
1999 struct i40e_aqc_remove_macvlan_element_data *del_list;
2000
2001 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2002 usleep_range(1000, 2000);
2003 pf = vsi->back;
2004
2005 if (vsi->netdev) {
2006 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2007 vsi->current_netdev_flags = vsi->netdev->flags;
2008 }
2009
Jacob Keller278e7d02016-10-05 09:30:37 -07002010 INIT_HLIST_HEAD(&tmp_add_list);
2011 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002012
Shannon Nelson2d1de822016-05-16 10:26:44 -07002013 if (vsi->type == I40E_VSI_SRIOV)
2014 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2015 else if (vsi->type != I40E_VSI_MAIN)
2016 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2017
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002018 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2019 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2020
Jacob Keller278e7d02016-10-05 09:30:37 -07002021 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002022 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002023 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002024 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002025 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002026 hash_del(&f->hlist);
2027 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002028
2029 /* Avoid counting removed filters */
2030 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002031 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002032 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002033 /* Create a temporary i40e_new_mac_filter */
2034 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2035 if (!new)
2036 goto err_no_memory_locked;
2037
2038 /* Store pointer to the real filter */
2039 new->f = f;
2040 new->state = f->state;
2041
2042 /* Add it to the hash list */
2043 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002044 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002045
Jacob Keller489a3262016-11-11 12:39:31 -08002046 /* Count the number of active (current and new) VLAN
2047 * filters we have now. Does not count filters which
2048 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002049 */
2050 if (f->vlan > 0)
2051 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002052 }
2053
Jacob Keller489a3262016-11-11 12:39:31 -08002054 retval = i40e_correct_mac_vlan_filters(vsi,
2055 &tmp_add_list,
2056 &tmp_del_list,
2057 vlan_filters);
2058 if (retval)
2059 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002060
Jacob Keller278e7d02016-10-05 09:30:37 -07002061 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002062 }
2063
2064 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002065 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002066 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002067 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002068 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002069 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002070 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002071 if (!del_list)
2072 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002073
Jacob Keller278e7d02016-10-05 09:30:37 -07002074 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002075 cmd_flags = 0;
2076
Jacob Keller435c0842016-11-08 13:05:10 -08002077 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002078 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002079 */
2080 if (is_broadcast_ether_addr(f->macaddr)) {
2081 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2082
2083 hlist_del(&f->hlist);
2084 kfree(f);
2085 continue;
2086 }
2087
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002088 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002089 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002090 if (f->vlan == I40E_VLAN_ANY) {
2091 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002092 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002093 } else {
2094 del_list[num_del].vlan_tag =
2095 cpu_to_le16((u16)(f->vlan));
2096 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002098 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2099 del_list[num_del].flags = cmd_flags;
2100 num_del++;
2101
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002102 /* flush a full buffer */
2103 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002104 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2105 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002106 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002107 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002108 }
Kiran Patil21659032015-09-30 14:09:03 -04002109 /* Release memory for MAC filter entries which were
2110 * synced up with HW.
2111 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002112 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002113 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002114 }
Kiran Patil21659032015-09-30 14:09:03 -04002115
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002116 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002117 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2118 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002119 }
2120
2121 kfree(del_list);
2122 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002123 }
2124
Jacob Keller278e7d02016-10-05 09:30:37 -07002125 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002126 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002127 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002128 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002129 list_size = filter_list_len *
2130 sizeof(struct i40e_aqc_add_macvlan_element_data);
2131 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002132 if (!add_list)
2133 goto err_no_memory;
2134
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002135 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002136 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002137 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2138 &vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002139 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002140 continue;
2141 }
Jacob Keller435c0842016-11-08 13:05:10 -08002142
2143 /* handle broadcast filters by updating the broadcast
2144 * promiscuous flag instead of adding a MAC filter.
2145 */
Jacob Keller671889e2016-12-02 12:33:00 -08002146 if (is_broadcast_ether_addr(new->f->macaddr)) {
2147 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2148 new->f))
2149 new->state = I40E_FILTER_FAILED;
2150 else
2151 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002152 continue;
2153 }
2154
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002155 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002156 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002157 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002158 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002159 ether_addr_copy(add_list[num_add].mac_addr,
2160 new->f->macaddr);
2161 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002162 add_list[num_add].vlan_tag = 0;
2163 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2164 } else {
2165 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002166 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002167 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002168 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002169 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002170 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002171 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002172 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2173 num_add++;
2174
2175 /* flush a full buffer */
2176 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002177 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2178 add_head, num_add,
2179 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002180 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002182 }
2183 }
2184 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002185 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2186 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002187 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002188 /* Now move all of the filters from the temp add list back to
2189 * the VSI's list.
2190 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002191 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002192 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2193 /* Only update the state if we're still NEW */
2194 if (new->f->state == I40E_FILTER_NEW)
2195 new->f->state = new->state;
2196 hlist_del(&new->hlist);
2197 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002198 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002199 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002200 kfree(add_list);
2201 add_list = NULL;
2202 }
2203
Jacob Keller38326212016-11-11 12:39:26 -08002204 /* Determine the number of active and failed filters. */
2205 spin_lock_bh(&vsi->mac_filter_hash_lock);
2206 vsi->active_filters = 0;
2207 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2208 if (f->state == I40E_FILTER_ACTIVE)
2209 vsi->active_filters++;
2210 else if (f->state == I40E_FILTER_FAILED)
2211 failed_filters++;
2212 }
2213 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2214
2215 /* If promiscuous mode has changed, we need to calculate a new
2216 * threshold for when we are safe to exit
2217 */
2218 if (promisc_changed)
2219 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2220
2221 /* Check if we are able to exit overflow promiscuous mode. We can
2222 * safely exit if we didn't just enter, we no longer have any failed
2223 * filters, and we have reduced filters below the threshold value.
2224 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002225 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002226 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002227 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002228 dev_info(&pf->pdev->dev,
2229 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2230 vsi_name);
2231 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2232 promisc_changed = true;
2233 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002234 }
2235
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002236 /* if the VF is not trusted do not do promisc */
2237 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2238 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2239 goto out;
2240 }
2241
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002242 /* check for changes in promiscuous modes */
2243 if (changed_flags & IFF_ALLMULTI) {
2244 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002245
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002246 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002247 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2248 vsi->seid,
2249 cur_multipromisc,
2250 NULL);
2251 if (aq_ret) {
2252 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002253 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002254 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002255 "set multi promisc failed on %s, err %s aq_err %s\n",
2256 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002257 i40e_stat_str(hw, aq_ret),
2258 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002259 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002260 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002261 if ((changed_flags & IFF_PROMISC) ||
2262 (promisc_changed &&
2263 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002264 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002265
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002266 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2267 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2268 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002269 if ((vsi->type == I40E_VSI_MAIN) &&
2270 (pf->lan_veb != I40E_NO_VEB) &&
2271 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002272 /* set defport ON for Main VSI instead of true promisc
2273 * this way we will get all unicast/multicast and VLAN
2274 * promisc behavior but will not get VF or VMDq traffic
2275 * replicated on the Main VSI.
2276 */
2277 if (pf->cur_promisc != cur_promisc) {
2278 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002279 if (cur_promisc)
2280 aq_ret =
2281 i40e_aq_set_default_vsi(hw,
2282 vsi->seid,
2283 NULL);
2284 else
2285 aq_ret =
2286 i40e_aq_clear_default_vsi(hw,
2287 vsi->seid,
2288 NULL);
2289 if (aq_ret) {
2290 retval = i40e_aq_rc_to_posix(aq_ret,
2291 hw->aq.asq_last_status);
2292 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002293 "Set default VSI failed on %s, err %s, aq_err %s\n",
2294 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002295 i40e_stat_str(hw, aq_ret),
2296 i40e_aq_str(hw,
2297 hw->aq.asq_last_status));
2298 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002299 }
2300 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002301 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002302 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002303 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002304 cur_promisc, NULL,
2305 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002306 if (aq_ret) {
2307 retval =
2308 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002309 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002310 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002311 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2312 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002313 i40e_stat_str(hw, aq_ret),
2314 i40e_aq_str(hw,
2315 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002316 }
2317 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002318 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002319 vsi->seid,
2320 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002321 if (aq_ret) {
2322 retval =
2323 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002324 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002325 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002326 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2327 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002328 i40e_stat_str(hw, aq_ret),
2329 i40e_aq_str(hw,
2330 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002331 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002332 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002333 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2334 vsi->seid,
2335 cur_promisc, NULL);
2336 if (aq_ret) {
2337 retval = i40e_aq_rc_to_posix(aq_ret,
2338 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002339 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002340 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002341 i40e_stat_str(hw, aq_ret),
2342 i40e_aq_str(hw,
2343 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002344 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002345 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002346out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002347 /* if something went wrong then set the changed flag so we try again */
2348 if (retval)
2349 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2350
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002351 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002352 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002353
2354err_no_memory:
2355 /* Restore elements on the temporary add and delete lists */
2356 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002357err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002358 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2359 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002360 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2361
2362 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2363 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2364 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002365}
2366
2367/**
2368 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2369 * @pf: board private structure
2370 **/
2371static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2372{
2373 int v;
2374
2375 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2376 return;
2377 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2378
Mitch Williams505682c2014-05-20 08:01:37 +00002379 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002380 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002381 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2382 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2383
2384 if (ret) {
2385 /* come back and try again later */
2386 pf->flags |= I40E_FLAG_FILTER_SYNC;
2387 break;
2388 }
2389 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002390 }
2391}
2392
2393/**
2394 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2395 * @netdev: network interface device structure
2396 * @new_mtu: new value for maximum frame size
2397 *
2398 * Returns 0 on success, negative on failure
2399 **/
2400static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2401{
2402 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002403 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002404 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002406 netdev_info(netdev, "changing MTU from %d to %d\n",
2407 netdev->mtu, new_mtu);
2408 netdev->mtu = new_mtu;
2409 if (netif_running(netdev))
2410 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002411 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2412 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002413 return 0;
2414}
2415
2416/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002417 * i40e_ioctl - Access the hwtstamp interface
2418 * @netdev: network interface device structure
2419 * @ifr: interface request data
2420 * @cmd: ioctl command
2421 **/
2422int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2423{
2424 struct i40e_netdev_priv *np = netdev_priv(netdev);
2425 struct i40e_pf *pf = np->vsi->back;
2426
2427 switch (cmd) {
2428 case SIOCGHWTSTAMP:
2429 return i40e_ptp_get_ts_config(pf, ifr);
2430 case SIOCSHWTSTAMP:
2431 return i40e_ptp_set_ts_config(pf, ifr);
2432 default:
2433 return -EOPNOTSUPP;
2434 }
2435}
2436
2437/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002438 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2439 * @vsi: the vsi being adjusted
2440 **/
2441void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2442{
2443 struct i40e_vsi_context ctxt;
2444 i40e_status ret;
2445
2446 if ((vsi->info.valid_sections &
2447 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2448 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2449 return; /* already enabled */
2450
2451 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2452 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2453 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2454
2455 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002456 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002457 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2458 if (ret) {
2459 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002460 "update vlan stripping failed, err %s aq_err %s\n",
2461 i40e_stat_str(&vsi->back->hw, ret),
2462 i40e_aq_str(&vsi->back->hw,
2463 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002464 }
2465}
2466
2467/**
2468 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2469 * @vsi: the vsi being adjusted
2470 **/
2471void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2472{
2473 struct i40e_vsi_context ctxt;
2474 i40e_status ret;
2475
2476 if ((vsi->info.valid_sections &
2477 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2478 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2479 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2480 return; /* already disabled */
2481
2482 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2483 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2484 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2485
2486 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002487 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002488 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2489 if (ret) {
2490 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002491 "update vlan stripping failed, err %s aq_err %s\n",
2492 i40e_stat_str(&vsi->back->hw, ret),
2493 i40e_aq_str(&vsi->back->hw,
2494 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002495 }
2496}
2497
2498/**
2499 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2500 * @netdev: network interface to be adjusted
2501 * @features: netdev features to test if VLAN offload is enabled or not
2502 **/
2503static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2504{
2505 struct i40e_netdev_priv *np = netdev_priv(netdev);
2506 struct i40e_vsi *vsi = np->vsi;
2507
2508 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2509 i40e_vlan_stripping_enable(vsi);
2510 else
2511 i40e_vlan_stripping_disable(vsi);
2512}
2513
2514/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002515 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002516 * @vsi: the vsi being configured
2517 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002518 *
2519 * This is a helper function for adding a new MAC/VLAN filter with the
2520 * specified VLAN for each existing MAC address already in the hash table.
2521 * This function does *not* perform any accounting to update filters based on
2522 * VLAN mode.
2523 *
2524 * NOTE: this function expects to be called while under the
2525 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002526 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002527int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002528{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002529 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002530 struct hlist_node *h;
2531 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002532
Jacob Keller278e7d02016-10-05 09:30:37 -07002533 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002534 if (f->state == I40E_FILTER_REMOVE)
2535 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002536 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002537 if (!add_f) {
2538 dev_info(&vsi->back->pdev->dev,
2539 "Could not add vlan filter %d for %pM\n",
2540 vid, f->macaddr);
2541 return -ENOMEM;
2542 }
2543 }
2544
Jacob Keller490a4ad2016-11-11 12:39:29 -08002545 return 0;
2546}
2547
2548/**
2549 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2550 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002551 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002552 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002553int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002554{
Jacob Keller489a3262016-11-11 12:39:31 -08002555 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002556
Jacob Kellerf94484b2016-12-07 14:05:34 -08002557 if (!vid || vsi->info.pvid)
2558 return -EINVAL;
2559
Jacob Keller490a4ad2016-11-11 12:39:29 -08002560 /* Locked once because all functions invoked below iterates list*/
2561 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002562 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002563 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002564 if (err)
2565 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002566
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002567 /* schedule our worker thread which will take care of
2568 * applying the new filter changes
2569 */
2570 i40e_service_event_schedule(vsi->back);
2571 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002572}
2573
2574/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002575 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002576 * @vsi: the vsi being configured
2577 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002578 *
2579 * This function should be used to remove all VLAN filters which match the
2580 * given VID. It does not schedule the service event and does not take the
2581 * mac_filter_hash_lock so it may be combined with other operations under
2582 * a single invocation of the mac_filter_hash_lock.
2583 *
2584 * NOTE: this function expects to be called while under the
2585 * mac_filter_hash_lock
2586 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002587void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002588{
Alan Brady84f5ca62016-10-05 09:30:39 -07002589 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002590 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002591 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002592
Jacob Keller278e7d02016-10-05 09:30:37 -07002593 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002594 if (f->vlan == vid)
2595 __i40e_del_filter(vsi, f);
2596 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002597}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002598
Jacob Keller490a4ad2016-11-11 12:39:29 -08002599/**
2600 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2601 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002602 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002603 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002604void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002605{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002606 if (!vid || vsi->info.pvid)
2607 return;
2608
Jacob Keller490a4ad2016-11-11 12:39:29 -08002609 spin_lock_bh(&vsi->mac_filter_hash_lock);
2610 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002611 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002612
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002613 /* schedule our worker thread which will take care of
2614 * applying the new filter changes
2615 */
2616 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002617}
2618
2619/**
2620 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2621 * @netdev: network interface to be adjusted
2622 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002623 *
2624 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002625 **/
2626static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2627 __always_unused __be16 proto, u16 vid)
2628{
2629 struct i40e_netdev_priv *np = netdev_priv(netdev);
2630 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002631 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002632
Jacob Keller6a1127852016-10-25 16:08:49 -07002633 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002634 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002635
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002636 /* If the network stack called us with vid = 0 then
2637 * it is asking to receive priority tagged packets with
2638 * vlan id 0. Our HW receives them by default when configured
2639 * to receive untagged packets so there is no need to add an
2640 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002641 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002642 if (vid)
2643 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002644
Jacob Keller6a1127852016-10-25 16:08:49 -07002645 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002646 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002647
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002648 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002649}
2650
2651/**
2652 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2653 * @netdev: network interface to be adjusted
2654 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002655 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002656 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002657 **/
2658static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2659 __always_unused __be16 proto, u16 vid)
2660{
2661 struct i40e_netdev_priv *np = netdev_priv(netdev);
2662 struct i40e_vsi *vsi = np->vsi;
2663
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002664 /* return code is ignored as there is nothing a user
2665 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002666 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002667 */
2668 i40e_vsi_kill_vlan(vsi, vid);
2669
2670 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002671
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002672 return 0;
2673}
2674
2675/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002676 * i40e_macaddr_init - explicitly write the mac address filters
2677 *
2678 * @vsi: pointer to the vsi
2679 * @macaddr: the MAC address
2680 *
2681 * This is needed when the macaddr has been obtained by other
2682 * means than the default, e.g., from Open Firmware or IDPROM.
2683 * Returns 0 on success, negative on failure
2684 **/
2685static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2686{
2687 int ret;
2688 struct i40e_aqc_add_macvlan_element_data element;
2689
2690 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2691 I40E_AQC_WRITE_TYPE_LAA_WOL,
2692 macaddr, NULL);
2693 if (ret) {
2694 dev_info(&vsi->back->pdev->dev,
2695 "Addr change for VSI failed: %d\n", ret);
2696 return -EADDRNOTAVAIL;
2697 }
2698
2699 memset(&element, 0, sizeof(element));
2700 ether_addr_copy(element.mac_addr, macaddr);
2701 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2702 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2703 if (ret) {
2704 dev_info(&vsi->back->pdev->dev,
2705 "add filter failed err %s aq_err %s\n",
2706 i40e_stat_str(&vsi->back->hw, ret),
2707 i40e_aq_str(&vsi->back->hw,
2708 vsi->back->hw.aq.asq_last_status));
2709 }
2710 return ret;
2711}
2712
2713/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002714 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2715 * @vsi: the vsi being brought back up
2716 **/
2717static void i40e_restore_vlan(struct i40e_vsi *vsi)
2718{
2719 u16 vid;
2720
2721 if (!vsi->netdev)
2722 return;
2723
2724 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2725
2726 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2727 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2728 vid);
2729}
2730
2731/**
2732 * i40e_vsi_add_pvid - Add pvid for the VSI
2733 * @vsi: the vsi being adjusted
2734 * @vid: the vlan id to set as a PVID
2735 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002736int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002737{
2738 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002739 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002740
2741 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2742 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002743 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2744 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002745 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002746
2747 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002748 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002749 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2750 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002751 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002752 "add pvid failed, err %s aq_err %s\n",
2753 i40e_stat_str(&vsi->back->hw, ret),
2754 i40e_aq_str(&vsi->back->hw,
2755 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002756 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002757 }
2758
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002759 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002760}
2761
2762/**
2763 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2764 * @vsi: the vsi being adjusted
2765 *
2766 * Just use the vlan_rx_register() service to put it back to normal
2767 **/
2768void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2769{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002770 i40e_vlan_stripping_disable(vsi);
2771
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002773}
2774
2775/**
2776 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2777 * @vsi: ptr to the VSI
2778 *
2779 * If this function returns with an error, then it's possible one or
2780 * more of the rings is populated (while the rest are not). It is the
2781 * callers duty to clean those orphaned rings.
2782 *
2783 * Return 0 on success, negative on failure
2784 **/
2785static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2786{
2787 int i, err = 0;
2788
2789 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002790 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002791
2792 return err;
2793}
2794
2795/**
2796 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2797 * @vsi: ptr to the VSI
2798 *
2799 * Free VSI's transmit software resources
2800 **/
2801static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2802{
2803 int i;
2804
Greg Rose8e9dca52013-12-18 13:45:53 +00002805 if (!vsi->tx_rings)
2806 return;
2807
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002808 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002809 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002810 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002811}
2812
2813/**
2814 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2815 * @vsi: ptr to the VSI
2816 *
2817 * If this function returns with an error, then it's possible one or
2818 * more of the rings is populated (while the rest are not). It is the
2819 * callers duty to clean those orphaned rings.
2820 *
2821 * Return 0 on success, negative on failure
2822 **/
2823static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2824{
2825 int i, err = 0;
2826
2827 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002828 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002829 return err;
2830}
2831
2832/**
2833 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2834 * @vsi: ptr to the VSI
2835 *
2836 * Free all receive software resources
2837 **/
2838static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2839{
2840 int i;
2841
Greg Rose8e9dca52013-12-18 13:45:53 +00002842 if (!vsi->rx_rings)
2843 return;
2844
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002845 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002846 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002847 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002848}
2849
2850/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002851 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2852 * @ring: The Tx ring to configure
2853 *
2854 * This enables/disables XPS for a given Tx descriptor ring
2855 * based on the TCs enabled for the VSI that ring belongs to.
2856 **/
2857static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2858{
2859 struct i40e_vsi *vsi = ring->vsi;
2860 cpumask_var_t mask;
2861
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002862 if (!ring->q_vector || !ring->netdev)
2863 return;
2864
2865 /* Single TC mode enable XPS */
2866 if (vsi->tc_config.numtc <= 1) {
2867 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002868 netif_set_xps_queue(ring->netdev,
2869 &ring->q_vector->affinity_mask,
2870 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002871 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2872 /* Disable XPS to allow selection based on TC */
2873 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2874 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2875 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002876 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002877
2878 /* schedule our worker thread which will take care of
2879 * applying the new filter changes
2880 */
2881 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002882}
2883
2884/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002885 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2886 * @ring: The Tx ring to configure
2887 *
2888 * Configure the Tx descriptor ring in the HMC context.
2889 **/
2890static int i40e_configure_tx_ring(struct i40e_ring *ring)
2891{
2892 struct i40e_vsi *vsi = ring->vsi;
2893 u16 pf_q = vsi->base_queue + ring->queue_index;
2894 struct i40e_hw *hw = &vsi->back->hw;
2895 struct i40e_hmc_obj_txq tx_ctx;
2896 i40e_status err = 0;
2897 u32 qtx_ctl = 0;
2898
2899 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002900 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002901 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2902 ring->atr_count = 0;
2903 } else {
2904 ring->atr_sample_rate = 0;
2905 }
2906
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002907 /* configure XPS */
2908 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002909
2910 /* clear the context structure first */
2911 memset(&tx_ctx, 0, sizeof(tx_ctx));
2912
2913 tx_ctx.new_context = 1;
2914 tx_ctx.base = (ring->dma / 128);
2915 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002916 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2917 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002918 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002919 /* FDIR VSI tx ring can still use RS bit and writebacks */
2920 if (vsi->type != I40E_VSI_FDIR)
2921 tx_ctx.head_wb_ena = 1;
2922 tx_ctx.head_wb_addr = ring->dma +
2923 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002924
2925 /* As part of VSI creation/update, FW allocates certain
2926 * Tx arbitration queue sets for each TC enabled for
2927 * the VSI. The FW returns the handles to these queue
2928 * sets as part of the response buffer to Add VSI,
2929 * Update VSI, etc. AQ commands. It is expected that
2930 * these queue set handles be associated with the Tx
2931 * queues by the driver as part of the TX queue context
2932 * initialization. This has to be done regardless of
2933 * DCB as by default everything is mapped to TC0.
2934 */
2935 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2936 tx_ctx.rdylist_act = 0;
2937
2938 /* clear the context in the HMC */
2939 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2940 if (err) {
2941 dev_info(&vsi->back->pdev->dev,
2942 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2943 ring->queue_index, pf_q, err);
2944 return -ENOMEM;
2945 }
2946
2947 /* set the context in the HMC */
2948 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2949 if (err) {
2950 dev_info(&vsi->back->pdev->dev,
2951 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2952 ring->queue_index, pf_q, err);
2953 return -ENOMEM;
2954 }
2955
2956 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002957 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002958 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002959 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2960 I40E_QTX_CTL_VFVM_INDX_MASK;
2961 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002962 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002963 }
2964
Shannon Nelson13fd977492013-09-28 07:14:19 +00002965 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2966 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002967 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2968 i40e_flush(hw);
2969
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002970 /* cache tail off for easier writes later */
2971 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2972
2973 return 0;
2974}
2975
2976/**
2977 * i40e_configure_rx_ring - Configure a receive ring context
2978 * @ring: The Rx ring to configure
2979 *
2980 * Configure the Rx descriptor ring in the HMC context.
2981 **/
2982static int i40e_configure_rx_ring(struct i40e_ring *ring)
2983{
2984 struct i40e_vsi *vsi = ring->vsi;
2985 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2986 u16 pf_q = vsi->base_queue + ring->queue_index;
2987 struct i40e_hw *hw = &vsi->back->hw;
2988 struct i40e_hmc_obj_rxq rx_ctx;
2989 i40e_status err = 0;
2990
2991 ring->state = 0;
2992
2993 /* clear the context structure first */
2994 memset(&rx_ctx, 0, sizeof(rx_ctx));
2995
2996 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002997
2998 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002999
3000 rx_ctx.base = (ring->dma / 128);
3001 rx_ctx.qlen = ring->count;
3002
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003003 /* use 32 byte descriptors */
3004 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003005
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003006 /* descriptor type is always zero
3007 * rx_ctx.dtype = 0;
3008 */
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003009 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003010
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003011 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003012 if (hw->revision_id == 0)
3013 rx_ctx.lrxqthresh = 0;
3014 else
3015 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016 rx_ctx.crcstrip = 1;
3017 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003018 /* this controls whether VLAN is stripped from inner headers */
3019 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003020 /* set the prefena field to 1 because the manual says to */
3021 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022
3023 /* clear the context in the HMC */
3024 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3025 if (err) {
3026 dev_info(&vsi->back->pdev->dev,
3027 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3028 ring->queue_index, pf_q, err);
3029 return -ENOMEM;
3030 }
3031
3032 /* set the context in the HMC */
3033 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3034 if (err) {
3035 dev_info(&vsi->back->pdev->dev,
3036 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3037 ring->queue_index, pf_q, err);
3038 return -ENOMEM;
3039 }
3040
3041 /* cache tail for quicker writes, and clear the reg before use */
3042 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3043 writel(0, ring->tail);
3044
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003045 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003046
3047 return 0;
3048}
3049
3050/**
3051 * i40e_vsi_configure_tx - Configure the VSI for Tx
3052 * @vsi: VSI structure describing this set of rings and resources
3053 *
3054 * Configure the Tx VSI for operation.
3055 **/
3056static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3057{
3058 int err = 0;
3059 u16 i;
3060
Alexander Duyck9f65e152013-09-28 06:00:58 +00003061 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3062 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003063
3064 return err;
3065}
3066
3067/**
3068 * i40e_vsi_configure_rx - Configure the VSI for Rx
3069 * @vsi: the VSI being configured
3070 *
3071 * Configure the Rx VSI for operation.
3072 **/
3073static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3074{
3075 int err = 0;
3076 u16 i;
3077
3078 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
3079 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
3080 + ETH_FCS_LEN + VLAN_HLEN;
3081 else
3082 vsi->max_frame = I40E_RXBUFFER_2048;
3083
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003084 vsi->rx_buf_len = I40E_RXBUFFER_2048;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003085
3086 /* round up for the chip's needs */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003087 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003088 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003089
3090 /* set up individual rings */
3091 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003092 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003093
3094 return err;
3095}
3096
3097/**
3098 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3099 * @vsi: ptr to the VSI
3100 **/
3101static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3102{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003103 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003104 u16 qoffset, qcount;
3105 int i, n;
3106
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003107 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3108 /* Reset the TC information */
3109 for (i = 0; i < vsi->num_queue_pairs; i++) {
3110 rx_ring = vsi->rx_rings[i];
3111 tx_ring = vsi->tx_rings[i];
3112 rx_ring->dcb_tc = 0;
3113 tx_ring->dcb_tc = 0;
3114 }
3115 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003116
3117 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003118 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003119 continue;
3120
3121 qoffset = vsi->tc_config.tc_info[n].qoffset;
3122 qcount = vsi->tc_config.tc_info[n].qcount;
3123 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003124 rx_ring = vsi->rx_rings[i];
3125 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003126 rx_ring->dcb_tc = n;
3127 tx_ring->dcb_tc = n;
3128 }
3129 }
3130}
3131
3132/**
3133 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3134 * @vsi: ptr to the VSI
3135 **/
3136static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3137{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003138 struct i40e_pf *pf = vsi->back;
3139 int err;
3140
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003141 if (vsi->netdev)
3142 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003143
3144 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3145 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3146 if (err) {
3147 dev_warn(&pf->pdev->dev,
3148 "could not set up macaddr; err %d\n", err);
3149 }
3150 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003151}
3152
3153/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003154 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3155 * @vsi: Pointer to the targeted VSI
3156 *
3157 * This function replays the hlist on the hw where all the SB Flow Director
3158 * filters were saved.
3159 **/
3160static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3161{
3162 struct i40e_fdir_filter *filter;
3163 struct i40e_pf *pf = vsi->back;
3164 struct hlist_node *node;
3165
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003166 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3167 return;
3168
Jacob Keller6d069422017-02-06 14:38:44 -08003169 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003170 pf->fd_tcp4_filter_cnt = 0;
3171 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003172 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003173 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003174
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003175 hlist_for_each_entry_safe(filter, node,
3176 &pf->fdir_filter_list, fdir_node) {
3177 i40e_add_del_fdir(vsi, filter, true);
3178 }
3179}
3180
3181/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003182 * i40e_vsi_configure - Set up the VSI for action
3183 * @vsi: the VSI being configured
3184 **/
3185static int i40e_vsi_configure(struct i40e_vsi *vsi)
3186{
3187 int err;
3188
3189 i40e_set_vsi_rx_mode(vsi);
3190 i40e_restore_vlan(vsi);
3191 i40e_vsi_config_dcb_rings(vsi);
3192 err = i40e_vsi_configure_tx(vsi);
3193 if (!err)
3194 err = i40e_vsi_configure_rx(vsi);
3195
3196 return err;
3197}
3198
3199/**
3200 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3201 * @vsi: the VSI being configured
3202 **/
3203static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3204{
3205 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003206 struct i40e_hw *hw = &pf->hw;
3207 u16 vector;
3208 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003209 u32 qp;
3210
3211 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3212 * and PFINT_LNKLSTn registers, e.g.:
3213 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3214 */
3215 qp = vsi->base_queue;
3216 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003217 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003218 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3219
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003220 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003221 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003222 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3223 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3224 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003225 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003226 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3227 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3228 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003229 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003230 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003231
3232 /* Linked list for the queuepairs assigned to this vector */
3233 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3234 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003235 u32 val;
3236
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003237 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3238 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3239 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3240 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3241 (I40E_QUEUE_TYPE_TX
3242 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3243
3244 wr32(hw, I40E_QINT_RQCTL(qp), val);
3245
3246 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3247 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3248 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3249 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3250 (I40E_QUEUE_TYPE_RX
3251 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3252
3253 /* Terminate the linked list */
3254 if (q == (q_vector->num_ringpairs - 1))
3255 val |= (I40E_QUEUE_END_OF_LIST
3256 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3257
3258 wr32(hw, I40E_QINT_TQCTL(qp), val);
3259 qp++;
3260 }
3261 }
3262
3263 i40e_flush(hw);
3264}
3265
3266/**
3267 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3268 * @hw: ptr to the hardware info
3269 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003270static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003271{
Jacob Kellerab437b52014-12-14 01:55:08 +00003272 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273 u32 val;
3274
3275 /* clear things first */
3276 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3277 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3278
3279 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3280 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3281 I40E_PFINT_ICR0_ENA_GRST_MASK |
3282 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3283 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003284 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3285 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3286 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3287
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003288 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3289 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3290
Jacob Kellerab437b52014-12-14 01:55:08 +00003291 if (pf->flags & I40E_FLAG_PTP)
3292 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3293
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003294 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3295
3296 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003297 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3298 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003299
3300 /* OTHER_ITR_IDX = 0 */
3301 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3302}
3303
3304/**
3305 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3306 * @vsi: the VSI being configured
3307 **/
3308static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3309{
Alexander Duyck493fb302013-09-28 07:01:44 +00003310 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003311 struct i40e_pf *pf = vsi->back;
3312 struct i40e_hw *hw = &pf->hw;
3313 u32 val;
3314
3315 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003316 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003317 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003318 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3319 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003320 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003321 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3322 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3323
Jacob Kellerab437b52014-12-14 01:55:08 +00003324 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003325
3326 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3327 wr32(hw, I40E_PFINT_LNKLST0, 0);
3328
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003329 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003330 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3331 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3332 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3333
3334 wr32(hw, I40E_QINT_RQCTL(0), val);
3335
3336 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3337 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3338 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3339
3340 wr32(hw, I40E_QINT_TQCTL(0), val);
3341 i40e_flush(hw);
3342}
3343
3344/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003345 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3346 * @pf: board private structure
3347 **/
3348void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3349{
3350 struct i40e_hw *hw = &pf->hw;
3351
3352 wr32(hw, I40E_PFINT_DYN_CTL0,
3353 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3354 i40e_flush(hw);
3355}
3356
3357/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003358 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3359 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003360 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003361 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003362void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003363{
3364 struct i40e_hw *hw = &pf->hw;
3365 u32 val;
3366
3367 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003368 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003369 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3370
3371 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3372 i40e_flush(hw);
3373}
3374
3375/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003376 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3377 * @irq: interrupt number
3378 * @data: pointer to a q_vector
3379 **/
3380static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3381{
3382 struct i40e_q_vector *q_vector = data;
3383
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003384 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003385 return IRQ_HANDLED;
3386
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003387 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003388
3389 return IRQ_HANDLED;
3390}
3391
3392/**
Alan Brady96db7762016-09-14 16:24:38 -07003393 * i40e_irq_affinity_notify - Callback for affinity changes
3394 * @notify: context as to what irq was changed
3395 * @mask: the new affinity mask
3396 *
3397 * This is a callback function used by the irq_set_affinity_notifier function
3398 * so that we may register to receive changes to the irq affinity masks.
3399 **/
3400static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3401 const cpumask_t *mask)
3402{
3403 struct i40e_q_vector *q_vector =
3404 container_of(notify, struct i40e_q_vector, affinity_notify);
3405
3406 q_vector->affinity_mask = *mask;
3407}
3408
3409/**
3410 * i40e_irq_affinity_release - Callback for affinity notifier release
3411 * @ref: internal core kernel usage
3412 *
3413 * This is a callback function used by the irq_set_affinity_notifier function
3414 * to inform the current notification subscriber that they will no longer
3415 * receive notifications.
3416 **/
3417static void i40e_irq_affinity_release(struct kref *ref) {}
3418
3419/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003420 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3421 * @vsi: the VSI being configured
3422 * @basename: name for the vector
3423 *
3424 * Allocates MSI-X vectors and requests interrupts from the kernel.
3425 **/
3426static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3427{
3428 int q_vectors = vsi->num_q_vectors;
3429 struct i40e_pf *pf = vsi->back;
3430 int base = vsi->base_vector;
3431 int rx_int_idx = 0;
3432 int tx_int_idx = 0;
3433 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003434 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003435
3436 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003437 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003438
Alan Brady96db7762016-09-14 16:24:38 -07003439 irq_num = pf->msix_entries[base + vector].vector;
3440
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003441 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003442 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3443 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3444 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003445 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003446 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3447 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003448 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003449 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3450 "%s-%s-%d", basename, "tx", tx_int_idx++);
3451 } else {
3452 /* skip this unused q_vector */
3453 continue;
3454 }
Alan Brady96db7762016-09-14 16:24:38 -07003455 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003456 vsi->irq_handler,
3457 0,
3458 q_vector->name,
3459 q_vector);
3460 if (err) {
3461 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003462 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003463 goto free_queue_irqs;
3464 }
Alan Brady96db7762016-09-14 16:24:38 -07003465
3466 /* register for affinity change notifications */
3467 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3468 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3469 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003470 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003471 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003472 }
3473
Shannon Nelson63741842014-04-23 04:50:16 +00003474 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003475 return 0;
3476
3477free_queue_irqs:
3478 while (vector) {
3479 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003480 irq_num = pf->msix_entries[base + vector].vector;
3481 irq_set_affinity_notifier(irq_num, NULL);
3482 irq_set_affinity_hint(irq_num, NULL);
3483 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003484 }
3485 return err;
3486}
3487
3488/**
3489 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3490 * @vsi: the VSI being un-configured
3491 **/
3492static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3493{
3494 struct i40e_pf *pf = vsi->back;
3495 struct i40e_hw *hw = &pf->hw;
3496 int base = vsi->base_vector;
3497 int i;
3498
3499 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003500 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3501 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003502 }
3503
3504 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3505 for (i = vsi->base_vector;
3506 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3507 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3508
3509 i40e_flush(hw);
3510 for (i = 0; i < vsi->num_q_vectors; i++)
3511 synchronize_irq(pf->msix_entries[i + base].vector);
3512 } else {
3513 /* Legacy and MSI mode - this stops all interrupt handling */
3514 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3515 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3516 i40e_flush(hw);
3517 synchronize_irq(pf->pdev->irq);
3518 }
3519}
3520
3521/**
3522 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3523 * @vsi: the VSI being configured
3524 **/
3525static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3526{
3527 struct i40e_pf *pf = vsi->back;
3528 int i;
3529
3530 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003531 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003532 i40e_irq_dynamic_enable(vsi, i);
3533 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003534 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003535 }
3536
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003537 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003538 return 0;
3539}
3540
3541/**
3542 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3543 * @pf: board private structure
3544 **/
3545static void i40e_stop_misc_vector(struct i40e_pf *pf)
3546{
3547 /* Disable ICR 0 */
3548 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3549 i40e_flush(&pf->hw);
3550}
3551
3552/**
3553 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3554 * @irq: interrupt number
3555 * @data: pointer to a q_vector
3556 *
3557 * This is the handler used for all MSI/Legacy interrupts, and deals
3558 * with both queue and non-queue interrupts. This is also used in
3559 * MSIX mode to handle the non-queue interrupts.
3560 **/
3561static irqreturn_t i40e_intr(int irq, void *data)
3562{
3563 struct i40e_pf *pf = (struct i40e_pf *)data;
3564 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003565 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003566 u32 icr0, icr0_remaining;
3567 u32 val, ena_mask;
3568
3569 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003570 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003571
Shannon Nelson116a57d2013-09-28 07:13:59 +00003572 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3573 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003574 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003575
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003576 /* if interrupt but no bits showing, must be SWINT */
3577 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3578 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3579 pf->sw_int_count++;
3580
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003581 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3582 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3583 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3584 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003585 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003586 }
3587
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003588 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3589 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003590 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3591 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003592
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003593 /* We do not have a way to disarm Queue causes while leaving
3594 * interrupt enabled for all other causes, ideally
3595 * interrupt should be disabled while we are in NAPI but
3596 * this is not a performance path and napi_schedule()
3597 * can deal with rescheduling.
3598 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003599 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003600 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003601 }
3602
3603 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3604 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3605 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003606 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003607 }
3608
3609 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3610 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3611 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3612 }
3613
3614 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3615 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3616 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3617 }
3618
3619 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3620 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3621 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3622 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3623 val = rd32(hw, I40E_GLGEN_RSTAT);
3624 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3625 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003626 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003627 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003628 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003629 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003630 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003631 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003632 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003633 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003634 }
3635
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003636 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3637 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3638 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003639 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3640 rd32(hw, I40E_PFHMC_ERRORINFO),
3641 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003642 }
3643
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003644 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3645 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3646
3647 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003648 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003649 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003650 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003651 }
3652
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003653 /* If a critical error is pending we have no choice but to reset the
3654 * device.
3655 * Report and mask out any remaining unexpected interrupts.
3656 */
3657 icr0_remaining = icr0 & ena_mask;
3658 if (icr0_remaining) {
3659 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3660 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003661 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003662 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003663 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003664 dev_info(&pf->pdev->dev, "device will be reset\n");
3665 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3666 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003667 }
3668 ena_mask &= ~icr0_remaining;
3669 }
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003670 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003671
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003672enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673 /* re-enable interrupt causes */
3674 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 if (!test_bit(__I40E_DOWN, &pf->state)) {
3676 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003677 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003678 }
3679
Anjali Singhai Jain5e8230662013-12-18 13:45:49 +00003680 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003681}
3682
3683/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003684 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3685 * @tx_ring: tx ring to clean
3686 * @budget: how many cleans we're allowed
3687 *
3688 * Returns true if there's any budget left (e.g. the clean is finished)
3689 **/
3690static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3691{
3692 struct i40e_vsi *vsi = tx_ring->vsi;
3693 u16 i = tx_ring->next_to_clean;
3694 struct i40e_tx_buffer *tx_buf;
3695 struct i40e_tx_desc *tx_desc;
3696
3697 tx_buf = &tx_ring->tx_bi[i];
3698 tx_desc = I40E_TX_DESC(tx_ring, i);
3699 i -= tx_ring->count;
3700
3701 do {
3702 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3703
3704 /* if next_to_watch is not set then there is no work pending */
3705 if (!eop_desc)
3706 break;
3707
3708 /* prevent any other reads prior to eop_desc */
3709 read_barrier_depends();
3710
3711 /* if the descriptor isn't done, no work yet to do */
3712 if (!(eop_desc->cmd_type_offset_bsz &
3713 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3714 break;
3715
3716 /* clear next_to_watch to prevent false hangs */
3717 tx_buf->next_to_watch = NULL;
3718
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003719 tx_desc->buffer_addr = 0;
3720 tx_desc->cmd_type_offset_bsz = 0;
3721 /* move past filter desc */
3722 tx_buf++;
3723 tx_desc++;
3724 i++;
3725 if (unlikely(!i)) {
3726 i -= tx_ring->count;
3727 tx_buf = tx_ring->tx_bi;
3728 tx_desc = I40E_TX_DESC(tx_ring, 0);
3729 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003730 /* unmap skb header data */
3731 dma_unmap_single(tx_ring->dev,
3732 dma_unmap_addr(tx_buf, dma),
3733 dma_unmap_len(tx_buf, len),
3734 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003735 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3736 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003737
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003738 tx_buf->raw_buf = NULL;
3739 tx_buf->tx_flags = 0;
3740 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003741 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003742 tx_desc->buffer_addr = 0;
3743 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003744
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003745 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003746 tx_buf++;
3747 tx_desc++;
3748 i++;
3749 if (unlikely(!i)) {
3750 i -= tx_ring->count;
3751 tx_buf = tx_ring->tx_bi;
3752 tx_desc = I40E_TX_DESC(tx_ring, 0);
3753 }
3754
3755 /* update budget accounting */
3756 budget--;
3757 } while (likely(budget));
3758
3759 i += tx_ring->count;
3760 tx_ring->next_to_clean = i;
3761
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003762 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003763 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003764
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003765 return budget > 0;
3766}
3767
3768/**
3769 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3770 * @irq: interrupt number
3771 * @data: pointer to a q_vector
3772 **/
3773static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3774{
3775 struct i40e_q_vector *q_vector = data;
3776 struct i40e_vsi *vsi;
3777
3778 if (!q_vector->tx.ring)
3779 return IRQ_HANDLED;
3780
3781 vsi = q_vector->tx.ring->vsi;
3782 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3783
3784 return IRQ_HANDLED;
3785}
3786
3787/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003788 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003789 * @vsi: the VSI being configured
3790 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003791 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003792 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003793static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003794{
Alexander Duyck493fb302013-09-28 07:01:44 +00003795 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003796 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3797 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003798
3799 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003800 tx_ring->next = q_vector->tx.ring;
3801 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003802 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003803
3804 rx_ring->q_vector = q_vector;
3805 rx_ring->next = q_vector->rx.ring;
3806 q_vector->rx.ring = rx_ring;
3807 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003808}
3809
3810/**
3811 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3812 * @vsi: the VSI being configured
3813 *
3814 * This function maps descriptor rings to the queue-specific vectors
3815 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3816 * one vector per queue pair, but on a constrained vector budget, we
3817 * group the queue pairs as "efficiently" as possible.
3818 **/
3819static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3820{
3821 int qp_remaining = vsi->num_queue_pairs;
3822 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003823 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003824 int v_start = 0;
3825 int qp_idx = 0;
3826
3827 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3828 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003829 * It is also important to go through all the vectors available to be
3830 * sure that if we don't use all the vectors, that the remaining vectors
3831 * are cleared. This is especially important when decreasing the
3832 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003833 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003834 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003835 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3836
3837 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3838
3839 q_vector->num_ringpairs = num_ringpairs;
3840
3841 q_vector->rx.count = 0;
3842 q_vector->tx.count = 0;
3843 q_vector->rx.ring = NULL;
3844 q_vector->tx.ring = NULL;
3845
3846 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003847 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003848 qp_idx++;
3849 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003850 }
3851 }
3852}
3853
3854/**
3855 * i40e_vsi_request_irq - Request IRQ from the OS
3856 * @vsi: the VSI being configured
3857 * @basename: name for the vector
3858 **/
3859static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3860{
3861 struct i40e_pf *pf = vsi->back;
3862 int err;
3863
3864 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3865 err = i40e_vsi_request_irq_msix(vsi, basename);
3866 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3867 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003868 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003869 else
3870 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003871 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003872
3873 if (err)
3874 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3875
3876 return err;
3877}
3878
3879#ifdef CONFIG_NET_POLL_CONTROLLER
3880/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003881 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003882 * @netdev: network interface device structure
3883 *
3884 * This is used by netconsole to send skbs without having to re-enable
3885 * interrupts. It's not called while the normal interrupt routine is executing.
3886 **/
3887static void i40e_netpoll(struct net_device *netdev)
3888{
3889 struct i40e_netdev_priv *np = netdev_priv(netdev);
3890 struct i40e_vsi *vsi = np->vsi;
3891 struct i40e_pf *pf = vsi->back;
3892 int i;
3893
3894 /* if interface is down do nothing */
3895 if (test_bit(__I40E_DOWN, &vsi->state))
3896 return;
3897
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003898 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3899 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003900 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003901 } else {
3902 i40e_intr(pf->pdev->irq, netdev);
3903 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003904}
3905#endif
3906
3907/**
Neerav Parikh23527302014-06-03 23:50:15 +00003908 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3909 * @pf: the PF being configured
3910 * @pf_q: the PF queue
3911 * @enable: enable or disable state of the queue
3912 *
3913 * This routine will wait for the given Tx queue of the PF to reach the
3914 * enabled or disabled state.
3915 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3916 * multiple retries; else will return 0 in case of success.
3917 **/
3918static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3919{
3920 int i;
3921 u32 tx_reg;
3922
3923 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3924 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3925 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3926 break;
3927
Neerav Parikhf98a2002014-09-13 07:40:44 +00003928 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003929 }
3930 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3931 return -ETIMEDOUT;
3932
3933 return 0;
3934}
3935
3936/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003937 * i40e_vsi_control_tx - Start or stop a VSI's rings
3938 * @vsi: the VSI being configured
3939 * @enable: start or stop the rings
3940 **/
3941static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3942{
3943 struct i40e_pf *pf = vsi->back;
3944 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003945 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003946 u32 tx_reg;
3947
3948 pf_q = vsi->base_queue;
3949 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003950
3951 /* warn the TX unit of coming changes */
3952 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3953 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003954 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003955
Mitch Williams6c5ef622014-02-20 19:29:16 -08003956 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003957 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003958 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3959 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3960 break;
3961 usleep_range(1000, 2000);
3962 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003963 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c1220072014-03-14 07:32:29 +00003964 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003965 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003966
3967 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003968 if (enable) {
3969 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003970 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003971 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003972 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003973 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003974
3975 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003976 /* No waiting for the Tx queue to disable */
3977 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3978 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003979
3980 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003981 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3982 if (ret) {
3983 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003984 "VSI seid %d Tx ring %d %sable timeout\n",
3985 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003986 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003987 }
3988 }
3989
Neerav Parikh23527302014-06-03 23:50:15 +00003990 return ret;
3991}
3992
3993/**
3994 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3995 * @pf: the PF being configured
3996 * @pf_q: the PF queue
3997 * @enable: enable or disable state of the queue
3998 *
3999 * This routine will wait for the given Rx queue of the PF to reach the
4000 * enabled or disabled state.
4001 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4002 * multiple retries; else will return 0 in case of success.
4003 **/
4004static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4005{
4006 int i;
4007 u32 rx_reg;
4008
4009 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4010 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4011 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4012 break;
4013
Neerav Parikhf98a2002014-09-13 07:40:44 +00004014 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004015 }
4016 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4017 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004019 return 0;
4020}
4021
4022/**
4023 * i40e_vsi_control_rx - Start or stop a VSI's rings
4024 * @vsi: the VSI being configured
4025 * @enable: start or stop the rings
4026 **/
4027static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4028{
4029 struct i40e_pf *pf = vsi->back;
4030 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004031 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004032 u32 rx_reg;
4033
4034 pf_q = vsi->base_queue;
4035 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004036 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004037 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004038 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4039 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4040 break;
4041 usleep_range(1000, 2000);
4042 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004043
Catherine Sullivan7c1220072014-03-14 07:32:29 +00004044 /* Skip if the queue is already in the requested state */
4045 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4046 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004047
4048 /* turn on/off the queue */
4049 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004050 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004051 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004052 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004053 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004054 /* No waiting for the Tx queue to disable */
4055 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4056 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004057
4058 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004059 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4060 if (ret) {
4061 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004062 "VSI seid %d Rx ring %d %sable timeout\n",
4063 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004064 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004065 }
4066 }
4067
Neerav Parikh23527302014-06-03 23:50:15 +00004068 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004069}
4070
4071/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004072 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004073 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004074 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004075int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004076{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004077 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004078
4079 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004080 ret = i40e_vsi_control_rx(vsi, true);
4081 if (ret)
4082 return ret;
4083 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004084
4085 return ret;
4086}
4087
4088/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004089 * i40e_vsi_stop_rings - Stop a VSI's rings
4090 * @vsi: the VSI being configured
4091 **/
4092void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4093{
4094 /* do rx first for enable and last for disable
4095 * Ignore return value, we need to shutdown whatever we can
4096 */
4097 i40e_vsi_control_tx(vsi, false);
4098 i40e_vsi_control_rx(vsi, false);
4099}
4100
4101/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004102 * i40e_vsi_free_irq - Free the irq association with the OS
4103 * @vsi: the VSI being configured
4104 **/
4105static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4106{
4107 struct i40e_pf *pf = vsi->back;
4108 struct i40e_hw *hw = &pf->hw;
4109 int base = vsi->base_vector;
4110 u32 val, qp;
4111 int i;
4112
4113 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4114 if (!vsi->q_vectors)
4115 return;
4116
Shannon Nelson63741842014-04-23 04:50:16 +00004117 if (!vsi->irqs_ready)
4118 return;
4119
4120 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004121 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004122 int irq_num;
4123 u16 vector;
4124
4125 vector = i + base;
4126 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004127
4128 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004129 if (!vsi->q_vectors[i] ||
4130 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004131 continue;
4132
Alan Brady96db7762016-09-14 16:24:38 -07004133 /* clear the affinity notifier in the IRQ descriptor */
4134 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004136 irq_set_affinity_hint(irq_num, NULL);
4137 synchronize_irq(irq_num);
4138 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004139
4140 /* Tear down the interrupt queue link list
4141 *
4142 * We know that they come in pairs and always
4143 * the Rx first, then the Tx. To clear the
4144 * link list, stick the EOL value into the
4145 * next_q field of the registers.
4146 */
4147 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4148 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4149 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4150 val |= I40E_QUEUE_END_OF_LIST
4151 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4152 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4153
4154 while (qp != I40E_QUEUE_END_OF_LIST) {
4155 u32 next;
4156
4157 val = rd32(hw, I40E_QINT_RQCTL(qp));
4158
4159 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4160 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4161 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4162 I40E_QINT_RQCTL_INTEVENT_MASK);
4163
4164 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4165 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4166
4167 wr32(hw, I40E_QINT_RQCTL(qp), val);
4168
4169 val = rd32(hw, I40E_QINT_TQCTL(qp));
4170
4171 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4172 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4173
4174 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4175 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4176 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4177 I40E_QINT_TQCTL_INTEVENT_MASK);
4178
4179 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4180 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4181
4182 wr32(hw, I40E_QINT_TQCTL(qp), val);
4183 qp = next;
4184 }
4185 }
4186 } else {
4187 free_irq(pf->pdev->irq, pf);
4188
4189 val = rd32(hw, I40E_PFINT_LNKLST0);
4190 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4191 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4192 val |= I40E_QUEUE_END_OF_LIST
4193 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4194 wr32(hw, I40E_PFINT_LNKLST0, val);
4195
4196 val = rd32(hw, I40E_QINT_RQCTL(qp));
4197 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4198 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4199 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4200 I40E_QINT_RQCTL_INTEVENT_MASK);
4201
4202 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4203 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4204
4205 wr32(hw, I40E_QINT_RQCTL(qp), val);
4206
4207 val = rd32(hw, I40E_QINT_TQCTL(qp));
4208
4209 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4210 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4211 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4212 I40E_QINT_TQCTL_INTEVENT_MASK);
4213
4214 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4215 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4216
4217 wr32(hw, I40E_QINT_TQCTL(qp), val);
4218 }
4219}
4220
4221/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004222 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4223 * @vsi: the VSI being configured
4224 * @v_idx: Index of vector to be freed
4225 *
4226 * This function frees the memory allocated to the q_vector. In addition if
4227 * NAPI is enabled it will delete any references to the NAPI struct prior
4228 * to freeing the q_vector.
4229 **/
4230static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4231{
4232 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004233 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004234
4235 if (!q_vector)
4236 return;
4237
4238 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004239 i40e_for_each_ring(ring, q_vector->tx)
4240 ring->q_vector = NULL;
4241
4242 i40e_for_each_ring(ring, q_vector->rx)
4243 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004244
4245 /* only VSI w/ an associated netdev is set up w/ NAPI */
4246 if (vsi->netdev)
4247 netif_napi_del(&q_vector->napi);
4248
4249 vsi->q_vectors[v_idx] = NULL;
4250
4251 kfree_rcu(q_vector, rcu);
4252}
4253
4254/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004255 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4256 * @vsi: the VSI being un-configured
4257 *
4258 * This frees the memory allocated to the q_vectors and
4259 * deletes references to the NAPI struct.
4260 **/
4261static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4262{
4263 int v_idx;
4264
Alexander Duyck493fb302013-09-28 07:01:44 +00004265 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4266 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004267}
4268
4269/**
4270 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4271 * @pf: board private structure
4272 **/
4273static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4274{
4275 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4276 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4277 pci_disable_msix(pf->pdev);
4278 kfree(pf->msix_entries);
4279 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004280 kfree(pf->irq_pile);
4281 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004282 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4283 pci_disable_msi(pf->pdev);
4284 }
4285 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4286}
4287
4288/**
4289 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4290 * @pf: board private structure
4291 *
4292 * We go through and clear interrupt specific resources and reset the structure
4293 * to pre-load conditions
4294 **/
4295static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4296{
4297 int i;
4298
Shannon Nelsone1477582015-02-21 06:44:33 +00004299 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004300 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004301 synchronize_irq(pf->msix_entries[0].vector);
4302 free_irq(pf->msix_entries[0].vector, pf);
4303 }
4304
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004305 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4306 I40E_IWARP_IRQ_PILE_ID);
4307
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004308 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004309 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004310 if (pf->vsi[i])
4311 i40e_vsi_free_q_vectors(pf->vsi[i]);
4312 i40e_reset_interrupt_capability(pf);
4313}
4314
4315/**
4316 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4317 * @vsi: the VSI being configured
4318 **/
4319static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4320{
4321 int q_idx;
4322
4323 if (!vsi->netdev)
4324 return;
4325
4326 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004327 napi_enable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004328}
4329
4330/**
4331 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4332 * @vsi: the VSI being configured
4333 **/
4334static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4335{
4336 int q_idx;
4337
4338 if (!vsi->netdev)
4339 return;
4340
4341 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
Alexander Duyck493fb302013-09-28 07:01:44 +00004342 napi_disable(&vsi->q_vectors[q_idx]->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004343}
4344
4345/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004346 * i40e_vsi_close - Shut down a VSI
4347 * @vsi: the vsi to be quelled
4348 **/
4349static void i40e_vsi_close(struct i40e_vsi *vsi)
4350{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004351 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004352 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4353 i40e_down(vsi);
4354 i40e_vsi_free_irq(vsi);
4355 i40e_vsi_free_tx_resources(vsi);
4356 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004357 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004358 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4359 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4360 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004361}
4362
4363/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004364 * i40e_quiesce_vsi - Pause a given VSI
4365 * @vsi: the VSI being paused
4366 **/
4367static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4368{
4369 if (test_bit(__I40E_DOWN, &vsi->state))
4370 return;
4371
4372 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004373 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004374 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004375 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004376 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004377}
4378
4379/**
4380 * i40e_unquiesce_vsi - Resume a given VSI
4381 * @vsi: the VSI being resumed
4382 **/
4383static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4384{
4385 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4386 return;
4387
4388 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4389 if (vsi->netdev && netif_running(vsi->netdev))
4390 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4391 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004392 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004393}
4394
4395/**
4396 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4397 * @pf: the PF
4398 **/
4399static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4400{
4401 int v;
4402
Mitch Williams505682c2014-05-20 08:01:37 +00004403 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004404 if (pf->vsi[v])
4405 i40e_quiesce_vsi(pf->vsi[v]);
4406 }
4407}
4408
4409/**
4410 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4411 * @pf: the PF
4412 **/
4413static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4414{
4415 int v;
4416
Mitch Williams505682c2014-05-20 08:01:37 +00004417 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004418 if (pf->vsi[v])
4419 i40e_unquiesce_vsi(pf->vsi[v]);
4420 }
4421}
4422
Neerav Parikh69129dc2014-11-12 00:18:46 +00004423#ifdef CONFIG_I40E_DCB
4424/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004425 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004426 * @vsi: the VSI being configured
4427 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004428 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004429 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004430static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004431{
4432 struct i40e_pf *pf = vsi->back;
4433 int i, pf_q, ret;
4434
4435 pf_q = vsi->base_queue;
4436 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4437 /* Check and wait for the disable status of the queue */
4438 ret = i40e_pf_txq_wait(pf, pf_q, false);
4439 if (ret) {
4440 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004441 "VSI seid %d Tx ring %d disable timeout\n",
4442 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004443 return ret;
4444 }
4445 }
4446
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004447 pf_q = vsi->base_queue;
4448 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4449 /* Check and wait for the disable status of the queue */
4450 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4451 if (ret) {
4452 dev_info(&pf->pdev->dev,
4453 "VSI seid %d Rx ring %d disable timeout\n",
4454 vsi->seid, pf_q);
4455 return ret;
4456 }
4457 }
4458
Neerav Parikh69129dc2014-11-12 00:18:46 +00004459 return 0;
4460}
4461
4462/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004463 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004464 * @pf: the PF
4465 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004466 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004467 * VSIs that are managed by this PF.
4468 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004469static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004470{
4471 int v, ret = 0;
4472
4473 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004474 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004475 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004476 if (ret)
4477 break;
4478 }
4479 }
4480
4481 return ret;
4482}
4483
4484#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004485
4486/**
4487 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4488 * @q_idx: TX queue number
4489 * @vsi: Pointer to VSI struct
4490 *
4491 * This function checks specified queue for given VSI. Detects hung condition.
4492 * Sets hung bit since it is two step process. Before next run of service task
4493 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4494 * hung condition remain unchanged and during subsequent run, this function
4495 * issues SW interrupt to recover from hung condition.
4496 **/
4497static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4498{
4499 struct i40e_ring *tx_ring = NULL;
4500 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004501 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004502 int i;
4503
4504 pf = vsi->back;
4505
4506 /* now that we have an index, find the tx_ring struct */
4507 for (i = 0; i < vsi->num_queue_pairs; i++) {
4508 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4509 if (q_idx == vsi->tx_rings[i]->queue_index) {
4510 tx_ring = vsi->tx_rings[i];
4511 break;
4512 }
4513 }
4514 }
4515
4516 if (!tx_ring)
4517 return;
4518
4519 /* Read interrupt register */
4520 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4521 val = rd32(&pf->hw,
4522 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4523 tx_ring->vsi->base_vector - 1));
4524 else
4525 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4526
4527 head = i40e_get_head(tx_ring);
4528
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004529 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004530
Kiran Patil9c6c1252015-11-06 15:26:02 -08004531 /* HW is done executing descriptors, updated HEAD write back,
4532 * but SW hasn't processed those descriptors. If interrupt is
4533 * not generated from this point ON, it could result into
4534 * dev_watchdog detecting timeout on those netdev_queue,
4535 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004536 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004537 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004538 /* NAPI Poll didn't run and clear since it was set */
4539 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4540 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004541 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",
4542 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004543 tx_ring->next_to_clean, head,
4544 tx_ring->next_to_use,
4545 readl(tx_ring->tail));
4546 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4547 vsi->seid, q_idx, val);
4548 i40e_force_wb(vsi, tx_ring->q_vector);
4549 } else {
4550 /* First Chance - detected possible hung */
4551 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4552 &tx_ring->q_vector->hung_detected);
4553 }
4554 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004555
4556 /* This is the case where we have interrupts missing,
4557 * so the tx_pending in HW will most likely be 0, but we
4558 * will have tx_pending in SW since the WB happened but the
4559 * interrupt got lost.
4560 */
4561 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4562 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004563 local_bh_disable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004564 if (napi_reschedule(&tx_ring->q_vector->napi))
4565 tx_ring->tx_stats.tx_lost_interrupt++;
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004566 local_bh_enable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004567 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004568}
4569
4570/**
4571 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4572 * @pf: pointer to PF struct
4573 *
4574 * LAN VSI has netdev and netdev has TX queues. This function is to check
4575 * each of those TX queues if they are hung, trigger recovery by issuing
4576 * SW interrupt.
4577 **/
4578static void i40e_detect_recover_hung(struct i40e_pf *pf)
4579{
4580 struct net_device *netdev;
4581 struct i40e_vsi *vsi;
4582 int i;
4583
4584 /* Only for LAN VSI */
4585 vsi = pf->vsi[pf->lan_vsi];
4586
4587 if (!vsi)
4588 return;
4589
4590 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4591 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4592 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4593 return;
4594
4595 /* Make sure type is MAIN VSI */
4596 if (vsi->type != I40E_VSI_MAIN)
4597 return;
4598
4599 netdev = vsi->netdev;
4600 if (!netdev)
4601 return;
4602
4603 /* Bail out if netif_carrier is not OK */
4604 if (!netif_carrier_ok(netdev))
4605 return;
4606
4607 /* Go thru' TX queues for netdev */
4608 for (i = 0; i < netdev->num_tx_queues; i++) {
4609 struct netdev_queue *q;
4610
4611 q = netdev_get_tx_queue(netdev, i);
4612 if (q)
4613 i40e_detect_recover_hung_queue(i, vsi);
4614 }
4615}
4616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004617/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004618 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004619 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004620 *
4621 * Get TC map for ISCSI PF type that will include iSCSI TC
4622 * and LAN TC.
4623 **/
4624static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4625{
4626 struct i40e_dcb_app_priority_table app;
4627 struct i40e_hw *hw = &pf->hw;
4628 u8 enabled_tc = 1; /* TC0 is always enabled */
4629 u8 tc, i;
4630 /* Get the iSCSI APP TLV */
4631 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4632
4633 for (i = 0; i < dcbcfg->numapps; i++) {
4634 app = dcbcfg->app[i];
4635 if (app.selector == I40E_APP_SEL_TCPIP &&
4636 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4637 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004638 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004639 break;
4640 }
4641 }
4642
4643 return enabled_tc;
4644}
4645
4646/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004647 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4648 * @dcbcfg: the corresponding DCBx configuration structure
4649 *
4650 * Return the number of TCs from given DCBx configuration
4651 **/
4652static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4653{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004654 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004655 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004656 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004657
4658 /* Scan the ETS Config Priority Table to find
4659 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004660 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004661 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004662 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4663 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4664
4665 /* Now scan the bitmask to check for
4666 * contiguous TCs starting with TC0
4667 */
4668 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4669 if (num_tc & BIT(i)) {
4670 if (!tc_unused) {
4671 ret++;
4672 } else {
4673 pr_err("Non-contiguous TC - Disabling DCB\n");
4674 return 1;
4675 }
4676 } else {
4677 tc_unused = 1;
4678 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004679 }
4680
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004681 /* There is always at least TC0 */
4682 if (!ret)
4683 ret = 1;
4684
4685 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004686}
4687
4688/**
4689 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4690 * @dcbcfg: the corresponding DCBx configuration structure
4691 *
4692 * Query the current DCB configuration and return the number of
4693 * traffic classes enabled from the given DCBX config
4694 **/
4695static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4696{
4697 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4698 u8 enabled_tc = 1;
4699 u8 i;
4700
4701 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004702 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004703
4704 return enabled_tc;
4705}
4706
4707/**
4708 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4709 * @pf: PF being queried
4710 *
4711 * Return number of traffic classes enabled for the given PF
4712 **/
4713static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4714{
4715 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004716 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004717 u8 num_tc = 0;
4718 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4719
4720 /* If DCB is not enabled then always in single TC */
4721 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4722 return 1;
4723
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004724 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004725 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4726 return i40e_dcb_get_num_tc(dcbcfg);
4727
4728 /* MFP mode return count of enabled TCs for this PF */
4729 if (pf->hw.func_caps.iscsi)
4730 enabled_tc = i40e_get_iscsi_tc_map(pf);
4731 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004732 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004733
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004734 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004735 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004736 num_tc++;
4737 }
4738 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004739}
4740
4741/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004742 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4743 * @pf: PF being queried
4744 *
4745 * Return a bitmap for enabled traffic classes for this PF.
4746 **/
4747static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4748{
4749 /* If DCB is not enabled for this PF then just return default TC */
4750 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004751 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004752
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004753 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004754 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4755 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4756
Neerav Parikhfc51de92015-02-24 06:58:53 +00004757 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004758 if (pf->hw.func_caps.iscsi)
4759 return i40e_get_iscsi_tc_map(pf);
4760 else
David Ertmanea6acb72016-09-20 07:10:50 -07004761 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004762}
4763
4764/**
4765 * i40e_vsi_get_bw_info - Query VSI BW Information
4766 * @vsi: the VSI being queried
4767 *
4768 * Returns 0 on success, negative value on failure
4769 **/
4770static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4771{
4772 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4773 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4774 struct i40e_pf *pf = vsi->back;
4775 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004776 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004777 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778 int i;
4779
4780 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004781 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4782 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004783 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004784 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4785 i40e_stat_str(&pf->hw, ret),
4786 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004787 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004788 }
4789
4790 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004791 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4792 NULL);
4793 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004794 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004795 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4796 i40e_stat_str(&pf->hw, ret),
4797 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004798 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004799 }
4800
4801 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4802 dev_info(&pf->pdev->dev,
4803 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4804 bw_config.tc_valid_bits,
4805 bw_ets_config.tc_valid_bits);
4806 /* Still continuing */
4807 }
4808
4809 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4810 vsi->bw_max_quanta = bw_config.max_bw;
4811 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4812 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4813 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4814 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4815 vsi->bw_ets_limit_credits[i] =
4816 le16_to_cpu(bw_ets_config.credits[i]);
4817 /* 3 bits out of 4 for each TC */
4818 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4819 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004820
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004821 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004822}
4823
4824/**
4825 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4826 * @vsi: the VSI being configured
4827 * @enabled_tc: TC bitmap
4828 * @bw_credits: BW shared credits per TC
4829 *
4830 * Returns 0 on success, negative value on failure
4831 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004832static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004833 u8 *bw_share)
4834{
4835 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004836 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004837 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838
4839 bw_data.tc_valid_bits = enabled_tc;
4840 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4841 bw_data.tc_bw_credits[i] = bw_share[i];
4842
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004843 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4844 NULL);
4845 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004846 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004847 "AQ command Config VSI BW allocation per TC failed = %d\n",
4848 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004849 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004850 }
4851
4852 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4853 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4854
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004855 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004856}
4857
4858/**
4859 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4860 * @vsi: the VSI being configured
4861 * @enabled_tc: TC map to be enabled
4862 *
4863 **/
4864static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4865{
4866 struct net_device *netdev = vsi->netdev;
4867 struct i40e_pf *pf = vsi->back;
4868 struct i40e_hw *hw = &pf->hw;
4869 u8 netdev_tc = 0;
4870 int i;
4871 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4872
4873 if (!netdev)
4874 return;
4875
4876 if (!enabled_tc) {
4877 netdev_reset_tc(netdev);
4878 return;
4879 }
4880
4881 /* Set up actual enabled TCs on the VSI */
4882 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4883 return;
4884
4885 /* set per TC queues for the VSI */
4886 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4887 /* Only set TC queues for enabled tcs
4888 *
4889 * e.g. For a VSI that has TC0 and TC3 enabled the
4890 * enabled_tc bitmap would be 0x00001001; the driver
4891 * will set the numtc for netdev as 2 that will be
4892 * referenced by the netdev layer as TC 0 and 1.
4893 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004894 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004895 netdev_set_tc_queue(netdev,
4896 vsi->tc_config.tc_info[i].netdev_tc,
4897 vsi->tc_config.tc_info[i].qcount,
4898 vsi->tc_config.tc_info[i].qoffset);
4899 }
4900
4901 /* Assign UP2TC map for the VSI */
4902 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4903 /* Get the actual TC# for the UP */
4904 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4905 /* Get the mapped netdev TC# for the UP */
4906 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4907 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4908 }
4909}
4910
4911/**
4912 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4913 * @vsi: the VSI being configured
4914 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4915 **/
4916static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4917 struct i40e_vsi_context *ctxt)
4918{
4919 /* copy just the sections touched not the entire info
4920 * since not all sections are valid as returned by
4921 * update vsi params
4922 */
4923 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4924 memcpy(&vsi->info.queue_mapping,
4925 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4926 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4927 sizeof(vsi->info.tc_mapping));
4928}
4929
4930/**
4931 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4932 * @vsi: VSI to be configured
4933 * @enabled_tc: TC bitmap
4934 *
4935 * This configures a particular VSI for TCs that are mapped to the
4936 * given TC bitmap. It uses default bandwidth share for TCs across
4937 * VSIs to configure TC for a particular VSI.
4938 *
4939 * NOTE:
4940 * It is expected that the VSI queues have been quisced before calling
4941 * this function.
4942 **/
4943static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4944{
4945 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4946 struct i40e_vsi_context ctxt;
4947 int ret = 0;
4948 int i;
4949
4950 /* Check if enabled_tc is same as existing or new TCs */
4951 if (vsi->tc_config.enabled_tc == enabled_tc)
4952 return ret;
4953
4954 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4955 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004956 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004957 bw_share[i] = 1;
4958 }
4959
4960 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4961 if (ret) {
4962 dev_info(&vsi->back->pdev->dev,
4963 "Failed configuring TC map %d for VSI %d\n",
4964 enabled_tc, vsi->seid);
4965 goto out;
4966 }
4967
4968 /* Update Queue Pairs Mapping for currently enabled UPs */
4969 ctxt.seid = vsi->seid;
4970 ctxt.pf_num = vsi->back->hw.pf_id;
4971 ctxt.vf_num = 0;
4972 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004973 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004974 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4975
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004976 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4977 ctxt.info.valid_sections |=
4978 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4979 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4980 }
4981
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004982 /* Update the VSI after updating the VSI queue-mapping information */
4983 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4984 if (ret) {
4985 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004986 "Update vsi tc config failed, err %s aq_err %s\n",
4987 i40e_stat_str(&vsi->back->hw, ret),
4988 i40e_aq_str(&vsi->back->hw,
4989 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004990 goto out;
4991 }
4992 /* update the local VSI info with updated queue map */
4993 i40e_vsi_update_queue_map(vsi, &ctxt);
4994 vsi->info.valid_sections = 0;
4995
4996 /* Update current VSI BW information */
4997 ret = i40e_vsi_get_bw_info(vsi);
4998 if (ret) {
4999 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005000 "Failed updating vsi bw info, err %s aq_err %s\n",
5001 i40e_stat_str(&vsi->back->hw, ret),
5002 i40e_aq_str(&vsi->back->hw,
5003 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005004 goto out;
5005 }
5006
5007 /* Update the netdev TC setup */
5008 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5009out:
5010 return ret;
5011}
5012
5013/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005014 * i40e_veb_config_tc - Configure TCs for given VEB
5015 * @veb: given VEB
5016 * @enabled_tc: TC bitmap
5017 *
5018 * Configures given TC bitmap for VEB (switching) element
5019 **/
5020int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5021{
5022 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5023 struct i40e_pf *pf = veb->pf;
5024 int ret = 0;
5025 int i;
5026
5027 /* No TCs or already enabled TCs just return */
5028 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5029 return ret;
5030
5031 bw_data.tc_valid_bits = enabled_tc;
5032 /* bw_data.absolute_credits is not set (relative) */
5033
5034 /* Enable ETS TCs with equal BW Share for now */
5035 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005036 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005037 bw_data.tc_bw_share_credits[i] = 1;
5038 }
5039
5040 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5041 &bw_data, NULL);
5042 if (ret) {
5043 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005044 "VEB bw config failed, err %s aq_err %s\n",
5045 i40e_stat_str(&pf->hw, ret),
5046 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005047 goto out;
5048 }
5049
5050 /* Update the BW information */
5051 ret = i40e_veb_get_bw_info(veb);
5052 if (ret) {
5053 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005054 "Failed getting veb bw config, err %s aq_err %s\n",
5055 i40e_stat_str(&pf->hw, ret),
5056 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005057 }
5058
5059out:
5060 return ret;
5061}
5062
5063#ifdef CONFIG_I40E_DCB
5064/**
5065 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5066 * @pf: PF struct
5067 *
5068 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5069 * the caller would've quiesce all the VSIs before calling
5070 * this function
5071 **/
5072static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5073{
5074 u8 tc_map = 0;
5075 int ret;
5076 u8 v;
5077
5078 /* Enable the TCs available on PF to all VEBs */
5079 tc_map = i40e_pf_get_tc_map(pf);
5080 for (v = 0; v < I40E_MAX_VEB; v++) {
5081 if (!pf->veb[v])
5082 continue;
5083 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5084 if (ret) {
5085 dev_info(&pf->pdev->dev,
5086 "Failed configuring TC for VEB seid=%d\n",
5087 pf->veb[v]->seid);
5088 /* Will try to configure as many components */
5089 }
5090 }
5091
5092 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005093 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005094 if (!pf->vsi[v])
5095 continue;
5096
5097 /* - Enable all TCs for the LAN VSI
5098 * - For all others keep them at TC0 for now
5099 */
5100 if (v == pf->lan_vsi)
5101 tc_map = i40e_pf_get_tc_map(pf);
5102 else
David Ertmanea6acb72016-09-20 07:10:50 -07005103 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005104
5105 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5106 if (ret) {
5107 dev_info(&pf->pdev->dev,
5108 "Failed configuring TC for VSI seid=%d\n",
5109 pf->vsi[v]->seid);
5110 /* Will try to configure as many components */
5111 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005112 /* Re-configure VSI vectors based on updated TC map */
5113 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005114 if (pf->vsi[v]->netdev)
5115 i40e_dcbnl_set_all(pf->vsi[v]);
5116 }
5117 }
5118}
5119
5120/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005121 * i40e_resume_port_tx - Resume port Tx
5122 * @pf: PF struct
5123 *
5124 * Resume a port's Tx and issue a PF reset in case of failure to
5125 * resume.
5126 **/
5127static int i40e_resume_port_tx(struct i40e_pf *pf)
5128{
5129 struct i40e_hw *hw = &pf->hw;
5130 int ret;
5131
5132 ret = i40e_aq_resume_port_tx(hw, NULL);
5133 if (ret) {
5134 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005135 "Resume Port Tx failed, err %s aq_err %s\n",
5136 i40e_stat_str(&pf->hw, ret),
5137 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005138 /* Schedule PF reset to recover */
5139 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5140 i40e_service_event_schedule(pf);
5141 }
5142
5143 return ret;
5144}
5145
5146/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005147 * i40e_init_pf_dcb - Initialize DCB configuration
5148 * @pf: PF being configured
5149 *
5150 * Query the current DCB configuration and cache it
5151 * in the hardware structure
5152 **/
5153static int i40e_init_pf_dcb(struct i40e_pf *pf)
5154{
5155 struct i40e_hw *hw = &pf->hw;
5156 int err = 0;
5157
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005158 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005159 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005160 goto out;
5161
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005162 /* Get the initial DCB configuration */
5163 err = i40e_init_dcb(hw);
5164 if (!err) {
5165 /* Device/Function is not DCBX capable */
5166 if ((!hw->func_caps.dcb) ||
5167 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5168 dev_info(&pf->pdev->dev,
5169 "DCBX offload is not supported or is disabled for this PF.\n");
5170
5171 if (pf->flags & I40E_FLAG_MFP_ENABLED)
5172 goto out;
5173
5174 } else {
5175 /* When status is not DISABLED then DCBX in FW */
5176 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5177 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005178
5179 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005180 /* Enable DCB tagging only when more than one TC
5181 * or explicitly disable if only one TC
5182 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005183 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5184 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005185 else
5186 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005187 dev_dbg(&pf->pdev->dev,
5188 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005189 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005190 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005191 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005192 "Query for DCB configuration failed, err %s aq_err %s\n",
5193 i40e_stat_str(&pf->hw, err),
5194 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005195 }
5196
5197out:
5198 return err;
5199}
5200#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005201#define SPEED_SIZE 14
5202#define FC_SIZE 8
5203/**
5204 * i40e_print_link_message - print link up or down
5205 * @vsi: the VSI for which link needs a message
5206 */
Matt Jaredc156f852015-08-27 11:42:39 -04005207void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005208{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005209 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005210 char *speed = "Unknown";
5211 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005212 char *fec = "";
5213 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005214
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005215 new_speed = vsi->back->hw.phy.link_info.link_speed;
5216
5217 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005218 return;
5219 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005220 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005221 if (!isup) {
5222 netdev_info(vsi->netdev, "NIC Link is Down\n");
5223 return;
5224 }
5225
Greg Rose148c2d82014-12-11 07:06:27 +00005226 /* Warn user if link speed on NPAR enabled partition is not at
5227 * least 10GB
5228 */
5229 if (vsi->back->hw.func_caps.npar_enable &&
5230 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5231 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5232 netdev_warn(vsi->netdev,
5233 "The partition detected link speed that is less than 10Gbps\n");
5234
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005235 switch (vsi->back->hw.phy.link_info.link_speed) {
5236 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005237 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005238 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005239 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005240 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005241 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005242 case I40E_LINK_SPEED_25GB:
5243 speed = "25 G";
5244 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005245 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005246 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005247 break;
5248 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005249 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005250 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005251 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005252 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005253 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005254 default:
5255 break;
5256 }
5257
5258 switch (vsi->back->hw.fc.current_mode) {
5259 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005260 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005261 break;
5262 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005263 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005264 break;
5265 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005266 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005267 break;
5268 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005269 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005270 break;
5271 }
5272
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005273 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5274 fec = ", FEC: None";
5275 an = ", Autoneg: False";
5276
5277 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5278 an = ", Autoneg: True";
5279
5280 if (vsi->back->hw.phy.link_info.fec_info &
5281 I40E_AQ_CONFIG_FEC_KR_ENA)
5282 fec = ", FEC: CL74 FC-FEC/BASE-R";
5283 else if (vsi->back->hw.phy.link_info.fec_info &
5284 I40E_AQ_CONFIG_FEC_RS_ENA)
5285 fec = ", FEC: CL108 RS-FEC";
5286 }
5287
5288 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5289 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005290}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005291
5292/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005293 * i40e_up_complete - Finish the last steps of bringing up a connection
5294 * @vsi: the VSI being configured
5295 **/
5296static int i40e_up_complete(struct i40e_vsi *vsi)
5297{
5298 struct i40e_pf *pf = vsi->back;
5299 int err;
5300
5301 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5302 i40e_vsi_configure_msix(vsi);
5303 else
5304 i40e_configure_msi_and_legacy(vsi);
5305
5306 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005307 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005308 if (err)
5309 return err;
5310
5311 clear_bit(__I40E_DOWN, &vsi->state);
5312 i40e_napi_enable_all(vsi);
5313 i40e_vsi_enable_irq(vsi);
5314
5315 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5316 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005317 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005318 netif_tx_start_all_queues(vsi->netdev);
5319 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005320 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005321 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005322 /* need to check for qualified module here*/
5323 if ((pf->hw.phy.link_info.link_info &
5324 I40E_AQ_MEDIA_AVAILABLE) &&
5325 (!(pf->hw.phy.link_info.an_info &
5326 I40E_AQ_QUALIFIED_MODULE)))
5327 netdev_err(vsi->netdev,
5328 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005329 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005330
5331 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005332 if (vsi->type == I40E_VSI_FDIR) {
5333 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005334 pf->fd_add_err = 0;
5335 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005336 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005337 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005338
5339 /* On the next run of the service_task, notify any clients of the new
5340 * opened netdev
5341 */
5342 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005343 i40e_service_event_schedule(pf);
5344
5345 return 0;
5346}
5347
5348/**
5349 * i40e_vsi_reinit_locked - Reset the VSI
5350 * @vsi: the VSI being configured
5351 *
5352 * Rebuild the ring structs after some configuration
5353 * has changed, e.g. MTU size.
5354 **/
5355static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5356{
5357 struct i40e_pf *pf = vsi->back;
5358
5359 WARN_ON(in_interrupt());
5360 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5361 usleep_range(1000, 2000);
5362 i40e_down(vsi);
5363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005364 i40e_up(vsi);
5365 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5366}
5367
5368/**
5369 * i40e_up - Bring the connection back up after being down
5370 * @vsi: the VSI being configured
5371 **/
5372int i40e_up(struct i40e_vsi *vsi)
5373{
5374 int err;
5375
5376 err = i40e_vsi_configure(vsi);
5377 if (!err)
5378 err = i40e_up_complete(vsi);
5379
5380 return err;
5381}
5382
5383/**
5384 * i40e_down - Shutdown the connection processing
5385 * @vsi: the VSI being stopped
5386 **/
5387void i40e_down(struct i40e_vsi *vsi)
5388{
5389 int i;
5390
5391 /* It is assumed that the caller of this function
5392 * sets the vsi->state __I40E_DOWN bit.
5393 */
5394 if (vsi->netdev) {
5395 netif_carrier_off(vsi->netdev);
5396 netif_tx_disable(vsi->netdev);
5397 }
5398 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005399 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005400 i40e_napi_disable_all(vsi);
5401
5402 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005403 i40e_clean_tx_ring(vsi->tx_rings[i]);
5404 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005405 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005406
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005407}
5408
5409/**
5410 * i40e_setup_tc - configure multiple traffic classes
5411 * @netdev: net device to configure
5412 * @tc: number of traffic classes to enable
5413 **/
5414static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5415{
5416 struct i40e_netdev_priv *np = netdev_priv(netdev);
5417 struct i40e_vsi *vsi = np->vsi;
5418 struct i40e_pf *pf = vsi->back;
5419 u8 enabled_tc = 0;
5420 int ret = -EINVAL;
5421 int i;
5422
5423 /* Check if DCB enabled to continue */
5424 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5425 netdev_info(netdev, "DCB is not enabled for adapter\n");
5426 goto exit;
5427 }
5428
5429 /* Check if MFP enabled */
5430 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5431 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5432 goto exit;
5433 }
5434
5435 /* Check whether tc count is within enabled limit */
5436 if (tc > i40e_pf_get_num_tc(pf)) {
5437 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5438 goto exit;
5439 }
5440
5441 /* Generate TC map for number of tc requested */
5442 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005443 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005444
5445 /* Requesting same TC configuration as already enabled */
5446 if (enabled_tc == vsi->tc_config.enabled_tc)
5447 return 0;
5448
5449 /* Quiesce VSI queues */
5450 i40e_quiesce_vsi(vsi);
5451
5452 /* Configure VSI for enabled TCs */
5453 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5454 if (ret) {
5455 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5456 vsi->seid);
5457 goto exit;
5458 }
5459
5460 /* Unquiesce VSI */
5461 i40e_unquiesce_vsi(vsi);
5462
5463exit:
5464 return ret;
5465}
5466
John Fastabend16e5cc62016-02-16 21:16:43 -08005467static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5468 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005469{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005470 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005471 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005472
5473 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5474
5475 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005476}
5477
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005478/**
5479 * i40e_open - Called when a network interface is made active
5480 * @netdev: network interface device structure
5481 *
5482 * The open entry point is called when a network interface is made
5483 * active by the system (IFF_UP). At this point all resources needed
5484 * for transmit and receive operations are allocated, the interrupt
5485 * handler is registered with the OS, the netdev watchdog subtask is
5486 * enabled, and the stack is notified that the interface is ready.
5487 *
5488 * Returns 0 on success, negative value on failure
5489 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005490int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005491{
5492 struct i40e_netdev_priv *np = netdev_priv(netdev);
5493 struct i40e_vsi *vsi = np->vsi;
5494 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005495 int err;
5496
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005497 /* disallow open during test or if eeprom is broken */
5498 if (test_bit(__I40E_TESTING, &pf->state) ||
5499 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005500 return -EBUSY;
5501
5502 netif_carrier_off(netdev);
5503
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005504 err = i40e_vsi_open(vsi);
5505 if (err)
5506 return err;
5507
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005508 /* configure global TSO hardware offload settings */
5509 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5510 TCP_FLAG_FIN) >> 16);
5511 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5512 TCP_FLAG_FIN |
5513 TCP_FLAG_CWR) >> 16);
5514 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5515
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005516 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005517
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005518 return 0;
5519}
5520
5521/**
5522 * i40e_vsi_open -
5523 * @vsi: the VSI to open
5524 *
5525 * Finish initialization of the VSI.
5526 *
5527 * Returns 0 on success, negative value on failure
5528 **/
5529int i40e_vsi_open(struct i40e_vsi *vsi)
5530{
5531 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005532 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005533 int err;
5534
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005535 /* allocate descriptors */
5536 err = i40e_vsi_setup_tx_resources(vsi);
5537 if (err)
5538 goto err_setup_tx;
5539 err = i40e_vsi_setup_rx_resources(vsi);
5540 if (err)
5541 goto err_setup_rx;
5542
5543 err = i40e_vsi_configure(vsi);
5544 if (err)
5545 goto err_setup_rx;
5546
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005547 if (vsi->netdev) {
5548 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5549 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5550 err = i40e_vsi_request_irq(vsi, int_name);
5551 if (err)
5552 goto err_setup_rx;
5553
5554 /* Notify the stack of the actual queue counts. */
5555 err = netif_set_real_num_tx_queues(vsi->netdev,
5556 vsi->num_queue_pairs);
5557 if (err)
5558 goto err_set_queues;
5559
5560 err = netif_set_real_num_rx_queues(vsi->netdev,
5561 vsi->num_queue_pairs);
5562 if (err)
5563 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005564
5565 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005566 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005567 dev_driver_string(&pf->pdev->dev),
5568 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005569 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005570
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005571 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005572 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005573 goto err_setup_rx;
5574 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005575
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005576 err = i40e_up_complete(vsi);
5577 if (err)
5578 goto err_up_complete;
5579
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005580 return 0;
5581
5582err_up_complete:
5583 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005584err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005585 i40e_vsi_free_irq(vsi);
5586err_setup_rx:
5587 i40e_vsi_free_rx_resources(vsi);
5588err_setup_tx:
5589 i40e_vsi_free_tx_resources(vsi);
5590 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005591 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005592
5593 return err;
5594}
5595
5596/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005597 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005598 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005599 *
5600 * This function destroys the hlist where all the Flow Director
5601 * filters were saved.
5602 **/
5603static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5604{
5605 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005606 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005607 struct hlist_node *node2;
5608
5609 hlist_for_each_entry_safe(filter, node2,
5610 &pf->fdir_filter_list, fdir_node) {
5611 hlist_del(&filter->fdir_node);
5612 kfree(filter);
5613 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005614
Jacob Keller0e588de2017-02-06 14:38:50 -08005615 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5616 list_del(&pit_entry->list);
5617 kfree(pit_entry);
5618 }
5619 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5620
5621 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5622 list_del(&pit_entry->list);
5623 kfree(pit_entry);
5624 }
5625 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5626
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005627 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005628 pf->fd_tcp4_filter_cnt = 0;
5629 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005630 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005631 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005632
5633 /* Reprogram the default input set for TCP/IPv4 */
5634 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5635 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5636 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5637
5638 /* Reprogram the default input set for UDP/IPv4 */
5639 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5640 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5641 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5642
5643 /* Reprogram the default input set for SCTP/IPv4 */
5644 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5645 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5646 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5647
5648 /* Reprogram the default input set for Other/IPv4 */
5649 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5650 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005651}
5652
5653/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005654 * i40e_close - Disables a network interface
5655 * @netdev: network interface device structure
5656 *
5657 * The close entry point is called when an interface is de-activated
5658 * by the OS. The hardware is still under the driver's control, but
5659 * this netdev interface is disabled.
5660 *
5661 * Returns 0, this is not allowed to fail
5662 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005663int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005664{
5665 struct i40e_netdev_priv *np = netdev_priv(netdev);
5666 struct i40e_vsi *vsi = np->vsi;
5667
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005668 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005669
5670 return 0;
5671}
5672
5673/**
5674 * i40e_do_reset - Start a PF or Core Reset sequence
5675 * @pf: board private structure
5676 * @reset_flags: which reset is requested
5677 *
5678 * The essential difference in resets is that the PF Reset
5679 * doesn't clear the packet buffers, doesn't reset the PE
5680 * firmware, and doesn't bother the other PFs on the chip.
5681 **/
5682void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5683{
5684 u32 val;
5685
5686 WARN_ON(in_interrupt());
5687
Mitch Williams263fc482014-04-23 04:50:11 +00005688
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005689 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005690 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691
5692 /* Request a Global Reset
5693 *
5694 * This will start the chip's countdown to the actual full
5695 * chip reset event, and a warning interrupt to be sent
5696 * to all PFs, including the requestor. Our handler
5697 * for the warning interrupt will deal with the shutdown
5698 * and recovery of the switch setup.
5699 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005700 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5702 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5703 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5704
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005705 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005706
5707 /* Request a Core Reset
5708 *
5709 * Same as Global Reset, except does *not* include the MAC/PHY
5710 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005711 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005712 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5713 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5714 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5715 i40e_flush(&pf->hw);
5716
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005717 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005718
5719 /* Request a PF Reset
5720 *
5721 * Resets only the PF-specific registers
5722 *
5723 * This goes directly to the tear-down and rebuild of
5724 * the switch, since we need to do all the recovery as
5725 * for the Core Reset.
5726 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005727 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005728 i40e_handle_reset_warning(pf);
5729
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005730 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005731 int v;
5732
5733 /* Find the VSI(s) that requested a re-init */
5734 dev_info(&pf->pdev->dev,
5735 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005736 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005737 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005738
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005739 if (vsi != NULL &&
5740 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5741 i40e_vsi_reinit_locked(pf->vsi[v]);
5742 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5743 }
5744 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005745 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005746 int v;
5747
5748 /* Find the VSI(s) that needs to be brought down */
5749 dev_info(&pf->pdev->dev, "VSI down requested\n");
5750 for (v = 0; v < pf->num_alloc_vsi; v++) {
5751 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005752
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005753 if (vsi != NULL &&
5754 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5755 set_bit(__I40E_DOWN, &vsi->state);
5756 i40e_down(vsi);
5757 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5758 }
5759 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005760 } else {
5761 dev_info(&pf->pdev->dev,
5762 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005763 }
5764}
5765
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005766#ifdef CONFIG_I40E_DCB
5767/**
5768 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5769 * @pf: board private structure
5770 * @old_cfg: current DCB config
5771 * @new_cfg: new DCB config
5772 **/
5773bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5774 struct i40e_dcbx_config *old_cfg,
5775 struct i40e_dcbx_config *new_cfg)
5776{
5777 bool need_reconfig = false;
5778
5779 /* Check if ETS configuration has changed */
5780 if (memcmp(&new_cfg->etscfg,
5781 &old_cfg->etscfg,
5782 sizeof(new_cfg->etscfg))) {
5783 /* If Priority Table has changed reconfig is needed */
5784 if (memcmp(&new_cfg->etscfg.prioritytable,
5785 &old_cfg->etscfg.prioritytable,
5786 sizeof(new_cfg->etscfg.prioritytable))) {
5787 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005788 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005789 }
5790
5791 if (memcmp(&new_cfg->etscfg.tcbwtable,
5792 &old_cfg->etscfg.tcbwtable,
5793 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005794 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005795
5796 if (memcmp(&new_cfg->etscfg.tsatable,
5797 &old_cfg->etscfg.tsatable,
5798 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005799 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005800 }
5801
5802 /* Check if PFC configuration has changed */
5803 if (memcmp(&new_cfg->pfc,
5804 &old_cfg->pfc,
5805 sizeof(new_cfg->pfc))) {
5806 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005807 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005808 }
5809
5810 /* Check if APP Table has changed */
5811 if (memcmp(&new_cfg->app,
5812 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005813 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005814 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005815 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005816 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005817
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005818 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005819 return need_reconfig;
5820}
5821
5822/**
5823 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5824 * @pf: board private structure
5825 * @e: event info posted on ARQ
5826 **/
5827static int i40e_handle_lldp_event(struct i40e_pf *pf,
5828 struct i40e_arq_event_info *e)
5829{
5830 struct i40e_aqc_lldp_get_mib *mib =
5831 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5832 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005833 struct i40e_dcbx_config tmp_dcbx_cfg;
5834 bool need_reconfig = false;
5835 int ret = 0;
5836 u8 type;
5837
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005838 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005839 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005840 return ret;
5841
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005842 /* Ignore if event is not for Nearest Bridge */
5843 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5844 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005845 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005846 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5847 return ret;
5848
5849 /* Check MIB Type and return if event for Remote MIB update */
5850 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005851 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005852 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005853 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5854 /* Update the remote cached instance and return */
5855 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5856 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5857 &hw->remote_dcbx_config);
5858 goto exit;
5859 }
5860
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005861 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005862 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005863
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005864 /* Reset the old DCBx configuration data */
5865 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005866 /* Get updated DCBX data from firmware */
5867 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005868 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005869 dev_info(&pf->pdev->dev,
5870 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5871 i40e_stat_str(&pf->hw, ret),
5872 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005873 goto exit;
5874 }
5875
5876 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005877 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5878 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005879 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005880 goto exit;
5881 }
5882
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005883 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5884 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005885
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005886 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005887
5888 if (!need_reconfig)
5889 goto exit;
5890
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005891 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005892 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005893 pf->flags |= I40E_FLAG_DCB_ENABLED;
5894 else
5895 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5896
Neerav Parikh69129dc2014-11-12 00:18:46 +00005897 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005898 /* Reconfiguration needed quiesce all VSIs */
5899 i40e_pf_quiesce_all_vsi(pf);
5900
5901 /* Changes in configuration update VEB/VSI */
5902 i40e_dcb_reconfigure(pf);
5903
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005904 ret = i40e_resume_port_tx(pf);
5905
Neerav Parikh69129dc2014-11-12 00:18:46 +00005906 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005907 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005908 if (ret)
5909 goto exit;
5910
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005911 /* Wait for the PF's queues to be disabled */
5912 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005913 if (ret) {
5914 /* Schedule PF reset to recover */
5915 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5916 i40e_service_event_schedule(pf);
5917 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005918 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08005919 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
5920 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005921 }
5922
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005923exit:
5924 return ret;
5925}
5926#endif /* CONFIG_I40E_DCB */
5927
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005928/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005929 * i40e_do_reset_safe - Protected reset path for userland calls.
5930 * @pf: board private structure
5931 * @reset_flags: which reset is requested
5932 *
5933 **/
5934void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5935{
5936 rtnl_lock();
5937 i40e_do_reset(pf, reset_flags);
5938 rtnl_unlock();
5939}
5940
5941/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005942 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5943 * @pf: board private structure
5944 * @e: event info posted on ARQ
5945 *
5946 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5947 * and VF queues
5948 **/
5949static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5950 struct i40e_arq_event_info *e)
5951{
5952 struct i40e_aqc_lan_overflow *data =
5953 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5954 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5955 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5956 struct i40e_hw *hw = &pf->hw;
5957 struct i40e_vf *vf;
5958 u16 vf_id;
5959
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005960 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5961 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005962
5963 /* Queue belongs to VF, find the VF and issue VF reset */
5964 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5965 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5966 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5967 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5968 vf_id -= hw->func_caps.vf_base_id;
5969 vf = &pf->vf[vf_id];
5970 i40e_vc_notify_vf_reset(vf);
5971 /* Allow VF to process pending reset notification */
5972 msleep(20);
5973 i40e_reset_vf(vf, false);
5974 }
5975}
5976
5977/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005978 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5979 * @pf: board private structure
5980 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005981u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005982{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005983 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005984
5985 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5986 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5987 return fcnt_prog;
5988}
5989
5990/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005991 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005992 * @pf: board private structure
5993 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005994u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005995{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005996 u32 val, fcnt_prog;
5997
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005998 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5999 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6000 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6001 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6002 return fcnt_prog;
6003}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006004
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006005/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006006 * i40e_get_global_fd_count - Get total FD filters programmed on device
6007 * @pf: board private structure
6008 **/
6009u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6010{
6011 u32 val, fcnt_prog;
6012
6013 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6014 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6015 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6016 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6017 return fcnt_prog;
6018}
6019
6020/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006021 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6022 * @pf: board private structure
6023 **/
6024void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6025{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006026 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006027 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006028 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006029
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006030 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6031 return;
6032
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006033 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6034 * to re-enable
6035 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006036 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006037 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006038 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6039 (pf->fd_add_err == 0) ||
6040 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006041 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006042 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6043 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006044 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6045 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 +00006046 }
6047 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006048
6049 /* Wait for some more space to be available to turn on ATR. We also
6050 * must check that no existing ntuple rules for TCP are in effect
6051 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006052 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6053 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006054 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006055 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006056 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006057 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006058 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 +00006059 }
6060 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006061
6062 /* if hw had a problem adding a filter, delete it */
6063 if (pf->fd_inv > 0) {
6064 hlist_for_each_entry_safe(filter, node,
6065 &pf->fdir_filter_list, fdir_node) {
6066 if (filter->fd_id == pf->fd_inv) {
6067 hlist_del(&filter->fdir_node);
6068 kfree(filter);
6069 pf->fdir_pf_active_filters--;
6070 }
6071 }
6072 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006073}
6074
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006075#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006076#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006077/**
6078 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6079 * @pf: board private structure
6080 **/
6081static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6082{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006083 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006084 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006085 bool disable_atr = false;
6086 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006087 int reg;
6088
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006089 if (!time_after(jiffies, pf->fd_flush_timestamp +
6090 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6091 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006092
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006093 /* If the flush is happening too quick and we have mostly SB rules we
6094 * should not re-enable ATR for some time.
6095 */
6096 min_flush_time = pf->fd_flush_timestamp +
6097 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6098 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006099
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006100 if (!(time_after(jiffies, min_flush_time)) &&
6101 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6102 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6103 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6104 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006105 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006106
6107 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006108 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006109 /* flush all filters */
6110 wr32(&pf->hw, I40E_PFQF_CTL_1,
6111 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6112 i40e_flush(&pf->hw);
6113 pf->fd_flush_cnt++;
6114 pf->fd_add_err = 0;
6115 do {
6116 /* Check FD flush status every 5-6msec */
6117 usleep_range(5000, 6000);
6118 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6119 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6120 break;
6121 } while (flush_wait_retry--);
6122 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6123 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6124 } else {
6125 /* replay sideband filters */
6126 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006127 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006128 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006129 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6130 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6131 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6132 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006133}
6134
6135/**
6136 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6137 * @pf: board private structure
6138 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006139u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006140{
6141 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6142}
6143
6144/* We can see up to 256 filter programming desc in transit if the filters are
6145 * being applied really fast; before we see the first
6146 * filter miss error on Rx queue 0. Accumulating enough error messages before
6147 * reacting will make sure we don't cause flush too often.
6148 */
6149#define I40E_MAX_FD_PROGRAM_ERROR 256
6150
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006151/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006152 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6153 * @pf: board private structure
6154 **/
6155static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6156{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006157
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006158 /* if interface is down do nothing */
6159 if (test_bit(__I40E_DOWN, &pf->state))
6160 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006161
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006162 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006163 i40e_fdir_flush_and_replay(pf);
6164
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006165 i40e_fdir_check_and_reenable(pf);
6166
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006167}
6168
6169/**
6170 * i40e_vsi_link_event - notify VSI of a link event
6171 * @vsi: vsi to be notified
6172 * @link_up: link up or down
6173 **/
6174static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6175{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006176 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006177 return;
6178
6179 switch (vsi->type) {
6180 case I40E_VSI_MAIN:
6181 if (!vsi->netdev || !vsi->netdev_registered)
6182 break;
6183
6184 if (link_up) {
6185 netif_carrier_on(vsi->netdev);
6186 netif_tx_wake_all_queues(vsi->netdev);
6187 } else {
6188 netif_carrier_off(vsi->netdev);
6189 netif_tx_stop_all_queues(vsi->netdev);
6190 }
6191 break;
6192
6193 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006194 case I40E_VSI_VMDQ2:
6195 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006196 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006197 case I40E_VSI_MIRROR:
6198 default:
6199 /* there is no notification for other VSIs */
6200 break;
6201 }
6202}
6203
6204/**
6205 * i40e_veb_link_event - notify elements on the veb of a link event
6206 * @veb: veb to be notified
6207 * @link_up: link up or down
6208 **/
6209static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6210{
6211 struct i40e_pf *pf;
6212 int i;
6213
6214 if (!veb || !veb->pf)
6215 return;
6216 pf = veb->pf;
6217
6218 /* depth first... */
6219 for (i = 0; i < I40E_MAX_VEB; i++)
6220 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6221 i40e_veb_link_event(pf->veb[i], link_up);
6222
6223 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006224 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006225 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6226 i40e_vsi_link_event(pf->vsi[i], link_up);
6227}
6228
6229/**
6230 * i40e_link_event - Update netif_carrier status
6231 * @pf: board private structure
6232 **/
6233static void i40e_link_event(struct i40e_pf *pf)
6234{
Mitch Williams320684c2014-10-17 03:14:43 +00006235 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006236 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006237 i40e_status status;
6238 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006239
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006240 /* save off old link status information */
6241 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6242
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006243 /* set this to force the get_link_status call to refresh state */
6244 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006245
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006246 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006247
6248 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006249
6250 /* On success, disable temp link polling */
6251 if (status == I40E_SUCCESS) {
6252 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6253 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6254 } else {
6255 /* Enable link polling temporarily until i40e_get_link_status
6256 * returns I40E_SUCCESS
6257 */
6258 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006259 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6260 status);
6261 return;
6262 }
6263
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006264 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6265 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006266
6267 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006268 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006269 (test_bit(__I40E_DOWN, &vsi->state) ||
6270 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006271 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006272
6273 if (!test_bit(__I40E_DOWN, &vsi->state))
6274 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006275
6276 /* Notify the base of the switch tree connected to
6277 * the link. Floating VEBs are not notified.
6278 */
6279 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6280 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6281 else
Mitch Williams320684c2014-10-17 03:14:43 +00006282 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006283
6284 if (pf->vf)
6285 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006286
6287 if (pf->flags & I40E_FLAG_PTP)
6288 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006289}
6290
6291/**
Shannon Nelson21536712014-10-25 10:35:25 +00006292 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006293 * @pf: board private structure
6294 **/
6295static void i40e_watchdog_subtask(struct i40e_pf *pf)
6296{
6297 int i;
6298
6299 /* if interface is down do nothing */
6300 if (test_bit(__I40E_DOWN, &pf->state) ||
6301 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6302 return;
6303
Shannon Nelson21536712014-10-25 10:35:25 +00006304 /* make sure we don't do these things too often */
6305 if (time_before(jiffies, (pf->service_timer_previous +
6306 pf->service_timer_period)))
6307 return;
6308 pf->service_timer_previous = jiffies;
6309
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006310 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6311 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006312 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006313
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006314 /* Update the stats for active netdevs so the network stack
6315 * can look at updated numbers whenever it cares to
6316 */
Mitch Williams505682c2014-05-20 08:01:37 +00006317 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006318 if (pf->vsi[i] && pf->vsi[i]->netdev)
6319 i40e_update_stats(pf->vsi[i]);
6320
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006321 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6322 /* Update the stats for the active switching components */
6323 for (i = 0; i < I40E_MAX_VEB; i++)
6324 if (pf->veb[i])
6325 i40e_update_veb_stats(pf->veb[i]);
6326 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006327
6328 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006329}
6330
6331/**
6332 * i40e_reset_subtask - Set up for resetting the device and driver
6333 * @pf: board private structure
6334 **/
6335static void i40e_reset_subtask(struct i40e_pf *pf)
6336{
6337 u32 reset_flags = 0;
6338
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006339 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006340 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006341 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006342 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6343 }
6344 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006345 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006346 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6347 }
6348 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006349 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006350 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6351 }
6352 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006353 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006354 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6355 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006356 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006357 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006358 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6359 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006360
6361 /* If there's a recovery already waiting, it takes
6362 * precedence before starting a new reset sequence.
6363 */
6364 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6365 i40e_handle_reset_warning(pf);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006366 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006367 }
6368
6369 /* If we're already down or resetting, just bail */
6370 if (reset_flags &&
6371 !test_bit(__I40E_DOWN, &pf->state) &&
6372 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6373 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00006374
6375unlock:
6376 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006377}
6378
6379/**
6380 * i40e_handle_link_event - Handle link event
6381 * @pf: board private structure
6382 * @e: event info posted on ARQ
6383 **/
6384static void i40e_handle_link_event(struct i40e_pf *pf,
6385 struct i40e_arq_event_info *e)
6386{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006387 struct i40e_aqc_get_link_status *status =
6388 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006390 /* Do a new status request to re-enable LSE reporting
6391 * and load new status information into the hw struct
6392 * This completely ignores any state information
6393 * in the ARQ event info, instead choosing to always
6394 * issue the AQ update link status command.
6395 */
6396 i40e_link_event(pf);
6397
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006398 /* check for unqualified module, if link is down */
6399 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6400 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6401 (!(status->link_info & I40E_AQ_LINK_UP)))
6402 dev_err(&pf->pdev->dev,
6403 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006404}
6405
6406/**
6407 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6408 * @pf: board private structure
6409 **/
6410static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6411{
6412 struct i40e_arq_event_info event;
6413 struct i40e_hw *hw = &pf->hw;
6414 u16 pending, i = 0;
6415 i40e_status ret;
6416 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006417 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006418 u32 val;
6419
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006420 /* Do not run clean AQ when PF reset fails */
6421 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6422 return;
6423
Shannon Nelson86df2422014-05-20 08:01:35 +00006424 /* check for error indications */
6425 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6426 oldval = val;
6427 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006428 if (hw->debug_mask & I40E_DEBUG_AQ)
6429 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006430 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6431 }
6432 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006433 if (hw->debug_mask & I40E_DEBUG_AQ)
6434 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006435 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006436 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006437 }
6438 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006439 if (hw->debug_mask & I40E_DEBUG_AQ)
6440 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006441 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6442 }
6443 if (oldval != val)
6444 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6445
6446 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6447 oldval = val;
6448 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006449 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6450 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006451 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6452 }
6453 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006454 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6455 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006456 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6457 }
6458 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006459 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6460 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006461 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6462 }
6463 if (oldval != val)
6464 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6465
Mitch Williams1001dc32014-11-11 20:02:19 +00006466 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6467 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006468 if (!event.msg_buf)
6469 return;
6470
6471 do {
6472 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006473 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006474 break;
Mitch Williams56497972014-06-04 08:45:18 +00006475 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006476 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6477 break;
6478 }
6479
6480 opcode = le16_to_cpu(event.desc.opcode);
6481 switch (opcode) {
6482
6483 case i40e_aqc_opc_get_link_status:
6484 i40e_handle_link_event(pf, &event);
6485 break;
6486 case i40e_aqc_opc_send_msg_to_pf:
6487 ret = i40e_vc_process_vf_msg(pf,
6488 le16_to_cpu(event.desc.retval),
6489 le32_to_cpu(event.desc.cookie_high),
6490 le32_to_cpu(event.desc.cookie_low),
6491 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006492 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006493 break;
6494 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006495 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006496#ifdef CONFIG_I40E_DCB
6497 rtnl_lock();
6498 ret = i40e_handle_lldp_event(pf, &event);
6499 rtnl_unlock();
6500#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006501 break;
6502 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006503 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006504 i40e_handle_lan_overflow_event(pf, &event);
6505 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006506 case i40e_aqc_opc_send_msg_to_peer:
6507 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6508 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006509 case i40e_aqc_opc_nvm_erase:
6510 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006511 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006512 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6513 "ARQ NVM operation 0x%04x completed\n",
6514 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006515 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 default:
6517 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006518 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006519 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006520 break;
6521 }
6522 } while (pending && (i++ < pf->adminq_work_limit));
6523
6524 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6525 /* re-enable Admin queue interrupt cause */
6526 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6527 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6528 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6529 i40e_flush(hw);
6530
6531 kfree(event.msg_buf);
6532}
6533
6534/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006535 * i40e_verify_eeprom - make sure eeprom is good to use
6536 * @pf: board private structure
6537 **/
6538static void i40e_verify_eeprom(struct i40e_pf *pf)
6539{
6540 int err;
6541
6542 err = i40e_diag_eeprom_test(&pf->hw);
6543 if (err) {
6544 /* retry in case of garbage read */
6545 err = i40e_diag_eeprom_test(&pf->hw);
6546 if (err) {
6547 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6548 err);
6549 set_bit(__I40E_BAD_EEPROM, &pf->state);
6550 }
6551 }
6552
6553 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6554 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6555 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6556 }
6557}
6558
6559/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006560 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006561 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006562 *
6563 * enable switch loop back or die - no point in a return value
6564 **/
6565static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6566{
6567 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6568 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006569 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006570
6571 ctxt.seid = pf->main_vsi_seid;
6572 ctxt.pf_num = pf->hw.pf_id;
6573 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006574 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6575 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006576 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006577 "couldn't get PF vsi config, err %s aq_err %s\n",
6578 i40e_stat_str(&pf->hw, ret),
6579 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006580 return;
6581 }
6582 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6583 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6584 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6585
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006586 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6587 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006588 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006589 "update vsi switch failed, err %s aq_err %s\n",
6590 i40e_stat_str(&pf->hw, ret),
6591 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006592 }
6593}
6594
6595/**
6596 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006597 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006598 *
6599 * disable switch loop back or die - no point in a return value
6600 **/
6601static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6602{
6603 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6604 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006605 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006606
6607 ctxt.seid = pf->main_vsi_seid;
6608 ctxt.pf_num = pf->hw.pf_id;
6609 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006610 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6611 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006612 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006613 "couldn't get PF vsi config, err %s aq_err %s\n",
6614 i40e_stat_str(&pf->hw, ret),
6615 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006616 return;
6617 }
6618 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6619 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6620 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6621
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006622 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6623 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006624 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006625 "update vsi switch failed, err %s aq_err %s\n",
6626 i40e_stat_str(&pf->hw, ret),
6627 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006628 }
6629}
6630
6631/**
Neerav Parikh51616012015-02-06 08:52:14 +00006632 * i40e_config_bridge_mode - Configure the HW bridge mode
6633 * @veb: pointer to the bridge instance
6634 *
6635 * Configure the loop back mode for the LAN VSI that is downlink to the
6636 * specified HW bridge instance. It is expected this function is called
6637 * when a new HW bridge is instantiated.
6638 **/
6639static void i40e_config_bridge_mode(struct i40e_veb *veb)
6640{
6641 struct i40e_pf *pf = veb->pf;
6642
Shannon Nelson6dec1012015-09-28 14:12:30 -04006643 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6644 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6645 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006646 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6647 i40e_disable_pf_switch_lb(pf);
6648 else
6649 i40e_enable_pf_switch_lb(pf);
6650}
6651
6652/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006653 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6654 * @veb: pointer to the VEB instance
6655 *
6656 * This is a recursive function that first builds the attached VSIs then
6657 * recurses in to build the next layer of VEB. We track the connections
6658 * through our own index numbers because the seid's from the HW could
6659 * change across the reset.
6660 **/
6661static int i40e_reconstitute_veb(struct i40e_veb *veb)
6662{
6663 struct i40e_vsi *ctl_vsi = NULL;
6664 struct i40e_pf *pf = veb->pf;
6665 int v, veb_idx;
6666 int ret;
6667
6668 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006669 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006670 if (pf->vsi[v] &&
6671 pf->vsi[v]->veb_idx == veb->idx &&
6672 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6673 ctl_vsi = pf->vsi[v];
6674 break;
6675 }
6676 }
6677 if (!ctl_vsi) {
6678 dev_info(&pf->pdev->dev,
6679 "missing owner VSI for veb_idx %d\n", veb->idx);
6680 ret = -ENOENT;
6681 goto end_reconstitute;
6682 }
6683 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6684 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6685 ret = i40e_add_vsi(ctl_vsi);
6686 if (ret) {
6687 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006688 "rebuild of veb_idx %d owner VSI failed: %d\n",
6689 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006690 goto end_reconstitute;
6691 }
6692 i40e_vsi_reset_stats(ctl_vsi);
6693
6694 /* create the VEB in the switch and move the VSI onto the VEB */
6695 ret = i40e_add_veb(veb, ctl_vsi);
6696 if (ret)
6697 goto end_reconstitute;
6698
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006699 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6700 veb->bridge_mode = BRIDGE_MODE_VEB;
6701 else
6702 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006703 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006704
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006705 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006706 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006707 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6708 continue;
6709
6710 if (pf->vsi[v]->veb_idx == veb->idx) {
6711 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006712
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006713 vsi->uplink_seid = veb->seid;
6714 ret = i40e_add_vsi(vsi);
6715 if (ret) {
6716 dev_info(&pf->pdev->dev,
6717 "rebuild of vsi_idx %d failed: %d\n",
6718 v, ret);
6719 goto end_reconstitute;
6720 }
6721 i40e_vsi_reset_stats(vsi);
6722 }
6723 }
6724
6725 /* create any VEBs attached to this VEB - RECURSION */
6726 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6727 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6728 pf->veb[veb_idx]->uplink_seid = veb->seid;
6729 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6730 if (ret)
6731 break;
6732 }
6733 }
6734
6735end_reconstitute:
6736 return ret;
6737}
6738
6739/**
6740 * i40e_get_capabilities - get info about the HW
6741 * @pf: the PF struct
6742 **/
6743static int i40e_get_capabilities(struct i40e_pf *pf)
6744{
6745 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6746 u16 data_size;
6747 int buf_len;
6748 int err;
6749
6750 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6751 do {
6752 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6753 if (!cap_buf)
6754 return -ENOMEM;
6755
6756 /* this loads the data into the hw struct for us */
6757 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6758 &data_size,
6759 i40e_aqc_opc_list_func_capabilities,
6760 NULL);
6761 /* data loaded, buffer no longer needed */
6762 kfree(cap_buf);
6763
6764 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6765 /* retry with a larger buffer */
6766 buf_len = data_size;
6767 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6768 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006769 "capability discovery failed, err %s aq_err %s\n",
6770 i40e_stat_str(&pf->hw, err),
6771 i40e_aq_str(&pf->hw,
6772 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006773 return -ENODEV;
6774 }
6775 } while (err);
6776
6777 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6778 dev_info(&pf->pdev->dev,
6779 "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",
6780 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6781 pf->hw.func_caps.num_msix_vectors,
6782 pf->hw.func_caps.num_msix_vectors_vf,
6783 pf->hw.func_caps.fd_filters_guaranteed,
6784 pf->hw.func_caps.fd_filters_best_effort,
6785 pf->hw.func_caps.num_tx_qp,
6786 pf->hw.func_caps.num_vsis);
6787
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006788#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6789 + pf->hw.func_caps.num_vfs)
6790 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6791 dev_info(&pf->pdev->dev,
6792 "got num_vsis %d, setting num_vsis to %d\n",
6793 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6794 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6795 }
6796
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006797 return 0;
6798}
6799
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006800static int i40e_vsi_clear(struct i40e_vsi *vsi);
6801
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006802/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006803 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006804 * @pf: board private structure
6805 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006806static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006807{
6808 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006809
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006810 /* quick workaround for an NVM issue that leaves a critical register
6811 * uninitialized
6812 */
6813 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6814 static const u32 hkey[] = {
6815 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6816 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6817 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6818 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006819 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006820
6821 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6822 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6823 }
6824
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006825 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006826 return;
6827
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006828 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006829 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006830
6831 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006832 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006833 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6834 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006835 if (!vsi) {
6836 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006837 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6838 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006839 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006840 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006841
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006842 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006843}
6844
6845/**
6846 * i40e_fdir_teardown - release the Flow Director resources
6847 * @pf: board private structure
6848 **/
6849static void i40e_fdir_teardown(struct i40e_pf *pf)
6850{
Alexander Duyck4b816442016-10-11 15:26:53 -07006851 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006852
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006853 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006854 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6855 if (vsi)
6856 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006857}
6858
6859/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006860 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861 * @pf: board private structure
6862 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006863 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006864 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006865static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006866{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006867 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006868 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869 u32 v;
6870
6871 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6872 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006873 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006874 if (i40e_check_asq_alive(&pf->hw))
6875 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006876
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006877 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006878
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006879 /* quiesce the VSIs and their queues that are not already DOWN */
6880 i40e_pf_quiesce_all_vsi(pf);
6881
Mitch Williams505682c2014-05-20 08:01:37 +00006882 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006883 if (pf->vsi[v])
6884 pf->vsi[v]->seid = 0;
6885 }
6886
6887 i40e_shutdown_adminq(&pf->hw);
6888
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006889 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006890 if (hw->hmc.hmc_obj) {
6891 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006892 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006893 dev_warn(&pf->pdev->dev,
6894 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006895 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006896}
6897
6898/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006899 * i40e_send_version - update firmware with driver version
6900 * @pf: PF struct
6901 */
6902static void i40e_send_version(struct i40e_pf *pf)
6903{
6904 struct i40e_driver_version dv;
6905
6906 dv.major_version = DRV_VERSION_MAJOR;
6907 dv.minor_version = DRV_VERSION_MINOR;
6908 dv.build_version = DRV_VERSION_BUILD;
6909 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006910 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006911 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6912}
6913
6914/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006915 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006916 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006917 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006918 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006919static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006920{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006921 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006922 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006923 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006924 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006925 u32 v;
6926
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006927 /* Now we wait for GRST to settle out.
6928 * We don't have to delete the VEBs or VSIs from the hw switch
6929 * because the reset will make them disappear.
6930 */
6931 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006932 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006933 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006934 set_bit(__I40E_RESET_FAILED, &pf->state);
6935 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006936 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006937 pf->pfr_count++;
6938
6939 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006940 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006941 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006942
6943 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6944 ret = i40e_init_adminq(&pf->hw);
6945 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006946 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6947 i40e_stat_str(&pf->hw, ret),
6948 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006949 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006950 }
6951
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006952 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006953 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006954 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006955
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006956 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006957 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006958 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006961 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08006962 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006963 if (ret) {
6964 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6965 goto end_core_reset;
6966 }
6967 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6968 if (ret) {
6969 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6970 goto end_core_reset;
6971 }
6972
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006973#ifdef CONFIG_I40E_DCB
6974 ret = i40e_init_pf_dcb(pf);
6975 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006976 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6977 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6978 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006979 }
6980#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006981 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006982 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983 if (ret)
6984 goto end_core_reset;
6985
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006986 /* The driver only wants link up/down and module qualification
6987 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006988 */
6989 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006990 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006991 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006992 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006993 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006994 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6995 i40e_stat_str(&pf->hw, ret),
6996 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006997
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006998 /* make sure our flow control settings are restored */
6999 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7000 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007001 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7002 i40e_stat_str(&pf->hw, ret),
7003 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007004
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007005 /* Rebuild the VSIs and VEBs that existed before reset.
7006 * They are still in our local switch element arrays, so only
7007 * need to rebuild the switch model in the HW.
7008 *
7009 * If there were VEBs but the reconstitution failed, we'll try
7010 * try to recover minimal use by getting the basic PF VSI working.
7011 */
7012 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007013 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007014 /* find the one VEB connected to the MAC, and find orphans */
7015 for (v = 0; v < I40E_MAX_VEB; v++) {
7016 if (!pf->veb[v])
7017 continue;
7018
7019 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7020 pf->veb[v]->uplink_seid == 0) {
7021 ret = i40e_reconstitute_veb(pf->veb[v]);
7022
7023 if (!ret)
7024 continue;
7025
7026 /* If Main VEB failed, we're in deep doodoo,
7027 * so give up rebuilding the switch and set up
7028 * for minimal rebuild of PF VSI.
7029 * If orphan failed, we'll report the error
7030 * but try to keep going.
7031 */
7032 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7033 dev_info(&pf->pdev->dev,
7034 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7035 ret);
7036 pf->vsi[pf->lan_vsi]->uplink_seid
7037 = pf->mac_seid;
7038 break;
7039 } else if (pf->veb[v]->uplink_seid == 0) {
7040 dev_info(&pf->pdev->dev,
7041 "rebuild of orphan VEB failed: %d\n",
7042 ret);
7043 }
7044 }
7045 }
7046 }
7047
7048 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007049 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007050 /* no VEB, so rebuild only the Main VSI */
7051 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7052 if (ret) {
7053 dev_info(&pf->pdev->dev,
7054 "rebuild of Main VSI failed: %d\n", ret);
7055 goto end_core_reset;
7056 }
7057 }
7058
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007059 /* Reconfigure hardware for allowing smaller MSS in the case
7060 * of TSO, so that we avoid the MDD being fired and causing
7061 * a reset in the case of small MSS+TSO.
7062 */
7063#define I40E_REG_MSS 0x000E64DC
7064#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7065#define I40E_64BYTE_MSS 0x400000
7066 val = rd32(hw, I40E_REG_MSS);
7067 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7068 val &= ~I40E_REG_MSS_MIN_MASK;
7069 val |= I40E_64BYTE_MSS;
7070 wr32(hw, I40E_REG_MSS, val);
7071 }
7072
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007073 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007074 msleep(75);
7075 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7076 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007077 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7078 i40e_stat_str(&pf->hw, ret),
7079 i40e_aq_str(&pf->hw,
7080 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007081 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007082 /* reinit the misc interrupt */
7083 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7084 ret = i40e_setup_misc_vector(pf);
7085
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007086 /* Add a filter to drop all Flow control frames from any VSI from being
7087 * transmitted. By doing so we stop a malicious VF from sending out
7088 * PAUSE or PFC frames and potentially controlling traffic for other
7089 * PF/VF VSIs.
7090 * The FW can still send Flow control frames if enabled.
7091 */
7092 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7093 pf->main_vsi_seid);
7094
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007095 /* restart the VSIs that were rebuilt and running before the reset */
7096 i40e_pf_unquiesce_all_vsi(pf);
7097
Mitch Williams69f64b22014-02-13 03:48:46 -08007098 if (pf->num_alloc_vfs) {
7099 for (v = 0; v < pf->num_alloc_vfs; v++)
7100 i40e_reset_vf(&pf->vf[v], true);
7101 }
7102
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007103 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007104 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007105
7106end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007107 clear_bit(__I40E_RESET_FAILED, &pf->state);
7108clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007109 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7110}
7111
7112/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007113 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007114 * @pf: board private structure
7115 *
7116 * Close up the VFs and other things in prep for a Core Reset,
7117 * then get ready to rebuild the world.
7118 **/
7119static void i40e_handle_reset_warning(struct i40e_pf *pf)
7120{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007121 i40e_prep_for_reset(pf);
7122 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007123}
7124
7125/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007126 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007127 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007128 *
7129 * Called from the MDD irq handler to identify possibly malicious vfs
7130 **/
7131static void i40e_handle_mdd_event(struct i40e_pf *pf)
7132{
7133 struct i40e_hw *hw = &pf->hw;
7134 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007135 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007136 struct i40e_vf *vf;
7137 u32 reg;
7138 int i;
7139
7140 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7141 return;
7142
7143 /* find what triggered the MDD event */
7144 reg = rd32(hw, I40E_GL_MDET_TX);
7145 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007146 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7147 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007148 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007149 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007150 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007151 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007152 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7153 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7154 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007155 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007156 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 +00007157 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007158 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7159 mdd_detected = true;
7160 }
7161 reg = rd32(hw, I40E_GL_MDET_RX);
7162 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007163 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7164 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007165 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007166 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007167 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7168 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7169 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007170 if (netif_msg_rx_err(pf))
7171 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7172 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007173 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7174 mdd_detected = true;
7175 }
7176
Neerav Parikhdf430b12014-06-04 01:23:15 +00007177 if (mdd_detected) {
7178 reg = rd32(hw, I40E_PF_MDET_TX);
7179 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7180 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007181 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007182 pf_mdd_detected = true;
7183 }
7184 reg = rd32(hw, I40E_PF_MDET_RX);
7185 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7186 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007187 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007188 pf_mdd_detected = true;
7189 }
7190 /* Queue belongs to the PF, initiate a reset */
7191 if (pf_mdd_detected) {
7192 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7193 i40e_service_event_schedule(pf);
7194 }
7195 }
7196
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007197 /* see if one of the VFs needs its hand slapped */
7198 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7199 vf = &(pf->vf[i]);
7200 reg = rd32(hw, I40E_VP_MDET_TX(i));
7201 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7202 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7203 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007204 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7205 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007206 }
7207
7208 reg = rd32(hw, I40E_VP_MDET_RX(i));
7209 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7210 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7211 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007212 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7213 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007214 }
7215
7216 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7217 dev_info(&pf->pdev->dev,
7218 "Too many MDD events on VF %d, disabled\n", i);
7219 dev_info(&pf->pdev->dev,
7220 "Use PF Control I/F to re-enable the VF\n");
7221 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7222 }
7223 }
7224
7225 /* re-enable mdd interrupt cause */
7226 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7227 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7228 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7229 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7230 i40e_flush(hw);
7231}
7232
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007233/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007234 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007235 * @pf: board private structure
7236 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007237static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007238{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007239 struct i40e_hw *hw = &pf->hw;
7240 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007241 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007242 int i;
7243
Singhai, Anjali6a899022015-12-14 12:21:18 -08007244 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007245 return;
7246
Singhai, Anjali6a899022015-12-14 12:21:18 -08007247 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007248
7249 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007250 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7251 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7252 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007253 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007254 ret = i40e_aq_add_udp_tunnel(hw, port,
7255 pf->udp_ports[i].type,
7256 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007257 else
7258 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007259
7260 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007261 dev_dbg(&pf->pdev->dev,
7262 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7263 pf->udp_ports[i].type ? "vxlan" : "geneve",
7264 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007265 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007266 i40e_stat_str(&pf->hw, ret),
7267 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007268 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007269 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007270 }
7271 }
7272 }
7273}
7274
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007275/**
7276 * i40e_service_task - Run the driver's async subtasks
7277 * @work: pointer to work_struct containing our data
7278 **/
7279static void i40e_service_task(struct work_struct *work)
7280{
7281 struct i40e_pf *pf = container_of(work,
7282 struct i40e_pf,
7283 service_task);
7284 unsigned long start_time = jiffies;
7285
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007286 /* don't bother with service tasks if a reset is in progress */
7287 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007288 return;
7289 }
7290
Mitch Williams91089032016-11-21 13:03:51 -08007291 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7292 return;
7293
Kiran Patilb03a8c12015-09-24 18:13:15 -04007294 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007295 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007296 i40e_reset_subtask(pf);
7297 i40e_handle_mdd_event(pf);
7298 i40e_vc_process_vflr_event(pf);
7299 i40e_watchdog_subtask(pf);
7300 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007301 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7302 /* Client subtask will reopen next time through. */
7303 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7304 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7305 } else {
7306 i40e_client_subtask(pf);
7307 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7308 i40e_notify_client_of_l2_param_changes(
7309 pf->vsi[pf->lan_vsi]);
7310 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7311 }
7312 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007313 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007314 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007315 i40e_clean_adminq_subtask(pf);
7316
Mitch Williams91089032016-11-21 13:03:51 -08007317 /* flush memory to make sure state is correct before next watchdog */
7318 smp_mb__before_atomic();
7319 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007320
7321 /* If the tasks have taken longer than one timer cycle or there
7322 * is more work to be done, reschedule the service task now
7323 * rather than wait for the timer to tick again.
7324 */
7325 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7326 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7327 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7328 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7329 i40e_service_event_schedule(pf);
7330}
7331
7332/**
7333 * i40e_service_timer - timer callback
7334 * @data: pointer to PF struct
7335 **/
7336static void i40e_service_timer(unsigned long data)
7337{
7338 struct i40e_pf *pf = (struct i40e_pf *)data;
7339
7340 mod_timer(&pf->service_timer,
7341 round_jiffies(jiffies + pf->service_timer_period));
7342 i40e_service_event_schedule(pf);
7343}
7344
7345/**
7346 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7347 * @vsi: the VSI being configured
7348 **/
7349static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7350{
7351 struct i40e_pf *pf = vsi->back;
7352
7353 switch (vsi->type) {
7354 case I40E_VSI_MAIN:
7355 vsi->alloc_queue_pairs = pf->num_lan_qps;
7356 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7357 I40E_REQ_DESCRIPTOR_MULTIPLE);
7358 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7359 vsi->num_q_vectors = pf->num_lan_msix;
7360 else
7361 vsi->num_q_vectors = 1;
7362
7363 break;
7364
7365 case I40E_VSI_FDIR:
7366 vsi->alloc_queue_pairs = 1;
7367 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7368 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007369 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007370 break;
7371
7372 case I40E_VSI_VMDQ2:
7373 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7374 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7375 I40E_REQ_DESCRIPTOR_MULTIPLE);
7376 vsi->num_q_vectors = pf->num_vmdq_msix;
7377 break;
7378
7379 case I40E_VSI_SRIOV:
7380 vsi->alloc_queue_pairs = pf->num_vf_qps;
7381 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7382 I40E_REQ_DESCRIPTOR_MULTIPLE);
7383 break;
7384
7385 default:
7386 WARN_ON(1);
7387 return -ENODATA;
7388 }
7389
7390 return 0;
7391}
7392
7393/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007394 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7395 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007396 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007397 *
7398 * On error: returns error code (negative)
7399 * On success: returns 0
7400 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007401static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007402{
7403 int size;
7404 int ret = 0;
7405
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007406 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007407 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7408 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7409 if (!vsi->tx_rings)
7410 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007411 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7412
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007413 if (alloc_qvectors) {
7414 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007415 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007416 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7417 if (!vsi->q_vectors) {
7418 ret = -ENOMEM;
7419 goto err_vectors;
7420 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007421 }
7422 return ret;
7423
7424err_vectors:
7425 kfree(vsi->tx_rings);
7426 return ret;
7427}
7428
7429/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007430 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7431 * @pf: board private structure
7432 * @type: type of VSI
7433 *
7434 * On error: returns error code (negative)
7435 * On success: returns vsi index in PF (positive)
7436 **/
7437static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7438{
7439 int ret = -ENODEV;
7440 struct i40e_vsi *vsi;
7441 int vsi_idx;
7442 int i;
7443
7444 /* Need to protect the allocation of the VSIs at the PF level */
7445 mutex_lock(&pf->switch_mutex);
7446
7447 /* VSI list may be fragmented if VSI creation/destruction has
7448 * been happening. We can afford to do a quick scan to look
7449 * for any free VSIs in the list.
7450 *
7451 * find next empty vsi slot, looping back around if necessary
7452 */
7453 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007454 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007455 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007456 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007457 i = 0;
7458 while (i < pf->next_vsi && pf->vsi[i])
7459 i++;
7460 }
7461
Mitch Williams505682c2014-05-20 08:01:37 +00007462 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007463 vsi_idx = i; /* Found one! */
7464 } else {
7465 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007466 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007467 }
7468 pf->next_vsi = ++i;
7469
7470 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7471 if (!vsi) {
7472 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007473 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007474 }
7475 vsi->type = type;
7476 vsi->back = pf;
7477 set_bit(__I40E_DOWN, &vsi->state);
7478 vsi->flags = 0;
7479 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007480 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007481 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7482 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007483 vsi->netdev_registered = false;
7484 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007485 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007486 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007487
Alexander Duyck9f65e152013-09-28 06:00:58 +00007488 ret = i40e_set_num_rings_in_vsi(vsi);
7489 if (ret)
7490 goto err_rings;
7491
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007492 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007493 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007494 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007495
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007496 /* Setup default MSIX irq handler for VSI */
7497 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7498
Kiran Patil21659032015-09-30 14:09:03 -04007499 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007500 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501 pf->vsi[vsi_idx] = vsi;
7502 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007503 goto unlock_pf;
7504
Alexander Duyck9f65e152013-09-28 06:00:58 +00007505err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007506 pf->next_vsi = i - 1;
7507 kfree(vsi);
7508unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007509 mutex_unlock(&pf->switch_mutex);
7510 return ret;
7511}
7512
7513/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007514 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7515 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007516 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007517 *
7518 * On error: returns error code (negative)
7519 * On success: returns 0
7520 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007521static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007522{
7523 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007524 if (free_qvectors) {
7525 kfree(vsi->q_vectors);
7526 vsi->q_vectors = NULL;
7527 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007528 kfree(vsi->tx_rings);
7529 vsi->tx_rings = NULL;
7530 vsi->rx_rings = NULL;
7531}
7532
7533/**
Helin Zhang28c58692015-10-26 19:44:27 -04007534 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7535 * and lookup table
7536 * @vsi: Pointer to VSI structure
7537 */
7538static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7539{
7540 if (!vsi)
7541 return;
7542
7543 kfree(vsi->rss_hkey_user);
7544 vsi->rss_hkey_user = NULL;
7545
7546 kfree(vsi->rss_lut_user);
7547 vsi->rss_lut_user = NULL;
7548}
7549
7550/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007551 * i40e_vsi_clear - Deallocate the VSI provided
7552 * @vsi: the VSI being un-configured
7553 **/
7554static int i40e_vsi_clear(struct i40e_vsi *vsi)
7555{
7556 struct i40e_pf *pf;
7557
7558 if (!vsi)
7559 return 0;
7560
7561 if (!vsi->back)
7562 goto free_vsi;
7563 pf = vsi->back;
7564
7565 mutex_lock(&pf->switch_mutex);
7566 if (!pf->vsi[vsi->idx]) {
7567 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7568 vsi->idx, vsi->idx, vsi, vsi->type);
7569 goto unlock_vsi;
7570 }
7571
7572 if (pf->vsi[vsi->idx] != vsi) {
7573 dev_err(&pf->pdev->dev,
7574 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7575 pf->vsi[vsi->idx]->idx,
7576 pf->vsi[vsi->idx],
7577 pf->vsi[vsi->idx]->type,
7578 vsi->idx, vsi, vsi->type);
7579 goto unlock_vsi;
7580 }
7581
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007582 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007583 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7584 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7585
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007586 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007587 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007589 pf->vsi[vsi->idx] = NULL;
7590 if (vsi->idx < pf->next_vsi)
7591 pf->next_vsi = vsi->idx;
7592
7593unlock_vsi:
7594 mutex_unlock(&pf->switch_mutex);
7595free_vsi:
7596 kfree(vsi);
7597
7598 return 0;
7599}
7600
7601/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007602 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7603 * @vsi: the VSI being cleaned
7604 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007605static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007606{
7607 int i;
7608
Greg Rose8e9dca52013-12-18 13:45:53 +00007609 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007610 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007611 kfree_rcu(vsi->tx_rings[i], rcu);
7612 vsi->tx_rings[i] = NULL;
7613 vsi->rx_rings[i] = NULL;
7614 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007615 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007616}
7617
7618/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007619 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7620 * @vsi: the VSI being configured
7621 **/
7622static int i40e_alloc_rings(struct i40e_vsi *vsi)
7623{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007624 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007625 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007626 int i;
7627
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007628 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007629 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007630 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007631 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7632 if (!tx_ring)
7633 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007634
7635 tx_ring->queue_index = i;
7636 tx_ring->reg_idx = vsi->base_queue + i;
7637 tx_ring->ring_active = false;
7638 tx_ring->vsi = vsi;
7639 tx_ring->netdev = vsi->netdev;
7640 tx_ring->dev = &pf->pdev->dev;
7641 tx_ring->count = vsi->num_desc;
7642 tx_ring->size = 0;
7643 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007644 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7645 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007646 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007647 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007648
Alexander Duyck9f65e152013-09-28 06:00:58 +00007649 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007650 rx_ring->queue_index = i;
7651 rx_ring->reg_idx = vsi->base_queue + i;
7652 rx_ring->ring_active = false;
7653 rx_ring->vsi = vsi;
7654 rx_ring->netdev = vsi->netdev;
7655 rx_ring->dev = &pf->pdev->dev;
7656 rx_ring->count = vsi->num_desc;
7657 rx_ring->size = 0;
7658 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007659 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007660 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007661 }
7662
7663 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007664
7665err_out:
7666 i40e_vsi_clear_rings(vsi);
7667 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007668}
7669
7670/**
7671 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7672 * @pf: board private structure
7673 * @vectors: the number of MSI-X vectors to request
7674 *
7675 * Returns the number of vectors reserved, or error
7676 **/
7677static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7678{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007679 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7680 I40E_MIN_MSIX, vectors);
7681 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007683 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007684 vectors = 0;
7685 }
7686
7687 return vectors;
7688}
7689
7690/**
7691 * i40e_init_msix - Setup the MSIX capability
7692 * @pf: board private structure
7693 *
7694 * Work with the OS to set up the MSIX vectors needed.
7695 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007696 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007697 **/
7698static int i40e_init_msix(struct i40e_pf *pf)
7699{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007700 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007701 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007702 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007703 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007704 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007705 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007706
7707 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7708 return -ENODEV;
7709
7710 /* The number of vectors we'll request will be comprised of:
7711 * - Add 1 for "other" cause for Admin Queue events, etc.
7712 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007713 * - Queues being used for RSS.
7714 * We don't need as many as max_rss_size vectors.
7715 * use rss_size instead in the calculation since that
7716 * is governed by number of cpus in the system.
7717 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007718 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007719 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007720 * Once we count this up, try the request.
7721 *
7722 * If we can't get what we want, we'll simplify to nearly nothing
7723 * and try again. If that still fails, we punt.
7724 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007725 vectors_left = hw->func_caps.num_msix_vectors;
7726 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007727
Shannon Nelson1e200e42015-02-27 09:15:24 +00007728 /* reserve one vector for miscellaneous handler */
7729 if (vectors_left) {
7730 v_budget++;
7731 vectors_left--;
7732 }
7733
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007734 /* reserve some vectors for the main PF traffic queues. Initially we
7735 * only reserve at most 50% of the available vectors, in the case that
7736 * the number of online CPUs is large. This ensures that we can enable
7737 * extra features as well. Once we've enabled the other features, we
7738 * will use any remaining vectors to reach as close as we can to the
7739 * number of online CPUs.
7740 */
7741 cpus = num_online_cpus();
7742 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007743 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007744
7745 /* reserve one vector for sideband flow director */
7746 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7747 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007748 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007749 v_budget++;
7750 vectors_left--;
7751 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007752 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007753 }
7754 }
John W Linville83840e42015-01-14 03:06:28 +00007755
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007756 /* can we reserve enough for iWARP? */
7757 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007758 iwarp_requested = pf->num_iwarp_msix;
7759
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007760 if (!vectors_left)
7761 pf->num_iwarp_msix = 0;
7762 else if (vectors_left < pf->num_iwarp_msix)
7763 pf->num_iwarp_msix = 1;
7764 v_budget += pf->num_iwarp_msix;
7765 vectors_left -= pf->num_iwarp_msix;
7766 }
7767
Shannon Nelson1e200e42015-02-27 09:15:24 +00007768 /* any vectors left over go for VMDq support */
7769 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7770 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7771 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7772
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007773 if (!vectors_left) {
7774 pf->num_vmdq_msix = 0;
7775 pf->num_vmdq_qps = 0;
7776 } else {
7777 /* if we're short on vectors for what's desired, we limit
7778 * the queues per vmdq. If this is still more than are
7779 * available, the user will need to change the number of
7780 * queues/vectors used by the PF later with the ethtool
7781 * channels command
7782 */
7783 if (vmdq_vecs < vmdq_vecs_wanted)
7784 pf->num_vmdq_qps = 1;
7785 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007786
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007787 v_budget += vmdq_vecs;
7788 vectors_left -= vmdq_vecs;
7789 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007790 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007791
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007792 /* On systems with a large number of SMP cores, we previously limited
7793 * the number of vectors for num_lan_msix to be at most 50% of the
7794 * available vectors, to allow for other features. Now, we add back
7795 * the remaining vectors. However, we ensure that the total
7796 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7797 * calculate the number of vectors we can add without going over the
7798 * cap of CPUs. For systems with a small number of CPUs this will be
7799 * zero.
7800 */
7801 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7802 pf->num_lan_msix += extra_vectors;
7803 vectors_left -= extra_vectors;
7804
7805 WARN(vectors_left < 0,
7806 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7807
7808 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007809 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7810 GFP_KERNEL);
7811 if (!pf->msix_entries)
7812 return -ENOMEM;
7813
7814 for (i = 0; i < v_budget; i++)
7815 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007816 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007817
Shannon Nelson3b444392015-02-26 16:15:57 +00007818 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007819 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7820 kfree(pf->msix_entries);
7821 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007822 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007823 return -ENODEV;
7824
Shannon Nelson3b444392015-02-26 16:15:57 +00007825 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007826 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007827 pf->num_vmdq_vsis = 0;
7828 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007829 pf->num_lan_qps = 1;
7830 pf->num_lan_msix = 1;
7831
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007832 } else if (!vectors_left) {
7833 /* If we have limited resources, we will start with no vectors
7834 * for the special features and then allocate vectors to some
7835 * of these features based on the policy and at the end disable
7836 * the features that did not get any vectors.
7837 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007838 int vec;
7839
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007840 dev_info(&pf->pdev->dev,
7841 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007842 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007843 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007844
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007845 /* Scale vector usage down */
7846 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007847 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007848 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007849
7850 /* partition out the remaining vectors */
7851 switch (vec) {
7852 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007853 pf->num_lan_msix = 1;
7854 break;
7855 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007856 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7857 pf->num_lan_msix = 1;
7858 pf->num_iwarp_msix = 1;
7859 } else {
7860 pf->num_lan_msix = 2;
7861 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007862 break;
7863 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007864 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7865 pf->num_iwarp_msix = min_t(int, (vec / 3),
7866 iwarp_requested);
7867 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7868 I40E_DEFAULT_NUM_VMDQ_VSI);
7869 } else {
7870 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7871 I40E_DEFAULT_NUM_VMDQ_VSI);
7872 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007873 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7874 pf->num_fdsb_msix = 1;
7875 vec--;
7876 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007877 pf->num_lan_msix = min_t(int,
7878 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7879 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007880 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007881 break;
7882 }
7883 }
7884
Stefan Assmannabd97a92016-09-19 13:37:51 +02007885 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7886 (pf->num_fdsb_msix == 0)) {
7887 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7888 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7889 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007890 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7891 (pf->num_vmdq_msix == 0)) {
7892 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7893 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7894 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007895
7896 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7897 (pf->num_iwarp_msix == 0)) {
7898 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7899 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7900 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007901 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7902 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7903 pf->num_lan_msix,
7904 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7905 pf->num_fdsb_msix,
7906 pf->num_iwarp_msix);
7907
Shannon Nelson3b444392015-02-26 16:15:57 +00007908 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007909}
7910
7911/**
Greg Rose90e04072014-03-06 08:59:57 +00007912 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007913 * @vsi: the VSI being configured
7914 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007915 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007916 *
7917 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7918 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007919static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007920{
7921 struct i40e_q_vector *q_vector;
7922
7923 /* allocate q_vector */
7924 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7925 if (!q_vector)
7926 return -ENOMEM;
7927
7928 q_vector->vsi = vsi;
7929 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007930 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7931
Alexander Duyck493fb302013-09-28 07:01:44 +00007932 if (vsi->netdev)
7933 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007934 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007935
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007936 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7937 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7938
Alexander Duyck493fb302013-09-28 07:01:44 +00007939 /* tie q_vector and vsi together */
7940 vsi->q_vectors[v_idx] = q_vector;
7941
7942 return 0;
7943}
7944
7945/**
Greg Rose90e04072014-03-06 08:59:57 +00007946 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007947 * @vsi: the VSI being configured
7948 *
7949 * We allocate one q_vector per queue interrupt. If allocation fails we
7950 * return -ENOMEM.
7951 **/
Greg Rose90e04072014-03-06 08:59:57 +00007952static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007953{
7954 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007955 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007956
7957 /* if not MSIX, give the one vector only to the LAN VSI */
7958 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7959 num_q_vectors = vsi->num_q_vectors;
7960 else if (vsi == pf->vsi[pf->lan_vsi])
7961 num_q_vectors = 1;
7962 else
7963 return -EINVAL;
7964
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007965 current_cpu = cpumask_first(cpu_online_mask);
7966
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007967 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007968 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007969 if (err)
7970 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007971 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7972 if (unlikely(current_cpu >= nr_cpu_ids))
7973 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007974 }
7975
7976 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007977
7978err_out:
7979 while (v_idx--)
7980 i40e_free_q_vector(vsi, v_idx);
7981
7982 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007983}
7984
7985/**
7986 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
7987 * @pf: board private structure to initialize
7988 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04007989static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007990{
Shannon Nelson3b444392015-02-26 16:15:57 +00007991 int vectors = 0;
7992 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007993
7994 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00007995 vectors = i40e_init_msix(pf);
7996 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007997 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007998 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08007999 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008000 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008001 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008002 I40E_FLAG_SRIOV_ENABLED |
8003 I40E_FLAG_FD_SB_ENABLED |
8004 I40E_FLAG_FD_ATR_ENABLED |
8005 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008006
8007 /* rework the queue expectations without MSIX */
8008 i40e_determine_queue_usage(pf);
8009 }
8010 }
8011
8012 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8013 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008014 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008015 vectors = pci_enable_msi(pf->pdev);
8016 if (vectors < 0) {
8017 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8018 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008019 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8020 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008021 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008022 }
8023
Shannon Nelson958a3e32013-09-28 07:13:28 +00008024 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008025 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008026
Shannon Nelson3b444392015-02-26 16:15:57 +00008027 /* set up vector assignment tracking */
8028 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8029 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008030 if (!pf->irq_pile) {
8031 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8032 return -ENOMEM;
8033 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008034 pf->irq_pile->num_entries = vectors;
8035 pf->irq_pile->search_hint = 0;
8036
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008037 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008038 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008039
8040 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008041}
8042
8043/**
8044 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8045 * @pf: board private structure
8046 *
8047 * This sets up the handler for MSIX 0, which is used to manage the
8048 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8049 * when in MSI or Legacy interrupt mode.
8050 **/
8051static int i40e_setup_misc_vector(struct i40e_pf *pf)
8052{
8053 struct i40e_hw *hw = &pf->hw;
8054 int err = 0;
8055
8056 /* Only request the irq if this is the first time through, and
8057 * not when we're rebuilding after a Reset
8058 */
8059 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8060 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008061 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008062 if (err) {
8063 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008064 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008065 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008066 return -EFAULT;
8067 }
8068 }
8069
Jacob Kellerab437b52014-12-14 01:55:08 +00008070 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008071
8072 /* associate no queues to the misc vector */
8073 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8074 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8075
8076 i40e_flush(hw);
8077
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008078 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008079
8080 return err;
8081}
8082
8083/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008084 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8085 * @vsi: vsi structure
8086 * @seed: RSS hash seed
8087 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008088static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8089 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008090{
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008091 struct i40e_pf *pf = vsi->back;
8092 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008093 int ret = 0;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008094
Jacob Keller776b2e12016-07-19 16:23:30 -07008095 if (seed) {
8096 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8097 (struct i40e_aqc_get_set_rss_key_data *)seed;
8098 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8099 if (ret) {
8100 dev_info(&pf->pdev->dev,
8101 "Cannot set RSS key, err %s aq_err %s\n",
8102 i40e_stat_str(hw, ret),
8103 i40e_aq_str(hw, hw->aq.asq_last_status));
8104 return ret;
8105 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008106 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008107 if (lut) {
8108 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008109
Jacob Keller776b2e12016-07-19 16:23:30 -07008110 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8111 if (ret) {
8112 dev_info(&pf->pdev->dev,
8113 "Cannot set RSS lut, err %s aq_err %s\n",
8114 i40e_stat_str(hw, ret),
8115 i40e_aq_str(hw, hw->aq.asq_last_status));
8116 return ret;
8117 }
8118 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008119 return ret;
8120}
8121
8122/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008123 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8124 * @vsi: Pointer to vsi structure
8125 * @seed: Buffter to store the hash keys
8126 * @lut: Buffer to store the lookup table entries
8127 * @lut_size: Size of buffer to store the lookup table entries
8128 *
8129 * Return 0 on success, negative on failure
8130 */
8131static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8132 u8 *lut, u16 lut_size)
8133{
8134 struct i40e_pf *pf = vsi->back;
8135 struct i40e_hw *hw = &pf->hw;
8136 int ret = 0;
8137
8138 if (seed) {
8139 ret = i40e_aq_get_rss_key(hw, vsi->id,
8140 (struct i40e_aqc_get_set_rss_key_data *)seed);
8141 if (ret) {
8142 dev_info(&pf->pdev->dev,
8143 "Cannot get RSS key, err %s aq_err %s\n",
8144 i40e_stat_str(&pf->hw, ret),
8145 i40e_aq_str(&pf->hw,
8146 pf->hw.aq.asq_last_status));
8147 return ret;
8148 }
8149 }
8150
8151 if (lut) {
8152 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8153
8154 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8155 if (ret) {
8156 dev_info(&pf->pdev->dev,
8157 "Cannot get RSS lut, err %s aq_err %s\n",
8158 i40e_stat_str(&pf->hw, ret),
8159 i40e_aq_str(&pf->hw,
8160 pf->hw.aq.asq_last_status));
8161 return ret;
8162 }
8163 }
8164
8165 return ret;
8166}
8167
8168/**
Jacob Keller0582b962016-07-19 16:23:29 -07008169 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8170 * @vsi: VSI structure
8171 **/
8172static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8173{
8174 u8 seed[I40E_HKEY_ARRAY_SIZE];
8175 struct i40e_pf *pf = vsi->back;
8176 u8 *lut;
8177 int ret;
8178
8179 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8180 return 0;
8181
Jacob Keller552b9962016-07-19 16:23:31 -07008182 if (!vsi->rss_size)
8183 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8184 vsi->num_queue_pairs);
8185 if (!vsi->rss_size)
8186 return -EINVAL;
8187
Jacob Keller0582b962016-07-19 16:23:29 -07008188 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8189 if (!lut)
8190 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008191 /* Use the user configured hash keys and lookup table if there is one,
8192 * otherwise use default
8193 */
8194 if (vsi->rss_lut_user)
8195 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8196 else
8197 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8198 if (vsi->rss_hkey_user)
8199 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8200 else
8201 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008202 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8203 kfree(lut);
8204
8205 return ret;
8206}
8207
8208/**
Helin Zhang043dd652015-10-21 19:56:23 -04008209 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008210 * @vsi: Pointer to vsi structure
8211 * @seed: RSS hash seed
8212 * @lut: Lookup table
8213 * @lut_size: Lookup table size
8214 *
8215 * Returns 0 on success, negative on failure
8216 **/
8217static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8218 const u8 *lut, u16 lut_size)
8219{
8220 struct i40e_pf *pf = vsi->back;
8221 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008222 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008223 u8 i;
8224
8225 /* Fill out hash function seed */
8226 if (seed) {
8227 u32 *seed_dw = (u32 *)seed;
8228
Mitch Williamsc4e18682016-04-12 08:30:40 -07008229 if (vsi->type == I40E_VSI_MAIN) {
8230 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008231 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008232 } else if (vsi->type == I40E_VSI_SRIOV) {
8233 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008234 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008235 } else {
8236 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8237 }
Helin Zhange69ff812015-10-21 19:56:22 -04008238 }
8239
8240 if (lut) {
8241 u32 *lut_dw = (u32 *)lut;
8242
Mitch Williamsc4e18682016-04-12 08:30:40 -07008243 if (vsi->type == I40E_VSI_MAIN) {
8244 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8245 return -EINVAL;
8246 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8247 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8248 } else if (vsi->type == I40E_VSI_SRIOV) {
8249 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8250 return -EINVAL;
8251 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008252 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008253 } else {
8254 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8255 }
Helin Zhange69ff812015-10-21 19:56:22 -04008256 }
8257 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008258
8259 return 0;
8260}
8261
8262/**
Helin Zhang043dd652015-10-21 19:56:23 -04008263 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8264 * @vsi: Pointer to VSI structure
8265 * @seed: Buffer to store the keys
8266 * @lut: Buffer to store the lookup table entries
8267 * @lut_size: Size of buffer to store the lookup table entries
8268 *
8269 * Returns 0 on success, negative on failure
8270 */
8271static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8272 u8 *lut, u16 lut_size)
8273{
8274 struct i40e_pf *pf = vsi->back;
8275 struct i40e_hw *hw = &pf->hw;
8276 u16 i;
8277
8278 if (seed) {
8279 u32 *seed_dw = (u32 *)seed;
8280
8281 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008282 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008283 }
8284 if (lut) {
8285 u32 *lut_dw = (u32 *)lut;
8286
8287 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8288 return -EINVAL;
8289 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8290 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8291 }
8292
8293 return 0;
8294}
8295
8296/**
8297 * i40e_config_rss - Configure RSS keys and lut
8298 * @vsi: Pointer to VSI structure
8299 * @seed: RSS hash seed
8300 * @lut: Lookup table
8301 * @lut_size: Lookup table size
8302 *
8303 * Returns 0 on success, negative on failure
8304 */
8305int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8306{
8307 struct i40e_pf *pf = vsi->back;
8308
8309 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8310 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8311 else
8312 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8313}
8314
8315/**
8316 * i40e_get_rss - Get RSS keys and lut
8317 * @vsi: Pointer to VSI structure
8318 * @seed: Buffer to store the keys
8319 * @lut: Buffer to store the lookup table entries
8320 * lut_size: Size of buffer to store the lookup table entries
8321 *
8322 * Returns 0 on success, negative on failure
8323 */
8324int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8325{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008326 struct i40e_pf *pf = vsi->back;
8327
8328 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8329 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8330 else
8331 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008332}
8333
8334/**
Helin Zhange69ff812015-10-21 19:56:22 -04008335 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8336 * @pf: Pointer to board private structure
8337 * @lut: Lookup table
8338 * @rss_table_size: Lookup table size
8339 * @rss_size: Range of queue number for hashing
8340 */
Alan Bradyf1582352016-08-24 11:33:46 -07008341void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8342 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008343{
Helin Zhange69ff812015-10-21 19:56:22 -04008344 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008345
Helin Zhange69ff812015-10-21 19:56:22 -04008346 for (i = 0; i < rss_table_size; i++)
8347 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008348}
8349
8350/**
Helin Zhang043dd652015-10-21 19:56:23 -04008351 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008352 * @pf: board private structure
8353 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008354static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008355{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008356 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008357 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008358 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008359 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008360 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008361 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008362 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008364 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008365 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8366 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008367 hena |= i40e_pf_get_default_rss_hena(pf);
8368
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008369 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8370 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008371
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008372 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008373 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008374 reg_val = (pf->rss_table_size == 512) ?
8375 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8376 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008377 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008378
Helin Zhang28c58692015-10-26 19:44:27 -04008379 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008380 if (!vsi->rss_size) {
8381 u16 qcount;
8382
8383 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8384 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8385 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008386 if (!vsi->rss_size)
8387 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008388
Helin Zhange69ff812015-10-21 19:56:22 -04008389 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8390 if (!lut)
8391 return -ENOMEM;
8392
Helin Zhang28c58692015-10-26 19:44:27 -04008393 /* Use user configured lut if there is one, otherwise use default */
8394 if (vsi->rss_lut_user)
8395 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8396 else
8397 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008398
Helin Zhang28c58692015-10-26 19:44:27 -04008399 /* Use user configured hash key if there is one, otherwise
8400 * use default.
8401 */
8402 if (vsi->rss_hkey_user)
8403 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8404 else
8405 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008406 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008407 kfree(lut);
8408
8409 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008410}
8411
8412/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008413 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8414 * @pf: board private structure
8415 * @queue_count: the requested queue count for rss.
8416 *
8417 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8418 * count which may be different from the requested queue count.
8419 **/
8420int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8421{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008422 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8423 int new_rss_size;
8424
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008425 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8426 return 0;
8427
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008428 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008429
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008430 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008431 u16 qcount;
8432
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008433 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008434 i40e_prep_for_reset(pf);
8435
Helin Zhangacd65442015-10-26 19:44:28 -04008436 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008437
8438 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008439
8440 /* Discard the user configured hash keys and lut, if less
8441 * queues are enabled.
8442 */
8443 if (queue_count < vsi->rss_size) {
8444 i40e_clear_rss_config_user(vsi);
8445 dev_dbg(&pf->pdev->dev,
8446 "discard user configured hash keys and lut\n");
8447 }
8448
8449 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008450 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8451 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008452
Helin Zhang043dd652015-10-21 19:56:23 -04008453 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008454 }
Lihong Yang12815052016-09-27 11:28:48 -07008455 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8456 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008457 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008458}
8459
8460/**
Greg Rosef4492db2015-02-06 08:52:12 +00008461 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8462 * @pf: board private structure
8463 **/
8464i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8465{
8466 i40e_status status;
8467 bool min_valid, max_valid;
8468 u32 max_bw, min_bw;
8469
8470 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8471 &min_valid, &max_valid);
8472
8473 if (!status) {
8474 if (min_valid)
8475 pf->npar_min_bw = min_bw;
8476 if (max_valid)
8477 pf->npar_max_bw = max_bw;
8478 }
8479
8480 return status;
8481}
8482
8483/**
8484 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8485 * @pf: board private structure
8486 **/
8487i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8488{
8489 struct i40e_aqc_configure_partition_bw_data bw_data;
8490 i40e_status status;
8491
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008492 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008493 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008494 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8495 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8496
8497 /* Set the new bandwidths */
8498 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8499
8500 return status;
8501}
8502
8503/**
8504 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8505 * @pf: board private structure
8506 **/
8507i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8508{
8509 /* Commit temporary BW setting to permanent NVM image */
8510 enum i40e_admin_queue_err last_aq_status;
8511 i40e_status ret;
8512 u16 nvm_word;
8513
8514 if (pf->hw.partition_id != 1) {
8515 dev_info(&pf->pdev->dev,
8516 "Commit BW only works on partition 1! This is partition %d",
8517 pf->hw.partition_id);
8518 ret = I40E_NOT_SUPPORTED;
8519 goto bw_commit_out;
8520 }
8521
8522 /* Acquire NVM for read access */
8523 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8524 last_aq_status = pf->hw.aq.asq_last_status;
8525 if (ret) {
8526 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008527 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8528 i40e_stat_str(&pf->hw, ret),
8529 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008530 goto bw_commit_out;
8531 }
8532
8533 /* Read word 0x10 of NVM - SW compatibility word 1 */
8534 ret = i40e_aq_read_nvm(&pf->hw,
8535 I40E_SR_NVM_CONTROL_WORD,
8536 0x10, sizeof(nvm_word), &nvm_word,
8537 false, NULL);
8538 /* Save off last admin queue command status before releasing
8539 * the NVM
8540 */
8541 last_aq_status = pf->hw.aq.asq_last_status;
8542 i40e_release_nvm(&pf->hw);
8543 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008544 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8545 i40e_stat_str(&pf->hw, ret),
8546 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008547 goto bw_commit_out;
8548 }
8549
8550 /* Wait a bit for NVM release to complete */
8551 msleep(50);
8552
8553 /* Acquire NVM for write access */
8554 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8555 last_aq_status = pf->hw.aq.asq_last_status;
8556 if (ret) {
8557 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008558 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8559 i40e_stat_str(&pf->hw, ret),
8560 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008561 goto bw_commit_out;
8562 }
8563 /* Write it back out unchanged to initiate update NVM,
8564 * which will force a write of the shadow (alt) RAM to
8565 * the NVM - thus storing the bandwidth values permanently.
8566 */
8567 ret = i40e_aq_update_nvm(&pf->hw,
8568 I40E_SR_NVM_CONTROL_WORD,
8569 0x10, sizeof(nvm_word),
8570 &nvm_word, true, NULL);
8571 /* Save off last admin queue command status before releasing
8572 * the NVM
8573 */
8574 last_aq_status = pf->hw.aq.asq_last_status;
8575 i40e_release_nvm(&pf->hw);
8576 if (ret)
8577 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008578 "BW settings NOT SAVED, err %s aq_err %s\n",
8579 i40e_stat_str(&pf->hw, ret),
8580 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008581bw_commit_out:
8582
8583 return ret;
8584}
8585
8586/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008587 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8588 * @pf: board private structure to initialize
8589 *
8590 * i40e_sw_init initializes the Adapter private data structure.
8591 * Fields are initialized based on PCI device information and
8592 * OS network device settings (MTU size).
8593 **/
8594static int i40e_sw_init(struct i40e_pf *pf)
8595{
8596 int err = 0;
8597 int size;
8598
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008599 /* Set default capability flags */
8600 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8601 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008602 I40E_FLAG_MSIX_ENABLED;
8603
Mitch Williamsca99eb92014-04-04 04:43:07 +00008604 /* Set default ITR */
8605 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8606 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8607
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008608 /* Depending on PF configurations, it is possible that the RSS
8609 * maximum might end up larger than the available queues
8610 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008611 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008612 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008613 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008614 pf->rss_size_max = min_t(int, pf->rss_size_max,
8615 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008616 if (pf->hw.func_caps.rss) {
8617 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008618 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8619 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008620 }
8621
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008622 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008623 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008624 pf->flags |= I40E_FLAG_MFP_ENABLED;
8625 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008626 if (i40e_get_npar_bw_setting(pf))
8627 dev_warn(&pf->pdev->dev,
8628 "Could not get NPAR bw settings\n");
8629 else
8630 dev_info(&pf->pdev->dev,
8631 "Min BW = %8.8x, Max BW = %8.8x\n",
8632 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008633 }
8634
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008635 /* FW/NVM is not yet fixed in this regard */
8636 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8637 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8638 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8639 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008640 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8641 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008642 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008643 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008644 else
8645 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008646 pf->fdir_pf_filter_count =
8647 pf->hw.func_caps.fd_filters_guaranteed;
8648 pf->hw.fdir_shared_filter_count =
8649 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008650 }
8651
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008652 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008653 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008654 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008655 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008656 /* No DCB support for FW < v4.33 */
8657 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8658 }
8659
8660 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008661 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008662 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8663 (pf->hw.aq.fw_maj_ver < 4)))
8664 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8665
8666 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008667 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008668 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8669 (pf->hw.aq.fw_maj_ver >= 5)))
8670 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008671
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008672 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008673 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008674 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008675 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008676 }
8677
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008678 if (pf->hw.func_caps.iwarp) {
8679 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8680 /* IWARP needs one extra vector for CQP just like MISC.*/
8681 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8682 }
8683
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008684#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008685 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008686 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8687 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8688 pf->num_req_vfs = min_t(int,
8689 pf->hw.func_caps.num_vfs,
8690 I40E_MAX_VF_COUNT);
8691 }
8692#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008693 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008694 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8695 | I40E_FLAG_128_QP_RSS_CAPABLE
8696 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8697 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8698 | I40E_FLAG_WB_ON_ITR_CAPABLE
8699 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8700 | I40E_FLAG_NO_PCI_LINK_CHECK
8701 | I40E_FLAG_USE_SET_LLDP_MIB
8702 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8703 | I40E_FLAG_PTP_L4_CAPABLE
8704 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008705 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8706 ((pf->hw.aq.api_maj_ver == 1) &&
8707 (pf->hw.aq.api_min_ver > 4))) {
8708 /* Supported in FW API version higher than 1.4 */
8709 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008710 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008711 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008712 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008713 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008714
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008715 pf->eeprom_version = 0xDEAD;
8716 pf->lan_veb = I40E_NO_VEB;
8717 pf->lan_vsi = I40E_NO_VSI;
8718
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008719 /* By default FW has this off for performance reasons */
8720 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8721
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008722 /* set up queue assignment tracking */
8723 size = sizeof(struct i40e_lump_tracking)
8724 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8725 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8726 if (!pf->qp_pile) {
8727 err = -ENOMEM;
8728 goto sw_init_done;
8729 }
8730 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8731 pf->qp_pile->search_hint = 0;
8732
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008733 pf->tx_timeout_recovery_level = 1;
8734
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008735 mutex_init(&pf->switch_mutex);
8736
Greg Rosec668a122015-02-26 16:10:39 +00008737 /* If NPAR is enabled nudge the Tx scheduler */
8738 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8739 i40e_set_npar_bw_setting(pf);
8740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008741sw_init_done:
8742 return err;
8743}
8744
8745/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008746 * i40e_set_ntuple - set the ntuple feature flag and take action
8747 * @pf: board private structure to initialize
8748 * @features: the feature set that the stack is suggesting
8749 *
8750 * returns a bool to indicate if reset needs to happen
8751 **/
8752bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8753{
8754 bool need_reset = false;
8755
8756 /* Check if Flow Director n-tuple support was enabled or disabled. If
8757 * the state changed, we need to reset.
8758 */
8759 if (features & NETIF_F_NTUPLE) {
8760 /* Enable filters and mark for reset */
8761 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8762 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008763 /* enable FD_SB only if there is MSI-X vector */
8764 if (pf->num_fdsb_msix > 0)
8765 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008766 } else {
8767 /* turn off filters, mark for reset and clear SW filter list */
8768 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8769 need_reset = true;
8770 i40e_fdir_filter_exit(pf);
8771 }
8772 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008773 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008774 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008775 pf->fd_add_err = 0;
8776 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008777 /* if ATR was auto disabled it can be re-enabled. */
8778 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008779 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8780 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008781 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8782 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8783 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008784 }
8785 return need_reset;
8786}
8787
8788/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008789 * i40e_clear_rss_lut - clear the rx hash lookup table
8790 * @vsi: the VSI being configured
8791 **/
8792static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8793{
8794 struct i40e_pf *pf = vsi->back;
8795 struct i40e_hw *hw = &pf->hw;
8796 u16 vf_id = vsi->vf_id;
8797 u8 i;
8798
8799 if (vsi->type == I40E_VSI_MAIN) {
8800 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8801 wr32(hw, I40E_PFQF_HLUT(i), 0);
8802 } else if (vsi->type == I40E_VSI_SRIOV) {
8803 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8804 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8805 } else {
8806 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8807 }
8808}
8809
8810/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008811 * i40e_set_features - set the netdev feature flags
8812 * @netdev: ptr to the netdev being adjusted
8813 * @features: the feature set that the stack is suggesting
8814 **/
8815static int i40e_set_features(struct net_device *netdev,
8816 netdev_features_t features)
8817{
8818 struct i40e_netdev_priv *np = netdev_priv(netdev);
8819 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008820 struct i40e_pf *pf = vsi->back;
8821 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008822
Alan Bradyd8ec9862016-07-27 12:02:38 -07008823 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8824 i40e_pf_config_rss(pf);
8825 else if (!(features & NETIF_F_RXHASH) &&
8826 netdev->features & NETIF_F_RXHASH)
8827 i40e_clear_rss_lut(vsi);
8828
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008829 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8830 i40e_vlan_stripping_enable(vsi);
8831 else
8832 i40e_vlan_stripping_disable(vsi);
8833
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008834 need_reset = i40e_set_ntuple(pf, features);
8835
8836 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008837 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008838
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008839 return 0;
8840}
8841
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008842/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008843 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008844 * @pf: board private structure
8845 * @port: The UDP port to look up
8846 *
8847 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8848 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008849static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008850{
8851 u8 i;
8852
8853 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008854 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008855 return i;
8856 }
8857
8858 return i;
8859}
8860
8861/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008862 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008863 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008864 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008865 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008866static void i40e_udp_tunnel_add(struct net_device *netdev,
8867 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008868{
8869 struct i40e_netdev_priv *np = netdev_priv(netdev);
8870 struct i40e_vsi *vsi = np->vsi;
8871 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008872 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008873 u8 next_idx;
8874 u8 idx;
8875
Singhai, Anjali6a899022015-12-14 12:21:18 -08008876 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008877
8878 /* Check if port already exists */
8879 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008880 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008881 return;
8882 }
8883
8884 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008885 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008886
8887 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008888 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008889 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008890 return;
8891 }
8892
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008893 switch (ti->type) {
8894 case UDP_TUNNEL_TYPE_VXLAN:
8895 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8896 break;
8897 case UDP_TUNNEL_TYPE_GENEVE:
8898 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8899 return;
8900 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8901 break;
8902 default:
8903 return;
8904 }
8905
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008906 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008907 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008908 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8909 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008910}
8911
8912/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008913 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008914 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008915 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008916 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008917static void i40e_udp_tunnel_del(struct net_device *netdev,
8918 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008919{
8920 struct i40e_netdev_priv *np = netdev_priv(netdev);
8921 struct i40e_vsi *vsi = np->vsi;
8922 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008923 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008924 u8 idx;
8925
Singhai, Anjali6a899022015-12-14 12:21:18 -08008926 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008927
8928 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008929 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8930 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008931
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008932 switch (ti->type) {
8933 case UDP_TUNNEL_TYPE_VXLAN:
8934 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8935 goto not_found;
8936 break;
8937 case UDP_TUNNEL_TYPE_GENEVE:
8938 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8939 goto not_found;
8940 break;
8941 default:
8942 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008943 }
8944
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008945 /* if port exists, set it to 0 (mark for deletion)
8946 * and make it pending
8947 */
8948 pf->udp_ports[idx].index = 0;
8949 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008950 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8951
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008952 return;
8953not_found:
8954 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008955 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008956}
8957
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008958static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008959 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008960{
8961 struct i40e_netdev_priv *np = netdev_priv(netdev);
8962 struct i40e_pf *pf = np->vsi->back;
8963 struct i40e_hw *hw = &pf->hw;
8964
8965 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8966 return -EOPNOTSUPP;
8967
8968 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8969 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8970
8971 return 0;
8972}
8973
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008974/**
8975 * i40e_ndo_fdb_add - add an entry to the hardware database
8976 * @ndm: the input from the stack
8977 * @tb: pointer to array of nladdr (unused)
8978 * @dev: the net device pointer
8979 * @addr: the MAC address entry being added
8980 * @flags: instructions from stack about fdb operation
8981 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008982static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8983 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008984 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008985 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00008986{
8987 struct i40e_netdev_priv *np = netdev_priv(dev);
8988 struct i40e_pf *pf = np->vsi->back;
8989 int err = 0;
8990
8991 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
8992 return -EOPNOTSUPP;
8993
Or Gerlitz65891fe2014-12-14 18:19:05 +02008994 if (vid) {
8995 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
8996 return -EINVAL;
8997 }
8998
Greg Rose4ba0dea2014-03-06 08:59:55 +00008999 /* Hardware does not support aging addresses so if a
9000 * ndm_state is given only allow permanent addresses
9001 */
9002 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9003 netdev_info(dev, "FDB only supports static addresses\n");
9004 return -EINVAL;
9005 }
9006
9007 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9008 err = dev_uc_add_excl(dev, addr);
9009 else if (is_multicast_ether_addr(addr))
9010 err = dev_mc_add_excl(dev, addr);
9011 else
9012 err = -EINVAL;
9013
9014 /* Only return duplicate errors if NLM_F_EXCL is set */
9015 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9016 err = 0;
9017
9018 return err;
9019}
9020
Neerav Parikh51616012015-02-06 08:52:14 +00009021/**
9022 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9023 * @dev: the netdev being configured
9024 * @nlh: RTNL message
9025 *
9026 * Inserts a new hardware bridge if not already created and
9027 * enables the bridging mode requested (VEB or VEPA). If the
9028 * hardware bridge has already been inserted and the request
9029 * is to change the mode then that requires a PF reset to
9030 * allow rebuild of the components with required hardware
9031 * bridge mode enabled.
9032 **/
9033static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009034 struct nlmsghdr *nlh,
9035 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009036{
9037 struct i40e_netdev_priv *np = netdev_priv(dev);
9038 struct i40e_vsi *vsi = np->vsi;
9039 struct i40e_pf *pf = vsi->back;
9040 struct i40e_veb *veb = NULL;
9041 struct nlattr *attr, *br_spec;
9042 int i, rem;
9043
9044 /* Only for PF VSI for now */
9045 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9046 return -EOPNOTSUPP;
9047
9048 /* Find the HW bridge for PF VSI */
9049 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9050 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9051 veb = pf->veb[i];
9052 }
9053
9054 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9055
9056 nla_for_each_nested(attr, br_spec, rem) {
9057 __u16 mode;
9058
9059 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9060 continue;
9061
9062 mode = nla_get_u16(attr);
9063 if ((mode != BRIDGE_MODE_VEPA) &&
9064 (mode != BRIDGE_MODE_VEB))
9065 return -EINVAL;
9066
9067 /* Insert a new HW bridge */
9068 if (!veb) {
9069 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9070 vsi->tc_config.enabled_tc);
9071 if (veb) {
9072 veb->bridge_mode = mode;
9073 i40e_config_bridge_mode(veb);
9074 } else {
9075 /* No Bridge HW offload available */
9076 return -ENOENT;
9077 }
9078 break;
9079 } else if (mode != veb->bridge_mode) {
9080 /* Existing HW bridge but different mode needs reset */
9081 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009082 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9083 if (mode == BRIDGE_MODE_VEB)
9084 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9085 else
9086 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9087 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009088 break;
9089 }
9090 }
9091
9092 return 0;
9093}
9094
9095/**
9096 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9097 * @skb: skb buff
9098 * @pid: process id
9099 * @seq: RTNL message seq #
9100 * @dev: the netdev being configured
9101 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009102 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009103 *
9104 * Return the mode in which the hardware bridge is operating in
9105 * i.e VEB or VEPA.
9106 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009107static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9108 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009109 u32 __always_unused filter_mask,
9110 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009111{
9112 struct i40e_netdev_priv *np = netdev_priv(dev);
9113 struct i40e_vsi *vsi = np->vsi;
9114 struct i40e_pf *pf = vsi->back;
9115 struct i40e_veb *veb = NULL;
9116 int i;
9117
9118 /* Only for PF VSI for now */
9119 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9120 return -EOPNOTSUPP;
9121
9122 /* Find the HW bridge for the PF VSI */
9123 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9124 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9125 veb = pf->veb[i];
9126 }
9127
9128 if (!veb)
9129 return 0;
9130
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009131 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009132 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009133}
Neerav Parikh51616012015-02-06 08:52:14 +00009134
Joe Stringerf44a75e2015-04-14 17:09:14 -07009135/**
9136 * i40e_features_check - Validate encapsulated packet conforms to limits
9137 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009138 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009139 * @features: Offload features that the stack believes apply
9140 **/
9141static netdev_features_t i40e_features_check(struct sk_buff *skb,
9142 struct net_device *dev,
9143 netdev_features_t features)
9144{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009145 size_t len;
9146
9147 /* No point in doing any of this if neither checksum nor GSO are
9148 * being requested for this frame. We can rule out both by just
9149 * checking for CHECKSUM_PARTIAL
9150 */
9151 if (skb->ip_summed != CHECKSUM_PARTIAL)
9152 return features;
9153
9154 /* We cannot support GSO if the MSS is going to be less than
9155 * 64 bytes. If it is then we need to drop support for GSO.
9156 */
9157 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9158 features &= ~NETIF_F_GSO_MASK;
9159
9160 /* MACLEN can support at most 63 words */
9161 len = skb_network_header(skb) - skb->data;
9162 if (len & ~(63 * 2))
9163 goto out_err;
9164
9165 /* IPLEN and EIPLEN can support at most 127 dwords */
9166 len = skb_transport_header(skb) - skb_network_header(skb);
9167 if (len & ~(127 * 4))
9168 goto out_err;
9169
9170 if (skb->encapsulation) {
9171 /* L4TUNLEN can support 127 words */
9172 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9173 if (len & ~(127 * 2))
9174 goto out_err;
9175
9176 /* IPLEN can support at most 127 dwords */
9177 len = skb_inner_transport_header(skb) -
9178 skb_inner_network_header(skb);
9179 if (len & ~(127 * 4))
9180 goto out_err;
9181 }
9182
9183 /* No need to validate L4LEN as TCP is the only protocol with a
9184 * a flexible value and we support all possible values supported
9185 * by TCP, which is at most 15 dwords
9186 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009187
9188 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009189out_err:
9190 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009191}
9192
Shannon Nelson37a29732015-02-27 09:15:19 +00009193static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009194 .ndo_open = i40e_open,
9195 .ndo_stop = i40e_close,
9196 .ndo_start_xmit = i40e_lan_xmit_frame,
9197 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9198 .ndo_set_rx_mode = i40e_set_rx_mode,
9199 .ndo_validate_addr = eth_validate_addr,
9200 .ndo_set_mac_address = i40e_set_mac,
9201 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009202 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009203 .ndo_tx_timeout = i40e_tx_timeout,
9204 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9205 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9206#ifdef CONFIG_NET_POLL_CONTROLLER
9207 .ndo_poll_controller = i40e_netpoll,
9208#endif
John Fastabende4c67342016-02-16 21:16:15 -08009209 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009210 .ndo_set_features = i40e_set_features,
9211 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9212 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009213 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009214 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009215 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009216 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009217 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009218 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9219 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009220 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009221 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009222 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009223 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9224 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009225};
9226
9227/**
9228 * i40e_config_netdev - Setup the netdev flags
9229 * @vsi: the VSI being configured
9230 *
9231 * Returns 0 on success, negative value on failure
9232 **/
9233static int i40e_config_netdev(struct i40e_vsi *vsi)
9234{
9235 struct i40e_pf *pf = vsi->back;
9236 struct i40e_hw *hw = &pf->hw;
9237 struct i40e_netdev_priv *np;
9238 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009239 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009240 u8 mac_addr[ETH_ALEN];
9241 int etherdev_size;
9242
9243 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009244 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009245 if (!netdev)
9246 return -ENOMEM;
9247
9248 vsi->netdev = netdev;
9249 np = netdev_priv(netdev);
9250 np->vsi = vsi;
9251
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009252 netdev->hw_enc_features |= NETIF_F_SG |
9253 NETIF_F_IP_CSUM |
9254 NETIF_F_IPV6_CSUM |
9255 NETIF_F_HIGHDMA |
9256 NETIF_F_SOFT_FEATURES |
9257 NETIF_F_TSO |
9258 NETIF_F_TSO_ECN |
9259 NETIF_F_TSO6 |
9260 NETIF_F_GSO_GRE |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009261 NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07009262 NETIF_F_GSO_IPXIP4 |
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07009263 NETIF_F_GSO_IPXIP6 |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009264 NETIF_F_GSO_UDP_TUNNEL |
9265 NETIF_F_GSO_UDP_TUNNEL_CSUM |
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009266 NETIF_F_GSO_PARTIAL |
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009267 NETIF_F_SCTP_CRC |
9268 NETIF_F_RXHASH |
9269 NETIF_F_RXCSUM |
Jesse Brandeburg5afdaaa2015-12-10 11:38:50 -08009270 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009271
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009272 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009273 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9274
9275 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009276
9277 /* record features VLANs can make use of */
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009278 netdev->vlan_features |= netdev->hw_enc_features |
9279 NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009280
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009281 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009282 netdev->hw_features |= NETIF_F_NTUPLE;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009283
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009284 netdev->hw_features |= netdev->hw_enc_features |
9285 NETIF_F_HW_VLAN_CTAG_TX |
9286 NETIF_F_HW_VLAN_CTAG_RX;
9287
9288 netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009289 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009290
9291 if (vsi->type == I40E_VSI_MAIN) {
9292 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009293 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Filip Sadowski3954b392017-01-30 12:29:34 -08009294 /* The following steps are necessary to properly keep track of
9295 * MAC-VLAN filters loaded into firmware - first we remove
9296 * filter that is automatically generated by firmware and then
9297 * add new filter both to the driver hash table and firmware.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009298 */
9299 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009300 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009301 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009302 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009303 } else {
9304 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9305 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9306 pf->vsi[pf->lan_vsi]->netdev->name);
9307 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009308
Jacob Keller278e7d02016-10-05 09:30:37 -07009309 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009310 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009311 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009312 }
Kiran Patil21659032015-09-30 14:09:03 -04009313
Jacob Keller435c0842016-11-08 13:05:10 -08009314 /* Add the broadcast filter so that we initially will receive
9315 * broadcast packets. Note that when a new VLAN is first added the
9316 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9317 * specific filters as part of transitioning into "vlan" operation.
9318 * When more VLANs are added, the driver will copy each existing MAC
9319 * filter and add it for the new VLAN.
9320 *
9321 * Broadcast filters are handled specially by
9322 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9323 * promiscuous bit instead of adding this directly as a MAC/VLAN
9324 * filter. The subtask will update the correct broadcast promiscuous
9325 * bits as VLANs become active or inactive.
9326 */
9327 eth_broadcast_addr(broadcast);
9328 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009329 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009330 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9331
Greg Rose9a173902014-05-22 06:32:02 +00009332 ether_addr_copy(netdev->dev_addr, mac_addr);
9333 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009334
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009335 netdev->priv_flags |= IFF_UNICAST_FLT;
9336 netdev->priv_flags |= IFF_SUPP_NOFCS;
9337 /* Setup netdev TC information */
9338 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9339
9340 netdev->netdev_ops = &i40e_netdev_ops;
9341 netdev->watchdog_timeo = 5 * HZ;
9342 i40e_set_ethtool_ops(netdev);
9343
Jarod Wilson91c527a2016-10-17 15:54:05 -04009344 /* MTU range: 68 - 9706 */
9345 netdev->min_mtu = ETH_MIN_MTU;
9346 netdev->max_mtu = I40E_MAX_RXBUFFER -
9347 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9348
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009349 return 0;
9350}
9351
9352/**
9353 * i40e_vsi_delete - Delete a VSI from the switch
9354 * @vsi: the VSI being removed
9355 *
9356 * Returns 0 on success, negative value on failure
9357 **/
9358static void i40e_vsi_delete(struct i40e_vsi *vsi)
9359{
9360 /* remove default VSI is not allowed */
9361 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9362 return;
9363
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009364 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365}
9366
9367/**
Neerav Parikh51616012015-02-06 08:52:14 +00009368 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9369 * @vsi: the VSI being queried
9370 *
9371 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9372 **/
9373int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9374{
9375 struct i40e_veb *veb;
9376 struct i40e_pf *pf = vsi->back;
9377
9378 /* Uplink is not a bridge so default to VEB */
9379 if (vsi->veb_idx == I40E_NO_VEB)
9380 return 1;
9381
9382 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009383 if (!veb) {
9384 dev_info(&pf->pdev->dev,
9385 "There is no veb associated with the bridge\n");
9386 return -ENOENT;
9387 }
Neerav Parikh51616012015-02-06 08:52:14 +00009388
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009389 /* Uplink is a bridge in VEPA mode */
9390 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9391 return 0;
9392 } else {
9393 /* Uplink is a bridge in VEB mode */
9394 return 1;
9395 }
9396
9397 /* VEPA is now default bridge, so return 0 */
9398 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009399}
9400
9401/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009402 * i40e_add_vsi - Add a VSI to the switch
9403 * @vsi: the VSI being configured
9404 *
9405 * This initializes a VSI context depending on the VSI type to be added and
9406 * passes it down to the add_vsi aq command.
9407 **/
9408static int i40e_add_vsi(struct i40e_vsi *vsi)
9409{
9410 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009411 struct i40e_pf *pf = vsi->back;
9412 struct i40e_hw *hw = &pf->hw;
9413 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009414 struct i40e_mac_filter *f;
9415 struct hlist_node *h;
9416 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009417
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009418 u8 enabled_tc = 0x1; /* TC0 enabled */
9419 int f_count = 0;
9420
9421 memset(&ctxt, 0, sizeof(ctxt));
9422 switch (vsi->type) {
9423 case I40E_VSI_MAIN:
9424 /* The PF's main VSI is already setup as part of the
9425 * device initialization, so we'll not bother with
9426 * the add_vsi call, but we will retrieve the current
9427 * VSI context.
9428 */
9429 ctxt.seid = pf->main_vsi_seid;
9430 ctxt.pf_num = pf->hw.pf_id;
9431 ctxt.vf_num = 0;
9432 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9433 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9434 if (ret) {
9435 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009436 "couldn't get PF vsi config, err %s aq_err %s\n",
9437 i40e_stat_str(&pf->hw, ret),
9438 i40e_aq_str(&pf->hw,
9439 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009440 return -ENOENT;
9441 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009442 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009443 vsi->info.valid_sections = 0;
9444
9445 vsi->seid = ctxt.seid;
9446 vsi->id = ctxt.vsi_number;
9447
9448 enabled_tc = i40e_pf_get_tc_map(pf);
9449
9450 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009451 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9452 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009453 memset(&ctxt, 0, sizeof(ctxt));
9454 ctxt.seid = pf->main_vsi_seid;
9455 ctxt.pf_num = pf->hw.pf_id;
9456 ctxt.vf_num = 0;
9457 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9458 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9459 if (ret) {
9460 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009461 "update vsi failed, err %s aq_err %s\n",
9462 i40e_stat_str(&pf->hw, ret),
9463 i40e_aq_str(&pf->hw,
9464 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009465 ret = -ENOENT;
9466 goto err;
9467 }
9468 /* update the local VSI info queue map */
9469 i40e_vsi_update_queue_map(vsi, &ctxt);
9470 vsi->info.valid_sections = 0;
9471 } else {
9472 /* Default/Main VSI is only enabled for TC0
9473 * reconfigure it to enable all TCs that are
9474 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009475 * For MFP case the iSCSI PF would use this
9476 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009477 */
9478 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9479 if (ret) {
9480 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009481 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9482 enabled_tc,
9483 i40e_stat_str(&pf->hw, ret),
9484 i40e_aq_str(&pf->hw,
9485 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009486 ret = -ENOENT;
9487 }
9488 }
9489 break;
9490
9491 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009492 ctxt.pf_num = hw->pf_id;
9493 ctxt.vf_num = 0;
9494 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009495 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009496 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009497 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9498 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009499 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009500 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009501 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009502 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009503 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009504 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009505 break;
9506
9507 case I40E_VSI_VMDQ2:
9508 ctxt.pf_num = hw->pf_id;
9509 ctxt.vf_num = 0;
9510 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009511 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009512 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9513
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009514 /* This VSI is connected to VEB so the switch_id
9515 * should be set to zero by default.
9516 */
Neerav Parikh51616012015-02-06 08:52:14 +00009517 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9518 ctxt.info.valid_sections |=
9519 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9520 ctxt.info.switch_id =
9521 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9522 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009523
9524 /* Setup the VSI tx/rx queue map for TC0 only for now */
9525 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9526 break;
9527
9528 case I40E_VSI_SRIOV:
9529 ctxt.pf_num = hw->pf_id;
9530 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9531 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009532 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009533 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9534
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009535 /* This VSI is connected to VEB so the switch_id
9536 * should be set to zero by default.
9537 */
Neerav Parikh51616012015-02-06 08:52:14 +00009538 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9539 ctxt.info.valid_sections |=
9540 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9541 ctxt.info.switch_id =
9542 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9543 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009544
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009545 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9546 ctxt.info.valid_sections |=
9547 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9548 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009549 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9550 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009551 }
9552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009553 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9554 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009555 if (pf->vf[vsi->vf_id].spoofchk) {
9556 ctxt.info.valid_sections |=
9557 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9558 ctxt.info.sec_flags |=
9559 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9560 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9561 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009562 /* Setup the VSI tx/rx queue map for TC0 only for now */
9563 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9564 break;
9565
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009566 case I40E_VSI_IWARP:
9567 /* send down message to iWARP */
9568 break;
9569
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009570 default:
9571 return -ENODEV;
9572 }
9573
9574 if (vsi->type != I40E_VSI_MAIN) {
9575 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9576 if (ret) {
9577 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009578 "add vsi failed, err %s aq_err %s\n",
9579 i40e_stat_str(&pf->hw, ret),
9580 i40e_aq_str(&pf->hw,
9581 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009582 ret = -ENOENT;
9583 goto err;
9584 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009585 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009586 vsi->info.valid_sections = 0;
9587 vsi->seid = ctxt.seid;
9588 vsi->id = ctxt.vsi_number;
9589 }
9590
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009591 vsi->active_filters = 0;
9592 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009593 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009594 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009595 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009596 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009597 f_count++;
9598 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009599 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009600
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009601 if (f_count) {
9602 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9603 pf->flags |= I40E_FLAG_FILTER_SYNC;
9604 }
9605
9606 /* Update VSI BW information */
9607 ret = i40e_vsi_get_bw_info(vsi);
9608 if (ret) {
9609 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009610 "couldn't get vsi bw info, err %s aq_err %s\n",
9611 i40e_stat_str(&pf->hw, ret),
9612 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009613 /* VSI is already added so not tearing that up */
9614 ret = 0;
9615 }
9616
9617err:
9618 return ret;
9619}
9620
9621/**
9622 * i40e_vsi_release - Delete a VSI and free its resources
9623 * @vsi: the VSI being removed
9624 *
9625 * Returns 0 on success or < 0 on error
9626 **/
9627int i40e_vsi_release(struct i40e_vsi *vsi)
9628{
Jacob Keller278e7d02016-10-05 09:30:37 -07009629 struct i40e_mac_filter *f;
9630 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009631 struct i40e_veb *veb = NULL;
9632 struct i40e_pf *pf;
9633 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009634 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009635
9636 pf = vsi->back;
9637
9638 /* release of a VEB-owner or last VSI is not allowed */
9639 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9640 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9641 vsi->seid, vsi->uplink_seid);
9642 return -ENODEV;
9643 }
9644 if (vsi == pf->vsi[pf->lan_vsi] &&
9645 !test_bit(__I40E_DOWN, &pf->state)) {
9646 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9647 return -ENODEV;
9648 }
9649
9650 uplink_seid = vsi->uplink_seid;
9651 if (vsi->type != I40E_VSI_SRIOV) {
9652 if (vsi->netdev_registered) {
9653 vsi->netdev_registered = false;
9654 if (vsi->netdev) {
9655 /* results in a call to i40e_close() */
9656 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009657 }
9658 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009659 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009660 }
9661 i40e_vsi_disable_irq(vsi);
9662 }
9663
Jacob Keller278e7d02016-10-05 09:30:37 -07009664 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009665
9666 /* clear the sync flag on all filters */
9667 if (vsi->netdev) {
9668 __dev_uc_unsync(vsi->netdev, NULL);
9669 __dev_mc_unsync(vsi->netdev, NULL);
9670 }
9671
9672 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009673 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009674 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009675
Jacob Keller278e7d02016-10-05 09:30:37 -07009676 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009677
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009678 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009679
9680 i40e_vsi_delete(vsi);
9681 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009682 if (vsi->netdev) {
9683 free_netdev(vsi->netdev);
9684 vsi->netdev = NULL;
9685 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009686 i40e_vsi_clear_rings(vsi);
9687 i40e_vsi_clear(vsi);
9688
9689 /* If this was the last thing on the VEB, except for the
9690 * controlling VSI, remove the VEB, which puts the controlling
9691 * VSI onto the next level down in the switch.
9692 *
9693 * Well, okay, there's one more exception here: don't remove
9694 * the orphan VEBs yet. We'll wait for an explicit remove request
9695 * from up the network stack.
9696 */
Mitch Williams505682c2014-05-20 08:01:37 +00009697 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009698 if (pf->vsi[i] &&
9699 pf->vsi[i]->uplink_seid == uplink_seid &&
9700 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9701 n++; /* count the VSIs */
9702 }
9703 }
9704 for (i = 0; i < I40E_MAX_VEB; i++) {
9705 if (!pf->veb[i])
9706 continue;
9707 if (pf->veb[i]->uplink_seid == uplink_seid)
9708 n++; /* count the VEBs */
9709 if (pf->veb[i]->seid == uplink_seid)
9710 veb = pf->veb[i];
9711 }
9712 if (n == 0 && veb && veb->uplink_seid != 0)
9713 i40e_veb_release(veb);
9714
9715 return 0;
9716}
9717
9718/**
9719 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9720 * @vsi: ptr to the VSI
9721 *
9722 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9723 * corresponding SW VSI structure and initializes num_queue_pairs for the
9724 * newly allocated VSI.
9725 *
9726 * Returns 0 on success or negative on failure
9727 **/
9728static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9729{
9730 int ret = -ENOENT;
9731 struct i40e_pf *pf = vsi->back;
9732
Alexander Duyck493fb302013-09-28 07:01:44 +00009733 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009734 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9735 vsi->seid);
9736 return -EEXIST;
9737 }
9738
9739 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009740 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009741 vsi->seid, vsi->base_vector);
9742 return -EEXIST;
9743 }
9744
Greg Rose90e04072014-03-06 08:59:57 +00009745 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009746 if (ret) {
9747 dev_info(&pf->pdev->dev,
9748 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9749 vsi->num_q_vectors, vsi->seid, ret);
9750 vsi->num_q_vectors = 0;
9751 goto vector_setup_out;
9752 }
9753
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009754 /* In Legacy mode, we do not have to get any other vector since we
9755 * piggyback on the misc/ICR0 for queue interrupts.
9756 */
9757 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9758 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009759 if (vsi->num_q_vectors)
9760 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9761 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009762 if (vsi->base_vector < 0) {
9763 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009764 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9765 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009766 i40e_vsi_free_q_vectors(vsi);
9767 ret = -ENOENT;
9768 goto vector_setup_out;
9769 }
9770
9771vector_setup_out:
9772 return ret;
9773}
9774
9775/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009776 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9777 * @vsi: pointer to the vsi.
9778 *
9779 * This re-allocates a vsi's queue resources.
9780 *
9781 * Returns pointer to the successfully allocated and configured VSI sw struct
9782 * on success, otherwise returns NULL on failure.
9783 **/
9784static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9785{
John Underwoodf5340392016-02-18 09:19:24 -08009786 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009787 u8 enabled_tc;
9788 int ret;
9789
John Underwoodf5340392016-02-18 09:19:24 -08009790 if (!vsi)
9791 return NULL;
9792
9793 pf = vsi->back;
9794
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009795 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9796 i40e_vsi_clear_rings(vsi);
9797
9798 i40e_vsi_free_arrays(vsi, false);
9799 i40e_set_num_rings_in_vsi(vsi);
9800 ret = i40e_vsi_alloc_arrays(vsi, false);
9801 if (ret)
9802 goto err_vsi;
9803
9804 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9805 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009806 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009807 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009808 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009809 goto err_vsi;
9810 }
9811 vsi->base_queue = ret;
9812
9813 /* Update the FW view of the VSI. Force a reset of TC and queue
9814 * layout configurations.
9815 */
9816 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9817 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9818 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9819 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009820 if (vsi->type == I40E_VSI_MAIN)
9821 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009822
9823 /* assign it some queues */
9824 ret = i40e_alloc_rings(vsi);
9825 if (ret)
9826 goto err_rings;
9827
9828 /* map all of the rings to the q_vectors */
9829 i40e_vsi_map_rings_to_vectors(vsi);
9830 return vsi;
9831
9832err_rings:
9833 i40e_vsi_free_q_vectors(vsi);
9834 if (vsi->netdev_registered) {
9835 vsi->netdev_registered = false;
9836 unregister_netdev(vsi->netdev);
9837 free_netdev(vsi->netdev);
9838 vsi->netdev = NULL;
9839 }
9840 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9841err_vsi:
9842 i40e_vsi_clear(vsi);
9843 return NULL;
9844}
9845
9846/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009847 * i40e_vsi_setup - Set up a VSI by a given type
9848 * @pf: board private structure
9849 * @type: VSI type
9850 * @uplink_seid: the switch element to link to
9851 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9852 *
9853 * This allocates the sw VSI structure and its queue resources, then add a VSI
9854 * to the identified VEB.
9855 *
9856 * Returns pointer to the successfully allocated and configure VSI sw struct on
9857 * success, otherwise returns NULL on failure.
9858 **/
9859struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9860 u16 uplink_seid, u32 param1)
9861{
9862 struct i40e_vsi *vsi = NULL;
9863 struct i40e_veb *veb = NULL;
9864 int ret, i;
9865 int v_idx;
9866
9867 /* The requested uplink_seid must be either
9868 * - the PF's port seid
9869 * no VEB is needed because this is the PF
9870 * or this is a Flow Director special case VSI
9871 * - seid of an existing VEB
9872 * - seid of a VSI that owns an existing VEB
9873 * - seid of a VSI that doesn't own a VEB
9874 * a new VEB is created and the VSI becomes the owner
9875 * - seid of the PF VSI, which is what creates the first VEB
9876 * this is a special case of the previous
9877 *
9878 * Find which uplink_seid we were given and create a new VEB if needed
9879 */
9880 for (i = 0; i < I40E_MAX_VEB; i++) {
9881 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9882 veb = pf->veb[i];
9883 break;
9884 }
9885 }
9886
9887 if (!veb && uplink_seid != pf->mac_seid) {
9888
Mitch Williams505682c2014-05-20 08:01:37 +00009889 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009890 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9891 vsi = pf->vsi[i];
9892 break;
9893 }
9894 }
9895 if (!vsi) {
9896 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9897 uplink_seid);
9898 return NULL;
9899 }
9900
9901 if (vsi->uplink_seid == pf->mac_seid)
9902 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9903 vsi->tc_config.enabled_tc);
9904 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9905 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9906 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009907 if (veb) {
9908 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9909 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009910 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009911 return NULL;
9912 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009913 /* We come up by default in VEPA mode if SRIOV is not
9914 * already enabled, in which case we can't force VEPA
9915 * mode.
9916 */
9917 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9918 veb->bridge_mode = BRIDGE_MODE_VEPA;
9919 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9920 }
Neerav Parikh51616012015-02-06 08:52:14 +00009921 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009922 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009923 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9924 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9925 veb = pf->veb[i];
9926 }
9927 if (!veb) {
9928 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9929 return NULL;
9930 }
9931
9932 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9933 uplink_seid = veb->seid;
9934 }
9935
9936 /* get vsi sw struct */
9937 v_idx = i40e_vsi_mem_alloc(pf, type);
9938 if (v_idx < 0)
9939 goto err_alloc;
9940 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009941 if (!vsi)
9942 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009943 vsi->type = type;
9944 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9945
9946 if (type == I40E_VSI_MAIN)
9947 pf->lan_vsi = v_idx;
9948 else if (type == I40E_VSI_SRIOV)
9949 vsi->vf_id = param1;
9950 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009951 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9952 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009953 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009954 dev_info(&pf->pdev->dev,
9955 "failed to get tracking for %d queues for VSI %d err=%d\n",
9956 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009957 goto err_vsi;
9958 }
9959 vsi->base_queue = ret;
9960
9961 /* get a VSI from the hardware */
9962 vsi->uplink_seid = uplink_seid;
9963 ret = i40e_add_vsi(vsi);
9964 if (ret)
9965 goto err_vsi;
9966
9967 switch (vsi->type) {
9968 /* setup the netdev if needed */
9969 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009970 /* Apply relevant filters if a platform-specific mac
9971 * address was selected.
9972 */
9973 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9974 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9975 if (ret) {
9976 dev_warn(&pf->pdev->dev,
9977 "could not set up macaddr; err %d\n",
9978 ret);
9979 }
9980 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009981 case I40E_VSI_VMDQ2:
9982 ret = i40e_config_netdev(vsi);
9983 if (ret)
9984 goto err_netdev;
9985 ret = register_netdev(vsi->netdev);
9986 if (ret)
9987 goto err_netdev;
9988 vsi->netdev_registered = true;
9989 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08009990#ifdef CONFIG_I40E_DCB
9991 /* Setup DCB netlink interface */
9992 i40e_dcbnl_setup(vsi);
9993#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009994 /* fall through */
9995
9996 case I40E_VSI_FDIR:
9997 /* set up vectors and rings if needed */
9998 ret = i40e_vsi_setup_vectors(vsi);
9999 if (ret)
10000 goto err_msix;
10001
10002 ret = i40e_alloc_rings(vsi);
10003 if (ret)
10004 goto err_rings;
10005
10006 /* map all of the rings to the q_vectors */
10007 i40e_vsi_map_rings_to_vectors(vsi);
10008
10009 i40e_vsi_reset_stats(vsi);
10010 break;
10011
10012 default:
10013 /* no netdev or rings for the other VSI types */
10014 break;
10015 }
10016
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -040010017 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10018 (vsi->type == I40E_VSI_VMDQ2)) {
10019 ret = i40e_vsi_config_rss(vsi);
10020 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010021 return vsi;
10022
10023err_rings:
10024 i40e_vsi_free_q_vectors(vsi);
10025err_msix:
10026 if (vsi->netdev_registered) {
10027 vsi->netdev_registered = false;
10028 unregister_netdev(vsi->netdev);
10029 free_netdev(vsi->netdev);
10030 vsi->netdev = NULL;
10031 }
10032err_netdev:
10033 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10034err_vsi:
10035 i40e_vsi_clear(vsi);
10036err_alloc:
10037 return NULL;
10038}
10039
10040/**
10041 * i40e_veb_get_bw_info - Query VEB BW information
10042 * @veb: the veb to query
10043 *
10044 * Query the Tx scheduler BW configuration data for given VEB
10045 **/
10046static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10047{
10048 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10049 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10050 struct i40e_pf *pf = veb->pf;
10051 struct i40e_hw *hw = &pf->hw;
10052 u32 tc_bw_max;
10053 int ret = 0;
10054 int i;
10055
10056 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10057 &bw_data, NULL);
10058 if (ret) {
10059 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010060 "query veb bw config failed, err %s aq_err %s\n",
10061 i40e_stat_str(&pf->hw, ret),
10062 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010063 goto out;
10064 }
10065
10066 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10067 &ets_data, NULL);
10068 if (ret) {
10069 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010070 "query veb bw ets config failed, err %s aq_err %s\n",
10071 i40e_stat_str(&pf->hw, ret),
10072 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010073 goto out;
10074 }
10075
10076 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10077 veb->bw_max_quanta = ets_data.tc_bw_max;
10078 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010079 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010080 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10081 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10082 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10083 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10084 veb->bw_tc_limit_credits[i] =
10085 le16_to_cpu(bw_data.tc_bw_limits[i]);
10086 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10087 }
10088
10089out:
10090 return ret;
10091}
10092
10093/**
10094 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10095 * @pf: board private structure
10096 *
10097 * On error: returns error code (negative)
10098 * On success: returns vsi index in PF (positive)
10099 **/
10100static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10101{
10102 int ret = -ENOENT;
10103 struct i40e_veb *veb;
10104 int i;
10105
10106 /* Need to protect the allocation of switch elements at the PF level */
10107 mutex_lock(&pf->switch_mutex);
10108
10109 /* VEB list may be fragmented if VEB creation/destruction has
10110 * been happening. We can afford to do a quick scan to look
10111 * for any free slots in the list.
10112 *
10113 * find next empty veb slot, looping back around if necessary
10114 */
10115 i = 0;
10116 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10117 i++;
10118 if (i >= I40E_MAX_VEB) {
10119 ret = -ENOMEM;
10120 goto err_alloc_veb; /* out of VEB slots! */
10121 }
10122
10123 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10124 if (!veb) {
10125 ret = -ENOMEM;
10126 goto err_alloc_veb;
10127 }
10128 veb->pf = pf;
10129 veb->idx = i;
10130 veb->enabled_tc = 1;
10131
10132 pf->veb[i] = veb;
10133 ret = i;
10134err_alloc_veb:
10135 mutex_unlock(&pf->switch_mutex);
10136 return ret;
10137}
10138
10139/**
10140 * i40e_switch_branch_release - Delete a branch of the switch tree
10141 * @branch: where to start deleting
10142 *
10143 * This uses recursion to find the tips of the branch to be
10144 * removed, deleting until we get back to and can delete this VEB.
10145 **/
10146static void i40e_switch_branch_release(struct i40e_veb *branch)
10147{
10148 struct i40e_pf *pf = branch->pf;
10149 u16 branch_seid = branch->seid;
10150 u16 veb_idx = branch->idx;
10151 int i;
10152
10153 /* release any VEBs on this VEB - RECURSION */
10154 for (i = 0; i < I40E_MAX_VEB; i++) {
10155 if (!pf->veb[i])
10156 continue;
10157 if (pf->veb[i]->uplink_seid == branch->seid)
10158 i40e_switch_branch_release(pf->veb[i]);
10159 }
10160
10161 /* Release the VSIs on this VEB, but not the owner VSI.
10162 *
10163 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10164 * the VEB itself, so don't use (*branch) after this loop.
10165 */
Mitch Williams505682c2014-05-20 08:01:37 +000010166 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010167 if (!pf->vsi[i])
10168 continue;
10169 if (pf->vsi[i]->uplink_seid == branch_seid &&
10170 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10171 i40e_vsi_release(pf->vsi[i]);
10172 }
10173 }
10174
10175 /* There's one corner case where the VEB might not have been
10176 * removed, so double check it here and remove it if needed.
10177 * This case happens if the veb was created from the debugfs
10178 * commands and no VSIs were added to it.
10179 */
10180 if (pf->veb[veb_idx])
10181 i40e_veb_release(pf->veb[veb_idx]);
10182}
10183
10184/**
10185 * i40e_veb_clear - remove veb struct
10186 * @veb: the veb to remove
10187 **/
10188static void i40e_veb_clear(struct i40e_veb *veb)
10189{
10190 if (!veb)
10191 return;
10192
10193 if (veb->pf) {
10194 struct i40e_pf *pf = veb->pf;
10195
10196 mutex_lock(&pf->switch_mutex);
10197 if (pf->veb[veb->idx] == veb)
10198 pf->veb[veb->idx] = NULL;
10199 mutex_unlock(&pf->switch_mutex);
10200 }
10201
10202 kfree(veb);
10203}
10204
10205/**
10206 * i40e_veb_release - Delete a VEB and free its resources
10207 * @veb: the VEB being removed
10208 **/
10209void i40e_veb_release(struct i40e_veb *veb)
10210{
10211 struct i40e_vsi *vsi = NULL;
10212 struct i40e_pf *pf;
10213 int i, n = 0;
10214
10215 pf = veb->pf;
10216
10217 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010218 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010219 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10220 n++;
10221 vsi = pf->vsi[i];
10222 }
10223 }
10224 if (n != 1) {
10225 dev_info(&pf->pdev->dev,
10226 "can't remove VEB %d with %d VSIs left\n",
10227 veb->seid, n);
10228 return;
10229 }
10230
10231 /* move the remaining VSI to uplink veb */
10232 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10233 if (veb->uplink_seid) {
10234 vsi->uplink_seid = veb->uplink_seid;
10235 if (veb->uplink_seid == pf->mac_seid)
10236 vsi->veb_idx = I40E_NO_VEB;
10237 else
10238 vsi->veb_idx = veb->veb_idx;
10239 } else {
10240 /* floating VEB */
10241 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10242 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10243 }
10244
10245 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10246 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010247}
10248
10249/**
10250 * i40e_add_veb - create the VEB in the switch
10251 * @veb: the VEB to be instantiated
10252 * @vsi: the controlling VSI
10253 **/
10254static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10255{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010256 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010257 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010258 int ret;
10259
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010260 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010261 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010262 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010263
10264 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010265 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010266 dev_info(&pf->pdev->dev,
10267 "couldn't add VEB, err %s aq_err %s\n",
10268 i40e_stat_str(&pf->hw, ret),
10269 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010270 return -EPERM;
10271 }
10272
10273 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010274 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010275 &veb->stats_idx, NULL, NULL, NULL);
10276 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010277 dev_info(&pf->pdev->dev,
10278 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10279 i40e_stat_str(&pf->hw, ret),
10280 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010281 return -EPERM;
10282 }
10283 ret = i40e_veb_get_bw_info(veb);
10284 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010285 dev_info(&pf->pdev->dev,
10286 "couldn't get VEB bw info, err %s aq_err %s\n",
10287 i40e_stat_str(&pf->hw, ret),
10288 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10289 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010290 return -ENOENT;
10291 }
10292
10293 vsi->uplink_seid = veb->seid;
10294 vsi->veb_idx = veb->idx;
10295 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10296
10297 return 0;
10298}
10299
10300/**
10301 * i40e_veb_setup - Set up a VEB
10302 * @pf: board private structure
10303 * @flags: VEB setup flags
10304 * @uplink_seid: the switch element to link to
10305 * @vsi_seid: the initial VSI seid
10306 * @enabled_tc: Enabled TC bit-map
10307 *
10308 * This allocates the sw VEB structure and links it into the switch
10309 * It is possible and legal for this to be a duplicate of an already
10310 * existing VEB. It is also possible for both uplink and vsi seids
10311 * to be zero, in order to create a floating VEB.
10312 *
10313 * Returns pointer to the successfully allocated VEB sw struct on
10314 * success, otherwise returns NULL on failure.
10315 **/
10316struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10317 u16 uplink_seid, u16 vsi_seid,
10318 u8 enabled_tc)
10319{
10320 struct i40e_veb *veb, *uplink_veb = NULL;
10321 int vsi_idx, veb_idx;
10322 int ret;
10323
10324 /* if one seid is 0, the other must be 0 to create a floating relay */
10325 if ((uplink_seid == 0 || vsi_seid == 0) &&
10326 (uplink_seid + vsi_seid != 0)) {
10327 dev_info(&pf->pdev->dev,
10328 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10329 uplink_seid, vsi_seid);
10330 return NULL;
10331 }
10332
10333 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010334 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010335 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10336 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010337 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010338 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10339 vsi_seid);
10340 return NULL;
10341 }
10342
10343 if (uplink_seid && uplink_seid != pf->mac_seid) {
10344 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10345 if (pf->veb[veb_idx] &&
10346 pf->veb[veb_idx]->seid == uplink_seid) {
10347 uplink_veb = pf->veb[veb_idx];
10348 break;
10349 }
10350 }
10351 if (!uplink_veb) {
10352 dev_info(&pf->pdev->dev,
10353 "uplink seid %d not found\n", uplink_seid);
10354 return NULL;
10355 }
10356 }
10357
10358 /* get veb sw struct */
10359 veb_idx = i40e_veb_mem_alloc(pf);
10360 if (veb_idx < 0)
10361 goto err_alloc;
10362 veb = pf->veb[veb_idx];
10363 veb->flags = flags;
10364 veb->uplink_seid = uplink_seid;
10365 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10366 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10367
10368 /* create the VEB in the switch */
10369 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10370 if (ret)
10371 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010372 if (vsi_idx == pf->lan_vsi)
10373 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010374
10375 return veb;
10376
10377err_veb:
10378 i40e_veb_clear(veb);
10379err_alloc:
10380 return NULL;
10381}
10382
10383/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010384 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010385 * @pf: board private structure
10386 * @ele: element we are building info from
10387 * @num_reported: total number of elements
10388 * @printconfig: should we print the contents
10389 *
10390 * helper function to assist in extracting a few useful SEID values.
10391 **/
10392static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10393 struct i40e_aqc_switch_config_element_resp *ele,
10394 u16 num_reported, bool printconfig)
10395{
10396 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10397 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10398 u8 element_type = ele->element_type;
10399 u16 seid = le16_to_cpu(ele->seid);
10400
10401 if (printconfig)
10402 dev_info(&pf->pdev->dev,
10403 "type=%d seid=%d uplink=%d downlink=%d\n",
10404 element_type, seid, uplink_seid, downlink_seid);
10405
10406 switch (element_type) {
10407 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10408 pf->mac_seid = seid;
10409 break;
10410 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10411 /* Main VEB? */
10412 if (uplink_seid != pf->mac_seid)
10413 break;
10414 if (pf->lan_veb == I40E_NO_VEB) {
10415 int v;
10416
10417 /* find existing or else empty VEB */
10418 for (v = 0; v < I40E_MAX_VEB; v++) {
10419 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10420 pf->lan_veb = v;
10421 break;
10422 }
10423 }
10424 if (pf->lan_veb == I40E_NO_VEB) {
10425 v = i40e_veb_mem_alloc(pf);
10426 if (v < 0)
10427 break;
10428 pf->lan_veb = v;
10429 }
10430 }
10431
10432 pf->veb[pf->lan_veb]->seid = seid;
10433 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10434 pf->veb[pf->lan_veb]->pf = pf;
10435 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10436 break;
10437 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10438 if (num_reported != 1)
10439 break;
10440 /* This is immediately after a reset so we can assume this is
10441 * the PF's VSI
10442 */
10443 pf->mac_seid = uplink_seid;
10444 pf->pf_seid = downlink_seid;
10445 pf->main_vsi_seid = seid;
10446 if (printconfig)
10447 dev_info(&pf->pdev->dev,
10448 "pf_seid=%d main_vsi_seid=%d\n",
10449 pf->pf_seid, pf->main_vsi_seid);
10450 break;
10451 case I40E_SWITCH_ELEMENT_TYPE_PF:
10452 case I40E_SWITCH_ELEMENT_TYPE_VF:
10453 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10454 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10455 case I40E_SWITCH_ELEMENT_TYPE_PE:
10456 case I40E_SWITCH_ELEMENT_TYPE_PA:
10457 /* ignore these for now */
10458 break;
10459 default:
10460 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10461 element_type, seid);
10462 break;
10463 }
10464}
10465
10466/**
10467 * i40e_fetch_switch_configuration - Get switch config from firmware
10468 * @pf: board private structure
10469 * @printconfig: should we print the contents
10470 *
10471 * Get the current switch configuration from the device and
10472 * extract a few useful SEID values.
10473 **/
10474int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10475{
10476 struct i40e_aqc_get_switch_config_resp *sw_config;
10477 u16 next_seid = 0;
10478 int ret = 0;
10479 u8 *aq_buf;
10480 int i;
10481
10482 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10483 if (!aq_buf)
10484 return -ENOMEM;
10485
10486 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10487 do {
10488 u16 num_reported, num_total;
10489
10490 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10491 I40E_AQ_LARGE_BUF,
10492 &next_seid, NULL);
10493 if (ret) {
10494 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010495 "get switch config failed err %s aq_err %s\n",
10496 i40e_stat_str(&pf->hw, ret),
10497 i40e_aq_str(&pf->hw,
10498 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010499 kfree(aq_buf);
10500 return -ENOENT;
10501 }
10502
10503 num_reported = le16_to_cpu(sw_config->header.num_reported);
10504 num_total = le16_to_cpu(sw_config->header.num_total);
10505
10506 if (printconfig)
10507 dev_info(&pf->pdev->dev,
10508 "header: %d reported %d total\n",
10509 num_reported, num_total);
10510
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010511 for (i = 0; i < num_reported; i++) {
10512 struct i40e_aqc_switch_config_element_resp *ele =
10513 &sw_config->element[i];
10514
10515 i40e_setup_pf_switch_element(pf, ele, num_reported,
10516 printconfig);
10517 }
10518 } while (next_seid != 0);
10519
10520 kfree(aq_buf);
10521 return ret;
10522}
10523
10524/**
10525 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10526 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010527 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010528 *
10529 * Returns 0 on success, negative value on failure
10530 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010531static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010532{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010533 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010534 int ret;
10535
10536 /* find out what's out there already */
10537 ret = i40e_fetch_switch_configuration(pf, false);
10538 if (ret) {
10539 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010540 "couldn't fetch switch config, err %s aq_err %s\n",
10541 i40e_stat_str(&pf->hw, ret),
10542 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010543 return ret;
10544 }
10545 i40e_pf_reset_stats(pf);
10546
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010547 /* set the switch config bit for the whole device to
10548 * support limited promisc or true promisc
10549 * when user requests promisc. The default is limited
10550 * promisc.
10551 */
10552
10553 if ((pf->hw.pf_id == 0) &&
10554 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10555 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10556
10557 if (pf->hw.pf_id == 0) {
10558 u16 valid_flags;
10559
10560 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10561 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10562 NULL);
10563 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10564 dev_info(&pf->pdev->dev,
10565 "couldn't set switch config bits, err %s aq_err %s\n",
10566 i40e_stat_str(&pf->hw, ret),
10567 i40e_aq_str(&pf->hw,
10568 pf->hw.aq.asq_last_status));
10569 /* not a fatal problem, just keep going */
10570 }
10571 }
10572
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010573 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010574 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010575 struct i40e_vsi *vsi = NULL;
10576 u16 uplink_seid;
10577
10578 /* Set up the PF VSI associated with the PF's main VSI
10579 * that is already in the HW switch
10580 */
10581 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10582 uplink_seid = pf->veb[pf->lan_veb]->seid;
10583 else
10584 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010585 if (pf->lan_vsi == I40E_NO_VSI)
10586 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10587 else if (reinit)
10588 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010589 if (!vsi) {
10590 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10591 i40e_fdir_teardown(pf);
10592 return -EAGAIN;
10593 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010594 } else {
10595 /* force a reset of TC and queue layout configurations */
10596 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010597
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010598 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10599 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10600 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10601 }
10602 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10603
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010604 i40e_fdir_sb_setup(pf);
10605
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010606 /* Setup static PF queue filter control settings */
10607 ret = i40e_setup_pf_filter_control(pf);
10608 if (ret) {
10609 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10610 ret);
10611 /* Failure here should not stop continuing other steps */
10612 }
10613
10614 /* enable RSS in the HW, even for only one queue, as the stack can use
10615 * the hash
10616 */
10617 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010618 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010619
10620 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010621 i40e_link_event(pf);
10622
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010623 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010624 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10625 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010626
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010627 i40e_ptp_init(pf);
10628
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010629 return ret;
10630}
10631
10632/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010633 * i40e_determine_queue_usage - Work out queue distribution
10634 * @pf: board private structure
10635 **/
10636static void i40e_determine_queue_usage(struct i40e_pf *pf)
10637{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010638 int queues_left;
10639
10640 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010641
10642 /* Find the max queues to be put into basic use. We'll always be
10643 * using TC0, whether or not DCB is running, and TC0 will get the
10644 * big RSS set.
10645 */
10646 queues_left = pf->hw.func_caps.num_tx_qp;
10647
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010648 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010649 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010650 /* one qp for PF, no queues for anything else */
10651 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010652 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010653
10654 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010655 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010656 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010657 I40E_FLAG_FD_SB_ENABLED |
10658 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010659 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010660 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010661 I40E_FLAG_SRIOV_ENABLED |
10662 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010663 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10664 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010665 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010666 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010667 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010668 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010669 queues_left -= pf->num_lan_qps;
10670
10671 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010672 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000010673 I40E_FLAG_FD_SB_ENABLED |
10674 I40E_FLAG_FD_ATR_ENABLED |
10675 I40E_FLAG_DCB_ENABLED |
10676 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010677 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010678 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010679 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010680 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010681 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10682 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010683 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10684 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010685 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10686 num_online_cpus());
10687 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10688 pf->hw.func_caps.num_tx_qp);
10689
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010690 queues_left -= pf->num_lan_qps;
10691 }
10692
10693 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10694 if (queues_left > 1) {
10695 queues_left -= 1; /* save 1 queue for FD */
10696 } else {
10697 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10698 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10699 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010700 }
10701
10702 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10703 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010704 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10705 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010706 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10707 }
10708
10709 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10710 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10711 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10712 (queues_left / pf->num_vmdq_qps));
10713 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10714 }
10715
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010716 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010717 dev_dbg(&pf->pdev->dev,
10718 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10719 pf->hw.func_caps.num_tx_qp,
10720 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010721 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10722 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10723 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010724}
10725
10726/**
10727 * i40e_setup_pf_filter_control - Setup PF static filter control
10728 * @pf: PF to be setup
10729 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010730 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010731 * settings. If PE/FCoE are enabled then it will also set the per PF
10732 * based filter sizes required for them. It also enables Flow director,
10733 * ethertype and macvlan type filter settings for the pf.
10734 *
10735 * Returns 0 on success, negative on failure
10736 **/
10737static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10738{
10739 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10740
10741 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10742
10743 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010744 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010745 settings->enable_fdir = true;
10746
10747 /* Ethtype and MACVLAN filters enabled for PF */
10748 settings->enable_ethtype = true;
10749 settings->enable_macvlan = true;
10750
10751 if (i40e_set_filter_control(&pf->hw, settings))
10752 return -ENOENT;
10753
10754 return 0;
10755}
10756
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010757#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010758#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010759static void i40e_print_features(struct i40e_pf *pf)
10760{
10761 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010762 char *buf;
10763 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010764
Joe Perches3b195842015-12-03 04:20:57 -080010765 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10766 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010767 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010768
Joe Perches3b195842015-12-03 04:20:57 -080010769 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010770#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010771 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010772#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010773 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010774 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010775 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010776 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010777 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010778 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010779 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010780 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010781 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10782 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010783 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010784 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010785 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010786 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010787 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010788 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010789 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010790 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010791 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010792 else
Joe Perches3b195842015-12-03 04:20:57 -080010793 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010794
Joe Perches3b195842015-12-03 04:20:57 -080010795 dev_info(&pf->pdev->dev, "%s\n", buf);
10796 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010797 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010798}
10799
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010800/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010801 * i40e_get_platform_mac_addr - get platform-specific MAC address
10802 *
10803 * @pdev: PCI device information struct
10804 * @pf: board private structure
10805 *
10806 * Look up the MAC address in Open Firmware on systems that support it,
10807 * and use IDPROM on SPARC if no OF address is found. On return, the
10808 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10809 * has been selected.
10810 **/
10811static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10812{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010813 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010814 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010815 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010816}
10817
10818/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010819 * i40e_probe - Device initialization routine
10820 * @pdev: PCI device information struct
10821 * @ent: entry in i40e_pci_tbl
10822 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010823 * i40e_probe initializes a PF identified by a pci_dev structure.
10824 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010825 * and a hardware reset occur.
10826 *
10827 * Returns 0 on success, negative on failure
10828 **/
10829static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10830{
Catherine Sullivane8278452015-02-06 08:52:08 +000010831 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010832 struct i40e_pf *pf;
10833 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010834 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010835 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010836 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010837 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010838 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010839 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010840 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010841
10842 err = pci_enable_device_mem(pdev);
10843 if (err)
10844 return err;
10845
10846 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010847 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010848 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010849 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10850 if (err) {
10851 dev_err(&pdev->dev,
10852 "DMA configuration failed: 0x%x\n", err);
10853 goto err_dma;
10854 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010855 }
10856
10857 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010858 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010859 if (err) {
10860 dev_info(&pdev->dev,
10861 "pci_request_selected_regions failed %d\n", err);
10862 goto err_pci_reg;
10863 }
10864
10865 pci_enable_pcie_error_reporting(pdev);
10866 pci_set_master(pdev);
10867
10868 /* Now that we have a PCI connection, we need to do the
10869 * low level device setup. This is primarily setting up
10870 * the Admin Queue structures and then querying for the
10871 * device's current profile information.
10872 */
10873 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10874 if (!pf) {
10875 err = -ENOMEM;
10876 goto err_pf_alloc;
10877 }
10878 pf->next_vsi = 0;
10879 pf->pdev = pdev;
10880 set_bit(__I40E_DOWN, &pf->state);
10881
10882 hw = &pf->hw;
10883 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010884
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010885 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10886 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010887
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010888 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010889 if (!hw->hw_addr) {
10890 err = -EIO;
10891 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10892 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010893 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010894 goto err_ioremap;
10895 }
10896 hw->vendor_id = pdev->vendor;
10897 hw->device_id = pdev->device;
10898 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10899 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10900 hw->subsystem_device_id = pdev->subsystem_device;
10901 hw->bus.device = PCI_SLOT(pdev->devfn);
10902 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080010903 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010904 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010905
Jacob Keller0e588de2017-02-06 14:38:50 -080010906 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
10907 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
10908
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010909 /* set up the locks for the AQ, do this only once in probe
10910 * and destroy them only once in remove
10911 */
10912 mutex_init(&hw->aq.asq_mutex);
10913 mutex_init(&hw->aq.arq_mutex);
10914
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010915 pf->msg_enable = netif_msg_init(debug,
10916 NETIF_MSG_DRV |
10917 NETIF_MSG_PROBE |
10918 NETIF_MSG_LINK);
10919 if (debug < -1)
10920 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010921
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010922 /* do a special CORER for clearing PXE mode once at init */
10923 if (hw->revision_id == 0 &&
10924 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10925 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10926 i40e_flush(hw);
10927 msleep(200);
10928 pf->corer_count++;
10929
10930 i40e_clear_pxe_mode(hw);
10931 }
10932
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010933 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010934 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010935 err = i40e_pf_reset(hw);
10936 if (err) {
10937 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10938 goto err_pf_reset;
10939 }
10940 pf->pfr_count++;
10941
10942 hw->aq.num_arq_entries = I40E_AQ_LEN;
10943 hw->aq.num_asq_entries = I40E_AQ_LEN;
10944 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10945 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10946 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010947
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010948 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010949 "%s-%s:misc",
10950 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010951
10952 err = i40e_init_shared_code(hw);
10953 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010954 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10955 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010956 goto err_pf_reset;
10957 }
10958
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010959 /* set up a default setting for link flow control */
10960 pf->hw.fc.requested_mode = I40E_FC_NONE;
10961
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010962 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010963 if (err) {
10964 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10965 dev_info(&pdev->dev,
10966 "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");
10967 else
10968 dev_info(&pdev->dev,
10969 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10970
10971 goto err_pf_reset;
10972 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010973
Shannon Nelson6dec1012015-09-28 14:12:30 -040010974 /* provide nvm, fw, api versions */
10975 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10976 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10977 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10978 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010979
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010980 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10981 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010982 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010983 "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");
10984 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
10985 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000010986 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010987 "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 +000010988
Shannon Nelson4eb3f762014-03-06 08:59:58 +000010989 i40e_verify_eeprom(pf);
10990
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000010991 /* Rev 0 hardware was never productized */
10992 if (hw->revision_id < 1)
10993 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");
10994
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000010995 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010996 err = i40e_get_capabilities(pf);
10997 if (err)
10998 goto err_adminq_setup;
10999
11000 err = i40e_sw_init(pf);
11001 if (err) {
11002 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11003 goto err_sw_init;
11004 }
11005
11006 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011007 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011008 if (err) {
11009 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11010 goto err_init_lan_hmc;
11011 }
11012
11013 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11014 if (err) {
11015 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11016 err = -ENOENT;
11017 goto err_configure_lan_hmc;
11018 }
11019
Neerav Parikhb686ece2014-12-14 01:55:11 +000011020 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11021 * Ignore error return codes because if it was already disabled via
11022 * hardware settings this will fail
11023 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011024 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011025 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11026 i40e_aq_stop_lldp(hw, true, NULL);
11027 }
11028
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011029 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011030 /* allow a platform config to override the HW addr */
11031 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011032 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011033 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11034 err = -EIO;
11035 goto err_mac_addr;
11036 }
11037 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011038 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011039 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11040 if (is_valid_ether_addr(hw->mac.port_addr))
11041 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011042
11043 pci_set_drvdata(pdev, pf);
11044 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011045#ifdef CONFIG_I40E_DCB
11046 err = i40e_init_pf_dcb(pf);
11047 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011048 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011049 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011050 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011051 }
11052#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011053
11054 /* set up periodic task facility */
11055 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11056 pf->service_timer_period = HZ;
11057
11058 INIT_WORK(&pf->service_task, i40e_service_task);
11059 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11060 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011061
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011062 /* NVM bit on means WoL disabled for the port */
11063 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011064 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011065 pf->wol_en = false;
11066 else
11067 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011068 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11069
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011070 /* set up the main switch operations */
11071 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011072 err = i40e_init_interrupt_scheme(pf);
11073 if (err)
11074 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011075
Mitch Williams505682c2014-05-20 08:01:37 +000011076 /* The number of VSIs reported by the FW is the minimum guaranteed
11077 * to us; HW supports far more and we share the remaining pool with
11078 * the other PFs. We allocate space for more than the guarantee with
11079 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011080 */
Mitch Williams505682c2014-05-20 08:01:37 +000011081 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11082 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11083 else
11084 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11085
11086 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011087 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11088 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011089 if (!pf->vsi) {
11090 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011091 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011092 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011093
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011094#ifdef CONFIG_PCI_IOV
11095 /* prep for VF support */
11096 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11097 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11098 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11099 if (pci_num_vf(pdev))
11100 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11101 }
11102#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011103 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011104 if (err) {
11105 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11106 goto err_vsis;
11107 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011108
11109 /* Make sure flow control is set according to current settings */
11110 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11111 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11112 dev_dbg(&pf->pdev->dev,
11113 "Set fc with err %s aq_err %s on get_phy_cap\n",
11114 i40e_stat_str(hw, err),
11115 i40e_aq_str(hw, hw->aq.asq_last_status));
11116 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11117 dev_dbg(&pf->pdev->dev,
11118 "Set fc with err %s aq_err %s on set_phy_config\n",
11119 i40e_stat_str(hw, err),
11120 i40e_aq_str(hw, hw->aq.asq_last_status));
11121 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11122 dev_dbg(&pf->pdev->dev,
11123 "Set fc with err %s aq_err %s on get_link_info\n",
11124 i40e_stat_str(hw, err),
11125 i40e_aq_str(hw, hw->aq.asq_last_status));
11126
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011127 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011128 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011129 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11130 i40e_vsi_open(pf->vsi[i]);
11131 break;
11132 }
11133 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011134
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011135 /* The driver only wants link up/down and module qualification
11136 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011137 */
11138 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011139 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011140 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011141 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011142 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011143 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11144 i40e_stat_str(&pf->hw, err),
11145 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011146
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011147 /* Reconfigure hardware for allowing smaller MSS in the case
11148 * of TSO, so that we avoid the MDD being fired and causing
11149 * a reset in the case of small MSS+TSO.
11150 */
11151 val = rd32(hw, I40E_REG_MSS);
11152 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11153 val &= ~I40E_REG_MSS_MIN_MASK;
11154 val |= I40E_64BYTE_MSS;
11155 wr32(hw, I40E_REG_MSS, val);
11156 }
11157
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011158 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011159 msleep(75);
11160 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11161 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011162 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11163 i40e_stat_str(&pf->hw, err),
11164 i40e_aq_str(&pf->hw,
11165 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011166 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011167 /* The main driver is (mostly) up and happy. We need to set this state
11168 * before setting up the misc vector or we get a race and the vector
11169 * ends up disabled forever.
11170 */
11171 clear_bit(__I40E_DOWN, &pf->state);
11172
11173 /* In case of MSIX we are going to setup the misc vector right here
11174 * to handle admin queue events etc. In case of legacy and MSI
11175 * the misc functionality and queue processing is combined in
11176 * the same vector and that gets setup at open.
11177 */
11178 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11179 err = i40e_setup_misc_vector(pf);
11180 if (err) {
11181 dev_info(&pdev->dev,
11182 "setup of misc vector failed: %d\n", err);
11183 goto err_vsis;
11184 }
11185 }
11186
Greg Rosedf805f62014-04-04 04:43:16 +000011187#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011188 /* prep for VF support */
11189 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011190 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11191 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011192 /* disable link interrupts for VFs */
11193 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11194 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11195 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11196 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011197
11198 if (pci_num_vf(pdev)) {
11199 dev_info(&pdev->dev,
11200 "Active VFs found, allocating resources.\n");
11201 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11202 if (err)
11203 dev_info(&pdev->dev,
11204 "Error %d allocating resources for existing VFs\n",
11205 err);
11206 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011207 }
Greg Rosedf805f62014-04-04 04:43:16 +000011208#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011209
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011210 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11211 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11212 pf->num_iwarp_msix,
11213 I40E_IWARP_IRQ_PILE_ID);
11214 if (pf->iwarp_base_vector < 0) {
11215 dev_info(&pdev->dev,
11216 "failed to get tracking for %d vectors for IWARP err=%d\n",
11217 pf->num_iwarp_msix, pf->iwarp_base_vector);
11218 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11219 }
11220 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011221
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011222 i40e_dbg_pf_init(pf);
11223
11224 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011225 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011226
11227 /* since everything's happy, start the service_task timer */
11228 mod_timer(&pf->service_timer,
11229 round_jiffies(jiffies + pf->service_timer_period));
11230
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011231 /* add this PF to client device list and launch a client service task */
11232 err = i40e_lan_add_device(pf);
11233 if (err)
11234 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11235 err);
11236
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011237#define PCI_SPEED_SIZE 8
11238#define PCI_WIDTH_SIZE 8
11239 /* Devices on the IOSF bus do not have this information
11240 * and will report PCI Gen 1 x 1 by default so don't bother
11241 * checking them.
11242 */
11243 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11244 char speed[PCI_SPEED_SIZE] = "Unknown";
11245 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011246
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011247 /* Get the negotiated link width and speed from PCI config
11248 * space
11249 */
11250 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11251 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011252
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011253 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011254
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011255 switch (hw->bus.speed) {
11256 case i40e_bus_speed_8000:
11257 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11258 case i40e_bus_speed_5000:
11259 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11260 case i40e_bus_speed_2500:
11261 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11262 default:
11263 break;
11264 }
11265 switch (hw->bus.width) {
11266 case i40e_bus_width_pcie_x8:
11267 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11268 case i40e_bus_width_pcie_x4:
11269 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11270 case i40e_bus_width_pcie_x2:
11271 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11272 case i40e_bus_width_pcie_x1:
11273 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11274 default:
11275 break;
11276 }
11277
11278 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11279 speed, width);
11280
11281 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11282 hw->bus.speed < i40e_bus_speed_8000) {
11283 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11284 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11285 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011286 }
11287
Catherine Sullivane8278452015-02-06 08:52:08 +000011288 /* get the requested speeds from the fw */
11289 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11290 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011291 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11292 i40e_stat_str(&pf->hw, err),
11293 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011294 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11295
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011296 /* get the supported phy types from the fw */
11297 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11298 if (err)
11299 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11300 i40e_stat_str(&pf->hw, err),
11301 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011302
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011303 /* Add a filter to drop all Flow control frames from any VSI from being
11304 * transmitted. By doing so we stop a malicious VF from sending out
11305 * PAUSE or PFC frames and potentially controlling traffic for other
11306 * PF/VF VSIs.
11307 * The FW can still send Flow control frames if enabled.
11308 */
11309 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11310 pf->main_vsi_seid);
11311
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011312 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011313 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11314 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011315 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11316 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011317 /* print a string summarizing features */
11318 i40e_print_features(pf);
11319
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011320 return 0;
11321
11322 /* Unwind what we've done if something failed in the setup */
11323err_vsis:
11324 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011325 i40e_clear_interrupt_scheme(pf);
11326 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011327err_switch_setup:
11328 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011329 del_timer_sync(&pf->service_timer);
11330err_mac_addr:
11331err_configure_lan_hmc:
11332 (void)i40e_shutdown_lan_hmc(hw);
11333err_init_lan_hmc:
11334 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011335err_sw_init:
11336err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011337err_pf_reset:
11338 iounmap(hw->hw_addr);
11339err_ioremap:
11340 kfree(pf);
11341err_pf_alloc:
11342 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011343 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011344err_pci_reg:
11345err_dma:
11346 pci_disable_device(pdev);
11347 return err;
11348}
11349
11350/**
11351 * i40e_remove - Device removal routine
11352 * @pdev: PCI device information struct
11353 *
11354 * i40e_remove is called by the PCI subsystem to alert the driver
11355 * that is should release a PCI device. This could be caused by a
11356 * Hot-Plug event, or because the driver is going to be removed from
11357 * memory.
11358 **/
11359static void i40e_remove(struct pci_dev *pdev)
11360{
11361 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011362 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011363 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011364 int i;
11365
11366 i40e_dbg_pf_exit(pf);
11367
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011368 i40e_ptp_stop(pf);
11369
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011370 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011371 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11372 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011373
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011374 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011375 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011376 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011377 if (pf->service_timer.data)
11378 del_timer_sync(&pf->service_timer);
11379 if (pf->service_task.func)
11380 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011381
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011382 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11383 i40e_free_vfs(pf);
11384 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11385 }
11386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011387 i40e_fdir_teardown(pf);
11388
11389 /* If there is a switch structure or any orphans, remove them.
11390 * This will leave only the PF's VSI remaining.
11391 */
11392 for (i = 0; i < I40E_MAX_VEB; i++) {
11393 if (!pf->veb[i])
11394 continue;
11395
11396 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11397 pf->veb[i]->uplink_seid == 0)
11398 i40e_switch_branch_release(pf->veb[i]);
11399 }
11400
11401 /* Now we can shutdown the PF's VSI, just before we kill
11402 * adminq and hmc.
11403 */
11404 if (pf->vsi[pf->lan_vsi])
11405 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11406
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011407 /* remove attached clients */
11408 ret_code = i40e_lan_del_device(pf);
11409 if (ret_code) {
11410 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11411 ret_code);
11412 }
11413
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011414 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011415 if (hw->hmc.hmc_obj) {
11416 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011417 if (ret_code)
11418 dev_warn(&pdev->dev,
11419 "Failed to destroy the HMC resources: %d\n",
11420 ret_code);
11421 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011422
11423 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011424 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011425
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011426 /* destroy the locks only once, here */
11427 mutex_destroy(&hw->aq.arq_mutex);
11428 mutex_destroy(&hw->aq.asq_mutex);
11429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011430 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11431 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011432 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011433 if (pf->vsi[i]) {
11434 i40e_vsi_clear_rings(pf->vsi[i]);
11435 i40e_vsi_clear(pf->vsi[i]);
11436 pf->vsi[i] = NULL;
11437 }
11438 }
11439
11440 for (i = 0; i < I40E_MAX_VEB; i++) {
11441 kfree(pf->veb[i]);
11442 pf->veb[i] = NULL;
11443 }
11444
11445 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446 kfree(pf->vsi);
11447
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011448 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011449 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011450 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011451
11452 pci_disable_pcie_error_reporting(pdev);
11453 pci_disable_device(pdev);
11454}
11455
11456/**
11457 * i40e_pci_error_detected - warning that something funky happened in PCI land
11458 * @pdev: PCI device information struct
11459 *
11460 * Called to warn that something happened and the error handling steps
11461 * are in progress. Allows the driver to quiesce things, be ready for
11462 * remediation.
11463 **/
11464static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11465 enum pci_channel_state error)
11466{
11467 struct i40e_pf *pf = pci_get_drvdata(pdev);
11468
11469 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11470
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011471 if (!pf) {
11472 dev_info(&pdev->dev,
11473 "Cannot recover - error happened during device probe\n");
11474 return PCI_ERS_RESULT_DISCONNECT;
11475 }
11476
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011477 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011478 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11479 rtnl_lock();
11480 i40e_prep_for_reset(pf);
11481 rtnl_unlock();
11482 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011483
11484 /* Request a slot reset */
11485 return PCI_ERS_RESULT_NEED_RESET;
11486}
11487
11488/**
11489 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11490 * @pdev: PCI device information struct
11491 *
11492 * Called to find if the driver can work with the device now that
11493 * the pci slot has been reset. If a basic connection seems good
11494 * (registers are readable and have sane content) then return a
11495 * happy little PCI_ERS_RESULT_xxx.
11496 **/
11497static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11498{
11499 struct i40e_pf *pf = pci_get_drvdata(pdev);
11500 pci_ers_result_t result;
11501 int err;
11502 u32 reg;
11503
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011504 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011505 if (pci_enable_device_mem(pdev)) {
11506 dev_info(&pdev->dev,
11507 "Cannot re-enable PCI device after reset.\n");
11508 result = PCI_ERS_RESULT_DISCONNECT;
11509 } else {
11510 pci_set_master(pdev);
11511 pci_restore_state(pdev);
11512 pci_save_state(pdev);
11513 pci_wake_from_d3(pdev, false);
11514
11515 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11516 if (reg == 0)
11517 result = PCI_ERS_RESULT_RECOVERED;
11518 else
11519 result = PCI_ERS_RESULT_DISCONNECT;
11520 }
11521
11522 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11523 if (err) {
11524 dev_info(&pdev->dev,
11525 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11526 err);
11527 /* non-fatal, continue */
11528 }
11529
11530 return result;
11531}
11532
11533/**
11534 * i40e_pci_error_resume - restart operations after PCI error recovery
11535 * @pdev: PCI device information struct
11536 *
11537 * Called to allow the driver to bring things back up after PCI error
11538 * and/or reset recovery has finished.
11539 **/
11540static void i40e_pci_error_resume(struct pci_dev *pdev)
11541{
11542 struct i40e_pf *pf = pci_get_drvdata(pdev);
11543
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011544 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011545 if (test_bit(__I40E_SUSPENDED, &pf->state))
11546 return;
11547
11548 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011549 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011550 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011551}
11552
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011553/**
Joshua Hay1d680052016-12-12 15:44:08 -080011554 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11555 * using the mac_address_write admin q function
11556 * @pf: pointer to i40e_pf struct
11557 **/
11558static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11559{
11560 struct i40e_hw *hw = &pf->hw;
11561 i40e_status ret;
11562 u8 mac_addr[6];
11563 u16 flags = 0;
11564
11565 /* Get current MAC address in case it's an LAA */
11566 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11567 ether_addr_copy(mac_addr,
11568 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11569 } else {
11570 dev_err(&pf->pdev->dev,
11571 "Failed to retrieve MAC address; using default\n");
11572 ether_addr_copy(mac_addr, hw->mac.addr);
11573 }
11574
11575 /* The FW expects the mac address write cmd to first be called with
11576 * one of these flags before calling it again with the multicast
11577 * enable flags.
11578 */
11579 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11580
11581 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11582 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11583
11584 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11585 if (ret) {
11586 dev_err(&pf->pdev->dev,
11587 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11588 return;
11589 }
11590
11591 flags = I40E_AQC_MC_MAG_EN
11592 | I40E_AQC_WOL_PRESERVE_ON_PFR
11593 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11594 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11595 if (ret)
11596 dev_err(&pf->pdev->dev,
11597 "Failed to enable Multicast Magic Packet wake up\n");
11598}
11599
11600/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011601 * i40e_shutdown - PCI callback for shutting down
11602 * @pdev: PCI device information struct
11603 **/
11604static void i40e_shutdown(struct pci_dev *pdev)
11605{
11606 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011607 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011608
11609 set_bit(__I40E_SUSPENDED, &pf->state);
11610 set_bit(__I40E_DOWN, &pf->state);
11611 rtnl_lock();
11612 i40e_prep_for_reset(pf);
11613 rtnl_unlock();
11614
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011615 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11616 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11617
Catherine Sullivan02b42492015-07-10 19:35:59 -040011618 del_timer_sync(&pf->service_timer);
11619 cancel_work_sync(&pf->service_task);
11620 i40e_fdir_teardown(pf);
11621
Joshua Hay1d680052016-12-12 15:44:08 -080011622 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11623 i40e_enable_mc_magic_wake(pf);
11624
Catherine Sullivan02b42492015-07-10 19:35:59 -040011625 rtnl_lock();
11626 i40e_prep_for_reset(pf);
11627 rtnl_unlock();
11628
11629 wr32(hw, I40E_PFPM_APM,
11630 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11631 wr32(hw, I40E_PFPM_WUFC,
11632 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11633
Shannon Nelsone1477582015-02-21 06:44:33 +000011634 i40e_clear_interrupt_scheme(pf);
11635
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011636 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011637 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011638 pci_set_power_state(pdev, PCI_D3hot);
11639 }
11640}
11641
11642#ifdef CONFIG_PM
11643/**
11644 * i40e_suspend - PCI callback for moving to D3
11645 * @pdev: PCI device information struct
11646 **/
11647static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11648{
11649 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011650 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011651 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011652
11653 set_bit(__I40E_SUSPENDED, &pf->state);
11654 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011655
Joshua Hay1d680052016-12-12 15:44:08 -080011656 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11657 i40e_enable_mc_magic_wake(pf);
11658
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011659 rtnl_lock();
11660 i40e_prep_for_reset(pf);
11661 rtnl_unlock();
11662
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011663 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11664 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11665
Greg Roseb33d3b72016-05-16 10:26:37 -070011666 i40e_stop_misc_vector(pf);
11667
Greg Rose059ff692016-05-16 10:26:38 -070011668 retval = pci_save_state(pdev);
11669 if (retval)
11670 return retval;
11671
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011672 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011673 pci_set_power_state(pdev, PCI_D3hot);
11674
Greg Rose059ff692016-05-16 10:26:38 -070011675 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011676}
11677
11678/**
11679 * i40e_resume - PCI callback for waking up from D3
11680 * @pdev: PCI device information struct
11681 **/
11682static int i40e_resume(struct pci_dev *pdev)
11683{
11684 struct i40e_pf *pf = pci_get_drvdata(pdev);
11685 u32 err;
11686
11687 pci_set_power_state(pdev, PCI_D0);
11688 pci_restore_state(pdev);
11689 /* pci_restore_state() clears dev->state_saves, so
11690 * call pci_save_state() again to restore it.
11691 */
11692 pci_save_state(pdev);
11693
11694 err = pci_enable_device_mem(pdev);
11695 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011696 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011697 return err;
11698 }
11699 pci_set_master(pdev);
11700
11701 /* no wakeup events while running */
11702 pci_wake_from_d3(pdev, false);
11703
11704 /* handling the reset will rebuild the device state */
11705 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11706 clear_bit(__I40E_DOWN, &pf->state);
11707 rtnl_lock();
11708 i40e_reset_and_rebuild(pf, false);
11709 rtnl_unlock();
11710 }
11711
11712 return 0;
11713}
11714
11715#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011716static const struct pci_error_handlers i40e_err_handler = {
11717 .error_detected = i40e_pci_error_detected,
11718 .slot_reset = i40e_pci_error_slot_reset,
11719 .resume = i40e_pci_error_resume,
11720};
11721
11722static struct pci_driver i40e_driver = {
11723 .name = i40e_driver_name,
11724 .id_table = i40e_pci_tbl,
11725 .probe = i40e_probe,
11726 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011727#ifdef CONFIG_PM
11728 .suspend = i40e_suspend,
11729 .resume = i40e_resume,
11730#endif
11731 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011732 .err_handler = &i40e_err_handler,
11733 .sriov_configure = i40e_pci_sriov_configure,
11734};
11735
11736/**
11737 * i40e_init_module - Driver registration routine
11738 *
11739 * i40e_init_module is the first routine called when the driver is
11740 * loaded. All it does is register with the PCI subsystem.
11741 **/
11742static int __init i40e_init_module(void)
11743{
11744 pr_info("%s: %s - version %s\n", i40e_driver_name,
11745 i40e_driver_string, i40e_driver_version_str);
11746 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011747
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011748 /* we will see if single thread per module is enough for now,
11749 * it can't be any worse than using the system workqueue which
11750 * was already single threaded
11751 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011752 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11753 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011754 if (!i40e_wq) {
11755 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11756 return -ENOMEM;
11757 }
11758
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011759 i40e_dbg_init();
11760 return pci_register_driver(&i40e_driver);
11761}
11762module_init(i40e_init_module);
11763
11764/**
11765 * i40e_exit_module - Driver exit cleanup routine
11766 *
11767 * i40e_exit_module is called just before the driver is removed
11768 * from memory.
11769 **/
11770static void __exit i40e_exit_module(void)
11771{
11772 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011773 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011774 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011775}
11776module_exit(i40e_exit_module);