blob: 7147c67a939de4e7146410f76439e4287311a972 [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 Kirsherb40c82e62015-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 Kirsherb40c82e62015-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 Jaine25d00b82015-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 Nelson13fd9772013-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
Alexander Duyckdab86af2017-03-14 10:15:27 -07002998 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
2999 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003000
3001 rx_ctx.base = (ring->dma / 128);
3002 rx_ctx.qlen = ring->count;
3003
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003004 /* use 32 byte descriptors */
3005 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003006
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003007 /* descriptor type is always zero
3008 * rx_ctx.dtype = 0;
3009 */
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003010 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003011
Jesse Brandeburgb32bfa172016-04-18 11:33:42 -07003012 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003013 if (hw->revision_id == 0)
3014 rx_ctx.lrxqthresh = 0;
3015 else
3016 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003017 rx_ctx.crcstrip = 1;
3018 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003019 /* this controls whether VLAN is stripped from inner headers */
3020 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003021 /* set the prefena field to 1 because the manual says to */
3022 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003023
3024 /* clear the context in the HMC */
3025 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3026 if (err) {
3027 dev_info(&vsi->back->pdev->dev,
3028 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3029 ring->queue_index, pf_q, err);
3030 return -ENOMEM;
3031 }
3032
3033 /* set the context in the HMC */
3034 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3035 if (err) {
3036 dev_info(&vsi->back->pdev->dev,
3037 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3038 ring->queue_index, pf_q, err);
3039 return -ENOMEM;
3040 }
3041
3042 /* cache tail for quicker writes, and clear the reg before use */
3043 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3044 writel(0, ring->tail);
3045
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003046 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003047
3048 return 0;
3049}
3050
3051/**
3052 * i40e_vsi_configure_tx - Configure the VSI for Tx
3053 * @vsi: VSI structure describing this set of rings and resources
3054 *
3055 * Configure the Tx VSI for operation.
3056 **/
3057static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3058{
3059 int err = 0;
3060 u16 i;
3061
Alexander Duyck9f65e152013-09-28 06:00:58 +00003062 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3063 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003064
3065 return err;
3066}
3067
3068/**
3069 * i40e_vsi_configure_rx - Configure the VSI for Rx
3070 * @vsi: the VSI being configured
3071 *
3072 * Configure the Rx VSI for operation.
3073 **/
3074static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3075{
3076 int err = 0;
3077 u16 i;
3078
Alexander Duyckdab86af2017-03-14 10:15:27 -07003079 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3080 vsi->max_frame = I40E_MAX_RXBUFFER;
3081 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3082#if (PAGE_SIZE < 8192)
3083 } else if (vsi->netdev->mtu <= ETH_DATA_LEN) {
3084 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3085 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3086#endif
3087 } else {
3088 vsi->max_frame = I40E_MAX_RXBUFFER;
3089 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3090 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003091
3092 /* set up individual rings */
3093 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00003094 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003095
3096 return err;
3097}
3098
3099/**
3100 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3101 * @vsi: ptr to the VSI
3102 **/
3103static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3104{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003105 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003106 u16 qoffset, qcount;
3107 int i, n;
3108
Parikh, Neeravcd238a32015-02-21 06:43:37 +00003109 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3110 /* Reset the TC information */
3111 for (i = 0; i < vsi->num_queue_pairs; i++) {
3112 rx_ring = vsi->rx_rings[i];
3113 tx_ring = vsi->tx_rings[i];
3114 rx_ring->dcb_tc = 0;
3115 tx_ring->dcb_tc = 0;
3116 }
3117 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003118
3119 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04003120 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003121 continue;
3122
3123 qoffset = vsi->tc_config.tc_info[n].qoffset;
3124 qcount = vsi->tc_config.tc_info[n].qcount;
3125 for (i = qoffset; i < (qoffset + qcount); i++) {
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00003126 rx_ring = vsi->rx_rings[i];
3127 tx_ring = vsi->tx_rings[i];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003128 rx_ring->dcb_tc = n;
3129 tx_ring->dcb_tc = n;
3130 }
3131 }
3132}
3133
3134/**
3135 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3136 * @vsi: ptr to the VSI
3137 **/
3138static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3139{
Tushar Daveb1b15df2016-07-01 10:11:20 -07003140 struct i40e_pf *pf = vsi->back;
3141 int err;
3142
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003143 if (vsi->netdev)
3144 i40e_set_rx_mode(vsi->netdev);
Tushar Daveb1b15df2016-07-01 10:11:20 -07003145
3146 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
3147 err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
3148 if (err) {
3149 dev_warn(&pf->pdev->dev,
3150 "could not set up macaddr; err %d\n", err);
3151 }
3152 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003153}
3154
3155/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003156 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3157 * @vsi: Pointer to the targeted VSI
3158 *
3159 * This function replays the hlist on the hw where all the SB Flow Director
3160 * filters were saved.
3161 **/
3162static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3163{
3164 struct i40e_fdir_filter *filter;
3165 struct i40e_pf *pf = vsi->back;
3166 struct hlist_node *node;
3167
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00003168 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3169 return;
3170
Jacob Keller6d069422017-02-06 14:38:44 -08003171 /* Reset FDir counters as we're replaying all existing filters */
Jacob Keller097dbf52017-02-06 14:38:46 -08003172 pf->fd_tcp4_filter_cnt = 0;
3173 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08003174 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08003175 pf->fd_ip4_filter_cnt = 0;
Jacob Keller6d069422017-02-06 14:38:44 -08003176
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00003177 hlist_for_each_entry_safe(filter, node,
3178 &pf->fdir_filter_list, fdir_node) {
3179 i40e_add_del_fdir(vsi, filter, true);
3180 }
3181}
3182
3183/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003184 * i40e_vsi_configure - Set up the VSI for action
3185 * @vsi: the VSI being configured
3186 **/
3187static int i40e_vsi_configure(struct i40e_vsi *vsi)
3188{
3189 int err;
3190
3191 i40e_set_vsi_rx_mode(vsi);
3192 i40e_restore_vlan(vsi);
3193 i40e_vsi_config_dcb_rings(vsi);
3194 err = i40e_vsi_configure_tx(vsi);
3195 if (!err)
3196 err = i40e_vsi_configure_rx(vsi);
3197
3198 return err;
3199}
3200
3201/**
3202 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3203 * @vsi: the VSI being configured
3204 **/
3205static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3206{
3207 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003208 struct i40e_hw *hw = &pf->hw;
3209 u16 vector;
3210 int i, q;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003211 u32 qp;
3212
3213 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
3214 * and PFINT_LNKLSTn registers, e.g.:
3215 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
3216 */
3217 qp = vsi->base_queue;
3218 vector = vsi->base_vector;
Alexander Duyck493fb302013-09-28 07:01:44 +00003219 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003220 struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3221
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003222 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003223 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003224 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3225 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3226 q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003227 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003228 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3229 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3230 q_vector->tx.itr);
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003231 wr32(hw, I40E_PFINT_RATEN(vector - 1),
Alan Brady1c0e6a32016-11-28 16:06:02 -08003232 i40e_intrl_usec_to_reg(vsi->int_rate_limit));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003233
3234 /* Linked list for the queuepairs assigned to this vector */
3235 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3236 for (q = 0; q < q_vector->num_ringpairs; q++) {
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04003237 u32 val;
3238
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003239 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3240 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3241 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3242 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3243 (I40E_QUEUE_TYPE_TX
3244 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3245
3246 wr32(hw, I40E_QINT_RQCTL(qp), val);
3247
3248 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3249 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3250 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3251 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
3252 (I40E_QUEUE_TYPE_RX
3253 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3254
3255 /* Terminate the linked list */
3256 if (q == (q_vector->num_ringpairs - 1))
3257 val |= (I40E_QUEUE_END_OF_LIST
3258 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3259
3260 wr32(hw, I40E_QINT_TQCTL(qp), val);
3261 qp++;
3262 }
3263 }
3264
3265 i40e_flush(hw);
3266}
3267
3268/**
3269 * i40e_enable_misc_int_causes - enable the non-queue interrupts
3270 * @hw: ptr to the hardware info
3271 **/
Jacob Kellerab437b52014-12-14 01:55:08 +00003272static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003273{
Jacob Kellerab437b52014-12-14 01:55:08 +00003274 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003275 u32 val;
3276
3277 /* clear things first */
3278 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
3279 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
3280
3281 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
3282 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
3283 I40E_PFINT_ICR0_ENA_GRST_MASK |
3284 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3285 I40E_PFINT_ICR0_ENA_GPIO_MASK |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003286 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
3287 I40E_PFINT_ICR0_ENA_VFLR_MASK |
3288 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3289
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003290 if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3291 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3292
Jacob Kellerab437b52014-12-14 01:55:08 +00003293 if (pf->flags & I40E_FLAG_PTP)
3294 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3295
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003296 wr32(hw, I40E_PFINT_ICR0_ENA, val);
3297
3298 /* SW_ITR_IDX = 0, but don't change INTENA */
Anjali Singhai Jain84ed40e2013-11-26 10:49:32 +00003299 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3300 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003301
3302 /* OTHER_ITR_IDX = 0 */
3303 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3304}
3305
3306/**
3307 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3308 * @vsi: the VSI being configured
3309 **/
3310static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3311{
Alexander Duyck493fb302013-09-28 07:01:44 +00003312 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003313 struct i40e_pf *pf = vsi->back;
3314 struct i40e_hw *hw = &pf->hw;
3315 u32 val;
3316
3317 /* set the ITR configuration */
Jesse Brandeburgee2319c2015-09-28 14:16:54 -04003318 q_vector->itr_countdown = ITR_COUNTDOWN_START;
Kan Lianga75e8002016-02-19 09:24:04 -05003319 q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003320 q_vector->rx.latency_range = I40E_LOW_LATENCY;
3321 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
Kan Lianga75e8002016-02-19 09:24:04 -05003322 q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003323 q_vector->tx.latency_range = I40E_LOW_LATENCY;
3324 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
3325
Jacob Kellerab437b52014-12-14 01:55:08 +00003326 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003327
3328 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3329 wr32(hw, I40E_PFINT_LNKLST0, 0);
3330
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00003331 /* Associate the queue pair to the vector and enable the queue int */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003332 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3333 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3334 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3335
3336 wr32(hw, I40E_QINT_RQCTL(0), val);
3337
3338 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3339 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3340 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3341
3342 wr32(hw, I40E_QINT_TQCTL(0), val);
3343 i40e_flush(hw);
3344}
3345
3346/**
Mitch Williams2ef28cf2013-11-28 06:39:32 +00003347 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3348 * @pf: board private structure
3349 **/
3350void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3351{
3352 struct i40e_hw *hw = &pf->hw;
3353
3354 wr32(hw, I40E_PFINT_DYN_CTL0,
3355 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3356 i40e_flush(hw);
3357}
3358
3359/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003360 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3361 * @pf: board private structure
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003362 * @clearpba: true when all pending interrupt events should be cleared
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003363 **/
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003364void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003365{
3366 struct i40e_hw *hw = &pf->hw;
3367 u32 val;
3368
3369 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003370 (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003371 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3372
3373 wr32(hw, I40E_PFINT_DYN_CTL0, val);
3374 i40e_flush(hw);
3375}
3376
3377/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003378 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3379 * @irq: interrupt number
3380 * @data: pointer to a q_vector
3381 **/
3382static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3383{
3384 struct i40e_q_vector *q_vector = data;
3385
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003386 if (!q_vector->tx.ring && !q_vector->rx.ring)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003387 return IRQ_HANDLED;
3388
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003389 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003390
3391 return IRQ_HANDLED;
3392}
3393
3394/**
Alan Brady96db7762016-09-14 16:24:38 -07003395 * i40e_irq_affinity_notify - Callback for affinity changes
3396 * @notify: context as to what irq was changed
3397 * @mask: the new affinity mask
3398 *
3399 * This is a callback function used by the irq_set_affinity_notifier function
3400 * so that we may register to receive changes to the irq affinity masks.
3401 **/
3402static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3403 const cpumask_t *mask)
3404{
3405 struct i40e_q_vector *q_vector =
3406 container_of(notify, struct i40e_q_vector, affinity_notify);
3407
3408 q_vector->affinity_mask = *mask;
3409}
3410
3411/**
3412 * i40e_irq_affinity_release - Callback for affinity notifier release
3413 * @ref: internal core kernel usage
3414 *
3415 * This is a callback function used by the irq_set_affinity_notifier function
3416 * to inform the current notification subscriber that they will no longer
3417 * receive notifications.
3418 **/
3419static void i40e_irq_affinity_release(struct kref *ref) {}
3420
3421/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003422 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3423 * @vsi: the VSI being configured
3424 * @basename: name for the vector
3425 *
3426 * Allocates MSI-X vectors and requests interrupts from the kernel.
3427 **/
3428static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3429{
3430 int q_vectors = vsi->num_q_vectors;
3431 struct i40e_pf *pf = vsi->back;
3432 int base = vsi->base_vector;
3433 int rx_int_idx = 0;
3434 int tx_int_idx = 0;
3435 int vector, err;
Alan Brady96db7762016-09-14 16:24:38 -07003436 int irq_num;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003437
3438 for (vector = 0; vector < q_vectors; vector++) {
Alexander Duyck493fb302013-09-28 07:01:44 +00003439 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003440
Alan Brady96db7762016-09-14 16:24:38 -07003441 irq_num = pf->msix_entries[base + vector].vector;
3442
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003443 if (q_vector->tx.ring && q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003444 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3445 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3446 tx_int_idx++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003447 } else if (q_vector->rx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003448 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3449 "%s-%s-%d", basename, "rx", rx_int_idx++);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003450 } else if (q_vector->tx.ring) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003451 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3452 "%s-%s-%d", basename, "tx", tx_int_idx++);
3453 } else {
3454 /* skip this unused q_vector */
3455 continue;
3456 }
Alan Brady96db7762016-09-14 16:24:38 -07003457 err = request_irq(irq_num,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003458 vsi->irq_handler,
3459 0,
3460 q_vector->name,
3461 q_vector);
3462 if (err) {
3463 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003464 "MSIX request_irq failed, error: %d\n", err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003465 goto free_queue_irqs;
3466 }
Alan Brady96db7762016-09-14 16:24:38 -07003467
3468 /* register for affinity change notifications */
3469 q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3470 q_vector->affinity_notify.release = i40e_irq_affinity_release;
3471 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003472 /* assign the mask for this irq */
Alan Brady96db7762016-09-14 16:24:38 -07003473 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003474 }
3475
Shannon Nelson63741842014-04-23 04:50:16 +00003476 vsi->irqs_ready = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003477 return 0;
3478
3479free_queue_irqs:
3480 while (vector) {
3481 vector--;
Alan Brady96db7762016-09-14 16:24:38 -07003482 irq_num = pf->msix_entries[base + vector].vector;
3483 irq_set_affinity_notifier(irq_num, NULL);
3484 irq_set_affinity_hint(irq_num, NULL);
3485 free_irq(irq_num, &vsi->q_vectors[vector]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003486 }
3487 return err;
3488}
3489
3490/**
3491 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3492 * @vsi: the VSI being un-configured
3493 **/
3494static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3495{
3496 struct i40e_pf *pf = vsi->back;
3497 struct i40e_hw *hw = &pf->hw;
3498 int base = vsi->base_vector;
3499 int i;
3500
3501 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00003502 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
3503 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003504 }
3505
3506 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3507 for (i = vsi->base_vector;
3508 i < (vsi->num_q_vectors + vsi->base_vector); i++)
3509 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3510
3511 i40e_flush(hw);
3512 for (i = 0; i < vsi->num_q_vectors; i++)
3513 synchronize_irq(pf->msix_entries[i + base].vector);
3514 } else {
3515 /* Legacy and MSI mode - this stops all interrupt handling */
3516 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3517 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3518 i40e_flush(hw);
3519 synchronize_irq(pf->pdev->irq);
3520 }
3521}
3522
3523/**
3524 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3525 * @vsi: the VSI being configured
3526 **/
3527static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3528{
3529 struct i40e_pf *pf = vsi->back;
3530 int i;
3531
3532 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Jesse Brandeburg78455482015-07-23 16:54:41 -04003533 for (i = 0; i < vsi->num_q_vectors; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003534 i40e_irq_dynamic_enable(vsi, i);
3535 } else {
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003536 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003537 }
3538
Jesse Brandeburg1022cb62013-09-28 07:13:08 +00003539 i40e_flush(&pf->hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003540 return 0;
3541}
3542
3543/**
3544 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
3545 * @pf: board private structure
3546 **/
3547static void i40e_stop_misc_vector(struct i40e_pf *pf)
3548{
3549 /* Disable ICR 0 */
3550 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3551 i40e_flush(&pf->hw);
3552}
3553
3554/**
3555 * i40e_intr - MSI/Legacy and non-queue interrupt handler
3556 * @irq: interrupt number
3557 * @data: pointer to a q_vector
3558 *
3559 * This is the handler used for all MSI/Legacy interrupts, and deals
3560 * with both queue and non-queue interrupts. This is also used in
3561 * MSIX mode to handle the non-queue interrupts.
3562 **/
3563static irqreturn_t i40e_intr(int irq, void *data)
3564{
3565 struct i40e_pf *pf = (struct i40e_pf *)data;
3566 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003567 irqreturn_t ret = IRQ_NONE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003568 u32 icr0, icr0_remaining;
3569 u32 val, ena_mask;
3570
3571 icr0 = rd32(hw, I40E_PFINT_ICR0);
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003572 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003573
Shannon Nelson116a57d2013-09-28 07:13:59 +00003574 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
3575 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003576 goto enable_intr;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003577
Shannon Nelsoncd92e722013-11-16 10:00:44 +00003578 /* if interrupt but no bits showing, must be SWINT */
3579 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
3580 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
3581 pf->sw_int_count++;
3582
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003583 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
3584 (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
3585 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3586 icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
Carolyn Wyborny23bb6dc2016-11-08 13:05:12 -08003587 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
Anjali Singhai Jain0d8e1432015-06-05 12:20:32 -04003588 }
3589
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003590 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
3591 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003592 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
3593 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003594
Anjali Singhai Jaina16ae2d2016-01-15 14:33:16 -08003595 /* We do not have a way to disarm Queue causes while leaving
3596 * interrupt enabled for all other causes, ideally
3597 * interrupt should be disabled while we are in NAPI but
3598 * this is not a performance path and napi_schedule()
3599 * can deal with rescheduling.
3600 */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003601 if (!test_bit(__I40E_DOWN, &pf->state))
Alexander Duyck5d3465a2015-09-29 15:19:50 -07003602 napi_schedule_irqoff(&q_vector->napi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003603 }
3604
3605 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
3606 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3607 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08003608 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003609 }
3610
3611 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
3612 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
3613 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
3614 }
3615
3616 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
3617 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
3618 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
3619 }
3620
3621 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
3622 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
3623 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
3624 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
3625 val = rd32(hw, I40E_GLGEN_RSTAT);
3626 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
3627 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003628 if (val == I40E_RESET_CORER) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003629 pf->corer_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003630 } else if (val == I40E_RESET_GLOBR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003631 pf->globr_count++;
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003632 } else if (val == I40E_RESET_EMPR) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003633 pf->empr_count++;
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00003634 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00003635 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003636 }
3637
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003638 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
3639 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
3640 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
Anjali Singhai Jain25fc0e62015-03-31 00:45:01 -07003641 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
3642 rd32(hw, I40E_PFHMC_ERRORINFO),
3643 rd32(hw, I40E_PFHMC_ERRORDATA));
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003644 }
3645
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003646 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
3647 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
3648
3649 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
Jacob Kellercafa1fc2014-04-24 18:05:03 -07003650 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003651 i40e_ptp_tx_hwtstamp(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003652 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003653 }
3654
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003655 /* If a critical error is pending we have no choice but to reset the
3656 * device.
3657 * Report and mask out any remaining unexpected interrupts.
3658 */
3659 icr0_remaining = icr0 & ena_mask;
3660 if (icr0_remaining) {
3661 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
3662 icr0_remaining);
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003663 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003664 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
Anjali Singhai Jainc0c28972014-02-12 01:45:34 +00003665 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
Anjali Singhai Jain9c010ee2013-11-28 06:39:20 +00003666 dev_info(&pf->pdev->dev, "device will be reset\n");
3667 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
3668 i40e_service_event_schedule(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003669 }
3670 ena_mask &= ~icr0_remaining;
3671 }
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003672 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003674enable_intr:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003675 /* re-enable interrupt causes */
3676 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003677 if (!test_bit(__I40E_DOWN, &pf->state)) {
3678 i40e_service_event_schedule(pf);
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08003679 i40e_irq_dynamic_enable_icr0(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003680 }
3681
Anjali Singhai Jain5e823062013-12-18 13:45:49 +00003682 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003683}
3684
3685/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003686 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
3687 * @tx_ring: tx ring to clean
3688 * @budget: how many cleans we're allowed
3689 *
3690 * Returns true if there's any budget left (e.g. the clean is finished)
3691 **/
3692static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
3693{
3694 struct i40e_vsi *vsi = tx_ring->vsi;
3695 u16 i = tx_ring->next_to_clean;
3696 struct i40e_tx_buffer *tx_buf;
3697 struct i40e_tx_desc *tx_desc;
3698
3699 tx_buf = &tx_ring->tx_bi[i];
3700 tx_desc = I40E_TX_DESC(tx_ring, i);
3701 i -= tx_ring->count;
3702
3703 do {
3704 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
3705
3706 /* if next_to_watch is not set then there is no work pending */
3707 if (!eop_desc)
3708 break;
3709
3710 /* prevent any other reads prior to eop_desc */
3711 read_barrier_depends();
3712
3713 /* if the descriptor isn't done, no work yet to do */
3714 if (!(eop_desc->cmd_type_offset_bsz &
3715 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
3716 break;
3717
3718 /* clear next_to_watch to prevent false hangs */
3719 tx_buf->next_to_watch = NULL;
3720
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003721 tx_desc->buffer_addr = 0;
3722 tx_desc->cmd_type_offset_bsz = 0;
3723 /* move past filter desc */
3724 tx_buf++;
3725 tx_desc++;
3726 i++;
3727 if (unlikely(!i)) {
3728 i -= tx_ring->count;
3729 tx_buf = tx_ring->tx_bi;
3730 tx_desc = I40E_TX_DESC(tx_ring, 0);
3731 }
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003732 /* unmap skb header data */
3733 dma_unmap_single(tx_ring->dev,
3734 dma_unmap_addr(tx_buf, dma),
3735 dma_unmap_len(tx_buf, len),
3736 DMA_TO_DEVICE);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003737 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
3738 kfree(tx_buf->raw_buf);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003739
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003740 tx_buf->raw_buf = NULL;
3741 tx_buf->tx_flags = 0;
3742 tx_buf->next_to_watch = NULL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003743 dma_unmap_len_set(tx_buf, len, 0);
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003744 tx_desc->buffer_addr = 0;
3745 tx_desc->cmd_type_offset_bsz = 0;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003746
Anjali Singhai Jain49d7d932014-06-04 08:45:15 +00003747 /* move us past the eop_desc for start of next FD desc */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003748 tx_buf++;
3749 tx_desc++;
3750 i++;
3751 if (unlikely(!i)) {
3752 i -= tx_ring->count;
3753 tx_buf = tx_ring->tx_bi;
3754 tx_desc = I40E_TX_DESC(tx_ring, 0);
3755 }
3756
3757 /* update budget accounting */
3758 budget--;
3759 } while (likely(budget));
3760
3761 i += tx_ring->count;
3762 tx_ring->next_to_clean = i;
3763
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003764 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
Jesse Brandeburg78455482015-07-23 16:54:41 -04003765 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04003766
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08003767 return budget > 0;
3768}
3769
3770/**
3771 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
3772 * @irq: interrupt number
3773 * @data: pointer to a q_vector
3774 **/
3775static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
3776{
3777 struct i40e_q_vector *q_vector = data;
3778 struct i40e_vsi *vsi;
3779
3780 if (!q_vector->tx.ring)
3781 return IRQ_HANDLED;
3782
3783 vsi = q_vector->tx.ring->vsi;
3784 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
3785
3786 return IRQ_HANDLED;
3787}
3788
3789/**
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003790 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003791 * @vsi: the VSI being configured
3792 * @v_idx: vector index
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003793 * @qp_idx: queue pair index
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003794 **/
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003795static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003796{
Alexander Duyck493fb302013-09-28 07:01:44 +00003797 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyck9f65e152013-09-28 06:00:58 +00003798 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
3799 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003800
3801 tx_ring->q_vector = q_vector;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003802 tx_ring->next = q_vector->tx.ring;
3803 q_vector->tx.ring = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003804 q_vector->tx.count++;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003805
3806 rx_ring->q_vector = q_vector;
3807 rx_ring->next = q_vector->rx.ring;
3808 q_vector->rx.ring = rx_ring;
3809 q_vector->rx.count++;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003810}
3811
3812/**
3813 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
3814 * @vsi: the VSI being configured
3815 *
3816 * This function maps descriptor rings to the queue-specific vectors
3817 * we were allotted through the MSI-X enabling code. Ideally, we'd have
3818 * one vector per queue pair, but on a constrained vector budget, we
3819 * group the queue pairs as "efficiently" as possible.
3820 **/
3821static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
3822{
3823 int qp_remaining = vsi->num_queue_pairs;
3824 int q_vectors = vsi->num_q_vectors;
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003825 int num_ringpairs;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003826 int v_start = 0;
3827 int qp_idx = 0;
3828
3829 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
3830 * group them so there are multiple queues per vector.
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003831 * It is also important to go through all the vectors available to be
3832 * sure that if we don't use all the vectors, that the remaining vectors
3833 * are cleared. This is especially important when decreasing the
3834 * number of queues in use.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003835 */
Anjali Singhai Jain70114ec2014-06-03 23:50:14 +00003836 for (; v_start < q_vectors; v_start++) {
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003837 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
3838
3839 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
3840
3841 q_vector->num_ringpairs = num_ringpairs;
3842
3843 q_vector->rx.count = 0;
3844 q_vector->tx.count = 0;
3845 q_vector->rx.ring = NULL;
3846 q_vector->tx.ring = NULL;
3847
3848 while (num_ringpairs--) {
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04003849 i40e_map_vector_to_qp(vsi, v_start, qp_idx);
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00003850 qp_idx++;
3851 qp_remaining--;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003852 }
3853 }
3854}
3855
3856/**
3857 * i40e_vsi_request_irq - Request IRQ from the OS
3858 * @vsi: the VSI being configured
3859 * @basename: name for the vector
3860 **/
3861static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
3862{
3863 struct i40e_pf *pf = vsi->back;
3864 int err;
3865
3866 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3867 err = i40e_vsi_request_irq_msix(vsi, basename);
3868 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
3869 err = request_irq(pf->pdev->irq, i40e_intr, 0,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003870 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003871 else
3872 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00003873 pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003874
3875 if (err)
3876 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
3877
3878 return err;
3879}
3880
3881#ifdef CONFIG_NET_POLL_CONTROLLER
3882/**
Jesse Brandeburgd89d9672016-01-04 10:33:02 -08003883 * i40e_netpoll - A Polling 'interrupt' handler
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003884 * @netdev: network interface device structure
3885 *
3886 * This is used by netconsole to send skbs without having to re-enable
3887 * interrupts. It's not called while the normal interrupt routine is executing.
3888 **/
3889static void i40e_netpoll(struct net_device *netdev)
3890{
3891 struct i40e_netdev_priv *np = netdev_priv(netdev);
3892 struct i40e_vsi *vsi = np->vsi;
3893 struct i40e_pf *pf = vsi->back;
3894 int i;
3895
3896 /* if interface is down do nothing */
3897 if (test_bit(__I40E_DOWN, &vsi->state))
3898 return;
3899
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003900 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3901 for (i = 0; i < vsi->num_q_vectors; i++)
Alexander Duyck493fb302013-09-28 07:01:44 +00003902 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003903 } else {
3904 i40e_intr(pf->pdev->irq, netdev);
3905 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003906}
3907#endif
3908
3909/**
Neerav Parikh23527302014-06-03 23:50:15 +00003910 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
3911 * @pf: the PF being configured
3912 * @pf_q: the PF queue
3913 * @enable: enable or disable state of the queue
3914 *
3915 * This routine will wait for the given Tx queue of the PF to reach the
3916 * enabled or disabled state.
3917 * Returns -ETIMEDOUT in case of failing to reach the requested state after
3918 * multiple retries; else will return 0 in case of success.
3919 **/
3920static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
3921{
3922 int i;
3923 u32 tx_reg;
3924
3925 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
3926 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
3927 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3928 break;
3929
Neerav Parikhf98a2002014-09-13 07:40:44 +00003930 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00003931 }
3932 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
3933 return -ETIMEDOUT;
3934
3935 return 0;
3936}
3937
3938/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003939 * i40e_vsi_control_tx - Start or stop a VSI's rings
3940 * @vsi: the VSI being configured
3941 * @enable: start or stop the rings
3942 **/
3943static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
3944{
3945 struct i40e_pf *pf = vsi->back;
3946 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00003947 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003948 u32 tx_reg;
3949
3950 pf_q = vsi->base_queue;
3951 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Matt Jared351499ab2014-04-23 04:50:03 +00003952
3953 /* warn the TX unit of coming changes */
3954 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
3955 if (!enable)
Neerav Parikhf98a2002014-09-13 07:40:44 +00003956 usleep_range(10, 20);
Matt Jared351499ab2014-04-23 04:50:03 +00003957
Mitch Williams6c5ef622014-02-20 19:29:16 -08003958 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003959 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08003960 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
3961 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
3962 break;
3963 usleep_range(1000, 2000);
3964 }
Mitch Williamsfda972f2013-11-28 06:39:29 +00003965 /* Skip if the queue is already in the requested state */
Catherine Sullivan7c122002014-03-14 07:32:29 +00003966 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
Mitch Williamsfda972f2013-11-28 06:39:29 +00003967 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003968
3969 /* turn on/off the queue */
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003970 if (enable) {
3971 wr32(hw, I40E_QTX_HEAD(pf_q), 0);
Mitch Williams6c5ef622014-02-20 19:29:16 -08003972 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003973 } else {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003974 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
Shannon Nelsonc5c9eb92013-12-21 05:44:48 +00003975 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003976
3977 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
Neerav Parikh69129dc2014-11-12 00:18:46 +00003978 /* No waiting for the Tx queue to disable */
3979 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
3980 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003981
3982 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00003983 ret = i40e_pf_txq_wait(pf, pf_q, enable);
3984 if (ret) {
3985 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04003986 "VSI seid %d Tx ring %d %sable timeout\n",
3987 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00003988 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003989 }
3990 }
3991
Neerav Parikh23527302014-06-03 23:50:15 +00003992 return ret;
3993}
3994
3995/**
3996 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
3997 * @pf: the PF being configured
3998 * @pf_q: the PF queue
3999 * @enable: enable or disable state of the queue
4000 *
4001 * This routine will wait for the given Rx queue of the PF to reach the
4002 * enabled or disabled state.
4003 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4004 * multiple retries; else will return 0 in case of success.
4005 **/
4006static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4007{
4008 int i;
4009 u32 rx_reg;
4010
4011 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4012 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4013 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4014 break;
4015
Neerav Parikhf98a2002014-09-13 07:40:44 +00004016 usleep_range(10, 20);
Neerav Parikh23527302014-06-03 23:50:15 +00004017 }
4018 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4019 return -ETIMEDOUT;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00004020
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004021 return 0;
4022}
4023
4024/**
4025 * i40e_vsi_control_rx - Start or stop a VSI's rings
4026 * @vsi: the VSI being configured
4027 * @enable: start or stop the rings
4028 **/
4029static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
4030{
4031 struct i40e_pf *pf = vsi->back;
4032 struct i40e_hw *hw = &pf->hw;
Neerav Parikh23527302014-06-03 23:50:15 +00004033 int i, j, pf_q, ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004034 u32 rx_reg;
4035
4036 pf_q = vsi->base_queue;
4037 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
Mitch Williams6c5ef622014-02-20 19:29:16 -08004038 for (j = 0; j < 50; j++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004039 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
Mitch Williams6c5ef622014-02-20 19:29:16 -08004040 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4041 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4042 break;
4043 usleep_range(1000, 2000);
4044 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004045
Catherine Sullivan7c122002014-03-14 07:32:29 +00004046 /* Skip if the queue is already in the requested state */
4047 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4048 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004049
4050 /* turn on/off the queue */
4051 if (enable)
Mitch Williams6c5ef622014-02-20 19:29:16 -08004052 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004053 else
Mitch Williams6c5ef622014-02-20 19:29:16 -08004054 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004055 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004056 /* No waiting for the Tx queue to disable */
4057 if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
4058 continue;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004059
4060 /* wait for the change to finish */
Neerav Parikh23527302014-06-03 23:50:15 +00004061 ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4062 if (ret) {
4063 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004064 "VSI seid %d Rx ring %d %sable timeout\n",
4065 vsi->seid, pf_q, (enable ? "en" : "dis"));
Neerav Parikh23527302014-06-03 23:50:15 +00004066 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004067 }
4068 }
4069
Wyborny, Carolynd08a9f62017-03-28 08:00:48 -07004070 /* Due to HW errata, on Rx disable only, the register can indicate done
4071 * before it really is. Needs 50ms to be sure
4072 */
4073 if (!enable)
4074 mdelay(50);
4075
Neerav Parikh23527302014-06-03 23:50:15 +00004076 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004077}
4078
4079/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004080 * i40e_vsi_start_rings - Start a VSI's rings
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004081 * @vsi: the VSI being configured
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004082 **/
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004083int i40e_vsi_start_rings(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004084{
Anjali Singhai Jain3b867b22013-12-21 05:44:44 +00004085 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004086
4087 /* do rx first for enable and last for disable */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004088 ret = i40e_vsi_control_rx(vsi, true);
4089 if (ret)
4090 return ret;
4091 ret = i40e_vsi_control_tx(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004092
4093 return ret;
4094}
4095
4096/**
Filip Sadowski3aa7b742016-10-11 15:26:58 -07004097 * i40e_vsi_stop_rings - Stop a VSI's rings
4098 * @vsi: the VSI being configured
4099 **/
4100void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4101{
4102 /* do rx first for enable and last for disable
4103 * Ignore return value, we need to shutdown whatever we can
4104 */
4105 i40e_vsi_control_tx(vsi, false);
4106 i40e_vsi_control_rx(vsi, false);
4107}
4108
4109/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004110 * i40e_vsi_free_irq - Free the irq association with the OS
4111 * @vsi: the VSI being configured
4112 **/
4113static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4114{
4115 struct i40e_pf *pf = vsi->back;
4116 struct i40e_hw *hw = &pf->hw;
4117 int base = vsi->base_vector;
4118 u32 val, qp;
4119 int i;
4120
4121 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4122 if (!vsi->q_vectors)
4123 return;
4124
Shannon Nelson63741842014-04-23 04:50:16 +00004125 if (!vsi->irqs_ready)
4126 return;
4127
4128 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004129 for (i = 0; i < vsi->num_q_vectors; i++) {
Alan Brady96db7762016-09-14 16:24:38 -07004130 int irq_num;
4131 u16 vector;
4132
4133 vector = i + base;
4134 irq_num = pf->msix_entries[vector].vector;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004135
4136 /* free only the irqs that were actually requested */
Shannon Nelson78681b12013-11-28 06:39:36 +00004137 if (!vsi->q_vectors[i] ||
4138 !vsi->q_vectors[i]->num_ringpairs)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004139 continue;
4140
Alan Brady96db7762016-09-14 16:24:38 -07004141 /* clear the affinity notifier in the IRQ descriptor */
4142 irq_set_affinity_notifier(irq_num, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004143 /* clear the affinity_mask in the IRQ descriptor */
Alan Brady96db7762016-09-14 16:24:38 -07004144 irq_set_affinity_hint(irq_num, NULL);
4145 synchronize_irq(irq_num);
4146 free_irq(irq_num, vsi->q_vectors[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004147
4148 /* Tear down the interrupt queue link list
4149 *
4150 * We know that they come in pairs and always
4151 * the Rx first, then the Tx. To clear the
4152 * link list, stick the EOL value into the
4153 * next_q field of the registers.
4154 */
4155 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4156 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4157 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4158 val |= I40E_QUEUE_END_OF_LIST
4159 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4160 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4161
4162 while (qp != I40E_QUEUE_END_OF_LIST) {
4163 u32 next;
4164
4165 val = rd32(hw, I40E_QINT_RQCTL(qp));
4166
4167 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4168 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4169 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4170 I40E_QINT_RQCTL_INTEVENT_MASK);
4171
4172 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4173 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4174
4175 wr32(hw, I40E_QINT_RQCTL(qp), val);
4176
4177 val = rd32(hw, I40E_QINT_TQCTL(qp));
4178
4179 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4180 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4181
4182 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4183 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4184 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4185 I40E_QINT_TQCTL_INTEVENT_MASK);
4186
4187 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4188 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4189
4190 wr32(hw, I40E_QINT_TQCTL(qp), val);
4191 qp = next;
4192 }
4193 }
4194 } else {
4195 free_irq(pf->pdev->irq, pf);
4196
4197 val = rd32(hw, I40E_PFINT_LNKLST0);
4198 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4199 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4200 val |= I40E_QUEUE_END_OF_LIST
4201 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4202 wr32(hw, I40E_PFINT_LNKLST0, val);
4203
4204 val = rd32(hw, I40E_QINT_RQCTL(qp));
4205 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
4206 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4207 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
4208 I40E_QINT_RQCTL_INTEVENT_MASK);
4209
4210 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4211 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4212
4213 wr32(hw, I40E_QINT_RQCTL(qp), val);
4214
4215 val = rd32(hw, I40E_QINT_TQCTL(qp));
4216
4217 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
4218 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4219 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
4220 I40E_QINT_TQCTL_INTEVENT_MASK);
4221
4222 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4223 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4224
4225 wr32(hw, I40E_QINT_TQCTL(qp), val);
4226 }
4227}
4228
4229/**
Alexander Duyck493fb302013-09-28 07:01:44 +00004230 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4231 * @vsi: the VSI being configured
4232 * @v_idx: Index of vector to be freed
4233 *
4234 * This function frees the memory allocated to the q_vector. In addition if
4235 * NAPI is enabled it will delete any references to the NAPI struct prior
4236 * to freeing the q_vector.
4237 **/
4238static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4239{
4240 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004241 struct i40e_ring *ring;
Alexander Duyck493fb302013-09-28 07:01:44 +00004242
4243 if (!q_vector)
4244 return;
4245
4246 /* disassociate q_vector from rings */
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00004247 i40e_for_each_ring(ring, q_vector->tx)
4248 ring->q_vector = NULL;
4249
4250 i40e_for_each_ring(ring, q_vector->rx)
4251 ring->q_vector = NULL;
Alexander Duyck493fb302013-09-28 07:01:44 +00004252
4253 /* only VSI w/ an associated netdev is set up w/ NAPI */
4254 if (vsi->netdev)
4255 netif_napi_del(&q_vector->napi);
4256
4257 vsi->q_vectors[v_idx] = NULL;
4258
4259 kfree_rcu(q_vector, rcu);
4260}
4261
4262/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004263 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4264 * @vsi: the VSI being un-configured
4265 *
4266 * This frees the memory allocated to the q_vectors and
4267 * deletes references to the NAPI struct.
4268 **/
4269static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4270{
4271 int v_idx;
4272
Alexander Duyck493fb302013-09-28 07:01:44 +00004273 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4274 i40e_free_q_vector(vsi, v_idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004275}
4276
4277/**
4278 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4279 * @pf: board private structure
4280 **/
4281static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4282{
4283 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4284 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4285 pci_disable_msix(pf->pdev);
4286 kfree(pf->msix_entries);
4287 pf->msix_entries = NULL;
Shannon Nelson3b444392015-02-26 16:15:57 +00004288 kfree(pf->irq_pile);
4289 pf->irq_pile = NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004290 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4291 pci_disable_msi(pf->pdev);
4292 }
4293 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4294}
4295
4296/**
4297 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4298 * @pf: board private structure
4299 *
4300 * We go through and clear interrupt specific resources and reset the structure
4301 * to pre-load conditions
4302 **/
4303static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4304{
4305 int i;
4306
Shannon Nelsone1477582015-02-21 06:44:33 +00004307 i40e_stop_misc_vector(pf);
Shannon Nelson69278392016-03-10 14:59:43 -08004308 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
Shannon Nelsone1477582015-02-21 06:44:33 +00004309 synchronize_irq(pf->msix_entries[0].vector);
4310 free_irq(pf->msix_entries[0].vector, pf);
4311 }
4312
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004313 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4314 I40E_IWARP_IRQ_PILE_ID);
4315
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004316 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
Mitch Williams505682c2014-05-20 08:01:37 +00004317 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004318 if (pf->vsi[i])
4319 i40e_vsi_free_q_vectors(pf->vsi[i]);
4320 i40e_reset_interrupt_capability(pf);
4321}
4322
4323/**
4324 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4325 * @vsi: the VSI being configured
4326 **/
4327static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4328{
4329 int q_idx;
4330
4331 if (!vsi->netdev)
4332 return;
4333
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004334 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4335 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4336
4337 if (q_vector->rx.ring || q_vector->tx.ring)
4338 napi_enable(&q_vector->napi);
4339 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004340}
4341
4342/**
4343 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4344 * @vsi: the VSI being configured
4345 **/
4346static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4347{
4348 int q_idx;
4349
4350 if (!vsi->netdev)
4351 return;
4352
Alexander Duyck13a8cd12017-03-24 15:01:42 -07004353 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4354 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4355
4356 if (q_vector->rx.ring || q_vector->tx.ring)
4357 napi_disable(&q_vector->napi);
4358 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004359}
4360
4361/**
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004362 * i40e_vsi_close - Shut down a VSI
4363 * @vsi: the vsi to be quelled
4364 **/
4365static void i40e_vsi_close(struct i40e_vsi *vsi)
4366{
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004367 struct i40e_pf *pf = vsi->back;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004368 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
4369 i40e_down(vsi);
4370 i40e_vsi_free_irq(vsi);
4371 i40e_vsi_free_tx_resources(vsi);
4372 i40e_vsi_free_rx_resources(vsi);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04004373 vsi->current_netdev_flags = 0;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08004374 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
4375 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4376 pf->flags |= I40E_FLAG_CLIENT_RESET;
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004377}
4378
4379/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004380 * i40e_quiesce_vsi - Pause a given VSI
4381 * @vsi: the VSI being paused
4382 **/
4383static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4384{
4385 if (test_bit(__I40E_DOWN, &vsi->state))
4386 return;
4387
4388 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004389 if (vsi->netdev && netif_running(vsi->netdev))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004390 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04004391 else
Shannon Nelson90ef8d42014-03-14 07:32:26 +00004392 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004393}
4394
4395/**
4396 * i40e_unquiesce_vsi - Resume a given VSI
4397 * @vsi: the VSI being resumed
4398 **/
4399static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4400{
4401 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
4402 return;
4403
4404 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4405 if (vsi->netdev && netif_running(vsi->netdev))
4406 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4407 else
Shannon Nelson8276f752014-03-14 07:32:27 +00004408 i40e_vsi_open(vsi); /* this clears the DOWN bit */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004409}
4410
4411/**
4412 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4413 * @pf: the PF
4414 **/
4415static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4416{
4417 int v;
4418
Mitch Williams505682c2014-05-20 08:01:37 +00004419 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004420 if (pf->vsi[v])
4421 i40e_quiesce_vsi(pf->vsi[v]);
4422 }
4423}
4424
4425/**
4426 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
4427 * @pf: the PF
4428 **/
4429static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
4430{
4431 int v;
4432
Mitch Williams505682c2014-05-20 08:01:37 +00004433 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004434 if (pf->vsi[v])
4435 i40e_unquiesce_vsi(pf->vsi[v]);
4436 }
4437}
4438
Neerav Parikh69129dc2014-11-12 00:18:46 +00004439#ifdef CONFIG_I40E_DCB
4440/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004441 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004442 * @vsi: the VSI being configured
4443 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004444 * This function waits for the given VSI's queues to be disabled.
Neerav Parikh69129dc2014-11-12 00:18:46 +00004445 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004446static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004447{
4448 struct i40e_pf *pf = vsi->back;
4449 int i, pf_q, ret;
4450
4451 pf_q = vsi->base_queue;
4452 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4453 /* Check and wait for the disable status of the queue */
4454 ret = i40e_pf_txq_wait(pf, pf_q, false);
4455 if (ret) {
4456 dev_info(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04004457 "VSI seid %d Tx ring %d disable timeout\n",
4458 vsi->seid, pf_q);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004459 return ret;
4460 }
4461 }
4462
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004463 pf_q = vsi->base_queue;
4464 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4465 /* Check and wait for the disable status of the queue */
4466 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4467 if (ret) {
4468 dev_info(&pf->pdev->dev,
4469 "VSI seid %d Rx ring %d disable timeout\n",
4470 vsi->seid, pf_q);
4471 return ret;
4472 }
4473 }
4474
Neerav Parikh69129dc2014-11-12 00:18:46 +00004475 return 0;
4476}
4477
4478/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004479 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004480 * @pf: the PF
4481 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004482 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004483 * VSIs that are managed by this PF.
4484 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004485static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004486{
4487 int v, ret = 0;
4488
4489 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004490 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004491 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004492 if (ret)
4493 break;
4494 }
4495 }
4496
4497 return ret;
4498}
4499
4500#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004501
4502/**
4503 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4504 * @q_idx: TX queue number
4505 * @vsi: Pointer to VSI struct
4506 *
4507 * This function checks specified queue for given VSI. Detects hung condition.
4508 * Sets hung bit since it is two step process. Before next run of service task
4509 * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
4510 * hung condition remain unchanged and during subsequent run, this function
4511 * issues SW interrupt to recover from hung condition.
4512 **/
4513static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4514{
4515 struct i40e_ring *tx_ring = NULL;
4516 struct i40e_pf *pf;
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004517 u32 head, val, tx_pending_hw;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004518 int i;
4519
4520 pf = vsi->back;
4521
4522 /* now that we have an index, find the tx_ring struct */
4523 for (i = 0; i < vsi->num_queue_pairs; i++) {
4524 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4525 if (q_idx == vsi->tx_rings[i]->queue_index) {
4526 tx_ring = vsi->tx_rings[i];
4527 break;
4528 }
4529 }
4530 }
4531
4532 if (!tx_ring)
4533 return;
4534
4535 /* Read interrupt register */
4536 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4537 val = rd32(&pf->hw,
4538 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4539 tx_ring->vsi->base_vector - 1));
4540 else
4541 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4542
4543 head = i40e_get_head(tx_ring);
4544
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004545 tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004546
Kiran Patil9c6c1252015-11-06 15:26:02 -08004547 /* HW is done executing descriptors, updated HEAD write back,
4548 * but SW hasn't processed those descriptors. If interrupt is
4549 * not generated from this point ON, it could result into
4550 * dev_watchdog detecting timeout on those netdev_queue,
4551 * hence proactively trigger SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004552 */
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004553 if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Kiran Patil9c6c1252015-11-06 15:26:02 -08004554 /* NAPI Poll didn't run and clear since it was set */
4555 if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
4556 &tx_ring->q_vector->hung_detected)) {
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004557 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",
4558 vsi->seid, q_idx, tx_pending_hw,
Kiran Patil9c6c1252015-11-06 15:26:02 -08004559 tx_ring->next_to_clean, head,
4560 tx_ring->next_to_use,
4561 readl(tx_ring->tail));
4562 netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
4563 vsi->seid, q_idx, val);
4564 i40e_force_wb(vsi, tx_ring->q_vector);
4565 } else {
4566 /* First Chance - detected possible hung */
4567 set_bit(I40E_Q_VECTOR_HUNG_DETECT,
4568 &tx_ring->q_vector->hung_detected);
4569 }
4570 }
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004571
4572 /* This is the case where we have interrupts missing,
4573 * so the tx_pending in HW will most likely be 0, but we
4574 * will have tx_pending in SW since the WB happened but the
4575 * interrupt got lost.
4576 */
4577 if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
4578 (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004579 local_bh_disable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004580 if (napi_reschedule(&tx_ring->q_vector->napi))
4581 tx_ring->tx_stats.tx_lost_interrupt++;
Benjamin Poirier83a0c6e2017-01-12 17:04:14 -08004582 local_bh_enable();
Anjali Singhai Jaindd353102016-01-15 14:33:12 -08004583 }
Kiran Patilb03a8c12015-09-24 18:13:15 -04004584}
4585
4586/**
4587 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4588 * @pf: pointer to PF struct
4589 *
4590 * LAN VSI has netdev and netdev has TX queues. This function is to check
4591 * each of those TX queues if they are hung, trigger recovery by issuing
4592 * SW interrupt.
4593 **/
4594static void i40e_detect_recover_hung(struct i40e_pf *pf)
4595{
4596 struct net_device *netdev;
4597 struct i40e_vsi *vsi;
4598 int i;
4599
4600 /* Only for LAN VSI */
4601 vsi = pf->vsi[pf->lan_vsi];
4602
4603 if (!vsi)
4604 return;
4605
4606 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4607 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4608 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4609 return;
4610
4611 /* Make sure type is MAIN VSI */
4612 if (vsi->type != I40E_VSI_MAIN)
4613 return;
4614
4615 netdev = vsi->netdev;
4616 if (!netdev)
4617 return;
4618
4619 /* Bail out if netif_carrier is not OK */
4620 if (!netif_carrier_ok(netdev))
4621 return;
4622
4623 /* Go thru' TX queues for netdev */
4624 for (i = 0; i < netdev->num_tx_queues; i++) {
4625 struct netdev_queue *q;
4626
4627 q = netdev_get_tx_queue(netdev, i);
4628 if (q)
4629 i40e_detect_recover_hung_queue(i, vsi);
4630 }
4631}
4632
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004633/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004634 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00004635 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004636 *
4637 * Get TC map for ISCSI PF type that will include iSCSI TC
4638 * and LAN TC.
4639 **/
4640static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4641{
4642 struct i40e_dcb_app_priority_table app;
4643 struct i40e_hw *hw = &pf->hw;
4644 u8 enabled_tc = 1; /* TC0 is always enabled */
4645 u8 tc, i;
4646 /* Get the iSCSI APP TLV */
4647 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4648
4649 for (i = 0; i < dcbcfg->numapps; i++) {
4650 app = dcbcfg->app[i];
4651 if (app.selector == I40E_APP_SEL_TCPIP &&
4652 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4653 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004654 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004655 break;
4656 }
4657 }
4658
4659 return enabled_tc;
4660}
4661
4662/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004663 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4664 * @dcbcfg: the corresponding DCBx configuration structure
4665 *
4666 * Return the number of TCs from given DCBx configuration
4667 **/
4668static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4669{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004670 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004671 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004672 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004673
4674 /* Scan the ETS Config Priority Table to find
4675 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004676 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004677 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004678 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4679 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4680
4681 /* Now scan the bitmask to check for
4682 * contiguous TCs starting with TC0
4683 */
4684 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4685 if (num_tc & BIT(i)) {
4686 if (!tc_unused) {
4687 ret++;
4688 } else {
4689 pr_err("Non-contiguous TC - Disabling DCB\n");
4690 return 1;
4691 }
4692 } else {
4693 tc_unused = 1;
4694 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004695 }
4696
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004697 /* There is always at least TC0 */
4698 if (!ret)
4699 ret = 1;
4700
4701 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004702}
4703
4704/**
4705 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4706 * @dcbcfg: the corresponding DCBx configuration structure
4707 *
4708 * Query the current DCB configuration and return the number of
4709 * traffic classes enabled from the given DCBX config
4710 **/
4711static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4712{
4713 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4714 u8 enabled_tc = 1;
4715 u8 i;
4716
4717 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004718 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004719
4720 return enabled_tc;
4721}
4722
4723/**
4724 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4725 * @pf: PF being queried
4726 *
4727 * Return number of traffic classes enabled for the given PF
4728 **/
4729static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4730{
4731 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004732 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004733 u8 num_tc = 0;
4734 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4735
4736 /* If DCB is not enabled then always in single TC */
4737 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4738 return 1;
4739
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004740 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004741 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4742 return i40e_dcb_get_num_tc(dcbcfg);
4743
4744 /* MFP mode return count of enabled TCs for this PF */
4745 if (pf->hw.func_caps.iscsi)
4746 enabled_tc = i40e_get_iscsi_tc_map(pf);
4747 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004748 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004749
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004750 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004751 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004752 num_tc++;
4753 }
4754 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004755}
4756
4757/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004758 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4759 * @pf: PF being queried
4760 *
4761 * Return a bitmap for enabled traffic classes for this PF.
4762 **/
4763static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4764{
4765 /* If DCB is not enabled for this PF then just return default TC */
4766 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004767 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004768
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004769 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004770 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4771 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4772
Neerav Parikhfc51de92015-02-24 06:58:53 +00004773 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004774 if (pf->hw.func_caps.iscsi)
4775 return i40e_get_iscsi_tc_map(pf);
4776 else
David Ertmanea6acb72016-09-20 07:10:50 -07004777 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778}
4779
4780/**
4781 * i40e_vsi_get_bw_info - Query VSI BW Information
4782 * @vsi: the VSI being queried
4783 *
4784 * Returns 0 on success, negative value on failure
4785 **/
4786static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4787{
4788 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4789 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4790 struct i40e_pf *pf = vsi->back;
4791 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004792 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004793 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004794 int i;
4795
4796 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004797 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4798 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004799 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004800 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4801 i40e_stat_str(&pf->hw, ret),
4802 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004803 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004804 }
4805
4806 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004807 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4808 NULL);
4809 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004810 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004811 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4812 i40e_stat_str(&pf->hw, ret),
4813 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004814 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004815 }
4816
4817 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4818 dev_info(&pf->pdev->dev,
4819 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4820 bw_config.tc_valid_bits,
4821 bw_ets_config.tc_valid_bits);
4822 /* Still continuing */
4823 }
4824
4825 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4826 vsi->bw_max_quanta = bw_config.max_bw;
4827 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4828 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4829 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4830 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4831 vsi->bw_ets_limit_credits[i] =
4832 le16_to_cpu(bw_ets_config.credits[i]);
4833 /* 3 bits out of 4 for each TC */
4834 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4835 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004836
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004837 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004838}
4839
4840/**
4841 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4842 * @vsi: the VSI being configured
4843 * @enabled_tc: TC bitmap
4844 * @bw_credits: BW shared credits per TC
4845 *
4846 * Returns 0 on success, negative value on failure
4847 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004848static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004849 u8 *bw_share)
4850{
4851 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004852 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004853 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004854
4855 bw_data.tc_valid_bits = enabled_tc;
4856 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4857 bw_data.tc_bw_credits[i] = bw_share[i];
4858
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004859 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4860 NULL);
4861 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004862 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004863 "AQ command Config VSI BW allocation per TC failed = %d\n",
4864 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004865 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004866 }
4867
4868 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4869 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4870
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004871 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004872}
4873
4874/**
4875 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4876 * @vsi: the VSI being configured
4877 * @enabled_tc: TC map to be enabled
4878 *
4879 **/
4880static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4881{
4882 struct net_device *netdev = vsi->netdev;
4883 struct i40e_pf *pf = vsi->back;
4884 struct i40e_hw *hw = &pf->hw;
4885 u8 netdev_tc = 0;
4886 int i;
4887 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4888
4889 if (!netdev)
4890 return;
4891
4892 if (!enabled_tc) {
4893 netdev_reset_tc(netdev);
4894 return;
4895 }
4896
4897 /* Set up actual enabled TCs on the VSI */
4898 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4899 return;
4900
4901 /* set per TC queues for the VSI */
4902 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4903 /* Only set TC queues for enabled tcs
4904 *
4905 * e.g. For a VSI that has TC0 and TC3 enabled the
4906 * enabled_tc bitmap would be 0x00001001; the driver
4907 * will set the numtc for netdev as 2 that will be
4908 * referenced by the netdev layer as TC 0 and 1.
4909 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004910 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004911 netdev_set_tc_queue(netdev,
4912 vsi->tc_config.tc_info[i].netdev_tc,
4913 vsi->tc_config.tc_info[i].qcount,
4914 vsi->tc_config.tc_info[i].qoffset);
4915 }
4916
4917 /* Assign UP2TC map for the VSI */
4918 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4919 /* Get the actual TC# for the UP */
4920 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4921 /* Get the mapped netdev TC# for the UP */
4922 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4923 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4924 }
4925}
4926
4927/**
4928 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4929 * @vsi: the VSI being configured
4930 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4931 **/
4932static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4933 struct i40e_vsi_context *ctxt)
4934{
4935 /* copy just the sections touched not the entire info
4936 * since not all sections are valid as returned by
4937 * update vsi params
4938 */
4939 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4940 memcpy(&vsi->info.queue_mapping,
4941 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4942 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4943 sizeof(vsi->info.tc_mapping));
4944}
4945
4946/**
4947 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4948 * @vsi: VSI to be configured
4949 * @enabled_tc: TC bitmap
4950 *
4951 * This configures a particular VSI for TCs that are mapped to the
4952 * given TC bitmap. It uses default bandwidth share for TCs across
4953 * VSIs to configure TC for a particular VSI.
4954 *
4955 * NOTE:
4956 * It is expected that the VSI queues have been quisced before calling
4957 * this function.
4958 **/
4959static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4960{
4961 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4962 struct i40e_vsi_context ctxt;
4963 int ret = 0;
4964 int i;
4965
4966 /* Check if enabled_tc is same as existing or new TCs */
4967 if (vsi->tc_config.enabled_tc == enabled_tc)
4968 return ret;
4969
4970 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4971 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004972 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004973 bw_share[i] = 1;
4974 }
4975
4976 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4977 if (ret) {
4978 dev_info(&vsi->back->pdev->dev,
4979 "Failed configuring TC map %d for VSI %d\n",
4980 enabled_tc, vsi->seid);
4981 goto out;
4982 }
4983
4984 /* Update Queue Pairs Mapping for currently enabled UPs */
4985 ctxt.seid = vsi->seid;
4986 ctxt.pf_num = vsi->back->hw.pf_id;
4987 ctxt.vf_num = 0;
4988 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004989 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004990 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4991
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004992 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4993 ctxt.info.valid_sections |=
4994 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4995 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4996 }
4997
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004998 /* Update the VSI after updating the VSI queue-mapping information */
4999 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
5000 if (ret) {
5001 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005002 "Update vsi tc config failed, err %s aq_err %s\n",
5003 i40e_stat_str(&vsi->back->hw, ret),
5004 i40e_aq_str(&vsi->back->hw,
5005 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005006 goto out;
5007 }
5008 /* update the local VSI info with updated queue map */
5009 i40e_vsi_update_queue_map(vsi, &ctxt);
5010 vsi->info.valid_sections = 0;
5011
5012 /* Update current VSI BW information */
5013 ret = i40e_vsi_get_bw_info(vsi);
5014 if (ret) {
5015 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005016 "Failed updating vsi bw info, err %s aq_err %s\n",
5017 i40e_stat_str(&vsi->back->hw, ret),
5018 i40e_aq_str(&vsi->back->hw,
5019 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005020 goto out;
5021 }
5022
5023 /* Update the netdev TC setup */
5024 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5025out:
5026 return ret;
5027}
5028
5029/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005030 * i40e_veb_config_tc - Configure TCs for given VEB
5031 * @veb: given VEB
5032 * @enabled_tc: TC bitmap
5033 *
5034 * Configures given TC bitmap for VEB (switching) element
5035 **/
5036int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5037{
5038 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5039 struct i40e_pf *pf = veb->pf;
5040 int ret = 0;
5041 int i;
5042
5043 /* No TCs or already enabled TCs just return */
5044 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5045 return ret;
5046
5047 bw_data.tc_valid_bits = enabled_tc;
5048 /* bw_data.absolute_credits is not set (relative) */
5049
5050 /* Enable ETS TCs with equal BW Share for now */
5051 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005052 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005053 bw_data.tc_bw_share_credits[i] = 1;
5054 }
5055
5056 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5057 &bw_data, NULL);
5058 if (ret) {
5059 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005060 "VEB bw config failed, err %s aq_err %s\n",
5061 i40e_stat_str(&pf->hw, ret),
5062 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005063 goto out;
5064 }
5065
5066 /* Update the BW information */
5067 ret = i40e_veb_get_bw_info(veb);
5068 if (ret) {
5069 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005070 "Failed getting veb bw config, err %s aq_err %s\n",
5071 i40e_stat_str(&pf->hw, ret),
5072 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005073 }
5074
5075out:
5076 return ret;
5077}
5078
5079#ifdef CONFIG_I40E_DCB
5080/**
5081 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5082 * @pf: PF struct
5083 *
5084 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5085 * the caller would've quiesce all the VSIs before calling
5086 * this function
5087 **/
5088static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5089{
5090 u8 tc_map = 0;
5091 int ret;
5092 u8 v;
5093
5094 /* Enable the TCs available on PF to all VEBs */
5095 tc_map = i40e_pf_get_tc_map(pf);
5096 for (v = 0; v < I40E_MAX_VEB; v++) {
5097 if (!pf->veb[v])
5098 continue;
5099 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5100 if (ret) {
5101 dev_info(&pf->pdev->dev,
5102 "Failed configuring TC for VEB seid=%d\n",
5103 pf->veb[v]->seid);
5104 /* Will try to configure as many components */
5105 }
5106 }
5107
5108 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005109 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005110 if (!pf->vsi[v])
5111 continue;
5112
5113 /* - Enable all TCs for the LAN VSI
5114 * - For all others keep them at TC0 for now
5115 */
5116 if (v == pf->lan_vsi)
5117 tc_map = i40e_pf_get_tc_map(pf);
5118 else
David Ertmanea6acb72016-09-20 07:10:50 -07005119 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005120
5121 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5122 if (ret) {
5123 dev_info(&pf->pdev->dev,
5124 "Failed configuring TC for VSI seid=%d\n",
5125 pf->vsi[v]->seid);
5126 /* Will try to configure as many components */
5127 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005128 /* Re-configure VSI vectors based on updated TC map */
5129 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005130 if (pf->vsi[v]->netdev)
5131 i40e_dcbnl_set_all(pf->vsi[v]);
5132 }
5133 }
5134}
5135
5136/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005137 * i40e_resume_port_tx - Resume port Tx
5138 * @pf: PF struct
5139 *
5140 * Resume a port's Tx and issue a PF reset in case of failure to
5141 * resume.
5142 **/
5143static int i40e_resume_port_tx(struct i40e_pf *pf)
5144{
5145 struct i40e_hw *hw = &pf->hw;
5146 int ret;
5147
5148 ret = i40e_aq_resume_port_tx(hw, NULL);
5149 if (ret) {
5150 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005151 "Resume Port Tx failed, err %s aq_err %s\n",
5152 i40e_stat_str(&pf->hw, ret),
5153 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005154 /* Schedule PF reset to recover */
5155 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5156 i40e_service_event_schedule(pf);
5157 }
5158
5159 return ret;
5160}
5161
5162/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005163 * i40e_init_pf_dcb - Initialize DCB configuration
5164 * @pf: PF being configured
5165 *
5166 * Query the current DCB configuration and cache it
5167 * in the hardware structure
5168 **/
5169static int i40e_init_pf_dcb(struct i40e_pf *pf)
5170{
5171 struct i40e_hw *hw = &pf->hw;
5172 int err = 0;
5173
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005174 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005175 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005176 goto out;
5177
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005178 /* Get the initial DCB configuration */
5179 err = i40e_init_dcb(hw);
5180 if (!err) {
5181 /* Device/Function is not DCBX capable */
5182 if ((!hw->func_caps.dcb) ||
5183 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5184 dev_info(&pf->pdev->dev,
5185 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005186 } else {
5187 /* When status is not DISABLED then DCBX in FW */
5188 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5189 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005190
5191 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005192 /* Enable DCB tagging only when more than one TC
5193 * or explicitly disable if only one TC
5194 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005195 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5196 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005197 else
5198 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005199 dev_dbg(&pf->pdev->dev,
5200 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005201 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005202 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005203 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005204 "Query for DCB configuration failed, err %s aq_err %s\n",
5205 i40e_stat_str(&pf->hw, err),
5206 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005207 }
5208
5209out:
5210 return err;
5211}
5212#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005213#define SPEED_SIZE 14
5214#define FC_SIZE 8
5215/**
5216 * i40e_print_link_message - print link up or down
5217 * @vsi: the VSI for which link needs a message
5218 */
Matt Jaredc156f852015-08-27 11:42:39 -04005219void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005220{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005221 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005222 char *speed = "Unknown";
5223 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005224 char *fec = "";
5225 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005226
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005227 new_speed = vsi->back->hw.phy.link_info.link_speed;
5228
5229 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005230 return;
5231 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005232 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005233 if (!isup) {
5234 netdev_info(vsi->netdev, "NIC Link is Down\n");
5235 return;
5236 }
5237
Greg Rose148c2d82014-12-11 07:06:27 +00005238 /* Warn user if link speed on NPAR enabled partition is not at
5239 * least 10GB
5240 */
5241 if (vsi->back->hw.func_caps.npar_enable &&
5242 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5243 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5244 netdev_warn(vsi->netdev,
5245 "The partition detected link speed that is less than 10Gbps\n");
5246
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005247 switch (vsi->back->hw.phy.link_info.link_speed) {
5248 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005249 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005250 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005251 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005252 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005253 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005254 case I40E_LINK_SPEED_25GB:
5255 speed = "25 G";
5256 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005257 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005258 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005259 break;
5260 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005261 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005262 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005263 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005264 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005265 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005266 default:
5267 break;
5268 }
5269
5270 switch (vsi->back->hw.fc.current_mode) {
5271 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005272 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005273 break;
5274 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005275 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005276 break;
5277 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005278 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005279 break;
5280 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005281 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005282 break;
5283 }
5284
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005285 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5286 fec = ", FEC: None";
5287 an = ", Autoneg: False";
5288
5289 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5290 an = ", Autoneg: True";
5291
5292 if (vsi->back->hw.phy.link_info.fec_info &
5293 I40E_AQ_CONFIG_FEC_KR_ENA)
5294 fec = ", FEC: CL74 FC-FEC/BASE-R";
5295 else if (vsi->back->hw.phy.link_info.fec_info &
5296 I40E_AQ_CONFIG_FEC_RS_ENA)
5297 fec = ", FEC: CL108 RS-FEC";
5298 }
5299
5300 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5301 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005302}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005303
5304/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005305 * i40e_up_complete - Finish the last steps of bringing up a connection
5306 * @vsi: the VSI being configured
5307 **/
5308static int i40e_up_complete(struct i40e_vsi *vsi)
5309{
5310 struct i40e_pf *pf = vsi->back;
5311 int err;
5312
5313 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5314 i40e_vsi_configure_msix(vsi);
5315 else
5316 i40e_configure_msi_and_legacy(vsi);
5317
5318 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005319 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005320 if (err)
5321 return err;
5322
5323 clear_bit(__I40E_DOWN, &vsi->state);
5324 i40e_napi_enable_all(vsi);
5325 i40e_vsi_enable_irq(vsi);
5326
5327 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5328 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005329 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005330 netif_tx_start_all_queues(vsi->netdev);
5331 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005332 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005333 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005334 /* need to check for qualified module here*/
5335 if ((pf->hw.phy.link_info.link_info &
5336 I40E_AQ_MEDIA_AVAILABLE) &&
5337 (!(pf->hw.phy.link_info.an_info &
5338 I40E_AQ_QUALIFIED_MODULE)))
5339 netdev_err(vsi->netdev,
5340 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005341 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005342
5343 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005344 if (vsi->type == I40E_VSI_FDIR) {
5345 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005346 pf->fd_add_err = 0;
5347 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005348 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005349 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005350
5351 /* On the next run of the service_task, notify any clients of the new
5352 * opened netdev
5353 */
5354 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005355 i40e_service_event_schedule(pf);
5356
5357 return 0;
5358}
5359
5360/**
5361 * i40e_vsi_reinit_locked - Reset the VSI
5362 * @vsi: the VSI being configured
5363 *
5364 * Rebuild the ring structs after some configuration
5365 * has changed, e.g. MTU size.
5366 **/
5367static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5368{
5369 struct i40e_pf *pf = vsi->back;
5370
5371 WARN_ON(in_interrupt());
5372 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5373 usleep_range(1000, 2000);
5374 i40e_down(vsi);
5375
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005376 i40e_up(vsi);
5377 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5378}
5379
5380/**
5381 * i40e_up - Bring the connection back up after being down
5382 * @vsi: the VSI being configured
5383 **/
5384int i40e_up(struct i40e_vsi *vsi)
5385{
5386 int err;
5387
5388 err = i40e_vsi_configure(vsi);
5389 if (!err)
5390 err = i40e_up_complete(vsi);
5391
5392 return err;
5393}
5394
5395/**
5396 * i40e_down - Shutdown the connection processing
5397 * @vsi: the VSI being stopped
5398 **/
5399void i40e_down(struct i40e_vsi *vsi)
5400{
5401 int i;
5402
5403 /* It is assumed that the caller of this function
5404 * sets the vsi->state __I40E_DOWN bit.
5405 */
5406 if (vsi->netdev) {
5407 netif_carrier_off(vsi->netdev);
5408 netif_tx_disable(vsi->netdev);
5409 }
5410 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005411 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005412 i40e_napi_disable_all(vsi);
5413
5414 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005415 i40e_clean_tx_ring(vsi->tx_rings[i]);
5416 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005417 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005418
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005419}
5420
5421/**
5422 * i40e_setup_tc - configure multiple traffic classes
5423 * @netdev: net device to configure
5424 * @tc: number of traffic classes to enable
5425 **/
5426static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5427{
5428 struct i40e_netdev_priv *np = netdev_priv(netdev);
5429 struct i40e_vsi *vsi = np->vsi;
5430 struct i40e_pf *pf = vsi->back;
5431 u8 enabled_tc = 0;
5432 int ret = -EINVAL;
5433 int i;
5434
5435 /* Check if DCB enabled to continue */
5436 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5437 netdev_info(netdev, "DCB is not enabled for adapter\n");
5438 goto exit;
5439 }
5440
5441 /* Check if MFP enabled */
5442 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5443 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5444 goto exit;
5445 }
5446
5447 /* Check whether tc count is within enabled limit */
5448 if (tc > i40e_pf_get_num_tc(pf)) {
5449 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5450 goto exit;
5451 }
5452
5453 /* Generate TC map for number of tc requested */
5454 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005455 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005456
5457 /* Requesting same TC configuration as already enabled */
5458 if (enabled_tc == vsi->tc_config.enabled_tc)
5459 return 0;
5460
5461 /* Quiesce VSI queues */
5462 i40e_quiesce_vsi(vsi);
5463
5464 /* Configure VSI for enabled TCs */
5465 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5466 if (ret) {
5467 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5468 vsi->seid);
5469 goto exit;
5470 }
5471
5472 /* Unquiesce VSI */
5473 i40e_unquiesce_vsi(vsi);
5474
5475exit:
5476 return ret;
5477}
5478
John Fastabend16e5cc62016-02-16 21:16:43 -08005479static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5480 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005481{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005482 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005483 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005484
5485 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5486
5487 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005488}
5489
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005490/**
5491 * i40e_open - Called when a network interface is made active
5492 * @netdev: network interface device structure
5493 *
5494 * The open entry point is called when a network interface is made
5495 * active by the system (IFF_UP). At this point all resources needed
5496 * for transmit and receive operations are allocated, the interrupt
5497 * handler is registered with the OS, the netdev watchdog subtask is
5498 * enabled, and the stack is notified that the interface is ready.
5499 *
5500 * Returns 0 on success, negative value on failure
5501 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005502int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005503{
5504 struct i40e_netdev_priv *np = netdev_priv(netdev);
5505 struct i40e_vsi *vsi = np->vsi;
5506 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005507 int err;
5508
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005509 /* disallow open during test or if eeprom is broken */
5510 if (test_bit(__I40E_TESTING, &pf->state) ||
5511 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005512 return -EBUSY;
5513
5514 netif_carrier_off(netdev);
5515
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005516 err = i40e_vsi_open(vsi);
5517 if (err)
5518 return err;
5519
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005520 /* configure global TSO hardware offload settings */
5521 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5522 TCP_FLAG_FIN) >> 16);
5523 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5524 TCP_FLAG_FIN |
5525 TCP_FLAG_CWR) >> 16);
5526 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5527
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005528 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005529
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005530 return 0;
5531}
5532
5533/**
5534 * i40e_vsi_open -
5535 * @vsi: the VSI to open
5536 *
5537 * Finish initialization of the VSI.
5538 *
5539 * Returns 0 on success, negative value on failure
5540 **/
5541int i40e_vsi_open(struct i40e_vsi *vsi)
5542{
5543 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005544 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005545 int err;
5546
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005547 /* allocate descriptors */
5548 err = i40e_vsi_setup_tx_resources(vsi);
5549 if (err)
5550 goto err_setup_tx;
5551 err = i40e_vsi_setup_rx_resources(vsi);
5552 if (err)
5553 goto err_setup_rx;
5554
5555 err = i40e_vsi_configure(vsi);
5556 if (err)
5557 goto err_setup_rx;
5558
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005559 if (vsi->netdev) {
5560 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5561 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5562 err = i40e_vsi_request_irq(vsi, int_name);
5563 if (err)
5564 goto err_setup_rx;
5565
5566 /* Notify the stack of the actual queue counts. */
5567 err = netif_set_real_num_tx_queues(vsi->netdev,
5568 vsi->num_queue_pairs);
5569 if (err)
5570 goto err_set_queues;
5571
5572 err = netif_set_real_num_rx_queues(vsi->netdev,
5573 vsi->num_queue_pairs);
5574 if (err)
5575 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005576
5577 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005578 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005579 dev_driver_string(&pf->pdev->dev),
5580 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005581 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005582
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005583 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005584 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005585 goto err_setup_rx;
5586 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005587
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005588 err = i40e_up_complete(vsi);
5589 if (err)
5590 goto err_up_complete;
5591
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005592 return 0;
5593
5594err_up_complete:
5595 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005596err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005597 i40e_vsi_free_irq(vsi);
5598err_setup_rx:
5599 i40e_vsi_free_rx_resources(vsi);
5600err_setup_tx:
5601 i40e_vsi_free_tx_resources(vsi);
5602 if (vsi == pf->vsi[pf->lan_vsi])
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005603 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005604
5605 return err;
5606}
5607
5608/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005609 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00005610 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005611 *
5612 * This function destroys the hlist where all the Flow Director
5613 * filters were saved.
5614 **/
5615static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5616{
5617 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005618 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005619 struct hlist_node *node2;
5620
5621 hlist_for_each_entry_safe(filter, node2,
5622 &pf->fdir_filter_list, fdir_node) {
5623 hlist_del(&filter->fdir_node);
5624 kfree(filter);
5625 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005626
Jacob Keller0e588de2017-02-06 14:38:50 -08005627 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5628 list_del(&pit_entry->list);
5629 kfree(pit_entry);
5630 }
5631 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5632
5633 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5634 list_del(&pit_entry->list);
5635 kfree(pit_entry);
5636 }
5637 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5638
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005639 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005640 pf->fd_tcp4_filter_cnt = 0;
5641 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005642 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005643 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005644
5645 /* Reprogram the default input set for TCP/IPv4 */
5646 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5647 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5648 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5649
5650 /* Reprogram the default input set for UDP/IPv4 */
5651 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5652 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5653 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5654
5655 /* Reprogram the default input set for SCTP/IPv4 */
5656 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5657 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5658 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5659
5660 /* Reprogram the default input set for Other/IPv4 */
5661 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5662 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005663}
5664
5665/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005666 * i40e_close - Disables a network interface
5667 * @netdev: network interface device structure
5668 *
5669 * The close entry point is called when an interface is de-activated
5670 * by the OS. The hardware is still under the driver's control, but
5671 * this netdev interface is disabled.
5672 *
5673 * Returns 0, this is not allowed to fail
5674 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005675int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005676{
5677 struct i40e_netdev_priv *np = netdev_priv(netdev);
5678 struct i40e_vsi *vsi = np->vsi;
5679
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005680 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005681
5682 return 0;
5683}
5684
5685/**
5686 * i40e_do_reset - Start a PF or Core Reset sequence
5687 * @pf: board private structure
5688 * @reset_flags: which reset is requested
5689 *
5690 * The essential difference in resets is that the PF Reset
5691 * doesn't clear the packet buffers, doesn't reset the PE
5692 * firmware, and doesn't bother the other PFs on the chip.
5693 **/
5694void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
5695{
5696 u32 val;
5697
5698 WARN_ON(in_interrupt());
5699
Mitch Williams263fc482014-04-23 04:50:11 +00005700
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005701 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005702 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005703
5704 /* Request a Global Reset
5705 *
5706 * This will start the chip's countdown to the actual full
5707 * chip reset event, and a warning interrupt to be sent
5708 * to all PFs, including the requestor. Our handler
5709 * for the warning interrupt will deal with the shutdown
5710 * and recovery of the switch setup.
5711 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005712 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005713 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5714 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5715 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5716
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005717 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005718
5719 /* Request a Core Reset
5720 *
5721 * Same as Global Reset, except does *not* include the MAC/PHY
5722 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005723 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005724 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5725 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5726 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5727 i40e_flush(&pf->hw);
5728
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005729 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005730
5731 /* Request a PF Reset
5732 *
5733 * Resets only the PF-specific registers
5734 *
5735 * This goes directly to the tear-down and rebuild of
5736 * the switch, since we need to do all the recovery as
5737 * for the Core Reset.
5738 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005739 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005740 i40e_handle_reset_warning(pf);
5741
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005742 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005743 int v;
5744
5745 /* Find the VSI(s) that requested a re-init */
5746 dev_info(&pf->pdev->dev,
5747 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005748 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005749 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005750
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005751 if (vsi != NULL &&
5752 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5753 i40e_vsi_reinit_locked(pf->vsi[v]);
5754 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5755 }
5756 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005757 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005758 int v;
5759
5760 /* Find the VSI(s) that needs to be brought down */
5761 dev_info(&pf->pdev->dev, "VSI down requested\n");
5762 for (v = 0; v < pf->num_alloc_vsi; v++) {
5763 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005764
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005765 if (vsi != NULL &&
5766 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5767 set_bit(__I40E_DOWN, &vsi->state);
5768 i40e_down(vsi);
5769 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5770 }
5771 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005772 } else {
5773 dev_info(&pf->pdev->dev,
5774 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005775 }
5776}
5777
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005778#ifdef CONFIG_I40E_DCB
5779/**
5780 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5781 * @pf: board private structure
5782 * @old_cfg: current DCB config
5783 * @new_cfg: new DCB config
5784 **/
5785bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5786 struct i40e_dcbx_config *old_cfg,
5787 struct i40e_dcbx_config *new_cfg)
5788{
5789 bool need_reconfig = false;
5790
5791 /* Check if ETS configuration has changed */
5792 if (memcmp(&new_cfg->etscfg,
5793 &old_cfg->etscfg,
5794 sizeof(new_cfg->etscfg))) {
5795 /* If Priority Table has changed reconfig is needed */
5796 if (memcmp(&new_cfg->etscfg.prioritytable,
5797 &old_cfg->etscfg.prioritytable,
5798 sizeof(new_cfg->etscfg.prioritytable))) {
5799 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005800 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005801 }
5802
5803 if (memcmp(&new_cfg->etscfg.tcbwtable,
5804 &old_cfg->etscfg.tcbwtable,
5805 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005806 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005807
5808 if (memcmp(&new_cfg->etscfg.tsatable,
5809 &old_cfg->etscfg.tsatable,
5810 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005811 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005812 }
5813
5814 /* Check if PFC configuration has changed */
5815 if (memcmp(&new_cfg->pfc,
5816 &old_cfg->pfc,
5817 sizeof(new_cfg->pfc))) {
5818 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005819 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005820 }
5821
5822 /* Check if APP Table has changed */
5823 if (memcmp(&new_cfg->app,
5824 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005825 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005826 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005827 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005828 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005829
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005830 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005831 return need_reconfig;
5832}
5833
5834/**
5835 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5836 * @pf: board private structure
5837 * @e: event info posted on ARQ
5838 **/
5839static int i40e_handle_lldp_event(struct i40e_pf *pf,
5840 struct i40e_arq_event_info *e)
5841{
5842 struct i40e_aqc_lldp_get_mib *mib =
5843 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5844 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005845 struct i40e_dcbx_config tmp_dcbx_cfg;
5846 bool need_reconfig = false;
5847 int ret = 0;
5848 u8 type;
5849
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005850 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005851 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005852 return ret;
5853
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005854 /* Ignore if event is not for Nearest Bridge */
5855 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5856 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005857 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005858 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5859 return ret;
5860
5861 /* Check MIB Type and return if event for Remote MIB update */
5862 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005863 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005864 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005865 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5866 /* Update the remote cached instance and return */
5867 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5868 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5869 &hw->remote_dcbx_config);
5870 goto exit;
5871 }
5872
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005873 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005874 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005875
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005876 /* Reset the old DCBx configuration data */
5877 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005878 /* Get updated DCBX data from firmware */
5879 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005880 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005881 dev_info(&pf->pdev->dev,
5882 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5883 i40e_stat_str(&pf->hw, ret),
5884 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005885 goto exit;
5886 }
5887
5888 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005889 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5890 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005891 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005892 goto exit;
5893 }
5894
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005895 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5896 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005897
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005898 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005899
5900 if (!need_reconfig)
5901 goto exit;
5902
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005903 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005904 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005905 pf->flags |= I40E_FLAG_DCB_ENABLED;
5906 else
5907 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5908
Neerav Parikh69129dc2014-11-12 00:18:46 +00005909 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005910 /* Reconfiguration needed quiesce all VSIs */
5911 i40e_pf_quiesce_all_vsi(pf);
5912
5913 /* Changes in configuration update VEB/VSI */
5914 i40e_dcb_reconfigure(pf);
5915
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005916 ret = i40e_resume_port_tx(pf);
5917
Neerav Parikh69129dc2014-11-12 00:18:46 +00005918 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005919 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005920 if (ret)
5921 goto exit;
5922
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005923 /* Wait for the PF's queues to be disabled */
5924 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005925 if (ret) {
5926 /* Schedule PF reset to recover */
5927 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5928 i40e_service_event_schedule(pf);
5929 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005930 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08005931 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
5932 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005933 }
5934
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005935exit:
5936 return ret;
5937}
5938#endif /* CONFIG_I40E_DCB */
5939
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005940/**
Anjali Singhai Jain233261862013-11-26 10:49:22 +00005941 * i40e_do_reset_safe - Protected reset path for userland calls.
5942 * @pf: board private structure
5943 * @reset_flags: which reset is requested
5944 *
5945 **/
5946void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5947{
5948 rtnl_lock();
5949 i40e_do_reset(pf, reset_flags);
5950 rtnl_unlock();
5951}
5952
5953/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005954 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5955 * @pf: board private structure
5956 * @e: event info posted on ARQ
5957 *
5958 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5959 * and VF queues
5960 **/
5961static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5962 struct i40e_arq_event_info *e)
5963{
5964 struct i40e_aqc_lan_overflow *data =
5965 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5966 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5967 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5968 struct i40e_hw *hw = &pf->hw;
5969 struct i40e_vf *vf;
5970 u16 vf_id;
5971
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005972 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5973 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005974
5975 /* Queue belongs to VF, find the VF and issue VF reset */
5976 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5977 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5978 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5979 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5980 vf_id -= hw->func_caps.vf_base_id;
5981 vf = &pf->vf[vf_id];
5982 i40e_vc_notify_vf_reset(vf);
5983 /* Allow VF to process pending reset notification */
5984 msleep(20);
5985 i40e_reset_vf(vf, false);
5986 }
5987}
5988
5989/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005990 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5991 * @pf: board private structure
5992 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005993u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005994{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005995 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005996
5997 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5998 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5999 return fcnt_prog;
6000}
6001
6002/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006003 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006004 * @pf: board private structure
6005 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006006u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006007{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006008 u32 val, fcnt_prog;
6009
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006010 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
6011 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
6012 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
6013 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
6014 return fcnt_prog;
6015}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006016
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006017/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006018 * i40e_get_global_fd_count - Get total FD filters programmed on device
6019 * @pf: board private structure
6020 **/
6021u32 i40e_get_global_fd_count(struct i40e_pf *pf)
6022{
6023 u32 val, fcnt_prog;
6024
6025 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
6026 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
6027 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
6028 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
6029 return fcnt_prog;
6030}
6031
6032/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006033 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6034 * @pf: board private structure
6035 **/
6036void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6037{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006038 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006039 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006040 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006041
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006042 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6043 return;
6044
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006045 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6046 * to re-enable
6047 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006048 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006049 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006050 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6051 (pf->fd_add_err == 0) ||
6052 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006053 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006054 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6055 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006056 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6057 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 +00006058 }
6059 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006060
6061 /* Wait for some more space to be available to turn on ATR. We also
6062 * must check that no existing ntuple rules for TCP are in effect
6063 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006064 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6065 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006066 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006067 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006068 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006069 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006070 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 +00006071 }
6072 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006073
6074 /* if hw had a problem adding a filter, delete it */
6075 if (pf->fd_inv > 0) {
6076 hlist_for_each_entry_safe(filter, node,
6077 &pf->fdir_filter_list, fdir_node) {
6078 if (filter->fd_id == pf->fd_inv) {
6079 hlist_del(&filter->fdir_node);
6080 kfree(filter);
6081 pf->fdir_pf_active_filters--;
6082 }
6083 }
6084 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006085}
6086
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006087#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006088#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006089/**
6090 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6091 * @pf: board private structure
6092 **/
6093static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6094{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006095 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006096 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006097 bool disable_atr = false;
6098 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006099 int reg;
6100
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006101 if (!time_after(jiffies, pf->fd_flush_timestamp +
6102 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6103 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006104
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006105 /* If the flush is happening too quick and we have mostly SB rules we
6106 * should not re-enable ATR for some time.
6107 */
6108 min_flush_time = pf->fd_flush_timestamp +
6109 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6110 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006111
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006112 if (!(time_after(jiffies, min_flush_time)) &&
6113 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6114 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6115 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6116 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006117 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006118
6119 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006120 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006121 /* flush all filters */
6122 wr32(&pf->hw, I40E_PFQF_CTL_1,
6123 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6124 i40e_flush(&pf->hw);
6125 pf->fd_flush_cnt++;
6126 pf->fd_add_err = 0;
6127 do {
6128 /* Check FD flush status every 5-6msec */
6129 usleep_range(5000, 6000);
6130 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6131 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6132 break;
6133 } while (flush_wait_retry--);
6134 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6135 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6136 } else {
6137 /* replay sideband filters */
6138 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006139 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006140 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006141 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6142 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6143 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6144 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006145}
6146
6147/**
6148 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6149 * @pf: board private structure
6150 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006151u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006152{
6153 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6154}
6155
6156/* We can see up to 256 filter programming desc in transit if the filters are
6157 * being applied really fast; before we see the first
6158 * filter miss error on Rx queue 0. Accumulating enough error messages before
6159 * reacting will make sure we don't cause flush too often.
6160 */
6161#define I40E_MAX_FD_PROGRAM_ERROR 256
6162
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006163/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006164 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6165 * @pf: board private structure
6166 **/
6167static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6168{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006169
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006170 /* if interface is down do nothing */
6171 if (test_bit(__I40E_DOWN, &pf->state))
6172 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006173
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006174 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006175 i40e_fdir_flush_and_replay(pf);
6176
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006177 i40e_fdir_check_and_reenable(pf);
6178
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006179}
6180
6181/**
6182 * i40e_vsi_link_event - notify VSI of a link event
6183 * @vsi: vsi to be notified
6184 * @link_up: link up or down
6185 **/
6186static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6187{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006188 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006189 return;
6190
6191 switch (vsi->type) {
6192 case I40E_VSI_MAIN:
6193 if (!vsi->netdev || !vsi->netdev_registered)
6194 break;
6195
6196 if (link_up) {
6197 netif_carrier_on(vsi->netdev);
6198 netif_tx_wake_all_queues(vsi->netdev);
6199 } else {
6200 netif_carrier_off(vsi->netdev);
6201 netif_tx_stop_all_queues(vsi->netdev);
6202 }
6203 break;
6204
6205 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006206 case I40E_VSI_VMDQ2:
6207 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006208 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006209 case I40E_VSI_MIRROR:
6210 default:
6211 /* there is no notification for other VSIs */
6212 break;
6213 }
6214}
6215
6216/**
6217 * i40e_veb_link_event - notify elements on the veb of a link event
6218 * @veb: veb to be notified
6219 * @link_up: link up or down
6220 **/
6221static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6222{
6223 struct i40e_pf *pf;
6224 int i;
6225
6226 if (!veb || !veb->pf)
6227 return;
6228 pf = veb->pf;
6229
6230 /* depth first... */
6231 for (i = 0; i < I40E_MAX_VEB; i++)
6232 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6233 i40e_veb_link_event(pf->veb[i], link_up);
6234
6235 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006236 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006237 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6238 i40e_vsi_link_event(pf->vsi[i], link_up);
6239}
6240
6241/**
6242 * i40e_link_event - Update netif_carrier status
6243 * @pf: board private structure
6244 **/
6245static void i40e_link_event(struct i40e_pf *pf)
6246{
Mitch Williams320684c2014-10-17 03:14:43 +00006247 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006248 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006249 i40e_status status;
6250 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006251
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006252 /* save off old link status information */
6253 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6254
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006255 /* set this to force the get_link_status call to refresh state */
6256 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006257
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006258 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006259
6260 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006261
6262 /* On success, disable temp link polling */
6263 if (status == I40E_SUCCESS) {
6264 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6265 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6266 } else {
6267 /* Enable link polling temporarily until i40e_get_link_status
6268 * returns I40E_SUCCESS
6269 */
6270 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006271 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6272 status);
6273 return;
6274 }
6275
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006276 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6277 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006278
6279 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006280 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006281 (test_bit(__I40E_DOWN, &vsi->state) ||
6282 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006283 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006284
6285 if (!test_bit(__I40E_DOWN, &vsi->state))
6286 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006287
6288 /* Notify the base of the switch tree connected to
6289 * the link. Floating VEBs are not notified.
6290 */
6291 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6292 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6293 else
Mitch Williams320684c2014-10-17 03:14:43 +00006294 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006295
6296 if (pf->vf)
6297 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006298
6299 if (pf->flags & I40E_FLAG_PTP)
6300 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006301}
6302
6303/**
Shannon Nelson21536712014-10-25 10:35:25 +00006304 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006305 * @pf: board private structure
6306 **/
6307static void i40e_watchdog_subtask(struct i40e_pf *pf)
6308{
6309 int i;
6310
6311 /* if interface is down do nothing */
6312 if (test_bit(__I40E_DOWN, &pf->state) ||
6313 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6314 return;
6315
Shannon Nelson21536712014-10-25 10:35:25 +00006316 /* make sure we don't do these things too often */
6317 if (time_before(jiffies, (pf->service_timer_previous +
6318 pf->service_timer_period)))
6319 return;
6320 pf->service_timer_previous = jiffies;
6321
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006322 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6323 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006324 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006325
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006326 /* Update the stats for active netdevs so the network stack
6327 * can look at updated numbers whenever it cares to
6328 */
Mitch Williams505682c2014-05-20 08:01:37 +00006329 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006330 if (pf->vsi[i] && pf->vsi[i]->netdev)
6331 i40e_update_stats(pf->vsi[i]);
6332
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006333 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6334 /* Update the stats for the active switching components */
6335 for (i = 0; i < I40E_MAX_VEB; i++)
6336 if (pf->veb[i])
6337 i40e_update_veb_stats(pf->veb[i]);
6338 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006339
6340 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006341}
6342
6343/**
6344 * i40e_reset_subtask - Set up for resetting the device and driver
6345 * @pf: board private structure
6346 **/
6347static void i40e_reset_subtask(struct i40e_pf *pf)
6348{
6349 u32 reset_flags = 0;
6350
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006351 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006352 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006353 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006354 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6355 }
6356 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006357 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006358 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6359 }
6360 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006361 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006362 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6363 }
6364 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006365 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006366 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6367 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006368 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006369 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006370 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6371 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006372
6373 /* If there's a recovery already waiting, it takes
6374 * precedence before starting a new reset sequence.
6375 */
6376 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
6377 i40e_handle_reset_warning(pf);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006378 goto unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006379 }
6380
6381 /* If we're already down or resetting, just bail */
6382 if (reset_flags &&
6383 !test_bit(__I40E_DOWN, &pf->state) &&
6384 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
6385 i40e_do_reset(pf, reset_flags);
Anjali Singhai Jain233261862013-11-26 10:49:22 +00006386
6387unlock:
6388 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006389}
6390
6391/**
6392 * i40e_handle_link_event - Handle link event
6393 * @pf: board private structure
6394 * @e: event info posted on ARQ
6395 **/
6396static void i40e_handle_link_event(struct i40e_pf *pf,
6397 struct i40e_arq_event_info *e)
6398{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006399 struct i40e_aqc_get_link_status *status =
6400 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006401
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006402 /* Do a new status request to re-enable LSE reporting
6403 * and load new status information into the hw struct
6404 * This completely ignores any state information
6405 * in the ARQ event info, instead choosing to always
6406 * issue the AQ update link status command.
6407 */
6408 i40e_link_event(pf);
6409
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006410 /* check for unqualified module, if link is down */
6411 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6412 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6413 (!(status->link_info & I40E_AQ_LINK_UP)))
6414 dev_err(&pf->pdev->dev,
6415 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006416}
6417
6418/**
6419 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6420 * @pf: board private structure
6421 **/
6422static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6423{
6424 struct i40e_arq_event_info event;
6425 struct i40e_hw *hw = &pf->hw;
6426 u16 pending, i = 0;
6427 i40e_status ret;
6428 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006429 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006430 u32 val;
6431
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006432 /* Do not run clean AQ when PF reset fails */
6433 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6434 return;
6435
Shannon Nelson86df2422014-05-20 08:01:35 +00006436 /* check for error indications */
6437 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6438 oldval = val;
6439 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006440 if (hw->debug_mask & I40E_DEBUG_AQ)
6441 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006442 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6443 }
6444 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006445 if (hw->debug_mask & I40E_DEBUG_AQ)
6446 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006447 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006448 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006449 }
6450 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006451 if (hw->debug_mask & I40E_DEBUG_AQ)
6452 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006453 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6454 }
6455 if (oldval != val)
6456 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6457
6458 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6459 oldval = val;
6460 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006461 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6462 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006463 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6464 }
6465 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006466 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6467 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006468 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6469 }
6470 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006471 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6472 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006473 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6474 }
6475 if (oldval != val)
6476 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6477
Mitch Williams1001dc32014-11-11 20:02:19 +00006478 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6479 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006480 if (!event.msg_buf)
6481 return;
6482
6483 do {
6484 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006485 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006486 break;
Mitch Williams56497972014-06-04 08:45:18 +00006487 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006488 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6489 break;
6490 }
6491
6492 opcode = le16_to_cpu(event.desc.opcode);
6493 switch (opcode) {
6494
6495 case i40e_aqc_opc_get_link_status:
6496 i40e_handle_link_event(pf, &event);
6497 break;
6498 case i40e_aqc_opc_send_msg_to_pf:
6499 ret = i40e_vc_process_vf_msg(pf,
6500 le16_to_cpu(event.desc.retval),
6501 le32_to_cpu(event.desc.cookie_high),
6502 le32_to_cpu(event.desc.cookie_low),
6503 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006504 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006505 break;
6506 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006507 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006508#ifdef CONFIG_I40E_DCB
6509 rtnl_lock();
6510 ret = i40e_handle_lldp_event(pf, &event);
6511 rtnl_unlock();
6512#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006513 break;
6514 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006515 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006516 i40e_handle_lan_overflow_event(pf, &event);
6517 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006518 case i40e_aqc_opc_send_msg_to_peer:
6519 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6520 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006521 case i40e_aqc_opc_nvm_erase:
6522 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006523 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006524 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6525 "ARQ NVM operation 0x%04x completed\n",
6526 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006527 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006528 default:
6529 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006530 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006531 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006532 break;
6533 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006534 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006535
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006536 if (i < pf->adminq_work_limit)
6537 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6538
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006539 /* re-enable Admin queue interrupt cause */
6540 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6541 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6542 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6543 i40e_flush(hw);
6544
6545 kfree(event.msg_buf);
6546}
6547
6548/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006549 * i40e_verify_eeprom - make sure eeprom is good to use
6550 * @pf: board private structure
6551 **/
6552static void i40e_verify_eeprom(struct i40e_pf *pf)
6553{
6554 int err;
6555
6556 err = i40e_diag_eeprom_test(&pf->hw);
6557 if (err) {
6558 /* retry in case of garbage read */
6559 err = i40e_diag_eeprom_test(&pf->hw);
6560 if (err) {
6561 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6562 err);
6563 set_bit(__I40E_BAD_EEPROM, &pf->state);
6564 }
6565 }
6566
6567 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6568 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6569 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6570 }
6571}
6572
6573/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006574 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006575 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006576 *
6577 * enable switch loop back or die - no point in a return value
6578 **/
6579static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6580{
6581 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6582 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006583 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006584
6585 ctxt.seid = pf->main_vsi_seid;
6586 ctxt.pf_num = pf->hw.pf_id;
6587 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006588 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6589 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006590 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006591 "couldn't get PF vsi config, err %s aq_err %s\n",
6592 i40e_stat_str(&pf->hw, ret),
6593 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006594 return;
6595 }
6596 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6597 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6598 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6599
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006600 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6601 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006602 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006603 "update vsi switch failed, err %s aq_err %s\n",
6604 i40e_stat_str(&pf->hw, ret),
6605 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006606 }
6607}
6608
6609/**
6610 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006611 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006612 *
6613 * disable switch loop back or die - no point in a return value
6614 **/
6615static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6616{
6617 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6618 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006619 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006620
6621 ctxt.seid = pf->main_vsi_seid;
6622 ctxt.pf_num = pf->hw.pf_id;
6623 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006624 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6625 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006626 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006627 "couldn't get PF vsi config, err %s aq_err %s\n",
6628 i40e_stat_str(&pf->hw, ret),
6629 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006630 return;
6631 }
6632 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6633 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6634 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6635
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006636 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6637 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006638 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006639 "update vsi switch failed, err %s aq_err %s\n",
6640 i40e_stat_str(&pf->hw, ret),
6641 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006642 }
6643}
6644
6645/**
Neerav Parikh51616012015-02-06 08:52:14 +00006646 * i40e_config_bridge_mode - Configure the HW bridge mode
6647 * @veb: pointer to the bridge instance
6648 *
6649 * Configure the loop back mode for the LAN VSI that is downlink to the
6650 * specified HW bridge instance. It is expected this function is called
6651 * when a new HW bridge is instantiated.
6652 **/
6653static void i40e_config_bridge_mode(struct i40e_veb *veb)
6654{
6655 struct i40e_pf *pf = veb->pf;
6656
Shannon Nelson6dec1012015-09-28 14:12:30 -04006657 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6658 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6659 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006660 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6661 i40e_disable_pf_switch_lb(pf);
6662 else
6663 i40e_enable_pf_switch_lb(pf);
6664}
6665
6666/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006667 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6668 * @veb: pointer to the VEB instance
6669 *
6670 * This is a recursive function that first builds the attached VSIs then
6671 * recurses in to build the next layer of VEB. We track the connections
6672 * through our own index numbers because the seid's from the HW could
6673 * change across the reset.
6674 **/
6675static int i40e_reconstitute_veb(struct i40e_veb *veb)
6676{
6677 struct i40e_vsi *ctl_vsi = NULL;
6678 struct i40e_pf *pf = veb->pf;
6679 int v, veb_idx;
6680 int ret;
6681
6682 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006683 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006684 if (pf->vsi[v] &&
6685 pf->vsi[v]->veb_idx == veb->idx &&
6686 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6687 ctl_vsi = pf->vsi[v];
6688 break;
6689 }
6690 }
6691 if (!ctl_vsi) {
6692 dev_info(&pf->pdev->dev,
6693 "missing owner VSI for veb_idx %d\n", veb->idx);
6694 ret = -ENOENT;
6695 goto end_reconstitute;
6696 }
6697 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6698 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6699 ret = i40e_add_vsi(ctl_vsi);
6700 if (ret) {
6701 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006702 "rebuild of veb_idx %d owner VSI failed: %d\n",
6703 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006704 goto end_reconstitute;
6705 }
6706 i40e_vsi_reset_stats(ctl_vsi);
6707
6708 /* create the VEB in the switch and move the VSI onto the VEB */
6709 ret = i40e_add_veb(veb, ctl_vsi);
6710 if (ret)
6711 goto end_reconstitute;
6712
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006713 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6714 veb->bridge_mode = BRIDGE_MODE_VEB;
6715 else
6716 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006717 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006718
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006719 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006720 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006721 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6722 continue;
6723
6724 if (pf->vsi[v]->veb_idx == veb->idx) {
6725 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006726
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006727 vsi->uplink_seid = veb->seid;
6728 ret = i40e_add_vsi(vsi);
6729 if (ret) {
6730 dev_info(&pf->pdev->dev,
6731 "rebuild of vsi_idx %d failed: %d\n",
6732 v, ret);
6733 goto end_reconstitute;
6734 }
6735 i40e_vsi_reset_stats(vsi);
6736 }
6737 }
6738
6739 /* create any VEBs attached to this VEB - RECURSION */
6740 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6741 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6742 pf->veb[veb_idx]->uplink_seid = veb->seid;
6743 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6744 if (ret)
6745 break;
6746 }
6747 }
6748
6749end_reconstitute:
6750 return ret;
6751}
6752
6753/**
6754 * i40e_get_capabilities - get info about the HW
6755 * @pf: the PF struct
6756 **/
6757static int i40e_get_capabilities(struct i40e_pf *pf)
6758{
6759 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6760 u16 data_size;
6761 int buf_len;
6762 int err;
6763
6764 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6765 do {
6766 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6767 if (!cap_buf)
6768 return -ENOMEM;
6769
6770 /* this loads the data into the hw struct for us */
6771 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6772 &data_size,
6773 i40e_aqc_opc_list_func_capabilities,
6774 NULL);
6775 /* data loaded, buffer no longer needed */
6776 kfree(cap_buf);
6777
6778 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6779 /* retry with a larger buffer */
6780 buf_len = data_size;
6781 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6782 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006783 "capability discovery failed, err %s aq_err %s\n",
6784 i40e_stat_str(&pf->hw, err),
6785 i40e_aq_str(&pf->hw,
6786 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006787 return -ENODEV;
6788 }
6789 } while (err);
6790
6791 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6792 dev_info(&pf->pdev->dev,
6793 "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",
6794 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6795 pf->hw.func_caps.num_msix_vectors,
6796 pf->hw.func_caps.num_msix_vectors_vf,
6797 pf->hw.func_caps.fd_filters_guaranteed,
6798 pf->hw.func_caps.fd_filters_best_effort,
6799 pf->hw.func_caps.num_tx_qp,
6800 pf->hw.func_caps.num_vsis);
6801
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006802#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6803 + pf->hw.func_caps.num_vfs)
6804 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6805 dev_info(&pf->pdev->dev,
6806 "got num_vsis %d, setting num_vsis to %d\n",
6807 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6808 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6809 }
6810
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006811 return 0;
6812}
6813
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006814static int i40e_vsi_clear(struct i40e_vsi *vsi);
6815
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006817 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006818 * @pf: board private structure
6819 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006820static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006821{
6822 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006823
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006824 /* quick workaround for an NVM issue that leaves a critical register
6825 * uninitialized
6826 */
6827 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6828 static const u32 hkey[] = {
6829 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6830 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6831 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6832 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006833 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006834
6835 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6836 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6837 }
6838
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006839 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006840 return;
6841
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006842 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006843 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006844
6845 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006846 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006847 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6848 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006849 if (!vsi) {
6850 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006851 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6852 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006853 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006854 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006855
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006856 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006857}
6858
6859/**
6860 * i40e_fdir_teardown - release the Flow Director resources
6861 * @pf: board private structure
6862 **/
6863static void i40e_fdir_teardown(struct i40e_pf *pf)
6864{
Alexander Duyck4b816442016-10-11 15:26:53 -07006865 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006866
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006867 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006868 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6869 if (vsi)
6870 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006871}
6872
6873/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006874 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006875 * @pf: board private structure
6876 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00006877 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006878 **/
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006879static void i40e_prep_for_reset(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006880{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006881 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006882 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006883 u32 v;
6884
6885 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6886 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006887 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006888 if (i40e_check_asq_alive(&pf->hw))
6889 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006890
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006891 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006892
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006893 /* quiesce the VSIs and their queues that are not already DOWN */
6894 i40e_pf_quiesce_all_vsi(pf);
6895
Mitch Williams505682c2014-05-20 08:01:37 +00006896 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006897 if (pf->vsi[v])
6898 pf->vsi[v]->seid = 0;
6899 }
6900
6901 i40e_shutdown_adminq(&pf->hw);
6902
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006903 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006904 if (hw->hmc.hmc_obj) {
6905 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006906 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006907 dev_warn(&pf->pdev->dev,
6908 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006909 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006910}
6911
6912/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006913 * i40e_send_version - update firmware with driver version
6914 * @pf: PF struct
6915 */
6916static void i40e_send_version(struct i40e_pf *pf)
6917{
6918 struct i40e_driver_version dv;
6919
6920 dv.major_version = DRV_VERSION_MAJOR;
6921 dv.minor_version = DRV_VERSION_MINOR;
6922 dv.build_version = DRV_VERSION_BUILD;
6923 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006924 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006925 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6926}
6927
6928/**
Jesse Brandeburg4dda12e2013-12-18 13:46:01 +00006929 * i40e_reset_and_rebuild - reset and rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006930 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006931 * @reinit: if the Main VSI needs to re-initialized.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006932 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006933static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006934{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006935 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006936 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006937 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006938 u32 val;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006939 u32 v;
6940
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006941 /* Now we wait for GRST to settle out.
6942 * We don't have to delete the VEBs or VSIs from the hw switch
6943 * because the reset will make them disappear.
6944 */
6945 ret = i40e_pf_reset(hw);
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006946 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006947 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006948 set_bit(__I40E_RESET_FAILED, &pf->state);
6949 goto clear_recovery;
Akeem G Abodunrinb5565402014-04-09 05:59:04 +00006950 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006951 pf->pfr_count++;
6952
6953 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006954 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006955 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006956
6957 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6958 ret = i40e_init_adminq(&pf->hw);
6959 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006960 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6961 i40e_stat_str(&pf->hw, ret),
6962 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006963 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006964 }
6965
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006966 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006967 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006968 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006969
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006970 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006971 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006972 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006973 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006974
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006975 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08006976 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006977 if (ret) {
6978 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6979 goto end_core_reset;
6980 }
6981 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6982 if (ret) {
6983 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6984 goto end_core_reset;
6985 }
6986
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006987#ifdef CONFIG_I40E_DCB
6988 ret = i40e_init_pf_dcb(pf);
6989 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006990 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6991 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6992 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006993 }
6994#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006995 /* do basic switch setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006996 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006997 if (ret)
6998 goto end_core_reset;
6999
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007000 /* The driver only wants link up/down and module qualification
7001 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007002 */
7003 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007004 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07007005 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08007006 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007007 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007008 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
7009 i40e_stat_str(&pf->hw, ret),
7010 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00007011
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007012 /* make sure our flow control settings are restored */
7013 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7014 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007015 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7016 i40e_stat_str(&pf->hw, ret),
7017 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007018
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007019 /* Rebuild the VSIs and VEBs that existed before reset.
7020 * They are still in our local switch element arrays, so only
7021 * need to rebuild the switch model in the HW.
7022 *
7023 * If there were VEBs but the reconstitution failed, we'll try
7024 * try to recover minimal use by getting the basic PF VSI working.
7025 */
7026 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007027 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007028 /* find the one VEB connected to the MAC, and find orphans */
7029 for (v = 0; v < I40E_MAX_VEB; v++) {
7030 if (!pf->veb[v])
7031 continue;
7032
7033 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7034 pf->veb[v]->uplink_seid == 0) {
7035 ret = i40e_reconstitute_veb(pf->veb[v]);
7036
7037 if (!ret)
7038 continue;
7039
7040 /* If Main VEB failed, we're in deep doodoo,
7041 * so give up rebuilding the switch and set up
7042 * for minimal rebuild of PF VSI.
7043 * If orphan failed, we'll report the error
7044 * but try to keep going.
7045 */
7046 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7047 dev_info(&pf->pdev->dev,
7048 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7049 ret);
7050 pf->vsi[pf->lan_vsi]->uplink_seid
7051 = pf->mac_seid;
7052 break;
7053 } else if (pf->veb[v]->uplink_seid == 0) {
7054 dev_info(&pf->pdev->dev,
7055 "rebuild of orphan VEB failed: %d\n",
7056 ret);
7057 }
7058 }
7059 }
7060 }
7061
7062 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007063 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007064 /* no VEB, so rebuild only the Main VSI */
7065 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7066 if (ret) {
7067 dev_info(&pf->pdev->dev,
7068 "rebuild of Main VSI failed: %d\n", ret);
7069 goto end_core_reset;
7070 }
7071 }
7072
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007073 /* Reconfigure hardware for allowing smaller MSS in the case
7074 * of TSO, so that we avoid the MDD being fired and causing
7075 * a reset in the case of small MSS+TSO.
7076 */
7077#define I40E_REG_MSS 0x000E64DC
7078#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7079#define I40E_64BYTE_MSS 0x400000
7080 val = rd32(hw, I40E_REG_MSS);
7081 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7082 val &= ~I40E_REG_MSS_MIN_MASK;
7083 val |= I40E_64BYTE_MSS;
7084 wr32(hw, I40E_REG_MSS, val);
7085 }
7086
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007087 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007088 msleep(75);
7089 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7090 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007091 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7092 i40e_stat_str(&pf->hw, ret),
7093 i40e_aq_str(&pf->hw,
7094 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007095 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007096 /* reinit the misc interrupt */
7097 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7098 ret = i40e_setup_misc_vector(pf);
7099
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007100 /* Add a filter to drop all Flow control frames from any VSI from being
7101 * transmitted. By doing so we stop a malicious VF from sending out
7102 * PAUSE or PFC frames and potentially controlling traffic for other
7103 * PF/VF VSIs.
7104 * The FW can still send Flow control frames if enabled.
7105 */
7106 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7107 pf->main_vsi_seid);
7108
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007109 /* restart the VSIs that were rebuilt and running before the reset */
7110 i40e_pf_unquiesce_all_vsi(pf);
7111
Mitch Williams69f64b22014-02-13 03:48:46 -08007112 if (pf->num_alloc_vfs) {
7113 for (v = 0; v < pf->num_alloc_vfs; v++)
7114 i40e_reset_vf(&pf->vf[v], true);
7115 }
7116
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007117 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007118 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007119
7120end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007121 clear_bit(__I40E_RESET_FAILED, &pf->state);
7122clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007123 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7124}
7125
7126/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007127 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007128 * @pf: board private structure
7129 *
7130 * Close up the VFs and other things in prep for a Core Reset,
7131 * then get ready to rebuild the world.
7132 **/
7133static void i40e_handle_reset_warning(struct i40e_pf *pf)
7134{
Shannon Nelson23cfbe02014-06-04 01:23:14 +00007135 i40e_prep_for_reset(pf);
7136 i40e_reset_and_rebuild(pf, false);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007137}
7138
7139/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007140 * i40e_handle_mdd_event
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007141 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007142 *
7143 * Called from the MDD irq handler to identify possibly malicious vfs
7144 **/
7145static void i40e_handle_mdd_event(struct i40e_pf *pf)
7146{
7147 struct i40e_hw *hw = &pf->hw;
7148 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007149 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007150 struct i40e_vf *vf;
7151 u32 reg;
7152 int i;
7153
7154 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7155 return;
7156
7157 /* find what triggered the MDD event */
7158 reg = rd32(hw, I40E_GL_MDET_TX);
7159 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007160 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7161 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007162 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007163 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007164 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007165 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007166 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7167 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7168 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007169 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007170 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 +00007171 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007172 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7173 mdd_detected = true;
7174 }
7175 reg = rd32(hw, I40E_GL_MDET_RX);
7176 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007177 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7178 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007179 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007180 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007181 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7182 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7183 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007184 if (netif_msg_rx_err(pf))
7185 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7186 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007187 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7188 mdd_detected = true;
7189 }
7190
Neerav Parikhdf430b12014-06-04 01:23:15 +00007191 if (mdd_detected) {
7192 reg = rd32(hw, I40E_PF_MDET_TX);
7193 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7194 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007195 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007196 pf_mdd_detected = true;
7197 }
7198 reg = rd32(hw, I40E_PF_MDET_RX);
7199 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7200 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007201 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007202 pf_mdd_detected = true;
7203 }
7204 /* Queue belongs to the PF, initiate a reset */
7205 if (pf_mdd_detected) {
7206 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7207 i40e_service_event_schedule(pf);
7208 }
7209 }
7210
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007211 /* see if one of the VFs needs its hand slapped */
7212 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7213 vf = &(pf->vf[i]);
7214 reg = rd32(hw, I40E_VP_MDET_TX(i));
7215 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7216 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7217 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007218 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7219 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007220 }
7221
7222 reg = rd32(hw, I40E_VP_MDET_RX(i));
7223 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7224 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7225 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007226 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7227 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007228 }
7229
7230 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7231 dev_info(&pf->pdev->dev,
7232 "Too many MDD events on VF %d, disabled\n", i);
7233 dev_info(&pf->pdev->dev,
7234 "Use PF Control I/F to re-enable the VF\n");
7235 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7236 }
7237 }
7238
7239 /* re-enable mdd interrupt cause */
7240 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7241 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7242 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7243 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7244 i40e_flush(hw);
7245}
7246
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007247/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007248 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007249 * @pf: board private structure
7250 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007251static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007252{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007253 struct i40e_hw *hw = &pf->hw;
7254 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007255 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007256 int i;
7257
Singhai, Anjali6a899022015-12-14 12:21:18 -08007258 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007259 return;
7260
Singhai, Anjali6a899022015-12-14 12:21:18 -08007261 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007262
7263 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007264 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7265 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7266 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007267 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007268 ret = i40e_aq_add_udp_tunnel(hw, port,
7269 pf->udp_ports[i].type,
7270 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007271 else
7272 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007273
7274 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007275 dev_dbg(&pf->pdev->dev,
7276 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7277 pf->udp_ports[i].type ? "vxlan" : "geneve",
7278 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007279 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007280 i40e_stat_str(&pf->hw, ret),
7281 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007282 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007283 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007284 }
7285 }
7286 }
7287}
7288
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007289/**
7290 * i40e_service_task - Run the driver's async subtasks
7291 * @work: pointer to work_struct containing our data
7292 **/
7293static void i40e_service_task(struct work_struct *work)
7294{
7295 struct i40e_pf *pf = container_of(work,
7296 struct i40e_pf,
7297 service_task);
7298 unsigned long start_time = jiffies;
7299
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007300 /* don't bother with service tasks if a reset is in progress */
7301 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007302 return;
7303 }
7304
Mitch Williams91089032016-11-21 13:03:51 -08007305 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7306 return;
7307
Kiran Patilb03a8c12015-09-24 18:13:15 -04007308 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007309 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007310 i40e_reset_subtask(pf);
7311 i40e_handle_mdd_event(pf);
7312 i40e_vc_process_vflr_event(pf);
7313 i40e_watchdog_subtask(pf);
7314 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007315 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7316 /* Client subtask will reopen next time through. */
7317 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7318 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7319 } else {
7320 i40e_client_subtask(pf);
7321 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7322 i40e_notify_client_of_l2_param_changes(
7323 pf->vsi[pf->lan_vsi]);
7324 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7325 }
7326 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007327 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007328 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007329 i40e_clean_adminq_subtask(pf);
7330
Mitch Williams91089032016-11-21 13:03:51 -08007331 /* flush memory to make sure state is correct before next watchdog */
7332 smp_mb__before_atomic();
7333 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007334
7335 /* If the tasks have taken longer than one timer cycle or there
7336 * is more work to be done, reschedule the service task now
7337 * rather than wait for the timer to tick again.
7338 */
7339 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7340 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7341 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7342 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7343 i40e_service_event_schedule(pf);
7344}
7345
7346/**
7347 * i40e_service_timer - timer callback
7348 * @data: pointer to PF struct
7349 **/
7350static void i40e_service_timer(unsigned long data)
7351{
7352 struct i40e_pf *pf = (struct i40e_pf *)data;
7353
7354 mod_timer(&pf->service_timer,
7355 round_jiffies(jiffies + pf->service_timer_period));
7356 i40e_service_event_schedule(pf);
7357}
7358
7359/**
7360 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7361 * @vsi: the VSI being configured
7362 **/
7363static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7364{
7365 struct i40e_pf *pf = vsi->back;
7366
7367 switch (vsi->type) {
7368 case I40E_VSI_MAIN:
7369 vsi->alloc_queue_pairs = pf->num_lan_qps;
7370 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7371 I40E_REQ_DESCRIPTOR_MULTIPLE);
7372 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7373 vsi->num_q_vectors = pf->num_lan_msix;
7374 else
7375 vsi->num_q_vectors = 1;
7376
7377 break;
7378
7379 case I40E_VSI_FDIR:
7380 vsi->alloc_queue_pairs = 1;
7381 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7382 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007383 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007384 break;
7385
7386 case I40E_VSI_VMDQ2:
7387 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7388 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7389 I40E_REQ_DESCRIPTOR_MULTIPLE);
7390 vsi->num_q_vectors = pf->num_vmdq_msix;
7391 break;
7392
7393 case I40E_VSI_SRIOV:
7394 vsi->alloc_queue_pairs = pf->num_vf_qps;
7395 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7396 I40E_REQ_DESCRIPTOR_MULTIPLE);
7397 break;
7398
7399 default:
7400 WARN_ON(1);
7401 return -ENODATA;
7402 }
7403
7404 return 0;
7405}
7406
7407/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007408 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7409 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007410 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007411 *
7412 * On error: returns error code (negative)
7413 * On success: returns 0
7414 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007415static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007416{
7417 int size;
7418 int ret = 0;
7419
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007420 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007421 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7422 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7423 if (!vsi->tx_rings)
7424 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007425 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7426
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007427 if (alloc_qvectors) {
7428 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007429 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007430 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7431 if (!vsi->q_vectors) {
7432 ret = -ENOMEM;
7433 goto err_vectors;
7434 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007435 }
7436 return ret;
7437
7438err_vectors:
7439 kfree(vsi->tx_rings);
7440 return ret;
7441}
7442
7443/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007444 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7445 * @pf: board private structure
7446 * @type: type of VSI
7447 *
7448 * On error: returns error code (negative)
7449 * On success: returns vsi index in PF (positive)
7450 **/
7451static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7452{
7453 int ret = -ENODEV;
7454 struct i40e_vsi *vsi;
7455 int vsi_idx;
7456 int i;
7457
7458 /* Need to protect the allocation of the VSIs at the PF level */
7459 mutex_lock(&pf->switch_mutex);
7460
7461 /* VSI list may be fragmented if VSI creation/destruction has
7462 * been happening. We can afford to do a quick scan to look
7463 * for any free VSIs in the list.
7464 *
7465 * find next empty vsi slot, looping back around if necessary
7466 */
7467 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007468 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007469 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007470 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007471 i = 0;
7472 while (i < pf->next_vsi && pf->vsi[i])
7473 i++;
7474 }
7475
Mitch Williams505682c2014-05-20 08:01:37 +00007476 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007477 vsi_idx = i; /* Found one! */
7478 } else {
7479 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007480 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007481 }
7482 pf->next_vsi = ++i;
7483
7484 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7485 if (!vsi) {
7486 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007487 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007488 }
7489 vsi->type = type;
7490 vsi->back = pf;
7491 set_bit(__I40E_DOWN, &vsi->state);
7492 vsi->flags = 0;
7493 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007494 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007495 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7496 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007497 vsi->netdev_registered = false;
7498 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007499 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007500 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007501
Alexander Duyck9f65e152013-09-28 06:00:58 +00007502 ret = i40e_set_num_rings_in_vsi(vsi);
7503 if (ret)
7504 goto err_rings;
7505
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007506 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007507 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007508 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007509
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007510 /* Setup default MSIX irq handler for VSI */
7511 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7512
Kiran Patil21659032015-09-30 14:09:03 -04007513 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007514 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007515 pf->vsi[vsi_idx] = vsi;
7516 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007517 goto unlock_pf;
7518
Alexander Duyck9f65e152013-09-28 06:00:58 +00007519err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007520 pf->next_vsi = i - 1;
7521 kfree(vsi);
7522unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007523 mutex_unlock(&pf->switch_mutex);
7524 return ret;
7525}
7526
7527/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007528 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7529 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007530 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007531 *
7532 * On error: returns error code (negative)
7533 * On success: returns 0
7534 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007535static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007536{
7537 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007538 if (free_qvectors) {
7539 kfree(vsi->q_vectors);
7540 vsi->q_vectors = NULL;
7541 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007542 kfree(vsi->tx_rings);
7543 vsi->tx_rings = NULL;
7544 vsi->rx_rings = NULL;
7545}
7546
7547/**
Helin Zhang28c58692015-10-26 19:44:27 -04007548 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7549 * and lookup table
7550 * @vsi: Pointer to VSI structure
7551 */
7552static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7553{
7554 if (!vsi)
7555 return;
7556
7557 kfree(vsi->rss_hkey_user);
7558 vsi->rss_hkey_user = NULL;
7559
7560 kfree(vsi->rss_lut_user);
7561 vsi->rss_lut_user = NULL;
7562}
7563
7564/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007565 * i40e_vsi_clear - Deallocate the VSI provided
7566 * @vsi: the VSI being un-configured
7567 **/
7568static int i40e_vsi_clear(struct i40e_vsi *vsi)
7569{
7570 struct i40e_pf *pf;
7571
7572 if (!vsi)
7573 return 0;
7574
7575 if (!vsi->back)
7576 goto free_vsi;
7577 pf = vsi->back;
7578
7579 mutex_lock(&pf->switch_mutex);
7580 if (!pf->vsi[vsi->idx]) {
7581 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7582 vsi->idx, vsi->idx, vsi, vsi->type);
7583 goto unlock_vsi;
7584 }
7585
7586 if (pf->vsi[vsi->idx] != vsi) {
7587 dev_err(&pf->pdev->dev,
7588 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7589 pf->vsi[vsi->idx]->idx,
7590 pf->vsi[vsi->idx],
7591 pf->vsi[vsi->idx]->type,
7592 vsi->idx, vsi, vsi->type);
7593 goto unlock_vsi;
7594 }
7595
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00007596 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007597 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7598 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7599
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007600 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007601 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007602
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007603 pf->vsi[vsi->idx] = NULL;
7604 if (vsi->idx < pf->next_vsi)
7605 pf->next_vsi = vsi->idx;
7606
7607unlock_vsi:
7608 mutex_unlock(&pf->switch_mutex);
7609free_vsi:
7610 kfree(vsi);
7611
7612 return 0;
7613}
7614
7615/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007616 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7617 * @vsi: the VSI being cleaned
7618 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007619static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007620{
7621 int i;
7622
Greg Rose8e9dca52013-12-18 13:45:53 +00007623 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007624 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007625 kfree_rcu(vsi->tx_rings[i], rcu);
7626 vsi->tx_rings[i] = NULL;
7627 vsi->rx_rings[i] = NULL;
7628 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007629 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007630}
7631
7632/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007633 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7634 * @vsi: the VSI being configured
7635 **/
7636static int i40e_alloc_rings(struct i40e_vsi *vsi)
7637{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007638 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007639 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007640 int i;
7641
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007642 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007643 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007644 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007645 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7646 if (!tx_ring)
7647 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007648
7649 tx_ring->queue_index = i;
7650 tx_ring->reg_idx = vsi->base_queue + i;
7651 tx_ring->ring_active = false;
7652 tx_ring->vsi = vsi;
7653 tx_ring->netdev = vsi->netdev;
7654 tx_ring->dev = &pf->pdev->dev;
7655 tx_ring->count = vsi->num_desc;
7656 tx_ring->size = 0;
7657 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007658 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7659 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007660 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007661 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007662
Alexander Duyck9f65e152013-09-28 06:00:58 +00007663 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007664 rx_ring->queue_index = i;
7665 rx_ring->reg_idx = vsi->base_queue + i;
7666 rx_ring->ring_active = false;
7667 rx_ring->vsi = vsi;
7668 rx_ring->netdev = vsi->netdev;
7669 rx_ring->dev = &pf->pdev->dev;
7670 rx_ring->count = vsi->num_desc;
7671 rx_ring->size = 0;
7672 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007673 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007674 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007675 }
7676
7677 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007678
7679err_out:
7680 i40e_vsi_clear_rings(vsi);
7681 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007682}
7683
7684/**
7685 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7686 * @pf: board private structure
7687 * @vectors: the number of MSI-X vectors to request
7688 *
7689 * Returns the number of vectors reserved, or error
7690 **/
7691static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7692{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007693 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7694 I40E_MIN_MSIX, vectors);
7695 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007696 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007697 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007698 vectors = 0;
7699 }
7700
7701 return vectors;
7702}
7703
7704/**
7705 * i40e_init_msix - Setup the MSIX capability
7706 * @pf: board private structure
7707 *
7708 * Work with the OS to set up the MSIX vectors needed.
7709 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007710 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007711 **/
7712static int i40e_init_msix(struct i40e_pf *pf)
7713{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007714 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007715 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007716 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007717 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007718 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007719 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007720
7721 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7722 return -ENODEV;
7723
7724 /* The number of vectors we'll request will be comprised of:
7725 * - Add 1 for "other" cause for Admin Queue events, etc.
7726 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007727 * - Queues being used for RSS.
7728 * We don't need as many as max_rss_size vectors.
7729 * use rss_size instead in the calculation since that
7730 * is governed by number of cpus in the system.
7731 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007732 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007733 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007734 * Once we count this up, try the request.
7735 *
7736 * If we can't get what we want, we'll simplify to nearly nothing
7737 * and try again. If that still fails, we punt.
7738 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007739 vectors_left = hw->func_caps.num_msix_vectors;
7740 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007741
Shannon Nelson1e200e42015-02-27 09:15:24 +00007742 /* reserve one vector for miscellaneous handler */
7743 if (vectors_left) {
7744 v_budget++;
7745 vectors_left--;
7746 }
7747
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007748 /* reserve some vectors for the main PF traffic queues. Initially we
7749 * only reserve at most 50% of the available vectors, in the case that
7750 * the number of online CPUs is large. This ensures that we can enable
7751 * extra features as well. Once we've enabled the other features, we
7752 * will use any remaining vectors to reach as close as we can to the
7753 * number of online CPUs.
7754 */
7755 cpus = num_online_cpus();
7756 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007757 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007758
7759 /* reserve one vector for sideband flow director */
7760 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7761 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007762 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007763 v_budget++;
7764 vectors_left--;
7765 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007766 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007767 }
7768 }
John W Linville83840e42015-01-14 03:06:28 +00007769
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007770 /* can we reserve enough for iWARP? */
7771 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007772 iwarp_requested = pf->num_iwarp_msix;
7773
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007774 if (!vectors_left)
7775 pf->num_iwarp_msix = 0;
7776 else if (vectors_left < pf->num_iwarp_msix)
7777 pf->num_iwarp_msix = 1;
7778 v_budget += pf->num_iwarp_msix;
7779 vectors_left -= pf->num_iwarp_msix;
7780 }
7781
Shannon Nelson1e200e42015-02-27 09:15:24 +00007782 /* any vectors left over go for VMDq support */
7783 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7784 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7785 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7786
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007787 if (!vectors_left) {
7788 pf->num_vmdq_msix = 0;
7789 pf->num_vmdq_qps = 0;
7790 } else {
7791 /* if we're short on vectors for what's desired, we limit
7792 * the queues per vmdq. If this is still more than are
7793 * available, the user will need to change the number of
7794 * queues/vectors used by the PF later with the ethtool
7795 * channels command
7796 */
7797 if (vmdq_vecs < vmdq_vecs_wanted)
7798 pf->num_vmdq_qps = 1;
7799 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007800
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007801 v_budget += vmdq_vecs;
7802 vectors_left -= vmdq_vecs;
7803 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007804 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007805
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007806 /* On systems with a large number of SMP cores, we previously limited
7807 * the number of vectors for num_lan_msix to be at most 50% of the
7808 * available vectors, to allow for other features. Now, we add back
7809 * the remaining vectors. However, we ensure that the total
7810 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7811 * calculate the number of vectors we can add without going over the
7812 * cap of CPUs. For systems with a small number of CPUs this will be
7813 * zero.
7814 */
7815 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7816 pf->num_lan_msix += extra_vectors;
7817 vectors_left -= extra_vectors;
7818
7819 WARN(vectors_left < 0,
7820 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7821
7822 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007823 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7824 GFP_KERNEL);
7825 if (!pf->msix_entries)
7826 return -ENOMEM;
7827
7828 for (i = 0; i < v_budget; i++)
7829 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007830 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007831
Shannon Nelson3b444392015-02-26 16:15:57 +00007832 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007833 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7834 kfree(pf->msix_entries);
7835 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007836 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007837 return -ENODEV;
7838
Shannon Nelson3b444392015-02-26 16:15:57 +00007839 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007840 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007841 pf->num_vmdq_vsis = 0;
7842 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007843 pf->num_lan_qps = 1;
7844 pf->num_lan_msix = 1;
7845
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007846 } else if (!vectors_left) {
7847 /* If we have limited resources, we will start with no vectors
7848 * for the special features and then allocate vectors to some
7849 * of these features based on the policy and at the end disable
7850 * the features that did not get any vectors.
7851 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007852 int vec;
7853
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007854 dev_info(&pf->pdev->dev,
7855 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007856 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007857 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007858
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007859 /* Scale vector usage down */
7860 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007861 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007862 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007863
7864 /* partition out the remaining vectors */
7865 switch (vec) {
7866 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007867 pf->num_lan_msix = 1;
7868 break;
7869 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007870 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7871 pf->num_lan_msix = 1;
7872 pf->num_iwarp_msix = 1;
7873 } else {
7874 pf->num_lan_msix = 2;
7875 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007876 break;
7877 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007878 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7879 pf->num_iwarp_msix = min_t(int, (vec / 3),
7880 iwarp_requested);
7881 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7882 I40E_DEFAULT_NUM_VMDQ_VSI);
7883 } else {
7884 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7885 I40E_DEFAULT_NUM_VMDQ_VSI);
7886 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007887 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7888 pf->num_fdsb_msix = 1;
7889 vec--;
7890 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007891 pf->num_lan_msix = min_t(int,
7892 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7893 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007894 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007895 break;
7896 }
7897 }
7898
Stefan Assmannabd97a92016-09-19 13:37:51 +02007899 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7900 (pf->num_fdsb_msix == 0)) {
7901 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7902 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7903 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007904 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7905 (pf->num_vmdq_msix == 0)) {
7906 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7907 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7908 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007909
7910 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7911 (pf->num_iwarp_msix == 0)) {
7912 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7913 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7914 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007915 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7916 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7917 pf->num_lan_msix,
7918 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7919 pf->num_fdsb_msix,
7920 pf->num_iwarp_msix);
7921
Shannon Nelson3b444392015-02-26 16:15:57 +00007922 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007923}
7924
7925/**
Greg Rose90e04072014-03-06 08:59:57 +00007926 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007927 * @vsi: the VSI being configured
7928 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007929 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007930 *
7931 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7932 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007933static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007934{
7935 struct i40e_q_vector *q_vector;
7936
7937 /* allocate q_vector */
7938 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7939 if (!q_vector)
7940 return -ENOMEM;
7941
7942 q_vector->vsi = vsi;
7943 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007944 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7945
Alexander Duyck493fb302013-09-28 07:01:44 +00007946 if (vsi->netdev)
7947 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007948 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007949
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007950 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7951 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7952
Alexander Duyck493fb302013-09-28 07:01:44 +00007953 /* tie q_vector and vsi together */
7954 vsi->q_vectors[v_idx] = q_vector;
7955
7956 return 0;
7957}
7958
7959/**
Greg Rose90e04072014-03-06 08:59:57 +00007960 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007961 * @vsi: the VSI being configured
7962 *
7963 * We allocate one q_vector per queue interrupt. If allocation fails we
7964 * return -ENOMEM.
7965 **/
Greg Rose90e04072014-03-06 08:59:57 +00007966static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007967{
7968 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007969 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007970
7971 /* if not MSIX, give the one vector only to the LAN VSI */
7972 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7973 num_q_vectors = vsi->num_q_vectors;
7974 else if (vsi == pf->vsi[pf->lan_vsi])
7975 num_q_vectors = 1;
7976 else
7977 return -EINVAL;
7978
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007979 current_cpu = cpumask_first(cpu_online_mask);
7980
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007982 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007983 if (err)
7984 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007985 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7986 if (unlikely(current_cpu >= nr_cpu_ids))
7987 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007988 }
7989
7990 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00007991
7992err_out:
7993 while (v_idx--)
7994 i40e_free_q_vector(vsi, v_idx);
7995
7996 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007997}
7998
7999/**
8000 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8001 * @pf: board private structure to initialize
8002 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008003static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008004{
Shannon Nelson3b444392015-02-26 16:15:57 +00008005 int vectors = 0;
8006 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008007
8008 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008009 vectors = i40e_init_msix(pf);
8010 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008011 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008012 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008013 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008014 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008015 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008016 I40E_FLAG_SRIOV_ENABLED |
8017 I40E_FLAG_FD_SB_ENABLED |
8018 I40E_FLAG_FD_ATR_ENABLED |
8019 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008020
8021 /* rework the queue expectations without MSIX */
8022 i40e_determine_queue_usage(pf);
8023 }
8024 }
8025
8026 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8027 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008028 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008029 vectors = pci_enable_msi(pf->pdev);
8030 if (vectors < 0) {
8031 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8032 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008033 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8034 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008035 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008036 }
8037
Shannon Nelson958a3e32013-09-28 07:13:28 +00008038 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008039 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008040
Shannon Nelson3b444392015-02-26 16:15:57 +00008041 /* set up vector assignment tracking */
8042 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8043 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008044 if (!pf->irq_pile) {
8045 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8046 return -ENOMEM;
8047 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008048 pf->irq_pile->num_entries = vectors;
8049 pf->irq_pile->search_hint = 0;
8050
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008051 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008052 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008053
8054 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008055}
8056
8057/**
8058 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8059 * @pf: board private structure
8060 *
8061 * This sets up the handler for MSIX 0, which is used to manage the
8062 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8063 * when in MSI or Legacy interrupt mode.
8064 **/
8065static int i40e_setup_misc_vector(struct i40e_pf *pf)
8066{
8067 struct i40e_hw *hw = &pf->hw;
8068 int err = 0;
8069
8070 /* Only request the irq if this is the first time through, and
8071 * not when we're rebuilding after a Reset
8072 */
8073 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8074 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008075 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008076 if (err) {
8077 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008078 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008079 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008080 return -EFAULT;
8081 }
8082 }
8083
Jacob Kellerab437b52014-12-14 01:55:08 +00008084 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008085
8086 /* associate no queues to the misc vector */
8087 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8088 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8089
8090 i40e_flush(hw);
8091
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008092 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008093
8094 return err;
8095}
8096
8097/**
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008098 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8099 * @vsi: vsi structure
8100 * @seed: RSS hash seed
8101 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008102static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8103 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008104{
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008105 struct i40e_pf *pf = vsi->back;
8106 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008107 int ret = 0;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008108
Jacob Keller776b2e12016-07-19 16:23:30 -07008109 if (seed) {
8110 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8111 (struct i40e_aqc_get_set_rss_key_data *)seed;
8112 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8113 if (ret) {
8114 dev_info(&pf->pdev->dev,
8115 "Cannot set RSS key, err %s aq_err %s\n",
8116 i40e_stat_str(hw, ret),
8117 i40e_aq_str(hw, hw->aq.asq_last_status));
8118 return ret;
8119 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008120 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008121 if (lut) {
8122 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008123
Jacob Keller776b2e12016-07-19 16:23:30 -07008124 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8125 if (ret) {
8126 dev_info(&pf->pdev->dev,
8127 "Cannot set RSS lut, err %s aq_err %s\n",
8128 i40e_stat_str(hw, ret),
8129 i40e_aq_str(hw, hw->aq.asq_last_status));
8130 return ret;
8131 }
8132 }
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008133 return ret;
8134}
8135
8136/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008137 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8138 * @vsi: Pointer to vsi structure
8139 * @seed: Buffter to store the hash keys
8140 * @lut: Buffer to store the lookup table entries
8141 * @lut_size: Size of buffer to store the lookup table entries
8142 *
8143 * Return 0 on success, negative on failure
8144 */
8145static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8146 u8 *lut, u16 lut_size)
8147{
8148 struct i40e_pf *pf = vsi->back;
8149 struct i40e_hw *hw = &pf->hw;
8150 int ret = 0;
8151
8152 if (seed) {
8153 ret = i40e_aq_get_rss_key(hw, vsi->id,
8154 (struct i40e_aqc_get_set_rss_key_data *)seed);
8155 if (ret) {
8156 dev_info(&pf->pdev->dev,
8157 "Cannot get RSS key, 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 if (lut) {
8166 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8167
8168 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8169 if (ret) {
8170 dev_info(&pf->pdev->dev,
8171 "Cannot get RSS lut, err %s aq_err %s\n",
8172 i40e_stat_str(&pf->hw, ret),
8173 i40e_aq_str(&pf->hw,
8174 pf->hw.aq.asq_last_status));
8175 return ret;
8176 }
8177 }
8178
8179 return ret;
8180}
8181
8182/**
Jacob Keller0582b962016-07-19 16:23:29 -07008183 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8184 * @vsi: VSI structure
8185 **/
8186static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8187{
8188 u8 seed[I40E_HKEY_ARRAY_SIZE];
8189 struct i40e_pf *pf = vsi->back;
8190 u8 *lut;
8191 int ret;
8192
8193 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8194 return 0;
8195
Jacob Keller552b9962016-07-19 16:23:31 -07008196 if (!vsi->rss_size)
8197 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8198 vsi->num_queue_pairs);
8199 if (!vsi->rss_size)
8200 return -EINVAL;
8201
Jacob Keller0582b962016-07-19 16:23:29 -07008202 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8203 if (!lut)
8204 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008205 /* Use the user configured hash keys and lookup table if there is one,
8206 * otherwise use default
8207 */
8208 if (vsi->rss_lut_user)
8209 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8210 else
8211 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8212 if (vsi->rss_hkey_user)
8213 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8214 else
8215 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008216 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8217 kfree(lut);
8218
8219 return ret;
8220}
8221
8222/**
Helin Zhang043dd652015-10-21 19:56:23 -04008223 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008224 * @vsi: Pointer to vsi structure
8225 * @seed: RSS hash seed
8226 * @lut: Lookup table
8227 * @lut_size: Lookup table size
8228 *
8229 * Returns 0 on success, negative on failure
8230 **/
8231static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8232 const u8 *lut, u16 lut_size)
8233{
8234 struct i40e_pf *pf = vsi->back;
8235 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008236 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008237 u8 i;
8238
8239 /* Fill out hash function seed */
8240 if (seed) {
8241 u32 *seed_dw = (u32 *)seed;
8242
Mitch Williamsc4e18682016-04-12 08:30:40 -07008243 if (vsi->type == I40E_VSI_MAIN) {
8244 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008245 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008246 } else if (vsi->type == I40E_VSI_SRIOV) {
8247 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008248 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008249 } else {
8250 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8251 }
Helin Zhange69ff812015-10-21 19:56:22 -04008252 }
8253
8254 if (lut) {
8255 u32 *lut_dw = (u32 *)lut;
8256
Mitch Williamsc4e18682016-04-12 08:30:40 -07008257 if (vsi->type == I40E_VSI_MAIN) {
8258 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8259 return -EINVAL;
8260 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8261 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8262 } else if (vsi->type == I40E_VSI_SRIOV) {
8263 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8264 return -EINVAL;
8265 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008266 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008267 } else {
8268 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8269 }
Helin Zhange69ff812015-10-21 19:56:22 -04008270 }
8271 i40e_flush(hw);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008272
8273 return 0;
8274}
8275
8276/**
Helin Zhang043dd652015-10-21 19:56:23 -04008277 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8278 * @vsi: Pointer to VSI structure
8279 * @seed: Buffer to store the keys
8280 * @lut: Buffer to store the lookup table entries
8281 * @lut_size: Size of buffer to store the lookup table entries
8282 *
8283 * Returns 0 on success, negative on failure
8284 */
8285static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8286 u8 *lut, u16 lut_size)
8287{
8288 struct i40e_pf *pf = vsi->back;
8289 struct i40e_hw *hw = &pf->hw;
8290 u16 i;
8291
8292 if (seed) {
8293 u32 *seed_dw = (u32 *)seed;
8294
8295 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008296 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008297 }
8298 if (lut) {
8299 u32 *lut_dw = (u32 *)lut;
8300
8301 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8302 return -EINVAL;
8303 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8304 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8305 }
8306
8307 return 0;
8308}
8309
8310/**
8311 * i40e_config_rss - Configure RSS keys and lut
8312 * @vsi: Pointer to VSI structure
8313 * @seed: RSS hash seed
8314 * @lut: Lookup table
8315 * @lut_size: Lookup table size
8316 *
8317 * Returns 0 on success, negative on failure
8318 */
8319int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8320{
8321 struct i40e_pf *pf = vsi->back;
8322
8323 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8324 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8325 else
8326 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8327}
8328
8329/**
8330 * i40e_get_rss - Get RSS keys and lut
8331 * @vsi: Pointer to VSI structure
8332 * @seed: Buffer to store the keys
8333 * @lut: Buffer to store the lookup table entries
8334 * lut_size: Size of buffer to store the lookup table entries
8335 *
8336 * Returns 0 on success, negative on failure
8337 */
8338int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8339{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008340 struct i40e_pf *pf = vsi->back;
8341
8342 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8343 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8344 else
8345 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008346}
8347
8348/**
Helin Zhange69ff812015-10-21 19:56:22 -04008349 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8350 * @pf: Pointer to board private structure
8351 * @lut: Lookup table
8352 * @rss_table_size: Lookup table size
8353 * @rss_size: Range of queue number for hashing
8354 */
Alan Bradyf1582352016-08-24 11:33:46 -07008355void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8356 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008357{
Helin Zhange69ff812015-10-21 19:56:22 -04008358 u16 i;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008359
Helin Zhange69ff812015-10-21 19:56:22 -04008360 for (i = 0; i < rss_table_size; i++)
8361 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008362}
8363
8364/**
Helin Zhang043dd652015-10-21 19:56:23 -04008365 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008366 * @pf: board private structure
8367 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008368static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008369{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008370 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008371 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008372 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008373 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008374 u32 reg_val;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008375 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008376 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008377
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008378 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008379 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8380 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008381 hena |= i40e_pf_get_default_rss_hena(pf);
8382
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008383 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8384 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008385
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008386 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008387 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008388 reg_val = (pf->rss_table_size == 512) ?
8389 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8390 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008391 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008392
Helin Zhang28c58692015-10-26 19:44:27 -04008393 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008394 if (!vsi->rss_size) {
8395 u16 qcount;
8396
8397 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8398 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8399 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008400 if (!vsi->rss_size)
8401 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008402
Helin Zhange69ff812015-10-21 19:56:22 -04008403 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8404 if (!lut)
8405 return -ENOMEM;
8406
Helin Zhang28c58692015-10-26 19:44:27 -04008407 /* Use user configured lut if there is one, otherwise use default */
8408 if (vsi->rss_lut_user)
8409 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8410 else
8411 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008412
Helin Zhang28c58692015-10-26 19:44:27 -04008413 /* Use user configured hash key if there is one, otherwise
8414 * use default.
8415 */
8416 if (vsi->rss_hkey_user)
8417 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8418 else
8419 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008420 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008421 kfree(lut);
8422
8423 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008424}
8425
8426/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008427 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8428 * @pf: board private structure
8429 * @queue_count: the requested queue count for rss.
8430 *
8431 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8432 * count which may be different from the requested queue count.
8433 **/
8434int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8435{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008436 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8437 int new_rss_size;
8438
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008439 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8440 return 0;
8441
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008442 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008443
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008444 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008445 u16 qcount;
8446
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008447 vsi->req_queue_pairs = queue_count;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008448 i40e_prep_for_reset(pf);
8449
Helin Zhangacd65442015-10-26 19:44:28 -04008450 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008451
8452 i40e_reset_and_rebuild(pf, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008453
8454 /* Discard the user configured hash keys and lut, if less
8455 * queues are enabled.
8456 */
8457 if (queue_count < vsi->rss_size) {
8458 i40e_clear_rss_config_user(vsi);
8459 dev_dbg(&pf->pdev->dev,
8460 "discard user configured hash keys and lut\n");
8461 }
8462
8463 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008464 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8465 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008466
Helin Zhang043dd652015-10-21 19:56:23 -04008467 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008468 }
Lihong Yang12815052016-09-27 11:28:48 -07008469 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8470 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008471 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008472}
8473
8474/**
Greg Rosef4492db2015-02-06 08:52:12 +00008475 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8476 * @pf: board private structure
8477 **/
8478i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8479{
8480 i40e_status status;
8481 bool min_valid, max_valid;
8482 u32 max_bw, min_bw;
8483
8484 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8485 &min_valid, &max_valid);
8486
8487 if (!status) {
8488 if (min_valid)
8489 pf->npar_min_bw = min_bw;
8490 if (max_valid)
8491 pf->npar_max_bw = max_bw;
8492 }
8493
8494 return status;
8495}
8496
8497/**
8498 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8499 * @pf: board private structure
8500 **/
8501i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8502{
8503 struct i40e_aqc_configure_partition_bw_data bw_data;
8504 i40e_status status;
8505
Jeff Kirsherb40c82e62015-02-27 09:18:34 +00008506 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008507 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008508 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8509 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8510
8511 /* Set the new bandwidths */
8512 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8513
8514 return status;
8515}
8516
8517/**
8518 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8519 * @pf: board private structure
8520 **/
8521i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8522{
8523 /* Commit temporary BW setting to permanent NVM image */
8524 enum i40e_admin_queue_err last_aq_status;
8525 i40e_status ret;
8526 u16 nvm_word;
8527
8528 if (pf->hw.partition_id != 1) {
8529 dev_info(&pf->pdev->dev,
8530 "Commit BW only works on partition 1! This is partition %d",
8531 pf->hw.partition_id);
8532 ret = I40E_NOT_SUPPORTED;
8533 goto bw_commit_out;
8534 }
8535
8536 /* Acquire NVM for read access */
8537 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8538 last_aq_status = pf->hw.aq.asq_last_status;
8539 if (ret) {
8540 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008541 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8542 i40e_stat_str(&pf->hw, ret),
8543 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008544 goto bw_commit_out;
8545 }
8546
8547 /* Read word 0x10 of NVM - SW compatibility word 1 */
8548 ret = i40e_aq_read_nvm(&pf->hw,
8549 I40E_SR_NVM_CONTROL_WORD,
8550 0x10, sizeof(nvm_word), &nvm_word,
8551 false, NULL);
8552 /* Save off last admin queue command status before releasing
8553 * the NVM
8554 */
8555 last_aq_status = pf->hw.aq.asq_last_status;
8556 i40e_release_nvm(&pf->hw);
8557 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008558 dev_info(&pf->pdev->dev, "NVM read error, 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
8564 /* Wait a bit for NVM release to complete */
8565 msleep(50);
8566
8567 /* Acquire NVM for write access */
8568 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8569 last_aq_status = pf->hw.aq.asq_last_status;
8570 if (ret) {
8571 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008572 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8573 i40e_stat_str(&pf->hw, ret),
8574 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008575 goto bw_commit_out;
8576 }
8577 /* Write it back out unchanged to initiate update NVM,
8578 * which will force a write of the shadow (alt) RAM to
8579 * the NVM - thus storing the bandwidth values permanently.
8580 */
8581 ret = i40e_aq_update_nvm(&pf->hw,
8582 I40E_SR_NVM_CONTROL_WORD,
8583 0x10, sizeof(nvm_word),
8584 &nvm_word, true, NULL);
8585 /* Save off last admin queue command status before releasing
8586 * the NVM
8587 */
8588 last_aq_status = pf->hw.aq.asq_last_status;
8589 i40e_release_nvm(&pf->hw);
8590 if (ret)
8591 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008592 "BW settings NOT SAVED, err %s aq_err %s\n",
8593 i40e_stat_str(&pf->hw, ret),
8594 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008595bw_commit_out:
8596
8597 return ret;
8598}
8599
8600/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008601 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8602 * @pf: board private structure to initialize
8603 *
8604 * i40e_sw_init initializes the Adapter private data structure.
8605 * Fields are initialized based on PCI device information and
8606 * OS network device settings (MTU size).
8607 **/
8608static int i40e_sw_init(struct i40e_pf *pf)
8609{
8610 int err = 0;
8611 int size;
8612
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008613 /* Set default capability flags */
8614 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8615 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008616 I40E_FLAG_MSIX_ENABLED;
8617
Mitch Williamsca99eb92014-04-04 04:43:07 +00008618 /* Set default ITR */
8619 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8620 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8621
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008622 /* Depending on PF configurations, it is possible that the RSS
8623 * maximum might end up larger than the available queues
8624 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008625 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008626 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008627 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008628 pf->rss_size_max = min_t(int, pf->rss_size_max,
8629 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008630 if (pf->hw.func_caps.rss) {
8631 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008632 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8633 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008634 }
8635
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008636 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008637 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008638 pf->flags |= I40E_FLAG_MFP_ENABLED;
8639 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008640 if (i40e_get_npar_bw_setting(pf))
8641 dev_warn(&pf->pdev->dev,
8642 "Could not get NPAR bw settings\n");
8643 else
8644 dev_info(&pf->pdev->dev,
8645 "Min BW = %8.8x, Max BW = %8.8x\n",
8646 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008647 }
8648
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008649 /* FW/NVM is not yet fixed in this regard */
8650 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8651 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8652 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8653 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008654 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8655 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008656 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008657 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008658 else
8659 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008660 pf->fdir_pf_filter_count =
8661 pf->hw.func_caps.fd_filters_guaranteed;
8662 pf->hw.fdir_shared_filter_count =
8663 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008664 }
8665
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008666 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008667 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008668 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008669 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008670 /* No DCB support for FW < v4.33 */
8671 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8672 }
8673
8674 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008675 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008676 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8677 (pf->hw.aq.fw_maj_ver < 4)))
8678 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8679
8680 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008681 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008682 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8683 (pf->hw.aq.fw_maj_ver >= 5)))
8684 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008685
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008686 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008687 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -04008688 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008689 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008690 }
8691
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008692 if (pf->hw.func_caps.iwarp) {
8693 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8694 /* IWARP needs one extra vector for CQP just like MISC.*/
8695 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8696 }
8697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008698#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008699 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008700 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8701 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8702 pf->num_req_vfs = min_t(int,
8703 pf->hw.func_caps.num_vfs,
8704 I40E_MAX_VF_COUNT);
8705 }
8706#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008707 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008708 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8709 | I40E_FLAG_128_QP_RSS_CAPABLE
8710 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8711 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8712 | I40E_FLAG_WB_ON_ITR_CAPABLE
8713 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8714 | I40E_FLAG_NO_PCI_LINK_CHECK
8715 | I40E_FLAG_USE_SET_LLDP_MIB
8716 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8717 | I40E_FLAG_PTP_L4_CAPABLE
8718 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008719 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8720 ((pf->hw.aq.api_maj_ver == 1) &&
8721 (pf->hw.aq.api_min_ver > 4))) {
8722 /* Supported in FW API version higher than 1.4 */
8723 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008724 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008725 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008726 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008727 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008728
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008729 pf->eeprom_version = 0xDEAD;
8730 pf->lan_veb = I40E_NO_VEB;
8731 pf->lan_vsi = I40E_NO_VSI;
8732
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008733 /* By default FW has this off for performance reasons */
8734 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8735
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008736 /* set up queue assignment tracking */
8737 size = sizeof(struct i40e_lump_tracking)
8738 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8739 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8740 if (!pf->qp_pile) {
8741 err = -ENOMEM;
8742 goto sw_init_done;
8743 }
8744 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8745 pf->qp_pile->search_hint = 0;
8746
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008747 pf->tx_timeout_recovery_level = 1;
8748
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008749 mutex_init(&pf->switch_mutex);
8750
Greg Rosec668a122015-02-26 16:10:39 +00008751 /* If NPAR is enabled nudge the Tx scheduler */
8752 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8753 i40e_set_npar_bw_setting(pf);
8754
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008755sw_init_done:
8756 return err;
8757}
8758
8759/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008760 * i40e_set_ntuple - set the ntuple feature flag and take action
8761 * @pf: board private structure to initialize
8762 * @features: the feature set that the stack is suggesting
8763 *
8764 * returns a bool to indicate if reset needs to happen
8765 **/
8766bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8767{
8768 bool need_reset = false;
8769
8770 /* Check if Flow Director n-tuple support was enabled or disabled. If
8771 * the state changed, we need to reset.
8772 */
8773 if (features & NETIF_F_NTUPLE) {
8774 /* Enable filters and mark for reset */
8775 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8776 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008777 /* enable FD_SB only if there is MSI-X vector */
8778 if (pf->num_fdsb_msix > 0)
8779 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008780 } else {
8781 /* turn off filters, mark for reset and clear SW filter list */
8782 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8783 need_reset = true;
8784 i40e_fdir_filter_exit(pf);
8785 }
8786 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008787 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008788 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008789 pf->fd_add_err = 0;
8790 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008791 /* if ATR was auto disabled it can be re-enabled. */
8792 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008793 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8794 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008795 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8796 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8797 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008798 }
8799 return need_reset;
8800}
8801
8802/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008803 * i40e_clear_rss_lut - clear the rx hash lookup table
8804 * @vsi: the VSI being configured
8805 **/
8806static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8807{
8808 struct i40e_pf *pf = vsi->back;
8809 struct i40e_hw *hw = &pf->hw;
8810 u16 vf_id = vsi->vf_id;
8811 u8 i;
8812
8813 if (vsi->type == I40E_VSI_MAIN) {
8814 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8815 wr32(hw, I40E_PFQF_HLUT(i), 0);
8816 } else if (vsi->type == I40E_VSI_SRIOV) {
8817 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8818 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8819 } else {
8820 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8821 }
8822}
8823
8824/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008825 * i40e_set_features - set the netdev feature flags
8826 * @netdev: ptr to the netdev being adjusted
8827 * @features: the feature set that the stack is suggesting
8828 **/
8829static int i40e_set_features(struct net_device *netdev,
8830 netdev_features_t features)
8831{
8832 struct i40e_netdev_priv *np = netdev_priv(netdev);
8833 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008834 struct i40e_pf *pf = vsi->back;
8835 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008836
Alan Bradyd8ec9862016-07-27 12:02:38 -07008837 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8838 i40e_pf_config_rss(pf);
8839 else if (!(features & NETIF_F_RXHASH) &&
8840 netdev->features & NETIF_F_RXHASH)
8841 i40e_clear_rss_lut(vsi);
8842
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008843 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8844 i40e_vlan_stripping_enable(vsi);
8845 else
8846 i40e_vlan_stripping_disable(vsi);
8847
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008848 need_reset = i40e_set_ntuple(pf, features);
8849
8850 if (need_reset)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008851 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008852
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008853 return 0;
8854}
8855
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008856/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008857 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008858 * @pf: board private structure
8859 * @port: The UDP port to look up
8860 *
8861 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8862 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008863static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008864{
8865 u8 i;
8866
8867 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008868 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008869 return i;
8870 }
8871
8872 return i;
8873}
8874
8875/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008876 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008877 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008878 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008879 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008880static void i40e_udp_tunnel_add(struct net_device *netdev,
8881 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008882{
8883 struct i40e_netdev_priv *np = netdev_priv(netdev);
8884 struct i40e_vsi *vsi = np->vsi;
8885 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008886 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008887 u8 next_idx;
8888 u8 idx;
8889
Singhai, Anjali6a899022015-12-14 12:21:18 -08008890 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008891
8892 /* Check if port already exists */
8893 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008894 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008895 return;
8896 }
8897
8898 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008899 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008900
8901 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008902 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008903 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008904 return;
8905 }
8906
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008907 switch (ti->type) {
8908 case UDP_TUNNEL_TYPE_VXLAN:
8909 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8910 break;
8911 case UDP_TUNNEL_TYPE_GENEVE:
8912 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8913 return;
8914 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8915 break;
8916 default:
8917 return;
8918 }
8919
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008920 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008921 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008922 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8923 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008924}
8925
8926/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008927 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008928 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008929 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008930 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008931static void i40e_udp_tunnel_del(struct net_device *netdev,
8932 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008933{
8934 struct i40e_netdev_priv *np = netdev_priv(netdev);
8935 struct i40e_vsi *vsi = np->vsi;
8936 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008937 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008938 u8 idx;
8939
Singhai, Anjali6a899022015-12-14 12:21:18 -08008940 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008941
8942 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008943 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8944 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008945
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008946 switch (ti->type) {
8947 case UDP_TUNNEL_TYPE_VXLAN:
8948 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8949 goto not_found;
8950 break;
8951 case UDP_TUNNEL_TYPE_GENEVE:
8952 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8953 goto not_found;
8954 break;
8955 default:
8956 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008957 }
8958
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008959 /* if port exists, set it to 0 (mark for deletion)
8960 * and make it pending
8961 */
8962 pf->udp_ports[idx].index = 0;
8963 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008964 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8965
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008966 return;
8967not_found:
8968 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008969 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008970}
8971
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008972static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008973 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008974{
8975 struct i40e_netdev_priv *np = netdev_priv(netdev);
8976 struct i40e_pf *pf = np->vsi->back;
8977 struct i40e_hw *hw = &pf->hw;
8978
8979 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8980 return -EOPNOTSUPP;
8981
8982 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8983 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8984
8985 return 0;
8986}
8987
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08008988/**
8989 * i40e_ndo_fdb_add - add an entry to the hardware database
8990 * @ndm: the input from the stack
8991 * @tb: pointer to array of nladdr (unused)
8992 * @dev: the net device pointer
8993 * @addr: the MAC address entry being added
8994 * @flags: instructions from stack about fdb operation
8995 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00008996static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
8997 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01008998 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00008999 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009000{
9001 struct i40e_netdev_priv *np = netdev_priv(dev);
9002 struct i40e_pf *pf = np->vsi->back;
9003 int err = 0;
9004
9005 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9006 return -EOPNOTSUPP;
9007
Or Gerlitz65891fe2014-12-14 18:19:05 +02009008 if (vid) {
9009 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9010 return -EINVAL;
9011 }
9012
Greg Rose4ba0dea2014-03-06 08:59:55 +00009013 /* Hardware does not support aging addresses so if a
9014 * ndm_state is given only allow permanent addresses
9015 */
9016 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9017 netdev_info(dev, "FDB only supports static addresses\n");
9018 return -EINVAL;
9019 }
9020
9021 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9022 err = dev_uc_add_excl(dev, addr);
9023 else if (is_multicast_ether_addr(addr))
9024 err = dev_mc_add_excl(dev, addr);
9025 else
9026 err = -EINVAL;
9027
9028 /* Only return duplicate errors if NLM_F_EXCL is set */
9029 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9030 err = 0;
9031
9032 return err;
9033}
9034
Neerav Parikh51616012015-02-06 08:52:14 +00009035/**
9036 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9037 * @dev: the netdev being configured
9038 * @nlh: RTNL message
9039 *
9040 * Inserts a new hardware bridge if not already created and
9041 * enables the bridging mode requested (VEB or VEPA). If the
9042 * hardware bridge has already been inserted and the request
9043 * is to change the mode then that requires a PF reset to
9044 * allow rebuild of the components with required hardware
9045 * bridge mode enabled.
9046 **/
9047static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009048 struct nlmsghdr *nlh,
9049 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009050{
9051 struct i40e_netdev_priv *np = netdev_priv(dev);
9052 struct i40e_vsi *vsi = np->vsi;
9053 struct i40e_pf *pf = vsi->back;
9054 struct i40e_veb *veb = NULL;
9055 struct nlattr *attr, *br_spec;
9056 int i, rem;
9057
9058 /* Only for PF VSI for now */
9059 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9060 return -EOPNOTSUPP;
9061
9062 /* Find the HW bridge for PF VSI */
9063 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9064 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9065 veb = pf->veb[i];
9066 }
9067
9068 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9069
9070 nla_for_each_nested(attr, br_spec, rem) {
9071 __u16 mode;
9072
9073 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9074 continue;
9075
9076 mode = nla_get_u16(attr);
9077 if ((mode != BRIDGE_MODE_VEPA) &&
9078 (mode != BRIDGE_MODE_VEB))
9079 return -EINVAL;
9080
9081 /* Insert a new HW bridge */
9082 if (!veb) {
9083 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9084 vsi->tc_config.enabled_tc);
9085 if (veb) {
9086 veb->bridge_mode = mode;
9087 i40e_config_bridge_mode(veb);
9088 } else {
9089 /* No Bridge HW offload available */
9090 return -ENOENT;
9091 }
9092 break;
9093 } else if (mode != veb->bridge_mode) {
9094 /* Existing HW bridge but different mode needs reset */
9095 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009096 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9097 if (mode == BRIDGE_MODE_VEB)
9098 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9099 else
9100 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9101 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Neerav Parikh51616012015-02-06 08:52:14 +00009102 break;
9103 }
9104 }
9105
9106 return 0;
9107}
9108
9109/**
9110 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9111 * @skb: skb buff
9112 * @pid: process id
9113 * @seq: RTNL message seq #
9114 * @dev: the netdev being configured
9115 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009116 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009117 *
9118 * Return the mode in which the hardware bridge is operating in
9119 * i.e VEB or VEPA.
9120 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009121static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9122 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009123 u32 __always_unused filter_mask,
9124 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009125{
9126 struct i40e_netdev_priv *np = netdev_priv(dev);
9127 struct i40e_vsi *vsi = np->vsi;
9128 struct i40e_pf *pf = vsi->back;
9129 struct i40e_veb *veb = NULL;
9130 int i;
9131
9132 /* Only for PF VSI for now */
9133 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9134 return -EOPNOTSUPP;
9135
9136 /* Find the HW bridge for the PF VSI */
9137 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9138 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9139 veb = pf->veb[i];
9140 }
9141
9142 if (!veb)
9143 return 0;
9144
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009145 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009146 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009147}
Neerav Parikh51616012015-02-06 08:52:14 +00009148
Joe Stringerf44a75e2015-04-14 17:09:14 -07009149/**
9150 * i40e_features_check - Validate encapsulated packet conforms to limits
9151 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009152 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009153 * @features: Offload features that the stack believes apply
9154 **/
9155static netdev_features_t i40e_features_check(struct sk_buff *skb,
9156 struct net_device *dev,
9157 netdev_features_t features)
9158{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009159 size_t len;
9160
9161 /* No point in doing any of this if neither checksum nor GSO are
9162 * being requested for this frame. We can rule out both by just
9163 * checking for CHECKSUM_PARTIAL
9164 */
9165 if (skb->ip_summed != CHECKSUM_PARTIAL)
9166 return features;
9167
9168 /* We cannot support GSO if the MSS is going to be less than
9169 * 64 bytes. If it is then we need to drop support for GSO.
9170 */
9171 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9172 features &= ~NETIF_F_GSO_MASK;
9173
9174 /* MACLEN can support at most 63 words */
9175 len = skb_network_header(skb) - skb->data;
9176 if (len & ~(63 * 2))
9177 goto out_err;
9178
9179 /* IPLEN and EIPLEN can support at most 127 dwords */
9180 len = skb_transport_header(skb) - skb_network_header(skb);
9181 if (len & ~(127 * 4))
9182 goto out_err;
9183
9184 if (skb->encapsulation) {
9185 /* L4TUNLEN can support 127 words */
9186 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9187 if (len & ~(127 * 2))
9188 goto out_err;
9189
9190 /* IPLEN can support at most 127 dwords */
9191 len = skb_inner_transport_header(skb) -
9192 skb_inner_network_header(skb);
9193 if (len & ~(127 * 4))
9194 goto out_err;
9195 }
9196
9197 /* No need to validate L4LEN as TCP is the only protocol with a
9198 * a flexible value and we support all possible values supported
9199 * by TCP, which is at most 15 dwords
9200 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009201
9202 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009203out_err:
9204 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009205}
9206
Shannon Nelson37a29732015-02-27 09:15:19 +00009207static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009208 .ndo_open = i40e_open,
9209 .ndo_stop = i40e_close,
9210 .ndo_start_xmit = i40e_lan_xmit_frame,
9211 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9212 .ndo_set_rx_mode = i40e_set_rx_mode,
9213 .ndo_validate_addr = eth_validate_addr,
9214 .ndo_set_mac_address = i40e_set_mac,
9215 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009216 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009217 .ndo_tx_timeout = i40e_tx_timeout,
9218 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9219 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9220#ifdef CONFIG_NET_POLL_CONTROLLER
9221 .ndo_poll_controller = i40e_netpoll,
9222#endif
John Fastabende4c67342016-02-16 21:16:15 -08009223 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009224 .ndo_set_features = i40e_set_features,
9225 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9226 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009227 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009228 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009229 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009230 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009231 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009232 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9233 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009234 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009235 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009236 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009237 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9238 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009239};
9240
9241/**
9242 * i40e_config_netdev - Setup the netdev flags
9243 * @vsi: the VSI being configured
9244 *
9245 * Returns 0 on success, negative value on failure
9246 **/
9247static int i40e_config_netdev(struct i40e_vsi *vsi)
9248{
9249 struct i40e_pf *pf = vsi->back;
9250 struct i40e_hw *hw = &pf->hw;
9251 struct i40e_netdev_priv *np;
9252 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009253 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009254 u8 mac_addr[ETH_ALEN];
9255 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009256 netdev_features_t hw_enc_features;
9257 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009258
9259 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009260 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009261 if (!netdev)
9262 return -ENOMEM;
9263
9264 vsi->netdev = netdev;
9265 np = netdev_priv(netdev);
9266 np->vsi = vsi;
9267
Preethi Banalabacd75c2017-03-27 14:43:18 -07009268 hw_enc_features = NETIF_F_SG |
9269 NETIF_F_IP_CSUM |
9270 NETIF_F_IPV6_CSUM |
9271 NETIF_F_HIGHDMA |
9272 NETIF_F_SOFT_FEATURES |
9273 NETIF_F_TSO |
9274 NETIF_F_TSO_ECN |
9275 NETIF_F_TSO6 |
9276 NETIF_F_GSO_GRE |
9277 NETIF_F_GSO_GRE_CSUM |
9278 NETIF_F_GSO_PARTIAL |
9279 NETIF_F_GSO_UDP_TUNNEL |
9280 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9281 NETIF_F_SCTP_CRC |
9282 NETIF_F_RXHASH |
9283 NETIF_F_RXCSUM |
9284 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009285
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009286 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009287 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9288
9289 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009290
Preethi Banalabacd75c2017-03-27 14:43:18 -07009291 netdev->hw_enc_features |= hw_enc_features;
9292
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009293 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009294 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009295
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009296 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009297 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009298 hw_features = hw_enc_features |
9299 NETIF_F_HW_VLAN_CTAG_TX |
9300 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009301
Preethi Banalabacd75c2017-03-27 14:43:18 -07009302 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009303
Preethi Banalabacd75c2017-03-27 14:43:18 -07009304 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009305 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009306
9307 if (vsi->type == I40E_VSI_MAIN) {
9308 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009309 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Filip Sadowski3954b392017-01-30 12:29:34 -08009310 /* The following steps are necessary to properly keep track of
9311 * MAC-VLAN filters loaded into firmware - first we remove
9312 * filter that is automatically generated by firmware and then
9313 * add new filter both to the driver hash table and firmware.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009314 */
9315 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009316 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009317 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009318 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009319 } else {
9320 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9321 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9322 pf->vsi[pf->lan_vsi]->netdev->name);
9323 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009324
Jacob Keller278e7d02016-10-05 09:30:37 -07009325 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009326 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009327 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009328 }
Kiran Patil21659032015-09-30 14:09:03 -04009329
Jacob Keller435c0842016-11-08 13:05:10 -08009330 /* Add the broadcast filter so that we initially will receive
9331 * broadcast packets. Note that when a new VLAN is first added the
9332 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9333 * specific filters as part of transitioning into "vlan" operation.
9334 * When more VLANs are added, the driver will copy each existing MAC
9335 * filter and add it for the new VLAN.
9336 *
9337 * Broadcast filters are handled specially by
9338 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9339 * promiscuous bit instead of adding this directly as a MAC/VLAN
9340 * filter. The subtask will update the correct broadcast promiscuous
9341 * bits as VLANs become active or inactive.
9342 */
9343 eth_broadcast_addr(broadcast);
9344 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009345 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009346 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9347
Greg Rose9a173902014-05-22 06:32:02 +00009348 ether_addr_copy(netdev->dev_addr, mac_addr);
9349 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009350
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009351 netdev->priv_flags |= IFF_UNICAST_FLT;
9352 netdev->priv_flags |= IFF_SUPP_NOFCS;
9353 /* Setup netdev TC information */
9354 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9355
9356 netdev->netdev_ops = &i40e_netdev_ops;
9357 netdev->watchdog_timeo = 5 * HZ;
9358 i40e_set_ethtool_ops(netdev);
9359
Jarod Wilson91c527a2016-10-17 15:54:05 -04009360 /* MTU range: 68 - 9706 */
9361 netdev->min_mtu = ETH_MIN_MTU;
9362 netdev->max_mtu = I40E_MAX_RXBUFFER -
9363 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9364
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009365 return 0;
9366}
9367
9368/**
9369 * i40e_vsi_delete - Delete a VSI from the switch
9370 * @vsi: the VSI being removed
9371 *
9372 * Returns 0 on success, negative value on failure
9373 **/
9374static void i40e_vsi_delete(struct i40e_vsi *vsi)
9375{
9376 /* remove default VSI is not allowed */
9377 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9378 return;
9379
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009380 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009381}
9382
9383/**
Neerav Parikh51616012015-02-06 08:52:14 +00009384 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9385 * @vsi: the VSI being queried
9386 *
9387 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9388 **/
9389int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9390{
9391 struct i40e_veb *veb;
9392 struct i40e_pf *pf = vsi->back;
9393
9394 /* Uplink is not a bridge so default to VEB */
9395 if (vsi->veb_idx == I40E_NO_VEB)
9396 return 1;
9397
9398 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009399 if (!veb) {
9400 dev_info(&pf->pdev->dev,
9401 "There is no veb associated with the bridge\n");
9402 return -ENOENT;
9403 }
Neerav Parikh51616012015-02-06 08:52:14 +00009404
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009405 /* Uplink is a bridge in VEPA mode */
9406 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9407 return 0;
9408 } else {
9409 /* Uplink is a bridge in VEB mode */
9410 return 1;
9411 }
9412
9413 /* VEPA is now default bridge, so return 0 */
9414 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009415}
9416
9417/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009418 * i40e_add_vsi - Add a VSI to the switch
9419 * @vsi: the VSI being configured
9420 *
9421 * This initializes a VSI context depending on the VSI type to be added and
9422 * passes it down to the add_vsi aq command.
9423 **/
9424static int i40e_add_vsi(struct i40e_vsi *vsi)
9425{
9426 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009427 struct i40e_pf *pf = vsi->back;
9428 struct i40e_hw *hw = &pf->hw;
9429 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009430 struct i40e_mac_filter *f;
9431 struct hlist_node *h;
9432 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009433
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009434 u8 enabled_tc = 0x1; /* TC0 enabled */
9435 int f_count = 0;
9436
9437 memset(&ctxt, 0, sizeof(ctxt));
9438 switch (vsi->type) {
9439 case I40E_VSI_MAIN:
9440 /* The PF's main VSI is already setup as part of the
9441 * device initialization, so we'll not bother with
9442 * the add_vsi call, but we will retrieve the current
9443 * VSI context.
9444 */
9445 ctxt.seid = pf->main_vsi_seid;
9446 ctxt.pf_num = pf->hw.pf_id;
9447 ctxt.vf_num = 0;
9448 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9449 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9450 if (ret) {
9451 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009452 "couldn't get PF vsi config, err %s aq_err %s\n",
9453 i40e_stat_str(&pf->hw, ret),
9454 i40e_aq_str(&pf->hw,
9455 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009456 return -ENOENT;
9457 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009458 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009459 vsi->info.valid_sections = 0;
9460
9461 vsi->seid = ctxt.seid;
9462 vsi->id = ctxt.vsi_number;
9463
9464 enabled_tc = i40e_pf_get_tc_map(pf);
9465
9466 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009467 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9468 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009469 memset(&ctxt, 0, sizeof(ctxt));
9470 ctxt.seid = pf->main_vsi_seid;
9471 ctxt.pf_num = pf->hw.pf_id;
9472 ctxt.vf_num = 0;
9473 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9474 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9475 if (ret) {
9476 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009477 "update vsi failed, err %s aq_err %s\n",
9478 i40e_stat_str(&pf->hw, ret),
9479 i40e_aq_str(&pf->hw,
9480 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009481 ret = -ENOENT;
9482 goto err;
9483 }
9484 /* update the local VSI info queue map */
9485 i40e_vsi_update_queue_map(vsi, &ctxt);
9486 vsi->info.valid_sections = 0;
9487 } else {
9488 /* Default/Main VSI is only enabled for TC0
9489 * reconfigure it to enable all TCs that are
9490 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009491 * For MFP case the iSCSI PF would use this
9492 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009493 */
9494 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9495 if (ret) {
9496 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009497 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9498 enabled_tc,
9499 i40e_stat_str(&pf->hw, ret),
9500 i40e_aq_str(&pf->hw,
9501 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009502 ret = -ENOENT;
9503 }
9504 }
9505 break;
9506
9507 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009508 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;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009512 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009513 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9514 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009515 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009516 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009517 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009518 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009519 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009520 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009521 break;
9522
9523 case I40E_VSI_VMDQ2:
9524 ctxt.pf_num = hw->pf_id;
9525 ctxt.vf_num = 0;
9526 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009527 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009528 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
9529
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009530 /* This VSI is connected to VEB so the switch_id
9531 * should be set to zero by default.
9532 */
Neerav Parikh51616012015-02-06 08:52:14 +00009533 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9534 ctxt.info.valid_sections |=
9535 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9536 ctxt.info.switch_id =
9537 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9538 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009539
9540 /* Setup the VSI tx/rx queue map for TC0 only for now */
9541 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9542 break;
9543
9544 case I40E_VSI_SRIOV:
9545 ctxt.pf_num = hw->pf_id;
9546 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9547 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009548 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009549 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9550
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009551 /* This VSI is connected to VEB so the switch_id
9552 * should be set to zero by default.
9553 */
Neerav Parikh51616012015-02-06 08:52:14 +00009554 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9555 ctxt.info.valid_sections |=
9556 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9557 ctxt.info.switch_id =
9558 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9559 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009560
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009561 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9562 ctxt.info.valid_sections |=
9563 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9564 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009565 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9566 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009567 }
9568
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009569 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9570 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009571 if (pf->vf[vsi->vf_id].spoofchk) {
9572 ctxt.info.valid_sections |=
9573 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9574 ctxt.info.sec_flags |=
9575 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9576 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9577 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009578 /* Setup the VSI tx/rx queue map for TC0 only for now */
9579 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9580 break;
9581
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009582 case I40E_VSI_IWARP:
9583 /* send down message to iWARP */
9584 break;
9585
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009586 default:
9587 return -ENODEV;
9588 }
9589
9590 if (vsi->type != I40E_VSI_MAIN) {
9591 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9592 if (ret) {
9593 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009594 "add vsi failed, err %s aq_err %s\n",
9595 i40e_stat_str(&pf->hw, ret),
9596 i40e_aq_str(&pf->hw,
9597 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009598 ret = -ENOENT;
9599 goto err;
9600 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009601 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009602 vsi->info.valid_sections = 0;
9603 vsi->seid = ctxt.seid;
9604 vsi->id = ctxt.vsi_number;
9605 }
9606
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009607 vsi->active_filters = 0;
9608 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009609 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009610 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009611 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009612 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009613 f_count++;
9614 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009615 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009616
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009617 if (f_count) {
9618 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9619 pf->flags |= I40E_FLAG_FILTER_SYNC;
9620 }
9621
9622 /* Update VSI BW information */
9623 ret = i40e_vsi_get_bw_info(vsi);
9624 if (ret) {
9625 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009626 "couldn't get vsi bw info, err %s aq_err %s\n",
9627 i40e_stat_str(&pf->hw, ret),
9628 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009629 /* VSI is already added so not tearing that up */
9630 ret = 0;
9631 }
9632
9633err:
9634 return ret;
9635}
9636
9637/**
9638 * i40e_vsi_release - Delete a VSI and free its resources
9639 * @vsi: the VSI being removed
9640 *
9641 * Returns 0 on success or < 0 on error
9642 **/
9643int i40e_vsi_release(struct i40e_vsi *vsi)
9644{
Jacob Keller278e7d02016-10-05 09:30:37 -07009645 struct i40e_mac_filter *f;
9646 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009647 struct i40e_veb *veb = NULL;
9648 struct i40e_pf *pf;
9649 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009650 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009651
9652 pf = vsi->back;
9653
9654 /* release of a VEB-owner or last VSI is not allowed */
9655 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9656 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9657 vsi->seid, vsi->uplink_seid);
9658 return -ENODEV;
9659 }
9660 if (vsi == pf->vsi[pf->lan_vsi] &&
9661 !test_bit(__I40E_DOWN, &pf->state)) {
9662 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9663 return -ENODEV;
9664 }
9665
9666 uplink_seid = vsi->uplink_seid;
9667 if (vsi->type != I40E_VSI_SRIOV) {
9668 if (vsi->netdev_registered) {
9669 vsi->netdev_registered = false;
9670 if (vsi->netdev) {
9671 /* results in a call to i40e_close() */
9672 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009673 }
9674 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009675 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009676 }
9677 i40e_vsi_disable_irq(vsi);
9678 }
9679
Jacob Keller278e7d02016-10-05 09:30:37 -07009680 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009681
9682 /* clear the sync flag on all filters */
9683 if (vsi->netdev) {
9684 __dev_uc_unsync(vsi->netdev, NULL);
9685 __dev_mc_unsync(vsi->netdev, NULL);
9686 }
9687
9688 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009689 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009690 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009691
Jacob Keller278e7d02016-10-05 09:30:37 -07009692 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009693
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009694 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009695
9696 i40e_vsi_delete(vsi);
9697 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009698 if (vsi->netdev) {
9699 free_netdev(vsi->netdev);
9700 vsi->netdev = NULL;
9701 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009702 i40e_vsi_clear_rings(vsi);
9703 i40e_vsi_clear(vsi);
9704
9705 /* If this was the last thing on the VEB, except for the
9706 * controlling VSI, remove the VEB, which puts the controlling
9707 * VSI onto the next level down in the switch.
9708 *
9709 * Well, okay, there's one more exception here: don't remove
9710 * the orphan VEBs yet. We'll wait for an explicit remove request
9711 * from up the network stack.
9712 */
Mitch Williams505682c2014-05-20 08:01:37 +00009713 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009714 if (pf->vsi[i] &&
9715 pf->vsi[i]->uplink_seid == uplink_seid &&
9716 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9717 n++; /* count the VSIs */
9718 }
9719 }
9720 for (i = 0; i < I40E_MAX_VEB; i++) {
9721 if (!pf->veb[i])
9722 continue;
9723 if (pf->veb[i]->uplink_seid == uplink_seid)
9724 n++; /* count the VEBs */
9725 if (pf->veb[i]->seid == uplink_seid)
9726 veb = pf->veb[i];
9727 }
9728 if (n == 0 && veb && veb->uplink_seid != 0)
9729 i40e_veb_release(veb);
9730
9731 return 0;
9732}
9733
9734/**
9735 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9736 * @vsi: ptr to the VSI
9737 *
9738 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9739 * corresponding SW VSI structure and initializes num_queue_pairs for the
9740 * newly allocated VSI.
9741 *
9742 * Returns 0 on success or negative on failure
9743 **/
9744static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9745{
9746 int ret = -ENOENT;
9747 struct i40e_pf *pf = vsi->back;
9748
Alexander Duyck493fb302013-09-28 07:01:44 +00009749 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009750 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9751 vsi->seid);
9752 return -EEXIST;
9753 }
9754
9755 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009756 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009757 vsi->seid, vsi->base_vector);
9758 return -EEXIST;
9759 }
9760
Greg Rose90e04072014-03-06 08:59:57 +00009761 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009762 if (ret) {
9763 dev_info(&pf->pdev->dev,
9764 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9765 vsi->num_q_vectors, vsi->seid, ret);
9766 vsi->num_q_vectors = 0;
9767 goto vector_setup_out;
9768 }
9769
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009770 /* In Legacy mode, we do not have to get any other vector since we
9771 * piggyback on the misc/ICR0 for queue interrupts.
9772 */
9773 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9774 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009775 if (vsi->num_q_vectors)
9776 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9777 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009778 if (vsi->base_vector < 0) {
9779 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009780 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9781 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009782 i40e_vsi_free_q_vectors(vsi);
9783 ret = -ENOENT;
9784 goto vector_setup_out;
9785 }
9786
9787vector_setup_out:
9788 return ret;
9789}
9790
9791/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009792 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9793 * @vsi: pointer to the vsi.
9794 *
9795 * This re-allocates a vsi's queue resources.
9796 *
9797 * Returns pointer to the successfully allocated and configured VSI sw struct
9798 * on success, otherwise returns NULL on failure.
9799 **/
9800static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9801{
John Underwoodf5340392016-02-18 09:19:24 -08009802 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009803 u8 enabled_tc;
9804 int ret;
9805
John Underwoodf5340392016-02-18 09:19:24 -08009806 if (!vsi)
9807 return NULL;
9808
9809 pf = vsi->back;
9810
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009811 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9812 i40e_vsi_clear_rings(vsi);
9813
9814 i40e_vsi_free_arrays(vsi, false);
9815 i40e_set_num_rings_in_vsi(vsi);
9816 ret = i40e_vsi_alloc_arrays(vsi, false);
9817 if (ret)
9818 goto err_vsi;
9819
9820 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9821 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009822 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009823 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009824 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009825 goto err_vsi;
9826 }
9827 vsi->base_queue = ret;
9828
9829 /* Update the FW view of the VSI. Force a reset of TC and queue
9830 * layout configurations.
9831 */
9832 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9833 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9834 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9835 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009836 if (vsi->type == I40E_VSI_MAIN)
9837 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009838
9839 /* assign it some queues */
9840 ret = i40e_alloc_rings(vsi);
9841 if (ret)
9842 goto err_rings;
9843
9844 /* map all of the rings to the q_vectors */
9845 i40e_vsi_map_rings_to_vectors(vsi);
9846 return vsi;
9847
9848err_rings:
9849 i40e_vsi_free_q_vectors(vsi);
9850 if (vsi->netdev_registered) {
9851 vsi->netdev_registered = false;
9852 unregister_netdev(vsi->netdev);
9853 free_netdev(vsi->netdev);
9854 vsi->netdev = NULL;
9855 }
9856 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9857err_vsi:
9858 i40e_vsi_clear(vsi);
9859 return NULL;
9860}
9861
9862/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009863 * i40e_vsi_setup - Set up a VSI by a given type
9864 * @pf: board private structure
9865 * @type: VSI type
9866 * @uplink_seid: the switch element to link to
9867 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9868 *
9869 * This allocates the sw VSI structure and its queue resources, then add a VSI
9870 * to the identified VEB.
9871 *
9872 * Returns pointer to the successfully allocated and configure VSI sw struct on
9873 * success, otherwise returns NULL on failure.
9874 **/
9875struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9876 u16 uplink_seid, u32 param1)
9877{
9878 struct i40e_vsi *vsi = NULL;
9879 struct i40e_veb *veb = NULL;
9880 int ret, i;
9881 int v_idx;
9882
9883 /* The requested uplink_seid must be either
9884 * - the PF's port seid
9885 * no VEB is needed because this is the PF
9886 * or this is a Flow Director special case VSI
9887 * - seid of an existing VEB
9888 * - seid of a VSI that owns an existing VEB
9889 * - seid of a VSI that doesn't own a VEB
9890 * a new VEB is created and the VSI becomes the owner
9891 * - seid of the PF VSI, which is what creates the first VEB
9892 * this is a special case of the previous
9893 *
9894 * Find which uplink_seid we were given and create a new VEB if needed
9895 */
9896 for (i = 0; i < I40E_MAX_VEB; i++) {
9897 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9898 veb = pf->veb[i];
9899 break;
9900 }
9901 }
9902
9903 if (!veb && uplink_seid != pf->mac_seid) {
9904
Mitch Williams505682c2014-05-20 08:01:37 +00009905 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009906 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9907 vsi = pf->vsi[i];
9908 break;
9909 }
9910 }
9911 if (!vsi) {
9912 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9913 uplink_seid);
9914 return NULL;
9915 }
9916
9917 if (vsi->uplink_seid == pf->mac_seid)
9918 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9919 vsi->tc_config.enabled_tc);
9920 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9921 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9922 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009923 if (veb) {
9924 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9925 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009926 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009927 return NULL;
9928 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009929 /* We come up by default in VEPA mode if SRIOV is not
9930 * already enabled, in which case we can't force VEPA
9931 * mode.
9932 */
9933 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9934 veb->bridge_mode = BRIDGE_MODE_VEPA;
9935 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9936 }
Neerav Parikh51616012015-02-06 08:52:14 +00009937 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009938 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009939 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9940 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9941 veb = pf->veb[i];
9942 }
9943 if (!veb) {
9944 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9945 return NULL;
9946 }
9947
9948 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9949 uplink_seid = veb->seid;
9950 }
9951
9952 /* get vsi sw struct */
9953 v_idx = i40e_vsi_mem_alloc(pf, type);
9954 if (v_idx < 0)
9955 goto err_alloc;
9956 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009957 if (!vsi)
9958 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009959 vsi->type = type;
9960 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9961
9962 if (type == I40E_VSI_MAIN)
9963 pf->lan_vsi = v_idx;
9964 else if (type == I40E_VSI_SRIOV)
9965 vsi->vf_id = param1;
9966 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009967 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9968 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009969 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009970 dev_info(&pf->pdev->dev,
9971 "failed to get tracking for %d queues for VSI %d err=%d\n",
9972 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009973 goto err_vsi;
9974 }
9975 vsi->base_queue = ret;
9976
9977 /* get a VSI from the hardware */
9978 vsi->uplink_seid = uplink_seid;
9979 ret = i40e_add_vsi(vsi);
9980 if (ret)
9981 goto err_vsi;
9982
9983 switch (vsi->type) {
9984 /* setup the netdev if needed */
9985 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -05009986 /* Apply relevant filters if a platform-specific mac
9987 * address was selected.
9988 */
9989 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
9990 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
9991 if (ret) {
9992 dev_warn(&pf->pdev->dev,
9993 "could not set up macaddr; err %d\n",
9994 ret);
9995 }
9996 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009997 case I40E_VSI_VMDQ2:
9998 ret = i40e_config_netdev(vsi);
9999 if (ret)
10000 goto err_netdev;
10001 ret = register_netdev(vsi->netdev);
10002 if (ret)
10003 goto err_netdev;
10004 vsi->netdev_registered = true;
10005 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010006#ifdef CONFIG_I40E_DCB
10007 /* Setup DCB netlink interface */
10008 i40e_dcbnl_setup(vsi);
10009#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010010 /* fall through */
10011
10012 case I40E_VSI_FDIR:
10013 /* set up vectors and rings if needed */
10014 ret = i40e_vsi_setup_vectors(vsi);
10015 if (ret)
10016 goto err_msix;
10017
10018 ret = i40e_alloc_rings(vsi);
10019 if (ret)
10020 goto err_rings;
10021
10022 /* map all of the rings to the q_vectors */
10023 i40e_vsi_map_rings_to_vectors(vsi);
10024
10025 i40e_vsi_reset_stats(vsi);
10026 break;
10027
10028 default:
10029 /* no netdev or rings for the other VSI types */
10030 break;
10031 }
10032
Anjali Singhai Jaine25d00b82015-06-23 19:00:04 -040010033 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10034 (vsi->type == I40E_VSI_VMDQ2)) {
10035 ret = i40e_vsi_config_rss(vsi);
10036 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010037 return vsi;
10038
10039err_rings:
10040 i40e_vsi_free_q_vectors(vsi);
10041err_msix:
10042 if (vsi->netdev_registered) {
10043 vsi->netdev_registered = false;
10044 unregister_netdev(vsi->netdev);
10045 free_netdev(vsi->netdev);
10046 vsi->netdev = NULL;
10047 }
10048err_netdev:
10049 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10050err_vsi:
10051 i40e_vsi_clear(vsi);
10052err_alloc:
10053 return NULL;
10054}
10055
10056/**
10057 * i40e_veb_get_bw_info - Query VEB BW information
10058 * @veb: the veb to query
10059 *
10060 * Query the Tx scheduler BW configuration data for given VEB
10061 **/
10062static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10063{
10064 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10065 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10066 struct i40e_pf *pf = veb->pf;
10067 struct i40e_hw *hw = &pf->hw;
10068 u32 tc_bw_max;
10069 int ret = 0;
10070 int i;
10071
10072 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10073 &bw_data, NULL);
10074 if (ret) {
10075 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010076 "query veb bw config failed, err %s aq_err %s\n",
10077 i40e_stat_str(&pf->hw, ret),
10078 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010079 goto out;
10080 }
10081
10082 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10083 &ets_data, NULL);
10084 if (ret) {
10085 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010086 "query veb bw ets config failed, err %s aq_err %s\n",
10087 i40e_stat_str(&pf->hw, ret),
10088 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010089 goto out;
10090 }
10091
10092 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10093 veb->bw_max_quanta = ets_data.tc_bw_max;
10094 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010095 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010096 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10097 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10098 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10099 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10100 veb->bw_tc_limit_credits[i] =
10101 le16_to_cpu(bw_data.tc_bw_limits[i]);
10102 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10103 }
10104
10105out:
10106 return ret;
10107}
10108
10109/**
10110 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10111 * @pf: board private structure
10112 *
10113 * On error: returns error code (negative)
10114 * On success: returns vsi index in PF (positive)
10115 **/
10116static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10117{
10118 int ret = -ENOENT;
10119 struct i40e_veb *veb;
10120 int i;
10121
10122 /* Need to protect the allocation of switch elements at the PF level */
10123 mutex_lock(&pf->switch_mutex);
10124
10125 /* VEB list may be fragmented if VEB creation/destruction has
10126 * been happening. We can afford to do a quick scan to look
10127 * for any free slots in the list.
10128 *
10129 * find next empty veb slot, looping back around if necessary
10130 */
10131 i = 0;
10132 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10133 i++;
10134 if (i >= I40E_MAX_VEB) {
10135 ret = -ENOMEM;
10136 goto err_alloc_veb; /* out of VEB slots! */
10137 }
10138
10139 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10140 if (!veb) {
10141 ret = -ENOMEM;
10142 goto err_alloc_veb;
10143 }
10144 veb->pf = pf;
10145 veb->idx = i;
10146 veb->enabled_tc = 1;
10147
10148 pf->veb[i] = veb;
10149 ret = i;
10150err_alloc_veb:
10151 mutex_unlock(&pf->switch_mutex);
10152 return ret;
10153}
10154
10155/**
10156 * i40e_switch_branch_release - Delete a branch of the switch tree
10157 * @branch: where to start deleting
10158 *
10159 * This uses recursion to find the tips of the branch to be
10160 * removed, deleting until we get back to and can delete this VEB.
10161 **/
10162static void i40e_switch_branch_release(struct i40e_veb *branch)
10163{
10164 struct i40e_pf *pf = branch->pf;
10165 u16 branch_seid = branch->seid;
10166 u16 veb_idx = branch->idx;
10167 int i;
10168
10169 /* release any VEBs on this VEB - RECURSION */
10170 for (i = 0; i < I40E_MAX_VEB; i++) {
10171 if (!pf->veb[i])
10172 continue;
10173 if (pf->veb[i]->uplink_seid == branch->seid)
10174 i40e_switch_branch_release(pf->veb[i]);
10175 }
10176
10177 /* Release the VSIs on this VEB, but not the owner VSI.
10178 *
10179 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10180 * the VEB itself, so don't use (*branch) after this loop.
10181 */
Mitch Williams505682c2014-05-20 08:01:37 +000010182 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010183 if (!pf->vsi[i])
10184 continue;
10185 if (pf->vsi[i]->uplink_seid == branch_seid &&
10186 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10187 i40e_vsi_release(pf->vsi[i]);
10188 }
10189 }
10190
10191 /* There's one corner case where the VEB might not have been
10192 * removed, so double check it here and remove it if needed.
10193 * This case happens if the veb was created from the debugfs
10194 * commands and no VSIs were added to it.
10195 */
10196 if (pf->veb[veb_idx])
10197 i40e_veb_release(pf->veb[veb_idx]);
10198}
10199
10200/**
10201 * i40e_veb_clear - remove veb struct
10202 * @veb: the veb to remove
10203 **/
10204static void i40e_veb_clear(struct i40e_veb *veb)
10205{
10206 if (!veb)
10207 return;
10208
10209 if (veb->pf) {
10210 struct i40e_pf *pf = veb->pf;
10211
10212 mutex_lock(&pf->switch_mutex);
10213 if (pf->veb[veb->idx] == veb)
10214 pf->veb[veb->idx] = NULL;
10215 mutex_unlock(&pf->switch_mutex);
10216 }
10217
10218 kfree(veb);
10219}
10220
10221/**
10222 * i40e_veb_release - Delete a VEB and free its resources
10223 * @veb: the VEB being removed
10224 **/
10225void i40e_veb_release(struct i40e_veb *veb)
10226{
10227 struct i40e_vsi *vsi = NULL;
10228 struct i40e_pf *pf;
10229 int i, n = 0;
10230
10231 pf = veb->pf;
10232
10233 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010234 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010235 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10236 n++;
10237 vsi = pf->vsi[i];
10238 }
10239 }
10240 if (n != 1) {
10241 dev_info(&pf->pdev->dev,
10242 "can't remove VEB %d with %d VSIs left\n",
10243 veb->seid, n);
10244 return;
10245 }
10246
10247 /* move the remaining VSI to uplink veb */
10248 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10249 if (veb->uplink_seid) {
10250 vsi->uplink_seid = veb->uplink_seid;
10251 if (veb->uplink_seid == pf->mac_seid)
10252 vsi->veb_idx = I40E_NO_VEB;
10253 else
10254 vsi->veb_idx = veb->veb_idx;
10255 } else {
10256 /* floating VEB */
10257 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10258 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10259 }
10260
10261 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10262 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010263}
10264
10265/**
10266 * i40e_add_veb - create the VEB in the switch
10267 * @veb: the VEB to be instantiated
10268 * @vsi: the controlling VSI
10269 **/
10270static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10271{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010272 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010273 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010274 int ret;
10275
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010276 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010277 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010278 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010279
10280 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010281 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010282 dev_info(&pf->pdev->dev,
10283 "couldn't add VEB, err %s aq_err %s\n",
10284 i40e_stat_str(&pf->hw, ret),
10285 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010286 return -EPERM;
10287 }
10288
10289 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010290 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010291 &veb->stats_idx, NULL, NULL, NULL);
10292 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010293 dev_info(&pf->pdev->dev,
10294 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10295 i40e_stat_str(&pf->hw, ret),
10296 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010297 return -EPERM;
10298 }
10299 ret = i40e_veb_get_bw_info(veb);
10300 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010301 dev_info(&pf->pdev->dev,
10302 "couldn't get VEB bw info, err %s aq_err %s\n",
10303 i40e_stat_str(&pf->hw, ret),
10304 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10305 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010306 return -ENOENT;
10307 }
10308
10309 vsi->uplink_seid = veb->seid;
10310 vsi->veb_idx = veb->idx;
10311 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10312
10313 return 0;
10314}
10315
10316/**
10317 * i40e_veb_setup - Set up a VEB
10318 * @pf: board private structure
10319 * @flags: VEB setup flags
10320 * @uplink_seid: the switch element to link to
10321 * @vsi_seid: the initial VSI seid
10322 * @enabled_tc: Enabled TC bit-map
10323 *
10324 * This allocates the sw VEB structure and links it into the switch
10325 * It is possible and legal for this to be a duplicate of an already
10326 * existing VEB. It is also possible for both uplink and vsi seids
10327 * to be zero, in order to create a floating VEB.
10328 *
10329 * Returns pointer to the successfully allocated VEB sw struct on
10330 * success, otherwise returns NULL on failure.
10331 **/
10332struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10333 u16 uplink_seid, u16 vsi_seid,
10334 u8 enabled_tc)
10335{
10336 struct i40e_veb *veb, *uplink_veb = NULL;
10337 int vsi_idx, veb_idx;
10338 int ret;
10339
10340 /* if one seid is 0, the other must be 0 to create a floating relay */
10341 if ((uplink_seid == 0 || vsi_seid == 0) &&
10342 (uplink_seid + vsi_seid != 0)) {
10343 dev_info(&pf->pdev->dev,
10344 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10345 uplink_seid, vsi_seid);
10346 return NULL;
10347 }
10348
10349 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010350 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010351 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10352 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010353 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010354 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10355 vsi_seid);
10356 return NULL;
10357 }
10358
10359 if (uplink_seid && uplink_seid != pf->mac_seid) {
10360 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10361 if (pf->veb[veb_idx] &&
10362 pf->veb[veb_idx]->seid == uplink_seid) {
10363 uplink_veb = pf->veb[veb_idx];
10364 break;
10365 }
10366 }
10367 if (!uplink_veb) {
10368 dev_info(&pf->pdev->dev,
10369 "uplink seid %d not found\n", uplink_seid);
10370 return NULL;
10371 }
10372 }
10373
10374 /* get veb sw struct */
10375 veb_idx = i40e_veb_mem_alloc(pf);
10376 if (veb_idx < 0)
10377 goto err_alloc;
10378 veb = pf->veb[veb_idx];
10379 veb->flags = flags;
10380 veb->uplink_seid = uplink_seid;
10381 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10382 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10383
10384 /* create the VEB in the switch */
10385 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10386 if (ret)
10387 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010388 if (vsi_idx == pf->lan_vsi)
10389 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010390
10391 return veb;
10392
10393err_veb:
10394 i40e_veb_clear(veb);
10395err_alloc:
10396 return NULL;
10397}
10398
10399/**
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010400 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010401 * @pf: board private structure
10402 * @ele: element we are building info from
10403 * @num_reported: total number of elements
10404 * @printconfig: should we print the contents
10405 *
10406 * helper function to assist in extracting a few useful SEID values.
10407 **/
10408static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10409 struct i40e_aqc_switch_config_element_resp *ele,
10410 u16 num_reported, bool printconfig)
10411{
10412 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10413 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10414 u8 element_type = ele->element_type;
10415 u16 seid = le16_to_cpu(ele->seid);
10416
10417 if (printconfig)
10418 dev_info(&pf->pdev->dev,
10419 "type=%d seid=%d uplink=%d downlink=%d\n",
10420 element_type, seid, uplink_seid, downlink_seid);
10421
10422 switch (element_type) {
10423 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10424 pf->mac_seid = seid;
10425 break;
10426 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10427 /* Main VEB? */
10428 if (uplink_seid != pf->mac_seid)
10429 break;
10430 if (pf->lan_veb == I40E_NO_VEB) {
10431 int v;
10432
10433 /* find existing or else empty VEB */
10434 for (v = 0; v < I40E_MAX_VEB; v++) {
10435 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10436 pf->lan_veb = v;
10437 break;
10438 }
10439 }
10440 if (pf->lan_veb == I40E_NO_VEB) {
10441 v = i40e_veb_mem_alloc(pf);
10442 if (v < 0)
10443 break;
10444 pf->lan_veb = v;
10445 }
10446 }
10447
10448 pf->veb[pf->lan_veb]->seid = seid;
10449 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10450 pf->veb[pf->lan_veb]->pf = pf;
10451 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10452 break;
10453 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10454 if (num_reported != 1)
10455 break;
10456 /* This is immediately after a reset so we can assume this is
10457 * the PF's VSI
10458 */
10459 pf->mac_seid = uplink_seid;
10460 pf->pf_seid = downlink_seid;
10461 pf->main_vsi_seid = seid;
10462 if (printconfig)
10463 dev_info(&pf->pdev->dev,
10464 "pf_seid=%d main_vsi_seid=%d\n",
10465 pf->pf_seid, pf->main_vsi_seid);
10466 break;
10467 case I40E_SWITCH_ELEMENT_TYPE_PF:
10468 case I40E_SWITCH_ELEMENT_TYPE_VF:
10469 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10470 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10471 case I40E_SWITCH_ELEMENT_TYPE_PE:
10472 case I40E_SWITCH_ELEMENT_TYPE_PA:
10473 /* ignore these for now */
10474 break;
10475 default:
10476 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10477 element_type, seid);
10478 break;
10479 }
10480}
10481
10482/**
10483 * i40e_fetch_switch_configuration - Get switch config from firmware
10484 * @pf: board private structure
10485 * @printconfig: should we print the contents
10486 *
10487 * Get the current switch configuration from the device and
10488 * extract a few useful SEID values.
10489 **/
10490int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10491{
10492 struct i40e_aqc_get_switch_config_resp *sw_config;
10493 u16 next_seid = 0;
10494 int ret = 0;
10495 u8 *aq_buf;
10496 int i;
10497
10498 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10499 if (!aq_buf)
10500 return -ENOMEM;
10501
10502 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10503 do {
10504 u16 num_reported, num_total;
10505
10506 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10507 I40E_AQ_LARGE_BUF,
10508 &next_seid, NULL);
10509 if (ret) {
10510 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010511 "get switch config failed err %s aq_err %s\n",
10512 i40e_stat_str(&pf->hw, ret),
10513 i40e_aq_str(&pf->hw,
10514 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010515 kfree(aq_buf);
10516 return -ENOENT;
10517 }
10518
10519 num_reported = le16_to_cpu(sw_config->header.num_reported);
10520 num_total = le16_to_cpu(sw_config->header.num_total);
10521
10522 if (printconfig)
10523 dev_info(&pf->pdev->dev,
10524 "header: %d reported %d total\n",
10525 num_reported, num_total);
10526
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010527 for (i = 0; i < num_reported; i++) {
10528 struct i40e_aqc_switch_config_element_resp *ele =
10529 &sw_config->element[i];
10530
10531 i40e_setup_pf_switch_element(pf, ele, num_reported,
10532 printconfig);
10533 }
10534 } while (next_seid != 0);
10535
10536 kfree(aq_buf);
10537 return ret;
10538}
10539
10540/**
10541 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10542 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010543 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010544 *
10545 * Returns 0 on success, negative value on failure
10546 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010547static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010548{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010549 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010550 int ret;
10551
10552 /* find out what's out there already */
10553 ret = i40e_fetch_switch_configuration(pf, false);
10554 if (ret) {
10555 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010556 "couldn't fetch switch config, err %s aq_err %s\n",
10557 i40e_stat_str(&pf->hw, ret),
10558 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010559 return ret;
10560 }
10561 i40e_pf_reset_stats(pf);
10562
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010563 /* set the switch config bit for the whole device to
10564 * support limited promisc or true promisc
10565 * when user requests promisc. The default is limited
10566 * promisc.
10567 */
10568
10569 if ((pf->hw.pf_id == 0) &&
10570 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10571 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10572
10573 if (pf->hw.pf_id == 0) {
10574 u16 valid_flags;
10575
10576 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10577 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10578 NULL);
10579 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10580 dev_info(&pf->pdev->dev,
10581 "couldn't set switch config bits, err %s aq_err %s\n",
10582 i40e_stat_str(&pf->hw, ret),
10583 i40e_aq_str(&pf->hw,
10584 pf->hw.aq.asq_last_status));
10585 /* not a fatal problem, just keep going */
10586 }
10587 }
10588
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010589 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010590 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010591 struct i40e_vsi *vsi = NULL;
10592 u16 uplink_seid;
10593
10594 /* Set up the PF VSI associated with the PF's main VSI
10595 * that is already in the HW switch
10596 */
10597 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10598 uplink_seid = pf->veb[pf->lan_veb]->seid;
10599 else
10600 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010601 if (pf->lan_vsi == I40E_NO_VSI)
10602 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10603 else if (reinit)
10604 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010605 if (!vsi) {
10606 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10607 i40e_fdir_teardown(pf);
10608 return -EAGAIN;
10609 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010610 } else {
10611 /* force a reset of TC and queue layout configurations */
10612 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010613
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010614 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10615 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10616 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10617 }
10618 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10619
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010620 i40e_fdir_sb_setup(pf);
10621
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010622 /* Setup static PF queue filter control settings */
10623 ret = i40e_setup_pf_filter_control(pf);
10624 if (ret) {
10625 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10626 ret);
10627 /* Failure here should not stop continuing other steps */
10628 }
10629
10630 /* enable RSS in the HW, even for only one queue, as the stack can use
10631 * the hash
10632 */
10633 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010634 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010635
10636 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010637 i40e_link_event(pf);
10638
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010639 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010640 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10641 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010642
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010643 i40e_ptp_init(pf);
10644
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010645 return ret;
10646}
10647
10648/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010649 * i40e_determine_queue_usage - Work out queue distribution
10650 * @pf: board private structure
10651 **/
10652static void i40e_determine_queue_usage(struct i40e_pf *pf)
10653{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010654 int queues_left;
10655
10656 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010657
10658 /* Find the max queues to be put into basic use. We'll always be
10659 * using TC0, whether or not DCB is running, and TC0 will get the
10660 * big RSS set.
10661 */
10662 queues_left = pf->hw.func_caps.num_tx_qp;
10663
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010664 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010665 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010666 /* one qp for PF, no queues for anything else */
10667 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010668 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010669
10670 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010671 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010672 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010673 I40E_FLAG_FD_SB_ENABLED |
10674 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010675 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010676 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010677 I40E_FLAG_SRIOV_ENABLED |
10678 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010679 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10680 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010681 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010682 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010683 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010684 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010685 queues_left -= pf->num_lan_qps;
10686
10687 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010688 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000010689 I40E_FLAG_FD_SB_ENABLED |
10690 I40E_FLAG_FD_ATR_ENABLED |
10691 I40E_FLAG_DCB_ENABLED |
10692 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010693 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010694 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010695 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010696 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010697 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10698 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010699 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10700 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010701 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10702 num_online_cpus());
10703 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10704 pf->hw.func_caps.num_tx_qp);
10705
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010706 queues_left -= pf->num_lan_qps;
10707 }
10708
10709 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10710 if (queues_left > 1) {
10711 queues_left -= 1; /* save 1 queue for FD */
10712 } else {
10713 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10714 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10715 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010716 }
10717
10718 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10719 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010720 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10721 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010722 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10723 }
10724
10725 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10726 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10727 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10728 (queues_left / pf->num_vmdq_qps));
10729 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10730 }
10731
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010732 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010733 dev_dbg(&pf->pdev->dev,
10734 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10735 pf->hw.func_caps.num_tx_qp,
10736 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010737 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10738 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10739 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010740}
10741
10742/**
10743 * i40e_setup_pf_filter_control - Setup PF static filter control
10744 * @pf: PF to be setup
10745 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010746 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010747 * settings. If PE/FCoE are enabled then it will also set the per PF
10748 * based filter sizes required for them. It also enables Flow director,
10749 * ethertype and macvlan type filter settings for the pf.
10750 *
10751 * Returns 0 on success, negative on failure
10752 **/
10753static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10754{
10755 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10756
10757 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10758
10759 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010760 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010761 settings->enable_fdir = true;
10762
10763 /* Ethtype and MACVLAN filters enabled for PF */
10764 settings->enable_ethtype = true;
10765 settings->enable_macvlan = true;
10766
10767 if (i40e_set_filter_control(&pf->hw, settings))
10768 return -ENOENT;
10769
10770 return 0;
10771}
10772
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010773#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010774#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010775static void i40e_print_features(struct i40e_pf *pf)
10776{
10777 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010778 char *buf;
10779 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010780
Joe Perches3b195842015-12-03 04:20:57 -080010781 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10782 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010783 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010784
Joe Perches3b195842015-12-03 04:20:57 -080010785 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010786#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010787 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010788#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010789 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010790 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010791 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010792 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010793 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010794 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010795 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010796 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010797 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10798 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010799 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010800 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010801 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010802 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010803 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010804 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010805 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010806 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010807 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010808 else
Joe Perches3b195842015-12-03 04:20:57 -080010809 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010810
Joe Perches3b195842015-12-03 04:20:57 -080010811 dev_info(&pf->pdev->dev, "%s\n", buf);
10812 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010813 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010814}
10815
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010816/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010817 * i40e_get_platform_mac_addr - get platform-specific MAC address
10818 *
10819 * @pdev: PCI device information struct
10820 * @pf: board private structure
10821 *
10822 * Look up the MAC address in Open Firmware on systems that support it,
10823 * and use IDPROM on SPARC if no OF address is found. On return, the
10824 * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
10825 * has been selected.
10826 **/
10827static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10828{
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010829 pf->flags &= ~I40E_FLAG_PF_MAC;
Sowmini Varadhanba942722016-01-12 19:32:31 -080010830 if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010831 pf->flags |= I40E_FLAG_PF_MAC;
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010832}
10833
10834/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010835 * i40e_probe - Device initialization routine
10836 * @pdev: PCI device information struct
10837 * @ent: entry in i40e_pci_tbl
10838 *
Jeff Kirsherb40c82e62015-02-27 09:18:34 +000010839 * i40e_probe initializes a PF identified by a pci_dev structure.
10840 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010841 * and a hardware reset occur.
10842 *
10843 * Returns 0 on success, negative on failure
10844 **/
10845static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10846{
Catherine Sullivane8278452015-02-06 08:52:08 +000010847 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010848 struct i40e_pf *pf;
10849 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010850 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010851 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010852 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010853 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010854 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010855 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010856 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010857
10858 err = pci_enable_device_mem(pdev);
10859 if (err)
10860 return err;
10861
10862 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010863 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010864 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010865 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10866 if (err) {
10867 dev_err(&pdev->dev,
10868 "DMA configuration failed: 0x%x\n", err);
10869 goto err_dma;
10870 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010871 }
10872
10873 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010874 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010875 if (err) {
10876 dev_info(&pdev->dev,
10877 "pci_request_selected_regions failed %d\n", err);
10878 goto err_pci_reg;
10879 }
10880
10881 pci_enable_pcie_error_reporting(pdev);
10882 pci_set_master(pdev);
10883
10884 /* Now that we have a PCI connection, we need to do the
10885 * low level device setup. This is primarily setting up
10886 * the Admin Queue structures and then querying for the
10887 * device's current profile information.
10888 */
10889 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10890 if (!pf) {
10891 err = -ENOMEM;
10892 goto err_pf_alloc;
10893 }
10894 pf->next_vsi = 0;
10895 pf->pdev = pdev;
10896 set_bit(__I40E_DOWN, &pf->state);
10897
10898 hw = &pf->hw;
10899 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010900
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010901 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10902 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010903
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010904 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010905 if (!hw->hw_addr) {
10906 err = -EIO;
10907 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10908 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010909 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010910 goto err_ioremap;
10911 }
10912 hw->vendor_id = pdev->vendor;
10913 hw->device_id = pdev->device;
10914 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10915 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10916 hw->subsystem_device_id = pdev->subsystem_device;
10917 hw->bus.device = PCI_SLOT(pdev->devfn);
10918 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080010919 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010920 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010921
Jacob Keller0e588de2017-02-06 14:38:50 -080010922 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
10923 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
10924
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010925 /* set up the locks for the AQ, do this only once in probe
10926 * and destroy them only once in remove
10927 */
10928 mutex_init(&hw->aq.asq_mutex);
10929 mutex_init(&hw->aq.arq_mutex);
10930
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010931 pf->msg_enable = netif_msg_init(debug,
10932 NETIF_MSG_DRV |
10933 NETIF_MSG_PROBE |
10934 NETIF_MSG_LINK);
10935 if (debug < -1)
10936 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010937
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010938 /* do a special CORER for clearing PXE mode once at init */
10939 if (hw->revision_id == 0 &&
10940 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10941 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10942 i40e_flush(hw);
10943 msleep(200);
10944 pf->corer_count++;
10945
10946 i40e_clear_pxe_mode(hw);
10947 }
10948
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010949 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010950 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010951 err = i40e_pf_reset(hw);
10952 if (err) {
10953 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10954 goto err_pf_reset;
10955 }
10956 pf->pfr_count++;
10957
10958 hw->aq.num_arq_entries = I40E_AQ_LEN;
10959 hw->aq.num_asq_entries = I40E_AQ_LEN;
10960 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10961 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10962 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010963
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010964 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010965 "%s-%s:misc",
10966 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010967
10968 err = i40e_init_shared_code(hw);
10969 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010970 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10971 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010972 goto err_pf_reset;
10973 }
10974
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010975 /* set up a default setting for link flow control */
10976 pf->hw.fc.requested_mode = I40E_FC_NONE;
10977
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010978 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010979 if (err) {
10980 if (err == I40E_ERR_FIRMWARE_API_VERSION)
10981 dev_info(&pdev->dev,
10982 "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");
10983 else
10984 dev_info(&pdev->dev,
10985 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
10986
10987 goto err_pf_reset;
10988 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010989
Shannon Nelson6dec1012015-09-28 14:12:30 -040010990 /* provide nvm, fw, api versions */
10991 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
10992 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
10993 hw->aq.api_maj_ver, hw->aq.api_min_ver,
10994 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040010995
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010996 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
10997 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000010998 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000010999 "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");
11000 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11001 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011002 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011003 "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 +000011004
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011005 i40e_verify_eeprom(pf);
11006
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011007 /* Rev 0 hardware was never productized */
11008 if (hw->revision_id < 1)
11009 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");
11010
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011011 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011012 err = i40e_get_capabilities(pf);
11013 if (err)
11014 goto err_adminq_setup;
11015
11016 err = i40e_sw_init(pf);
11017 if (err) {
11018 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11019 goto err_sw_init;
11020 }
11021
11022 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011023 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011024 if (err) {
11025 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11026 goto err_init_lan_hmc;
11027 }
11028
11029 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11030 if (err) {
11031 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11032 err = -ENOENT;
11033 goto err_configure_lan_hmc;
11034 }
11035
Neerav Parikhb686ece2014-12-14 01:55:11 +000011036 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11037 * Ignore error return codes because if it was already disabled via
11038 * hardware settings this will fail
11039 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011040 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011041 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11042 i40e_aq_stop_lldp(hw, true, NULL);
11043 }
11044
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011045 i40e_get_mac_addr(hw, hw->mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011046 /* allow a platform config to override the HW addr */
11047 i40e_get_platform_mac_addr(pdev, pf);
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011048 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011049 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11050 err = -EIO;
11051 goto err_mac_addr;
11052 }
11053 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011054 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011055 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11056 if (is_valid_ether_addr(hw->mac.port_addr))
11057 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011058
11059 pci_set_drvdata(pdev, pf);
11060 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011061#ifdef CONFIG_I40E_DCB
11062 err = i40e_init_pf_dcb(pf);
11063 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011064 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011065 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011066 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011067 }
11068#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011069
11070 /* set up periodic task facility */
11071 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11072 pf->service_timer_period = HZ;
11073
11074 INIT_WORK(&pf->service_task, i40e_service_task);
11075 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
11076 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011077
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011078 /* NVM bit on means WoL disabled for the port */
11079 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011080 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011081 pf->wol_en = false;
11082 else
11083 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011084 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11085
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011086 /* set up the main switch operations */
11087 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011088 err = i40e_init_interrupt_scheme(pf);
11089 if (err)
11090 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011091
Mitch Williams505682c2014-05-20 08:01:37 +000011092 /* The number of VSIs reported by the FW is the minimum guaranteed
11093 * to us; HW supports far more and we share the remaining pool with
11094 * the other PFs. We allocate space for more than the guarantee with
11095 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011096 */
Mitch Williams505682c2014-05-20 08:01:37 +000011097 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11098 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11099 else
11100 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11101
11102 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011103 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11104 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011105 if (!pf->vsi) {
11106 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011107 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011108 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011109
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011110#ifdef CONFIG_PCI_IOV
11111 /* prep for VF support */
11112 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11113 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11114 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11115 if (pci_num_vf(pdev))
11116 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11117 }
11118#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011119 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011120 if (err) {
11121 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11122 goto err_vsis;
11123 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011124
11125 /* Make sure flow control is set according to current settings */
11126 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11127 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11128 dev_dbg(&pf->pdev->dev,
11129 "Set fc with err %s aq_err %s on get_phy_cap\n",
11130 i40e_stat_str(hw, err),
11131 i40e_aq_str(hw, hw->aq.asq_last_status));
11132 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11133 dev_dbg(&pf->pdev->dev,
11134 "Set fc with err %s aq_err %s on set_phy_config\n",
11135 i40e_stat_str(hw, err),
11136 i40e_aq_str(hw, hw->aq.asq_last_status));
11137 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11138 dev_dbg(&pf->pdev->dev,
11139 "Set fc with err %s aq_err %s on get_link_info\n",
11140 i40e_stat_str(hw, err),
11141 i40e_aq_str(hw, hw->aq.asq_last_status));
11142
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011143 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011144 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011145 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11146 i40e_vsi_open(pf->vsi[i]);
11147 break;
11148 }
11149 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011150
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011151 /* The driver only wants link up/down and module qualification
11152 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011153 */
11154 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011155 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011156 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011157 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011158 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011159 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11160 i40e_stat_str(&pf->hw, err),
11161 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011162
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011163 /* Reconfigure hardware for allowing smaller MSS in the case
11164 * of TSO, so that we avoid the MDD being fired and causing
11165 * a reset in the case of small MSS+TSO.
11166 */
11167 val = rd32(hw, I40E_REG_MSS);
11168 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11169 val &= ~I40E_REG_MSS_MIN_MASK;
11170 val |= I40E_64BYTE_MSS;
11171 wr32(hw, I40E_REG_MSS, val);
11172 }
11173
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011174 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011175 msleep(75);
11176 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11177 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011178 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11179 i40e_stat_str(&pf->hw, err),
11180 i40e_aq_str(&pf->hw,
11181 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011182 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011183 /* The main driver is (mostly) up and happy. We need to set this state
11184 * before setting up the misc vector or we get a race and the vector
11185 * ends up disabled forever.
11186 */
11187 clear_bit(__I40E_DOWN, &pf->state);
11188
11189 /* In case of MSIX we are going to setup the misc vector right here
11190 * to handle admin queue events etc. In case of legacy and MSI
11191 * the misc functionality and queue processing is combined in
11192 * the same vector and that gets setup at open.
11193 */
11194 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11195 err = i40e_setup_misc_vector(pf);
11196 if (err) {
11197 dev_info(&pdev->dev,
11198 "setup of misc vector failed: %d\n", err);
11199 goto err_vsis;
11200 }
11201 }
11202
Greg Rosedf805f62014-04-04 04:43:16 +000011203#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011204 /* prep for VF support */
11205 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011206 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11207 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011208 /* disable link interrupts for VFs */
11209 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11210 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11211 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11212 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011213
11214 if (pci_num_vf(pdev)) {
11215 dev_info(&pdev->dev,
11216 "Active VFs found, allocating resources.\n");
11217 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11218 if (err)
11219 dev_info(&pdev->dev,
11220 "Error %d allocating resources for existing VFs\n",
11221 err);
11222 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011223 }
Greg Rosedf805f62014-04-04 04:43:16 +000011224#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011225
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011226 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11227 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11228 pf->num_iwarp_msix,
11229 I40E_IWARP_IRQ_PILE_ID);
11230 if (pf->iwarp_base_vector < 0) {
11231 dev_info(&pdev->dev,
11232 "failed to get tracking for %d vectors for IWARP err=%d\n",
11233 pf->num_iwarp_msix, pf->iwarp_base_vector);
11234 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11235 }
11236 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011237
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011238 i40e_dbg_pf_init(pf);
11239
11240 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011241 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011242
11243 /* since everything's happy, start the service_task timer */
11244 mod_timer(&pf->service_timer,
11245 round_jiffies(jiffies + pf->service_timer_period));
11246
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011247 /* add this PF to client device list and launch a client service task */
11248 err = i40e_lan_add_device(pf);
11249 if (err)
11250 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11251 err);
11252
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011253#define PCI_SPEED_SIZE 8
11254#define PCI_WIDTH_SIZE 8
11255 /* Devices on the IOSF bus do not have this information
11256 * and will report PCI Gen 1 x 1 by default so don't bother
11257 * checking them.
11258 */
11259 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11260 char speed[PCI_SPEED_SIZE] = "Unknown";
11261 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011262
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011263 /* Get the negotiated link width and speed from PCI config
11264 * space
11265 */
11266 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11267 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011268
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011269 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011270
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011271 switch (hw->bus.speed) {
11272 case i40e_bus_speed_8000:
11273 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11274 case i40e_bus_speed_5000:
11275 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11276 case i40e_bus_speed_2500:
11277 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11278 default:
11279 break;
11280 }
11281 switch (hw->bus.width) {
11282 case i40e_bus_width_pcie_x8:
11283 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11284 case i40e_bus_width_pcie_x4:
11285 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11286 case i40e_bus_width_pcie_x2:
11287 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11288 case i40e_bus_width_pcie_x1:
11289 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11290 default:
11291 break;
11292 }
11293
11294 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11295 speed, width);
11296
11297 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11298 hw->bus.speed < i40e_bus_speed_8000) {
11299 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11300 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11301 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011302 }
11303
Catherine Sullivane8278452015-02-06 08:52:08 +000011304 /* get the requested speeds from the fw */
11305 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11306 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011307 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11308 i40e_stat_str(&pf->hw, err),
11309 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011310 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11311
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011312 /* get the supported phy types from the fw */
11313 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11314 if (err)
11315 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11316 i40e_stat_str(&pf->hw, err),
11317 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011318
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011319 /* Add a filter to drop all Flow control frames from any VSI from being
11320 * transmitted. By doing so we stop a malicious VF from sending out
11321 * PAUSE or PFC frames and potentially controlling traffic for other
11322 * PF/VF VSIs.
11323 * The FW can still send Flow control frames if enabled.
11324 */
11325 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11326 pf->main_vsi_seid);
11327
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011328 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011329 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11330 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011331 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11332 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011333 /* print a string summarizing features */
11334 i40e_print_features(pf);
11335
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011336 return 0;
11337
11338 /* Unwind what we've done if something failed in the setup */
11339err_vsis:
11340 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011341 i40e_clear_interrupt_scheme(pf);
11342 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011343err_switch_setup:
11344 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011345 del_timer_sync(&pf->service_timer);
11346err_mac_addr:
11347err_configure_lan_hmc:
11348 (void)i40e_shutdown_lan_hmc(hw);
11349err_init_lan_hmc:
11350 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011351err_sw_init:
11352err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011353err_pf_reset:
11354 iounmap(hw->hw_addr);
11355err_ioremap:
11356 kfree(pf);
11357err_pf_alloc:
11358 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011359 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011360err_pci_reg:
11361err_dma:
11362 pci_disable_device(pdev);
11363 return err;
11364}
11365
11366/**
11367 * i40e_remove - Device removal routine
11368 * @pdev: PCI device information struct
11369 *
11370 * i40e_remove is called by the PCI subsystem to alert the driver
11371 * that is should release a PCI device. This could be caused by a
11372 * Hot-Plug event, or because the driver is going to be removed from
11373 * memory.
11374 **/
11375static void i40e_remove(struct pci_dev *pdev)
11376{
11377 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011378 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011379 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011380 int i;
11381
11382 i40e_dbg_pf_exit(pf);
11383
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011384 i40e_ptp_stop(pf);
11385
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011386 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011387 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11388 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011389
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011390 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011391 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011392 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011393 if (pf->service_timer.data)
11394 del_timer_sync(&pf->service_timer);
11395 if (pf->service_task.func)
11396 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011397
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011398 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11399 i40e_free_vfs(pf);
11400 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11401 }
11402
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011403 i40e_fdir_teardown(pf);
11404
11405 /* If there is a switch structure or any orphans, remove them.
11406 * This will leave only the PF's VSI remaining.
11407 */
11408 for (i = 0; i < I40E_MAX_VEB; i++) {
11409 if (!pf->veb[i])
11410 continue;
11411
11412 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11413 pf->veb[i]->uplink_seid == 0)
11414 i40e_switch_branch_release(pf->veb[i]);
11415 }
11416
11417 /* Now we can shutdown the PF's VSI, just before we kill
11418 * adminq and hmc.
11419 */
11420 if (pf->vsi[pf->lan_vsi])
11421 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11422
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011423 /* remove attached clients */
11424 ret_code = i40e_lan_del_device(pf);
11425 if (ret_code) {
11426 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11427 ret_code);
11428 }
11429
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011430 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011431 if (hw->hmc.hmc_obj) {
11432 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011433 if (ret_code)
11434 dev_warn(&pdev->dev,
11435 "Failed to destroy the HMC resources: %d\n",
11436 ret_code);
11437 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011438
11439 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011440 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011441
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011442 /* destroy the locks only once, here */
11443 mutex_destroy(&hw->aq.arq_mutex);
11444 mutex_destroy(&hw->aq.asq_mutex);
11445
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011446 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11447 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011448 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011449 if (pf->vsi[i]) {
11450 i40e_vsi_clear_rings(pf->vsi[i]);
11451 i40e_vsi_clear(pf->vsi[i]);
11452 pf->vsi[i] = NULL;
11453 }
11454 }
11455
11456 for (i = 0; i < I40E_MAX_VEB; i++) {
11457 kfree(pf->veb[i]);
11458 pf->veb[i] = NULL;
11459 }
11460
11461 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011462 kfree(pf->vsi);
11463
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011464 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011465 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011466 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011467
11468 pci_disable_pcie_error_reporting(pdev);
11469 pci_disable_device(pdev);
11470}
11471
11472/**
11473 * i40e_pci_error_detected - warning that something funky happened in PCI land
11474 * @pdev: PCI device information struct
11475 *
11476 * Called to warn that something happened and the error handling steps
11477 * are in progress. Allows the driver to quiesce things, be ready for
11478 * remediation.
11479 **/
11480static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11481 enum pci_channel_state error)
11482{
11483 struct i40e_pf *pf = pci_get_drvdata(pdev);
11484
11485 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11486
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011487 if (!pf) {
11488 dev_info(&pdev->dev,
11489 "Cannot recover - error happened during device probe\n");
11490 return PCI_ERS_RESULT_DISCONNECT;
11491 }
11492
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011493 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011494 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11495 rtnl_lock();
11496 i40e_prep_for_reset(pf);
11497 rtnl_unlock();
11498 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011499
11500 /* Request a slot reset */
11501 return PCI_ERS_RESULT_NEED_RESET;
11502}
11503
11504/**
11505 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11506 * @pdev: PCI device information struct
11507 *
11508 * Called to find if the driver can work with the device now that
11509 * the pci slot has been reset. If a basic connection seems good
11510 * (registers are readable and have sane content) then return a
11511 * happy little PCI_ERS_RESULT_xxx.
11512 **/
11513static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11514{
11515 struct i40e_pf *pf = pci_get_drvdata(pdev);
11516 pci_ers_result_t result;
11517 int err;
11518 u32 reg;
11519
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011520 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011521 if (pci_enable_device_mem(pdev)) {
11522 dev_info(&pdev->dev,
11523 "Cannot re-enable PCI device after reset.\n");
11524 result = PCI_ERS_RESULT_DISCONNECT;
11525 } else {
11526 pci_set_master(pdev);
11527 pci_restore_state(pdev);
11528 pci_save_state(pdev);
11529 pci_wake_from_d3(pdev, false);
11530
11531 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11532 if (reg == 0)
11533 result = PCI_ERS_RESULT_RECOVERED;
11534 else
11535 result = PCI_ERS_RESULT_DISCONNECT;
11536 }
11537
11538 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11539 if (err) {
11540 dev_info(&pdev->dev,
11541 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11542 err);
11543 /* non-fatal, continue */
11544 }
11545
11546 return result;
11547}
11548
11549/**
11550 * i40e_pci_error_resume - restart operations after PCI error recovery
11551 * @pdev: PCI device information struct
11552 *
11553 * Called to allow the driver to bring things back up after PCI error
11554 * and/or reset recovery has finished.
11555 **/
11556static void i40e_pci_error_resume(struct pci_dev *pdev)
11557{
11558 struct i40e_pf *pf = pci_get_drvdata(pdev);
11559
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011560 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011561 if (test_bit(__I40E_SUSPENDED, &pf->state))
11562 return;
11563
11564 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011565 i40e_handle_reset_warning(pf);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011566 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011567}
11568
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011569/**
Joshua Hay1d680052016-12-12 15:44:08 -080011570 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11571 * using the mac_address_write admin q function
11572 * @pf: pointer to i40e_pf struct
11573 **/
11574static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11575{
11576 struct i40e_hw *hw = &pf->hw;
11577 i40e_status ret;
11578 u8 mac_addr[6];
11579 u16 flags = 0;
11580
11581 /* Get current MAC address in case it's an LAA */
11582 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11583 ether_addr_copy(mac_addr,
11584 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11585 } else {
11586 dev_err(&pf->pdev->dev,
11587 "Failed to retrieve MAC address; using default\n");
11588 ether_addr_copy(mac_addr, hw->mac.addr);
11589 }
11590
11591 /* The FW expects the mac address write cmd to first be called with
11592 * one of these flags before calling it again with the multicast
11593 * enable flags.
11594 */
11595 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11596
11597 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11598 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11599
11600 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11601 if (ret) {
11602 dev_err(&pf->pdev->dev,
11603 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11604 return;
11605 }
11606
11607 flags = I40E_AQC_MC_MAG_EN
11608 | I40E_AQC_WOL_PRESERVE_ON_PFR
11609 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11610 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11611 if (ret)
11612 dev_err(&pf->pdev->dev,
11613 "Failed to enable Multicast Magic Packet wake up\n");
11614}
11615
11616/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011617 * i40e_shutdown - PCI callback for shutting down
11618 * @pdev: PCI device information struct
11619 **/
11620static void i40e_shutdown(struct pci_dev *pdev)
11621{
11622 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011623 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011624
11625 set_bit(__I40E_SUSPENDED, &pf->state);
11626 set_bit(__I40E_DOWN, &pf->state);
11627 rtnl_lock();
11628 i40e_prep_for_reset(pf);
11629 rtnl_unlock();
11630
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011631 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11632 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11633
Catherine Sullivan02b42492015-07-10 19:35:59 -040011634 del_timer_sync(&pf->service_timer);
11635 cancel_work_sync(&pf->service_task);
11636 i40e_fdir_teardown(pf);
11637
Joshua Hay1d680052016-12-12 15:44:08 -080011638 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11639 i40e_enable_mc_magic_wake(pf);
11640
Catherine Sullivan02b42492015-07-10 19:35:59 -040011641 rtnl_lock();
11642 i40e_prep_for_reset(pf);
11643 rtnl_unlock();
11644
11645 wr32(hw, I40E_PFPM_APM,
11646 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11647 wr32(hw, I40E_PFPM_WUFC,
11648 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11649
Shannon Nelsone1477582015-02-21 06:44:33 +000011650 i40e_clear_interrupt_scheme(pf);
11651
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011652 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011653 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011654 pci_set_power_state(pdev, PCI_D3hot);
11655 }
11656}
11657
11658#ifdef CONFIG_PM
11659/**
11660 * i40e_suspend - PCI callback for moving to D3
11661 * @pdev: PCI device information struct
11662 **/
11663static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11664{
11665 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011666 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011667 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011668
11669 set_bit(__I40E_SUSPENDED, &pf->state);
11670 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011671
Joshua Hay1d680052016-12-12 15:44:08 -080011672 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11673 i40e_enable_mc_magic_wake(pf);
11674
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011675 rtnl_lock();
11676 i40e_prep_for_reset(pf);
11677 rtnl_unlock();
11678
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011679 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11680 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11681
Greg Roseb33d3b72016-05-16 10:26:37 -070011682 i40e_stop_misc_vector(pf);
11683
Greg Rose059ff692016-05-16 10:26:38 -070011684 retval = pci_save_state(pdev);
11685 if (retval)
11686 return retval;
11687
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011688 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011689 pci_set_power_state(pdev, PCI_D3hot);
11690
Greg Rose059ff692016-05-16 10:26:38 -070011691 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011692}
11693
11694/**
11695 * i40e_resume - PCI callback for waking up from D3
11696 * @pdev: PCI device information struct
11697 **/
11698static int i40e_resume(struct pci_dev *pdev)
11699{
11700 struct i40e_pf *pf = pci_get_drvdata(pdev);
11701 u32 err;
11702
11703 pci_set_power_state(pdev, PCI_D0);
11704 pci_restore_state(pdev);
11705 /* pci_restore_state() clears dev->state_saves, so
11706 * call pci_save_state() again to restore it.
11707 */
11708 pci_save_state(pdev);
11709
11710 err = pci_enable_device_mem(pdev);
11711 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011712 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011713 return err;
11714 }
11715 pci_set_master(pdev);
11716
11717 /* no wakeup events while running */
11718 pci_wake_from_d3(pdev, false);
11719
11720 /* handling the reset will rebuild the device state */
11721 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11722 clear_bit(__I40E_DOWN, &pf->state);
11723 rtnl_lock();
11724 i40e_reset_and_rebuild(pf, false);
11725 rtnl_unlock();
11726 }
11727
11728 return 0;
11729}
11730
11731#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011732static const struct pci_error_handlers i40e_err_handler = {
11733 .error_detected = i40e_pci_error_detected,
11734 .slot_reset = i40e_pci_error_slot_reset,
11735 .resume = i40e_pci_error_resume,
11736};
11737
11738static struct pci_driver i40e_driver = {
11739 .name = i40e_driver_name,
11740 .id_table = i40e_pci_tbl,
11741 .probe = i40e_probe,
11742 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011743#ifdef CONFIG_PM
11744 .suspend = i40e_suspend,
11745 .resume = i40e_resume,
11746#endif
11747 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011748 .err_handler = &i40e_err_handler,
11749 .sriov_configure = i40e_pci_sriov_configure,
11750};
11751
11752/**
11753 * i40e_init_module - Driver registration routine
11754 *
11755 * i40e_init_module is the first routine called when the driver is
11756 * loaded. All it does is register with the PCI subsystem.
11757 **/
11758static int __init i40e_init_module(void)
11759{
11760 pr_info("%s: %s - version %s\n", i40e_driver_name,
11761 i40e_driver_string, i40e_driver_version_str);
11762 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011763
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011764 /* we will see if single thread per module is enough for now,
11765 * it can't be any worse than using the system workqueue which
11766 * was already single threaded
11767 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011768 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11769 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011770 if (!i40e_wq) {
11771 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11772 return -ENOMEM;
11773 }
11774
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011775 i40e_dbg_init();
11776 return pci_register_driver(&i40e_driver);
11777}
11778module_init(i40e_init_module);
11779
11780/**
11781 * i40e_exit_module - Driver exit cleanup routine
11782 *
11783 * i40e_exit_module is called just before the driver is removed
11784 * from memory.
11785 **/
11786static void __exit i40e_exit_module(void)
11787{
11788 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011789 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011790 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011791}
11792module_exit(i40e_exit_module);