blob: 97489d69029a805118980d0611167a837373f413 [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);
Maciej Sosin373149f2017-04-05 07:50:55 -040053static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000054static 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);
Maciej Sosin373149f2017-04-05 07:50:55 -040060static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
61static int i40e_reset(struct i40e_pf *pf);
62static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080063static void i40e_fdir_sb_setup(struct i40e_pf *pf);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080064static int i40e_veb_get_bw_info(struct i40e_veb *veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000065
66/* i40e_pci_tbl - PCI Device ID Table
67 *
68 * Last entry must be all 0s
69 *
70 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
71 * Class, Class Mask, private data (not used) }
72 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020073static const struct pci_device_id i40e_pci_tbl[] = {
Shannon Nelsonab600852014-01-17 15:36:39 -080074 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080075 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080076 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
77 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
Shannon Nelsonab600852014-01-17 15:36:39 -080078 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
79 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
80 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
Mitch Williams5960d332014-09-13 07:40:47 +000081 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
Shannon Nelsonbc5166b92015-08-26 15:14:10 -040082 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
Anjali Singhai Jain35dae512015-12-22 14:25:03 -080083 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
84 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
Anjali Singhai Jain87e6c1d2015-06-05 12:20:25 -040085 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
86 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
87 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
Catherine Sullivand6bf58c2016-03-18 12:18:08 -070088 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
Shannon Nelson48a3b512015-07-23 16:54:39 -040089 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
Carolyn Wyborny31232372016-11-21 13:03:48 -080091 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000093 /* required last entry */
94 {0, }
95};
96MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
97
98#define I40E_MAX_VF_COUNT 128
99static int debug = -1;
Alexander Duyck5d4ca232016-09-30 08:21:46 -0400100module_param(debug, uint, 0);
101MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000102
103MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
104MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
105MODULE_LICENSE("GPL");
106MODULE_VERSION(DRV_VERSION);
107
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800108static struct workqueue_struct *i40e_wq;
109
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000110/**
111 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
112 * @hw: pointer to the HW structure
113 * @mem: ptr to mem struct to fill out
114 * @size: size of memory requested
115 * @alignment: what to align the allocation to
116 **/
117int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
118 u64 size, u32 alignment)
119{
120 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
121
122 mem->size = ALIGN(size, alignment);
123 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
124 &mem->pa, GFP_KERNEL);
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000125 if (!mem->va)
126 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000127
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000128 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000129}
130
131/**
132 * i40e_free_dma_mem_d - OS specific memory free for shared code
133 * @hw: pointer to the HW structure
134 * @mem: ptr to mem struct to free
135 **/
136int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
137{
138 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
139
140 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
141 mem->va = NULL;
142 mem->pa = 0;
143 mem->size = 0;
144
145 return 0;
146}
147
148/**
149 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
150 * @hw: pointer to the HW structure
151 * @mem: ptr to mem struct to fill out
152 * @size: size of memory requested
153 **/
154int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
155 u32 size)
156{
157 mem->size = size;
158 mem->va = kzalloc(size, GFP_KERNEL);
159
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000160 if (!mem->va)
161 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000162
Jesse Brandeburg93bc73b2013-09-13 08:23:18 +0000163 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000164}
165
166/**
167 * i40e_free_virt_mem_d - OS specific memory free for shared code
168 * @hw: pointer to the HW structure
169 * @mem: ptr to mem struct to free
170 **/
171int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
172{
173 /* it's ok to kfree a NULL pointer */
174 kfree(mem->va);
175 mem->va = NULL;
176 mem->size = 0;
177
178 return 0;
179}
180
181/**
182 * i40e_get_lump - find a lump of free generic resource
183 * @pf: board private structure
184 * @pile: the pile of resource to search
185 * @needed: the number of items needed
186 * @id: an owner id to stick on the items assigned
187 *
188 * Returns the base item index of the lump, or negative for error
189 *
190 * The search_hint trick and lack of advanced fit-finding only work
191 * because we're highly likely to have all the same size lump requests.
192 * Linear search time and any fragmentation should be minimal.
193 **/
194static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
195 u16 needed, u16 id)
196{
197 int ret = -ENOMEM;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000198 int i, j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000199
200 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
201 dev_info(&pf->pdev->dev,
202 "param err: pile=%p needed=%d id=0x%04x\n",
203 pile, needed, id);
204 return -EINVAL;
205 }
206
207 /* start the linear search with an imperfect hint */
208 i = pile->search_hint;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000209 while (i < pile->num_entries) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000210 /* skip already allocated entries */
211 if (pile->list[i] & I40E_PILE_VALID_BIT) {
212 i++;
213 continue;
214 }
215
216 /* do we have enough in this lump? */
217 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
218 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
219 break;
220 }
221
222 if (j == needed) {
223 /* there was enough, so assign it to the requestor */
224 for (j = 0; j < needed; j++)
225 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
226 ret = i;
227 pile->search_hint = i + j;
Jesse Brandeburgddf434a2013-09-13 08:23:19 +0000228 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000229 }
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400230
231 /* not enough, so skip over it and continue looking */
232 i += j;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000233 }
234
235 return ret;
236}
237
238/**
239 * i40e_put_lump - return a lump of generic resource
240 * @pile: the pile of resource to search
241 * @index: the base item index
242 * @id: the owner id of the items assigned
243 *
244 * Returns the count of items in the lump
245 **/
246static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
247{
248 int valid_id = (id | I40E_PILE_VALID_BIT);
249 int count = 0;
250 int i;
251
252 if (!pile || index >= pile->num_entries)
253 return -EINVAL;
254
255 for (i = index;
256 i < pile->num_entries && pile->list[i] == valid_id;
257 i++) {
258 pile->list[i] = 0;
259 count++;
260 }
261
262 if (count && index < pile->search_hint)
263 pile->search_hint = index;
264
265 return count;
266}
267
268/**
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700269 * i40e_find_vsi_from_id - searches for the vsi with the given id
270 * @pf - the pf structure to search for the vsi
271 * @id - id of the vsi it is searching for
272 **/
273struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
274{
275 int i;
276
277 for (i = 0; i < pf->num_alloc_vsi; i++)
278 if (pf->vsi[i] && (pf->vsi[i]->id == id))
279 return pf->vsi[i];
280
281 return NULL;
282}
283
284/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000285 * i40e_service_event_schedule - Schedule the service task to wake up
286 * @pf: board private structure
287 *
288 * If not already scheduled, this puts the task into the work queue
289 **/
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -0600290void i40e_service_event_schedule(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000291{
292 if (!test_bit(__I40E_DOWN, &pf->state) &&
Mitch Williams91089032016-11-21 13:03:51 -0800293 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Jesse Brandeburg2803b162015-12-22 14:25:08 -0800294 queue_work(i40e_wq, &pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000295}
296
297/**
298 * i40e_tx_timeout - Respond to a Tx Hang
299 * @netdev: network interface device structure
300 *
301 * If any port has noticed a Tx timeout, it is likely that the whole
302 * device is munged, not just the one netdev port, so go for the full
303 * reset.
304 **/
305static void i40e_tx_timeout(struct net_device *netdev)
306{
307 struct i40e_netdev_priv *np = netdev_priv(netdev);
308 struct i40e_vsi *vsi = np->vsi;
309 struct i40e_pf *pf = vsi->back;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400310 struct i40e_ring *tx_ring = NULL;
311 unsigned int i, hung_queue = 0;
312 u32 head, val;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000313
314 pf->tx_timeout_count++;
315
Kiran Patilb03a8c12015-09-24 18:13:15 -0400316 /* find the stopped queue the same way the stack does */
317 for (i = 0; i < netdev->num_tx_queues; i++) {
318 struct netdev_queue *q;
319 unsigned long trans_start;
320
321 q = netdev_get_tx_queue(netdev, i);
Florian Westphal9b366272016-05-03 16:33:14 +0200322 trans_start = q->trans_start;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400323 if (netif_xmit_stopped(q) &&
324 time_after(jiffies,
325 (trans_start + netdev->watchdog_timeo))) {
326 hung_queue = i;
327 break;
328 }
329 }
330
331 if (i == netdev->num_tx_queues) {
332 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
333 } else {
334 /* now that we have an index, find the tx_ring struct */
335 for (i = 0; i < vsi->num_queue_pairs; i++) {
336 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
337 if (hung_queue ==
338 vsi->tx_rings[i]->queue_index) {
339 tx_ring = vsi->tx_rings[i];
340 break;
341 }
342 }
343 }
344 }
345
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000346 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
Kiran Patilb03a8c12015-09-24 18:13:15 -0400347 pf->tx_timeout_recovery_level = 1; /* reset after some time */
348 else if (time_before(jiffies,
349 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
350 return; /* don't do any new action before the next timeout */
351
352 if (tx_ring) {
353 head = i40e_get_head(tx_ring);
354 /* Read interrupt register */
355 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
356 val = rd32(&pf->hw,
357 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
358 tx_ring->vsi->base_vector - 1));
359 else
360 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
361
362 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",
363 vsi->seid, hung_queue, tx_ring->next_to_clean,
364 head, tx_ring->next_to_use,
365 readl(tx_ring->tail), val);
366 }
367
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000368 pf->tx_timeout_last_recovery = jiffies;
Kiran Patilb03a8c12015-09-24 18:13:15 -0400369 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
370 pf->tx_timeout_recovery_level, hung_queue);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000371
372 switch (pf->tx_timeout_recovery_level) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000373 case 1:
374 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
375 break;
376 case 2:
377 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
378 break;
379 case 3:
380 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
381 break;
382 default:
383 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000384 break;
385 }
Kiran Patilb03a8c12015-09-24 18:13:15 -0400386
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000387 i40e_service_event_schedule(pf);
388 pf->tx_timeout_recovery_level++;
389}
390
391/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000392 * i40e_get_vsi_stats_struct - Get System Network Statistics
393 * @vsi: the VSI we care about
394 *
395 * Returns the address of the device statistics structure.
396 * The statistics are actually updated from the service task.
397 **/
398struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
399{
400 return &vsi->net_stats;
401}
402
403/**
404 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
405 * @netdev: network interface device structure
406 *
407 * Returns the address of the device statistics structure.
408 * The statistics are actually updated from the service task.
409 **/
Alexander Duyck9eed69a2017-02-21 15:55:47 -0800410static void i40e_get_netdev_stats_struct(struct net_device *netdev,
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800411 struct rtnl_link_stats64 *stats)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000412{
413 struct i40e_netdev_priv *np = netdev_priv(netdev);
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +0000414 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000415 struct i40e_vsi *vsi = np->vsi;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000416 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
417 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000418
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000419 if (test_bit(__I40E_DOWN, &vsi->state))
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800420 return;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +0000421
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800422 if (!vsi->tx_rings)
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800423 return;
Jesse Brandeburg3c325ce2013-12-14 03:26:45 -0800424
Alexander Duyck980e9b12013-09-28 06:01:03 +0000425 rcu_read_lock();
426 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000427 u64 bytes, packets;
428 unsigned int start;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000429
Alexander Duyck980e9b12013-09-28 06:01:03 +0000430 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
431 if (!tx_ring)
432 continue;
433
434 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700435 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000436 packets = tx_ring->stats.packets;
437 bytes = tx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700438 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000439
440 stats->tx_packets += packets;
441 stats->tx_bytes += bytes;
442 rx_ring = &tx_ring[1];
443
444 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700445 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000446 packets = rx_ring->stats.packets;
447 bytes = rx_ring->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700448 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000449
450 stats->rx_packets += packets;
451 stats->rx_bytes += bytes;
452 }
453 rcu_read_unlock();
454
Akeem G Abodunrina5282f42014-05-10 04:49:03 +0000455 /* following stats updated by i40e_watchdog_subtask() */
Alexander Duyck980e9b12013-09-28 06:01:03 +0000456 stats->multicast = vsi_stats->multicast;
457 stats->tx_errors = vsi_stats->tx_errors;
458 stats->tx_dropped = vsi_stats->tx_dropped;
459 stats->rx_errors = vsi_stats->rx_errors;
Jesse Brandeburgd8201e22015-07-23 16:54:35 -0400460 stats->rx_dropped = vsi_stats->rx_dropped;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000461 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
462 stats->rx_length_errors = vsi_stats->rx_length_errors;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000463}
464
465/**
466 * i40e_vsi_reset_stats - Resets all stats of the given vsi
467 * @vsi: the VSI to have its stats reset
468 **/
469void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
470{
471 struct rtnl_link_stats64 *ns;
472 int i;
473
474 if (!vsi)
475 return;
476
477 ns = i40e_get_vsi_stats_struct(vsi);
478 memset(ns, 0, sizeof(*ns));
479 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
480 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
481 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
Greg Rose8e9dca52013-12-18 13:45:53 +0000482 if (vsi->rx_rings && vsi->rx_rings[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000483 for (i = 0; i < vsi->num_queue_pairs; i++) {
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400484 memset(&vsi->rx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000485 sizeof(vsi->rx_rings[i]->stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400486 memset(&vsi->rx_rings[i]->rx_stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000487 sizeof(vsi->rx_rings[i]->rx_stats));
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400488 memset(&vsi->tx_rings[i]->stats, 0,
Alexander Duyck9f65e152013-09-28 06:00:58 +0000489 sizeof(vsi->tx_rings[i]->stats));
490 memset(&vsi->tx_rings[i]->tx_stats, 0,
491 sizeof(vsi->tx_rings[i]->tx_stats));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000492 }
Greg Rose8e9dca52013-12-18 13:45:53 +0000493 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000494 vsi->stat_offsets_loaded = false;
495}
496
497/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000498 * i40e_pf_reset_stats - Reset all of the stats for the given PF
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000499 * @pf: the PF to be reset
500 **/
501void i40e_pf_reset_stats(struct i40e_pf *pf)
502{
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000503 int i;
504
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000505 memset(&pf->stats, 0, sizeof(pf->stats));
506 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
507 pf->stat_offsets_loaded = false;
Shannon Nelsone91fdf72014-06-03 23:50:18 +0000508
509 for (i = 0; i < I40E_MAX_VEB; i++) {
510 if (pf->veb[i]) {
511 memset(&pf->veb[i]->stats, 0,
512 sizeof(pf->veb[i]->stats));
513 memset(&pf->veb[i]->stats_offsets, 0,
514 sizeof(pf->veb[i]->stats_offsets));
515 pf->veb[i]->stat_offsets_loaded = false;
516 }
517 }
Catherine Sullivan42bce042016-07-27 12:02:32 -0700518 pf->hw_csum_rx_error = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000519}
520
521/**
522 * i40e_stat_update48 - read and update a 48 bit stat from the chip
523 * @hw: ptr to the hardware info
524 * @hireg: the high 32 bit reg to read
525 * @loreg: the low 32 bit reg to read
526 * @offset_loaded: has the initial offset been loaded yet
527 * @offset: ptr to current offset value
528 * @stat: ptr to the stat
529 *
530 * Since the device stats are not reset at PFReset, they likely will not
531 * be zeroed when the driver starts. We'll save the first values read
532 * and use them as offsets to be subtracted from the raw values in order
533 * to report stats that count from zero. In the process, we also manage
534 * the potential roll-over.
535 **/
536static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
537 bool offset_loaded, u64 *offset, u64 *stat)
538{
539 u64 new_data;
540
Shannon Nelsonab600852014-01-17 15:36:39 -0800541 if (hw->device_id == I40E_DEV_ID_QEMU) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000542 new_data = rd32(hw, loreg);
543 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
544 } else {
545 new_data = rd64(hw, loreg);
546 }
547 if (!offset_loaded)
548 *offset = new_data;
549 if (likely(new_data >= *offset))
550 *stat = new_data - *offset;
551 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400552 *stat = (new_data + BIT_ULL(48)) - *offset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000553 *stat &= 0xFFFFFFFFFFFFULL;
554}
555
556/**
557 * i40e_stat_update32 - read and update a 32 bit stat from the chip
558 * @hw: ptr to the hardware info
559 * @reg: the hw reg to read
560 * @offset_loaded: has the initial offset been loaded yet
561 * @offset: ptr to current offset value
562 * @stat: ptr to the stat
563 **/
564static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
565 bool offset_loaded, u64 *offset, u64 *stat)
566{
567 u32 new_data;
568
569 new_data = rd32(hw, reg);
570 if (!offset_loaded)
571 *offset = new_data;
572 if (likely(new_data >= *offset))
573 *stat = (u32)(new_data - *offset);
574 else
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400575 *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000576}
577
578/**
579 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
580 * @vsi: the VSI to be updated
581 **/
582void i40e_update_eth_stats(struct i40e_vsi *vsi)
583{
584 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
585 struct i40e_pf *pf = vsi->back;
586 struct i40e_hw *hw = &pf->hw;
587 struct i40e_eth_stats *oes;
588 struct i40e_eth_stats *es; /* device's eth stats */
589
590 es = &vsi->eth_stats;
591 oes = &vsi->eth_stats_offsets;
592
593 /* Gather up the stats that the hw collects */
594 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
595 vsi->stat_offsets_loaded,
596 &oes->tx_errors, &es->tx_errors);
597 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
598 vsi->stat_offsets_loaded,
599 &oes->rx_discards, &es->rx_discards);
Shannon Nelson41a9e552014-04-23 04:50:20 +0000600 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
601 vsi->stat_offsets_loaded,
602 &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
603 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
604 vsi->stat_offsets_loaded,
605 &oes->tx_errors, &es->tx_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000606
607 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
608 I40E_GLV_GORCL(stat_idx),
609 vsi->stat_offsets_loaded,
610 &oes->rx_bytes, &es->rx_bytes);
611 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
612 I40E_GLV_UPRCL(stat_idx),
613 vsi->stat_offsets_loaded,
614 &oes->rx_unicast, &es->rx_unicast);
615 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
616 I40E_GLV_MPRCL(stat_idx),
617 vsi->stat_offsets_loaded,
618 &oes->rx_multicast, &es->rx_multicast);
619 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
620 I40E_GLV_BPRCL(stat_idx),
621 vsi->stat_offsets_loaded,
622 &oes->rx_broadcast, &es->rx_broadcast);
623
624 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
625 I40E_GLV_GOTCL(stat_idx),
626 vsi->stat_offsets_loaded,
627 &oes->tx_bytes, &es->tx_bytes);
628 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
629 I40E_GLV_UPTCL(stat_idx),
630 vsi->stat_offsets_loaded,
631 &oes->tx_unicast, &es->tx_unicast);
632 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
633 I40E_GLV_MPTCL(stat_idx),
634 vsi->stat_offsets_loaded,
635 &oes->tx_multicast, &es->tx_multicast);
636 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
637 I40E_GLV_BPTCL(stat_idx),
638 vsi->stat_offsets_loaded,
639 &oes->tx_broadcast, &es->tx_broadcast);
640 vsi->stat_offsets_loaded = true;
641}
642
643/**
644 * i40e_update_veb_stats - Update Switch component statistics
645 * @veb: the VEB being updated
646 **/
647static void i40e_update_veb_stats(struct i40e_veb *veb)
648{
649 struct i40e_pf *pf = veb->pf;
650 struct i40e_hw *hw = &pf->hw;
651 struct i40e_eth_stats *oes;
652 struct i40e_eth_stats *es; /* device's eth stats */
Neerav Parikhfe860af2015-07-10 19:36:02 -0400653 struct i40e_veb_tc_stats *veb_oes;
654 struct i40e_veb_tc_stats *veb_es;
655 int i, idx = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000656
657 idx = veb->stats_idx;
658 es = &veb->stats;
659 oes = &veb->stats_offsets;
Neerav Parikhfe860af2015-07-10 19:36:02 -0400660 veb_es = &veb->tc_stats;
661 veb_oes = &veb->tc_stats_offsets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000662
663 /* Gather up the stats that the hw collects */
664 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
665 veb->stat_offsets_loaded,
666 &oes->tx_discards, &es->tx_discards);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +0000667 if (hw->revision_id > 0)
668 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
669 veb->stat_offsets_loaded,
670 &oes->rx_unknown_protocol,
671 &es->rx_unknown_protocol);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000672 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
673 veb->stat_offsets_loaded,
674 &oes->rx_bytes, &es->rx_bytes);
675 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
676 veb->stat_offsets_loaded,
677 &oes->rx_unicast, &es->rx_unicast);
678 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
679 veb->stat_offsets_loaded,
680 &oes->rx_multicast, &es->rx_multicast);
681 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
682 veb->stat_offsets_loaded,
683 &oes->rx_broadcast, &es->rx_broadcast);
684
685 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
686 veb->stat_offsets_loaded,
687 &oes->tx_bytes, &es->tx_bytes);
688 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
689 veb->stat_offsets_loaded,
690 &oes->tx_unicast, &es->tx_unicast);
691 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
692 veb->stat_offsets_loaded,
693 &oes->tx_multicast, &es->tx_multicast);
694 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
695 veb->stat_offsets_loaded,
696 &oes->tx_broadcast, &es->tx_broadcast);
Neerav Parikhfe860af2015-07-10 19:36:02 -0400697 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
698 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
699 I40E_GLVEBTC_RPCL(i, idx),
700 veb->stat_offsets_loaded,
701 &veb_oes->tc_rx_packets[i],
702 &veb_es->tc_rx_packets[i]);
703 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
704 I40E_GLVEBTC_RBCL(i, idx),
705 veb->stat_offsets_loaded,
706 &veb_oes->tc_rx_bytes[i],
707 &veb_es->tc_rx_bytes[i]);
708 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
709 I40E_GLVEBTC_TPCL(i, idx),
710 veb->stat_offsets_loaded,
711 &veb_oes->tc_tx_packets[i],
712 &veb_es->tc_tx_packets[i]);
713 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
714 I40E_GLVEBTC_TBCL(i, idx),
715 veb->stat_offsets_loaded,
716 &veb_oes->tc_tx_bytes[i],
717 &veb_es->tc_tx_bytes[i]);
718 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000719 veb->stat_offsets_loaded = true;
720}
721
722/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000723 * i40e_update_vsi_stats - Update the vsi statistics counters.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000724 * @vsi: the VSI to be updated
725 *
726 * There are a few instances where we store the same stat in a
727 * couple of different structs. This is partly because we have
728 * the netdev stats that need to be filled out, which is slightly
729 * different from the "eth_stats" defined by the chip and used in
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000730 * VF communications. We sort it out here.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000731 **/
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000732static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000733{
734 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000735 struct rtnl_link_stats64 *ons;
736 struct rtnl_link_stats64 *ns; /* netdev stats */
737 struct i40e_eth_stats *oes;
738 struct i40e_eth_stats *es; /* device's eth stats */
739 u32 tx_restart, tx_busy;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000740 struct i40e_ring *p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000741 u32 rx_page, rx_buf;
Akeem G Abodunrinbf00b372014-10-17 03:14:39 +0000742 u64 bytes, packets;
743 unsigned int start;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400744 u64 tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400745 u64 tx_force_wb;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000746 u64 rx_p, rx_b;
747 u64 tx_p, tx_b;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000748 u16 q;
749
750 if (test_bit(__I40E_DOWN, &vsi->state) ||
751 test_bit(__I40E_CONFIG_BUSY, &pf->state))
752 return;
753
754 ns = i40e_get_vsi_stats_struct(vsi);
755 ons = &vsi->net_stats_offsets;
756 es = &vsi->eth_stats;
757 oes = &vsi->eth_stats_offsets;
758
759 /* Gather up the netdev and vsi stats that the driver collects
760 * on the fly during packet processing
761 */
762 rx_b = rx_p = 0;
763 tx_b = tx_p = 0;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400764 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000765 rx_page = 0;
766 rx_buf = 0;
Alexander Duyck980e9b12013-09-28 06:01:03 +0000767 rcu_read_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000768 for (q = 0; q < vsi->num_queue_pairs; q++) {
Alexander Duyck980e9b12013-09-28 06:01:03 +0000769 /* locate Tx ring */
770 p = ACCESS_ONCE(vsi->tx_rings[q]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000771
Alexander Duyck980e9b12013-09-28 06:01:03 +0000772 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700773 start = u64_stats_fetch_begin_irq(&p->syncp);
Alexander Duyck980e9b12013-09-28 06:01:03 +0000774 packets = p->stats.packets;
775 bytes = p->stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700776 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
Alexander Duyck980e9b12013-09-28 06:01:03 +0000777 tx_b += bytes;
778 tx_p += packets;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000779 tx_restart += p->tx_stats.restart_queue;
780 tx_busy += p->tx_stats.tx_busy;
Anjali Singhai Jain2fc3d712015-08-27 11:42:29 -0400781 tx_linearize += p->tx_stats.tx_linearize;
Anjali Singhai Jain164c9f52015-10-21 19:47:08 -0400782 tx_force_wb += p->tx_stats.tx_force_wb;
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;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000801 vsi->rx_page_failed = rx_page;
802 vsi->rx_buf_failed = rx_buf;
803
804 ns->rx_packets = rx_p;
805 ns->rx_bytes = rx_b;
806 ns->tx_packets = tx_p;
807 ns->tx_bytes = tx_b;
808
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000809 /* update netdev stats from eth stats */
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000810 i40e_update_eth_stats(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000811 ons->tx_errors = oes->tx_errors;
812 ns->tx_errors = es->tx_errors;
813 ons->multicast = oes->rx_multicast;
814 ns->multicast = es->rx_multicast;
Shannon Nelson41a9e552014-04-23 04:50:20 +0000815 ons->rx_dropped = oes->rx_discards;
816 ns->rx_dropped = es->rx_discards;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000817 ons->tx_dropped = oes->tx_discards;
818 ns->tx_dropped = es->tx_discards;
819
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000820 /* pull in a couple PF stats if this is the main vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000821 if (vsi == pf->vsi[pf->lan_vsi]) {
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000822 ns->rx_crc_errors = pf->stats.crc_errors;
823 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
824 ns->rx_length_errors = pf->stats.rx_length_errors;
825 }
826}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000827
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000828/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000829 * i40e_update_pf_stats - Update the PF statistics counters.
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000830 * @pf: the PF to be updated
831 **/
832static void i40e_update_pf_stats(struct i40e_pf *pf)
833{
834 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
835 struct i40e_hw_port_stats *nsd = &pf->stats;
836 struct i40e_hw *hw = &pf->hw;
837 u32 val;
838 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000839
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000840 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
841 I40E_GLPRT_GORCL(hw->port),
842 pf->stat_offsets_loaded,
843 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
844 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
845 I40E_GLPRT_GOTCL(hw->port),
846 pf->stat_offsets_loaded,
847 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
848 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
849 pf->stat_offsets_loaded,
850 &osd->eth.rx_discards,
851 &nsd->eth.rx_discards);
Shannon Nelson532d2832014-04-23 04:50:09 +0000852 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
853 I40E_GLPRT_UPRCL(hw->port),
854 pf->stat_offsets_loaded,
855 &osd->eth.rx_unicast,
856 &nsd->eth.rx_unicast);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000857 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
858 I40E_GLPRT_MPRCL(hw->port),
859 pf->stat_offsets_loaded,
860 &osd->eth.rx_multicast,
861 &nsd->eth.rx_multicast);
Shannon Nelson532d2832014-04-23 04:50:09 +0000862 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
863 I40E_GLPRT_BPRCL(hw->port),
864 pf->stat_offsets_loaded,
865 &osd->eth.rx_broadcast,
866 &nsd->eth.rx_broadcast);
867 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
868 I40E_GLPRT_UPTCL(hw->port),
869 pf->stat_offsets_loaded,
870 &osd->eth.tx_unicast,
871 &nsd->eth.tx_unicast);
872 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
873 I40E_GLPRT_MPTCL(hw->port),
874 pf->stat_offsets_loaded,
875 &osd->eth.tx_multicast,
876 &nsd->eth.tx_multicast);
877 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
878 I40E_GLPRT_BPTCL(hw->port),
879 pf->stat_offsets_loaded,
880 &osd->eth.tx_broadcast,
881 &nsd->eth.tx_broadcast);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000882
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000883 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
884 pf->stat_offsets_loaded,
885 &osd->tx_dropped_link_down,
886 &nsd->tx_dropped_link_down);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000887
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000888 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
889 pf->stat_offsets_loaded,
890 &osd->crc_errors, &nsd->crc_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000891
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000892 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
893 pf->stat_offsets_loaded,
894 &osd->illegal_bytes, &nsd->illegal_bytes);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000895
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000896 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
897 pf->stat_offsets_loaded,
898 &osd->mac_local_faults,
899 &nsd->mac_local_faults);
900 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
901 pf->stat_offsets_loaded,
902 &osd->mac_remote_faults,
903 &nsd->mac_remote_faults);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000904
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000905 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
906 pf->stat_offsets_loaded,
907 &osd->rx_length_errors,
908 &nsd->rx_length_errors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000909
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000910 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
911 pf->stat_offsets_loaded,
912 &osd->link_xon_rx, &nsd->link_xon_rx);
913 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
914 pf->stat_offsets_loaded,
915 &osd->link_xon_tx, &nsd->link_xon_tx);
Neerav Parikh95db2392015-11-06 15:26:09 -0800916 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->link_xoff_rx, &nsd->link_xoff_rx);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000919 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
920 pf->stat_offsets_loaded,
921 &osd->link_xoff_tx, &nsd->link_xoff_tx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000922
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000923 for (i = 0; i < 8; i++) {
Neerav Parikh95db2392015-11-06 15:26:09 -0800924 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
925 pf->stat_offsets_loaded,
926 &osd->priority_xoff_rx[i],
927 &nsd->priority_xoff_rx[i]);
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000928 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000929 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000930 &osd->priority_xon_rx[i],
931 &nsd->priority_xon_rx[i]);
932 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000933 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000934 &osd->priority_xon_tx[i],
935 &nsd->priority_xon_tx[i]);
936 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000937 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000938 &osd->priority_xoff_tx[i],
939 &nsd->priority_xoff_tx[i]);
940 i40e_stat_update32(hw,
941 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000942 pf->stat_offsets_loaded,
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000943 &osd->priority_xon_2_xoff[i],
944 &nsd->priority_xon_2_xoff[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +0000945 }
946
Shannon Nelson7812fdd2014-04-23 04:50:18 +0000947 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
948 I40E_GLPRT_PRC64L(hw->port),
949 pf->stat_offsets_loaded,
950 &osd->rx_size_64, &nsd->rx_size_64);
951 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
952 I40E_GLPRT_PRC127L(hw->port),
953 pf->stat_offsets_loaded,
954 &osd->rx_size_127, &nsd->rx_size_127);
955 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
956 I40E_GLPRT_PRC255L(hw->port),
957 pf->stat_offsets_loaded,
958 &osd->rx_size_255, &nsd->rx_size_255);
959 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
960 I40E_GLPRT_PRC511L(hw->port),
961 pf->stat_offsets_loaded,
962 &osd->rx_size_511, &nsd->rx_size_511);
963 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
964 I40E_GLPRT_PRC1023L(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->rx_size_1023, &nsd->rx_size_1023);
967 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
968 I40E_GLPRT_PRC1522L(hw->port),
969 pf->stat_offsets_loaded,
970 &osd->rx_size_1522, &nsd->rx_size_1522);
971 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
972 I40E_GLPRT_PRC9522L(hw->port),
973 pf->stat_offsets_loaded,
974 &osd->rx_size_big, &nsd->rx_size_big);
975
976 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
977 I40E_GLPRT_PTC64L(hw->port),
978 pf->stat_offsets_loaded,
979 &osd->tx_size_64, &nsd->tx_size_64);
980 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
981 I40E_GLPRT_PTC127L(hw->port),
982 pf->stat_offsets_loaded,
983 &osd->tx_size_127, &nsd->tx_size_127);
984 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
985 I40E_GLPRT_PTC255L(hw->port),
986 pf->stat_offsets_loaded,
987 &osd->tx_size_255, &nsd->tx_size_255);
988 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
989 I40E_GLPRT_PTC511L(hw->port),
990 pf->stat_offsets_loaded,
991 &osd->tx_size_511, &nsd->tx_size_511);
992 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
993 I40E_GLPRT_PTC1023L(hw->port),
994 pf->stat_offsets_loaded,
995 &osd->tx_size_1023, &nsd->tx_size_1023);
996 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
997 I40E_GLPRT_PTC1522L(hw->port),
998 pf->stat_offsets_loaded,
999 &osd->tx_size_1522, &nsd->tx_size_1522);
1000 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1001 I40E_GLPRT_PTC9522L(hw->port),
1002 pf->stat_offsets_loaded,
1003 &osd->tx_size_big, &nsd->tx_size_big);
1004
1005 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1006 pf->stat_offsets_loaded,
1007 &osd->rx_undersize, &nsd->rx_undersize);
1008 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1009 pf->stat_offsets_loaded,
1010 &osd->rx_fragments, &nsd->rx_fragments);
1011 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1012 pf->stat_offsets_loaded,
1013 &osd->rx_oversize, &nsd->rx_oversize);
1014 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1015 pf->stat_offsets_loaded,
1016 &osd->rx_jabber, &nsd->rx_jabber);
1017
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001018 /* FDIR stats */
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001019 i40e_stat_update32(hw,
1020 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001021 pf->stat_offsets_loaded,
1022 &osd->fd_atr_match, &nsd->fd_atr_match);
Anjali Singhai Jain0bf4b1b2015-04-16 20:06:02 -04001023 i40e_stat_update32(hw,
1024 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001025 pf->stat_offsets_loaded,
1026 &osd->fd_sb_match, &nsd->fd_sb_match);
Anjali Singhai Jain60ccd452015-04-16 20:06:01 -04001027 i40e_stat_update32(hw,
1028 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
1029 pf->stat_offsets_loaded,
1030 &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
Anjali Singhai Jain433c47d2014-05-22 06:32:17 +00001031
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001032 val = rd32(hw, I40E_PRTPM_EEE_STAT);
1033 nsd->tx_lpi_status =
1034 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1035 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1036 nsd->rx_lpi_status =
1037 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1038 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1039 i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1040 pf->stat_offsets_loaded,
1041 &osd->tx_lpi_count, &nsd->tx_lpi_count);
1042 i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1043 pf->stat_offsets_loaded,
1044 &osd->rx_lpi_count, &nsd->rx_lpi_count);
1045
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001046 if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001047 !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001048 nsd->fd_sb_status = true;
1049 else
1050 nsd->fd_sb_status = false;
1051
1052 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08001053 !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
Anjali Singhai Jaind0389e52015-04-22 19:34:05 -04001054 nsd->fd_atr_status = true;
1055 else
1056 nsd->fd_atr_status = false;
1057
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001058 pf->stat_offsets_loaded = true;
1059}
1060
1061/**
Shannon Nelson7812fdd2014-04-23 04:50:18 +00001062 * i40e_update_stats - Update the various statistics counters.
1063 * @vsi: the VSI to be updated
1064 *
1065 * Update the various stats for this VSI and its related entities.
1066 **/
1067void i40e_update_stats(struct i40e_vsi *vsi)
1068{
1069 struct i40e_pf *pf = vsi->back;
1070
1071 if (vsi == pf->vsi[pf->lan_vsi])
1072 i40e_update_pf_stats(pf);
1073
1074 i40e_update_vsi_stats(vsi);
1075}
1076
1077/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001078 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1079 * @vsi: the VSI to be searched
1080 * @macaddr: the MAC address
1081 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001082 *
1083 * Returns ptr to the filter object or NULL
1084 **/
1085static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001086 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001087{
1088 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001089 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001090
1091 if (!vsi || !macaddr)
1092 return NULL;
1093
Jacob Keller278e7d02016-10-05 09:30:37 -07001094 key = i40e_addr_to_hkey(macaddr);
1095 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001096 if ((ether_addr_equal(macaddr, f->macaddr)) &&
Jacob Keller1bc87e82016-10-05 09:30:31 -07001097 (vlan == f->vlan))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001098 return f;
1099 }
1100 return NULL;
1101}
1102
1103/**
1104 * i40e_find_mac - Find a mac addr in the macvlan filters list
1105 * @vsi: the VSI to be searched
1106 * @macaddr: the MAC address we are searching for
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001107 *
1108 * Returns the first filter with the provided MAC address or NULL if
1109 * MAC address was not found
1110 **/
Jacob Keller6622f5c2016-10-05 09:30:32 -07001111struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001112{
1113 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001114 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001115
1116 if (!vsi || !macaddr)
1117 return NULL;
1118
Jacob Keller278e7d02016-10-05 09:30:37 -07001119 key = i40e_addr_to_hkey(macaddr);
1120 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001121 if ((ether_addr_equal(macaddr, f->macaddr)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001122 return f;
1123 }
1124 return NULL;
1125}
1126
1127/**
1128 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1129 * @vsi: the VSI to be searched
1130 *
1131 * Returns true if VSI is in vlan mode or false otherwise
1132 **/
1133bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1134{
Jacob Kellercbebb852016-10-05 09:30:40 -07001135 /* If we have a PVID, always operate in VLAN mode */
1136 if (vsi->info.pvid)
1137 return true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001138
Jacob Kellercbebb852016-10-05 09:30:40 -07001139 /* We need to operate in VLAN mode whenever we have any filters with
1140 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1141 * time, incurring search cost repeatedly. However, we can notice two
1142 * things:
1143 *
1144 * 1) the only place where we can gain a VLAN filter is in
1145 * i40e_add_filter.
1146 *
1147 * 2) the only place where filters are actually removed is in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001148 * i40e_sync_filters_subtask.
Jacob Kellercbebb852016-10-05 09:30:40 -07001149 *
1150 * Thus, we can simply use a boolean value, has_vlan_filters which we
1151 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1152 * we have to perform the full search after deleting filters in
Jacob Keller0b7c8b52016-10-25 16:08:52 -07001153 * i40e_sync_filters_subtask, but we already have to search
Jacob Kellercbebb852016-10-05 09:30:40 -07001154 * filters here and can perform the check at the same time. This
1155 * results in avoiding embedding a loop for VLAN mode inside another
1156 * loop over all the filters, and should maintain correctness as noted
1157 * above.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001158 */
Jacob Kellercbebb852016-10-05 09:30:40 -07001159 return vsi->has_vlan_filter;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001160}
1161
1162/**
Jacob Keller489a3262016-11-11 12:39:31 -08001163 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1164 * @vsi: the VSI to configure
1165 * @tmp_add_list: list of filters ready to be added
1166 * @tmp_del_list: list of filters ready to be deleted
1167 * @vlan_filters: the number of active VLAN filters
1168 *
1169 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1170 * behave as expected. If we have any active VLAN filters remaining or about
1171 * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1172 * so that they only match against untagged traffic. If we no longer have any
1173 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1174 * so that they match against both tagged and untagged traffic. In this way,
1175 * we ensure that we correctly receive the desired traffic. This ensures that
1176 * when we have an active VLAN we will receive only untagged traffic and
1177 * traffic matching active VLANs. If we have no active VLANs then we will
1178 * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1179 *
1180 * Finally, in a similar fashion, this function also corrects filters when
1181 * there is an active PVID assigned to this VSI.
1182 *
1183 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1184 *
1185 * This function is only expected to be called from within
1186 * i40e_sync_vsi_filters.
1187 *
1188 * NOTE: This function expects to be called while under the
1189 * mac_filter_hash_lock
1190 */
1191static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1192 struct hlist_head *tmp_add_list,
1193 struct hlist_head *tmp_del_list,
1194 int vlan_filters)
1195{
Jacob Keller5cb25902016-12-12 15:44:15 -08001196 s16 pvid = le16_to_cpu(vsi->info.pvid);
Jacob Keller489a3262016-11-11 12:39:31 -08001197 struct i40e_mac_filter *f, *add_head;
Jacob Keller671889e2016-12-02 12:33:00 -08001198 struct i40e_new_mac_filter *new;
Jacob Keller489a3262016-11-11 12:39:31 -08001199 struct hlist_node *h;
1200 int bkt, new_vlan;
1201
1202 /* To determine if a particular filter needs to be replaced we
1203 * have the three following conditions:
1204 *
1205 * a) if we have a PVID assigned, then all filters which are
1206 * not marked as VLAN=PVID must be replaced with filters that
1207 * are.
1208 * b) otherwise, if we have any active VLANS, all filters
1209 * which are marked as VLAN=-1 must be replaced with
1210 * filters marked as VLAN=0
1211 * c) finally, if we do not have any active VLANS, all filters
1212 * which are marked as VLAN=0 must be replaced with filters
1213 * marked as VLAN=-1
1214 */
1215
1216 /* Update the filters about to be added in place */
Jacob Keller671889e2016-12-02 12:33:00 -08001217 hlist_for_each_entry(new, tmp_add_list, hlist) {
Jacob Keller5cb25902016-12-12 15:44:15 -08001218 if (pvid && new->f->vlan != pvid)
1219 new->f->vlan = pvid;
Jacob Keller671889e2016-12-02 12:33:00 -08001220 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1221 new->f->vlan = 0;
1222 else if (!vlan_filters && new->f->vlan == 0)
1223 new->f->vlan = I40E_VLAN_ANY;
Jacob Keller489a3262016-11-11 12:39:31 -08001224 }
1225
1226 /* Update the remaining active filters */
1227 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1228 /* Combine the checks for whether a filter needs to be changed
1229 * and then determine the new VLAN inside the if block, in
1230 * order to avoid duplicating code for adding the new filter
1231 * then deleting the old filter.
1232 */
Jacob Keller5cb25902016-12-12 15:44:15 -08001233 if ((pvid && f->vlan != pvid) ||
Jacob Keller489a3262016-11-11 12:39:31 -08001234 (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1235 (!vlan_filters && f->vlan == 0)) {
1236 /* Determine the new vlan we will be adding */
Jacob Keller5cb25902016-12-12 15:44:15 -08001237 if (pvid)
1238 new_vlan = pvid;
Jacob Keller489a3262016-11-11 12:39:31 -08001239 else if (vlan_filters)
1240 new_vlan = 0;
1241 else
1242 new_vlan = I40E_VLAN_ANY;
1243
1244 /* Create the new filter */
1245 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1246 if (!add_head)
1247 return -ENOMEM;
1248
Jacob Keller671889e2016-12-02 12:33:00 -08001249 /* Create a temporary i40e_new_mac_filter */
1250 new = kzalloc(sizeof(*new), GFP_ATOMIC);
1251 if (!new)
1252 return -ENOMEM;
1253
1254 new->f = add_head;
1255 new->state = add_head->state;
1256
1257 /* Add the new filter to the tmp list */
1258 hlist_add_head(&new->hlist, tmp_add_list);
Jacob Keller489a3262016-11-11 12:39:31 -08001259
1260 /* Put the original filter into the delete list */
1261 f->state = I40E_FILTER_REMOVE;
1262 hash_del(&f->hlist);
1263 hlist_add_head(&f->hlist, tmp_del_list);
1264 }
1265 }
1266
1267 vsi->has_vlan_filter = !!vlan_filters;
1268
1269 return 0;
1270}
1271
1272/**
Jacob Keller1596b5d2016-11-08 13:05:15 -08001273 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1274 * @vsi: the PF Main VSI - inappropriate for any other VSI
1275 * @macaddr: the MAC address
1276 *
1277 * Remove whatever filter the firmware set up so the driver can manage
1278 * its own filtering intelligently.
1279 **/
1280static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1281{
1282 struct i40e_aqc_remove_macvlan_element_data element;
1283 struct i40e_pf *pf = vsi->back;
1284
1285 /* Only appropriate for the PF main VSI */
1286 if (vsi->type != I40E_VSI_MAIN)
1287 return;
1288
1289 memset(&element, 0, sizeof(element));
1290 ether_addr_copy(element.mac_addr, macaddr);
1291 element.vlan_tag = 0;
1292 /* Ignore error returns, some firmware does it this way... */
1293 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1294 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1295
1296 memset(&element, 0, sizeof(element));
1297 ether_addr_copy(element.mac_addr, macaddr);
1298 element.vlan_tag = 0;
1299 /* ...and some firmware does it this way. */
1300 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1301 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1302 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1303}
1304
1305/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001306 * i40e_add_filter - Add a mac/vlan filter to the VSI
1307 * @vsi: the VSI to be searched
1308 * @macaddr: the MAC address
1309 * @vlan: the vlan
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001310 *
1311 * Returns ptr to the filter object or NULL when no memory available.
Kiran Patil21659032015-09-30 14:09:03 -04001312 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001313 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001314 * being held.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001315 **/
1316struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
Jacob Keller6622f5c2016-10-05 09:30:32 -07001317 const u8 *macaddr, s16 vlan)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001318{
1319 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07001320 u64 key;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001321
1322 if (!vsi || !macaddr)
1323 return NULL;
1324
Jacob Keller1bc87e82016-10-05 09:30:31 -07001325 f = i40e_find_filter(vsi, macaddr, vlan);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001326 if (!f) {
1327 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1328 if (!f)
Jacob Keller1bc87e82016-10-05 09:30:31 -07001329 return NULL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001330
Jacob Kellercbebb852016-10-05 09:30:40 -07001331 /* Update the boolean indicating if we need to function in
1332 * VLAN mode.
1333 */
1334 if (vlan >= 0)
1335 vsi->has_vlan_filter = true;
1336
Greg Rose9a173902014-05-22 06:32:02 +00001337 ether_addr_copy(f->macaddr, macaddr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001338 f->vlan = vlan;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001339 /* If we're in overflow promisc mode, set the state directly
1340 * to failed, so we don't bother to try sending the filter
1341 * to the hardware.
1342 */
1343 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
1344 f->state = I40E_FILTER_FAILED;
1345 else
1346 f->state = I40E_FILTER_NEW;
Jacob Keller278e7d02016-10-05 09:30:37 -07001347 INIT_HLIST_NODE(&f->hlist);
1348
1349 key = i40e_addr_to_hkey(macaddr);
1350 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001351
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001352 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1353 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1354 }
1355
Jacob Keller1bc87e82016-10-05 09:30:31 -07001356 /* If we're asked to add a filter that has been marked for removal, it
1357 * is safe to simply restore it to active state. __i40e_del_filter
1358 * will have simply deleted any filters which were previously marked
1359 * NEW or FAILED, so if it is currently marked REMOVE it must have
1360 * previously been ACTIVE. Since we haven't yet run the sync filters
1361 * task, just restore this filter to the ACTIVE state so that the
1362 * sync task leaves it in place
1363 */
1364 if (f->state == I40E_FILTER_REMOVE)
1365 f->state = I40E_FILTER_ACTIVE;
1366
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001367 return f;
1368}
1369
1370/**
Jacob Keller290d2552016-10-05 09:30:36 -07001371 * __i40e_del_filter - Remove a specific filter from the VSI
1372 * @vsi: VSI to remove from
1373 * @f: the filter to remove from the list
1374 *
1375 * This function should be called instead of i40e_del_filter only if you know
1376 * the exact filter you will remove already, such as via i40e_find_filter or
1377 * i40e_find_mac.
Kiran Patil21659032015-09-30 14:09:03 -04001378 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001379 * NOTE: This function is expected to be called with mac_filter_hash_lock
Kiran Patil21659032015-09-30 14:09:03 -04001380 * being held.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001381 * ANOTHER NOTE: This function MUST be called from within the context of
1382 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1383 * instead of list_for_each_entry().
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001384 **/
Jacob Keller148141b2016-11-11 12:39:36 -08001385void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001386{
Jacob Keller1bc87e82016-10-05 09:30:31 -07001387 if (!f)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001388 return;
1389
Alan Bradya410c822016-12-12 15:44:07 -08001390 /* If the filter was never added to firmware then we can just delete it
1391 * directly and we don't want to set the status to remove or else an
1392 * admin queue command will unnecessarily fire.
1393 */
Jacob Keller1bc87e82016-10-05 09:30:31 -07001394 if ((f->state == I40E_FILTER_FAILED) ||
1395 (f->state == I40E_FILTER_NEW)) {
Jacob Keller278e7d02016-10-05 09:30:37 -07001396 hash_del(&f->hlist);
Jacob Keller1bc87e82016-10-05 09:30:31 -07001397 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001398 } else {
Jacob Keller1bc87e82016-10-05 09:30:31 -07001399 f->state = I40E_FILTER_REMOVE;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001400 }
Alan Bradya410c822016-12-12 15:44:07 -08001401
1402 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1403 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001404}
1405
1406/**
Jacob Keller290d2552016-10-05 09:30:36 -07001407 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1408 * @vsi: the VSI to be searched
1409 * @macaddr: the MAC address
1410 * @vlan: the VLAN
1411 *
Jacob Keller278e7d02016-10-05 09:30:37 -07001412 * NOTE: This function is expected to be called with mac_filter_hash_lock
Jacob Keller290d2552016-10-05 09:30:36 -07001413 * being held.
1414 * ANOTHER NOTE: This function MUST be called from within the context of
1415 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1416 * instead of list_for_each_entry().
1417 **/
1418void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1419{
1420 struct i40e_mac_filter *f;
1421
1422 if (!vsi || !macaddr)
1423 return;
1424
1425 f = i40e_find_filter(vsi, macaddr, vlan);
1426 __i40e_del_filter(vsi, f);
1427}
1428
1429/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001430 * i40e_add_mac_filter - Add a MAC filter for all active VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001431 * @vsi: the VSI to be searched
1432 * @macaddr: the mac address to be filtered
1433 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001434 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1435 * go through all the macvlan filters and add a macvlan filter for each
Jacob Keller5feb3d72016-10-05 09:30:34 -07001436 * unique vlan that already exists. If a PVID has been assigned, instead only
1437 * add the macaddr to that VLAN.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001438 *
Jacob Keller5feb3d72016-10-05 09:30:34 -07001439 * Returns last filter added on success, else NULL
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001440 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001441struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1442 const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001443{
Jacob Keller5feb3d72016-10-05 09:30:34 -07001444 struct i40e_mac_filter *f, *add = NULL;
Jacob Keller278e7d02016-10-05 09:30:37 -07001445 struct hlist_node *h;
1446 int bkt;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001447
1448 if (vsi->info.pvid)
1449 return i40e_add_filter(vsi, macaddr,
1450 le16_to_cpu(vsi->info.pvid));
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001451
Jacob Keller7aaf95362016-11-11 12:39:33 -08001452 if (!i40e_is_vsi_in_vlan(vsi))
1453 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1454
Jacob Keller278e7d02016-10-05 09:30:37 -07001455 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07001456 if (f->state == I40E_FILTER_REMOVE)
1457 continue;
Jacob Keller5feb3d72016-10-05 09:30:34 -07001458 add = i40e_add_filter(vsi, macaddr, f->vlan);
1459 if (!add)
1460 return NULL;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001461 }
1462
Jacob Keller5feb3d72016-10-05 09:30:34 -07001463 return add;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001464}
1465
1466/**
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001467 * i40e_del_mac_filter - Remove a MAC filter from all VLANs
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001468 * @vsi: the VSI to be searched
1469 * @macaddr: the mac address to be removed
1470 *
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001471 * Removes a given MAC address from a VSI regardless of what VLAN it has been
1472 * associated with.
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001473 *
1474 * Returns 0 for success, or error
1475 **/
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001476int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001477{
Jacob Keller278e7d02016-10-05 09:30:37 -07001478 struct i40e_mac_filter *f;
1479 struct hlist_node *h;
Jacob Keller290d2552016-10-05 09:30:36 -07001480 bool found = false;
Jacob Keller278e7d02016-10-05 09:30:37 -07001481 int bkt;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001482
Jacob Keller278e7d02016-10-05 09:30:37 -07001483 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
1484 "Missing mac_filter_hash_lock\n");
1485 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07001486 if (ether_addr_equal(macaddr, f->macaddr)) {
1487 __i40e_del_filter(vsi, f);
1488 found = true;
1489 }
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001490 }
Jacob Keller290d2552016-10-05 09:30:36 -07001491
1492 if (found)
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001493 return 0;
Jacob Keller290d2552016-10-05 09:30:36 -07001494 else
1495 return -ENOENT;
Jacob Keller35ec2ff2016-10-05 09:30:33 -07001496}
1497
1498/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001499 * i40e_set_mac - NDO callback to set mac address
1500 * @netdev: network interface device structure
1501 * @p: pointer to an address structure
1502 *
1503 * Returns 0 on success, negative on failure
1504 **/
1505static int i40e_set_mac(struct net_device *netdev, void *p)
1506{
1507 struct i40e_netdev_priv *np = netdev_priv(netdev);
1508 struct i40e_vsi *vsi = np->vsi;
Shannon Nelson30650cc2014-07-29 04:01:50 +00001509 struct i40e_pf *pf = vsi->back;
1510 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001511 struct sockaddr *addr = p;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001512
1513 if (!is_valid_ether_addr(addr->sa_data))
1514 return -EADDRNOTAVAIL;
1515
Shannon Nelson30650cc2014-07-29 04:01:50 +00001516 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1517 netdev_info(netdev, "already using mac address %pM\n",
1518 addr->sa_data);
1519 return 0;
1520 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001521
Anjali Singhai Jain80f64282013-11-28 06:39:47 +00001522 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1523 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1524 return -EADDRNOTAVAIL;
1525
Shannon Nelson30650cc2014-07-29 04:01:50 +00001526 if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1527 netdev_info(netdev, "returning to hw mac address %pM\n",
1528 hw->mac.addr);
1529 else
1530 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1531
Jacob Keller278e7d02016-10-05 09:30:37 -07001532 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001533 i40e_del_mac_filter(vsi, netdev->dev_addr);
1534 i40e_add_mac_filter(vsi, addr->sa_data);
Jacob Keller278e7d02016-10-05 09:30:37 -07001535 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001536 ether_addr_copy(netdev->dev_addr, addr->sa_data);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001537 if (vsi->type == I40E_VSI_MAIN) {
1538 i40e_status ret;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001539
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001540 ret = i40e_aq_mac_address_write(&vsi->back->hw,
Shannon Nelsoncc412222014-06-04 01:23:21 +00001541 I40E_AQC_WRITE_TYPE_LAA_WOL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001542 addr->sa_data, NULL);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001543 if (ret)
1544 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1545 i40e_stat_str(hw, ret),
1546 i40e_aq_str(hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001547 }
1548
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001549 /* schedule our worker thread which will take care of
1550 * applying the new filter changes
1551 */
1552 i40e_service_event_schedule(vsi->back);
1553 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001554}
1555
1556/**
1557 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1558 * @vsi: the VSI being setup
1559 * @ctxt: VSI context structure
1560 * @enabled_tc: Enabled TCs bitmap
1561 * @is_add: True if called before Add VSI
1562 *
1563 * Setup VSI queue mapping for enabled traffic classes.
1564 **/
1565static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1566 struct i40e_vsi_context *ctxt,
1567 u8 enabled_tc,
1568 bool is_add)
1569{
1570 struct i40e_pf *pf = vsi->back;
1571 u16 sections = 0;
1572 u8 netdev_tc = 0;
1573 u16 numtc = 0;
1574 u16 qcount;
1575 u8 offset;
1576 u16 qmap;
1577 int i;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001578 u16 num_tc_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001579
1580 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1581 offset = 0;
1582
1583 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1584 /* Find numtc from enabled TC bitmap */
1585 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001586 if (enabled_tc & BIT(i)) /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001587 numtc++;
1588 }
1589 if (!numtc) {
1590 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1591 numtc = 1;
1592 }
1593 } else {
1594 /* At least TC0 is enabled in case of non-DCB case */
1595 numtc = 1;
1596 }
1597
1598 vsi->tc_config.numtc = numtc;
1599 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001600 /* Number of queues per enabled TC */
Catherine Sullivan7d644022016-05-16 10:26:41 -07001601 qcount = vsi->alloc_queue_pairs;
1602
Anjali Singhai7f9ff472015-02-21 06:43:19 +00001603 num_tc_qps = qcount / numtc;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001604 num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001605
1606 /* Setup queue offset/count for all TCs for given VSI */
1607 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1608 /* See if the given TC is enabled for the given VSI */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08001609 if (vsi->tc_config.enabled_tc & BIT(i)) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001610 /* TC is enabled */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001611 int pow, num_qps;
1612
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001613 switch (vsi->type) {
1614 case I40E_VSI_MAIN:
Helin Zhangacd65442015-10-26 19:44:28 -04001615 qcount = min_t(int, pf->alloc_rss_size,
1616 num_tc_qps);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001617 break;
1618 case I40E_VSI_FDIR:
1619 case I40E_VSI_SRIOV:
1620 case I40E_VSI_VMDQ2:
1621 default:
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001622 qcount = num_tc_qps;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001623 WARN_ON(i != 0);
1624 break;
1625 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001626 vsi->tc_config.tc_info[i].qoffset = offset;
1627 vsi->tc_config.tc_info[i].qcount = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001628
Shannon Nelson1e200e42015-02-27 09:15:24 +00001629 /* find the next higher power-of-2 of num queue pairs */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001630 num_qps = qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001631 pow = 0;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001632 while (num_qps && (BIT_ULL(pow) < qcount)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001633 pow++;
1634 num_qps >>= 1;
1635 }
1636
1637 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1638 qmap =
1639 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1640 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1641
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08001642 offset += qcount;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001643 } else {
1644 /* TC is not enabled so set the offset to
1645 * default queue and allocate one queue
1646 * for the given TC.
1647 */
1648 vsi->tc_config.tc_info[i].qoffset = 0;
1649 vsi->tc_config.tc_info[i].qcount = 1;
1650 vsi->tc_config.tc_info[i].netdev_tc = 0;
1651
1652 qmap = 0;
1653 }
1654 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1655 }
1656
1657 /* Set actual Tx/Rx queue pairs */
1658 vsi->num_queue_pairs = offset;
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001659 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
1660 if (vsi->req_queue_pairs > 0)
1661 vsi->num_queue_pairs = vsi->req_queue_pairs;
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04001662 else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00001663 vsi->num_queue_pairs = pf->num_lan_msix;
1664 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001665
1666 /* Scheduler section valid can only be set for ADD VSI */
1667 if (is_add) {
1668 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1669
1670 ctxt->info.up_enable_bits = enabled_tc;
1671 }
1672 if (vsi->type == I40E_VSI_SRIOV) {
1673 ctxt->info.mapping_flags |=
1674 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1675 for (i = 0; i < vsi->num_queue_pairs; i++)
1676 ctxt->info.queue_mapping[i] =
1677 cpu_to_le16(vsi->base_queue + i);
1678 } else {
1679 ctxt->info.mapping_flags |=
1680 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1681 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1682 }
1683 ctxt->info.valid_sections |= cpu_to_le16(sections);
1684}
1685
1686/**
Jacob Keller6622f5c2016-10-05 09:30:32 -07001687 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1688 * @netdev: the netdevice
1689 * @addr: address to add
1690 *
1691 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1692 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1693 */
1694static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1695{
1696 struct i40e_netdev_priv *np = netdev_priv(netdev);
1697 struct i40e_vsi *vsi = np->vsi;
Jacob Keller6622f5c2016-10-05 09:30:32 -07001698
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001699 if (i40e_add_mac_filter(vsi, addr))
Jacob Keller6622f5c2016-10-05 09:30:32 -07001700 return 0;
1701 else
1702 return -ENOMEM;
1703}
1704
1705/**
1706 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1707 * @netdev: the netdevice
1708 * @addr: address to add
1709 *
1710 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1711 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1712 */
1713static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1714{
1715 struct i40e_netdev_priv *np = netdev_priv(netdev);
1716 struct i40e_vsi *vsi = np->vsi;
1717
Jacob Kellerfeffdbe2016-11-11 12:39:35 -08001718 i40e_del_mac_filter(vsi, addr);
Jacob Keller6622f5c2016-10-05 09:30:32 -07001719
1720 return 0;
1721}
1722
1723/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001724 * i40e_set_rx_mode - NDO callback to set the netdev filters
1725 * @netdev: network interface device structure
1726 **/
1727static void i40e_set_rx_mode(struct net_device *netdev)
1728{
1729 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001730 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001731
Jacob Keller278e7d02016-10-05 09:30:37 -07001732 spin_lock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04001733
Jacob Keller6622f5c2016-10-05 09:30:32 -07001734 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
1735 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001736
Jacob Keller278e7d02016-10-05 09:30:37 -07001737 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001738
1739 /* check for other flag changes */
1740 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1741 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1742 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1743 }
Jesse Brandeburgc53934c2016-01-04 10:33:06 -08001744
1745 /* schedule our worker thread which will take care of
1746 * applying the new filter changes
1747 */
1748 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001749}
1750
1751/**
Jacob Keller671889e2016-12-02 12:33:00 -08001752 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
Jacob Keller4a2ce272016-10-05 09:30:38 -07001753 * @vsi: Pointer to VSI struct
Kiran Patil21659032015-09-30 14:09:03 -04001754 * @from: Pointer to list which contains MAC filter entries - changes to
1755 * those entries needs to be undone.
1756 *
Jacob Keller671889e2016-12-02 12:33:00 -08001757 * MAC filter entries from this list were slated for deletion.
Kiran Patil21659032015-09-30 14:09:03 -04001758 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001759static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
1760 struct hlist_head *from)
Kiran Patil21659032015-09-30 14:09:03 -04001761{
Jacob Keller278e7d02016-10-05 09:30:37 -07001762 struct i40e_mac_filter *f;
1763 struct hlist_node *h;
Kiran Patil21659032015-09-30 14:09:03 -04001764
Jacob Keller278e7d02016-10-05 09:30:37 -07001765 hlist_for_each_entry_safe(f, h, from, hlist) {
1766 u64 key = i40e_addr_to_hkey(f->macaddr);
1767
Kiran Patil21659032015-09-30 14:09:03 -04001768 /* Move the element back into MAC filter list*/
Jacob Keller278e7d02016-10-05 09:30:37 -07001769 hlist_del(&f->hlist);
1770 hash_add(vsi->mac_filter_hash, &f->hlist, key);
Kiran Patil21659032015-09-30 14:09:03 -04001771 }
1772}
1773
1774/**
Jacob Keller671889e2016-12-02 12:33:00 -08001775 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
1776 * @vsi: Pointer to vsi struct
1777 * @from: Pointer to list which contains MAC filter entries - changes to
1778 * those entries needs to be undone.
1779 *
1780 * MAC filter entries from this list were slated for addition.
1781 **/
1782static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
1783 struct hlist_head *from)
1784{
1785 struct i40e_new_mac_filter *new;
1786 struct hlist_node *h;
1787
1788 hlist_for_each_entry_safe(new, h, from, hlist) {
1789 /* We can simply free the wrapper structure */
1790 hlist_del(&new->hlist);
1791 kfree(new);
1792 }
1793}
1794
1795/**
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001796 * i40e_next_entry - Get the next non-broadcast filter from a list
Jacob Keller671889e2016-12-02 12:33:00 -08001797 * @next: pointer to filter in list
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001798 *
1799 * Returns the next non-broadcast filter in the list. Required so that we
1800 * ignore broadcast filters within the list, since these are not handled via
1801 * the normal firmware update path.
1802 */
Jacob Keller671889e2016-12-02 12:33:00 -08001803static
1804struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001805{
Jacob Keller584a8872017-03-07 15:17:52 -08001806 hlist_for_each_entry_continue(next, hlist) {
1807 if (!is_broadcast_ether_addr(next->f->macaddr))
1808 return next;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001809 }
1810
Jacob Keller584a8872017-03-07 15:17:52 -08001811 return NULL;
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001812}
1813
1814/**
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001815 * i40e_update_filter_state - Update filter state based on return data
1816 * from firmware
1817 * @count: Number of filters added
1818 * @add_list: return data from fw
1819 * @head: pointer to first filter in current batch
Kiran Patil21659032015-09-30 14:09:03 -04001820 *
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001821 * MAC filter entries from list were slated to be added to device. Returns
1822 * number of successful filters. Note that 0 does NOT mean success!
Kiran Patil21659032015-09-30 14:09:03 -04001823 **/
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001824static int
1825i40e_update_filter_state(int count,
1826 struct i40e_aqc_add_macvlan_element_data *add_list,
Jacob Keller671889e2016-12-02 12:33:00 -08001827 struct i40e_new_mac_filter *add_head)
Kiran Patil21659032015-09-30 14:09:03 -04001828{
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001829 int retval = 0;
1830 int i;
Kiran Patil21659032015-09-30 14:09:03 -04001831
Jacob Kellerac9e2392016-11-11 12:39:27 -08001832 for (i = 0; i < count; i++) {
1833 /* Always check status of each filter. We don't need to check
1834 * the firmware return status because we pre-set the filter
1835 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
1836 * request to the adminq. Thus, if it no longer matches then
1837 * we know the filter is active.
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001838 */
Jacob Kellerac9e2392016-11-11 12:39:27 -08001839 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001840 add_head->state = I40E_FILTER_FAILED;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001841 } else {
1842 add_head->state = I40E_FILTER_ACTIVE;
1843 retval++;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001844 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001845
Jacob Kellerd88d40b2016-12-02 12:32:59 -08001846 add_head = i40e_next_filter(add_head);
1847 if (!add_head)
1848 break;
Kiran Patil21659032015-09-30 14:09:03 -04001849 }
Jacob Kellerac9e2392016-11-11 12:39:27 -08001850
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001851 return retval;
Kiran Patil21659032015-09-30 14:09:03 -04001852}
1853
1854/**
Jacob Keller00936312016-10-05 09:30:41 -07001855 * i40e_aqc_del_filters - Request firmware to delete a set of filters
1856 * @vsi: ptr to the VSI
1857 * @vsi_name: name to display in messages
1858 * @list: the list of filters to send to firmware
1859 * @num_del: the number of filters to delete
1860 * @retval: Set to -EIO on failure to delete
1861 *
1862 * Send a request to firmware via AdminQ to delete a set of filters. Uses
1863 * *retval instead of a return value so that success does not force ret_val to
1864 * be set to 0. This ensures that a sequence of calls to this function
1865 * preserve the previous value of *retval on successful delete.
1866 */
1867static
1868void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
1869 struct i40e_aqc_remove_macvlan_element_data *list,
1870 int num_del, int *retval)
1871{
1872 struct i40e_hw *hw = &vsi->back->hw;
1873 i40e_status aq_ret;
1874 int aq_err;
1875
1876 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
1877 aq_err = hw->aq.asq_last_status;
1878
1879 /* Explicitly ignore and do not report when firmware returns ENOENT */
1880 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
1881 *retval = -EIO;
1882 dev_info(&vsi->back->pdev->dev,
1883 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
1884 vsi_name, i40e_stat_str(hw, aq_ret),
1885 i40e_aq_str(hw, aq_err));
1886 }
1887}
1888
1889/**
1890 * i40e_aqc_add_filters - Request firmware to add a set of filters
1891 * @vsi: ptr to the VSI
1892 * @vsi_name: name to display in messages
1893 * @list: the list of filters to send to firmware
1894 * @add_head: Position in the add hlist
1895 * @num_add: the number of filters to add
1896 * @promisc_change: set to true on exit if promiscuous mode was forced on
1897 *
1898 * Send a request to firmware via AdminQ to add a chunk of filters. Will set
1899 * promisc_changed to true if the firmware has run out of space for more
1900 * filters.
1901 */
1902static
1903void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
1904 struct i40e_aqc_add_macvlan_element_data *list,
Jacob Keller671889e2016-12-02 12:33:00 -08001905 struct i40e_new_mac_filter *add_head,
Jacob Keller00936312016-10-05 09:30:41 -07001906 int num_add, bool *promisc_changed)
1907{
1908 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller00936312016-10-05 09:30:41 -07001909 int aq_err, fcnt;
1910
Jacob Kellerac9e2392016-11-11 12:39:27 -08001911 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
Jacob Keller00936312016-10-05 09:30:41 -07001912 aq_err = hw->aq.asq_last_status;
Jacob Kellerac9e2392016-11-11 12:39:27 -08001913 fcnt = i40e_update_filter_state(num_add, list, add_head);
Jacob Keller00936312016-10-05 09:30:41 -07001914
1915 if (fcnt != num_add) {
1916 *promisc_changed = true;
1917 set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller00936312016-10-05 09:30:41 -07001918 dev_warn(&vsi->back->pdev->dev,
1919 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
1920 i40e_aq_str(hw, aq_err),
1921 vsi_name);
1922 }
1923}
1924
1925/**
Jacob Keller435c0842016-11-08 13:05:10 -08001926 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
1927 * @vsi: pointer to the VSI
1928 * @f: filter data
1929 *
1930 * This function sets or clears the promiscuous broadcast flags for VLAN
1931 * filters in order to properly receive broadcast frames. Assumes that only
1932 * broadcast filters are passed.
Jacob Keller671889e2016-12-02 12:33:00 -08001933 *
1934 * Returns status indicating success or failure;
Jacob Keller435c0842016-11-08 13:05:10 -08001935 **/
Jacob Keller671889e2016-12-02 12:33:00 -08001936static i40e_status
1937i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
1938 struct i40e_mac_filter *f)
Jacob Keller435c0842016-11-08 13:05:10 -08001939{
1940 bool enable = f->state == I40E_FILTER_NEW;
1941 struct i40e_hw *hw = &vsi->back->hw;
1942 i40e_status aq_ret;
1943
1944 if (f->vlan == I40E_VLAN_ANY) {
1945 aq_ret = i40e_aq_set_vsi_broadcast(hw,
1946 vsi->seid,
1947 enable,
1948 NULL);
1949 } else {
1950 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
1951 vsi->seid,
1952 enable,
1953 f->vlan,
1954 NULL);
1955 }
1956
Jacob Keller671889e2016-12-02 12:33:00 -08001957 if (aq_ret)
Jacob Keller435c0842016-11-08 13:05:10 -08001958 dev_warn(&vsi->back->pdev->dev,
1959 "Error %s setting broadcast promiscuous mode on %s\n",
1960 i40e_aq_str(hw, hw->aq.asq_last_status),
1961 vsi_name);
Jacob Keller671889e2016-12-02 12:33:00 -08001962
1963 return aq_ret;
Jacob Keller435c0842016-11-08 13:05:10 -08001964}
1965
1966/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001967 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1968 * @vsi: ptr to the VSI
1969 *
1970 * Push any outstanding VSI filter changes through the AdminQ.
1971 *
1972 * Returns 0 or error value
1973 **/
Jesse Brandeburg17652c62015-11-05 17:01:02 -08001974int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001975{
Jacob Keller278e7d02016-10-05 09:30:37 -07001976 struct hlist_head tmp_add_list, tmp_del_list;
Jacob Keller671889e2016-12-02 12:33:00 -08001977 struct i40e_mac_filter *f;
1978 struct i40e_new_mac_filter *new, *add_head = NULL;
Mitch Williams3e25a8f2016-05-16 10:26:32 -07001979 struct i40e_hw *hw = &vsi->back->hw;
Jacob Keller38326212016-11-11 12:39:26 -08001980 unsigned int failed_filters = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001981 unsigned int vlan_filters = 0;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001982 bool promisc_changed = false;
Shannon Nelson2d1de822016-05-16 10:26:44 -07001983 char vsi_name[16] = "PF";
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001984 int filter_list_len = 0;
Mitch Williamsea02e902015-11-09 15:35:50 -08001985 i40e_status aq_ret = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001986 u32 changed_flags = 0;
Jacob Keller278e7d02016-10-05 09:30:37 -07001987 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001988 struct i40e_pf *pf;
1989 int num_add = 0;
1990 int num_del = 0;
Alan Brady84f5ca62016-10-05 09:30:39 -07001991 int retval = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001992 u16 cmd_flags;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07001993 int list_size;
Jacob Keller278e7d02016-10-05 09:30:37 -07001994 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00001995
1996 /* empty array typed pointers, kcalloc later */
1997 struct i40e_aqc_add_macvlan_element_data *add_list;
1998 struct i40e_aqc_remove_macvlan_element_data *del_list;
1999
2000 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
2001 usleep_range(1000, 2000);
2002 pf = vsi->back;
2003
2004 if (vsi->netdev) {
2005 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2006 vsi->current_netdev_flags = vsi->netdev->flags;
2007 }
2008
Jacob Keller278e7d02016-10-05 09:30:37 -07002009 INIT_HLIST_HEAD(&tmp_add_list);
2010 INIT_HLIST_HEAD(&tmp_del_list);
Kiran Patil21659032015-09-30 14:09:03 -04002011
Shannon Nelson2d1de822016-05-16 10:26:44 -07002012 if (vsi->type == I40E_VSI_SRIOV)
2013 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2014 else if (vsi->type != I40E_VSI_MAIN)
2015 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2016
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002017 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2018 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2019
Jacob Keller278e7d02016-10-05 09:30:37 -07002020 spin_lock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002021 /* Create a list of filters to delete. */
Jacob Keller278e7d02016-10-05 09:30:37 -07002022 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002023 if (f->state == I40E_FILTER_REMOVE) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002024 /* Move the element into temporary del_list */
Jacob Keller278e7d02016-10-05 09:30:37 -07002025 hash_del(&f->hlist);
2026 hlist_add_head(&f->hlist, &tmp_del_list);
Alan Brady84f5ca62016-10-05 09:30:39 -07002027
2028 /* Avoid counting removed filters */
2029 continue;
Kiran Patil21659032015-09-30 14:09:03 -04002030 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002031 if (f->state == I40E_FILTER_NEW) {
Jacob Keller671889e2016-12-02 12:33:00 -08002032 /* Create a temporary i40e_new_mac_filter */
2033 new = kzalloc(sizeof(*new), GFP_ATOMIC);
2034 if (!new)
2035 goto err_no_memory_locked;
2036
2037 /* Store pointer to the real filter */
2038 new->f = f;
2039 new->state = f->state;
2040
2041 /* Add it to the hash list */
2042 hlist_add_head(&new->hlist, &tmp_add_list);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002043 }
Alan Brady84f5ca62016-10-05 09:30:39 -07002044
Jacob Keller489a3262016-11-11 12:39:31 -08002045 /* Count the number of active (current and new) VLAN
2046 * filters we have now. Does not count filters which
2047 * are marked for deletion.
Alan Brady84f5ca62016-10-05 09:30:39 -07002048 */
2049 if (f->vlan > 0)
2050 vlan_filters++;
Alan Brady84f5ca62016-10-05 09:30:39 -07002051 }
2052
Jacob Keller489a3262016-11-11 12:39:31 -08002053 retval = i40e_correct_mac_vlan_filters(vsi,
2054 &tmp_add_list,
2055 &tmp_del_list,
2056 vlan_filters);
2057 if (retval)
2058 goto err_no_memory_locked;
Alan Brady84f5ca62016-10-05 09:30:39 -07002059
Jacob Keller278e7d02016-10-05 09:30:37 -07002060 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002061 }
2062
2063 /* Now process 'del_list' outside the lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07002064 if (!hlist_empty(&tmp_del_list)) {
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002065 filter_list_len = hw->aq.asq_buf_size /
Kiran Patil21659032015-09-30 14:09:03 -04002066 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002067 list_size = filter_list_len *
Shannon Nelsonf1199992015-11-19 11:34:23 -08002068 sizeof(struct i40e_aqc_remove_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002069 del_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002070 if (!del_list)
2071 goto err_no_memory;
Kiran Patil21659032015-09-30 14:09:03 -04002072
Jacob Keller278e7d02016-10-05 09:30:37 -07002073 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002074 cmd_flags = 0;
2075
Jacob Keller435c0842016-11-08 13:05:10 -08002076 /* handle broadcast filters by updating the broadcast
Jacob Kellerd88d40b2016-12-02 12:32:59 -08002077 * promiscuous flag and release filter list.
Jacob Keller435c0842016-11-08 13:05:10 -08002078 */
2079 if (is_broadcast_ether_addr(f->macaddr)) {
2080 i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2081
2082 hlist_del(&f->hlist);
2083 kfree(f);
2084 continue;
2085 }
2086
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002087 /* add to delete list */
Greg Rose9a173902014-05-22 06:32:02 +00002088 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002089 if (f->vlan == I40E_VLAN_ANY) {
2090 del_list[num_del].vlan_tag = 0;
Alan Bradya6cb9142016-09-06 18:05:07 -07002091 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002092 } else {
2093 del_list[num_del].vlan_tag =
2094 cpu_to_le16((u16)(f->vlan));
2095 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002096
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002097 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2098 del_list[num_del].flags = cmd_flags;
2099 num_del++;
2100
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002101 /* flush a full buffer */
2102 if (num_del == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002103 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2104 num_del, &retval);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002105 memset(del_list, 0, list_size);
Jacob Keller00936312016-10-05 09:30:41 -07002106 num_del = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002107 }
Kiran Patil21659032015-09-30 14:09:03 -04002108 /* Release memory for MAC filter entries which were
2109 * synced up with HW.
2110 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002111 hlist_del(&f->hlist);
Kiran Patil21659032015-09-30 14:09:03 -04002112 kfree(f);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002113 }
Kiran Patil21659032015-09-30 14:09:03 -04002114
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002115 if (num_del) {
Jacob Keller00936312016-10-05 09:30:41 -07002116 i40e_aqc_del_filters(vsi, vsi_name, del_list,
2117 num_del, &retval);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002118 }
2119
2120 kfree(del_list);
2121 del_list = NULL;
Kiran Patil21659032015-09-30 14:09:03 -04002122 }
2123
Jacob Keller278e7d02016-10-05 09:30:37 -07002124 if (!hlist_empty(&tmp_add_list)) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002125 /* Do all the adds now. */
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002126 filter_list_len = hw->aq.asq_buf_size /
Shannon Nelsonf1199992015-11-19 11:34:23 -08002127 sizeof(struct i40e_aqc_add_macvlan_element_data);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002128 list_size = filter_list_len *
2129 sizeof(struct i40e_aqc_add_macvlan_element_data);
2130 add_list = kzalloc(list_size, GFP_ATOMIC);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002131 if (!add_list)
2132 goto err_no_memory;
2133
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002134 num_add = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002135 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002136 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2137 &vsi->state)) {
Jacob Keller671889e2016-12-02 12:33:00 -08002138 new->state = I40E_FILTER_FAILED;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002139 continue;
2140 }
Jacob Keller435c0842016-11-08 13:05:10 -08002141
2142 /* handle broadcast filters by updating the broadcast
2143 * promiscuous flag instead of adding a MAC filter.
2144 */
Jacob Keller671889e2016-12-02 12:33:00 -08002145 if (is_broadcast_ether_addr(new->f->macaddr)) {
2146 if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2147 new->f))
2148 new->state = I40E_FILTER_FAILED;
2149 else
2150 new->state = I40E_FILTER_ACTIVE;
Jacob Keller435c0842016-11-08 13:05:10 -08002151 continue;
2152 }
2153
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002154 /* add to add array */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002155 if (num_add == 0)
Jacob Keller671889e2016-12-02 12:33:00 -08002156 add_head = new;
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002157 cmd_flags = 0;
Jacob Keller671889e2016-12-02 12:33:00 -08002158 ether_addr_copy(add_list[num_add].mac_addr,
2159 new->f->macaddr);
2160 if (new->f->vlan == I40E_VLAN_ANY) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002161 add_list[num_add].vlan_tag = 0;
2162 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2163 } else {
2164 add_list[num_add].vlan_tag =
Jacob Keller671889e2016-12-02 12:33:00 -08002165 cpu_to_le16((u16)(new->f->vlan));
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002166 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002167 add_list[num_add].queue_number = 0;
Jacob Kellerac9e2392016-11-11 12:39:27 -08002168 /* set invalid match method for later detection */
Keller, Jacob E0266ac42016-12-09 13:39:21 -08002169 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002170 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002171 add_list[num_add].flags = cpu_to_le16(cmd_flags);
2172 num_add++;
2173
2174 /* flush a full buffer */
2175 if (num_add == filter_list_len) {
Jacob Keller00936312016-10-05 09:30:41 -07002176 i40e_aqc_add_filters(vsi, vsi_name, add_list,
2177 add_head, num_add,
2178 &promisc_changed);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002179 memset(add_list, 0, list_size);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002180 num_add = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002181 }
2182 }
2183 if (num_add) {
Jacob Keller00936312016-10-05 09:30:41 -07002184 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2185 num_add, &promisc_changed);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002186 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002187 /* Now move all of the filters from the temp add list back to
2188 * the VSI's list.
2189 */
Jacob Keller278e7d02016-10-05 09:30:37 -07002190 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller671889e2016-12-02 12:33:00 -08002191 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2192 /* Only update the state if we're still NEW */
2193 if (new->f->state == I40E_FILTER_NEW)
2194 new->f->state = new->state;
2195 hlist_del(&new->hlist);
2196 kfree(new);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002197 }
Jacob Keller278e7d02016-10-05 09:30:37 -07002198 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002199 kfree(add_list);
2200 add_list = NULL;
2201 }
2202
Jacob Keller38326212016-11-11 12:39:26 -08002203 /* Determine the number of active and failed filters. */
2204 spin_lock_bh(&vsi->mac_filter_hash_lock);
2205 vsi->active_filters = 0;
2206 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2207 if (f->state == I40E_FILTER_ACTIVE)
2208 vsi->active_filters++;
2209 else if (f->state == I40E_FILTER_FAILED)
2210 failed_filters++;
2211 }
2212 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2213
2214 /* If promiscuous mode has changed, we need to calculate a new
2215 * threshold for when we are safe to exit
2216 */
2217 if (promisc_changed)
2218 vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2219
2220 /* Check if we are able to exit overflow promiscuous mode. We can
2221 * safely exit if we didn't just enter, we no longer have any failed
2222 * filters, and we have reduced filters below the threshold value.
2223 */
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002224 if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
Jacob Keller38326212016-11-11 12:39:26 -08002225 !promisc_changed && !failed_filters &&
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002226 (vsi->active_filters < vsi->promisc_threshold)) {
Jacob Keller38326212016-11-11 12:39:26 -08002227 dev_info(&pf->pdev->dev,
2228 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2229 vsi_name);
2230 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2231 promisc_changed = true;
2232 vsi->promisc_threshold = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002233 }
2234
Anjali Singhai Jaina856b5c2016-04-13 03:08:23 -07002235 /* if the VF is not trusted do not do promisc */
2236 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2237 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
2238 goto out;
2239 }
2240
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002241 /* check for changes in promiscuous modes */
2242 if (changed_flags & IFF_ALLMULTI) {
2243 bool cur_multipromisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002244
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002245 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
Mitch Williamsea02e902015-11-09 15:35:50 -08002246 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2247 vsi->seid,
2248 cur_multipromisc,
2249 NULL);
2250 if (aq_ret) {
2251 retval = i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002252 hw->aq.asq_last_status);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002253 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002254 "set multi promisc failed on %s, err %s aq_err %s\n",
2255 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002256 i40e_stat_str(hw, aq_ret),
2257 i40e_aq_str(hw, hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002258 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002259 }
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07002260 if ((changed_flags & IFF_PROMISC) ||
2261 (promisc_changed &&
2262 test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002263 bool cur_promisc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002264
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002265 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2266 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
2267 &vsi->state));
Anjali Singhai Jain6784ed52016-01-15 14:33:13 -08002268 if ((vsi->type == I40E_VSI_MAIN) &&
2269 (pf->lan_veb != I40E_NO_VEB) &&
2270 !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002271 /* set defport ON for Main VSI instead of true promisc
2272 * this way we will get all unicast/multicast and VLAN
2273 * promisc behavior but will not get VF or VMDq traffic
2274 * replicated on the Main VSI.
2275 */
2276 if (pf->cur_promisc != cur_promisc) {
2277 pf->cur_promisc = cur_promisc;
Mitch Williams5bc16032016-05-16 10:26:43 -07002278 if (cur_promisc)
2279 aq_ret =
2280 i40e_aq_set_default_vsi(hw,
2281 vsi->seid,
2282 NULL);
2283 else
2284 aq_ret =
2285 i40e_aq_clear_default_vsi(hw,
2286 vsi->seid,
2287 NULL);
2288 if (aq_ret) {
2289 retval = i40e_aq_rc_to_posix(aq_ret,
2290 hw->aq.asq_last_status);
2291 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002292 "Set default VSI failed on %s, err %s, aq_err %s\n",
2293 vsi_name,
Mitch Williams5bc16032016-05-16 10:26:43 -07002294 i40e_stat_str(hw, aq_ret),
2295 i40e_aq_str(hw,
2296 hw->aq.asq_last_status));
2297 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002298 }
2299 } else {
Mitch Williamsea02e902015-11-09 15:35:50 -08002300 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002301 hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002302 vsi->seid,
Anjali Singhai Jainb5569892016-05-03 15:13:12 -07002303 cur_promisc, NULL,
2304 true);
Mitch Williamsea02e902015-11-09 15:35:50 -08002305 if (aq_ret) {
2306 retval =
2307 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002308 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002309 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002310 "set unicast promisc failed on %s, err %s, aq_err %s\n",
2311 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002312 i40e_stat_str(hw, aq_ret),
2313 i40e_aq_str(hw,
2314 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002315 }
2316 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002317 hw,
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002318 vsi->seid,
2319 cur_promisc, NULL);
Mitch Williamsea02e902015-11-09 15:35:50 -08002320 if (aq_ret) {
2321 retval =
2322 i40e_aq_rc_to_posix(aq_ret,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002323 hw->aq.asq_last_status);
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002324 dev_info(&pf->pdev->dev,
Shannon Nelson2d1de822016-05-16 10:26:44 -07002325 "set multicast promisc failed on %s, err %s, aq_err %s\n",
2326 vsi_name,
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002327 i40e_stat_str(hw, aq_ret),
2328 i40e_aq_str(hw,
2329 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002330 }
Anjali Singhai Jain92faef82015-07-28 13:02:00 -04002331 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002332 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
2333 vsi->seid,
2334 cur_promisc, NULL);
2335 if (aq_ret) {
2336 retval = i40e_aq_rc_to_posix(aq_ret,
2337 pf->hw.aq.asq_last_status);
Greg Rose1a103702013-11-28 06:42:39 +00002338 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002339 "set brdcast promisc failed, err %s, aq_err %s\n",
Mitch Williams3e25a8f2016-05-16 10:26:32 -07002340 i40e_stat_str(hw, aq_ret),
2341 i40e_aq_str(hw,
2342 hw->aq.asq_last_status));
Mitch Williamsea02e902015-11-09 15:35:50 -08002343 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002344 }
Mitch Williamsea02e902015-11-09 15:35:50 -08002345out:
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08002346 /* if something went wrong then set the changed flag so we try again */
2347 if (retval)
2348 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2349
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002350 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
Mitch Williamsea02e902015-11-09 15:35:50 -08002351 return retval;
Jacob Keller4a2ce272016-10-05 09:30:38 -07002352
2353err_no_memory:
2354 /* Restore elements on the temporary add and delete lists */
2355 spin_lock_bh(&vsi->mac_filter_hash_lock);
Alan Brady84f5ca62016-10-05 09:30:39 -07002356err_no_memory_locked:
Jacob Keller671889e2016-12-02 12:33:00 -08002357 i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2358 i40e_undo_add_filter_entries(vsi, &tmp_add_list);
Jacob Keller4a2ce272016-10-05 09:30:38 -07002359 spin_unlock_bh(&vsi->mac_filter_hash_lock);
2360
2361 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2362 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
2363 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002364}
2365
2366/**
2367 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2368 * @pf: board private structure
2369 **/
2370static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2371{
2372 int v;
2373
2374 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
2375 return;
2376 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
2377
Mitch Williams505682c2014-05-20 08:01:37 +00002378 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002379 if (pf->vsi[v] &&
Jesse Brandeburg17652c62015-11-05 17:01:02 -08002380 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
2381 int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2382
2383 if (ret) {
2384 /* come back and try again later */
2385 pf->flags |= I40E_FLAG_FILTER_SYNC;
2386 break;
2387 }
2388 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002389 }
2390}
2391
2392/**
2393 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2394 * @netdev: network interface device structure
2395 * @new_mtu: new value for maximum frame size
2396 *
2397 * Returns 0 on success, negative on failure
2398 **/
2399static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2400{
2401 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002402 struct i40e_vsi *vsi = np->vsi;
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002403 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002404
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002405 netdev_info(netdev, "changing MTU from %d to %d\n",
2406 netdev->mtu, new_mtu);
2407 netdev->mtu = new_mtu;
2408 if (netif_running(netdev))
2409 i40e_vsi_reinit_locked(vsi);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08002410 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
2411 I40E_FLAG_CLIENT_L2_CHANGE);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002412 return 0;
2413}
2414
2415/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002416 * i40e_ioctl - Access the hwtstamp interface
2417 * @netdev: network interface device structure
2418 * @ifr: interface request data
2419 * @cmd: ioctl command
2420 **/
2421int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2422{
2423 struct i40e_netdev_priv *np = netdev_priv(netdev);
2424 struct i40e_pf *pf = np->vsi->back;
2425
2426 switch (cmd) {
2427 case SIOCGHWTSTAMP:
2428 return i40e_ptp_get_ts_config(pf, ifr);
2429 case SIOCSHWTSTAMP:
2430 return i40e_ptp_set_ts_config(pf, ifr);
2431 default:
2432 return -EOPNOTSUPP;
2433 }
2434}
2435
2436/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002437 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2438 * @vsi: the vsi being adjusted
2439 **/
2440void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2441{
2442 struct i40e_vsi_context ctxt;
2443 i40e_status ret;
2444
2445 if ((vsi->info.valid_sections &
2446 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2447 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2448 return; /* already enabled */
2449
2450 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2451 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2452 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2453
2454 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002455 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002456 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2457 if (ret) {
2458 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002459 "update vlan stripping failed, err %s aq_err %s\n",
2460 i40e_stat_str(&vsi->back->hw, ret),
2461 i40e_aq_str(&vsi->back->hw,
2462 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002463 }
2464}
2465
2466/**
2467 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2468 * @vsi: the vsi being adjusted
2469 **/
2470void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2471{
2472 struct i40e_vsi_context ctxt;
2473 i40e_status ret;
2474
2475 if ((vsi->info.valid_sections &
2476 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2477 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2478 I40E_AQ_VSI_PVLAN_EMOD_MASK))
2479 return; /* already disabled */
2480
2481 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2482 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2483 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2484
2485 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002486 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002487 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2488 if (ret) {
2489 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002490 "update vlan stripping failed, err %s aq_err %s\n",
2491 i40e_stat_str(&vsi->back->hw, ret),
2492 i40e_aq_str(&vsi->back->hw,
2493 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002494 }
2495}
2496
2497/**
2498 * i40e_vlan_rx_register - Setup or shutdown vlan offload
2499 * @netdev: network interface to be adjusted
2500 * @features: netdev features to test if VLAN offload is enabled or not
2501 **/
2502static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
2503{
2504 struct i40e_netdev_priv *np = netdev_priv(netdev);
2505 struct i40e_vsi *vsi = np->vsi;
2506
2507 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2508 i40e_vlan_stripping_enable(vsi);
2509 else
2510 i40e_vlan_stripping_disable(vsi);
2511}
2512
2513/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002514 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002515 * @vsi: the vsi being configured
2516 * @vid: vlan id to be added (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002517 *
2518 * This is a helper function for adding a new MAC/VLAN filter with the
2519 * specified VLAN for each existing MAC address already in the hash table.
2520 * This function does *not* perform any accounting to update filters based on
2521 * VLAN mode.
2522 *
2523 * NOTE: this function expects to be called while under the
2524 * mac_filter_hash_lock
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002525 **/
Jacob Keller9af52f62016-11-11 12:39:30 -08002526int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002527{
Jacob Keller490a4ad2016-11-11 12:39:29 -08002528 struct i40e_mac_filter *f, *add_f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002529 struct hlist_node *h;
2530 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002531
Jacob Keller278e7d02016-10-05 09:30:37 -07002532 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller57b341d2016-10-05 09:30:35 -07002533 if (f->state == I40E_FILTER_REMOVE)
2534 continue;
Jacob Keller1bc87e82016-10-05 09:30:31 -07002535 add_f = i40e_add_filter(vsi, f->macaddr, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002536 if (!add_f) {
2537 dev_info(&vsi->back->pdev->dev,
2538 "Could not add vlan filter %d for %pM\n",
2539 vid, f->macaddr);
2540 return -ENOMEM;
2541 }
2542 }
2543
Jacob Keller490a4ad2016-11-11 12:39:29 -08002544 return 0;
2545}
2546
2547/**
2548 * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2549 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002550 * @vid: VLAN id to be added
Jacob Keller490a4ad2016-11-11 12:39:29 -08002551 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002552int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002553{
Jacob Keller489a3262016-11-11 12:39:31 -08002554 int err;
Jacob Keller490a4ad2016-11-11 12:39:29 -08002555
Jacob Kellerf94484b2016-12-07 14:05:34 -08002556 if (!vid || vsi->info.pvid)
2557 return -EINVAL;
2558
Jacob Keller490a4ad2016-11-11 12:39:29 -08002559 /* Locked once because all functions invoked below iterates list*/
2560 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller490a4ad2016-11-11 12:39:29 -08002561 err = i40e_add_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002562 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller489a3262016-11-11 12:39:31 -08002563 if (err)
2564 return err;
Kiran Patil21659032015-09-30 14:09:03 -04002565
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002566 /* schedule our worker thread which will take care of
2567 * applying the new filter changes
2568 */
2569 i40e_service_event_schedule(vsi->back);
2570 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002571}
2572
2573/**
Jacob Keller490a4ad2016-11-11 12:39:29 -08002574 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002575 * @vsi: the vsi being configured
2576 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002577 *
2578 * This function should be used to remove all VLAN filters which match the
2579 * given VID. It does not schedule the service event and does not take the
2580 * mac_filter_hash_lock so it may be combined with other operations under
2581 * a single invocation of the mac_filter_hash_lock.
2582 *
2583 * NOTE: this function expects to be called while under the
2584 * mac_filter_hash_lock
2585 */
Jacob Keller9af52f62016-11-11 12:39:30 -08002586void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002587{
Alan Brady84f5ca62016-10-05 09:30:39 -07002588 struct i40e_mac_filter *f;
Jacob Keller278e7d02016-10-05 09:30:37 -07002589 struct hlist_node *h;
Jacob Keller278e7d02016-10-05 09:30:37 -07002590 int bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002591
Jacob Keller278e7d02016-10-05 09:30:37 -07002592 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Jacob Keller290d2552016-10-05 09:30:36 -07002593 if (f->vlan == vid)
2594 __i40e_del_filter(vsi, f);
2595 }
Jacob Keller490a4ad2016-11-11 12:39:29 -08002596}
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002597
Jacob Keller490a4ad2016-11-11 12:39:29 -08002598/**
2599 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2600 * @vsi: the VSI being configured
Jacob Kellerf94484b2016-12-07 14:05:34 -08002601 * @vid: VLAN id to be removed
Jacob Keller490a4ad2016-11-11 12:39:29 -08002602 **/
Jacob Kellerf94484b2016-12-07 14:05:34 -08002603void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
Jacob Keller490a4ad2016-11-11 12:39:29 -08002604{
Jacob Kellerf94484b2016-12-07 14:05:34 -08002605 if (!vid || vsi->info.pvid)
2606 return;
2607
Jacob Keller490a4ad2016-11-11 12:39:29 -08002608 spin_lock_bh(&vsi->mac_filter_hash_lock);
2609 i40e_rm_vlan_all_mac(vsi, vid);
Jacob Keller278e7d02016-10-05 09:30:37 -07002610 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04002611
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002612 /* schedule our worker thread which will take care of
2613 * applying the new filter changes
2614 */
2615 i40e_service_event_schedule(vsi->back);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002616}
2617
2618/**
2619 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2620 * @netdev: network interface to be adjusted
2621 * @vid: vlan id to be added
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002622 *
2623 * net_device_ops implementation for adding vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002624 **/
2625static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2626 __always_unused __be16 proto, u16 vid)
2627{
2628 struct i40e_netdev_priv *np = netdev_priv(netdev);
2629 struct i40e_vsi *vsi = np->vsi;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002630 int ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002631
Jacob Keller6a1127852016-10-25 16:08:49 -07002632 if (vid >= VLAN_N_VID)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002633 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002634
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002635 /* If the network stack called us with vid = 0 then
2636 * it is asking to receive priority tagged packets with
2637 * vlan id 0. Our HW receives them by default when configured
2638 * to receive untagged packets so there is no need to add an
2639 * extra filter for vlan 0 tagged packets.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002640 */
Anjali Singhai Jain6982d422014-02-06 05:51:10 +00002641 if (vid)
2642 ret = i40e_vsi_add_vlan(vsi, vid);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002643
Jacob Keller6a1127852016-10-25 16:08:49 -07002644 if (!ret)
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002645 set_bit(vid, vsi->active_vlans);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002646
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002647 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002648}
2649
2650/**
2651 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2652 * @netdev: network interface to be adjusted
2653 * @vid: vlan id to be removed
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002654 *
Akeem G Abodunrinfdfd9432014-02-11 08:24:15 +00002655 * net_device_ops implementation for removing vlan ids
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002656 **/
2657static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2658 __always_unused __be16 proto, u16 vid)
2659{
2660 struct i40e_netdev_priv *np = netdev_priv(netdev);
2661 struct i40e_vsi *vsi = np->vsi;
2662
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002663 /* return code is ignored as there is nothing a user
2664 * can do about failure to remove and a log message was
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002665 * already printed from the other function
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002666 */
2667 i40e_vsi_kill_vlan(vsi, vid);
2668
2669 clear_bit(vid, vsi->active_vlans);
Jesse Brandeburg078b5872013-09-25 23:41:14 +00002670
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002671 return 0;
2672}
2673
2674/**
Tushar Daveb1b15df2016-07-01 10:11:20 -07002675 * i40e_macaddr_init - explicitly write the mac address filters
2676 *
2677 * @vsi: pointer to the vsi
2678 * @macaddr: the MAC address
2679 *
2680 * This is needed when the macaddr has been obtained by other
2681 * means than the default, e.g., from Open Firmware or IDPROM.
2682 * Returns 0 on success, negative on failure
2683 **/
2684static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
2685{
2686 int ret;
2687 struct i40e_aqc_add_macvlan_element_data element;
2688
2689 ret = i40e_aq_mac_address_write(&vsi->back->hw,
2690 I40E_AQC_WRITE_TYPE_LAA_WOL,
2691 macaddr, NULL);
2692 if (ret) {
2693 dev_info(&vsi->back->pdev->dev,
2694 "Addr change for VSI failed: %d\n", ret);
2695 return -EADDRNOTAVAIL;
2696 }
2697
2698 memset(&element, 0, sizeof(element));
2699 ether_addr_copy(element.mac_addr, macaddr);
2700 element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
2701 ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
2702 if (ret) {
2703 dev_info(&vsi->back->pdev->dev,
2704 "add filter failed err %s aq_err %s\n",
2705 i40e_stat_str(&vsi->back->hw, ret),
2706 i40e_aq_str(&vsi->back->hw,
2707 vsi->back->hw.aq.asq_last_status));
2708 }
2709 return ret;
2710}
2711
2712/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002713 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
2714 * @vsi: the vsi being brought back up
2715 **/
2716static void i40e_restore_vlan(struct i40e_vsi *vsi)
2717{
2718 u16 vid;
2719
2720 if (!vsi->netdev)
2721 return;
2722
2723 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
2724
2725 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
2726 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2727 vid);
2728}
2729
2730/**
2731 * i40e_vsi_add_pvid - Add pvid for the VSI
2732 * @vsi: the vsi being adjusted
2733 * @vid: the vlan id to set as a PVID
2734 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002735int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002736{
2737 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002738 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002739
2740 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2741 vsi->info.pvid = cpu_to_le16(vid);
Greg Rose6c12fcb2013-11-28 06:39:34 +00002742 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2743 I40E_AQ_VSI_PVLAN_INSERT_PVID |
Greg Roseb774c7d2013-11-28 06:39:44 +00002744 I40E_AQ_VSI_PVLAN_EMOD_STR;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002745
2746 ctxt.seid = vsi->seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07002747 ctxt.info = vsi->info;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002748 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2749 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002750 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04002751 "add pvid failed, err %s aq_err %s\n",
2752 i40e_stat_str(&vsi->back->hw, ret),
2753 i40e_aq_str(&vsi->back->hw,
2754 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002755 return -ENOENT;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002756 }
2757
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00002758 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002759}
2760
2761/**
2762 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2763 * @vsi: the vsi being adjusted
2764 *
2765 * Just use the vlan_rx_register() service to put it back to normal
2766 **/
2767void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2768{
Greg Rose6c12fcb2013-11-28 06:39:34 +00002769 i40e_vlan_stripping_disable(vsi);
2770
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002771 vsi->info.pvid = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002772}
2773
2774/**
2775 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2776 * @vsi: ptr to the VSI
2777 *
2778 * If this function returns with an error, then it's possible one or
2779 * more of the rings is populated (while the rest are not). It is the
2780 * callers duty to clean those orphaned rings.
2781 *
2782 * Return 0 on success, negative on failure
2783 **/
2784static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2785{
2786 int i, err = 0;
2787
2788 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002789 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002790
2791 return err;
2792}
2793
2794/**
2795 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2796 * @vsi: ptr to the VSI
2797 *
2798 * Free VSI's transmit software resources
2799 **/
2800static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2801{
2802 int i;
2803
Greg Rose8e9dca52013-12-18 13:45:53 +00002804 if (!vsi->tx_rings)
2805 return;
2806
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002807 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002808 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002809 i40e_free_tx_resources(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002810}
2811
2812/**
2813 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2814 * @vsi: ptr to the VSI
2815 *
2816 * If this function returns with an error, then it's possible one or
2817 * more of the rings is populated (while the rest are not). It is the
2818 * callers duty to clean those orphaned rings.
2819 *
2820 * Return 0 on success, negative on failure
2821 **/
2822static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2823{
2824 int i, err = 0;
2825
2826 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002827 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002828 return err;
2829}
2830
2831/**
2832 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2833 * @vsi: ptr to the VSI
2834 *
2835 * Free all receive software resources
2836 **/
2837static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2838{
2839 int i;
2840
Greg Rose8e9dca52013-12-18 13:45:53 +00002841 if (!vsi->rx_rings)
2842 return;
2843
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002844 for (i = 0; i < vsi->num_queue_pairs; i++)
Greg Rose8e9dca52013-12-18 13:45:53 +00002845 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
Alexander Duyck9f65e152013-09-28 06:00:58 +00002846 i40e_free_rx_resources(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002847}
2848
2849/**
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002850 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
2851 * @ring: The Tx ring to configure
2852 *
2853 * This enables/disables XPS for a given Tx descriptor ring
2854 * based on the TCs enabled for the VSI that ring belongs to.
2855 **/
2856static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
2857{
2858 struct i40e_vsi *vsi = ring->vsi;
2859 cpumask_var_t mask;
2860
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002861 if (!ring->q_vector || !ring->netdev)
2862 return;
2863
2864 /* Single TC mode enable XPS */
2865 if (vsi->tc_config.numtc <= 1) {
2866 if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002867 netif_set_xps_queue(ring->netdev,
2868 &ring->q_vector->affinity_mask,
2869 ring->queue_index);
Jesse Brandeburg9a660ee2015-02-26 16:13:22 +00002870 } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
2871 /* Disable XPS to allow selection based on TC */
2872 bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
2873 netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
2874 free_cpumask_var(mask);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002875 }
Jesse Brandeburg0e4425e2015-11-05 17:01:01 -08002876
2877 /* schedule our worker thread which will take care of
2878 * applying the new filter changes
2879 */
2880 i40e_service_event_schedule(vsi->back);
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002881}
2882
2883/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002884 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2885 * @ring: The Tx ring to configure
2886 *
2887 * Configure the Tx descriptor ring in the HMC context.
2888 **/
2889static int i40e_configure_tx_ring(struct i40e_ring *ring)
2890{
2891 struct i40e_vsi *vsi = ring->vsi;
2892 u16 pf_q = vsi->base_queue + ring->queue_index;
2893 struct i40e_hw *hw = &vsi->back->hw;
2894 struct i40e_hmc_obj_txq tx_ctx;
2895 i40e_status err = 0;
2896 u32 qtx_ctl = 0;
2897
2898 /* some ATR related tx ring init */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002899 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002900 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2901 ring->atr_count = 0;
2902 } else {
2903 ring->atr_sample_rate = 0;
2904 }
2905
Neerav Parikh3ffa0372014-11-12 00:19:02 +00002906 /* configure XPS */
2907 i40e_config_xps_tx_ring(ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002908
2909 /* clear the context structure first */
2910 memset(&tx_ctx, 0, sizeof(tx_ctx));
2911
2912 tx_ctx.new_context = 1;
2913 tx_ctx.base = (ring->dma / 128);
2914 tx_ctx.qlen = ring->count;
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08002915 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
2916 I40E_FLAG_FD_ATR_ENABLED));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00002917 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
Jesse Brandeburg1943d8b2014-02-14 02:14:40 +00002918 /* FDIR VSI tx ring can still use RS bit and writebacks */
2919 if (vsi->type != I40E_VSI_FDIR)
2920 tx_ctx.head_wb_ena = 1;
2921 tx_ctx.head_wb_addr = ring->dma +
2922 (ring->count * sizeof(struct i40e_tx_desc));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002923
2924 /* As part of VSI creation/update, FW allocates certain
2925 * Tx arbitration queue sets for each TC enabled for
2926 * the VSI. The FW returns the handles to these queue
2927 * sets as part of the response buffer to Add VSI,
2928 * Update VSI, etc. AQ commands. It is expected that
2929 * these queue set handles be associated with the Tx
2930 * queues by the driver as part of the TX queue context
2931 * initialization. This has to be done regardless of
2932 * DCB as by default everything is mapped to TC0.
2933 */
2934 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2935 tx_ctx.rdylist_act = 0;
2936
2937 /* clear the context in the HMC */
2938 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2939 if (err) {
2940 dev_info(&vsi->back->pdev->dev,
2941 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2942 ring->queue_index, pf_q, err);
2943 return -ENOMEM;
2944 }
2945
2946 /* set the context in the HMC */
2947 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2948 if (err) {
2949 dev_info(&vsi->back->pdev->dev,
2950 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2951 ring->queue_index, pf_q, err);
2952 return -ENOMEM;
2953 }
2954
2955 /* Now associate this queue with this PCI function */
Mitch Williams7a28d882014-10-17 03:14:52 +00002956 if (vsi->type == I40E_VSI_VMDQ2) {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002957 qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002958 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
2959 I40E_QTX_CTL_VFVM_INDX_MASK;
2960 } else {
Shannon Nelson9d8bf542014-01-14 00:49:50 -08002961 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
Mitch Williams7a28d882014-10-17 03:14:52 +00002962 }
2963
Shannon Nelson13fd977492013-09-28 07:14:19 +00002964 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2965 I40E_QTX_CTL_PF_INDX_MASK);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002966 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2967 i40e_flush(hw);
2968
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002969 /* cache tail off for easier writes later */
2970 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2971
2972 return 0;
2973}
2974
2975/**
2976 * i40e_configure_rx_ring - Configure a receive ring context
2977 * @ring: The Rx ring to configure
2978 *
2979 * Configure the Rx descriptor ring in the HMC context.
2980 **/
2981static int i40e_configure_rx_ring(struct i40e_ring *ring)
2982{
2983 struct i40e_vsi *vsi = ring->vsi;
2984 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2985 u16 pf_q = vsi->base_queue + ring->queue_index;
2986 struct i40e_hw *hw = &vsi->back->hw;
2987 struct i40e_hmc_obj_rxq rx_ctx;
2988 i40e_status err = 0;
2989
2990 ring->state = 0;
2991
2992 /* clear the context structure first */
2993 memset(&rx_ctx, 0, sizeof(rx_ctx));
2994
2995 ring->rx_buf_len = vsi->rx_buf_len;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002996
Alexander Duyckdab86af2017-03-14 10:15:27 -07002997 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
2998 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00002999
3000 rx_ctx.base = (ring->dma / 128);
3001 rx_ctx.qlen = ring->count;
3002
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003003 /* use 32 byte descriptors */
3004 rx_ctx.dsize = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003005
Jesse Brandeburgbec60fc2016-04-18 11:33:47 -07003006 /* descriptor type is always zero
3007 * rx_ctx.dtype = 0;
3008 */
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003009 rx_ctx.hsplit_0 = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003010
Jesse Brandeburgb32bfa12016-04-18 11:33:42 -07003011 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00003012 if (hw->revision_id == 0)
3013 rx_ctx.lrxqthresh = 0;
3014 else
3015 rx_ctx.lrxqthresh = 2;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003016 rx_ctx.crcstrip = 1;
3017 rx_ctx.l2tsel = 1;
Jesse Brandeburgc4bbac32015-09-28 11:21:48 -07003018 /* this controls whether VLAN is stripped from inner headers */
3019 rx_ctx.showiv = 0;
Catherine Sullivanacb36762014-03-06 09:02:30 +00003020 /* set the prefena field to 1 because the manual says to */
3021 rx_ctx.prefena = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003022
3023 /* clear the context in the HMC */
3024 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3025 if (err) {
3026 dev_info(&vsi->back->pdev->dev,
3027 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3028 ring->queue_index, pf_q, err);
3029 return -ENOMEM;
3030 }
3031
3032 /* set the context in the HMC */
3033 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3034 if (err) {
3035 dev_info(&vsi->back->pdev->dev,
3036 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3037 ring->queue_index, pf_q, err);
3038 return -ENOMEM;
3039 }
3040
3041 /* cache tail for quicker writes, and clear the reg before use */
3042 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3043 writel(0, ring->tail);
3044
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -07003045 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003046
3047 return 0;
3048}
3049
3050/**
3051 * i40e_vsi_configure_tx - Configure the VSI for Tx
3052 * @vsi: VSI structure describing this set of rings and resources
3053 *
3054 * Configure the Tx VSI for operation.
3055 **/
3056static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3057{
3058 int err = 0;
3059 u16 i;
3060
Alexander Duyck9f65e152013-09-28 06:00:58 +00003061 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3062 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003063
3064 return err;
3065}
3066
3067/**
3068 * i40e_vsi_configure_rx - Configure the VSI for Rx
3069 * @vsi: the VSI being configured
3070 *
3071 * Configure the Rx VSI for operation.
3072 **/
3073static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3074{
3075 int err = 0;
3076 u16 i;
3077
Alexander Duyckdab86af2017-03-14 10:15:27 -07003078 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3079 vsi->max_frame = I40E_MAX_RXBUFFER;
3080 vsi->rx_buf_len = I40E_RXBUFFER_2048;
3081#if (PAGE_SIZE < 8192)
3082 } else if (vsi->netdev->mtu <= ETH_DATA_LEN) {
3083 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3084 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3085#endif
3086 } else {
3087 vsi->max_frame = I40E_MAX_RXBUFFER;
Alexander Duyck98efd692017-04-05 07:51:01 -04003088 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3089 I40E_RXBUFFER_2048;
Alexander Duyckdab86af2017-03-14 10:15:27 -07003090 }
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 Jain5e8230662013-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 Jain5e8230662013-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 Jain5e8230662013-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 Jain5e8230662013-12-18 13:45:49 +00003672 ret = IRQ_HANDLED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00003673
Anjali Singhai Jain5e8230662013-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 Jain5e8230662013-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 Sullivan7c1220072014-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 Sullivan7c1220072014-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 *
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004444 * Wait until all queues on a given VSI have been 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++) {
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004453 /* Check and wait for the Tx queue */
Neerav Parikh69129dc2014-11-12 00:18:46 +00004454 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 }
Jacob Kelleraf26ce22017-04-05 07:50:58 -04004461 /* Check and wait for the Tx queue */
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004462 ret = i40e_pf_rxq_wait(pf, pf_q, false);
4463 if (ret) {
4464 dev_info(&pf->pdev->dev,
4465 "VSI seid %d Rx ring %d disable timeout\n",
4466 vsi->seid, pf_q);
4467 return ret;
4468 }
4469 }
4470
Neerav Parikh69129dc2014-11-12 00:18:46 +00004471 return 0;
4472}
4473
4474/**
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004475 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
Neerav Parikh69129dc2014-11-12 00:18:46 +00004476 * @pf: the PF
4477 *
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004478 * This function waits for the queues to be in disabled state for all the
Neerav Parikh69129dc2014-11-12 00:18:46 +00004479 * VSIs that are managed by this PF.
4480 **/
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004481static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
Neerav Parikh69129dc2014-11-12 00:18:46 +00004482{
4483 int v, ret = 0;
4484
4485 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08004486 if (pf->vsi[v]) {
Neerav Parikh3fe06f42016-02-17 16:12:15 -08004487 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
Neerav Parikh69129dc2014-11-12 00:18:46 +00004488 if (ret)
4489 break;
4490 }
4491 }
4492
4493 return ret;
4494}
4495
4496#endif
Kiran Patilb03a8c12015-09-24 18:13:15 -04004497
4498/**
4499 * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
4500 * @q_idx: TX queue number
4501 * @vsi: Pointer to VSI struct
4502 *
4503 * This function checks specified queue for given VSI. Detects hung condition.
Alan Brady17daabb2017-04-05 07:50:56 -04004504 * We proactively detect hung TX queues by checking if interrupts are disabled
4505 * but there are pending descriptors. If it appears hung, attempt to recover
4506 * by triggering a SW interrupt.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004507 **/
4508static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
4509{
4510 struct i40e_ring *tx_ring = NULL;
4511 struct i40e_pf *pf;
Alan Brady17daabb2017-04-05 07:50:56 -04004512 u32 val, tx_pending;
Kiran Patilb03a8c12015-09-24 18:13:15 -04004513 int i;
4514
4515 pf = vsi->back;
4516
4517 /* now that we have an index, find the tx_ring struct */
4518 for (i = 0; i < vsi->num_queue_pairs; i++) {
4519 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
4520 if (q_idx == vsi->tx_rings[i]->queue_index) {
4521 tx_ring = vsi->tx_rings[i];
4522 break;
4523 }
4524 }
4525 }
4526
4527 if (!tx_ring)
4528 return;
4529
4530 /* Read interrupt register */
4531 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4532 val = rd32(&pf->hw,
4533 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
4534 tx_ring->vsi->base_vector - 1));
4535 else
4536 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
4537
Alan Brady17daabb2017-04-05 07:50:56 -04004538 tx_pending = i40e_get_tx_pending(tx_ring);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004539
Alan Brady17daabb2017-04-05 07:50:56 -04004540 /* Interrupts are disabled and TX pending is non-zero,
4541 * trigger the SW interrupt (don't wait). Worst case
4542 * there will be one extra interrupt which may result
4543 * into not cleaning any queues because queues are cleaned.
Kiran Patilb03a8c12015-09-24 18:13:15 -04004544 */
Alan Brady17daabb2017-04-05 07:50:56 -04004545 if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
4546 i40e_force_wb(vsi, tx_ring->q_vector);
Kiran Patilb03a8c12015-09-24 18:13:15 -04004547}
4548
4549/**
4550 * i40e_detect_recover_hung - Function to detect and recover hung_queues
4551 * @pf: pointer to PF struct
4552 *
4553 * LAN VSI has netdev and netdev has TX queues. This function is to check
4554 * each of those TX queues if they are hung, trigger recovery by issuing
4555 * SW interrupt.
4556 **/
4557static void i40e_detect_recover_hung(struct i40e_pf *pf)
4558{
4559 struct net_device *netdev;
4560 struct i40e_vsi *vsi;
4561 int i;
4562
4563 /* Only for LAN VSI */
4564 vsi = pf->vsi[pf->lan_vsi];
4565
4566 if (!vsi)
4567 return;
4568
4569 /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
4570 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
4571 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
4572 return;
4573
4574 /* Make sure type is MAIN VSI */
4575 if (vsi->type != I40E_VSI_MAIN)
4576 return;
4577
4578 netdev = vsi->netdev;
4579 if (!netdev)
4580 return;
4581
4582 /* Bail out if netif_carrier is not OK */
4583 if (!netif_carrier_ok(netdev))
4584 return;
4585
4586 /* Go thru' TX queues for netdev */
4587 for (i = 0; i < netdev->num_tx_queues; i++) {
4588 struct netdev_queue *q;
4589
4590 q = netdev_get_tx_queue(netdev, i);
4591 if (q)
4592 i40e_detect_recover_hung_queue(i, vsi);
4593 }
4594}
4595
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004596/**
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004597 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00004598 * @pf: pointer to PF
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004599 *
4600 * Get TC map for ISCSI PF type that will include iSCSI TC
4601 * and LAN TC.
4602 **/
4603static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
4604{
4605 struct i40e_dcb_app_priority_table app;
4606 struct i40e_hw *hw = &pf->hw;
4607 u8 enabled_tc = 1; /* TC0 is always enabled */
4608 u8 tc, i;
4609 /* Get the iSCSI APP TLV */
4610 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4611
4612 for (i = 0; i < dcbcfg->numapps; i++) {
4613 app = dcbcfg->app[i];
4614 if (app.selector == I40E_APP_SEL_TCPIP &&
4615 app.protocolid == I40E_APP_PROTOID_ISCSI) {
4616 tc = dcbcfg->etscfg.prioritytable[app.priority];
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004617 enabled_tc |= BIT(tc);
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004618 break;
4619 }
4620 }
4621
4622 return enabled_tc;
4623}
4624
4625/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004626 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
4627 * @dcbcfg: the corresponding DCBx configuration structure
4628 *
4629 * Return the number of TCs from given DCBx configuration
4630 **/
4631static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
4632{
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004633 int i, tc_unused = 0;
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004634 u8 num_tc = 0;
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004635 u8 ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004636
4637 /* Scan the ETS Config Priority Table to find
4638 * traffic class enabled for a given priority
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004639 * and create a bitmask of enabled TCs
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004640 */
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004641 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4642 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
4643
4644 /* Now scan the bitmask to check for
4645 * contiguous TCs starting with TC0
4646 */
4647 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4648 if (num_tc & BIT(i)) {
4649 if (!tc_unused) {
4650 ret++;
4651 } else {
4652 pr_err("Non-contiguous TC - Disabling DCB\n");
4653 return 1;
4654 }
4655 } else {
4656 tc_unused = 1;
4657 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004658 }
4659
Dave Ertmanfbfe12c2016-08-12 09:56:32 -07004660 /* There is always at least TC0 */
4661 if (!ret)
4662 ret = 1;
4663
4664 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004665}
4666
4667/**
4668 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
4669 * @dcbcfg: the corresponding DCBx configuration structure
4670 *
4671 * Query the current DCB configuration and return the number of
4672 * traffic classes enabled from the given DCBX config
4673 **/
4674static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
4675{
4676 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
4677 u8 enabled_tc = 1;
4678 u8 i;
4679
4680 for (i = 0; i < num_tc; i++)
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04004681 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004682
4683 return enabled_tc;
4684}
4685
4686/**
4687 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
4688 * @pf: PF being queried
4689 *
4690 * Return number of traffic classes enabled for the given PF
4691 **/
4692static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
4693{
4694 struct i40e_hw *hw = &pf->hw;
Dave Ertman52a08ca2016-07-27 12:02:34 -07004695 u8 i, enabled_tc = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004696 u8 num_tc = 0;
4697 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4698
4699 /* If DCB is not enabled then always in single TC */
4700 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
4701 return 1;
4702
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004703 /* SFP mode will be enabled for all TCs on port */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004704 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4705 return i40e_dcb_get_num_tc(dcbcfg);
4706
4707 /* MFP mode return count of enabled TCs for this PF */
4708 if (pf->hw.func_caps.iscsi)
4709 enabled_tc = i40e_get_iscsi_tc_map(pf);
4710 else
Neerav Parikhfc51de92015-02-24 06:58:53 +00004711 return 1; /* Only TC0 */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004712
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004713 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004714 if (enabled_tc & BIT(i))
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004715 num_tc++;
4716 }
4717 return num_tc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004718}
4719
4720/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004721 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
4722 * @pf: PF being queried
4723 *
4724 * Return a bitmap for enabled traffic classes for this PF.
4725 **/
4726static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
4727{
4728 /* If DCB is not enabled for this PF then just return default TC */
4729 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
David Ertmanea6acb72016-09-20 07:10:50 -07004730 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004731
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004732 /* SFP mode we want PF to be enabled for all TCs */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004733 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
4734 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
4735
Neerav Parikhfc51de92015-02-24 06:58:53 +00004736 /* MFP enabled and iSCSI PF type */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00004737 if (pf->hw.func_caps.iscsi)
4738 return i40e_get_iscsi_tc_map(pf);
4739 else
David Ertmanea6acb72016-09-20 07:10:50 -07004740 return I40E_DEFAULT_TRAFFIC_CLASS;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004741}
4742
4743/**
4744 * i40e_vsi_get_bw_info - Query VSI BW Information
4745 * @vsi: the VSI being queried
4746 *
4747 * Returns 0 on success, negative value on failure
4748 **/
4749static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
4750{
4751 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
4752 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
4753 struct i40e_pf *pf = vsi->back;
4754 struct i40e_hw *hw = &pf->hw;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004755 i40e_status ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004756 u32 tc_bw_max;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004757 int i;
4758
4759 /* Get the VSI level BW configuration */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004760 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
4761 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004762 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004763 "couldn't get PF vsi bw config, err %s aq_err %s\n",
4764 i40e_stat_str(&pf->hw, ret),
4765 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004766 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004767 }
4768
4769 /* Get the VSI level BW configuration per TC */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004770 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
4771 NULL);
4772 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004773 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004774 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
4775 i40e_stat_str(&pf->hw, ret),
4776 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004777 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004778 }
4779
4780 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
4781 dev_info(&pf->pdev->dev,
4782 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
4783 bw_config.tc_valid_bits,
4784 bw_ets_config.tc_valid_bits);
4785 /* Still continuing */
4786 }
4787
4788 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
4789 vsi->bw_max_quanta = bw_config.max_bw;
4790 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
4791 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
4792 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4793 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
4794 vsi->bw_ets_limit_credits[i] =
4795 le16_to_cpu(bw_ets_config.credits[i]);
4796 /* 3 bits out of 4 for each TC */
4797 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
4798 }
Jesse Brandeburg078b5872013-09-25 23:41:14 +00004799
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004800 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004801}
4802
4803/**
4804 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
4805 * @vsi: the VSI being configured
4806 * @enabled_tc: TC bitmap
4807 * @bw_credits: BW shared credits per TC
4808 *
4809 * Returns 0 on success, negative value on failure
4810 **/
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004811static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004812 u8 *bw_share)
4813{
4814 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004815 i40e_status ret;
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004816 int i;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004817
4818 bw_data.tc_valid_bits = enabled_tc;
4819 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4820 bw_data.tc_bw_credits[i] = bw_share[i];
4821
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004822 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
4823 NULL);
4824 if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004825 dev_info(&vsi->back->pdev->dev,
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00004826 "AQ command Config VSI BW allocation per TC failed = %d\n",
4827 vsi->back->hw.aq.asq_last_status);
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004828 return -EINVAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004829 }
4830
4831 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
4832 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
4833
Jesse Brandeburgdcae29b2013-09-13 08:23:20 +00004834 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004835}
4836
4837/**
4838 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
4839 * @vsi: the VSI being configured
4840 * @enabled_tc: TC map to be enabled
4841 *
4842 **/
4843static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4844{
4845 struct net_device *netdev = vsi->netdev;
4846 struct i40e_pf *pf = vsi->back;
4847 struct i40e_hw *hw = &pf->hw;
4848 u8 netdev_tc = 0;
4849 int i;
4850 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
4851
4852 if (!netdev)
4853 return;
4854
4855 if (!enabled_tc) {
4856 netdev_reset_tc(netdev);
4857 return;
4858 }
4859
4860 /* Set up actual enabled TCs on the VSI */
4861 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
4862 return;
4863
4864 /* set per TC queues for the VSI */
4865 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
4866 /* Only set TC queues for enabled tcs
4867 *
4868 * e.g. For a VSI that has TC0 and TC3 enabled the
4869 * enabled_tc bitmap would be 0x00001001; the driver
4870 * will set the numtc for netdev as 2 that will be
4871 * referenced by the netdev layer as TC 0 and 1.
4872 */
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004873 if (vsi->tc_config.enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004874 netdev_set_tc_queue(netdev,
4875 vsi->tc_config.tc_info[i].netdev_tc,
4876 vsi->tc_config.tc_info[i].qcount,
4877 vsi->tc_config.tc_info[i].qoffset);
4878 }
4879
4880 /* Assign UP2TC map for the VSI */
4881 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
4882 /* Get the actual TC# for the UP */
4883 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
4884 /* Get the mapped netdev TC# for the UP */
4885 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
4886 netdev_set_prio_tc_map(netdev, i, netdev_tc);
4887 }
4888}
4889
4890/**
4891 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
4892 * @vsi: the VSI being configured
4893 * @ctxt: the ctxt buffer returned from AQ VSI update param command
4894 **/
4895static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
4896 struct i40e_vsi_context *ctxt)
4897{
4898 /* copy just the sections touched not the entire info
4899 * since not all sections are valid as returned by
4900 * update vsi params
4901 */
4902 vsi->info.mapping_flags = ctxt->info.mapping_flags;
4903 memcpy(&vsi->info.queue_mapping,
4904 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
4905 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
4906 sizeof(vsi->info.tc_mapping));
4907}
4908
4909/**
4910 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
4911 * @vsi: VSI to be configured
4912 * @enabled_tc: TC bitmap
4913 *
4914 * This configures a particular VSI for TCs that are mapped to the
4915 * given TC bitmap. It uses default bandwidth share for TCs across
4916 * VSIs to configure TC for a particular VSI.
4917 *
4918 * NOTE:
4919 * It is expected that the VSI queues have been quisced before calling
4920 * this function.
4921 **/
4922static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
4923{
4924 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
4925 struct i40e_vsi_context ctxt;
4926 int ret = 0;
4927 int i;
4928
4929 /* Check if enabled_tc is same as existing or new TCs */
4930 if (vsi->tc_config.enabled_tc == enabled_tc)
4931 return ret;
4932
4933 /* Enable ETS TCs with equal BW Share for now across all VSIs */
4934 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08004935 if (enabled_tc & BIT(i))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004936 bw_share[i] = 1;
4937 }
4938
4939 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
4940 if (ret) {
4941 dev_info(&vsi->back->pdev->dev,
4942 "Failed configuring TC map %d for VSI %d\n",
4943 enabled_tc, vsi->seid);
4944 goto out;
4945 }
4946
4947 /* Update Queue Pairs Mapping for currently enabled UPs */
4948 ctxt.seid = vsi->seid;
4949 ctxt.pf_num = vsi->back->hw.pf_id;
4950 ctxt.vf_num = 0;
4951 ctxt.uplink_seid = vsi->uplink_seid;
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07004952 ctxt.info = vsi->info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004953 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
4954
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06004955 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
4956 ctxt.info.valid_sections |=
4957 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
4958 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
4959 }
4960
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004961 /* Update the VSI after updating the VSI queue-mapping information */
4962 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
4963 if (ret) {
4964 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004965 "Update vsi tc config failed, err %s aq_err %s\n",
4966 i40e_stat_str(&vsi->back->hw, ret),
4967 i40e_aq_str(&vsi->back->hw,
4968 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004969 goto out;
4970 }
4971 /* update the local VSI info with updated queue map */
4972 i40e_vsi_update_queue_map(vsi, &ctxt);
4973 vsi->info.valid_sections = 0;
4974
4975 /* Update current VSI BW information */
4976 ret = i40e_vsi_get_bw_info(vsi);
4977 if (ret) {
4978 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04004979 "Failed updating vsi bw info, err %s aq_err %s\n",
4980 i40e_stat_str(&vsi->back->hw, ret),
4981 i40e_aq_str(&vsi->back->hw,
4982 vsi->back->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00004983 goto out;
4984 }
4985
4986 /* Update the netdev TC setup */
4987 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
4988out:
4989 return ret;
4990}
4991
4992/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08004993 * i40e_veb_config_tc - Configure TCs for given VEB
4994 * @veb: given VEB
4995 * @enabled_tc: TC bitmap
4996 *
4997 * Configures given TC bitmap for VEB (switching) element
4998 **/
4999int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
5000{
5001 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
5002 struct i40e_pf *pf = veb->pf;
5003 int ret = 0;
5004 int i;
5005
5006 /* No TCs or already enabled TCs just return */
5007 if (!enabled_tc || veb->enabled_tc == enabled_tc)
5008 return ret;
5009
5010 bw_data.tc_valid_bits = enabled_tc;
5011 /* bw_data.absolute_credits is not set (relative) */
5012
5013 /* Enable ETS TCs with equal BW Share for now */
5014 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005015 if (enabled_tc & BIT(i))
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005016 bw_data.tc_bw_share_credits[i] = 1;
5017 }
5018
5019 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
5020 &bw_data, NULL);
5021 if (ret) {
5022 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005023 "VEB bw config failed, err %s aq_err %s\n",
5024 i40e_stat_str(&pf->hw, ret),
5025 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005026 goto out;
5027 }
5028
5029 /* Update the BW information */
5030 ret = i40e_veb_get_bw_info(veb);
5031 if (ret) {
5032 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005033 "Failed getting veb bw config, err %s aq_err %s\n",
5034 i40e_stat_str(&pf->hw, ret),
5035 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005036 }
5037
5038out:
5039 return ret;
5040}
5041
5042#ifdef CONFIG_I40E_DCB
5043/**
5044 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
5045 * @pf: PF struct
5046 *
5047 * Reconfigure VEB/VSIs on a given PF; it is assumed that
5048 * the caller would've quiesce all the VSIs before calling
5049 * this function
5050 **/
5051static void i40e_dcb_reconfigure(struct i40e_pf *pf)
5052{
5053 u8 tc_map = 0;
5054 int ret;
5055 u8 v;
5056
5057 /* Enable the TCs available on PF to all VEBs */
5058 tc_map = i40e_pf_get_tc_map(pf);
5059 for (v = 0; v < I40E_MAX_VEB; v++) {
5060 if (!pf->veb[v])
5061 continue;
5062 ret = i40e_veb_config_tc(pf->veb[v], tc_map);
5063 if (ret) {
5064 dev_info(&pf->pdev->dev,
5065 "Failed configuring TC for VEB seid=%d\n",
5066 pf->veb[v]->seid);
5067 /* Will try to configure as many components */
5068 }
5069 }
5070
5071 /* Update each VSI */
Mitch Williams505682c2014-05-20 08:01:37 +00005072 for (v = 0; v < pf->num_alloc_vsi; v++) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005073 if (!pf->vsi[v])
5074 continue;
5075
5076 /* - Enable all TCs for the LAN VSI
5077 * - For all others keep them at TC0 for now
5078 */
5079 if (v == pf->lan_vsi)
5080 tc_map = i40e_pf_get_tc_map(pf);
5081 else
David Ertmanea6acb72016-09-20 07:10:50 -07005082 tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005083
5084 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
5085 if (ret) {
5086 dev_info(&pf->pdev->dev,
5087 "Failed configuring TC for VSI seid=%d\n",
5088 pf->vsi[v]->seid);
5089 /* Will try to configure as many components */
5090 } else {
Neerav Parikh0672a092014-04-01 07:11:47 +00005091 /* Re-configure VSI vectors based on updated TC map */
5092 i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005093 if (pf->vsi[v]->netdev)
5094 i40e_dcbnl_set_all(pf->vsi[v]);
5095 }
5096 }
5097}
5098
5099/**
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005100 * i40e_resume_port_tx - Resume port Tx
5101 * @pf: PF struct
5102 *
5103 * Resume a port's Tx and issue a PF reset in case of failure to
5104 * resume.
5105 **/
5106static int i40e_resume_port_tx(struct i40e_pf *pf)
5107{
5108 struct i40e_hw *hw = &pf->hw;
5109 int ret;
5110
5111 ret = i40e_aq_resume_port_tx(hw, NULL);
5112 if (ret) {
5113 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005114 "Resume Port Tx failed, err %s aq_err %s\n",
5115 i40e_stat_str(&pf->hw, ret),
5116 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005117 /* Schedule PF reset to recover */
5118 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5119 i40e_service_event_schedule(pf);
5120 }
5121
5122 return ret;
5123}
5124
5125/**
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005126 * i40e_init_pf_dcb - Initialize DCB configuration
5127 * @pf: PF being configured
5128 *
5129 * Query the current DCB configuration and cache it
5130 * in the hardware structure
5131 **/
5132static int i40e_init_pf_dcb(struct i40e_pf *pf)
5133{
5134 struct i40e_hw *hw = &pf->hw;
5135 int err = 0;
5136
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005137 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
Neerav Parikhf1bbad32016-01-13 16:51:39 -08005138 if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00005139 goto out;
5140
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005141 /* Get the initial DCB configuration */
5142 err = i40e_init_dcb(hw);
5143 if (!err) {
5144 /* Device/Function is not DCBX capable */
5145 if ((!hw->func_caps.dcb) ||
5146 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
5147 dev_info(&pf->pdev->dev,
5148 "DCBX offload is not supported or is disabled for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005149 } else {
5150 /* When status is not DISABLED then DCBX in FW */
5151 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
5152 DCB_CAP_DCBX_VER_IEEE;
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005153
5154 pf->flags |= I40E_FLAG_DCB_CAPABLE;
Dave Ertmana0362442016-08-29 17:38:26 -07005155 /* Enable DCB tagging only when more than one TC
5156 * or explicitly disable if only one TC
5157 */
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005158 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
5159 pf->flags |= I40E_FLAG_DCB_ENABLED;
Dave Ertmana0362442016-08-29 17:38:26 -07005160 else
5161 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005162 dev_dbg(&pf->pdev->dev,
5163 "DCBX offload is supported for this PF.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005164 }
Neerav Parikh014269f2014-04-01 07:11:48 +00005165 } else {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00005166 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005167 "Query for DCB configuration failed, err %s aq_err %s\n",
5168 i40e_stat_str(&pf->hw, err),
5169 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005170 }
5171
5172out:
5173 return err;
5174}
5175#endif /* CONFIG_I40E_DCB */
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005176#define SPEED_SIZE 14
5177#define FC_SIZE 8
5178/**
5179 * i40e_print_link_message - print link up or down
5180 * @vsi: the VSI for which link needs a message
5181 */
Matt Jaredc156f852015-08-27 11:42:39 -04005182void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005183{
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005184 enum i40e_aq_link_speed new_speed;
Shannon Nelsona9165492015-09-03 17:19:00 -04005185 char *speed = "Unknown";
5186 char *fc = "Unknown";
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005187 char *fec = "";
5188 char *an = "";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005189
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005190 new_speed = vsi->back->hw.phy.link_info.link_speed;
5191
5192 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
Matt Jaredc156f852015-08-27 11:42:39 -04005193 return;
5194 vsi->current_isup = isup;
Filip Sadowski7ec9ba12016-11-08 13:05:13 -08005195 vsi->current_speed = new_speed;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005196 if (!isup) {
5197 netdev_info(vsi->netdev, "NIC Link is Down\n");
5198 return;
5199 }
5200
Greg Rose148c2d82014-12-11 07:06:27 +00005201 /* Warn user if link speed on NPAR enabled partition is not at
5202 * least 10GB
5203 */
5204 if (vsi->back->hw.func_caps.npar_enable &&
5205 (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
5206 vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
5207 netdev_warn(vsi->netdev,
5208 "The partition detected link speed that is less than 10Gbps\n");
5209
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005210 switch (vsi->back->hw.phy.link_info.link_speed) {
5211 case I40E_LINK_SPEED_40GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005212 speed = "40 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005213 break;
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005214 case I40E_LINK_SPEED_20GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005215 speed = "20 G";
Jesse Brandeburgae24b402015-03-27 00:12:09 -07005216 break;
Carolyn Wyborny31232372016-11-21 13:03:48 -08005217 case I40E_LINK_SPEED_25GB:
5218 speed = "25 G";
5219 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005220 case I40E_LINK_SPEED_10GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005221 speed = "10 G";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005222 break;
5223 case I40E_LINK_SPEED_1GB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005224 speed = "1000 M";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005225 break;
Mitch Williams5960d332014-09-13 07:40:47 +00005226 case I40E_LINK_SPEED_100MB:
Shannon Nelsona9165492015-09-03 17:19:00 -04005227 speed = "100 M";
Mitch Williams5960d332014-09-13 07:40:47 +00005228 break;
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005229 default:
5230 break;
5231 }
5232
5233 switch (vsi->back->hw.fc.current_mode) {
5234 case I40E_FC_FULL:
Shannon Nelsona9165492015-09-03 17:19:00 -04005235 fc = "RX/TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005236 break;
5237 case I40E_FC_TX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005238 fc = "TX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005239 break;
5240 case I40E_FC_RX_PAUSE:
Shannon Nelsona9165492015-09-03 17:19:00 -04005241 fc = "RX";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005242 break;
5243 default:
Shannon Nelsona9165492015-09-03 17:19:00 -04005244 fc = "None";
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005245 break;
5246 }
5247
Henry Tieman3e03d7c2016-12-02 12:32:57 -08005248 if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
5249 fec = ", FEC: None";
5250 an = ", Autoneg: False";
5251
5252 if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
5253 an = ", Autoneg: True";
5254
5255 if (vsi->back->hw.phy.link_info.fec_info &
5256 I40E_AQ_CONFIG_FEC_KR_ENA)
5257 fec = ", FEC: CL74 FC-FEC/BASE-R";
5258 else if (vsi->back->hw.phy.link_info.fec_info &
5259 I40E_AQ_CONFIG_FEC_RS_ENA)
5260 fec = ", FEC: CL108 RS-FEC";
5261 }
5262
5263 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
5264 speed, fec, an, fc);
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005265}
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005266
5267/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005268 * i40e_up_complete - Finish the last steps of bringing up a connection
5269 * @vsi: the VSI being configured
5270 **/
5271static int i40e_up_complete(struct i40e_vsi *vsi)
5272{
5273 struct i40e_pf *pf = vsi->back;
5274 int err;
5275
5276 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5277 i40e_vsi_configure_msix(vsi);
5278 else
5279 i40e_configure_msi_and_legacy(vsi);
5280
5281 /* start rings */
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005282 err = i40e_vsi_start_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005283 if (err)
5284 return err;
5285
5286 clear_bit(__I40E_DOWN, &vsi->state);
5287 i40e_napi_enable_all(vsi);
5288 i40e_vsi_enable_irq(vsi);
5289
5290 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
5291 (vsi->netdev)) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005292 i40e_print_link_message(vsi, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005293 netif_tx_start_all_queues(vsi->netdev);
5294 netif_carrier_on(vsi->netdev);
Anjali Singhai6d779b42013-09-28 06:00:02 +00005295 } else if (vsi->netdev) {
Jesse Brandeburgcf05ed02014-04-23 04:50:12 +00005296 i40e_print_link_message(vsi, false);
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00005297 /* need to check for qualified module here*/
5298 if ((pf->hw.phy.link_info.link_info &
5299 I40E_AQ_MEDIA_AVAILABLE) &&
5300 (!(pf->hw.phy.link_info.an_info &
5301 I40E_AQ_QUALIFIED_MODULE)))
5302 netdev_err(vsi->netdev,
5303 "the driver failed to link because an unqualified module was detected.");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005304 }
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005305
5306 /* replay FDIR SB filters */
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005307 if (vsi->type == I40E_VSI_FDIR) {
5308 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08005309 pf->fd_add_err = 0;
5310 pf->fd_atr_cnt = 0;
Anjali Singhai Jainca64fa42014-02-11 08:26:30 +00005311 i40e_fdir_filter_restore(vsi);
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005312 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005313
5314 /* On the next run of the service_task, notify any clients of the new
5315 * opened netdev
5316 */
5317 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005318 i40e_service_event_schedule(pf);
5319
5320 return 0;
5321}
5322
5323/**
5324 * i40e_vsi_reinit_locked - Reset the VSI
5325 * @vsi: the VSI being configured
5326 *
5327 * Rebuild the ring structs after some configuration
5328 * has changed, e.g. MTU size.
5329 **/
5330static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
5331{
5332 struct i40e_pf *pf = vsi->back;
5333
5334 WARN_ON(in_interrupt());
5335 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
5336 usleep_range(1000, 2000);
5337 i40e_down(vsi);
5338
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005339 i40e_up(vsi);
5340 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
5341}
5342
5343/**
5344 * i40e_up - Bring the connection back up after being down
5345 * @vsi: the VSI being configured
5346 **/
5347int i40e_up(struct i40e_vsi *vsi)
5348{
5349 int err;
5350
5351 err = i40e_vsi_configure(vsi);
5352 if (!err)
5353 err = i40e_up_complete(vsi);
5354
5355 return err;
5356}
5357
5358/**
5359 * i40e_down - Shutdown the connection processing
5360 * @vsi: the VSI being stopped
5361 **/
5362void i40e_down(struct i40e_vsi *vsi)
5363{
5364 int i;
5365
5366 /* It is assumed that the caller of this function
5367 * sets the vsi->state __I40E_DOWN bit.
5368 */
5369 if (vsi->netdev) {
5370 netif_carrier_off(vsi->netdev);
5371 netif_tx_disable(vsi->netdev);
5372 }
5373 i40e_vsi_disable_irq(vsi);
Filip Sadowski3aa7b742016-10-11 15:26:58 -07005374 i40e_vsi_stop_rings(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005375 i40e_napi_disable_all(vsi);
5376
5377 for (i = 0; i < vsi->num_queue_pairs; i++) {
Alexander Duyck9f65e152013-09-28 06:00:58 +00005378 i40e_clean_tx_ring(vsi->tx_rings[i]);
5379 i40e_clean_rx_ring(vsi->rx_rings[i]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005380 }
Catherine Sullivanf980d442016-05-16 10:26:34 -07005381
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005382}
5383
5384/**
5385 * i40e_setup_tc - configure multiple traffic classes
5386 * @netdev: net device to configure
5387 * @tc: number of traffic classes to enable
5388 **/
5389static int i40e_setup_tc(struct net_device *netdev, u8 tc)
5390{
5391 struct i40e_netdev_priv *np = netdev_priv(netdev);
5392 struct i40e_vsi *vsi = np->vsi;
5393 struct i40e_pf *pf = vsi->back;
5394 u8 enabled_tc = 0;
5395 int ret = -EINVAL;
5396 int i;
5397
5398 /* Check if DCB enabled to continue */
5399 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5400 netdev_info(netdev, "DCB is not enabled for adapter\n");
5401 goto exit;
5402 }
5403
5404 /* Check if MFP enabled */
5405 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
5406 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
5407 goto exit;
5408 }
5409
5410 /* Check whether tc count is within enabled limit */
5411 if (tc > i40e_pf_get_num_tc(pf)) {
5412 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
5413 goto exit;
5414 }
5415
5416 /* Generate TC map for number of tc requested */
5417 for (i = 0; i < tc; i++)
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08005418 enabled_tc |= BIT(i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005419
5420 /* Requesting same TC configuration as already enabled */
5421 if (enabled_tc == vsi->tc_config.enabled_tc)
5422 return 0;
5423
5424 /* Quiesce VSI queues */
5425 i40e_quiesce_vsi(vsi);
5426
5427 /* Configure VSI for enabled TCs */
5428 ret = i40e_vsi_config_tc(vsi, enabled_tc);
5429 if (ret) {
5430 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
5431 vsi->seid);
5432 goto exit;
5433 }
5434
5435 /* Unquiesce VSI */
5436 i40e_unquiesce_vsi(vsi);
5437
5438exit:
5439 return ret;
5440}
5441
John Fastabend16e5cc62016-02-16 21:16:43 -08005442static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
5443 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08005444{
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005445 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005446 return -EINVAL;
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07005447
5448 tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
5449
5450 return i40e_setup_tc(netdev, tc->mqprio->num_tc);
John Fastabende4c67342016-02-16 21:16:15 -08005451}
5452
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005453/**
5454 * i40e_open - Called when a network interface is made active
5455 * @netdev: network interface device structure
5456 *
5457 * The open entry point is called when a network interface is made
5458 * active by the system (IFF_UP). At this point all resources needed
5459 * for transmit and receive operations are allocated, the interrupt
5460 * handler is registered with the OS, the netdev watchdog subtask is
5461 * enabled, and the stack is notified that the interface is ready.
5462 *
5463 * Returns 0 on success, negative value on failure
5464 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005465int i40e_open(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005466{
5467 struct i40e_netdev_priv *np = netdev_priv(netdev);
5468 struct i40e_vsi *vsi = np->vsi;
5469 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005470 int err;
5471
Shannon Nelson4eb3f762014-03-06 08:59:58 +00005472 /* disallow open during test or if eeprom is broken */
5473 if (test_bit(__I40E_TESTING, &pf->state) ||
5474 test_bit(__I40E_BAD_EEPROM, &pf->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005475 return -EBUSY;
5476
5477 netif_carrier_off(netdev);
5478
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005479 err = i40e_vsi_open(vsi);
5480 if (err)
5481 return err;
5482
Jesse Brandeburg059dab62014-04-01 09:07:20 +00005483 /* configure global TSO hardware offload settings */
5484 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
5485 TCP_FLAG_FIN) >> 16);
5486 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
5487 TCP_FLAG_FIN |
5488 TCP_FLAG_CWR) >> 16);
5489 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
5490
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07005491 udp_tunnel_get_rx_info(netdev);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06005492
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005493 return 0;
5494}
5495
5496/**
5497 * i40e_vsi_open -
5498 * @vsi: the VSI to open
5499 *
5500 * Finish initialization of the VSI.
5501 *
5502 * Returns 0 on success, negative value on failure
Maciej Sosin373149f2017-04-05 07:50:55 -04005503 *
5504 * Note: expects to be called while under rtnl_lock()
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005505 **/
5506int i40e_vsi_open(struct i40e_vsi *vsi)
5507{
5508 struct i40e_pf *pf = vsi->back;
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00005509 char int_name[I40E_INT_NAME_STR_LEN];
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005510 int err;
5511
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005512 /* allocate descriptors */
5513 err = i40e_vsi_setup_tx_resources(vsi);
5514 if (err)
5515 goto err_setup_tx;
5516 err = i40e_vsi_setup_rx_resources(vsi);
5517 if (err)
5518 goto err_setup_rx;
5519
5520 err = i40e_vsi_configure(vsi);
5521 if (err)
5522 goto err_setup_rx;
5523
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005524 if (vsi->netdev) {
5525 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5526 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
5527 err = i40e_vsi_request_irq(vsi, int_name);
5528 if (err)
5529 goto err_setup_rx;
5530
5531 /* Notify the stack of the actual queue counts. */
5532 err = netif_set_real_num_tx_queues(vsi->netdev,
5533 vsi->num_queue_pairs);
5534 if (err)
5535 goto err_set_queues;
5536
5537 err = netif_set_real_num_rx_queues(vsi->netdev,
5538 vsi->num_queue_pairs);
5539 if (err)
5540 goto err_set_queues;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005541
5542 } else if (vsi->type == I40E_VSI_FDIR) {
Carolyn Wybornye240f672014-12-11 07:06:37 +00005543 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005544 dev_driver_string(&pf->pdev->dev),
5545 dev_name(&pf->pdev->dev));
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00005546 err = i40e_vsi_request_irq(vsi, int_name);
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +00005547
Shannon Nelsonc22e3c62014-03-14 07:32:25 +00005548 } else {
Jean Sacrence9ccb12014-05-01 14:31:18 +00005549 err = -EINVAL;
Elizabeth Kappler6c167f52014-02-15 07:41:38 +00005550 goto err_setup_rx;
5551 }
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005552
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005553 err = i40e_up_complete(vsi);
5554 if (err)
5555 goto err_up_complete;
5556
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005557 return 0;
5558
5559err_up_complete:
5560 i40e_down(vsi);
Anjali Singhai Jain25946dd2013-11-26 10:49:14 +00005561err_set_queues:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005562 i40e_vsi_free_irq(vsi);
5563err_setup_rx:
5564 i40e_vsi_free_rx_resources(vsi);
5565err_setup_tx:
5566 i40e_vsi_free_tx_resources(vsi);
5567 if (vsi == pf->vsi[pf->lan_vsi])
Maciej Sosin373149f2017-04-05 07:50:55 -04005568 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005569
5570 return err;
5571}
5572
5573/**
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005574 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00005575 * @pf: Pointer to PF
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005576 *
5577 * This function destroys the hlist where all the Flow Director
5578 * filters were saved.
5579 **/
5580static void i40e_fdir_filter_exit(struct i40e_pf *pf)
5581{
5582 struct i40e_fdir_filter *filter;
Jacob Keller0e588de2017-02-06 14:38:50 -08005583 struct i40e_flex_pit *pit_entry, *tmp;
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005584 struct hlist_node *node2;
5585
5586 hlist_for_each_entry_safe(filter, node2,
5587 &pf->fdir_filter_list, fdir_node) {
5588 hlist_del(&filter->fdir_node);
5589 kfree(filter);
5590 }
Jacob Keller097dbf52017-02-06 14:38:46 -08005591
Jacob Keller0e588de2017-02-06 14:38:50 -08005592 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
5593 list_del(&pit_entry->list);
5594 kfree(pit_entry);
5595 }
5596 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
5597
5598 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
5599 list_del(&pit_entry->list);
5600 kfree(pit_entry);
5601 }
5602 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
5603
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005604 pf->fdir_pf_active_filters = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005605 pf->fd_tcp4_filter_cnt = 0;
5606 pf->fd_udp4_filter_cnt = 0;
Jacob Kellerf223c872017-02-06 14:38:51 -08005607 pf->fd_sctp4_filter_cnt = 0;
Jacob Keller097dbf52017-02-06 14:38:46 -08005608 pf->fd_ip4_filter_cnt = 0;
Jacob Keller3bcee1e2017-02-06 14:38:46 -08005609
5610 /* Reprogram the default input set for TCP/IPv4 */
5611 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
5612 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5613 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5614
5615 /* Reprogram the default input set for UDP/IPv4 */
5616 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
5617 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5618 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5619
5620 /* Reprogram the default input set for SCTP/IPv4 */
5621 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
5622 I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
5623 I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
5624
5625 /* Reprogram the default input set for Other/IPv4 */
5626 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
5627 I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00005628}
5629
5630/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005631 * i40e_close - Disables a network interface
5632 * @netdev: network interface device structure
5633 *
5634 * The close entry point is called when an interface is de-activated
5635 * by the OS. The hardware is still under the driver's control, but
5636 * this netdev interface is disabled.
5637 *
5638 * Returns 0, this is not allowed to fail
5639 **/
Vasu Dev38e00432014-08-01 13:27:03 -07005640int i40e_close(struct net_device *netdev)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005641{
5642 struct i40e_netdev_priv *np = netdev_priv(netdev);
5643 struct i40e_vsi *vsi = np->vsi;
5644
Shannon Nelson90ef8d42014-03-14 07:32:26 +00005645 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005646
5647 return 0;
5648}
5649
5650/**
5651 * i40e_do_reset - Start a PF or Core Reset sequence
5652 * @pf: board private structure
5653 * @reset_flags: which reset is requested
Maciej Sosin373149f2017-04-05 07:50:55 -04005654 * @lock_acquired: indicates whether or not the lock has been acquired
5655 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005656 *
5657 * The essential difference in resets is that the PF Reset
5658 * doesn't clear the packet buffers, doesn't reset the PE
5659 * firmware, and doesn't bother the other PFs on the chip.
5660 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04005661void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005662{
5663 u32 val;
5664
5665 WARN_ON(in_interrupt());
5666
Mitch Williams263fc482014-04-23 04:50:11 +00005667
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005668 /* do the biggest reset indicated */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005669 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005670
5671 /* Request a Global Reset
5672 *
5673 * This will start the chip's countdown to the actual full
5674 * chip reset event, and a warning interrupt to be sent
5675 * to all PFs, including the requestor. Our handler
5676 * for the warning interrupt will deal with the shutdown
5677 * and recovery of the switch setup.
5678 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005679 dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005680 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5681 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
5682 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5683
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005684 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005685
5686 /* Request a Core Reset
5687 *
5688 * Same as Global Reset, except does *not* include the MAC/PHY
5689 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005690 dev_dbg(&pf->pdev->dev, "CoreR requested\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005691 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
5692 val |= I40E_GLGEN_RTRIG_CORER_MASK;
5693 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
5694 i40e_flush(&pf->hw);
5695
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005696 } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005697
5698 /* Request a PF Reset
5699 *
5700 * Resets only the PF-specific registers
5701 *
5702 * This goes directly to the tear-down and rebuild of
5703 * the switch, since we need to do all the recovery as
5704 * for the Core Reset.
5705 */
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005706 dev_dbg(&pf->pdev->dev, "PFR requested\n");
Maciej Sosin373149f2017-04-05 07:50:55 -04005707 i40e_handle_reset_warning(pf, lock_acquired);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005708
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005709 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005710 int v;
5711
5712 /* Find the VSI(s) that requested a re-init */
5713 dev_info(&pf->pdev->dev,
5714 "VSI reinit requested\n");
Mitch Williams505682c2014-05-20 08:01:37 +00005715 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005716 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005717
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005718 if (vsi != NULL &&
5719 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
5720 i40e_vsi_reinit_locked(pf->vsi[v]);
5721 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
5722 }
5723 }
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04005724 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005725 int v;
5726
5727 /* Find the VSI(s) that needs to be brought down */
5728 dev_info(&pf->pdev->dev, "VSI down requested\n");
5729 for (v = 0; v < pf->num_alloc_vsi; v++) {
5730 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04005731
Neerav Parikhb5d06f02014-06-03 23:50:17 +00005732 if (vsi != NULL &&
5733 test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
5734 set_bit(__I40E_DOWN, &vsi->state);
5735 i40e_down(vsi);
5736 clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
5737 }
5738 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005739 } else {
5740 dev_info(&pf->pdev->dev,
5741 "bad reset request 0x%08x\n", reset_flags);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005742 }
5743}
5744
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005745#ifdef CONFIG_I40E_DCB
5746/**
5747 * i40e_dcb_need_reconfig - Check if DCB needs reconfig
5748 * @pf: board private structure
5749 * @old_cfg: current DCB config
5750 * @new_cfg: new DCB config
5751 **/
5752bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
5753 struct i40e_dcbx_config *old_cfg,
5754 struct i40e_dcbx_config *new_cfg)
5755{
5756 bool need_reconfig = false;
5757
5758 /* Check if ETS configuration has changed */
5759 if (memcmp(&new_cfg->etscfg,
5760 &old_cfg->etscfg,
5761 sizeof(new_cfg->etscfg))) {
5762 /* If Priority Table has changed reconfig is needed */
5763 if (memcmp(&new_cfg->etscfg.prioritytable,
5764 &old_cfg->etscfg.prioritytable,
5765 sizeof(new_cfg->etscfg.prioritytable))) {
5766 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005767 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005768 }
5769
5770 if (memcmp(&new_cfg->etscfg.tcbwtable,
5771 &old_cfg->etscfg.tcbwtable,
5772 sizeof(new_cfg->etscfg.tcbwtable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005773 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005774
5775 if (memcmp(&new_cfg->etscfg.tsatable,
5776 &old_cfg->etscfg.tsatable,
5777 sizeof(new_cfg->etscfg.tsatable)))
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005778 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005779 }
5780
5781 /* Check if PFC configuration has changed */
5782 if (memcmp(&new_cfg->pfc,
5783 &old_cfg->pfc,
5784 sizeof(new_cfg->pfc))) {
5785 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005786 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005787 }
5788
5789 /* Check if APP Table has changed */
5790 if (memcmp(&new_cfg->app,
5791 &old_cfg->app,
Dave Jones3d9667a2014-01-27 23:11:09 -05005792 sizeof(new_cfg->app))) {
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005793 need_reconfig = true;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005794 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
Dave Jones3d9667a2014-01-27 23:11:09 -05005795 }
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005796
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005797 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005798 return need_reconfig;
5799}
5800
5801/**
5802 * i40e_handle_lldp_event - Handle LLDP Change MIB event
5803 * @pf: board private structure
5804 * @e: event info posted on ARQ
5805 **/
5806static int i40e_handle_lldp_event(struct i40e_pf *pf,
5807 struct i40e_arq_event_info *e)
5808{
5809 struct i40e_aqc_lldp_get_mib *mib =
5810 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
5811 struct i40e_hw *hw = &pf->hw;
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005812 struct i40e_dcbx_config tmp_dcbx_cfg;
5813 bool need_reconfig = false;
5814 int ret = 0;
5815 u8 type;
5816
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005817 /* Not DCB capable or capability disabled */
David Ertmanea6acb72016-09-20 07:10:50 -07005818 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005819 return ret;
5820
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005821 /* Ignore if event is not for Nearest Bridge */
5822 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
5823 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005824 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005825 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
5826 return ret;
5827
5828 /* Check MIB Type and return if event for Remote MIB update */
5829 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005830 dev_dbg(&pf->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04005831 "LLDP event mib type %s\n", type ? "remote" : "local");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005832 if (type == I40E_AQ_LLDP_MIB_REMOTE) {
5833 /* Update the remote cached instance and return */
5834 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
5835 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
5836 &hw->remote_dcbx_config);
5837 goto exit;
5838 }
5839
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005840 /* Store the old configuration */
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07005841 tmp_dcbx_cfg = hw->local_dcbx_config;
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005842
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005843 /* Reset the old DCBx configuration data */
5844 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
Neerav Parikh9fa61dd2014-11-12 00:18:25 +00005845 /* Get updated DCBX data from firmware */
5846 ret = i40e_get_dcb_config(&pf->hw);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005847 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04005848 dev_info(&pf->pdev->dev,
5849 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
5850 i40e_stat_str(&pf->hw, ret),
5851 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005852 goto exit;
5853 }
5854
5855 /* No change detected in DCBX configs */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005856 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
5857 sizeof(tmp_dcbx_cfg))) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005858 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005859 goto exit;
5860 }
5861
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005862 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
5863 &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005864
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005865 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005866
5867 if (!need_reconfig)
5868 goto exit;
5869
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005870 /* Enable DCB tagging only when more than one TC */
Neerav Parikh750fcbc2015-02-24 06:58:47 +00005871 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
Neerav Parikh4d9b6042014-05-22 06:31:51 +00005872 pf->flags |= I40E_FLAG_DCB_ENABLED;
5873 else
5874 pf->flags &= ~I40E_FLAG_DCB_ENABLED;
5875
Neerav Parikh69129dc2014-11-12 00:18:46 +00005876 set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005877 /* Reconfiguration needed quiesce all VSIs */
5878 i40e_pf_quiesce_all_vsi(pf);
5879
5880 /* Changes in configuration update VEB/VSI */
5881 i40e_dcb_reconfigure(pf);
5882
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005883 ret = i40e_resume_port_tx(pf);
5884
Neerav Parikh69129dc2014-11-12 00:18:46 +00005885 clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005886 /* In case of error no point in resuming VSIs */
Neerav Parikh69129dc2014-11-12 00:18:46 +00005887 if (ret)
5888 goto exit;
5889
Neerav Parikh3fe06f42016-02-17 16:12:15 -08005890 /* Wait for the PF's queues to be disabled */
5891 ret = i40e_pf_wait_queues_disabled(pf);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005892 if (ret) {
5893 /* Schedule PF reset to recover */
5894 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
5895 i40e_service_event_schedule(pf);
5896 } else {
Neerav Parikh2fd75f32014-11-12 00:18:20 +00005897 i40e_pf_unquiesce_all_vsi(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08005898 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
5899 I40E_FLAG_CLIENT_L2_CHANGE);
Parikh, Neerav11e47702015-02-21 06:43:55 +00005900 }
5901
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08005902exit:
5903 return ret;
5904}
5905#endif /* CONFIG_I40E_DCB */
5906
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005907/**
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005908 * i40e_do_reset_safe - Protected reset path for userland calls.
5909 * @pf: board private structure
5910 * @reset_flags: which reset is requested
5911 *
5912 **/
5913void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
5914{
5915 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -04005916 i40e_do_reset(pf, reset_flags, true);
Anjali Singhai Jain23326182013-11-26 10:49:22 +00005917 rtnl_unlock();
5918}
5919
5920/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005921 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
5922 * @pf: board private structure
5923 * @e: event info posted on ARQ
5924 *
5925 * Handler for LAN Queue Overflow Event generated by the firmware for PF
5926 * and VF queues
5927 **/
5928static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
5929 struct i40e_arq_event_info *e)
5930{
5931 struct i40e_aqc_lan_overflow *data =
5932 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
5933 u32 queue = le32_to_cpu(data->prtdcb_rupto);
5934 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
5935 struct i40e_hw *hw = &pf->hw;
5936 struct i40e_vf *vf;
5937 u16 vf_id;
5938
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00005939 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
5940 queue, qtx_ctl);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00005941
5942 /* Queue belongs to VF, find the VF and issue VF reset */
5943 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
5944 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
5945 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
5946 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
5947 vf_id -= hw->func_caps.vf_base_id;
5948 vf = &pf->vf[vf_id];
5949 i40e_vc_notify_vf_reset(vf);
5950 /* Allow VF to process pending reset notification */
5951 msleep(20);
5952 i40e_reset_vf(vf, false);
5953 }
5954}
5955
5956/**
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005957 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
5958 * @pf: board private structure
5959 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005960u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005961{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005962 u32 val, fcnt_prog;
Anjali Singhai Jain12957382014-06-04 04:22:47 +00005963
5964 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5965 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
5966 return fcnt_prog;
5967}
5968
5969/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005970 * i40e_get_current_fd_count - Get total FD filters programmed for this PF
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005971 * @pf: board private structure
5972 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005973u32 i40e_get_current_fd_count(struct i40e_pf *pf)
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005974{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005975 u32 val, fcnt_prog;
5976
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005977 val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
5978 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
5979 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
5980 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
5981 return fcnt_prog;
5982}
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00005983
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00005984/**
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00005985 * i40e_get_global_fd_count - Get total FD filters programmed on device
5986 * @pf: board private structure
5987 **/
5988u32 i40e_get_global_fd_count(struct i40e_pf *pf)
5989{
5990 u32 val, fcnt_prog;
5991
5992 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
5993 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
5994 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
5995 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
5996 return fcnt_prog;
5997}
5998
5999/**
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006000 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
6001 * @pf: board private structure
6002 **/
6003void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
6004{
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006005 struct i40e_fdir_filter *filter;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006006 u32 fcnt_prog, fcnt_avail;
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006007 struct hlist_node *node;
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006008
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006009 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
6010 return;
6011
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006012 /* Check if, FD SB or ATR was auto disabled and if there is enough room
6013 * to re-enable
6014 */
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006015 fcnt_prog = i40e_get_global_fd_count(pf);
Anjali Singhai Jain12957382014-06-04 04:22:47 +00006016 fcnt_avail = pf->fdir_pf_filter_count;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006017 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
6018 (pf->fd_add_err == 0) ||
6019 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006020 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006021 (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
6022 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006023 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6024 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 +00006025 }
6026 }
Jacob Kellera3417d22016-09-06 18:05:10 -07006027
6028 /* Wait for some more space to be available to turn on ATR. We also
6029 * must check that no existing ntuple rules for TCP are in effect
6030 */
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006031 if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
6032 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006033 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
Jacob Keller097dbf52017-02-06 14:38:46 -08006034 (pf->fd_tcp4_filter_cnt == 0)) {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006035 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Anjali Singhai Jain2e4875e2015-04-16 20:06:06 -04006036 if (I40E_DEBUG_FD & pf->hw.debug_mask)
Jacob Kellera3417d22016-09-06 18:05:10 -07006037 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 +00006038 }
6039 }
Carolyn Wyborny3487b6c2015-08-27 11:42:38 -04006040
6041 /* if hw had a problem adding a filter, delete it */
6042 if (pf->fd_inv > 0) {
6043 hlist_for_each_entry_safe(filter, node,
6044 &pf->fdir_filter_list, fdir_node) {
6045 if (filter->fd_id == pf->fd_inv) {
6046 hlist_del(&filter->fdir_node);
6047 kfree(filter);
6048 pf->fdir_pf_active_filters--;
6049 }
6050 }
6051 }
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006052}
6053
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006054#define I40E_MIN_FD_FLUSH_INTERVAL 10
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006055#define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006056/**
6057 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
6058 * @pf: board private structure
6059 **/
6060static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
6061{
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006062 unsigned long min_flush_time;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006063 int flush_wait_retry = 50;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006064 bool disable_atr = false;
6065 int fd_room;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006066 int reg;
6067
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006068 if (!time_after(jiffies, pf->fd_flush_timestamp +
6069 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
6070 return;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006071
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006072 /* If the flush is happening too quick and we have mostly SB rules we
6073 * should not re-enable ATR for some time.
6074 */
6075 min_flush_time = pf->fd_flush_timestamp +
6076 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
6077 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006078
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006079 if (!(time_after(jiffies, min_flush_time)) &&
6080 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
6081 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6082 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
6083 disable_atr = true;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006084 }
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006085
6086 pf->fd_flush_timestamp = jiffies;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006087 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006088 /* flush all filters */
6089 wr32(&pf->hw, I40E_PFQF_CTL_1,
6090 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
6091 i40e_flush(&pf->hw);
6092 pf->fd_flush_cnt++;
6093 pf->fd_add_err = 0;
6094 do {
6095 /* Check FD flush status every 5-6msec */
6096 usleep_range(5000, 6000);
6097 reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
6098 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
6099 break;
6100 } while (flush_wait_retry--);
6101 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
6102 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
6103 } else {
6104 /* replay sideband filters */
6105 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
Jacob Keller097dbf52017-02-06 14:38:46 -08006106 if (!disable_atr && !pf->fd_tcp4_filter_cnt)
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08006107 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jesse Brandeburga5fdaf32015-08-28 17:55:56 -04006108 clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
6109 if (I40E_DEBUG_FD & pf->hw.debug_mask)
6110 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
6111 }
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006112}
6113
6114/**
6115 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
6116 * @pf: board private structure
6117 **/
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006118u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006119{
6120 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
6121}
6122
6123/* We can see up to 256 filter programming desc in transit if the filters are
6124 * being applied really fast; before we see the first
6125 * filter miss error on Rx queue 0. Accumulating enough error messages before
6126 * reacting will make sure we don't cause flush too often.
6127 */
6128#define I40E_MAX_FD_PROGRAM_ERROR 256
6129
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006130/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006131 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
6132 * @pf: board private structure
6133 **/
6134static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
6135{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006136
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006137 /* if interface is down do nothing */
6138 if (test_bit(__I40E_DOWN, &pf->state))
6139 return;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006140
Anjali Singhai Jain04294e32015-02-27 09:15:28 +00006141 if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00006142 i40e_fdir_flush_and_replay(pf);
6143
Anjali Singhai Jain55a5e602014-02-12 06:33:25 +00006144 i40e_fdir_check_and_reenable(pf);
6145
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006146}
6147
6148/**
6149 * i40e_vsi_link_event - notify VSI of a link event
6150 * @vsi: vsi to be notified
6151 * @link_up: link up or down
6152 **/
6153static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
6154{
Jesse Brandeburg32b5b812014-08-12 06:33:14 +00006155 if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006156 return;
6157
6158 switch (vsi->type) {
6159 case I40E_VSI_MAIN:
6160 if (!vsi->netdev || !vsi->netdev_registered)
6161 break;
6162
6163 if (link_up) {
6164 netif_carrier_on(vsi->netdev);
6165 netif_tx_wake_all_queues(vsi->netdev);
6166 } else {
6167 netif_carrier_off(vsi->netdev);
6168 netif_tx_stop_all_queues(vsi->netdev);
6169 }
6170 break;
6171
6172 case I40E_VSI_SRIOV:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006173 case I40E_VSI_VMDQ2:
6174 case I40E_VSI_CTRL:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06006175 case I40E_VSI_IWARP:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006176 case I40E_VSI_MIRROR:
6177 default:
6178 /* there is no notification for other VSIs */
6179 break;
6180 }
6181}
6182
6183/**
6184 * i40e_veb_link_event - notify elements on the veb of a link event
6185 * @veb: veb to be notified
6186 * @link_up: link up or down
6187 **/
6188static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
6189{
6190 struct i40e_pf *pf;
6191 int i;
6192
6193 if (!veb || !veb->pf)
6194 return;
6195 pf = veb->pf;
6196
6197 /* depth first... */
6198 for (i = 0; i < I40E_MAX_VEB; i++)
6199 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
6200 i40e_veb_link_event(pf->veb[i], link_up);
6201
6202 /* ... now the local VSIs */
Mitch Williams505682c2014-05-20 08:01:37 +00006203 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006204 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
6205 i40e_vsi_link_event(pf->vsi[i], link_up);
6206}
6207
6208/**
6209 * i40e_link_event - Update netif_carrier status
6210 * @pf: board private structure
6211 **/
6212static void i40e_link_event(struct i40e_pf *pf)
6213{
Mitch Williams320684c2014-10-17 03:14:43 +00006214 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006215 u8 new_link_speed, old_link_speed;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006216 i40e_status status;
6217 bool new_link, old_link;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006218
Catherine Sullivan1f9610e2015-10-21 19:47:09 -04006219 /* save off old link status information */
6220 pf->hw.phy.link_info_old = pf->hw.phy.link_info;
6221
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006222 /* set this to force the get_link_status call to refresh state */
6223 pf->hw.phy.get_link_info = true;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006224
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006225 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006226
6227 status = i40e_get_link_status(&pf->hw, &new_link);
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006228
6229 /* On success, disable temp link polling */
6230 if (status == I40E_SUCCESS) {
6231 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
6232 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
6233 } else {
6234 /* Enable link polling temporarily until i40e_get_link_status
6235 * returns I40E_SUCCESS
6236 */
6237 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
Jesse Brandeburga72a5abc2015-08-26 15:14:19 -04006238 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
6239 status);
6240 return;
6241 }
6242
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006243 old_link_speed = pf->hw.phy.link_info_old.link_speed;
6244 new_link_speed = pf->hw.phy.link_info.link_speed;
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006245
6246 if (new_link == old_link &&
Catherine Sullivanfef59dd2014-12-11 07:06:33 +00006247 new_link_speed == old_link_speed &&
Mitch Williams320684c2014-10-17 03:14:43 +00006248 (test_bit(__I40E_DOWN, &vsi->state) ||
6249 new_link == netif_carrier_ok(vsi->netdev)))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006250 return;
Mitch Williams320684c2014-10-17 03:14:43 +00006251
6252 if (!test_bit(__I40E_DOWN, &vsi->state))
6253 i40e_print_link_message(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006254
6255 /* Notify the base of the switch tree connected to
6256 * the link. Floating VEBs are not notified.
6257 */
6258 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
6259 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
6260 else
Mitch Williams320684c2014-10-17 03:14:43 +00006261 i40e_vsi_link_event(vsi, new_link);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006262
6263 if (pf->vf)
6264 i40e_vc_notify_link_state(pf);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006265
6266 if (pf->flags & I40E_FLAG_PTP)
6267 i40e_ptp_set_increment(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006268}
6269
6270/**
Shannon Nelson21536712014-10-25 10:35:25 +00006271 * i40e_watchdog_subtask - periodic checks not using event driven response
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006272 * @pf: board private structure
6273 **/
6274static void i40e_watchdog_subtask(struct i40e_pf *pf)
6275{
6276 int i;
6277
6278 /* if interface is down do nothing */
6279 if (test_bit(__I40E_DOWN, &pf->state) ||
6280 test_bit(__I40E_CONFIG_BUSY, &pf->state))
6281 return;
6282
Shannon Nelson21536712014-10-25 10:35:25 +00006283 /* make sure we don't do these things too often */
6284 if (time_before(jiffies, (pf->service_timer_previous +
6285 pf->service_timer_period)))
6286 return;
6287 pf->service_timer_previous = jiffies;
6288
Harshitha Ramamurthyae136702016-12-12 15:44:16 -08006289 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
6290 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
Shannon Nelson9ac77262015-08-27 11:42:40 -04006291 i40e_link_event(pf);
Shannon Nelson21536712014-10-25 10:35:25 +00006292
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006293 /* Update the stats for active netdevs so the network stack
6294 * can look at updated numbers whenever it cares to
6295 */
Mitch Williams505682c2014-05-20 08:01:37 +00006296 for (i = 0; i < pf->num_alloc_vsi; i++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006297 if (pf->vsi[i] && pf->vsi[i]->netdev)
6298 i40e_update_stats(pf->vsi[i]);
6299
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04006300 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
6301 /* Update the stats for the active switching components */
6302 for (i = 0; i < I40E_MAX_VEB; i++)
6303 if (pf->veb[i])
6304 i40e_update_veb_stats(pf->veb[i]);
6305 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00006306
6307 i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006308}
6309
6310/**
6311 * i40e_reset_subtask - Set up for resetting the device and driver
6312 * @pf: board private structure
6313 **/
6314static void i40e_reset_subtask(struct i40e_pf *pf)
6315{
6316 u32 reset_flags = 0;
6317
6318 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006319 reset_flags |= BIT(__I40E_REINIT_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006320 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
6321 }
6322 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006323 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006324 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
6325 }
6326 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006327 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006328 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
6329 }
6330 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006331 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006332 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
6333 }
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006334 if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -08006335 reset_flags |= BIT(__I40E_DOWN_REQUESTED);
Neerav Parikhb5d06f02014-06-03 23:50:17 +00006336 clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
6337 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006338
6339 /* If there's a recovery already waiting, it takes
6340 * precedence before starting a new reset sequence.
6341 */
6342 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
Maciej Sosin373149f2017-04-05 07:50:55 -04006343 i40e_prep_for_reset(pf, false);
6344 i40e_reset(pf);
6345 i40e_rebuild(pf, false, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006346 }
6347
6348 /* If we're already down or resetting, just bail */
6349 if (reset_flags &&
6350 !test_bit(__I40E_DOWN, &pf->state) &&
Maciej Sosin373149f2017-04-05 07:50:55 -04006351 !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
6352 rtnl_lock();
6353 i40e_do_reset(pf, reset_flags, true);
6354 rtnl_unlock();
6355 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006356}
6357
6358/**
6359 * i40e_handle_link_event - Handle link event
6360 * @pf: board private structure
6361 * @e: event info posted on ARQ
6362 **/
6363static void i40e_handle_link_event(struct i40e_pf *pf,
6364 struct i40e_arq_event_info *e)
6365{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006366 struct i40e_aqc_get_link_status *status =
6367 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006368
Jesse Brandeburg1e701e02014-09-13 07:40:42 +00006369 /* Do a new status request to re-enable LSE reporting
6370 * and load new status information into the hw struct
6371 * This completely ignores any state information
6372 * in the ARQ event info, instead choosing to always
6373 * issue the AQ update link status command.
6374 */
6375 i40e_link_event(pf);
6376
Carolyn Wyborny7b592f62014-07-10 07:58:19 +00006377 /* check for unqualified module, if link is down */
6378 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
6379 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
6380 (!(status->link_info & I40E_AQ_LINK_UP)))
6381 dev_err(&pf->pdev->dev,
6382 "The driver failed to link because an unqualified module was detected.\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006383}
6384
6385/**
6386 * i40e_clean_adminq_subtask - Clean the AdminQ rings
6387 * @pf: board private structure
6388 **/
6389static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
6390{
6391 struct i40e_arq_event_info event;
6392 struct i40e_hw *hw = &pf->hw;
6393 u16 pending, i = 0;
6394 i40e_status ret;
6395 u16 opcode;
Shannon Nelson86df2422014-05-20 08:01:35 +00006396 u32 oldval;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006397 u32 val;
6398
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006399 /* Do not run clean AQ when PF reset fails */
6400 if (test_bit(__I40E_RESET_FAILED, &pf->state))
6401 return;
6402
Shannon Nelson86df2422014-05-20 08:01:35 +00006403 /* check for error indications */
6404 val = rd32(&pf->hw, pf->hw.aq.arq.len);
6405 oldval = val;
6406 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006407 if (hw->debug_mask & I40E_DEBUG_AQ)
6408 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006409 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
6410 }
6411 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006412 if (hw->debug_mask & I40E_DEBUG_AQ)
6413 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006414 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
Mitch Williams1d0a4ad2015-12-23 12:05:48 -08006415 pf->arq_overflows++;
Shannon Nelson86df2422014-05-20 08:01:35 +00006416 }
6417 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006418 if (hw->debug_mask & I40E_DEBUG_AQ)
6419 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006420 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
6421 }
6422 if (oldval != val)
6423 wr32(&pf->hw, pf->hw.aq.arq.len, val);
6424
6425 val = rd32(&pf->hw, pf->hw.aq.asq.len);
6426 oldval = val;
6427 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006428 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6429 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006430 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
6431 }
6432 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006433 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6434 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006435 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
6436 }
6437 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
Mitch Williams75eb73c2015-11-19 11:34:21 -08006438 if (pf->hw.debug_mask & I40E_DEBUG_AQ)
6439 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
Shannon Nelson86df2422014-05-20 08:01:35 +00006440 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
6441 }
6442 if (oldval != val)
6443 wr32(&pf->hw, pf->hw.aq.asq.len, val);
6444
Mitch Williams1001dc32014-11-11 20:02:19 +00006445 event.buf_len = I40E_MAX_AQ_BUF_SIZE;
6446 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006447 if (!event.msg_buf)
6448 return;
6449
6450 do {
6451 ret = i40e_clean_arq_element(hw, &event, &pending);
Mitch Williams56497972014-06-04 08:45:18 +00006452 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006453 break;
Mitch Williams56497972014-06-04 08:45:18 +00006454 else if (ret) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006455 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
6456 break;
6457 }
6458
6459 opcode = le16_to_cpu(event.desc.opcode);
6460 switch (opcode) {
6461
6462 case i40e_aqc_opc_get_link_status:
6463 i40e_handle_link_event(pf, &event);
6464 break;
6465 case i40e_aqc_opc_send_msg_to_pf:
6466 ret = i40e_vc_process_vf_msg(pf,
6467 le16_to_cpu(event.desc.retval),
6468 le32_to_cpu(event.desc.cookie_high),
6469 le32_to_cpu(event.desc.cookie_low),
6470 event.msg_buf,
Mitch Williams1001dc32014-11-11 20:02:19 +00006471 event.msg_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006472 break;
6473 case i40e_aqc_opc_lldp_update_mib:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006474 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006475#ifdef CONFIG_I40E_DCB
6476 rtnl_lock();
6477 ret = i40e_handle_lldp_event(pf, &event);
6478 rtnl_unlock();
6479#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006480 break;
6481 case i40e_aqc_opc_event_lan_overflow:
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006482 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006483 i40e_handle_lan_overflow_event(pf, &event);
6484 break;
Shannon Nelson0467bc92013-12-18 13:45:58 +00006485 case i40e_aqc_opc_send_msg_to_peer:
6486 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
6487 break;
Shannon Nelson91a0f932015-03-19 14:32:01 -07006488 case i40e_aqc_opc_nvm_erase:
6489 case i40e_aqc_opc_nvm_update:
Michal Kosiarz00ada502015-11-19 11:34:20 -08006490 case i40e_aqc_opc_oem_post_update:
Shannon Nelson6e93d0c2016-01-15 14:33:18 -08006491 i40e_debug(&pf->hw, I40E_DEBUG_NVM,
6492 "ARQ NVM operation 0x%04x completed\n",
6493 opcode);
Shannon Nelson91a0f932015-03-19 14:32:01 -07006494 break;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006495 default:
6496 dev_info(&pf->pdev->dev,
Shannon Nelson56e5ca62016-03-10 14:59:48 -08006497 "ARQ: Unknown event 0x%04x ignored\n",
Shannon Nelson0467bc92013-12-18 13:45:58 +00006498 opcode);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006499 break;
6500 }
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006501 } while (i++ < pf->adminq_work_limit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006502
Christopher N Bednarz1fca3262017-03-10 12:22:03 -08006503 if (i < pf->adminq_work_limit)
6504 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
6505
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006506 /* re-enable Admin queue interrupt cause */
6507 val = rd32(hw, I40E_PFINT_ICR0_ENA);
6508 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
6509 wr32(hw, I40E_PFINT_ICR0_ENA, val);
6510 i40e_flush(hw);
6511
6512 kfree(event.msg_buf);
6513}
6514
6515/**
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006516 * i40e_verify_eeprom - make sure eeprom is good to use
6517 * @pf: board private structure
6518 **/
6519static void i40e_verify_eeprom(struct i40e_pf *pf)
6520{
6521 int err;
6522
6523 err = i40e_diag_eeprom_test(&pf->hw);
6524 if (err) {
6525 /* retry in case of garbage read */
6526 err = i40e_diag_eeprom_test(&pf->hw);
6527 if (err) {
6528 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
6529 err);
6530 set_bit(__I40E_BAD_EEPROM, &pf->state);
6531 }
6532 }
6533
6534 if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
6535 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
6536 clear_bit(__I40E_BAD_EEPROM, &pf->state);
6537 }
6538}
6539
6540/**
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006541 * i40e_enable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006542 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006543 *
6544 * enable switch loop back or die - no point in a return value
6545 **/
6546static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
6547{
6548 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6549 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006550 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006551
6552 ctxt.seid = pf->main_vsi_seid;
6553 ctxt.pf_num = pf->hw.pf_id;
6554 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006555 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6556 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006557 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006558 "couldn't get PF vsi config, err %s aq_err %s\n",
6559 i40e_stat_str(&pf->hw, ret),
6560 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006561 return;
6562 }
6563 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6564 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6565 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6566
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006567 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6568 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006569 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006570 "update vsi switch failed, err %s aq_err %s\n",
6571 i40e_stat_str(&pf->hw, ret),
6572 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006573 }
6574}
6575
6576/**
6577 * i40e_disable_pf_switch_lb
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006578 * @pf: pointer to the PF structure
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006579 *
6580 * disable switch loop back or die - no point in a return value
6581 **/
6582static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
6583{
6584 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
6585 struct i40e_vsi_context ctxt;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006586 int ret;
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006587
6588 ctxt.seid = pf->main_vsi_seid;
6589 ctxt.pf_num = pf->hw.pf_id;
6590 ctxt.vf_num = 0;
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006591 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6592 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006593 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006594 "couldn't get PF vsi config, err %s aq_err %s\n",
6595 i40e_stat_str(&pf->hw, ret),
6596 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006597 return;
6598 }
6599 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6600 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6601 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6602
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006603 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
6604 if (ret) {
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006605 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006606 "update vsi switch failed, err %s aq_err %s\n",
6607 i40e_stat_str(&pf->hw, ret),
6608 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Akeem G Abodunrin386a0af2015-02-27 09:15:25 +00006609 }
6610}
6611
6612/**
Neerav Parikh51616012015-02-06 08:52:14 +00006613 * i40e_config_bridge_mode - Configure the HW bridge mode
6614 * @veb: pointer to the bridge instance
6615 *
6616 * Configure the loop back mode for the LAN VSI that is downlink to the
6617 * specified HW bridge instance. It is expected this function is called
6618 * when a new HW bridge is instantiated.
6619 **/
6620static void i40e_config_bridge_mode(struct i40e_veb *veb)
6621{
6622 struct i40e_pf *pf = veb->pf;
6623
Shannon Nelson6dec1012015-09-28 14:12:30 -04006624 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
6625 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
6626 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
Neerav Parikh51616012015-02-06 08:52:14 +00006627 if (veb->bridge_mode & BRIDGE_MODE_VEPA)
6628 i40e_disable_pf_switch_lb(pf);
6629 else
6630 i40e_enable_pf_switch_lb(pf);
6631}
6632
6633/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006634 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
6635 * @veb: pointer to the VEB instance
6636 *
6637 * This is a recursive function that first builds the attached VSIs then
6638 * recurses in to build the next layer of VEB. We track the connections
6639 * through our own index numbers because the seid's from the HW could
6640 * change across the reset.
6641 **/
6642static int i40e_reconstitute_veb(struct i40e_veb *veb)
6643{
6644 struct i40e_vsi *ctl_vsi = NULL;
6645 struct i40e_pf *pf = veb->pf;
6646 int v, veb_idx;
6647 int ret;
6648
6649 /* build VSI that owns this VEB, temporarily attached to base VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006650 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006651 if (pf->vsi[v] &&
6652 pf->vsi[v]->veb_idx == veb->idx &&
6653 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
6654 ctl_vsi = pf->vsi[v];
6655 break;
6656 }
6657 }
6658 if (!ctl_vsi) {
6659 dev_info(&pf->pdev->dev,
6660 "missing owner VSI for veb_idx %d\n", veb->idx);
6661 ret = -ENOENT;
6662 goto end_reconstitute;
6663 }
6664 if (ctl_vsi != pf->vsi[pf->lan_vsi])
6665 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6666 ret = i40e_add_vsi(ctl_vsi);
6667 if (ret) {
6668 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006669 "rebuild of veb_idx %d owner VSI failed: %d\n",
6670 veb->idx, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006671 goto end_reconstitute;
6672 }
6673 i40e_vsi_reset_stats(ctl_vsi);
6674
6675 /* create the VEB in the switch and move the VSI onto the VEB */
6676 ret = i40e_add_veb(veb, ctl_vsi);
6677 if (ret)
6678 goto end_reconstitute;
6679
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07006680 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
6681 veb->bridge_mode = BRIDGE_MODE_VEB;
6682 else
6683 veb->bridge_mode = BRIDGE_MODE_VEPA;
Neerav Parikh51616012015-02-06 08:52:14 +00006684 i40e_config_bridge_mode(veb);
Anjali Singhai Jainb64ba082014-11-13 03:06:15 +00006685
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006686 /* create the remaining VSIs attached to this VEB */
Mitch Williams505682c2014-05-20 08:01:37 +00006687 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006688 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
6689 continue;
6690
6691 if (pf->vsi[v]->veb_idx == veb->idx) {
6692 struct i40e_vsi *vsi = pf->vsi[v];
Jesse Brandeburg6995b362015-08-28 17:55:54 -04006693
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006694 vsi->uplink_seid = veb->seid;
6695 ret = i40e_add_vsi(vsi);
6696 if (ret) {
6697 dev_info(&pf->pdev->dev,
6698 "rebuild of vsi_idx %d failed: %d\n",
6699 v, ret);
6700 goto end_reconstitute;
6701 }
6702 i40e_vsi_reset_stats(vsi);
6703 }
6704 }
6705
6706 /* create any VEBs attached to this VEB - RECURSION */
6707 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6708 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
6709 pf->veb[veb_idx]->uplink_seid = veb->seid;
6710 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
6711 if (ret)
6712 break;
6713 }
6714 }
6715
6716end_reconstitute:
6717 return ret;
6718}
6719
6720/**
6721 * i40e_get_capabilities - get info about the HW
6722 * @pf: the PF struct
6723 **/
6724static int i40e_get_capabilities(struct i40e_pf *pf)
6725{
6726 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
6727 u16 data_size;
6728 int buf_len;
6729 int err;
6730
6731 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
6732 do {
6733 cap_buf = kzalloc(buf_len, GFP_KERNEL);
6734 if (!cap_buf)
6735 return -ENOMEM;
6736
6737 /* this loads the data into the hw struct for us */
6738 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
6739 &data_size,
6740 i40e_aqc_opc_list_func_capabilities,
6741 NULL);
6742 /* data loaded, buffer no longer needed */
6743 kfree(cap_buf);
6744
6745 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
6746 /* retry with a larger buffer */
6747 buf_len = data_size;
6748 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
6749 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006750 "capability discovery failed, err %s aq_err %s\n",
6751 i40e_stat_str(&pf->hw, err),
6752 i40e_aq_str(&pf->hw,
6753 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006754 return -ENODEV;
6755 }
6756 } while (err);
6757
6758 if (pf->hw.debug_mask & I40E_DEBUG_USER)
6759 dev_info(&pf->pdev->dev,
6760 "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",
6761 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
6762 pf->hw.func_caps.num_msix_vectors,
6763 pf->hw.func_caps.num_msix_vectors_vf,
6764 pf->hw.func_caps.fd_filters_guaranteed,
6765 pf->hw.func_caps.fd_filters_best_effort,
6766 pf->hw.func_caps.num_tx_qp,
6767 pf->hw.func_caps.num_vsis);
6768
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00006769#define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
6770 + pf->hw.func_caps.num_vfs)
6771 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
6772 dev_info(&pf->pdev->dev,
6773 "got num_vsis %d, setting num_vsis to %d\n",
6774 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
6775 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
6776 }
6777
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006778 return 0;
6779}
6780
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006781static int i40e_vsi_clear(struct i40e_vsi *vsi);
6782
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006783/**
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006784 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006785 * @pf: board private structure
6786 **/
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006787static void i40e_fdir_sb_setup(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006788{
6789 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006790
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006791 /* quick workaround for an NVM issue that leaves a critical register
6792 * uninitialized
6793 */
6794 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
6795 static const u32 hkey[] = {
6796 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
6797 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
6798 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
6799 0x95b3a76d};
Alexander Duyck4b816442016-10-11 15:26:53 -07006800 int i;
Jesse Brandeburg407e0632014-06-03 23:50:12 +00006801
6802 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
6803 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
6804 }
6805
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006806 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006807 return;
6808
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006809 /* find existing VSI and see if it needs configuring */
Alexander Duyck4b816442016-10-11 15:26:53 -07006810 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006811
6812 /* create a new VSI if none exists */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006813 if (!vsi) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006814 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
6815 pf->vsi[pf->lan_vsi]->seid, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006816 if (!vsi) {
6817 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006818 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
6819 return;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006820 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006821 }
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +00006822
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08006823 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006824}
6825
6826/**
6827 * i40e_fdir_teardown - release the Flow Director resources
6828 * @pf: board private structure
6829 **/
6830static void i40e_fdir_teardown(struct i40e_pf *pf)
6831{
Alexander Duyck4b816442016-10-11 15:26:53 -07006832 struct i40e_vsi *vsi;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006833
Joseph Gasparakis17a73f62014-02-12 01:45:30 +00006834 i40e_fdir_filter_exit(pf);
Alexander Duyck4b816442016-10-11 15:26:53 -07006835 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
6836 if (vsi)
6837 i40e_vsi_release(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006838}
6839
6840/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006841 * i40e_prep_for_reset - prep for the core to reset
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006842 * @pf: board private structure
Maciej Sosin373149f2017-04-05 07:50:55 -04006843 * @lock_acquired: indicates whether or not the lock has been acquired
6844 * before this function was called.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006845 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00006846 * Close up the VFs and other things in prep for PF Reset.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006847 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006848static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006849{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006850 struct i40e_hw *hw = &pf->hw;
Shannon Nelson60442de2014-04-23 04:50:13 +00006851 i40e_status ret = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006852 u32 v;
6853
6854 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
6855 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006856 return;
Mitch Williamsd3ce57342016-03-10 14:59:46 -08006857 if (i40e_check_asq_alive(&pf->hw))
6858 i40e_vc_notify_reset(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006859
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006860 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006861
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006862 /* quiesce the VSIs and their queues that are not already DOWN */
Maciej Sosin373149f2017-04-05 07:50:55 -04006863 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
6864 if (!lock_acquired)
6865 rtnl_lock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006866 i40e_pf_quiesce_all_vsi(pf);
Maciej Sosin373149f2017-04-05 07:50:55 -04006867 if (!lock_acquired)
6868 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006869
Mitch Williams505682c2014-05-20 08:01:37 +00006870 for (v = 0; v < pf->num_alloc_vsi; v++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006871 if (pf->vsi[v])
6872 pf->vsi[v]->seid = 0;
6873 }
6874
6875 i40e_shutdown_adminq(&pf->hw);
6876
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006877 /* call shutdown HMC */
Shannon Nelson60442de2014-04-23 04:50:13 +00006878 if (hw->hmc.hmc_obj) {
6879 ret = i40e_shutdown_lan_hmc(hw);
Shannon Nelson23cfbe02014-06-04 01:23:14 +00006880 if (ret)
Shannon Nelson60442de2014-04-23 04:50:13 +00006881 dev_warn(&pf->pdev->dev,
6882 "shutdown_lan_hmc failed: %d\n", ret);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006883 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006884}
6885
6886/**
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006887 * i40e_send_version - update firmware with driver version
6888 * @pf: PF struct
6889 */
6890static void i40e_send_version(struct i40e_pf *pf)
6891{
6892 struct i40e_driver_version dv;
6893
6894 dv.major_version = DRV_VERSION_MAJOR;
6895 dv.minor_version = DRV_VERSION_MINOR;
6896 dv.build_version = DRV_VERSION_BUILD;
6897 dv.subbuild_version = 0;
Rickard Strandqvist35a7d802014-07-29 09:26:25 +00006898 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00006899 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
6900}
6901
6902/**
Maciej Sosin373149f2017-04-05 07:50:55 -04006903 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
6904 * @pf: board private structure
6905 **/
6906static int i40e_reset(struct i40e_pf *pf)
6907{
6908 struct i40e_hw *hw = &pf->hw;
6909 i40e_status ret;
6910
6911 ret = i40e_pf_reset(hw);
6912 if (ret) {
6913 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
6914 set_bit(__I40E_RESET_FAILED, &pf->state);
6915 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
6916 } else {
6917 pf->pfr_count++;
6918 }
6919 return ret;
6920}
6921
6922/**
6923 * i40e_rebuild - rebuild using a saved config
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006924 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006925 * @reinit: if the Main VSI needs to re-initialized.
Maciej Sosin373149f2017-04-05 07:50:55 -04006926 * @lock_acquired: indicates whether or not the lock has been acquired
6927 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006928 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04006929static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006930{
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006931 struct i40e_hw *hw = &pf->hw;
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006932 u8 set_fc_aq_fail = 0;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00006933 i40e_status ret;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04006934 u32 val;
Maciej Sosin373149f2017-04-05 07:50:55 -04006935 int v;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006936
6937 if (test_bit(__I40E_DOWN, &pf->state))
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006938 goto clear_recovery;
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00006939 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006940
6941 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
6942 ret = i40e_init_adminq(&pf->hw);
6943 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006944 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
6945 i40e_stat_str(&pf->hw, ret),
6946 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00006947 goto clear_recovery;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006948 }
6949
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006950 /* re-verify the eeprom if we just had an EMP reset */
Anjali Singhai Jain9df42d12015-01-24 09:58:40 +00006951 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006952 i40e_verify_eeprom(pf);
Shannon Nelson4eb3f762014-03-06 08:59:58 +00006953
Shannon Nelsone78ac4bf2014-05-10 04:49:09 +00006954 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006955 ret = i40e_get_capabilities(pf);
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006956 if (ret)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006957 goto end_core_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006958
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006959 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -08006960 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006961 if (ret) {
6962 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
6963 goto end_core_reset;
6964 }
6965 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
6966 if (ret) {
6967 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
6968 goto end_core_reset;
6969 }
6970
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006971#ifdef CONFIG_I40E_DCB
6972 ret = i40e_init_pf_dcb(pf);
6973 if (ret) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +00006974 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
6975 pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
6976 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -08006977 }
6978#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006979 /* do basic switch setup */
Maciej Sosin373149f2017-04-05 07:50:55 -04006980 if (!lock_acquired)
6981 rtnl_lock();
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00006982 ret = i40e_setup_pf_switch(pf, reinit);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006983 if (ret)
Maciej Sosin373149f2017-04-05 07:50:55 -04006984 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00006985
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006986 /* The driver only wants link up/down and module qualification
6987 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006988 */
6989 ret = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006990 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -07006991 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -08006992 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006993 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04006994 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
6995 i40e_stat_str(&pf->hw, ret),
6996 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +00006997
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00006998 /* make sure our flow control settings are restored */
6999 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
7000 if (ret)
Neerav Parikh8279e492015-09-03 17:18:50 -04007001 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
7002 i40e_stat_str(&pf->hw, ret),
7003 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007004
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007005 /* Rebuild the VSIs and VEBs that existed before reset.
7006 * They are still in our local switch element arrays, so only
7007 * need to rebuild the switch model in the HW.
7008 *
7009 * If there were VEBs but the reconstitution failed, we'll try
7010 * try to recover minimal use by getting the basic PF VSI working.
7011 */
7012 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
Jesse Brandeburg69bfb112014-02-11 08:24:13 +00007013 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007014 /* find the one VEB connected to the MAC, and find orphans */
7015 for (v = 0; v < I40E_MAX_VEB; v++) {
7016 if (!pf->veb[v])
7017 continue;
7018
7019 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
7020 pf->veb[v]->uplink_seid == 0) {
7021 ret = i40e_reconstitute_veb(pf->veb[v]);
7022
7023 if (!ret)
7024 continue;
7025
7026 /* If Main VEB failed, we're in deep doodoo,
7027 * so give up rebuilding the switch and set up
7028 * for minimal rebuild of PF VSI.
7029 * If orphan failed, we'll report the error
7030 * but try to keep going.
7031 */
7032 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
7033 dev_info(&pf->pdev->dev,
7034 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
7035 ret);
7036 pf->vsi[pf->lan_vsi]->uplink_seid
7037 = pf->mac_seid;
7038 break;
7039 } else if (pf->veb[v]->uplink_seid == 0) {
7040 dev_info(&pf->pdev->dev,
7041 "rebuild of orphan VEB failed: %d\n",
7042 ret);
7043 }
7044 }
7045 }
7046 }
7047
7048 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
Shannon Nelsoncde4cbc2014-06-04 01:23:17 +00007049 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007050 /* no VEB, so rebuild only the Main VSI */
7051 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
7052 if (ret) {
7053 dev_info(&pf->pdev->dev,
7054 "rebuild of Main VSI failed: %d\n", ret);
Maciej Sosin373149f2017-04-05 07:50:55 -04007055 goto end_unlock;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007056 }
7057 }
7058
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -04007059 /* Reconfigure hardware for allowing smaller MSS in the case
7060 * of TSO, so that we avoid the MDD being fired and causing
7061 * a reset in the case of small MSS+TSO.
7062 */
7063#define I40E_REG_MSS 0x000E64DC
7064#define I40E_REG_MSS_MIN_MASK 0x3FF0000
7065#define I40E_64BYTE_MSS 0x400000
7066 val = rd32(hw, I40E_REG_MSS);
7067 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
7068 val &= ~I40E_REG_MSS_MIN_MASK;
7069 val |= I40E_64BYTE_MSS;
7070 wr32(hw, I40E_REG_MSS, val);
7071 }
7072
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08007073 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +00007074 msleep(75);
7075 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
7076 if (ret)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007077 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
7078 i40e_stat_str(&pf->hw, ret),
7079 i40e_aq_str(&pf->hw,
7080 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +00007081 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007082 /* reinit the misc interrupt */
7083 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7084 ret = i40e_setup_misc_vector(pf);
7085
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -04007086 /* Add a filter to drop all Flow control frames from any VSI from being
7087 * transmitted. By doing so we stop a malicious VF from sending out
7088 * PAUSE or PFC frames and potentially controlling traffic for other
7089 * PF/VF VSIs.
7090 * The FW can still send Flow control frames if enabled.
7091 */
7092 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
7093 pf->main_vsi_seid);
7094
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007095 /* restart the VSIs that were rebuilt and running before the reset */
7096 i40e_pf_unquiesce_all_vsi(pf);
7097
Mitch Williams69f64b22014-02-13 03:48:46 -08007098 if (pf->num_alloc_vfs) {
7099 for (v = 0; v < pf->num_alloc_vfs; v++)
7100 i40e_reset_vf(&pf->vf[v], true);
7101 }
7102
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007103 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +00007104 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007105
Maciej Sosin373149f2017-04-05 07:50:55 -04007106end_unlock:
7107if (!lock_acquired)
7108 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007109end_core_reset:
Anjali Singhai Jaina316f652014-07-12 07:28:25 +00007110 clear_bit(__I40E_RESET_FAILED, &pf->state);
7111clear_recovery:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007112 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
7113}
7114
7115/**
Maciej Sosin373149f2017-04-05 07:50:55 -04007116 * i40e_reset_and_rebuild - reset and rebuild using a saved config
7117 * @pf: board private structure
7118 * @reinit: if the Main VSI needs to re-initialized.
7119 * @lock_acquired: indicates whether or not the lock has been acquired
7120 * before this function was called.
7121 **/
7122static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
7123 bool lock_acquired)
7124{
7125 int ret;
7126 /* Now we wait for GRST to settle out.
7127 * We don't have to delete the VEBs or VSIs from the hw switch
7128 * because the reset will make them disappear.
7129 */
7130 ret = i40e_reset(pf);
7131 if (!ret)
7132 i40e_rebuild(pf, reinit, lock_acquired);
7133}
7134
7135/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007136 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007137 * @pf: board private structure
7138 *
7139 * Close up the VFs and other things in prep for a Core Reset,
7140 * then get ready to rebuild the world.
Maciej Sosin373149f2017-04-05 07:50:55 -04007141 * @lock_acquired: indicates whether or not the lock has been acquired
7142 * before this function was called.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007143 **/
Maciej Sosin373149f2017-04-05 07:50:55 -04007144static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007145{
Maciej Sosin373149f2017-04-05 07:50:55 -04007146 i40e_prep_for_reset(pf, lock_acquired);
7147 i40e_reset_and_rebuild(pf, false, lock_acquired);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007148}
7149
7150/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007151 * i40e_handle_mdd_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007152 * @pf: pointer to the PF structure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007153 *
7154 * Called from the MDD irq handler to identify possibly malicious vfs
7155 **/
7156static void i40e_handle_mdd_event(struct i40e_pf *pf)
7157{
7158 struct i40e_hw *hw = &pf->hw;
7159 bool mdd_detected = false;
Neerav Parikhdf430b12014-06-04 01:23:15 +00007160 bool pf_mdd_detected = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007161 struct i40e_vf *vf;
7162 u32 reg;
7163 int i;
7164
7165 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
7166 return;
7167
7168 /* find what triggered the MDD event */
7169 reg = rd32(hw, I40E_GL_MDET_TX);
7170 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007171 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
7172 I40E_GL_MDET_TX_PF_NUM_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007173 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007174 I40E_GL_MDET_TX_VF_NUM_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007175 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007176 I40E_GL_MDET_TX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007177 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
7178 I40E_GL_MDET_TX_QUEUE_SHIFT) -
7179 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007180 if (netif_msg_tx_err(pf))
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007181 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 +00007182 event, queue, pf_num, vf_num);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007183 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
7184 mdd_detected = true;
7185 }
7186 reg = rd32(hw, I40E_GL_MDET_RX);
7187 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007188 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
7189 I40E_GL_MDET_RX_FUNCTION_SHIFT;
Dan Carpenter013f6572014-10-22 20:06:29 -07007190 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
Anjali Singhai Jain4c33f832014-06-05 00:18:21 +00007191 I40E_GL_MDET_RX_EVENT_SHIFT;
Mitch Williams2089ad02014-10-17 03:14:53 +00007192 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
7193 I40E_GL_MDET_RX_QUEUE_SHIFT) -
7194 pf->hw.func_caps.base_queue;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007195 if (netif_msg_rx_err(pf))
7196 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
7197 event, queue, func);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007198 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
7199 mdd_detected = true;
7200 }
7201
Neerav Parikhdf430b12014-06-04 01:23:15 +00007202 if (mdd_detected) {
7203 reg = rd32(hw, I40E_PF_MDET_TX);
7204 if (reg & I40E_PF_MDET_TX_VALID_MASK) {
7205 wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007206 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007207 pf_mdd_detected = true;
7208 }
7209 reg = rd32(hw, I40E_PF_MDET_RX);
7210 if (reg & I40E_PF_MDET_RX_VALID_MASK) {
7211 wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007212 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
Neerav Parikhdf430b12014-06-04 01:23:15 +00007213 pf_mdd_detected = true;
7214 }
7215 /* Queue belongs to the PF, initiate a reset */
7216 if (pf_mdd_detected) {
7217 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
7218 i40e_service_event_schedule(pf);
7219 }
7220 }
7221
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007222 /* see if one of the VFs needs its hand slapped */
7223 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
7224 vf = &(pf->vf[i]);
7225 reg = rd32(hw, I40E_VP_MDET_TX(i));
7226 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
7227 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
7228 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007229 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
7230 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007231 }
7232
7233 reg = rd32(hw, I40E_VP_MDET_RX(i));
7234 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
7235 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
7236 vf->num_mdd_events++;
Jesse Brandeburgfaf32972014-07-12 07:28:21 +00007237 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
7238 i);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007239 }
7240
7241 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
7242 dev_info(&pf->pdev->dev,
7243 "Too many MDD events on VF %d, disabled\n", i);
7244 dev_info(&pf->pdev->dev,
7245 "Use PF Control I/F to re-enable the VF\n");
7246 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
7247 }
7248 }
7249
7250 /* re-enable mdd interrupt cause */
7251 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
7252 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
7253 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
7254 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
7255 i40e_flush(hw);
7256}
7257
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007258/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08007259 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007260 * @pf: board private structure
7261 **/
Singhai, Anjali6a899022015-12-14 12:21:18 -08007262static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007263{
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007264 struct i40e_hw *hw = &pf->hw;
7265 i40e_status ret;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007266 u16 port;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007267 int i;
7268
Singhai, Anjali6a899022015-12-14 12:21:18 -08007269 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007270 return;
7271
Singhai, Anjali6a899022015-12-14 12:21:18 -08007272 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007273
7274 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08007275 if (pf->pending_udp_bitmap & BIT_ULL(i)) {
7276 pf->pending_udp_bitmap &= ~BIT_ULL(i);
7277 port = pf->udp_ports[i].index;
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007278 if (port)
Carolyn Wybornyb3f5c7b2016-08-24 11:33:51 -07007279 ret = i40e_aq_add_udp_tunnel(hw, port,
7280 pf->udp_ports[i].type,
7281 NULL, NULL);
Shannon Nelsonc22c06c2015-03-31 00:45:04 -07007282 else
7283 ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007284
7285 if (ret) {
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007286 dev_dbg(&pf->pdev->dev,
7287 "%s %s port %d, index %d failed, err %s aq_err %s\n",
7288 pf->udp_ports[i].type ? "vxlan" : "geneve",
7289 port ? "add" : "delete",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08007290 port, i,
Carolyn Wyborny730a8f82016-02-17 16:12:16 -08007291 i40e_stat_str(&pf->hw, ret),
7292 i40e_aq_str(&pf->hw,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04007293 pf->hw.aq.asq_last_status));
Singhai, Anjali6a899022015-12-14 12:21:18 -08007294 pf->udp_ports[i].index = 0;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00007295 }
7296 }
7297 }
7298}
7299
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007300/**
7301 * i40e_service_task - Run the driver's async subtasks
7302 * @work: pointer to work_struct containing our data
7303 **/
7304static void i40e_service_task(struct work_struct *work)
7305{
7306 struct i40e_pf *pf = container_of(work,
7307 struct i40e_pf,
7308 service_task);
7309 unsigned long start_time = jiffies;
7310
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007311 /* don't bother with service tasks if a reset is in progress */
7312 if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
Shannon Nelsone57a2fe2014-06-03 23:50:19 +00007313 return;
7314 }
7315
Mitch Williams91089032016-11-21 13:03:51 -08007316 if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
7317 return;
7318
Kiran Patilb03a8c12015-09-24 18:13:15 -04007319 i40e_detect_recover_hung(pf);
Jesse Brandeburg2818ccd2016-01-13 16:51:38 -08007320 i40e_sync_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007321 i40e_reset_subtask(pf);
7322 i40e_handle_mdd_event(pf);
7323 i40e_vc_process_vflr_event(pf);
7324 i40e_watchdog_subtask(pf);
7325 i40e_fdir_reinit_subtask(pf);
Mitch Williams0ef2d5a2017-01-24 10:24:00 -08007326 if (pf->flags & I40E_FLAG_CLIENT_RESET) {
7327 /* Client subtask will reopen next time through. */
7328 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
7329 pf->flags &= ~I40E_FLAG_CLIENT_RESET;
7330 } else {
7331 i40e_client_subtask(pf);
7332 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
7333 i40e_notify_client_of_l2_param_changes(
7334 pf->vsi[pf->lan_vsi]);
7335 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
7336 }
7337 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007338 i40e_sync_filters_subtask(pf);
Singhai, Anjali6a899022015-12-14 12:21:18 -08007339 i40e_sync_udp_filters_subtask(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007340 i40e_clean_adminq_subtask(pf);
7341
Mitch Williams91089032016-11-21 13:03:51 -08007342 /* flush memory to make sure state is correct before next watchdog */
7343 smp_mb__before_atomic();
7344 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007345
7346 /* If the tasks have taken longer than one timer cycle or there
7347 * is more work to be done, reschedule the service task now
7348 * rather than wait for the timer to tick again.
7349 */
7350 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
7351 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
7352 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
7353 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
7354 i40e_service_event_schedule(pf);
7355}
7356
7357/**
7358 * i40e_service_timer - timer callback
7359 * @data: pointer to PF struct
7360 **/
7361static void i40e_service_timer(unsigned long data)
7362{
7363 struct i40e_pf *pf = (struct i40e_pf *)data;
7364
7365 mod_timer(&pf->service_timer,
7366 round_jiffies(jiffies + pf->service_timer_period));
7367 i40e_service_event_schedule(pf);
7368}
7369
7370/**
7371 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
7372 * @vsi: the VSI being configured
7373 **/
7374static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
7375{
7376 struct i40e_pf *pf = vsi->back;
7377
7378 switch (vsi->type) {
7379 case I40E_VSI_MAIN:
7380 vsi->alloc_queue_pairs = pf->num_lan_qps;
7381 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7382 I40E_REQ_DESCRIPTOR_MULTIPLE);
7383 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7384 vsi->num_q_vectors = pf->num_lan_msix;
7385 else
7386 vsi->num_q_vectors = 1;
7387
7388 break;
7389
7390 case I40E_VSI_FDIR:
7391 vsi->alloc_queue_pairs = 1;
7392 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
7393 I40E_REQ_DESCRIPTOR_MULTIPLE);
Tushar Davea70e4072016-05-16 12:40:53 -07007394 vsi->num_q_vectors = pf->num_fdsb_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007395 break;
7396
7397 case I40E_VSI_VMDQ2:
7398 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
7399 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7400 I40E_REQ_DESCRIPTOR_MULTIPLE);
7401 vsi->num_q_vectors = pf->num_vmdq_msix;
7402 break;
7403
7404 case I40E_VSI_SRIOV:
7405 vsi->alloc_queue_pairs = pf->num_vf_qps;
7406 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
7407 I40E_REQ_DESCRIPTOR_MULTIPLE);
7408 break;
7409
7410 default:
7411 WARN_ON(1);
7412 return -ENODATA;
7413 }
7414
7415 return 0;
7416}
7417
7418/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007419 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
7420 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007421 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007422 *
7423 * On error: returns error code (negative)
7424 * On success: returns 0
7425 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007426static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007427{
7428 int size;
7429 int ret = 0;
7430
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007431 /* allocate memory for both Tx and Rx ring pointers */
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007432 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
7433 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
7434 if (!vsi->tx_rings)
7435 return -ENOMEM;
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007436 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
7437
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007438 if (alloc_qvectors) {
7439 /* allocate memory for q_vector pointers */
Julia Lawallf57e4fb2014-07-30 03:11:09 +00007440 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007441 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
7442 if (!vsi->q_vectors) {
7443 ret = -ENOMEM;
7444 goto err_vectors;
7445 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007446 }
7447 return ret;
7448
7449err_vectors:
7450 kfree(vsi->tx_rings);
7451 return ret;
7452}
7453
7454/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007455 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
7456 * @pf: board private structure
7457 * @type: type of VSI
7458 *
7459 * On error: returns error code (negative)
7460 * On success: returns vsi index in PF (positive)
7461 **/
7462static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
7463{
7464 int ret = -ENODEV;
7465 struct i40e_vsi *vsi;
7466 int vsi_idx;
7467 int i;
7468
7469 /* Need to protect the allocation of the VSIs at the PF level */
7470 mutex_lock(&pf->switch_mutex);
7471
7472 /* VSI list may be fragmented if VSI creation/destruction has
7473 * been happening. We can afford to do a quick scan to look
7474 * for any free VSIs in the list.
7475 *
7476 * find next empty vsi slot, looping back around if necessary
7477 */
7478 i = pf->next_vsi;
Mitch Williams505682c2014-05-20 08:01:37 +00007479 while (i < pf->num_alloc_vsi && pf->vsi[i])
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007480 i++;
Mitch Williams505682c2014-05-20 08:01:37 +00007481 if (i >= pf->num_alloc_vsi) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007482 i = 0;
7483 while (i < pf->next_vsi && pf->vsi[i])
7484 i++;
7485 }
7486
Mitch Williams505682c2014-05-20 08:01:37 +00007487 if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007488 vsi_idx = i; /* Found one! */
7489 } else {
7490 ret = -ENODEV;
Alexander Duyck493fb302013-09-28 07:01:44 +00007491 goto unlock_pf; /* out of VSI slots! */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007492 }
7493 pf->next_vsi = ++i;
7494
7495 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
7496 if (!vsi) {
7497 ret = -ENOMEM;
Alexander Duyck493fb302013-09-28 07:01:44 +00007498 goto unlock_pf;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007499 }
7500 vsi->type = type;
7501 vsi->back = pf;
7502 set_bit(__I40E_DOWN, &vsi->state);
7503 vsi->flags = 0;
7504 vsi->idx = vsi_idx;
Jesse Brandeburgac26fc12015-09-28 14:12:37 -04007505 vsi->int_rate_limit = 0;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00007506 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
7507 pf->rss_table_size : 64;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007508 vsi->netdev_registered = false;
7509 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
Jacob Keller278e7d02016-10-05 09:30:37 -07007510 hash_init(vsi->mac_filter_hash);
Shannon Nelson63741842014-04-23 04:50:16 +00007511 vsi->irqs_ready = false;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007512
Alexander Duyck9f65e152013-09-28 06:00:58 +00007513 ret = i40e_set_num_rings_in_vsi(vsi);
7514 if (ret)
7515 goto err_rings;
7516
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007517 ret = i40e_vsi_alloc_arrays(vsi, true);
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007518 if (ret)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007519 goto err_rings;
Alexander Duyck493fb302013-09-28 07:01:44 +00007520
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007521 /* Setup default MSIX irq handler for VSI */
7522 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
7523
Kiran Patil21659032015-09-30 14:09:03 -04007524 /* Initialize VSI lock */
Jacob Keller278e7d02016-10-05 09:30:37 -07007525 spin_lock_init(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007526 pf->vsi[vsi_idx] = vsi;
7527 ret = vsi_idx;
Alexander Duyck493fb302013-09-28 07:01:44 +00007528 goto unlock_pf;
7529
Alexander Duyck9f65e152013-09-28 06:00:58 +00007530err_rings:
Alexander Duyck493fb302013-09-28 07:01:44 +00007531 pf->next_vsi = i - 1;
7532 kfree(vsi);
7533unlock_pf:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007534 mutex_unlock(&pf->switch_mutex);
7535 return ret;
7536}
7537
7538/**
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007539 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
7540 * @type: VSI pointer
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007541 * @free_qvectors: a bool to specify if q_vectors need to be freed.
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007542 *
7543 * On error: returns error code (negative)
7544 * On success: returns 0
7545 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007546static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007547{
7548 /* free the ring and vector containers */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007549 if (free_qvectors) {
7550 kfree(vsi->q_vectors);
7551 vsi->q_vectors = NULL;
7552 }
Anjali Singhai Jainf650a382013-11-20 10:02:55 +00007553 kfree(vsi->tx_rings);
7554 vsi->tx_rings = NULL;
7555 vsi->rx_rings = NULL;
7556}
7557
7558/**
Helin Zhang28c58692015-10-26 19:44:27 -04007559 * i40e_clear_rss_config_user - clear the user configured RSS hash keys
7560 * and lookup table
7561 * @vsi: Pointer to VSI structure
7562 */
7563static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
7564{
7565 if (!vsi)
7566 return;
7567
7568 kfree(vsi->rss_hkey_user);
7569 vsi->rss_hkey_user = NULL;
7570
7571 kfree(vsi->rss_lut_user);
7572 vsi->rss_lut_user = NULL;
7573}
7574
7575/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007576 * i40e_vsi_clear - Deallocate the VSI provided
7577 * @vsi: the VSI being un-configured
7578 **/
7579static int i40e_vsi_clear(struct i40e_vsi *vsi)
7580{
7581 struct i40e_pf *pf;
7582
7583 if (!vsi)
7584 return 0;
7585
7586 if (!vsi->back)
7587 goto free_vsi;
7588 pf = vsi->back;
7589
7590 mutex_lock(&pf->switch_mutex);
7591 if (!pf->vsi[vsi->idx]) {
7592 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
7593 vsi->idx, vsi->idx, vsi, vsi->type);
7594 goto unlock_vsi;
7595 }
7596
7597 if (pf->vsi[vsi->idx] != vsi) {
7598 dev_err(&pf->pdev->dev,
7599 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
7600 pf->vsi[vsi->idx]->idx,
7601 pf->vsi[vsi->idx],
7602 pf->vsi[vsi->idx]->type,
7603 vsi->idx, vsi, vsi->type);
7604 goto unlock_vsi;
7605 }
7606
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00007607 /* updates the PF for this cleared vsi */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007608 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
7609 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
7610
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00007611 i40e_vsi_free_arrays(vsi, true);
Helin Zhang28c58692015-10-26 19:44:27 -04007612 i40e_clear_rss_config_user(vsi);
Alexander Duyck493fb302013-09-28 07:01:44 +00007613
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007614 pf->vsi[vsi->idx] = NULL;
7615 if (vsi->idx < pf->next_vsi)
7616 pf->next_vsi = vsi->idx;
7617
7618unlock_vsi:
7619 mutex_unlock(&pf->switch_mutex);
7620free_vsi:
7621 kfree(vsi);
7622
7623 return 0;
7624}
7625
7626/**
Alexander Duyck9f65e152013-09-28 06:00:58 +00007627 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
7628 * @vsi: the VSI being cleaned
7629 **/
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007630static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
Alexander Duyck9f65e152013-09-28 06:00:58 +00007631{
7632 int i;
7633
Greg Rose8e9dca52013-12-18 13:45:53 +00007634 if (vsi->tx_rings && vsi->tx_rings[0]) {
Neerav Parikhd7397642013-11-28 06:39:37 +00007635 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Mitch Williams00403f02013-09-28 07:13:13 +00007636 kfree_rcu(vsi->tx_rings[i], rcu);
7637 vsi->tx_rings[i] = NULL;
7638 vsi->rx_rings[i] = NULL;
7639 }
Shannon Nelsonbe1d5ee2013-11-28 06:39:23 +00007640 }
Alexander Duyck9f65e152013-09-28 06:00:58 +00007641}
7642
7643/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007644 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
7645 * @vsi: the VSI being configured
7646 **/
7647static int i40e_alloc_rings(struct i40e_vsi *vsi)
7648{
Akeem G Abodunrine7046ee2014-04-09 05:58:58 +00007649 struct i40e_ring *tx_ring, *rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007650 struct i40e_pf *pf = vsi->back;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007651 int i;
7652
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007653 /* Set basic values in the rings to be used later during open() */
Neerav Parikhd7397642013-11-28 06:39:37 +00007654 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
Shannon Nelsonac6c5e32013-11-20 10:02:57 +00007655 /* allocate space for both Tx and Rx in one shot */
Alexander Duyck9f65e152013-09-28 06:00:58 +00007656 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
7657 if (!tx_ring)
7658 goto err_out;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007659
7660 tx_ring->queue_index = i;
7661 tx_ring->reg_idx = vsi->base_queue + i;
7662 tx_ring->ring_active = false;
7663 tx_ring->vsi = vsi;
7664 tx_ring->netdev = vsi->netdev;
7665 tx_ring->dev = &pf->pdev->dev;
7666 tx_ring->count = vsi->num_desc;
7667 tx_ring->size = 0;
7668 tx_ring->dcb_tc = 0;
Anjali Singhai Jain8e0764b2015-06-05 12:20:30 -04007669 if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
7670 tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
Kan Lianga75e8002016-02-19 09:24:04 -05007671 tx_ring->tx_itr_setting = pf->tx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007672 vsi->tx_rings[i] = tx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007673
Alexander Duyck9f65e152013-09-28 06:00:58 +00007674 rx_ring = &tx_ring[1];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007675 rx_ring->queue_index = i;
7676 rx_ring->reg_idx = vsi->base_queue + i;
7677 rx_ring->ring_active = false;
7678 rx_ring->vsi = vsi;
7679 rx_ring->netdev = vsi->netdev;
7680 rx_ring->dev = &pf->pdev->dev;
7681 rx_ring->count = vsi->num_desc;
7682 rx_ring->size = 0;
7683 rx_ring->dcb_tc = 0;
Kan Lianga75e8002016-02-19 09:24:04 -05007684 rx_ring->rx_itr_setting = pf->rx_itr_default;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007685 vsi->rx_rings[i] = rx_ring;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007686 }
7687
7688 return 0;
Alexander Duyck9f65e152013-09-28 06:00:58 +00007689
7690err_out:
7691 i40e_vsi_clear_rings(vsi);
7692 return -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007693}
7694
7695/**
7696 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
7697 * @pf: board private structure
7698 * @vectors: the number of MSI-X vectors to request
7699 *
7700 * Returns the number of vectors reserved, or error
7701 **/
7702static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
7703{
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007704 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
7705 I40E_MIN_MSIX, vectors);
7706 if (vectors < 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007707 dev_info(&pf->pdev->dev,
Alexander Gordeev7b37f372014-02-18 11:11:42 +01007708 "MSI-X vector reservation failed: %d\n", vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007709 vectors = 0;
7710 }
7711
7712 return vectors;
7713}
7714
7715/**
7716 * i40e_init_msix - Setup the MSIX capability
7717 * @pf: board private structure
7718 *
7719 * Work with the OS to set up the MSIX vectors needed.
7720 *
Shannon Nelson3b444392015-02-26 16:15:57 +00007721 * Returns the number of vectors reserved or negative on failure
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007722 **/
7723static int i40e_init_msix(struct i40e_pf *pf)
7724{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007725 struct i40e_hw *hw = &pf->hw;
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007726 int cpus, extra_vectors;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007727 int vectors_left;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007728 int v_budget, i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007729 int v_actual;
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007730 int iwarp_requested = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007731
7732 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7733 return -ENODEV;
7734
7735 /* The number of vectors we'll request will be comprised of:
7736 * - Add 1 for "other" cause for Admin Queue events, etc.
7737 * - The number of LAN queue pairs
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00007738 * - Queues being used for RSS.
7739 * We don't need as many as max_rss_size vectors.
7740 * use rss_size instead in the calculation since that
7741 * is governed by number of cpus in the system.
7742 * - assumes symmetric Tx/Rx pairing
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007743 * - The number of VMDq pairs
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007744 * - The CPU count within the NUMA node if iWARP is enabled
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007745 * Once we count this up, try the request.
7746 *
7747 * If we can't get what we want, we'll simplify to nearly nothing
7748 * and try again. If that still fails, we punt.
7749 */
Shannon Nelson1e200e42015-02-27 09:15:24 +00007750 vectors_left = hw->func_caps.num_msix_vectors;
7751 v_budget = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007752
Shannon Nelson1e200e42015-02-27 09:15:24 +00007753 /* reserve one vector for miscellaneous handler */
7754 if (vectors_left) {
7755 v_budget++;
7756 vectors_left--;
7757 }
7758
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007759 /* reserve some vectors for the main PF traffic queues. Initially we
7760 * only reserve at most 50% of the available vectors, in the case that
7761 * the number of online CPUs is large. This ensures that we can enable
7762 * extra features as well. Once we've enabled the other features, we
7763 * will use any remaining vectors to reach as close as we can to the
7764 * number of online CPUs.
7765 */
7766 cpus = num_online_cpus();
7767 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
Shannon Nelson1e200e42015-02-27 09:15:24 +00007768 vectors_left -= pf->num_lan_msix;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007769
7770 /* reserve one vector for sideband flow director */
7771 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7772 if (vectors_left) {
Tushar Davea70e4072016-05-16 12:40:53 -07007773 pf->num_fdsb_msix = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007774 v_budget++;
7775 vectors_left--;
7776 } else {
Tushar Davea70e4072016-05-16 12:40:53 -07007777 pf->num_fdsb_msix = 0;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007778 }
7779 }
John W Linville83840e42015-01-14 03:06:28 +00007780
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007781 /* can we reserve enough for iWARP? */
7782 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007783 iwarp_requested = pf->num_iwarp_msix;
7784
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007785 if (!vectors_left)
7786 pf->num_iwarp_msix = 0;
7787 else if (vectors_left < pf->num_iwarp_msix)
7788 pf->num_iwarp_msix = 1;
7789 v_budget += pf->num_iwarp_msix;
7790 vectors_left -= pf->num_iwarp_msix;
7791 }
7792
Shannon Nelson1e200e42015-02-27 09:15:24 +00007793 /* any vectors left over go for VMDq support */
7794 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
7795 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
7796 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
7797
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007798 if (!vectors_left) {
7799 pf->num_vmdq_msix = 0;
7800 pf->num_vmdq_qps = 0;
7801 } else {
7802 /* if we're short on vectors for what's desired, we limit
7803 * the queues per vmdq. If this is still more than are
7804 * available, the user will need to change the number of
7805 * queues/vectors used by the PF later with the ethtool
7806 * channels command
7807 */
7808 if (vmdq_vecs < vmdq_vecs_wanted)
7809 pf->num_vmdq_qps = 1;
7810 pf->num_vmdq_msix = pf->num_vmdq_qps;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007811
Stefan Assmann9ca57e92016-09-19 13:37:49 +02007812 v_budget += vmdq_vecs;
7813 vectors_left -= vmdq_vecs;
7814 }
Shannon Nelson1e200e42015-02-27 09:15:24 +00007815 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007816
Jacob Kellerc0cf70a2017-01-24 10:24:01 -08007817 /* On systems with a large number of SMP cores, we previously limited
7818 * the number of vectors for num_lan_msix to be at most 50% of the
7819 * available vectors, to allow for other features. Now, we add back
7820 * the remaining vectors. However, we ensure that the total
7821 * num_lan_msix will not exceed num_online_cpus(). To do this, we
7822 * calculate the number of vectors we can add without going over the
7823 * cap of CPUs. For systems with a small number of CPUs this will be
7824 * zero.
7825 */
7826 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
7827 pf->num_lan_msix += extra_vectors;
7828 vectors_left -= extra_vectors;
7829
7830 WARN(vectors_left < 0,
7831 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
7832
7833 v_budget += pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007834 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
7835 GFP_KERNEL);
7836 if (!pf->msix_entries)
7837 return -ENOMEM;
7838
7839 for (i = 0; i < v_budget; i++)
7840 pf->msix_entries[i].entry = i;
Shannon Nelson3b444392015-02-26 16:15:57 +00007841 v_actual = i40e_reserve_msix_vectors(pf, v_budget);
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007842
Shannon Nelson3b444392015-02-26 16:15:57 +00007843 if (v_actual < I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007844 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
7845 kfree(pf->msix_entries);
7846 pf->msix_entries = NULL;
Guilherme G Piccoli4c95aa52016-09-22 10:03:58 -03007847 pci_disable_msix(pf->pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007848 return -ENODEV;
7849
Shannon Nelson3b444392015-02-26 16:15:57 +00007850 } else if (v_actual == I40E_MIN_MSIX) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007851 /* Adjust for minimal MSIX use */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007852 pf->num_vmdq_vsis = 0;
7853 pf->num_vmdq_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007854 pf->num_lan_qps = 1;
7855 pf->num_lan_msix = 1;
7856
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007857 } else if (!vectors_left) {
7858 /* If we have limited resources, we will start with no vectors
7859 * for the special features and then allocate vectors to some
7860 * of these features based on the policy and at the end disable
7861 * the features that did not get any vectors.
7862 */
Shannon Nelson3b444392015-02-26 16:15:57 +00007863 int vec;
7864
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007865 dev_info(&pf->pdev->dev,
7866 "MSI-X vector limit reached, attempting to redistribute vectors\n");
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007867 /* reserve the misc vector */
Shannon Nelson3b444392015-02-26 16:15:57 +00007868 vec = v_actual - 1;
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007869
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007870 /* Scale vector usage down */
7871 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007872 pf->num_vmdq_vsis = 1;
Shannon Nelson1e200e42015-02-27 09:15:24 +00007873 pf->num_vmdq_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007874
7875 /* partition out the remaining vectors */
7876 switch (vec) {
7877 case 2:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007878 pf->num_lan_msix = 1;
7879 break;
7880 case 3:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007881 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7882 pf->num_lan_msix = 1;
7883 pf->num_iwarp_msix = 1;
7884 } else {
7885 pf->num_lan_msix = 2;
7886 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007887 break;
7888 default:
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007889 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
7890 pf->num_iwarp_msix = min_t(int, (vec / 3),
7891 iwarp_requested);
7892 pf->num_vmdq_vsis = min_t(int, (vec / 3),
7893 I40E_DEFAULT_NUM_VMDQ_VSI);
7894 } else {
7895 pf->num_vmdq_vsis = min_t(int, (vec / 2),
7896 I40E_DEFAULT_NUM_VMDQ_VSI);
7897 }
Stefan Assmannabd97a92016-09-19 13:37:51 +02007898 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
7899 pf->num_fdsb_msix = 1;
7900 vec--;
7901 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007902 pf->num_lan_msix = min_t(int,
7903 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
7904 pf->num_lan_msix);
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007905 pf->num_lan_qps = pf->num_lan_msix;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007906 break;
7907 }
7908 }
7909
Stefan Assmannabd97a92016-09-19 13:37:51 +02007910 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
7911 (pf->num_fdsb_msix == 0)) {
7912 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
7913 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
7914 }
Anjali Singhai Jaina34977b2014-05-21 23:32:43 +00007915 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7916 (pf->num_vmdq_msix == 0)) {
7917 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
7918 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
7919 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06007920
7921 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
7922 (pf->num_iwarp_msix == 0)) {
7923 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
7924 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
7925 }
Stefan Assmann4ce20ab2016-09-19 13:37:50 +02007926 i40e_debug(&pf->hw, I40E_DEBUG_INIT,
7927 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
7928 pf->num_lan_msix,
7929 pf->num_vmdq_msix * pf->num_vmdq_vsis,
7930 pf->num_fdsb_msix,
7931 pf->num_iwarp_msix);
7932
Shannon Nelson3b444392015-02-26 16:15:57 +00007933 return v_actual;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007934}
7935
7936/**
Greg Rose90e04072014-03-06 08:59:57 +00007937 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
Alexander Duyck493fb302013-09-28 07:01:44 +00007938 * @vsi: the VSI being configured
7939 * @v_idx: index of the vector in the vsi struct
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007940 * @cpu: cpu to be used on affinity_mask
Alexander Duyck493fb302013-09-28 07:01:44 +00007941 *
7942 * We allocate one q_vector. If allocation fails we return -ENOMEM.
7943 **/
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007944static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
Alexander Duyck493fb302013-09-28 07:01:44 +00007945{
7946 struct i40e_q_vector *q_vector;
7947
7948 /* allocate q_vector */
7949 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
7950 if (!q_vector)
7951 return -ENOMEM;
7952
7953 q_vector->vsi = vsi;
7954 q_vector->v_idx = v_idx;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007955 cpumask_set_cpu(cpu, &q_vector->affinity_mask);
7956
Alexander Duyck493fb302013-09-28 07:01:44 +00007957 if (vsi->netdev)
7958 netif_napi_add(vsi->netdev, &q_vector->napi,
Jesse Brandeburgeefeace2014-05-10 04:49:13 +00007959 i40e_napi_poll, NAPI_POLL_WEIGHT);
Alexander Duyck493fb302013-09-28 07:01:44 +00007960
Alexander Duyckcd0b6fa2013-09-28 06:00:53 +00007961 q_vector->rx.latency_range = I40E_LOW_LATENCY;
7962 q_vector->tx.latency_range = I40E_LOW_LATENCY;
7963
Alexander Duyck493fb302013-09-28 07:01:44 +00007964 /* tie q_vector and vsi together */
7965 vsi->q_vectors[v_idx] = q_vector;
7966
7967 return 0;
7968}
7969
7970/**
Greg Rose90e04072014-03-06 08:59:57 +00007971 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007972 * @vsi: the VSI being configured
7973 *
7974 * We allocate one q_vector per queue interrupt. If allocation fails we
7975 * return -ENOMEM.
7976 **/
Greg Rose90e04072014-03-06 08:59:57 +00007977static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007978{
7979 struct i40e_pf *pf = vsi->back;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007980 int err, v_idx, num_q_vectors, current_cpu;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007981
7982 /* if not MSIX, give the one vector only to the LAN VSI */
7983 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7984 num_q_vectors = vsi->num_q_vectors;
7985 else if (vsi == pf->vsi[pf->lan_vsi])
7986 num_q_vectors = 1;
7987 else
7988 return -EINVAL;
7989
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007990 current_cpu = cpumask_first(cpu_online_mask);
7991
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007992 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007993 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
Alexander Duyck493fb302013-09-28 07:01:44 +00007994 if (err)
7995 goto err_out;
Guilherme G. Piccoli7f6c5532016-06-27 12:16:43 -03007996 current_cpu = cpumask_next(current_cpu, cpu_online_mask);
7997 if (unlikely(current_cpu >= nr_cpu_ids))
7998 current_cpu = cpumask_first(cpu_online_mask);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00007999 }
8000
8001 return 0;
Alexander Duyck493fb302013-09-28 07:01:44 +00008002
8003err_out:
8004 while (v_idx--)
8005 i40e_free_q_vector(vsi, v_idx);
8006
8007 return err;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008008}
8009
8010/**
8011 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
8012 * @pf: board private structure to initialize
8013 **/
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008014static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008015{
Shannon Nelson3b444392015-02-26 16:15:57 +00008016 int vectors = 0;
8017 ssize_t size;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008018
8019 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
Shannon Nelson3b444392015-02-26 16:15:57 +00008020 vectors = i40e_init_msix(pf);
8021 if (vectors < 0) {
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008022 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008023 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008024 I40E_FLAG_RSS_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +00008025 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -07008026 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -08008027 I40E_FLAG_SRIOV_ENABLED |
8028 I40E_FLAG_FD_SB_ENABLED |
8029 I40E_FLAG_FD_ATR_ENABLED |
8030 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008031
8032 /* rework the queue expectations without MSIX */
8033 i40e_determine_queue_usage(pf);
8034 }
8035 }
8036
8037 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
8038 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008039 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
Shannon Nelson3b444392015-02-26 16:15:57 +00008040 vectors = pci_enable_msi(pf->pdev);
8041 if (vectors < 0) {
8042 dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
8043 vectors);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008044 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
8045 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008046 vectors = 1; /* one MSI or Legacy vector */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008047 }
8048
Shannon Nelson958a3e32013-09-28 07:13:28 +00008049 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008050 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
Shannon Nelson958a3e32013-09-28 07:13:28 +00008051
Shannon Nelson3b444392015-02-26 16:15:57 +00008052 /* set up vector assignment tracking */
8053 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
8054 pf->irq_pile = kzalloc(size, GFP_KERNEL);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008055 if (!pf->irq_pile) {
8056 dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
8057 return -ENOMEM;
8058 }
Shannon Nelson3b444392015-02-26 16:15:57 +00008059 pf->irq_pile->num_entries = vectors;
8060 pf->irq_pile->search_hint = 0;
8061
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008062 /* track first vector for misc interrupts, ignore return */
Shannon Nelson3b444392015-02-26 16:15:57 +00008063 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
Jesse Brandeburgc11472802015-04-07 19:45:39 -04008064
8065 return 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008066}
8067
8068/**
8069 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
8070 * @pf: board private structure
8071 *
8072 * This sets up the handler for MSIX 0, which is used to manage the
8073 * non-queue interrupts, e.g. AdminQ and errors. This is not used
8074 * when in MSI or Legacy interrupt mode.
8075 **/
8076static int i40e_setup_misc_vector(struct i40e_pf *pf)
8077{
8078 struct i40e_hw *hw = &pf->hw;
8079 int err = 0;
8080
8081 /* Only request the irq if this is the first time through, and
8082 * not when we're rebuilding after a Reset
8083 */
8084 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
8085 err = request_irq(pf->msix_entries[0].vector,
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008086 i40e_intr, 0, pf->int_name, pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008087 if (err) {
8088 dev_info(&pf->pdev->dev,
Catherine Sullivan77fa28b2014-02-20 19:29:17 -08008089 "request_irq for %s failed: %d\n",
Carolyn Wybornyb294ac72014-12-11 07:06:39 +00008090 pf->int_name, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008091 return -EFAULT;
8092 }
8093 }
8094
Jacob Kellerab437b52014-12-14 01:55:08 +00008095 i40e_enable_misc_int_causes(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008096
8097 /* associate no queues to the misc vector */
8098 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
8099 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
8100
8101 i40e_flush(hw);
8102
Jesse Brandeburg40d72a52016-01-13 16:51:45 -08008103 i40e_irq_dynamic_enable_icr0(pf, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008104
8105 return err;
8106}
8107
8108/**
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008109 * i40e_config_rss_aq - Prepare for RSS using AQ commands
8110 * @vsi: vsi structure
8111 * @seed: RSS hash seed
8112 **/
Helin Zhange69ff812015-10-21 19:56:22 -04008113static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8114 u8 *lut, u16 lut_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008115{
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008116 struct i40e_pf *pf = vsi->back;
8117 struct i40e_hw *hw = &pf->hw;
Jacob Keller776b2e12016-07-19 16:23:30 -07008118 int ret = 0;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008119
Jacob Keller776b2e12016-07-19 16:23:30 -07008120 if (seed) {
8121 struct i40e_aqc_get_set_rss_key_data *seed_dw =
8122 (struct i40e_aqc_get_set_rss_key_data *)seed;
8123 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
8124 if (ret) {
8125 dev_info(&pf->pdev->dev,
8126 "Cannot set RSS key, err %s aq_err %s\n",
8127 i40e_stat_str(hw, ret),
8128 i40e_aq_str(hw, hw->aq.asq_last_status));
8129 return ret;
8130 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008131 }
Jacob Keller776b2e12016-07-19 16:23:30 -07008132 if (lut) {
8133 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008134
Jacob Keller776b2e12016-07-19 16:23:30 -07008135 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8136 if (ret) {
8137 dev_info(&pf->pdev->dev,
8138 "Cannot set RSS lut, err %s aq_err %s\n",
8139 i40e_stat_str(hw, ret),
8140 i40e_aq_str(hw, hw->aq.asq_last_status));
8141 return ret;
8142 }
8143 }
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008144 return ret;
8145}
8146
8147/**
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008148 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
8149 * @vsi: Pointer to vsi structure
8150 * @seed: Buffter to store the hash keys
8151 * @lut: Buffer to store the lookup table entries
8152 * @lut_size: Size of buffer to store the lookup table entries
8153 *
8154 * Return 0 on success, negative on failure
8155 */
8156static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
8157 u8 *lut, u16 lut_size)
8158{
8159 struct i40e_pf *pf = vsi->back;
8160 struct i40e_hw *hw = &pf->hw;
8161 int ret = 0;
8162
8163 if (seed) {
8164 ret = i40e_aq_get_rss_key(hw, vsi->id,
8165 (struct i40e_aqc_get_set_rss_key_data *)seed);
8166 if (ret) {
8167 dev_info(&pf->pdev->dev,
8168 "Cannot get RSS key, err %s aq_err %s\n",
8169 i40e_stat_str(&pf->hw, ret),
8170 i40e_aq_str(&pf->hw,
8171 pf->hw.aq.asq_last_status));
8172 return ret;
8173 }
8174 }
8175
8176 if (lut) {
8177 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
8178
8179 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
8180 if (ret) {
8181 dev_info(&pf->pdev->dev,
8182 "Cannot get RSS lut, err %s aq_err %s\n",
8183 i40e_stat_str(&pf->hw, ret),
8184 i40e_aq_str(&pf->hw,
8185 pf->hw.aq.asq_last_status));
8186 return ret;
8187 }
8188 }
8189
8190 return ret;
8191}
8192
8193/**
Jacob Keller0582b962016-07-19 16:23:29 -07008194 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
8195 * @vsi: VSI structure
8196 **/
8197static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
8198{
8199 u8 seed[I40E_HKEY_ARRAY_SIZE];
8200 struct i40e_pf *pf = vsi->back;
8201 u8 *lut;
8202 int ret;
8203
8204 if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
8205 return 0;
8206
Jacob Keller552b9962016-07-19 16:23:31 -07008207 if (!vsi->rss_size)
8208 vsi->rss_size = min_t(int, pf->alloc_rss_size,
8209 vsi->num_queue_pairs);
8210 if (!vsi->rss_size)
8211 return -EINVAL;
8212
Jacob Keller0582b962016-07-19 16:23:29 -07008213 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8214 if (!lut)
8215 return -ENOMEM;
Jacob Keller552b9962016-07-19 16:23:31 -07008216 /* Use the user configured hash keys and lookup table if there is one,
8217 * otherwise use default
8218 */
8219 if (vsi->rss_lut_user)
8220 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8221 else
8222 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
8223 if (vsi->rss_hkey_user)
8224 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8225 else
8226 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Jacob Keller0582b962016-07-19 16:23:29 -07008227 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
8228 kfree(lut);
8229
8230 return ret;
8231}
8232
8233/**
Helin Zhang043dd652015-10-21 19:56:23 -04008234 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
Helin Zhange69ff812015-10-21 19:56:22 -04008235 * @vsi: Pointer to vsi structure
8236 * @seed: RSS hash seed
8237 * @lut: Lookup table
8238 * @lut_size: Lookup table size
8239 *
8240 * Returns 0 on success, negative on failure
8241 **/
8242static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
8243 const u8 *lut, u16 lut_size)
8244{
8245 struct i40e_pf *pf = vsi->back;
8246 struct i40e_hw *hw = &pf->hw;
Mitch Williamsc4e18682016-04-12 08:30:40 -07008247 u16 vf_id = vsi->vf_id;
Helin Zhange69ff812015-10-21 19:56:22 -04008248 u8 i;
8249
8250 /* Fill out hash function seed */
8251 if (seed) {
8252 u32 *seed_dw = (u32 *)seed;
8253
Mitch Williamsc4e18682016-04-12 08:30:40 -07008254 if (vsi->type == I40E_VSI_MAIN) {
8255 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008256 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008257 } else if (vsi->type == I40E_VSI_SRIOV) {
8258 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008259 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008260 } else {
8261 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
8262 }
Helin Zhange69ff812015-10-21 19:56:22 -04008263 }
8264
8265 if (lut) {
8266 u32 *lut_dw = (u32 *)lut;
8267
Mitch Williamsc4e18682016-04-12 08:30:40 -07008268 if (vsi->type == I40E_VSI_MAIN) {
8269 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8270 return -EINVAL;
8271 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8272 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
8273 } else if (vsi->type == I40E_VSI_SRIOV) {
8274 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
8275 return -EINVAL;
8276 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
Lihong Yang26f77e52017-01-30 12:29:33 -08008277 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
Mitch Williamsc4e18682016-04-12 08:30:40 -07008278 } else {
8279 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8280 }
Helin Zhange69ff812015-10-21 19:56:22 -04008281 }
8282 i40e_flush(hw);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008283
8284 return 0;
8285}
8286
8287/**
Helin Zhang043dd652015-10-21 19:56:23 -04008288 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
8289 * @vsi: Pointer to VSI structure
8290 * @seed: Buffer to store the keys
8291 * @lut: Buffer to store the lookup table entries
8292 * @lut_size: Size of buffer to store the lookup table entries
8293 *
8294 * Returns 0 on success, negative on failure
8295 */
8296static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
8297 u8 *lut, u16 lut_size)
8298{
8299 struct i40e_pf *pf = vsi->back;
8300 struct i40e_hw *hw = &pf->hw;
8301 u16 i;
8302
8303 if (seed) {
8304 u32 *seed_dw = (u32 *)seed;
8305
8306 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008307 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
Helin Zhang043dd652015-10-21 19:56:23 -04008308 }
8309 if (lut) {
8310 u32 *lut_dw = (u32 *)lut;
8311
8312 if (lut_size != I40E_HLUT_ARRAY_SIZE)
8313 return -EINVAL;
8314 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8315 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
8316 }
8317
8318 return 0;
8319}
8320
8321/**
8322 * i40e_config_rss - Configure RSS keys and lut
8323 * @vsi: Pointer to VSI structure
8324 * @seed: RSS hash seed
8325 * @lut: Lookup table
8326 * @lut_size: Lookup table size
8327 *
8328 * Returns 0 on success, negative on failure
8329 */
8330int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8331{
8332 struct i40e_pf *pf = vsi->back;
8333
8334 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8335 return i40e_config_rss_aq(vsi, seed, lut, lut_size);
8336 else
8337 return i40e_config_rss_reg(vsi, seed, lut, lut_size);
8338}
8339
8340/**
8341 * i40e_get_rss - Get RSS keys and lut
8342 * @vsi: Pointer to VSI structure
8343 * @seed: Buffer to store the keys
8344 * @lut: Buffer to store the lookup table entries
8345 * lut_size: Size of buffer to store the lookup table entries
8346 *
8347 * Returns 0 on success, negative on failure
8348 */
8349int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
8350{
Anjali Singhai Jain95a73782015-12-22 14:25:04 -08008351 struct i40e_pf *pf = vsi->back;
8352
8353 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
8354 return i40e_get_rss_aq(vsi, seed, lut, lut_size);
8355 else
8356 return i40e_get_rss_reg(vsi, seed, lut, lut_size);
Helin Zhang043dd652015-10-21 19:56:23 -04008357}
8358
8359/**
Helin Zhange69ff812015-10-21 19:56:22 -04008360 * i40e_fill_rss_lut - Fill the RSS lookup table with default values
8361 * @pf: Pointer to board private structure
8362 * @lut: Lookup table
8363 * @rss_table_size: Lookup table size
8364 * @rss_size: Range of queue number for hashing
8365 */
Alan Bradyf1582352016-08-24 11:33:46 -07008366void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
8367 u16 rss_table_size, u16 rss_size)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008368{
Helin Zhange69ff812015-10-21 19:56:22 -04008369 u16 i;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008370
Helin Zhange69ff812015-10-21 19:56:22 -04008371 for (i = 0; i < rss_table_size; i++)
8372 lut[i] = i % rss_size;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008373}
8374
8375/**
Helin Zhang043dd652015-10-21 19:56:23 -04008376 * i40e_pf_config_rss - Prepare for RSS if used
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008377 * @pf: board private structure
8378 **/
Helin Zhang043dd652015-10-21 19:56:23 -04008379static int i40e_pf_config_rss(struct i40e_pf *pf)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008380{
Anjali Singhai Jain66ddcff2015-02-24 06:58:50 +00008381 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008382 u8 seed[I40E_HKEY_ARRAY_SIZE];
Helin Zhange69ff812015-10-21 19:56:22 -04008383 u8 *lut;
Anjali Singhai Jain4617e8c2013-11-20 10:02:56 +00008384 struct i40e_hw *hw = &pf->hw;
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008385 u32 reg_val;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008386 u64 hena;
Helin Zhange69ff812015-10-21 19:56:22 -04008387 int ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008388
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008389 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008390 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
8391 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008392 hena |= i40e_pf_get_default_rss_hena(pf);
8393
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008394 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
8395 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008396
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008397 /* Determine the RSS table size based on the hardware capabilities */
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008398 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008399 reg_val = (pf->rss_table_size == 512) ?
8400 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
8401 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
Shannon Nelson272cdaf22016-02-17 16:12:21 -08008402 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
Carolyn Wybornye157ea32014-06-03 23:50:22 +00008403
Helin Zhang28c58692015-10-26 19:44:27 -04008404 /* Determine the RSS size of the VSI */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008405 if (!vsi->rss_size) {
8406 u16 qcount;
8407
8408 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8409 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
8410 }
Mitch Williamsa4fa59c2016-09-12 14:18:43 -07008411 if (!vsi->rss_size)
8412 return -EINVAL;
Helin Zhang28c58692015-10-26 19:44:27 -04008413
Helin Zhange69ff812015-10-21 19:56:22 -04008414 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
8415 if (!lut)
8416 return -ENOMEM;
8417
Helin Zhang28c58692015-10-26 19:44:27 -04008418 /* Use user configured lut if there is one, otherwise use default */
8419 if (vsi->rss_lut_user)
8420 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
8421 else
8422 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008423
Helin Zhang28c58692015-10-26 19:44:27 -04008424 /* Use user configured hash key if there is one, otherwise
8425 * use default.
8426 */
8427 if (vsi->rss_hkey_user)
8428 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
8429 else
8430 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
Helin Zhang043dd652015-10-21 19:56:23 -04008431 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
Helin Zhange69ff812015-10-21 19:56:22 -04008432 kfree(lut);
8433
8434 return ret;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008435}
8436
8437/**
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008438 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
8439 * @pf: board private structure
8440 * @queue_count: the requested queue count for rss.
8441 *
8442 * returns 0 if rss is not enabled, if enabled returns the final rss queue
8443 * count which may be different from the requested queue count.
Maciej Sosin373149f2017-04-05 07:50:55 -04008444 * Note: expects to be called while under rtnl_lock()
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008445 **/
8446int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
8447{
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008448 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
8449 int new_rss_size;
8450
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008451 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
8452 return 0;
8453
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008454 new_rss_size = min_t(int, queue_count, pf->rss_size_max);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008455
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008456 if (queue_count != vsi->num_queue_pairs) {
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008457 u16 qcount;
8458
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +00008459 vsi->req_queue_pairs = queue_count;
Maciej Sosin373149f2017-04-05 07:50:55 -04008460 i40e_prep_for_reset(pf, true);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008461
Helin Zhangacd65442015-10-26 19:44:28 -04008462 pf->alloc_rss_size = new_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008463
Maciej Sosin373149f2017-04-05 07:50:55 -04008464 i40e_reset_and_rebuild(pf, true, true);
Helin Zhang28c58692015-10-26 19:44:27 -04008465
8466 /* Discard the user configured hash keys and lut, if less
8467 * queues are enabled.
8468 */
8469 if (queue_count < vsi->rss_size) {
8470 i40e_clear_rss_config_user(vsi);
8471 dev_dbg(&pf->pdev->dev,
8472 "discard user configured hash keys and lut\n");
8473 }
8474
8475 /* Reset vsi->rss_size, as number of enabled queues changed */
Harshitha Ramamurthyf25571b2017-02-21 15:55:40 -08008476 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
8477 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
Helin Zhang28c58692015-10-26 19:44:27 -04008478
Helin Zhang043dd652015-10-21 19:56:23 -04008479 i40e_pf_config_rss(pf);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008480 }
Lihong Yang12815052016-09-27 11:28:48 -07008481 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
8482 vsi->req_queue_pairs, pf->rss_size_max);
Helin Zhangacd65442015-10-26 19:44:28 -04008483 return pf->alloc_rss_size;
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00008484}
8485
8486/**
Greg Rosef4492db2015-02-06 08:52:12 +00008487 * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
8488 * @pf: board private structure
8489 **/
8490i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
8491{
8492 i40e_status status;
8493 bool min_valid, max_valid;
8494 u32 max_bw, min_bw;
8495
8496 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
8497 &min_valid, &max_valid);
8498
8499 if (!status) {
8500 if (min_valid)
8501 pf->npar_min_bw = min_bw;
8502 if (max_valid)
8503 pf->npar_max_bw = max_bw;
8504 }
8505
8506 return status;
8507}
8508
8509/**
8510 * i40e_set_npar_bw_setting - Set BW settings for this PF partition
8511 * @pf: board private structure
8512 **/
8513i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
8514{
8515 struct i40e_aqc_configure_partition_bw_data bw_data;
8516 i40e_status status;
8517
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00008518 /* Set the valid bit for this PF */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008519 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
Greg Rosef4492db2015-02-06 08:52:12 +00008520 bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
8521 bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
8522
8523 /* Set the new bandwidths */
8524 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
8525
8526 return status;
8527}
8528
8529/**
8530 * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
8531 * @pf: board private structure
8532 **/
8533i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
8534{
8535 /* Commit temporary BW setting to permanent NVM image */
8536 enum i40e_admin_queue_err last_aq_status;
8537 i40e_status ret;
8538 u16 nvm_word;
8539
8540 if (pf->hw.partition_id != 1) {
8541 dev_info(&pf->pdev->dev,
8542 "Commit BW only works on partition 1! This is partition %d",
8543 pf->hw.partition_id);
8544 ret = I40E_NOT_SUPPORTED;
8545 goto bw_commit_out;
8546 }
8547
8548 /* Acquire NVM for read access */
8549 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
8550 last_aq_status = pf->hw.aq.asq_last_status;
8551 if (ret) {
8552 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008553 "Cannot acquire NVM for read access, err %s aq_err %s\n",
8554 i40e_stat_str(&pf->hw, ret),
8555 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008556 goto bw_commit_out;
8557 }
8558
8559 /* Read word 0x10 of NVM - SW compatibility word 1 */
8560 ret = i40e_aq_read_nvm(&pf->hw,
8561 I40E_SR_NVM_CONTROL_WORD,
8562 0x10, sizeof(nvm_word), &nvm_word,
8563 false, NULL);
8564 /* Save off last admin queue command status before releasing
8565 * the NVM
8566 */
8567 last_aq_status = pf->hw.aq.asq_last_status;
8568 i40e_release_nvm(&pf->hw);
8569 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008570 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
8571 i40e_stat_str(&pf->hw, ret),
8572 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008573 goto bw_commit_out;
8574 }
8575
8576 /* Wait a bit for NVM release to complete */
8577 msleep(50);
8578
8579 /* Acquire NVM for write access */
8580 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
8581 last_aq_status = pf->hw.aq.asq_last_status;
8582 if (ret) {
8583 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008584 "Cannot acquire NVM for write access, err %s aq_err %s\n",
8585 i40e_stat_str(&pf->hw, ret),
8586 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008587 goto bw_commit_out;
8588 }
8589 /* Write it back out unchanged to initiate update NVM,
8590 * which will force a write of the shadow (alt) RAM to
8591 * the NVM - thus storing the bandwidth values permanently.
8592 */
8593 ret = i40e_aq_update_nvm(&pf->hw,
8594 I40E_SR_NVM_CONTROL_WORD,
8595 0x10, sizeof(nvm_word),
8596 &nvm_word, true, NULL);
8597 /* Save off last admin queue command status before releasing
8598 * the NVM
8599 */
8600 last_aq_status = pf->hw.aq.asq_last_status;
8601 i40e_release_nvm(&pf->hw);
8602 if (ret)
8603 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04008604 "BW settings NOT SAVED, err %s aq_err %s\n",
8605 i40e_stat_str(&pf->hw, ret),
8606 i40e_aq_str(&pf->hw, last_aq_status));
Greg Rosef4492db2015-02-06 08:52:12 +00008607bw_commit_out:
8608
8609 return ret;
8610}
8611
8612/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008613 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
8614 * @pf: board private structure to initialize
8615 *
8616 * i40e_sw_init initializes the Adapter private data structure.
8617 * Fields are initialized based on PCI device information and
8618 * OS network device settings (MTU size).
8619 **/
8620static int i40e_sw_init(struct i40e_pf *pf)
8621{
8622 int err = 0;
8623 int size;
8624
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008625 /* Set default capability flags */
8626 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
8627 I40E_FLAG_MSI_ENABLED |
Mitch Williams2bc7ee82015-02-06 08:52:11 +00008628 I40E_FLAG_MSIX_ENABLED;
8629
Mitch Williamsca99eb92014-04-04 04:43:07 +00008630 /* Set default ITR */
8631 pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
8632 pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
8633
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008634 /* Depending on PF configurations, it is possible that the RSS
8635 * maximum might end up larger than the available queues
8636 */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04008637 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
Helin Zhangacd65442015-10-26 19:44:28 -04008638 pf->alloc_rss_size = 1;
Anjali Singhai Jain5db4cb52015-02-24 06:58:49 +00008639 pf->rss_table_size = pf->hw.func_caps.rss_table_size;
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +00008640 pf->rss_size_max = min_t(int, pf->rss_size_max,
8641 pf->hw.func_caps.num_tx_qp);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008642 if (pf->hw.func_caps.rss) {
8643 pf->flags |= I40E_FLAG_RSS_ENABLED;
Helin Zhangacd65442015-10-26 19:44:28 -04008644 pf->alloc_rss_size = min_t(int, pf->rss_size_max,
8645 num_online_cpus());
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008646 }
8647
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008648 /* MFP mode enabled */
Pawel Orlowskic78b9532015-04-22 19:34:06 -04008649 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008650 pf->flags |= I40E_FLAG_MFP_ENABLED;
8651 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
Greg Rosef4492db2015-02-06 08:52:12 +00008652 if (i40e_get_npar_bw_setting(pf))
8653 dev_warn(&pf->pdev->dev,
8654 "Could not get NPAR bw settings\n");
8655 else
8656 dev_info(&pf->pdev->dev,
8657 "Min BW = %8.8x, Max BW = %8.8x\n",
8658 pf->npar_min_bw, pf->npar_max_bw);
Catherine Sullivan2050bc62013-12-18 13:46:03 +00008659 }
8660
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008661 /* FW/NVM is not yet fixed in this regard */
8662 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
8663 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
8664 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
8665 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008666 if (pf->flags & I40E_FLAG_MFP_ENABLED &&
8667 pf->hw.num_partitions > 1)
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008668 dev_info(&pf->pdev->dev,
Anjali Singhai Jain0b675842014-03-06 08:59:51 +00008669 "Flow Director Sideband mode Disabled in MFP mode\n");
Shannon Nelson6eae9c62015-09-03 17:18:55 -04008670 else
8671 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08008672 pf->fdir_pf_filter_count =
8673 pf->hw.func_caps.fd_filters_guaranteed;
8674 pf->hw.fdir_shared_filter_count =
8675 pf->hw.func_caps.fd_filters_best_effort;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008676 }
8677
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008678 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008679 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008680 (pf->hw.aq.fw_maj_ver < 4))) {
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008681 pf->flags |= I40E_FLAG_RESTART_AUTONEG;
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008682 /* No DCB support for FW < v4.33 */
8683 pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
8684 }
8685
8686 /* Disable FW LLDP if FW < v4.3 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008687 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008688 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
8689 (pf->hw.aq.fw_maj_ver < 4)))
8690 pf->flags |= I40E_FLAG_STOP_FW_LLDP;
8691
8692 /* Use the FW Set LLDP MIB API if FW > v4.40 */
Bimmy Pujari6de432c2016-11-11 12:39:38 -08008693 if ((pf->hw.mac.type == I40E_MAC_XL710) &&
Neerav Parikhf1bbad32016-01-13 16:51:39 -08008694 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
8695 (pf->hw.aq.fw_maj_ver >= 5)))
8696 pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -08008697
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008698 if (pf->hw.func_caps.vmdq) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008699 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04008700 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
Jesse Brandeburge9e53662015-10-02 17:57:21 -07008701 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008702 }
8703
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06008704 if (pf->hw.func_caps.iwarp) {
8705 pf->flags |= I40E_FLAG_IWARP_ENABLED;
8706 /* IWARP needs one extra vector for CQP just like MISC.*/
8707 pf->num_iwarp_msix = (int)num_online_cpus() + 1;
8708 }
8709
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008710#ifdef CONFIG_PCI_IOV
Shannon Nelsonba252f132014-12-11 07:06:34 +00008711 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008712 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
8713 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
8714 pf->num_req_vfs = min_t(int,
8715 pf->hw.func_caps.num_vfs,
8716 I40E_MAX_VF_COUNT);
8717 }
8718#endif /* CONFIG_PCI_IOV */
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008719 if (pf->hw.mac.type == I40E_MAC_X722) {
Joshua Hay1d680052016-12-12 15:44:08 -08008720 pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
8721 | I40E_FLAG_128_QP_RSS_CAPABLE
8722 | I40E_FLAG_HW_ATR_EVICT_CAPABLE
8723 | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
8724 | I40E_FLAG_WB_ON_ITR_CAPABLE
8725 | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
8726 | I40E_FLAG_NO_PCI_LINK_CHECK
8727 | I40E_FLAG_USE_SET_LLDP_MIB
8728 | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
8729 | I40E_FLAG_PTP_L4_CAPABLE
8730 | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
Anjali Singhaia340c782016-01-06 11:49:28 -08008731 } else if ((pf->hw.aq.api_maj_ver > 1) ||
8732 ((pf->hw.aq.api_maj_ver == 1) &&
8733 (pf->hw.aq.api_min_ver > 4))) {
8734 /* Supported in FW API version higher than 1.4 */
8735 pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008736 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jain72b74862016-01-08 17:50:21 -08008737 } else {
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008738 pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
Anjali Singhai Jaind502ce02015-06-05 12:20:26 -04008739 }
Anjali Singhaia340c782016-01-06 11:49:28 -08008740
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008741 pf->eeprom_version = 0xDEAD;
8742 pf->lan_veb = I40E_NO_VEB;
8743 pf->lan_vsi = I40E_NO_VSI;
8744
Anjali Singhai Jaind1a8d272015-07-23 16:54:40 -04008745 /* By default FW has this off for performance reasons */
8746 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
8747
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008748 /* set up queue assignment tracking */
8749 size = sizeof(struct i40e_lump_tracking)
8750 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
8751 pf->qp_pile = kzalloc(size, GFP_KERNEL);
8752 if (!pf->qp_pile) {
8753 err = -ENOMEM;
8754 goto sw_init_done;
8755 }
8756 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
8757 pf->qp_pile->search_hint = 0;
8758
Anjali Singhai Jain327fe042014-06-04 01:23:26 +00008759 pf->tx_timeout_recovery_level = 1;
8760
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008761 mutex_init(&pf->switch_mutex);
8762
Greg Rosec668a122015-02-26 16:10:39 +00008763 /* If NPAR is enabled nudge the Tx scheduler */
8764 if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
8765 i40e_set_npar_bw_setting(pf);
8766
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008767sw_init_done:
8768 return err;
8769}
8770
8771/**
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008772 * i40e_set_ntuple - set the ntuple feature flag and take action
8773 * @pf: board private structure to initialize
8774 * @features: the feature set that the stack is suggesting
8775 *
8776 * returns a bool to indicate if reset needs to happen
8777 **/
8778bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
8779{
8780 bool need_reset = false;
8781
8782 /* Check if Flow Director n-tuple support was enabled or disabled. If
8783 * the state changed, we need to reset.
8784 */
8785 if (features & NETIF_F_NTUPLE) {
8786 /* Enable filters and mark for reset */
8787 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
8788 need_reset = true;
Tushar Davea70e4072016-05-16 12:40:53 -07008789 /* enable FD_SB only if there is MSI-X vector */
8790 if (pf->num_fdsb_msix > 0)
8791 pf->flags |= I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008792 } else {
8793 /* turn off filters, mark for reset and clear SW filter list */
8794 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
8795 need_reset = true;
8796 i40e_fdir_filter_exit(pf);
8797 }
8798 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008799 pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
Anjali Singhai Jain1e1be8f2014-07-10 08:03:26 +00008800 /* reset fd counters */
Jacob Keller097dbf52017-02-06 14:38:46 -08008801 pf->fd_add_err = 0;
8802 pf->fd_atr_cnt = 0;
Anjali Singhai Jain8a4f34f2014-06-04 08:45:20 +00008803 /* if ATR was auto disabled it can be re-enabled. */
8804 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
Harshitha Ramamurthyb77ac972017-02-03 10:57:42 -08008805 (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
8806 pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
Jacob Keller234dc4e2016-09-06 18:05:09 -07008807 if (I40E_DEBUG_FD & pf->hw.debug_mask)
8808 dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
8809 }
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008810 }
8811 return need_reset;
8812}
8813
8814/**
Alan Bradyd8ec9862016-07-27 12:02:38 -07008815 * i40e_clear_rss_lut - clear the rx hash lookup table
8816 * @vsi: the VSI being configured
8817 **/
8818static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
8819{
8820 struct i40e_pf *pf = vsi->back;
8821 struct i40e_hw *hw = &pf->hw;
8822 u16 vf_id = vsi->vf_id;
8823 u8 i;
8824
8825 if (vsi->type == I40E_VSI_MAIN) {
8826 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
8827 wr32(hw, I40E_PFQF_HLUT(i), 0);
8828 } else if (vsi->type == I40E_VSI_SRIOV) {
8829 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
8830 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
8831 } else {
8832 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
8833 }
8834}
8835
8836/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008837 * i40e_set_features - set the netdev feature flags
8838 * @netdev: ptr to the netdev being adjusted
8839 * @features: the feature set that the stack is suggesting
Maciej Sosin373149f2017-04-05 07:50:55 -04008840 * Note: expects to be called while under rtnl_lock()
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008841 **/
8842static int i40e_set_features(struct net_device *netdev,
8843 netdev_features_t features)
8844{
8845 struct i40e_netdev_priv *np = netdev_priv(netdev);
8846 struct i40e_vsi *vsi = np->vsi;
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008847 struct i40e_pf *pf = vsi->back;
8848 bool need_reset;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008849
Alan Bradyd8ec9862016-07-27 12:02:38 -07008850 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
8851 i40e_pf_config_rss(pf);
8852 else if (!(features & NETIF_F_RXHASH) &&
8853 netdev->features & NETIF_F_RXHASH)
8854 i40e_clear_rss_lut(vsi);
8855
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008856 if (features & NETIF_F_HW_VLAN_CTAG_RX)
8857 i40e_vlan_stripping_enable(vsi);
8858 else
8859 i40e_vlan_stripping_disable(vsi);
8860
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008861 need_reset = i40e_set_ntuple(pf, features);
8862
8863 if (need_reset)
Maciej Sosin373149f2017-04-05 07:50:55 -04008864 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
Anjali Singhai Jain7c3c2882014-02-14 02:14:38 +00008865
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00008866 return 0;
8867}
8868
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008869/**
Singhai, Anjali6a899022015-12-14 12:21:18 -08008870 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008871 * @pf: board private structure
8872 * @port: The UDP port to look up
8873 *
8874 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
8875 **/
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008876static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008877{
8878 u8 i;
8879
8880 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
Singhai, Anjali6a899022015-12-14 12:21:18 -08008881 if (pf->udp_ports[i].index == port)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008882 return i;
8883 }
8884
8885 return i;
8886}
8887
8888/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008889 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008890 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008891 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008892 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008893static void i40e_udp_tunnel_add(struct net_device *netdev,
8894 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008895{
8896 struct i40e_netdev_priv *np = netdev_priv(netdev);
8897 struct i40e_vsi *vsi = np->vsi;
8898 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008899 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008900 u8 next_idx;
8901 u8 idx;
8902
Singhai, Anjali6a899022015-12-14 12:21:18 -08008903 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008904
8905 /* Check if port already exists */
8906 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008907 netdev_info(netdev, "port %d already offloaded\n", port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008908 return;
8909 }
8910
8911 /* Now check if there is space to add the new port */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008912 next_idx = i40e_get_udp_port_idx(pf, 0);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008913
8914 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008915 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008916 port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008917 return;
8918 }
8919
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008920 switch (ti->type) {
8921 case UDP_TUNNEL_TYPE_VXLAN:
8922 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
8923 break;
8924 case UDP_TUNNEL_TYPE_GENEVE:
8925 if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
8926 return;
8927 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
8928 break;
8929 default:
8930 return;
8931 }
8932
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008933 /* New port: add it and mark its index in the bitmap */
Singhai, Anjali6a899022015-12-14 12:21:18 -08008934 pf->udp_ports[next_idx].index = port;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008935 pf->pending_udp_bitmap |= BIT_ULL(next_idx);
8936 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008937}
8938
8939/**
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008940 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008941 * @netdev: This physical port's netdev
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008942 * @ti: Tunnel endpoint information
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008943 **/
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008944static void i40e_udp_tunnel_del(struct net_device *netdev,
8945 struct udp_tunnel_info *ti)
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008946{
8947 struct i40e_netdev_priv *np = netdev_priv(netdev);
8948 struct i40e_vsi *vsi = np->vsi;
8949 struct i40e_pf *pf = vsi->back;
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008950 u16 port = ntohs(ti->port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008951 u8 idx;
8952
Singhai, Anjali6a899022015-12-14 12:21:18 -08008953 idx = i40e_get_udp_port_idx(pf, port);
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008954
8955 /* Check if port already exists */
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008956 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
8957 goto not_found;
Jeff Kirshera1c9a9d2013-12-28 07:32:18 +00008958
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008959 switch (ti->type) {
8960 case UDP_TUNNEL_TYPE_VXLAN:
8961 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
8962 goto not_found;
8963 break;
8964 case UDP_TUNNEL_TYPE_GENEVE:
8965 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
8966 goto not_found;
8967 break;
8968 default:
8969 goto not_found;
Singhai, Anjali6a899022015-12-14 12:21:18 -08008970 }
8971
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008972 /* if port exists, set it to 0 (mark for deletion)
8973 * and make it pending
8974 */
8975 pf->udp_ports[idx].index = 0;
8976 pf->pending_udp_bitmap |= BIT_ULL(idx);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008977 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
8978
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07008979 return;
8980not_found:
8981 netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
Jacob Kellerfe0b0cd2017-02-06 14:38:38 -08008982 port);
Singhai, Anjali6a899022015-12-14 12:21:18 -08008983}
8984
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008985static int i40e_get_phys_port_id(struct net_device *netdev,
Jiri Pirko02637fc2014-11-28 14:34:16 +01008986 struct netdev_phys_item_id *ppid)
Neerav Parikh1f224ad2014-02-12 01:45:31 +00008987{
8988 struct i40e_netdev_priv *np = netdev_priv(netdev);
8989 struct i40e_pf *pf = np->vsi->back;
8990 struct i40e_hw *hw = &pf->hw;
8991
8992 if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
8993 return -EOPNOTSUPP;
8994
8995 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
8996 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
8997
8998 return 0;
8999}
9000
Jesse Brandeburg2f90ade2014-11-20 16:30:02 -08009001/**
9002 * i40e_ndo_fdb_add - add an entry to the hardware database
9003 * @ndm: the input from the stack
9004 * @tb: pointer to array of nladdr (unused)
9005 * @dev: the net device pointer
9006 * @addr: the MAC address entry being added
9007 * @flags: instructions from stack about fdb operation
9008 */
Greg Rose4ba0dea2014-03-06 08:59:55 +00009009static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
9010 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01009011 const unsigned char *addr, u16 vid,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009012 u16 flags)
Greg Rose4ba0dea2014-03-06 08:59:55 +00009013{
9014 struct i40e_netdev_priv *np = netdev_priv(dev);
9015 struct i40e_pf *pf = np->vsi->back;
9016 int err = 0;
9017
9018 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
9019 return -EOPNOTSUPP;
9020
Or Gerlitz65891fe2014-12-14 18:19:05 +02009021 if (vid) {
9022 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
9023 return -EINVAL;
9024 }
9025
Greg Rose4ba0dea2014-03-06 08:59:55 +00009026 /* Hardware does not support aging addresses so if a
9027 * ndm_state is given only allow permanent addresses
9028 */
9029 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
9030 netdev_info(dev, "FDB only supports static addresses\n");
9031 return -EINVAL;
9032 }
9033
9034 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
9035 err = dev_uc_add_excl(dev, addr);
9036 else if (is_multicast_ether_addr(addr))
9037 err = dev_mc_add_excl(dev, addr);
9038 else
9039 err = -EINVAL;
9040
9041 /* Only return duplicate errors if NLM_F_EXCL is set */
9042 if (err == -EEXIST && !(flags & NLM_F_EXCL))
9043 err = 0;
9044
9045 return err;
9046}
9047
Neerav Parikh51616012015-02-06 08:52:14 +00009048/**
9049 * i40e_ndo_bridge_setlink - Set the hardware bridge mode
9050 * @dev: the netdev being configured
9051 * @nlh: RTNL message
9052 *
9053 * Inserts a new hardware bridge if not already created and
9054 * enables the bridging mode requested (VEB or VEPA). If the
9055 * hardware bridge has already been inserted and the request
9056 * is to change the mode then that requires a PF reset to
9057 * allow rebuild of the components with required hardware
9058 * bridge mode enabled.
Maciej Sosin373149f2017-04-05 07:50:55 -04009059 *
9060 * Note: expects to be called while under rtnl_lock()
Neerav Parikh51616012015-02-06 08:52:14 +00009061 **/
9062static int i40e_ndo_bridge_setlink(struct net_device *dev,
Carolyn Wyborny9df70b62015-04-27 14:57:11 -04009063 struct nlmsghdr *nlh,
9064 u16 flags)
Neerav Parikh51616012015-02-06 08:52:14 +00009065{
9066 struct i40e_netdev_priv *np = netdev_priv(dev);
9067 struct i40e_vsi *vsi = np->vsi;
9068 struct i40e_pf *pf = vsi->back;
9069 struct i40e_veb *veb = NULL;
9070 struct nlattr *attr, *br_spec;
9071 int i, rem;
9072
9073 /* Only for PF VSI for now */
9074 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9075 return -EOPNOTSUPP;
9076
9077 /* Find the HW bridge for PF VSI */
9078 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9079 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9080 veb = pf->veb[i];
9081 }
9082
9083 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
9084
9085 nla_for_each_nested(attr, br_spec, rem) {
9086 __u16 mode;
9087
9088 if (nla_type(attr) != IFLA_BRIDGE_MODE)
9089 continue;
9090
9091 mode = nla_get_u16(attr);
9092 if ((mode != BRIDGE_MODE_VEPA) &&
9093 (mode != BRIDGE_MODE_VEB))
9094 return -EINVAL;
9095
9096 /* Insert a new HW bridge */
9097 if (!veb) {
9098 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9099 vsi->tc_config.enabled_tc);
9100 if (veb) {
9101 veb->bridge_mode = mode;
9102 i40e_config_bridge_mode(veb);
9103 } else {
9104 /* No Bridge HW offload available */
9105 return -ENOENT;
9106 }
9107 break;
9108 } else if (mode != veb->bridge_mode) {
9109 /* Existing HW bridge but different mode needs reset */
9110 veb->bridge_mode = mode;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009111 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
9112 if (mode == BRIDGE_MODE_VEB)
9113 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
9114 else
9115 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
Maciej Sosin373149f2017-04-05 07:50:55 -04009116 i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
9117 true);
Neerav Parikh51616012015-02-06 08:52:14 +00009118 break;
9119 }
9120 }
9121
9122 return 0;
9123}
9124
9125/**
9126 * i40e_ndo_bridge_getlink - Get the hardware bridge mode
9127 * @skb: skb buff
9128 * @pid: process id
9129 * @seq: RTNL message seq #
9130 * @dev: the netdev being configured
9131 * @filter_mask: unused
Jesse Brandeburgd4b2f9f2015-09-03 17:18:48 -04009132 * @nlflags: netlink flags passed in
Neerav Parikh51616012015-02-06 08:52:14 +00009133 *
9134 * Return the mode in which the hardware bridge is operating in
9135 * i.e VEB or VEPA.
9136 **/
Neerav Parikh51616012015-02-06 08:52:14 +00009137static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
9138 struct net_device *dev,
Carolyn Wyborny9f4ffc42015-08-31 19:54:42 -04009139 u32 __always_unused filter_mask,
9140 int nlflags)
Neerav Parikh51616012015-02-06 08:52:14 +00009141{
9142 struct i40e_netdev_priv *np = netdev_priv(dev);
9143 struct i40e_vsi *vsi = np->vsi;
9144 struct i40e_pf *pf = vsi->back;
9145 struct i40e_veb *veb = NULL;
9146 int i;
9147
9148 /* Only for PF VSI for now */
9149 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
9150 return -EOPNOTSUPP;
9151
9152 /* Find the HW bridge for the PF VSI */
9153 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9154 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9155 veb = pf->veb[i];
9156 }
9157
9158 if (!veb)
9159 return 0;
9160
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02009161 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
Huaibin Wang599b0762016-09-26 09:51:18 +02009162 0, 0, nlflags, filter_mask, NULL);
Neerav Parikh51616012015-02-06 08:52:14 +00009163}
Neerav Parikh51616012015-02-06 08:52:14 +00009164
Joe Stringerf44a75e2015-04-14 17:09:14 -07009165/**
9166 * i40e_features_check - Validate encapsulated packet conforms to limits
9167 * @skb: skb buff
Jean Sacren2bc11c62015-09-19 05:08:43 -06009168 * @dev: This physical port's netdev
Joe Stringerf44a75e2015-04-14 17:09:14 -07009169 * @features: Offload features that the stack believes apply
9170 **/
9171static netdev_features_t i40e_features_check(struct sk_buff *skb,
9172 struct net_device *dev,
9173 netdev_features_t features)
9174{
Alexander Duyckf114dca2016-10-25 16:08:46 -07009175 size_t len;
9176
9177 /* No point in doing any of this if neither checksum nor GSO are
9178 * being requested for this frame. We can rule out both by just
9179 * checking for CHECKSUM_PARTIAL
9180 */
9181 if (skb->ip_summed != CHECKSUM_PARTIAL)
9182 return features;
9183
9184 /* We cannot support GSO if the MSS is going to be less than
9185 * 64 bytes. If it is then we need to drop support for GSO.
9186 */
9187 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
9188 features &= ~NETIF_F_GSO_MASK;
9189
9190 /* MACLEN can support at most 63 words */
9191 len = skb_network_header(skb) - skb->data;
9192 if (len & ~(63 * 2))
9193 goto out_err;
9194
9195 /* IPLEN and EIPLEN can support at most 127 dwords */
9196 len = skb_transport_header(skb) - skb_network_header(skb);
9197 if (len & ~(127 * 4))
9198 goto out_err;
9199
9200 if (skb->encapsulation) {
9201 /* L4TUNLEN can support 127 words */
9202 len = skb_inner_network_header(skb) - skb_transport_header(skb);
9203 if (len & ~(127 * 2))
9204 goto out_err;
9205
9206 /* IPLEN can support at most 127 dwords */
9207 len = skb_inner_transport_header(skb) -
9208 skb_inner_network_header(skb);
9209 if (len & ~(127 * 4))
9210 goto out_err;
9211 }
9212
9213 /* No need to validate L4LEN as TCP is the only protocol with a
9214 * a flexible value and we support all possible values supported
9215 * by TCP, which is at most 15 dwords
9216 */
Joe Stringerf44a75e2015-04-14 17:09:14 -07009217
9218 return features;
Alexander Duyckf114dca2016-10-25 16:08:46 -07009219out_err:
9220 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Joe Stringerf44a75e2015-04-14 17:09:14 -07009221}
9222
Shannon Nelson37a29732015-02-27 09:15:19 +00009223static const struct net_device_ops i40e_netdev_ops = {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009224 .ndo_open = i40e_open,
9225 .ndo_stop = i40e_close,
9226 .ndo_start_xmit = i40e_lan_xmit_frame,
9227 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
9228 .ndo_set_rx_mode = i40e_set_rx_mode,
9229 .ndo_validate_addr = eth_validate_addr,
9230 .ndo_set_mac_address = i40e_set_mac,
9231 .ndo_change_mtu = i40e_change_mtu,
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00009232 .ndo_do_ioctl = i40e_ioctl,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009233 .ndo_tx_timeout = i40e_tx_timeout,
9234 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
9235 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
9236#ifdef CONFIG_NET_POLL_CONTROLLER
9237 .ndo_poll_controller = i40e_netpoll,
9238#endif
John Fastabende4c67342016-02-16 21:16:15 -08009239 .ndo_setup_tc = __i40e_setup_tc,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009240 .ndo_set_features = i40e_set_features,
9241 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
9242 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04009243 .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009244 .ndo_get_vf_config = i40e_ndo_get_vf_config,
Mitch Williams588aefa2014-02-11 08:27:49 +00009245 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
Serey Konge6d90042014-07-12 07:28:14 +00009246 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
Anjali Singhai Jainc3bbbd22016-04-01 03:56:07 -07009247 .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
Alexander Duyck06a5f7f2016-06-16 12:22:06 -07009248 .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
9249 .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
Neerav Parikh1f224ad2014-02-12 01:45:31 +00009250 .ndo_get_phys_port_id = i40e_get_phys_port_id,
Greg Rose4ba0dea2014-03-06 08:59:55 +00009251 .ndo_fdb_add = i40e_ndo_fdb_add,
Joe Stringerf44a75e2015-04-14 17:09:14 -07009252 .ndo_features_check = i40e_features_check,
Neerav Parikh51616012015-02-06 08:52:14 +00009253 .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
9254 .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009255};
9256
9257/**
9258 * i40e_config_netdev - Setup the netdev flags
9259 * @vsi: the VSI being configured
9260 *
9261 * Returns 0 on success, negative value on failure
9262 **/
9263static int i40e_config_netdev(struct i40e_vsi *vsi)
9264{
9265 struct i40e_pf *pf = vsi->back;
9266 struct i40e_hw *hw = &pf->hw;
9267 struct i40e_netdev_priv *np;
9268 struct net_device *netdev;
Jacob Keller435c0842016-11-08 13:05:10 -08009269 u8 broadcast[ETH_ALEN];
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009270 u8 mac_addr[ETH_ALEN];
9271 int etherdev_size;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009272 netdev_features_t hw_enc_features;
9273 netdev_features_t hw_features;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009274
9275 etherdev_size = sizeof(struct i40e_netdev_priv);
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +00009276 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009277 if (!netdev)
9278 return -ENOMEM;
9279
9280 vsi->netdev = netdev;
9281 np = netdev_priv(netdev);
9282 np->vsi = vsi;
9283
Preethi Banalabacd75c2017-03-27 14:43:18 -07009284 hw_enc_features = NETIF_F_SG |
9285 NETIF_F_IP_CSUM |
9286 NETIF_F_IPV6_CSUM |
9287 NETIF_F_HIGHDMA |
9288 NETIF_F_SOFT_FEATURES |
9289 NETIF_F_TSO |
9290 NETIF_F_TSO_ECN |
9291 NETIF_F_TSO6 |
9292 NETIF_F_GSO_GRE |
9293 NETIF_F_GSO_GRE_CSUM |
9294 NETIF_F_GSO_PARTIAL |
9295 NETIF_F_GSO_UDP_TUNNEL |
9296 NETIF_F_GSO_UDP_TUNNEL_CSUM |
9297 NETIF_F_SCTP_CRC |
9298 NETIF_F_RXHASH |
9299 NETIF_F_RXCSUM |
9300 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009301
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009302 if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009303 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
9304
9305 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009306
Preethi Banalabacd75c2017-03-27 14:43:18 -07009307 netdev->hw_enc_features |= hw_enc_features;
9308
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009309 /* record features VLANs can make use of */
Preethi Banalabacd75c2017-03-27 14:43:18 -07009310 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009311
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009312 if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009313 netdev->hw_features |= NETIF_F_NTUPLE;
Preethi Banalabacd75c2017-03-27 14:43:18 -07009314 hw_features = hw_enc_features |
9315 NETIF_F_HW_VLAN_CTAG_TX |
9316 NETIF_F_HW_VLAN_CTAG_RX;
Anjali Singhai Jain2e86a0b2014-04-01 07:11:53 +00009317
Preethi Banalabacd75c2017-03-27 14:43:18 -07009318 netdev->hw_features |= hw_features;
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009319
Preethi Banalabacd75c2017-03-27 14:43:18 -07009320 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
Alexander Duyck1c7b4a22016-04-14 17:19:25 -04009321 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009322
9323 if (vsi->type == I40E_VSI_MAIN) {
9324 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
Greg Rose9a173902014-05-22 06:32:02 +00009325 ether_addr_copy(mac_addr, hw->mac.perm_addr);
Jacob Keller41c4c2b2017-04-05 07:50:57 -04009326 /* The following steps are necessary for two reasons. First,
9327 * some older NVM configurations load a default MAC-VLAN
9328 * filter that will accept any tagged packet, and we want to
9329 * replace this with a normal filter. Additionally, it is
9330 * possible our MAC address was provided by the platform using
9331 * Open Firmware or similar.
9332 *
9333 * Thus, we need to remove the default filter and install one
9334 * specific to the MAC address.
Jacob Keller1596b5d2016-11-08 13:05:15 -08009335 */
9336 i40e_rm_default_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009337 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009338 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009339 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009340 } else {
9341 /* relate the VSI_VMDQ name to the VSI_MAIN name */
9342 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
9343 pf->vsi[pf->lan_vsi]->netdev->name);
9344 random_ether_addr(mac_addr);
Kiran Patil21659032015-09-30 14:09:03 -04009345
Jacob Keller278e7d02016-10-05 09:30:37 -07009346 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009347 i40e_add_mac_filter(vsi, mac_addr);
Jacob Keller278e7d02016-10-05 09:30:37 -07009348 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009349 }
Kiran Patil21659032015-09-30 14:09:03 -04009350
Jacob Keller435c0842016-11-08 13:05:10 -08009351 /* Add the broadcast filter so that we initially will receive
9352 * broadcast packets. Note that when a new VLAN is first added the
9353 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
9354 * specific filters as part of transitioning into "vlan" operation.
9355 * When more VLANs are added, the driver will copy each existing MAC
9356 * filter and add it for the new VLAN.
9357 *
9358 * Broadcast filters are handled specially by
9359 * i40e_sync_filters_subtask, as the driver must to set the broadcast
9360 * promiscuous bit instead of adding this directly as a MAC/VLAN
9361 * filter. The subtask will update the correct broadcast promiscuous
9362 * bits as VLANs become active or inactive.
9363 */
9364 eth_broadcast_addr(broadcast);
9365 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller9569a9a2016-11-11 12:39:37 -08009366 i40e_add_mac_filter(vsi, broadcast);
Jacob Keller435c0842016-11-08 13:05:10 -08009367 spin_unlock_bh(&vsi->mac_filter_hash_lock);
9368
Greg Rose9a173902014-05-22 06:32:02 +00009369 ether_addr_copy(netdev->dev_addr, mac_addr);
9370 ether_addr_copy(netdev->perm_addr, mac_addr);
Alexander Duyckb0fe3302016-04-02 00:05:14 -07009371
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009372 netdev->priv_flags |= IFF_UNICAST_FLT;
9373 netdev->priv_flags |= IFF_SUPP_NOFCS;
9374 /* Setup netdev TC information */
9375 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
9376
9377 netdev->netdev_ops = &i40e_netdev_ops;
9378 netdev->watchdog_timeo = 5 * HZ;
9379 i40e_set_ethtool_ops(netdev);
9380
Jarod Wilson91c527a2016-10-17 15:54:05 -04009381 /* MTU range: 68 - 9706 */
9382 netdev->min_mtu = ETH_MIN_MTU;
9383 netdev->max_mtu = I40E_MAX_RXBUFFER -
9384 (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
9385
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009386 return 0;
9387}
9388
9389/**
9390 * i40e_vsi_delete - Delete a VSI from the switch
9391 * @vsi: the VSI being removed
9392 *
9393 * Returns 0 on success, negative value on failure
9394 **/
9395static void i40e_vsi_delete(struct i40e_vsi *vsi)
9396{
9397 /* remove default VSI is not allowed */
9398 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
9399 return;
9400
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009401 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009402}
9403
9404/**
Neerav Parikh51616012015-02-06 08:52:14 +00009405 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
9406 * @vsi: the VSI being queried
9407 *
9408 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
9409 **/
9410int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
9411{
9412 struct i40e_veb *veb;
9413 struct i40e_pf *pf = vsi->back;
9414
9415 /* Uplink is not a bridge so default to VEB */
9416 if (vsi->veb_idx == I40E_NO_VEB)
9417 return 1;
9418
9419 veb = pf->veb[vsi->veb_idx];
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009420 if (!veb) {
9421 dev_info(&pf->pdev->dev,
9422 "There is no veb associated with the bridge\n");
9423 return -ENOENT;
9424 }
Neerav Parikh51616012015-02-06 08:52:14 +00009425
Akeem G Abodunrin09603ea2015-10-01 14:37:36 -04009426 /* Uplink is a bridge in VEPA mode */
9427 if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
9428 return 0;
9429 } else {
9430 /* Uplink is a bridge in VEB mode */
9431 return 1;
9432 }
9433
9434 /* VEPA is now default bridge, so return 0 */
9435 return 0;
Neerav Parikh51616012015-02-06 08:52:14 +00009436}
9437
9438/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009439 * i40e_add_vsi - Add a VSI to the switch
9440 * @vsi: the VSI being configured
9441 *
9442 * This initializes a VSI context depending on the VSI type to be added and
9443 * passes it down to the add_vsi aq command.
9444 **/
9445static int i40e_add_vsi(struct i40e_vsi *vsi)
9446{
9447 int ret = -ENODEV;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009448 struct i40e_pf *pf = vsi->back;
9449 struct i40e_hw *hw = &pf->hw;
9450 struct i40e_vsi_context ctxt;
Jacob Keller278e7d02016-10-05 09:30:37 -07009451 struct i40e_mac_filter *f;
9452 struct hlist_node *h;
9453 int bkt;
Kiran Patil21659032015-09-30 14:09:03 -04009454
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009455 u8 enabled_tc = 0x1; /* TC0 enabled */
9456 int f_count = 0;
9457
9458 memset(&ctxt, 0, sizeof(ctxt));
9459 switch (vsi->type) {
9460 case I40E_VSI_MAIN:
9461 /* The PF's main VSI is already setup as part of the
9462 * device initialization, so we'll not bother with
9463 * the add_vsi call, but we will retrieve the current
9464 * VSI context.
9465 */
9466 ctxt.seid = pf->main_vsi_seid;
9467 ctxt.pf_num = pf->hw.pf_id;
9468 ctxt.vf_num = 0;
9469 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9470 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9471 if (ret) {
9472 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009473 "couldn't get PF vsi config, err %s aq_err %s\n",
9474 i40e_stat_str(&pf->hw, ret),
9475 i40e_aq_str(&pf->hw,
9476 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009477 return -ENOENT;
9478 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009479 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009480 vsi->info.valid_sections = 0;
9481
9482 vsi->seid = ctxt.seid;
9483 vsi->id = ctxt.vsi_number;
9484
9485 enabled_tc = i40e_pf_get_tc_map(pf);
9486
9487 /* MFP mode setup queue map and update VSI */
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009488 if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
9489 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009490 memset(&ctxt, 0, sizeof(ctxt));
9491 ctxt.seid = pf->main_vsi_seid;
9492 ctxt.pf_num = pf->hw.pf_id;
9493 ctxt.vf_num = 0;
9494 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
9495 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
9496 if (ret) {
9497 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009498 "update vsi failed, err %s aq_err %s\n",
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 goto err;
9504 }
9505 /* update the local VSI info queue map */
9506 i40e_vsi_update_queue_map(vsi, &ctxt);
9507 vsi->info.valid_sections = 0;
9508 } else {
9509 /* Default/Main VSI is only enabled for TC0
9510 * reconfigure it to enable all TCs that are
9511 * available on the port in SFP mode.
Neerav Parikh63d7e5a2014-12-14 01:55:16 +00009512 * For MFP case the iSCSI PF would use this
9513 * flow to enable LAN+iSCSI TC.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009514 */
9515 ret = i40e_vsi_config_tc(vsi, enabled_tc);
9516 if (ret) {
9517 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009518 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
9519 enabled_tc,
9520 i40e_stat_str(&pf->hw, ret),
9521 i40e_aq_str(&pf->hw,
9522 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009523 ret = -ENOENT;
9524 }
9525 }
9526 break;
9527
9528 case I40E_VSI_FDIR:
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009529 ctxt.pf_num = hw->pf_id;
9530 ctxt.vf_num = 0;
9531 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009532 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009533 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009534 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
9535 (i40e_is_vsi_uplink_mode_veb(vsi))) {
Neerav Parikh51616012015-02-06 08:52:14 +00009536 ctxt.info.valid_sections |=
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009537 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
Neerav Parikh51616012015-02-06 08:52:14 +00009538 ctxt.info.switch_id =
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07009539 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
Neerav Parikh51616012015-02-06 08:52:14 +00009540 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009541 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009542 break;
9543
9544 case I40E_VSI_VMDQ2:
9545 ctxt.pf_num = hw->pf_id;
9546 ctxt.vf_num = 0;
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_VMDQ2;
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
9561 /* Setup the VSI tx/rx queue map for TC0 only for now */
9562 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9563 break;
9564
9565 case I40E_VSI_SRIOV:
9566 ctxt.pf_num = hw->pf_id;
9567 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
9568 ctxt.uplink_seid = vsi->uplink_seid;
Neerav Parikh2b18e592015-01-24 09:58:38 +00009569 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009570 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
9571
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009572 /* This VSI is connected to VEB so the switch_id
9573 * should be set to zero by default.
9574 */
Neerav Parikh51616012015-02-06 08:52:14 +00009575 if (i40e_is_vsi_uplink_mode_veb(vsi)) {
9576 ctxt.info.valid_sections |=
9577 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9578 ctxt.info.switch_id =
9579 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9580 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009581
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009582 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
9583 ctxt.info.valid_sections |=
9584 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
9585 ctxt.info.queueing_opt_flags |=
Ashish Shah4b28cdb2016-05-03 15:13:17 -07009586 (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
9587 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009588 }
9589
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009590 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
9591 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
Mitch Williamsc674d122014-05-20 08:01:40 +00009592 if (pf->vf[vsi->vf_id].spoofchk) {
9593 ctxt.info.valid_sections |=
9594 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
9595 ctxt.info.sec_flags |=
9596 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
9597 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
9598 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009599 /* Setup the VSI tx/rx queue map for TC0 only for now */
9600 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
9601 break;
9602
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -06009603 case I40E_VSI_IWARP:
9604 /* send down message to iWARP */
9605 break;
9606
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009607 default:
9608 return -ENODEV;
9609 }
9610
9611 if (vsi->type != I40E_VSI_MAIN) {
9612 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
9613 if (ret) {
9614 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009615 "add vsi failed, err %s aq_err %s\n",
9616 i40e_stat_str(&pf->hw, ret),
9617 i40e_aq_str(&pf->hw,
9618 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009619 ret = -ENOENT;
9620 goto err;
9621 }
Jesse Brandeburg1a2f6242015-03-31 00:45:01 -07009622 vsi->info = ctxt.info;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009623 vsi->info.valid_sections = 0;
9624 vsi->seid = ctxt.seid;
9625 vsi->id = ctxt.vsi_number;
9626 }
9627
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009628 vsi->active_filters = 0;
9629 clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
Jacob Keller278e7d02016-10-05 09:30:37 -07009630 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009631 /* If macvlan filters already exist, force them to get loaded */
Jacob Keller278e7d02016-10-05 09:30:37 -07009632 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
Mitch Williamsc3c7ea22016-06-20 09:10:38 -07009633 f->state = I40E_FILTER_NEW;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009634 f_count++;
9635 }
Jacob Keller278e7d02016-10-05 09:30:37 -07009636 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009637
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009638 if (f_count) {
9639 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
9640 pf->flags |= I40E_FLAG_FILTER_SYNC;
9641 }
9642
9643 /* Update VSI BW information */
9644 ret = i40e_vsi_get_bw_info(vsi);
9645 if (ret) {
9646 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009647 "couldn't get vsi bw info, err %s aq_err %s\n",
9648 i40e_stat_str(&pf->hw, ret),
9649 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009650 /* VSI is already added so not tearing that up */
9651 ret = 0;
9652 }
9653
9654err:
9655 return ret;
9656}
9657
9658/**
9659 * i40e_vsi_release - Delete a VSI and free its resources
9660 * @vsi: the VSI being removed
9661 *
9662 * Returns 0 on success or < 0 on error
9663 **/
9664int i40e_vsi_release(struct i40e_vsi *vsi)
9665{
Jacob Keller278e7d02016-10-05 09:30:37 -07009666 struct i40e_mac_filter *f;
9667 struct hlist_node *h;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009668 struct i40e_veb *veb = NULL;
9669 struct i40e_pf *pf;
9670 u16 uplink_seid;
Jacob Keller278e7d02016-10-05 09:30:37 -07009671 int i, n, bkt;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009672
9673 pf = vsi->back;
9674
9675 /* release of a VEB-owner or last VSI is not allowed */
9676 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
9677 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
9678 vsi->seid, vsi->uplink_seid);
9679 return -ENODEV;
9680 }
9681 if (vsi == pf->vsi[pf->lan_vsi] &&
9682 !test_bit(__I40E_DOWN, &pf->state)) {
9683 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
9684 return -ENODEV;
9685 }
9686
9687 uplink_seid = vsi->uplink_seid;
9688 if (vsi->type != I40E_VSI_SRIOV) {
9689 if (vsi->netdev_registered) {
9690 vsi->netdev_registered = false;
9691 if (vsi->netdev) {
9692 /* results in a call to i40e_close() */
9693 unregister_netdev(vsi->netdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009694 }
9695 } else {
Shannon Nelson90ef8d42014-03-14 07:32:26 +00009696 i40e_vsi_close(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009697 }
9698 i40e_vsi_disable_irq(vsi);
9699 }
9700
Jacob Keller278e7d02016-10-05 09:30:37 -07009701 spin_lock_bh(&vsi->mac_filter_hash_lock);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009702
9703 /* clear the sync flag on all filters */
9704 if (vsi->netdev) {
9705 __dev_uc_unsync(vsi->netdev, NULL);
9706 __dev_mc_unsync(vsi->netdev, NULL);
9707 }
9708
9709 /* make sure any remaining filters are marked for deletion */
Jacob Keller278e7d02016-10-05 09:30:37 -07009710 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
Jacob Keller290d2552016-10-05 09:30:36 -07009711 __i40e_del_filter(vsi, f);
Jacob Keller6622f5c2016-10-05 09:30:32 -07009712
Jacob Keller278e7d02016-10-05 09:30:37 -07009713 spin_unlock_bh(&vsi->mac_filter_hash_lock);
Kiran Patil21659032015-09-30 14:09:03 -04009714
Jesse Brandeburg17652c62015-11-05 17:01:02 -08009715 i40e_sync_vsi_filters(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009716
9717 i40e_vsi_delete(vsi);
9718 i40e_vsi_free_q_vectors(vsi);
Shannon Nelsona4866592014-02-11 08:24:07 +00009719 if (vsi->netdev) {
9720 free_netdev(vsi->netdev);
9721 vsi->netdev = NULL;
9722 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009723 i40e_vsi_clear_rings(vsi);
9724 i40e_vsi_clear(vsi);
9725
9726 /* If this was the last thing on the VEB, except for the
9727 * controlling VSI, remove the VEB, which puts the controlling
9728 * VSI onto the next level down in the switch.
9729 *
9730 * Well, okay, there's one more exception here: don't remove
9731 * the orphan VEBs yet. We'll wait for an explicit remove request
9732 * from up the network stack.
9733 */
Mitch Williams505682c2014-05-20 08:01:37 +00009734 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009735 if (pf->vsi[i] &&
9736 pf->vsi[i]->uplink_seid == uplink_seid &&
9737 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
9738 n++; /* count the VSIs */
9739 }
9740 }
9741 for (i = 0; i < I40E_MAX_VEB; i++) {
9742 if (!pf->veb[i])
9743 continue;
9744 if (pf->veb[i]->uplink_seid == uplink_seid)
9745 n++; /* count the VEBs */
9746 if (pf->veb[i]->seid == uplink_seid)
9747 veb = pf->veb[i];
9748 }
9749 if (n == 0 && veb && veb->uplink_seid != 0)
9750 i40e_veb_release(veb);
9751
9752 return 0;
9753}
9754
9755/**
9756 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
9757 * @vsi: ptr to the VSI
9758 *
9759 * This should only be called after i40e_vsi_mem_alloc() which allocates the
9760 * corresponding SW VSI structure and initializes num_queue_pairs for the
9761 * newly allocated VSI.
9762 *
9763 * Returns 0 on success or negative on failure
9764 **/
9765static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
9766{
9767 int ret = -ENOENT;
9768 struct i40e_pf *pf = vsi->back;
9769
Alexander Duyck493fb302013-09-28 07:01:44 +00009770 if (vsi->q_vectors[0]) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009771 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
9772 vsi->seid);
9773 return -EEXIST;
9774 }
9775
9776 if (vsi->base_vector) {
Jesse Brandeburgf29eaa32014-02-11 08:24:12 +00009777 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009778 vsi->seid, vsi->base_vector);
9779 return -EEXIST;
9780 }
9781
Greg Rose90e04072014-03-06 08:59:57 +00009782 ret = i40e_vsi_alloc_q_vectors(vsi);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009783 if (ret) {
9784 dev_info(&pf->pdev->dev,
9785 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
9786 vsi->num_q_vectors, vsi->seid, ret);
9787 vsi->num_q_vectors = 0;
9788 goto vector_setup_out;
9789 }
9790
Anjali Singhai Jain26cdc442015-07-10 19:36:00 -04009791 /* In Legacy mode, we do not have to get any other vector since we
9792 * piggyback on the misc/ICR0 for queue interrupts.
9793 */
9794 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
9795 return ret;
Shannon Nelson958a3e32013-09-28 07:13:28 +00009796 if (vsi->num_q_vectors)
9797 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
9798 vsi->num_q_vectors, vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009799 if (vsi->base_vector < 0) {
9800 dev_info(&pf->pdev->dev,
Shannon Nelson049a2be2014-10-17 03:14:50 +00009801 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
9802 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009803 i40e_vsi_free_q_vectors(vsi);
9804 ret = -ENOENT;
9805 goto vector_setup_out;
9806 }
9807
9808vector_setup_out:
9809 return ret;
9810}
9811
9812/**
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009813 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
9814 * @vsi: pointer to the vsi.
9815 *
9816 * This re-allocates a vsi's queue resources.
9817 *
9818 * Returns pointer to the successfully allocated and configured VSI sw struct
9819 * on success, otherwise returns NULL on failure.
9820 **/
9821static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
9822{
John Underwoodf5340392016-02-18 09:19:24 -08009823 struct i40e_pf *pf;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009824 u8 enabled_tc;
9825 int ret;
9826
John Underwoodf5340392016-02-18 09:19:24 -08009827 if (!vsi)
9828 return NULL;
9829
9830 pf = vsi->back;
9831
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009832 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
9833 i40e_vsi_clear_rings(vsi);
9834
9835 i40e_vsi_free_arrays(vsi, false);
9836 i40e_set_num_rings_in_vsi(vsi);
9837 ret = i40e_vsi_alloc_arrays(vsi, false);
9838 if (ret)
9839 goto err_vsi;
9840
9841 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
9842 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009843 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -04009844 "failed to get tracking for %d queues for VSI %d err %d\n",
Shannon Nelson049a2be2014-10-17 03:14:50 +00009845 vsi->alloc_queue_pairs, vsi->seid, ret);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009846 goto err_vsi;
9847 }
9848 vsi->base_queue = ret;
9849
9850 /* Update the FW view of the VSI. Force a reset of TC and queue
9851 * layout configurations.
9852 */
9853 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
9854 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
9855 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
9856 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
Jacob Keller1596b5d2016-11-08 13:05:15 -08009857 if (vsi->type == I40E_VSI_MAIN)
9858 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +00009859
9860 /* assign it some queues */
9861 ret = i40e_alloc_rings(vsi);
9862 if (ret)
9863 goto err_rings;
9864
9865 /* map all of the rings to the q_vectors */
9866 i40e_vsi_map_rings_to_vectors(vsi);
9867 return vsi;
9868
9869err_rings:
9870 i40e_vsi_free_q_vectors(vsi);
9871 if (vsi->netdev_registered) {
9872 vsi->netdev_registered = false;
9873 unregister_netdev(vsi->netdev);
9874 free_netdev(vsi->netdev);
9875 vsi->netdev = NULL;
9876 }
9877 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
9878err_vsi:
9879 i40e_vsi_clear(vsi);
9880 return NULL;
9881}
9882
9883/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009884 * i40e_vsi_setup - Set up a VSI by a given type
9885 * @pf: board private structure
9886 * @type: VSI type
9887 * @uplink_seid: the switch element to link to
9888 * @param1: usage depends upon VSI type. For VF types, indicates VF id
9889 *
9890 * This allocates the sw VSI structure and its queue resources, then add a VSI
9891 * to the identified VEB.
9892 *
9893 * Returns pointer to the successfully allocated and configure VSI sw struct on
9894 * success, otherwise returns NULL on failure.
9895 **/
9896struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
9897 u16 uplink_seid, u32 param1)
9898{
9899 struct i40e_vsi *vsi = NULL;
9900 struct i40e_veb *veb = NULL;
9901 int ret, i;
9902 int v_idx;
9903
9904 /* The requested uplink_seid must be either
9905 * - the PF's port seid
9906 * no VEB is needed because this is the PF
9907 * or this is a Flow Director special case VSI
9908 * - seid of an existing VEB
9909 * - seid of a VSI that owns an existing VEB
9910 * - seid of a VSI that doesn't own a VEB
9911 * a new VEB is created and the VSI becomes the owner
9912 * - seid of the PF VSI, which is what creates the first VEB
9913 * this is a special case of the previous
9914 *
9915 * Find which uplink_seid we were given and create a new VEB if needed
9916 */
9917 for (i = 0; i < I40E_MAX_VEB; i++) {
9918 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
9919 veb = pf->veb[i];
9920 break;
9921 }
9922 }
9923
9924 if (!veb && uplink_seid != pf->mac_seid) {
9925
Mitch Williams505682c2014-05-20 08:01:37 +00009926 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009927 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
9928 vsi = pf->vsi[i];
9929 break;
9930 }
9931 }
9932 if (!vsi) {
9933 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
9934 uplink_seid);
9935 return NULL;
9936 }
9937
9938 if (vsi->uplink_seid == pf->mac_seid)
9939 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
9940 vsi->tc_config.enabled_tc);
9941 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
9942 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
9943 vsi->tc_config.enabled_tc);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009944 if (veb) {
9945 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
9946 dev_info(&vsi->back->pdev->dev,
Shannon Nelsonfb43201f2015-08-26 15:14:17 -04009947 "New VSI creation error, uplink seid of LAN VSI expected.\n");
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009948 return NULL;
9949 }
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -04009950 /* We come up by default in VEPA mode if SRIOV is not
9951 * already enabled, in which case we can't force VEPA
9952 * mode.
9953 */
9954 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
9955 veb->bridge_mode = BRIDGE_MODE_VEPA;
9956 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
9957 }
Neerav Parikh51616012015-02-06 08:52:14 +00009958 i40e_config_bridge_mode(veb);
Anjali Singhai Jain79c21a82014-11-13 03:06:14 +00009959 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009960 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
9961 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
9962 veb = pf->veb[i];
9963 }
9964 if (!veb) {
9965 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
9966 return NULL;
9967 }
9968
9969 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
9970 uplink_seid = veb->seid;
9971 }
9972
9973 /* get vsi sw struct */
9974 v_idx = i40e_vsi_mem_alloc(pf, type);
9975 if (v_idx < 0)
9976 goto err_alloc;
9977 vsi = pf->vsi[v_idx];
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009978 if (!vsi)
9979 goto err_alloc;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009980 vsi->type = type;
9981 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
9982
9983 if (type == I40E_VSI_MAIN)
9984 pf->lan_vsi = v_idx;
9985 else if (type == I40E_VSI_SRIOV)
9986 vsi->vf_id = param1;
9987 /* assign it some queues */
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -08009988 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
9989 vsi->idx);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009990 if (ret < 0) {
Shannon Nelson049a2be2014-10-17 03:14:50 +00009991 dev_info(&pf->pdev->dev,
9992 "failed to get tracking for %d queues for VSI %d err=%d\n",
9993 vsi->alloc_queue_pairs, vsi->seid, ret);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +00009994 goto err_vsi;
9995 }
9996 vsi->base_queue = ret;
9997
9998 /* get a VSI from the hardware */
9999 vsi->uplink_seid = uplink_seid;
10000 ret = i40e_add_vsi(vsi);
10001 if (ret)
10002 goto err_vsi;
10003
10004 switch (vsi->type) {
10005 /* setup the netdev if needed */
10006 case I40E_VSI_MAIN:
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010007 /* Apply relevant filters if a platform-specific mac
10008 * address was selected.
10009 */
10010 if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
10011 ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
10012 if (ret) {
10013 dev_warn(&pf->pdev->dev,
10014 "could not set up macaddr; err %d\n",
10015 ret);
10016 }
10017 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010018 case I40E_VSI_VMDQ2:
10019 ret = i40e_config_netdev(vsi);
10020 if (ret)
10021 goto err_netdev;
10022 ret = register_netdev(vsi->netdev);
10023 if (ret)
10024 goto err_netdev;
10025 vsi->netdev_registered = true;
10026 netif_carrier_off(vsi->netdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080010027#ifdef CONFIG_I40E_DCB
10028 /* Setup DCB netlink interface */
10029 i40e_dcbnl_setup(vsi);
10030#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010031 /* fall through */
10032
10033 case I40E_VSI_FDIR:
10034 /* set up vectors and rings if needed */
10035 ret = i40e_vsi_setup_vectors(vsi);
10036 if (ret)
10037 goto err_msix;
10038
10039 ret = i40e_alloc_rings(vsi);
10040 if (ret)
10041 goto err_rings;
10042
10043 /* map all of the rings to the q_vectors */
10044 i40e_vsi_map_rings_to_vectors(vsi);
10045
10046 i40e_vsi_reset_stats(vsi);
10047 break;
10048
10049 default:
10050 /* no netdev or rings for the other VSI types */
10051 break;
10052 }
10053
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -040010054 if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
10055 (vsi->type == I40E_VSI_VMDQ2)) {
10056 ret = i40e_vsi_config_rss(vsi);
10057 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010058 return vsi;
10059
10060err_rings:
10061 i40e_vsi_free_q_vectors(vsi);
10062err_msix:
10063 if (vsi->netdev_registered) {
10064 vsi->netdev_registered = false;
10065 unregister_netdev(vsi->netdev);
10066 free_netdev(vsi->netdev);
10067 vsi->netdev = NULL;
10068 }
10069err_netdev:
10070 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
10071err_vsi:
10072 i40e_vsi_clear(vsi);
10073err_alloc:
10074 return NULL;
10075}
10076
10077/**
10078 * i40e_veb_get_bw_info - Query VEB BW information
10079 * @veb: the veb to query
10080 *
10081 * Query the Tx scheduler BW configuration data for given VEB
10082 **/
10083static int i40e_veb_get_bw_info(struct i40e_veb *veb)
10084{
10085 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
10086 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
10087 struct i40e_pf *pf = veb->pf;
10088 struct i40e_hw *hw = &pf->hw;
10089 u32 tc_bw_max;
10090 int ret = 0;
10091 int i;
10092
10093 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
10094 &bw_data, NULL);
10095 if (ret) {
10096 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010097 "query veb bw config failed, err %s aq_err %s\n",
10098 i40e_stat_str(&pf->hw, ret),
10099 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010100 goto out;
10101 }
10102
10103 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
10104 &ets_data, NULL);
10105 if (ret) {
10106 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010107 "query veb bw ets config failed, err %s aq_err %s\n",
10108 i40e_stat_str(&pf->hw, ret),
10109 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010110 goto out;
10111 }
10112
10113 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
10114 veb->bw_max_quanta = ets_data.tc_bw_max;
10115 veb->is_abs_credits = bw_data.absolute_credits_enable;
Neerav Parikh23cd1f02014-11-12 00:18:41 +000010116 veb->enabled_tc = ets_data.tc_valid_bits;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010117 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
10118 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
10119 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
10120 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
10121 veb->bw_tc_limit_credits[i] =
10122 le16_to_cpu(bw_data.tc_bw_limits[i]);
10123 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
10124 }
10125
10126out:
10127 return ret;
10128}
10129
10130/**
10131 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
10132 * @pf: board private structure
10133 *
10134 * On error: returns error code (negative)
10135 * On success: returns vsi index in PF (positive)
10136 **/
10137static int i40e_veb_mem_alloc(struct i40e_pf *pf)
10138{
10139 int ret = -ENOENT;
10140 struct i40e_veb *veb;
10141 int i;
10142
10143 /* Need to protect the allocation of switch elements at the PF level */
10144 mutex_lock(&pf->switch_mutex);
10145
10146 /* VEB list may be fragmented if VEB creation/destruction has
10147 * been happening. We can afford to do a quick scan to look
10148 * for any free slots in the list.
10149 *
10150 * find next empty veb slot, looping back around if necessary
10151 */
10152 i = 0;
10153 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
10154 i++;
10155 if (i >= I40E_MAX_VEB) {
10156 ret = -ENOMEM;
10157 goto err_alloc_veb; /* out of VEB slots! */
10158 }
10159
10160 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
10161 if (!veb) {
10162 ret = -ENOMEM;
10163 goto err_alloc_veb;
10164 }
10165 veb->pf = pf;
10166 veb->idx = i;
10167 veb->enabled_tc = 1;
10168
10169 pf->veb[i] = veb;
10170 ret = i;
10171err_alloc_veb:
10172 mutex_unlock(&pf->switch_mutex);
10173 return ret;
10174}
10175
10176/**
10177 * i40e_switch_branch_release - Delete a branch of the switch tree
10178 * @branch: where to start deleting
10179 *
10180 * This uses recursion to find the tips of the branch to be
10181 * removed, deleting until we get back to and can delete this VEB.
10182 **/
10183static void i40e_switch_branch_release(struct i40e_veb *branch)
10184{
10185 struct i40e_pf *pf = branch->pf;
10186 u16 branch_seid = branch->seid;
10187 u16 veb_idx = branch->idx;
10188 int i;
10189
10190 /* release any VEBs on this VEB - RECURSION */
10191 for (i = 0; i < I40E_MAX_VEB; i++) {
10192 if (!pf->veb[i])
10193 continue;
10194 if (pf->veb[i]->uplink_seid == branch->seid)
10195 i40e_switch_branch_release(pf->veb[i]);
10196 }
10197
10198 /* Release the VSIs on this VEB, but not the owner VSI.
10199 *
10200 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
10201 * the VEB itself, so don't use (*branch) after this loop.
10202 */
Mitch Williams505682c2014-05-20 08:01:37 +000010203 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010204 if (!pf->vsi[i])
10205 continue;
10206 if (pf->vsi[i]->uplink_seid == branch_seid &&
10207 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
10208 i40e_vsi_release(pf->vsi[i]);
10209 }
10210 }
10211
10212 /* There's one corner case where the VEB might not have been
10213 * removed, so double check it here and remove it if needed.
10214 * This case happens if the veb was created from the debugfs
10215 * commands and no VSIs were added to it.
10216 */
10217 if (pf->veb[veb_idx])
10218 i40e_veb_release(pf->veb[veb_idx]);
10219}
10220
10221/**
10222 * i40e_veb_clear - remove veb struct
10223 * @veb: the veb to remove
10224 **/
10225static void i40e_veb_clear(struct i40e_veb *veb)
10226{
10227 if (!veb)
10228 return;
10229
10230 if (veb->pf) {
10231 struct i40e_pf *pf = veb->pf;
10232
10233 mutex_lock(&pf->switch_mutex);
10234 if (pf->veb[veb->idx] == veb)
10235 pf->veb[veb->idx] = NULL;
10236 mutex_unlock(&pf->switch_mutex);
10237 }
10238
10239 kfree(veb);
10240}
10241
10242/**
10243 * i40e_veb_release - Delete a VEB and free its resources
10244 * @veb: the VEB being removed
10245 **/
10246void i40e_veb_release(struct i40e_veb *veb)
10247{
10248 struct i40e_vsi *vsi = NULL;
10249 struct i40e_pf *pf;
10250 int i, n = 0;
10251
10252 pf = veb->pf;
10253
10254 /* find the remaining VSI and check for extras */
Mitch Williams505682c2014-05-20 08:01:37 +000010255 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010256 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
10257 n++;
10258 vsi = pf->vsi[i];
10259 }
10260 }
10261 if (n != 1) {
10262 dev_info(&pf->pdev->dev,
10263 "can't remove VEB %d with %d VSIs left\n",
10264 veb->seid, n);
10265 return;
10266 }
10267
10268 /* move the remaining VSI to uplink veb */
10269 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
10270 if (veb->uplink_seid) {
10271 vsi->uplink_seid = veb->uplink_seid;
10272 if (veb->uplink_seid == pf->mac_seid)
10273 vsi->veb_idx = I40E_NO_VEB;
10274 else
10275 vsi->veb_idx = veb->veb_idx;
10276 } else {
10277 /* floating VEB */
10278 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10279 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
10280 }
10281
10282 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
10283 i40e_veb_clear(veb);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010284}
10285
10286/**
10287 * i40e_add_veb - create the VEB in the switch
10288 * @veb: the VEB to be instantiated
10289 * @vsi: the controlling VSI
10290 **/
10291static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
10292{
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010293 struct i40e_pf *pf = veb->pf;
Shannon Nelson66fc3602016-01-13 16:51:42 -080010294 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010295 int ret;
10296
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010297 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
Mitch Williams5bc16032016-05-16 10:26:43 -070010298 veb->enabled_tc, false,
Shannon Nelson66fc3602016-01-13 16:51:42 -080010299 &veb->seid, enable_stats, NULL);
Mitch Williams5bc16032016-05-16 10:26:43 -070010300
10301 /* get a VEB from the hardware */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010302 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010303 dev_info(&pf->pdev->dev,
10304 "couldn't add VEB, err %s aq_err %s\n",
10305 i40e_stat_str(&pf->hw, ret),
10306 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010307 return -EPERM;
10308 }
10309
10310 /* get statistics counter */
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010311 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010312 &veb->stats_idx, NULL, NULL, NULL);
10313 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010314 dev_info(&pf->pdev->dev,
10315 "couldn't get VEB statistics idx, err %s aq_err %s\n",
10316 i40e_stat_str(&pf->hw, ret),
10317 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010318 return -EPERM;
10319 }
10320 ret = i40e_veb_get_bw_info(veb);
10321 if (ret) {
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010322 dev_info(&pf->pdev->dev,
10323 "couldn't get VEB bw info, err %s aq_err %s\n",
10324 i40e_stat_str(&pf->hw, ret),
10325 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10326 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010327 return -ENOENT;
10328 }
10329
10330 vsi->uplink_seid = veb->seid;
10331 vsi->veb_idx = veb->idx;
10332 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
10333
10334 return 0;
10335}
10336
10337/**
10338 * i40e_veb_setup - Set up a VEB
10339 * @pf: board private structure
10340 * @flags: VEB setup flags
10341 * @uplink_seid: the switch element to link to
10342 * @vsi_seid: the initial VSI seid
10343 * @enabled_tc: Enabled TC bit-map
10344 *
10345 * This allocates the sw VEB structure and links it into the switch
10346 * It is possible and legal for this to be a duplicate of an already
10347 * existing VEB. It is also possible for both uplink and vsi seids
10348 * to be zero, in order to create a floating VEB.
10349 *
10350 * Returns pointer to the successfully allocated VEB sw struct on
10351 * success, otherwise returns NULL on failure.
10352 **/
10353struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
10354 u16 uplink_seid, u16 vsi_seid,
10355 u8 enabled_tc)
10356{
10357 struct i40e_veb *veb, *uplink_veb = NULL;
10358 int vsi_idx, veb_idx;
10359 int ret;
10360
10361 /* if one seid is 0, the other must be 0 to create a floating relay */
10362 if ((uplink_seid == 0 || vsi_seid == 0) &&
10363 (uplink_seid + vsi_seid != 0)) {
10364 dev_info(&pf->pdev->dev,
10365 "one, not both seid's are 0: uplink=%d vsi=%d\n",
10366 uplink_seid, vsi_seid);
10367 return NULL;
10368 }
10369
10370 /* make sure there is such a vsi and uplink */
Mitch Williams505682c2014-05-20 08:01:37 +000010371 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010372 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
10373 break;
Mitch Williams505682c2014-05-20 08:01:37 +000010374 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010375 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
10376 vsi_seid);
10377 return NULL;
10378 }
10379
10380 if (uplink_seid && uplink_seid != pf->mac_seid) {
10381 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10382 if (pf->veb[veb_idx] &&
10383 pf->veb[veb_idx]->seid == uplink_seid) {
10384 uplink_veb = pf->veb[veb_idx];
10385 break;
10386 }
10387 }
10388 if (!uplink_veb) {
10389 dev_info(&pf->pdev->dev,
10390 "uplink seid %d not found\n", uplink_seid);
10391 return NULL;
10392 }
10393 }
10394
10395 /* get veb sw struct */
10396 veb_idx = i40e_veb_mem_alloc(pf);
10397 if (veb_idx < 0)
10398 goto err_alloc;
10399 veb = pf->veb[veb_idx];
10400 veb->flags = flags;
10401 veb->uplink_seid = uplink_seid;
10402 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
10403 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
10404
10405 /* create the VEB in the switch */
10406 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
10407 if (ret)
10408 goto err_veb;
Shannon Nelson1bb8b932014-04-23 04:49:54 +000010409 if (vsi_idx == pf->lan_vsi)
10410 pf->lan_veb = veb->idx;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010411
10412 return veb;
10413
10414err_veb:
10415 i40e_veb_clear(veb);
10416err_alloc:
10417 return NULL;
10418}
10419
10420/**
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010421 * i40e_setup_pf_switch_element - set PF vars based on switch type
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010422 * @pf: board private structure
10423 * @ele: element we are building info from
10424 * @num_reported: total number of elements
10425 * @printconfig: should we print the contents
10426 *
10427 * helper function to assist in extracting a few useful SEID values.
10428 **/
10429static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
10430 struct i40e_aqc_switch_config_element_resp *ele,
10431 u16 num_reported, bool printconfig)
10432{
10433 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
10434 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
10435 u8 element_type = ele->element_type;
10436 u16 seid = le16_to_cpu(ele->seid);
10437
10438 if (printconfig)
10439 dev_info(&pf->pdev->dev,
10440 "type=%d seid=%d uplink=%d downlink=%d\n",
10441 element_type, seid, uplink_seid, downlink_seid);
10442
10443 switch (element_type) {
10444 case I40E_SWITCH_ELEMENT_TYPE_MAC:
10445 pf->mac_seid = seid;
10446 break;
10447 case I40E_SWITCH_ELEMENT_TYPE_VEB:
10448 /* Main VEB? */
10449 if (uplink_seid != pf->mac_seid)
10450 break;
10451 if (pf->lan_veb == I40E_NO_VEB) {
10452 int v;
10453
10454 /* find existing or else empty VEB */
10455 for (v = 0; v < I40E_MAX_VEB; v++) {
10456 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
10457 pf->lan_veb = v;
10458 break;
10459 }
10460 }
10461 if (pf->lan_veb == I40E_NO_VEB) {
10462 v = i40e_veb_mem_alloc(pf);
10463 if (v < 0)
10464 break;
10465 pf->lan_veb = v;
10466 }
10467 }
10468
10469 pf->veb[pf->lan_veb]->seid = seid;
10470 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
10471 pf->veb[pf->lan_veb]->pf = pf;
10472 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
10473 break;
10474 case I40E_SWITCH_ELEMENT_TYPE_VSI:
10475 if (num_reported != 1)
10476 break;
10477 /* This is immediately after a reset so we can assume this is
10478 * the PF's VSI
10479 */
10480 pf->mac_seid = uplink_seid;
10481 pf->pf_seid = downlink_seid;
10482 pf->main_vsi_seid = seid;
10483 if (printconfig)
10484 dev_info(&pf->pdev->dev,
10485 "pf_seid=%d main_vsi_seid=%d\n",
10486 pf->pf_seid, pf->main_vsi_seid);
10487 break;
10488 case I40E_SWITCH_ELEMENT_TYPE_PF:
10489 case I40E_SWITCH_ELEMENT_TYPE_VF:
10490 case I40E_SWITCH_ELEMENT_TYPE_EMP:
10491 case I40E_SWITCH_ELEMENT_TYPE_BMC:
10492 case I40E_SWITCH_ELEMENT_TYPE_PE:
10493 case I40E_SWITCH_ELEMENT_TYPE_PA:
10494 /* ignore these for now */
10495 break;
10496 default:
10497 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
10498 element_type, seid);
10499 break;
10500 }
10501}
10502
10503/**
10504 * i40e_fetch_switch_configuration - Get switch config from firmware
10505 * @pf: board private structure
10506 * @printconfig: should we print the contents
10507 *
10508 * Get the current switch configuration from the device and
10509 * extract a few useful SEID values.
10510 **/
10511int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
10512{
10513 struct i40e_aqc_get_switch_config_resp *sw_config;
10514 u16 next_seid = 0;
10515 int ret = 0;
10516 u8 *aq_buf;
10517 int i;
10518
10519 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
10520 if (!aq_buf)
10521 return -ENOMEM;
10522
10523 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
10524 do {
10525 u16 num_reported, num_total;
10526
10527 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
10528 I40E_AQ_LARGE_BUF,
10529 &next_seid, NULL);
10530 if (ret) {
10531 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010532 "get switch config failed err %s aq_err %s\n",
10533 i40e_stat_str(&pf->hw, ret),
10534 i40e_aq_str(&pf->hw,
10535 pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010536 kfree(aq_buf);
10537 return -ENOENT;
10538 }
10539
10540 num_reported = le16_to_cpu(sw_config->header.num_reported);
10541 num_total = le16_to_cpu(sw_config->header.num_total);
10542
10543 if (printconfig)
10544 dev_info(&pf->pdev->dev,
10545 "header: %d reported %d total\n",
10546 num_reported, num_total);
10547
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010548 for (i = 0; i < num_reported; i++) {
10549 struct i40e_aqc_switch_config_element_resp *ele =
10550 &sw_config->element[i];
10551
10552 i40e_setup_pf_switch_element(pf, ele, num_reported,
10553 printconfig);
10554 }
10555 } while (next_seid != 0);
10556
10557 kfree(aq_buf);
10558 return ret;
10559}
10560
10561/**
10562 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
10563 * @pf: board private structure
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010564 * @reinit: if the Main VSI needs to re-initialized.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010565 *
10566 * Returns 0 on success, negative value on failure
10567 **/
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010568static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010569{
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010570 u16 flags = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010571 int ret;
10572
10573 /* find out what's out there already */
10574 ret = i40e_fetch_switch_configuration(pf, false);
10575 if (ret) {
10576 dev_info(&pf->pdev->dev,
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040010577 "couldn't fetch switch config, err %s aq_err %s\n",
10578 i40e_stat_str(&pf->hw, ret),
10579 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010580 return ret;
10581 }
10582 i40e_pf_reset_stats(pf);
10583
Anjali Singhai Jainb5569892016-05-03 15:13:12 -070010584 /* set the switch config bit for the whole device to
10585 * support limited promisc or true promisc
10586 * when user requests promisc. The default is limited
10587 * promisc.
10588 */
10589
10590 if ((pf->hw.pf_id == 0) &&
10591 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
10592 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10593
10594 if (pf->hw.pf_id == 0) {
10595 u16 valid_flags;
10596
10597 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
10598 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
10599 NULL);
10600 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
10601 dev_info(&pf->pdev->dev,
10602 "couldn't set switch config bits, err %s aq_err %s\n",
10603 i40e_stat_str(&pf->hw, ret),
10604 i40e_aq_str(&pf->hw,
10605 pf->hw.aq.asq_last_status));
10606 /* not a fatal problem, just keep going */
10607 }
10608 }
10609
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010610 /* first time setup */
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010611 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010612 struct i40e_vsi *vsi = NULL;
10613 u16 uplink_seid;
10614
10615 /* Set up the PF VSI associated with the PF's main VSI
10616 * that is already in the HW switch
10617 */
10618 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
10619 uplink_seid = pf->veb[pf->lan_veb]->seid;
10620 else
10621 uplink_seid = pf->mac_seid;
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000010622 if (pf->lan_vsi == I40E_NO_VSI)
10623 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
10624 else if (reinit)
10625 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010626 if (!vsi) {
10627 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
10628 i40e_fdir_teardown(pf);
10629 return -EAGAIN;
10630 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010631 } else {
10632 /* force a reset of TC and queue layout configurations */
10633 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
Jesse Brandeburg6995b362015-08-28 17:55:54 -040010634
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010635 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
10636 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
10637 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
10638 }
10639 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
10640
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010641 i40e_fdir_sb_setup(pf);
10642
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010643 /* Setup static PF queue filter control settings */
10644 ret = i40e_setup_pf_filter_control(pf);
10645 if (ret) {
10646 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
10647 ret);
10648 /* Failure here should not stop continuing other steps */
10649 }
10650
10651 /* enable RSS in the HW, even for only one queue, as the stack can use
10652 * the hash
10653 */
10654 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
Helin Zhang043dd652015-10-21 19:56:23 -040010655 i40e_pf_config_rss(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010656
10657 /* fill in link information and enable LSE reporting */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010658 i40e_link_event(pf);
10659
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010660 /* Initialize user-specific link properties */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010661 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
10662 I40E_AQ_AN_COMPLETED) ? true : false);
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010663
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000010664 i40e_ptp_init(pf);
10665
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010666 return ret;
10667}
10668
10669/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010670 * i40e_determine_queue_usage - Work out queue distribution
10671 * @pf: board private structure
10672 **/
10673static void i40e_determine_queue_usage(struct i40e_pf *pf)
10674{
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010675 int queues_left;
10676
10677 pf->num_lan_qps = 0;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010678
10679 /* Find the max queues to be put into basic use. We'll always be
10680 * using TC0, whether or not DCB is running, and TC0 will get the
10681 * big RSS set.
10682 */
10683 queues_left = pf->hw.func_caps.num_tx_qp;
10684
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010685 if ((queues_left == 1) ||
Frank Zhang9aa7e932014-05-20 08:01:42 +000010686 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010687 /* one qp for PF, no queues for anything else */
10688 queues_left = 0;
Helin Zhangacd65442015-10-26 19:44:28 -040010689 pf->alloc_rss_size = pf->num_lan_qps = 1;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010690
10691 /* make sure all the fancies are disabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010692 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010693 I40E_FLAG_IWARP_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010694 I40E_FLAG_FD_SB_ENABLED |
10695 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010696 I40E_FLAG_DCB_CAPABLE |
Dave Ertmana0362442016-08-29 17:38:26 -070010697 I40E_FLAG_DCB_ENABLED |
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010698 I40E_FLAG_SRIOV_ENABLED |
10699 I40E_FLAG_VMDQ_ENABLED);
Frank Zhang9aa7e932014-05-20 08:01:42 +000010700 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
10701 I40E_FLAG_FD_SB_ENABLED |
Anjali Singhai Jainbbe7d0e2014-05-20 08:01:44 +000010702 I40E_FLAG_FD_ATR_ENABLED |
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010703 I40E_FLAG_DCB_CAPABLE))) {
Frank Zhang9aa7e932014-05-20 08:01:42 +000010704 /* one qp for PF */
Helin Zhangacd65442015-10-26 19:44:28 -040010705 pf->alloc_rss_size = pf->num_lan_qps = 1;
Frank Zhang9aa7e932014-05-20 08:01:42 +000010706 queues_left -= pf->num_lan_qps;
10707
10708 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060010709 I40E_FLAG_IWARP_ENABLED |
Frank Zhang9aa7e932014-05-20 08:01:42 +000010710 I40E_FLAG_FD_SB_ENABLED |
10711 I40E_FLAG_FD_ATR_ENABLED |
10712 I40E_FLAG_DCB_ENABLED |
10713 I40E_FLAG_VMDQ_ENABLED);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010714 } else {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010715 /* Not enough queues for all TCs */
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010716 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010717 (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
Dave Ertmana0362442016-08-29 17:38:26 -070010718 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
10719 I40E_FLAG_DCB_ENABLED);
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010720 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
10721 }
Anjali Singhai Jain9a3bd2f2015-02-24 06:58:44 +000010722 pf->num_lan_qps = max_t(int, pf->rss_size_max,
10723 num_online_cpus());
10724 pf->num_lan_qps = min_t(int, pf->num_lan_qps,
10725 pf->hw.func_caps.num_tx_qp);
10726
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010727 queues_left -= pf->num_lan_qps;
10728 }
10729
10730 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
10731 if (queues_left > 1) {
10732 queues_left -= 1; /* save 1 queue for FD */
10733 } else {
10734 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10735 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
10736 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010737 }
10738
10739 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
10740 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
Anjali Singhai Jaincbf61322014-01-17 15:36:35 -080010741 pf->num_req_vfs = min_t(int, pf->num_req_vfs,
10742 (queues_left / pf->num_vf_qps));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010743 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
10744 }
10745
10746 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
10747 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
10748 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
10749 (queues_left / pf->num_vmdq_qps));
10750 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
10751 }
10752
Anjali Singhai Jainf8ff1462013-11-26 10:49:19 +000010753 pf->queues_left = queues_left;
Neerav Parikh8279e492015-09-03 17:18:50 -040010754 dev_dbg(&pf->pdev->dev,
10755 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
10756 pf->hw.func_caps.num_tx_qp,
10757 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
Helin Zhangacd65442015-10-26 19:44:28 -040010758 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
10759 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
10760 queues_left);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010761}
10762
10763/**
10764 * i40e_setup_pf_filter_control - Setup PF static filter control
10765 * @pf: PF to be setup
10766 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010767 * i40e_setup_pf_filter_control sets up a PF's initial filter control
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010768 * settings. If PE/FCoE are enabled then it will also set the per PF
10769 * based filter sizes required for them. It also enables Flow director,
10770 * ethertype and macvlan type filter settings for the pf.
10771 *
10772 * Returns 0 on success, negative on failure
10773 **/
10774static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
10775{
10776 struct i40e_filter_control_settings *settings = &pf->filter_settings;
10777
10778 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
10779
10780 /* Flow Director is enabled */
Jesse Brandeburg60ea5f82014-01-17 15:36:34 -080010781 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010782 settings->enable_fdir = true;
10783
10784 /* Ethtype and MACVLAN filters enabled for PF */
10785 settings->enable_ethtype = true;
10786 settings->enable_macvlan = true;
10787
10788 if (i40e_set_filter_control(&pf->hw, settings))
10789 return -ENOENT;
10790
10791 return 0;
10792}
10793
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010794#define INFO_STRING_LEN 255
Shannon Nelson7fd89542015-10-21 19:47:04 -040010795#define REMAIN(__x) (INFO_STRING_LEN - (__x))
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010796static void i40e_print_features(struct i40e_pf *pf)
10797{
10798 struct i40e_hw *hw = &pf->hw;
Joe Perches3b195842015-12-03 04:20:57 -080010799 char *buf;
10800 int i;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010801
Joe Perches3b195842015-12-03 04:20:57 -080010802 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
10803 if (!buf)
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010804 return;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010805
Joe Perches3b195842015-12-03 04:20:57 -080010806 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010807#ifdef CONFIG_PCI_IOV
Joe Perches3b195842015-12-03 04:20:57 -080010808 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010809#endif
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010810 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
Shannon Nelson7fd89542015-10-21 19:47:04 -040010811 pf->hw.func_caps.num_vsis,
Jesse Brandeburg1a557afc2016-04-20 19:43:37 -070010812 pf->vsi[pf->lan_vsi]->num_queue_pairs);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010813 if (pf->flags & I40E_FLAG_RSS_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010814 i += snprintf(&buf[i], REMAIN(i), " RSS");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010815 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010816 i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010817 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
Joe Perches3b195842015-12-03 04:20:57 -080010818 i += snprintf(&buf[i], REMAIN(i), " FD_SB");
10819 i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
Akeem G Abodunrinc6423ff2014-05-10 04:49:08 +000010820 }
Neerav Parikh4d9b6042014-05-22 06:31:51 +000010821 if (pf->flags & I40E_FLAG_DCB_CAPABLE)
Joe Perches3b195842015-12-03 04:20:57 -080010822 i += snprintf(&buf[i], REMAIN(i), " DCB");
Joe Perches3b195842015-12-03 04:20:57 -080010823 i += snprintf(&buf[i], REMAIN(i), " VxLAN");
Singhai, Anjali6a899022015-12-14 12:21:18 -080010824 i += snprintf(&buf[i], REMAIN(i), " Geneve");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010825 if (pf->flags & I40E_FLAG_PTP)
Joe Perches3b195842015-12-03 04:20:57 -080010826 i += snprintf(&buf[i], REMAIN(i), " PTP");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010827 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
Joe Perches3b195842015-12-03 04:20:57 -080010828 i += snprintf(&buf[i], REMAIN(i), " VEB");
Shannon Nelson6dec1012015-09-28 14:12:30 -040010829 else
Joe Perches3b195842015-12-03 04:20:57 -080010830 i += snprintf(&buf[i], REMAIN(i), " VEPA");
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010831
Joe Perches3b195842015-12-03 04:20:57 -080010832 dev_info(&pf->pdev->dev, "%s\n", buf);
10833 kfree(buf);
Shannon Nelson7fd89542015-10-21 19:47:04 -040010834 WARN_ON(i > INFO_STRING_LEN);
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000010835}
10836
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010837/**
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010838 * i40e_get_platform_mac_addr - get platform-specific MAC address
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010839 * @pdev: PCI device information struct
10840 * @pf: board private structure
10841 *
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010842 * Look up the MAC address for the device. First we'll try
10843 * eth_platform_get_mac_address, which will check Open Firmware, or arch
10844 * specific fallback. Otherwise, we'll default to the stored value in
10845 * firmware.
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010846 **/
10847static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
10848{
Jacob Keller41c4c2b2017-04-05 07:50:57 -040010849 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
10850 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050010851}
10852
10853/**
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010854 * i40e_probe - Device initialization routine
10855 * @pdev: PCI device information struct
10856 * @ent: entry in i40e_pci_tbl
10857 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000010858 * i40e_probe initializes a PF identified by a pci_dev structure.
10859 * The OS initialization, configuring of the PF private structure,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010860 * and a hardware reset occur.
10861 *
10862 * Returns 0 on success, negative on failure
10863 **/
10864static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
10865{
Catherine Sullivane8278452015-02-06 08:52:08 +000010866 struct i40e_aq_get_phy_abilities_resp abilities;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010867 struct i40e_pf *pf;
10868 struct i40e_hw *hw;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010869 static u16 pfs_found;
Shannon Nelson1d5109d2015-08-26 15:14:08 -040010870 u16 wol_nvm_bits;
Catherine Sullivand4dfb812013-11-28 06:39:21 +000010871 u16 link_status;
Jean Sacren6f66a482015-09-19 05:08:45 -060010872 int err;
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040010873 u32 val;
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000010874 u32 i;
Helin Zhang58fc3262015-10-01 14:37:38 -040010875 u8 set_fc_aq_fail;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010876
10877 err = pci_enable_device_mem(pdev);
10878 if (err)
10879 return err;
10880
10881 /* set up for high or low dma */
Mitch Williams64942942014-02-11 08:26:33 +000010882 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Mitch Williams64942942014-02-11 08:26:33 +000010883 if (err) {
Jean Sacrene3e3bfd2014-03-25 04:30:27 +000010884 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
10885 if (err) {
10886 dev_err(&pdev->dev,
10887 "DMA configuration failed: 0x%x\n", err);
10888 goto err_dma;
10889 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010890 }
10891
10892 /* set up pci connections */
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020010893 err = pci_request_mem_regions(pdev, i40e_driver_name);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010894 if (err) {
10895 dev_info(&pdev->dev,
10896 "pci_request_selected_regions failed %d\n", err);
10897 goto err_pci_reg;
10898 }
10899
10900 pci_enable_pcie_error_reporting(pdev);
10901 pci_set_master(pdev);
10902
10903 /* Now that we have a PCI connection, we need to do the
10904 * low level device setup. This is primarily setting up
10905 * the Admin Queue structures and then querying for the
10906 * device's current profile information.
10907 */
10908 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
10909 if (!pf) {
10910 err = -ENOMEM;
10911 goto err_pf_alloc;
10912 }
10913 pf->next_vsi = 0;
10914 pf->pdev = pdev;
10915 set_bit(__I40E_DOWN, &pf->state);
10916
10917 hw = &pf->hw;
10918 hw->back = pf;
Anjali Singhai232f4702015-02-26 16:15:39 +000010919
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010920 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
10921 I40E_MAX_CSR_SPACE);
Anjali Singhai232f4702015-02-26 16:15:39 +000010922
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010923 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010924 if (!hw->hw_addr) {
10925 err = -EIO;
10926 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
10927 (unsigned int)pci_resource_start(pdev, 0),
Shannon Nelson2ac8b672015-07-23 16:54:37 -040010928 pf->ioremap_len, err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010929 goto err_ioremap;
10930 }
10931 hw->vendor_id = pdev->vendor;
10932 hw->device_id = pdev->device;
10933 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
10934 hw->subsystem_vendor_id = pdev->subsystem_vendor;
10935 hw->subsystem_device_id = pdev->subsystem_device;
10936 hw->bus.device = PCI_SLOT(pdev->devfn);
10937 hw->bus.func = PCI_FUNC(pdev->devfn);
Sudheer Mogilappagarib3f028f2017-02-09 23:58:22 -080010938 hw->bus.bus_id = pdev->bus->number;
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000010939 pf->instance = pfs_found;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010940
Jacob Keller0e588de2017-02-06 14:38:50 -080010941 INIT_LIST_HEAD(&pf->l3_flex_pit_list);
10942 INIT_LIST_HEAD(&pf->l4_flex_pit_list);
10943
Shannon Nelsonde03d2b2016-03-10 14:59:44 -080010944 /* set up the locks for the AQ, do this only once in probe
10945 * and destroy them only once in remove
10946 */
10947 mutex_init(&hw->aq.asq_mutex);
10948 mutex_init(&hw->aq.arq_mutex);
10949
Alexander Duyck5d4ca232016-09-30 08:21:46 -040010950 pf->msg_enable = netif_msg_init(debug,
10951 NETIF_MSG_DRV |
10952 NETIF_MSG_PROBE |
10953 NETIF_MSG_LINK);
10954 if (debug < -1)
10955 pf->hw.debug_mask = debug;
Shannon Nelson5b5faa42014-10-17 03:14:51 +000010956
Jesse Brandeburg7134f9c2013-11-26 08:56:05 +000010957 /* do a special CORER for clearing PXE mode once at init */
10958 if (hw->revision_id == 0 &&
10959 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
10960 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
10961 i40e_flush(hw);
10962 msleep(200);
10963 pf->corer_count++;
10964
10965 i40e_clear_pxe_mode(hw);
10966 }
10967
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010968 /* Reset here to make sure all is clean and to define PF 'n' */
Shannon Nelson838d41d2014-06-04 20:41:27 +000010969 i40e_clear_hw(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010970 err = i40e_pf_reset(hw);
10971 if (err) {
10972 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
10973 goto err_pf_reset;
10974 }
10975 pf->pfr_count++;
10976
10977 hw->aq.num_arq_entries = I40E_AQ_LEN;
10978 hw->aq.num_asq_entries = I40E_AQ_LEN;
10979 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10980 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
10981 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010982
Carolyn Wybornyb294ac72014-12-11 07:06:39 +000010983 snprintf(pf->int_name, sizeof(pf->int_name) - 1,
Carolyn Wybornyb2008cb2014-11-11 20:05:26 +000010984 "%s-%s:misc",
10985 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010986
10987 err = i40e_init_shared_code(hw);
10988 if (err) {
Anjali Singhai Jainb2a75c52015-04-27 14:57:20 -040010989 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
10990 err);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010991 goto err_pf_reset;
10992 }
10993
Jesse Brandeburgd52c20b2013-11-26 10:49:15 +000010994 /* set up a default setting for link flow control */
10995 pf->hw.fc.requested_mode = I40E_FC_NONE;
10996
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000010997 err = i40e_init_adminq(hw);
Carolyn Wyborny2b2426a762015-10-26 19:44:35 -040010998 if (err) {
10999 if (err == I40E_ERR_FIRMWARE_API_VERSION)
11000 dev_info(&pdev->dev,
11001 "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");
11002 else
11003 dev_info(&pdev->dev,
11004 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
11005
11006 goto err_pf_reset;
11007 }
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011008
Shannon Nelson6dec1012015-09-28 14:12:30 -040011009 /* provide nvm, fw, api versions */
11010 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
11011 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
11012 hw->aq.api_maj_ver, hw->aq.api_min_ver,
11013 i40e_nvm_version_str(hw));
Carolyn Wybornyf0b44442015-08-31 19:54:49 -040011014
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011015 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
11016 hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
Shannon Nelson278b6f62014-06-04 01:41:03 +000011017 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011018 "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");
11019 else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
11020 hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
Shannon Nelson278b6f62014-06-04 01:41:03 +000011021 dev_info(&pdev->dev,
Catherine Sullivan7aa67612014-07-09 07:46:17 +000011022 "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 +000011023
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011024 i40e_verify_eeprom(pf);
11025
Jesse Brandeburg2c5fe332014-04-23 04:49:57 +000011026 /* Rev 0 hardware was never productized */
11027 if (hw->revision_id < 1)
11028 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");
11029
Shannon Nelson6ff4ef82013-12-21 05:44:49 +000011030 i40e_clear_pxe_mode(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011031 err = i40e_get_capabilities(pf);
11032 if (err)
11033 goto err_adminq_setup;
11034
11035 err = i40e_sw_init(pf);
11036 if (err) {
11037 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
11038 goto err_sw_init;
11039 }
11040
11041 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
Alexander Duyckc76cb6e2017-02-21 15:55:48 -080011042 hw->func_caps.num_rx_qp, 0, 0);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011043 if (err) {
11044 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
11045 goto err_init_lan_hmc;
11046 }
11047
11048 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
11049 if (err) {
11050 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
11051 err = -ENOENT;
11052 goto err_configure_lan_hmc;
11053 }
11054
Neerav Parikhb686ece2014-12-14 01:55:11 +000011055 /* Disable LLDP for NICs that have firmware versions lower than v4.3.
11056 * Ignore error return codes because if it was already disabled via
11057 * hardware settings this will fail
11058 */
Neerav Parikhf1bbad32016-01-13 16:51:39 -080011059 if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
Neerav Parikhb686ece2014-12-14 01:55:11 +000011060 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
11061 i40e_aq_stop_lldp(hw, true, NULL);
11062 }
11063
Sowmini Varadhanb499ffb2015-12-07 15:06:34 -050011064 /* allow a platform config to override the HW addr */
11065 i40e_get_platform_mac_addr(pdev, pf);
Jacob Keller41c4c2b2017-04-05 07:50:57 -040011066
Jesse Brandeburgf62b5062013-11-28 06:39:27 +000011067 if (!is_valid_ether_addr(hw->mac.addr)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011068 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
11069 err = -EIO;
11070 goto err_mac_addr;
11071 }
11072 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
Greg Rose9a173902014-05-22 06:32:02 +000011073 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
Neerav Parikh1f224ad2014-02-12 01:45:31 +000011074 i40e_get_port_mac_addr(hw, hw->mac.port_addr);
11075 if (is_valid_ether_addr(hw->mac.port_addr))
11076 pf->flags |= I40E_FLAG_PORT_ID_VALID;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011077
11078 pci_set_drvdata(pdev, pf);
11079 pci_save_state(pdev);
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011080#ifdef CONFIG_I40E_DCB
11081 err = i40e_init_pf_dcb(pf);
11082 if (err) {
Shannon Nelsonaebfc812014-12-11 07:06:38 +000011083 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
David Ertmanc17ef432016-09-30 01:36:21 -070011084 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
Neerav Parikh014269f2014-04-01 07:11:48 +000011085 /* Continue without DCB enabled */
Neerav Parikh4e3b35b2014-01-17 15:36:37 -080011086 }
11087#endif /* CONFIG_I40E_DCB */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011088
11089 /* set up periodic task facility */
11090 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
11091 pf->service_timer_period = HZ;
11092
11093 INIT_WORK(&pf->service_task, i40e_service_task);
11094 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011095
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011096 /* NVM bit on means WoL disabled for the port */
11097 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
Jesse Brandeburg75f5cea2015-11-19 11:34:14 -080011098 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
Shannon Nelson1d5109d2015-08-26 15:14:08 -040011099 pf->wol_en = false;
11100 else
11101 pf->wol_en = true;
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011102 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
11103
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011104 /* set up the main switch operations */
11105 i40e_determine_queue_usage(pf);
Jesse Brandeburgc11472802015-04-07 19:45:39 -040011106 err = i40e_init_interrupt_scheme(pf);
11107 if (err)
11108 goto err_switch_setup;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011109
Mitch Williams505682c2014-05-20 08:01:37 +000011110 /* The number of VSIs reported by the FW is the minimum guaranteed
11111 * to us; HW supports far more and we share the remaining pool with
11112 * the other PFs. We allocate space for more than the guarantee with
11113 * the understanding that we might not get them all later.
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011114 */
Mitch Williams505682c2014-05-20 08:01:37 +000011115 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
11116 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
11117 else
11118 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
11119
11120 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
Jesse Brandeburgd17038d2015-12-23 12:05:55 -080011121 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
11122 GFP_KERNEL);
Wei Yongjuned87ac02013-09-24 05:17:25 +000011123 if (!pf->vsi) {
11124 err = -ENOMEM;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011125 goto err_switch_setup;
Wei Yongjuned87ac02013-09-24 05:17:25 +000011126 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011127
Anjali Singhai Jainfa11cb32015-05-27 12:06:14 -040011128#ifdef CONFIG_PCI_IOV
11129 /* prep for VF support */
11130 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
11131 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11132 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
11133 if (pci_num_vf(pdev))
11134 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
11135 }
11136#endif
Anjali Singhai Jainbc7d3382013-11-26 10:49:18 +000011137 err = i40e_setup_pf_switch(pf, false);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011138 if (err) {
11139 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
11140 goto err_vsis;
11141 }
Helin Zhang58fc3262015-10-01 14:37:38 -040011142
11143 /* Make sure flow control is set according to current settings */
11144 err = i40e_set_fc(hw, &set_fc_aq_fail, true);
11145 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
11146 dev_dbg(&pf->pdev->dev,
11147 "Set fc with err %s aq_err %s on get_phy_cap\n",
11148 i40e_stat_str(hw, err),
11149 i40e_aq_str(hw, hw->aq.asq_last_status));
11150 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
11151 dev_dbg(&pf->pdev->dev,
11152 "Set fc with err %s aq_err %s on set_phy_config\n",
11153 i40e_stat_str(hw, err),
11154 i40e_aq_str(hw, hw->aq.asq_last_status));
11155 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
11156 dev_dbg(&pf->pdev->dev,
11157 "Set fc with err %s aq_err %s on get_link_info\n",
11158 i40e_stat_str(hw, err),
11159 i40e_aq_str(hw, hw->aq.asq_last_status));
11160
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011161 /* if FDIR VSI was set up, start it now */
Mitch Williams505682c2014-05-20 08:01:37 +000011162 for (i = 0; i < pf->num_alloc_vsi; i++) {
Shannon Nelson8a9eb7d2014-03-14 07:32:28 +000011163 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
11164 i40e_vsi_open(pf->vsi[i]);
11165 break;
11166 }
11167 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011168
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011169 /* The driver only wants link up/down and module qualification
11170 * reports from firmware. Note the negative logic.
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011171 */
11172 err = i40e_aq_set_phy_int_mask(&pf->hw,
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011173 ~(I40E_AQ_EVENT_LINK_UPDOWN |
Shannon Nelson867a79e2016-03-18 12:18:15 -070011174 I40E_AQ_EVENT_MEDIA_NA |
Shannon Nelson2f0aff42016-01-04 10:33:08 -080011175 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011176 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011177 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
11178 i40e_stat_str(&pf->hw, err),
11179 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Jesse Brandeburg7e2453f2014-09-13 07:40:41 +000011180
Anjali Singhai Jain4f2f017c2015-10-21 19:47:07 -040011181 /* Reconfigure hardware for allowing smaller MSS in the case
11182 * of TSO, so that we avoid the MDD being fired and causing
11183 * a reset in the case of small MSS+TSO.
11184 */
11185 val = rd32(hw, I40E_REG_MSS);
11186 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11187 val &= ~I40E_REG_MSS_MIN_MASK;
11188 val |= I40E_64BYTE_MSS;
11189 wr32(hw, I40E_REG_MSS, val);
11190 }
11191
Anjali Singhai Jain8eed76f2015-12-09 15:50:31 -080011192 if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
Anjali Singhai Jain025b4a52015-02-24 06:58:46 +000011193 msleep(75);
11194 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11195 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040011196 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11197 i40e_stat_str(&pf->hw, err),
11198 i40e_aq_str(&pf->hw,
11199 pf->hw.aq.asq_last_status));
Anjali Singhai Jaincafa2ee2014-09-13 07:40:45 +000011200 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011201 /* The main driver is (mostly) up and happy. We need to set this state
11202 * before setting up the misc vector or we get a race and the vector
11203 * ends up disabled forever.
11204 */
11205 clear_bit(__I40E_DOWN, &pf->state);
11206
11207 /* In case of MSIX we are going to setup the misc vector right here
11208 * to handle admin queue events etc. In case of legacy and MSI
11209 * the misc functionality and queue processing is combined in
11210 * the same vector and that gets setup at open.
11211 */
11212 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11213 err = i40e_setup_misc_vector(pf);
11214 if (err) {
11215 dev_info(&pdev->dev,
11216 "setup of misc vector failed: %d\n", err);
11217 goto err_vsis;
11218 }
11219 }
11220
Greg Rosedf805f62014-04-04 04:43:16 +000011221#ifdef CONFIG_PCI_IOV
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011222 /* prep for VF support */
11223 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
Shannon Nelson4eb3f762014-03-06 08:59:58 +000011224 (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11225 !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011226 /* disable link interrupts for VFs */
11227 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
11228 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
11229 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
11230 i40e_flush(hw);
Mitch Williams4aeec012014-02-13 03:48:47 -080011231
11232 if (pci_num_vf(pdev)) {
11233 dev_info(&pdev->dev,
11234 "Active VFs found, allocating resources.\n");
11235 err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
11236 if (err)
11237 dev_info(&pdev->dev,
11238 "Error %d allocating resources for existing VFs\n",
11239 err);
11240 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011241 }
Greg Rosedf805f62014-04-04 04:43:16 +000011242#endif /* CONFIG_PCI_IOV */
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011243
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011244 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11245 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
11246 pf->num_iwarp_msix,
11247 I40E_IWARP_IRQ_PILE_ID);
11248 if (pf->iwarp_base_vector < 0) {
11249 dev_info(&pdev->dev,
11250 "failed to get tracking for %d vectors for IWARP err=%d\n",
11251 pf->num_iwarp_msix, pf->iwarp_base_vector);
11252 pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11253 }
11254 }
Anjali Singhai Jain93cd7652013-11-20 10:03:01 +000011255
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011256 i40e_dbg_pf_init(pf);
11257
11258 /* tell the firmware that we're starting */
Jesse Brandeburg44033fa2014-04-23 04:50:15 +000011259 i40e_send_version(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011260
11261 /* since everything's happy, start the service_task timer */
11262 mod_timer(&pf->service_timer,
11263 round_jiffies(jiffies + pf->service_timer_period));
11264
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011265 /* add this PF to client device list and launch a client service task */
Mitch Williams004eb612017-04-04 12:40:16 -070011266 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11267 err = i40e_lan_add_device(pf);
11268 if (err)
11269 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
11270 err);
11271 }
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011272
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011273#define PCI_SPEED_SIZE 8
11274#define PCI_WIDTH_SIZE 8
11275 /* Devices on the IOSF bus do not have this information
11276 * and will report PCI Gen 1 x 1 by default so don't bother
11277 * checking them.
11278 */
11279 if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
11280 char speed[PCI_SPEED_SIZE] = "Unknown";
11281 char width[PCI_WIDTH_SIZE] = "Unknown";
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011282
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011283 /* Get the negotiated link width and speed from PCI config
11284 * space
11285 */
11286 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
11287 &link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011288
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011289 i40e_set_pci_config_data(hw, link_status);
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011290
Anjali Singhai Jain3fced532015-09-03 17:18:59 -040011291 switch (hw->bus.speed) {
11292 case i40e_bus_speed_8000:
11293 strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
11294 case i40e_bus_speed_5000:
11295 strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
11296 case i40e_bus_speed_2500:
11297 strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
11298 default:
11299 break;
11300 }
11301 switch (hw->bus.width) {
11302 case i40e_bus_width_pcie_x8:
11303 strncpy(width, "8", PCI_WIDTH_SIZE); break;
11304 case i40e_bus_width_pcie_x4:
11305 strncpy(width, "4", PCI_WIDTH_SIZE); break;
11306 case i40e_bus_width_pcie_x2:
11307 strncpy(width, "2", PCI_WIDTH_SIZE); break;
11308 case i40e_bus_width_pcie_x1:
11309 strncpy(width, "1", PCI_WIDTH_SIZE); break;
11310 default:
11311 break;
11312 }
11313
11314 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
11315 speed, width);
11316
11317 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
11318 hw->bus.speed < i40e_bus_speed_8000) {
11319 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
11320 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
11321 }
Catherine Sullivand4dfb812013-11-28 06:39:21 +000011322 }
11323
Catherine Sullivane8278452015-02-06 08:52:08 +000011324 /* get the requested speeds from the fw */
11325 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
11326 if (err)
Neerav Parikh8279e492015-09-03 17:18:50 -040011327 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
11328 i40e_stat_str(&pf->hw, err),
11329 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivane8278452015-02-06 08:52:08 +000011330 pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
11331
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011332 /* get the supported phy types from the fw */
11333 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
11334 if (err)
11335 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
11336 i40e_stat_str(&pf->hw, err),
11337 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
Catherine Sullivanfc72dbc2015-09-01 11:36:30 -040011338
Anjali Singhai Jaine7358f52015-10-01 14:37:34 -040011339 /* Add a filter to drop all Flow control frames from any VSI from being
11340 * transmitted. By doing so we stop a malicious VF from sending out
11341 * PAUSE or PFC frames and potentially controlling traffic for other
11342 * PF/VF VSIs.
11343 * The FW can still send Flow control frames if enabled.
11344 */
11345 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11346 pf->main_vsi_seid);
11347
Carolyn Wyborny31b606d2016-02-17 16:12:12 -080011348 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
Henry Tieman4f9b4302016-11-08 13:05:18 -080011349 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
11350 pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
Harshitha Ramamurthy4ad9f4f2016-11-08 13:05:09 -080011351 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
11352 pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
Jesse Brandeburg0c22b3d2014-02-11 08:24:14 +000011353 /* print a string summarizing features */
11354 i40e_print_features(pf);
11355
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011356 return 0;
11357
11358 /* Unwind what we've done if something failed in the setup */
11359err_vsis:
11360 set_bit(__I40E_DOWN, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011361 i40e_clear_interrupt_scheme(pf);
11362 kfree(pf->vsi);
Shannon Nelson04b03012013-11-28 06:39:34 +000011363err_switch_setup:
11364 i40e_reset_interrupt_capability(pf);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011365 del_timer_sync(&pf->service_timer);
11366err_mac_addr:
11367err_configure_lan_hmc:
11368 (void)i40e_shutdown_lan_hmc(hw);
11369err_init_lan_hmc:
11370 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011371err_sw_init:
11372err_adminq_setup:
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011373err_pf_reset:
11374 iounmap(hw->hw_addr);
11375err_ioremap:
11376 kfree(pf);
11377err_pf_alloc:
11378 pci_disable_pcie_error_reporting(pdev);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011379 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011380err_pci_reg:
11381err_dma:
11382 pci_disable_device(pdev);
11383 return err;
11384}
11385
11386/**
11387 * i40e_remove - Device removal routine
11388 * @pdev: PCI device information struct
11389 *
11390 * i40e_remove is called by the PCI subsystem to alert the driver
11391 * that is should release a PCI device. This could be caused by a
11392 * Hot-Plug event, or because the driver is going to be removed from
11393 * memory.
11394 **/
11395static void i40e_remove(struct pci_dev *pdev)
11396{
11397 struct i40e_pf *pf = pci_get_drvdata(pdev);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011398 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011399 i40e_status ret_code;
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011400 int i;
11401
11402 i40e_dbg_pf_exit(pf);
11403
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000011404 i40e_ptp_stop(pf);
11405
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011406 /* Disable RSS in hw */
Shannon Nelson272cdaf22016-02-17 16:12:21 -080011407 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
11408 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
Carolyn Wybornybcab2db2015-09-28 14:16:55 -040011409
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011410 /* no more scheduling of any task */
Pandi Kumar Maharajana4618ec2016-02-18 09:19:25 -080011411 set_bit(__I40E_SUSPENDED, &pf->state);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011412 set_bit(__I40E_DOWN, &pf->state);
Shannon Nelsonc99abb42016-03-10 14:59:45 -080011413 if (pf->service_timer.data)
11414 del_timer_sync(&pf->service_timer);
11415 if (pf->service_task.func)
11416 cancel_work_sync(&pf->service_task);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011417
Mitch Williams921c4672017-03-30 00:46:08 -070011418 /* Client close must be called explicitly here because the timer
11419 * has been stopped.
11420 */
11421 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11422
Mitch Williamseb2d80b2014-02-13 03:48:48 -080011423 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
11424 i40e_free_vfs(pf);
11425 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
11426 }
11427
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011428 i40e_fdir_teardown(pf);
11429
11430 /* If there is a switch structure or any orphans, remove them.
11431 * This will leave only the PF's VSI remaining.
11432 */
11433 for (i = 0; i < I40E_MAX_VEB; i++) {
11434 if (!pf->veb[i])
11435 continue;
11436
11437 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
11438 pf->veb[i]->uplink_seid == 0)
11439 i40e_switch_branch_release(pf->veb[i]);
11440 }
11441
11442 /* Now we can shutdown the PF's VSI, just before we kill
11443 * adminq and hmc.
11444 */
11445 if (pf->vsi[pf->lan_vsi])
11446 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
11447
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011448 /* remove attached clients */
Mitch Williams004eb612017-04-04 12:40:16 -070011449 if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11450 ret_code = i40e_lan_del_device(pf);
11451 if (ret_code)
11452 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
11453 ret_code);
Anjali Singhai Jaine3219ce2016-01-20 13:40:01 -060011454 }
11455
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011456 /* shutdown and destroy the HMC */
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011457 if (hw->hmc.hmc_obj) {
11458 ret_code = i40e_shutdown_lan_hmc(hw);
Shannon Nelson60442de2014-04-23 04:50:13 +000011459 if (ret_code)
11460 dev_warn(&pdev->dev,
11461 "Failed to destroy the HMC resources: %d\n",
11462 ret_code);
11463 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011464
11465 /* shutdown the adminq */
Henry Tiemanac9c5c62016-09-06 18:05:11 -070011466 i40e_shutdown_adminq(hw);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011467
Jesse Brandeburg8ddb3322015-11-18 15:47:06 -080011468 /* destroy the locks only once, here */
11469 mutex_destroy(&hw->aq.arq_mutex);
11470 mutex_destroy(&hw->aq.asq_mutex);
11471
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011472 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
11473 i40e_clear_interrupt_scheme(pf);
Mitch Williams505682c2014-05-20 08:01:37 +000011474 for (i = 0; i < pf->num_alloc_vsi; i++) {
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011475 if (pf->vsi[i]) {
11476 i40e_vsi_clear_rings(pf->vsi[i]);
11477 i40e_vsi_clear(pf->vsi[i]);
11478 pf->vsi[i] = NULL;
11479 }
11480 }
11481
11482 for (i = 0; i < I40E_MAX_VEB; i++) {
11483 kfree(pf->veb[i]);
11484 pf->veb[i] = NULL;
11485 }
11486
11487 kfree(pf->qp_pile);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011488 kfree(pf->vsi);
11489
Jesse Brandeburgf734dff2016-01-15 14:33:11 -080011490 iounmap(hw->hw_addr);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011491 kfree(pf);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +020011492 pci_release_mem_regions(pdev);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011493
11494 pci_disable_pcie_error_reporting(pdev);
11495 pci_disable_device(pdev);
11496}
11497
11498/**
11499 * i40e_pci_error_detected - warning that something funky happened in PCI land
11500 * @pdev: PCI device information struct
11501 *
11502 * Called to warn that something happened and the error handling steps
11503 * are in progress. Allows the driver to quiesce things, be ready for
11504 * remediation.
11505 **/
11506static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
11507 enum pci_channel_state error)
11508{
11509 struct i40e_pf *pf = pci_get_drvdata(pdev);
11510
11511 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
11512
Guilherme G Piccoliedfc23ee2016-10-03 00:31:12 -070011513 if (!pf) {
11514 dev_info(&pdev->dev,
11515 "Cannot recover - error happened during device probe\n");
11516 return PCI_ERS_RESULT_DISCONNECT;
11517 }
11518
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011519 /* shutdown all operations */
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011520 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
11521 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011522 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011523 rtnl_unlock();
11524 }
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011525
11526 /* Request a slot reset */
11527 return PCI_ERS_RESULT_NEED_RESET;
11528}
11529
11530/**
11531 * i40e_pci_error_slot_reset - a PCI slot reset just happened
11532 * @pdev: PCI device information struct
11533 *
11534 * Called to find if the driver can work with the device now that
11535 * the pci slot has been reset. If a basic connection seems good
11536 * (registers are readable and have sane content) then return a
11537 * happy little PCI_ERS_RESULT_xxx.
11538 **/
11539static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
11540{
11541 struct i40e_pf *pf = pci_get_drvdata(pdev);
11542 pci_ers_result_t result;
11543 int err;
11544 u32 reg;
11545
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011546 dev_dbg(&pdev->dev, "%s\n", __func__);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011547 if (pci_enable_device_mem(pdev)) {
11548 dev_info(&pdev->dev,
11549 "Cannot re-enable PCI device after reset.\n");
11550 result = PCI_ERS_RESULT_DISCONNECT;
11551 } else {
11552 pci_set_master(pdev);
11553 pci_restore_state(pdev);
11554 pci_save_state(pdev);
11555 pci_wake_from_d3(pdev, false);
11556
11557 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
11558 if (reg == 0)
11559 result = PCI_ERS_RESULT_RECOVERED;
11560 else
11561 result = PCI_ERS_RESULT_DISCONNECT;
11562 }
11563
11564 err = pci_cleanup_aer_uncorrect_error_status(pdev);
11565 if (err) {
11566 dev_info(&pdev->dev,
11567 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
11568 err);
11569 /* non-fatal, continue */
11570 }
11571
11572 return result;
11573}
11574
11575/**
11576 * i40e_pci_error_resume - restart operations after PCI error recovery
11577 * @pdev: PCI device information struct
11578 *
11579 * Called to allow the driver to bring things back up after PCI error
11580 * and/or reset recovery has finished.
11581 **/
11582static void i40e_pci_error_resume(struct pci_dev *pdev)
11583{
11584 struct i40e_pf *pf = pci_get_drvdata(pdev);
11585
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011586 dev_dbg(&pdev->dev, "%s\n", __func__);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011587 if (test_bit(__I40E_SUSPENDED, &pf->state))
11588 return;
11589
11590 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011591 i40e_handle_reset_warning(pf, true);
Vasily Averin4c4935a2015-07-08 15:04:26 +030011592 rtnl_unlock();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011593}
11594
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011595/**
Joshua Hay1d680052016-12-12 15:44:08 -080011596 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
11597 * using the mac_address_write admin q function
11598 * @pf: pointer to i40e_pf struct
11599 **/
11600static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
11601{
11602 struct i40e_hw *hw = &pf->hw;
11603 i40e_status ret;
11604 u8 mac_addr[6];
11605 u16 flags = 0;
11606
11607 /* Get current MAC address in case it's an LAA */
11608 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
11609 ether_addr_copy(mac_addr,
11610 pf->vsi[pf->lan_vsi]->netdev->dev_addr);
11611 } else {
11612 dev_err(&pf->pdev->dev,
11613 "Failed to retrieve MAC address; using default\n");
11614 ether_addr_copy(mac_addr, hw->mac.addr);
11615 }
11616
11617 /* The FW expects the mac address write cmd to first be called with
11618 * one of these flags before calling it again with the multicast
11619 * enable flags.
11620 */
11621 flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
11622
11623 if (hw->func_caps.flex10_enable && hw->partition_id != 1)
11624 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
11625
11626 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11627 if (ret) {
11628 dev_err(&pf->pdev->dev,
11629 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
11630 return;
11631 }
11632
11633 flags = I40E_AQC_MC_MAG_EN
11634 | I40E_AQC_WOL_PRESERVE_ON_PFR
11635 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
11636 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
11637 if (ret)
11638 dev_err(&pf->pdev->dev,
11639 "Failed to enable Multicast Magic Packet wake up\n");
11640}
11641
11642/**
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011643 * i40e_shutdown - PCI callback for shutting down
11644 * @pdev: PCI device information struct
11645 **/
11646static void i40e_shutdown(struct pci_dev *pdev)
11647{
11648 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011649 struct i40e_hw *hw = &pf->hw;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011650
11651 set_bit(__I40E_SUSPENDED, &pf->state);
11652 set_bit(__I40E_DOWN, &pf->state);
11653 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011654 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011655 rtnl_unlock();
11656
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011657 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11658 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11659
Catherine Sullivan02b42492015-07-10 19:35:59 -040011660 del_timer_sync(&pf->service_timer);
11661 cancel_work_sync(&pf->service_task);
11662 i40e_fdir_teardown(pf);
11663
Mitch Williams921c4672017-03-30 00:46:08 -070011664 /* Client close must be called explicitly here because the timer
11665 * has been stopped.
11666 */
11667 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
11668
Joshua Hay1d680052016-12-12 15:44:08 -080011669 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11670 i40e_enable_mc_magic_wake(pf);
11671
Catherine Sullivan02b42492015-07-10 19:35:59 -040011672 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011673 i40e_prep_for_reset(pf, true);
Catherine Sullivan02b42492015-07-10 19:35:59 -040011674 rtnl_unlock();
11675
11676 wr32(hw, I40E_PFPM_APM,
11677 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11678 wr32(hw, I40E_PFPM_WUFC,
11679 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11680
Shannon Nelsone1477582015-02-21 06:44:33 +000011681 i40e_clear_interrupt_scheme(pf);
11682
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011683 if (system_state == SYSTEM_POWER_OFF) {
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011684 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011685 pci_set_power_state(pdev, PCI_D3hot);
11686 }
11687}
11688
11689#ifdef CONFIG_PM
11690/**
11691 * i40e_suspend - PCI callback for moving to D3
11692 * @pdev: PCI device information struct
11693 **/
11694static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
11695{
11696 struct i40e_pf *pf = pci_get_drvdata(pdev);
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011697 struct i40e_hw *hw = &pf->hw;
Greg Rose059ff692016-05-16 10:26:38 -070011698 int retval = 0;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011699
11700 set_bit(__I40E_SUSPENDED, &pf->state);
11701 set_bit(__I40E_DOWN, &pf->state);
Mitch Williams3932dbf2015-03-31 00:45:04 -070011702
Joshua Hay1d680052016-12-12 15:44:08 -080011703 if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
11704 i40e_enable_mc_magic_wake(pf);
11705
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011706 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011707 i40e_prep_for_reset(pf, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011708 rtnl_unlock();
11709
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011710 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
11711 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
11712
Greg Roseb33d3b72016-05-16 10:26:37 -070011713 i40e_stop_misc_vector(pf);
11714
Greg Rose059ff692016-05-16 10:26:38 -070011715 retval = pci_save_state(pdev);
11716 if (retval)
11717 return retval;
11718
Shannon Nelson8e2773a2013-11-28 06:39:22 +000011719 pci_wake_from_d3(pdev, pf->wol_en);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011720 pci_set_power_state(pdev, PCI_D3hot);
11721
Greg Rose059ff692016-05-16 10:26:38 -070011722 return retval;
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011723}
11724
11725/**
11726 * i40e_resume - PCI callback for waking up from D3
11727 * @pdev: PCI device information struct
11728 **/
11729static int i40e_resume(struct pci_dev *pdev)
11730{
11731 struct i40e_pf *pf = pci_get_drvdata(pdev);
11732 u32 err;
11733
11734 pci_set_power_state(pdev, PCI_D0);
11735 pci_restore_state(pdev);
11736 /* pci_restore_state() clears dev->state_saves, so
11737 * call pci_save_state() again to restore it.
11738 */
11739 pci_save_state(pdev);
11740
11741 err = pci_enable_device_mem(pdev);
11742 if (err) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -040011743 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011744 return err;
11745 }
11746 pci_set_master(pdev);
11747
11748 /* no wakeup events while running */
11749 pci_wake_from_d3(pdev, false);
11750
11751 /* handling the reset will rebuild the device state */
11752 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
11753 clear_bit(__I40E_DOWN, &pf->state);
11754 rtnl_lock();
Maciej Sosin373149f2017-04-05 07:50:55 -040011755 i40e_reset_and_rebuild(pf, false, true);
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011756 rtnl_unlock();
11757 }
11758
11759 return 0;
11760}
11761
11762#endif
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011763static const struct pci_error_handlers i40e_err_handler = {
11764 .error_detected = i40e_pci_error_detected,
11765 .slot_reset = i40e_pci_error_slot_reset,
11766 .resume = i40e_pci_error_resume,
11767};
11768
11769static struct pci_driver i40e_driver = {
11770 .name = i40e_driver_name,
11771 .id_table = i40e_pci_tbl,
11772 .probe = i40e_probe,
11773 .remove = i40e_remove,
Shannon Nelson9007bcc2013-11-26 10:49:23 +000011774#ifdef CONFIG_PM
11775 .suspend = i40e_suspend,
11776 .resume = i40e_resume,
11777#endif
11778 .shutdown = i40e_shutdown,
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011779 .err_handler = &i40e_err_handler,
11780 .sriov_configure = i40e_pci_sriov_configure,
11781};
11782
11783/**
11784 * i40e_init_module - Driver registration routine
11785 *
11786 * i40e_init_module is the first routine called when the driver is
11787 * loaded. All it does is register with the PCI subsystem.
11788 **/
11789static int __init i40e_init_module(void)
11790{
11791 pr_info("%s: %s - version %s\n", i40e_driver_name,
11792 i40e_driver_string, i40e_driver_version_str);
11793 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
Greg Rose96664482015-02-06 08:52:13 +000011794
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011795 /* we will see if single thread per module is enough for now,
11796 * it can't be any worse than using the system workqueue which
11797 * was already single threaded
11798 */
Jacob Keller6992a6c2016-08-04 11:37:01 -070011799 i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
11800 i40e_driver_name);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011801 if (!i40e_wq) {
11802 pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
11803 return -ENOMEM;
11804 }
11805
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011806 i40e_dbg_init();
11807 return pci_register_driver(&i40e_driver);
11808}
11809module_init(i40e_init_module);
11810
11811/**
11812 * i40e_exit_module - Driver exit cleanup routine
11813 *
11814 * i40e_exit_module is called just before the driver is removed
11815 * from memory.
11816 **/
11817static void __exit i40e_exit_module(void)
11818{
11819 pci_unregister_driver(&i40e_driver);
Jesse Brandeburg2803b162015-12-22 14:25:08 -080011820 destroy_workqueue(i40e_wq);
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011821 i40e_dbg_exit();
Jesse Brandeburg41c445f2013-09-11 08:39:46 +000011822}
11823module_exit(i40e_exit_module);